nhpoly1305.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Common values and helper functions for the NHPoly1305 hash function.
  4. */
  5. #ifndef _NHPOLY1305_H
  6. #define _NHPOLY1305_H
  7. #include <crypto/hash.h>
  8. #include <crypto/poly1305.h>
  9. /* NH parameterization: */
  10. /* Endianness: little */
  11. /* Word size: 32 bits (works well on NEON, SSE2, AVX2) */
  12. /* Stride: 2 words (optimal on ARM32 NEON; works okay on other CPUs too) */
  13. #define NH_PAIR_STRIDE 2
  14. #define NH_MESSAGE_UNIT (NH_PAIR_STRIDE * 2 * sizeof(u32))
  15. /* Num passes (Toeplitz iteration count): 4, to give ε = 2^{-128} */
  16. #define NH_NUM_PASSES 4
  17. #define NH_HASH_BYTES (NH_NUM_PASSES * sizeof(u64))
  18. /* Max message size: 1024 bytes (32x compression factor) */
  19. #define NH_NUM_STRIDES 64
  20. #define NH_MESSAGE_WORDS (NH_PAIR_STRIDE * 2 * NH_NUM_STRIDES)
  21. #define NH_MESSAGE_BYTES (NH_MESSAGE_WORDS * sizeof(u32))
  22. #define NH_KEY_WORDS (NH_MESSAGE_WORDS + \
  23. NH_PAIR_STRIDE * 2 * (NH_NUM_PASSES - 1))
  24. #define NH_KEY_BYTES (NH_KEY_WORDS * sizeof(u32))
  25. #define NHPOLY1305_KEY_SIZE (POLY1305_BLOCK_SIZE + NH_KEY_BYTES)
  26. struct nhpoly1305_key {
  27. struct poly1305_key poly_key;
  28. u32 nh_key[NH_KEY_WORDS];
  29. };
  30. struct nhpoly1305_state {
  31. /* Running total of polynomial evaluation */
  32. struct poly1305_state poly_state;
  33. /* Partial block buffer */
  34. u8 buffer[NH_MESSAGE_UNIT];
  35. unsigned int buflen;
  36. /*
  37. * Number of bytes remaining until the current NH message reaches
  38. * NH_MESSAGE_BYTES. When nonzero, 'nh_hash' holds the partial NH hash.
  39. */
  40. unsigned int nh_remaining;
  41. __le64 nh_hash[NH_NUM_PASSES];
  42. };
  43. typedef void (*nh_t)(const u32 *key, const u8 *message, size_t message_len,
  44. __le64 hash[NH_NUM_PASSES]);
  45. int crypto_nhpoly1305_setkey(struct crypto_shash *tfm,
  46. const u8 *key, unsigned int keylen);
  47. int crypto_nhpoly1305_init(struct shash_desc *desc);
  48. int crypto_nhpoly1305_update(struct shash_desc *desc,
  49. const u8 *src, unsigned int srclen);
  50. int crypto_nhpoly1305_update_helper(struct shash_desc *desc,
  51. const u8 *src, unsigned int srclen,
  52. nh_t nh_fn);
  53. int crypto_nhpoly1305_final(struct shash_desc *desc, u8 *dst);
  54. int crypto_nhpoly1305_final_helper(struct shash_desc *desc, u8 *dst,
  55. nh_t nh_fn);
  56. #endif /* _NHPOLY1305_H */