ima_crypto.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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;
  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. i_size = i_size_read(file->f_dentry->d_inode);
  57. while (offset < i_size) {
  58. int rbuf_len;
  59. rbuf_len = kernel_read(file, offset, rbuf, PAGE_SIZE);
  60. if (rbuf_len < 0) {
  61. rc = rbuf_len;
  62. break;
  63. }
  64. if (rbuf_len == 0)
  65. break;
  66. offset += rbuf_len;
  67. sg_init_one(sg, rbuf, rbuf_len);
  68. rc = crypto_hash_update(&desc, sg, rbuf_len);
  69. if (rc)
  70. break;
  71. }
  72. kfree(rbuf);
  73. if (!rc)
  74. rc = crypto_hash_final(&desc, digest);
  75. out:
  76. crypto_free_hash(desc.tfm);
  77. return rc;
  78. }
  79. /*
  80. * Calculate the hash of a given template
  81. */
  82. int ima_calc_template_hash(int template_len, void *template, char *digest)
  83. {
  84. struct hash_desc desc;
  85. struct scatterlist sg[1];
  86. int rc;
  87. rc = init_desc(&desc);
  88. if (rc != 0)
  89. return rc;
  90. sg_init_one(sg, template, template_len);
  91. rc = crypto_hash_update(&desc, sg, template_len);
  92. if (!rc)
  93. rc = crypto_hash_final(&desc, digest);
  94. crypto_free_hash(desc.tfm);
  95. return rc;
  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. struct hash_desc desc;
  110. struct scatterlist sg;
  111. u8 pcr_i[IMA_DIGEST_SIZE];
  112. int rc, i;
  113. rc = init_desc(&desc);
  114. if (rc != 0)
  115. return rc;
  116. /* cumulative sha1 over tpm registers 0-7 */
  117. for (i = TPM_PCR0; i < TPM_PCR8; i++) {
  118. ima_pcrread(i, pcr_i);
  119. /* now accumulate with current aggregate */
  120. sg_init_one(&sg, pcr_i, IMA_DIGEST_SIZE);
  121. rc = crypto_hash_update(&desc, &sg, IMA_DIGEST_SIZE);
  122. }
  123. if (!rc)
  124. crypto_hash_final(&desc, digest);
  125. crypto_free_hash(desc.tfm);
  126. return rc;
  127. }