sha256_glue.c 3.4 KB

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