DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
quic_aead.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file quic_aead.h
6 * @brief AES-128 block cipher and AEAD_AES_128_GCM (RFC 5116 / NIST SP 800-38D).
7 *
8 * The QUIC Initial packet protection AEAD is AEAD_AES_128_GCM (RFC 9001 sec 5.3), and header
9 * protection samples a keystream block from AES-128 in ECB mode (sec 5.4). Both need a single
10 * primitive: encrypt one 16-byte block under a 128-bit key. GCM's counter mode and its GHASH
11 * authenticator are then layered on top in software.
12 *
13 * This mirrors the SSH cipher split: on Arduino (ESP32) the AES block is mbedtls, routed to the
14 * hardware AES accelerator; on native host builds a compact software AES-128 is used so the whole
15 * AEAD is unit-testable off-target. GHASH and the counter loop are the same software on both.
16 *
17 * Pure, zero heap, host-tested against the NIST GCM test vectors and RFC 9001 Appendix A.
18 *
19 * @author Douglas Quigg (dstroy0)
20 * @date 2026
21 */
22
23#ifndef DETERMINISTICESPASYNCWEBSERVER_QUIC_AEAD_H
24#define DETERMINISTICESPASYNCWEBSERVER_QUIC_AEAD_H
25
26#include "ServerConfig.h"
27
28// Shared by the HTTP/3 (QUIC) packet protection and the DTLS 1.3 record layer.
29#if (DETWS_ENABLE_HTTP3 || DETWS_ENABLE_DTLS)
30
31#include <stddef.h>
32#include <stdint.h>
33
34/** @brief AEAD_AES_128_GCM authentication tag length in bytes. */
35#define QUIC_AEAD_TAG_LEN 16
36
37// ---------------------------------------------------------------------------
38// AES-128 single-block primitive (used by GCM and by header protection)
39// ---------------------------------------------------------------------------
40
41#ifdef ARDUINO
42#include <mbedtls/aes.h>
43struct QuicAes128
44{
45 mbedtls_aes_context mbed; ///< mbedtls context (HW-accelerated on ESP32), key schedule loaded.
46};
47#else
48struct QuicAes128
49{
50 uint32_t rk[44]; ///< AES-128 expanded round-key schedule (11 round keys x 4 words).
51};
52#endif
53
54/** @brief Load a 128-bit key and expand the encryption key schedule. */
55void quic_aes128_init(QuicAes128 *ctx, const uint8_t key[16]);
56
57/** @brief Encrypt one 16-byte block (ECB). @p in and @p out may alias. */
58void quic_aes128_encrypt_block(QuicAes128 *ctx, const uint8_t in[16], uint8_t out[16]);
59
60/** @brief Wipe the key schedule (and release mbedtls state on Arduino). */
61void quic_aes128_wipe(QuicAes128 *ctx);
62
63// ---------------------------------------------------------------------------
64// AEAD_AES_128_GCM (96-bit nonce, 128-bit tag)
65// ---------------------------------------------------------------------------
66
67/**
68 * @brief Seal: AEAD_AES_128_GCM encrypt-and-authenticate.
69 *
70 * Writes @p pt_len ciphertext bytes followed by the 16-byte tag into @p out, so @p out must hold
71 * at least @p pt_len + QUIC_AEAD_TAG_LEN bytes. @p out may alias @p pt (in-place encryption).
72 *
73 * @param key 16-byte key.
74 * @param nonce 12-byte nonce.
75 * @param aad Additional authenticated data (may be NULL when @p aad_len is 0).
76 * @param aad_len AAD length.
77 * @param pt Plaintext.
78 * @param pt_len Plaintext length.
79 * @param out Output: ciphertext || tag (@p pt_len + 16 bytes).
80 */
81void quic_aes128_gcm_seal(const uint8_t key[16], const uint8_t nonce[12], const uint8_t *aad, size_t aad_len,
82 const uint8_t *pt, size_t pt_len, uint8_t *out);
83
84/**
85 * @brief Open: AEAD_AES_128_GCM verify-and-decrypt.
86 *
87 * @p ct is ciphertext followed by the 16-byte tag (so @p ct_len >= QUIC_AEAD_TAG_LEN). The tag is
88 * verified in constant time before any plaintext is exposed; on mismatch nothing is written and the
89 * function returns false. @p out receives @p ct_len - 16 plaintext bytes and may alias @p ct.
90 *
91 * @return true if the tag is valid (plaintext written), false otherwise.
92 */
93bool quic_aes128_gcm_open(const uint8_t key[16], const uint8_t nonce[12], const uint8_t *aad, size_t aad_len,
94 const uint8_t *ct, size_t ct_len, uint8_t *out);
95
96#endif // DETWS_ENABLE_HTTP3 || DETWS_ENABLE_DTLS
97#endif // DETERMINISTICESPASYNCWEBSERVER_QUIC_AEAD_H
User-facing configuration for DeterministicESPAsyncWebServer.