]>
Commit | Line | Data |
---|---|---|
671e67b4 EB |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * fs/verity/hash_algs.c: fs-verity hash algorithms | |
4 | * | |
5 | * Copyright 2019 Google LLC | |
6 | */ | |
7 | ||
8 | #include "fsverity_private.h" | |
9 | ||
10 | #include <crypto/hash.h> | |
11 | #include <linux/scatterlist.h> | |
12 | ||
13 | /* The hash algorithms supported by fs-verity */ | |
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 | }; | |
21 | ||
22 | /** | |
23 | * fsverity_get_hash_alg() - validate and prepare a hash algorithm | |
24 | * @inode: optional inode for logging purposes | |
25 | * @num: the hash algorithm number | |
26 | * | |
27 | * Get the struct fsverity_hash_alg for the given hash algorithm number, and | |
28 | * ensure it has a hash transform ready to go. The hash transforms are | |
29 | * allocated on-demand so that we don't waste resources unnecessarily, and | |
30 | * because the crypto modules may be initialized later than fs/verity/. | |
31 | * | |
32 | * Return: pointer to the hash alg on success, else an ERR_PTR() | |
33 | */ | |
34 | const struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode, | |
35 | unsigned int num) | |
36 | { | |
37 | struct fsverity_hash_alg *alg; | |
38 | struct crypto_ahash *tfm; | |
39 | int err; | |
40 | ||
41 | if (num >= ARRAY_SIZE(fsverity_hash_algs) || | |
42 | !fsverity_hash_algs[num].name) { | |
43 | fsverity_warn(inode, "Unknown hash algorithm number: %u", num); | |
44 | return ERR_PTR(-EINVAL); | |
45 | } | |
46 | alg = &fsverity_hash_algs[num]; | |
47 | ||
48 | /* pairs with cmpxchg() below */ | |
49 | tfm = READ_ONCE(alg->tfm); | |
50 | if (likely(tfm != NULL)) | |
51 | return alg; | |
52 | /* | |
53 | * Using the shash API would make things a bit simpler, but the ahash | |
54 | * API is preferable as it allows the use of crypto accelerators. | |
55 | */ | |
56 | tfm = crypto_alloc_ahash(alg->name, 0, 0); | |
57 | if (IS_ERR(tfm)) { | |
58 | if (PTR_ERR(tfm) == -ENOENT) { | |
59 | fsverity_warn(inode, | |
60 | "Missing crypto API support for hash algorithm \"%s\"", | |
61 | alg->name); | |
62 | return ERR_PTR(-ENOPKG); | |
63 | } | |
64 | fsverity_err(inode, | |
65 | "Error allocating hash algorithm \"%s\": %ld", | |
66 | alg->name, PTR_ERR(tfm)); | |
67 | return ERR_CAST(tfm); | |
68 | } | |
69 | ||
70 | err = -EINVAL; | |
71 | if (WARN_ON(alg->digest_size != crypto_ahash_digestsize(tfm))) | |
72 | goto err_free_tfm; | |
73 | if (WARN_ON(alg->block_size != crypto_ahash_blocksize(tfm))) | |
74 | goto err_free_tfm; | |
75 | ||
76 | pr_info("%s using implementation \"%s\"\n", | |
77 | alg->name, crypto_ahash_driver_name(tfm)); | |
78 | ||
79 | /* pairs with READ_ONCE() above */ | |
80 | if (cmpxchg(&alg->tfm, NULL, tfm) != NULL) | |
81 | crypto_free_ahash(tfm); | |
82 | ||
83 | return alg; | |
84 | ||
85 | err_free_tfm: | |
86 | crypto_free_ahash(tfm); | |
87 | return ERR_PTR(err); | |
88 | } | |
89 | ||
90 | /** | |
91 | * fsverity_prepare_hash_state() - precompute the initial hash state | |
92 | * @alg: hash algorithm | |
93 | * @salt: a salt which is to be prepended to all data to be hashed | |
94 | * @salt_size: salt size in bytes, possibly 0 | |
95 | * | |
96 | * Return: NULL if the salt is empty, otherwise the kmalloc()'ed precomputed | |
97 | * initial hash state on success or an ERR_PTR() on failure. | |
98 | */ | |
99 | const u8 *fsverity_prepare_hash_state(const struct fsverity_hash_alg *alg, | |
100 | const u8 *salt, size_t salt_size) | |
101 | { | |
102 | u8 *hashstate = NULL; | |
103 | struct ahash_request *req = NULL; | |
104 | u8 *padded_salt = NULL; | |
105 | size_t padded_salt_size; | |
106 | struct scatterlist sg; | |
107 | DECLARE_CRYPTO_WAIT(wait); | |
108 | int err; | |
109 | ||
110 | if (salt_size == 0) | |
111 | return NULL; | |
112 | ||
113 | hashstate = kmalloc(crypto_ahash_statesize(alg->tfm), GFP_KERNEL); | |
114 | if (!hashstate) | |
115 | return ERR_PTR(-ENOMEM); | |
116 | ||
117 | req = ahash_request_alloc(alg->tfm, GFP_KERNEL); | |
118 | if (!req) { | |
119 | err = -ENOMEM; | |
120 | goto err_free; | |
121 | } | |
122 | ||
123 | /* | |
124 | * Zero-pad the salt to the next multiple of the input size of the hash | |
125 | * algorithm's compression function, e.g. 64 bytes for SHA-256 or 128 | |
126 | * bytes for SHA-512. This ensures that the hash algorithm won't have | |
127 | * any bytes buffered internally after processing the salt, thus making | |
128 | * salted hashing just as fast as unsalted hashing. | |
129 | */ | |
130 | padded_salt_size = round_up(salt_size, alg->block_size); | |
131 | padded_salt = kzalloc(padded_salt_size, GFP_KERNEL); | |
132 | if (!padded_salt) { | |
133 | err = -ENOMEM; | |
134 | goto err_free; | |
135 | } | |
136 | memcpy(padded_salt, salt, salt_size); | |
137 | ||
138 | sg_init_one(&sg, padded_salt, padded_salt_size); | |
139 | ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP | | |
140 | CRYPTO_TFM_REQ_MAY_BACKLOG, | |
141 | crypto_req_done, &wait); | |
142 | ahash_request_set_crypt(req, &sg, NULL, padded_salt_size); | |
143 | ||
144 | err = crypto_wait_req(crypto_ahash_init(req), &wait); | |
145 | if (err) | |
146 | goto err_free; | |
147 | ||
148 | err = crypto_wait_req(crypto_ahash_update(req), &wait); | |
149 | if (err) | |
150 | goto err_free; | |
151 | ||
152 | err = crypto_ahash_export(req, hashstate); | |
153 | if (err) | |
154 | goto err_free; | |
155 | out: | |
156 | ahash_request_free(req); | |
157 | kfree(padded_salt); | |
158 | return hashstate; | |
159 | ||
160 | err_free: | |
161 | kfree(hashstate); | |
162 | hashstate = ERR_PTR(err); | |
163 | goto out; | |
164 | } | |
165 | ||
166 | /** | |
167 | * fsverity_hash_page() - hash a single data or hash page | |
168 | * @params: the Merkle tree's parameters | |
169 | * @inode: inode for which the hashing is being done | |
170 | * @req: preallocated hash request | |
171 | * @page: the page to hash | |
172 | * @out: output digest, size 'params->digest_size' bytes | |
173 | * | |
174 | * Hash a single data or hash block, assuming block_size == PAGE_SIZE. | |
175 | * The hash is salted if a salt is specified in the Merkle tree parameters. | |
176 | * | |
177 | * Return: 0 on success, -errno on failure | |
178 | */ | |
179 | int fsverity_hash_page(const struct merkle_tree_params *params, | |
180 | const struct inode *inode, | |
181 | struct ahash_request *req, struct page *page, u8 *out) | |
182 | { | |
183 | struct scatterlist sg; | |
184 | DECLARE_CRYPTO_WAIT(wait); | |
185 | int err; | |
186 | ||
187 | if (WARN_ON(params->block_size != PAGE_SIZE)) | |
188 | return -EINVAL; | |
189 | ||
190 | sg_init_table(&sg, 1); | |
191 | sg_set_page(&sg, page, PAGE_SIZE, 0); | |
192 | ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP | | |
193 | CRYPTO_TFM_REQ_MAY_BACKLOG, | |
194 | crypto_req_done, &wait); | |
195 | ahash_request_set_crypt(req, &sg, out, PAGE_SIZE); | |
196 | ||
197 | if (params->hashstate) { | |
198 | err = crypto_ahash_import(req, params->hashstate); | |
199 | if (err) { | |
200 | fsverity_err(inode, | |
201 | "Error %d importing hash state", err); | |
202 | return err; | |
203 | } | |
204 | err = crypto_ahash_finup(req); | |
205 | } else { | |
206 | err = crypto_ahash_digest(req); | |
207 | } | |
208 | ||
209 | err = crypto_wait_req(err, &wait); | |
210 | if (err) | |
211 | fsverity_err(inode, "Error %d computing page hash", err); | |
212 | return err; | |
213 | } | |
214 | ||
215 | /** | |
216 | * fsverity_hash_buffer() - hash some data | |
217 | * @alg: the hash algorithm to use | |
218 | * @data: the data to hash | |
219 | * @size: size of data to hash, in bytes | |
220 | * @out: output digest, size 'alg->digest_size' bytes | |
221 | * | |
222 | * Hash some data which is located in physically contiguous memory (i.e. memory | |
223 | * allocated by kmalloc(), not by vmalloc()). No salt is used. | |
224 | * | |
225 | * Return: 0 on success, -errno on failure | |
226 | */ | |
227 | int fsverity_hash_buffer(const struct fsverity_hash_alg *alg, | |
228 | const void *data, size_t size, u8 *out) | |
229 | { | |
230 | struct ahash_request *req; | |
231 | struct scatterlist sg; | |
232 | DECLARE_CRYPTO_WAIT(wait); | |
233 | int err; | |
234 | ||
235 | req = ahash_request_alloc(alg->tfm, GFP_KERNEL); | |
236 | if (!req) | |
237 | return -ENOMEM; | |
238 | ||
239 | sg_init_one(&sg, data, size); | |
240 | ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP | | |
241 | CRYPTO_TFM_REQ_MAY_BACKLOG, | |
242 | crypto_req_done, &wait); | |
243 | ahash_request_set_crypt(req, &sg, out, size); | |
244 | ||
245 | err = crypto_wait_req(crypto_ahash_digest(req), &wait); | |
246 | ||
247 | ahash_request_free(req); | |
248 | return err; | |
249 | } | |
250 | ||
251 | void __init fsverity_check_hash_algs(void) | |
252 | { | |
253 | size_t i; | |
254 | ||
255 | /* | |
256 | * Sanity check the hash algorithms (could be a build-time check, but | |
257 | * they're in an array) | |
258 | */ | |
259 | for (i = 0; i < ARRAY_SIZE(fsverity_hash_algs); i++) { | |
260 | const struct fsverity_hash_alg *alg = &fsverity_hash_algs[i]; | |
261 | ||
262 | if (!alg->name) | |
263 | continue; | |
264 | ||
265 | BUG_ON(alg->digest_size > FS_VERITY_MAX_DIGEST_SIZE); | |
266 | ||
267 | /* | |
268 | * For efficiency, the implementation currently assumes the | |
269 | * digest and block sizes are powers of 2. This limitation can | |
270 | * be lifted if the code is updated to handle other values. | |
271 | */ | |
272 | BUG_ON(!is_power_of_2(alg->digest_size)); | |
273 | BUG_ON(!is_power_of_2(alg->block_size)); | |
274 | } | |
275 | } |