ahash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 void ahash_op_unaligned_finish(struct ahash_request *req, int err)
  154. {
  155. struct ahash_request_priv *priv = req->priv;
  156. if (err == -EINPROGRESS)
  157. return;
  158. if (!err)
  159. memcpy(priv->result, req->result,
  160. crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
  161. kzfree(priv);
  162. }
  163. static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
  164. {
  165. struct ahash_request *areq = req->data;
  166. struct ahash_request_priv *priv = areq->priv;
  167. crypto_completion_t complete = priv->complete;
  168. void *data = priv->data;
  169. ahash_op_unaligned_finish(areq, err);
  170. areq->base.complete = complete;
  171. areq->base.data = data;
  172. complete(&areq->base, err);
  173. }
  174. static int ahash_op_unaligned(struct ahash_request *req,
  175. int (*op)(struct ahash_request *))
  176. {
  177. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  178. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  179. unsigned int ds = crypto_ahash_digestsize(tfm);
  180. struct ahash_request_priv *priv;
  181. int err;
  182. priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
  183. (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  184. GFP_KERNEL : GFP_ATOMIC);
  185. if (!priv)
  186. return -ENOMEM;
  187. priv->result = req->result;
  188. priv->complete = req->base.complete;
  189. priv->data = req->base.data;
  190. req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
  191. req->base.complete = ahash_op_unaligned_done;
  192. req->base.data = req;
  193. req->priv = priv;
  194. err = op(req);
  195. ahash_op_unaligned_finish(req, err);
  196. return err;
  197. }
  198. static int crypto_ahash_op(struct ahash_request *req,
  199. int (*op)(struct ahash_request *))
  200. {
  201. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  202. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  203. if ((unsigned long)req->result & alignmask)
  204. return ahash_op_unaligned(req, op);
  205. return op(req);
  206. }
  207. int crypto_ahash_final(struct ahash_request *req)
  208. {
  209. return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
  210. }
  211. EXPORT_SYMBOL_GPL(crypto_ahash_final);
  212. int crypto_ahash_finup(struct ahash_request *req)
  213. {
  214. return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
  215. }
  216. EXPORT_SYMBOL_GPL(crypto_ahash_finup);
  217. int crypto_ahash_digest(struct ahash_request *req)
  218. {
  219. return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->digest);
  220. }
  221. EXPORT_SYMBOL_GPL(crypto_ahash_digest);
  222. static void ahash_def_finup_finish2(struct ahash_request *req, int err)
  223. {
  224. struct ahash_request_priv *priv = req->priv;
  225. if (err == -EINPROGRESS)
  226. return;
  227. if (!err)
  228. memcpy(priv->result, req->result,
  229. crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
  230. kzfree(priv);
  231. }
  232. static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
  233. {
  234. struct ahash_request *areq = req->data;
  235. struct ahash_request_priv *priv = areq->priv;
  236. crypto_completion_t complete = priv->complete;
  237. void *data = priv->data;
  238. ahash_def_finup_finish2(areq, err);
  239. complete(data, err);
  240. }
  241. static int ahash_def_finup_finish1(struct ahash_request *req, int err)
  242. {
  243. if (err)
  244. goto out;
  245. req->base.complete = ahash_def_finup_done2;
  246. req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  247. err = crypto_ahash_reqtfm(req)->final(req);
  248. out:
  249. ahash_def_finup_finish2(req, err);
  250. return err;
  251. }
  252. static void ahash_def_finup_done1(struct crypto_async_request *req, int err)
  253. {
  254. struct ahash_request *areq = req->data;
  255. struct ahash_request_priv *priv = areq->priv;
  256. crypto_completion_t complete = priv->complete;
  257. void *data = priv->data;
  258. err = ahash_def_finup_finish1(areq, err);
  259. complete(data, err);
  260. }
  261. static int ahash_def_finup(struct ahash_request *req)
  262. {
  263. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  264. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  265. unsigned int ds = crypto_ahash_digestsize(tfm);
  266. struct ahash_request_priv *priv;
  267. priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
  268. (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  269. GFP_KERNEL : GFP_ATOMIC);
  270. if (!priv)
  271. return -ENOMEM;
  272. priv->result = req->result;
  273. priv->complete = req->base.complete;
  274. priv->data = req->base.data;
  275. req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
  276. req->base.complete = ahash_def_finup_done1;
  277. req->base.data = req;
  278. req->priv = priv;
  279. return ahash_def_finup_finish1(req, tfm->update(req));
  280. }
  281. static int ahash_no_export(struct ahash_request *req, void *out)
  282. {
  283. return -ENOSYS;
  284. }
  285. static int ahash_no_import(struct ahash_request *req, const void *in)
  286. {
  287. return -ENOSYS;
  288. }
  289. static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
  290. {
  291. struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
  292. struct ahash_alg *alg = crypto_ahash_alg(hash);
  293. hash->setkey = ahash_nosetkey;
  294. hash->export = ahash_no_export;
  295. hash->import = ahash_no_import;
  296. if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
  297. return crypto_init_shash_ops_async(tfm);
  298. hash->init = alg->init;
  299. hash->update = alg->update;
  300. hash->final = alg->final;
  301. hash->finup = alg->finup ?: ahash_def_finup;
  302. hash->digest = alg->digest;
  303. if (alg->setkey)
  304. hash->setkey = alg->setkey;
  305. if (alg->export)
  306. hash->export = alg->export;
  307. if (alg->import)
  308. hash->import = alg->import;
  309. return 0;
  310. }
  311. static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
  312. {
  313. if (alg->cra_type == &crypto_ahash_type)
  314. return alg->cra_ctxsize;
  315. return sizeof(struct crypto_shash *);
  316. }
  317. #ifdef CONFIG_NET
  318. static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
  319. {
  320. struct crypto_report_hash rhash;
  321. strncpy(rhash.type, "ahash", sizeof(rhash.type));
  322. rhash.blocksize = alg->cra_blocksize;
  323. rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
  324. if (nla_put(skb, CRYPTOCFGA_REPORT_HASH,
  325. sizeof(struct crypto_report_hash), &rhash))
  326. goto nla_put_failure;
  327. return 0;
  328. nla_put_failure:
  329. return -EMSGSIZE;
  330. }
  331. #else
  332. static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
  333. {
  334. return -ENOSYS;
  335. }
  336. #endif
  337. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  338. __attribute__ ((unused));
  339. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  340. {
  341. seq_printf(m, "type : ahash\n");
  342. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  343. "yes" : "no");
  344. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  345. seq_printf(m, "digestsize : %u\n",
  346. __crypto_hash_alg_common(alg)->digestsize);
  347. }
  348. const struct crypto_type crypto_ahash_type = {
  349. .extsize = crypto_ahash_extsize,
  350. .init_tfm = crypto_ahash_init_tfm,
  351. #ifdef CONFIG_PROC_FS
  352. .show = crypto_ahash_show,
  353. #endif
  354. .report = crypto_ahash_report,
  355. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  356. .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
  357. .type = CRYPTO_ALG_TYPE_AHASH,
  358. .tfmsize = offsetof(struct crypto_ahash, base),
  359. };
  360. EXPORT_SYMBOL_GPL(crypto_ahash_type);
  361. struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
  362. u32 mask)
  363. {
  364. return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
  365. }
  366. EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
  367. static int ahash_prepare_alg(struct ahash_alg *alg)
  368. {
  369. struct crypto_alg *base = &alg->halg.base;
  370. if (alg->halg.digestsize > PAGE_SIZE / 8 ||
  371. alg->halg.statesize > PAGE_SIZE / 8)
  372. return -EINVAL;
  373. base->cra_type = &crypto_ahash_type;
  374. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  375. base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
  376. return 0;
  377. }
  378. int crypto_register_ahash(struct ahash_alg *alg)
  379. {
  380. struct crypto_alg *base = &alg->halg.base;
  381. int err;
  382. err = ahash_prepare_alg(alg);
  383. if (err)
  384. return err;
  385. return crypto_register_alg(base);
  386. }
  387. EXPORT_SYMBOL_GPL(crypto_register_ahash);
  388. int crypto_unregister_ahash(struct ahash_alg *alg)
  389. {
  390. return crypto_unregister_alg(&alg->halg.base);
  391. }
  392. EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
  393. int ahash_register_instance(struct crypto_template *tmpl,
  394. struct ahash_instance *inst)
  395. {
  396. int err;
  397. err = ahash_prepare_alg(&inst->alg);
  398. if (err)
  399. return err;
  400. return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
  401. }
  402. EXPORT_SYMBOL_GPL(ahash_register_instance);
  403. void ahash_free_instance(struct crypto_instance *inst)
  404. {
  405. crypto_drop_spawn(crypto_instance_ctx(inst));
  406. kfree(ahash_instance(inst));
  407. }
  408. EXPORT_SYMBOL_GPL(ahash_free_instance);
  409. int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
  410. struct hash_alg_common *alg,
  411. struct crypto_instance *inst)
  412. {
  413. return crypto_init_spawn2(&spawn->base, &alg->base, inst,
  414. &crypto_ahash_type);
  415. }
  416. EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn);
  417. struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
  418. {
  419. struct crypto_alg *alg;
  420. alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask);
  421. return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg);
  422. }
  423. EXPORT_SYMBOL_GPL(ahash_attr_alg);
  424. MODULE_LICENSE("GPL");
  425. MODULE_DESCRIPTION("Asynchronous cryptographic hash type");