sha3-ce-glue.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * sha3-ce-glue.c - core SHA-3 transform using v8.2 Crypto Extensions
  4. *
  5. * Copyright (C) 2018 Linaro Ltd <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <asm/hwcap.h>
  12. #include <asm/neon.h>
  13. #include <asm/simd.h>
  14. #include <asm/unaligned.h>
  15. #include <crypto/internal/hash.h>
  16. #include <crypto/internal/simd.h>
  17. #include <crypto/sha3.h>
  18. #include <linux/cpufeature.h>
  19. #include <linux/crypto.h>
  20. #include <linux/module.h>
  21. MODULE_DESCRIPTION("SHA3 secure hash using ARMv8 Crypto Extensions");
  22. MODULE_AUTHOR("Ard Biesheuvel <[email protected]>");
  23. MODULE_LICENSE("GPL v2");
  24. MODULE_ALIAS_CRYPTO("sha3-224");
  25. MODULE_ALIAS_CRYPTO("sha3-256");
  26. MODULE_ALIAS_CRYPTO("sha3-384");
  27. MODULE_ALIAS_CRYPTO("sha3-512");
  28. asmlinkage int sha3_ce_transform(u64 *st, const u8 *data, int blocks,
  29. int md_len);
  30. static int sha3_update(struct shash_desc *desc, const u8 *data,
  31. unsigned int len)
  32. {
  33. struct sha3_state *sctx = shash_desc_ctx(desc);
  34. unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
  35. if (!crypto_simd_usable())
  36. return crypto_sha3_update(desc, data, len);
  37. if ((sctx->partial + len) >= sctx->rsiz) {
  38. int blocks;
  39. if (sctx->partial) {
  40. int p = sctx->rsiz - sctx->partial;
  41. memcpy(sctx->buf + sctx->partial, data, p);
  42. kernel_neon_begin();
  43. sha3_ce_transform(sctx->st, sctx->buf, 1, digest_size);
  44. kernel_neon_end();
  45. data += p;
  46. len -= p;
  47. sctx->partial = 0;
  48. }
  49. blocks = len / sctx->rsiz;
  50. len %= sctx->rsiz;
  51. while (blocks) {
  52. int rem;
  53. kernel_neon_begin();
  54. rem = sha3_ce_transform(sctx->st, data, blocks,
  55. digest_size);
  56. kernel_neon_end();
  57. data += (blocks - rem) * sctx->rsiz;
  58. blocks = rem;
  59. }
  60. }
  61. if (len) {
  62. memcpy(sctx->buf + sctx->partial, data, len);
  63. sctx->partial += len;
  64. }
  65. return 0;
  66. }
  67. static int sha3_final(struct shash_desc *desc, u8 *out)
  68. {
  69. struct sha3_state *sctx = shash_desc_ctx(desc);
  70. unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
  71. __le64 *digest = (__le64 *)out;
  72. int i;
  73. if (!crypto_simd_usable())
  74. return crypto_sha3_final(desc, out);
  75. sctx->buf[sctx->partial++] = 0x06;
  76. memset(sctx->buf + sctx->partial, 0, sctx->rsiz - sctx->partial);
  77. sctx->buf[sctx->rsiz - 1] |= 0x80;
  78. kernel_neon_begin();
  79. sha3_ce_transform(sctx->st, sctx->buf, 1, digest_size);
  80. kernel_neon_end();
  81. for (i = 0; i < digest_size / 8; i++)
  82. put_unaligned_le64(sctx->st[i], digest++);
  83. if (digest_size & 4)
  84. put_unaligned_le32(sctx->st[i], (__le32 *)digest);
  85. memzero_explicit(sctx, sizeof(*sctx));
  86. return 0;
  87. }
  88. static struct shash_alg algs[] = { {
  89. .digestsize = SHA3_224_DIGEST_SIZE,
  90. .init = crypto_sha3_init,
  91. .update = sha3_update,
  92. .final = sha3_final,
  93. .descsize = sizeof(struct sha3_state),
  94. .base.cra_name = "sha3-224",
  95. .base.cra_driver_name = "sha3-224-ce",
  96. .base.cra_blocksize = SHA3_224_BLOCK_SIZE,
  97. .base.cra_module = THIS_MODULE,
  98. .base.cra_priority = 200,
  99. }, {
  100. .digestsize = SHA3_256_DIGEST_SIZE,
  101. .init = crypto_sha3_init,
  102. .update = sha3_update,
  103. .final = sha3_final,
  104. .descsize = sizeof(struct sha3_state),
  105. .base.cra_name = "sha3-256",
  106. .base.cra_driver_name = "sha3-256-ce",
  107. .base.cra_blocksize = SHA3_256_BLOCK_SIZE,
  108. .base.cra_module = THIS_MODULE,
  109. .base.cra_priority = 200,
  110. }, {
  111. .digestsize = SHA3_384_DIGEST_SIZE,
  112. .init = crypto_sha3_init,
  113. .update = sha3_update,
  114. .final = sha3_final,
  115. .descsize = sizeof(struct sha3_state),
  116. .base.cra_name = "sha3-384",
  117. .base.cra_driver_name = "sha3-384-ce",
  118. .base.cra_blocksize = SHA3_384_BLOCK_SIZE,
  119. .base.cra_module = THIS_MODULE,
  120. .base.cra_priority = 200,
  121. }, {
  122. .digestsize = SHA3_512_DIGEST_SIZE,
  123. .init = crypto_sha3_init,
  124. .update = sha3_update,
  125. .final = sha3_final,
  126. .descsize = sizeof(struct sha3_state),
  127. .base.cra_name = "sha3-512",
  128. .base.cra_driver_name = "sha3-512-ce",
  129. .base.cra_blocksize = SHA3_512_BLOCK_SIZE,
  130. .base.cra_module = THIS_MODULE,
  131. .base.cra_priority = 200,
  132. } };
  133. static int __init sha3_neon_mod_init(void)
  134. {
  135. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  136. }
  137. static void __exit sha3_neon_mod_fini(void)
  138. {
  139. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  140. }
  141. module_cpu_feature_match(SHA3, sha3_neon_mod_init);
  142. module_exit(sha3_neon_mod_fini);