ima_crypto.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <crypto/hash_info.h>
  23. #include "ima.h"
  24. static struct crypto_shash *ima_shash_tfm;
  25. int ima_init_crypto(void)
  26. {
  27. long rc;
  28. ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0);
  29. if (IS_ERR(ima_shash_tfm)) {
  30. rc = PTR_ERR(ima_shash_tfm);
  31. pr_err("Can not allocate %s (reason: %ld)\n",
  32. hash_algo_name[ima_hash_algo], rc);
  33. return rc;
  34. }
  35. return 0;
  36. }
  37. static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
  38. {
  39. struct crypto_shash *tfm = ima_shash_tfm;
  40. int rc;
  41. if (algo != ima_hash_algo && algo < HASH_ALGO__LAST) {
  42. tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
  43. if (IS_ERR(tfm)) {
  44. rc = PTR_ERR(tfm);
  45. pr_err("Can not allocate %s (reason: %d)\n",
  46. hash_algo_name[algo], rc);
  47. }
  48. }
  49. return tfm;
  50. }
  51. static void ima_free_tfm(struct crypto_shash *tfm)
  52. {
  53. if (tfm != ima_shash_tfm)
  54. crypto_free_shash(tfm);
  55. }
  56. /*
  57. * Calculate the MD5/SHA1 file digest
  58. */
  59. static int ima_calc_file_hash_tfm(struct file *file,
  60. struct ima_digest_data *hash,
  61. struct crypto_shash *tfm)
  62. {
  63. loff_t i_size, offset = 0;
  64. char *rbuf;
  65. int rc, read = 0;
  66. struct {
  67. struct shash_desc shash;
  68. char ctx[crypto_shash_descsize(tfm)];
  69. } desc;
  70. desc.shash.tfm = tfm;
  71. desc.shash.flags = 0;
  72. hash->length = crypto_shash_digestsize(tfm);
  73. rc = crypto_shash_init(&desc.shash);
  74. if (rc != 0)
  75. return rc;
  76. rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  77. if (!rbuf) {
  78. rc = -ENOMEM;
  79. goto out;
  80. }
  81. if (!(file->f_mode & FMODE_READ)) {
  82. file->f_mode |= FMODE_READ;
  83. read = 1;
  84. }
  85. i_size = i_size_read(file_inode(file));
  86. while (offset < i_size) {
  87. int rbuf_len;
  88. rbuf_len = kernel_read(file, offset, rbuf, PAGE_SIZE);
  89. if (rbuf_len < 0) {
  90. rc = rbuf_len;
  91. break;
  92. }
  93. if (rbuf_len == 0)
  94. break;
  95. offset += rbuf_len;
  96. rc = crypto_shash_update(&desc.shash, rbuf, rbuf_len);
  97. if (rc)
  98. break;
  99. }
  100. kfree(rbuf);
  101. if (!rc)
  102. rc = crypto_shash_final(&desc.shash, hash->digest);
  103. if (read)
  104. file->f_mode &= ~FMODE_READ;
  105. out:
  106. return rc;
  107. }
  108. int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
  109. {
  110. struct crypto_shash *tfm;
  111. int rc;
  112. tfm = ima_alloc_tfm(hash->algo);
  113. if (IS_ERR(tfm))
  114. return PTR_ERR(tfm);
  115. rc = ima_calc_file_hash_tfm(file, hash, tfm);
  116. ima_free_tfm(tfm);
  117. return rc;
  118. }
  119. /*
  120. * Calculate the hash of a given buffer
  121. */
  122. static int ima_calc_buffer_hash_tfm(const void *buf, int len,
  123. struct ima_digest_data *hash,
  124. struct crypto_shash *tfm)
  125. {
  126. struct {
  127. struct shash_desc shash;
  128. char ctx[crypto_shash_descsize(tfm)];
  129. } desc;
  130. desc.shash.tfm = tfm;
  131. desc.shash.flags = 0;
  132. hash->length = crypto_shash_digestsize(tfm);
  133. return crypto_shash_digest(&desc.shash, buf, len, hash->digest);
  134. }
  135. int ima_calc_buffer_hash(const void *buf, int len, struct ima_digest_data *hash)
  136. {
  137. struct crypto_shash *tfm;
  138. int rc;
  139. tfm = ima_alloc_tfm(hash->algo);
  140. if (IS_ERR(tfm))
  141. return PTR_ERR(tfm);
  142. rc = ima_calc_buffer_hash_tfm(buf, len, hash, tfm);
  143. ima_free_tfm(tfm);
  144. return rc;
  145. }
  146. static void __init ima_pcrread(int idx, u8 *pcr)
  147. {
  148. if (!ima_used_chip)
  149. return;
  150. if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0)
  151. pr_err("IMA: Error Communicating to TPM chip\n");
  152. }
  153. /*
  154. * Calculate the boot aggregate hash
  155. */
  156. int __init ima_calc_boot_aggregate(char *digest)
  157. {
  158. u8 pcr_i[TPM_DIGEST_SIZE];
  159. int rc, i;
  160. struct {
  161. struct shash_desc shash;
  162. char ctx[crypto_shash_descsize(ima_shash_tfm)];
  163. } desc;
  164. desc.shash.tfm = ima_shash_tfm;
  165. desc.shash.flags = 0;
  166. rc = crypto_shash_init(&desc.shash);
  167. if (rc != 0)
  168. return rc;
  169. /* cumulative sha1 over tpm registers 0-7 */
  170. for (i = TPM_PCR0; i < TPM_PCR8; i++) {
  171. ima_pcrread(i, pcr_i);
  172. /* now accumulate with current aggregate */
  173. rc = crypto_shash_update(&desc.shash, pcr_i, TPM_DIGEST_SIZE);
  174. }
  175. if (!rc)
  176. crypto_shash_final(&desc.shash, digest);
  177. return rc;
  178. }