ahash.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
  89. unsigned int keylen)
  90. {
  91. struct ahash_alg *ahash = crypto_ahash_alg(tfm);
  92. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  93. int ret;
  94. u8 *buffer, *alignbuffer;
  95. unsigned long absize;
  96. absize = keylen + alignmask;
  97. buffer = kmalloc(absize, GFP_ATOMIC);
  98. if (!buffer)
  99. return -ENOMEM;
  100. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  101. memcpy(alignbuffer, key, keylen);
  102. ret = ahash->setkey(tfm, alignbuffer, keylen);
  103. memset(alignbuffer, 0, keylen);
  104. kfree(buffer);
  105. return ret;
  106. }
  107. static int ahash_setkey(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. if ((unsigned long)key & alignmask)
  113. return ahash_setkey_unaligned(tfm, key, keylen);
  114. return ahash->setkey(tfm, key, keylen);
  115. }
  116. static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type,
  117. u32 mask)
  118. {
  119. return alg->cra_ctxsize;
  120. }
  121. static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  122. {
  123. struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash;
  124. struct ahash_tfm *crt = &tfm->crt_ahash;
  125. if (alg->digestsize > PAGE_SIZE / 8)
  126. return -EINVAL;
  127. crt->init = alg->init;
  128. crt->update = alg->update;
  129. crt->final = alg->final;
  130. crt->digest = alg->digest;
  131. crt->setkey = ahash_setkey;
  132. crt->digestsize = alg->digestsize;
  133. return 0;
  134. }
  135. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  136. __attribute__ ((unused));
  137. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  138. {
  139. seq_printf(m, "type : ahash\n");
  140. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  141. "yes" : "no");
  142. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  143. seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize);
  144. }
  145. const struct crypto_type crypto_ahash_type = {
  146. .ctxsize = crypto_ahash_ctxsize,
  147. .init = crypto_init_ahash_ops,
  148. #ifdef CONFIG_PROC_FS
  149. .show = crypto_ahash_show,
  150. #endif
  151. };
  152. EXPORT_SYMBOL_GPL(crypto_ahash_type);
  153. MODULE_LICENSE("GPL");
  154. MODULE_DESCRIPTION("Asynchronous cryptographic hash type");