sm3-ce-glue.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sm3-ce-glue.c - SM3 secure hash using ARMv8.2 Crypto Extensions
  4. *
  5. * Copyright (C) 2018 Linaro Ltd <[email protected]>
  6. */
  7. #include <asm/neon.h>
  8. #include <asm/simd.h>
  9. #include <asm/unaligned.h>
  10. #include <crypto/internal/hash.h>
  11. #include <crypto/internal/simd.h>
  12. #include <crypto/sm3.h>
  13. #include <crypto/sm3_base.h>
  14. #include <linux/cpufeature.h>
  15. #include <linux/crypto.h>
  16. #include <linux/module.h>
  17. MODULE_DESCRIPTION("SM3 secure hash using ARMv8 Crypto Extensions");
  18. MODULE_AUTHOR("Ard Biesheuvel <[email protected]>");
  19. MODULE_LICENSE("GPL v2");
  20. asmlinkage void sm3_ce_transform(struct sm3_state *sst, u8 const *src,
  21. int blocks);
  22. static int sm3_ce_update(struct shash_desc *desc, const u8 *data,
  23. unsigned int len)
  24. {
  25. if (!crypto_simd_usable()) {
  26. sm3_update(shash_desc_ctx(desc), data, len);
  27. return 0;
  28. }
  29. kernel_neon_begin();
  30. sm3_base_do_update(desc, data, len, sm3_ce_transform);
  31. kernel_neon_end();
  32. return 0;
  33. }
  34. static int sm3_ce_final(struct shash_desc *desc, u8 *out)
  35. {
  36. if (!crypto_simd_usable()) {
  37. sm3_final(shash_desc_ctx(desc), out);
  38. return 0;
  39. }
  40. kernel_neon_begin();
  41. sm3_base_do_finalize(desc, sm3_ce_transform);
  42. kernel_neon_end();
  43. return sm3_base_finish(desc, out);
  44. }
  45. static int sm3_ce_finup(struct shash_desc *desc, const u8 *data,
  46. unsigned int len, u8 *out)
  47. {
  48. if (!crypto_simd_usable()) {
  49. struct sm3_state *sctx = shash_desc_ctx(desc);
  50. if (len)
  51. sm3_update(sctx, data, len);
  52. sm3_final(sctx, out);
  53. return 0;
  54. }
  55. kernel_neon_begin();
  56. if (len)
  57. sm3_base_do_update(desc, data, len, sm3_ce_transform);
  58. sm3_base_do_finalize(desc, sm3_ce_transform);
  59. kernel_neon_end();
  60. return sm3_base_finish(desc, out);
  61. }
  62. static struct shash_alg sm3_alg = {
  63. .digestsize = SM3_DIGEST_SIZE,
  64. .init = sm3_base_init,
  65. .update = sm3_ce_update,
  66. .final = sm3_ce_final,
  67. .finup = sm3_ce_finup,
  68. .descsize = sizeof(struct sm3_state),
  69. .base.cra_name = "sm3",
  70. .base.cra_driver_name = "sm3-ce",
  71. .base.cra_blocksize = SM3_BLOCK_SIZE,
  72. .base.cra_module = THIS_MODULE,
  73. .base.cra_priority = 200,
  74. };
  75. static int __init sm3_ce_mod_init(void)
  76. {
  77. return crypto_register_shash(&sm3_alg);
  78. }
  79. static void __exit sm3_ce_mod_fini(void)
  80. {
  81. crypto_unregister_shash(&sm3_alg);
  82. }
  83. module_cpu_feature_match(SM3, sm3_ce_mod_init);
  84. module_exit(sm3_ce_mod_fini);