ima_crypto.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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/moduleparam.h>
  18. #include <linux/ratelimit.h>
  19. #include <linux/file.h>
  20. #include <linux/crypto.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/err.h>
  23. #include <linux/slab.h>
  24. #include <crypto/hash.h>
  25. #include "ima.h"
  26. /* minimum file size for ahash use */
  27. static unsigned long ima_ahash_minsize;
  28. module_param_named(ahash_minsize, ima_ahash_minsize, ulong, 0644);
  29. MODULE_PARM_DESC(ahash_minsize, "Minimum file size for ahash use");
  30. /* default is 0 - 1 page. */
  31. static int ima_maxorder;
  32. static unsigned int ima_bufsize = PAGE_SIZE;
  33. static int param_set_bufsize(const char *val, const struct kernel_param *kp)
  34. {
  35. unsigned long long size;
  36. int order;
  37. size = memparse(val, NULL);
  38. order = get_order(size);
  39. if (order >= MAX_ORDER)
  40. return -EINVAL;
  41. ima_maxorder = order;
  42. ima_bufsize = PAGE_SIZE << order;
  43. return 0;
  44. }
  45. static const struct kernel_param_ops param_ops_bufsize = {
  46. .set = param_set_bufsize,
  47. .get = param_get_uint,
  48. };
  49. #define param_check_bufsize(name, p) __param_check(name, p, unsigned int)
  50. module_param_named(ahash_bufsize, ima_bufsize, bufsize, 0644);
  51. MODULE_PARM_DESC(ahash_bufsize, "Maximum ahash buffer size");
  52. static struct crypto_shash *ima_shash_tfm;
  53. static struct crypto_ahash *ima_ahash_tfm;
  54. int __init ima_init_crypto(void)
  55. {
  56. long rc;
  57. ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0);
  58. if (IS_ERR(ima_shash_tfm)) {
  59. rc = PTR_ERR(ima_shash_tfm);
  60. pr_err("Can not allocate %s (reason: %ld)\n",
  61. hash_algo_name[ima_hash_algo], rc);
  62. return rc;
  63. }
  64. pr_info("Allocated hash algorithm: %s\n",
  65. hash_algo_name[ima_hash_algo]);
  66. return 0;
  67. }
  68. static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
  69. {
  70. struct crypto_shash *tfm = ima_shash_tfm;
  71. int rc;
  72. if (algo < 0 || algo >= HASH_ALGO__LAST)
  73. algo = ima_hash_algo;
  74. if (algo != ima_hash_algo) {
  75. tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
  76. if (IS_ERR(tfm)) {
  77. rc = PTR_ERR(tfm);
  78. pr_err("Can not allocate %s (reason: %d)\n",
  79. hash_algo_name[algo], rc);
  80. }
  81. }
  82. return tfm;
  83. }
  84. static void ima_free_tfm(struct crypto_shash *tfm)
  85. {
  86. if (tfm != ima_shash_tfm)
  87. crypto_free_shash(tfm);
  88. }
  89. /**
  90. * ima_alloc_pages() - Allocate contiguous pages.
  91. * @max_size: Maximum amount of memory to allocate.
  92. * @allocated_size: Returned size of actual allocation.
  93. * @last_warn: Should the min_size allocation warn or not.
  94. *
  95. * Tries to do opportunistic allocation for memory first trying to allocate
  96. * max_size amount of memory and then splitting that until zero order is
  97. * reached. Allocation is tried without generating allocation warnings unless
  98. * last_warn is set. Last_warn set affects only last allocation of zero order.
  99. *
  100. * By default, ima_maxorder is 0 and it is equivalent to kmalloc(GFP_KERNEL)
  101. *
  102. * Return pointer to allocated memory, or NULL on failure.
  103. */
  104. static void *ima_alloc_pages(loff_t max_size, size_t *allocated_size,
  105. int last_warn)
  106. {
  107. void *ptr;
  108. int order = ima_maxorder;
  109. gfp_t gfp_mask = __GFP_RECLAIM | __GFP_NOWARN | __GFP_NORETRY;
  110. if (order)
  111. order = min(get_order(max_size), order);
  112. for (; order; order--) {
  113. ptr = (void *)__get_free_pages(gfp_mask, order);
  114. if (ptr) {
  115. *allocated_size = PAGE_SIZE << order;
  116. return ptr;
  117. }
  118. }
  119. /* order is zero - one page */
  120. gfp_mask = GFP_KERNEL;
  121. if (!last_warn)
  122. gfp_mask |= __GFP_NOWARN;
  123. ptr = (void *)__get_free_pages(gfp_mask, 0);
  124. if (ptr) {
  125. *allocated_size = PAGE_SIZE;
  126. return ptr;
  127. }
  128. *allocated_size = 0;
  129. return NULL;
  130. }
  131. /**
  132. * ima_free_pages() - Free pages allocated by ima_alloc_pages().
  133. * @ptr: Pointer to allocated pages.
  134. * @size: Size of allocated buffer.
  135. */
  136. static void ima_free_pages(void *ptr, size_t size)
  137. {
  138. if (!ptr)
  139. return;
  140. free_pages((unsigned long)ptr, get_order(size));
  141. }
  142. static struct crypto_ahash *ima_alloc_atfm(enum hash_algo algo)
  143. {
  144. struct crypto_ahash *tfm = ima_ahash_tfm;
  145. int rc;
  146. if (algo < 0 || algo >= HASH_ALGO__LAST)
  147. algo = ima_hash_algo;
  148. if (algo != ima_hash_algo || !tfm) {
  149. tfm = crypto_alloc_ahash(hash_algo_name[algo], 0, 0);
  150. if (!IS_ERR(tfm)) {
  151. if (algo == ima_hash_algo)
  152. ima_ahash_tfm = tfm;
  153. } else {
  154. rc = PTR_ERR(tfm);
  155. pr_err("Can not allocate %s (reason: %d)\n",
  156. hash_algo_name[algo], rc);
  157. }
  158. }
  159. return tfm;
  160. }
  161. static void ima_free_atfm(struct crypto_ahash *tfm)
  162. {
  163. if (tfm != ima_ahash_tfm)
  164. crypto_free_ahash(tfm);
  165. }
  166. static inline int ahash_wait(int err, struct crypto_wait *wait)
  167. {
  168. err = crypto_wait_req(err, wait);
  169. if (err)
  170. pr_crit_ratelimited("ahash calculation failed: err: %d\n", err);
  171. return err;
  172. }
  173. static int ima_calc_file_hash_atfm(struct file *file,
  174. struct ima_digest_data *hash,
  175. struct crypto_ahash *tfm)
  176. {
  177. loff_t i_size, offset;
  178. char *rbuf[2] = { NULL, };
  179. int rc, rbuf_len, active = 0, ahash_rc = 0;
  180. struct ahash_request *req;
  181. struct scatterlist sg[1];
  182. struct crypto_wait wait;
  183. size_t rbuf_size[2];
  184. hash->length = crypto_ahash_digestsize(tfm);
  185. req = ahash_request_alloc(tfm, GFP_KERNEL);
  186. if (!req)
  187. return -ENOMEM;
  188. crypto_init_wait(&wait);
  189. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  190. CRYPTO_TFM_REQ_MAY_SLEEP,
  191. crypto_req_done, &wait);
  192. rc = ahash_wait(crypto_ahash_init(req), &wait);
  193. if (rc)
  194. goto out1;
  195. i_size = i_size_read(file_inode(file));
  196. if (i_size == 0)
  197. goto out2;
  198. /*
  199. * Try to allocate maximum size of memory.
  200. * Fail if even a single page cannot be allocated.
  201. */
  202. rbuf[0] = ima_alloc_pages(i_size, &rbuf_size[0], 1);
  203. if (!rbuf[0]) {
  204. rc = -ENOMEM;
  205. goto out1;
  206. }
  207. /* Only allocate one buffer if that is enough. */
  208. if (i_size > rbuf_size[0]) {
  209. /*
  210. * Try to allocate secondary buffer. If that fails fallback to
  211. * using single buffering. Use previous memory allocation size
  212. * as baseline for possible allocation size.
  213. */
  214. rbuf[1] = ima_alloc_pages(i_size - rbuf_size[0],
  215. &rbuf_size[1], 0);
  216. }
  217. for (offset = 0; offset < i_size; offset += rbuf_len) {
  218. if (!rbuf[1] && offset) {
  219. /* Not using two buffers, and it is not the first
  220. * read/request, wait for the completion of the
  221. * previous ahash_update() request.
  222. */
  223. rc = ahash_wait(ahash_rc, &wait);
  224. if (rc)
  225. goto out3;
  226. }
  227. /* read buffer */
  228. rbuf_len = min_t(loff_t, i_size - offset, rbuf_size[active]);
  229. rc = integrity_kernel_read(file, offset, rbuf[active],
  230. rbuf_len);
  231. if (rc != rbuf_len) {
  232. if (rc >= 0)
  233. rc = -EINVAL;
  234. /*
  235. * Forward current rc, do not overwrite with return value
  236. * from ahash_wait()
  237. */
  238. ahash_wait(ahash_rc, &wait);
  239. goto out3;
  240. }
  241. if (rbuf[1] && offset) {
  242. /* Using two buffers, and it is not the first
  243. * read/request, wait for the completion of the
  244. * previous ahash_update() request.
  245. */
  246. rc = ahash_wait(ahash_rc, &wait);
  247. if (rc)
  248. goto out3;
  249. }
  250. sg_init_one(&sg[0], rbuf[active], rbuf_len);
  251. ahash_request_set_crypt(req, sg, NULL, rbuf_len);
  252. ahash_rc = crypto_ahash_update(req);
  253. if (rbuf[1])
  254. active = !active; /* swap buffers, if we use two */
  255. }
  256. /* wait for the last update request to complete */
  257. rc = ahash_wait(ahash_rc, &wait);
  258. out3:
  259. ima_free_pages(rbuf[0], rbuf_size[0]);
  260. ima_free_pages(rbuf[1], rbuf_size[1]);
  261. out2:
  262. if (!rc) {
  263. ahash_request_set_crypt(req, NULL, hash->digest, 0);
  264. rc = ahash_wait(crypto_ahash_final(req), &wait);
  265. }
  266. out1:
  267. ahash_request_free(req);
  268. return rc;
  269. }
  270. static int ima_calc_file_ahash(struct file *file, struct ima_digest_data *hash)
  271. {
  272. struct crypto_ahash *tfm;
  273. int rc;
  274. tfm = ima_alloc_atfm(hash->algo);
  275. if (IS_ERR(tfm))
  276. return PTR_ERR(tfm);
  277. rc = ima_calc_file_hash_atfm(file, hash, tfm);
  278. ima_free_atfm(tfm);
  279. return rc;
  280. }
  281. static int ima_calc_file_hash_tfm(struct file *file,
  282. struct ima_digest_data *hash,
  283. struct crypto_shash *tfm)
  284. {
  285. loff_t i_size, offset = 0;
  286. char *rbuf;
  287. int rc;
  288. SHASH_DESC_ON_STACK(shash, tfm);
  289. shash->tfm = tfm;
  290. shash->flags = 0;
  291. hash->length = crypto_shash_digestsize(tfm);
  292. rc = crypto_shash_init(shash);
  293. if (rc != 0)
  294. return rc;
  295. i_size = i_size_read(file_inode(file));
  296. if (i_size == 0)
  297. goto out;
  298. rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  299. if (!rbuf)
  300. return -ENOMEM;
  301. while (offset < i_size) {
  302. int rbuf_len;
  303. rbuf_len = integrity_kernel_read(file, offset, rbuf, PAGE_SIZE);
  304. if (rbuf_len < 0) {
  305. rc = rbuf_len;
  306. break;
  307. }
  308. if (rbuf_len == 0)
  309. break;
  310. offset += rbuf_len;
  311. rc = crypto_shash_update(shash, rbuf, rbuf_len);
  312. if (rc)
  313. break;
  314. }
  315. kfree(rbuf);
  316. out:
  317. if (!rc)
  318. rc = crypto_shash_final(shash, hash->digest);
  319. return rc;
  320. }
  321. static int ima_calc_file_shash(struct file *file, struct ima_digest_data *hash)
  322. {
  323. struct crypto_shash *tfm;
  324. int rc;
  325. tfm = ima_alloc_tfm(hash->algo);
  326. if (IS_ERR(tfm))
  327. return PTR_ERR(tfm);
  328. rc = ima_calc_file_hash_tfm(file, hash, tfm);
  329. ima_free_tfm(tfm);
  330. return rc;
  331. }
  332. /*
  333. * ima_calc_file_hash - calculate file hash
  334. *
  335. * Asynchronous hash (ahash) allows using HW acceleration for calculating
  336. * a hash. ahash performance varies for different data sizes on different
  337. * crypto accelerators. shash performance might be better for smaller files.
  338. * The 'ima.ahash_minsize' module parameter allows specifying the best
  339. * minimum file size for using ahash on the system.
  340. *
  341. * If the ima.ahash_minsize parameter is not specified, this function uses
  342. * shash for the hash calculation. If ahash fails, it falls back to using
  343. * shash.
  344. */
  345. int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
  346. {
  347. loff_t i_size;
  348. int rc;
  349. struct file *f = file;
  350. bool new_file_instance = false;
  351. /*
  352. * For consistency, fail file's opened with the O_DIRECT flag on
  353. * filesystems mounted with/without DAX option.
  354. */
  355. if (file->f_flags & O_DIRECT) {
  356. hash->length = hash_digest_size[ima_hash_algo];
  357. hash->algo = ima_hash_algo;
  358. return -EINVAL;
  359. }
  360. /* Open a new file instance in O_RDONLY if we cannot read */
  361. if (!(file->f_mode & FMODE_READ)) {
  362. int flags = file->f_flags & ~(O_WRONLY | O_APPEND |
  363. O_TRUNC | O_CREAT | O_NOCTTY | O_EXCL);
  364. flags |= O_RDONLY;
  365. f = dentry_open(&file->f_path, flags, file->f_cred);
  366. if (IS_ERR(f))
  367. return PTR_ERR(f);
  368. new_file_instance = true;
  369. }
  370. i_size = i_size_read(file_inode(f));
  371. if (ima_ahash_minsize && i_size >= ima_ahash_minsize) {
  372. rc = ima_calc_file_ahash(f, hash);
  373. if (!rc)
  374. goto out;
  375. }
  376. rc = ima_calc_file_shash(f, hash);
  377. out:
  378. if (new_file_instance)
  379. fput(f);
  380. return rc;
  381. }
  382. /*
  383. * Calculate the hash of template data
  384. */
  385. static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
  386. struct ima_template_desc *td,
  387. int num_fields,
  388. struct ima_digest_data *hash,
  389. struct crypto_shash *tfm)
  390. {
  391. SHASH_DESC_ON_STACK(shash, tfm);
  392. int rc, i;
  393. shash->tfm = tfm;
  394. shash->flags = 0;
  395. hash->length = crypto_shash_digestsize(tfm);
  396. rc = crypto_shash_init(shash);
  397. if (rc != 0)
  398. return rc;
  399. for (i = 0; i < num_fields; i++) {
  400. u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 };
  401. u8 *data_to_hash = field_data[i].data;
  402. u32 datalen = field_data[i].len;
  403. u32 datalen_to_hash =
  404. !ima_canonical_fmt ? datalen : cpu_to_le32(datalen);
  405. if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) {
  406. rc = crypto_shash_update(shash,
  407. (const u8 *) &datalen_to_hash,
  408. sizeof(datalen_to_hash));
  409. if (rc)
  410. break;
  411. } else if (strcmp(td->fields[i]->field_id, "n") == 0) {
  412. memcpy(buffer, data_to_hash, datalen);
  413. data_to_hash = buffer;
  414. datalen = IMA_EVENT_NAME_LEN_MAX + 1;
  415. }
  416. rc = crypto_shash_update(shash, data_to_hash, datalen);
  417. if (rc)
  418. break;
  419. }
  420. if (!rc)
  421. rc = crypto_shash_final(shash, hash->digest);
  422. return rc;
  423. }
  424. int ima_calc_field_array_hash(struct ima_field_data *field_data,
  425. struct ima_template_desc *desc, int num_fields,
  426. struct ima_digest_data *hash)
  427. {
  428. struct crypto_shash *tfm;
  429. int rc;
  430. tfm = ima_alloc_tfm(hash->algo);
  431. if (IS_ERR(tfm))
  432. return PTR_ERR(tfm);
  433. rc = ima_calc_field_array_hash_tfm(field_data, desc, num_fields,
  434. hash, tfm);
  435. ima_free_tfm(tfm);
  436. return rc;
  437. }
  438. static int calc_buffer_ahash_atfm(const void *buf, loff_t len,
  439. struct ima_digest_data *hash,
  440. struct crypto_ahash *tfm)
  441. {
  442. struct ahash_request *req;
  443. struct scatterlist sg;
  444. struct crypto_wait wait;
  445. int rc, ahash_rc = 0;
  446. hash->length = crypto_ahash_digestsize(tfm);
  447. req = ahash_request_alloc(tfm, GFP_KERNEL);
  448. if (!req)
  449. return -ENOMEM;
  450. crypto_init_wait(&wait);
  451. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  452. CRYPTO_TFM_REQ_MAY_SLEEP,
  453. crypto_req_done, &wait);
  454. rc = ahash_wait(crypto_ahash_init(req), &wait);
  455. if (rc)
  456. goto out;
  457. sg_init_one(&sg, buf, len);
  458. ahash_request_set_crypt(req, &sg, NULL, len);
  459. ahash_rc = crypto_ahash_update(req);
  460. /* wait for the update request to complete */
  461. rc = ahash_wait(ahash_rc, &wait);
  462. if (!rc) {
  463. ahash_request_set_crypt(req, NULL, hash->digest, 0);
  464. rc = ahash_wait(crypto_ahash_final(req), &wait);
  465. }
  466. out:
  467. ahash_request_free(req);
  468. return rc;
  469. }
  470. static int calc_buffer_ahash(const void *buf, loff_t len,
  471. struct ima_digest_data *hash)
  472. {
  473. struct crypto_ahash *tfm;
  474. int rc;
  475. tfm = ima_alloc_atfm(hash->algo);
  476. if (IS_ERR(tfm))
  477. return PTR_ERR(tfm);
  478. rc = calc_buffer_ahash_atfm(buf, len, hash, tfm);
  479. ima_free_atfm(tfm);
  480. return rc;
  481. }
  482. static int calc_buffer_shash_tfm(const void *buf, loff_t size,
  483. struct ima_digest_data *hash,
  484. struct crypto_shash *tfm)
  485. {
  486. SHASH_DESC_ON_STACK(shash, tfm);
  487. unsigned int len;
  488. int rc;
  489. shash->tfm = tfm;
  490. shash->flags = 0;
  491. hash->length = crypto_shash_digestsize(tfm);
  492. rc = crypto_shash_init(shash);
  493. if (rc != 0)
  494. return rc;
  495. while (size) {
  496. len = size < PAGE_SIZE ? size : PAGE_SIZE;
  497. rc = crypto_shash_update(shash, buf, len);
  498. if (rc)
  499. break;
  500. buf += len;
  501. size -= len;
  502. }
  503. if (!rc)
  504. rc = crypto_shash_final(shash, hash->digest);
  505. return rc;
  506. }
  507. static int calc_buffer_shash(const void *buf, loff_t len,
  508. struct ima_digest_data *hash)
  509. {
  510. struct crypto_shash *tfm;
  511. int rc;
  512. tfm = ima_alloc_tfm(hash->algo);
  513. if (IS_ERR(tfm))
  514. return PTR_ERR(tfm);
  515. rc = calc_buffer_shash_tfm(buf, len, hash, tfm);
  516. ima_free_tfm(tfm);
  517. return rc;
  518. }
  519. int ima_calc_buffer_hash(const void *buf, loff_t len,
  520. struct ima_digest_data *hash)
  521. {
  522. int rc;
  523. if (ima_ahash_minsize && len >= ima_ahash_minsize) {
  524. rc = calc_buffer_ahash(buf, len, hash);
  525. if (!rc)
  526. return 0;
  527. }
  528. return calc_buffer_shash(buf, len, hash);
  529. }
  530. static void ima_pcrread(int idx, u8 *pcr)
  531. {
  532. if (!ima_tpm_chip)
  533. return;
  534. if (tpm_pcr_read(ima_tpm_chip, idx, pcr) != 0)
  535. pr_err("Error Communicating to TPM chip\n");
  536. }
  537. /*
  538. * Calculate the boot aggregate hash
  539. */
  540. static int ima_calc_boot_aggregate_tfm(char *digest,
  541. struct crypto_shash *tfm)
  542. {
  543. u8 pcr_i[TPM_DIGEST_SIZE];
  544. int rc, i;
  545. SHASH_DESC_ON_STACK(shash, tfm);
  546. shash->tfm = tfm;
  547. shash->flags = 0;
  548. rc = crypto_shash_init(shash);
  549. if (rc != 0)
  550. return rc;
  551. /* cumulative sha1 over tpm registers 0-7 */
  552. for (i = TPM_PCR0; i < TPM_PCR8; i++) {
  553. ima_pcrread(i, pcr_i);
  554. /* now accumulate with current aggregate */
  555. rc = crypto_shash_update(shash, pcr_i, TPM_DIGEST_SIZE);
  556. if (rc != 0)
  557. return rc;
  558. }
  559. if (!rc)
  560. crypto_shash_final(shash, digest);
  561. return rc;
  562. }
  563. int ima_calc_boot_aggregate(struct ima_digest_data *hash)
  564. {
  565. struct crypto_shash *tfm;
  566. int rc;
  567. tfm = ima_alloc_tfm(hash->algo);
  568. if (IS_ERR(tfm))
  569. return PTR_ERR(tfm);
  570. hash->length = crypto_shash_digestsize(tfm);
  571. rc = ima_calc_boot_aggregate_tfm(hash->digest, tfm);
  572. ima_free_tfm(tfm);
  573. return rc;
  574. }