ahash.c 15 KB

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