sha256_neon_glue.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Glue code for the SHA256 Secure Hash Algorithm assembly implementation
  4. * using NEON instructions.
  5. *
  6. * Copyright © 2015 Google Inc.
  7. *
  8. * This file is based on sha512_neon_glue.c:
  9. * Copyright © 2014 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  10. */
  11. #include <crypto/internal/hash.h>
  12. #include <crypto/internal/simd.h>
  13. #include <linux/types.h>
  14. #include <linux/string.h>
  15. #include <crypto/sha.h>
  16. #include <crypto/sha256_base.h>
  17. #include <asm/byteorder.h>
  18. #include <asm/simd.h>
  19. #include <asm/neon.h>
  20. #include "sha256_glue.h"
  21. asmlinkage void sha256_block_data_order_neon(u32 *digest, const void *data,
  22. unsigned int num_blks);
  23. static int crypto_sha256_neon_update(struct shash_desc *desc, const u8 *data,
  24. unsigned int len)
  25. {
  26. struct sha256_state *sctx = shash_desc_ctx(desc);
  27. if (!crypto_simd_usable() ||
  28. (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
  29. return crypto_sha256_arm_update(desc, data, len);
  30. kernel_neon_begin();
  31. sha256_base_do_update(desc, data, len,
  32. (sha256_block_fn *)sha256_block_data_order_neon);
  33. kernel_neon_end();
  34. return 0;
  35. }
  36. static int crypto_sha256_neon_finup(struct shash_desc *desc, const u8 *data,
  37. unsigned int len, u8 *out)
  38. {
  39. if (!crypto_simd_usable())
  40. return crypto_sha256_arm_finup(desc, data, len, out);
  41. kernel_neon_begin();
  42. if (len)
  43. sha256_base_do_update(desc, data, len,
  44. (sha256_block_fn *)sha256_block_data_order_neon);
  45. sha256_base_do_finalize(desc,
  46. (sha256_block_fn *)sha256_block_data_order_neon);
  47. kernel_neon_end();
  48. return sha256_base_finish(desc, out);
  49. }
  50. static int crypto_sha256_neon_final(struct shash_desc *desc, u8 *out)
  51. {
  52. return crypto_sha256_neon_finup(desc, NULL, 0, out);
  53. }
  54. struct shash_alg sha256_neon_algs[] = { {
  55. .digestsize = SHA256_DIGEST_SIZE,
  56. .init = sha256_base_init,
  57. .update = crypto_sha256_neon_update,
  58. .final = crypto_sha256_neon_final,
  59. .finup = crypto_sha256_neon_finup,
  60. .descsize = sizeof(struct sha256_state),
  61. .base = {
  62. .cra_name = "sha256",
  63. .cra_driver_name = "sha256-neon",
  64. .cra_priority = 250,
  65. .cra_blocksize = SHA256_BLOCK_SIZE,
  66. .cra_module = THIS_MODULE,
  67. }
  68. }, {
  69. .digestsize = SHA224_DIGEST_SIZE,
  70. .init = sha224_base_init,
  71. .update = crypto_sha256_neon_update,
  72. .final = crypto_sha256_neon_final,
  73. .finup = crypto_sha256_neon_finup,
  74. .descsize = sizeof(struct sha256_state),
  75. .base = {
  76. .cra_name = "sha224",
  77. .cra_driver_name = "sha224-neon",
  78. .cra_priority = 250,
  79. .cra_blocksize = SHA224_BLOCK_SIZE,
  80. .cra_module = THIS_MODULE,
  81. }
  82. } };