sha3-ce-glue.c 4.1 KB

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