sha2-ce-glue.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions
  3. *
  4. * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <crypto/internal/hash.h>
  11. #include <crypto/sha.h>
  12. #include <crypto/sha256_base.h>
  13. #include <linux/cpufeature.h>
  14. #include <linux/crypto.h>
  15. #include <linux/module.h>
  16. #include <asm/hwcap.h>
  17. #include <asm/simd.h>
  18. #include <asm/neon.h>
  19. #include <asm/unaligned.h>
  20. #include "sha256_glue.h"
  21. MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions");
  22. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  23. MODULE_LICENSE("GPL v2");
  24. asmlinkage void sha2_ce_transform(struct sha256_state *sst, u8 const *src,
  25. int blocks);
  26. static int sha2_ce_update(struct shash_desc *desc, const u8 *data,
  27. unsigned int len)
  28. {
  29. struct sha256_state *sctx = shash_desc_ctx(desc);
  30. if (!may_use_simd() ||
  31. (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
  32. return crypto_sha256_arm_update(desc, data, len);
  33. kernel_neon_begin();
  34. sha256_base_do_update(desc, data, len,
  35. (sha256_block_fn *)sha2_ce_transform);
  36. kernel_neon_end();
  37. return 0;
  38. }
  39. static int sha2_ce_finup(struct shash_desc *desc, const u8 *data,
  40. unsigned int len, u8 *out)
  41. {
  42. if (!may_use_simd())
  43. return crypto_sha256_arm_finup(desc, data, len, out);
  44. kernel_neon_begin();
  45. if (len)
  46. sha256_base_do_update(desc, data, len,
  47. (sha256_block_fn *)sha2_ce_transform);
  48. sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform);
  49. kernel_neon_end();
  50. return sha256_base_finish(desc, out);
  51. }
  52. static int sha2_ce_final(struct shash_desc *desc, u8 *out)
  53. {
  54. return sha2_ce_finup(desc, NULL, 0, out);
  55. }
  56. static struct shash_alg algs[] = { {
  57. .init = sha224_base_init,
  58. .update = sha2_ce_update,
  59. .final = sha2_ce_final,
  60. .finup = sha2_ce_finup,
  61. .descsize = sizeof(struct sha256_state),
  62. .digestsize = SHA224_DIGEST_SIZE,
  63. .base = {
  64. .cra_name = "sha224",
  65. .cra_driver_name = "sha224-ce",
  66. .cra_priority = 300,
  67. .cra_blocksize = SHA256_BLOCK_SIZE,
  68. .cra_module = THIS_MODULE,
  69. }
  70. }, {
  71. .init = sha256_base_init,
  72. .update = sha2_ce_update,
  73. .final = sha2_ce_final,
  74. .finup = sha2_ce_finup,
  75. .descsize = sizeof(struct sha256_state),
  76. .digestsize = SHA256_DIGEST_SIZE,
  77. .base = {
  78. .cra_name = "sha256",
  79. .cra_driver_name = "sha256-ce",
  80. .cra_priority = 300,
  81. .cra_blocksize = SHA256_BLOCK_SIZE,
  82. .cra_module = THIS_MODULE,
  83. }
  84. } };
  85. static int __init sha2_ce_mod_init(void)
  86. {
  87. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  88. }
  89. static void __exit sha2_ce_mod_fini(void)
  90. {
  91. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  92. }
  93. module_cpu_feature_match(SHA2, sha2_ce_mod_init);
  94. module_exit(sha2_ce_mod_fini);