1 // SPDX-License-Identifier: GPL-2.0
3 * fs/verity/hash_algs.c: fs-verity hash algorithms
5 * Copyright 2019 Google LLC
8 #include "fsverity_private.h"
10 #include <crypto/hash.h>
11 #include <linux/scatterlist.h>
13 /* The hash algorithms supported by fs-verity */
14 struct fsverity_hash_alg fsverity_hash_algs
[] = {
15 [FS_VERITY_HASH_ALG_SHA256
] = {
17 .digest_size
= SHA256_DIGEST_SIZE
,
18 .block_size
= SHA256_BLOCK_SIZE
,
20 [FS_VERITY_HASH_ALG_SHA512
] = {
22 .digest_size
= SHA512_DIGEST_SIZE
,
23 .block_size
= SHA512_BLOCK_SIZE
,
28 * fsverity_get_hash_alg() - validate and prepare a hash algorithm
29 * @inode: optional inode for logging purposes
30 * @num: the hash algorithm number
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/.
37 * Return: pointer to the hash alg on success, else an ERR_PTR()
39 const struct fsverity_hash_alg
*fsverity_get_hash_alg(const struct inode
*inode
,
42 struct fsverity_hash_alg
*alg
;
43 struct crypto_ahash
*tfm
;
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
);
51 alg
= &fsverity_hash_algs
[num
];
53 /* pairs with cmpxchg() below */
54 tfm
= READ_ONCE(alg
->tfm
);
55 if (likely(tfm
!= NULL
))
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.
61 tfm
= crypto_alloc_ahash(alg
->name
, 0, 0);
63 if (PTR_ERR(tfm
) == -ENOENT
) {
65 "Missing crypto API support for hash algorithm \"%s\"",
67 return ERR_PTR(-ENOPKG
);
70 "Error allocating hash algorithm \"%s\": %ld",
71 alg
->name
, PTR_ERR(tfm
));
76 if (WARN_ON(alg
->digest_size
!= crypto_ahash_digestsize(tfm
)))
78 if (WARN_ON(alg
->block_size
!= crypto_ahash_blocksize(tfm
)))
81 pr_info("%s using implementation \"%s\"\n",
82 alg
->name
, crypto_ahash_driver_name(tfm
));
84 /* pairs with READ_ONCE() above */
85 if (cmpxchg(&alg
->tfm
, NULL
, tfm
) != NULL
)
86 crypto_free_ahash(tfm
);
91 crypto_free_ahash(tfm
);
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
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.
104 const u8
*fsverity_prepare_hash_state(const struct fsverity_hash_alg
*alg
,
105 const u8
*salt
, size_t salt_size
)
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
);
118 hashstate
= kmalloc(crypto_ahash_statesize(alg
->tfm
), GFP_KERNEL
);
120 return ERR_PTR(-ENOMEM
);
122 req
= ahash_request_alloc(alg
->tfm
, GFP_KERNEL
);
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.
135 padded_salt_size
= round_up(salt_size
, alg
->block_size
);
136 padded_salt
= kzalloc(padded_salt_size
, GFP_KERNEL
);
141 memcpy(padded_salt
, salt
, salt_size
);
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
);
149 err
= crypto_wait_req(crypto_ahash_init(req
), &wait
);
153 err
= crypto_wait_req(crypto_ahash_update(req
), &wait
);
157 err
= crypto_ahash_export(req
, hashstate
);
161 ahash_request_free(req
);
167 hashstate
= ERR_PTR(err
);
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
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.
182 * Return: 0 on success, -errno on failure
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
)
188 struct scatterlist sg
;
189 DECLARE_CRYPTO_WAIT(wait
);
192 if (WARN_ON(params
->block_size
!= PAGE_SIZE
))
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
);
202 if (params
->hashstate
) {
203 err
= crypto_ahash_import(req
, params
->hashstate
);
206 "Error %d importing hash state", err
);
209 err
= crypto_ahash_finup(req
);
211 err
= crypto_ahash_digest(req
);
214 err
= crypto_wait_req(err
, &wait
);
216 fsverity_err(inode
, "Error %d computing page hash", err
);
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
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.
230 * Return: 0 on success, -errno on failure
232 int fsverity_hash_buffer(const struct fsverity_hash_alg
*alg
,
233 const void *data
, size_t size
, u8
*out
)
235 struct ahash_request
*req
;
236 struct scatterlist sg
;
237 DECLARE_CRYPTO_WAIT(wait
);
240 req
= ahash_request_alloc(alg
->tfm
, GFP_KERNEL
);
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
);
250 err
= crypto_wait_req(crypto_ahash_digest(req
), &wait
);
252 ahash_request_free(req
);
256 void __init
fsverity_check_hash_algs(void)
261 * Sanity check the hash algorithms (could be a build-time check, but
262 * they're in an array)
264 for (i
= 0; i
< ARRAY_SIZE(fsverity_hash_algs
); i
++) {
265 const struct fsverity_hash_alg
*alg
= &fsverity_hash_algs
[i
];
270 BUG_ON(alg
->digest_size
> FS_VERITY_MAX_DIGEST_SIZE
);
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.
277 BUG_ON(!is_power_of_2(alg
->digest_size
));
278 BUG_ON(!is_power_of_2(alg
->block_size
));