sha2-ce-glue.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/crypto.h>
  14. #include <linux/module.h>
  15. #include <asm/hwcap.h>
  16. #include <asm/simd.h>
  17. #include <asm/neon.h>
  18. #include <asm/unaligned.h>
  19. #include "sha256_glue.h"
  20. MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions");
  21. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  22. MODULE_LICENSE("GPL v2");
  23. asmlinkage void sha2_ce_transform(struct sha256_state *sst, u8 const *src,
  24. int blocks);
  25. static int sha2_ce_update(struct shash_desc *desc, const u8 *data,
  26. unsigned int len)
  27. {
  28. struct sha256_state *sctx = shash_desc_ctx(desc);
  29. if (!may_use_simd() ||
  30. (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
  31. return crypto_sha256_arm_update(desc, data, len);
  32. kernel_neon_begin();
  33. sha256_base_do_update(desc, data, len,
  34. (sha256_block_fn *)sha2_ce_transform);
  35. kernel_neon_end();
  36. return 0;
  37. }
  38. static int sha2_ce_finup(struct shash_desc *desc, const u8 *data,
  39. unsigned int len, u8 *out)
  40. {
  41. if (!may_use_simd())
  42. return crypto_sha256_arm_finup(desc, data, len, out);
  43. kernel_neon_begin();
  44. if (len)
  45. sha256_base_do_update(desc, data, len,
  46. (sha256_block_fn *)sha2_ce_transform);
  47. sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform);
  48. kernel_neon_end();
  49. return sha256_base_finish(desc, out);
  50. }
  51. static int sha2_ce_final(struct shash_desc *desc, u8 *out)
  52. {
  53. return sha2_ce_finup(desc, NULL, 0, out);
  54. }
  55. static struct shash_alg algs[] = { {
  56. .init = sha224_base_init,
  57. .update = sha2_ce_update,
  58. .final = sha2_ce_final,
  59. .finup = sha2_ce_finup,
  60. .descsize = sizeof(struct sha256_state),
  61. .digestsize = SHA224_DIGEST_SIZE,
  62. .base = {
  63. .cra_name = "sha224",
  64. .cra_driver_name = "sha224-ce",
  65. .cra_priority = 300,
  66. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  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_flags = CRYPTO_ALG_TYPE_SHASH,
  82. .cra_blocksize = SHA256_BLOCK_SIZE,
  83. .cra_module = THIS_MODULE,
  84. }
  85. } };
  86. static int __init sha2_ce_mod_init(void)
  87. {
  88. if (!(elf_hwcap2 & HWCAP2_SHA2))
  89. return -ENODEV;
  90. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  91. }
  92. static void __exit sha2_ce_mod_fini(void)
  93. {
  94. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  95. }
  96. module_init(sha2_ce_mod_init);
  97. module_exit(sha2_ce_mod_fini);