2 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
5 * Mimi Zohar <zohar@us.ibm.com>
6 * Kylene Hall <kjhall@us.ibm.com>
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.
13 * Calculates md5/sha1 file hash, template hash, boot-aggreate hash
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/kernel.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 <crypto/hash_info.h>
28 static struct crypto_shash *ima_shash_tfm;
30 int ima_init_crypto(void)
34 ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0);
35 if (IS_ERR(ima_shash_tfm)) {
36 rc = PTR_ERR(ima_shash_tfm);
37 pr_err("Can not allocate %s (reason: %ld)\n",
38 hash_algo_name[ima_hash_algo], rc);
44 static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
46 struct crypto_shash *tfm = ima_shash_tfm;
49 if (algo != ima_hash_algo && algo < HASH_ALGO__LAST) {
50 tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
53 pr_err("Can not allocate %s (reason: %d)\n",
54 hash_algo_name[algo], rc);
60 static void ima_free_tfm(struct crypto_shash *tfm)
62 if (tfm != ima_shash_tfm)
63 crypto_free_shash(tfm);
67 * Calculate the MD5/SHA1 file digest
69 static int ima_calc_file_hash_tfm(struct file *file,
70 struct ima_digest_data *hash,
71 struct crypto_shash *tfm)
73 loff_t i_size, offset = 0;
77 struct shash_desc shash;
78 char ctx[crypto_shash_descsize(tfm)];
84 hash->length = crypto_shash_digestsize(tfm);
86 rc = crypto_shash_init(&desc.shash);
90 i_size = i_size_read(file_inode(file));
95 rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
99 if (!(file->f_mode & FMODE_READ)) {
100 file->f_mode |= FMODE_READ;
104 while (offset < i_size) {
107 rbuf_len = kernel_read(file, offset, rbuf, PAGE_SIZE);
116 rc = crypto_shash_update(&desc.shash, rbuf, rbuf_len);
121 file->f_mode &= ~FMODE_READ;
125 rc = crypto_shash_final(&desc.shash, hash->digest);
129 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
131 struct crypto_shash *tfm;
134 tfm = ima_alloc_tfm(hash->algo);
138 rc = ima_calc_file_hash_tfm(file, hash, tfm);
146 * Calculate the hash of template data
148 static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
149 struct ima_template_desc *td,
151 struct ima_digest_data *hash,
152 struct crypto_shash *tfm)
155 struct shash_desc shash;
156 char ctx[crypto_shash_descsize(tfm)];
160 desc.shash.tfm = tfm;
161 desc.shash.flags = 0;
163 hash->length = crypto_shash_digestsize(tfm);
165 rc = crypto_shash_init(&desc.shash);
169 for (i = 0; i < num_fields; i++) {
170 u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 };
171 u8 *data_to_hash = field_data[i].data;
172 u32 datalen = field_data[i].len;
174 if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) {
175 rc = crypto_shash_update(&desc.shash,
176 (const u8 *) &field_data[i].len,
177 sizeof(field_data[i].len));
180 } else if (strcmp(td->fields[i]->field_id, "n") == 0) {
181 memcpy(buffer, data_to_hash, datalen);
182 data_to_hash = buffer;
183 datalen = IMA_EVENT_NAME_LEN_MAX + 1;
185 rc = crypto_shash_update(&desc.shash, data_to_hash, datalen);
191 rc = crypto_shash_final(&desc.shash, hash->digest);
196 int ima_calc_field_array_hash(struct ima_field_data *field_data,
197 struct ima_template_desc *desc, int num_fields,
198 struct ima_digest_data *hash)
200 struct crypto_shash *tfm;
203 tfm = ima_alloc_tfm(hash->algo);
207 rc = ima_calc_field_array_hash_tfm(field_data, desc, num_fields,
215 static void __init ima_pcrread(int idx, u8 *pcr)
220 if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0)
221 pr_err("Error Communicating to TPM chip\n");
225 * Calculate the boot aggregate hash
227 static int __init ima_calc_boot_aggregate_tfm(char *digest,
228 struct crypto_shash *tfm)
230 u8 pcr_i[TPM_DIGEST_SIZE];
233 struct shash_desc shash;
234 char ctx[crypto_shash_descsize(tfm)];
237 desc.shash.tfm = tfm;
238 desc.shash.flags = 0;
240 rc = crypto_shash_init(&desc.shash);
244 /* cumulative sha1 over tpm registers 0-7 */
245 for (i = TPM_PCR0; i < TPM_PCR8; i++) {
246 ima_pcrread(i, pcr_i);
247 /* now accumulate with current aggregate */
248 rc = crypto_shash_update(&desc.shash, pcr_i, TPM_DIGEST_SIZE);
251 crypto_shash_final(&desc.shash, digest);
255 int __init ima_calc_boot_aggregate(struct ima_digest_data *hash)
257 struct crypto_shash *tfm;
260 tfm = ima_alloc_tfm(hash->algo);
264 hash->length = crypto_shash_digestsize(tfm);
265 rc = ima_calc_boot_aggregate_tfm(hash->digest, tfm);