nhpoly1305-neon-glue.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NHPoly1305 - ε-almost-∆-universal hash function for Adiantum
  4. * (NEON accelerated version)
  5. *
  6. * Copyright 2018 Google LLC
  7. */
  8. #include <asm/neon.h>
  9. #include <asm/simd.h>
  10. #include <crypto/internal/hash.h>
  11. #include <crypto/internal/simd.h>
  12. #include <crypto/nhpoly1305.h>
  13. #include <linux/module.h>
  14. asmlinkage void nh_neon(const u32 *key, const u8 *message, size_t message_len,
  15. u8 hash[NH_HASH_BYTES]);
  16. /* wrapper to avoid indirect call to assembly, which doesn't work with CFI */
  17. static void _nh_neon(const u32 *key, const u8 *message, size_t message_len,
  18. __le64 hash[NH_NUM_PASSES])
  19. {
  20. nh_neon(key, message, message_len, (u8 *)hash);
  21. }
  22. static int nhpoly1305_neon_update(struct shash_desc *desc,
  23. const u8 *src, unsigned int srclen)
  24. {
  25. if (srclen < 64 || !crypto_simd_usable())
  26. return crypto_nhpoly1305_update(desc, src, srclen);
  27. do {
  28. unsigned int n = min_t(unsigned int, srclen, SZ_4K);
  29. kernel_neon_begin();
  30. crypto_nhpoly1305_update_helper(desc, src, n, _nh_neon);
  31. kernel_neon_end();
  32. src += n;
  33. srclen -= n;
  34. } while (srclen);
  35. return 0;
  36. }
  37. static struct shash_alg nhpoly1305_alg = {
  38. .base.cra_name = "nhpoly1305",
  39. .base.cra_driver_name = "nhpoly1305-neon",
  40. .base.cra_priority = 200,
  41. .base.cra_ctxsize = sizeof(struct nhpoly1305_key),
  42. .base.cra_module = THIS_MODULE,
  43. .digestsize = POLY1305_DIGEST_SIZE,
  44. .init = crypto_nhpoly1305_init,
  45. .update = nhpoly1305_neon_update,
  46. .final = crypto_nhpoly1305_final,
  47. .setkey = crypto_nhpoly1305_setkey,
  48. .descsize = sizeof(struct nhpoly1305_state),
  49. };
  50. static int __init nhpoly1305_mod_init(void)
  51. {
  52. if (!(elf_hwcap & HWCAP_NEON))
  53. return -ENODEV;
  54. return crypto_register_shash(&nhpoly1305_alg);
  55. }
  56. static void __exit nhpoly1305_mod_exit(void)
  57. {
  58. crypto_unregister_shash(&nhpoly1305_alg);
  59. }
  60. module_init(nhpoly1305_mod_init);
  61. module_exit(nhpoly1305_mod_exit);
  62. MODULE_DESCRIPTION("NHPoly1305 ε-almost-∆-universal hash function (NEON-accelerated)");
  63. MODULE_LICENSE("GPL v2");
  64. MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
  65. MODULE_ALIAS_CRYPTO("nhpoly1305");
  66. MODULE_ALIAS_CRYPTO("nhpoly1305-neon");