sha512-ce-glue.c 3.3 KB

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