sha2-ce-glue.c 4.0 KB

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