ahash.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
  25. {
  26. return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
  27. halg);
  28. }
  29. static int hash_walk_next(struct crypto_hash_walk *walk)
  30. {
  31. unsigned int alignmask = walk->alignmask;
  32. unsigned int offset = walk->offset;
  33. unsigned int nbytes = min(walk->entrylen,
  34. ((unsigned int)(PAGE_SIZE)) - offset);
  35. walk->data = crypto_kmap(walk->pg, 0);
  36. walk->data += offset;
  37. if (offset & alignmask)
  38. nbytes = alignmask + 1 - (offset & alignmask);
  39. walk->entrylen -= nbytes;
  40. return nbytes;
  41. }
  42. static int hash_walk_new_entry(struct crypto_hash_walk *walk)
  43. {
  44. struct scatterlist *sg;
  45. sg = walk->sg;
  46. walk->pg = sg_page(sg);
  47. walk->offset = sg->offset;
  48. walk->entrylen = sg->length;
  49. if (walk->entrylen > walk->total)
  50. walk->entrylen = walk->total;
  51. walk->total -= walk->entrylen;
  52. return hash_walk_next(walk);
  53. }
  54. int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
  55. {
  56. unsigned int alignmask = walk->alignmask;
  57. unsigned int nbytes = walk->entrylen;
  58. walk->data -= walk->offset;
  59. if (nbytes && walk->offset & alignmask && !err) {
  60. walk->offset += alignmask - 1;
  61. walk->offset = ALIGN(walk->offset, alignmask + 1);
  62. walk->data += walk->offset;
  63. nbytes = min(nbytes,
  64. ((unsigned int)(PAGE_SIZE)) - walk->offset);
  65. walk->entrylen -= nbytes;
  66. return nbytes;
  67. }
  68. crypto_kunmap(walk->data, 0);
  69. crypto_yield(walk->flags);
  70. if (err)
  71. return err;
  72. if (nbytes) {
  73. walk->offset = 0;
  74. walk->pg++;
  75. return hash_walk_next(walk);
  76. }
  77. if (!walk->total)
  78. return 0;
  79. walk->sg = scatterwalk_sg_next(walk->sg);
  80. return hash_walk_new_entry(walk);
  81. }
  82. EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
  83. int crypto_hash_walk_first(struct ahash_request *req,
  84. struct crypto_hash_walk *walk)
  85. {
  86. walk->total = req->nbytes;
  87. if (!walk->total)
  88. return 0;
  89. walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
  90. walk->sg = req->src;
  91. walk->flags = req->base.flags;
  92. return hash_walk_new_entry(walk);
  93. }
  94. EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
  95. int crypto_hash_walk_first_compat(struct hash_desc *hdesc,
  96. struct crypto_hash_walk *walk,
  97. struct scatterlist *sg, unsigned int len)
  98. {
  99. walk->total = len;
  100. if (!walk->total)
  101. return 0;
  102. walk->alignmask = crypto_hash_alignmask(hdesc->tfm);
  103. walk->sg = sg;
  104. walk->flags = hdesc->flags;
  105. return hash_walk_new_entry(walk);
  106. }
  107. static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
  108. unsigned int keylen)
  109. {
  110. struct ahash_alg *ahash = crypto_ahash_alg(tfm);
  111. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  112. int ret;
  113. u8 *buffer, *alignbuffer;
  114. unsigned long absize;
  115. absize = keylen + alignmask;
  116. buffer = kmalloc(absize, GFP_ATOMIC);
  117. if (!buffer)
  118. return -ENOMEM;
  119. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  120. memcpy(alignbuffer, key, keylen);
  121. ret = ahash->setkey(tfm, alignbuffer, keylen);
  122. memset(alignbuffer, 0, keylen);
  123. kfree(buffer);
  124. return ret;
  125. }
  126. static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  127. unsigned int keylen)
  128. {
  129. struct ahash_alg *ahash = crypto_ahash_alg(tfm);
  130. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  131. if ((unsigned long)key & alignmask)
  132. return ahash_setkey_unaligned(tfm, key, keylen);
  133. return ahash->setkey(tfm, key, keylen);
  134. }
  135. static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
  136. unsigned int keylen)
  137. {
  138. return -ENOSYS;
  139. }
  140. static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  141. {
  142. struct old_ahash_alg *alg = &tfm->__crt_alg->cra_ahash;
  143. struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
  144. struct ahash_alg *nalg = crypto_ahash_alg(crt);
  145. if (alg->digestsize > PAGE_SIZE / 8)
  146. return -EINVAL;
  147. crt->init = alg->init;
  148. crt->update = alg->update;
  149. crt->final = alg->final;
  150. crt->digest = alg->digest;
  151. crt->setkey = alg->setkey ? ahash_setkey : ahash_nosetkey;
  152. crt->digestsize = alg->digestsize;
  153. nalg->setkey = alg->setkey;
  154. nalg->halg.digestsize = alg->digestsize;
  155. return 0;
  156. }
  157. static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
  158. {
  159. struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
  160. struct ahash_alg *alg = crypto_ahash_alg(hash);
  161. struct old_ahash_alg *oalg = crypto_old_ahash_alg(hash);
  162. if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
  163. return crypto_init_shash_ops_async(tfm);
  164. if (oalg->init)
  165. return crypto_init_ahash_ops(tfm, 0, 0);
  166. hash->init = alg->init;
  167. hash->update = alg->update;
  168. hash->final = alg->final;
  169. hash->digest = alg->digest;
  170. hash->setkey = alg->setkey ? ahash_setkey : ahash_nosetkey;
  171. hash->digestsize = alg->halg.digestsize;
  172. return 0;
  173. }
  174. static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
  175. {
  176. if (alg->cra_type == &crypto_ahash_type)
  177. return alg->cra_ctxsize;
  178. return sizeof(struct crypto_shash *);
  179. }
  180. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  181. __attribute__ ((unused));
  182. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  183. {
  184. seq_printf(m, "type : ahash\n");
  185. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  186. "yes" : "no");
  187. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  188. seq_printf(m, "digestsize : %u\n",
  189. __crypto_hash_alg_common(alg)->digestsize);
  190. }
  191. const struct crypto_type crypto_ahash_type = {
  192. .extsize = crypto_ahash_extsize,
  193. .init_tfm = crypto_ahash_init_tfm,
  194. #ifdef CONFIG_PROC_FS
  195. .show = crypto_ahash_show,
  196. #endif
  197. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  198. .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
  199. .type = CRYPTO_ALG_TYPE_AHASH,
  200. .tfmsize = offsetof(struct crypto_ahash, base),
  201. };
  202. EXPORT_SYMBOL_GPL(crypto_ahash_type);
  203. struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
  204. u32 mask)
  205. {
  206. return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
  207. }
  208. EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
  209. static int ahash_prepare_alg(struct ahash_alg *alg)
  210. {
  211. struct crypto_alg *base = &alg->halg.base;
  212. if (alg->halg.digestsize > PAGE_SIZE / 8 ||
  213. alg->halg.statesize > PAGE_SIZE / 8)
  214. return -EINVAL;
  215. base->cra_type = &crypto_ahash_type;
  216. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  217. base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
  218. return 0;
  219. }
  220. int crypto_register_ahash(struct ahash_alg *alg)
  221. {
  222. struct crypto_alg *base = &alg->halg.base;
  223. int err;
  224. err = ahash_prepare_alg(alg);
  225. if (err)
  226. return err;
  227. return crypto_register_alg(base);
  228. }
  229. EXPORT_SYMBOL_GPL(crypto_register_ahash);
  230. int crypto_unregister_ahash(struct ahash_alg *alg)
  231. {
  232. return crypto_unregister_alg(&alg->halg.base);
  233. }
  234. EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
  235. int ahash_register_instance(struct crypto_template *tmpl,
  236. struct ahash_instance *inst)
  237. {
  238. int err;
  239. err = ahash_prepare_alg(&inst->alg);
  240. if (err)
  241. return err;
  242. return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
  243. }
  244. EXPORT_SYMBOL_GPL(ahash_register_instance);
  245. void ahash_free_instance(struct crypto_instance *inst)
  246. {
  247. crypto_drop_spawn(crypto_instance_ctx(inst));
  248. kfree(ahash_instance(inst));
  249. }
  250. EXPORT_SYMBOL_GPL(ahash_free_instance);
  251. int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
  252. struct hash_alg_common *alg,
  253. struct crypto_instance *inst)
  254. {
  255. return crypto_init_spawn2(&spawn->base, &alg->base, inst,
  256. &crypto_ahash_type);
  257. }
  258. EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn);
  259. struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
  260. {
  261. struct crypto_alg *alg;
  262. alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask);
  263. return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg);
  264. }
  265. EXPORT_SYMBOL_GPL(ahash_attr_alg);
  266. MODULE_LICENSE("GPL");
  267. MODULE_DESCRIPTION("Asynchronous cryptographic hash type");