Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/m5/tests/m5_test.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / m5 / tests / m5_test.c` 1114 lines ยท 52556 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 /* native/m5 tests: key hierarchy, envelopes, corruption matrix, 18 * all-or-nothing open, anti-rollback, identity separation, migration, 19 * deterministic recovery. Host-only (hosted libc), not QEMU. 20 * Last line: "M5_NATIVE: PASS" (exit 0) or "M5_NATIVE: FAIL" (exit 1). */ 21 #include 22 #include 23 #include 24 25 #include "../m5.h" 26 #include "../../argus/sha256.h" 27 #include "../../crypto/aienos_crypto.h" 28 29 static int g_fail; 30 #define CHECK(c) do { if (!(c)) { printf(" check failed: %s (line %d)\n", #c, __LINE__); g_fail = 1; } } while (0) 31 32 static int all_zero(const uint8_t *p, size_t n) { for (size_t i = 0; i < n; i++) if (p[i]) return 0; return 1; } 33 static void put64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); } 34 static void put32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); } 35 static uint64_t rng_state = 0x9E3779B97F4A7C15ull; 36 static uint64_t rnd(void) { rng_state ^= rng_state << 13; rng_state ^= rng_state >> 7; rng_state ^= rng_state << 17; return rng_state; } 37 38 /* ---------- fixtures ---------- */ 39 #define CS 64u 40 #define PT_LEN 224u /* 4 chunks: 64, 64, 64, 32 */ 41 #define ENV_LEN (64u + PT_LEN + 4u * 16u) 42 #define CHUNK_OFF(i) (64u + (i) * (CS + 16u)) 43 #define SG 9u /* store generation of the fixture commit */ 44 45 static uint8_t K_VOL[32], NONCE8[8], PT[PT_LEN]; 46 static m5_subkeys SK, SK_TEST; 47 static m5_object_binding OBJ; 48 static uint8_t ENV[ENV_LEN]; 49 static uint8_t COMMIT[M5_COMMIT_LEN], ANCHOR[M5_ANCHOR_LEN]; 50 51 static void binding_init(m5_object_binding *b, uint8_t cls) 52 { 53 memset(b, 0, sizeof *b); 54 memset(b->store_uuid, 0xA0, 16); 55 b->object_kind = 7; 56 b->object_version = 1; 57 memset(b->envelope_id, 0xB0, 16); 58 b->key_generation = 3; 59 b->identity_class = cls; 60 } 61 62 static void make_commit(const m5_object_binding *b, uint64_t sg, const uint8_t *env, size_t len, uint64_t counter, 63 const uint8_t key[32], uint8_t out[M5_COMMIT_LEN]) 64 { 65 m5_commit c; 66 memset(&c, 0, sizeof c); 67 c.obj = *b; 68 c.store_generation = sg; 69 c.counter = counter; 70 c.object_sequence = 1; 71 sha256_hash(env, len, c.object_id); 72 m5_commit_seal(&c, key, out); 73 } 74 75 static void make_anchor(uint8_t cls, const uint8_t uuid[16], uint64_t sg, uint64_t kg, uint64_t ctr, 76 const uint8_t commit[M5_COMMIT_LEN], const uint8_t key[32], uint8_t out[M5_ANCHOR_LEN]) 77 { 78 m5_anchor a; 79 memset(&a, 0, sizeof a); 80 a.identity_class = cls; 81 memcpy(a.store_uuid, uuid, 16); 82 a.store_generation = sg; 83 a.key_generation = kg; 84 a.counter = ctr; 85 sha256_hash(commit, M5_COMMIT_LEN, a.commit_digest); 86 m5_anchor_seal(&a, key, out, M5_ANCHOR_LEN); 87 } 88 89 static void fixtures(void) 90 { 91 for (int i = 0; i < 32; i++) K_VOL[i] = (uint8_t)(0x11 + i); 92 for (int i = 0; i < 8; i++) NONCE8[i] = (uint8_t)(0xC0 + i); 93 for (unsigned i = 0; i < PT_LEN; i++) PT[i] = (uint8_t)(i * 7 + 1); 94 m5_derive_subkeys(K_VOL, M5_ID_PRODUCTION, 1, &SK); 95 m5_derive_subkeys(K_VOL, M5_ID_TEST, 1, &SK_TEST); 96 binding_init(&OBJ, M5_ID_PRODUCTION); 97 size_t n = 0; 98 if (m5_envelope_seal(SK.k_artifact, &OBJ, NONCE8, CS, PT, PT_LEN, ENV, sizeof ENV, &n) != M5_OK || n != ENV_LEN) { 99 printf("fixture seal failed\n"); 100 g_fail = 1; 101 } 102 make_commit(&OBJ, SG, ENV, ENV_LEN, 5, SK.k_root_auth, COMMIT); 103 make_anchor(M5_ID_PRODUCTION, OBJ.store_uuid, 9, 3, 5, COMMIT, SK.k_root_auth, ANCHOR); 104 } 105 106 /* Open env (copied to an exact-size heap buffer so ASan sees over-reads) and 107 * require refusal with an all-zero output. Returns the error code. */ 108 static int open_refused(const uint8_t *env, size_t len, const m5_object_binding *b, uint8_t mode) 109 { 110 uint8_t *e = malloc(len ? len : 1); 111 memcpy(e, env, len); 112 uint8_t out[1024]; 113 memset(out, 0xAA, sizeof out); 114 size_t n = 77; 115 int rc = m5_envelope_open(SK.k_artifact, mode, b, e, len, out, sizeof out, &n); 116 CHECK(rc < 0); 117 CHECK(n == 0); 118 CHECK(all_zero(out, sizeof out)); 119 free(e); 120 return rc; 121 } 122 123 /* ---------- 1. key hierarchy ---------- */ 124 static void t_hkdf_rfc5869_case1(void) 125 { 126 static const uint8_t prk[32] = {0x07,0x77,0x09,0x36,0x2c,0x2e,0x32,0xdf,0x0d,0xdc,0x3f,0x0d,0xc4,0x7b,0xba,0x63, 127 0x90,0xb6,0xc7,0x3b,0xb5,0x0f,0x9c,0x31,0x22,0xec,0x84,0x4a,0xd7,0xc2,0xb3,0xe5}; 128 static const uint8_t info[10] = {0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9}; 129 static const uint8_t okm[42] = {0x3c,0xb2,0x5f,0x25,0xfa,0xac,0xd5,0x7a,0x90,0x43,0x4f,0x64,0xd0,0x36,0x2f,0x2a, 130 0x2d,0x2d,0x0a,0x90,0xcf,0x1a,0x5a,0x4c,0x5d,0xb0,0x2d,0x56,0xec,0xc4,0xc5,0xbf, 131 0x34,0x00,0x72,0x08,0xd5,0xb8,0x87,0x18,0x58,0x65}; 132 uint8_t out[42]; 133 CHECK(m5_hkdf_expand(prk, info, sizeof info, out, sizeof out) == M5_OK); 134 CHECK(memcmp(out, okm, 42) == 0); 135 uint8_t big[1]; 136 CHECK(m5_hkdf_expand(prk, info, 10, big, 255 * 32 + 1) == M5_ERR_BOUNDS); 137 } 138 139 static void t_subkeys_deterministic_and_separated(void) 140 { 141 m5_subkeys a, b, t, g2, v1; 142 CHECK(m5_derive_subkeys(K_VOL, M5_ID_PRODUCTION, 1, &a) == M5_OK); 143 CHECK(m5_derive_subkeys(K_VOL, M5_ID_PRODUCTION, 1, &b) == M5_OK); 144 CHECK(memcmp(&a, &b, sizeof a) == 0); 145 CHECK(memcmp(a.k_cortex, a.k_agent, 32) && memcmp(a.k_agent, a.k_artifact, 32) && 146 memcmp(a.k_artifact, a.k_root_auth, 32) && memcmp(a.k_cortex, a.k_root_auth, 32)); 147 m5_derive_subkeys(K_VOL, M5_ID_TEST, 1, &t); 148 CHECK(memcmp(a.k_root_auth, t.k_root_auth, 32) != 0); 149 CHECK(memcmp(a.k_artifact, t.k_artifact, 32) != 0); 150 m5_derive_subkeys(K_VOL, M5_ID_PRODUCTION, 2, &g2); 151 CHECK(memcmp(a.k_root_auth, g2.k_root_auth, 32) != 0); 152 m5_derive_subkeys_v1(K_VOL, &v1); 153 CHECK(memcmp(a.k_root_auth, v1.k_root_auth, 32) != 0); 154 CHECK(m5_derive_subkeys(K_VOL, 0, 1, &b) == M5_ERR_ARG); 155 CHECK(m5_derive_subkeys(K_VOL, 3, 1, &b) == M5_ERR_ARG); 156 m5_subkeys_wipe(&a); 157 CHECK(all_zero((uint8_t *)&a, sizeof a)); 158 } 159 160 static void t_object_key_binds_every_field(void) 161 { 162 uint8_t base[32], k[32]; 163 m5_derive_object_key(SK.k_artifact, &OBJ, base); 164 for (int f = 0; f < 6; f++) { 165 m5_object_binding b = OBJ; 166 switch (f) { 167 case 0: b.store_uuid[3] ^= 1; break; 168 case 1: b.object_kind++; break; 169 case 2: b.object_version++; break; 170 case 3: b.envelope_id[15] ^= 1; break; 171 case 4: b.key_generation++; break; 172 case 5: b.identity_class = M5_ID_TEST; break; 173 } 174 m5_derive_object_key(SK.k_artifact, &b, k); 175 CHECK(memcmp(base, k, 32) != 0); 176 } 177 } 178 179 static m5_keyslot slot_fixture(void) 180 { 181 m5_keyslot s; 182 memset(&s, 0, sizeof s); 183 s.slot_type = M5_SLOT_RECOVERY_RAW_SECRET; 184 s.slot_id = 1; 185 s.key_epoch = 3; 186 return s; 187 } 188 189 static void t_keyslot_wrap_unwrap(void) 190 { 191 uint8_t kek[32], nonce[12], uuid[16], kv[32], enc[M5_KEYSLOT_LEN]; 192 memset(kek, 0x42, 32); memset(nonce, 0x24, 12); memset(uuid, 0xA0, 16); 193 m5_keyslot s = slot_fixture(), d; 194 CHECK(m5_keyslot_wrap(&s, uuid, kek, K_VOL, nonce) == M5_OK); 195 m5_keyslot_encode(&s, enc); 196 CHECK(m5_keyslot_decode(enc, sizeof enc, &d) == M5_OK); 197 CHECK(m5_keyslot_unwrap(&d, uuid, kek, kv) == M5_OK); 198 CHECK(memcmp(kv, K_VOL, 32) == 0); 199 CHECK(memcmp(K_VOL + 0, enc + 56, 32) != 0); /* wrapped, not plain */ 200 } 201 202 static void t_keyslot_refusals(void) 203 { 204 uint8_t kek[32], bad[32], nonce[12], uuid[16], uuid2[16], kv[32], enc[M5_KEYSLOT_LEN]; 205 memset(kek, 0x42, 32); memset(bad, 0x43, 32); memset(nonce, 0x24, 12); 206 memset(uuid, 0xA0, 16); memset(uuid2, 0xA1, 16); 207 m5_keyslot s = slot_fixture(), d; 208 m5_keyslot_wrap(&s, uuid, kek, K_VOL, nonce); 209 memset(kv, 0x55, 32); 210 CHECK(m5_keyslot_unwrap(&s, uuid, bad, kv) == M5_ERR_AUTH && all_zero(kv, 32)); 211 CHECK(m5_keyslot_unwrap(&s, uuid2, kek, kv) == M5_ERR_AUTH && all_zero(kv, 32)); 212 d = s; d.slot_id = 2; 213 CHECK(m5_keyslot_unwrap(&d, uuid, kek, kv) == M5_ERR_AUTH); 214 d = s; d.key_epoch = 4; 215 CHECK(m5_keyslot_unwrap(&d, uuid, kek, kv) == M5_ERR_AUTH); 216 d = s; d.wrap_tag[0] ^= 1; 217 CHECK(m5_keyslot_unwrap(&d, uuid, kek, kv) == M5_ERR_AUTH); 218 d = s; d.slot_type = M5_SLOT_EMPTY; 219 CHECK(m5_keyslot_unwrap(&d, uuid, kek, kv) == M5_ERR_FORMAT); 220 d = s; d.wrap_suite = 2; 221 CHECK(m5_keyslot_unwrap(&d, uuid, kek, kv) == M5_ERR_FORMAT); 222 /* in-memory descriptor values the decoder would refuse; wrap with an 223 * unknown type cannot be produced by m5_keyslot_wrap, so build it by hand */ 224 d = s; d.argon_t_cost = 11; 225 CHECK(m5_keyslot_unwrap(&d, uuid, kek, kv) == M5_ERR_BOUNDS && all_zero(kv, 32)); 226 d = s; d.slot_type = 9; 227 CHECK(m5_keyslot_unwrap(&d, uuid, kek, kv) == M5_ERR_FORMAT); 228 m5_keyslot_encode(&s, enc); 229 CHECK(m5_keyslot_decode(enc, sizeof enc - 1, &d) == M5_ERR_BOUNDS); 230 enc[0] = 9; CHECK(m5_keyslot_decode(enc, sizeof enc, &d) == M5_ERR_FORMAT); enc[0] = 3; 231 put32(enc + 32, 131073); CHECK(m5_keyslot_decode(enc, sizeof enc, &d) == M5_ERR_BOUNDS); put32(enc + 32, 0); 232 enc[120] = 1; CHECK(m5_keyslot_decode(enc, sizeof enc, &d) == M5_ERR_FORMAT); 233 enc[120] = 0; 234 enc[1] = 2; CHECK(m5_keyslot_decode(enc, sizeof enc, &d) == M5_ERR_FORMAT); enc[1] = 1; 235 enc[2] = 3; CHECK(m5_keyslot_decode(enc, sizeof enc, &d) == M5_ERR_FORMAT); enc[2] = 0; 236 enc[3] = 1; CHECK(m5_keyslot_decode(enc, sizeof enc, &d) == M5_ERR_FORMAT); enc[3] = 0; 237 CHECK(m5_keyslot_decode(enc, sizeof enc, &d) == M5_OK); 238 d = s; d.kdf_suite = 3; 239 CHECK(m5_keyslot_unwrap(&d, uuid, kek, kv) == M5_ERR_FORMAT); 240 d = s; d.flags = 0x80; 241 CHECK(m5_keyslot_unwrap(&d, uuid, kek, kv) == M5_ERR_FORMAT); 242 } 243 244 static m5_secman secman_fixture(void) 245 { 246 m5_secman m; 247 memset(&m, 0, sizeof m); 248 memset(m.store_uuid, 0xA0, 16); 249 m.generation = 9; m.security_sequence = 4; m.epoch = 5; m.key_epoch = 3; 250 memset(m.keyslot_manifest_id, 0x31, 32); 251 memset(m.agent_root_id, 0x32, 32); 252 return m; 253 } 254 255 static void t_secman_roundtrip_rust_v1_layout(void) 256 { 257 m5_secman m = secman_fixture(), o; 258 uint8_t buf[M5_SECMAN_LEN]; 259 CHECK(M5_SECMAN_LEN == 256); 260 CHECK(m5_secman_seal(&m, SK.k_root_auth, buf) == M5_OK); 261 CHECK(m5_secman_open(buf, sizeof buf, SK.k_root_auth, &o) == M5_OK); 262 CHECK(o.generation == 9 && o.security_sequence == 4 && o.epoch == 5 && o.key_epoch == 3); 263 CHECK(memcmp(o.root_mac, m.root_mac, 32) == 0); 264 /* security.rs encode: magic, version 1, flags 0, reserved 0, fields, MAC at 224 */ 265 CHECK(memcmp(buf, "AIENSEC1", 8) == 0 && buf[8] == 1 && buf[9] == 0); 266 CHECK(all_zero(buf + 10, 6)); 267 CHECK(memcmp(buf + 16, m.store_uuid, 16) == 0 && buf[32] == 9 && buf[56] == 3); 268 CHECK(memcmp(buf + 96, m.keyslot_manifest_id, 32) == 0 && memcmp(buf + 128, m.agent_root_id, 32) == 0); 269 /* compute_mac: HMAC(k_root_auth, "AIENOS-M5-ROOT-AUTH-V1\0" || bytes[0..224]) */ 270 aienos_hmac_sha256_ctx h; 271 uint8_t mac[32]; 272 aienos_hmac_sha256_init(&h, SK.k_root_auth); 273 aienos_hmac_sha256_update(&h, (const uint8_t *)"AIENOS-M5-ROOT-AUTH-V1", 23); 274 aienos_hmac_sha256_update(&h, buf, 224); 275 aienos_hmac_sha256_final(&h, mac); 276 CHECK(memcmp(mac, buf + 224, 32) == 0); 277 /* identity class and owner generation are bound through k_root_auth */ 278 m5_subkeys g2; 279 m5_derive_subkeys(K_VOL, M5_ID_PRODUCTION, 2, &g2); 280 CHECK(m5_secman_open(buf, sizeof buf, g2.k_root_auth, &o) == M5_ERR_AUTH); 281 CHECK(m5_secman_open(buf, sizeof buf, SK_TEST.k_root_auth, &o) == M5_ERR_AUTH); 282 /* reserved and flags must stay zero (as Rust decode) */ 283 buf[12] = 1; 284 CHECK(m5_secman_open(buf, sizeof buf, SK.k_root_auth, &o) == M5_ERR_FORMAT); 285 } 286 287 static void t_secman_wrong_root_auth_key_refused(void) 288 { 289 m5_secman m = secman_fixture(), o; 290 uint8_t buf[M5_SECMAN_LEN]; 291 m5_secman_seal(&m, SK.k_root_auth, buf); 292 m5_subkeys g2, other; 293 m5_derive_subkeys(K_VOL, M5_ID_PRODUCTION, 2, &g2); /* other owner hierarchy generation */ 294 uint8_t kv2[32]; memset(kv2, 0x99, 32); 295 m5_derive_subkeys(kv2, M5_ID_PRODUCTION, 1, &other); /* other volume */ 296 CHECK(m5_secman_open(buf, sizeof buf, g2.k_root_auth, &o) == M5_ERR_AUTH); 297 CHECK(m5_secman_open(buf, sizeof buf, other.k_root_auth, &o) == M5_ERR_AUTH); 298 CHECK(m5_secman_open(buf, sizeof buf, SK.k_artifact, &o) == M5_ERR_AUTH); 299 } 300 301 static void t_secman_every_byte_tamper_refused(void) 302 { 303 m5_secman m = secman_fixture(), o; 304 uint8_t buf[M5_SECMAN_LEN]; 305 m5_secman_seal(&m, SK.k_root_auth, buf); 306 for (size_t i = 0; i < sizeof buf; i++) { 307 buf[i] ^= 0x01; 308 CHECK(m5_secman_open(buf, sizeof buf, SK.k_root_auth, &o) < 0); 309 buf[i] ^= 0x01; 310 } 311 CHECK(m5_secman_open(buf, sizeof buf - 1, SK.k_root_auth, &o) == M5_ERR_BOUNDS); 312 } 313 314 315 /* ---------- 2. envelopes ---------- */ 316 static void t_env_roundtrip_sizes(void) 317 { 318 static const size_t sizes[] = {0, 1, 63, 64, 65, 224, 1000}; 319 for (size_t k = 0; k < sizeof sizes / sizeof sizes[0]; k++) { 320 size_t len = m5_envelope_len(sizes[k], CS); 321 uint8_t *env = malloc(len), *pt = malloc(sizes[k] + 1), *out = malloc(sizes[k] + 1); 322 for (size_t i = 0; i < sizes[k]; i++) pt[i] = (uint8_t)rnd(); 323 size_t n = 0, m = 0; 324 CHECK(m5_envelope_seal(SK.k_artifact, &OBJ, NONCE8, CS, pt, sizes[k], env, len, &n) == M5_OK && n == len); 325 CHECK(m5_envelope_open(SK.k_artifact, M5_ID_PRODUCTION, &OBJ, env, n, out, sizes[k] + 1, &m) == M5_OK); 326 CHECK(m == sizes[k] && memcmp(out, pt, m) == 0); 327 free(env); free(pt); free(out); 328 } 329 /* default 64 KiB chunking, 3 chunks */ 330 size_t pl = 150000, len = m5_envelope_len(pl, M5_ENV_MAX_CHUNK); 331 uint8_t *env = malloc(len), *pt = malloc(pl), *out = malloc(pl); 332 memset(pt, 0x5A, pl); 333 size_t n = 0, m = 0; 334 CHECK(len == 64 + pl + 3 * 16); 335 CHECK(m5_envelope_seal(SK.k_artifact, &OBJ, NONCE8, M5_ENV_MAX_CHUNK, pt, pl, env, len, &n) == M5_OK); 336 CHECK(m5_envelope_open(SK.k_artifact, M5_ID_PRODUCTION, &OBJ, env, n, out, pl, &m) == M5_OK && m == pl); 337 CHECK(memcmp(out, pt, pl) == 0); 338 free(env); free(pt); free(out); 339 CHECK(m5_envelope_len(M5_ENV_MAX_PLAINTEXT + 1, M5_ENV_MAX_CHUNK) == 0); 340 CHECK(m5_envelope_len(10, 0) == 0 && m5_envelope_len(10, M5_ENV_MAX_CHUNK + 1) == 0); 341 CHECK(m5_envelope_len(M5_ENV_MAX_CHUNKS + 1, 1) == 0); 342 } 343 344 static void t_env_header_bounds_each_field(void) 345 { 346 uint8_t e[ENV_LEN]; 347 struct { size_t off; int width; uint64_t v; } edges[] = { 348 {16, 4, 0}, {16, 4, M5_ENV_MAX_CHUNK + 1}, {16, 4, 0xFFFFFFFFu}, {16, 4, 1}, 349 {20, 8, 0}, {20, 8, PT_LEN + 1}, {20, 8, M5_ENV_MAX_PLAINTEXT + 1}, {20, 8, UINT64_MAX}, 350 {20, 8, UINT64_MAX - 15}, 351 {8, 2, 2}, {8, 2, 0}, {10, 2, 2}, {12, 4, 1}, {12, 4, 0x80000000u}, 352 {60, 4, 1}, {60, 4, 0x80000000u}, 353 }; 354 for (size_t k = 0; k < sizeof edges / sizeof edges[0]; k++) { 355 memcpy(e, ENV, ENV_LEN); 356 if (edges[k].width == 8) put64(e + edges[k].off, edges[k].v); 357 else if (edges[k].width == 4) put32(e + edges[k].off, (uint32_t)edges[k].v); 358 else { e[edges[k].off] = (uint8_t)edges[k].v; e[edges[k].off + 1] = 0; } 359 open_refused(e, ENV_LEN, &OBJ, M5_ID_PRODUCTION); 360 } 361 m5_env_header h; 362 CHECK(m5_envelope_parse_header(ENV, ENV_LEN + 1, &h) == M5_ERR_BOUNDS); 363 CHECK(m5_envelope_parse_header(ENV, ENV_LEN - 1, &h) == M5_ERR_BOUNDS); 364 CHECK(m5_envelope_parse_header(ENV, 63, &h) == M5_ERR_BOUNDS); 365 CHECK(m5_envelope_parse_header(ENV, ENV_LEN, &h) == M5_OK && h.chunk_count == 4); 366 /* a trailing byte must not be ignored */ 367 uint8_t big[ENV_LEN + 1]; 368 memcpy(big, ENV, ENV_LEN); big[ENV_LEN] = 0; 369 CHECK(open_refused(big, sizeof big, &OBJ, M5_ID_PRODUCTION) == M5_ERR_BOUNDS); 370 /* nonzero reserved (Rust NonzeroReserved) */ 371 memcpy(e, ENV, ENV_LEN); e[63] = 1; 372 CHECK(open_refused(e, ENV_LEN, &OBJ, M5_ID_PRODUCTION) == M5_ERR_FORMAT); 373 } 374 375 /* The sealed bytes follow envelope.rs exactly: 64-byte V1 header and the 376 * AGENTS.md section 3 chunk AAD, rebuilt here by hand. */ 377 static void t_env_rust_v1_header_and_standard_aad(void) 378 { 379 CHECK(M5_ENV_HEADER_LEN == 64); 380 CHECK(memcmp(ENV, "AIENENV1", 8) == 0 && ENV[8] == 1 && ENV[9] == 0 && ENV[10] == 1 && ENV[11] == 0); 381 CHECK(all_zero(ENV + 12, 4)); 382 CHECK(ENV[16] == CS && all_zero(ENV + 17, 3)); 383 CHECK(ENV[20] == PT_LEN && all_zero(ENV + 21, 7)); 384 CHECK(memcmp(ENV + 28, OBJ.envelope_id, 16) == 0 && memcmp(ENV + 44, NONCE8, 8) == 0); 385 CHECK(ENV[52] == 3 && all_zero(ENV + 53, 7) && all_zero(ENV + 60, 4)); 386 uint8_t key[32]; 387 m5_derive_object_key(SK.k_artifact, &OBJ, key); 388 for (uint32_t c = 0; c < 4; c++) { 389 uint32_t n = c < 3 ? CS : PT_LEN - 3 * CS; 390 uint8_t aad[111], *p = aad, nonce[12], pt[CS]; 391 memcpy(p, "AIENOS-M5-CHUNK-V1", 19); p += 19; /* includes the trailing NUL */ 392 memcpy(p, OBJ.store_uuid, 16); p += 16; 393 *p++ = 7; *p++ = 0; /* object_kind u16le */ 394 *p++ = 1; *p++ = 0; /* object_version u16le */ 395 memcpy(p, ENV, 64); p += 64; 396 put32(p, c); p += 4; 397 put32(p, n); p += 4; 398 CHECK(p == aad + 111); 399 memcpy(nonce, NONCE8, 8); put32(nonce + 8, c); 400 CHECK(aienos_gcmsiv_open(key, nonce, aad, sizeof aad, ENV + CHUNK_OFF(c), n + 16, pt, n) == AIENOS_CRYPTO_OK); 401 CHECK(memcmp(pt, PT + c * CS, n) == 0); 402 } 403 } 404 405 static void t_env_hostile_header_fuzz(void) 406 { 407 static const uint64_t edge[] = {0, 1, 2, 15, 16, 63, 64, 65, 79, 80, 4095, 4096, 4097, 65535, 65536, 65537, 408 0x7FFFFFFF, 0xFFFFFFFF, 0x100000000ull, M5_ENV_MAX_PLAINTEXT, 409 M5_ENV_MAX_PLAINTEXT + 1, UINT64_MAX - 1, UINT64_MAX}; 410 const size_t ne = sizeof edge / sizeof edge[0]; 411 uint8_t tmp[1024]; 412 unsigned accepted = 0; 413 for (int it = 0; it < 20000; it++) { 414 size_t len = ENV_LEN; 415 memcpy(tmp, ENV, ENV_LEN); 416 switch (it % 5) { 417 case 0: for (int j = 1 + (int)(rnd() % 4); j > 0; j--) tmp[rnd() % 64] ^= (uint8_t)(1 + rnd() % 255); break; 418 case 1: { static const size_t off[] = {16, 20, 60}; size_t o = off[rnd() % 3]; 419 if (o == 20) put64(tmp + o, edge[rnd() % ne]); else put32(tmp + o, (uint32_t)edge[rnd() % ne]); } break; 420 case 2: len = (size_t)(rnd() % sizeof tmp); for (size_t i = ENV_LEN; i < len; i++) tmp[i] = (uint8_t)rnd(); break; 421 case 3: len = (size_t)(rnd() % sizeof tmp); for (size_t i = 0; i < len; i++) tmp[i] = (uint8_t)rnd(); 422 if (len >= 8) { memcpy(tmp, "AIENENV1", 8); } 423 break; 424 case 4: put32(tmp + 16, (uint32_t)edge[rnd() % ne]); put64(tmp + 20, edge[rnd() % ne]); 425 put32(tmp + 60, (uint32_t)edge[rnd() % ne]); len = (size_t)(rnd() % sizeof tmp); break; 426 } 427 if (len == ENV_LEN && memcmp(tmp, ENV, ENV_LEN) == 0) continue; 428 uint8_t *e = malloc(len ? len : 1); 429 memcpy(e, tmp, len); 430 uint8_t out[300]; 431 memset(out, 0xAA, sizeof out); 432 size_t n = 1; 433 int rc = m5_envelope_open(SK.k_artifact, M5_ID_PRODUCTION, &OBJ, e, len, out, sizeof out, &n); 434 if (rc == M5_OK) accepted++; 435 CHECK(rc < 0); 436 if (rc < 0) { CHECK(n == 0); CHECK(all_zero(out, sizeof out)); } 437 m5_env_header h; 438 (void)m5_envelope_parse_header(e, len, &h); 439 free(e); 440 } 441 CHECK(accepted == 0); 442 } 443 444 /* ---------- 3. all-or-nothing ---------- */ 445 static void t_all_or_nothing_late_chunk_failure_zeroes_output(void) 446 { 447 uint8_t e[ENV_LEN]; 448 for (unsigned c = 0; c < 4; c++) { 449 memcpy(e, ENV, ENV_LEN); 450 e[CHUNK_OFF(c) + 5] ^= 0x10; /* chunks before c verify, c fails */ 451 CHECK(open_refused(e, ENV_LEN, &OBJ, M5_ID_PRODUCTION) == M5_ERR_AUTH); 452 } 453 /* output buffer larger than plaintext: the whole buffer is zero, not just the prefix */ 454 memcpy(e, ENV, ENV_LEN); 455 e[CHUNK_OFF(3) + 40] ^= 1; /* last chunk tag */ 456 uint8_t out[4096]; 457 memset(out, 0xEE, sizeof out); 458 size_t n = 5; 459 CHECK(m5_envelope_open(SK.k_artifact, M5_ID_PRODUCTION, &OBJ, e, ENV_LEN, out, sizeof out, &n) == M5_ERR_AUTH); 460 CHECK(n == 0 && all_zero(out, sizeof out)); 461 /* too-small output is refused before any plaintext is written */ 462 memset(out, 0xEE, sizeof out); 463 CHECK(m5_envelope_open(SK.k_artifact, M5_ID_PRODUCTION, &OBJ, ENV, ENV_LEN, out, PT_LEN - 1, &n) == M5_ERR_SPACE); 464 CHECK(all_zero(out, PT_LEN - 1)); 465 } 466 467 /* ---------- 4. corruption matrix ---------- */ 468 static void t_corrupt_header(void) 469 { 470 uint8_t e[ENV_LEN]; 471 /* bytes that pass every structural check: nonce prefix, envelope id */ 472 memcpy(e, ENV, ENV_LEN); e[44] ^= 1; 473 CHECK(open_refused(e, ENV_LEN, &OBJ, M5_ID_PRODUCTION) == M5_ERR_AUTH); 474 memcpy(e, ENV, ENV_LEN); e[28] ^= 1; 475 CHECK(open_refused(e, ENV_LEN, &OBJ, M5_ID_PRODUCTION) == M5_ERR_BINDING); 476 m5_object_binding b = OBJ; b.envelope_id[0] ^= 1; /* attacker relabels the expectation too */ 477 CHECK(open_refused(e, ENV_LEN, &b, M5_ID_PRODUCTION) == M5_ERR_AUTH); 478 memcpy(e, ENV, ENV_LEN); e[0] ^= 1; 479 CHECK(open_refused(e, ENV_LEN, &OBJ, M5_ID_PRODUCTION) == M5_ERR_FORMAT); 480 } 481 static void corrupt_chunk(unsigned c) 482 { 483 uint8_t e[ENV_LEN]; 484 memcpy(e, ENV, ENV_LEN); e[CHUNK_OFF(c)] ^= 0x80; 485 CHECK(open_refused(e, ENV_LEN, &OBJ, M5_ID_PRODUCTION) == M5_ERR_AUTH); 486 } 487 static void t_corrupt_first_chunk(void) { corrupt_chunk(0); } 488 static void t_corrupt_middle_chunk(void) { corrupt_chunk(1); corrupt_chunk(2); } 489 static void t_corrupt_last_chunk(void) { corrupt_chunk(3); } 490 491 static int recover(const uint8_t *commit, const uint8_t *anchor, size_t alen, const m5_candidate *c, size_t n, 492 uint8_t *out, size_t cap, size_t *chosen) 493 { 494 size_t ol = 9; 495 memset(out, 0xAA, cap); 496 int rc = m5_recover_object(SK.k_root_auth, SK.k_artifact, M5_ID_PRODUCTION, OBJ.store_uuid, 497 commit, M5_COMMIT_LEN, anchor, alen, c, n, out, cap, &ol, chosen); 498 if (rc < 0) { CHECK(ol == 0); CHECK(all_zero(out, cap)); CHECK(*chosen == SIZE_MAX); } 499 else CHECK(ol == PT_LEN); 500 return rc; 501 } 502 503 static void t_corrupt_commit_record(void) 504 { 505 m5_candidate c[1] = {{ENV, ENV_LEN}}; 506 uint8_t out[PT_LEN]; size_t ch; 507 uint8_t bad[M5_COMMIT_LEN]; 508 for (size_t i = 0; i < M5_COMMIT_LEN; i++) { 509 memcpy(bad, COMMIT, sizeof bad); bad[i] ^= 0x04; 510 CHECK(recover(bad, ANCHOR, M5_ANCHOR_LEN, c, 1, out, sizeof out, &ch) < 0); 511 } 512 memcpy(bad, COMMIT, sizeof bad); bad[90] ^= 1; /* envelope digest field */ 513 CHECK(recover(bad, ANCHOR, M5_ANCHOR_LEN, c, 1, out, sizeof out, &ch) == M5_ERR_AUTH); 514 m5_commit co; 515 CHECK(m5_commit_open(bad, sizeof bad, SK.k_root_auth, M5_ID_PRODUCTION, &co) == M5_ERR_AUTH); 516 } 517 518 static void t_corrupt_rollback_anchor(void) 519 { 520 m5_candidate c[1] = {{ENV, ENV_LEN}}; 521 uint8_t out[PT_LEN]; size_t ch; 522 uint8_t bad[M5_ANCHOR_LEN]; 523 for (size_t i = 0; i < M5_ANCHOR_LEN; i++) { 524 memcpy(bad, ANCHOR, sizeof bad); bad[i] ^= 0x02; 525 CHECK(recover(COMMIT, bad, M5_ANCHOR_LEN, c, 1, out, sizeof out, &ch) < 0); 526 } 527 memcpy(bad, ANCHOR, sizeof bad); bad[48] ^= 1; /* counter */ 528 m5_anchor a; 529 CHECK(m5_anchor_open(bad, sizeof bad, SK.k_root_auth, M5_ID_PRODUCTION, &a) == M5_ERR_AUTH); 530 CHECK(recover(COMMIT, ANCHOR, M5_ANCHOR_LEN - 1, c, 1, out, sizeof out, &ch) == M5_ERR_BOUNDS); 531 } 532 533 /* Key generation is the header key_epoch (inside every chunk's AAD) and is 534 * also bound by the commit record, which is checked after sealing. */ 535 static void t_corrupt_key_generation(void) 536 { 537 uint8_t e[ENV_LEN]; 538 memcpy(e, ENV, ENV_LEN); put64(e + 52, 4); 539 CHECK(open_refused(e, ENV_LEN, &OBJ, M5_ID_PRODUCTION) == M5_ERR_BINDING); 540 m5_object_binding b = OBJ; b.key_generation = 4; /* attacker also relabels the expectation */ 541 CHECK(open_refused(e, ENV_LEN, &b, M5_ID_PRODUCTION) == M5_ERR_AUTH); 542 CHECK(open_refused(ENV, ENV_LEN, &b, M5_ID_PRODUCTION) == M5_ERR_BINDING); 543 /* commit record layer: tampered key_generation field */ 544 m5_candidate c[1] = {{ENV, ENV_LEN}}; 545 uint8_t out[PT_LEN], bad[M5_COMMIT_LEN]; size_t ch; 546 memcpy(bad, COMMIT, sizeof bad); bad[56] ^= 1; 547 m5_commit co; 548 CHECK(m5_commit_open(bad, sizeof bad, SK.k_root_auth, M5_ID_PRODUCTION, &co) == M5_ERR_AUTH); 549 CHECK(recover(bad, ANCHOR, M5_ANCHOR_LEN, c, 1, out, sizeof out, &ch) == M5_ERR_AUTH); 550 /* an authentic commit naming another key generation for these bytes */ 551 uint8_t cb[M5_COMMIT_LEN]; 552 make_commit(&b, SG, ENV, ENV_LEN, 5, SK.k_root_auth, cb); 553 CHECK(m5_commit_open(cb, sizeof cb, SK.k_root_auth, M5_ID_PRODUCTION, &co) == M5_OK); 554 CHECK(m5_commit_check_object(&co, ENV, ENV_LEN) == M5_ERR_BINDING); 555 } 556 557 /* Store generation is not in the envelope (AGENTS.md section 3): it is bound 558 * by the commit record and checked against the anchor. */ 559 static void t_corrupt_store_generation(void) 560 { 561 m5_candidate c[1] = {{ENV, ENV_LEN}}; 562 uint8_t out[PT_LEN], bad[M5_COMMIT_LEN], cb[M5_COMMIT_LEN], ab[M5_ANCHOR_LEN]; size_t ch; 563 m5_commit co; 564 /* tampered store_generation field in the commit record */ 565 memcpy(bad, COMMIT, sizeof bad); bad[48] ^= 1; 566 CHECK(m5_commit_open(bad, sizeof bad, SK.k_root_auth, M5_ID_PRODUCTION, &co) == M5_ERR_AUTH); 567 CHECK(recover(bad, ANCHOR, M5_ANCHOR_LEN, c, 1, out, sizeof out, &ch) == M5_ERR_AUTH); 568 /* authentic commit from an older store generation: rollback against the anchor */ 569 make_commit(&OBJ, SG - 1, ENV, ENV_LEN, 6, SK.k_root_auth, cb); 570 CHECK(recover(cb, ANCHOR, M5_ANCHOR_LEN, c, 1, out, sizeof out, &ch) == M5_ERR_ROLLBACK); 571 /* authentic commit claiming another store generation at the anchored counter: fork */ 572 make_commit(&OBJ, SG + 1, ENV, ENV_LEN, 5, SK.k_root_auth, cb); 573 CHECK(recover(cb, ANCHOR, M5_ANCHOR_LEN, c, 1, out, sizeof out, &ch) == M5_ERR_ROLLBACK); 574 /* the same immutable envelope reused in the next store generation is valid */ 575 make_commit(&OBJ, SG + 1, ENV, ENV_LEN, 6, SK.k_root_auth, cb); 576 CHECK(recover(cb, ANCHOR, M5_ANCHOR_LEN, c, 1, out, sizeof out, &ch) == M5_OK && memcmp(out, PT, PT_LEN) == 0); 577 make_anchor(M5_ID_PRODUCTION, OBJ.store_uuid, SG + 1, 3, 6, cb, SK.k_root_auth, ab); 578 CHECK(recover(cb, ab, M5_ANCHOR_LEN, c, 1, out, sizeof out, &ch) == M5_OK); 579 /* and the old generation's commit is then a rollback */ 580 CHECK(recover(COMMIT, ab, M5_ANCHOR_LEN, c, 1, out, sizeof out, &ch) == M5_ERR_ROLLBACK); 581 } 582 583 /* Post-seal ObjectId check: SHA-256 of the bytes must match the commit. */ 584 static void t_commit_check_object(void) 585 { 586 m5_commit co; 587 CHECK(m5_commit_open(COMMIT, M5_COMMIT_LEN, SK.k_root_auth, M5_ID_PRODUCTION, &co) == M5_OK); 588 CHECK(m5_commit_check_object(&co, ENV, ENV_LEN) == M5_OK); 589 uint8_t e[ENV_LEN]; 590 memcpy(e, ENV, ENV_LEN); e[CHUNK_OFF(2) + 3] ^= 1; 591 CHECK(m5_commit_check_object(&co, e, ENV_LEN) == M5_ERR_TORN); 592 CHECK(m5_commit_check_object(&co, ENV, ENV_LEN - 1) == M5_ERR_TORN); 593 /* authentic commit, right ObjectId, but naming another envelope id */ 594 m5_object_binding b = OBJ; b.envelope_id[3] ^= 1; 595 uint8_t cb[M5_COMMIT_LEN]; 596 make_commit(&b, SG, ENV, ENV_LEN, 5, SK.k_root_auth, cb); 597 CHECK(m5_commit_open(cb, sizeof cb, SK.k_root_auth, M5_ID_PRODUCTION, &co) == M5_OK); 598 CHECK(m5_commit_check_object(&co, ENV, ENV_LEN) == M5_ERR_BINDING); 599 } 600 601 static void t_truncate_drop_last_chunk(void) 602 { 603 uint8_t e[ENV_LEN]; 604 size_t tl = CHUNK_OFF(3); 605 CHECK(open_refused(ENV, tl, &OBJ, M5_ID_PRODUCTION) == M5_ERR_BOUNDS); 606 memcpy(e, ENV, tl); put64(e + 20, 192); /* header rewritten to match */ 607 CHECK(open_refused(e, tl, &OBJ, M5_ID_PRODUCTION) == M5_ERR_AUTH); 608 } 609 610 static void t_extend_append_chunk(void) 611 { 612 uint8_t e[ENV_LEN + CS + 16]; 613 memcpy(e, ENV, ENV_LEN); 614 memcpy(e + ENV_LEN, ENV + CHUNK_OFF(0), CS + 16); 615 CHECK(open_refused(e, sizeof e, &OBJ, M5_ID_PRODUCTION) == M5_ERR_BOUNDS); 616 /* rewrite the last chunk position: 4 full chunks + appended chunk 0 as #5 */ 617 uint8_t f[64 + 5 * (CS + 16)]; 618 memset(f, 0, sizeof f); 619 memcpy(f, ENV, ENV_LEN); 620 memcpy(f + CHUNK_OFF(4), ENV + CHUNK_OFF(0), CS + 16); 621 put64(f + 20, 4 * CS + CS); 622 CHECK(open_refused(f, sizeof f, &OBJ, M5_ID_PRODUCTION) == M5_ERR_AUTH); 623 } 624 625 static void t_reorder_chunks(void) 626 { 627 uint8_t e[ENV_LEN]; 628 memcpy(e, ENV, ENV_LEN); 629 memcpy(e + CHUNK_OFF(1), ENV + CHUNK_OFF(2), CS + 16); 630 memcpy(e + CHUNK_OFF(2), ENV + CHUNK_OFF(1), CS + 16); 631 CHECK(open_refused(e, ENV_LEN, &OBJ, M5_ID_PRODUCTION) == M5_ERR_AUTH); 632 } 633 634 static void t_splice_cross_object(void) 635 { 636 m5_object_binding b2 = OBJ; 637 b2.envelope_id[0] ^= 0xFF; 638 uint8_t env2[ENV_LEN], e[ENV_LEN]; 639 size_t n; 640 CHECK(m5_envelope_seal(SK.k_artifact, &b2, NONCE8, CS, PT, PT_LEN, env2, sizeof env2, &n) == M5_OK); 641 /* whole envelope of object 2 presented as object 1 */ 642 CHECK(open_refused(env2, ENV_LEN, &OBJ, M5_ID_PRODUCTION) == M5_ERR_BINDING); 643 /* one chunk of object 2 spliced into object 1 (same nonce prefix, same plaintext) */ 644 memcpy(e, ENV, ENV_LEN); 645 memcpy(e + CHUNK_OFF(1), env2 + CHUNK_OFF(1), CS + 16); 646 CHECK(open_refused(e, ENV_LEN, &OBJ, M5_ID_PRODUCTION) == M5_ERR_AUTH); 647 /* chunk from another key generation of the same object */ 648 m5_object_binding b3 = OBJ; b3.key_generation = 4; 649 uint8_t env3[ENV_LEN]; 650 m5_envelope_seal(SK.k_artifact, &b3, NONCE8, CS, PT, PT_LEN, env3, sizeof env3, &n); 651 memcpy(e, ENV, ENV_LEN); 652 memcpy(e + CHUNK_OFF(2), env3 + CHUNK_OFF(2), CS + 16); 653 CHECK(open_refused(e, ENV_LEN, &OBJ, M5_ID_PRODUCTION) == M5_ERR_AUTH); 654 /* other object kind or store uuid */ 655 m5_object_binding b4 = OBJ; b4.object_kind = 8; 656 CHECK(open_refused(ENV, ENV_LEN, &b4, M5_ID_PRODUCTION) == M5_ERR_AUTH); 657 b4 = OBJ; b4.store_uuid[0] ^= 1; 658 CHECK(open_refused(ENV, ENV_LEN, &b4, M5_ID_PRODUCTION) == M5_ERR_AUTH); 659 } 660 661 /* ---------- 5. anti-rollback ---------- */ 662 static m5_disk_state disk_now(void) 663 { 664 m5_disk_state d; 665 d.store_generation = 9; d.key_generation = 3; d.counter = 5; 666 sha256_hash(COMMIT, M5_COMMIT_LEN, d.commit_digest); 667 return d; 668 } 669 #define EVAL(buf, len, gen, d) m5_evaluate_anti_rollback(SK.k_root_auth, M5_ID_PRODUCTION, OBJ.store_uuid, buf, len, gen, d) 670 671 static void t_anchor_caller_buffer_roundtrip(void) 672 { 673 m5_anchor a, o; 674 memset(&a, 0, sizeof a); 675 a.identity_class = M5_ID_PRODUCTION; memcpy(a.store_uuid, OBJ.store_uuid, 16); 676 a.store_generation = 9; a.key_generation = 3; a.counter = 5; 677 uint8_t buf[M5_ANCHOR_LEN]; 678 CHECK(m5_anchor_seal(&a, SK.k_root_auth, buf, sizeof buf - 1) == M5_ERR_SPACE); 679 CHECK(m5_anchor_seal(&a, SK.k_root_auth, buf, sizeof buf) == M5_OK); 680 CHECK(m5_anchor_open(buf, sizeof buf, SK.k_root_auth, M5_ID_PRODUCTION, &o) == M5_OK); 681 CHECK(o.counter == 5 && o.store_generation == 9 && o.key_generation == 3); 682 } 683 684 static void t_rb_valid_resume_and_advance(void) 685 { 686 m5_disk_state d = disk_now(); 687 CHECK(EVAL(ANCHOR, M5_ANCHOR_LEN, 0, &d) == M5_RB_VALID_RESUME); 688 d.counter = 6; d.store_generation = 10; d.commit_digest[0] ^= 1; 689 CHECK(EVAL(ANCHOR, M5_ANCHOR_LEN, 0, &d) == M5_RB_PREPARED_ADVANCE); 690 d.key_generation = 4; 691 CHECK(EVAL(ANCHOR, M5_ANCHOR_LEN, 0, &d) == M5_RB_PREPARED_ADVANCE); 692 } 693 694 static void t_rb_older_store_generation_refused(void) 695 { 696 m5_disk_state d = disk_now(); 697 d.store_generation = 8; d.counter = 6; 698 CHECK(EVAL(ANCHOR, M5_ANCHOR_LEN, 0, &d) == M5_ERR_ROLLBACK); 699 } 700 static void t_rb_older_key_generation_refused(void) 701 { 702 m5_disk_state d = disk_now(); 703 d.key_generation = 2; d.counter = 6; 704 CHECK(EVAL(ANCHOR, M5_ANCHOR_LEN, 0, &d) == M5_ERR_ROLLBACK); 705 } 706 static void t_rb_lower_counter_refused(void) 707 { 708 m5_disk_state d = disk_now(); 709 d.counter = 4; 710 CHECK(EVAL(ANCHOR, M5_ANCHOR_LEN, 0, &d) == M5_ERR_ROLLBACK); 711 d.counter = 0; 712 CHECK(EVAL(ANCHOR, M5_ANCHOR_LEN, 0, &d) == M5_ERR_ROLLBACK); 713 } 714 static void t_rb_fork_at_same_counter_refused(void) 715 { 716 m5_disk_state d = disk_now(); 717 d.commit_digest[31] ^= 1; 718 CHECK(EVAL(ANCHOR, M5_ANCHOR_LEN, 0, &d) == M5_ERR_ROLLBACK); 719 d = disk_now(); d.store_generation = 10; 720 CHECK(EVAL(ANCHOR, M5_ANCHOR_LEN, 0, &d) == M5_ERR_ROLLBACK); 721 } 722 static void t_rb_counter_jump_inconsistent(void) 723 { 724 m5_disk_state d = disk_now(); 725 d.counter = 7; 726 CHECK(EVAL(ANCHOR, M5_ANCHOR_LEN, 0, &d) == M5_ERR_INCONSISTENT); 727 /* anchor at the counter ceiling: counter 0 is a rollback, not a wrapped advance */ 728 uint8_t top[M5_ANCHOR_LEN]; 729 make_anchor(M5_ID_PRODUCTION, OBJ.store_uuid, 9, 3, UINT64_MAX, COMMIT, SK.k_root_auth, top); 730 d = disk_now(); d.counter = 0; 731 CHECK(EVAL(top, M5_ANCHOR_LEN, 0, &d) == M5_ERR_ROLLBACK); 732 } 733 static void t_rb_forged_anchor_refused(void) 734 { 735 m5_disk_state d = disk_now(); 736 uint8_t forged[M5_ANCHOR_LEN]; 737 uint8_t fake[32]; memset(fake, 0x77, 32); 738 make_anchor(M5_ID_PRODUCTION, OBJ.store_uuid, 0, 0, 0, COMMIT, fake, forged); /* attacker lowers the anchor */ 739 d.counter = 1; d.store_generation = 1; d.key_generation = 1; 740 CHECK(EVAL(forged, M5_ANCHOR_LEN, 0, &d) == M5_ERR_AUTH); 741 make_anchor(M5_ID_PRODUCTION, OBJ.store_uuid, 9, 3, 5, COMMIT, SK.k_artifact, forged); /* wrong subkey */ 742 d = disk_now(); 743 CHECK(EVAL(forged, M5_ANCHOR_LEN, 0, &d) == M5_ERR_AUTH); 744 } 745 static void t_rb_missing_anchor_and_genesis(void) 746 { 747 m5_disk_state d = disk_now(); 748 CHECK(EVAL(NULL, 0, 0, &d) == M5_ERR_ROLLBACK); 749 CHECK(EVAL(NULL, 0, 1, &d) == M5_RB_GENESIS); 750 } 751 static void t_rb_anchor_other_store_refused(void) 752 { 753 uint8_t other[16], a2[M5_ANCHOR_LEN]; 754 memset(other, 0xA7, 16); 755 make_anchor(M5_ID_PRODUCTION, other, 9, 3, 5, COMMIT, SK.k_root_auth, a2); 756 m5_disk_state d = disk_now(); 757 CHECK(EVAL(a2, M5_ANCHOR_LEN, 0, &d) == M5_ERR_BINDING); 758 } 759 760 /* ---------- 6. identity separation ---------- */ 761 static void t_identity_production_refuses_test(void) 762 { 763 m5_object_binding tb; binding_init(&tb, M5_ID_TEST); 764 uint8_t env[ENV_LEN]; size_t n; 765 CHECK(m5_envelope_seal(SK.k_artifact, &tb, NONCE8, CS, PT, PT_LEN, env, sizeof env, &n) == M5_OK); 766 /* production mode, TEST-class object, caller expects PRODUCTION: the class 767 * is bound only through the object key, so authentication fails */ 768 CHECK(open_refused(env, ENV_LEN, &OBJ, M5_ID_PRODUCTION) == M5_ERR_AUTH); 769 /* production mode, caller expectation TEST */ 770 CHECK(open_refused(env, ENV_LEN, &tb, M5_ID_PRODUCTION) == M5_ERR_IDENTITY); 771 /* header PRODUCTION but expectation TEST in production mode */ 772 CHECK(open_refused(ENV, ENV_LEN, &tb, M5_ID_PRODUCTION) == M5_ERR_IDENTITY); 773 /* a TEST-hierarchy manifest does not authenticate under the production key; 774 * TEST commit and anchor records under the same key are refused by class */ 775 m5_secman m = secman_fixture(), o; 776 uint8_t sb[M5_SECMAN_LEN]; 777 m5_secman_seal(&m, SK_TEST.k_root_auth, sb); 778 CHECK(m5_secman_open(sb, sizeof sb, SK.k_root_auth, &o) == M5_ERR_AUTH); 779 uint8_t cb[M5_COMMIT_LEN], ab[M5_ANCHOR_LEN]; 780 m5_commit co; m5_anchor ao; 781 make_commit(&tb, SG, env, ENV_LEN, 5, SK.k_root_auth, cb); 782 CHECK(m5_commit_open(cb, sizeof cb, SK.k_root_auth, M5_ID_PRODUCTION, &co) == M5_ERR_IDENTITY); 783 make_anchor(M5_ID_TEST, OBJ.store_uuid, 9, 3, 5, cb, SK.k_root_auth, ab); 784 CHECK(m5_anchor_open(ab, sizeof ab, SK.k_root_auth, M5_ID_PRODUCTION, &ao) == M5_ERR_IDENTITY); 785 } 786 787 static void t_identity_test_refuses_production(void) 788 { 789 m5_object_binding tb; binding_init(&tb, M5_ID_TEST); 790 uint8_t out[PT_LEN]; size_t n = 1; 791 memset(out, 0xAA, sizeof out); 792 /* a PRODUCTION object opened in TEST mode: the class is in the key */ 793 CHECK(m5_envelope_open(SK.k_artifact, M5_ID_TEST, &tb, ENV, ENV_LEN, out, sizeof out, &n) == M5_ERR_AUTH); 794 CHECK(all_zero(out, sizeof out)); 795 m5_secman m = secman_fixture(), o; 796 uint8_t sb[M5_SECMAN_LEN]; 797 m5_secman_seal(&m, SK.k_root_auth, sb); 798 CHECK(m5_secman_open(sb, sizeof sb, SK_TEST.k_root_auth, &o) == M5_ERR_AUTH); 799 m5_commit co; 800 CHECK(m5_commit_open(COMMIT, M5_COMMIT_LEN, SK.k_root_auth, M5_ID_TEST, &co) == M5_ERR_IDENTITY); 801 /* a TEST-hierarchy store works in TEST mode */ 802 m5_secman t = secman_fixture(); 803 m5_secman_seal(&t, SK_TEST.k_root_auth, sb); 804 CHECK(m5_secman_open(sb, sizeof sb, SK_TEST.k_root_auth, &o) == M5_OK); 805 } 806 807 static void t_identity_relabel_breaks_mac(void) 808 { 809 m5_secman t = secman_fixture(), o; 810 uint8_t sb[M5_SECMAN_LEN]; 811 m5_secman_seal(&t, SK_TEST.k_root_auth, sb); 812 CHECK(m5_secman_open(sb, sizeof sb, SK_TEST.k_root_auth, &o) == M5_OK); 813 /* the manifest carries no class byte: its class is bound by the hierarchy key */ 814 CHECK(m5_secman_open(sb, sizeof sb, SK.k_root_auth, &o) == M5_ERR_AUTH); 815 /* a TEST envelope presented as PRODUCTION */ 816 m5_object_binding tb; binding_init(&tb, M5_ID_TEST); 817 uint8_t env[ENV_LEN]; size_t n; 818 m5_envelope_seal(SK_TEST.k_artifact, &tb, NONCE8, CS, PT, PT_LEN, env, sizeof env, &n); 819 CHECK(open_refused(env, ENV_LEN, &OBJ, M5_ID_PRODUCTION) == M5_ERR_AUTH); 820 /* relabel a TEST commit record and anchor */ 821 uint8_t cb[M5_COMMIT_LEN], ab[M5_ANCHOR_LEN]; 822 m5_commit co; m5_anchor ao; 823 make_commit(&tb, SG, env, ENV_LEN, 5, SK_TEST.k_root_auth, cb); 824 cb[10] = M5_ID_PRODUCTION; 825 CHECK(m5_commit_open(cb, sizeof cb, SK_TEST.k_root_auth, M5_ID_PRODUCTION, &co) == M5_ERR_AUTH); 826 make_anchor(M5_ID_TEST, OBJ.store_uuid, 9, 3, 5, cb, SK_TEST.k_root_auth, ab); 827 ab[10] = M5_ID_PRODUCTION; 828 CHECK(m5_anchor_open(ab, sizeof ab, SK_TEST.k_root_auth, M5_ID_PRODUCTION, &ao) == M5_ERR_AUTH); 829 } 830 831 /* ---------- 7. migration ---------- */ 832 static m5_migration mig_fixture(void) 833 { 834 m5_migration m; 835 memset(&m, 0, sizeof m); 836 m.identity_class = M5_ID_PRODUCTION; 837 memset(m.agent_root_id, 0x32, 32); 838 memset(m.genesis_store_uuid, 0xA0, 16); 839 memset(m.source_store_uuid, 0xA0, 16); 840 memset(m.dest_store_uuid, 0xD0, 16); 841 m.source_store_generation = 9; 842 m.source_anchor_counter = 5; 843 m.migration_counter = 2; 844 m.owner_hierarchy_generation = 1; 845 sha256_hash(COMMIT, M5_COMMIT_LEN, m.source_commit_digest); 846 return m; 847 } 848 #define MIG_AUTH(buf, e, last, o) m5_migration_authorize(buf, M5_MIGRATION_LEN, SK.k_root_auth, M5_ID_PRODUCTION, e, last, o) 849 850 static void t_mig_authorized(void) 851 { 852 m5_migration m = mig_fixture(), o; 853 uint8_t b[M5_MIGRATION_LEN]; 854 CHECK(m5_migration_seal(&m, SK.k_root_auth, b) == M5_OK); 855 CHECK(MIG_AUTH(b, &m, 1, &o) == M5_OK && o.migration_counter == 2); 856 } 857 static void t_mig_wrong_key_refused(void) 858 { 859 m5_migration m = mig_fixture(), o; 860 uint8_t b[M5_MIGRATION_LEN]; 861 m5_migration_seal(&m, SK.k_artifact, b); 862 CHECK(MIG_AUTH(b, &m, 1, &o) == M5_ERR_AUTH); 863 m5_migration_seal(&m, SK.k_root_auth, b); 864 for (size_t i = 0; i < sizeof b; i++) { 865 b[i] ^= 0x08; 866 CHECK(MIG_AUTH(b, &m, 1, &o) < 0); 867 b[i] ^= 0x08; 868 } 869 } 870 static void t_mig_replay_refused(void) 871 { 872 m5_migration m = mig_fixture(), o; 873 uint8_t b[M5_MIGRATION_LEN]; 874 m5_migration_seal(&m, SK.k_root_auth, b); 875 CHECK(MIG_AUTH(b, &m, 2, &o) == M5_ERR_REPLAY); 876 CHECK(MIG_AUTH(b, &m, 7, &o) == M5_ERR_REPLAY); 877 } 878 static void t_mig_binding_refusals(void) 879 { 880 m5_migration m = mig_fixture(), e, o; 881 uint8_t b[M5_MIGRATION_LEN]; 882 m5_migration_seal(&m, SK.k_root_auth, b); 883 e = m; e.source_store_uuid[0] ^= 1; CHECK(MIG_AUTH(b, &e, 1, &o) == M5_ERR_BINDING); 884 e = m; e.dest_store_uuid[0] ^= 1; CHECK(MIG_AUTH(b, &e, 1, &o) == M5_ERR_BINDING); 885 e = m; e.source_store_generation = 8; CHECK(MIG_AUTH(b, &e, 1, &o) == M5_ERR_BINDING); 886 e = m; e.source_anchor_counter = 4; CHECK(MIG_AUTH(b, &e, 1, &o) == M5_ERR_BINDING); 887 e = m; e.source_commit_digest[0] ^= 1; CHECK(MIG_AUTH(b, &e, 1, &o) == M5_ERR_BINDING); 888 e = m; e.agent_root_id[0] ^= 1; CHECK(MIG_AUTH(b, &e, 1, &o) == M5_ERR_BINDING); 889 e = m; e.genesis_store_uuid[0] ^= 1; CHECK(MIG_AUTH(b, &e, 1, &o) == M5_ERR_BINDING); 890 e = m; e.owner_hierarchy_generation = 2; CHECK(MIG_AUTH(b, &e, 1, &o) == M5_ERR_BINDING); 891 CHECK(all_zero((uint8_t *)&o, sizeof o)); 892 /* migrating a store onto itself */ 893 m5_migration s = m; memcpy(s.dest_store_uuid, s.source_store_uuid, 16); 894 m5_migration_seal(&s, SK.k_root_auth, b); 895 CHECK(MIG_AUTH(b, &s, 1, &o) == M5_ERR_BINDING); 896 } 897 static void t_mig_test_identity_refused_in_production(void) 898 { 899 m5_migration m = mig_fixture(), o; 900 m.identity_class = M5_ID_TEST; 901 uint8_t b[M5_MIGRATION_LEN]; 902 m5_migration_seal(&m, SK.k_root_auth, b); 903 m5_migration e = mig_fixture(); 904 CHECK(MIG_AUTH(b, &e, 1, &o) == M5_ERR_IDENTITY); 905 } 906 907 /* ---------- 8. deterministic recovery ---------- */ 908 static void t_recovery_deterministic(void) 909 { 910 /* candidates: committed version, an older version, an uncommitted newer torn copy, junk */ 911 m5_object_binding old = OBJ; old.envelope_id[0] ^= 0x11; 912 m5_object_binding nw = OBJ; nw.envelope_id[0] ^= 0x22; 913 uint8_t env_old[ENV_LEN], env_new[ENV_LEN], junk[100]; 914 size_t n; 915 m5_envelope_seal(SK.k_artifact, &old, NONCE8, CS, PT, PT_LEN, env_old, sizeof env_old, &n); 916 m5_envelope_seal(SK.k_artifact, &nw, NONCE8, CS, PT, PT_LEN, env_new, sizeof env_new, &n); 917 env_new[CHUNK_OFF(3) + 1] ^= 1; /* torn, never committed */ 918 memset(junk, 0x3C, sizeof junk); 919 m5_candidate base[4] = {{env_old, ENV_LEN}, {ENV, ENV_LEN}, {env_new, ENV_LEN}, {junk, sizeof junk}}; 920 uint8_t first[PT_LEN], out[PT_LEN]; 921 size_t ch; 922 CHECK(recover(COMMIT, ANCHOR, M5_ANCHOR_LEN, base, 4, first, sizeof first, &ch) == M5_OK); 923 CHECK(ch == 1 && memcmp(first, PT, PT_LEN) == 0); 924 for (int r = 0; r < 64; r++) { 925 m5_candidate c[4]; 926 memcpy(c, base, sizeof c); 927 for (int i = 3; i > 0; i--) { int j = (int)(rnd() % (uint64_t)(i + 1)); m5_candidate t = c[i]; c[i] = c[j]; c[j] = t; } 928 CHECK(recover(COMMIT, ANCHOR, M5_ANCHOR_LEN, c, 4, out, sizeof out, &ch) == M5_OK); 929 CHECK(ch < 4 && c[ch].bytes == ENV); 930 CHECK(memcmp(out, first, PT_LEN) == 0); 931 } 932 } 933 static void t_recovery_torn_refused(void) 934 { 935 uint8_t torn[ENV_LEN], out[PT_LEN]; 936 size_t ch; 937 memcpy(torn, ENV, ENV_LEN); 938 memset(torn + CHUNK_OFF(2), 0, ENV_LEN - CHUNK_OFF(2)); /* tail never reached the media */ 939 m5_candidate c[1] = {{torn, ENV_LEN}}; 940 CHECK(recover(COMMIT, ANCHOR, M5_ANCHOR_LEN, c, 1, out, sizeof out, &ch) == M5_ERR_TORN); 941 m5_candidate c2[1] = {{ENV, CHUNK_OFF(2)}}; 942 CHECK(recover(COMMIT, ANCHOR, M5_ANCHOR_LEN, c2, 1, out, sizeof out, &ch) == M5_ERR_TORN); 943 CHECK(recover(COMMIT, ANCHOR, M5_ANCHOR_LEN, NULL, 0, out, sizeof out, &ch) == M5_ERR_TORN); 944 } 945 static void t_recovery_ambiguous_refused(void) 946 { 947 uint8_t torn[ENV_LEN], out[PT_LEN]; 948 size_t ch; 949 memcpy(torn, ENV, ENV_LEN); 950 torn[CHUNK_OFF(1)] ^= 1; 951 m5_candidate c[2] = {{ENV, ENV_LEN}, {torn, ENV_LEN}}; 952 CHECK(recover(COMMIT, ANCHOR, M5_ANCHOR_LEN, c, 2, out, sizeof out, &ch) == M5_ERR_AMBIGUOUS); 953 m5_candidate d[2] = {{torn, ENV_LEN}, {ENV, ENV_LEN}}; 954 CHECK(recover(COMMIT, ANCHOR, M5_ANCHOR_LEN, d, 2, out, sizeof out, &ch) == M5_ERR_AMBIGUOUS); 955 /* identical duplicate copies are not ambiguous */ 956 uint8_t dup[ENV_LEN]; 957 memcpy(dup, ENV, ENV_LEN); 958 m5_candidate e[2] = {{dup, ENV_LEN}, {ENV, ENV_LEN}}; 959 CHECK(recover(COMMIT, ANCHOR, M5_ANCHOR_LEN, e, 2, out, sizeof out, &ch) == M5_OK && ch == 0); 960 } 961 static void t_recovery_old_commit_is_rollback(void) 962 { 963 /* anchor advanced to counter 6 for a newer commit; the old commit (counter 5) is replayed */ 964 m5_object_binding nw = OBJ; nw.envelope_id[0] ^= 0x22; 965 uint8_t env_new[ENV_LEN], cnew[M5_COMMIT_LEN], anew[M5_ANCHOR_LEN], out[PT_LEN]; 966 size_t n, ch; 967 m5_envelope_seal(SK.k_artifact, &nw, NONCE8, CS, PT, PT_LEN, env_new, sizeof env_new, &n); 968 make_commit(&nw, 10, env_new, ENV_LEN, 6, SK.k_root_auth, cnew); 969 make_anchor(M5_ID_PRODUCTION, OBJ.store_uuid, 10, 3, 6, cnew, SK.k_root_auth, anew); 970 m5_candidate c[2] = {{ENV, ENV_LEN}, {env_new, ENV_LEN}}; 971 CHECK(recover(COMMIT, anew, M5_ANCHOR_LEN, c, 2, out, sizeof out, &ch) == M5_ERR_ROLLBACK); 972 CHECK(recover(cnew, anew, M5_ANCHOR_LEN, c, 2, out, sizeof out, &ch) == M5_OK && ch == 1); 973 /* new commit written, anchor not yet advanced: prepared advance is recoverable */ 974 CHECK(recover(cnew, ANCHOR, M5_ANCHOR_LEN, c, 2, out, sizeof out, &ch) == M5_OK && ch == 1); 975 /* missing anchor is not silently treated as genesis */ 976 CHECK(recover(COMMIT, NULL, 0, c, 2, out, sizeof out, &ch) == M5_ERR_ROLLBACK); 977 } 978 static void t_recovery_store_binding(void) 979 { 980 /* commit for store B, anchor for store A whose digest names that commit */ 981 m5_object_binding bb = OBJ; memset(bb.store_uuid, 0xB7, 16); 982 uint8_t envb[ENV_LEN], cb[M5_COMMIT_LEN], ab[M5_ANCHOR_LEN], out[PT_LEN]; 983 size_t n, ch; 984 m5_envelope_seal(SK.k_artifact, &bb, NONCE8, CS, PT, PT_LEN, envb, sizeof envb, &n); 985 make_commit(&bb, SG, envb, ENV_LEN, 5, SK.k_root_auth, cb); 986 make_anchor(M5_ID_PRODUCTION, OBJ.store_uuid, 9, 3, 5, cb, SK.k_root_auth, ab); 987 m5_candidate c[1] = {{envb, ENV_LEN}}; 988 CHECK(recover(cb, ab, M5_ANCHOR_LEN, c, 1, out, sizeof out, &ch) == M5_ERR_BINDING); 989 } 990 991 struct test { const char *name; void (*fn)(void); }; 992 993 /* ---- KeySlotManifest (kind 21) and the recovery KEK (ADR 0017 section 2.4) ---- */ 994 995 static m5_ksm ksm_fixture(void) 996 { 997 m5_ksm m; 998 memset(&m, 0, sizeof m); 999 memset(m.store_uuid, 0xA0, 16); 1000 m.manifest_sequence = 1; 1001 m.active_key_epoch = 3; 1002 m.slot_count = 2; /* slot 0 reserved Empty (TPM later), slot 1 recovery */ 1003 m.slots[1] = slot_fixture(); 1004 m.slots[1].kdf_suite = 0x02; 1005 m.slots[1].wrap_suite = M5_WRAP_SUITE_GCMSIV; 1006 memset(m.slots[1].salt, 0x5a, 16); 1007 return m; 1008 } 1009 1010 static void t_ksm_roundtrip(void) 1011 { 1012 uint8_t kek[32], nonce[12], kv[32], enc[M5_KSM_FIXED_LEN + 8]; 1013 memset(kek, 0x42, 32); memset(nonce, 0x24, 12); 1014 m5_ksm m = ksm_fixture(), d; 1015 CHECK(m5_keyslot_wrap(&m.slots[1], m.store_uuid, kek, K_VOL, nonce) == M5_OK); 1016 m5_ksm_encode(&m, enc); 1017 CHECK(memcmp(enc, "AIENKSL1", 8) == 0 && enc[8] == 1 && enc[9] == 0 && enc[76] == 2); 1018 CHECK(memcmp(enc + 12, m.store_uuid, 16) == 0 && enc[68] == 3); 1019 CHECK(all_zero(enc + 80, 48) && all_zero(enc + 128, M5_KEYSLOT_LEN)); /* reserved; slot 0 Empty */ 1020 CHECK(m5_ksm_decode(enc, M5_KSM_FIXED_LEN, &d) == M5_OK); 1021 CHECK(d.slot_count == 2 && d.active_key_epoch == 3 && d.manifest_sequence == 1 && d.arena_len == 0); 1022 CHECK(d.slots[1].slot_type == M5_SLOT_RECOVERY_RAW_SECRET && d.slots[1].kdf_suite == 0x02); 1023 CHECK(m5_keyslot_unwrap(&d.slots[1], d.store_uuid, kek, kv) == M5_OK && memcmp(kv, K_VOL, 32) == 0); 1024 /* an arena is carried as length only */ 1025 memset(enc + M5_KSM_FIXED_LEN, 7, 8); 1026 CHECK(m5_ksm_decode(enc, sizeof enc, &d) == M5_OK && d.arena_len == 8); 1027 } 1028 1029 static void t_ksm_refusals(void) 1030 { 1031 uint8_t enc[M5_KSM_FIXED_LEN + 8], e2[M5_KSM_FIXED_LEN + 8]; 1032 m5_ksm m = ksm_fixture(), d; 1033 m5_ksm_encode(&m, enc); 1034 memset(enc + M5_KSM_FIXED_LEN, 0, 8); 1035 CHECK(m5_ksm_decode(enc, M5_KSM_FIXED_LEN - 1, &d) == M5_ERR_BOUNDS); 1036 CHECK(m5_ksm_decode(NULL, M5_KSM_FIXED_LEN, &d) == M5_ERR_ARG); 1037 memcpy(e2, enc, sizeof enc); e2[0] = 'X'; 1038 CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_FORMAT); 1039 memcpy(e2, enc, sizeof enc); e2[8] = 2; /* format_version */ 1040 CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_FORMAT); 1041 memcpy(e2, enc, sizeof enc); e2[10] = 1; /* flags */ 1042 CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_FORMAT); 1043 memcpy(e2, enc, sizeof enc); e2[100] = 1; /* reserved */ 1044 CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_FORMAT); 1045 memcpy(e2, enc, sizeof enc); e2[76] = 5; /* slot_count > 4 */ 1046 CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_BOUNDS); 1047 memcpy(e2, enc, sizeof enc); e2[128 + 3] = 1; /* slot 0 flags: descriptor refused */ 1048 CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_FORMAT); 1049 /* payload range: slot 1 names 8 bytes of arena that are not there */ 1050 memcpy(e2, enc, sizeof enc); e2[128 + M5_KEYSLOT_LEN + 108] = 8; 1051 CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_BOUNDS); 1052 CHECK(m5_ksm_decode(e2, sizeof e2, &d) == M5_OK); /* present: fine */ 1053 /* a descriptor past slot_count must be Empty */ 1054 memcpy(e2, enc, sizeof enc); e2[76] = 1; 1055 CHECK(m5_ksm_decode(e2, M5_KSM_FIXED_LEN, &d) == M5_ERR_FORMAT); 1056 } 1057 1058 static void t_recovery_kek_vector(void) 1059 { 1060 /* openssl kdf -keylen 32 -kdfopt digest:SHA256 -kdfopt hexkey: 1061 * -kdfopt hexsalt: -kdfopt info:AIENOS/M5/RECOVERY-KEK-V1 HKDF */ 1062 uint8_t secret[32], salt[16], out[32], out2[32]; 1063 for (int i = 0; i < 32; i++) secret[i] = (uint8_t)i; 1064 for (int i = 0; i < 16; i++) salt[i] = (uint8_t)(0xa0 + i); 1065 static const uint8_t want[32] = {0x73, 0xa4, 0xe8, 0x99, 0x5a, 0x85, 0x9f, 0xca, 0xb9, 0xbd, 0x19, 0x01, 0x91, 1066 0xb4, 0x33, 0x4c, 0x5e, 0x75, 0x2c, 0xdc, 0x59, 0x21, 0x4c, 0x00, 0xe0, 0x72, 1067 0x21, 0xa2, 0x5e, 0xb6, 0x26, 0x38}; 1068 CHECK(m5_recovery_kek(salt, secret, 32, out) == M5_OK); 1069 CHECK(memcmp(out, want, 32) == 0); 1070 salt[0] ^= 1; 1071 CHECK(m5_recovery_kek(salt, secret, 32, out2) == M5_OK && memcmp(out, out2, 32) != 0); 1072 CHECK(m5_recovery_kek(salt, secret, 0, out2) == M5_ERR_ARG && all_zero(out2, 32)); 1073 CHECK(m5_recovery_kek(NULL, secret, 32, out2) == M5_ERR_ARG && all_zero(out2, 32)); 1074 } 1075 1076 static const struct test TESTS[] = { 1077 #define T(x) {#x, x} 1078 T(t_hkdf_rfc5869_case1), T(t_subkeys_deterministic_and_separated), T(t_object_key_binds_every_field), 1079 T(t_keyslot_wrap_unwrap), T(t_keyslot_refusals), T(t_ksm_roundtrip), T(t_ksm_refusals), T(t_recovery_kek_vector), 1080 T(t_secman_roundtrip_rust_v1_layout), 1081 T(t_secman_wrong_root_auth_key_refused), T(t_secman_every_byte_tamper_refused), 1082 T(t_env_roundtrip_sizes), T(t_env_header_bounds_each_field), T(t_env_rust_v1_header_and_standard_aad), T(t_env_hostile_header_fuzz), 1083 T(t_all_or_nothing_late_chunk_failure_zeroes_output), 1084 T(t_corrupt_header), T(t_corrupt_first_chunk), T(t_corrupt_middle_chunk), T(t_corrupt_last_chunk), 1085 T(t_corrupt_commit_record), T(t_corrupt_rollback_anchor), T(t_corrupt_key_generation), 1086 T(t_corrupt_store_generation), T(t_commit_check_object), T(t_truncate_drop_last_chunk), T(t_extend_append_chunk), 1087 T(t_reorder_chunks), T(t_splice_cross_object), 1088 T(t_anchor_caller_buffer_roundtrip), T(t_rb_valid_resume_and_advance), T(t_rb_older_store_generation_refused), 1089 T(t_rb_older_key_generation_refused), T(t_rb_lower_counter_refused), T(t_rb_fork_at_same_counter_refused), 1090 T(t_rb_counter_jump_inconsistent), T(t_rb_forged_anchor_refused), T(t_rb_missing_anchor_and_genesis), 1091 T(t_rb_anchor_other_store_refused), 1092 T(t_identity_production_refuses_test), T(t_identity_test_refuses_production), T(t_identity_relabel_breaks_mac), 1093 T(t_mig_authorized), T(t_mig_wrong_key_refused), T(t_mig_replay_refused), T(t_mig_binding_refusals), 1094 T(t_mig_test_identity_refused_in_production), 1095 T(t_recovery_deterministic), T(t_recovery_torn_refused), T(t_recovery_ambiguous_refused), 1096 T(t_recovery_old_commit_is_rollback), T(t_recovery_store_binding), 1097 #undef T 1098 }; 1099 1100 int main(void) 1101 { 1102 int failed = 0; 1103 fixtures(); 1104 if (g_fail) { printf("M5_NATIVE: FAIL\n"); return 1; } 1105 for (size_t i = 0; i < sizeof TESTS / sizeof TESTS[0]; i++) { 1106 g_fail = 0; 1107 TESTS[i].fn(); 1108 printf("%s %s\n", g_fail ? "FAIL" : "ok ", TESTS[i].name); 1109 if (g_fail) failed++; 1110 } 1111 printf("%zu tests, %d failed\n", sizeof TESTS / sizeof TESTS[0], failed); 1112 printf("M5_NATIVE: %s\n", failed ? "FAIL" : "PASS"); 1113 return failed ? 1 : 0; 1114 }