sha2-ce-glue.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions
  3. *
  4. * Copyright (C) 2014 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 <asm/neon.h>
  11. #include <asm/unaligned.h>
  12. #include <crypto/internal/hash.h>
  13. #include <crypto/sha.h>
  14. #include <crypto/sha256_base.h>
  15. #include <linux/cpufeature.h>
  16. #include <linux/crypto.h>
  17. #include <linux/module.h>
  18. MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions");
  19. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  20. MODULE_LICENSE("GPL v2");
  21. struct sha256_ce_state {
  22. struct sha256_state sst;
  23. u32 finalize;
  24. };
  25. asmlinkage void sha2_ce_transform(struct sha256_ce_state *sst, u8 const *src,
  26. int blocks);
  27. const u32 sha256_ce_offsetof_count = offsetof(struct sha256_ce_state,
  28. sst.count);
  29. const u32 sha256_ce_offsetof_finalize = offsetof(struct sha256_ce_state,
  30. finalize);
  31. static int sha256_ce_update(struct shash_desc *desc, const u8 *data,
  32. unsigned int len)
  33. {
  34. struct sha256_ce_state *sctx = shash_desc_ctx(desc);
  35. sctx->finalize = 0;
  36. kernel_neon_begin_partial(28);
  37. sha256_base_do_update(desc, data, len,
  38. (sha256_block_fn *)sha2_ce_transform);
  39. kernel_neon_end();
  40. return 0;
  41. }
  42. static int sha256_ce_finup(struct shash_desc *desc, const u8 *data,
  43. unsigned int len, u8 *out)
  44. {
  45. struct sha256_ce_state *sctx = shash_desc_ctx(desc);
  46. bool finalize = !sctx->sst.count && !(len % SHA256_BLOCK_SIZE);
  47. /*
  48. * Allow the asm code to perform the finalization if there is no
  49. * partial data and the input is a round multiple of the block size.
  50. */
  51. sctx->finalize = finalize;
  52. kernel_neon_begin_partial(28);
  53. sha256_base_do_update(desc, data, len,
  54. (sha256_block_fn *)sha2_ce_transform);
  55. if (!finalize)
  56. sha256_base_do_finalize(desc,
  57. (sha256_block_fn *)sha2_ce_transform);
  58. kernel_neon_end();
  59. return sha256_base_finish(desc, out);
  60. }
  61. static int sha256_ce_final(struct shash_desc *desc, u8 *out)
  62. {
  63. struct sha256_ce_state *sctx = shash_desc_ctx(desc);
  64. sctx->finalize = 0;
  65. kernel_neon_begin_partial(28);
  66. sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform);
  67. kernel_neon_end();
  68. return sha256_base_finish(desc, out);
  69. }
  70. static struct shash_alg algs[] = { {
  71. .init = sha224_base_init,
  72. .update = sha256_ce_update,
  73. .final = sha256_ce_final,
  74. .finup = sha256_ce_finup,
  75. .descsize = sizeof(struct sha256_ce_state),
  76. .digestsize = SHA224_DIGEST_SIZE,
  77. .base = {
  78. .cra_name = "sha224",
  79. .cra_driver_name = "sha224-ce",
  80. .cra_priority = 200,
  81. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  82. .cra_blocksize = SHA256_BLOCK_SIZE,
  83. .cra_module = THIS_MODULE,
  84. }
  85. }, {
  86. .init = sha256_base_init,
  87. .update = sha256_ce_update,
  88. .final = sha256_ce_final,
  89. .finup = sha256_ce_finup,
  90. .descsize = sizeof(struct sha256_ce_state),
  91. .digestsize = SHA256_DIGEST_SIZE,
  92. .base = {
  93. .cra_name = "sha256",
  94. .cra_driver_name = "sha256-ce",
  95. .cra_priority = 200,
  96. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  97. .cra_blocksize = SHA256_BLOCK_SIZE,
  98. .cra_module = THIS_MODULE,
  99. }
  100. } };
  101. static int __init sha2_ce_mod_init(void)
  102. {
  103. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  104. }
  105. static void __exit sha2_ce_mod_fini(void)
  106. {
  107. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  108. }
  109. module_cpu_feature_match(SHA2, sha2_ce_mod_init);
  110. module_exit(sha2_ce_mod_fini);