ima_crypto.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/kernel.h>
  17. #include <linux/file.h>
  18. #include <linux/crypto.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/err.h>
  21. #include <linux/slab.h>
  22. #include <crypto/hash.h>
  23. #include <crypto/hash_info.h>
  24. #include "ima.h"
  25. static struct crypto_shash *ima_shash_tfm;
  26. int ima_init_crypto(void)
  27. {
  28. long rc;
  29. ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0);
  30. if (IS_ERR(ima_shash_tfm)) {
  31. rc = PTR_ERR(ima_shash_tfm);
  32. pr_err("Can not allocate %s (reason: %ld)\n",
  33. hash_algo_name[ima_hash_algo], rc);
  34. return rc;
  35. }
  36. return 0;
  37. }
  38. static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
  39. {
  40. struct crypto_shash *tfm = ima_shash_tfm;
  41. int rc;
  42. if (algo != ima_hash_algo && algo < HASH_ALGO__LAST) {
  43. tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
  44. if (IS_ERR(tfm)) {
  45. rc = PTR_ERR(tfm);
  46. pr_err("Can not allocate %s (reason: %d)\n",
  47. hash_algo_name[algo], rc);
  48. }
  49. }
  50. return tfm;
  51. }
  52. static void ima_free_tfm(struct crypto_shash *tfm)
  53. {
  54. if (tfm != ima_shash_tfm)
  55. crypto_free_shash(tfm);
  56. }
  57. /*
  58. * Calculate the MD5/SHA1 file digest
  59. */
  60. static int ima_calc_file_hash_tfm(struct file *file,
  61. struct ima_digest_data *hash,
  62. struct crypto_shash *tfm)
  63. {
  64. loff_t i_size, offset = 0;
  65. char *rbuf;
  66. int rc, read = 0;
  67. struct {
  68. struct shash_desc shash;
  69. char ctx[crypto_shash_descsize(tfm)];
  70. } desc;
  71. desc.shash.tfm = tfm;
  72. desc.shash.flags = 0;
  73. hash->length = crypto_shash_digestsize(tfm);
  74. rc = crypto_shash_init(&desc.shash);
  75. if (rc != 0)
  76. return rc;
  77. i_size = i_size_read(file_inode(file));
  78. if (i_size == 0)
  79. goto out;
  80. rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  81. if (!rbuf)
  82. return -ENOMEM;
  83. if (!(file->f_mode & FMODE_READ)) {
  84. file->f_mode |= FMODE_READ;
  85. read = 1;
  86. }
  87. while (offset < i_size) {
  88. int rbuf_len;
  89. rbuf_len = kernel_read(file, offset, rbuf, PAGE_SIZE);
  90. if (rbuf_len < 0) {
  91. rc = rbuf_len;
  92. break;
  93. }
  94. if (rbuf_len == 0)
  95. break;
  96. offset += rbuf_len;
  97. rc = crypto_shash_update(&desc.shash, rbuf, rbuf_len);
  98. if (rc)
  99. break;
  100. }
  101. if (read)
  102. file->f_mode &= ~FMODE_READ;
  103. kfree(rbuf);
  104. out:
  105. if (!rc)
  106. rc = crypto_shash_final(&desc.shash, hash->digest);
  107. return rc;
  108. }
  109. int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
  110. {
  111. struct crypto_shash *tfm;
  112. int rc;
  113. tfm = ima_alloc_tfm(hash->algo);
  114. if (IS_ERR(tfm))
  115. return PTR_ERR(tfm);
  116. rc = ima_calc_file_hash_tfm(file, hash, tfm);
  117. ima_free_tfm(tfm);
  118. return rc;
  119. }
  120. /*
  121. * Calculate the hash of template data
  122. */
  123. static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
  124. struct ima_template_desc *td,
  125. int num_fields,
  126. struct ima_digest_data *hash,
  127. struct crypto_shash *tfm)
  128. {
  129. struct {
  130. struct shash_desc shash;
  131. char ctx[crypto_shash_descsize(tfm)];
  132. } desc;
  133. int rc, i;
  134. desc.shash.tfm = tfm;
  135. desc.shash.flags = 0;
  136. hash->length = crypto_shash_digestsize(tfm);
  137. rc = crypto_shash_init(&desc.shash);
  138. if (rc != 0)
  139. return rc;
  140. for (i = 0; i < num_fields; i++) {
  141. u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 };
  142. u8 *data_to_hash = field_data[i].data;
  143. u32 datalen = field_data[i].len;
  144. if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) {
  145. rc = crypto_shash_update(&desc.shash,
  146. (const u8 *) &field_data[i].len,
  147. sizeof(field_data[i].len));
  148. if (rc)
  149. break;
  150. } else if (strcmp(td->fields[i]->field_id, "n") == 0) {
  151. memcpy(buffer, data_to_hash, datalen);
  152. data_to_hash = buffer;
  153. datalen = IMA_EVENT_NAME_LEN_MAX + 1;
  154. }
  155. rc = crypto_shash_update(&desc.shash, data_to_hash, datalen);
  156. if (rc)
  157. break;
  158. }
  159. if (!rc)
  160. rc = crypto_shash_final(&desc.shash, hash->digest);
  161. return rc;
  162. }
  163. int ima_calc_field_array_hash(struct ima_field_data *field_data,
  164. struct ima_template_desc *desc, int num_fields,
  165. struct ima_digest_data *hash)
  166. {
  167. struct crypto_shash *tfm;
  168. int rc;
  169. tfm = ima_alloc_tfm(hash->algo);
  170. if (IS_ERR(tfm))
  171. return PTR_ERR(tfm);
  172. rc = ima_calc_field_array_hash_tfm(field_data, desc, num_fields,
  173. hash, tfm);
  174. ima_free_tfm(tfm);
  175. return rc;
  176. }
  177. static void __init ima_pcrread(int idx, u8 *pcr)
  178. {
  179. if (!ima_used_chip)
  180. return;
  181. if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0)
  182. pr_err("Error Communicating to TPM chip\n");
  183. }
  184. /*
  185. * Calculate the boot aggregate hash
  186. */
  187. static int __init ima_calc_boot_aggregate_tfm(char *digest,
  188. struct crypto_shash *tfm)
  189. {
  190. u8 pcr_i[TPM_DIGEST_SIZE];
  191. int rc, i;
  192. struct {
  193. struct shash_desc shash;
  194. char ctx[crypto_shash_descsize(tfm)];
  195. } desc;
  196. desc.shash.tfm = tfm;
  197. desc.shash.flags = 0;
  198. rc = crypto_shash_init(&desc.shash);
  199. if (rc != 0)
  200. return rc;
  201. /* cumulative sha1 over tpm registers 0-7 */
  202. for (i = TPM_PCR0; i < TPM_PCR8; i++) {
  203. ima_pcrread(i, pcr_i);
  204. /* now accumulate with current aggregate */
  205. rc = crypto_shash_update(&desc.shash, pcr_i, TPM_DIGEST_SIZE);
  206. }
  207. if (!rc)
  208. crypto_shash_final(&desc.shash, digest);
  209. return rc;
  210. }
  211. int __init ima_calc_boot_aggregate(struct ima_digest_data *hash)
  212. {
  213. struct crypto_shash *tfm;
  214. int rc;
  215. tfm = ima_alloc_tfm(hash->algo);
  216. if (IS_ERR(tfm))
  217. return PTR_ERR(tfm);
  218. hash->length = crypto_shash_digestsize(tfm);
  219. rc = ima_calc_boot_aggregate_tfm(hash->digest, tfm);
  220. ima_free_tfm(tfm);
  221. return rc;
  222. }