ahash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * Asynchronous Cryptographic Hash operations.
  3. *
  4. * This is the asynchronous version of hash.c with notification of
  5. * completion via a callback.
  6. *
  7. * Copyright (c) 2008 Loc Ho <lho@amcc.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. */
  15. #include <crypto/internal/hash.h>
  16. #include <crypto/scatterwalk.h>
  17. #include <linux/bug.h>
  18. #include <linux/err.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/cryptouser.h>
  25. #include <linux/compiler.h>
  26. #include <net/netlink.h>
  27. #include "internal.h"
  28. struct ahash_request_priv {
  29. crypto_completion_t complete;
  30. void *data;
  31. u8 *result;
  32. u32 flags;
  33. void *ubuf[] CRYPTO_MINALIGN_ATTR;
  34. };
  35. static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
  36. {
  37. return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
  38. halg);
  39. }
  40. static int hash_walk_next(struct crypto_hash_walk *walk)
  41. {
  42. unsigned int alignmask = walk->alignmask;
  43. unsigned int offset = walk->offset;
  44. unsigned int nbytes = min(walk->entrylen,
  45. ((unsigned int)(PAGE_SIZE)) - offset);
  46. if (walk->flags & CRYPTO_ALG_ASYNC)
  47. walk->data = kmap(walk->pg);
  48. else
  49. walk->data = kmap_atomic(walk->pg);
  50. walk->data += offset;
  51. if (offset & alignmask) {
  52. unsigned int unaligned = alignmask + 1 - (offset & alignmask);
  53. if (nbytes > unaligned)
  54. nbytes = unaligned;
  55. }
  56. walk->entrylen -= nbytes;
  57. return nbytes;
  58. }
  59. static int hash_walk_new_entry(struct crypto_hash_walk *walk)
  60. {
  61. struct scatterlist *sg;
  62. sg = walk->sg;
  63. walk->offset = sg->offset;
  64. walk->pg = sg_page(walk->sg) + (walk->offset >> PAGE_SHIFT);
  65. walk->offset = offset_in_page(walk->offset);
  66. walk->entrylen = sg->length;
  67. if (walk->entrylen > walk->total)
  68. walk->entrylen = walk->total;
  69. walk->total -= walk->entrylen;
  70. return hash_walk_next(walk);
  71. }
  72. int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
  73. {
  74. unsigned int alignmask = walk->alignmask;
  75. unsigned int nbytes = walk->entrylen;
  76. walk->data -= walk->offset;
  77. if (nbytes && walk->offset & alignmask && !err) {
  78. walk->offset = ALIGN(walk->offset, alignmask + 1);
  79. walk->data += walk->offset;
  80. nbytes = min(nbytes,
  81. ((unsigned int)(PAGE_SIZE)) - walk->offset);
  82. walk->entrylen -= nbytes;
  83. return nbytes;
  84. }
  85. if (walk->flags & CRYPTO_ALG_ASYNC)
  86. kunmap(walk->pg);
  87. else {
  88. kunmap_atomic(walk->data);
  89. /*
  90. * The may sleep test only makes sense for sync users.
  91. * Async users don't need to sleep here anyway.
  92. */
  93. crypto_yield(walk->flags);
  94. }
  95. if (err)
  96. return err;
  97. if (nbytes) {
  98. walk->offset = 0;
  99. walk->pg++;
  100. return hash_walk_next(walk);
  101. }
  102. if (!walk->total)
  103. return 0;
  104. walk->sg = sg_next(walk->sg);
  105. return hash_walk_new_entry(walk);
  106. }
  107. EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
  108. int crypto_hash_walk_first(struct ahash_request *req,
  109. struct crypto_hash_walk *walk)
  110. {
  111. walk->total = req->nbytes;
  112. if (!walk->total) {
  113. walk->entrylen = 0;
  114. return 0;
  115. }
  116. walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
  117. walk->sg = req->src;
  118. walk->flags = req->base.flags & CRYPTO_TFM_REQ_MASK;
  119. return hash_walk_new_entry(walk);
  120. }
  121. EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
  122. int crypto_ahash_walk_first(struct ahash_request *req,
  123. struct crypto_hash_walk *walk)
  124. {
  125. walk->total = req->nbytes;
  126. if (!walk->total) {
  127. walk->entrylen = 0;
  128. return 0;
  129. }
  130. walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
  131. walk->sg = req->src;
  132. walk->flags = req->base.flags & CRYPTO_TFM_REQ_MASK;
  133. walk->flags |= CRYPTO_ALG_ASYNC;
  134. BUILD_BUG_ON(CRYPTO_TFM_REQ_MASK & CRYPTO_ALG_ASYNC);
  135. return hash_walk_new_entry(walk);
  136. }
  137. EXPORT_SYMBOL_GPL(crypto_ahash_walk_first);
  138. static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
  139. unsigned int keylen)
  140. {
  141. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  142. int ret;
  143. u8 *buffer, *alignbuffer;
  144. unsigned long absize;
  145. absize = keylen + alignmask;
  146. buffer = kmalloc(absize, GFP_KERNEL);
  147. if (!buffer)
  148. return -ENOMEM;
  149. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  150. memcpy(alignbuffer, key, keylen);
  151. ret = tfm->setkey(tfm, alignbuffer, keylen);
  152. kzfree(buffer);
  153. return ret;
  154. }
  155. int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  156. unsigned int keylen)
  157. {
  158. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  159. if ((unsigned long)key & alignmask)
  160. return ahash_setkey_unaligned(tfm, key, keylen);
  161. return tfm->setkey(tfm, key, keylen);
  162. }
  163. EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
  164. static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
  165. unsigned int keylen)
  166. {
  167. return -ENOSYS;
  168. }
  169. static inline unsigned int ahash_align_buffer_size(unsigned len,
  170. unsigned long mask)
  171. {
  172. return len + (mask & ~(crypto_tfm_ctx_alignment() - 1));
  173. }
  174. static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt)
  175. {
  176. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  177. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  178. unsigned int ds = crypto_ahash_digestsize(tfm);
  179. struct ahash_request_priv *priv;
  180. priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
  181. (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  182. GFP_KERNEL : GFP_ATOMIC);
  183. if (!priv)
  184. return -ENOMEM;
  185. /*
  186. * WARNING: Voodoo programming below!
  187. *
  188. * The code below is obscure and hard to understand, thus explanation
  189. * is necessary. See include/crypto/hash.h and include/linux/crypto.h
  190. * to understand the layout of structures used here!
  191. *
  192. * The code here will replace portions of the ORIGINAL request with
  193. * pointers to new code and buffers so the hashing operation can store
  194. * the result in aligned buffer. We will call the modified request
  195. * an ADJUSTED request.
  196. *
  197. * The newly mangled request will look as such:
  198. *
  199. * req {
  200. * .result = ADJUSTED[new aligned buffer]
  201. * .base.complete = ADJUSTED[pointer to completion function]
  202. * .base.data = ADJUSTED[*req (pointer to self)]
  203. * .priv = ADJUSTED[new priv] {
  204. * .result = ORIGINAL(result)
  205. * .complete = ORIGINAL(base.complete)
  206. * .data = ORIGINAL(base.data)
  207. * }
  208. */
  209. priv->result = req->result;
  210. priv->complete = req->base.complete;
  211. priv->data = req->base.data;
  212. priv->flags = req->base.flags;
  213. /*
  214. * WARNING: We do not backup req->priv here! The req->priv
  215. * is for internal use of the Crypto API and the
  216. * user must _NOT_ _EVER_ depend on it's content!
  217. */
  218. req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
  219. req->base.complete = cplt;
  220. req->base.data = req;
  221. req->priv = priv;
  222. return 0;
  223. }
  224. static void ahash_restore_req(struct ahash_request *req, int err)
  225. {
  226. struct ahash_request_priv *priv = req->priv;
  227. if (!err)
  228. memcpy(priv->result, req->result,
  229. crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
  230. /* Restore the original crypto request. */
  231. req->result = priv->result;
  232. ahash_request_set_callback(req, priv->flags,
  233. priv->complete, priv->data);
  234. req->priv = NULL;
  235. /* Free the req->priv.priv from the ADJUSTED request. */
  236. kzfree(priv);
  237. }
  238. static void ahash_notify_einprogress(struct ahash_request *req)
  239. {
  240. struct ahash_request_priv *priv = req->priv;
  241. struct crypto_async_request oreq;
  242. oreq.data = priv->data;
  243. priv->complete(&oreq, -EINPROGRESS);
  244. }
  245. static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
  246. {
  247. struct ahash_request *areq = req->data;
  248. if (err == -EINPROGRESS) {
  249. ahash_notify_einprogress(areq);
  250. return;
  251. }
  252. /*
  253. * Restore the original request, see ahash_op_unaligned() for what
  254. * goes where.
  255. *
  256. * The "struct ahash_request *req" here is in fact the "req.base"
  257. * from the ADJUSTED request from ahash_op_unaligned(), thus as it
  258. * is a pointer to self, it is also the ADJUSTED "req" .
  259. */
  260. /* First copy req->result into req->priv.result */
  261. ahash_restore_req(areq, err);
  262. /* Complete the ORIGINAL request. */
  263. areq->base.complete(&areq->base, err);
  264. }
  265. static int ahash_op_unaligned(struct ahash_request *req,
  266. int (*op)(struct ahash_request *))
  267. {
  268. int err;
  269. err = ahash_save_req(req, ahash_op_unaligned_done);
  270. if (err)
  271. return err;
  272. err = op(req);
  273. if (err == -EINPROGRESS ||
  274. (err == -EBUSY && (ahash_request_flags(req) &
  275. CRYPTO_TFM_REQ_MAY_BACKLOG)))
  276. return err;
  277. ahash_restore_req(req, err);
  278. return err;
  279. }
  280. static int crypto_ahash_op(struct ahash_request *req,
  281. int (*op)(struct ahash_request *))
  282. {
  283. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  284. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  285. if ((unsigned long)req->result & alignmask)
  286. return ahash_op_unaligned(req, op);
  287. return op(req);
  288. }
  289. int crypto_ahash_final(struct ahash_request *req)
  290. {
  291. return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
  292. }
  293. EXPORT_SYMBOL_GPL(crypto_ahash_final);
  294. int crypto_ahash_finup(struct ahash_request *req)
  295. {
  296. return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
  297. }
  298. EXPORT_SYMBOL_GPL(crypto_ahash_finup);
  299. int crypto_ahash_digest(struct ahash_request *req)
  300. {
  301. return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->digest);
  302. }
  303. EXPORT_SYMBOL_GPL(crypto_ahash_digest);
  304. static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
  305. {
  306. struct ahash_request *areq = req->data;
  307. if (err == -EINPROGRESS)
  308. return;
  309. ahash_restore_req(areq, err);
  310. areq->base.complete(&areq->base, err);
  311. }
  312. static int ahash_def_finup_finish1(struct ahash_request *req, int err)
  313. {
  314. if (err)
  315. goto out;
  316. req->base.complete = ahash_def_finup_done2;
  317. err = crypto_ahash_reqtfm(req)->final(req);
  318. if (err == -EINPROGRESS ||
  319. (err == -EBUSY && (ahash_request_flags(req) &
  320. CRYPTO_TFM_REQ_MAY_BACKLOG)))
  321. return err;
  322. out:
  323. ahash_restore_req(req, err);
  324. return err;
  325. }
  326. static void ahash_def_finup_done1(struct crypto_async_request *req, int err)
  327. {
  328. struct ahash_request *areq = req->data;
  329. if (err == -EINPROGRESS) {
  330. ahash_notify_einprogress(areq);
  331. return;
  332. }
  333. areq->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  334. err = ahash_def_finup_finish1(areq, err);
  335. if (areq->priv)
  336. return;
  337. areq->base.complete(&areq->base, err);
  338. }
  339. static int ahash_def_finup(struct ahash_request *req)
  340. {
  341. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  342. int err;
  343. err = ahash_save_req(req, ahash_def_finup_done1);
  344. if (err)
  345. return err;
  346. err = tfm->update(req);
  347. if (err == -EINPROGRESS ||
  348. (err == -EBUSY && (ahash_request_flags(req) &
  349. CRYPTO_TFM_REQ_MAY_BACKLOG)))
  350. return err;
  351. return ahash_def_finup_finish1(req, err);
  352. }
  353. static int ahash_no_export(struct ahash_request *req, void *out)
  354. {
  355. return -ENOSYS;
  356. }
  357. static int ahash_no_import(struct ahash_request *req, const void *in)
  358. {
  359. return -ENOSYS;
  360. }
  361. static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
  362. {
  363. struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
  364. struct ahash_alg *alg = crypto_ahash_alg(hash);
  365. hash->setkey = ahash_nosetkey;
  366. hash->has_setkey = false;
  367. hash->export = ahash_no_export;
  368. hash->import = ahash_no_import;
  369. if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
  370. return crypto_init_shash_ops_async(tfm);
  371. hash->init = alg->init;
  372. hash->update = alg->update;
  373. hash->final = alg->final;
  374. hash->finup = alg->finup ?: ahash_def_finup;
  375. hash->digest = alg->digest;
  376. if (alg->setkey) {
  377. hash->setkey = alg->setkey;
  378. hash->has_setkey = true;
  379. }
  380. if (alg->export)
  381. hash->export = alg->export;
  382. if (alg->import)
  383. hash->import = alg->import;
  384. return 0;
  385. }
  386. static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
  387. {
  388. if (alg->cra_type != &crypto_ahash_type)
  389. return sizeof(struct crypto_shash *);
  390. return crypto_alg_extsize(alg);
  391. }
  392. #ifdef CONFIG_NET
  393. static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
  394. {
  395. struct crypto_report_hash rhash;
  396. strncpy(rhash.type, "ahash", sizeof(rhash.type));
  397. rhash.blocksize = alg->cra_blocksize;
  398. rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
  399. if (nla_put(skb, CRYPTOCFGA_REPORT_HASH,
  400. sizeof(struct crypto_report_hash), &rhash))
  401. goto nla_put_failure;
  402. return 0;
  403. nla_put_failure:
  404. return -EMSGSIZE;
  405. }
  406. #else
  407. static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
  408. {
  409. return -ENOSYS;
  410. }
  411. #endif
  412. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  413. __maybe_unused;
  414. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  415. {
  416. seq_printf(m, "type : ahash\n");
  417. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  418. "yes" : "no");
  419. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  420. seq_printf(m, "digestsize : %u\n",
  421. __crypto_hash_alg_common(alg)->digestsize);
  422. }
  423. const struct crypto_type crypto_ahash_type = {
  424. .extsize = crypto_ahash_extsize,
  425. .init_tfm = crypto_ahash_init_tfm,
  426. #ifdef CONFIG_PROC_FS
  427. .show = crypto_ahash_show,
  428. #endif
  429. .report = crypto_ahash_report,
  430. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  431. .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
  432. .type = CRYPTO_ALG_TYPE_AHASH,
  433. .tfmsize = offsetof(struct crypto_ahash, base),
  434. };
  435. EXPORT_SYMBOL_GPL(crypto_ahash_type);
  436. struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
  437. u32 mask)
  438. {
  439. return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
  440. }
  441. EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
  442. int crypto_has_ahash(const char *alg_name, u32 type, u32 mask)
  443. {
  444. return crypto_type_has_alg(alg_name, &crypto_ahash_type, type, mask);
  445. }
  446. EXPORT_SYMBOL_GPL(crypto_has_ahash);
  447. static int ahash_prepare_alg(struct ahash_alg *alg)
  448. {
  449. struct crypto_alg *base = &alg->halg.base;
  450. if (alg->halg.digestsize > PAGE_SIZE / 8 ||
  451. alg->halg.statesize > PAGE_SIZE / 8 ||
  452. alg->halg.statesize == 0)
  453. return -EINVAL;
  454. base->cra_type = &crypto_ahash_type;
  455. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  456. base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
  457. return 0;
  458. }
  459. int crypto_register_ahash(struct ahash_alg *alg)
  460. {
  461. struct crypto_alg *base = &alg->halg.base;
  462. int err;
  463. err = ahash_prepare_alg(alg);
  464. if (err)
  465. return err;
  466. return crypto_register_alg(base);
  467. }
  468. EXPORT_SYMBOL_GPL(crypto_register_ahash);
  469. int crypto_unregister_ahash(struct ahash_alg *alg)
  470. {
  471. return crypto_unregister_alg(&alg->halg.base);
  472. }
  473. EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
  474. int ahash_register_instance(struct crypto_template *tmpl,
  475. struct ahash_instance *inst)
  476. {
  477. int err;
  478. err = ahash_prepare_alg(&inst->alg);
  479. if (err)
  480. return err;
  481. return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
  482. }
  483. EXPORT_SYMBOL_GPL(ahash_register_instance);
  484. void ahash_free_instance(struct crypto_instance *inst)
  485. {
  486. crypto_drop_spawn(crypto_instance_ctx(inst));
  487. kfree(ahash_instance(inst));
  488. }
  489. EXPORT_SYMBOL_GPL(ahash_free_instance);
  490. int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
  491. struct hash_alg_common *alg,
  492. struct crypto_instance *inst)
  493. {
  494. return crypto_init_spawn2(&spawn->base, &alg->base, inst,
  495. &crypto_ahash_type);
  496. }
  497. EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn);
  498. struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
  499. {
  500. struct crypto_alg *alg;
  501. alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask);
  502. return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg);
  503. }
  504. EXPORT_SYMBOL_GPL(ahash_attr_alg);
  505. MODULE_LICENSE("GPL");
  506. MODULE_DESCRIPTION("Asynchronous cryptographic hash type");