tls_chacha20.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* ChaCha20-Poly1305 AEAD (RFC 8439) for the ring-3 TLS client.
  2. * The open path is incremental (ciphertext arrives in TCP-sized chunks);
  3. * the seal path is one-shot (everything we send is small). */
  4. #ifndef TLS_CHACHA20_H
  5. #define TLS_CHACHA20_H
  6. struct chacha20_ctx {
  7. unsigned int st[16];
  8. unsigned char block[64];
  9. unsigned int used; /* bytes consumed from block (64 = none) */
  10. };
  11. void chacha20_init(struct chacha20_ctx *c, const unsigned char key[32],
  12. const unsigned char nonce[12], unsigned int counter);
  13. void chacha20_xor(struct chacha20_ctx *c, unsigned char *out,
  14. const unsigned char *in, unsigned int len);
  15. struct poly1305_ctx {
  16. unsigned int r[5], h[5];
  17. unsigned int pad[4];
  18. unsigned int leftover;
  19. unsigned char buf[16];
  20. };
  21. void poly1305_init(struct poly1305_ctx *c, const unsigned char key[32]);
  22. void poly1305_update(struct poly1305_ctx *c, const unsigned char *m,
  23. unsigned int len);
  24. void poly1305_final(struct poly1305_ctx *c, unsigned char tag[16]);
  25. /* Incremental AEAD open: init with the key/nonce and the record header as
  26. * AAD, feed ciphertext chunks, then read the tag to compare. */
  27. struct aead_open_ctx {
  28. struct chacha20_ctx ch;
  29. struct poly1305_ctx poly;
  30. unsigned int aad_len;
  31. unsigned int ct_len;
  32. };
  33. void aead_open_init(struct aead_open_ctx *a, const unsigned char key[32],
  34. const unsigned char nonce[12],
  35. const unsigned char *aad, unsigned int aad_len);
  36. void aead_open_update(struct aead_open_ctx *a, unsigned char *out,
  37. const unsigned char *in, unsigned int len);
  38. void aead_open_tag(struct aead_open_ctx *a, unsigned char tag[16]);
  39. /* One-shot seal: ct[0..len-1] and the 16-byte tag. */
  40. void aead_seal(const unsigned char key[32], const unsigned char nonce[12],
  41. const unsigned char *aad, unsigned int aad_len,
  42. const unsigned char *pt, unsigned int len,
  43. unsigned char *ct, unsigned char tag[16]);
  44. #endif