sha1-ce-glue.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * sha1-ce-glue.c - SHA-1 secure hash 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/sha1_base.h>
  13. #include <linux/crypto.h>
  14. #include <linux/module.h>
  15. #include <asm/hwcap.h>
  16. #include <asm/neon.h>
  17. #include <asm/simd.h>
  18. #include "sha1.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. asmlinkage void sha1_ce_transform(struct sha1_state *sst, u8 const *src,
  23. int blocks);
  24. static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
  25. unsigned int len)
  26. {
  27. struct sha1_state *sctx = shash_desc_ctx(desc);
  28. if (!may_use_simd() ||
  29. (sctx->count % SHA1_BLOCK_SIZE) + len < SHA1_BLOCK_SIZE)
  30. return sha1_update_arm(desc, data, len);
  31. kernel_neon_begin();
  32. sha1_base_do_update(desc, data, len, sha1_ce_transform);
  33. kernel_neon_end();
  34. return 0;
  35. }
  36. static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
  37. unsigned int len, u8 *out)
  38. {
  39. if (!may_use_simd())
  40. return sha1_finup_arm(desc, data, len, out);
  41. kernel_neon_begin();
  42. if (len)
  43. sha1_base_do_update(desc, data, len, sha1_ce_transform);
  44. sha1_base_do_finalize(desc, sha1_ce_transform);
  45. kernel_neon_end();
  46. return sha1_base_finish(desc, out);
  47. }
  48. static int sha1_ce_final(struct shash_desc *desc, u8 *out)
  49. {
  50. return sha1_ce_finup(desc, NULL, 0, out);
  51. }
  52. static struct shash_alg alg = {
  53. .init = sha1_base_init,
  54. .update = sha1_ce_update,
  55. .final = sha1_ce_final,
  56. .finup = sha1_ce_finup,
  57. .descsize = sizeof(struct sha1_state),
  58. .digestsize = SHA1_DIGEST_SIZE,
  59. .base = {
  60. .cra_name = "sha1",
  61. .cra_driver_name = "sha1-ce",
  62. .cra_priority = 200,
  63. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  64. .cra_blocksize = SHA1_BLOCK_SIZE,
  65. .cra_module = THIS_MODULE,
  66. }
  67. };
  68. static int __init sha1_ce_mod_init(void)
  69. {
  70. if (!(elf_hwcap2 & HWCAP2_SHA1))
  71. return -ENODEV;
  72. return crypto_register_shash(&alg);
  73. }
  74. static void __exit sha1_ce_mod_fini(void)
  75. {
  76. crypto_unregister_shash(&alg);
  77. }
  78. module_init(sha1_ce_mod_init);
  79. module_exit(sha1_ce_mod_fini);