ima_crypto.c 2.9 KB

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