sha256_neon_glue.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/sha2.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(struct sha256_state *digest,
  22. const u8 *data, 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, sha256_block_data_order_neon);
  32. kernel_neon_end();
  33. return 0;
  34. }
  35. static int crypto_sha256_neon_finup(struct shash_desc *desc, const u8 *data,
  36. unsigned int len, u8 *out)
  37. {
  38. if (!crypto_simd_usable())
  39. return crypto_sha256_arm_finup(desc, data, len, out);
  40. kernel_neon_begin();
  41. if (len)
  42. sha256_base_do_update(desc, data, len,
  43. sha256_block_data_order_neon);
  44. sha256_base_do_finalize(desc, sha256_block_data_order_neon);
  45. kernel_neon_end();
  46. return sha256_base_finish(desc, out);
  47. }
  48. static int crypto_sha256_neon_final(struct shash_desc *desc, u8 *out)
  49. {
  50. return crypto_sha256_neon_finup(desc, NULL, 0, out);
  51. }
  52. struct shash_alg sha256_neon_algs[] = { {
  53. .digestsize = SHA256_DIGEST_SIZE,
  54. .init = sha256_base_init,
  55. .update = crypto_sha256_neon_update,
  56. .final = crypto_sha256_neon_final,
  57. .finup = crypto_sha256_neon_finup,
  58. .descsize = sizeof(struct sha256_state),
  59. .base = {
  60. .cra_name = "sha256",
  61. .cra_driver_name = "sha256-neon",
  62. .cra_priority = 250,
  63. .cra_blocksize = SHA256_BLOCK_SIZE,
  64. .cra_module = THIS_MODULE,
  65. }
  66. }, {
  67. .digestsize = SHA224_DIGEST_SIZE,
  68. .init = sha224_base_init,
  69. .update = crypto_sha256_neon_update,
  70. .final = crypto_sha256_neon_final,
  71. .finup = crypto_sha256_neon_finup,
  72. .descsize = sizeof(struct sha256_state),
  73. .base = {
  74. .cra_name = "sha224",
  75. .cra_driver_name = "sha224-neon",
  76. .cra_priority = 250,
  77. .cra_blocksize = SHA224_BLOCK_SIZE,
  78. .cra_module = THIS_MODULE,
  79. }
  80. } };