sha1-ce-glue.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <[email protected]>
  6. */
  7. #include <asm/neon.h>
  8. #include <asm/simd.h>
  9. #include <asm/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 <[email protected]>");
  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, struct sha1_ce_state,
  36. sst), src, blocks);
  37. kernel_neon_end();
  38. src += (blocks - rem) * SHA1_BLOCK_SIZE;
  39. blocks = rem;
  40. }
  41. }
  42. const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
  43. const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
  44. static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
  45. unsigned int len)
  46. {
  47. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  48. if (!crypto_simd_usable())
  49. return crypto_sha1_update(desc, data, len);
  50. sctx->finalize = 0;
  51. sha1_base_do_update(desc, data, len, __sha1_ce_transform);
  52. return 0;
  53. }
  54. static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
  55. unsigned int len, u8 *out)
  56. {
  57. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  58. bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE) && len;
  59. if (!crypto_simd_usable())
  60. return crypto_sha1_finup(desc, data, len, out);
  61. /*
  62. * Allow the asm code to perform the finalization if there is no
  63. * partial data and the input is a round multiple of the block size.
  64. */
  65. sctx->finalize = finalize;
  66. sha1_base_do_update(desc, data, len, __sha1_ce_transform);
  67. if (!finalize)
  68. sha1_base_do_finalize(desc, __sha1_ce_transform);
  69. return sha1_base_finish(desc, out);
  70. }
  71. static int sha1_ce_final(struct shash_desc *desc, u8 *out)
  72. {
  73. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  74. if (!crypto_simd_usable())
  75. return crypto_sha1_finup(desc, NULL, 0, out);
  76. sctx->finalize = 0;
  77. sha1_base_do_finalize(desc, __sha1_ce_transform);
  78. return sha1_base_finish(desc, out);
  79. }
  80. static int sha1_ce_export(struct shash_desc *desc, void *out)
  81. {
  82. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  83. memcpy(out, &sctx->sst, sizeof(struct sha1_state));
  84. return 0;
  85. }
  86. static int sha1_ce_import(struct shash_desc *desc, const void *in)
  87. {
  88. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  89. memcpy(&sctx->sst, in, sizeof(struct sha1_state));
  90. sctx->finalize = 0;
  91. return 0;
  92. }
  93. static struct shash_alg alg = {
  94. .init = sha1_base_init,
  95. .update = sha1_ce_update,
  96. .final = sha1_ce_final,
  97. .finup = sha1_ce_finup,
  98. .import = sha1_ce_import,
  99. .export = sha1_ce_export,
  100. .descsize = sizeof(struct sha1_ce_state),
  101. .statesize = sizeof(struct sha1_state),
  102. .digestsize = SHA1_DIGEST_SIZE,
  103. .base = {
  104. .cra_name = "sha1",
  105. .cra_driver_name = "sha1-ce",
  106. .cra_priority = 200,
  107. .cra_blocksize = SHA1_BLOCK_SIZE,
  108. .cra_module = THIS_MODULE,
  109. }
  110. };
  111. static int __init sha1_ce_mod_init(void)
  112. {
  113. return crypto_register_shash(&alg);
  114. }
  115. static void __exit sha1_ce_mod_fini(void)
  116. {
  117. crypto_unregister_shash(&alg);
  118. }
  119. module_cpu_feature_match(SHA1, sha1_ce_mod_init);
  120. module_exit(sha1_ce_mod_fini);