sha3.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Crypto API support for SHA-3
  4. * (https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf)
  5. */
  6. #include <crypto/internal/hash.h>
  7. #include <crypto/sha3.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #define SHA3_CTX(desc) ((struct sha3_ctx *)shash_desc_ctx(desc))
  11. static int crypto_sha3_224_init(struct shash_desc *desc)
  12. {
  13. sha3_224_init(SHA3_CTX(desc));
  14. return 0;
  15. }
  16. static int crypto_sha3_256_init(struct shash_desc *desc)
  17. {
  18. sha3_256_init(SHA3_CTX(desc));
  19. return 0;
  20. }
  21. static int crypto_sha3_384_init(struct shash_desc *desc)
  22. {
  23. sha3_384_init(SHA3_CTX(desc));
  24. return 0;
  25. }
  26. static int crypto_sha3_512_init(struct shash_desc *desc)
  27. {
  28. sha3_512_init(SHA3_CTX(desc));
  29. return 0;
  30. }
  31. static int crypto_sha3_update(struct shash_desc *desc, const u8 *data,
  32. unsigned int len)
  33. {
  34. sha3_update(SHA3_CTX(desc), data, len);
  35. return 0;
  36. }
  37. static int crypto_sha3_final(struct shash_desc *desc, u8 *out)
  38. {
  39. sha3_final(SHA3_CTX(desc), out);
  40. return 0;
  41. }
  42. static int crypto_sha3_224_digest(struct shash_desc *desc,
  43. const u8 *data, unsigned int len, u8 *out)
  44. {
  45. sha3_224(data, len, out);
  46. return 0;
  47. }
  48. static int crypto_sha3_256_digest(struct shash_desc *desc,
  49. const u8 *data, unsigned int len, u8 *out)
  50. {
  51. sha3_256(data, len, out);
  52. return 0;
  53. }
  54. static int crypto_sha3_384_digest(struct shash_desc *desc,
  55. const u8 *data, unsigned int len, u8 *out)
  56. {
  57. sha3_384(data, len, out);
  58. return 0;
  59. }
  60. static int crypto_sha3_512_digest(struct shash_desc *desc,
  61. const u8 *data, unsigned int len, u8 *out)
  62. {
  63. sha3_512(data, len, out);
  64. return 0;
  65. }
  66. static int crypto_sha3_export_core(struct shash_desc *desc, void *out)
  67. {
  68. memcpy(out, SHA3_CTX(desc), sizeof(struct sha3_ctx));
  69. return 0;
  70. }
  71. static int crypto_sha3_import_core(struct shash_desc *desc, const void *in)
  72. {
  73. memcpy(SHA3_CTX(desc), in, sizeof(struct sha3_ctx));
  74. return 0;
  75. }
  76. static struct shash_alg algs[] = { {
  77. .digestsize = SHA3_224_DIGEST_SIZE,
  78. .init = crypto_sha3_224_init,
  79. .update = crypto_sha3_update,
  80. .final = crypto_sha3_final,
  81. .digest = crypto_sha3_224_digest,
  82. .export_core = crypto_sha3_export_core,
  83. .import_core = crypto_sha3_import_core,
  84. .descsize = sizeof(struct sha3_ctx),
  85. .base.cra_name = "sha3-224",
  86. .base.cra_driver_name = "sha3-224-lib",
  87. .base.cra_blocksize = SHA3_224_BLOCK_SIZE,
  88. .base.cra_module = THIS_MODULE,
  89. }, {
  90. .digestsize = SHA3_256_DIGEST_SIZE,
  91. .init = crypto_sha3_256_init,
  92. .update = crypto_sha3_update,
  93. .final = crypto_sha3_final,
  94. .digest = crypto_sha3_256_digest,
  95. .export_core = crypto_sha3_export_core,
  96. .import_core = crypto_sha3_import_core,
  97. .descsize = sizeof(struct sha3_ctx),
  98. .base.cra_name = "sha3-256",
  99. .base.cra_driver_name = "sha3-256-lib",
  100. .base.cra_blocksize = SHA3_256_BLOCK_SIZE,
  101. .base.cra_module = THIS_MODULE,
  102. }, {
  103. .digestsize = SHA3_384_DIGEST_SIZE,
  104. .init = crypto_sha3_384_init,
  105. .update = crypto_sha3_update,
  106. .final = crypto_sha3_final,
  107. .digest = crypto_sha3_384_digest,
  108. .export_core = crypto_sha3_export_core,
  109. .import_core = crypto_sha3_import_core,
  110. .descsize = sizeof(struct sha3_ctx),
  111. .base.cra_name = "sha3-384",
  112. .base.cra_driver_name = "sha3-384-lib",
  113. .base.cra_blocksize = SHA3_384_BLOCK_SIZE,
  114. .base.cra_module = THIS_MODULE,
  115. }, {
  116. .digestsize = SHA3_512_DIGEST_SIZE,
  117. .init = crypto_sha3_512_init,
  118. .update = crypto_sha3_update,
  119. .final = crypto_sha3_final,
  120. .digest = crypto_sha3_512_digest,
  121. .export_core = crypto_sha3_export_core,
  122. .import_core = crypto_sha3_import_core,
  123. .descsize = sizeof(struct sha3_ctx),
  124. .base.cra_name = "sha3-512",
  125. .base.cra_driver_name = "sha3-512-lib",
  126. .base.cra_blocksize = SHA3_512_BLOCK_SIZE,
  127. .base.cra_module = THIS_MODULE,
  128. } };
  129. static int __init crypto_sha3_mod_init(void)
  130. {
  131. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  132. }
  133. module_init(crypto_sha3_mod_init);
  134. static void __exit crypto_sha3_mod_exit(void)
  135. {
  136. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  137. }
  138. module_exit(crypto_sha3_mod_exit);
  139. MODULE_LICENSE("GPL");
  140. MODULE_DESCRIPTION("Crypto API support for SHA-3");
  141. MODULE_ALIAS_CRYPTO("sha3-224");
  142. MODULE_ALIAS_CRYPTO("sha3-224-lib");
  143. MODULE_ALIAS_CRYPTO("sha3-256");
  144. MODULE_ALIAS_CRYPTO("sha3-256-lib");
  145. MODULE_ALIAS_CRYPTO("sha3-384");
  146. MODULE_ALIAS_CRYPTO("sha3-384-lib");
  147. MODULE_ALIAS_CRYPTO("sha3-512");
  148. MODULE_ALIAS_CRYPTO("sha3-512-lib");