poly1305.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Common values for the Poly1305 algorithm
  4. */
  5. #ifndef _CRYPTO_POLY1305_H
  6. #define _CRYPTO_POLY1305_H
  7. #include <linux/types.h>
  8. #include <linux/crypto.h>
  9. #define POLY1305_BLOCK_SIZE 16
  10. #define POLY1305_KEY_SIZE 32
  11. #define POLY1305_DIGEST_SIZE 16
  12. struct poly1305_key {
  13. u32 r[5]; /* key, base 2^26 */
  14. };
  15. struct poly1305_state {
  16. u32 h[5]; /* accumulator, base 2^26 */
  17. };
  18. struct poly1305_desc_ctx {
  19. /* key */
  20. struct poly1305_key r;
  21. /* finalize key */
  22. u32 s[4];
  23. /* accumulator */
  24. struct poly1305_state h;
  25. /* partial buffer */
  26. u8 buf[POLY1305_BLOCK_SIZE];
  27. /* bytes used in partial buffer */
  28. unsigned int buflen;
  29. /* r key has been set */
  30. bool rset;
  31. /* s key has been set */
  32. bool sset;
  33. };
  34. /*
  35. * Poly1305 core functions. These implement the ε-almost-∆-universal hash
  36. * function underlying the Poly1305 MAC, i.e. they don't add an encrypted nonce
  37. * ("s key") at the end. They also only support block-aligned inputs.
  38. */
  39. void poly1305_core_setkey(struct poly1305_key *key, const u8 *raw_key);
  40. static inline void poly1305_core_init(struct poly1305_state *state)
  41. {
  42. memset(state->h, 0, sizeof(state->h));
  43. }
  44. void poly1305_core_blocks(struct poly1305_state *state,
  45. const struct poly1305_key *key,
  46. const void *src, unsigned int nblocks);
  47. void poly1305_core_emit(const struct poly1305_state *state, void *dst);
  48. /* Crypto API helper functions for the Poly1305 MAC */
  49. int crypto_poly1305_init(struct shash_desc *desc);
  50. unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx,
  51. const u8 *src, unsigned int srclen);
  52. int crypto_poly1305_update(struct shash_desc *desc,
  53. const u8 *src, unsigned int srclen);
  54. int crypto_poly1305_final(struct shash_desc *desc, u8 *dst);
  55. #endif