1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fs-verity hash algorithms
4  *
5  * Copyright 2019 Google LLC
6  */
7 
8 #include "fsverity_private.h"
9 
10 #include <crypto/hash.h>
11 
12 /* The hash algorithms supported by fs-verity */
13 struct fsverity_hash_alg fsverity_hash_algs[] = {
14 	[FS_VERITY_HASH_ALG_SHA256] = {
15 		.name = "sha256",
16 		.digest_size = SHA256_DIGEST_SIZE,
17 		.block_size = SHA256_BLOCK_SIZE,
18 		.algo_id = HASH_ALGO_SHA256,
19 	},
20 	[FS_VERITY_HASH_ALG_SHA512] = {
21 		.name = "sha512",
22 		.digest_size = SHA512_DIGEST_SIZE,
23 		.block_size = SHA512_BLOCK_SIZE,
24 		.algo_id = HASH_ALGO_SHA512,
25 	},
26 };
27 
28 static DEFINE_MUTEX(fsverity_hash_alg_init_mutex);
29 
30 /**
31  * fsverity_get_hash_alg() - validate and prepare a hash algorithm
32  * @inode: optional inode for logging purposes
33  * @num: the hash algorithm number
34  *
35  * Get the struct fsverity_hash_alg for the given hash algorithm number, and
36  * ensure it has a hash transform ready to go.  The hash transforms are
37  * allocated on-demand so that we don't waste resources unnecessarily, and
38  * because the crypto modules may be initialized later than fs/verity/.
39  *
40  * Return: pointer to the hash alg on success, else an ERR_PTR()
41  */
fsverity_get_hash_alg(const struct inode * inode,unsigned int num)42 const struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
43 						      unsigned int num)
44 {
45 	struct fsverity_hash_alg *alg;
46 	struct crypto_shash *tfm;
47 	int err;
48 
49 	if (num >= ARRAY_SIZE(fsverity_hash_algs) ||
50 	    !fsverity_hash_algs[num].name) {
51 		fsverity_warn(inode, "Unknown hash algorithm number: %u", num);
52 		return ERR_PTR(-EINVAL);
53 	}
54 	alg = &fsverity_hash_algs[num];
55 
56 	/* pairs with smp_store_release() below */
57 	if (likely(smp_load_acquire(&alg->tfm) != NULL))
58 		return alg;
59 
60 	mutex_lock(&fsverity_hash_alg_init_mutex);
61 
62 	if (alg->tfm != NULL)
63 		goto out_unlock;
64 
65 	tfm = crypto_alloc_shash(alg->name, 0, 0);
66 	if (IS_ERR(tfm)) {
67 		if (PTR_ERR(tfm) == -ENOENT) {
68 			fsverity_warn(inode,
69 				      "Missing crypto API support for hash algorithm \"%s\"",
70 				      alg->name);
71 			alg = ERR_PTR(-ENOPKG);
72 			goto out_unlock;
73 		}
74 		fsverity_err(inode,
75 			     "Error allocating hash algorithm \"%s\": %ld",
76 			     alg->name, PTR_ERR(tfm));
77 		alg = ERR_CAST(tfm);
78 		goto out_unlock;
79 	}
80 
81 	err = -EINVAL;
82 	if (WARN_ON_ONCE(alg->digest_size != crypto_shash_digestsize(tfm)))
83 		goto err_free_tfm;
84 	if (WARN_ON_ONCE(alg->block_size != crypto_shash_blocksize(tfm)))
85 		goto err_free_tfm;
86 
87 	alg->mb_max_msgs = min(crypto_shash_mb_max_msgs(tfm),
88 			       FS_VERITY_MAX_PENDING_DATA_BLOCKS);
89 
90 	pr_info("%s using implementation \"%s\"%s\n",
91 		alg->name, crypto_shash_driver_name(tfm),
92 		alg->mb_max_msgs > 1 ? " (multibuffer)" : "");
93 
94 	/* pairs with smp_load_acquire() above */
95 	smp_store_release(&alg->tfm, tfm);
96 	goto out_unlock;
97 
98 err_free_tfm:
99 	crypto_free_shash(tfm);
100 	alg = ERR_PTR(err);
101 out_unlock:
102 	mutex_unlock(&fsverity_hash_alg_init_mutex);
103 	return alg;
104 }
105 
106 /**
107  * fsverity_prepare_hash_state() - precompute the initial hash state
108  * @alg: hash algorithm
109  * @salt: a salt which is to be prepended to all data to be hashed
110  * @salt_size: salt size in bytes, possibly 0
111  *
112  * Return: NULL if the salt is empty, otherwise the kmalloc()'ed precomputed
113  *	   initial hash state on success or an ERR_PTR() on failure.
114  */
fsverity_prepare_hash_state(const struct fsverity_hash_alg * alg,const u8 * salt,size_t salt_size)115 const u8 *fsverity_prepare_hash_state(const struct fsverity_hash_alg *alg,
116 				      const u8 *salt, size_t salt_size)
117 {
118 	u8 *hashstate = NULL;
119 	SHASH_DESC_ON_STACK(desc, alg->tfm);
120 	u8 *padded_salt = NULL;
121 	size_t padded_salt_size;
122 	int err;
123 
124 	desc->tfm = alg->tfm;
125 
126 	if (salt_size == 0)
127 		return NULL;
128 
129 	hashstate = kmalloc(crypto_shash_statesize(alg->tfm), GFP_KERNEL);
130 	if (!hashstate)
131 		return ERR_PTR(-ENOMEM);
132 
133 	/*
134 	 * Zero-pad the salt to the next multiple of the input size of the hash
135 	 * algorithm's compression function, e.g. 64 bytes for SHA-256 or 128
136 	 * bytes for SHA-512.  This ensures that the hash algorithm won't have
137 	 * any bytes buffered internally after processing the salt, thus making
138 	 * salted hashing just as fast as unsalted hashing.
139 	 */
140 	padded_salt_size = round_up(salt_size, alg->block_size);
141 	padded_salt = kzalloc(padded_salt_size, GFP_KERNEL);
142 	if (!padded_salt) {
143 		err = -ENOMEM;
144 		goto err_free;
145 	}
146 	memcpy(padded_salt, salt, salt_size);
147 	err = crypto_shash_init(desc);
148 	if (err)
149 		goto err_free;
150 
151 	err = crypto_shash_update(desc, padded_salt, padded_salt_size);
152 	if (err)
153 		goto err_free;
154 
155 	err = crypto_shash_export(desc, hashstate);
156 	if (err)
157 		goto err_free;
158 out:
159 	kfree(padded_salt);
160 	return hashstate;
161 
162 err_free:
163 	kfree(hashstate);
164 	hashstate = ERR_PTR(err);
165 	goto out;
166 }
167 
168 /**
169  * fsverity_hash_block() - hash a single data or hash block
170  * @params: the Merkle tree's parameters
171  * @inode: inode for which the hashing is being done
172  * @data: virtual address of a buffer containing the block to hash
173  * @out: output digest, size 'params->digest_size' bytes
174  *
175  * Hash a single data or hash block.  The hash is salted if a salt is specified
176  * in the Merkle tree parameters.
177  *
178  * Return: 0 on success, -errno on failure
179  */
fsverity_hash_block(const struct merkle_tree_params * params,const struct inode * inode,const void * data,u8 * out)180 int fsverity_hash_block(const struct merkle_tree_params *params,
181 			const struct inode *inode, const void *data, u8 *out)
182 {
183 	SHASH_DESC_ON_STACK(desc, params->hash_alg->tfm);
184 	int err;
185 
186 	desc->tfm = params->hash_alg->tfm;
187 
188 	if (params->hashstate) {
189 		err = crypto_shash_import(desc, params->hashstate);
190 		if (err) {
191 			fsverity_err(inode,
192 				     "Error %d importing hash state", err);
193 			return err;
194 		}
195 		err = crypto_shash_finup(desc, data, params->block_size, out);
196 	} else {
197 		err = crypto_shash_digest(desc, data, params->block_size, out);
198 	}
199 	if (err)
200 		fsverity_err(inode, "Error %d computing block hash", err);
201 	return err;
202 }
203 
204 /**
205  * fsverity_hash_buffer() - hash some data
206  * @alg: the hash algorithm to use
207  * @data: the data to hash
208  * @size: size of data to hash, in bytes
209  * @out: output digest, size 'alg->digest_size' bytes
210  *
211  * Return: 0 on success, -errno on failure
212  */
fsverity_hash_buffer(const struct fsverity_hash_alg * alg,const void * data,size_t size,u8 * out)213 int fsverity_hash_buffer(const struct fsverity_hash_alg *alg,
214 			 const void *data, size_t size, u8 *out)
215 {
216 	return crypto_shash_tfm_digest(alg->tfm, data, size, out);
217 }
218 
fsverity_check_hash_algs(void)219 void __init fsverity_check_hash_algs(void)
220 {
221 	size_t i;
222 
223 	/*
224 	 * Sanity check the hash algorithms (could be a build-time check, but
225 	 * they're in an array)
226 	 */
227 	for (i = 0; i < ARRAY_SIZE(fsverity_hash_algs); i++) {
228 		const struct fsverity_hash_alg *alg = &fsverity_hash_algs[i];
229 
230 		if (!alg->name)
231 			continue;
232 
233 		/*
234 		 * 0 must never be allocated as an FS_VERITY_HASH_ALG_* value,
235 		 * as it is reserved for users that use 0 to mean unspecified or
236 		 * a default value.  fs/verity/ itself doesn't care and doesn't
237 		 * have a default algorithm, but some users make use of this.
238 		 */
239 		BUG_ON(i == 0);
240 
241 		BUG_ON(alg->digest_size > FS_VERITY_MAX_DIGEST_SIZE);
242 
243 		/*
244 		 * For efficiency, the implementation currently assumes the
245 		 * digest and block sizes are powers of 2.  This limitation can
246 		 * be lifted if the code is updated to handle other values.
247 		 */
248 		BUG_ON(!is_power_of_2(alg->digest_size));
249 		BUG_ON(!is_power_of_2(alg->block_size));
250 
251 		/* Verify that there is a valid mapping to HASH_ALGO_*. */
252 		BUG_ON(alg->algo_id == 0);
253 		BUG_ON(alg->digest_size != hash_digest_size[alg->algo_id]);
254 	}
255 }
256