ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
ikev2.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/**
5 * @file ikev2.h
6 * @brief IKEv2 (RFC 7296): the message and payload codec, the key schedule, and the handshake driver.
7 *
8 * RFC 7296 sec 3.1: a message begins with the 28-octet IKE header - IKE SA Initiator's SPI, IKE SA
9 * Responder's SPI, Next Payload, MjVer/MnVer, Exchange Type, Flags, Message ID, Length - and every
10 * multi-octet field is big endian. RFC 7296 sec 3.2: every payload begins with the generic payload
11 * header - Next Payload, the Critical bit, RESERVED, Payload Length - so the chain is walked forward
12 * from the header's Next Payload until a Next Payload of zero.
13 *
14 * The codec frames the Security Association payload (sec 3.3) with its Proposal (sec 3.3.1) and
15 * Transform (sec 3.3.2) substructures and the Key Length attribute (sec 3.3.5), Key Exchange
16 * (sec 3.4), Identification (sec 3.5), Certificate and Certificate Request (sec 3.6, 3.7),
17 * Authentication (sec 3.8), Nonce (sec 3.9), Notify (sec 3.10), Delete (sec 3.11), Traffic Selector
18 * (sec 3.13), Encrypted (sec 3.14), Configuration (sec 3.15), and the Encrypted Fragment payload of
19 * RFC 7383 sec 2.5.
20 *
21 * On the codec sits the crypto: prf+ (sec 2.13), the SKEYSEED / SK_* schedule (sec 2.14), the IKE SA
22 * rekey schedule (sec 2.18), Child SA KEYMAT (sec 2.17), the authenticated encryption that protects
23 * the Encrypted payload (RFC 5282 sec 3, 4 and 5.1: AES-GCM with a 16-octet ICV, ENCR transform id 20
24 * per RFC 5282 sec 7.2), the Curve25519 key exchange (Diffie-Hellman Group Num 31, RFC 8031 sec 3),
25 * pre-shared key and digital-signature authentication (sec 2.15, RFC 7427 sec 3), and the stateless
26 * COOKIE (sec 2.6).
27 *
28 * On the crypto sits the handshake driver: both roles run IKE_SA_INIT then IKE_AUTH (sec 1.2) to
29 * IKE_ST_ESTABLISHED with mutual pre-shared key authentication, then INFORMATIONAL (sec 1.4) and
30 * CREATE_CHILD_SA (sec 1.3) exchanges over the established SA.
31 *
32 * The PRF is HMAC-SHA2-256, PRF transform id 5 (RFC 4868 sec 4), whose preferred key length fixes
33 * SK_d, SK_pi and SK_pr (sec 2.13). The integrity transform, when one is negotiated, is
34 * AUTH_HMAC_SHA2_256_128, id 12 (RFC 4868 sec 4), keyed with the 32-octet hash output
35 * (RFC 4868 sec 2.1.1). An AEAD cipher carries its own integrity, so SK_ai and SK_ar are then zero
36 * octets (RFC 5282 sec 7.1).
37 *
38 * The module exports one symbol, @ref Ike. Everything in ikev2.c has internal linkage. A caller sets
39 * the members a call takes, invokes it through ::Ike, and reads the outcome off the same handle.
40 *
41 * @author Douglas Quigg (dstroy0)
42 * @date 2026
43 */
44
45#ifndef PROTOCORE_IKEV2_H
46#define PROTOCORE_IKEV2_H
47
48#include "protocore_config.h" // the entry point: protocore_types.h for the widths
49
50#if PROTOCORE_ENABLE_IKEV2
51
52EMBED_BEGIN_DECLS
53
54// ---------------------------------------------------------------------------
55// Literals
56// ---------------------------------------------------------------------------
57
58/** @brief UDP port IKE runs on (RFC 7296 sec 2.23). */
59#define PROTOCORE_IKEV2_PORT 500
60/** @brief UDP port reserved for UDP-encapsulated ESP and IKE (RFC 7296 sec 2.23, RFC 3948 sec 2.2). */
61#define PROTOCORE_IKEV2_NAT_PORT 4500
62/** @brief IKE header size (RFC 7296 sec 3.1). */
63#define PROTOCORE_IKE_HDR_LEN 28
64/** @brief IKE SA Initiator's / Responder's SPI size (RFC 7296 sec 3.1). */
65#define PROTOCORE_IKE_SPI_LEN 8
66/** @brief Generic payload header size: Next Payload, C bit + RESERVED, Payload Length (RFC 7296 sec 3.2). */
67#define PROTOCORE_IKE_PAYLOAD_HDR_LEN 4
68/** @brief MjVer 2, MnVer 0 in the version octet (RFC 7296 sec 3.1). */
69#define PROTOCORE_IKE_VERSION 0x20
70/** @brief The Critical bit in a payload's second header octet (RFC 7296 sec 3.2). */
71#define PROTOCORE_IKE_CRITICAL 0x80
72
73/** @brief IKE header Flags octet, bit layout X|X|R|V|I|X|X|X (RFC 7296 sec 3.1). */
74#define PROTOCORE_IKE_FLAG_INITIATOR 0x08 ///< I: sent by the original initiator of the IKE SA
75#define PROTOCORE_IKE_FLAG_VERSION 0x10 ///< V: a higher major version is supported
76#define PROTOCORE_IKE_FLAG_RESPONSE 0x20 ///< R: a response to the message with the same Message ID
77
78/** @brief Transform attribute type Key Length, in bits, TV form (RFC 7296 sec 3.3.5). */
79#define IKE_ATTR_KEY_LENGTH 14
80
81/** @brief Transform IDs this build names; any 16-bit id is accepted on the wire. */
82#define IKE_ENCR_AES_CBC 12 ///< ENCR_AES_CBC
83#define IKE_ENCR_AES_GCM_16 20 ///< AES-GCM with a 16-octet ICV (RFC 5282 sec 7.2)
84#define IKE_ENCR_CHACHA20_POLY1305 28 ///< ENCR_CHACHA20_POLY1305
85#define IKE_PRF_HMAC_SHA2_256 5 ///< PRF_HMAC_SHA2_256 (RFC 4868 sec 4)
86#define IKE_INTEG_HMAC_SHA2_256_128 12 ///< AUTH_HMAC_SHA2_256_128 (RFC 4868 sec 4)
87#define IKE_DH_MODP2048 14 ///< 2048-bit MODP group (RFC 7296 app. B)
88#define IKE_DH_ECP256 19 ///< 256-bit random ECP group
89#define IKE_DH_CURVE25519 31 ///< Curve25519 (RFC 8031 sec 3)
90
91// Configuration Attribute types (RFC 7296 sec 3.15.1).
92#define PROTOCORE_IKE_CFG_INTERNAL_IP4_ADDRESS 1
93#define PROTOCORE_IKE_CFG_INTERNAL_IP4_NETMASK 2
94#define PROTOCORE_IKE_CFG_INTERNAL_IP4_DNS 3
95#define PROTOCORE_IKE_CFG_INTERNAL_IP4_NBNS 4
96#define PROTOCORE_IKE_CFG_INTERNAL_IP4_DHCP 6
97#define PROTOCORE_IKE_CFG_APPLICATION_VERSION 7
98#define PROTOCORE_IKE_CFG_INTERNAL_IP6_ADDRESS 8
99#define PROTOCORE_IKE_CFG_INTERNAL_IP6_DNS 10
100#define PROTOCORE_IKE_CFG_INTERNAL_IP6_DHCP 12
101#define PROTOCORE_IKE_CFG_INTERNAL_IP4_SUBNET 13
102#define PROTOCORE_IKE_CFG_INTERNAL_IP6_SUBNET 15
103
104/** @brief IKEV2_FRAGMENTATION_SUPPORTED Notify Message Type, no data (RFC 7383 sec 2.3, sec 6). */
105#define PROTOCORE_IKE_N_FRAGMENTATION_SUPPORTED 16430
106/** @brief Largest Total Fragments this reassembler tracks (RFC 7383 sec 2.5). */
107#define PROTOCORE_IKE_FRAG_MAX 32
108
109/** @brief COOKIE Notify Message Type (RFC 7296 sec 3.10.1). */
110#define PROTOCORE_IKE_N_COOKIE 16390
111/** @brief Cookie length here: the VersionIDofSecret octet plus a SHA-256 hash (RFC 7296 sec 2.6). */
112#define PROTOCORE_IKE_COOKIE_LEN 33
113
114/** @brief PRF_HMAC_SHA2_256 output and preferred key length, in octets (RFC 4868 sec 2.1.2). */
115#define PROTOCORE_IKE_PRF_LEN 32
116/** @brief Largest single SK_* key stored: a 32-octet cipher key plus a 4-octet salt, with margin. */
117#define PROTOCORE_IKE_SK_MAX 40
118/** @brief Largest nonce stored; a nonce is at least 128 bits (RFC 7296 sec 2.10). */
119#define PROTOCORE_IKE_NONCE_MAX 256
120
121/** @brief AES-256 cipher key length inside SK_ei / SK_er, salt excluded (RFC 5282 sec 7.1). */
122#define PROTOCORE_IKE_AEAD_KEY_LEN 32
123/** @brief Salt: the 4-octet tail of SK_ei / SK_er, not sent on the wire (RFC 5282 sec 4, sec 7.1). */
124#define PROTOCORE_IKE_GCM_SALT_LEN 4
125/** @brief Initialization Vector carried in the Encrypted payload (RFC 5282 sec 3.1). */
126#define PROTOCORE_IKE_GCM_IV_LEN 8
127/** @brief Integrity Check Value length: the full AES-GCM Authentication Tag (RFC 5282 sec 3.2). */
128#define PROTOCORE_IKE_AEAD_ICV_LEN 16
129
130/** @brief Octets the SK envelope adds around the inner payloads: generic header, IV, Pad Length, ICV. */
131#define PROTOCORE_IKE_SK_OVERHEAD \
132 (PROTOCORE_IKE_PAYLOAD_HDR_LEN + PROTOCORE_IKE_GCM_IV_LEN + 1 + PROTOCORE_IKE_AEAD_ICV_LEN)
133
134/** @brief AUTH payload Authentication Data length for a PRF_HMAC_SHA2_256 MAC (RFC 7296 sec 2.15). */
135#define PROTOCORE_IKE_AUTH_LEN 32
136/** @brief The pre-shared key pad string: 17 ASCII characters, no null termination (RFC 7296 sec 2.15). */
137#define PROTOCORE_IKE_PSK_PAD "Key Pad for IKEv2"
138
139/** @brief Curve25519 private, public and shared-secret length (RFC 8031 sec 2, sec 3.1). */
140#define PROTOCORE_IKE_X25519_LEN 32
141
142/** @brief P-256 public point length, uncompressed (0x04 | X | Y). */
143#define PROTOCORE_IKE_ECDSA_P256_PUB_LEN 65
144/** @brief P-256 private scalar length. */
145#define PROTOCORE_IKE_ECDSA_P256_PRIV_LEN 32
146/** @brief ECDSA-P256 signature length, r | s. */
147#define PROTOCORE_IKE_ECDSA_P256_SIG_LEN 64
148
149/** @brief Largest message the handshake stores as RealMessage1 / RealMessage2 (RFC 7296 sec 2.15). */
150#define PROTOCORE_IKE_MSG_MAX 640
151
152// ---------------------------------------------------------------------------
153// Typedefs
154// ---------------------------------------------------------------------------
155
156/** @brief Exchange Type (RFC 7296 sec 3.1). */
157EMBED_ENUM_BYTE(IkeExchange, IKE_SA_INIT = 34, IKE_AUTH = 35, IKE_CREATE_CHILD_SA = 36, IKE_INFORMATIONAL = 37, );
158
159/** @brief Next Payload / payload types; zero ends the chain (RFC 7296 sec 3.2). */
160EMBED_ENUM_BYTE(IkePayloadType, IKE_PL_NONE = 0,
161 IKE_PL_SA = 33, ///< Security Association, SA
162 IKE_PL_KE = 34, ///< Key Exchange, KE
163 IKE_PL_IDI = 35, ///< Identification - Initiator, IDi
164 IKE_PL_IDR = 36, ///< Identification - Responder, IDr
165 IKE_PL_CERT = 37, ///< Certificate, CERT
166 IKE_PL_CERTREQ = 38, ///< Certificate Request, CERTREQ
167 IKE_PL_AUTH = 39, ///< Authentication, AUTH
168 IKE_PL_NONCE = 40, ///< Nonce, Ni or Nr
169 IKE_PL_NOTIFY = 41, ///< Notify, N
170 IKE_PL_DELETE = 42, ///< Delete, D
171 IKE_PL_VENDOR = 43, ///< Vendor ID, V
172 IKE_PL_TSI = 44, ///< Traffic Selector - Initiator, TSi
173 IKE_PL_TSR = 45, ///< Traffic Selector - Responder, TSr
174 IKE_PL_SK = 46, ///< Encrypted and Authenticated, SK
175 IKE_PL_CP = 47, ///< Configuration, CP
176 IKE_PL_EAP = 48, ///< Extensible Authentication, EAP
177 IKE_PL_SKF = 53, ///< Encrypted and Authenticated Fragment, SKF (RFC 7383 sec 2.5)
178);
179
180/** @brief Transform Type (RFC 7296 sec 3.3.2). */
181EMBED_ENUM_BYTE(IkeTransformType,
182 IKE_TRANSFORM_ENCR = 1, ///< Encryption Algorithm
183 IKE_TRANSFORM_PRF = 2, ///< Pseudorandom Function
184 IKE_TRANSFORM_INTEG = 3, ///< Integrity Algorithm
185 IKE_TRANSFORM_DH = 4, ///< Diffie-Hellman Group
186 IKE_TRANSFORM_ESN = 5, ///< Extended Sequence Numbers
187);
188
189/** @brief Protocol ID (RFC 7296 sec 3.3.1, sec 3.10, sec 3.11). */
190EMBED_ENUM_BYTE(IkeProtocol,
191 IKE_PROTO_NONE = 0, ///< the notification concerns no existing SA and the SPI field is empty
192 IKE_PROTO_IKE = 1, IKE_PROTO_AH = 2, IKE_PROTO_ESP = 3, );
193
194/** @brief ID Type (RFC 7296 sec 3.5). */
195EMBED_ENUM_BYTE(IkeIdType,
196 IKE_ID_RESERVED = 0, ///< reserved; the value an out member holds before a parse succeeds
197 IKE_ID_IPV4_ADDR = 1, IKE_ID_FQDN = 2, IKE_ID_RFC822_ADDR = 3, IKE_ID_IPV6_ADDR = 5,
198 IKE_ID_KEY_ID = 11, );
199
200/** @brief Auth Method (RFC 7296 sec 3.8). */
201EMBED_ENUM_BYTE(IkeAuthMethod,
202 IKE_AUTH_RESERVED = 0, ///< reserved; the value an out member holds before a parse succeeds
203 IKE_AUTH_RSA_SIG = 1, ///< RSA Digital Signature
204 IKE_AUTH_PSK = 2, ///< Shared Key Message Integrity Code
205 IKE_AUTH_DSS_SIG = 3, ///< DSS Digital Signature
206 IKE_AUTH_DIGITAL_SIG = 14 ///< Digital Signature (RFC 7427 sec 3)
207);
208
209/** @brief TS Type (RFC 7296 sec 3.13.1). */
210EMBED_ENUM_BYTE(IkeTsType, IKE_TS_IPV4_ADDR_RANGE = 7, IKE_TS_IPV6_ADDR_RANGE = 8, );
211
212/** @brief CFG Type (RFC 7296 sec 3.15.1). */
213EMBED_ENUM_BYTE(IkeCfgType,
214 IKE_CFG_REQUEST = 1, ///< CFG_REQUEST
215 IKE_CFG_REPLY = 2, ///< CFG_REPLY
216 IKE_CFG_SET = 3, ///< CFG_SET
217 IKE_CFG_ACK = 4, ///< CFG_ACK
218);
219
220/** @brief Handshake progress across IKE_SA_INIT and IKE_AUTH (RFC 7296 sec 1.2). */
221EMBED_ENUM_BYTE(IkeState,
222 IKE_ST_INIT = 0, ///< nothing sent yet
223 IKE_ST_SA_INIT_SENT, ///< IKE_SA_INIT emitted, awaiting the response
224 IKE_ST_SA_INIT_DONE, ///< the peer's IKE_SA_INIT consumed and the SK_* keys derived
225 IKE_ST_AUTH_SENT, ///< IKE_AUTH request emitted, awaiting the response
226 IKE_ST_ESTABLISHED, ///< the peer's AUTH verified; the IKE SA is up
227 IKE_ST_FAILED, ///< a received message was rejected
228);
229
230/** @brief The IKE header, decoded or to be encoded (RFC 7296 sec 3.1). */
231typedef struct
232{
233 uint8_t init_spi[PROTOCORE_IKE_SPI_LEN]; ///< IKE SA Initiator's SPI
234 uint8_t resp_spi[PROTOCORE_IKE_SPI_LEN]; ///< IKE SA Responder's SPI
235 IkePayloadType next_payload; ///< type of the first payload in the message
236 uint8_t version; ///< MjVer | MnVer, 0x20 for IKEv2
237 IkeExchange exchange; ///< Exchange Type
238 uint8_t flags; ///< OR of PROTOCORE_IKE_FLAG_*
239 uint32_t message_id; ///< Message ID
240 uint32_t length; ///< Length of the whole message
241} IkeHeader;
242
243/** @brief One payload off the chain: its type, the following type, and its body (RFC 7296 sec 3.2). */
244typedef struct
245{
246 IkePayloadType type; ///< this payload's type, taken from the chain
247 IkePayloadType next_payload; ///< Next Payload; IKE_PL_NONE ends the chain
248 embed_bool critical; ///< the Critical bit
249 const uint8_t *body; ///< the octets after the generic payload header
250 size_t body_len;
251} IkePayload;
252
253/** @brief Forward walk of a message's payload chain (RFC 7296 sec 3.2). */
254typedef struct
255{
256 const uint8_t *area; ///< the payload area: the message plus PROTOCORE_IKE_HDR_LEN
257 size_t len; ///< octets in that area
258 size_t off; ///< current offset into @c area
259 IkePayloadType next_type; ///< type of the payload at @c off; IKE_PL_NONE is done
260} IkePayloadIter;
261
262/** @brief One Transform Substructure to encode (RFC 7296 sec 3.3.2). */
263typedef struct
264{
265 IkeTransformType type; ///< Transform Type
266 uint16_t id; ///< Transform ID
267 int32_t key_length; ///< Key Length attribute in bits (sec 3.3.5), or below zero for none
268} IkeTransform;
269
270/** @brief One decoded Transform Substructure (RFC 7296 sec 3.3.2). */
271typedef struct
272{
273 IkeTransformType type; ///< Transform Type
274 uint16_t id; ///< Transform ID
275 int32_t key_length; ///< decoded Key Length attribute, or below zero when absent
276 embed_bool last; ///< Last Substruc was 0: no transform follows
277} IkeTransformRef;
278
279/** @brief One decoded Proposal Substructure (RFC 7296 sec 3.3.1). */
280typedef struct
281{
282 uint8_t proposal_num; ///< Proposal Num
283 IkeProtocol protocol_id; ///< Protocol ID
284 uint8_t spi_size; ///< SPI Size
285 uint8_t num_transforms; ///< Num Transforms
286 const uint8_t *spi; ///< SPI, @c spi_size octets, or nullptr when SPI Size is zero
287 const uint8_t *transforms;
288 size_t transforms_len;
289 embed_bool last; ///< Last Substruc was 0: no proposal follows
290} IkeProposalRef;
291
292/** @brief Walk of the Transform Substructures inside one proposal (RFC 7296 sec 3.3.2). */
293typedef struct
294{
295 const uint8_t *area;
296 size_t len;
297 size_t off;
298} IkeTransformIter;
299
300/** @brief One Traffic Selector (RFC 7296 sec 3.13.1). */
301typedef struct
302{
303 IkeTsType ts_type; ///< TS Type
304 uint8_t ip_protocol; ///< IP Protocol ID; zero means any
305 uint16_t start_port; ///< Start Port
306 uint16_t end_port; ///< End Port
307 const uint8_t *start_addr; ///< Starting Address, 4 or 16 octets
308 const uint8_t *end_addr; ///< Ending Address, the same length
309 size_t addr_len; ///< 4 or 16
310} IkeTrafficSelector;
311
312/** @brief One Configuration Attribute: a 15-bit type and its value (RFC 7296 sec 3.15.1). */
313typedef struct
314{
315 uint16_t type; ///< Attribute Type, the reserved high bit masked off
316 const uint8_t *value; ///< Value, or nullptr when @c value_len is zero
317 uint16_t value_len; ///< Length
318} IkeCfgAttr;
319
320/** @brief Walk of a Configuration payload's attribute area (RFC 7296 sec 3.15.1). */
321typedef struct
322{
323 const uint8_t *area;
324 size_t len;
325 size_t off;
326} IkeCfgAttrIter;
327
328/**
329 * @brief Reassembly of one fragmented message (RFC 7383 sec 2.6).
330 *
331 * Decrypted Encrypted Fragment contents are staged into a caller-owned pool in arrival order and
332 * merged 1..Total once every fragment is present.
333 */
334typedef struct
335{
336 uint16_t total; ///< Total Fragments; zero until the first fragment sets it
337 uint16_t count; ///< distinct fragments stored
338 embed_bool present[PROTOCORE_IKE_FRAG_MAX]; ///< present[i] holds fragment (i+1)
339 size_t off[PROTOCORE_IKE_FRAG_MAX]; ///< pool offset of fragment (i+1)
340 size_t len[PROTOCORE_IKE_FRAG_MAX]; ///< length of fragment (i+1)
341 uint8_t *pool; ///< caller-owned staging buffer
342 size_t pool_cap;
343 size_t pool_used;
344} IkeFragReasm;
345
346/** @brief Per-key lengths of the SK_* chain, in octets (RFC 7296 sec 2.13, sec 2.14). */
347typedef struct
348{
349 size_t sk_d; ///< SK_d: the PRF's preferred key length
350 size_t sk_a; ///< SK_ai / SK_ar: the integrity key length, zero for an AEAD cipher
351 size_t sk_e; ///< SK_ei / SK_er: the cipher key plus any AEAD salt
352 size_t sk_p; ///< SK_pi / SK_pr: the PRF's preferred key length
353} IkeKeyLengths;
354
355/** @brief The seven keys taken in order from prf+ (RFC 7296 sec 2.14). */
356typedef struct
357{
358 uint8_t sk_d[PROTOCORE_IKE_SK_MAX];
359 uint8_t sk_ai[PROTOCORE_IKE_SK_MAX];
360 uint8_t sk_ar[PROTOCORE_IKE_SK_MAX];
361 uint8_t sk_ei[PROTOCORE_IKE_SK_MAX];
362 uint8_t sk_er[PROTOCORE_IKE_SK_MAX];
363 uint8_t sk_pi[PROTOCORE_IKE_SK_MAX];
364 uint8_t sk_pr[PROTOCORE_IKE_SK_MAX];
365 size_t sk_d_len; ///< valid octets in sk_d
366 size_t sk_a_len; ///< valid octets in sk_ai / sk_ar
367 size_t sk_e_len; ///< valid octets in sk_ei / sk_er
368 size_t sk_p_len; ///< valid octets in sk_pi / sk_pr
369} IkeKeyMaterial;
370
371/** @brief The four transforms negotiated for an IKE SA (RFC 7296 sec 2.13). */
372typedef struct
373{
374 uint16_t encr; ///< Encryption Algorithm transform id
375 int32_t encr_keylen; ///< Key Length attribute in bits, or below zero for a fixed-length key
376 uint16_t prf; ///< Pseudorandom Function transform id
377 uint16_t integ; ///< Integrity Algorithm transform id, zero for an AEAD cipher
378 uint16_t dh; ///< Diffie-Hellman Group Num
379} IkeSuite;
380
381/** @brief One IKE SA after IKE_SA_INIT: the SPIs, the negotiated suite, and the SK_* keys. */
382typedef struct
383{
384 uint8_t init_spi[PROTOCORE_IKE_SPI_LEN]; ///< IKE SA Initiator's SPI
385 uint8_t resp_spi[PROTOCORE_IKE_SPI_LEN]; ///< IKE SA Responder's SPI
386 embed_bool is_initiator; ///< this side is the original initiator
387 IkeSuite suite; ///< the negotiated transforms
388 IkeKeyMaterial keys; ///< SK_d, SK_ai, SK_ar, SK_ei, SK_er, SK_pi, SK_pr
389 // The cookie hash, the prf+ chain, the AUTH MAC and the signature run in sequence, so one region
390 // sized for the largest serves them all.
391 uint8_t work[PROTOCORE_IKE_BORROW];
392} IkeSa;
393
394/** @brief The salient contents of a parsed IKE_SA_INIT; slices point into the message (sec 1.2). */
395typedef struct
396{
397 uint8_t init_spi[PROTOCORE_IKE_SPI_LEN];
398 uint8_t resp_spi[PROTOCORE_IKE_SPI_LEN];
399 embed_bool is_response; ///< the R flag was set
400 IkeProposalRef proposal; ///< the first proposal of SAi1 or SAr1
401 uint16_t dh_group; ///< Diffie-Hellman Group Num from the KE payload
402 const uint8_t *ke_data; ///< Key Exchange Data
403 size_t ke_len;
404 const uint8_t *nonce; ///< Ni or Nr data
405 size_t nonce_len;
406} IkeSaInitMsg;
407
408/** @brief Handshake context: the SA under construction and what the next step signs over. */
409typedef struct
410{
411 IkeSa sa; ///< the SA being established
412 IkeState state; ///< @ref IkeState
413 uint8_t our_dh_priv[PROTOCORE_IKE_X25519_LEN]; ///< our ephemeral private, to compute g^ir
414 uint8_t our_nonce[PROTOCORE_IKE_NONCE_MAX]; ///< the nonce this side sent
415 uint16_t our_nonce_len;
416 uint8_t peer_nonce[PROTOCORE_IKE_NONCE_MAX]; ///< the nonce the peer sent
417 uint16_t peer_nonce_len;
418 uint8_t init_msg[PROTOCORE_IKE_MSG_MAX]; ///< RealMessage1: the IKE_SA_INIT request (sec 2.15)
419 uint16_t init_msg_len;
420 uint8_t resp_msg[PROTOCORE_IKE_MSG_MAX]; ///< RealMessage2: the IKE_SA_INIT response (sec 2.15)
421 uint16_t resp_msg_len;
422} IkeHandshake;
423
424/** @brief A decoded Key Exchange payload (RFC 7296 sec 3.4). */
425typedef struct
426{
427 uint16_t dh_group; ///< Diffie-Hellman Group Num
428 const uint8_t *ke_data; ///< Key Exchange Data
429 size_t ke_len;
430} IkeKeRef;
431
432/** @brief A decoded Identification payload (RFC 7296 sec 3.5). */
433typedef struct
434{
435 IkeIdType id_type; ///< ID Type
436 const uint8_t *id_data; ///< Identification Data
437 size_t id_len;
438} IkeIdRef;
439
440/** @brief A decoded Authentication payload (RFC 7296 sec 3.8). */
441typedef struct
442{
443 IkeAuthMethod auth_method; ///< Auth Method
444 const uint8_t *auth_data; ///< Authentication Data
445 size_t auth_len;
446} IkeAuthRef;
447
448/** @brief A decoded Notify payload (RFC 7296 sec 3.10). */
449typedef struct
450{
451 IkeProtocol protocol_id; ///< Protocol ID
452 uint8_t spi_size; ///< SPI Size
453 uint16_t notify_type; ///< Notify Message Type
454 const uint8_t *spi; ///< SPI, or nullptr when SPI Size is zero
455 const uint8_t *data; ///< Notification Data
456 size_t data_len;
457} IkeNotifyRef;
458
459/** @brief A decoded Delete payload (RFC 7296 sec 3.11). */
460typedef struct
461{
462 IkeProtocol protocol_id; ///< Protocol ID
463 uint8_t spi_size; ///< SPI Size
464 uint16_t num_spis; ///< Num of SPIs
465 const uint8_t *spis; ///< the SPI list, or nullptr when it is empty
466} IkeDeleteRef;
467
468/** @brief A sliced Encrypted or Encrypted Fragment payload body (RFC 7296 sec 3.14, RFC 7383 sec 2.5). */
469typedef struct
470{
471 uint16_t frag_num; ///< Fragment Number, zero for an unfragmented Encrypted payload
472 uint16_t total; ///< Total Fragments, zero for an unfragmented Encrypted payload
473 const uint8_t *iv; ///< Initialization Vector
474 const uint8_t *ct; ///< Ciphertext
475 size_t ct_len;
476 const uint8_t *icv; ///< Integrity Checksum Data
477} IkeSkRef;
478
479/** @brief A decoded Configuration payload (RFC 7296 sec 3.15). */
480typedef struct
481{
482 IkeCfgType cfg_type; ///< CFG Type
483 const uint8_t *attrs; ///< the Configuration Attribute area
484 size_t attrs_len;
485} IkeCpRef;
486
487/** @brief The inner payload chain an Encrypted payload was carrying (RFC 7296 sec 3.14). */
488typedef struct
489{
490 IkePayloadType first_inner_type; ///< the Encrypted payload's Next Payload
491 const uint8_t *inner; ///< the decrypted chain, inside the caller's message buffer
492 size_t inner_len;
493} IkeInnerRef;
494
495/** @brief Where a build, a hash or a signature writes. */
496typedef struct
497{
498 uint8_t *buf; ///< the octets a call writes
499 size_t cap; ///< room there; prf+ and child_keymat fill it exactly
500} IkeOutArgs;
501
502/** @brief The octets a parse reads: a whole message, or one payload body (RFC 7296 sec 3.2). */
503typedef struct
504{
505 const uint8_t *msg; ///< a message, a payload body, or an attribute area
506 size_t len;
507} IkeWireArgs;
508
509/** @brief The generic payload header a build writes, and the payload's own variable field (sec 3.2). */
510typedef struct
511{
512 IkePayloadType next_payload; ///< Next Payload: the type of the payload that follows this one
513 embed_bool critical; ///< the Critical bit
514 const uint8_t *data; ///< this payload's variable field, named per payload by each call
515 size_t data_len;
516} IkePayloadArgs;
517
518/** @brief The Proposal Substructure a build encodes, and the SPIs a Notify or Delete names (sec 3.3.1). */
519typedef struct
520{
521 uint8_t proposal_num; ///< Proposal Num
522 IkeProtocol protocol_id; ///< Protocol ID
523 const uint8_t *spi; ///< SPI
524 uint8_t spi_size; ///< SPI Size
525 uint16_t num_spis; ///< Num of SPIs in a Delete payload (sec 3.11)
526 const IkeTransform *transforms; ///< the Transform Substructures to encode (sec 3.3.2)
527 uint8_t num_transforms; ///< Num Transforms
528} IkeProposalArgs;
529
530/** @brief The key exchange: the group and the values (RFC 7296 sec 3.4, RFC 8031 sec 3). */
531typedef struct
532{
533 uint16_t dh_group; ///< Diffie-Hellman Group Num
534 const uint8_t *our_priv; ///< our ephemeral private value
535 size_t our_priv_len; ///< its length; 32 for group 31
536 const uint8_t *our_pub; ///< our Key Exchange Data
537 size_t our_pub_len; ///< its length; 32 for group 31
538 const uint8_t *peer_pub; ///< the peer's Key Exchange Data
539 size_t peer_pub_len; ///< its length
540} IkeKeArgs;
541
542/** @brief The identity: the ID payload's fields and its signed remainder (sec 3.5, 3.6, 2.15). */
543typedef struct
544{
545 IkeIdType id_type; ///< ID Type
546 const uint8_t *id_body; ///< RestOfInitIDPayload / RestOfRespIDPayload: the ID payload body
547 size_t id_body_len;
548 uint8_t cert_encoding; ///< Cert Encoding for a CERT or CERTREQ payload (sec 3.6)
549} IkeIdArgs;
550
551/** @brief Authentication: the method and every input the AUTH value is computed from (sec 2.15). */
552typedef struct
553{
554 IkeAuthMethod auth_method; ///< Auth Method (sec 3.8)
555 const uint8_t *psk; ///< the Shared Secret
556 size_t psk_len;
557 const uint8_t *real_msg; ///< RealMessage1 when the initiator signs, RealMessage2 when the responder does
558 size_t real_len;
559 const uint8_t *peer_nonce; ///< NonceRData when the initiator signs, NonceIData when the responder does
560 size_t peer_nonce_len;
561 const uint8_t *sk_p; ///< SK_pi when the initiator signs, SK_pr when the responder does
562 size_t sk_p_len;
563 uint8_t *scratch; ///< where the signed octets are assembled
564 size_t scratch_cap; ///< room there: real_len + peer_nonce_len + PROTOCORE_IKE_AUTH_LEN
565 const uint8_t *sig; ///< the signature a verify judges
566 size_t sig_len;
567 const uint8_t *priv; ///< the P-256 private scalar a sign uses
568 const uint8_t *pub; ///< the peer's P-256 public point
569 const uint8_t *rsa_n; ///< the peer's RSA modulus, 256 octets big endian
570 const uint8_t *rsa_e; ///< the peer's RSA exponent, 4 octets big endian
571} IkeAuthArgs;
572
573/** @brief The Notify payload's own fields and the COOKIE it carries (sec 3.10, sec 2.6). */
574typedef struct
575{
576 uint16_t notify_type; ///< Notify Message Type
577 uint8_t version; ///< VersionIDofSecret tagging which secret a cookie was made with
578 const uint8_t *secret; ///< the responder's current secret
579 size_t secret_len;
580 const uint8_t *ni; ///< Ni: the initiator's nonce data
581 size_t ni_len;
582 const uint8_t *ipi; ///< IPi: the initiator's source address octets
583 size_t ipi_len;
584 const uint8_t *spii; ///< SPIi: the initiator's SPI
585 const uint8_t *cookie; ///< the cookie a verify judges or a build carries
586 size_t cookie_len;
587} IkeNotifyArgs;
588
589/** @brief The Traffic Selectors a build encodes and the one a get names (RFC 7296 sec 3.13). */
590typedef struct
591{
592 const IkeTrafficSelector *sels; ///< the selectors to encode
593 uint8_t num; ///< Number of TSs
594 uint8_t index; ///< which selector a get decodes, zero based
595} IkeTsArgs;
596
597/** @brief The Configuration payload a build encodes (RFC 7296 sec 3.15). */
598typedef struct
599{
600 IkeCfgType cfg_type; ///< CFG Type
601 const IkeCfgAttr *attrs; ///< the Configuration Attributes to encode
602 uint8_t num_attrs; ///< how many
603} IkeCpArgs;
604
605/** @brief The Encrypted payload and its AEAD inputs (RFC 7296 sec 3.14, RFC 5282 sec 3, 4, 5.1). */
606typedef struct
607{
608 uint8_t *msg; ///< an SK-framed message an open verifies and decrypts in place
609 size_t msg_len; ///< octets received in it
610 const uint8_t *key; ///< the cipher key: SK_ei or SK_er, salt excluded (RFC 5282 sec 7.1)
611 const uint8_t *salt; ///< the implicit nonce half (RFC 5282 sec 4)
612 const uint8_t *iv; ///< Initialization Vector, the explicit nonce half
613 size_t iv_len; ///< its length as the negotiated transform defines it
614 const uint8_t *ct; ///< Ciphertext
615 size_t ct_len;
616 const uint8_t *icv; ///< Integrity Checksum Data
617 size_t icv_len; ///< its length as the negotiated transform defines it
618 const uint8_t *aad; ///< associated data: the header through the Encrypted payload's own header
619 size_t aad_len;
620 const uint8_t *pt; ///< the plaintext a seal encrypts
621 size_t pt_len;
622} IkeSkArgs;
623
624/** @brief The Encrypted Fragment counters and the chunk a reassembler stages (RFC 7383 sec 2.5, 2.6). */
625typedef struct
626{
627 uint16_t frag_num; ///< Fragment Number, from 1
628 uint16_t total; ///< Total Fragments
629 IkeFragReasm *reasm; ///< the reassembly a call acts on
630 const uint8_t *chunk; ///< one fragment's decrypted content
631 size_t chunk_len;
632} IkeFragArgs;
633
634/** @brief The key schedule's inputs and outputs (RFC 7296 sec 2.13, 2.14, 2.17, 2.18). */
635typedef struct
636{
637 const IkeSuite *suite; ///< the negotiated transforms a length mapping reads
638 IkeKeyLengths *lens; ///< in: the per-key lengths a derive uses; out: what suite_keylengths computed
639 IkeKeyMaterial *keys; ///< where a derive writes the seven SK_* keys
640 const uint8_t *prf_key; ///< K for a bare prf+ call
641 size_t prf_key_len;
642 const uint8_t *seed; ///< S for a bare prf+ call
643 size_t seed_len;
644 const uint8_t *dh_secret; ///< g^ir; nullptr in a Child SA derivation without a new exchange
645 size_t dh_len;
646 const uint8_t *ni; ///< Ni, stripped of its payload header
647 size_t ni_len;
648 const uint8_t *nr; ///< Nr, stripped of its payload header
649 size_t nr_len;
650 const uint8_t *spi_i; ///< SPIi of the SA being keyed
651 const uint8_t *spi_r; ///< SPIr of the SA being keyed
652 const uint8_t *sk_d; ///< SK_d: the old SA's for a rekey, this SA's for a Child SA
653 size_t sk_d_len;
654} IkeKeyArgs;
655
656/** @brief What a whole-message build stamps into the header and wraps (sec 3.1, sec 3.14). */
657typedef struct
658{
659 const uint8_t *init_spi; ///< IKE SA Initiator's SPI
660 const uint8_t *resp_spi; ///< IKE SA Responder's SPI
661 uint32_t message_id; ///< Message ID
662 embed_bool is_response; ///< set the R flag rather than the I flag
663 uint32_t length; ///< the Length a patch writes into an already-built header
664 IkePayloadType first_inner_type; ///< the Encrypted payload's Next Payload
665 const uint8_t *inner; ///< the chained inner payloads to encrypt
666 size_t inner_len;
667} IkeMsgArgs;
668
669/** @brief The walks a caller owns, so nested walks do not share one cursor (RFC 7296 sec 3.2). */
670typedef struct
671{
672 IkePayloadIter *chain; ///< the payload chain walk
673 IkeTransformIter *transforms; ///< the transform walk
674 IkeCfgAttrIter *attrs; ///< the Configuration Attribute walk
675 const IkeProposalRef *proposal; ///< the proposal a transform walk starts on
676 IkePayloadType first_type; ///< the header's Next Payload, where a chain walk starts
677} IkeWalkArgs;
678
679/** @brief The session an SA-level or handshake call acts on (RFC 7296 sec 1.2). */
680typedef struct
681{
682 IkeSa *sa; ///< the SA a post-auth exchange is protected by
683 IkeHandshake *hs; ///< the handshake a driver step advances
684 const uint8_t *our_spi; ///< the SPI this side chose
685 const uint8_t *our_nonce; ///< the nonce this side sends
686 size_t our_nonce_len;
687} IkeSessionArgs;
688
689/**
690 * @brief The IKEv2 handle (RFC 7296).
691 *
692 * A caller sets the members a call takes, invokes it through ::Ike, and reads the outcome off the
693 * same handle. Slices returned by a parse point into the caller's own buffer.
694 *
695 * No storage member: every octet, SA, handshake and reassembly a call touches belongs to the caller,
696 * so nothing survives a call.
697 *
698 * @var IkeNs::work the caller's scratch region the hash, PRF and signature calls borrow
699 * @var IkeNs::hdr in: the header a build encodes; out: the header a parse decoded (sec 3.1)
700 * @var IkeNs::out where a build, a hash or a signature writes
701 * @var IkeNs::wire the octets a parse reads
702 * @var IkeNs::pl the generic payload header a build writes, and the payload's variable field
703 * @var IkeNs::prop the Proposal Substructure a build encodes, and the SPIs a Notify or Delete names
704 * @var IkeNs::ke the key exchange: the group and the public and private values
705 * @var IkeNs::id the identity: the ID payload's fields and its signed remainder
706 * @var IkeNs::auth the Auth Method and every input the AUTH value is computed from
707 * @var IkeNs::notify the Notify payload's own fields and the COOKIE it carries
708 * @var IkeNs::ts the Traffic Selectors a build encodes and the one a get names
709 * @var IkeNs::cp the Configuration payload a build encodes
710 * @var IkeNs::sk the Encrypted payload and its AEAD inputs
711 * @var IkeNs::frag the Encrypted Fragment counters and the chunk a reassembler stages
712 * @var IkeNs::keymat the key schedule's inputs and outputs
713 * @var IkeNs::msg what a whole-message build stamps into the header and wraps
714 * @var IkeNs::walk the caller-owned chain, transform and attribute walks
715 * @var IkeNs::sess the SA or handshake a call acts on
716 *
717 * @var IkeNs::ok a call's true/false outcome
718 * @var IkeNs::n octets a build wrote or a call produced, zero on failure
719 * @var IkeNs::u8 Number of TSs a count reports (sec 3.13)
720 * @var IkeNs::payload the payload a chain walk produced
721 * @var IkeNs::proposal the first Proposal Substructure of an SA payload
722 * @var IkeNs::transform the Transform Substructure a transform walk produced
723 * @var IkeNs::sel the Traffic Selector a get decoded
724 * @var IkeNs::attr the Configuration Attribute an attribute walk produced
725 * @var IkeNs::ke_ref the decoded Key Exchange payload
726 * @var IkeNs::id_ref the decoded Identification payload
727 * @var IkeNs::auth_ref the decoded Authentication payload
728 * @var IkeNs::notify_ref the decoded Notify payload
729 * @var IkeNs::delete_ref the decoded Delete payload
730 * @var IkeNs::sk_ref the sliced Encrypted or Encrypted Fragment body
731 * @var IkeNs::cp_ref the decoded Configuration payload
732 * @var IkeNs::opened the inner payload chain an open exposed
733 * @var IkeNs::sa_init the parsed IKE_SA_INIT message
734 *
735 * @var IkeNs::hdr_build write the 28-octet IKE header from @c hdr, Length verbatim (sec 3.1)
736 * @var IkeNs::hdr_parse read the 28-octet IKE header into @c hdr (sec 3.1)
737 * @var IkeNs::set_length patch octets 24..27 of a built header to @c msg.length (sec 3.1)
738 * @var IkeNs::payload_iter_init start @c walk.chain at @c walk.first_type over @c wire (sec 3.2)
739 * @var IkeNs::payload_next read the next payload into @c payload and advance (sec 3.2)
740 * @var IkeNs::payload_build write a generic payload header and @c pl.data behind it (sec 3.2)
741 * @var IkeNs::sa_build write an SA payload carrying one proposal (sec 3.3, 3.3.1, 3.3.2)
742 * @var IkeNs::ke_build write a KE payload: @c ke.dh_group then @c pl.data (sec 3.4)
743 * @var IkeNs::nonce_build write a Nonce payload: @c pl.data is the Nonce Data (sec 3.9)
744 * @var IkeNs::id_build write an ID payload: @c id.id_type then @c pl.data (sec 3.5)
745 * @var IkeNs::auth_build write an AUTH payload: @c auth.auth_method then @c pl.data (sec 3.8)
746 * @var IkeNs::cert_build write a CERT or CERTREQ body: @c id.cert_encoding then @c pl.data (sec 3.6)
747 * @var IkeNs::notify_build write a Notify payload: @c prop SPI fields, type, @c pl.data (sec 3.10)
748 * @var IkeNs::delete_build write a Delete payload: @c prop fields then @c pl.data SPIs (sec 3.11)
749 * @var IkeNs::ts_build write a TSi or TSr payload from @c ts.sels (sec 3.13)
750 * @var IkeNs::cp_build write a CP payload: @c cp.cfg_type then its attributes (sec 3.15)
751 * @var IkeNs::sk_build lay out an SK payload: IV, Ciphertext, ICV (sec 3.14)
752 * @var IkeNs::skf_build lay out an SKF payload: the counters then IV, Ciphertext, ICV (RFC 7383 sec 2.5)
753 * @var IkeNs::skf_parse slice an SKF body into @c sk_ref (RFC 7383 sec 2.5)
754 * @var IkeNs::frag_reasm_init bind @c frag.reasm to the pool at @c out (RFC 7383 sec 2.6)
755 * @var IkeNs::frag_reasm_add stage one fragment's content (RFC 7383 sec 2.6)
756 * @var IkeNs::frag_reasm_complete every one of Total Fragments is staged (RFC 7383 sec 2.6)
757 * @var IkeNs::frag_reasm_assemble merge the staged fragments 1..Total into @c out (RFC 7383 sec 2.6)
758 * @var IkeNs::cookie_compute VersionIDofSecret | SHA-256(Ni | IPi | SPIi | secret) (sec 2.6)
759 * @var IkeNs::cookie_verify recompute that cookie and compare it in constant time (sec 2.6)
760 * @var IkeNs::cookie_notify_build write a COOKIE Notify carrying @c notify.cookie (sec 2.6)
761 * @var IkeNs::ke_parse decode a KE body into @c ke_ref (sec 3.4)
762 * @var IkeNs::id_parse decode an ID body into @c id_ref (sec 3.5)
763 * @var IkeNs::auth_parse decode an AUTH body into @c auth_ref (sec 3.8)
764 * @var IkeNs::notify_parse decode a Notify body into @c notify_ref (sec 3.10)
765 * @var IkeNs::delete_parse decode a Delete body into @c delete_ref (sec 3.11)
766 * @var IkeNs::sk_parse slice an SK body into @c sk_ref by @c sk.iv_len and @c sk.icv_len (sec 3.14)
767 * @var IkeNs::sa_first_proposal decode the first Proposal Substructure into @c proposal (sec 3.3.1)
768 * @var IkeNs::transform_iter_init start @c walk.transforms over @c walk.proposal (sec 3.3.2)
769 * @var IkeNs::transform_next read the next Transform Substructure into @c transform (sec 3.3.2)
770 * @var IkeNs::ts_count Number of TSs in a TS body, into @c u8 (sec 3.13)
771 * @var IkeNs::ts_get decode selector @c ts.index into @c sel (sec 3.13.1)
772 * @var IkeNs::cp_parse decode a CP body into @c cp_ref (sec 3.15)
773 * @var IkeNs::cp_attr_iter_init start @c walk.attrs over @c wire (sec 3.15.1)
774 * @var IkeNs::cp_attr_next read the next Configuration Attribute into @c attr (sec 3.15.1)
775 * @var IkeNs::prf_plus expand prf+(@c keymat.prf_key, @c keymat.seed) into @c out (sec 2.13)
776 * @var IkeNs::derive_keys SKEYSEED then the seven SK_* keys for a new IKE SA (sec 2.14)
777 * @var IkeNs::rekey_derive_keys the same split with SKEYSEED keyed by the old SK_d (sec 2.18)
778 * @var IkeNs::child_keymat KEYMAT = prf+(SK_d, [g^ir |] Ni | Nr) into @c out (sec 2.17)
779 * @var IkeNs::suite_keylengths map @c keymat.suite to the SK_* lengths in @c keymat.lens (sec 2.13)
780 * @var IkeNs::sa_keys_from_init compute g^ir then run the sec 2.14 schedule for @c sess.sa
781 * @var IkeNs::sk_aead_seal encrypt @c sk.pt under @c sk.key, writing Ciphertext then ICV (RFC 5282 sec 3.2)
782 * @var IkeNs::sk_aead_open verify the ICV, then decrypt into @c out (RFC 5282 sec 3.2, 5.1)
783 * @var IkeNs::dh_public our Key Exchange Data for @c ke.dh_group (RFC 8031 sec 3.1)
784 * @var IkeNs::dh_compute g^ir from @c ke.our_priv and @c ke.peer_pub (RFC 8031 sec 2)
785 * @var IkeNs::auth_psk AUTH = prf(prf(Shared Secret, pad), SignedOctets) (sec 2.15)
786 * @var IkeNs::signed_octets assemble RealMessage | Nonce | MACedID into @c auth.scratch (sec 2.15)
787 * @var IkeNs::auth_sign_ecdsa_p256 sign those octets with P-256, writing r | s to @c out (RFC 7427 sec 3)
788 * @var IkeNs::auth_verify_ecdsa_p256 verify a peer's P-256 signature over them (RFC 7427 sec 3)
789 * @var IkeNs::auth_verify_rsa_sha256 verify a peer's RSA PKCS#1 v1.5 SHA-256 signature (sec 3.8)
790 * @var IkeNs::sa_init_build build HDR, SA, KE, Nonce as one IKE_SA_INIT message (sec 1.2)
791 * @var IkeNs::sa_init_parse parse an IKE_SA_INIT message into @c sa_init (sec 1.2)
792 * @var IkeNs::auth_msg_build build HDR, SK{ @c msg.inner } for an IKE_AUTH exchange (sec 3.14)
793 * @var IkeNs::auth_msg_open verify and decrypt an SK message in place into @c opened (sec 3.14)
794 * @var IkeNs::initiator_start emit the initiator's IKE_SA_INIT request (sec 1.2)
795 * @var IkeNs::initiator_on_sa_init consume the IKE_SA_INIT response and derive the keys (sec 1.2)
796 * @var IkeNs::initiator_build_auth_psk emit SK{ IDi, AUTH } with a pre-shared key (sec 1.2, 2.15)
797 * @var IkeNs::initiator_on_auth_psk verify the responder's SK{ IDr, AUTH } (sec 1.2, 2.15)
798 * @var IkeNs::responder_on_sa_init consume an IKE_SA_INIT request and emit the response (sec 1.2)
799 * @var IkeNs::responder_on_auth_psk verify SK{ IDi, AUTH } and emit SK{ IDr, AUTH } (sec 1.2, 2.15)
800 * @var IkeNs::informational_build build an SK-protected INFORMATIONAL message (sec 1.4)
801 * @var IkeNs::informational_open verify and decrypt a received SK-protected message (sec 1.4)
802 * @var IkeNs::create_child_sa_build build an SK-protected CREATE_CHILD_SA message (sec 1.3)
803 */
804typedef struct
805{
806 uint8_t *work; ///< the caller's scratch region the hash, PRF and signature calls borrow
807 IkeHeader hdr; ///< the IKE header a build encodes and a parse decodes (sec 3.1)
808 IkeOutArgs out; ///< where a build writes
809 IkeWireArgs wire; ///< the octets a parse reads
810 IkePayloadArgs pl; ///< the generic payload header and the payload's variable field (sec 3.2)
811 IkeProposalArgs prop; ///< the proposal a build encodes and the SPIs a payload names (sec 3.3.1)
812 IkeKeArgs ke; ///< the key exchange values (sec 3.4)
813 IkeIdArgs id; ///< the identity (sec 3.5, 3.6)
814 IkeAuthArgs auth; ///< authentication inputs (sec 2.15, 3.8)
815 IkeNotifyArgs notify; ///< the Notify payload and the COOKIE (sec 3.10, 2.6)
816 IkeTsArgs ts; ///< the Traffic Selectors (sec 3.13)
817 IkeCpArgs cp; ///< the Configuration payload (sec 3.15)
818 IkeSkArgs sk; ///< the Encrypted payload and its AEAD inputs (sec 3.14)
819 IkeFragArgs frag; ///< the Encrypted Fragment counters and reassembly (RFC 7383)
820 IkeKeyArgs keymat; ///< the key schedule (sec 2.13, 2.14, 2.17, 2.18)
821 IkeMsgArgs msg; ///< whole-message assembly (sec 3.1, 3.14)
822 IkeWalkArgs walk; ///< the caller-owned walks (sec 3.2)
823 IkeSessionArgs sess; ///< the SA or handshake a call acts on (sec 1.2)
824 embed_bool ok;
825 size_t n;
826 uint8_t u8;
827 IkePayload payload;
828 IkeProposalRef proposal;
829 IkeTransformRef transform;
830 IkeTrafficSelector sel;
831 IkeCfgAttr attr;
832 IkeKeRef ke_ref;
833 IkeIdRef id_ref;
834 IkeAuthRef auth_ref;
835 IkeNotifyRef notify_ref;
836 IkeDeleteRef delete_ref;
837 IkeSkRef sk_ref;
838 IkeCpRef cp_ref;
839 IkeInnerRef opened;
840 IkeSaInitMsg sa_init;
841} IkeVars;
842
843/** @brief The operands and the outcome. */
844extern IkeVars IkeV;
845
846/** @brief The entries. */
847typedef struct
848{
849 void (*const hdr_build)(const uint8_t *work);
850 void (*const hdr_parse)(const uint8_t *work);
851 void (*const set_length)(const uint8_t *work);
852 void (*const payload_iter_init)(const uint8_t *work);
853 void (*const payload_next)(const uint8_t *work);
854 void (*const payload_build)(const uint8_t *work);
855 void (*const sa_build)(const uint8_t *work);
856 void (*const ke_build)(const uint8_t *work);
857 void (*const nonce_build)(const uint8_t *work);
858 void (*const id_build)(const uint8_t *work);
859 void (*const auth_build)(const uint8_t *work);
860 void (*const cert_build)(const uint8_t *work);
861 void (*const notify_build)(const uint8_t *work);
862 void (*const delete_build)(const uint8_t *work);
863 void (*const ts_build)(const uint8_t *work);
864 void (*const cp_build)(const uint8_t *work);
865 void (*const sk_build)(const uint8_t *work);
866 void (*const skf_build)(const uint8_t *work);
867 void (*const skf_parse)(const uint8_t *work);
868 void (*const frag_reasm_init)(const uint8_t *work);
869 void (*const frag_reasm_add)(const uint8_t *work);
870 void (*const frag_reasm_complete)(const uint8_t *work);
871 void (*const frag_reasm_assemble)(const uint8_t *work);
872 void (*const cookie_compute)(const uint8_t *work);
873 void (*const cookie_verify)(const uint8_t *work);
874 void (*const cookie_notify_build)(const uint8_t *work);
875 void (*const ke_parse)(const uint8_t *work);
876 void (*const id_parse)(const uint8_t *work);
877 void (*const auth_parse)(const uint8_t *work);
878 void (*const notify_parse)(const uint8_t *work);
879 void (*const delete_parse)(const uint8_t *work);
880 void (*const sk_parse)(const uint8_t *work);
881 void (*const sa_first_proposal)(const uint8_t *work);
882 void (*const transform_iter_init)(const uint8_t *work);
883 void (*const transform_next)(const uint8_t *work);
884 void (*const ts_count)(const uint8_t *work);
885 void (*const ts_get)(const uint8_t *work);
886 void (*const cp_parse)(const uint8_t *work);
887 void (*const cp_attr_iter_init)(const uint8_t *work);
888 void (*const cp_attr_next)(const uint8_t *work);
889 void (*const prf_plus)(const uint8_t *work);
890 void (*const derive_keys)(const uint8_t *work);
891 void (*const rekey_derive_keys)(const uint8_t *work);
892 void (*const child_keymat)(const uint8_t *work);
893 void (*const suite_keylengths)(const uint8_t *work);
894 void (*const sa_keys_from_init)(const uint8_t *work);
895 void (*const sk_aead_seal)(const uint8_t *work);
896 void (*const sk_aead_open)(const uint8_t *work);
897 void (*const dh_public)(const uint8_t *work);
898 void (*const dh_compute)(const uint8_t *work);
899 void (*const auth_psk)(const uint8_t *work);
900 void (*const signed_octets)(const uint8_t *work);
901 void (*const auth_sign_ecdsa_p256)(const uint8_t *work);
902 void (*const auth_verify_ecdsa_p256)(const uint8_t *work);
903 void (*const auth_verify_rsa_sha256)(const uint8_t *work);
904 void (*const sa_init_build)(const uint8_t *work);
905 void (*const sa_init_parse)(const uint8_t *work);
906 void (*const auth_msg_build)(const uint8_t *work);
907 void (*const auth_msg_open)(const uint8_t *work);
908 void (*const initiator_start)(const uint8_t *work);
909 void (*const initiator_on_sa_init)(const uint8_t *work);
910 void (*const initiator_build_auth_psk)(const uint8_t *work);
911 void (*const initiator_on_auth_psk)(const uint8_t *work);
912 void (*const responder_on_sa_init)(const uint8_t *work);
913 void (*const responder_on_auth_psk)(const uint8_t *work);
914 void (*const informational_build)(const uint8_t *work);
915 void (*const informational_open)(const uint8_t *work);
916 void (*const create_child_sa_build)(const uint8_t *work);
917} IkeNs;
918
919// What the table binds, defined once in the .c and taking one parameter each: everything
920// else an entry needs is an operand in IkeV or a region of the borrow at a fixed offset.
921void protocore_ike_hdr_build(const uint8_t *work);
922void protocore_ike_hdr_parse(const uint8_t *work);
923void protocore_ike_set_length(const uint8_t *work);
924void protocore_ike_payload_iter_init(const uint8_t *work);
925void protocore_ike_payload_next(const uint8_t *work);
926void protocore_ike_payload_build(const uint8_t *work);
927void protocore_ike_sa_build(const uint8_t *work);
928void protocore_ike_ke_build(const uint8_t *work);
929void protocore_ike_nonce_build(const uint8_t *work);
930void protocore_ike_id_build(const uint8_t *work);
931void protocore_ike_auth_build(const uint8_t *work);
932void protocore_ike_cert_build(const uint8_t *work);
933void protocore_ike_notify_build(const uint8_t *work);
934void protocore_ike_delete_build(const uint8_t *work);
935void protocore_ike_ts_build(const uint8_t *work);
936void protocore_ike_cp_build(const uint8_t *work);
937void protocore_ike_sk_build(const uint8_t *work);
938void protocore_ike_skf_build(const uint8_t *work);
939void protocore_ike_skf_parse(const uint8_t *work);
940void protocore_ike_frag_reasm_init(const uint8_t *work);
941void protocore_ike_frag_reasm_add(const uint8_t *work);
942void protocore_ike_frag_reasm_complete(const uint8_t *work);
943void protocore_ike_frag_reasm_assemble(const uint8_t *work);
944void protocore_ike_cookie_compute(const uint8_t *work);
945void protocore_ike_cookie_verify(const uint8_t *work);
946void protocore_ike_cookie_notify_build(const uint8_t *work);
947void protocore_ike_ke_parse(const uint8_t *work);
948void protocore_ike_id_parse(const uint8_t *work);
949void protocore_ike_auth_parse(const uint8_t *work);
950void protocore_ike_notify_parse(const uint8_t *work);
951void protocore_ike_delete_parse(const uint8_t *work);
952void protocore_ike_sk_parse(const uint8_t *work);
953void protocore_ike_sa_first_proposal(const uint8_t *work);
954void protocore_ike_transform_iter_init(const uint8_t *work);
955void protocore_ike_transform_next(const uint8_t *work);
956void protocore_ike_ts_count(const uint8_t *work);
957void protocore_ike_ts_get(const uint8_t *work);
958void protocore_ike_cp_parse(const uint8_t *work);
959void protocore_ike_cp_attr_iter_init(const uint8_t *work);
960void protocore_ike_cp_attr_next(const uint8_t *work);
961void protocore_ike_prf_plus(const uint8_t *work);
962void protocore_ike_derive_keys(const uint8_t *work);
963void protocore_ike_rekey_derive_keys(const uint8_t *work);
964void protocore_ike_child_keymat(const uint8_t *work);
965void protocore_ike_suite_keylengths(const uint8_t *work);
966void protocore_ike_sa_keys_from_init(const uint8_t *work);
967void protocore_ike_sk_aead_seal(const uint8_t *work);
968void protocore_ike_sk_aead_open(const uint8_t *work);
969void protocore_ike_dh_public(const uint8_t *work);
970void protocore_ike_dh_compute(const uint8_t *work);
971void protocore_ike_auth_psk(const uint8_t *work);
972void protocore_ike_signed_octets(const uint8_t *work);
973void protocore_ike_auth_sign_ecdsa_p256(const uint8_t *work);
974void protocore_ike_auth_verify_ecdsa_p256(const uint8_t *work);
975void protocore_ike_auth_verify_rsa_sha256(const uint8_t *work);
976void protocore_ike_sa_init_build(const uint8_t *work);
977void protocore_ike_sa_init_parse(const uint8_t *work);
978void protocore_ike_auth_msg_build(const uint8_t *work);
979void protocore_ike_auth_msg_open(const uint8_t *work);
980void protocore_ike_initiator_start(const uint8_t *work);
981void protocore_ike_initiator_on_sa_init(const uint8_t *work);
982void protocore_ike_initiator_build_auth_psk(const uint8_t *work);
983void protocore_ike_initiator_on_auth_psk(const uint8_t *work);
984void protocore_ike_responder_on_sa_init(const uint8_t *work);
985void protocore_ike_responder_on_auth_psk(const uint8_t *work);
986void protocore_ike_informational_build(const uint8_t *work);
987void protocore_ike_informational_open(const uint8_t *work);
988void protocore_ike_create_child_sa_build(const uint8_t *work);
989
990// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
991// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
992// `Ike.hdr_build(work)` resolves to a named function and becomes a DIRECT call. An extern table
993// leaves the call indirect and the symbol live at every level, -O2 -flto included.
994static const IkeNs Ike __attribute__((unused)) = {
995 .hdr_build = protocore_ike_hdr_build,
996 .hdr_parse = protocore_ike_hdr_parse,
997 .set_length = protocore_ike_set_length,
998 .payload_iter_init = protocore_ike_payload_iter_init,
999 .payload_next = protocore_ike_payload_next,
1000 .payload_build = protocore_ike_payload_build,
1001 .sa_build = protocore_ike_sa_build,
1002 .ke_build = protocore_ike_ke_build,
1003 .nonce_build = protocore_ike_nonce_build,
1004 .id_build = protocore_ike_id_build,
1005 .auth_build = protocore_ike_auth_build,
1006 .cert_build = protocore_ike_cert_build,
1007 .notify_build = protocore_ike_notify_build,
1008 .delete_build = protocore_ike_delete_build,
1009 .ts_build = protocore_ike_ts_build,
1010 .cp_build = protocore_ike_cp_build,
1011 .sk_build = protocore_ike_sk_build,
1012 .skf_build = protocore_ike_skf_build,
1013 .skf_parse = protocore_ike_skf_parse,
1014 .frag_reasm_init = protocore_ike_frag_reasm_init,
1015 .frag_reasm_add = protocore_ike_frag_reasm_add,
1016 .frag_reasm_complete = protocore_ike_frag_reasm_complete,
1017 .frag_reasm_assemble = protocore_ike_frag_reasm_assemble,
1018 .cookie_compute = protocore_ike_cookie_compute,
1019 .cookie_verify = protocore_ike_cookie_verify,
1020 .cookie_notify_build = protocore_ike_cookie_notify_build,
1021 .ke_parse = protocore_ike_ke_parse,
1022 .id_parse = protocore_ike_id_parse,
1023 .auth_parse = protocore_ike_auth_parse,
1024 .notify_parse = protocore_ike_notify_parse,
1025 .delete_parse = protocore_ike_delete_parse,
1026 .sk_parse = protocore_ike_sk_parse,
1027 .sa_first_proposal = protocore_ike_sa_first_proposal,
1028 .transform_iter_init = protocore_ike_transform_iter_init,
1029 .transform_next = protocore_ike_transform_next,
1030 .ts_count = protocore_ike_ts_count,
1031 .ts_get = protocore_ike_ts_get,
1032 .cp_parse = protocore_ike_cp_parse,
1033 .cp_attr_iter_init = protocore_ike_cp_attr_iter_init,
1034 .cp_attr_next = protocore_ike_cp_attr_next,
1035 .prf_plus = protocore_ike_prf_plus,
1036 .derive_keys = protocore_ike_derive_keys,
1037 .rekey_derive_keys = protocore_ike_rekey_derive_keys,
1038 .child_keymat = protocore_ike_child_keymat,
1039 .suite_keylengths = protocore_ike_suite_keylengths,
1040 .sa_keys_from_init = protocore_ike_sa_keys_from_init,
1041 .sk_aead_seal = protocore_ike_sk_aead_seal,
1042 .sk_aead_open = protocore_ike_sk_aead_open,
1043 .dh_public = protocore_ike_dh_public,
1044 .dh_compute = protocore_ike_dh_compute,
1045 .auth_psk = protocore_ike_auth_psk,
1046 .signed_octets = protocore_ike_signed_octets,
1047 .auth_sign_ecdsa_p256 = protocore_ike_auth_sign_ecdsa_p256,
1048 .auth_verify_ecdsa_p256 = protocore_ike_auth_verify_ecdsa_p256,
1049 .auth_verify_rsa_sha256 = protocore_ike_auth_verify_rsa_sha256,
1050 .sa_init_build = protocore_ike_sa_init_build,
1051 .sa_init_parse = protocore_ike_sa_init_parse,
1052 .auth_msg_build = protocore_ike_auth_msg_build,
1053 .auth_msg_open = protocore_ike_auth_msg_open,
1054 .initiator_start = protocore_ike_initiator_start,
1055 .initiator_on_sa_init = protocore_ike_initiator_on_sa_init,
1056 .initiator_build_auth_psk = protocore_ike_initiator_build_auth_psk,
1057 .initiator_on_auth_psk = protocore_ike_initiator_on_auth_psk,
1058 .responder_on_sa_init = protocore_ike_responder_on_sa_init,
1059 .responder_on_auth_psk = protocore_ike_responder_on_auth_psk,
1060 .informational_build = protocore_ike_informational_build,
1061 .informational_open = protocore_ike_informational_open,
1062 .create_child_sa_build = protocore_ike_create_child_sa_build,
1063};
1064
1065EMBED_END_DECLS
1066
1067#endif // PROTOCORE_ENABLE_IKEV2
1068
1069#endif // PROTOCORE_IKEV2_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.
#define PROTOCORE_IKE_BORROW