ima_crypto.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "ima.h"
  22. static int init_desc(struct hash_desc *desc)
  23. {
  24. int rc;
  25. desc->tfm = crypto_alloc_hash(ima_hash, 0, CRYPTO_ALG_ASYNC);
  26. if (IS_ERR(desc->tfm)) {
  27. pr_info("IMA: failed to load %s transform: %ld\n",
  28. ima_hash, PTR_ERR(desc->tfm));
  29. rc = PTR_ERR(desc->tfm);
  30. return rc;
  31. }
  32. desc->flags = 0;
  33. rc = crypto_hash_init(desc);
  34. if (rc)
  35. crypto_free_hash(desc->tfm);
  36. return rc;
  37. }
  38. /*
  39. * Calculate the MD5/SHA1 file digest
  40. */
  41. int ima_calc_hash(struct file *file, char *digest)
  42. {
  43. struct hash_desc desc;
  44. struct scatterlist sg[1];
  45. loff_t i_size, offset = 0;
  46. char *rbuf;
  47. int rc, read = 0;
  48. rc = init_desc(&desc);
  49. if (rc != 0)
  50. return rc;
  51. rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  52. if (!rbuf) {
  53. rc = -ENOMEM;
  54. goto out;
  55. }
  56. if (!(file->f_mode & FMODE_READ)) {
  57. file->f_mode |= FMODE_READ;
  58. read = 1;
  59. }
  60. i_size = i_size_read(file->f_dentry->d_inode);
  61. while (offset < i_size) {
  62. int rbuf_len;
  63. rbuf_len = kernel_read(file, offset, rbuf, PAGE_SIZE);
  64. if (rbuf_len < 0) {
  65. rc = rbuf_len;
  66. break;
  67. }
  68. if (rbuf_len == 0)
  69. break;
  70. offset += rbuf_len;
  71. sg_init_one(sg, rbuf, rbuf_len);
  72. rc = crypto_hash_update(&desc, sg, rbuf_len);
  73. if (rc)
  74. break;
  75. }
  76. kfree(rbuf);
  77. if (!rc)
  78. rc = crypto_hash_final(&desc, digest);
  79. if (read)
  80. file->f_mode &= ~FMODE_READ;
  81. out:
  82. crypto_free_hash(desc.tfm);
  83. return rc;
  84. }
  85. /*
  86. * Calculate the hash of a given template
  87. */
  88. int ima_calc_template_hash(int template_len, void *template, char *digest)
  89. {
  90. struct hash_desc desc;
  91. struct scatterlist sg[1];
  92. int rc;
  93. rc = init_desc(&desc);
  94. if (rc != 0)
  95. return rc;
  96. sg_init_one(sg, template, template_len);
  97. rc = crypto_hash_update(&desc, sg, template_len);
  98. if (!rc)
  99. rc = crypto_hash_final(&desc, digest);
  100. crypto_free_hash(desc.tfm);
  101. return rc;
  102. }
  103. static void __init ima_pcrread(int idx, u8 *pcr)
  104. {
  105. if (!ima_used_chip)
  106. return;
  107. if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0)
  108. pr_err("IMA: Error Communicating to TPM chip\n");
  109. }
  110. /*
  111. * Calculate the boot aggregate hash
  112. */
  113. int __init ima_calc_boot_aggregate(char *digest)
  114. {
  115. struct hash_desc desc;
  116. struct scatterlist sg;
  117. u8 pcr_i[IMA_DIGEST_SIZE];
  118. int rc, i;
  119. rc = init_desc(&desc);
  120. if (rc != 0)
  121. return rc;
  122. /* cumulative sha1 over tpm registers 0-7 */
  123. for (i = TPM_PCR0; i < TPM_PCR8; i++) {
  124. ima_pcrread(i, pcr_i);
  125. /* now accumulate with current aggregate */
  126. sg_init_one(&sg, pcr_i, IMA_DIGEST_SIZE);
  127. rc = crypto_hash_update(&desc, &sg, IMA_DIGEST_SIZE);
  128. }
  129. if (!rc)
  130. crypto_hash_final(&desc, digest);
  131. crypto_free_hash(desc.tfm);
  132. return rc;
  133. }