ahash.c 16 KB

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