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 */ 11 const 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 */ 35 const 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 */ 54 union fsverity_hash_ctx * 55 fsverity_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 * @inode: inode for which the hashing is being done 98 * @data: virtual address of a buffer containing the block to hash 99 * @out: output digest, size 'params->digest_size' bytes 100 * 101 * Hash a single data or hash block. The hash is salted if a salt is specified 102 * in the Merkle tree parameters. 103 */ 104 void fsverity_hash_block(const struct merkle_tree_params *params, 105 const struct inode *inode, const void *data, u8 *out) 106 { 107 union fsverity_hash_ctx ctx; 108 109 if (!params->hashstate) { 110 fsverity_hash_buffer(params->hash_alg, data, params->block_size, 111 out); 112 return; 113 } 114 115 switch (params->hash_alg->algo_id) { 116 case HASH_ALGO_SHA256: 117 ctx.sha256 = params->hashstate->sha256; 118 sha256_update(&ctx.sha256, data, params->block_size); 119 sha256_final(&ctx.sha256, out); 120 return; 121 case HASH_ALGO_SHA512: 122 ctx.sha512 = params->hashstate->sha512; 123 sha512_update(&ctx.sha512, data, params->block_size); 124 sha512_final(&ctx.sha512, out); 125 return; 126 default: 127 BUG(); 128 } 129 } 130 131 /** 132 * fsverity_hash_buffer() - hash some data 133 * @alg: the hash algorithm to use 134 * @data: the data to hash 135 * @size: size of data to hash, in bytes 136 * @out: output digest, size 'alg->digest_size' bytes 137 */ 138 void fsverity_hash_buffer(const struct fsverity_hash_alg *alg, 139 const void *data, size_t size, u8 *out) 140 { 141 switch (alg->algo_id) { 142 case HASH_ALGO_SHA256: 143 sha256(data, size, out); 144 return; 145 case HASH_ALGO_SHA512: 146 sha512(data, size, out); 147 return; 148 default: 149 BUG(); 150 } 151 } 152 153 void __init fsverity_check_hash_algs(void) 154 { 155 size_t i; 156 157 /* 158 * Sanity check the hash algorithms (could be a build-time check, but 159 * they're in an array) 160 */ 161 for (i = 0; i < ARRAY_SIZE(fsverity_hash_algs); i++) { 162 const struct fsverity_hash_alg *alg = &fsverity_hash_algs[i]; 163 164 if (!alg->name) 165 continue; 166 167 /* 168 * 0 must never be allocated as an FS_VERITY_HASH_ALG_* value, 169 * as it is reserved for users that use 0 to mean unspecified or 170 * a default value. fs/verity/ itself doesn't care and doesn't 171 * have a default algorithm, but some users make use of this. 172 */ 173 BUG_ON(i == 0); 174 175 BUG_ON(alg->digest_size > FS_VERITY_MAX_DIGEST_SIZE); 176 177 /* 178 * For efficiency, the implementation currently assumes the 179 * digest and block sizes are powers of 2. This limitation can 180 * be lifted if the code is updated to handle other values. 181 */ 182 BUG_ON(!is_power_of_2(alg->digest_size)); 183 BUG_ON(!is_power_of_2(alg->block_size)); 184 185 /* Verify that there is a valid mapping to HASH_ALGO_*. */ 186 BUG_ON(alg->algo_id == 0); 187 BUG_ON(alg->digest_size != hash_digest_size[alg->algo_id]); 188 } 189 } 190