ima_crypto.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2005,2006,2007,2008 IBM Corporation
  3. *
  4. * Authors:
  5. * Mimi Zohar <zohar@us.ibm.com>
  6. * Kylene Hall <kjhall@us.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, version 2 of the License.
  11. *
  12. * File: ima_crypto.c
  13. * Calculates md5/sha1 file hash, template hash, boot-aggreate hash
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/file.h>
  17. #include <linux/crypto.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/err.h>
  20. #include <linux/slab.h>
  21. #include <crypto/hash.h>
  22. #include "ima.h"
  23. static struct crypto_shash *ima_shash_tfm;
  24. int ima_init_crypto(void)
  25. {
  26. long rc;
  27. ima_shash_tfm = crypto_alloc_shash(ima_hash, 0, 0);
  28. if (IS_ERR(ima_shash_tfm)) {
  29. rc = PTR_ERR(ima_shash_tfm);
  30. pr_err("Can not allocate %s (reason: %ld)\n", ima_hash, rc);
  31. return rc;
  32. }
  33. return 0;
  34. }
  35. /*
  36. * Calculate the MD5/SHA1 file digest
  37. */
  38. int ima_calc_file_hash(struct file *file, char *digest)
  39. {
  40. loff_t i_size, offset = 0;
  41. char *rbuf;
  42. int rc, read = 0;
  43. struct {
  44. struct shash_desc shash;
  45. char ctx[crypto_shash_descsize(ima_shash_tfm)];
  46. } desc;
  47. desc.shash.tfm = ima_shash_tfm;
  48. desc.shash.flags = 0;
  49. rc = crypto_shash_init(&desc.shash);
  50. if (rc != 0)
  51. return rc;
  52. rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  53. if (!rbuf) {
  54. rc = -ENOMEM;
  55. goto out;
  56. }
  57. if (!(file->f_mode & FMODE_READ)) {
  58. file->f_mode |= FMODE_READ;
  59. read = 1;
  60. }
  61. i_size = i_size_read(file_inode(file));
  62. while (offset < i_size) {
  63. int rbuf_len;
  64. rbuf_len = kernel_read(file, offset, rbuf, PAGE_SIZE);
  65. if (rbuf_len < 0) {
  66. rc = rbuf_len;
  67. break;
  68. }
  69. if (rbuf_len == 0)
  70. break;
  71. offset += rbuf_len;
  72. rc = crypto_shash_update(&desc.shash, rbuf, rbuf_len);
  73. if (rc)
  74. break;
  75. }
  76. kfree(rbuf);
  77. if (!rc)
  78. rc = crypto_shash_final(&desc.shash, digest);
  79. if (read)
  80. file->f_mode &= ~FMODE_READ;
  81. out:
  82. return rc;
  83. }
  84. /*
  85. * Calculate the hash of a given buffer
  86. */
  87. int ima_calc_buffer_hash(const void *data, int len, char *digest)
  88. {
  89. struct {
  90. struct shash_desc shash;
  91. char ctx[crypto_shash_descsize(ima_shash_tfm)];
  92. } desc;
  93. desc.shash.tfm = ima_shash_tfm;
  94. desc.shash.flags = 0;
  95. return crypto_shash_digest(&desc.shash, data, len, digest);
  96. }
  97. static void __init ima_pcrread(int idx, u8 *pcr)
  98. {
  99. if (!ima_used_chip)
  100. return;
  101. if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0)
  102. pr_err("IMA: Error Communicating to TPM chip\n");
  103. }
  104. /*
  105. * Calculate the boot aggregate hash
  106. */
  107. int __init ima_calc_boot_aggregate(char *digest)
  108. {
  109. u8 pcr_i[IMA_DIGEST_SIZE];
  110. int rc, i;
  111. struct {
  112. struct shash_desc shash;
  113. char ctx[crypto_shash_descsize(ima_shash_tfm)];
  114. } desc;
  115. desc.shash.tfm = ima_shash_tfm;
  116. desc.shash.flags = 0;
  117. rc = crypto_shash_init(&desc.shash);
  118. if (rc != 0)
  119. return rc;
  120. /* cumulative sha1 over tpm registers 0-7 */
  121. for (i = TPM_PCR0; i < TPM_PCR8; i++) {
  122. ima_pcrread(i, pcr_i);
  123. /* now accumulate with current aggregate */
  124. rc = crypto_shash_update(&desc.shash, pcr_i, IMA_DIGEST_SIZE);
  125. }
  126. if (!rc)
  127. crypto_shash_final(&desc.shash, digest);
  128. return rc;
  129. }