sm3-ce-glue.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * sm3-ce-glue.c - SM3 secure hash using ARMv8.2 Crypto Extensions
  3. *
  4. * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <asm/neon.h>
  11. #include <asm/simd.h>
  12. #include <asm/unaligned.h>
  13. #include <crypto/internal/hash.h>
  14. #include <crypto/sm3.h>
  15. #include <crypto/sm3_base.h>
  16. #include <linux/cpufeature.h>
  17. #include <linux/crypto.h>
  18. #include <linux/module.h>
  19. MODULE_DESCRIPTION("SM3 secure hash using ARMv8 Crypto Extensions");
  20. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  21. MODULE_LICENSE("GPL v2");
  22. asmlinkage void sm3_ce_transform(struct sm3_state *sst, u8 const *src,
  23. int blocks);
  24. static int sm3_ce_update(struct shash_desc *desc, const u8 *data,
  25. unsigned int len)
  26. {
  27. if (!may_use_simd())
  28. return crypto_sm3_update(desc, data, len);
  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 (!may_use_simd())
  37. return crypto_sm3_finup(desc, NULL, 0, out);
  38. kernel_neon_begin();
  39. sm3_base_do_finalize(desc, sm3_ce_transform);
  40. kernel_neon_end();
  41. return sm3_base_finish(desc, out);
  42. }
  43. static int sm3_ce_finup(struct shash_desc *desc, const u8 *data,
  44. unsigned int len, u8 *out)
  45. {
  46. if (!may_use_simd())
  47. return crypto_sm3_finup(desc, data, len, out);
  48. kernel_neon_begin();
  49. sm3_base_do_update(desc, data, len, sm3_ce_transform);
  50. kernel_neon_end();
  51. return sm3_ce_final(desc, out);
  52. }
  53. static struct shash_alg sm3_alg = {
  54. .digestsize = SM3_DIGEST_SIZE,
  55. .init = sm3_base_init,
  56. .update = sm3_ce_update,
  57. .final = sm3_ce_final,
  58. .finup = sm3_ce_finup,
  59. .descsize = sizeof(struct sm3_state),
  60. .base.cra_name = "sm3",
  61. .base.cra_driver_name = "sm3-ce",
  62. .base.cra_blocksize = SM3_BLOCK_SIZE,
  63. .base.cra_module = THIS_MODULE,
  64. .base.cra_priority = 200,
  65. };
  66. static int __init sm3_ce_mod_init(void)
  67. {
  68. return crypto_register_shash(&sm3_alg);
  69. }
  70. static void __exit sm3_ce_mod_fini(void)
  71. {
  72. crypto_unregister_shash(&sm3_alg);
  73. }
  74. module_cpu_feature_match(SM3, sm3_ce_mod_init);
  75. module_exit(sm3_ce_mod_fini);