ahash.c 15 KB

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