ahash.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. kzfree(buffer);
  123. return ret;
  124. }
  125. static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  126. unsigned int keylen)
  127. {
  128. struct ahash_alg *ahash = crypto_ahash_alg(tfm);
  129. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  130. if ((unsigned long)key & alignmask)
  131. return ahash_setkey_unaligned(tfm, key, keylen);
  132. return ahash->setkey(tfm, key, keylen);
  133. }
  134. static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
  135. unsigned int keylen)
  136. {
  137. return -ENOSYS;
  138. }
  139. static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
  140. {
  141. struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
  142. struct ahash_alg *alg = crypto_ahash_alg(hash);
  143. if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
  144. return crypto_init_shash_ops_async(tfm);
  145. hash->init = alg->init;
  146. hash->update = alg->update;
  147. hash->final = alg->final;
  148. hash->digest = alg->digest;
  149. hash->setkey = alg->setkey ? ahash_setkey : ahash_nosetkey;
  150. return 0;
  151. }
  152. static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
  153. {
  154. if (alg->cra_type == &crypto_ahash_type)
  155. return alg->cra_ctxsize;
  156. return sizeof(struct crypto_shash *);
  157. }
  158. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  159. __attribute__ ((unused));
  160. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  161. {
  162. seq_printf(m, "type : ahash\n");
  163. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  164. "yes" : "no");
  165. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  166. seq_printf(m, "digestsize : %u\n",
  167. __crypto_hash_alg_common(alg)->digestsize);
  168. }
  169. const struct crypto_type crypto_ahash_type = {
  170. .extsize = crypto_ahash_extsize,
  171. .init_tfm = crypto_ahash_init_tfm,
  172. #ifdef CONFIG_PROC_FS
  173. .show = crypto_ahash_show,
  174. #endif
  175. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  176. .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
  177. .type = CRYPTO_ALG_TYPE_AHASH,
  178. .tfmsize = offsetof(struct crypto_ahash, base),
  179. };
  180. EXPORT_SYMBOL_GPL(crypto_ahash_type);
  181. struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
  182. u32 mask)
  183. {
  184. return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
  185. }
  186. EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
  187. static int ahash_prepare_alg(struct ahash_alg *alg)
  188. {
  189. struct crypto_alg *base = &alg->halg.base;
  190. if (alg->halg.digestsize > PAGE_SIZE / 8 ||
  191. alg->halg.statesize > PAGE_SIZE / 8)
  192. return -EINVAL;
  193. base->cra_type = &crypto_ahash_type;
  194. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  195. base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
  196. return 0;
  197. }
  198. int crypto_register_ahash(struct ahash_alg *alg)
  199. {
  200. struct crypto_alg *base = &alg->halg.base;
  201. int err;
  202. err = ahash_prepare_alg(alg);
  203. if (err)
  204. return err;
  205. return crypto_register_alg(base);
  206. }
  207. EXPORT_SYMBOL_GPL(crypto_register_ahash);
  208. int crypto_unregister_ahash(struct ahash_alg *alg)
  209. {
  210. return crypto_unregister_alg(&alg->halg.base);
  211. }
  212. EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
  213. int ahash_register_instance(struct crypto_template *tmpl,
  214. struct ahash_instance *inst)
  215. {
  216. int err;
  217. err = ahash_prepare_alg(&inst->alg);
  218. if (err)
  219. return err;
  220. return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
  221. }
  222. EXPORT_SYMBOL_GPL(ahash_register_instance);
  223. void ahash_free_instance(struct crypto_instance *inst)
  224. {
  225. crypto_drop_spawn(crypto_instance_ctx(inst));
  226. kfree(ahash_instance(inst));
  227. }
  228. EXPORT_SYMBOL_GPL(ahash_free_instance);
  229. int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
  230. struct hash_alg_common *alg,
  231. struct crypto_instance *inst)
  232. {
  233. return crypto_init_spawn2(&spawn->base, &alg->base, inst,
  234. &crypto_ahash_type);
  235. }
  236. EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn);
  237. struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
  238. {
  239. struct crypto_alg *alg;
  240. alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask);
  241. return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg);
  242. }
  243. EXPORT_SYMBOL_GPL(ahash_attr_alg);
  244. MODULE_LICENSE("GPL");
  245. MODULE_DESCRIPTION("Asynchronous cryptographic hash type");