blob: 0bd732843fe70861b0d1bb58bceada6c972f8122 [file] [log] [blame]
Mimi Zohar3323eec92009-02-04 09:06:58 -05001/*
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
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020013 * Calculates md5/sha1 file hash, template hash, boot-aggreate hash
Mimi Zohar3323eec92009-02-04 09:06:58 -050014 */
15
Joe Perches20ee4512014-02-24 13:59:56 -080016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Mimi Zohar3323eec92009-02-04 09:06:58 -050018#include <linux/kernel.h>
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +020019#include <linux/moduleparam.h>
20#include <linux/ratelimit.h>
Mimi Zohar3323eec92009-02-04 09:06:58 -050021#include <linux/file.h>
22#include <linux/crypto.h>
23#include <linux/scatterlist.h>
24#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +030026#include <crypto/hash.h>
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +030027#include <crypto/hash_info.h>
Mimi Zohar3323eec92009-02-04 09:06:58 -050028#include "ima.h"
29
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +020030struct ahash_completion {
31 struct completion completion;
32 int err;
33};
34
35/* minimum file size for ahash use */
36static unsigned long ima_ahash_minsize;
37module_param_named(ahash_minsize, ima_ahash_minsize, ulong, 0644);
38MODULE_PARM_DESC(ahash_minsize, "Minimum file size for ahash use");
39
Dmitry Kasatkin6edf7a82014-05-06 14:47:13 +030040/* default is 0 - 1 page. */
41static int ima_maxorder;
42static unsigned int ima_bufsize = PAGE_SIZE;
43
44static int param_set_bufsize(const char *val, const struct kernel_param *kp)
45{
46 unsigned long long size;
47 int order;
48
49 size = memparse(val, NULL);
50 order = get_order(size);
51 if (order >= MAX_ORDER)
52 return -EINVAL;
53 ima_maxorder = order;
54 ima_bufsize = PAGE_SIZE << order;
55 return 0;
56}
57
58static struct kernel_param_ops param_ops_bufsize = {
59 .set = param_set_bufsize,
60 .get = param_get_uint,
61};
62#define param_check_bufsize(name, p) __param_check(name, p, unsigned int)
63
64module_param_named(ahash_bufsize, ima_bufsize, bufsize, 0644);
65MODULE_PARM_DESC(ahash_bufsize, "Maximum ahash buffer size");
66
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +030067static struct crypto_shash *ima_shash_tfm;
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +020068static struct crypto_ahash *ima_ahash_tfm;
Mimi Zohar3323eec92009-02-04 09:06:58 -050069
Dmitry Kasatkin0430e492014-05-08 14:03:22 +030070/**
71 * ima_kernel_read - read file content
72 *
73 * This is a function for reading file content instead of kernel_read().
74 * It does not perform locking checks to ensure it cannot be blocked.
75 * It does not perform security checks because it is irrelevant for IMA.
76 *
77 */
78static int ima_kernel_read(struct file *file, loff_t offset,
79 char *addr, unsigned long count)
80{
81 mm_segment_t old_fs;
82 char __user *buf = addr;
83 ssize_t ret;
84
85 if (!(file->f_mode & FMODE_READ))
86 return -EBADF;
87 if (!file->f_op->read && !file->f_op->aio_read)
88 return -EINVAL;
89
90 old_fs = get_fs();
91 set_fs(get_ds());
92 if (file->f_op->read)
93 ret = file->f_op->read(file, buf, count, &offset);
94 else
95 ret = do_sync_read(file, buf, count, &offset);
96 set_fs(old_fs);
97 return ret;
98}
99
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300100int ima_init_crypto(void)
101{
102 long rc;
103
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300104 ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0);
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300105 if (IS_ERR(ima_shash_tfm)) {
106 rc = PTR_ERR(ima_shash_tfm);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300107 pr_err("Can not allocate %s (reason: %ld)\n",
108 hash_algo_name[ima_hash_algo], rc);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500109 return rc;
110 }
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300111 return 0;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500112}
113
Dmitry Kasatkin723326b2013-07-04 17:40:01 +0300114static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
115{
116 struct crypto_shash *tfm = ima_shash_tfm;
117 int rc;
118
119 if (algo != ima_hash_algo && algo < HASH_ALGO__LAST) {
120 tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
121 if (IS_ERR(tfm)) {
122 rc = PTR_ERR(tfm);
123 pr_err("Can not allocate %s (reason: %d)\n",
124 hash_algo_name[algo], rc);
125 }
126 }
127 return tfm;
128}
129
130static void ima_free_tfm(struct crypto_shash *tfm)
131{
132 if (tfm != ima_shash_tfm)
133 crypto_free_shash(tfm);
134}
135
Dmitry Kasatkin6edf7a82014-05-06 14:47:13 +0300136/**
137 * ima_alloc_pages() - Allocate contiguous pages.
138 * @max_size: Maximum amount of memory to allocate.
139 * @allocated_size: Returned size of actual allocation.
140 * @last_warn: Should the min_size allocation warn or not.
141 *
142 * Tries to do opportunistic allocation for memory first trying to allocate
143 * max_size amount of memory and then splitting that until zero order is
144 * reached. Allocation is tried without generating allocation warnings unless
145 * last_warn is set. Last_warn set affects only last allocation of zero order.
146 *
147 * By default, ima_maxorder is 0 and it is equivalent to kmalloc(GFP_KERNEL)
148 *
149 * Return pointer to allocated memory, or NULL on failure.
150 */
151static void *ima_alloc_pages(loff_t max_size, size_t *allocated_size,
152 int last_warn)
153{
154 void *ptr;
155 int order = ima_maxorder;
156 gfp_t gfp_mask = __GFP_WAIT | __GFP_NOWARN | __GFP_NORETRY;
157
158 if (order)
159 order = min(get_order(max_size), order);
160
161 for (; order; order--) {
162 ptr = (void *)__get_free_pages(gfp_mask, order);
163 if (ptr) {
164 *allocated_size = PAGE_SIZE << order;
165 return ptr;
166 }
167 }
168
169 /* order is zero - one page */
170
171 gfp_mask = GFP_KERNEL;
172
173 if (!last_warn)
174 gfp_mask |= __GFP_NOWARN;
175
176 ptr = (void *)__get_free_pages(gfp_mask, 0);
177 if (ptr) {
178 *allocated_size = PAGE_SIZE;
179 return ptr;
180 }
181
182 *allocated_size = 0;
183 return NULL;
184}
185
186/**
187 * ima_free_pages() - Free pages allocated by ima_alloc_pages().
188 * @ptr: Pointer to allocated pages.
189 * @size: Size of allocated buffer.
190 */
191static void ima_free_pages(void *ptr, size_t size)
192{
193 if (!ptr)
194 return;
195 free_pages((unsigned long)ptr, get_order(size));
196}
197
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +0200198static struct crypto_ahash *ima_alloc_atfm(enum hash_algo algo)
199{
200 struct crypto_ahash *tfm = ima_ahash_tfm;
201 int rc;
202
203 if ((algo != ima_hash_algo && algo < HASH_ALGO__LAST) || !tfm) {
204 tfm = crypto_alloc_ahash(hash_algo_name[algo], 0, 0);
205 if (!IS_ERR(tfm)) {
206 if (algo == ima_hash_algo)
207 ima_ahash_tfm = tfm;
208 } else {
209 rc = PTR_ERR(tfm);
210 pr_err("Can not allocate %s (reason: %d)\n",
211 hash_algo_name[algo], rc);
212 }
213 }
214 return tfm;
215}
216
217static void ima_free_atfm(struct crypto_ahash *tfm)
218{
219 if (tfm != ima_ahash_tfm)
220 crypto_free_ahash(tfm);
221}
222
223static void ahash_complete(struct crypto_async_request *req, int err)
224{
225 struct ahash_completion *res = req->data;
226
227 if (err == -EINPROGRESS)
228 return;
229 res->err = err;
230 complete(&res->completion);
231}
232
233static int ahash_wait(int err, struct ahash_completion *res)
234{
235 switch (err) {
236 case 0:
237 break;
238 case -EINPROGRESS:
239 case -EBUSY:
240 wait_for_completion(&res->completion);
241 reinit_completion(&res->completion);
242 err = res->err;
243 /* fall through */
244 default:
245 pr_crit_ratelimited("ahash calculation failed: err: %d\n", err);
246 }
247
248 return err;
249}
250
251static int ima_calc_file_hash_atfm(struct file *file,
252 struct ima_digest_data *hash,
253 struct crypto_ahash *tfm)
254{
255 loff_t i_size, offset;
Dmitry Kasatkin32c2e672014-05-06 14:54:27 +0300256 char *rbuf[2] = { NULL, };
257 int rc, read = 0, rbuf_len, active = 0, ahash_rc = 0;
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +0200258 struct ahash_request *req;
259 struct scatterlist sg[1];
260 struct ahash_completion res;
Dmitry Kasatkin32c2e672014-05-06 14:54:27 +0300261 size_t rbuf_size[2];
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +0200262
263 hash->length = crypto_ahash_digestsize(tfm);
264
265 req = ahash_request_alloc(tfm, GFP_KERNEL);
266 if (!req)
267 return -ENOMEM;
268
269 init_completion(&res.completion);
270 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
271 CRYPTO_TFM_REQ_MAY_SLEEP,
272 ahash_complete, &res);
273
274 rc = ahash_wait(crypto_ahash_init(req), &res);
275 if (rc)
276 goto out1;
277
278 i_size = i_size_read(file_inode(file));
279
280 if (i_size == 0)
281 goto out2;
282
Dmitry Kasatkin6edf7a82014-05-06 14:47:13 +0300283 /*
284 * Try to allocate maximum size of memory.
285 * Fail if even a single page cannot be allocated.
286 */
Dmitry Kasatkin32c2e672014-05-06 14:54:27 +0300287 rbuf[0] = ima_alloc_pages(i_size, &rbuf_size[0], 1);
288 if (!rbuf[0]) {
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +0200289 rc = -ENOMEM;
290 goto out1;
291 }
292
Dmitry Kasatkin32c2e672014-05-06 14:54:27 +0300293 /* Only allocate one buffer if that is enough. */
294 if (i_size > rbuf_size[0]) {
295 /*
296 * Try to allocate secondary buffer. If that fails fallback to
297 * using single buffering. Use previous memory allocation size
298 * as baseline for possible allocation size.
299 */
300 rbuf[1] = ima_alloc_pages(i_size - rbuf_size[0],
301 &rbuf_size[1], 0);
302 }
303
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +0200304 if (!(file->f_mode & FMODE_READ)) {
305 file->f_mode |= FMODE_READ;
306 read = 1;
307 }
308
309 for (offset = 0; offset < i_size; offset += rbuf_len) {
Dmitry Kasatkin32c2e672014-05-06 14:54:27 +0300310 if (!rbuf[1] && offset) {
311 /* Not using two buffers, and it is not the first
312 * read/request, wait for the completion of the
313 * previous ahash_update() request.
314 */
315 rc = ahash_wait(ahash_rc, &res);
316 if (rc)
317 goto out3;
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +0200318 }
Dmitry Kasatkin32c2e672014-05-06 14:54:27 +0300319 /* read buffer */
320 rbuf_len = min_t(loff_t, i_size - offset, rbuf_size[active]);
321 rc = ima_kernel_read(file, offset, rbuf[active], rbuf_len);
322 if (rc != rbuf_len)
323 goto out3;
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +0200324
Dmitry Kasatkin32c2e672014-05-06 14:54:27 +0300325 if (rbuf[1] && offset) {
326 /* Using two buffers, and it is not the first
327 * read/request, wait for the completion of the
328 * previous ahash_update() request.
329 */
330 rc = ahash_wait(ahash_rc, &res);
331 if (rc)
332 goto out3;
333 }
334
335 sg_init_one(&sg[0], rbuf[active], rbuf_len);
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +0200336 ahash_request_set_crypt(req, sg, NULL, rbuf_len);
337
Dmitry Kasatkin32c2e672014-05-06 14:54:27 +0300338 ahash_rc = crypto_ahash_update(req);
339
340 if (rbuf[1])
341 active = !active; /* swap buffers, if we use two */
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +0200342 }
Dmitry Kasatkin32c2e672014-05-06 14:54:27 +0300343 /* wait for the last update request to complete */
344 rc = ahash_wait(ahash_rc, &res);
345out3:
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +0200346 if (read)
347 file->f_mode &= ~FMODE_READ;
Dmitry Kasatkin32c2e672014-05-06 14:54:27 +0300348 ima_free_pages(rbuf[0], rbuf_size[0]);
349 ima_free_pages(rbuf[1], rbuf_size[1]);
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +0200350out2:
351 if (!rc) {
352 ahash_request_set_crypt(req, NULL, hash->digest, 0);
353 rc = ahash_wait(crypto_ahash_final(req), &res);
354 }
355out1:
356 ahash_request_free(req);
357 return rc;
358}
359
360static int ima_calc_file_ahash(struct file *file, struct ima_digest_data *hash)
361{
362 struct crypto_ahash *tfm;
363 int rc;
364
365 tfm = ima_alloc_atfm(hash->algo);
366 if (IS_ERR(tfm))
367 return PTR_ERR(tfm);
368
369 rc = ima_calc_file_hash_atfm(file, hash, tfm);
370
371 ima_free_atfm(tfm);
372
373 return rc;
374}
375
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300376static int ima_calc_file_hash_tfm(struct file *file,
377 struct ima_digest_data *hash,
378 struct crypto_shash *tfm)
Mimi Zohar3323eec92009-02-04 09:06:58 -0500379{
Mimi Zohar16bfa382009-08-21 14:32:49 -0400380 loff_t i_size, offset = 0;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500381 char *rbuf;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500382 int rc, read = 0;
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300383 struct {
384 struct shash_desc shash;
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300385 char ctx[crypto_shash_descsize(tfm)];
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300386 } desc;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500387
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300388 desc.shash.tfm = tfm;
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300389 desc.shash.flags = 0;
390
Dmitry Kasatkin723326b2013-07-04 17:40:01 +0300391 hash->length = crypto_shash_digestsize(tfm);
392
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300393 rc = crypto_shash_init(&desc.shash);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500394 if (rc != 0)
395 return rc;
396
Dmitry Kasatkin1d91ac62014-02-27 20:16:47 +0200397 i_size = i_size_read(file_inode(file));
398
399 if (i_size == 0)
Mimi Zohar3323eec92009-02-04 09:06:58 -0500400 goto out;
Dmitry Kasatkin1d91ac62014-02-27 20:16:47 +0200401
402 rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
403 if (!rbuf)
404 return -ENOMEM;
405
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500406 if (!(file->f_mode & FMODE_READ)) {
407 file->f_mode |= FMODE_READ;
408 read = 1;
409 }
Dmitry Kasatkin1d91ac62014-02-27 20:16:47 +0200410
Mimi Zohar3323eec92009-02-04 09:06:58 -0500411 while (offset < i_size) {
412 int rbuf_len;
413
Dmitry Kasatkin0430e492014-05-08 14:03:22 +0300414 rbuf_len = ima_kernel_read(file, offset, rbuf, PAGE_SIZE);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500415 if (rbuf_len < 0) {
416 rc = rbuf_len;
417 break;
418 }
Mimi Zohar16bfa382009-08-21 14:32:49 -0400419 if (rbuf_len == 0)
420 break;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500421 offset += rbuf_len;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500422
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300423 rc = crypto_shash_update(&desc.shash, rbuf, rbuf_len);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500424 if (rc)
425 break;
426 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500427 if (read)
428 file->f_mode &= ~FMODE_READ;
Dmitry Kasatkin1d91ac62014-02-27 20:16:47 +0200429 kfree(rbuf);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500430out:
Dmitry Kasatkin1d91ac62014-02-27 20:16:47 +0200431 if (!rc)
432 rc = crypto_shash_final(&desc.shash, hash->digest);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500433 return rc;
434}
435
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +0200436static int ima_calc_file_shash(struct file *file, struct ima_digest_data *hash)
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300437{
Dmitry Kasatkin723326b2013-07-04 17:40:01 +0300438 struct crypto_shash *tfm;
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300439 int rc;
440
Dmitry Kasatkin723326b2013-07-04 17:40:01 +0300441 tfm = ima_alloc_tfm(hash->algo);
442 if (IS_ERR(tfm))
443 return PTR_ERR(tfm);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300444
445 rc = ima_calc_file_hash_tfm(file, hash, tfm);
446
Dmitry Kasatkin723326b2013-07-04 17:40:01 +0300447 ima_free_tfm(tfm);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300448
449 return rc;
450}
451
Mimi Zohar3323eec92009-02-04 09:06:58 -0500452/*
Dmitry Kasatkin3bcced32014-02-26 17:05:20 +0200453 * ima_calc_file_hash - calculate file hash
454 *
455 * Asynchronous hash (ahash) allows using HW acceleration for calculating
456 * a hash. ahash performance varies for different data sizes on different
457 * crypto accelerators. shash performance might be better for smaller files.
458 * The 'ima.ahash_minsize' module parameter allows specifying the best
459 * minimum file size for using ahash on the system.
460 *
461 * If the ima.ahash_minsize parameter is not specified, this function uses
462 * shash for the hash calculation. If ahash fails, it falls back to using
463 * shash.
464 */
465int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
466{
467 loff_t i_size;
468 int rc;
469
470 i_size = i_size_read(file_inode(file));
471
472 if (ima_ahash_minsize && i_size >= ima_ahash_minsize) {
473 rc = ima_calc_file_ahash(file, hash);
474 if (!rc)
475 return 0;
476 }
477
478 return ima_calc_file_shash(file, hash);
479}
480
481/*
Roberto Sassua71dc652013-06-07 12:16:33 +0200482 * Calculate the hash of template data
Mimi Zohar3323eec92009-02-04 09:06:58 -0500483 */
Roberto Sassua71dc652013-06-07 12:16:33 +0200484static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
Roberto Sassub6f8f162013-11-08 19:21:39 +0100485 struct ima_template_desc *td,
Roberto Sassua71dc652013-06-07 12:16:33 +0200486 int num_fields,
487 struct ima_digest_data *hash,
488 struct crypto_shash *tfm)
Mimi Zohar3323eec92009-02-04 09:06:58 -0500489{
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300490 struct {
491 struct shash_desc shash;
Dmitry Kasatkinea593992013-06-07 12:16:24 +0200492 char ctx[crypto_shash_descsize(tfm)];
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300493 } desc;
Roberto Sassua71dc652013-06-07 12:16:33 +0200494 int rc, i;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500495
Dmitry Kasatkinea593992013-06-07 12:16:24 +0200496 desc.shash.tfm = tfm;
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300497 desc.shash.flags = 0;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500498
Dmitry Kasatkinea593992013-06-07 12:16:24 +0200499 hash->length = crypto_shash_digestsize(tfm);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300500
Roberto Sassua71dc652013-06-07 12:16:33 +0200501 rc = crypto_shash_init(&desc.shash);
502 if (rc != 0)
503 return rc;
504
505 for (i = 0; i < num_fields; i++) {
Roberto Sassue3b64c22014-02-03 13:56:05 +0100506 u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 };
507 u8 *data_to_hash = field_data[i].data;
508 u32 datalen = field_data[i].len;
509
Roberto Sassub6f8f162013-11-08 19:21:39 +0100510 if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) {
511 rc = crypto_shash_update(&desc.shash,
512 (const u8 *) &field_data[i].len,
513 sizeof(field_data[i].len));
514 if (rc)
515 break;
Roberto Sassue3b64c22014-02-03 13:56:05 +0100516 } else if (strcmp(td->fields[i]->field_id, "n") == 0) {
517 memcpy(buffer, data_to_hash, datalen);
518 data_to_hash = buffer;
519 datalen = IMA_EVENT_NAME_LEN_MAX + 1;
Roberto Sassub6f8f162013-11-08 19:21:39 +0100520 }
Roberto Sassue3b64c22014-02-03 13:56:05 +0100521 rc = crypto_shash_update(&desc.shash, data_to_hash, datalen);
Roberto Sassua71dc652013-06-07 12:16:33 +0200522 if (rc)
523 break;
524 }
525
526 if (!rc)
527 rc = crypto_shash_final(&desc.shash, hash->digest);
528
529 return rc;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500530}
531
Roberto Sassub6f8f162013-11-08 19:21:39 +0100532int ima_calc_field_array_hash(struct ima_field_data *field_data,
533 struct ima_template_desc *desc, int num_fields,
Roberto Sassua71dc652013-06-07 12:16:33 +0200534 struct ima_digest_data *hash)
Dmitry Kasatkinea593992013-06-07 12:16:24 +0200535{
536 struct crypto_shash *tfm;
537 int rc;
538
539 tfm = ima_alloc_tfm(hash->algo);
540 if (IS_ERR(tfm))
541 return PTR_ERR(tfm);
542
Roberto Sassub6f8f162013-11-08 19:21:39 +0100543 rc = ima_calc_field_array_hash_tfm(field_data, desc, num_fields,
544 hash, tfm);
Dmitry Kasatkinea593992013-06-07 12:16:24 +0200545
546 ima_free_tfm(tfm);
547
548 return rc;
549}
550
Eric Paris932995f2009-05-21 15:43:32 -0400551static void __init ima_pcrread(int idx, u8 *pcr)
Mimi Zohar3323eec92009-02-04 09:06:58 -0500552{
553 if (!ima_used_chip)
554 return;
555
556 if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0)
Joe Perches20ee4512014-02-24 13:59:56 -0800557 pr_err("Error Communicating to TPM chip\n");
Mimi Zohar3323eec92009-02-04 09:06:58 -0500558}
559
560/*
561 * Calculate the boot aggregate hash
562 */
Dmitry Kasatkin09ef5432013-06-07 12:16:25 +0200563static int __init ima_calc_boot_aggregate_tfm(char *digest,
564 struct crypto_shash *tfm)
Mimi Zohar3323eec92009-02-04 09:06:58 -0500565{
Mimi Zohar140d8022013-03-11 20:29:47 -0400566 u8 pcr_i[TPM_DIGEST_SIZE];
Mimi Zohar3323eec92009-02-04 09:06:58 -0500567 int rc, i;
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300568 struct {
569 struct shash_desc shash;
Dmitry Kasatkin09ef5432013-06-07 12:16:25 +0200570 char ctx[crypto_shash_descsize(tfm)];
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300571 } desc;
Mimi Zohar3323eec92009-02-04 09:06:58 -0500572
Dmitry Kasatkin09ef5432013-06-07 12:16:25 +0200573 desc.shash.tfm = tfm;
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300574 desc.shash.flags = 0;
575
576 rc = crypto_shash_init(&desc.shash);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500577 if (rc != 0)
578 return rc;
579
580 /* cumulative sha1 over tpm registers 0-7 */
581 for (i = TPM_PCR0; i < TPM_PCR8; i++) {
582 ima_pcrread(i, pcr_i);
583 /* now accumulate with current aggregate */
Mimi Zohar140d8022013-03-11 20:29:47 -0400584 rc = crypto_shash_update(&desc.shash, pcr_i, TPM_DIGEST_SIZE);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500585 }
586 if (!rc)
Dmitry Kasatkin76bb28f2012-06-08 10:42:30 +0300587 crypto_shash_final(&desc.shash, digest);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500588 return rc;
589}
Dmitry Kasatkin09ef5432013-06-07 12:16:25 +0200590
591int __init ima_calc_boot_aggregate(struct ima_digest_data *hash)
592{
593 struct crypto_shash *tfm;
594 int rc;
595
596 tfm = ima_alloc_tfm(hash->algo);
597 if (IS_ERR(tfm))
598 return PTR_ERR(tfm);
599
600 hash->length = crypto_shash_digestsize(tfm);
601 rc = ima_calc_boot_aggregate_tfm(hash->digest, tfm);
602
603 ima_free_tfm(tfm);
604
605 return rc;
606}