sha2-ce-glue.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions
  4. *
  5. * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
  6. */
  7. #include <crypto/internal/hash.h>
  8. #include <crypto/internal/simd.h>
  9. #include <crypto/sha.h>
  10. #include <crypto/sha256_base.h>
  11. #include <linux/cpufeature.h>
  12. #include <linux/crypto.h>
  13. #include <linux/module.h>
  14. #include <asm/hwcap.h>
  15. #include <asm/simd.h>
  16. #include <asm/neon.h>
  17. #include <asm/unaligned.h>
  18. #include "sha256_glue.h"
  19. MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions");
  20. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  21. MODULE_LICENSE("GPL v2");
  22. asmlinkage void sha2_ce_transform(struct sha256_state *sst, u8 const *src,
  23. int blocks);
  24. static int sha2_ce_update(struct shash_desc *desc, const u8 *data,
  25. unsigned int len)
  26. {
  27. struct sha256_state *sctx = shash_desc_ctx(desc);
  28. if (!crypto_simd_usable() ||
  29. (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
  30. return crypto_sha256_arm_update(desc, data, len);
  31. kernel_neon_begin();
  32. sha256_base_do_update(desc, data, len,
  33. (sha256_block_fn *)sha2_ce_transform);
  34. kernel_neon_end();
  35. return 0;
  36. }
  37. static int sha2_ce_finup(struct shash_desc *desc, const u8 *data,
  38. unsigned int len, u8 *out)
  39. {
  40. if (!crypto_simd_usable())
  41. return crypto_sha256_arm_finup(desc, data, len, out);
  42. kernel_neon_begin();
  43. if (len)
  44. sha256_base_do_update(desc, data, len,
  45. (sha256_block_fn *)sha2_ce_transform);
  46. sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform);
  47. kernel_neon_end();
  48. return sha256_base_finish(desc, out);
  49. }
  50. static int sha2_ce_final(struct shash_desc *desc, u8 *out)
  51. {
  52. return sha2_ce_finup(desc, NULL, 0, out);
  53. }
  54. static struct shash_alg algs[] = { {
  55. .init = sha224_base_init,
  56. .update = sha2_ce_update,
  57. .final = sha2_ce_final,
  58. .finup = sha2_ce_finup,
  59. .descsize = sizeof(struct sha256_state),
  60. .digestsize = SHA224_DIGEST_SIZE,
  61. .base = {
  62. .cra_name = "sha224",
  63. .cra_driver_name = "sha224-ce",
  64. .cra_priority = 300,
  65. .cra_blocksize = SHA256_BLOCK_SIZE,
  66. .cra_module = THIS_MODULE,
  67. }
  68. }, {
  69. .init = sha256_base_init,
  70. .update = sha2_ce_update,
  71. .final = sha2_ce_final,
  72. .finup = sha2_ce_finup,
  73. .descsize = sizeof(struct sha256_state),
  74. .digestsize = SHA256_DIGEST_SIZE,
  75. .base = {
  76. .cra_name = "sha256",
  77. .cra_driver_name = "sha256-ce",
  78. .cra_priority = 300,
  79. .cra_blocksize = SHA256_BLOCK_SIZE,
  80. .cra_module = THIS_MODULE,
  81. }
  82. } };
  83. static int __init sha2_ce_mod_init(void)
  84. {
  85. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  86. }
  87. static void __exit sha2_ce_mod_fini(void)
  88. {
  89. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  90. }
  91. module_cpu_feature_match(SHA2, sha2_ce_mod_init);
  92. module_exit(sha2_ce_mod_fini);