hash_algs.c 9.2 KB

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