ahash.c 15 KB

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