nhpoly1305-neon-glue.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. __le64 hash[NH_NUM_PASSES]);
  16. static int nhpoly1305_neon_update(struct shash_desc *desc,
  17. const u8 *src, unsigned int srclen)
  18. {
  19. if (srclen < 64 || !crypto_simd_usable())
  20. return crypto_nhpoly1305_update(desc, src, srclen);
  21. do {
  22. unsigned int n = min_t(unsigned int, srclen, SZ_4K);
  23. kernel_neon_begin();
  24. crypto_nhpoly1305_update_helper(desc, src, n, nh_neon);
  25. kernel_neon_end();
  26. src += n;
  27. srclen -= n;
  28. } while (srclen);
  29. return 0;
  30. }
  31. static int nhpoly1305_neon_digest(struct shash_desc *desc,
  32. const u8 *src, unsigned int srclen, u8 *out)
  33. {
  34. return crypto_nhpoly1305_init(desc) ?:
  35. nhpoly1305_neon_update(desc, src, srclen) ?:
  36. crypto_nhpoly1305_final(desc, out);
  37. }
  38. static struct shash_alg nhpoly1305_alg = {
  39. .base.cra_name = "nhpoly1305",
  40. .base.cra_driver_name = "nhpoly1305-neon",
  41. .base.cra_priority = 200,
  42. .base.cra_ctxsize = sizeof(struct nhpoly1305_key),
  43. .base.cra_module = THIS_MODULE,
  44. .digestsize = POLY1305_DIGEST_SIZE,
  45. .init = crypto_nhpoly1305_init,
  46. .update = nhpoly1305_neon_update,
  47. .final = crypto_nhpoly1305_final,
  48. .digest = nhpoly1305_neon_digest,
  49. .setkey = crypto_nhpoly1305_setkey,
  50. .descsize = sizeof(struct nhpoly1305_state),
  51. };
  52. static int __init nhpoly1305_mod_init(void)
  53. {
  54. if (!(elf_hwcap & HWCAP_NEON))
  55. return -ENODEV;
  56. return crypto_register_shash(&nhpoly1305_alg);
  57. }
  58. static void __exit nhpoly1305_mod_exit(void)
  59. {
  60. crypto_unregister_shash(&nhpoly1305_alg);
  61. }
  62. module_init(nhpoly1305_mod_init);
  63. module_exit(nhpoly1305_mod_exit);
  64. MODULE_DESCRIPTION("NHPoly1305 ε-almost-∆-universal hash function (NEON-accelerated)");
  65. MODULE_LICENSE("GPL v2");
  66. MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
  67. MODULE_ALIAS_CRYPTO("nhpoly1305");
  68. MODULE_ALIAS_CRYPTO("nhpoly1305-neon");