ahash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 "internal.h"
  24. struct ahash_request_priv {
  25. crypto_completion_t complete;
  26. void *data;
  27. u8 *result;
  28. void *ubuf[] CRYPTO_MINALIGN_ATTR;
  29. };
  30. static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
  31. {
  32. return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
  33. halg);
  34. }
  35. static int hash_walk_next(struct crypto_hash_walk *walk)
  36. {
  37. unsigned int alignmask = walk->alignmask;
  38. unsigned int offset = walk->offset;
  39. unsigned int nbytes = min(walk->entrylen,
  40. ((unsigned int)(PAGE_SIZE)) - offset);
  41. walk->data = crypto_kmap(walk->pg, 0);
  42. walk->data += offset;
  43. if (offset & alignmask)
  44. nbytes = alignmask + 1 - (offset & alignmask);
  45. walk->entrylen -= nbytes;
  46. return nbytes;
  47. }
  48. static int hash_walk_new_entry(struct crypto_hash_walk *walk)
  49. {
  50. struct scatterlist *sg;
  51. sg = walk->sg;
  52. walk->pg = sg_page(sg);
  53. walk->offset = sg->offset;
  54. walk->entrylen = sg->length;
  55. if (walk->entrylen > walk->total)
  56. walk->entrylen = walk->total;
  57. walk->total -= walk->entrylen;
  58. return hash_walk_next(walk);
  59. }
  60. int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
  61. {
  62. unsigned int alignmask = walk->alignmask;
  63. unsigned int nbytes = walk->entrylen;
  64. walk->data -= walk->offset;
  65. if (nbytes && walk->offset & alignmask && !err) {
  66. walk->offset = ALIGN(walk->offset, alignmask + 1);
  67. walk->data += walk->offset;
  68. nbytes = min(nbytes,
  69. ((unsigned int)(PAGE_SIZE)) - walk->offset);
  70. walk->entrylen -= nbytes;
  71. return nbytes;
  72. }
  73. crypto_kunmap(walk->data, 0);
  74. crypto_yield(walk->flags);
  75. if (err)
  76. return err;
  77. if (nbytes) {
  78. walk->offset = 0;
  79. walk->pg++;
  80. return hash_walk_next(walk);
  81. }
  82. if (!walk->total)
  83. return 0;
  84. walk->sg = scatterwalk_sg_next(walk->sg);
  85. return hash_walk_new_entry(walk);
  86. }
  87. EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
  88. int crypto_hash_walk_first(struct ahash_request *req,
  89. struct crypto_hash_walk *walk)
  90. {
  91. walk->total = req->nbytes;
  92. if (!walk->total)
  93. return 0;
  94. walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
  95. walk->sg = req->src;
  96. walk->flags = req->base.flags;
  97. return hash_walk_new_entry(walk);
  98. }
  99. EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
  100. int crypto_hash_walk_first_compat(struct hash_desc *hdesc,
  101. struct crypto_hash_walk *walk,
  102. struct scatterlist *sg, unsigned int len)
  103. {
  104. walk->total = len;
  105. if (!walk->total)
  106. return 0;
  107. walk->alignmask = crypto_hash_alignmask(hdesc->tfm);
  108. walk->sg = sg;
  109. walk->flags = hdesc->flags;
  110. return hash_walk_new_entry(walk);
  111. }
  112. static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
  113. unsigned int keylen)
  114. {
  115. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  116. int ret;
  117. u8 *buffer, *alignbuffer;
  118. unsigned long absize;
  119. absize = keylen + alignmask;
  120. buffer = kmalloc(absize, GFP_KERNEL);
  121. if (!buffer)
  122. return -ENOMEM;
  123. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  124. memcpy(alignbuffer, key, keylen);
  125. ret = tfm->setkey(tfm, alignbuffer, keylen);
  126. kzfree(buffer);
  127. return ret;
  128. }
  129. int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  130. unsigned int keylen)
  131. {
  132. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  133. if ((unsigned long)key & alignmask)
  134. return ahash_setkey_unaligned(tfm, key, keylen);
  135. return tfm->setkey(tfm, key, keylen);
  136. }
  137. EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
  138. static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
  139. unsigned int keylen)
  140. {
  141. return -ENOSYS;
  142. }
  143. static inline unsigned int ahash_align_buffer_size(unsigned len,
  144. unsigned long mask)
  145. {
  146. return len + (mask & ~(crypto_tfm_ctx_alignment() - 1));
  147. }
  148. static void ahash_op_unaligned_finish(struct ahash_request *req, int err)
  149. {
  150. struct ahash_request_priv *priv = req->priv;
  151. if (err == -EINPROGRESS)
  152. return;
  153. if (!err)
  154. memcpy(priv->result, req->result,
  155. crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
  156. kzfree(priv);
  157. }
  158. static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
  159. {
  160. struct ahash_request *areq = req->data;
  161. struct ahash_request_priv *priv = areq->priv;
  162. crypto_completion_t complete = priv->complete;
  163. void *data = priv->data;
  164. ahash_op_unaligned_finish(areq, err);
  165. complete(data, err);
  166. }
  167. static int ahash_op_unaligned(struct ahash_request *req,
  168. int (*op)(struct ahash_request *))
  169. {
  170. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  171. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  172. unsigned int ds = crypto_ahash_digestsize(tfm);
  173. struct ahash_request_priv *priv;
  174. int err;
  175. priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
  176. (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  177. GFP_KERNEL : GFP_ATOMIC);
  178. if (!priv)
  179. return -ENOMEM;
  180. priv->result = req->result;
  181. priv->complete = req->base.complete;
  182. priv->data = req->base.data;
  183. req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
  184. req->base.complete = ahash_op_unaligned_done;
  185. req->base.data = req;
  186. req->priv = priv;
  187. err = op(req);
  188. ahash_op_unaligned_finish(req, err);
  189. return err;
  190. }
  191. static int crypto_ahash_op(struct ahash_request *req,
  192. int (*op)(struct ahash_request *))
  193. {
  194. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  195. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  196. if ((unsigned long)req->result & alignmask)
  197. return ahash_op_unaligned(req, op);
  198. return op(req);
  199. }
  200. int crypto_ahash_final(struct ahash_request *req)
  201. {
  202. return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
  203. }
  204. EXPORT_SYMBOL_GPL(crypto_ahash_final);
  205. int crypto_ahash_finup(struct ahash_request *req)
  206. {
  207. return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
  208. }
  209. EXPORT_SYMBOL_GPL(crypto_ahash_finup);
  210. int crypto_ahash_digest(struct ahash_request *req)
  211. {
  212. return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->digest);
  213. }
  214. EXPORT_SYMBOL_GPL(crypto_ahash_digest);
  215. static void ahash_def_finup_finish2(struct ahash_request *req, int err)
  216. {
  217. struct ahash_request_priv *priv = req->priv;
  218. if (err == -EINPROGRESS)
  219. return;
  220. if (!err)
  221. memcpy(priv->result, req->result,
  222. crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
  223. kzfree(priv);
  224. }
  225. static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
  226. {
  227. struct ahash_request *areq = req->data;
  228. struct ahash_request_priv *priv = areq->priv;
  229. crypto_completion_t complete = priv->complete;
  230. void *data = priv->data;
  231. ahash_def_finup_finish2(areq, err);
  232. complete(data, err);
  233. }
  234. static int ahash_def_finup_finish1(struct ahash_request *req, int err)
  235. {
  236. if (err)
  237. goto out;
  238. req->base.complete = ahash_def_finup_done2;
  239. req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  240. err = crypto_ahash_reqtfm(req)->final(req);
  241. out:
  242. ahash_def_finup_finish2(req, err);
  243. return err;
  244. }
  245. static void ahash_def_finup_done1(struct crypto_async_request *req, int err)
  246. {
  247. struct ahash_request *areq = req->data;
  248. struct ahash_request_priv *priv = areq->priv;
  249. crypto_completion_t complete = priv->complete;
  250. void *data = priv->data;
  251. err = ahash_def_finup_finish1(areq, err);
  252. complete(data, err);
  253. }
  254. static int ahash_def_finup(struct ahash_request *req)
  255. {
  256. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  257. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  258. unsigned int ds = crypto_ahash_digestsize(tfm);
  259. struct ahash_request_priv *priv;
  260. priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
  261. (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  262. GFP_KERNEL : GFP_ATOMIC);
  263. if (!priv)
  264. return -ENOMEM;
  265. priv->result = req->result;
  266. priv->complete = req->base.complete;
  267. priv->data = req->base.data;
  268. req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
  269. req->base.complete = ahash_def_finup_done1;
  270. req->base.data = req;
  271. req->priv = priv;
  272. return ahash_def_finup_finish1(req, tfm->update(req));
  273. }
  274. static int ahash_no_export(struct ahash_request *req, void *out)
  275. {
  276. return -ENOSYS;
  277. }
  278. static int ahash_no_import(struct ahash_request *req, const void *in)
  279. {
  280. return -ENOSYS;
  281. }
  282. static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
  283. {
  284. struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
  285. struct ahash_alg *alg = crypto_ahash_alg(hash);
  286. hash->setkey = ahash_nosetkey;
  287. hash->export = ahash_no_export;
  288. hash->import = ahash_no_import;
  289. if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
  290. return crypto_init_shash_ops_async(tfm);
  291. hash->init = alg->init;
  292. hash->update = alg->update;
  293. hash->final = alg->final;
  294. hash->finup = alg->finup ?: ahash_def_finup;
  295. hash->digest = alg->digest;
  296. if (alg->setkey)
  297. hash->setkey = alg->setkey;
  298. if (alg->export)
  299. hash->export = alg->export;
  300. if (alg->import)
  301. hash->import = alg->import;
  302. return 0;
  303. }
  304. static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
  305. {
  306. if (alg->cra_type == &crypto_ahash_type)
  307. return alg->cra_ctxsize;
  308. return sizeof(struct crypto_shash *);
  309. }
  310. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  311. __attribute__ ((unused));
  312. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  313. {
  314. seq_printf(m, "type : ahash\n");
  315. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  316. "yes" : "no");
  317. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  318. seq_printf(m, "digestsize : %u\n",
  319. __crypto_hash_alg_common(alg)->digestsize);
  320. }
  321. const struct crypto_type crypto_ahash_type = {
  322. .extsize = crypto_ahash_extsize,
  323. .init_tfm = crypto_ahash_init_tfm,
  324. #ifdef CONFIG_PROC_FS
  325. .show = crypto_ahash_show,
  326. #endif
  327. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  328. .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
  329. .type = CRYPTO_ALG_TYPE_AHASH,
  330. .tfmsize = offsetof(struct crypto_ahash, base),
  331. };
  332. EXPORT_SYMBOL_GPL(crypto_ahash_type);
  333. struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
  334. u32 mask)
  335. {
  336. return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
  337. }
  338. EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
  339. static int ahash_prepare_alg(struct ahash_alg *alg)
  340. {
  341. struct crypto_alg *base = &alg->halg.base;
  342. if (alg->halg.digestsize > PAGE_SIZE / 8 ||
  343. alg->halg.statesize > PAGE_SIZE / 8)
  344. return -EINVAL;
  345. base->cra_type = &crypto_ahash_type;
  346. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  347. base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
  348. return 0;
  349. }
  350. int crypto_register_ahash(struct ahash_alg *alg)
  351. {
  352. struct crypto_alg *base = &alg->halg.base;
  353. int err;
  354. err = ahash_prepare_alg(alg);
  355. if (err)
  356. return err;
  357. return crypto_register_alg(base);
  358. }
  359. EXPORT_SYMBOL_GPL(crypto_register_ahash);
  360. int crypto_unregister_ahash(struct ahash_alg *alg)
  361. {
  362. return crypto_unregister_alg(&alg->halg.base);
  363. }
  364. EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
  365. int ahash_register_instance(struct crypto_template *tmpl,
  366. struct ahash_instance *inst)
  367. {
  368. int err;
  369. err = ahash_prepare_alg(&inst->alg);
  370. if (err)
  371. return err;
  372. return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
  373. }
  374. EXPORT_SYMBOL_GPL(ahash_register_instance);
  375. void ahash_free_instance(struct crypto_instance *inst)
  376. {
  377. crypto_drop_spawn(crypto_instance_ctx(inst));
  378. kfree(ahash_instance(inst));
  379. }
  380. EXPORT_SYMBOL_GPL(ahash_free_instance);
  381. int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
  382. struct hash_alg_common *alg,
  383. struct crypto_instance *inst)
  384. {
  385. return crypto_init_spawn2(&spawn->base, &alg->base, inst,
  386. &crypto_ahash_type);
  387. }
  388. EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn);
  389. struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
  390. {
  391. struct crypto_alg *alg;
  392. alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask);
  393. return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg);
  394. }
  395. EXPORT_SYMBOL_GPL(ahash_attr_alg);
  396. MODULE_LICENSE("GPL");
  397. MODULE_DESCRIPTION("Asynchronous cryptographic hash type");