ima_crypto.c 3.9 KB

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