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 #include <linux/kernel.h>
17 #include <linux/file.h>
18 #include <linux/crypto.h>
19 #include <linux/scatterlist.h>
20 #include <linux/err.h>
21 #include <linux/slab.h>
22 #include <crypto/hash.h>
23 #include <crypto/hash_info.h>
26 static struct crypto_shash *ima_shash_tfm;
28 int ima_init_crypto(void)
32 ima_shash_tfm = crypto_alloc_shash(hash_algo_name[ima_hash_algo], 0, 0);
33 if (IS_ERR(ima_shash_tfm)) {
34 rc = PTR_ERR(ima_shash_tfm);
35 pr_err("Can not allocate %s (reason: %ld)\n",
36 hash_algo_name[ima_hash_algo], rc);
42 static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
44 struct crypto_shash *tfm = ima_shash_tfm;
47 if (algo != ima_hash_algo && algo < HASH_ALGO__LAST) {
48 tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
51 pr_err("Can not allocate %s (reason: %d)\n",
52 hash_algo_name[algo], rc);
58 static void ima_free_tfm(struct crypto_shash *tfm)
60 if (tfm != ima_shash_tfm)
61 crypto_free_shash(tfm);
65 * Calculate the MD5/SHA1 file digest
67 static int ima_calc_file_hash_tfm(struct file *file,
68 struct ima_digest_data *hash,
69 struct crypto_shash *tfm)
71 loff_t i_size, offset = 0;
75 struct shash_desc shash;
76 char ctx[crypto_shash_descsize(tfm)];
82 hash->length = crypto_shash_digestsize(tfm);
84 rc = crypto_shash_init(&desc.shash);
88 rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
93 if (!(file->f_mode & FMODE_READ)) {
94 file->f_mode |= FMODE_READ;
97 i_size = i_size_read(file_inode(file));
98 while (offset < i_size) {
101 rbuf_len = kernel_read(file, offset, rbuf, PAGE_SIZE);
110 rc = crypto_shash_update(&desc.shash, rbuf, rbuf_len);
116 rc = crypto_shash_final(&desc.shash, hash->digest);
118 file->f_mode &= ~FMODE_READ;
123 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
125 struct crypto_shash *tfm;
128 tfm = ima_alloc_tfm(hash->algo);
132 rc = ima_calc_file_hash_tfm(file, hash, tfm);
140 * Calculate the hash of a given buffer
142 static int ima_calc_buffer_hash_tfm(const void *buf, int len,
143 struct ima_digest_data *hash,
144 struct crypto_shash *tfm)
147 struct shash_desc shash;
148 char ctx[crypto_shash_descsize(tfm)];
151 desc.shash.tfm = tfm;
152 desc.shash.flags = 0;
154 hash->length = crypto_shash_digestsize(tfm);
156 return crypto_shash_digest(&desc.shash, buf, len, hash->digest);
159 int ima_calc_buffer_hash(const void *buf, int len, struct ima_digest_data *hash)
161 struct crypto_shash *tfm;
164 tfm = ima_alloc_tfm(hash->algo);
168 rc = ima_calc_buffer_hash_tfm(buf, len, hash, tfm);
175 static void __init ima_pcrread(int idx, u8 *pcr)
180 if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0)
181 pr_err("IMA: Error Communicating to TPM chip\n");
185 * Calculate the boot aggregate hash
187 static int __init ima_calc_boot_aggregate_tfm(char *digest,
188 struct crypto_shash *tfm)
190 u8 pcr_i[TPM_DIGEST_SIZE];
193 struct shash_desc shash;
194 char ctx[crypto_shash_descsize(tfm)];
197 desc.shash.tfm = tfm;
198 desc.shash.flags = 0;
200 rc = crypto_shash_init(&desc.shash);
204 /* cumulative sha1 over tpm registers 0-7 */
205 for (i = TPM_PCR0; i < TPM_PCR8; i++) {
206 ima_pcrread(i, pcr_i);
207 /* now accumulate with current aggregate */
208 rc = crypto_shash_update(&desc.shash, pcr_i, TPM_DIGEST_SIZE);
211 crypto_shash_final(&desc.shash, digest);
215 int __init ima_calc_boot_aggregate(struct ima_digest_data *hash)
217 struct crypto_shash *tfm;
220 tfm = ima_alloc_tfm(hash->algo);
224 hash->length = crypto_shash_digestsize(tfm);
225 rc = ima_calc_boot_aggregate_tfm(hash->digest, tfm);