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>
21 #include <linux/slab.h>
22 #include <crypto/hash.h>
23 #include <crypto/hash_info.h>
24 #include "ima.h"
25 
26 static struct crypto_shash *ima_shash_tfm;
27 
28 int ima_init_crypto(void)
29 {
30 	long rc;
31 
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);
37 		return rc;
38 	}
39 	return 0;
40 }
41 
42 static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
43 {
44 	struct crypto_shash *tfm = ima_shash_tfm;
45 	int rc;
46 
47 	if (algo != ima_hash_algo && algo < HASH_ALGO__LAST) {
48 		tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
49 		if (IS_ERR(tfm)) {
50 			rc = PTR_ERR(tfm);
51 			pr_err("Can not allocate %s (reason: %d)\n",
52 			       hash_algo_name[algo], rc);
53 		}
54 	}
55 	return tfm;
56 }
57 
58 static void ima_free_tfm(struct crypto_shash *tfm)
59 {
60 	if (tfm != ima_shash_tfm)
61 		crypto_free_shash(tfm);
62 }
63 
64 /*
65  * Calculate the MD5/SHA1 file digest
66  */
67 static int ima_calc_file_hash_tfm(struct file *file,
68 				  struct ima_digest_data *hash,
69 				  struct crypto_shash *tfm)
70 {
71 	loff_t i_size, offset = 0;
72 	char *rbuf;
73 	int rc, read = 0;
74 	struct {
75 		struct shash_desc shash;
76 		char ctx[crypto_shash_descsize(tfm)];
77 	} desc;
78 
79 	desc.shash.tfm = tfm;
80 	desc.shash.flags = 0;
81 
82 	hash->length = crypto_shash_digestsize(tfm);
83 
84 	rc = crypto_shash_init(&desc.shash);
85 	if (rc != 0)
86 		return rc;
87 
88 	rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
89 	if (!rbuf) {
90 		rc = -ENOMEM;
91 		goto out;
92 	}
93 	if (!(file->f_mode & FMODE_READ)) {
94 		file->f_mode |= FMODE_READ;
95 		read = 1;
96 	}
97 	i_size = i_size_read(file_inode(file));
98 	while (offset < i_size) {
99 		int rbuf_len;
100 
101 		rbuf_len = kernel_read(file, offset, rbuf, PAGE_SIZE);
102 		if (rbuf_len < 0) {
103 			rc = rbuf_len;
104 			break;
105 		}
106 		if (rbuf_len == 0)
107 			break;
108 		offset += rbuf_len;
109 
110 		rc = crypto_shash_update(&desc.shash, rbuf, rbuf_len);
111 		if (rc)
112 			break;
113 	}
114 	kfree(rbuf);
115 	if (!rc)
116 		rc = crypto_shash_final(&desc.shash, hash->digest);
117 	if (read)
118 		file->f_mode &= ~FMODE_READ;
119 out:
120 	return rc;
121 }
122 
123 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
124 {
125 	struct crypto_shash *tfm;
126 	int rc;
127 
128 	tfm = ima_alloc_tfm(hash->algo);
129 	if (IS_ERR(tfm))
130 		return PTR_ERR(tfm);
131 
132 	rc = ima_calc_file_hash_tfm(file, hash, tfm);
133 
134 	ima_free_tfm(tfm);
135 
136 	return rc;
137 }
138 
139 /*
140  * Calculate the hash of template data
141  */
142 static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
143 					 int num_fields,
144 					 struct ima_digest_data *hash,
145 					 struct crypto_shash *tfm)
146 {
147 	struct {
148 		struct shash_desc shash;
149 		char ctx[crypto_shash_descsize(tfm)];
150 	} desc;
151 	int rc, i;
152 
153 	desc.shash.tfm = tfm;
154 	desc.shash.flags = 0;
155 
156 	hash->length = crypto_shash_digestsize(tfm);
157 
158 	rc = crypto_shash_init(&desc.shash);
159 	if (rc != 0)
160 		return rc;
161 
162 	for (i = 0; i < num_fields; i++) {
163 		rc = crypto_shash_update(&desc.shash,
164 					 (const u8 *) &field_data[i].len,
165 					 sizeof(field_data[i].len));
166 		rc = crypto_shash_update(&desc.shash, field_data[i].data,
167 					 field_data[i].len);
168 		if (rc)
169 			break;
170 	}
171 
172 	if (!rc)
173 		rc = crypto_shash_final(&desc.shash, hash->digest);
174 
175 	return rc;
176 }
177 
178 int ima_calc_field_array_hash(struct ima_field_data *field_data, int num_fields,
179 			      struct ima_digest_data *hash)
180 {
181 	struct crypto_shash *tfm;
182 	int rc;
183 
184 	tfm = ima_alloc_tfm(hash->algo);
185 	if (IS_ERR(tfm))
186 		return PTR_ERR(tfm);
187 
188 	rc = ima_calc_field_array_hash_tfm(field_data, num_fields, hash, tfm);
189 
190 	ima_free_tfm(tfm);
191 
192 	return rc;
193 }
194 
195 static void __init ima_pcrread(int idx, u8 *pcr)
196 {
197 	if (!ima_used_chip)
198 		return;
199 
200 	if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0)
201 		pr_err("IMA: Error Communicating to TPM chip\n");
202 }
203 
204 /*
205  * Calculate the boot aggregate hash
206  */
207 static int __init ima_calc_boot_aggregate_tfm(char *digest,
208 					      struct crypto_shash *tfm)
209 {
210 	u8 pcr_i[TPM_DIGEST_SIZE];
211 	int rc, i;
212 	struct {
213 		struct shash_desc shash;
214 		char ctx[crypto_shash_descsize(tfm)];
215 	} desc;
216 
217 	desc.shash.tfm = tfm;
218 	desc.shash.flags = 0;
219 
220 	rc = crypto_shash_init(&desc.shash);
221 	if (rc != 0)
222 		return rc;
223 
224 	/* cumulative sha1 over tpm registers 0-7 */
225 	for (i = TPM_PCR0; i < TPM_PCR8; i++) {
226 		ima_pcrread(i, pcr_i);
227 		/* now accumulate with current aggregate */
228 		rc = crypto_shash_update(&desc.shash, pcr_i, TPM_DIGEST_SIZE);
229 	}
230 	if (!rc)
231 		crypto_shash_final(&desc.shash, digest);
232 	return rc;
233 }
234 
235 int __init ima_calc_boot_aggregate(struct ima_digest_data *hash)
236 {
237 	struct crypto_shash *tfm;
238 	int rc;
239 
240 	tfm = ima_alloc_tfm(hash->algo);
241 	if (IS_ERR(tfm))
242 		return PTR_ERR(tfm);
243 
244 	hash->length = crypto_shash_digestsize(tfm);
245 	rc = ima_calc_boot_aggregate_tfm(hash->digest, tfm);
246 
247 	ima_free_tfm(tfm);
248 
249 	return rc;
250 }
251