ahash.c 13 KB

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