interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / net / tests / sec_test.c

956 lines · 41843 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* M6-B secure control transport tests (hosted, TEST identities only).
18 *
19 * Two in-process machines exchange datagrams over the M6-A stack: every
20 * datagram is built into a real Ethernet/IPv4/UDP frame with aienos_net,
21 * queued on a simulated wire, parsed back and handed to the receiver.
22 * Scenarios: handshake, wrong identity, transcript tamper (every byte of
23 * every handshake message), splice and impersonation attempts, replay,
24 * reordering inside and outside the window, truncation/extension/bit flips,
25 * rekey across epochs, close, deterministic loss (drop every Nth datagram)
26 * with the M6-A reliable channel carried inside the secure records, and a
27 * malformed-input sweep. Prints AIENOS_NET_SECURE_TESTS: PASS or FAIL.
28 *
29 * Every key here is a public TEST key (sec_test_identity); the "entropy"
30 * is a seeded generator, NOT-FOR-PRODUCTION.
31 */
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include "../aienos_ctl.h"
37#include "../aienos_net.h"
38#include "../aienos_sec.h"
39#include "../x25519.h"
40#include "../../argus/sha256.h"
41#include "../../crypto/aienos_crypto.h"
42#include "../../sig/aienos_sig.h"
43
44static unsigned long checks, failures;
45#define CHECK(c) do { checks++; if (!(c)) { failures++; fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); } } while (0)
46#define EQ(a, b) do { long long _a = (long long)(a), _b = (long long)(b); checks++; if (_a != _b) { failures++; fprintf(stderr, "FAIL %s:%d: %s == %s (%lld vs %lld)\n", __FILE__, __LINE__, #a, #b, _a, _b); } } while (0)
47
48static uint64_t prng_s = 0x5ec0de;
49static uint8_t rnd8(void)
50{
51 prng_s ^= prng_s << 13; prng_s ^= prng_s >> 7; prng_s ^= prng_s << 17;
52 return (uint8_t)(prng_s >> 24);
53}
54static void fill(uint8_t *b, size_t n) { for (size_t i = 0; i < n; i++) b[i] = rnd8(); }
55
56static int all_zero(const uint8_t *b, size_t n)
57{
58 uint8_t a = 0;
59 for (size_t i = 0; i < n; i++) a |= b[i];
60 return a == 0;
61}
62
63static int contains(const void *hay, size_t hn, const uint8_t *needle, size_t nn)
64{
65 const uint8_t *h = hay;
66 for (size_t i = 0; i + nn <= hn; i++) if (memcmp(h + i, needle, nn) == 0) return 1;
67 return 0;
68}
69
70/* ---- identities and endpoints ---- */
71
72static uint8_t SK_A[32], PK_A[32], SK_B[32], PK_B[32], SK_M[32], PK_M[32];
73
74static void cfg_for(sec_config *c, int role, const uint8_t *sk, const uint8_t *peer_pk)
75{
76 memset(c, 0, sizeof *c);
77 c->role = (uint8_t)role;
78 memcpy(c->id_sk, sk, 32);
79 memcpy(c->peer_pk, peer_pk, 32);
80 fill(c->entropy, 64);
81 c->rto = 4;
82 c->max_retries = 6;
83 c->rekey_interval = 1u << 16;
84 c->max_records = (uint64_t)1 << 40;
85}
86
87static void make_pair(sec_endpoint *a, sec_endpoint *b, uint64_t rekey, uint32_t retries)
88{
89 sec_config ca, cb;
90 cfg_for(&ca, 0, SK_A, PK_B);
91 cfg_for(&cb, 1, SK_B, PK_A);
92 ca.rekey_interval = cb.rekey_interval = rekey;
93 ca.max_retries = cb.max_retries = retries;
94 EQ(sec_init(a, &ca), SEC_OK);
95 EQ(sec_init(b, &cb), SEC_OK);
96}
97
98/* ---- M6-A wire: datagram -> Ethernet/IPv4/UDP frame -> queue -> parse ---- */
99
100#define PORT 7016
101#define QMAX 512
102#define FRAME_MAX 1600
103typedef struct { int to; size_t n; uint8_t f[FRAME_MAX]; } wframe;
104typedef struct {
105 wframe q[QMAX];
106 size_t head, nq;
107 uint64_t sent, dropped, drop_every, count[2]; /* drop pattern counted per direction */
108 int tamper_type, tamper_pos; /* flip byte tamper_pos of every datagram of this type */
109 int blackout; /* drop everything */
110} wire;
111
112static const net_mac MAC[2] = {{{0x02, 0, 0, 0, 0, 0xa1}}, {{0x02, 0, 0, 0, 0, 0xb2}}};
113static const net_ipv4 IP[2] = {{{10, 6, 0, 1}}, {{10, 6, 0, 2}}};
114
115static void wire_put(wire *w, int from, const uint8_t *d, size_t n)
116{
117 w->sent++;
118 w->count[from]++;
119 if (w->blackout || (w->drop_every && w->count[from] % w->drop_every == 0)) { w->dropped++; return; }
120 uint8_t dg[SEC_MAX_DATAGRAM];
121 if (n > sizeof dg) { CHECK(0); return; }
122 memcpy(dg, d, n);
123 if (w->tamper_type && n >= 6 && dg[5] == w->tamper_type && (size_t)w->tamper_pos < n)
124 dg[w->tamper_pos] ^= 0x01;
125 uint8_t udp[FRAME_MAX], ip[FRAME_MAX];
126 size_t un = 0, in = 0, fn = 0;
127 net_udp_header uh = {PORT, PORT};
128 int to = 1 - from;
129 CHECK(net_udp_build(&uh, dg, n, IP[from], IP[to], udp, sizeof udp, &un) == NET_OK);
130 net_ipv4_header ih = {0, (uint16_t)w->sent, 64, NET_IPPROTO_UDP, IP[from], IP[to]};
131 CHECK(net_ipv4_build(&ih, udp, un, ip, sizeof ip, &in) == NET_OK);
132 net_eth_frame ef = {MAC[to], MAC[from], NET_ETHERTYPE_IPV4, ip, in};
133 if (w->nq >= QMAX) { w->dropped++; return; }
134 wframe *x = &w->q[(w->head + w->nq++) % QMAX];
135 x->to = to;
136 CHECK(net_eth_build(&ef, x->f, sizeof x->f, &fn) == NET_OK);
137 x->n = fn;
138}
139
140/* Pop the next frame and unwrap it to the secure-transport datagram. */
141static int wire_get(wire *w, int *to, const uint8_t **dg, size_t *dn, uint8_t *keep)
142{
143 if (!w->nq) return 0;
144 wframe *x = &w->q[w->head];
145 w->head = (w->head + 1) % QMAX;
146 w->nq--;
147 memcpy(keep, x->f, x->n);
148 *to = x->to;
149 net_eth_frame ef;
150 net_ipv4_header ih;
151 net_udp_header uh;
152 const uint8_t *ipp, *udpp;
153 size_t ipn, udpn;
154 if (net_eth_parse(keep, x->n, &ef) != NET_OK || ef.ethertype != NET_ETHERTYPE_IPV4) return -1;
155 if (memcmp(ef.destination.b, MAC[x->to].b, 6) != 0) return -1;
156 if (net_ipv4_parse(ef.payload, ef.payload_len, &ih, &ipp, &ipn) != NET_OK) return -1;
157 if (ih.protocol != NET_IPPROTO_UDP || memcmp(ih.destination.b, IP[x->to].b, 4) != 0) return -1;
158 if (net_udp_parse(ipp, ipn, ih.source, ih.destination, &uh, &udpp, &udpn) != NET_OK) return -1;
159 if (uh.destination_port != PORT) return -1;
160 *dg = udpp;
161 *dn = udpn;
162 return 1;
163}
164
165/* ---- two machines ---- */
166
167#define NMSG 150
168typedef struct {
169 sec_endpoint s;
170 ct_endpoint ct;
171 int use_ct;
172 uint32_t next_send, next_expect;
173 uint64_t delivered, misordered, data_records;
174 int last_status, saw_status[32]; /* index -status for errors */
175 int peer_closed;
176} machine;
177
178typedef struct {
179 machine m[2];
180 wire w;
181 uint64_t now;
182} sys_t;
183
184static void ct_msg(int from, uint32_t i, uint8_t *m, size_t *n)
185{
186 *n = (i * 29u + (unsigned)from * 13u) % 300u + 4u;
187 for (size_t k = 0; k < *n; k++) m[k] = (uint8_t)(i * 151u + k * 3u + (unsigned)from);
188 m[0] = (uint8_t)(i >> 8); m[1] = (uint8_t)i; m[2] = (uint8_t)from; m[3] = 0x5a;
189}
190
191static void note(machine *x, sec_status s)
192{
193 x->last_status = s;
194 if (s < 0 && -s < 32) x->saw_status[-s] = 1;
195}
196
197static void sys_init(sys_t *S, uint64_t rekey, uint32_t retries, int use_ct, uint32_t ct_rto)
198{
199 memset(S, 0, sizeof *S);
200 make_pair(&S->m[0].s, &S->m[1].s, rekey, retries);
201 for (int i = 0; i < 2; i++) {
202 S->m[i].use_ct = use_ct;
203 if (use_ct) {
204 ct_identity id;
205 CHECK(ct_test_identity(&id, "lane16-inner") == CT_OK);
206 CHECK(ct_init(&S->m[i].ct, &id, (uint8_t)i, 0x16, ct_rto, 40) == CT_OK);
207 }
208 }
209}
210
211/* One tick: each side emits handshake/control datagrams, then (if
212 * established) its M6-A control frames sealed in records; then the wire is
213 * drained into the receivers. */
214static void sys_tick(sys_t *S)
215{
216 uint8_t out[FRAME_MAX], frame[CT_MAX_FRAME], msg[SEC_MAX_PAYLOAD];
217 size_t n;
218 for (int i = 0; i < 2; i++) {
219 machine *x = &S->m[i];
220 for (int k = 0; k < 8; k++) {
221 sec_status s = sec_poll_tx(&x->s, S->now, out, sizeof out, &n);
222 if (s != SEC_OK) { if (s != SEC_IDLE) note(x, s); break; }
223 wire_put(&S->w, i, out, n);
224 }
225 if (!x->use_ct || x->s.state != SEC_ST_ESTABLISHED) continue;
226 int cap_frames = 16;
227 while (x->next_send < NMSG && ct_in_flight(&x->ct) < CT_WINDOW) {
228 size_t ml;
229 ct_msg(i, x->next_send, msg, &ml);
230 if (ct_send(&x->ct, msg, ml) != CT_OK) break;
231 x->next_send++;
232 }
233 for (int k = 0; k < cap_frames; k++) {
234 ct_status c = ct_poll_tx(&x->ct, S->now, frame, sizeof frame, &n);
235 if (c != CT_OK) break;
236 size_t rn;
237 sec_status s = sec_seal(&x->s, frame, n, out, sizeof out, &rn);
238 if (s != SEC_OK) { note(x, s); break; }
239 wire_put(&S->w, i, out, rn);
240 }
241 }
242 uint8_t keep[FRAME_MAX], got[CT_MAX_PAYLOAD], exp[CT_MAX_PAYLOAD];
243 size_t budget = S->w.nq;
244 while (budget--) {
245 int to;
246 const uint8_t *dg;
247 size_t dn;
248 int r = wire_get(&S->w, &to, &dg, &dn, keep);
249 if (r <= 0) { CHECK(r == 0); continue; }
250 machine *x = &S->m[to];
251 size_t ml;
252 sec_status s = sec_receive(&x->s, S->now, dg, dn, msg, sizeof msg, &ml);
253 note(x, s);
254 if (s == SEC_PEER_CLOSED) x->peer_closed = 1;
255 if (s != SEC_DATA) continue;
256 x->data_records++;
257 if (!x->use_ct) continue;
258 size_t gl;
259 ct_status c = ct_receive(&x->ct, msg, ml, got, sizeof got, &gl);
260 if (c == CT_DELIVERED) {
261 size_t el;
262 ct_msg(1 - to, x->next_expect, exp, &el);
263 if (el != gl || memcmp(exp, got, gl) != 0) x->misordered++;
264 x->next_expect++;
265 x->delivered++;
266 }
267 }
268 S->now++;
269}
270
271static int established(const sys_t *S)
272{
273 return S->m[0].s.state == SEC_ST_ESTABLISHED && S->m[1].s.state == SEC_ST_ESTABLISHED;
274}
275
276static void run_handshake(sys_t *S, int ticks)
277{
278 for (int t = 0; t < ticks && !(established(S) && S->m[0].s.confirmed); t++) sys_tick(S);
279}
280
281/* ---- tests ---- */
282
283static void test_hkdf(void)
284{
285 /* RFC 5869 A.1 (rfc-editor.org/rfc/rfc5869.txt, sha256
286 * 7a40eb3835b35fc947eb12a2ed614db079d43b26e50dbc537c31fba16397089c).
287 * The 13-byte salt zero-padded to 32 bytes is the same HMAC key. */
288 static const uint8_t prk_exp[32] = {
289 0x07, 0x77, 0x09, 0x36, 0x2c, 0x2e, 0x32, 0xdf, 0x0d, 0xdc, 0x3f, 0x0d, 0xc4, 0x7b, 0xba, 0x63,
290 0x90, 0xb6, 0xc7, 0x3b, 0xb5, 0x0f, 0x9c, 0x31, 0x22, 0xec, 0x84, 0x4a, 0xd7, 0xc2, 0xb3, 0xe5};
291 static const uint8_t okm_exp[32] = {
292 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
293 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c, 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf};
294 uint8_t ikm[22], salt[32] = {0}, info[10], prk[32], okm[32];
295 memset(ikm, 0x0b, sizeof ikm);
296 for (int i = 0; i < 13; i++) salt[i] = (uint8_t)i;
297 for (int i = 0; i < 10; i++) info[i] = (uint8_t)(0xf0 + i);
298 sec_hkdf_extract(prk, salt, ikm, sizeof ikm);
299 sec_hkdf_expand1(okm, prk, info, sizeof info);
300 CHECK(memcmp(prk, prk_exp, 32) == 0);
301 CHECK(memcmp(okm, okm_exp, 32) == 0);
302}
303
304static void test_handshake_and_data(void)
305{
306 sys_t S;
307 sys_init(&S, 1u << 16, 6, 0, 4);
308 run_handshake(&S, 20);
309 CHECK(established(&S));
310 CHECK(S.m[0].s.confirmed);
311 sec_endpoint *a = &S.m[0].s, *b = &S.m[1].s;
312 CHECK(memcmp(a->session_id, b->session_id, 8) == 0);
313 CHECK(memcmp(a->tx_key, b->rx_key, 32) == 0 && memcmp(b->tx_key, a->rx_key, 32) == 0);
314 CHECK(memcmp(a->tx_key, a->rx_key, 32) != 0);
315 CHECK(all_zero(a->eph_sk, 32) && all_zero(b->eph_sk, 32) && all_zero(a->prk, 32) &&
316 all_zero(b->prk, 32) && all_zero(b->fin_key_i, 32));
317 /* data both ways, every payload size 0..SEC_MAX_PAYLOAD step 37, plus the max */
318 uint8_t m[SEC_MAX_PAYLOAD], d[SEC_MAX_DATAGRAM], got[SEC_MAX_PAYLOAD];
319 size_t n, gl;
320 int ok = 1;
321 for (size_t len = 0; len <= SEC_MAX_PAYLOAD; len = len == SEC_MAX_PAYLOAD ? len + 1 : (len + 37 > SEC_MAX_PAYLOAD ? SEC_MAX_PAYLOAD : len + 37)) {
322 for (int dir = 0; dir < 2; dir++) {
323 sec_endpoint *tx = dir ? b : a, *rx = dir ? a : b;
324 fill(m, len);
325 if (sec_seal(tx, m, len, d, sizeof d, &n) != SEC_OK || n != SEC_REC_OVERHEAD + len) ok = 0;
326 if (sec_receive(rx, 0, d, n, got, sizeof got, &gl) != SEC_DATA || gl != len || memcmp(got, m, len)) ok = 0;
327 }
328 }
329 CHECK(ok);
330 EQ(sec_seal(a, m, SEC_MAX_PAYLOAD + 1, d, sizeof d, &n), SEC_ERR_TOO_LARGE);
331 EQ(sec_seal(a, m, 10, d, SEC_REC_OVERHEAD + 9, &n), SEC_ERR_CAPACITY);
332 /* receiver capacity: refused before anything changes, then accepted */
333 CHECK(sec_seal(a, m, 100, d, sizeof d, &n) == SEC_OK);
334 EQ(sec_receive(b, 0, d, n, got, 99, &gl), SEC_ERR_CAPACITY);
335 EQ(sec_receive(b, 0, d, n, got, 100, &gl), SEC_DATA);
336 /* the ciphertext differs from the plaintext and between records */
337 uint8_t d2[SEC_MAX_DATAGRAM];
338 size_t n2;
339 memset(m, 0x41, 64);
340 CHECK(sec_seal(a, m, 64, d, sizeof d, &n) == SEC_OK && sec_seal(a, m, 64, d2, sizeof d2, &n2) == SEC_OK);
341 CHECK(!contains(d, n, m, 16) && memcmp(d + 24, d2 + 24, n - 24) != 0);
342 /* reflection: our own record sent back to us */
343 EQ(sec_receive(a, 0, d, n, got, sizeof got, &gl), SEC_ERR_MALFORMED);
344 /* state machine: messages for the wrong role or stage */
345 EQ(sec_receive(a, 0, a->hs1, SEC_HS1_LEN, got, sizeof got, &gl), SEC_ERR_STATE);
346 EQ(sec_receive(b, 0, b->hs2, SEC_HS2_LEN, got, sizeof got, &gl), SEC_ERR_STATE);
347 EQ(sec_receive(a, 0, a->hs3, SEC_HS3_LEN, got, sizeof got, &gl), SEC_ERR_STATE);
348}
349
350/* ct over sec, with a loss pattern. Returns 1 when both sides got all messages. */
351static int run_ct(uint64_t drop_every, uint64_t rekey, uint64_t *digest_out)
352{
353 sys_t S;
354 prng_s = 0x10ad + drop_every;
355 sys_init(&S, rekey, 40, 1, 3);
356 S.w.drop_every = drop_every;
357 for (int t = 0; t < 20000 && !(S.m[0].delivered == NMSG && S.m[1].delivered == NMSG); t++) sys_tick(&S);
358 int ok = S.m[0].delivered == NMSG && S.m[1].delivered == NMSG && !S.m[0].misordered &&
359 !S.m[1].misordered && S.m[0].ct.c.rx_bad_tag == 0 && S.m[1].ct.c.rx_bad_tag == 0;
360 if (!ok)
361 fprintf(stderr, "loss N=%llu: delivered %llu/%llu misordered %llu/%llu state %d/%d\n",
362 (unsigned long long)drop_every, (unsigned long long)S.m[0].delivered,
363 (unsigned long long)S.m[1].delivered, (unsigned long long)S.m[0].misordered,
364 (unsigned long long)S.m[1].misordered, S.m[0].s.state, S.m[1].s.state);
365 if (!ok) for (int i = 0; i < 2; i++) fprintf(stderr, " m%d ct down %d inflight %u base %u next %u exp %u retx %llu; sec rx %llu replay %llu badtag %llu state %llu malformed %llu; ticks %llu\n", i, S.m[i].ct.link_down, ct_in_flight(&S.m[i].ct), S.m[i].ct.base, S.m[i].ct.next_seq, S.m[i].ct.expected, (unsigned long long)S.m[i].ct.c.tx_retransmits, (unsigned long long)S.m[i].s.c.rx_records, (unsigned long long)S.m[i].s.c.rx_replay, (unsigned long long)S.m[i].s.c.rx_bad_tag, (unsigned long long)S.m[i].s.c.rx_state, (unsigned long long)S.m[i].s.c.rx_malformed, (unsigned long long)S.now);
366 /* no record ever failed authentication on an honest (lossy) link */
367 CHECK(S.m[0].s.c.rx_bad_tag == 0 && S.m[1].s.c.rx_bad_tag == 0);
368 if (digest_out)
369 *digest_out = S.now * 1000003u + S.w.sent * 31u + S.w.dropped + S.m[0].s.c.tx_records * 7u +
370 S.m[1].s.c.tx_hs_resend * 11u;
371 if (drop_every && S.w.dropped == 0) ok = 0; /* loss really happened */
372 return ok;
373}
374
375static void test_loss(void)
376{
377 static const uint64_t N[] = {0, 2, 3, 4, 5, 7, 11, 16};
378 for (size_t i = 0; i < sizeof N / sizeof N[0]; i++) {
379 uint64_t d1 = 0, d2 = 0;
380 CHECK(run_ct(N[i], 1u << 16, &d1));
381 CHECK(run_ct(N[i], 1u << 16, &d2));
382 EQ(d1, d2); /* deterministic: bit-identical rerun */
383 }
384 /* loss plus frequent rekeys: records cross many epochs and get reordered by resends */
385 CHECK(run_ct(3, SEC_MIN_REKEY_INTERVAL, NULL));
386 CHECK(run_ct(5, SEC_MIN_REKEY_INTERVAL, NULL));
387 /* total blackout: the initiator gives up after max_retries */
388 sys_t S;
389 sys_init(&S, 1u << 16, 3, 0, 4);
390 S.w.blackout = 1;
391 for (int t = 0; t < 200; t++) sys_tick(&S);
392 EQ(S.m[0].s.state, SEC_ST_DOWN);
393 CHECK(S.m[0].saw_status[-SEC_ERR_LINK_DOWN]);
394 CHECK(all_zero(S.m[0].s.id_sk, 32));
395 EQ(S.m[0].s.c.tx_hs, 1 + 3);
396 /* HS3 lost forever (responder never established): initiator goes down */
397 sys_init(&S, 1u << 16, 3, 0, 4);
398 S.w.tamper_type = SEC_TYPE_HS3;
399 S.w.tamper_pos = 0; /* bad magic: always malformed */
400 for (int t = 0; t < 200; t++) sys_tick(&S);
401 EQ(S.m[0].s.state, SEC_ST_DOWN);
402 EQ(S.m[1].s.state, SEC_ST_WAIT_HS3);
403}
404
405static void test_wrong_identity(void)
406{
407 sys_t S;
408 /* initiator expects B, responder is Mallory with a valid key of its own */
409 memset(&S, 0, sizeof S);
410 sec_config ca, cb;
411 cfg_for(&ca, 0, SK_A, PK_B);
412 cfg_for(&cb, 1, SK_M, PK_A);
413 ca.max_retries = cb.max_retries = 3;
414 CHECK(sec_init(&S.m[0].s, &ca) == SEC_OK && sec_init(&S.m[1].s, &cb) == SEC_OK);
415 for (int t = 0; t < 200; t++) sys_tick(&S);
416 CHECK(S.m[0].saw_status[-SEC_ERR_IDENTITY]);
417 CHECK(S.m[0].s.c.rx_identity >= 1);
418 EQ(S.m[0].s.state, SEC_ST_DOWN);
419 CHECK(all_zero(S.m[0].s.tx_key, 32));
420 /* responder expects A, initiator is Mallory */
421 memset(&S, 0, sizeof S);
422 cfg_for(&ca, 0, SK_M, PK_B);
423 cfg_for(&cb, 1, SK_B, PK_A);
424 ca.max_retries = cb.max_retries = 3;
425 CHECK(sec_init(&S.m[0].s, &ca) == SEC_OK && sec_init(&S.m[1].s, &cb) == SEC_OK);
426 for (int t = 0; t < 200; t++) sys_tick(&S);
427 CHECK(S.m[1].saw_status[-SEC_ERR_IDENTITY]);
428 CHECK(S.m[1].s.state != SEC_ST_ESTABLISHED);
429 EQ(S.m[0].s.state, SEC_ST_DOWN); /* never confirmed */
430 /* records Mallory sends are refused: B has no keys */
431 CHECK(S.m[1].s.c.rx_records == 0);
432}
433
434/* Impersonation: Mallory claims the expected identity but can only sign
435 * with her own key; she knows every ephemeral secret she uses, so the
436 * finished MAC is valid and only the signature check can refuse. */
437static void test_impersonation(void)
438{
439 sys_t S;
440 sec_config ca, cb;
441 /* Mallory as responder claiming to be B */
442 memset(&S, 0, sizeof S);
443 cfg_for(&ca, 0, SK_A, PK_B);
444 cfg_for(&cb, 1, SK_M, PK_A);
445 ca.max_retries = cb.max_retries = 3;
446 CHECK(sec_init(&S.m[0].s, &ca) == SEC_OK && sec_init(&S.m[1].s, &cb) == SEC_OK);
447 memcpy(S.m[1].s.id_pk, PK_B, 32);
448 for (int t = 0; t < 200; t++) sys_tick(&S);
449 CHECK(S.m[0].saw_status[-SEC_ERR_BAD_SIG]);
450 CHECK(S.m[0].s.state != SEC_ST_ESTABLISHED);
451 /* Mallory as initiator claiming to be A */
452 memset(&S, 0, sizeof S);
453 cfg_for(&ca, 0, SK_M, PK_B);
454 cfg_for(&cb, 1, SK_B, PK_A);
455 ca.max_retries = cb.max_retries = 3;
456 CHECK(sec_init(&S.m[0].s, &ca) == SEC_OK && sec_init(&S.m[1].s, &cb) == SEC_OK);
457 memcpy(S.m[0].s.id_pk, PK_A, 32);
458 for (int t = 0; t < 200; t++) sys_tick(&S);
459 CHECK(S.m[1].saw_status[-SEC_ERR_BAD_SIG]);
460 CHECK(S.m[1].s.state != SEC_ST_ESTABLISHED);
461}
462
463/* Flip one byte of every copy of one handshake message type in transit. */
464static void test_transcript_tamper(void)
465{
466 static const struct { int type; size_t len; } T[] = {
467 {SEC_TYPE_HS1, SEC_HS1_LEN}, {SEC_TYPE_HS2, SEC_HS2_LEN}, {SEC_TYPE_HS3, SEC_HS3_LEN}};
468 unsigned long refused = 0, positions = 0, split = 0, est = 0;
469 for (size_t t = 0; t < 3; t++) {
470 for (size_t pos = 0; pos < T[t].len; pos++) {
471 if (pos == 5) continue; /* the type byte itself: the message becomes another type */
472 sys_t S;
473 sys_init(&S, 1u << 16, 2, 0, 3);
474 S.w.tamper_type = T[t].type;
475 S.w.tamper_pos = (int)pos;
476 for (int k = 0; k < 60; k++) sys_tick(&S);
477 positions++;
478 (void)0; /* an HS1 random byte is only caught by the initiator (signature) */
479 int saw = 0;
480 for (int e = 1; e < 32; e++) saw |= S.m[0].saw_status[e] | S.m[1].saw_status[e];
481 if (saw) refused++;
482 if (S.m[1].s.state == SEC_ST_ESTABLISHED) est++;
483 if (established(&S) && memcmp(S.m[0].s.tx_key, S.m[1].s.rx_key, 32) != 0) split++;
484 if (S.m[0].s.state == SEC_ST_ESTABLISHED && S.m[0].s.confirmed) est++;
485 }
486 }
487 EQ(refused, positions); /* every tampered message was refused by a machine */
488 EQ(est, 0); /* and no handshake completed */
489 EQ(split, 0);
490 /* the type byte: HS1 relabelled as HS2/HS3 etc. is refused too */
491 for (size_t t = 0; t < 3; t++) {
492 sys_t S;
493 sys_init(&S, 1u << 16, 2, 0, 3);
494 S.w.tamper_type = T[t].type;
495 S.w.tamper_pos = 5;
496 for (int k = 0; k < 60; k++) sys_tick(&S);
497 CHECK(!(S.m[1].s.state == SEC_ST_ESTABLISHED) && !(S.m[0].s.confirmed));
498 }
499}
500
501static void test_splice(void)
502{
503 /* two concurrent handshakes between the same two identities */
504 sec_endpoint a1, b1, a2, b2;
505 uint8_t h1a[SEC_HS1_LEN], h1b[SEC_HS1_LEN], h2a[SEC_HS2_LEN], h2b[SEC_HS2_LEN], h3a[SEC_HS3_LEN], h3b[SEC_HS3_LEN];
506 uint8_t msg[64];
507 size_t n, ml;
508 make_pair(&a1, &b1, 1u << 16, 3);
509 make_pair(&a2, &b2, 1u << 16, 3);
510 CHECK(sec_poll_tx(&a1, 0, h1a, sizeof h1a, &n) == SEC_OK && n == SEC_HS1_LEN);
511 CHECK(sec_poll_tx(&a2, 0, h1b, sizeof h1b, &n) == SEC_OK);
512 EQ(sec_receive(&b1, 0, h1a, SEC_HS1_LEN, msg, sizeof msg, &ml), SEC_OK);
513 EQ(sec_receive(&b2, 0, h1b, SEC_HS1_LEN, msg, sizeof msg, &ml), SEC_OK);
514 CHECK(sec_poll_tx(&b1, 0, h2a, sizeof h2a, &n) == SEC_OK && n == SEC_HS2_LEN);
515 CHECK(sec_poll_tx(&b2, 0, h2b, sizeof h2b, &n) == SEC_OK);
516 /* session 2's HS2 spliced into session 1 */
517 EQ(sec_receive(&a1, 0, h2b, SEC_HS2_LEN, msg, sizeof msg, &ml), SEC_ERR_BAD_SIG);
518 /* a mixed HS2: session 1's signature with session 2's ephemeral */
519 uint8_t mix[SEC_HS2_LEN];
520 memcpy(mix, h2a, SEC_HS2_LEN);
521 memcpy(mix + 40, h2b + 40, 32);
522 EQ(sec_receive(&a1, 0, mix, SEC_HS2_LEN, msg, sizeof msg, &ml), SEC_ERR_BAD_SIG);
523 EQ(a1.state, SEC_ST_WAIT_HS2); /* refusals change nothing */
524 EQ(sec_receive(&a1, 0, h2a, SEC_HS2_LEN, msg, sizeof msg, &ml), SEC_OK);
525 EQ(sec_receive(&a2, 0, h2b, SEC_HS2_LEN, msg, sizeof msg, &ml), SEC_OK);
526 CHECK(sec_poll_tx(&a1, 0, h3a, sizeof h3a, &n) == SEC_OK && n == SEC_HS3_LEN);
527 CHECK(sec_poll_tx(&a2, 0, h3b, sizeof h3b, &n) == SEC_OK);
528 /* session 2's HS3 spliced into session 1 */
529 EQ(sec_receive(&b1, 0, h3b, SEC_HS3_LEN, msg, sizeof msg, &ml), SEC_ERR_BAD_SIG);
530 /* a different HS1 to a responder already in a handshake */
531 EQ(sec_receive(&b1, 0, h1b, SEC_HS1_LEN, msg, sizeof msg, &ml), SEC_ERR_REFUSED);
532 /* the exact HS1 again: answered with the stored HS2 */
533 EQ(sec_receive(&b1, 0, h1a, SEC_HS1_LEN, msg, sizeof msg, &ml), SEC_OK);
534 uint8_t again[SEC_HS2_LEN];
535 CHECK(sec_poll_tx(&b1, 1, again, sizeof again, &n) == SEC_OK && n == SEC_HS2_LEN &&
536 memcmp(again, h2a, SEC_HS2_LEN) == 0);
537 EQ(sec_receive(&b1, 0, h3a, SEC_HS3_LEN, msg, sizeof msg, &ml), SEC_OK);
538 EQ(sec_receive(&b2, 0, h3b, SEC_HS3_LEN, msg, sizeof msg, &ml), SEC_OK);
539 CHECK(b1.state == SEC_ST_ESTABLISHED && b2.state == SEC_ST_ESTABLISHED);
540 CHECK(memcmp(a1.session_id, b1.session_id, 8) == 0 && memcmp(a1.session_id, a2.session_id, 8) != 0);
541 /* a record of session 2 delivered into session 1 */
542 uint8_t rec[SEC_MAX_DATAGRAM], got[SEC_MAX_PAYLOAD];
543 CHECK(sec_seal(&a2, (const uint8_t *)"x", 1, rec, sizeof rec, &n) == SEC_OK);
544 EQ(sec_receive(&b1, 0, rec, n, got, sizeof got, &ml), SEC_ERR_REFUSED);
545 /* session 2's record with session 1's id patched in: header is authenticated */
546 memcpy(rec + 8, a1.session_id, 8);
547 EQ(sec_receive(&b1, 0, rec, n, got, sizeof got, &ml), SEC_ERR_BAD_TAG);
548 /* an old HS2 replayed to a fresh initiator */
549 sec_endpoint a3, b3;
550 make_pair(&a3, &b3, 1u << 16, 3);
551 CHECK(sec_poll_tx(&a3, 0, h1b, sizeof h1b, &n) == SEC_OK);
552 EQ(sec_receive(&a3, 0, h2a, SEC_HS2_LEN, msg, sizeof msg, &ml), SEC_ERR_BAD_SIG);
553 /* a duplicate HS3 after establishment gets a fresh CONFIRM; a different one is refused */
554 EQ(sec_receive(&b1, 0, h3a, SEC_HS3_LEN, msg, sizeof msg, &ml), SEC_OK);
555 CHECK(sec_poll_tx(&b1, 2, rec, sizeof rec, &n) == SEC_OK && n == SEC_REC_OVERHEAD);
556 EQ(sec_receive(&b1, 0, h3b, SEC_HS3_LEN, msg, sizeof msg, &ml), SEC_ERR_REFUSED);
557}
558
559/* Build an HS2 from a chosen ephemeral share, signed with B's TEST key the
560 * way an honest-but-malicious responder would. */
561static void forge_hs2(uint8_t h2[SEC_HS2_LEN], const uint8_t hs1[SEC_HS1_LEN], const uint8_t eph[32])
562{
563 memset(h2, 0, SEC_HS2_LEN);
564 memcpy(h2, "ASC1", 4);
565 h2[4] = 1; h2[5] = SEC_TYPE_HS2;
566 fill(h2 + 8, 32);
567 memcpy(h2 + 40, eph, 32);
568 memcpy(h2 + 72, PK_B, 32);
569 sha256_ctx c;
570 uint8_t th[32], sm[52];
571 sha256_init(&c);
572 sha256_update(&c, (const uint8_t *)"AIENOS-M6B-v1 transcript", 24);
573 sha256_update(&c, hs1, SEC_HS1_LEN);
574 sha256_update(&c, h2, 104);
575 sha256_final(&c, th);
576 memcpy(sm, "AIENOS-M6B-v1 sig r", 20);
577 memcpy(sm + 20, th, 32);
578 aienos_ed25519_sign(h2 + 104, sm, sizeof sm, SK_B);
579}
580
581static void test_weak_keys(void)
582{
583 static const uint8_t ZERO[32] = {0};
584 static const uint8_t ONE[32] = {1};
585 sec_endpoint a, b;
586 uint8_t h1[SEC_HS1_LEN], h2[SEC_HS2_LEN], msg[8];
587 size_t n, ml;
588 /* small-order initiator share */
589 make_pair(&a, &b, 1u << 16, 3);
590 CHECK(sec_poll_tx(&a, 0, h1, sizeof h1, &n) == SEC_OK);
591 memcpy(h1 + 40, ONE, 32);
592 EQ(sec_receive(&b, 0, h1, SEC_HS1_LEN, msg, sizeof msg, &ml), SEC_ERR_WEAK_KEY);
593 EQ(b.state, SEC_ST_START);
594 /* small-order responder share with a valid signature (forged by B's own key) */
595 make_pair(&a, &b, 1u << 16, 3);
596 CHECK(sec_poll_tx(&a, 0, h1, sizeof h1, &n) == SEC_OK);
597 forge_hs2(h2, h1, ZERO);
598 EQ(sec_receive(&a, 0, h2, SEC_HS2_LEN, msg, sizeof msg, &ml), SEC_ERR_WEAK_KEY);
599 EQ(a.state, SEC_ST_WAIT_HS2);
600 /* control: same forging path with an honest share reaches the MAC check */
601 uint8_t s[32], p[32];
602 fill(s, 32);
603 aienos_x25519_base(p, s);
604 forge_hs2(h2, h1, p);
605 EQ(sec_receive(&a, 0, h2, SEC_HS2_LEN, msg, sizeof msg, &ml), SEC_ERR_BAD_TAG);
606}
607
608/* Two established endpoints, direct (no wire). */
609static void pair_up(sec_endpoint *a, sec_endpoint *b, uint64_t rekey, uint64_t max_records)
610{
611 sec_config ca, cb;
612 cfg_for(&ca, 0, SK_A, PK_B);
613 cfg_for(&cb, 1, SK_B, PK_A);
614 ca.rekey_interval = cb.rekey_interval = rekey;
615 ca.max_records = cb.max_records = max_records;
616 CHECK(sec_init(a, &ca) == SEC_OK && sec_init(b, &cb) == SEC_OK);
617 uint8_t d[SEC_MAX_DATAGRAM], msg[SEC_MAX_PAYLOAD];
618 size_t n, ml;
619 CHECK(sec_poll_tx(a, 0, d, sizeof d, &n) == SEC_OK);
620 CHECK(sec_receive(b, 0, d, n, msg, sizeof msg, &ml) == SEC_OK);
621 CHECK(sec_poll_tx(b, 0, d, sizeof d, &n) == SEC_OK);
622 CHECK(sec_receive(a, 0, d, n, msg, sizeof msg, &ml) == SEC_OK);
623 CHECK(sec_poll_tx(a, 0, d, sizeof d, &n) == SEC_OK);
624 CHECK(sec_receive(b, 0, d, n, msg, sizeof msg, &ml) == SEC_OK);
625 CHECK(sec_poll_tx(b, 0, d, sizeof d, &n) == SEC_OK); /* CONFIRM */
626 CHECK(sec_receive(a, 0, d, n, msg, sizeof msg, &ml) == SEC_OK);
627 CHECK(a->state == SEC_ST_ESTABLISHED && a->confirmed && b->state == SEC_ST_ESTABLISHED);
628}
629
630#define RMAX 200
631static uint8_t recs[RMAX][SEC_MAX_DATAGRAM];
632static size_t rlen[RMAX];
633
634static void seal_n(sec_endpoint *tx, int count)
635{
636 for (int i = 0; i < count; i++) {
637 uint8_t m[8];
638 for (int k = 0; k < 8; k++) m[k] = (uint8_t)(i + k);
639 CHECK(sec_seal(tx, m, sizeof m, recs[i], sizeof recs[i], &rlen[i]) == SEC_OK);
640 }
641}
642
643static int deliver(sec_endpoint *rx, int i)
644{
645 uint8_t got[SEC_MAX_PAYLOAD];
646 size_t gl;
647 return sec_receive(rx, 0, recs[i], rlen[i], got, sizeof got, &gl);
648}
649
650static void test_replay_reorder(void)
651{
652 sec_endpoint a, b;
653 pair_up(&a, &b, 1u << 16, (uint64_t)1 << 40);
654 /* a has sent no records yet: seq 0..199 */
655 seal_n(&a, 200);
656 /* in order 0..9, then duplicates */
657 for (int i = 0; i < 10; i++) EQ(deliver(&b, i), SEC_DATA);
658 for (int i = 0; i < 10; i++) EQ(deliver(&b, i), SEC_ERR_REPLAY);
659 /* 74 first, then 73..11 in reverse (inside the window); 10 is outside */
660 EQ(deliver(&b, 74), SEC_DATA);
661 for (int i = 73; i >= 11; i--) EQ(deliver(&b, i), SEC_DATA);
662 EQ(deliver(&b, 10), SEC_ERR_REPLAY); /* 75 - 10 = 65 > 64: too old */
663 EQ(deliver(&b, 74), SEC_ERR_REPLAY);
664 EQ(deliver(&b, 11), SEC_ERR_REPLAY); /* exactly at the window edge, already seen */
665 /* jump ahead, then a record just inside and just outside the window */
666 EQ(deliver(&b, 199), SEC_DATA); /* top = 200 */
667 EQ(deliver(&b, 136), SEC_DATA); /* 200 - 136 = 64: inside, never seen */
668 EQ(deliver(&b, 135), SEC_ERR_REPLAY); /* 65 back: outside */
669 EQ(deliver(&b, 134), SEC_ERR_REPLAY); /* 66 back, never seen; the bit a wrapped shift would hit (198) is unset */
670 EQ(deliver(&b, 70), SEC_ERR_REPLAY); /* 130 back */
671 EQ(deliver(&b, 136), SEC_ERR_REPLAY);
672 /* shuffled order inside the window: every record accepted exactly once */
673 sec_endpoint c, d;
674 pair_up(&c, &d, 1u << 16, (uint64_t)1 << 40);
675 seal_n(&c, 64);
676 int order[64];
677 for (int i = 0; i < 64; i++) order[i] = i;
678 for (int i = 63; i > 0; i--) { int j = rnd8() % (i + 1); int t = order[i]; order[i] = order[j]; order[j] = t; }
679 int ok = 1;
680 for (int i = 0; i < 64; i++) ok &= deliver(&d, order[i]) == SEC_DATA;
681 for (int i = 0; i < 64; i++) ok &= deliver(&d, i) == SEC_ERR_REPLAY;
682 CHECK(ok);
683 EQ(d.c.rx_records, 64);
684}
685
686static void test_mutilation(void)
687{
688 sec_endpoint a, b;
689 pair_up(&a, &b, 1u << 16, (uint64_t)1 << 40);
690 seal_n(&a, 2);
691 uint8_t buf[SEC_MAX_DATAGRAM + 64], got[SEC_MAX_PAYLOAD];
692 size_t gl, n = rlen[0];
693 unsigned long bad = 0, tries = 0, data = 0;
694 /* every truncation */
695 for (size_t l = 0; l < n; l++) {
696 memcpy(buf, recs[0], l);
697 int s = sec_receive(&b, 0, buf, l, got, sizeof got, &gl);
698 tries++; bad += s < 0; data += s == SEC_DATA;
699 if (l >= 8 && l < SEC_REC_OVERHEAD) EQ(s, SEC_ERR_MALFORMED);
700 }
701 /* extensions by 1..64 bytes */
702 for (size_t e = 1; e <= 64; e++) {
703 memcpy(buf, recs[0], n);
704 fill(buf + n, e);
705 int s = sec_receive(&b, 0, buf, n + e, got, sizeof got, &gl);
706 tries++; bad += s < 0; data += s == SEC_DATA;
707 }
708 /* every single-bit flip */
709 for (size_t bit = 0; bit < n * 8; bit++) {
710 memcpy(buf, recs[0], n);
711 buf[bit / 8] ^= (uint8_t)(1u << (bit % 8));
712 int s = sec_receive(&b, 0, buf, n, got, sizeof got, &gl);
713 tries++; bad += s < 0; data += s == SEC_DATA;
714 if (bit / 8 < 8) EQ(s, SEC_ERR_MALFORMED);
715 if (bit / 8 >= SEC_REC_HEADER_LEN) EQ(s, SEC_ERR_BAD_TAG);
716 if (bit / 8 >= 8 && bit / 8 < 16) EQ(s, SEC_ERR_REFUSED);
717 }
718 EQ(bad, tries);
719 EQ(data, 0);
720 /* an oversize datagram is refused on length alone */
721 memset(buf, 0, sizeof buf);
722 memcpy(buf, recs[0], 24);
723 EQ(sec_receive(&b, 0, buf, SEC_MAX_DATAGRAM + 1, got, sizeof got, &gl), SEC_ERR_MALFORMED);
724 /* nothing above changed the receiver: the genuine records are still accepted once */
725 EQ(deliver(&b, 0), SEC_DATA);
726 EQ(deliver(&b, 1), SEC_DATA);
727 EQ(deliver(&b, 0), SEC_ERR_REPLAY);
728 /* handshake messages: every truncation and extension is malformed */
729 sec_endpoint x, y;
730 make_pair(&x, &y, 1u << 16, 3);
731 uint8_t h1[SEC_HS1_LEN + 8] = {0};
732 size_t hn;
733 CHECK(sec_poll_tx(&x, 0, h1, sizeof h1, &hn) == SEC_OK);
734 int ok = 1;
735 for (size_t l = 0; l < SEC_HS1_LEN + 8; l++) {
736 if (l == SEC_HS1_LEN) continue;
737 ok &= sec_receive(&y, 0, h1, l, got, sizeof got, &gl) == SEC_ERR_MALFORMED;
738 }
739 CHECK(ok);
740 EQ(y.state, SEC_ST_START);
741 /* non-zero reserved bytes */
742 h1[6] = 1;
743 EQ(sec_receive(&y, 0, h1, SEC_HS1_LEN, got, sizeof got, &gl), SEC_ERR_MALFORMED);
744 /* records before the handshake: state error, never data */
745 EQ(sec_receive(&y, 0, recs[0], rlen[0], got, sizeof got, &gl), SEC_ERR_STATE);
746}
747
748static void test_rekey(void)
749{
750 const uint64_t R = SEC_MIN_REKEY_INTERVAL;
751 sec_endpoint a, b;
752 pair_up(&a, &b, R, (uint64_t)1 << 40);
753 uint8_t key0_tx[32];
754 memcpy(key0_tx, a.tx_key, 32);
755 sec_endpoint snap = a; /* epoch-0 sender snapshot */
756 uint8_t m[16], d[SEC_MAX_DATAGRAM], got[SEC_MAX_PAYLOAD];
757 size_t n, gl;
758 int ok = 1;
759 /* 10 epochs and a bit; hold back the last two records of each epoch and
760 * deliver them after the first two of the next one (reorder across the
761 * boundary, needs the previous epoch's key) */
762 uint8_t held[2][SEC_MAX_DATAGRAM];
763 size_t heldn[2] = {0, 0};
764 uint64_t total = 10 * R + 5;
765 for (uint64_t i = 0; i < total; i++) {
766 for (int k = 0; k < 16; k++) m[k] = (uint8_t)(i * 7 + k);
767 if (sec_seal(&a, m, sizeof m, d, sizeof d, &n) != SEC_OK) { ok = 0; break; }
768 uint64_t pos = i % R;
769 if (pos >= R - 2) { memcpy(held[pos - (R - 2)], d, n); heldn[pos - (R - 2)] = n; continue; }
770 int s = sec_receive(&b, 0, d, n, got, sizeof got, &gl);
771 ok &= s == SEC_DATA && gl == 16 && memcmp(got, m, 16) == 0;
772 if (pos == 1 && i > R) {
773 for (int h = 0; h < 2; h++)
774 ok &= sec_receive(&b, 0, held[h], heldn[h], got, sizeof got, &gl) == SEC_DATA;
775 }
776 }
777 CHECK(ok);
778 EQ(a.tx_epoch, 10);
779 EQ(a.c.rekeys_tx, 10);
780 EQ(b.rx_epoch, 10);
781 EQ(b.c.rekeys_rx, 10);
782 EQ(b.c.rx_records, total); /* every held pair delivered after the next epoch began */
783 CHECK(memcmp(a.tx_key, b.rx_key, 32) == 0);
784 /* forward secrecy of old epochs: the epoch-0 key is gone from both sides */
785 CHECK(!contains(&a, sizeof a, key0_tx, 32));
786 CHECK(!contains(&b, sizeof b, key0_tx, 32));
787 /* the ratchet is what the spec says: key[e+1] = Expand(key[e], "rekey") */
788 uint8_t k[32];
789 memcpy(k, key0_tx, 32);
790 for (int e = 0; e < 10; e++) sec_hkdf_expand1(k, k, (const uint8_t *)"rekey", 5);
791 CHECK(memcmp(k, a.tx_key, 32) == 0);
792 /* a record for the current epoch sealed with an old key is refused */
793 snap.tx_seq = 10 * R + 50;
794 snap.tx_epoch = 10 * R; /* stop the snapshot from ratcheting: it keeps key 0 */
795 CHECK(sec_seal(&snap, m, sizeof m, d, sizeof d, &n) == SEC_OK);
796 EQ(sec_receive(&b, 0, d, n, got, sizeof got, &gl), SEC_ERR_BAD_TAG);
797 /* jumping ahead up to SEC_MAX_EPOCH_SKIP epochs is accepted, further is refused */
798 sec_endpoint ahead = a;
799 ahead.tx_seq = (10 + SEC_MAX_EPOCH_SKIP + 1) * R;
800 CHECK(sec_seal(&ahead, m, sizeof m, d, sizeof d, &n) == SEC_OK);
801 EQ(sec_receive(&b, 0, d, n, got, sizeof got, &gl), SEC_ERR_REPLAY);
802 ahead = a;
803 ahead.tx_seq = (10 + SEC_MAX_EPOCH_SKIP) * R + 3;
804 CHECK(sec_seal(&ahead, m, sizeof m, d, sizeof d, &n) == SEC_OK);
805 EQ(sec_receive(&b, 0, d, n, got, sizeof got, &gl), SEC_DATA);
806 EQ(b.rx_epoch, 10 + SEC_MAX_EPOCH_SKIP);
807 /* max_records: the sender stops, the receiver refuses beyond it */
808 sec_endpoint c, e;
809 pair_up(&c, &e, R, 5);
810 ok = 1;
811 for (int i = 0; i < 4; i++) ok &= sec_seal(&c, m, 4, d, sizeof d, &n) == SEC_OK; /* CONFIRM used seq 0 of e's side only */
812 CHECK(ok);
813 CHECK(sec_seal(&c, m, 4, d, sizeof d, &n) == SEC_OK);
814 EQ(sec_seal(&c, m, 4, d, sizeof d, &n), SEC_ERR_EXHAUSTED);
815 sec_endpoint over = c;
816 over.max_records = 100;
817 CHECK(sec_seal(&over, m, 4, d, sizeof d, &n) == SEC_OK);
818 EQ(sec_receive(&e, 0, d, n, got, sizeof got, &gl), SEC_ERR_EXHAUSTED);
819 /* config limits */
820 sec_config bad;
821 cfg_for(&bad, 0, SK_A, PK_B);
822 bad.rekey_interval = SEC_MIN_REKEY_INTERVAL - 1;
823 EQ(sec_init(&c, &bad), SEC_ERR_ARG);
824}
825
826static void test_close(void)
827{
828 sec_endpoint a, b;
829 pair_up(&a, &b, 1u << 16, (uint64_t)1 << 40);
830 uint8_t d[SEC_MAX_DATAGRAM], d2[SEC_MAX_DATAGRAM], got[SEC_MAX_PAYLOAD];
831 size_t n, n2, gl;
832 seal_n(&a, 1);
833 /* too small an output buffer: refused before anything changes (Gemini review finding) */
834 uint64_t seq0 = a.tx_seq;
835 EQ(sec_close(&a, 10, d, SEC_REC_OVERHEAD - 1, &n), SEC_ERR_CAPACITY);
836 CHECK(a.tx_seq == seq0 && a.state == SEC_ST_ESTABLISHED && !all_zero(a.tx_key, 32));
837 { /* unconfirmed initiator: its stored HS3 resend must survive a refused close */
838 sec_endpoint u, v;
839 uint8_t h[SEC_MAX_DATAGRAM], h3[SEC_HS3_LEN];
840 size_t hn, ml;
841 make_pair(&u, &v, 1u << 16, 3);
842 CHECK(sec_poll_tx(&u, 0, h, sizeof h, &hn) == SEC_OK);
843 CHECK(sec_receive(&v, 0, h, hn, got, sizeof got, &ml) == SEC_OK);
844 CHECK(sec_poll_tx(&v, 0, h, sizeof h, &hn) == SEC_OK);
845 CHECK(sec_receive(&u, 0, h, hn, got, sizeof got, &ml) == SEC_OK);
846 memcpy(h3, u.hs3, SEC_HS3_LEN);
847 EQ(sec_close(&u, 0, h, 20, &hn), SEC_ERR_CAPACITY);
848 CHECK(sec_poll_tx(&u, 0, h, sizeof h, &hn) == SEC_OK && hn == SEC_HS3_LEN && memcmp(h, h3, SEC_HS3_LEN) == 0);
849 EQ(u.tx_seq, 0);
850 }
851 CHECK(sec_close(&a, 10, d, sizeof d, &n) == SEC_OK && n == SEC_REC_OVERHEAD);
852 EQ(a.state, SEC_ST_CLOSED);
853 CHECK(all_zero(a.tx_key, 32) && all_zero(a.rx_key, 32) && all_zero(a.rx_prev_key, 32));
854 EQ(sec_seal(&a, got, 1, d2, sizeof d2, &n2), SEC_ERR_CLOSED);
855 /* the close datagram is resent max_retries times, then never again */
856 int resends = 0;
857 for (uint64_t t = 11; t < 200; t++) {
858 sec_status s = sec_poll_tx(&a, t, d2, sizeof d2, &n2);
859 if (s == SEC_OK) { resends++; CHECK(n2 == n && memcmp(d, d2, n) == 0); }
860 }
861 EQ(resends, 6);
862 /* data before close is still delivered; close is authenticated */
863 EQ(deliver(&b, 0), SEC_DATA);
864 uint8_t forged[SEC_MAX_DATAGRAM];
865 memcpy(forged, d, n);
866 forged[n - 1] ^= 1;
867 EQ(sec_receive(&b, 0, forged, n, got, sizeof got, &gl), SEC_ERR_BAD_TAG);
868 EQ(b.state, SEC_ST_ESTABLISHED);
869 EQ(sec_receive(&b, 0, d, n, got, sizeof got, &gl), SEC_PEER_CLOSED);
870 EQ(b.state, SEC_ST_CLOSED);
871 CHECK(all_zero(b.tx_key, 32) && all_zero(b.rx_key, 32));
872 EQ(sec_receive(&b, 0, d, n, got, sizeof got, &gl), SEC_ERR_CLOSED);
873 EQ(sec_seal(&b, got, 1, d2, sizeof d2, &n2), SEC_ERR_CLOSED);
874 EQ(sec_close(&b, 0, d2, sizeof d2, &n2), SEC_ERR_STATE);
875}
876
877static void test_fuzz(void)
878{
879 sec_endpoint a, b, x, y;
880 pair_up(&a, &b, SEC_MIN_REKEY_INTERVAL, (uint64_t)1 << 40);
881 make_pair(&x, &y, 1u << 16, 3);
882 uint8_t buf[SEC_MAX_DATAGRAM + 16], got[SEC_MAX_PAYLOAD];
883 size_t gl;
884 unsigned long data = 0;
885 prng_s = 0xf022;
886 for (int i = 0; i < 100000; i++) {
887 size_t n = rnd8() % 4 == 0 ? (size_t)(rnd8() % 48) : (size_t)((rnd8() << 3 | rnd8()) % sizeof buf);
888 fill(buf, n);
889 if (n >= 8 && rnd8() % 2) { memcpy(buf, "ASC1", 4); buf[4] = 1; buf[5] = (uint8_t)(rnd8() % 2 ? 0x10 : rnd8() % 4); }
890 if (n >= 16 && buf[5] == 0x10 && rnd8() % 2) { memcpy(buf + 8, b.session_id, 8); buf[6] = 0; buf[7] = 0; }
891 data += sec_receive(&b, 0, buf, n, got, sizeof got, &gl) == SEC_DATA;
892 data += sec_receive(&y, 0, buf, n, got, sizeof got, &gl) == SEC_DATA;
893 }
894 EQ(data, 0);
895 EQ(y.state, SEC_ST_START);
896 EQ(b.c.rx_records, 0);
897 /* the endpoints still work */
898 seal_n(&a, 1);
899 EQ(deliver(&b, 0), SEC_DATA);
900}
901
902static void test_args(void)
903{
904 sec_endpoint a;
905 sec_config c;
906 uint8_t d[8];
907 size_t n;
908 cfg_for(&c, 0, SK_A, PK_B);
909 EQ(sec_init(NULL, &c), SEC_ERR_ARG);
910 c.role = 2;
911 EQ(sec_init(&a, &c), SEC_ERR_ARG);
912 c.role = 0; c.rto = 0;
913 EQ(sec_init(&a, &c), SEC_ERR_ARG);
914 c.rto = 1; c.max_records = 0;
915 EQ(sec_init(&a, &c), SEC_ERR_ARG);
916 c.max_records = 10;
917 EQ(sec_init(&a, &c), SEC_OK);
918 EQ(sec_poll_tx(&a, 0, d, sizeof d, &n), SEC_ERR_CAPACITY);
919 EQ(sec_seal(&a, d, 1, d, sizeof d, &n), SEC_ERR_STATE);
920 EQ(sec_close(&a, 0, d, sizeof d, &n), SEC_ERR_STATE);
921 EQ(sec_receive(&a, 0, NULL, 4, d, sizeof d, &n), SEC_ERR_ARG);
922 uint8_t sk[32], pk[32];
923 EQ(sec_test_identity(sk, pk, ""), SEC_ERR_ARG);
924 CHECK(strcmp(sec_status_name(SEC_ERR_WEAK_KEY), "WeakKey") == 0);
925}
926
927int main(void)
928{
929 CHECK(sec_test_identity(SK_A, PK_A, "lane16-machine-a") == SEC_OK);
930 CHECK(sec_test_identity(SK_B, PK_B, "lane16-machine-b") == SEC_OK);
931 CHECK(sec_test_identity(SK_M, PK_M, "lane16-mallory") == SEC_OK);
932 struct { const char *name; void (*fn)(void); } T[] = {
933 {"hkdf_rfc5869", test_hkdf},
934 {"handshake_and_data", test_handshake_and_data},
935 {"wrong_identity", test_wrong_identity},
936 {"impersonation", test_impersonation},
937 {"transcript_tamper", test_transcript_tamper},
938 {"splice", test_splice},
939 {"weak_keys", test_weak_keys},
940 {"replay_reorder", test_replay_reorder},
941 {"mutilation", test_mutilation},
942 {"rekey", test_rekey},
943 {"close", test_close},
944 {"loss_recovery", test_loss},
945 {"fuzz", test_fuzz},
946 {"args", test_args},
947 };
948 for (size_t i = 0; i < sizeof T / sizeof T[0]; i++) {
949 unsigned long f0 = failures, c0 = checks;
950 T[i].fn();
951 printf(" %-20s %s (%lu checks)\n", T[i].name, failures == f0 ? "PASS" : "FAIL", checks - c0);
952 }
953 printf("AIENOS_NET_SECURE_TESTS: %s (%lu checks, %lu failures)\n", failures ? "FAIL" : "PASS",
954 checks, failures);
955 return failures ? 1 : 0;
956}