shash.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Synchronous Cryptographic Hash operations.
  4. *
  5. * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
  6. */
  7. #include <crypto/scatterwalk.h>
  8. #include <linux/cryptouser.h>
  9. #include <linux/err.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/string.h>
  14. #include <net/netlink.h>
  15. #include "hash.h"
  16. int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
  17. unsigned int keylen)
  18. {
  19. return -ENOSYS;
  20. }
  21. EXPORT_SYMBOL_GPL(shash_no_setkey);
  22. static void shash_set_needkey(struct crypto_shash *tfm, struct shash_alg *alg)
  23. {
  24. if (crypto_shash_alg_needs_key(alg))
  25. crypto_shash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
  26. }
  27. int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
  28. unsigned int keylen)
  29. {
  30. struct shash_alg *shash = crypto_shash_alg(tfm);
  31. int err;
  32. err = shash->setkey(tfm, key, keylen);
  33. if (unlikely(err)) {
  34. shash_set_needkey(tfm, shash);
  35. return err;
  36. }
  37. crypto_shash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
  38. return 0;
  39. }
  40. EXPORT_SYMBOL_GPL(crypto_shash_setkey);
  41. int crypto_shash_update(struct shash_desc *desc, const u8 *data,
  42. unsigned int len)
  43. {
  44. return crypto_shash_alg(desc->tfm)->update(desc, data, len);
  45. }
  46. EXPORT_SYMBOL_GPL(crypto_shash_update);
  47. int crypto_shash_final(struct shash_desc *desc, u8 *out)
  48. {
  49. return crypto_shash_alg(desc->tfm)->final(desc, out);
  50. }
  51. EXPORT_SYMBOL_GPL(crypto_shash_final);
  52. static int shash_default_finup(struct shash_desc *desc, const u8 *data,
  53. unsigned int len, u8 *out)
  54. {
  55. struct shash_alg *shash = crypto_shash_alg(desc->tfm);
  56. return shash->update(desc, data, len) ?:
  57. shash->final(desc, out);
  58. }
  59. int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
  60. unsigned int len, u8 *out)
  61. {
  62. return crypto_shash_alg(desc->tfm)->finup(desc, data, len, out);
  63. }
  64. EXPORT_SYMBOL_GPL(crypto_shash_finup);
  65. static int shash_default_digest(struct shash_desc *desc, const u8 *data,
  66. unsigned int len, u8 *out)
  67. {
  68. struct shash_alg *shash = crypto_shash_alg(desc->tfm);
  69. return shash->init(desc) ?:
  70. shash->finup(desc, data, len, out);
  71. }
  72. int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
  73. unsigned int len, u8 *out)
  74. {
  75. struct crypto_shash *tfm = desc->tfm;
  76. if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  77. return -ENOKEY;
  78. return crypto_shash_alg(tfm)->digest(desc, data, len, out);
  79. }
  80. EXPORT_SYMBOL_GPL(crypto_shash_digest);
  81. int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data,
  82. unsigned int len, u8 *out)
  83. {
  84. SHASH_DESC_ON_STACK(desc, tfm);
  85. int err;
  86. desc->tfm = tfm;
  87. err = crypto_shash_digest(desc, data, len, out);
  88. shash_desc_zero(desc);
  89. return err;
  90. }
  91. EXPORT_SYMBOL_GPL(crypto_shash_tfm_digest);
  92. int crypto_shash_export(struct shash_desc *desc, void *out)
  93. {
  94. struct crypto_shash *tfm = desc->tfm;
  95. struct shash_alg *shash = crypto_shash_alg(tfm);
  96. if (shash->export)
  97. return shash->export(desc, out);
  98. memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(tfm));
  99. return 0;
  100. }
  101. EXPORT_SYMBOL_GPL(crypto_shash_export);
  102. int crypto_shash_import(struct shash_desc *desc, const void *in)
  103. {
  104. struct crypto_shash *tfm = desc->tfm;
  105. struct shash_alg *shash = crypto_shash_alg(tfm);
  106. if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  107. return -ENOKEY;
  108. if (shash->import)
  109. return shash->import(desc, in);
  110. memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(tfm));
  111. return 0;
  112. }
  113. EXPORT_SYMBOL_GPL(crypto_shash_import);
  114. static void crypto_shash_exit_tfm(struct crypto_tfm *tfm)
  115. {
  116. struct crypto_shash *hash = __crypto_shash_cast(tfm);
  117. struct shash_alg *alg = crypto_shash_alg(hash);
  118. alg->exit_tfm(hash);
  119. }
  120. static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
  121. {
  122. struct crypto_shash *hash = __crypto_shash_cast(tfm);
  123. struct shash_alg *alg = crypto_shash_alg(hash);
  124. int err;
  125. hash->descsize = alg->descsize;
  126. shash_set_needkey(hash, alg);
  127. if (alg->exit_tfm)
  128. tfm->exit = crypto_shash_exit_tfm;
  129. if (!alg->init_tfm)
  130. return 0;
  131. err = alg->init_tfm(hash);
  132. if (err)
  133. return err;
  134. /* ->init_tfm() may have increased the descsize. */
  135. if (WARN_ON_ONCE(hash->descsize > HASH_MAX_DESCSIZE)) {
  136. if (alg->exit_tfm)
  137. alg->exit_tfm(hash);
  138. return -EINVAL;
  139. }
  140. return 0;
  141. }
  142. static void crypto_shash_free_instance(struct crypto_instance *inst)
  143. {
  144. struct shash_instance *shash = shash_instance(inst);
  145. shash->free(shash);
  146. }
  147. static int __maybe_unused crypto_shash_report(
  148. struct sk_buff *skb, struct crypto_alg *alg)
  149. {
  150. struct crypto_report_hash rhash;
  151. struct shash_alg *salg = __crypto_shash_alg(alg);
  152. memset(&rhash, 0, sizeof(rhash));
  153. strscpy(rhash.type, "shash", sizeof(rhash.type));
  154. rhash.blocksize = alg->cra_blocksize;
  155. rhash.digestsize = salg->digestsize;
  156. return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash);
  157. }
  158. static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
  159. __maybe_unused;
  160. static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
  161. {
  162. struct shash_alg *salg = __crypto_shash_alg(alg);
  163. seq_printf(m, "type : shash\n");
  164. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  165. seq_printf(m, "digestsize : %u\n", salg->digestsize);
  166. }
  167. const struct crypto_type crypto_shash_type = {
  168. .extsize = crypto_alg_extsize,
  169. .init_tfm = crypto_shash_init_tfm,
  170. .free = crypto_shash_free_instance,
  171. #ifdef CONFIG_PROC_FS
  172. .show = crypto_shash_show,
  173. #endif
  174. #if IS_ENABLED(CONFIG_CRYPTO_USER)
  175. .report = crypto_shash_report,
  176. #endif
  177. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  178. .maskset = CRYPTO_ALG_TYPE_MASK,
  179. .type = CRYPTO_ALG_TYPE_SHASH,
  180. .tfmsize = offsetof(struct crypto_shash, base),
  181. };
  182. int crypto_grab_shash(struct crypto_shash_spawn *spawn,
  183. struct crypto_instance *inst,
  184. const char *name, u32 type, u32 mask)
  185. {
  186. spawn->base.frontend = &crypto_shash_type;
  187. return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
  188. }
  189. EXPORT_SYMBOL_GPL(crypto_grab_shash);
  190. struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
  191. u32 mask)
  192. {
  193. return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
  194. }
  195. EXPORT_SYMBOL_GPL(crypto_alloc_shash);
  196. int crypto_has_shash(const char *alg_name, u32 type, u32 mask)
  197. {
  198. return crypto_type_has_alg(alg_name, &crypto_shash_type, type, mask);
  199. }
  200. EXPORT_SYMBOL_GPL(crypto_has_shash);
  201. struct crypto_shash *crypto_clone_shash(struct crypto_shash *hash)
  202. {
  203. struct crypto_tfm *tfm = crypto_shash_tfm(hash);
  204. struct shash_alg *alg = crypto_shash_alg(hash);
  205. struct crypto_shash *nhash;
  206. int err;
  207. if (!crypto_shash_alg_has_setkey(alg)) {
  208. tfm = crypto_tfm_get(tfm);
  209. if (IS_ERR(tfm))
  210. return ERR_CAST(tfm);
  211. return hash;
  212. }
  213. if (!alg->clone_tfm && (alg->init_tfm || alg->base.cra_init))
  214. return ERR_PTR(-ENOSYS);
  215. nhash = crypto_clone_tfm(&crypto_shash_type, tfm);
  216. if (IS_ERR(nhash))
  217. return nhash;
  218. nhash->descsize = hash->descsize;
  219. if (alg->clone_tfm) {
  220. err = alg->clone_tfm(nhash, hash);
  221. if (err) {
  222. crypto_free_shash(nhash);
  223. return ERR_PTR(err);
  224. }
  225. }
  226. return nhash;
  227. }
  228. EXPORT_SYMBOL_GPL(crypto_clone_shash);
  229. int hash_prepare_alg(struct hash_alg_common *alg)
  230. {
  231. struct crypto_alg *base = &alg->base;
  232. if (alg->digestsize > HASH_MAX_DIGESTSIZE)
  233. return -EINVAL;
  234. /* alignmask is not useful for hashes, so it is not supported. */
  235. if (base->cra_alignmask)
  236. return -EINVAL;
  237. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  238. return 0;
  239. }
  240. static int shash_prepare_alg(struct shash_alg *alg)
  241. {
  242. struct crypto_alg *base = &alg->halg.base;
  243. int err;
  244. if (alg->descsize > HASH_MAX_DESCSIZE)
  245. return -EINVAL;
  246. if ((alg->export && !alg->import) || (alg->import && !alg->export))
  247. return -EINVAL;
  248. err = hash_prepare_alg(&alg->halg);
  249. if (err)
  250. return err;
  251. base->cra_type = &crypto_shash_type;
  252. base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
  253. /*
  254. * Handle missing optional functions. For each one we can either
  255. * install a default here, or we can leave the pointer as NULL and check
  256. * the pointer for NULL in crypto_shash_*(), avoiding an indirect call
  257. * when the default behavior is desired. For ->finup and ->digest we
  258. * install defaults, since for optimal performance algorithms should
  259. * implement these anyway. On the other hand, for ->import and
  260. * ->export the common case and best performance comes from the simple
  261. * memcpy of the shash_desc_ctx, so when those pointers are NULL we
  262. * leave them NULL and provide the memcpy with no indirect call.
  263. */
  264. if (!alg->finup)
  265. alg->finup = shash_default_finup;
  266. if (!alg->digest)
  267. alg->digest = shash_default_digest;
  268. if (!alg->export)
  269. alg->halg.statesize = alg->descsize;
  270. if (!alg->setkey)
  271. alg->setkey = shash_no_setkey;
  272. return 0;
  273. }
  274. int crypto_register_shash(struct shash_alg *alg)
  275. {
  276. struct crypto_alg *base = &alg->base;
  277. int err;
  278. err = shash_prepare_alg(alg);
  279. if (err)
  280. return err;
  281. return crypto_register_alg(base);
  282. }
  283. EXPORT_SYMBOL_GPL(crypto_register_shash);
  284. void crypto_unregister_shash(struct shash_alg *alg)
  285. {
  286. crypto_unregister_alg(&alg->base);
  287. }
  288. EXPORT_SYMBOL_GPL(crypto_unregister_shash);
  289. int crypto_register_shashes(struct shash_alg *algs, int count)
  290. {
  291. int i, ret;
  292. for (i = 0; i < count; i++) {
  293. ret = crypto_register_shash(&algs[i]);
  294. if (ret)
  295. goto err;
  296. }
  297. return 0;
  298. err:
  299. for (--i; i >= 0; --i)
  300. crypto_unregister_shash(&algs[i]);
  301. return ret;
  302. }
  303. EXPORT_SYMBOL_GPL(crypto_register_shashes);
  304. void crypto_unregister_shashes(struct shash_alg *algs, int count)
  305. {
  306. int i;
  307. for (i = count - 1; i >= 0; --i)
  308. crypto_unregister_shash(&algs[i]);
  309. }
  310. EXPORT_SYMBOL_GPL(crypto_unregister_shashes);
  311. int shash_register_instance(struct crypto_template *tmpl,
  312. struct shash_instance *inst)
  313. {
  314. int err;
  315. if (WARN_ON(!inst->free))
  316. return -EINVAL;
  317. err = shash_prepare_alg(&inst->alg);
  318. if (err)
  319. return err;
  320. return crypto_register_instance(tmpl, shash_crypto_instance(inst));
  321. }
  322. EXPORT_SYMBOL_GPL(shash_register_instance);
  323. void shash_free_singlespawn_instance(struct shash_instance *inst)
  324. {
  325. crypto_drop_spawn(shash_instance_ctx(inst));
  326. kfree(inst);
  327. }
  328. EXPORT_SYMBOL_GPL(shash_free_singlespawn_instance);
  329. MODULE_LICENSE("GPL");
  330. MODULE_DESCRIPTION("Synchronous cryptographic hash type");