hash_algs.c 9.0 KB

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