File Index
Files and directories
Sign in
FILE

hash_algs.c

File preview with syntax highlighting.

linux-6.19.14/fs/verity/hash_algs.c C
  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/* The hash algorithms supported by fs-verity */
 11const struct fsverity_hash_alg fsverity_hash_algs[] = {
 12	[FS_VERITY_HASH_ALG_SHA256] = {
 13		.name = "sha256",
 14		.digest_size = SHA256_DIGEST_SIZE,
 15		.block_size = SHA256_BLOCK_SIZE,
 16		.algo_id = HASH_ALGO_SHA256,
 17	},
 18	[FS_VERITY_HASH_ALG_SHA512] = {
 19		.name = "sha512",
 20		.digest_size = SHA512_DIGEST_SIZE,
 21		.block_size = SHA512_BLOCK_SIZE,
 22		.algo_id = HASH_ALGO_SHA512,
 23	},
 24};
 25
 26/**
 27 * fsverity_get_hash_alg() - get a hash algorithm by number
 28 * @inode: optional inode for logging purposes
 29 * @num: the hash algorithm number
 30 *
 31 * Get the struct fsverity_hash_alg for the given hash algorithm number.
 32 *
 33 * Return: pointer to the hash alg if it's known, otherwise NULL.
 34 */
 35const struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
 36						      unsigned int num)
 37{
 38	if (num >= ARRAY_SIZE(fsverity_hash_algs) ||
 39	    !fsverity_hash_algs[num].name) {
 40		fsverity_warn(inode, "Unknown hash algorithm number: %u", num);
 41		return NULL;
 42	}
 43	return &fsverity_hash_algs[num];
 44}
 45
 46/**
 47 * fsverity_prepare_hash_state() - precompute the initial hash state
 48 * @alg: hash algorithm
 49 * @salt: a salt which is to be prepended to all data to be hashed
 50 * @salt_size: salt size in bytes
 51 *
 52 * Return: the kmalloc()'ed initial hash state, or NULL if out of memory.
 53 */
 54union fsverity_hash_ctx *
 55fsverity_prepare_hash_state(const struct fsverity_hash_alg *alg,
 56			    const u8 *salt, size_t salt_size)
 57{
 58	u8 *padded_salt = NULL;
 59	size_t padded_salt_size;
 60	union fsverity_hash_ctx ctx;
 61	void *res = NULL;
 62
 63	/*
 64	 * Zero-pad the salt to the next multiple of the input size of the hash
 65	 * algorithm's compression function, e.g. 64 bytes for SHA-256 or 128
 66	 * bytes for SHA-512.  This ensures that the hash algorithm won't have
 67	 * any bytes buffered internally after processing the salt, thus making
 68	 * salted hashing just as fast as unsalted hashing.
 69	 */
 70	padded_salt_size = round_up(salt_size, alg->block_size);
 71	padded_salt = kzalloc(padded_salt_size, GFP_KERNEL);
 72	if (!padded_salt)
 73		return NULL;
 74	memcpy(padded_salt, salt, salt_size);
 75
 76	switch (alg->algo_id) {
 77	case HASH_ALGO_SHA256:
 78		sha256_init(&ctx.sha256);
 79		sha256_update(&ctx.sha256, padded_salt, padded_salt_size);
 80		res = kmemdup(&ctx.sha256, sizeof(ctx.sha256), GFP_KERNEL);
 81		break;
 82	case HASH_ALGO_SHA512:
 83		sha512_init(&ctx.sha512);
 84		sha512_update(&ctx.sha512, padded_salt, padded_salt_size);
 85		res = kmemdup(&ctx.sha512, sizeof(ctx.sha512), GFP_KERNEL);
 86		break;
 87	default:
 88		WARN_ON_ONCE(1);
 89	}
 90	kfree(padded_salt);
 91	return res;
 92}
 93
 94/**
 95 * fsverity_hash_block() - hash a single data or hash block
 96 * @params: the Merkle tree's parameters
 97 * @data: virtual address of a buffer containing the block to hash
 98 * @out: output digest, size 'params->digest_size' bytes
 99 *
100 * Hash a single data or hash block.  The hash is salted if a salt is specified
101 * in the Merkle tree parameters.
102 */
103void fsverity_hash_block(const struct merkle_tree_params *params,
104			 const void *data, u8 *out)
105{
106	union fsverity_hash_ctx ctx;
107
108	if (!params->hashstate) {
109		fsverity_hash_buffer(params->hash_alg, data, params->block_size,
110				     out);
111		return;
112	}
113
114	switch (params->hash_alg->algo_id) {
115	case HASH_ALGO_SHA256:
116		ctx.sha256 = params->hashstate->sha256;
117		sha256_update(&ctx.sha256, data, params->block_size);
118		sha256_final(&ctx.sha256, out);
119		return;
120	case HASH_ALGO_SHA512:
121		ctx.sha512 = params->hashstate->sha512;
122		sha512_update(&ctx.sha512, data, params->block_size);
123		sha512_final(&ctx.sha512, out);
124		return;
125	default:
126		BUG();
127	}
128}
129
130/**
131 * fsverity_hash_buffer() - hash some data
132 * @alg: the hash algorithm to use
133 * @data: the data to hash
134 * @size: size of data to hash, in bytes
135 * @out: output digest, size 'alg->digest_size' bytes
136 */
137void fsverity_hash_buffer(const struct fsverity_hash_alg *alg,
138			  const void *data, size_t size, u8 *out)
139{
140	switch (alg->algo_id) {
141	case HASH_ALGO_SHA256:
142		sha256(data, size, out);
143		return;
144	case HASH_ALGO_SHA512:
145		sha512(data, size, out);
146		return;
147	default:
148		BUG();
149	}
150}
151
152void __init fsverity_check_hash_algs(void)
153{
154	size_t i;
155
156	/*
157	 * Sanity check the hash algorithms (could be a build-time check, but
158	 * they're in an array)
159	 */
160	for (i = 0; i < ARRAY_SIZE(fsverity_hash_algs); i++) {
161		const struct fsverity_hash_alg *alg = &fsverity_hash_algs[i];
162
163		if (!alg->name)
164			continue;
165
166		/*
167		 * 0 must never be allocated as an FS_VERITY_HASH_ALG_* value,
168		 * as it is reserved for users that use 0 to mean unspecified or
169		 * a default value.  fs/verity/ itself doesn't care and doesn't
170		 * have a default algorithm, but some users make use of this.
171		 */
172		BUG_ON(i == 0);
173
174		BUG_ON(alg->digest_size > FS_VERITY_MAX_DIGEST_SIZE);
175
176		/*
177		 * For efficiency, the implementation currently assumes the
178		 * digest and block sizes are powers of 2.  This limitation can
179		 * be lifted if the code is updated to handle other values.
180		 */
181		BUG_ON(!is_power_of_2(alg->digest_size));
182		BUG_ON(!is_power_of_2(alg->block_size));
183
184		/* Verify that there is a valid mapping to HASH_ALGO_*. */
185		BUG_ON(alg->algo_id == 0);
186		BUG_ON(alg->digest_size != hash_digest_size[alg->algo_id]);
187	}
188}
~/ Read-only file preview.