hash.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Cryptographic Hash operations.
  3. *
  4. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/seq_file.h>
  16. #include "internal.h"
  17. static unsigned int crypto_hash_ctxsize(struct crypto_alg *alg, u32 type,
  18. u32 mask)
  19. {
  20. return alg->cra_ctxsize;
  21. }
  22. static int hash_setkey_unaligned(struct crypto_hash *crt, const u8 *key,
  23. unsigned int keylen)
  24. {
  25. struct crypto_tfm *tfm = crypto_hash_tfm(crt);
  26. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  27. unsigned long alignmask = crypto_hash_alignmask(crt);
  28. int ret;
  29. u8 *buffer, *alignbuffer;
  30. unsigned long absize;
  31. absize = keylen + alignmask;
  32. buffer = kmalloc(absize, GFP_ATOMIC);
  33. if (!buffer)
  34. return -ENOMEM;
  35. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  36. memcpy(alignbuffer, key, keylen);
  37. ret = alg->setkey(crt, alignbuffer, keylen);
  38. memset(alignbuffer, 0, keylen);
  39. kfree(buffer);
  40. return ret;
  41. }
  42. static int hash_setkey(struct crypto_hash *crt, const u8 *key,
  43. unsigned int keylen)
  44. {
  45. struct crypto_tfm *tfm = crypto_hash_tfm(crt);
  46. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  47. unsigned long alignmask = crypto_hash_alignmask(crt);
  48. if ((unsigned long)key & alignmask)
  49. return hash_setkey_unaligned(crt, key, keylen);
  50. return alg->setkey(crt, key, keylen);
  51. }
  52. static int hash_async_setkey(struct crypto_ahash *tfm_async, const u8 *key,
  53. unsigned int keylen)
  54. {
  55. struct crypto_tfm *tfm = crypto_ahash_tfm(tfm_async);
  56. struct crypto_hash *tfm_hash = __crypto_hash_cast(tfm);
  57. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  58. return alg->setkey(tfm_hash, key, keylen);
  59. }
  60. static int hash_async_init(struct ahash_request *req)
  61. {
  62. struct crypto_tfm *tfm = req->base.tfm;
  63. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  64. struct hash_desc desc = {
  65. .tfm = __crypto_hash_cast(tfm),
  66. .flags = req->base.flags,
  67. };
  68. return alg->init(&desc);
  69. }
  70. static int hash_async_update(struct ahash_request *req)
  71. {
  72. struct crypto_tfm *tfm = req->base.tfm;
  73. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  74. struct hash_desc desc = {
  75. .tfm = __crypto_hash_cast(tfm),
  76. .flags = req->base.flags,
  77. };
  78. return alg->update(&desc, req->src, req->nbytes);
  79. }
  80. static int hash_async_final(struct ahash_request *req)
  81. {
  82. struct crypto_tfm *tfm = req->base.tfm;
  83. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  84. struct hash_desc desc = {
  85. .tfm = __crypto_hash_cast(tfm),
  86. .flags = req->base.flags,
  87. };
  88. return alg->final(&desc, req->result);
  89. }
  90. static int hash_async_digest(struct ahash_request *req)
  91. {
  92. struct crypto_tfm *tfm = req->base.tfm;
  93. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  94. struct hash_desc desc = {
  95. .tfm = __crypto_hash_cast(tfm),
  96. .flags = req->base.flags,
  97. };
  98. return alg->digest(&desc, req->src, req->nbytes, req->result);
  99. }
  100. static int crypto_init_hash_ops_async(struct crypto_tfm *tfm)
  101. {
  102. struct ahash_tfm *crt = &tfm->crt_ahash;
  103. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  104. crt->init = hash_async_init;
  105. crt->update = hash_async_update;
  106. crt->final = hash_async_final;
  107. crt->digest = hash_async_digest;
  108. crt->setkey = hash_async_setkey;
  109. crt->digestsize = alg->digestsize;
  110. return 0;
  111. }
  112. static int crypto_init_hash_ops_sync(struct crypto_tfm *tfm)
  113. {
  114. struct hash_tfm *crt = &tfm->crt_hash;
  115. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  116. crt->init = alg->init;
  117. crt->update = alg->update;
  118. crt->final = alg->final;
  119. crt->digest = alg->digest;
  120. crt->setkey = hash_setkey;
  121. crt->digestsize = alg->digestsize;
  122. return 0;
  123. }
  124. static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  125. {
  126. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  127. if (alg->digestsize > PAGE_SIZE / 8)
  128. return -EINVAL;
  129. if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) != CRYPTO_ALG_TYPE_HASH_MASK)
  130. return crypto_init_hash_ops_async(tfm);
  131. else
  132. return crypto_init_hash_ops_sync(tfm);
  133. }
  134. static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
  135. __attribute__ ((unused));
  136. static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
  137. {
  138. seq_printf(m, "type : hash\n");
  139. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  140. seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize);
  141. }
  142. const struct crypto_type crypto_hash_type = {
  143. .ctxsize = crypto_hash_ctxsize,
  144. .init = crypto_init_hash_ops,
  145. #ifdef CONFIG_PROC_FS
  146. .show = crypto_hash_show,
  147. #endif
  148. };
  149. EXPORT_SYMBOL_GPL(crypto_hash_type);
  150. MODULE_LICENSE("GPL");
  151. MODULE_DESCRIPTION("Generic cryptographic hash type");