sha2-ce-glue.c 4.0 KB

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