ahash.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. unsigned int nbytes = walk->entrylen;
  75. walk->data -= walk->offset;
  76. if (nbytes && walk->offset & alignmask && !err) {
  77. walk->offset = ALIGN(walk->offset, alignmask + 1);
  78. nbytes = min(nbytes,
  79. ((unsigned int)(PAGE_SIZE)) - walk->offset);
  80. walk->entrylen -= nbytes;
  81. if (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 (nbytes) {
  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. static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
  140. unsigned int keylen)
  141. {
  142. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  143. int ret;
  144. u8 *buffer, *alignbuffer;
  145. unsigned long absize;
  146. absize = keylen + alignmask;
  147. buffer = kmalloc(absize, GFP_KERNEL);
  148. if (!buffer)
  149. return -ENOMEM;
  150. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  151. memcpy(alignbuffer, key, keylen);
  152. ret = tfm->setkey(tfm, alignbuffer, keylen);
  153. kzfree(buffer);
  154. return ret;
  155. }
  156. int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  157. unsigned int keylen)
  158. {
  159. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  160. int err;
  161. if ((unsigned long)key & alignmask)
  162. err = ahash_setkey_unaligned(tfm, key, keylen);
  163. else
  164. err = tfm->setkey(tfm, key, keylen);
  165. if (err)
  166. return err;
  167. crypto_ahash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
  168. return 0;
  169. }
  170. EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
  171. static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
  172. unsigned int keylen)
  173. {
  174. return -ENOSYS;
  175. }
  176. static inline unsigned int ahash_align_buffer_size(unsigned len,
  177. unsigned long mask)
  178. {
  179. return len + (mask & ~(crypto_tfm_ctx_alignment() - 1));
  180. }
  181. static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt)
  182. {
  183. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  184. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  185. unsigned int ds = crypto_ahash_digestsize(tfm);
  186. struct ahash_request_priv *priv;
  187. priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
  188. (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  189. GFP_KERNEL : GFP_ATOMIC);
  190. if (!priv)
  191. return -ENOMEM;
  192. /*
  193. * WARNING: Voodoo programming below!
  194. *
  195. * The code below is obscure and hard to understand, thus explanation
  196. * is necessary. See include/crypto/hash.h and include/linux/crypto.h
  197. * to understand the layout of structures used here!
  198. *
  199. * The code here will replace portions of the ORIGINAL request with
  200. * pointers to new code and buffers so the hashing operation can store
  201. * the result in aligned buffer. We will call the modified request
  202. * an ADJUSTED request.
  203. *
  204. * The newly mangled request will look as such:
  205. *
  206. * req {
  207. * .result = ADJUSTED[new aligned buffer]
  208. * .base.complete = ADJUSTED[pointer to completion function]
  209. * .base.data = ADJUSTED[*req (pointer to self)]
  210. * .priv = ADJUSTED[new priv] {
  211. * .result = ORIGINAL(result)
  212. * .complete = ORIGINAL(base.complete)
  213. * .data = ORIGINAL(base.data)
  214. * }
  215. */
  216. priv->result = req->result;
  217. priv->complete = req->base.complete;
  218. priv->data = req->base.data;
  219. priv->flags = req->base.flags;
  220. /*
  221. * WARNING: We do not backup req->priv here! The req->priv
  222. * is for internal use of the Crypto API and the
  223. * user must _NOT_ _EVER_ depend on it's content!
  224. */
  225. req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
  226. req->base.complete = cplt;
  227. req->base.data = req;
  228. req->priv = priv;
  229. return 0;
  230. }
  231. static void ahash_restore_req(struct ahash_request *req, int err)
  232. {
  233. struct ahash_request_priv *priv = req->priv;
  234. if (!err)
  235. memcpy(priv->result, req->result,
  236. crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
  237. /* Restore the original crypto request. */
  238. req->result = priv->result;
  239. ahash_request_set_callback(req, priv->flags,
  240. priv->complete, priv->data);
  241. req->priv = NULL;
  242. /* Free the req->priv.priv from the ADJUSTED request. */
  243. kzfree(priv);
  244. }
  245. static void ahash_notify_einprogress(struct ahash_request *req)
  246. {
  247. struct ahash_request_priv *priv = req->priv;
  248. struct crypto_async_request oreq;
  249. oreq.data = priv->data;
  250. priv->complete(&oreq, -EINPROGRESS);
  251. }
  252. static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
  253. {
  254. struct ahash_request *areq = req->data;
  255. if (err == -EINPROGRESS) {
  256. ahash_notify_einprogress(areq);
  257. return;
  258. }
  259. /*
  260. * Restore the original request, see ahash_op_unaligned() for what
  261. * goes where.
  262. *
  263. * The "struct ahash_request *req" here is in fact the "req.base"
  264. * from the ADJUSTED request from ahash_op_unaligned(), thus as it
  265. * is a pointer to self, it is also the ADJUSTED "req" .
  266. */
  267. /* First copy req->result into req->priv.result */
  268. ahash_restore_req(areq, err);
  269. /* Complete the ORIGINAL request. */
  270. areq->base.complete(&areq->base, err);
  271. }
  272. static int ahash_op_unaligned(struct ahash_request *req,
  273. int (*op)(struct ahash_request *))
  274. {
  275. int err;
  276. err = ahash_save_req(req, ahash_op_unaligned_done);
  277. if (err)
  278. return err;
  279. err = op(req);
  280. if (err == -EINPROGRESS ||
  281. (err == -EBUSY && (ahash_request_flags(req) &
  282. CRYPTO_TFM_REQ_MAY_BACKLOG)))
  283. return err;
  284. ahash_restore_req(req, err);
  285. return err;
  286. }
  287. static int crypto_ahash_op(struct ahash_request *req,
  288. int (*op)(struct ahash_request *))
  289. {
  290. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  291. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  292. if ((unsigned long)req->result & alignmask)
  293. return ahash_op_unaligned(req, op);
  294. return op(req);
  295. }
  296. int crypto_ahash_final(struct ahash_request *req)
  297. {
  298. return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
  299. }
  300. EXPORT_SYMBOL_GPL(crypto_ahash_final);
  301. int crypto_ahash_finup(struct ahash_request *req)
  302. {
  303. return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
  304. }
  305. EXPORT_SYMBOL_GPL(crypto_ahash_finup);
  306. int crypto_ahash_digest(struct ahash_request *req)
  307. {
  308. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  309. if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  310. return -ENOKEY;
  311. return crypto_ahash_op(req, tfm->digest);
  312. }
  313. EXPORT_SYMBOL_GPL(crypto_ahash_digest);
  314. static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
  315. {
  316. struct ahash_request *areq = req->data;
  317. if (err == -EINPROGRESS)
  318. return;
  319. ahash_restore_req(areq, err);
  320. areq->base.complete(&areq->base, err);
  321. }
  322. static int ahash_def_finup_finish1(struct ahash_request *req, int err)
  323. {
  324. if (err)
  325. goto out;
  326. req->base.complete = ahash_def_finup_done2;
  327. err = crypto_ahash_reqtfm(req)->final(req);
  328. if (err == -EINPROGRESS ||
  329. (err == -EBUSY && (ahash_request_flags(req) &
  330. CRYPTO_TFM_REQ_MAY_BACKLOG)))
  331. return err;
  332. out:
  333. ahash_restore_req(req, err);
  334. return err;
  335. }
  336. static void ahash_def_finup_done1(struct crypto_async_request *req, int err)
  337. {
  338. struct ahash_request *areq = req->data;
  339. if (err == -EINPROGRESS) {
  340. ahash_notify_einprogress(areq);
  341. return;
  342. }
  343. areq->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  344. err = ahash_def_finup_finish1(areq, err);
  345. if (areq->priv)
  346. return;
  347. areq->base.complete(&areq->base, err);
  348. }
  349. static int ahash_def_finup(struct ahash_request *req)
  350. {
  351. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  352. int err;
  353. err = ahash_save_req(req, ahash_def_finup_done1);
  354. if (err)
  355. return err;
  356. err = tfm->update(req);
  357. if (err == -EINPROGRESS ||
  358. (err == -EBUSY && (ahash_request_flags(req) &
  359. CRYPTO_TFM_REQ_MAY_BACKLOG)))
  360. return err;
  361. return ahash_def_finup_finish1(req, err);
  362. }
  363. static int ahash_no_export(struct ahash_request *req, void *out)
  364. {
  365. return -ENOSYS;
  366. }
  367. static int ahash_no_import(struct ahash_request *req, const void *in)
  368. {
  369. return -ENOSYS;
  370. }
  371. static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
  372. {
  373. struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
  374. struct ahash_alg *alg = crypto_ahash_alg(hash);
  375. hash->setkey = ahash_nosetkey;
  376. hash->export = ahash_no_export;
  377. hash->import = ahash_no_import;
  378. if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
  379. return crypto_init_shash_ops_async(tfm);
  380. hash->init = alg->init;
  381. hash->update = alg->update;
  382. hash->final = alg->final;
  383. hash->finup = alg->finup ?: ahash_def_finup;
  384. hash->digest = alg->digest;
  385. if (alg->setkey) {
  386. hash->setkey = alg->setkey;
  387. if (!(alg->halg.base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
  388. crypto_ahash_set_flags(hash, CRYPTO_TFM_NEED_KEY);
  389. }
  390. if (alg->export)
  391. hash->export = alg->export;
  392. if (alg->import)
  393. hash->import = alg->import;
  394. return 0;
  395. }
  396. static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
  397. {
  398. if (alg->cra_type != &crypto_ahash_type)
  399. return sizeof(struct crypto_shash *);
  400. return crypto_alg_extsize(alg);
  401. }
  402. #ifdef CONFIG_NET
  403. static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
  404. {
  405. struct crypto_report_hash rhash;
  406. strncpy(rhash.type, "ahash", sizeof(rhash.type));
  407. rhash.blocksize = alg->cra_blocksize;
  408. rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
  409. if (nla_put(skb, CRYPTOCFGA_REPORT_HASH,
  410. sizeof(struct crypto_report_hash), &rhash))
  411. goto nla_put_failure;
  412. return 0;
  413. nla_put_failure:
  414. return -EMSGSIZE;
  415. }
  416. #else
  417. static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
  418. {
  419. return -ENOSYS;
  420. }
  421. #endif
  422. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  423. __attribute__ ((unused));
  424. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  425. {
  426. seq_printf(m, "type : ahash\n");
  427. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  428. "yes" : "no");
  429. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  430. seq_printf(m, "digestsize : %u\n",
  431. __crypto_hash_alg_common(alg)->digestsize);
  432. }
  433. const struct crypto_type crypto_ahash_type = {
  434. .extsize = crypto_ahash_extsize,
  435. .init_tfm = crypto_ahash_init_tfm,
  436. #ifdef CONFIG_PROC_FS
  437. .show = crypto_ahash_show,
  438. #endif
  439. .report = crypto_ahash_report,
  440. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  441. .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
  442. .type = CRYPTO_ALG_TYPE_AHASH,
  443. .tfmsize = offsetof(struct crypto_ahash, base),
  444. };
  445. EXPORT_SYMBOL_GPL(crypto_ahash_type);
  446. struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
  447. u32 mask)
  448. {
  449. return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
  450. }
  451. EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
  452. int crypto_has_ahash(const char *alg_name, u32 type, u32 mask)
  453. {
  454. return crypto_type_has_alg(alg_name, &crypto_ahash_type, type, mask);
  455. }
  456. EXPORT_SYMBOL_GPL(crypto_has_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");