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. MODULE_ALIAS_CRYPTO("sha1");
  23. struct sha1_ce_state {
  24. struct sha1_state sst;
  25. u32 finalize;
  26. };
  27. asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
  28. int blocks);
  29. const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
  30. const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
  31. static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
  32. unsigned int len)
  33. {
  34. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  35. if (!may_use_simd())
  36. return crypto_sha1_update(desc, data, len);
  37. sctx->finalize = 0;
  38. kernel_neon_begin();
  39. sha1_base_do_update(desc, data, len,
  40. (sha1_block_fn *)sha1_ce_transform);
  41. kernel_neon_end();
  42. return 0;
  43. }
  44. static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
  45. unsigned int len, u8 *out)
  46. {
  47. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  48. bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE) && len;
  49. if (!may_use_simd())
  50. return crypto_sha1_finup(desc, data, len, out);
  51. /*
  52. * Allow the asm code to perform the finalization if there is no
  53. * partial data and the input is a round multiple of the block size.
  54. */
  55. sctx->finalize = finalize;
  56. kernel_neon_begin();
  57. sha1_base_do_update(desc, data, len,
  58. (sha1_block_fn *)sha1_ce_transform);
  59. if (!finalize)
  60. sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
  61. kernel_neon_end();
  62. return sha1_base_finish(desc, out);
  63. }
  64. static int sha1_ce_final(struct shash_desc *desc, u8 *out)
  65. {
  66. struct sha1_ce_state *sctx = shash_desc_ctx(desc);
  67. if (!may_use_simd())
  68. return crypto_sha1_finup(desc, NULL, 0, out);
  69. sctx->finalize = 0;
  70. kernel_neon_begin();
  71. sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
  72. kernel_neon_end();
  73. return sha1_base_finish(desc, out);
  74. }
  75. static struct shash_alg alg = {
  76. .init = sha1_base_init,
  77. .update = sha1_ce_update,
  78. .final = sha1_ce_final,
  79. .finup = sha1_ce_finup,
  80. .descsize = sizeof(struct sha1_ce_state),
  81. .digestsize = SHA1_DIGEST_SIZE,
  82. .base = {
  83. .cra_name = "sha1",
  84. .cra_driver_name = "sha1-ce",
  85. .cra_priority = 200,
  86. .cra_blocksize = SHA1_BLOCK_SIZE,
  87. .cra_module = THIS_MODULE,
  88. }
  89. };
  90. static int __init sha1_ce_mod_init(void)
  91. {
  92. return crypto_register_shash(&alg);
  93. }
  94. static void __exit sha1_ce_mod_fini(void)
  95. {
  96. crypto_unregister_shash(&alg);
  97. }
  98. module_cpu_feature_match(SHA1, sha1_ce_mod_init);
  99. module_exit(sha1_ce_mod_fini);