sha1-ce-glue.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
  4. *
  5. * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
  6. */
  7. #include <asm/neon.h>
  8. #include <asm/simd.h>
  9. #include <linux/unaligned.h>
  10. #include <crypto/internal/hash.h>
  11. #include <crypto/internal/simd.h>
  12. #include <crypto/sha1.h>
  13. #include <crypto/sha1_base.h>
  14. #include <linux/cpufeature.h>
  15. #include <linux/crypto.h>
  16. #include <linux/module.h>
  17. MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
  18. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  19. MODULE_LICENSE("GPL v2");
  20. MODULE_ALIAS_CRYPTO("sha1");
  21. struct sha1_ce_state {
  22. struct sha1_state sst;
  23. u32 finalize;
  24. };
  25. extern const u32 sha1_ce_offsetof_count;
  26. extern const u32 sha1_ce_offsetof_finalize;
  27. asmlinkage int __sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
  28. int blocks);
  29. static void sha1_ce_transform(struct sha1_state *sst, u8 const *src,
  30. int blocks)
  31. {
  32. while (blocks) {
  33. int rem;
  34. kernel_neon_begin();
  35. rem = __sha1_ce_transform(container_of(sst,
  36. struct sha1_ce_state,
  37. sst), src, blocks);
  38. kernel_neon_end();
  39. src += (blocks - rem) * SHA1_BLOCK_SIZE;
  40. blocks = rem;
  41. }
  42. }
  43. const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
  44. const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
  45. static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
  46. unsigned int len)
  47. {
  48. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  49. if (!crypto_simd_usable())
  50. return crypto_sha1_update(desc, data, len);
  51. sctx->finalize = 0;
  52. sha1_base_do_update(desc, data, len, sha1_ce_transform);
  53. return 0;
  54. }
  55. static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
  56. unsigned int len, u8 *out)
  57. {
  58. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  59. bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE) && len;
  60. if (!crypto_simd_usable())
  61. return crypto_sha1_finup(desc, data, len, out);
  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. sha1_base_do_update(desc, data, len, sha1_ce_transform);
  68. if (!finalize)
  69. sha1_base_do_finalize(desc, sha1_ce_transform);
  70. return sha1_base_finish(desc, out);
  71. }
  72. static int sha1_ce_final(struct shash_desc *desc, u8 *out)
  73. {
  74. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  75. if (!crypto_simd_usable())
  76. return crypto_sha1_finup(desc, NULL, 0, out);
  77. sctx->finalize = 0;
  78. sha1_base_do_finalize(desc, sha1_ce_transform);
  79. return sha1_base_finish(desc, out);
  80. }
  81. static int sha1_ce_export(struct shash_desc *desc, void *out)
  82. {
  83. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  84. memcpy(out, &sctx->sst, sizeof(struct sha1_state));
  85. return 0;
  86. }
  87. static int sha1_ce_import(struct shash_desc *desc, const void *in)
  88. {
  89. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  90. memcpy(&sctx->sst, in, sizeof(struct sha1_state));
  91. sctx->finalize = 0;
  92. return 0;
  93. }
  94. static struct shash_alg alg = {
  95. .init = sha1_base_init,
  96. .update = sha1_ce_update,
  97. .final = sha1_ce_final,
  98. .finup = sha1_ce_finup,
  99. .import = sha1_ce_import,
  100. .export = sha1_ce_export,
  101. .descsize = sizeof(struct sha1_ce_state),
  102. .statesize = sizeof(struct sha1_state),
  103. .digestsize = SHA1_DIGEST_SIZE,
  104. .base = {
  105. .cra_name = "sha1",
  106. .cra_driver_name = "sha1-ce",
  107. .cra_priority = 200,
  108. .cra_blocksize = SHA1_BLOCK_SIZE,
  109. .cra_module = THIS_MODULE,
  110. }
  111. };
  112. static int __init sha1_ce_mod_init(void)
  113. {
  114. return crypto_register_shash(&alg);
  115. }
  116. static void __exit sha1_ce_mod_fini(void)
  117. {
  118. crypto_unregister_shash(&alg);
  119. }
  120. module_cpu_feature_match(SHA1, sha1_ce_mod_init);
  121. module_exit(sha1_ce_mod_fini);