ahash.c 5.5 KB

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