sha2-ce-glue.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #define ASM_EXPORT(sym, val) \
  19. asm(".globl " #sym "; .set " #sym ", %0" :: "I"(val));
  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. struct sha256_ce_state {
  24. struct sha256_state sst;
  25. u32 finalize;
  26. };
  27. asmlinkage void sha2_ce_transform(struct sha256_ce_state *sst, u8 const *src,
  28. int blocks);
  29. static int sha256_ce_update(struct shash_desc *desc, const u8 *data,
  30. unsigned int len)
  31. {
  32. struct sha256_ce_state *sctx = shash_desc_ctx(desc);
  33. sctx->finalize = 0;
  34. kernel_neon_begin_partial(28);
  35. sha256_base_do_update(desc, data, len,
  36. (sha256_block_fn *)sha2_ce_transform);
  37. kernel_neon_end();
  38. return 0;
  39. }
  40. static int sha256_ce_finup(struct shash_desc *desc, const u8 *data,
  41. unsigned int len, u8 *out)
  42. {
  43. struct sha256_ce_state *sctx = shash_desc_ctx(desc);
  44. bool finalize = !sctx->sst.count && !(len % SHA256_BLOCK_SIZE);
  45. ASM_EXPORT(sha256_ce_offsetof_count,
  46. offsetof(struct sha256_ce_state, sst.count));
  47. ASM_EXPORT(sha256_ce_offsetof_finalize,
  48. offsetof(struct sha256_ce_state, finalize));
  49. /*
  50. * Allow the asm code to perform the finalization if there is no
  51. * partial data and the input is a round multiple of the block size.
  52. */
  53. sctx->finalize = finalize;
  54. kernel_neon_begin_partial(28);
  55. sha256_base_do_update(desc, data, len,
  56. (sha256_block_fn *)sha2_ce_transform);
  57. if (!finalize)
  58. sha256_base_do_finalize(desc,
  59. (sha256_block_fn *)sha2_ce_transform);
  60. kernel_neon_end();
  61. return sha256_base_finish(desc, out);
  62. }
  63. static int sha256_ce_final(struct shash_desc *desc, u8 *out)
  64. {
  65. struct sha256_ce_state *sctx = shash_desc_ctx(desc);
  66. sctx->finalize = 0;
  67. kernel_neon_begin_partial(28);
  68. sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform);
  69. kernel_neon_end();
  70. return sha256_base_finish(desc, out);
  71. }
  72. static struct shash_alg algs[] = { {
  73. .init = sha224_base_init,
  74. .update = sha256_ce_update,
  75. .final = sha256_ce_final,
  76. .finup = sha256_ce_finup,
  77. .descsize = sizeof(struct sha256_ce_state),
  78. .digestsize = SHA224_DIGEST_SIZE,
  79. .base = {
  80. .cra_name = "sha224",
  81. .cra_driver_name = "sha224-ce",
  82. .cra_priority = 200,
  83. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  84. .cra_blocksize = SHA256_BLOCK_SIZE,
  85. .cra_module = THIS_MODULE,
  86. }
  87. }, {
  88. .init = sha256_base_init,
  89. .update = sha256_ce_update,
  90. .final = sha256_ce_final,
  91. .finup = sha256_ce_finup,
  92. .descsize = sizeof(struct sha256_ce_state),
  93. .digestsize = SHA256_DIGEST_SIZE,
  94. .base = {
  95. .cra_name = "sha256",
  96. .cra_driver_name = "sha256-ce",
  97. .cra_priority = 200,
  98. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  99. .cra_blocksize = SHA256_BLOCK_SIZE,
  100. .cra_module = THIS_MODULE,
  101. }
  102. } };
  103. static int __init sha2_ce_mod_init(void)
  104. {
  105. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  106. }
  107. static void __exit sha2_ce_mod_fini(void)
  108. {
  109. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  110. }
  111. module_cpu_feature_match(SHA2, sha2_ce_mod_init);
  112. module_exit(sha2_ce_mod_fini);