ima_crypto.c 16 KB

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