hash_algs.c 6.8 KB

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