sha1-ce-glue.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
  3. *
  4. * Copyright (C) 2014 - 2017 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/simd.h>
  12. #include <asm/unaligned.h>
  13. #include <crypto/internal/hash.h>
  14. #include <crypto/sha.h>
  15. #include <crypto/sha1_base.h>
  16. #include <linux/cpufeature.h>
  17. #include <linux/crypto.h>
  18. #include <linux/module.h>
  19. MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
  20. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  21. MODULE_LICENSE("GPL v2");
  22. struct sha1_ce_state {
  23. struct sha1_state sst;
  24. u32 finalize;
  25. };
  26. asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
  27. int blocks);
  28. const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
  29. const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
  30. static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
  31. unsigned int len)
  32. {
  33. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  34. if (!may_use_simd())
  35. return crypto_sha1_update(desc, data, len);
  36. sctx->finalize = 0;
  37. kernel_neon_begin();
  38. sha1_base_do_update(desc, data, len,
  39. (sha1_block_fn *)sha1_ce_transform);
  40. kernel_neon_end();
  41. return 0;
  42. }
  43. static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
  44. unsigned int len, u8 *out)
  45. {
  46. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  47. bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE) && len;
  48. if (!may_use_simd())
  49. return crypto_sha1_finup(desc, data, len, out);
  50. /*
  51. * Allow the asm code to perform the finalization if there is no
  52. * partial data and the input is a round multiple of the block size.
  53. */
  54. sctx->finalize = finalize;
  55. kernel_neon_begin();
  56. sha1_base_do_update(desc, data, len,
  57. (sha1_block_fn *)sha1_ce_transform);
  58. if (!finalize)
  59. sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
  60. kernel_neon_end();
  61. return sha1_base_finish(desc, out);
  62. }
  63. static int sha1_ce_final(struct shash_desc *desc, u8 *out)
  64. {
  65. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  66. if (!may_use_simd())
  67. return crypto_sha1_finup(desc, NULL, 0, out);
  68. sctx->finalize = 0;
  69. kernel_neon_begin();
  70. sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
  71. kernel_neon_end();
  72. return sha1_base_finish(desc, out);
  73. }
  74. static struct shash_alg alg = {
  75. .init = sha1_base_init,
  76. .update = sha1_ce_update,
  77. .final = sha1_ce_final,
  78. .finup = sha1_ce_finup,
  79. .descsize = sizeof(struct sha1_ce_state),
  80. .digestsize = SHA1_DIGEST_SIZE,
  81. .base = {
  82. .cra_name = "sha1",
  83. .cra_driver_name = "sha1-ce",
  84. .cra_priority = 200,
  85. .cra_blocksize = SHA1_BLOCK_SIZE,
  86. .cra_module = THIS_MODULE,
  87. }
  88. };
  89. static int __init sha1_ce_mod_init(void)
  90. {
  91. return crypto_register_shash(&alg);
  92. }
  93. static void __exit sha1_ce_mod_fini(void)
  94. {
  95. crypto_unregister_shash(&alg);
  96. }
  97. module_cpu_feature_match(SHA1, sha1_ce_mod_init);
  98. module_exit(sha1_ce_mod_fini);