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