shash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Synchronous Cryptographic Hash operations.
  3. *
  4. * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #include <crypto/scatterwalk.h>
  13. #include <crypto/internal/hash.h>
  14. #include <linux/err.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/cryptouser.h>
  20. #include <net/netlink.h>
  21. #include <linux/compiler.h>
  22. #include "internal.h"
  23. static const struct crypto_type crypto_shash_type;
  24. int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
  25. unsigned int keylen)
  26. {
  27. return -ENOSYS;
  28. }
  29. EXPORT_SYMBOL_GPL(shash_no_setkey);
  30. static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
  31. unsigned int keylen)
  32. {
  33. struct shash_alg *shash = crypto_shash_alg(tfm);
  34. unsigned long alignmask = crypto_shash_alignmask(tfm);
  35. unsigned long absize;
  36. u8 *buffer, *alignbuffer;
  37. int err;
  38. absize = keylen + (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  39. buffer = kmalloc(absize, GFP_ATOMIC);
  40. if (!buffer)
  41. return -ENOMEM;
  42. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  43. memcpy(alignbuffer, key, keylen);
  44. err = shash->setkey(tfm, alignbuffer, keylen);
  45. kzfree(buffer);
  46. return err;
  47. }
  48. int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
  49. unsigned int keylen)
  50. {
  51. struct shash_alg *shash = crypto_shash_alg(tfm);
  52. unsigned long alignmask = crypto_shash_alignmask(tfm);
  53. int err;
  54. if ((unsigned long)key & alignmask)
  55. err = shash_setkey_unaligned(tfm, key, keylen);
  56. else
  57. err = shash->setkey(tfm, key, keylen);
  58. if (err)
  59. return err;
  60. crypto_shash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
  61. return 0;
  62. }
  63. EXPORT_SYMBOL_GPL(crypto_shash_setkey);
  64. static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
  65. unsigned int len)
  66. {
  67. struct crypto_shash *tfm = desc->tfm;
  68. struct shash_alg *shash = crypto_shash_alg(tfm);
  69. unsigned long alignmask = crypto_shash_alignmask(tfm);
  70. unsigned int unaligned_len = alignmask + 1 -
  71. ((unsigned long)data & alignmask);
  72. /*
  73. * We cannot count on __aligned() working for large values:
  74. * https://patchwork.kernel.org/patch/9507697/
  75. */
  76. u8 ubuf[MAX_ALGAPI_ALIGNMASK * 2];
  77. u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
  78. int err;
  79. if (WARN_ON(buf + unaligned_len > ubuf + sizeof(ubuf)))
  80. return -EINVAL;
  81. if (unaligned_len > len)
  82. unaligned_len = len;
  83. memcpy(buf, data, unaligned_len);
  84. err = shash->update(desc, buf, unaligned_len);
  85. memset(buf, 0, unaligned_len);
  86. return err ?:
  87. shash->update(desc, data + unaligned_len, len - unaligned_len);
  88. }
  89. int crypto_shash_update(struct shash_desc *desc, const u8 *data,
  90. unsigned int len)
  91. {
  92. struct crypto_shash *tfm = desc->tfm;
  93. struct shash_alg *shash = crypto_shash_alg(tfm);
  94. unsigned long alignmask = crypto_shash_alignmask(tfm);
  95. if ((unsigned long)data & alignmask)
  96. return shash_update_unaligned(desc, data, len);
  97. return shash->update(desc, data, len);
  98. }
  99. EXPORT_SYMBOL_GPL(crypto_shash_update);
  100. static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
  101. {
  102. struct crypto_shash *tfm = desc->tfm;
  103. unsigned long alignmask = crypto_shash_alignmask(tfm);
  104. struct shash_alg *shash = crypto_shash_alg(tfm);
  105. unsigned int ds = crypto_shash_digestsize(tfm);
  106. /*
  107. * We cannot count on __aligned() working for large values:
  108. * https://patchwork.kernel.org/patch/9507697/
  109. */
  110. u8 ubuf[MAX_ALGAPI_ALIGNMASK + HASH_MAX_DIGESTSIZE];
  111. u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
  112. int err;
  113. if (WARN_ON(buf + ds > ubuf + sizeof(ubuf)))
  114. return -EINVAL;
  115. err = shash->final(desc, buf);
  116. if (err)
  117. goto out;
  118. memcpy(out, buf, ds);
  119. out:
  120. memset(buf, 0, ds);
  121. return err;
  122. }
  123. int crypto_shash_final(struct shash_desc *desc, u8 *out)
  124. {
  125. struct crypto_shash *tfm = desc->tfm;
  126. struct shash_alg *shash = crypto_shash_alg(tfm);
  127. unsigned long alignmask = crypto_shash_alignmask(tfm);
  128. if ((unsigned long)out & alignmask)
  129. return shash_final_unaligned(desc, out);
  130. return shash->final(desc, out);
  131. }
  132. EXPORT_SYMBOL_GPL(crypto_shash_final);
  133. static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
  134. unsigned int len, u8 *out)
  135. {
  136. return crypto_shash_update(desc, data, len) ?:
  137. crypto_shash_final(desc, out);
  138. }
  139. int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
  140. unsigned int len, u8 *out)
  141. {
  142. struct crypto_shash *tfm = desc->tfm;
  143. struct shash_alg *shash = crypto_shash_alg(tfm);
  144. unsigned long alignmask = crypto_shash_alignmask(tfm);
  145. if (((unsigned long)data | (unsigned long)out) & alignmask)
  146. return shash_finup_unaligned(desc, data, len, out);
  147. return shash->finup(desc, data, len, out);
  148. }
  149. EXPORT_SYMBOL_GPL(crypto_shash_finup);
  150. static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
  151. unsigned int len, u8 *out)
  152. {
  153. return crypto_shash_init(desc) ?:
  154. crypto_shash_finup(desc, data, len, out);
  155. }
  156. int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
  157. unsigned int len, u8 *out)
  158. {
  159. struct crypto_shash *tfm = desc->tfm;
  160. struct shash_alg *shash = crypto_shash_alg(tfm);
  161. unsigned long alignmask = crypto_shash_alignmask(tfm);
  162. if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  163. return -ENOKEY;
  164. if (((unsigned long)data | (unsigned long)out) & alignmask)
  165. return shash_digest_unaligned(desc, data, len, out);
  166. return shash->digest(desc, data, len, out);
  167. }
  168. EXPORT_SYMBOL_GPL(crypto_shash_digest);
  169. static int shash_default_export(struct shash_desc *desc, void *out)
  170. {
  171. memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm));
  172. return 0;
  173. }
  174. static int shash_default_import(struct shash_desc *desc, const void *in)
  175. {
  176. memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(desc->tfm));
  177. return 0;
  178. }
  179. static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
  180. unsigned int keylen)
  181. {
  182. struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
  183. return crypto_shash_setkey(*ctx, key, keylen);
  184. }
  185. static int shash_async_init(struct ahash_request *req)
  186. {
  187. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  188. struct shash_desc *desc = ahash_request_ctx(req);
  189. desc->tfm = *ctx;
  190. desc->flags = req->base.flags;
  191. return crypto_shash_init(desc);
  192. }
  193. int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc)
  194. {
  195. struct crypto_hash_walk walk;
  196. int nbytes;
  197. for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
  198. nbytes = crypto_hash_walk_done(&walk, nbytes))
  199. nbytes = crypto_shash_update(desc, walk.data, nbytes);
  200. return nbytes;
  201. }
  202. EXPORT_SYMBOL_GPL(shash_ahash_update);
  203. static int shash_async_update(struct ahash_request *req)
  204. {
  205. return shash_ahash_update(req, ahash_request_ctx(req));
  206. }
  207. static int shash_async_final(struct ahash_request *req)
  208. {
  209. return crypto_shash_final(ahash_request_ctx(req), req->result);
  210. }
  211. int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc)
  212. {
  213. struct crypto_hash_walk walk;
  214. int nbytes;
  215. nbytes = crypto_hash_walk_first(req, &walk);
  216. if (!nbytes)
  217. return crypto_shash_final(desc, req->result);
  218. do {
  219. nbytes = crypto_hash_walk_last(&walk) ?
  220. crypto_shash_finup(desc, walk.data, nbytes,
  221. req->result) :
  222. crypto_shash_update(desc, walk.data, nbytes);
  223. nbytes = crypto_hash_walk_done(&walk, nbytes);
  224. } while (nbytes > 0);
  225. return nbytes;
  226. }
  227. EXPORT_SYMBOL_GPL(shash_ahash_finup);
  228. static int shash_async_finup(struct ahash_request *req)
  229. {
  230. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  231. struct shash_desc *desc = ahash_request_ctx(req);
  232. desc->tfm = *ctx;
  233. desc->flags = req->base.flags;
  234. return shash_ahash_finup(req, desc);
  235. }
  236. int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
  237. {
  238. unsigned int nbytes = req->nbytes;
  239. struct scatterlist *sg;
  240. unsigned int offset;
  241. int err;
  242. if (nbytes &&
  243. (sg = req->src, offset = sg->offset,
  244. nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset))) {
  245. void *data;
  246. data = kmap_atomic(sg_page(sg));
  247. err = crypto_shash_digest(desc, data + offset, nbytes,
  248. req->result);
  249. kunmap_atomic(data);
  250. crypto_yield(desc->flags);
  251. } else
  252. err = crypto_shash_init(desc) ?:
  253. shash_ahash_finup(req, desc);
  254. return err;
  255. }
  256. EXPORT_SYMBOL_GPL(shash_ahash_digest);
  257. static int shash_async_digest(struct ahash_request *req)
  258. {
  259. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  260. struct shash_desc *desc = ahash_request_ctx(req);
  261. desc->tfm = *ctx;
  262. desc->flags = req->base.flags;
  263. return shash_ahash_digest(req, desc);
  264. }
  265. static int shash_async_export(struct ahash_request *req, void *out)
  266. {
  267. return crypto_shash_export(ahash_request_ctx(req), out);
  268. }
  269. static int shash_async_import(struct ahash_request *req, const void *in)
  270. {
  271. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  272. struct shash_desc *desc = ahash_request_ctx(req);
  273. desc->tfm = *ctx;
  274. desc->flags = req->base.flags;
  275. return crypto_shash_import(desc, in);
  276. }
  277. static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
  278. {
  279. struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
  280. crypto_free_shash(*ctx);
  281. }
  282. int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
  283. {
  284. struct crypto_alg *calg = tfm->__crt_alg;
  285. struct shash_alg *alg = __crypto_shash_alg(calg);
  286. struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
  287. struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
  288. struct crypto_shash *shash;
  289. if (!crypto_mod_get(calg))
  290. return -EAGAIN;
  291. shash = crypto_create_tfm(calg, &crypto_shash_type);
  292. if (IS_ERR(shash)) {
  293. crypto_mod_put(calg);
  294. return PTR_ERR(shash);
  295. }
  296. *ctx = shash;
  297. tfm->exit = crypto_exit_shash_ops_async;
  298. crt->init = shash_async_init;
  299. crt->update = shash_async_update;
  300. crt->final = shash_async_final;
  301. crt->finup = shash_async_finup;
  302. crt->digest = shash_async_digest;
  303. crt->setkey = shash_async_setkey;
  304. crypto_ahash_set_flags(crt, crypto_shash_get_flags(shash) &
  305. CRYPTO_TFM_NEED_KEY);
  306. if (alg->export)
  307. crt->export = shash_async_export;
  308. if (alg->import)
  309. crt->import = shash_async_import;
  310. crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
  311. return 0;
  312. }
  313. static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
  314. {
  315. struct crypto_shash *hash = __crypto_shash_cast(tfm);
  316. struct shash_alg *alg = crypto_shash_alg(hash);
  317. hash->descsize = alg->descsize;
  318. if (crypto_shash_alg_has_setkey(alg) &&
  319. !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
  320. crypto_shash_set_flags(hash, CRYPTO_TFM_NEED_KEY);
  321. return 0;
  322. }
  323. #ifdef CONFIG_NET
  324. static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
  325. {
  326. struct crypto_report_hash rhash;
  327. struct shash_alg *salg = __crypto_shash_alg(alg);
  328. strncpy(rhash.type, "shash", sizeof(rhash.type));
  329. rhash.blocksize = alg->cra_blocksize;
  330. rhash.digestsize = salg->digestsize;
  331. if (nla_put(skb, CRYPTOCFGA_REPORT_HASH,
  332. sizeof(struct crypto_report_hash), &rhash))
  333. goto nla_put_failure;
  334. return 0;
  335. nla_put_failure:
  336. return -EMSGSIZE;
  337. }
  338. #else
  339. static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
  340. {
  341. return -ENOSYS;
  342. }
  343. #endif
  344. static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
  345. __maybe_unused;
  346. static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
  347. {
  348. struct shash_alg *salg = __crypto_shash_alg(alg);
  349. seq_printf(m, "type : shash\n");
  350. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  351. seq_printf(m, "digestsize : %u\n", salg->digestsize);
  352. }
  353. static const struct crypto_type crypto_shash_type = {
  354. .extsize = crypto_alg_extsize,
  355. .init_tfm = crypto_shash_init_tfm,
  356. #ifdef CONFIG_PROC_FS
  357. .show = crypto_shash_show,
  358. #endif
  359. .report = crypto_shash_report,
  360. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  361. .maskset = CRYPTO_ALG_TYPE_MASK,
  362. .type = CRYPTO_ALG_TYPE_SHASH,
  363. .tfmsize = offsetof(struct crypto_shash, base),
  364. };
  365. struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
  366. u32 mask)
  367. {
  368. return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
  369. }
  370. EXPORT_SYMBOL_GPL(crypto_alloc_shash);
  371. static int shash_prepare_alg(struct shash_alg *alg)
  372. {
  373. struct crypto_alg *base = &alg->base;
  374. if (alg->digestsize > HASH_MAX_DIGESTSIZE ||
  375. alg->descsize > HASH_MAX_DESCSIZE ||
  376. alg->statesize > HASH_MAX_STATESIZE)
  377. return -EINVAL;
  378. base->cra_type = &crypto_shash_type;
  379. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  380. base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
  381. if (!alg->finup)
  382. alg->finup = shash_finup_unaligned;
  383. if (!alg->digest)
  384. alg->digest = shash_digest_unaligned;
  385. if (!alg->export) {
  386. alg->export = shash_default_export;
  387. alg->import = shash_default_import;
  388. alg->statesize = alg->descsize;
  389. }
  390. if (!alg->setkey)
  391. alg->setkey = shash_no_setkey;
  392. return 0;
  393. }
  394. int crypto_register_shash(struct shash_alg *alg)
  395. {
  396. struct crypto_alg *base = &alg->base;
  397. int err;
  398. err = shash_prepare_alg(alg);
  399. if (err)
  400. return err;
  401. return crypto_register_alg(base);
  402. }
  403. EXPORT_SYMBOL_GPL(crypto_register_shash);
  404. int crypto_unregister_shash(struct shash_alg *alg)
  405. {
  406. return crypto_unregister_alg(&alg->base);
  407. }
  408. EXPORT_SYMBOL_GPL(crypto_unregister_shash);
  409. int crypto_register_shashes(struct shash_alg *algs, int count)
  410. {
  411. int i, ret;
  412. for (i = 0; i < count; i++) {
  413. ret = crypto_register_shash(&algs[i]);
  414. if (ret)
  415. goto err;
  416. }
  417. return 0;
  418. err:
  419. for (--i; i >= 0; --i)
  420. crypto_unregister_shash(&algs[i]);
  421. return ret;
  422. }
  423. EXPORT_SYMBOL_GPL(crypto_register_shashes);
  424. int crypto_unregister_shashes(struct shash_alg *algs, int count)
  425. {
  426. int i, ret;
  427. for (i = count - 1; i >= 0; --i) {
  428. ret = crypto_unregister_shash(&algs[i]);
  429. if (ret)
  430. pr_err("Failed to unregister %s %s: %d\n",
  431. algs[i].base.cra_driver_name,
  432. algs[i].base.cra_name, ret);
  433. }
  434. return 0;
  435. }
  436. EXPORT_SYMBOL_GPL(crypto_unregister_shashes);
  437. int shash_register_instance(struct crypto_template *tmpl,
  438. struct shash_instance *inst)
  439. {
  440. int err;
  441. err = shash_prepare_alg(&inst->alg);
  442. if (err)
  443. return err;
  444. return crypto_register_instance(tmpl, shash_crypto_instance(inst));
  445. }
  446. EXPORT_SYMBOL_GPL(shash_register_instance);
  447. void shash_free_instance(struct crypto_instance *inst)
  448. {
  449. crypto_drop_spawn(crypto_instance_ctx(inst));
  450. kfree(shash_instance(inst));
  451. }
  452. EXPORT_SYMBOL_GPL(shash_free_instance);
  453. int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn,
  454. struct shash_alg *alg,
  455. struct crypto_instance *inst)
  456. {
  457. return crypto_init_spawn2(&spawn->base, &alg->base, inst,
  458. &crypto_shash_type);
  459. }
  460. EXPORT_SYMBOL_GPL(crypto_init_shash_spawn);
  461. struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
  462. {
  463. struct crypto_alg *alg;
  464. alg = crypto_attr_alg2(rta, &crypto_shash_type, type, mask);
  465. return IS_ERR(alg) ? ERR_CAST(alg) :
  466. container_of(alg, struct shash_alg, base);
  467. }
  468. EXPORT_SYMBOL_GPL(shash_attr_alg);
  469. MODULE_LICENSE("GPL");
  470. MODULE_DESCRIPTION("Synchronous cryptographic hash type");