sha512-ce-glue.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * sha512-ce-glue.c - SHA-384/SHA-512 using ARMv8 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/neon.h>
  12. #include <asm/simd.h>
  13. #include <asm/unaligned.h>
  14. #include <crypto/internal/hash.h>
  15. #include <crypto/sha.h>
  16. #include <crypto/sha512_base.h>
  17. #include <linux/cpufeature.h>
  18. #include <linux/crypto.h>
  19. #include <linux/module.h>
  20. MODULE_DESCRIPTION("SHA-384/SHA-512 secure hash using ARMv8 Crypto Extensions");
  21. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  22. MODULE_LICENSE("GPL v2");
  23. asmlinkage void sha512_ce_transform(struct sha512_state *sst, u8 const *src,
  24. int blocks);
  25. asmlinkage void sha512_block_data_order(u64 *digest, u8 const *src, int blocks);
  26. static int sha512_ce_update(struct shash_desc *desc, const u8 *data,
  27. unsigned int len)
  28. {
  29. if (!may_use_simd())
  30. return sha512_base_do_update(desc, data, len,
  31. (sha512_block_fn *)sha512_block_data_order);
  32. kernel_neon_begin();
  33. sha512_base_do_update(desc, data, len,
  34. (sha512_block_fn *)sha512_ce_transform);
  35. kernel_neon_end();
  36. return 0;
  37. }
  38. static int sha512_ce_finup(struct shash_desc *desc, const u8 *data,
  39. unsigned int len, u8 *out)
  40. {
  41. if (!may_use_simd()) {
  42. if (len)
  43. sha512_base_do_update(desc, data, len,
  44. (sha512_block_fn *)sha512_block_data_order);
  45. sha512_base_do_finalize(desc,
  46. (sha512_block_fn *)sha512_block_data_order);
  47. return sha512_base_finish(desc, out);
  48. }
  49. kernel_neon_begin();
  50. sha512_base_do_update(desc, data, len,
  51. (sha512_block_fn *)sha512_ce_transform);
  52. sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_ce_transform);
  53. kernel_neon_end();
  54. return sha512_base_finish(desc, out);
  55. }
  56. static int sha512_ce_final(struct shash_desc *desc, u8 *out)
  57. {
  58. if (!may_use_simd()) {
  59. sha512_base_do_finalize(desc,
  60. (sha512_block_fn *)sha512_block_data_order);
  61. return sha512_base_finish(desc, out);
  62. }
  63. kernel_neon_begin();
  64. sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_ce_transform);
  65. kernel_neon_end();
  66. return sha512_base_finish(desc, out);
  67. }
  68. static struct shash_alg algs[] = { {
  69. .init = sha384_base_init,
  70. .update = sha512_ce_update,
  71. .final = sha512_ce_final,
  72. .finup = sha512_ce_finup,
  73. .descsize = sizeof(struct sha512_state),
  74. .digestsize = SHA384_DIGEST_SIZE,
  75. .base.cra_name = "sha384",
  76. .base.cra_driver_name = "sha384-ce",
  77. .base.cra_priority = 200,
  78. .base.cra_blocksize = SHA512_BLOCK_SIZE,
  79. .base.cra_module = THIS_MODULE,
  80. }, {
  81. .init = sha512_base_init,
  82. .update = sha512_ce_update,
  83. .final = sha512_ce_final,
  84. .finup = sha512_ce_finup,
  85. .descsize = sizeof(struct sha512_state),
  86. .digestsize = SHA512_DIGEST_SIZE,
  87. .base.cra_name = "sha512",
  88. .base.cra_driver_name = "sha512-ce",
  89. .base.cra_priority = 200,
  90. .base.cra_blocksize = SHA512_BLOCK_SIZE,
  91. .base.cra_module = THIS_MODULE,
  92. } };
  93. static int __init sha512_ce_mod_init(void)
  94. {
  95. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  96. }
  97. static void __exit sha512_ce_mod_fini(void)
  98. {
  99. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  100. }
  101. module_cpu_feature_match(SHA512, sha512_ce_mod_init);
  102. module_exit(sha512_ce_mod_fini);