sha256_glue.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_blocksize = SHA256_BLOCK_SIZE,
  67. .cra_module = THIS_MODULE,
  68. }
  69. }, {
  70. .digestsize = SHA224_DIGEST_SIZE,
  71. .init = sha224_base_init,
  72. .update = crypto_sha256_arm_update,
  73. .final = sha256_final,
  74. .finup = crypto_sha256_arm_finup,
  75. .descsize = sizeof(struct sha256_state),
  76. .base = {
  77. .cra_name = "sha224",
  78. .cra_driver_name = "sha224-asm",
  79. .cra_priority = 150,
  80. .cra_blocksize = SHA224_BLOCK_SIZE,
  81. .cra_module = THIS_MODULE,
  82. }
  83. } };
  84. static int __init sha256_mod_init(void)
  85. {
  86. int res = crypto_register_shashes(algs, ARRAY_SIZE(algs));
  87. if (res < 0)
  88. return res;
  89. if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon()) {
  90. res = crypto_register_shashes(sha256_neon_algs,
  91. ARRAY_SIZE(sha256_neon_algs));
  92. if (res < 0)
  93. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  94. }
  95. return res;
  96. }
  97. static void __exit sha256_mod_fini(void)
  98. {
  99. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  100. if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon())
  101. crypto_unregister_shashes(sha256_neon_algs,
  102. ARRAY_SIZE(sha256_neon_algs));
  103. }
  104. module_init(sha256_mod_init);
  105. module_exit(sha256_mod_fini);
  106. MODULE_LICENSE("GPL");
  107. MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm (ARM), including NEON");
  108. MODULE_ALIAS_CRYPTO("sha256");