sha256_glue.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Glue code for the SHA256 Secure Hash Algorithm assembly implementation
  4. * using optimized ARM assembler and NEON instructions.
  5. *
  6. * Copyright © 2015 Google Inc.
  7. *
  8. * This file is based on sha256_ssse3_glue.c:
  9. * Copyright (C) 2013 Intel Corporation
  10. * Author: Tim Chen <[email protected]>
  11. */
  12. #include <crypto/internal/hash.h>
  13. #include <linux/crypto.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/mm.h>
  17. #include <linux/types.h>
  18. #include <linux/string.h>
  19. #include <crypto/sha2.h>
  20. #include <crypto/sha256_base.h>
  21. #include <asm/simd.h>
  22. #include <asm/neon.h>
  23. #include "sha256_glue.h"
  24. asmlinkage void sha256_block_data_order(u32 *digest, const void *data,
  25. unsigned int num_blks);
  26. int crypto_sha256_arm_update(struct shash_desc *desc, const u8 *data,
  27. unsigned int len)
  28. {
  29. /* make sure casting to sha256_block_fn() is safe */
  30. BUILD_BUG_ON(offsetof(struct sha256_state, state) != 0);
  31. return sha256_base_do_update(desc, data, len,
  32. (sha256_block_fn *)sha256_block_data_order);
  33. }
  34. EXPORT_SYMBOL(crypto_sha256_arm_update);
  35. static int crypto_sha256_arm_final(struct shash_desc *desc, u8 *out)
  36. {
  37. sha256_base_do_finalize(desc,
  38. (sha256_block_fn *)sha256_block_data_order);
  39. return sha256_base_finish(desc, out);
  40. }
  41. int crypto_sha256_arm_finup(struct shash_desc *desc, const u8 *data,
  42. unsigned int len, u8 *out)
  43. {
  44. sha256_base_do_update(desc, data, len,
  45. (sha256_block_fn *)sha256_block_data_order);
  46. return crypto_sha256_arm_final(desc, out);
  47. }
  48. EXPORT_SYMBOL(crypto_sha256_arm_finup);
  49. static struct shash_alg algs[] = { {
  50. .digestsize = SHA256_DIGEST_SIZE,
  51. .init = sha256_base_init,
  52. .update = crypto_sha256_arm_update,
  53. .final = crypto_sha256_arm_final,
  54. .finup = crypto_sha256_arm_finup,
  55. .descsize = sizeof(struct sha256_state),
  56. .base = {
  57. .cra_name = "sha256",
  58. .cra_driver_name = "sha256-asm",
  59. .cra_priority = 150,
  60. .cra_blocksize = SHA256_BLOCK_SIZE,
  61. .cra_module = THIS_MODULE,
  62. }
  63. }, {
  64. .digestsize = SHA224_DIGEST_SIZE,
  65. .init = sha224_base_init,
  66. .update = crypto_sha256_arm_update,
  67. .final = crypto_sha256_arm_final,
  68. .finup = crypto_sha256_arm_finup,
  69. .descsize = sizeof(struct sha256_state),
  70. .base = {
  71. .cra_name = "sha224",
  72. .cra_driver_name = "sha224-asm",
  73. .cra_priority = 150,
  74. .cra_blocksize = SHA224_BLOCK_SIZE,
  75. .cra_module = THIS_MODULE,
  76. }
  77. } };
  78. static int __init sha256_mod_init(void)
  79. {
  80. int res = crypto_register_shashes(algs, ARRAY_SIZE(algs));
  81. if (res < 0)
  82. return res;
  83. if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon()) {
  84. res = crypto_register_shashes(sha256_neon_algs,
  85. ARRAY_SIZE(sha256_neon_algs));
  86. if (res < 0)
  87. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  88. }
  89. return res;
  90. }
  91. static void __exit sha256_mod_fini(void)
  92. {
  93. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  94. if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon())
  95. crypto_unregister_shashes(sha256_neon_algs,
  96. ARRAY_SIZE(sha256_neon_algs));
  97. }
  98. module_init(sha256_mod_init);
  99. module_exit(sha256_mod_fini);
  100. MODULE_LICENSE("GPL");
  101. MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm (ARM), including NEON");
  102. MODULE_ALIAS_CRYPTO("sha256");