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