ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
dtls_record.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#ifndef PROTOCORE_DTLS_RECORD_H
5#define PROTOCORE_DTLS_RECORD_H
6
7#include "protocore_config.h" // the entry point: protocore_types.h for the widths
8
9EMBED_BEGIN_DECLS
10
11/**
12 * @file dtls_record.h
13 * @brief DTLS 1.3 record layer (RFC 9147 §4).
14 *
15 * The datagram counterpart to the TLS 1.3 record layer: it protects and unprotects individual
16 * UDP-carried records. This is the transport-specific half of DTLS 1.3; the handshake it carries
17 * reuses the TLS 1.3 crypto that already backs HTTP/3 (protocore_tls13_*, protocore_hkdf, aes128gcm).
18 *
19 * Two record shapes (RFC 9147 §4):
20 * - **DTLSPlaintext** - the classic 13-byte header (type, legacy_version, epoch, 48-bit sequence
21 * number, length, fragment). Used unencrypted for the first handshake flight and for alerts
22 * sent in epoch 0.
23 * - **DTLSCiphertext** - the compact "unified header" plus an AEAD-sealed body, used once record
24 * keys exist. The record's sequence number is itself encrypted (RFC 9147 §4.2.3), and the AEAD
25 * nonce is the TLS 1.3 construction over the full 64-bit sequence number (§4.2.2, epoch excluded).
26 *
27 * ─ Reuse ─
28 * AEAD (AEAD_AES_128_GCM) and the AES-128 block used for sequence-number encryption come from
29 * aes128gcm; key/iv/sn derivation from protocore_hkdf (HKDF-Expand-Label). Phase 1 supports the one
30 * cipher suite the whole hand-rolled TLS 1.3 stack uses: TLS_AES_128_GCM_SHA256.
31 *
32 * Pure, zero heap, host-tested. Not the mbedTLS TCP-TLS engine (network_drivers/tls) - this is the
33 * self-contained datagram record layer.
34 *
35 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
36 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
37 * a caller drives every namespace the same way.
38 *
39 * @author Douglas Quigg (dstroy0)
40 * @date 2026
41 */
42
43// PROTOCORE_DTLS_RECORD_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
44// it into its arena. Its size and its offset are each a static_assert, so a feature
45// combination that does not fit fails to compile rather than overrunning at run time.
46
47/** @name Record content types (RFC 8446 §5 / RFC 9147 §4).
48 * Shared by the DTLSPlaintext `type` field and the DTLSInnerPlaintext trailing content type. */
49///@{
50#define PROTOCORE_DTLS_CT_CHANGE_CIPHER_SPEC 20
51#define PROTOCORE_DTLS_CT_ALERT 21
52#define PROTOCORE_DTLS_CT_HANDSHAKE 22
53#define PROTOCORE_DTLS_CT_APPLICATION_DATA 23
54#define PROTOCORE_DTLS_CT_ACK 26 ///< DTLS 1.3 acknowledgement (RFC 9147 §7)
55///@}
56
57/** @brief DTLSPlaintext legacy_version on the wire: DTLS 1.2 (RFC 9147 §4). */
58#define PROTOCORE_DTLS_LEGACY_VERSION 0xFEFD
59
60/** @brief DTLSPlaintext header length: type(1) + version(2) + epoch(2) + seq(6) + length(2). */
61#define PROTOCORE_DTLS_PLAINTEXT_HDR_LEN 13
62
63/** @brief AEAD tag length (all supported suites: 16 bytes). */
64#define PROTOCORE_DTLS_TAG_LEN 16
65
66/** @brief Largest connection id carried in a DTLSCiphertext header (RFC 9146 / RFC 9147 §9). The CID
67 * is not length-prefixed on the wire, so the receiver must know its length from negotiation; 8
68 * bytes is ample routing entropy and bounds the fixed header-scratch buffers. */
69#define PROTOCORE_DTLS_CID_MAX 8
70
71/** @brief Record-layer AEAD suites (phase 1: AEAD_AES_128_GCM with SHA-256). */
72EMBED_ENUM_BYTE(DtlsCipher, DTLS_CIPHER_AES_128_GCM_SHA256 = 0);
73
74/**
75 * @brief One direction's record-protection keys for one epoch (RFC 9147 §4).
76 *
77 * Derived from a TLS 1.3 traffic secret; holds the AEAD key + write IV plus the separate
78 * sequence-number-encryption key. One instance per (epoch, direction).
79 */
80typedef struct
81{
82 DtlsCipher cipher; ///< negotiated AEAD (phase 1: AES-128-GCM)
83 uint16_t epoch; ///< this epoch number; its low 2 bits appear in the unified header
84 uint8_t gcm[PROTOCORE_AES128GCM_BORROW]; ///< this epoch's AEAD borrow. Carries both keyed
85 ///< contexts: the record AEAD and the
86 ///< sequence-number-protection block. Replaces the raw
87 ///< keys, so neither stays resident.
88 uint8_t iv[12]; ///< AEAD write IV (per-record nonce = iv XOR sequence_number)
90
91/** @brief Parsed view of a DTLSPlaintext record (fields point into the caller's buffer). */
92typedef struct
93{
94 uint8_t content_type;
95 uint16_t epoch;
96 uint64_t seq; ///< 48-bit record sequence number
97 const uint8_t *fragment; ///< into the input buffer
98 size_t frag_len;
100
101/** @brief Result of a successful @ref protocore_dtls_ciphertext_unprotect. */
102typedef struct
103{
104 uint8_t content_type; ///< recovered inner content type (last non-zero byte of the inner plaintext)
105 uint16_t epoch; ///< epoch of @p keys (its low 2 bits matched the header)
106 uint64_t seq; ///< reconstructed full sequence number
107 size_t pt_len; ///< plaintext bytes written to @p out
109
110/** @brief 64-record sliding replay window over the highest sequence number accepted in an epoch. */
111typedef struct
112{
113 uint64_t highest; ///< highest accepted sequence number (bit 0 of @ref bitmap)
114 uint64_t bitmap; ///< bit i set => (highest - i) has been accepted
115 embed_bool seeded; ///< false until the first record is accepted
117
118/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
119typedef struct
120{
121 void (*keys_derive)(uint8_t *, DtlsRecordKeys *, DtlsCipher, uint16_t, const uint8_t *);
122 size_t (*plaintext_build)(const uint8_t *, uint8_t, uint16_t, uint64_t, const uint8_t *, size_t, uint8_t *, size_t);
123 size_t (*plaintext_parse)(const uint8_t *, const uint8_t *, size_t, DtlsPlaintext *);
124 size_t (*protect)(const uint8_t *, DtlsRecordKeys *, uint64_t, uint8_t, const uint8_t *, size_t, uint8_t *, size_t,
125 const uint8_t *, size_t);
126 embed_bool (*unprotect)(const uint8_t *, DtlsRecordKeys *, uint64_t, const uint8_t *, size_t, uint8_t *, size_t,
127 DtlsCiphertext *, const uint8_t *, size_t);
128 void (*replay_init)(const uint8_t *, DtlsReplayWindow *);
129 embed_bool (*replay_check)(const uint8_t *, const DtlsReplayWindow *, uint64_t);
130 void (*replay_mark)(const uint8_t *, DtlsReplayWindow *, uint64_t);
132PROTOCORE_NS_LAYOUT(DtlsRecordNs, keys_derive, plaintext_build, plaintext_parse, protect, unprotect, replay_init,
133 replay_check, replay_mark);
134
135/**
136 * @brief Derive one direction's record keys from a 32-byte TLS 1.3 traffic .
137 * @param work PROTOCORE_DTLS_RECORD_BORROW bytes the caller took. Not held past the call.
138 * @param out Out
139 * @param cipher Cipher
140 * @param epoch Epoch
141 * @param secret 32 bytes
142 */
143void protocore_dtls_record_keys_derive(uint8_t *work, DtlsRecordKeys *out, DtlsCipher cipher, uint16_t epoch,
144 const uint8_t *secret);
145/**
146 * @brief A DTLSPlaintext record; bytes written (13 + frag_len), or 0 on .
147 * @param work PROTOCORE_DTLS_RECORD_BORROW bytes the caller took. Not held past the call.
148 * @param content_type Content type
149 * @param epoch Epoch
150 * @param seq Seq
151 * @param fragment Fragment
152 * @param frag_len Frag len
153 * @param out Out
154 * @param out_cap Out cap
155 * @return The size_t.
156 */
157size_t protocore_dtls_record_plaintext_build(const uint8_t *work, uint8_t content_type, uint16_t epoch, uint64_t seq,
158 const uint8_t *fragment, size_t frag_len, uint8_t *out, size_t out_cap);
159/**
160 * @brief The same record back, validating legacy_version and the length .
161 * @param work PROTOCORE_DTLS_RECORD_BORROW bytes the caller took. Not held past the call.
162 * @param rec Rec
163 * @param rec_len Rec len
164 * @param out Out
165 * @return The size_t.
166 */
167size_t protocore_dtls_record_plaintext_parse(const uint8_t *work, const uint8_t *rec, size_t rec_len,
168 DtlsPlaintext *out);
169/**
170 * @brief Seal one record (RFC 9147 sec 4.2): the unified header, the .
171 * @param work PROTOCORE_DTLS_RECORD_BORROW bytes the caller took. Not held past the call.
172 * @param keys Keys
173 * @param seq Seq
174 * @param content_type Content type
175 * @param plaintext Plaintext
176 * @param pt_len Pt len
177 * @param out Out
178 * @param out_cap Out cap
179 * @param cid Cid
180 * @param cid_len Cid len
181 * @return The size_t.
182 */
183size_t protocore_dtls_record_protect(const uint8_t *work, DtlsRecordKeys *keys, uint64_t seq, uint8_t content_type,
184 const uint8_t *plaintext, size_t pt_len, uint8_t *out, size_t out_cap,
185 const uint8_t *cid, size_t cid_len);
186/**
187 * @brief Open one received record: decrypt the sequence number, rebuild the .
188 * @param work PROTOCORE_DTLS_RECORD_BORROW bytes the caller took. Not held past the call.
189 * @param keys Keys
190 * @param next_seq Next seq
191 * @param rec Rec
192 * @param rec_len Rec len
193 * @param out Out
194 * @param out_cap Out cap
195 * @param info Info
196 * @param expected_cid Expected cid
197 * @param expected_cid_len Expected cid len
198 * @return EMBED_TRUE on success.
199 */
200embed_bool protocore_dtls_record_unprotect(const uint8_t *work, DtlsRecordKeys *keys, uint64_t next_seq,
201 const uint8_t *rec, size_t rec_len, uint8_t *out, size_t out_cap,
202 DtlsCiphertext *info, const uint8_t *expected_cid, size_t expected_cid_len);
203/**
204 * @brief Reset a replay window to empty.
205 * @param work PROTOCORE_DTLS_RECORD_BORROW bytes the caller took. Not held past the call.
206 * @param w W
207 */
209/**
210 * @brief Whether seq is new and inside the window, rather than a replay or .
211 * @param work PROTOCORE_DTLS_RECORD_BORROW bytes the caller took. Not held past the call.
212 * @param w W
213 * @param seq Seq
214 * @return EMBED_TRUE on success.
215 */
216embed_bool protocore_dtls_record_replay_check(const uint8_t *work, const DtlsReplayWindow *w, uint64_t seq);
217/**
218 * @brief Record seq as accepted and advance the window; only after a .
219 * @param work PROTOCORE_DTLS_RECORD_BORROW bytes the caller took. Not held past the call.
220 * @param w W
221 * @param seq Seq
222 */
223void protocore_dtls_record_replay_mark(const uint8_t *work, DtlsReplayWindow *w, uint64_t seq);
224
225/** @brief Module namespace. */
234
235EMBED_END_DECLS
236
237#endif // PROTOCORE_DTLS_RECORD_H
void protocore_dtls_record_replay_mark(const uint8_t *work, DtlsReplayWindow *w, uint64_t seq)
Record seq as accepted and advance the window; only after a .
embed_bool protocore_dtls_record_replay_check(const uint8_t *work, const DtlsReplayWindow *w, uint64_t seq)
Whether seq is new and inside the window, rather than a replay or .
size_t protocore_dtls_record_protect(const uint8_t *work, DtlsRecordKeys *keys, uint64_t seq, uint8_t content_type, const uint8_t *plaintext, size_t pt_len, uint8_t *out, size_t out_cap, const uint8_t *cid, size_t cid_len)
Seal one record (RFC 9147 sec 4.2): the unified header, the .
embed_bool protocore_dtls_record_unprotect(const uint8_t *work, DtlsRecordKeys *keys, uint64_t next_seq, const uint8_t *rec, size_t rec_len, uint8_t *out, size_t out_cap, DtlsCiphertext *info, const uint8_t *expected_cid, size_t expected_cid_len)
Open one received record: decrypt the sequence number, rebuild the .
EMBED_ENUM_BYTE(DtlsCipher, DTLS_CIPHER_AES_128_GCM_SHA256=0)
Record-layer AEAD suites (phase 1: AEAD_AES_128_GCM with SHA-256).
size_t protocore_dtls_record_plaintext_build(const uint8_t *work, uint8_t content_type, uint16_t epoch, uint64_t seq, const uint8_t *fragment, size_t frag_len, uint8_t *out, size_t out_cap)
A DTLSPlaintext record; bytes written (13 + frag_len), or 0 on .
size_t protocore_dtls_record_plaintext_parse(const uint8_t *work, const uint8_t *rec, size_t rec_len, DtlsPlaintext *out)
The same record back, validating legacy_version and the length .
void protocore_dtls_record_replay_init(const uint8_t *work, DtlsReplayWindow *w)
Reset a replay window to empty.
void protocore_dtls_record_keys_derive(uint8_t *work, DtlsRecordKeys *out, DtlsCipher cipher, uint16_t epoch, const uint8_t *secret)
Derive one direction's record keys from a 32-byte TLS 1.3 traffic .
#define PROTOCORE_AES128GCM_BORROW
#define PROTOCORE_UNUSED
Suppress the unused warning. Every module ends in a namespace most callers only partly use.
Definition ns_contract.h:36
#define PROTOCORE_NS_LAYOUT(T,...)
Pin every dispatch slot of a table that is nothing but function pointers.
#define PROTOCORE_NS
Storage for a dispatch table. The const is load bearing.
Result of a successful protocore_dtls_ciphertext_unprotect.
uint64_t seq
reconstructed full sequence number
size_t pt_len
plaintext bytes written to out
uint16_t epoch
epoch of keys (its low 2 bits matched the header)
uint8_t content_type
recovered inner content type (last non-zero byte of the inner plaintext)
Parsed view of a DTLSPlaintext record (fields point into the caller's buffer).
Definition dtls_record.h:93
uint64_t seq
48-bit record sequence number
Definition dtls_record.h:96
uint8_t content_type
Definition dtls_record.h:94
uint16_t epoch
Definition dtls_record.h:95
size_t frag_len
Definition dtls_record.h:98
const uint8_t * fragment
into the input buffer
Definition dtls_record.h:97
One direction's record-protection keys for one epoch (RFC 9147 §4).
Definition dtls_record.h:81
DtlsCipher cipher
negotiated AEAD (phase 1: AES-128-GCM)
Definition dtls_record.h:82
uint16_t epoch
this epoch number; its low 2 bits appear in the unified header
Definition dtls_record.h:83
uint8_t iv[12]
AEAD write IV (per-record nonce = iv XOR sequence_number).
Definition dtls_record.h:88
uint8_t gcm[PROTOCORE_AES128GCM_BORROW]
Definition dtls_record.h:84
Dispatch table. Addressed by offset, so the layout is asserted below.
void(* replay_init)(const uint8_t *, DtlsReplayWindow *)
size_t(* plaintext_parse)(const uint8_t *, const uint8_t *, size_t, DtlsPlaintext *)
void(* keys_derive)(uint8_t *, DtlsRecordKeys *, DtlsCipher, uint16_t, const uint8_t *)
size_t(* plaintext_build)(const uint8_t *, uint8_t, uint16_t, uint64_t, const uint8_t *, size_t, uint8_t *, size_t)
embed_bool(* unprotect)(const uint8_t *, DtlsRecordKeys *, uint64_t, const uint8_t *, size_t, uint8_t *, size_t, DtlsCiphertext *, const uint8_t *, size_t)
embed_bool(* replay_check)(const uint8_t *, const DtlsReplayWindow *, uint64_t)
void(* replay_mark)(const uint8_t *, DtlsReplayWindow *, uint64_t)
size_t(* protect)(const uint8_t *, DtlsRecordKeys *, uint64_t, uint8_t, const uint8_t *, size_t, uint8_t *, size_t, const uint8_t *, size_t)
64-record sliding replay window over the highest sequence number accepted in an epoch.
uint64_t highest
highest accepted sequence number (bit 0 of bitmap)
uint64_t bitmap
bit i set => (highest - i) has been accepted
embed_bool seeded
false until the first record is accepted