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.h
| 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 (hosted), in C. No heap, clock or I/O. |
| 18 | * |
| 19 | * A mutually authenticated, encrypted datagram channel between two machines, |
| 20 | * carried in M6-A UDP payloads. This header is the wire spec. The caller |
| 21 | * supplies the clock (ticks), the randomness and the identity keys, puts |
| 22 | * datagrams on the wire and hands received ones back. |
| 23 | * |
| 24 | * Identity: each machine has an Ed25519 identity key (native/sig) and is |
| 25 | * told the one peer public key it will accept (a pinned roster of one). |
| 26 | * In this lane only TEST keys exist (sec_test_identity, public labels, |
| 27 | * NOT-FOR-PRODUCTION). Real keys wait for the TRUST-1 owner-key chain. |
| 28 | * |
| 29 | * Handshake (SIGMA style, 3 messages, identities in clear). All integers |
| 30 | * big-endian. Every handshake message starts with |
| 31 | * 0 magic "ASC1" 4 version 1 5 type 6 reserved u16 (0) |
| 32 | * HS1 (type 1, 72 bytes, initiator -> responder) |
| 33 | * 8 random_i[32] 40 eph_i[32] X25519 public key |
| 34 | * HS2 (type 2, 200 bytes, responder -> initiator) |
| 35 | * 8 random_r[32] 40 eph_r[32] 72 id_r[32] 104 sig_r[64] 168 fin_r[32] |
| 36 | * HS3 (type 3, 136 bytes, initiator -> responder) |
| 37 | * 8 id_i[32] 40 sig_i[64] 104 fin_i[32] |
| 38 | * Transcript hashes (SHA-256, C = "AIENOS-M6B-v1 transcript"): |
| 39 | * TH_a = H(C | HS1 | HS2[0..104)) sig_r = Ed25519(sk_r, "AIENOS-M6B-v1 sig r" | 0 | TH_a) |
| 40 | * TH_b = H(C | HS1 | HS2[0..168)) fin_r = HMAC(fin_key_r, TH_b) |
| 41 | * TH_c = H(C | HS1 | HS2 | HS3[0..40)) sig_i = Ed25519(sk_i, "AIENOS-M6B-v1 sig i" | 0 | TH_c) |
| 42 | * TH_d = H(C | HS1 | HS2 | HS3[0..104)) fin_i = HMAC(fin_key_i, TH_d) |
| 43 | * TH_f = H(C | HS1 | HS2 | HS3) |
| 44 | * Each signature covers both ephemeral keys, both randoms and the signer's |
| 45 | * identity, so a message from one handshake cannot be spliced into another. |
| 46 | * Key schedule (HKDF-SHA256, RFC 5869, one 32-byte output block each): |
| 47 | * dh = X25519(eph_sk, peer_eph) (all-zero result refused) |
| 48 | * PRK = HKDF-Extract(salt = SHA-256("AIENOS-M6B-v1 hkdf salt"), dh) |
| 49 | * fin_key_r = Expand(PRK, "fin r" | TH_a), fin_key_i = Expand(PRK, "fin i" | TH_a) |
| 50 | * master = Expand(PRK, "master" | TH_f) |
| 51 | * key_i2r[0] = Expand(master, "i2r"), key_r2i[0] = Expand(master, "r2i") |
| 52 | * session_id = Expand(master, "session")[0..8) |
| 53 | * key_dir[e + 1] = Expand(key_dir[e], "rekey") (old key wiped) |
| 54 | * |
| 55 | * Record (type 0x10), total = 24 + 1 + payload_len + 16: |
| 56 | * 0 magic "ASC1" 4 version 1 5 type 0x10 6 dir (0 i2r, 1 r2i) |
| 57 | * 7 reserved (0) 8 session_id[8] 16 seq u64 |
| 58 | * 24 AES-256-GCM-SIV(key_dir[seq / rekey_interval], |
| 59 | * nonce = dir | 0 0 0 | seq, aad = bytes 0..24, |
| 60 | * plaintext = inner_type | payload) || tag[16] |
| 61 | * inner_type: 1 DATA, 2 CLOSE, 3 CONFIRM (responder's "keys installed") |
| 62 | * The sender's seq starts at 0 and rises by one per record. The receiver |
| 63 | * keeps a SEC_WINDOW-record sliding replay window (accepts reordering inside |
| 64 | * it, refuses duplicates and anything older) and the keys of the current |
| 65 | * and previous epoch only. After max_records the channel refuses to send: |
| 66 | * run a new handshake. |
| 67 | * |
| 68 | * Loss: HS1 is resent every rto ticks until HS2 arrives; HS3 every rto until |
| 69 | * an authenticated record from the responder arrives (the responder answers |
| 70 | * every valid HS3 with a CONFIRM record); the responder answers a repeat of |
| 71 | * the exact HS1 with its stored HS2. More than max_retries resends of one |
| 72 | * flight -> SEC_ERR_LINK_DOWN. Data records are not resent here: carry the |
| 73 | * M6-A reliable channel (aienos_ctl) inside them for ordered, exactly-once |
| 74 | * delivery. |
| 75 | */ |
| 76 | #ifndef AIENOS_SEC_H |
| 77 | #define AIENOS_SEC_H |
| 78 | |
| 79 | #include <stddef.h> |
| 80 | #include <stdint.h> |
| 81 | |
| 82 | #define SEC_VERSION 1u |
| 83 | #define SEC_TYPE_HS1 1u |
| 84 | #define SEC_TYPE_HS2 2u |
| 85 | #define SEC_TYPE_HS3 3u |
| 86 | #define SEC_TYPE_RECORD 0x10u |
| 87 | #define SEC_INNER_DATA 1u |
| 88 | #define SEC_INNER_CLOSE 2u |
| 89 | #define SEC_INNER_CONFIRM 3u |
| 90 | |
| 91 | #define SEC_HS1_LEN 72u |
| 92 | #define SEC_HS2_LEN 200u |
| 93 | #define SEC_HS3_LEN 136u |
| 94 | #define SEC_REC_HEADER_LEN 24u |
| 95 | #define SEC_TAG_LEN 16u |
| 96 | #define SEC_MAX_PAYLOAD 1100u /* holds one full M6-A control frame (1064) */ |
| 97 | #define SEC_REC_OVERHEAD (SEC_REC_HEADER_LEN + 1u + SEC_TAG_LEN) |
| 98 | #define SEC_MAX_DATAGRAM (SEC_REC_OVERHEAD + SEC_MAX_PAYLOAD) |
| 99 | #define SEC_WINDOW 64u |
| 100 | #define SEC_MAX_EPOCH_SKIP 4u |
| 101 | #define SEC_MIN_REKEY_INTERVAL (2u * SEC_WINDOW) |
| 102 | |
| 103 | typedef enum { |
| 104 | SEC_OK = 0, /* accepted (handshake progress, confirm, duplicate HS) */ |
| 105 | SEC_DATA = 1, /* receive: one authenticated payload copied out */ |
| 106 | SEC_IDLE = 2, /* poll: nothing to send now */ |
| 107 | SEC_PEER_CLOSED = 3, /* receive: authenticated CLOSE; keys wiped */ |
| 108 | SEC_ERR_ARG = -1, |
| 109 | SEC_ERR_MALFORMED = -2, /* magic/version/type/length/reserved/direction */ |
| 110 | SEC_ERR_BAD_TAG = -3, /* record or finished MAC did not verify */ |
| 111 | SEC_ERR_REFUSED = -4, /* wrong session, unexpected or different handshake message */ |
| 112 | SEC_ERR_IDENTITY = -5, /* peer presented an identity key we were not told to accept */ |
| 113 | SEC_ERR_BAD_SIG = -6, /* handshake signature did not verify over our transcript */ |
| 114 | SEC_ERR_REPLAY = -7, /* duplicate, older than the window, or unknown epoch */ |
| 115 | SEC_ERR_CAPACITY = -8, |
| 116 | SEC_ERR_TOO_LARGE = -9, |
| 117 | SEC_ERR_STATE = -10, /* not established (or wrong role for this message) */ |
| 118 | SEC_ERR_LINK_DOWN = -11, |
| 119 | SEC_ERR_EXHAUSTED = -12, /* max_records used; new handshake needed */ |
| 120 | SEC_ERR_CLOSED = -13, |
| 121 | SEC_ERR_WEAK_KEY = -14, /* small-order X25519 share (all-zero secret) */ |
| 122 | } sec_status; |
| 123 | |
| 124 | typedef enum { |
| 125 | SEC_ST_START = 0, /* initiator: HS1 not sent yet; responder: waiting for HS1 */ |
| 126 | SEC_ST_WAIT_HS2 = 1, /* initiator */ |
| 127 | SEC_ST_WAIT_HS3 = 2, /* responder */ |
| 128 | SEC_ST_ESTABLISHED = 3, |
| 129 | SEC_ST_CLOSED = 4, |
| 130 | SEC_ST_DOWN = 5, |
| 131 | } sec_state; |
| 132 | |
| 133 | typedef struct { |
| 134 | uint8_t role; /* 0 initiator, 1 responder */ |
| 135 | uint8_t id_sk[32]; /* our Ed25519 identity secret key (TEST only here) */ |
| 136 | uint8_t peer_pk[32]; /* the one peer identity key we accept */ |
| 137 | uint8_t entropy[64]; /* caller's fresh randomness: ephemeral scalar | random */ |
| 138 | uint32_t rto; /* resend interval, ticks (>= 1) */ |
| 139 | uint32_t max_retries; /* resends of one flight before LINK_DOWN */ |
| 140 | uint64_t rekey_interval; /* records per key epoch, >= SEC_MIN_REKEY_INTERVAL */ |
| 141 | uint64_t max_records; /* records per direction before EXHAUSTED, >= 1 */ |
| 142 | } sec_config; |
| 143 | |
| 144 | typedef struct { |
| 145 | uint64_t tx_hs, tx_hs_resend, tx_records, rekeys_tx, rekeys_rx; |
| 146 | uint64_t rx_hs, rx_hs_dup, rx_records, rx_malformed, rx_bad_tag, rx_refused; |
| 147 | uint64_t rx_identity, rx_bad_sig, rx_replay, rx_state, rx_weak; |
| 148 | } sec_counters; |
| 149 | |
| 150 | typedef struct { |
| 151 | uint8_t role, state, confirmed, flight_pending, confirm_pending, close_sent; |
| 152 | uint8_t id_sk[32], id_pk[32], peer_pk[32]; |
| 153 | uint8_t eph_sk[32], eph_pk[32], random[32]; |
| 154 | uint8_t fin_key_i[32], prk[32]; /* responder keeps them until HS3 */ |
| 155 | uint8_t hs1[SEC_HS1_LEN], hs2[SEC_HS2_LEN], hs3[SEC_HS3_LEN]; |
| 156 | uint8_t flight[SEC_MAX_DATAGRAM]; /* current resendable datagram */ |
| 157 | uint16_t flight_len; |
| 158 | uint32_t rto, max_retries, tries; |
| 159 | uint64_t last_tx; |
| 160 | uint64_t rekey_interval, max_records; |
| 161 | uint8_t session_id[8]; |
| 162 | /* send direction */ |
| 163 | uint8_t tx_key[32]; |
| 164 | uint64_t tx_epoch, tx_seq; |
| 165 | /* receive direction */ |
| 166 | uint8_t rx_key[32], rx_prev_key[32]; |
| 167 | uint8_t rx_has_prev, rx_any; |
| 168 | uint64_t rx_epoch, rx_top; /* rx_top = highest accepted seq + 1 */ |
| 169 | uint64_t rx_bitmap; /* bit i set: seq rx_top - 1 - i accepted */ |
| 170 | sec_counters c; |
| 171 | } sec_endpoint; |
| 172 | |
| 173 | /* Public TEST identity from a label (sk = SHA-256(public prefix | label)). |
| 174 | * NOT-FOR-PRODUCTION: anyone can recompute it. */ |
| 175 | sec_status sec_test_identity(uint8_t sk[32], uint8_t pk[32], const char *label); |
| 176 | |
| 177 | sec_status sec_init(sec_endpoint *ep, const sec_config *cfg); |
| 178 | /* Next handshake/control datagram to put on the wire, or SEC_IDLE. */ |
| 179 | sec_status sec_poll_tx(sec_endpoint *ep, uint64_t now, uint8_t *out, size_t cap, size_t *written); |
| 180 | /* Seal one payload into a record datagram (established only). */ |
| 181 | sec_status sec_seal(sec_endpoint *ep, const uint8_t *msg, size_t len, uint8_t *out, size_t cap, |
| 182 | size_t *written); |
| 183 | /* Process one received datagram. SEC_DATA copies the payload into msg. */ |
| 184 | sec_status sec_receive(sec_endpoint *ep, uint64_t now, const uint8_t *in, size_t len, uint8_t *msg, |
| 185 | size_t cap, size_t *msg_len); |
| 186 | /* Send CLOSE (written to out), wipe every key, enter SEC_ST_CLOSED. The |
| 187 | * same datagram is resent by sec_poll_tx every rto, max_retries times. */ |
| 188 | sec_status sec_close(sec_endpoint *ep, uint64_t now, uint8_t *out, size_t cap, size_t *written); |
| 189 | const char *sec_status_name(sec_status s); |
| 190 | |
| 191 | /* HKDF-SHA256 single-block expand / extract, exposed for the RFC 5869 test. */ |
| 192 | void sec_hkdf_extract(uint8_t prk[32], const uint8_t salt[32], const uint8_t *ikm, size_t len); |
| 193 | void sec_hkdf_expand1(uint8_t out[32], const uint8_t prk[32], const uint8_t *info, size_t len); |
| 194 | |
| 195 | #endif |