This source file includes following definitions.
- fsverity_get_hash_alg
- fsverity_prepare_hash_state
- fsverity_hash_page
- fsverity_hash_buffer
- fsverity_check_hash_algs
1
2
3
4
5
6
7
8 #include "fsverity_private.h"
9
10 #include <crypto/hash.h>
11 #include <linux/scatterlist.h>
12
13
14 struct fsverity_hash_alg fsverity_hash_algs[] = {
15 [FS_VERITY_HASH_ALG_SHA256] = {
16 .name = "sha256",
17 .digest_size = SHA256_DIGEST_SIZE,
18 .block_size = SHA256_BLOCK_SIZE,
19 },
20 [FS_VERITY_HASH_ALG_SHA512] = {
21 .name = "sha512",
22 .digest_size = SHA512_DIGEST_SIZE,
23 .block_size = SHA512_BLOCK_SIZE,
24 },
25 };
26
27
28
29
30
31
32
33
34
35
36
37
38
39 const struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
40 unsigned int num)
41 {
42 struct fsverity_hash_alg *alg;
43 struct crypto_ahash *tfm;
44 int err;
45
46 if (num >= ARRAY_SIZE(fsverity_hash_algs) ||
47 !fsverity_hash_algs[num].name) {
48 fsverity_warn(inode, "Unknown hash algorithm number: %u", num);
49 return ERR_PTR(-EINVAL);
50 }
51 alg = &fsverity_hash_algs[num];
52
53
54 tfm = READ_ONCE(alg->tfm);
55 if (likely(tfm != NULL))
56 return alg;
57
58
59
60
61 tfm = crypto_alloc_ahash(alg->name, 0, 0);
62 if (IS_ERR(tfm)) {
63 if (PTR_ERR(tfm) == -ENOENT) {
64 fsverity_warn(inode,
65 "Missing crypto API support for hash algorithm \"%s\"",
66 alg->name);
67 return ERR_PTR(-ENOPKG);
68 }
69 fsverity_err(inode,
70 "Error allocating hash algorithm \"%s\": %ld",
71 alg->name, PTR_ERR(tfm));
72 return ERR_CAST(tfm);
73 }
74
75 err = -EINVAL;
76 if (WARN_ON(alg->digest_size != crypto_ahash_digestsize(tfm)))
77 goto err_free_tfm;
78 if (WARN_ON(alg->block_size != crypto_ahash_blocksize(tfm)))
79 goto err_free_tfm;
80
81 pr_info("%s using implementation \"%s\"\n",
82 alg->name, crypto_ahash_driver_name(tfm));
83
84
85 if (cmpxchg(&alg->tfm, NULL, tfm) != NULL)
86 crypto_free_ahash(tfm);
87
88 return alg;
89
90 err_free_tfm:
91 crypto_free_ahash(tfm);
92 return ERR_PTR(err);
93 }
94
95
96
97
98
99
100
101
102
103
104 const u8 *fsverity_prepare_hash_state(const struct fsverity_hash_alg *alg,
105 const u8 *salt, size_t salt_size)
106 {
107 u8 *hashstate = NULL;
108 struct ahash_request *req = NULL;
109 u8 *padded_salt = NULL;
110 size_t padded_salt_size;
111 struct scatterlist sg;
112 DECLARE_CRYPTO_WAIT(wait);
113 int err;
114
115 if (salt_size == 0)
116 return NULL;
117
118 hashstate = kmalloc(crypto_ahash_statesize(alg->tfm), GFP_KERNEL);
119 if (!hashstate)
120 return ERR_PTR(-ENOMEM);
121
122 req = ahash_request_alloc(alg->tfm, GFP_KERNEL);
123 if (!req) {
124 err = -ENOMEM;
125 goto err_free;
126 }
127
128
129
130
131
132
133
134
135 padded_salt_size = round_up(salt_size, alg->block_size);
136 padded_salt = kzalloc(padded_salt_size, GFP_KERNEL);
137 if (!padded_salt) {
138 err = -ENOMEM;
139 goto err_free;
140 }
141 memcpy(padded_salt, salt, salt_size);
142
143 sg_init_one(&sg, padded_salt, padded_salt_size);
144 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP |
145 CRYPTO_TFM_REQ_MAY_BACKLOG,
146 crypto_req_done, &wait);
147 ahash_request_set_crypt(req, &sg, NULL, padded_salt_size);
148
149 err = crypto_wait_req(crypto_ahash_init(req), &wait);
150 if (err)
151 goto err_free;
152
153 err = crypto_wait_req(crypto_ahash_update(req), &wait);
154 if (err)
155 goto err_free;
156
157 err = crypto_ahash_export(req, hashstate);
158 if (err)
159 goto err_free;
160 out:
161 ahash_request_free(req);
162 kfree(padded_salt);
163 return hashstate;
164
165 err_free:
166 kfree(hashstate);
167 hashstate = ERR_PTR(err);
168 goto out;
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184 int fsverity_hash_page(const struct merkle_tree_params *params,
185 const struct inode *inode,
186 struct ahash_request *req, struct page *page, u8 *out)
187 {
188 struct scatterlist sg;
189 DECLARE_CRYPTO_WAIT(wait);
190 int err;
191
192 if (WARN_ON(params->block_size != PAGE_SIZE))
193 return -EINVAL;
194
195 sg_init_table(&sg, 1);
196 sg_set_page(&sg, page, PAGE_SIZE, 0);
197 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP |
198 CRYPTO_TFM_REQ_MAY_BACKLOG,
199 crypto_req_done, &wait);
200 ahash_request_set_crypt(req, &sg, out, PAGE_SIZE);
201
202 if (params->hashstate) {
203 err = crypto_ahash_import(req, params->hashstate);
204 if (err) {
205 fsverity_err(inode,
206 "Error %d importing hash state", err);
207 return err;
208 }
209 err = crypto_ahash_finup(req);
210 } else {
211 err = crypto_ahash_digest(req);
212 }
213
214 err = crypto_wait_req(err, &wait);
215 if (err)
216 fsverity_err(inode, "Error %d computing page hash", err);
217 return err;
218 }
219
220
221
222
223
224
225
226
227
228
229
230
231
232 int fsverity_hash_buffer(const struct fsverity_hash_alg *alg,
233 const void *data, size_t size, u8 *out)
234 {
235 struct ahash_request *req;
236 struct scatterlist sg;
237 DECLARE_CRYPTO_WAIT(wait);
238 int err;
239
240 req = ahash_request_alloc(alg->tfm, GFP_KERNEL);
241 if (!req)
242 return -ENOMEM;
243
244 sg_init_one(&sg, data, size);
245 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP |
246 CRYPTO_TFM_REQ_MAY_BACKLOG,
247 crypto_req_done, &wait);
248 ahash_request_set_crypt(req, &sg, out, size);
249
250 err = crypto_wait_req(crypto_ahash_digest(req), &wait);
251
252 ahash_request_free(req);
253 return err;
254 }
255
256 void __init fsverity_check_hash_algs(void)
257 {
258 size_t i;
259
260
261
262
263
264 for (i = 0; i < ARRAY_SIZE(fsverity_hash_algs); i++) {
265 const struct fsverity_hash_alg *alg = &fsverity_hash_algs[i];
266
267 if (!alg->name)
268 continue;
269
270 BUG_ON(alg->digest_size > FS_VERITY_MAX_DIGEST_SIZE);
271
272
273
274
275
276
277 BUG_ON(!is_power_of_2(alg->digest_size));
278 BUG_ON(!is_power_of_2(alg->block_size));
279 }
280 }