sha3-ce-glue.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <ard.biesheuvel@linaro.org>
  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/sha3.h>
  17. #include <linux/cpufeature.h>
  18. #include <linux/crypto.h>
  19. #include <linux/module.h>
  20. MODULE_DESCRIPTION("SHA3 secure hash using ARMv8 Crypto Extensions");
  21. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  22. MODULE_LICENSE("GPL v2");
  23. MODULE_ALIAS_CRYPTO("sha3-224");
  24. MODULE_ALIAS_CRYPTO("sha3-256");
  25. MODULE_ALIAS_CRYPTO("sha3-384");
  26. MODULE_ALIAS_CRYPTO("sha3-512");
  27. asmlinkage void sha3_ce_transform(u64 *st, const u8 *data, int blocks,
  28. int md_len);
  29. static int sha3_update(struct shash_desc *desc, const u8 *data,
  30. unsigned int len)
  31. {
  32. struct sha3_state *sctx = shash_desc_ctx(desc);
  33. unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
  34. if (!may_use_simd())
  35. return crypto_sha3_update(desc, data, len);
  36. if ((sctx->partial + len) >= sctx->rsiz) {
  37. int blocks;
  38. if (sctx->partial) {
  39. int p = sctx->rsiz - sctx->partial;
  40. memcpy(sctx->buf + sctx->partial, data, p);
  41. kernel_neon_begin();
  42. sha3_ce_transform(sctx->st, sctx->buf, 1, digest_size);
  43. kernel_neon_end();
  44. data += p;
  45. len -= p;
  46. sctx->partial = 0;
  47. }
  48. blocks = len / sctx->rsiz;
  49. len %= sctx->rsiz;
  50. if (blocks) {
  51. kernel_neon_begin();
  52. sha3_ce_transform(sctx->st, data, blocks, digest_size);
  53. kernel_neon_end();
  54. data += blocks * sctx->rsiz;
  55. }
  56. }
  57. if (len) {
  58. memcpy(sctx->buf + sctx->partial, data, len);
  59. sctx->partial += len;
  60. }
  61. return 0;
  62. }
  63. static int sha3_final(struct shash_desc *desc, u8 *out)
  64. {
  65. struct sha3_state *sctx = shash_desc_ctx(desc);
  66. unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
  67. __le64 *digest = (__le64 *)out;
  68. int i;
  69. if (!may_use_simd())
  70. return crypto_sha3_final(desc, out);
  71. sctx->buf[sctx->partial++] = 0x06;
  72. memset(sctx->buf + sctx->partial, 0, sctx->rsiz - sctx->partial);
  73. sctx->buf[sctx->rsiz - 1] |= 0x80;
  74. kernel_neon_begin();
  75. sha3_ce_transform(sctx->st, sctx->buf, 1, digest_size);
  76. kernel_neon_end();
  77. for (i = 0; i < digest_size / 8; i++)
  78. put_unaligned_le64(sctx->st[i], digest++);
  79. if (digest_size & 4)
  80. put_unaligned_le32(sctx->st[i], (__le32 *)digest);
  81. *sctx = (struct sha3_state){};
  82. return 0;
  83. }
  84. static struct shash_alg algs[] = { {
  85. .digestsize = SHA3_224_DIGEST_SIZE,
  86. .init = crypto_sha3_init,
  87. .update = sha3_update,
  88. .final = sha3_final,
  89. .descsize = sizeof(struct sha3_state),
  90. .base.cra_name = "sha3-224",
  91. .base.cra_driver_name = "sha3-224-ce",
  92. .base.cra_blocksize = SHA3_224_BLOCK_SIZE,
  93. .base.cra_module = THIS_MODULE,
  94. .base.cra_priority = 200,
  95. }, {
  96. .digestsize = SHA3_256_DIGEST_SIZE,
  97. .init = crypto_sha3_init,
  98. .update = sha3_update,
  99. .final = sha3_final,
  100. .descsize = sizeof(struct sha3_state),
  101. .base.cra_name = "sha3-256",
  102. .base.cra_driver_name = "sha3-256-ce",
  103. .base.cra_blocksize = SHA3_256_BLOCK_SIZE,
  104. .base.cra_module = THIS_MODULE,
  105. .base.cra_priority = 200,
  106. }, {
  107. .digestsize = SHA3_384_DIGEST_SIZE,
  108. .init = crypto_sha3_init,
  109. .update = sha3_update,
  110. .final = sha3_final,
  111. .descsize = sizeof(struct sha3_state),
  112. .base.cra_name = "sha3-384",
  113. .base.cra_driver_name = "sha3-384-ce",
  114. .base.cra_blocksize = SHA3_384_BLOCK_SIZE,
  115. .base.cra_module = THIS_MODULE,
  116. .base.cra_priority = 200,
  117. }, {
  118. .digestsize = SHA3_512_DIGEST_SIZE,
  119. .init = crypto_sha3_init,
  120. .update = sha3_update,
  121. .final = sha3_final,
  122. .descsize = sizeof(struct sha3_state),
  123. .base.cra_name = "sha3-512",
  124. .base.cra_driver_name = "sha3-512-ce",
  125. .base.cra_blocksize = SHA3_512_BLOCK_SIZE,
  126. .base.cra_module = THIS_MODULE,
  127. .base.cra_priority = 200,
  128. } };
  129. static int __init sha3_neon_mod_init(void)
  130. {
  131. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  132. }
  133. static void __exit sha3_neon_mod_fini(void)
  134. {
  135. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  136. }
  137. module_cpu_feature_match(SHA3, sha3_neon_mod_init);
  138. module_exit(sha3_neon_mod_fini);