Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 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 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/file.h> |
| 18 | #include <linux/crypto.h> |
| 19 | #include <linux/scatterlist.h> |
| 20 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 22 | #include <crypto/hash.h> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 23 | #include "ima.h" |
| 24 | |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 25 | static struct crypto_shash *ima_shash_tfm; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 26 | |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 27 | int ima_init_crypto(void) |
| 28 | { |
| 29 | long rc; |
| 30 | |
| 31 | ima_shash_tfm = crypto_alloc_shash(ima_hash, 0, 0); |
| 32 | if (IS_ERR(ima_shash_tfm)) { |
| 33 | rc = PTR_ERR(ima_shash_tfm); |
| 34 | pr_err("Can not allocate %s (reason: %ld)\n", ima_hash, rc); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 35 | return rc; |
| 36 | } |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 37 | return 0; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /* |
| 41 | * Calculate the MD5/SHA1 file digest |
| 42 | */ |
Dmitry Kasatkin | 50af554 | 2012-05-14 14:13:56 +0300 | [diff] [blame] | 43 | int ima_calc_file_hash(struct file *file, char *digest) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 44 | { |
Mimi Zohar | 16bfa38 | 2009-08-21 14:32:49 -0400 | [diff] [blame] | 45 | loff_t i_size, offset = 0; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 46 | char *rbuf; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 47 | int rc, read = 0; |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 48 | struct { |
| 49 | struct shash_desc shash; |
| 50 | char ctx[crypto_shash_descsize(ima_shash_tfm)]; |
| 51 | } desc; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 52 | |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 53 | desc.shash.tfm = ima_shash_tfm; |
| 54 | desc.shash.flags = 0; |
| 55 | |
| 56 | rc = crypto_shash_init(&desc.shash); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 57 | if (rc != 0) |
| 58 | return rc; |
| 59 | |
| 60 | rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 61 | if (!rbuf) { |
| 62 | rc = -ENOMEM; |
| 63 | goto out; |
| 64 | } |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 65 | if (!(file->f_mode & FMODE_READ)) { |
| 66 | file->f_mode |= FMODE_READ; |
| 67 | read = 1; |
| 68 | } |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 69 | i_size = i_size_read(file_inode(file)); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 70 | while (offset < i_size) { |
| 71 | int rbuf_len; |
| 72 | |
| 73 | rbuf_len = kernel_read(file, offset, rbuf, PAGE_SIZE); |
| 74 | if (rbuf_len < 0) { |
| 75 | rc = rbuf_len; |
| 76 | break; |
| 77 | } |
Mimi Zohar | 16bfa38 | 2009-08-21 14:32:49 -0400 | [diff] [blame] | 78 | if (rbuf_len == 0) |
| 79 | break; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 80 | offset += rbuf_len; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 81 | |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 82 | rc = crypto_shash_update(&desc.shash, rbuf, rbuf_len); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 83 | if (rc) |
| 84 | break; |
| 85 | } |
| 86 | kfree(rbuf); |
| 87 | if (!rc) |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 88 | rc = crypto_shash_final(&desc.shash, digest); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 89 | if (read) |
| 90 | file->f_mode &= ~FMODE_READ; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 91 | out: |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 92 | return rc; |
| 93 | } |
| 94 | |
| 95 | /* |
Dmitry Kasatkin | 50af554 | 2012-05-14 14:13:56 +0300 | [diff] [blame] | 96 | * Calculate the hash of a given buffer |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 97 | */ |
Dmitry Kasatkin | 50af554 | 2012-05-14 14:13:56 +0300 | [diff] [blame] | 98 | int ima_calc_buffer_hash(const void *data, int len, char *digest) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 99 | { |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 100 | struct { |
| 101 | struct shash_desc shash; |
| 102 | char ctx[crypto_shash_descsize(ima_shash_tfm)]; |
| 103 | } desc; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 104 | |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 105 | desc.shash.tfm = ima_shash_tfm; |
| 106 | desc.shash.flags = 0; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 107 | |
Dmitry Kasatkin | 50af554 | 2012-05-14 14:13:56 +0300 | [diff] [blame] | 108 | return crypto_shash_digest(&desc.shash, data, len, digest); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Eric Paris | 932995f | 2009-05-21 15:43:32 -0400 | [diff] [blame] | 111 | static void __init ima_pcrread(int idx, u8 *pcr) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 112 | { |
| 113 | if (!ima_used_chip) |
| 114 | return; |
| 115 | |
| 116 | if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0) |
Eric Paris | eb8dae9 | 2010-04-22 10:49:36 -0400 | [diff] [blame] | 117 | pr_err("IMA: Error Communicating to TPM chip\n"); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Calculate the boot aggregate hash |
| 122 | */ |
Eric Paris | 932995f | 2009-05-21 15:43:32 -0400 | [diff] [blame] | 123 | int __init ima_calc_boot_aggregate(char *digest) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 124 | { |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 125 | u8 pcr_i[IMA_DIGEST_SIZE]; |
| 126 | int rc, i; |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 127 | struct { |
| 128 | struct shash_desc shash; |
| 129 | char ctx[crypto_shash_descsize(ima_shash_tfm)]; |
| 130 | } desc; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 131 | |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 132 | desc.shash.tfm = ima_shash_tfm; |
| 133 | desc.shash.flags = 0; |
| 134 | |
| 135 | rc = crypto_shash_init(&desc.shash); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 136 | if (rc != 0) |
| 137 | return rc; |
| 138 | |
| 139 | /* cumulative sha1 over tpm registers 0-7 */ |
| 140 | for (i = TPM_PCR0; i < TPM_PCR8; i++) { |
| 141 | ima_pcrread(i, pcr_i); |
| 142 | /* now accumulate with current aggregate */ |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 143 | rc = crypto_shash_update(&desc.shash, pcr_i, IMA_DIGEST_SIZE); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 144 | } |
| 145 | if (!rc) |
Dmitry Kasatkin | 76bb28f | 2012-06-08 10:42:30 +0300 | [diff] [blame] | 146 | crypto_shash_final(&desc.shash, digest); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 147 | return rc; |
| 148 | } |