aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / net / aienos_sec.c
| 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 | /* AIENOS M6-B secure control transport. See aienos_sec.h for the wire spec. |
| 18 | * No heap, clock or I/O. Lines tagged GUARD:<name> are removed one at a time |
| 19 | * by `make mutants`; the tests must catch every removal. |
| 20 | */ |
| 21 | #include "aienos_sec.h" |
| 22 | |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "../argus/sha256.h" |
| 26 | #include "../crypto/aienos_crypto.h" |
| 27 | #include "../sig/aienos_sig.h" |
| 28 | #include "x25519.h" |
| 29 | |
| 30 | static const uint8_t SEC_MAGIC[4] = {'A', 'S', 'C', '1'}; |
| 31 | static const char TH_LABEL[] = "AIENOS-M6B-v1 transcript"; |
| 32 | static const char SALT_LABEL[] = "AIENOS-M6B-v1 hkdf salt"; |
| 33 | static const char SIG_R_LABEL[] = "AIENOS-M6B-v1 sig r"; /* signed with its NUL */ |
| 34 | static const char SIG_I_LABEL[] = "AIENOS-M6B-v1 sig i"; |
| 35 | static const char TEST_ID_LABEL[] = "AIENOS-M6B-TEST-IDENTITY/NOT-FOR-PRODUCTION/"; |
| 36 | |
| 37 | static void wr64(uint8_t *b, uint64_t v) { for (int i = 7; i >= 0; i--) { b[i] = (uint8_t)v; v >>= 8; } } |
| 38 | static uint64_t rd64(const uint8_t *b) |
| 39 | { |
| 40 | uint64_t v = 0; |
| 41 | for (int i = 0; i < 8; i++) v = v << 8 | b[i]; |
| 42 | return v; |
| 43 | } |
| 44 | |
| 45 | const char *sec_status_name(sec_status s) |
| 46 | { |
| 47 | switch (s) { |
| 48 | case SEC_OK: return "Ok"; |
| 49 | case SEC_DATA: return "Data"; |
| 50 | case SEC_IDLE: return "Idle"; |
| 51 | case SEC_PEER_CLOSED: return "PeerClosed"; |
| 52 | case SEC_ERR_ARG: return "Arg"; |
| 53 | case SEC_ERR_MALFORMED: return "Malformed"; |
| 54 | case SEC_ERR_BAD_TAG: return "BadTag"; |
| 55 | case SEC_ERR_REFUSED: return "Refused"; |
| 56 | case SEC_ERR_IDENTITY: return "Identity"; |
| 57 | case SEC_ERR_BAD_SIG: return "BadSig"; |
| 58 | case SEC_ERR_REPLAY: return "Replay"; |
| 59 | case SEC_ERR_CAPACITY: return "Capacity"; |
| 60 | case SEC_ERR_TOO_LARGE: return "TooLarge"; |
| 61 | case SEC_ERR_STATE: return "State"; |
| 62 | case SEC_ERR_LINK_DOWN: return "LinkDown"; |
| 63 | case SEC_ERR_EXHAUSTED: return "Exhausted"; |
| 64 | case SEC_ERR_CLOSED: return "Closed"; |
| 65 | case SEC_ERR_WEAK_KEY: return "WeakKey"; |
| 66 | } |
| 67 | return "Unknown"; |
| 68 | } |
| 69 | |
| 70 | /* ---- HKDF-SHA256 (RFC 5869) with 32-byte keys, one output block ---- */ |
| 71 | |
| 72 | void sec_hkdf_extract(uint8_t prk[32], const uint8_t salt[32], const uint8_t *ikm, size_t len) |
| 73 | { |
| 74 | aienos_hmac_sha256(salt, ikm, len, prk); |
| 75 | } |
| 76 | |
| 77 | void sec_hkdf_expand1(uint8_t out[32], const uint8_t prk[32], const uint8_t *info, size_t len) |
| 78 | { |
| 79 | static const uint8_t one = 1; |
| 80 | aienos_hmac_sha256_ctx c; |
| 81 | aienos_hmac_sha256_init(&c, prk); |
| 82 | aienos_hmac_sha256_update(&c, info, len); |
| 83 | aienos_hmac_sha256_update(&c, &one, 1); |
| 84 | aienos_hmac_sha256_final(&c, out); |
| 85 | } |
| 86 | |
| 87 | /* Expand(key, label | th), label without its NUL. */ |
| 88 | static void expand_label(uint8_t out[32], const uint8_t key[32], const char *label, |
| 89 | const uint8_t *th, size_t th_len) |
| 90 | { |
| 91 | static const uint8_t one = 1; |
| 92 | aienos_hmac_sha256_ctx c; |
| 93 | aienos_hmac_sha256_init(&c, key); |
| 94 | aienos_hmac_sha256_update(&c, (const uint8_t *)label, strlen(label)); |
| 95 | if (th_len) aienos_hmac_sha256_update(&c, th, th_len); |
| 96 | aienos_hmac_sha256_update(&c, &one, 1); |
| 97 | aienos_hmac_sha256_final(&c, out); |
| 98 | } |
| 99 | |
| 100 | /* TH = SHA-256(label | a | b | c) */ |
| 101 | static void transcript(uint8_t th[32], const uint8_t *a, size_t al, const uint8_t *b, size_t bl, |
| 102 | const uint8_t *c, size_t cl) |
| 103 | { |
| 104 | sha256_ctx h; |
| 105 | sha256_init(&h); |
| 106 | sha256_update(&h, (const uint8_t *)TH_LABEL, sizeof TH_LABEL - 1); |
| 107 | sha256_update(&h, a, al); |
| 108 | if (bl) sha256_update(&h, b, bl); |
| 109 | if (cl) sha256_update(&h, c, cl); |
| 110 | sha256_final(&h, th); |
| 111 | } |
| 112 | |
| 113 | /* Signed message: label | NUL | th (fixed 52 bytes). */ |
| 114 | static void sig_msg(uint8_t m[52], const char *label, const uint8_t th[32]) |
| 115 | { |
| 116 | memcpy(m, label, 20); /* both labels are 19 chars + NUL */ |
| 117 | memcpy(m + 20, th, 32); |
| 118 | } |
| 119 | |
| 120 | static void hs_header(uint8_t *b, uint8_t type) |
| 121 | { |
| 122 | memcpy(b, SEC_MAGIC, 4); |
| 123 | b[4] = SEC_VERSION; |
| 124 | b[5] = type; |
| 125 | b[6] = 0; |
| 126 | b[7] = 0; |
| 127 | } |
| 128 | |
| 129 | static void wipe_secrets(sec_endpoint *ep) |
| 130 | { |
| 131 | aienos_wipe(ep->eph_sk, sizeof ep->eph_sk); |
| 132 | aienos_wipe(ep->fin_key_i, sizeof ep->fin_key_i); |
| 133 | aienos_wipe(ep->prk, sizeof ep->prk); |
| 134 | aienos_wipe(ep->tx_key, sizeof ep->tx_key); |
| 135 | aienos_wipe(ep->rx_key, sizeof ep->rx_key); |
| 136 | aienos_wipe(ep->rx_prev_key, sizeof ep->rx_prev_key); |
| 137 | } |
| 138 | |
| 139 | static void go_down(sec_endpoint *ep) |
| 140 | { |
| 141 | wipe_secrets(ep); |
| 142 | aienos_wipe(ep->id_sk, sizeof ep->id_sk); |
| 143 | ep->state = SEC_ST_DOWN; |
| 144 | } |
| 145 | |
| 146 | sec_status sec_test_identity(uint8_t sk[32], uint8_t pk[32], const char *label) |
| 147 | { |
| 148 | if (!sk || !pk || !label) return SEC_ERR_ARG; |
| 149 | size_t n = strnlen(label, 65); |
| 150 | if (n == 0 || n > 64) return SEC_ERR_ARG; |
| 151 | sha256_ctx c; |
| 152 | sha256_init(&c); |
| 153 | sha256_update(&c, (const uint8_t *)TEST_ID_LABEL, sizeof TEST_ID_LABEL - 1); |
| 154 | sha256_update(&c, (const uint8_t *)label, n); |
| 155 | sha256_final(&c, sk); |
| 156 | return aienos_ed25519_public_key(pk, sk) == AIENOS_SIG_OK ? SEC_OK : SEC_ERR_ARG; |
| 157 | } |
| 158 | |
| 159 | sec_status sec_init(sec_endpoint *ep, const sec_config *cfg) |
| 160 | { |
| 161 | if (!ep || !cfg || cfg->role > 1 || cfg->rto == 0 || cfg->max_records == 0 || |
| 162 | cfg->rekey_interval < SEC_MIN_REKEY_INTERVAL) |
| 163 | return SEC_ERR_ARG; |
| 164 | memset(ep, 0, sizeof *ep); |
| 165 | ep->role = cfg->role; |
| 166 | memcpy(ep->id_sk, cfg->id_sk, 32); |
| 167 | if (aienos_ed25519_public_key(ep->id_pk, ep->id_sk) != AIENOS_SIG_OK) { |
| 168 | aienos_wipe(ep, sizeof *ep); |
| 169 | return SEC_ERR_ARG; |
| 170 | } |
| 171 | memcpy(ep->peer_pk, cfg->peer_pk, 32); |
| 172 | memcpy(ep->eph_sk, cfg->entropy, 32); |
| 173 | memcpy(ep->random, cfg->entropy + 32, 32); |
| 174 | aienos_x25519_base(ep->eph_pk, ep->eph_sk); |
| 175 | ep->rto = cfg->rto; |
| 176 | ep->max_retries = cfg->max_retries; |
| 177 | ep->rekey_interval = cfg->rekey_interval; |
| 178 | ep->max_records = cfg->max_records; |
| 179 | ep->state = SEC_ST_START; |
| 180 | return SEC_OK; |
| 181 | } |
| 182 | |
| 183 | /* Derive record keys and session id from PRK and TH_f; wipe PRK. */ |
| 184 | static void install_keys(sec_endpoint *ep, const uint8_t th_f[32]) |
| 185 | { |
| 186 | uint8_t master[32], sid[32]; |
| 187 | expand_label(master, ep->prk, "master", th_f, 32); |
| 188 | if (ep->role == 0) { |
| 189 | expand_label(ep->tx_key, master, "i2r", NULL, 0); |
| 190 | expand_label(ep->rx_key, master, "r2i", NULL, 0); |
| 191 | } else { |
| 192 | expand_label(ep->tx_key, master, "r2i", NULL, 0); |
| 193 | expand_label(ep->rx_key, master, "i2r", NULL, 0); |
| 194 | } |
| 195 | expand_label(sid, master, "session", NULL, 0); |
| 196 | memcpy(ep->session_id, sid, 8); |
| 197 | aienos_wipe(master, sizeof master); |
| 198 | aienos_wipe(ep->prk, sizeof ep->prk); |
| 199 | aienos_wipe(ep->fin_key_i, sizeof ep->fin_key_i); |
| 200 | aienos_wipe(ep->eph_sk, sizeof ep->eph_sk); |
| 201 | ep->tx_epoch = ep->tx_seq = 0; |
| 202 | ep->rx_epoch = ep->rx_top = ep->rx_bitmap = 0; |
| 203 | ep->rx_has_prev = 0; |
| 204 | ep->state = SEC_ST_ESTABLISHED; |
| 205 | } |
| 206 | |
| 207 | /* dh -> PRK. Returns 0, or -1 for a small-order peer share. */ |
| 208 | static int derive_prk(sec_endpoint *ep, const uint8_t peer_eph[32]) |
| 209 | { |
| 210 | uint8_t dh[32], salt[32]; |
| 211 | int r = aienos_x25519(dh, ep->eph_sk, peer_eph); |
| 212 | if (r != 0) { |
| 213 | aienos_wipe(dh, sizeof dh); |
| 214 | return -1; /* GUARD:weak-dh */ |
| 215 | } |
| 216 | sha256_hash((const uint8_t *)SALT_LABEL, sizeof SALT_LABEL - 1, salt); |
| 217 | sec_hkdf_extract(ep->prk, salt, dh, sizeof dh); |
| 218 | aienos_wipe(dh, sizeof dh); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static sec_status build_record(sec_endpoint *ep, uint8_t inner, const uint8_t *msg, size_t len, |
| 223 | uint8_t *out, size_t cap, size_t *written) |
| 224 | { |
| 225 | if (len > SEC_MAX_PAYLOAD) return SEC_ERR_TOO_LARGE; |
| 226 | size_t total = SEC_REC_OVERHEAD + len; |
| 227 | if (cap < total) return SEC_ERR_CAPACITY; |
| 228 | if (ep->tx_seq >= ep->max_records) return SEC_ERR_EXHAUSTED; /* GUARD:max-records */ |
| 229 | uint64_t epoch = ep->tx_seq / ep->rekey_interval; |
| 230 | while (ep->tx_epoch < epoch) { /* one step per epoch; old key wiped */ |
| 231 | uint8_t next[32]; |
| 232 | expand_label(next, ep->tx_key, "rekey", NULL, 0); |
| 233 | memcpy(ep->tx_key, next, 32); /* GUARD:rekey-tx */ |
| 234 | aienos_wipe(next, sizeof next); |
| 235 | ep->tx_epoch++; |
| 236 | ep->c.rekeys_tx++; |
| 237 | } |
| 238 | uint8_t dir = ep->role; |
| 239 | memcpy(out, SEC_MAGIC, 4); |
| 240 | out[4] = SEC_VERSION; |
| 241 | out[5] = SEC_TYPE_RECORD; |
| 242 | out[6] = dir; |
| 243 | out[7] = 0; |
| 244 | memcpy(out + 8, ep->session_id, 8); |
| 245 | wr64(out + 16, ep->tx_seq); |
| 246 | uint8_t nonce[12] = {dir, 0, 0, 0}; |
| 247 | wr64(nonce + 4, ep->tx_seq); |
| 248 | out[SEC_REC_HEADER_LEN] = inner; |
| 249 | if (len) memmove(out + SEC_REC_HEADER_LEN + 1, msg, len); |
| 250 | if (aienos_gcmsiv_seal(ep->tx_key, nonce, out, SEC_REC_HEADER_LEN, out + SEC_REC_HEADER_LEN, |
| 251 | 1 + len, out + SEC_REC_HEADER_LEN, 1 + len + SEC_TAG_LEN) != AIENOS_CRYPTO_OK) |
| 252 | return SEC_ERR_ARG; |
| 253 | ep->tx_seq++; |
| 254 | ep->c.tx_records++; |
| 255 | *written = total; |
| 256 | return SEC_OK; |
| 257 | } |
| 258 | |
| 259 | sec_status sec_seal(sec_endpoint *ep, const uint8_t *msg, size_t len, uint8_t *out, size_t cap, |
| 260 | size_t *written) |
| 261 | { |
| 262 | if (!ep || (!msg && len) || !out || !written) return SEC_ERR_ARG; |
| 263 | *written = 0; |
| 264 | if (ep->state == SEC_ST_CLOSED) return SEC_ERR_CLOSED; |
| 265 | if (ep->state == SEC_ST_DOWN) return SEC_ERR_LINK_DOWN; |
| 266 | if (ep->state != SEC_ST_ESTABLISHED) return SEC_ERR_STATE; |
| 267 | return build_record(ep, SEC_INNER_DATA, msg, len, out, cap, written); |
| 268 | } |
| 269 | |
| 270 | static sec_status emit_flight(sec_endpoint *ep, uint64_t now, uint8_t *out, size_t cap, |
| 271 | size_t *written) |
| 272 | { |
| 273 | if (cap < ep->flight_len) return SEC_ERR_CAPACITY; |
| 274 | memcpy(out, ep->flight, ep->flight_len); |
| 275 | *written = ep->flight_len; |
| 276 | ep->last_tx = now; |
| 277 | ep->c.tx_hs++; |
| 278 | return SEC_OK; |
| 279 | } |
| 280 | |
| 281 | sec_status sec_poll_tx(sec_endpoint *ep, uint64_t now, uint8_t *out, size_t cap, size_t *written) |
| 282 | { |
| 283 | if (!ep || !out || !written) return SEC_ERR_ARG; |
| 284 | *written = 0; |
| 285 | if (ep->state == SEC_ST_DOWN) return SEC_ERR_LINK_DOWN; |
| 286 | if (ep->role == 0 && ep->state == SEC_ST_START) { |
| 287 | if (cap < SEC_HS1_LEN) return SEC_ERR_CAPACITY; |
| 288 | hs_header(ep->hs1, SEC_TYPE_HS1); |
| 289 | memcpy(ep->hs1 + 8, ep->random, 32); |
| 290 | memcpy(ep->hs1 + 40, ep->eph_pk, 32); |
| 291 | memcpy(ep->flight, ep->hs1, SEC_HS1_LEN); |
| 292 | ep->flight_len = SEC_HS1_LEN; |
| 293 | ep->state = SEC_ST_WAIT_HS2; |
| 294 | ep->tries = 0; |
| 295 | return emit_flight(ep, now, out, cap, written); |
| 296 | } |
| 297 | if (ep->flight_pending) { |
| 298 | sec_status s = emit_flight(ep, now, out, cap, written); |
| 299 | if (s == SEC_OK) ep->flight_pending = 0; |
| 300 | return s; |
| 301 | } |
| 302 | if (ep->confirm_pending && ep->state == SEC_ST_ESTABLISHED) { |
| 303 | sec_status s = build_record(ep, SEC_INNER_CONFIRM, NULL, 0, out, cap, written); |
| 304 | if (s == SEC_OK) ep->confirm_pending = 0; |
| 305 | return s; |
| 306 | } |
| 307 | int resend = (ep->role == 0 && ep->state == SEC_ST_WAIT_HS2) || |
| 308 | (ep->role == 0 && ep->state == SEC_ST_ESTABLISHED && !ep->confirmed) || |
| 309 | (ep->state == SEC_ST_CLOSED && ep->close_sent); |
| 310 | if (resend && now >= ep->last_tx && now - ep->last_tx >= ep->rto) { |
| 311 | if (ep->tries >= ep->max_retries) { |
| 312 | if (ep->state == SEC_ST_CLOSED) { ep->close_sent = 0; return SEC_IDLE; } |
| 313 | go_down(ep); |
| 314 | return SEC_ERR_LINK_DOWN; |
| 315 | } |
| 316 | sec_status s = emit_flight(ep, now, out, cap, written); |
| 317 | if (s == SEC_OK) { ep->tries++; ep->c.tx_hs_resend++; } |
| 318 | return s; |
| 319 | } |
| 320 | return SEC_IDLE; |
| 321 | } |
| 322 | |
| 323 | sec_status sec_close(sec_endpoint *ep, uint64_t now, uint8_t *out, size_t cap, size_t *written) |
| 324 | { |
| 325 | if (!ep || !out || !written) return SEC_ERR_ARG; |
| 326 | *written = 0; |
| 327 | if (ep->state != SEC_ST_ESTABLISHED) return SEC_ERR_STATE; |
| 328 | if (cap < SEC_REC_OVERHEAD) return SEC_ERR_CAPACITY; /* before anything changes */ |
| 329 | size_t n = 0; |
| 330 | sec_status s = build_record(ep, SEC_INNER_CLOSE, NULL, 0, ep->flight, sizeof ep->flight, &n); |
| 331 | if (s != SEC_OK) return s; |
| 332 | ep->flight_len = (uint16_t)n; |
| 333 | wipe_secrets(ep); /* GUARD:close-wipe */ |
| 334 | ep->state = SEC_ST_CLOSED; |
| 335 | ep->close_sent = 1; |
| 336 | ep->flight_pending = ep->confirm_pending = 0; |
| 337 | ep->tries = 0; |
| 338 | return emit_flight(ep, now, out, cap, written); |
| 339 | } |
| 340 | |
| 341 | /* ---- handshake receive ---- */ |
| 342 | |
| 343 | static sec_status on_hs1(sec_endpoint *ep, const uint8_t *m) |
| 344 | { |
| 345 | if (ep->role != 1) { ep->c.rx_state++; return SEC_ERR_STATE; } |
| 346 | if (ep->state != SEC_ST_START) { |
| 347 | if (memcmp(m, ep->hs1, SEC_HS1_LEN) != 0) { |
| 348 | ep->c.rx_refused++; |
| 349 | return SEC_ERR_REFUSED; /* GUARD:hs1-dup */ |
| 350 | } |
| 351 | ep->c.rx_hs_dup++; |
| 352 | if (ep->state == SEC_ST_WAIT_HS3) ep->flight_pending = 1; /* HS2 was lost */ |
| 353 | return SEC_OK; |
| 354 | } |
| 355 | if (derive_prk(ep, m + 40) != 0) { ep->c.rx_weak++; return SEC_ERR_WEAK_KEY; } |
| 356 | memcpy(ep->hs1, m, SEC_HS1_LEN); |
| 357 | uint8_t *h2 = ep->hs2, th[32], sm[52], fin_key_r[32]; |
| 358 | hs_header(h2, SEC_TYPE_HS2); |
| 359 | memcpy(h2 + 8, ep->random, 32); |
| 360 | memcpy(h2 + 40, ep->eph_pk, 32); |
| 361 | memcpy(h2 + 72, ep->id_pk, 32); |
| 362 | transcript(th, ep->hs1, SEC_HS1_LEN, h2, 104, NULL, 0); /* TH_a */ |
| 363 | expand_label(fin_key_r, ep->prk, "fin r", th, 32); |
| 364 | expand_label(ep->fin_key_i, ep->prk, "fin i", th, 32); |
| 365 | sig_msg(sm, SIG_R_LABEL, th); |
| 366 | aienos_ed25519_sign(h2 + 104, sm, sizeof sm, ep->id_sk); |
| 367 | transcript(th, ep->hs1, SEC_HS1_LEN, h2, 168, NULL, 0); /* TH_b */ |
| 368 | aienos_hmac_sha256(fin_key_r, th, 32, h2 + 168); |
| 369 | aienos_wipe(fin_key_r, sizeof fin_key_r); |
| 370 | memcpy(ep->flight, h2, SEC_HS2_LEN); |
| 371 | ep->flight_len = SEC_HS2_LEN; |
| 372 | ep->flight_pending = 1; |
| 373 | ep->state = SEC_ST_WAIT_HS3; |
| 374 | ep->c.rx_hs++; |
| 375 | return SEC_OK; |
| 376 | } |
| 377 | |
| 378 | static sec_status on_hs2(sec_endpoint *ep, const uint8_t *m) |
| 379 | { |
| 380 | if (ep->role != 0) { ep->c.rx_state++; return SEC_ERR_STATE; } |
| 381 | if (ep->state == SEC_ST_ESTABLISHED && memcmp(m, ep->hs2, SEC_HS2_LEN) == 0) { |
| 382 | ep->c.rx_hs_dup++; |
| 383 | if (!ep->confirmed) ep->flight_pending = 1; /* HS3 was lost */ |
| 384 | return SEC_OK; |
| 385 | } |
| 386 | if (ep->state != SEC_ST_WAIT_HS2) { ep->c.rx_refused++; return SEC_ERR_REFUSED; } |
| 387 | if (memcmp(m + 72, ep->peer_pk, 32) != 0) { |
| 388 | ep->c.rx_identity++; |
| 389 | return SEC_ERR_IDENTITY; /* GUARD:hs-identity-r */ |
| 390 | } |
| 391 | uint8_t th[32], sm[52], fin_key_r[32], fin_key_i[32], fin[32]; |
| 392 | transcript(th, ep->hs1, SEC_HS1_LEN, m, 104, NULL, 0); /* TH_a */ |
| 393 | sig_msg(sm, SIG_R_LABEL, th); |
| 394 | if (aienos_ed25519_verify(m + 104, sm, sizeof sm, ep->peer_pk) != AIENOS_SIG_OK) { |
| 395 | ep->c.rx_bad_sig++; |
| 396 | return SEC_ERR_BAD_SIG; /* GUARD:hs-sig-r */ |
| 397 | } |
| 398 | if (derive_prk(ep, m + 40) != 0) { ep->c.rx_weak++; return SEC_ERR_WEAK_KEY; } |
| 399 | expand_label(fin_key_r, ep->prk, "fin r", th, 32); |
| 400 | expand_label(fin_key_i, ep->prk, "fin i", th, 32); |
| 401 | transcript(th, ep->hs1, SEC_HS1_LEN, m, 168, NULL, 0); /* TH_b */ |
| 402 | aienos_hmac_sha256(fin_key_r, th, 32, fin); |
| 403 | aienos_wipe(fin_key_r, sizeof fin_key_r); |
| 404 | if (!aienos_ct_equal(fin, m + 168, 32)) { |
| 405 | aienos_wipe(fin_key_i, sizeof fin_key_i); |
| 406 | aienos_wipe(ep->prk, sizeof ep->prk); |
| 407 | ep->c.rx_bad_tag++; |
| 408 | return SEC_ERR_BAD_TAG; /* GUARD:hs-fin-r */ |
| 409 | } |
| 410 | memcpy(ep->hs2, m, SEC_HS2_LEN); |
| 411 | uint8_t *h3 = ep->hs3; |
| 412 | hs_header(h3, SEC_TYPE_HS3); |
| 413 | memcpy(h3 + 8, ep->id_pk, 32); |
| 414 | transcript(th, ep->hs1, SEC_HS1_LEN, ep->hs2, SEC_HS2_LEN, h3, 40); /* TH_c */ |
| 415 | sig_msg(sm, SIG_I_LABEL, th); |
| 416 | aienos_ed25519_sign(h3 + 40, sm, sizeof sm, ep->id_sk); |
| 417 | transcript(th, ep->hs1, SEC_HS1_LEN, ep->hs2, SEC_HS2_LEN, h3, 104); /* TH_d */ |
| 418 | aienos_hmac_sha256(fin_key_i, th, 32, h3 + 104); |
| 419 | aienos_wipe(fin_key_i, sizeof fin_key_i); |
| 420 | transcript(th, ep->hs1, SEC_HS1_LEN, ep->hs2, SEC_HS2_LEN, h3, SEC_HS3_LEN); /* TH_f */ |
| 421 | install_keys(ep, th); |
| 422 | memcpy(ep->flight, h3, SEC_HS3_LEN); |
| 423 | ep->flight_len = SEC_HS3_LEN; |
| 424 | ep->flight_pending = 1; |
| 425 | ep->confirmed = 0; |
| 426 | ep->tries = 0; |
| 427 | ep->c.rx_hs++; |
| 428 | return SEC_OK; |
| 429 | } |
| 430 | |
| 431 | static sec_status on_hs3(sec_endpoint *ep, const uint8_t *m) |
| 432 | { |
| 433 | if (ep->role != 1) { ep->c.rx_state++; return SEC_ERR_STATE; } |
| 434 | if (ep->state == SEC_ST_ESTABLISHED && memcmp(m, ep->hs3, SEC_HS3_LEN) == 0) { |
| 435 | ep->c.rx_hs_dup++; |
| 436 | ep->confirm_pending = 1; /* our CONFIRM was lost */ |
| 437 | return SEC_OK; |
| 438 | } |
| 439 | if (ep->state != SEC_ST_WAIT_HS3) { ep->c.rx_refused++; return SEC_ERR_REFUSED; } |
| 440 | if (memcmp(m + 8, ep->peer_pk, 32) != 0) { |
| 441 | ep->c.rx_identity++; |
| 442 | return SEC_ERR_IDENTITY; /* GUARD:hs-identity-i */ |
| 443 | } |
| 444 | uint8_t th[32], sm[52], fin[32]; |
| 445 | transcript(th, ep->hs1, SEC_HS1_LEN, ep->hs2, SEC_HS2_LEN, m, 40); /* TH_c */ |
| 446 | sig_msg(sm, SIG_I_LABEL, th); |
| 447 | if (aienos_ed25519_verify(m + 40, sm, sizeof sm, ep->peer_pk) != AIENOS_SIG_OK) { |
| 448 | ep->c.rx_bad_sig++; |
| 449 | return SEC_ERR_BAD_SIG; /* GUARD:hs-sig-i */ |
| 450 | } |
| 451 | transcript(th, ep->hs1, SEC_HS1_LEN, ep->hs2, SEC_HS2_LEN, m, 104); /* TH_d */ |
| 452 | aienos_hmac_sha256(ep->fin_key_i, th, 32, fin); |
| 453 | if (!aienos_ct_equal(fin, m + 104, 32)) { |
| 454 | ep->c.rx_bad_tag++; |
| 455 | return SEC_ERR_BAD_TAG; /* GUARD:hs-fin-i */ |
| 456 | } |
| 457 | memcpy(ep->hs3, m, SEC_HS3_LEN); |
| 458 | transcript(th, ep->hs1, SEC_HS1_LEN, ep->hs2, SEC_HS2_LEN, ep->hs3, SEC_HS3_LEN); /* TH_f */ |
| 459 | install_keys(ep, th); |
| 460 | ep->confirmed = 1; |
| 461 | ep->confirm_pending = 1; |
| 462 | ep->c.rx_hs++; |
| 463 | return SEC_OK; |
| 464 | } |
| 465 | |
| 466 | /* ---- record receive ---- */ |
| 467 | |
| 468 | static sec_status on_record(sec_endpoint *ep, const uint8_t *in, size_t len, uint8_t *msg, |
| 469 | size_t cap, size_t *msg_len) |
| 470 | { |
| 471 | if (len < SEC_REC_OVERHEAD || len > SEC_MAX_DATAGRAM) { |
| 472 | ep->c.rx_malformed++; |
| 473 | return SEC_ERR_MALFORMED; /* GUARD:rec-len */ |
| 474 | } |
| 475 | if (in[6] != (uint8_t)(1 - ep->role) || in[7] != 0) { |
| 476 | ep->c.rx_malformed++; |
| 477 | return SEC_ERR_MALFORMED; /* GUARD:rec-dir */ |
| 478 | } |
| 479 | if (ep->state == SEC_ST_CLOSED) return SEC_ERR_CLOSED; |
| 480 | if (ep->state != SEC_ST_ESTABLISHED) { ep->c.rx_state++; return SEC_ERR_STATE; } |
| 481 | if (memcmp(in + 8, ep->session_id, 8) != 0) { |
| 482 | ep->c.rx_refused++; |
| 483 | return SEC_ERR_REFUSED; /* GUARD:rec-session */ |
| 484 | } |
| 485 | uint64_t seq = rd64(in + 16); |
| 486 | if (seq >= ep->max_records) { ep->c.rx_replay++; return SEC_ERR_EXHAUSTED; } |
| 487 | /* replay window check, nothing changes yet */ |
| 488 | if (seq < ep->rx_top) { |
| 489 | uint64_t back = ep->rx_top - seq; |
| 490 | if (back > SEC_WINDOW) { ep->c.rx_replay++; return SEC_ERR_REPLAY; } /* GUARD:replay-old */ |
| 491 | if ((ep->rx_bitmap >> (back - 1)) & 1) { ep->c.rx_replay++; return SEC_ERR_REPLAY; } /* GUARD:replay-dup */ |
| 492 | } |
| 493 | size_t ct_len = len - SEC_REC_HEADER_LEN, pt_len = ct_len - SEC_TAG_LEN; |
| 494 | if (pt_len - 1 > cap) return SEC_ERR_CAPACITY; |
| 495 | /* key for this record's epoch */ |
| 496 | uint64_t e = seq / ep->rekey_interval; |
| 497 | uint8_t fwd[32], fwd_prev[32]; |
| 498 | const uint8_t *key; |
| 499 | int advance = 0; |
| 500 | if (e == ep->rx_epoch) key = ep->rx_key; |
| 501 | else if (ep->rx_has_prev && e + 1 == ep->rx_epoch) key = ep->rx_prev_key; |
| 502 | else if (e > ep->rx_epoch && e - ep->rx_epoch <= SEC_MAX_EPOCH_SKIP) { |
| 503 | memcpy(fwd, ep->rx_key, 32); |
| 504 | for (uint64_t k = ep->rx_epoch; k < e; k++) { |
| 505 | memcpy(fwd_prev, fwd, 32); |
| 506 | expand_label(fwd, fwd_prev, "rekey", NULL, 0); |
| 507 | } |
| 508 | key = fwd; |
| 509 | advance = 1; |
| 510 | } else { |
| 511 | ep->c.rx_replay++; |
| 512 | return SEC_ERR_REPLAY; |
| 513 | } |
| 514 | uint8_t nonce[12] = {in[6], 0, 0, 0}; |
| 515 | memcpy(nonce + 4, in + 16, 8); |
| 516 | uint8_t pt[SEC_MAX_PAYLOAD + 1]; |
| 517 | int r = aienos_gcmsiv_open(key, nonce, in, SEC_REC_HEADER_LEN, in + SEC_REC_HEADER_LEN, ct_len, |
| 518 | pt, pt_len); |
| 519 | if (r != AIENOS_CRYPTO_OK) { |
| 520 | if (advance) { aienos_wipe(fwd, 32); aienos_wipe(fwd_prev, 32); } |
| 521 | aienos_wipe(pt, sizeof pt); |
| 522 | ep->c.rx_bad_tag++; |
| 523 | return SEC_ERR_BAD_TAG; /* GUARD:rec-tag */ |
| 524 | } |
| 525 | /* authenticated: commit window and epoch */ |
| 526 | if (seq >= ep->rx_top) { |
| 527 | uint64_t shift = seq + 1 - ep->rx_top; |
| 528 | ep->rx_bitmap = shift >= 64 ? 0 : ep->rx_bitmap << shift; |
| 529 | ep->rx_bitmap |= 1; |
| 530 | ep->rx_top = seq + 1; |
| 531 | } else { |
| 532 | ep->rx_bitmap |= (uint64_t)1 << (ep->rx_top - 1 - seq); |
| 533 | } |
| 534 | if (advance) { |
| 535 | memcpy(ep->rx_prev_key, fwd_prev, 32); /* GUARD:rekey-rx */ |
| 536 | memcpy(ep->rx_key, fwd, 32); |
| 537 | aienos_wipe(fwd, 32); |
| 538 | aienos_wipe(fwd_prev, 32); |
| 539 | ep->c.rekeys_rx += e - ep->rx_epoch; |
| 540 | ep->rx_epoch = e; |
| 541 | ep->rx_has_prev = 1; |
| 542 | } |
| 543 | ep->c.rx_records++; |
| 544 | if (ep->role == 0) ep->confirmed = 1; /* any record from the responder confirms HS3 */ |
| 545 | uint8_t inner = pt[0]; |
| 546 | sec_status out; |
| 547 | if (inner == SEC_INNER_DATA) { |
| 548 | if (pt_len > 1) memcpy(msg, pt + 1, pt_len - 1); |
| 549 | *msg_len = pt_len - 1; |
| 550 | out = SEC_DATA; |
| 551 | } else if (inner == SEC_INNER_CLOSE && pt_len == 1) { |
| 552 | wipe_secrets(ep); |
| 553 | ep->state = SEC_ST_CLOSED; |
| 554 | ep->close_sent = 0; |
| 555 | ep->flight_pending = ep->confirm_pending = 0; |
| 556 | out = SEC_PEER_CLOSED; |
| 557 | } else if (inner == SEC_INNER_CONFIRM && pt_len == 1 && ep->role == 0) { |
| 558 | out = SEC_OK; |
| 559 | } else { |
| 560 | ep->c.rx_malformed++; |
| 561 | out = SEC_ERR_MALFORMED; |
| 562 | } |
| 563 | aienos_wipe(pt, sizeof pt); |
| 564 | return out; |
| 565 | } |
| 566 | |
| 567 | sec_status sec_receive(sec_endpoint *ep, uint64_t now, const uint8_t *in, size_t len, uint8_t *msg, |
| 568 | size_t cap, size_t *msg_len) |
| 569 | { |
| 570 | (void)now; |
| 571 | if (!ep || (!in && len) || !msg_len || (!msg && cap)) return SEC_ERR_ARG; |
| 572 | *msg_len = 0; |
| 573 | if (ep->state == SEC_ST_DOWN) return SEC_ERR_LINK_DOWN; |
| 574 | /* structure first, before any field is trusted */ |
| 575 | if (len < 8 || memcmp(in, SEC_MAGIC, 4) != 0 || in[4] != SEC_VERSION) { |
| 576 | ep->c.rx_malformed++; |
| 577 | return SEC_ERR_MALFORMED; |
| 578 | } |
| 579 | uint8_t type = in[5]; |
| 580 | if (type == SEC_TYPE_RECORD) return on_record(ep, in, len, msg, cap, msg_len); |
| 581 | size_t want = type == SEC_TYPE_HS1 ? SEC_HS1_LEN : type == SEC_TYPE_HS2 ? SEC_HS2_LEN |
| 582 | : type == SEC_TYPE_HS3 ? SEC_HS3_LEN : 0; |
| 583 | if (want == 0 || len != want || in[6] != 0 || in[7] != 0) { |
| 584 | ep->c.rx_malformed++; |
| 585 | return SEC_ERR_MALFORMED; /* GUARD:hs-len */ |
| 586 | } |
| 587 | if (ep->state == SEC_ST_CLOSED) return SEC_ERR_CLOSED; |
| 588 | if (type == SEC_TYPE_HS1) return on_hs1(ep, in); |
| 589 | if (type == SEC_TYPE_HS2) return on_hs2(ep, in); |
| 590 | return on_hs3(ep, in); |
| 591 | } |