ahash.c 14 KB

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