tls_gcm.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* AES-128-GCM (NIST SP 800-38D) for the TLS 1.2 client: 96-bit nonces,
  2. * 16-byte tags. The block cipher is wpa_crypto.c's AES-128; GHASH uses
  3. * Shoup's 4-bit tables. Same streaming shape as the ChaCha20-Poly1305
  4. * AEAD in tls_chacha20.h, so the record pump can decrypt a record in
  5. * chunks straight from the TCP stream. Pure freestanding C with host
  6. * vectors (make test-crypto). */
  7. #ifndef TLS_GCM_H
  8. #define TLS_GCM_H
  9. struct gcm_key {
  10. unsigned int rk[44]; /* AES-128 round keys */
  11. unsigned int hl[16][4]; /* GHASH 4-bit table: hl[i] = i*H */
  12. };
  13. struct gcm_ctx {
  14. const struct gcm_key *k;
  15. unsigned int y[4]; /* GHASH accumulator, big-endian words */
  16. unsigned char ctr[16]; /* current counter block */
  17. unsigned char ks[16]; /* key stream of ctr */
  18. unsigned char ej0[16]; /* E_K(J0), XORed into the tag */
  19. unsigned char part[16]; /* partial GHASH block */
  20. unsigned int ks_pos, part_len;
  21. unsigned int aad_len, ct_len;
  22. };
  23. void gcm_setkey(struct gcm_key *k, const unsigned char key[16]);
  24. /* Decryption, streamed: open_update may be called any number of times. */
  25. void gcm_open_init(struct gcm_ctx *c, const struct gcm_key *k,
  26. const unsigned char nonce[12],
  27. const unsigned char *aad, unsigned int aad_len);
  28. void gcm_open_update(struct gcm_ctx *c, unsigned char *out,
  29. const unsigned char *in, unsigned int len);
  30. void gcm_open_tag(struct gcm_ctx *c, unsigned char tag[16]);
  31. /* One-shot seal: ct[0..len-1] (may alias pt) and the 16-byte tag. */
  32. void gcm_seal(const struct gcm_key *k, const unsigned char nonce[12],
  33. const unsigned char *aad, unsigned int aad_len,
  34. const unsigned char *pt, unsigned int len,
  35. unsigned char *ct, unsigned char tag[16]);
  36. #endif