xref: /openbmc/linux/fs/verity/hash_algs.c (revision 81de3bf3)
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 	[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  * fsverity_get_hash_alg() - validate and prepare a hash algorithm
29  * @inode: optional inode for logging purposes
30  * @num: the hash algorithm number
31  *
32  * Get the struct fsverity_hash_alg for the given hash algorithm number, and
33  * ensure it has a hash transform ready to go.  The hash transforms are
34  * allocated on-demand so that we don't waste resources unnecessarily, and
35  * because the crypto modules may be initialized later than fs/verity/.
36  *
37  * Return: pointer to the hash alg on success, else an ERR_PTR()
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 	/* pairs with cmpxchg() below */
54 	tfm = READ_ONCE(alg->tfm);
55 	if (likely(tfm != NULL))
56 		return alg;
57 	/*
58 	 * Using the shash API would make things a bit simpler, but the ahash
59 	 * API is preferable as it allows the use of crypto accelerators.
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 	/* pairs with READ_ONCE() above */
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  * fsverity_prepare_hash_state() - precompute the initial hash state
97  * @alg: hash algorithm
98  * @salt: a salt which is to be prepended to all data to be hashed
99  * @salt_size: salt size in bytes, possibly 0
100  *
101  * Return: NULL if the salt is empty, otherwise the kmalloc()'ed precomputed
102  *	   initial hash state on success or an ERR_PTR() on failure.
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 	 * Zero-pad the salt to the next multiple of the input size of the hash
130 	 * algorithm's compression function, e.g. 64 bytes for SHA-256 or 128
131 	 * bytes for SHA-512.  This ensures that the hash algorithm won't have
132 	 * any bytes buffered internally after processing the salt, thus making
133 	 * salted hashing just as fast as unsalted hashing.
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  * fsverity_hash_page() - hash a single data or hash page
173  * @params: the Merkle tree's parameters
174  * @inode: inode for which the hashing is being done
175  * @req: preallocated hash request
176  * @page: the page to hash
177  * @out: output digest, size 'params->digest_size' bytes
178  *
179  * Hash a single data or hash block, assuming block_size == PAGE_SIZE.
180  * The hash is salted if a salt is specified in the Merkle tree parameters.
181  *
182  * Return: 0 on success, -errno on failure
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  * fsverity_hash_buffer() - hash some data
222  * @alg: the hash algorithm to use
223  * @data: the data to hash
224  * @size: size of data to hash, in bytes
225  * @out: output digest, size 'alg->digest_size' bytes
226  *
227  * Hash some data which is located in physically contiguous memory (i.e. memory
228  * allocated by kmalloc(), not by vmalloc()).  No salt is used.
229  *
230  * Return: 0 on success, -errno on failure
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 	 * Sanity check the hash algorithms (could be a build-time check, but
262 	 * they're in an array)
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 		 * For efficiency, the implementation currently assumes the
274 		 * digest and block sizes are powers of 2.  This limitation can
275 		 * be lifted if the code is updated to handle other values.
276 		 */
277 		BUG_ON(!is_power_of_2(alg->digest_size));
278 		BUG_ON(!is_power_of_2(alg->block_size));
279 	}
280 }
281