ahash.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 int hash_walk_next(struct crypto_hash_walk *walk)
  25. {
  26. unsigned int alignmask = walk->alignmask;
  27. unsigned int offset = walk->offset;
  28. unsigned int nbytes = min(walk->entrylen,
  29. ((unsigned int)(PAGE_SIZE)) - offset);
  30. walk->data = crypto_kmap(walk->pg, 0);
  31. walk->data += offset;
  32. if (offset & alignmask)
  33. nbytes = alignmask + 1 - (offset & alignmask);
  34. walk->entrylen -= nbytes;
  35. return nbytes;
  36. }
  37. static int hash_walk_new_entry(struct crypto_hash_walk *walk)
  38. {
  39. struct scatterlist *sg;
  40. sg = walk->sg;
  41. walk->pg = sg_page(sg);
  42. walk->offset = sg->offset;
  43. walk->entrylen = sg->length;
  44. if (walk->entrylen > walk->total)
  45. walk->entrylen = walk->total;
  46. walk->total -= walk->entrylen;
  47. return hash_walk_next(walk);
  48. }
  49. int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
  50. {
  51. unsigned int alignmask = walk->alignmask;
  52. unsigned int nbytes = walk->entrylen;
  53. walk->data -= walk->offset;
  54. if (nbytes && walk->offset & alignmask && !err) {
  55. walk->offset += alignmask - 1;
  56. walk->offset = ALIGN(walk->offset, alignmask + 1);
  57. walk->data += walk->offset;
  58. nbytes = min(nbytes,
  59. ((unsigned int)(PAGE_SIZE)) - walk->offset);
  60. walk->entrylen -= nbytes;
  61. return nbytes;
  62. }
  63. crypto_kunmap(walk->data, 0);
  64. crypto_yield(walk->flags);
  65. if (err)
  66. return err;
  67. if (nbytes) {
  68. walk->offset = 0;
  69. walk->pg++;
  70. return hash_walk_next(walk);
  71. }
  72. if (!walk->total)
  73. return 0;
  74. walk->sg = scatterwalk_sg_next(walk->sg);
  75. return hash_walk_new_entry(walk);
  76. }
  77. EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
  78. int crypto_hash_walk_first(struct ahash_request *req,
  79. struct crypto_hash_walk *walk)
  80. {
  81. walk->total = req->nbytes;
  82. if (!walk->total)
  83. return 0;
  84. walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
  85. walk->sg = req->src;
  86. walk->flags = req->base.flags;
  87. return hash_walk_new_entry(walk);
  88. }
  89. EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
  90. int crypto_hash_walk_first_compat(struct hash_desc *hdesc,
  91. struct crypto_hash_walk *walk,
  92. struct scatterlist *sg, unsigned int len)
  93. {
  94. walk->total = len;
  95. if (!walk->total)
  96. return 0;
  97. walk->alignmask = crypto_hash_alignmask(hdesc->tfm);
  98. walk->sg = sg;
  99. walk->flags = hdesc->flags;
  100. return hash_walk_new_entry(walk);
  101. }
  102. static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
  103. unsigned int keylen)
  104. {
  105. struct ahash_alg *ahash = crypto_ahash_alg(tfm);
  106. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  107. int ret;
  108. u8 *buffer, *alignbuffer;
  109. unsigned long absize;
  110. absize = keylen + alignmask;
  111. buffer = kmalloc(absize, GFP_ATOMIC);
  112. if (!buffer)
  113. return -ENOMEM;
  114. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  115. memcpy(alignbuffer, key, keylen);
  116. ret = ahash->setkey(tfm, alignbuffer, keylen);
  117. memset(alignbuffer, 0, keylen);
  118. kfree(buffer);
  119. return ret;
  120. }
  121. static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  122. unsigned int keylen)
  123. {
  124. struct ahash_alg *ahash = crypto_ahash_alg(tfm);
  125. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  126. if ((unsigned long)key & alignmask)
  127. return ahash_setkey_unaligned(tfm, key, keylen);
  128. return ahash->setkey(tfm, key, keylen);
  129. }
  130. static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
  131. unsigned int keylen)
  132. {
  133. return -ENOSYS;
  134. }
  135. int crypto_ahash_import(struct ahash_request *req, const u8 *in)
  136. {
  137. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  138. struct ahash_alg *alg = crypto_ahash_alg(tfm);
  139. memcpy(ahash_request_ctx(req), in, crypto_ahash_reqsize(tfm));
  140. if (alg->reinit)
  141. alg->reinit(req);
  142. return 0;
  143. }
  144. EXPORT_SYMBOL_GPL(crypto_ahash_import);
  145. static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type,
  146. u32 mask)
  147. {
  148. return alg->cra_ctxsize;
  149. }
  150. static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  151. {
  152. struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash;
  153. struct ahash_tfm *crt = &tfm->crt_ahash;
  154. if (alg->digestsize > PAGE_SIZE / 8)
  155. return -EINVAL;
  156. crt->init = alg->init;
  157. crt->update = alg->update;
  158. crt->final = alg->final;
  159. crt->digest = alg->digest;
  160. crt->setkey = alg->setkey ? ahash_setkey : ahash_nosetkey;
  161. crt->digestsize = alg->digestsize;
  162. return 0;
  163. }
  164. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  165. __attribute__ ((unused));
  166. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  167. {
  168. seq_printf(m, "type : ahash\n");
  169. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  170. "yes" : "no");
  171. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  172. seq_printf(m, "digestsize : %u\n", alg->cra_ahash.digestsize);
  173. }
  174. const struct crypto_type crypto_ahash_type = {
  175. .ctxsize = crypto_ahash_ctxsize,
  176. .init = crypto_init_ahash_ops,
  177. #ifdef CONFIG_PROC_FS
  178. .show = crypto_ahash_show,
  179. #endif
  180. };
  181. EXPORT_SYMBOL_GPL(crypto_ahash_type);
  182. MODULE_LICENSE("GPL");
  183. MODULE_DESCRIPTION("Asynchronous cryptographic hash type");