sha2-ce-glue.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions
  3. *
  4. * Copyright (C) 2014 - 2017 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/sha.h>
  15. #include <crypto/sha256_base.h>
  16. #include <linux/cpufeature.h>
  17. #include <linux/crypto.h>
  18. #include <linux/module.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. struct sha256_ce_state {
  23. struct sha256_state sst;
  24. u32 finalize;
  25. };
  26. asmlinkage void sha2_ce_transform(struct sha256_ce_state *sst, u8 const *src,
  27. int blocks);
  28. #ifdef CONFIG_CFI_CLANG
  29. static inline void __cfi_sha2_ce_transform(struct sha256_state *sst,
  30. u8 const *src, int blocks)
  31. {
  32. sha2_ce_transform((struct sha256_ce_state *)sst, src, blocks);
  33. }
  34. #define sha2_ce_transform __cfi_sha2_ce_transform
  35. #endif
  36. const u32 sha256_ce_offsetof_count = offsetof(struct sha256_ce_state,
  37. sst.count);
  38. const u32 sha256_ce_offsetof_finalize = offsetof(struct sha256_ce_state,
  39. finalize);
  40. asmlinkage void sha256_block_data_order(u32 *digest, u8 const *src, int blocks);
  41. static int sha256_ce_update(struct shash_desc *desc, const u8 *data,
  42. unsigned int len)
  43. {
  44. struct sha256_ce_state *sctx = shash_desc_ctx(desc);
  45. if (!may_use_simd())
  46. return sha256_base_do_update(desc, data, len,
  47. (sha256_block_fn *)sha256_block_data_order);
  48. sctx->finalize = 0;
  49. kernel_neon_begin();
  50. sha256_base_do_update(desc, data, len,
  51. (sha256_block_fn *)sha2_ce_transform);
  52. kernel_neon_end();
  53. return 0;
  54. }
  55. static int sha256_ce_finup(struct shash_desc *desc, const u8 *data,
  56. unsigned int len, u8 *out)
  57. {
  58. struct sha256_ce_state *sctx = shash_desc_ctx(desc);
  59. bool finalize = !sctx->sst.count && !(len % SHA256_BLOCK_SIZE) && len;
  60. if (!may_use_simd()) {
  61. if (len)
  62. sha256_base_do_update(desc, data, len,
  63. (sha256_block_fn *)sha256_block_data_order);
  64. sha256_base_do_finalize(desc,
  65. (sha256_block_fn *)sha256_block_data_order);
  66. return sha256_base_finish(desc, out);
  67. }
  68. /*
  69. * Allow the asm code to perform the finalization if there is no
  70. * partial data and the input is a round multiple of the block size.
  71. */
  72. sctx->finalize = finalize;
  73. kernel_neon_begin();
  74. sha256_base_do_update(desc, data, len,
  75. (sha256_block_fn *)sha2_ce_transform);
  76. if (!finalize)
  77. sha256_base_do_finalize(desc,
  78. (sha256_block_fn *)sha2_ce_transform);
  79. kernel_neon_end();
  80. return sha256_base_finish(desc, out);
  81. }
  82. static int sha256_ce_final(struct shash_desc *desc, u8 *out)
  83. {
  84. struct sha256_ce_state *sctx = shash_desc_ctx(desc);
  85. if (!may_use_simd()) {
  86. sha256_base_do_finalize(desc,
  87. (sha256_block_fn *)sha256_block_data_order);
  88. return sha256_base_finish(desc, out);
  89. }
  90. sctx->finalize = 0;
  91. kernel_neon_begin();
  92. sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform);
  93. kernel_neon_end();
  94. return sha256_base_finish(desc, out);
  95. }
  96. static struct shash_alg algs[] = { {
  97. .init = sha224_base_init,
  98. .update = sha256_ce_update,
  99. .final = sha256_ce_final,
  100. .finup = sha256_ce_finup,
  101. .descsize = sizeof(struct sha256_ce_state),
  102. .digestsize = SHA224_DIGEST_SIZE,
  103. .base = {
  104. .cra_name = "sha224",
  105. .cra_driver_name = "sha224-ce",
  106. .cra_priority = 200,
  107. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  108. .cra_blocksize = SHA256_BLOCK_SIZE,
  109. .cra_module = THIS_MODULE,
  110. }
  111. }, {
  112. .init = sha256_base_init,
  113. .update = sha256_ce_update,
  114. .final = sha256_ce_final,
  115. .finup = sha256_ce_finup,
  116. .descsize = sizeof(struct sha256_ce_state),
  117. .digestsize = SHA256_DIGEST_SIZE,
  118. .base = {
  119. .cra_name = "sha256",
  120. .cra_driver_name = "sha256-ce",
  121. .cra_priority = 200,
  122. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  123. .cra_blocksize = SHA256_BLOCK_SIZE,
  124. .cra_module = THIS_MODULE,
  125. }
  126. } };
  127. static int __init sha2_ce_mod_init(void)
  128. {
  129. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  130. }
  131. static void __exit sha2_ce_mod_fini(void)
  132. {
  133. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  134. }
  135. module_cpu_feature_match(SHA2, sha2_ce_mod_init);
  136. module_exit(sha2_ce_mod_fini);