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 / store / tests / store_sealed_test.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 | /* Sealed store tests (M5 envelopes + transaction records + torn_slot anchor), |
| 18 | * file-backed through native/disk disk_file, 512 and 4096 geometry. */ |
| 19 | #include "store_test_util.h" |
| 20 | #include "store_rig.h" |
| 21 | #include "../argus/sha256.h" |
| 22 | #include "store_v1.h" |
| 23 | |
| 24 | #define SCHECK_EQ(got, want, what) \ |
| 25 | do { \ |
| 26 | long long g_ = (long long)(got), w_ = (long long)(want); \ |
| 27 | CHECK(g_ == w_, "%s: got %lld (%s), want %lld (%s)", what, g_, ss_strerror((int)g_), \ |
| 28 | w_, ss_strerror((int)w_)); \ |
| 29 | } while (0) |
| 30 | |
| 31 | static ss_workspace g_ws; |
| 32 | static uint8_t img_a[RIG_BYTES], img_b[RIG_BYTES], img_c[RIG_BYTES]; |
| 33 | static char g_path[512]; |
| 34 | static uint32_t g_bs; |
| 35 | static ss_keys K, K_TEST, K_OTHER; |
| 36 | |
| 37 | static uint8_t P1[700], P2[9000], P3[16384], P4[1]; |
| 38 | |
| 39 | static void fill(uint8_t *p, size_t n, uint32_t seed) |
| 40 | { |
| 41 | for (size_t i = 0; i < n; i++) p[i] = (uint8_t)((i * 131u + seed * 7u + (i >> 9)) & 0xff); |
| 42 | } |
| 43 | |
| 44 | static int sopen(rig *r, ss_store *s, const ss_keys *k) |
| 45 | { |
| 46 | if (rig_open(r, g_path, g_bs, 0) != 0) return -9999; |
| 47 | int rc = ss_open(s, &r->sdev, &r->tdev, 0, k, &g_ws); |
| 48 | return rc; |
| 49 | } |
| 50 | |
| 51 | /* region helpers: anchor = units 0..3, store = units 4.. */ |
| 52 | static void copy_anchor(uint8_t *dst, const uint8_t *src) |
| 53 | { |
| 54 | memcpy(dst, src, (size_t)RIG_ANCHOR_UNITS * 4096u); |
| 55 | } |
| 56 | static void copy_store(uint8_t *dst, const uint8_t *src) |
| 57 | { |
| 58 | memcpy(dst + RIG_ANCHOR_UNITS * 4096u, src + RIG_ANCHOR_UNITS * 4096u, |
| 59 | (size_t)RIG_STORE_UNITS * 4096u); |
| 60 | } |
| 61 | |
| 62 | static int check_read(ss_store *s, const uint8_t sid[32], const uint8_t *want, size_t n, uint16_t kind) |
| 63 | { |
| 64 | static uint8_t out[SS_MAX_PLAINTEXT]; |
| 65 | size_t len = 0; |
| 66 | uint16_t k = 0; |
| 67 | int rc = ss_read(s, sid, out, sizeof out, &len, &k); |
| 68 | if (rc != 0) return rc; |
| 69 | if (len != n || k != kind || memcmp(out, want, n) != 0) return -1; |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | /* Format, commit gen 2 {P1 kind 40, P2 kind 41}. */ |
| 74 | static void make_gen2(uint8_t ids[][32]) |
| 75 | { |
| 76 | rig r; |
| 77 | CHECK(rig_open(&r, g_path, g_bs, 1) == 0, "rig create"); |
| 78 | SCHECK_EQ(ss_format(&r.sdev, &r.tdev, 0, RIG_UUID, &K, &g_ws), 0, "format"); |
| 79 | ss_store s; |
| 80 | SCHECK_EQ(ss_open(&s, &r.sdev, &r.tdev, 0, &K, &g_ws), 0, "open genesis"); |
| 81 | SCHECK_EQ(s.rb, SS_RB_VALID_RESUME, "genesis anchor resumes"); |
| 82 | ss_object o[2] = {{40, 1, P1, sizeof P1}, {41, 2, P2, sizeof P2}}; |
| 83 | SCHECK_EQ(ss_transact(&s, o, 2, NULL, NULL, ids), 0, "gen 2 transact"); |
| 84 | rig_close(&r); |
| 85 | } |
| 86 | |
| 87 | static void advance(const ss_keys *k, uint16_t kind, const uint8_t *p, size_t n, uint8_t id[32], int want) |
| 88 | { |
| 89 | rig r; |
| 90 | ss_store s; |
| 91 | SCHECK_EQ(sopen(&r, &s, k), 0, "open for advance"); |
| 92 | ss_object o = {kind, 1, p, n}; |
| 93 | uint8_t ids[1][32]; |
| 94 | SCHECK_EQ(ss_transact(&s, &o, 1, NULL, NULL, ids), want, "advance"); |
| 95 | if (id) memcpy(id, ids[0], 32); |
| 96 | rig_close(&r); |
| 97 | } |
| 98 | |
| 99 | static int open_rc(const ss_keys *k) |
| 100 | { |
| 101 | rig r; |
| 102 | ss_store s; |
| 103 | int rc = sopen(&r, &s, k); |
| 104 | rig_close(&r); |
| 105 | return rc; |
| 106 | } |
| 107 | |
| 108 | static void test_basic(void) |
| 109 | { |
| 110 | uint8_t ids[2][32]; |
| 111 | make_gen2(ids); |
| 112 | rig r; |
| 113 | ss_store s; |
| 114 | SCHECK_EQ(sopen(&r, &s, &K), 0, "reopen gen 2"); |
| 115 | SCHECK_EQ(ss_generation(&s), 2, "generation 2"); |
| 116 | SCHECK_EQ(s.rb, SS_RB_VALID_RESUME, "anchor current"); |
| 117 | SCHECK_EQ(check_read(&s, ids[0], P1, sizeof P1, 40), 0, "read P1"); |
| 118 | SCHECK_EQ(check_read(&s, ids[1], P2, sizeof P2, 41), 0, "read P2"); |
| 119 | /* every catalog object is sealed: envelopes or one record per generation */ |
| 120 | uint32_t n; |
| 121 | const sv1_entry *e = st_catalog(&s.st, &n); |
| 122 | int nenv = 0, ntx = 0; |
| 123 | for (uint32_t i = 0; i < n; i++) { |
| 124 | nenv += e[i].kind == SS_KIND_ENVELOPE; |
| 125 | ntx += e[i].kind == SS_KIND_TXREC; |
| 126 | } |
| 127 | CHECK(nenv == 2 && ntx == 1 && n == 3, "catalog holds 2 envelopes + 1 record (%d %d %u)", nenv, ntx, n); |
| 128 | /* plaintext is not on disk */ |
| 129 | rig_close(&r); |
| 130 | CHECK(file_load(g_path, img_a, RIG_BYTES) == 0, "load"); |
| 131 | int found = 0; |
| 132 | for (size_t i = 0; i + 64 <= RIG_BYTES; i += 1) |
| 133 | if (memcmp(img_a + i, P2 + 100, 64) == 0) { found = 1; break; } |
| 134 | CHECK(!found, "plaintext bytes absent from the image"); |
| 135 | /* arguments */ |
| 136 | SCHECK_EQ(sopen(&r, &s, &K), 0, "reopen"); |
| 137 | uint8_t out[16]; |
| 138 | size_t len; |
| 139 | SCHECK_EQ(ss_read(&s, ids[1], out, sizeof out, &len, NULL), SS_E_ARG, "read cap too small"); |
| 140 | uint8_t bogus[32] = {1}; |
| 141 | SCHECK_EQ(ss_read(&s, bogus, out, sizeof out, &len, NULL), ST_E_INVALID_OBJECT, "read unknown id"); |
| 142 | ss_object many[SS_MAX_OBJECTS + 1]; |
| 143 | for (unsigned i = 0; i <= SS_MAX_OBJECTS; i++) many[i] = (ss_object){50, 1, P4, 1}; |
| 144 | SCHECK_EQ(ss_transact(&s, many, 0, NULL, NULL, NULL), SS_E_ARG, "zero objects"); |
| 145 | SCHECK_EQ(ss_transact(&s, many, SS_MAX_OBJECTS + 1, NULL, NULL, NULL), SS_E_ARG, "too many objects"); |
| 146 | static uint8_t big[SS_MAX_PLAINTEXT + 1]; |
| 147 | ss_object ob = {50, 1, big, sizeof big}; |
| 148 | SCHECK_EQ(ss_transact(&s, &ob, 1, NULL, NULL, NULL), SS_E_ARG, "object too large"); |
| 149 | ss_object oz = {0, 1, P4, 1}; |
| 150 | SCHECK_EQ(ss_transact(&s, &oz, 1, NULL, NULL, NULL), SS_E_ARG, "kind zero"); |
| 151 | /* max objects, empty and max-size plaintext */ |
| 152 | for (unsigned i = 0; i < SS_MAX_OBJECTS; i++) many[i] = (ss_object){(uint16_t)(60 + i), 1, P3, i == 0 ? 0 : sizeof P3 / (i + 1)}; |
| 153 | many[SS_MAX_OBJECTS - 1].len = sizeof P3; |
| 154 | uint8_t mids[SS_MAX_OBJECTS][32]; |
| 155 | SCHECK_EQ(ss_transact(&s, many, SS_MAX_OBJECTS, NULL, NULL, mids), 0, "8-object transaction"); |
| 156 | rig_close(&r); |
| 157 | SCHECK_EQ(sopen(&r, &s, &K), 0, "reopen gen 3"); |
| 158 | SCHECK_EQ(ss_generation(&s), 3, "generation 3"); |
| 159 | for (unsigned i = 0; i < SS_MAX_OBJECTS; i++) |
| 160 | SCHECK_EQ(check_read(&s, mids[i], P3, many[i].len, (uint16_t)(60 + i)), 0, "read max transaction"); |
| 161 | SCHECK_EQ(check_read(&s, ids[0], P1, sizeof P1, 40), 0, "gen 2 object still readable"); |
| 162 | rig_close(&r); |
| 163 | /* genesis with an empty anchor region is accepted only at generation 1 */ |
| 164 | CHECK(rig_open(&r, g_path, g_bs, 1) == 0, "rig create"); |
| 165 | SCHECK_EQ(st_format(&r.sdev, RIG_UUID), 0, "store-only genesis"); |
| 166 | SCHECK_EQ(ss_open(&s, &r.sdev, &r.tdev, 0, &K, &g_ws), 0, "empty anchor at genesis"); |
| 167 | SCHECK_EQ(s.rb, SS_RB_GENESIS, "genesis decision"); |
| 168 | ss_object o1 = {40, 1, P1, sizeof P1}; |
| 169 | SCHECK_EQ(ss_transact(&s, &o1, 1, NULL, NULL, NULL), 0, "first transaction catches the anchor up"); |
| 170 | rig_close(&r); |
| 171 | SCHECK_EQ(open_rc(&K), 0, "reopen after genesis catch-up"); |
| 172 | } |
| 173 | |
| 174 | static void test_rollback(void) |
| 175 | { |
| 176 | uint8_t ids[2][32]; |
| 177 | make_gen2(ids); |
| 178 | CHECK(file_load(g_path, img_a, RIG_BYTES) == 0, "snapshot gen 2"); |
| 179 | advance(&K, 42, P1, 10, NULL, 0); /* gen 3 */ |
| 180 | CHECK(file_load(g_path, img_b, RIG_BYTES) == 0, "snapshot gen 3"); |
| 181 | advance(&K, 43, P1, 20, NULL, 0); /* gen 4 */ |
| 182 | CHECK(file_load(g_path, img_c, RIG_BYTES) == 0, "snapshot gen 4"); |
| 183 | SCHECK_EQ(open_rc(&K), 0, "gen 4 mounts"); |
| 184 | |
| 185 | /* Store rolled back one generation under the current anchor */ |
| 186 | static uint8_t t[RIG_BYTES]; |
| 187 | memcpy(t, img_c, RIG_BYTES); |
| 188 | copy_store(t, img_b); |
| 189 | CHECK(file_save(g_path, t, RIG_BYTES) == 0, "write"); |
| 190 | SCHECK_EQ(open_rc(&K), SS_E_ROLLBACK, "store gen 3 under anchor 4"); |
| 191 | memcpy(t, img_c, RIG_BYTES); |
| 192 | copy_store(t, img_a); |
| 193 | CHECK(file_save(g_path, t, RIG_BYTES) == 0, "write"); |
| 194 | SCHECK_EQ(open_rc(&K), SS_E_ROLLBACK, "store gen 2 under anchor 4"); |
| 195 | /* anchor replayed: one behind is a crash window (accepted), two behind is not */ |
| 196 | memcpy(t, img_c, RIG_BYTES); |
| 197 | copy_anchor(t, img_b); |
| 198 | CHECK(file_save(g_path, t, RIG_BYTES) == 0, "write"); |
| 199 | { |
| 200 | rig r; |
| 201 | ss_store s; |
| 202 | SCHECK_EQ(sopen(&r, &s, &K), 0, "anchor one behind"); |
| 203 | SCHECK_EQ(s.rb, SS_RB_PREPARED_ADVANCE, "prepared advance"); |
| 204 | rig_close(&r); |
| 205 | } |
| 206 | memcpy(t, img_c, RIG_BYTES); |
| 207 | copy_anchor(t, img_a); |
| 208 | CHECK(file_save(g_path, t, RIG_BYTES) == 0, "write"); |
| 209 | SCHECK_EQ(open_rc(&K), SS_E_INCONSISTENT, "replayed anchor two behind"); |
| 210 | /* anchor missing after genesis */ |
| 211 | memcpy(t, img_c, RIG_BYTES); |
| 212 | memset(t, 0, (size_t)RIG_ANCHOR_UNITS * 4096u); |
| 213 | CHECK(file_save(g_path, t, RIG_BYTES) == 0, "write"); |
| 214 | SCHECK_EQ(open_rc(&K), SS_E_ROLLBACK, "missing anchor at gen 4"); |
| 215 | /* LIMITATION, whole-image freshness: rolling back both regions together is |
| 216 | * NOT detected. It needs a monotonic counter outside the disk (TPM NV, |
| 217 | * TRUST-1, aienux#32). The mount succeeds at the stale generation 2 and |
| 218 | * reports a valid resume, so a successful mount is never proof of freshness. */ |
| 219 | CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "write"); |
| 220 | { |
| 221 | rig r; |
| 222 | ss_store s; |
| 223 | SCHECK_EQ(sopen(&r, &s, &K), 0, "whole-disk rollback mounts (LIMITATION: needs TPM NV, #32)"); |
| 224 | SCHECK_EQ(ss_generation(&s), 2, "whole-disk rollback: stale generation 2 is served, not 4"); |
| 225 | SCHECK_EQ(s.rb, SS_RB_VALID_RESUME, "whole-disk rollback reports a valid resume (undetected)"); |
| 226 | rig_close(&r); |
| 227 | } |
| 228 | |
| 229 | /* forked anchor: same counter, different digest, valid MAC */ |
| 230 | CHECK(file_save(g_path, img_c, RIG_BYTES) == 0, "write"); |
| 231 | { |
| 232 | rig r; |
| 233 | ss_store s; |
| 234 | SCHECK_EQ(sopen(&r, &s, &K), 0, "open gen 4"); |
| 235 | uint8_t *rec = g_ws.tsrec; |
| 236 | memset(rec, 0, 4096); |
| 237 | memcpy(rec, "AIENSAN1", 8); |
| 238 | rec[8] = 1; |
| 239 | rec[10] = K.identity_class; |
| 240 | memcpy(rec + 16, RIG_UUID, 16); |
| 241 | m5_anchor a = {0}; |
| 242 | a.identity_class = K.identity_class; |
| 243 | memcpy(a.store_uuid, RIG_UUID, 16); |
| 244 | a.store_generation = 4; |
| 245 | a.key_generation = 1; |
| 246 | a.counter = 4; |
| 247 | memset(a.commit_digest, 0x77, 32); |
| 248 | CHECK(m5_anchor_seal(&a, K.k_root_auth, rec + 32, M5_ANCHOR_LEN) == M5_OK, "seal"); |
| 249 | CHECK(ts_commit(&s.ts, rec) == TS_OK, "commit forked anchor"); |
| 250 | rig_close(&r); |
| 251 | } |
| 252 | SCHECK_EQ(open_rc(&K), SS_E_ROLLBACK, "forked anchor at the same counter"); |
| 253 | |
| 254 | /* anchor record naming another store around a valid M5 anchor */ |
| 255 | CHECK(file_save(g_path, img_c, RIG_BYTES) == 0, "write"); |
| 256 | { |
| 257 | rig r; |
| 258 | ss_store s; |
| 259 | SCHECK_EQ(sopen(&r, &s, &K), 0, "open gen 4"); |
| 260 | uint8_t *rec = g_ws.tsrec; |
| 261 | memset(rec, 0, 4096); |
| 262 | memcpy(rec, "AIENSAN1", 8); |
| 263 | rec[8] = 1; |
| 264 | rec[10] = K.identity_class; |
| 265 | memcpy(rec + 16, RIG_UUID, 16); |
| 266 | rec[16] ^= 1; |
| 267 | m5_anchor a = {0}; |
| 268 | a.identity_class = K.identity_class; |
| 269 | memcpy(a.store_uuid, RIG_UUID, 16); |
| 270 | a.store_generation = 4; |
| 271 | a.key_generation = 1; |
| 272 | a.counter = 4; |
| 273 | memcpy(a.commit_digest, s.last_tx_digest, 32); |
| 274 | CHECK(m5_anchor_seal(&a, K.k_root_auth, rec + 32, M5_ANCHOR_LEN) == M5_OK, "seal"); |
| 275 | CHECK(ts_commit(&s.ts, rec) == TS_OK, "commit relabelled anchor"); |
| 276 | rig_close(&r); |
| 277 | } |
| 278 | SCHECK_EQ(open_rc(&K), SS_E_IDENTITY, "anchor record names another store"); |
| 279 | |
| 280 | /* replayed anchor seq: two valid torn_slot slots claiming one sequence */ |
| 281 | CHECK(file_save(g_path, img_c, RIG_BYTES) == 0, "write"); |
| 282 | { |
| 283 | rig r; |
| 284 | ss_store s; |
| 285 | SCHECK_EQ(sopen(&r, &s, &K), 0, "open gen 4"); |
| 286 | uint8_t rec[4096]; |
| 287 | uint64_t seq; |
| 288 | CHECK(ts_recover(&s.ts, rec, &seq, NULL) == TS_OK, "recover"); |
| 289 | int other = s.ts.newest_slot == 0 ? 1 : 0; |
| 290 | uint8_t hdr[4096]; |
| 291 | memset(hdr, 0, sizeof hdr); |
| 292 | ts_encode_commit(hdr, other, seq, rec); |
| 293 | uint64_t base = (uint64_t)other * 2u * r.bpu; |
| 294 | CHECK(r.d.write(r.d.ctx, base, r.bpu, rec) == 0, "body"); |
| 295 | CHECK(r.d.write(r.d.ctx, base + r.bpu, r.bpu, hdr) == 0, "header"); |
| 296 | rig_close(&r); |
| 297 | } |
| 298 | SCHECK_EQ(open_rc(&K), SS_E_ANCHOR, "duplicate anchor sequence"); |
| 299 | } |
| 300 | |
| 301 | static void test_identity(void) |
| 302 | { |
| 303 | uint8_t ids[2][32]; |
| 304 | make_gen2(ids); |
| 305 | SCHECK_EQ(open_rc(&K_TEST), SS_E_IDENTITY, "PRODUCTION store, TEST keys"); |
| 306 | SCHECK_EQ(open_rc(&K_OTHER), SS_E_TXREC, "same class, other volume key"); |
| 307 | /* anchor region of another store (other uuid) */ |
| 308 | CHECK(file_load(g_path, img_a, RIG_BYTES) == 0, "load"); |
| 309 | { |
| 310 | rig r; |
| 311 | static const uint8_t U2[16] = {9, 9, 9}; |
| 312 | CHECK(rig_open(&r, g_path, g_bs, 1) == 0, "create"); |
| 313 | SCHECK_EQ(ss_format(&r.sdev, &r.tdev, 0, U2, &K, &g_ws), 0, "format other store"); |
| 314 | rig_close(&r); |
| 315 | CHECK(file_load(g_path, img_b, RIG_BYTES) == 0, "load"); |
| 316 | } |
| 317 | copy_anchor(img_a, img_b); |
| 318 | CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "write"); |
| 319 | SCHECK_EQ(open_rc(&K), SS_E_IDENTITY, "anchor of another store"); |
| 320 | /* TEST-class store refused by PRODUCTION keys at genesis too */ |
| 321 | rig r; |
| 322 | CHECK(rig_open(&r, g_path, g_bs, 1) == 0, "create"); |
| 323 | SCHECK_EQ(ss_format(&r.sdev, &r.tdev, 0, RIG_UUID, &K_TEST, &g_ws), 0, "format TEST store"); |
| 324 | rig_close(&r); |
| 325 | SCHECK_EQ(open_rc(&K), SS_E_IDENTITY, "TEST genesis, PRODUCTION keys"); |
| 326 | SCHECK_EQ(open_rc(&K_TEST), 0, "TEST genesis, TEST keys"); |
| 327 | } |
| 328 | |
| 329 | /* Forge helper: prepare one object, let edit() change the workspace, commit |
| 330 | * with the real Store engine, then return the next mount's result. */ |
| 331 | typedef void (*forge_fn)(ss_store *s, void *arg); |
| 332 | static int forge_objs(forge_fn edit, void *arg, const ss_object *o, size_t n) |
| 333 | { |
| 334 | rig r; |
| 335 | ss_store s; |
| 336 | int rc = sopen(&r, &s, &K); |
| 337 | if (rc != 0) { rig_close(&r); return -8888; } |
| 338 | rc = ss_prepare(&s, o, n); |
| 339 | if (rc == 0) { |
| 340 | edit(&s, arg); |
| 341 | rc = ss_commit_prepared(&s, NULL, NULL); |
| 342 | } |
| 343 | rig_close(&r); |
| 344 | if (rc != 0) return -7777; |
| 345 | return open_rc(&K); |
| 346 | } |
| 347 | static int forge(forge_fn edit, void *arg) |
| 348 | { |
| 349 | ss_object o[2] = {{44, 1, P1, 300}, {45, 1, P2, 5000}}; |
| 350 | return forge_objs(edit, arg, o, 2); |
| 351 | } |
| 352 | |
| 353 | static void put64le(uint8_t *p, uint64_t v) |
| 354 | { |
| 355 | for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); |
| 356 | } |
| 357 | static void ed_flip_env(ss_store *s, void *a) { (void)a; s->ws->env[0][100] ^= 1; } |
| 358 | static void ed_flip_env_rebind(ss_store *s, void *a) |
| 359 | { |
| 360 | (void)a; |
| 361 | ss_workspace *w = s->ws; |
| 362 | w->env[0][100] ^= 1; /* ciphertext byte: AEAD tag will fail */ |
| 363 | uint8_t *e = w->tx + SS_TX_HDR; |
| 364 | sv1_object_id(SS_KIND_ENVELOPE, 1, w->env[0], w->env_len[0], e); |
| 365 | m5_commit c; |
| 366 | m5_commit_open(e + 32, M5_COMMIT_LEN, K.k_root_auth, K.identity_class, &c); |
| 367 | sha256_hash(w->env[0], w->env_len[0], c.object_id); |
| 368 | m5_commit_seal(&c, K.k_root_auth, e + 32); |
| 369 | ss_tx_remac(s); |
| 370 | } |
| 371 | static uint8_t junk[5000]; |
| 372 | static void ed_extra_env(ss_store *s, void *a) |
| 373 | { |
| 374 | (void)a; |
| 375 | s->ws->objs[s->ws->nobjs++] = (st_object){SS_KIND_ENVELOPE, 1, junk, sizeof junk}; |
| 376 | } |
| 377 | static void ed_plain(ss_store *s, void *a) |
| 378 | { |
| 379 | (void)a; |
| 380 | s->ws->objs[s->ws->nobjs++] = (st_object){3, 1, junk, 100}; |
| 381 | } |
| 382 | static void ed_gen_skip(ss_store *s, void *a) |
| 383 | { |
| 384 | (void)a; |
| 385 | uint64_t g = ss_generation(s) + 2; |
| 386 | put64le(s->ws->tx + 32, g); |
| 387 | put64le(s->ws->tx + 40, g); |
| 388 | ss_tx_remac(s); |
| 389 | } |
| 390 | static void ed_counter(ss_store *s, void *a) |
| 391 | { |
| 392 | (void)a; |
| 393 | put64le(s->ws->tx + 40, ss_generation(s) + 2); |
| 394 | ss_tx_remac(s); |
| 395 | } |
| 396 | static uint8_t saved_tx[SS_TX_MAX]; |
| 397 | static size_t saved_tx_len; |
| 398 | static void ed_stale_commit(ss_store *s, void *a) |
| 399 | { |
| 400 | (void)a; /* entry 0 carries the previous generation's commit record */ |
| 401 | memcpy(s->ws->tx + SS_TX_HDR + 32, saved_tx + SS_TX_HDR + 32, M5_COMMIT_LEN); |
| 402 | ss_tx_remac(s); |
| 403 | } |
| 404 | static void ed_replay_record(ss_store *s, void *a) |
| 405 | { |
| 406 | (void)a; /* the previous record replayed whole: the Store deduplicates it */ |
| 407 | memcpy(s->ws->tx, saved_tx, saved_tx_len); |
| 408 | s->ws->tx_len = saved_tx_len; |
| 409 | s->ws->objs[s->ws->nobjs - 1].len = saved_tx_len; |
| 410 | } |
| 411 | static void ed_chain(ss_store *s, void *a) |
| 412 | { |
| 413 | (void)a; |
| 414 | s->ws->tx[56] ^= 1; |
| 415 | ss_tx_remac(s); |
| 416 | } |
| 417 | static void ed_bad_mac(ss_store *s, void *a) { (void)a; s->ws->tx[s->ws->tx_len - 1] ^= 1; } |
| 418 | static void ed_bad_magic(ss_store *s, void *a) { (void)a; s->ws->tx[0] = 'X'; ss_tx_remac(s); } |
| 419 | static void ed_tx_class(ss_store *s, void *a) { (void)a; s->ws->tx[10] = M5_ID_TEST; ss_tx_remac(s); } |
| 420 | static void ed_seq(ss_store *s, void *a) |
| 421 | { |
| 422 | (void)a; /* swap the two commit records: object_sequence no longer matches */ |
| 423 | uint8_t t[M5_COMMIT_LEN]; |
| 424 | uint8_t *e0 = s->ws->tx + SS_TX_HDR + 32, *e1 = e0 + SS_TX_ENTRY; |
| 425 | memcpy(t, e0, sizeof t); |
| 426 | memcpy(e0, e1, sizeof t); |
| 427 | memcpy(e1, t, sizeof t); |
| 428 | ss_tx_remac(s); |
| 429 | } |
| 430 | static uint8_t dup_tx[SS_TX_MAX]; |
| 431 | static void ed_dup_gen(ss_store *s, void *a) |
| 432 | { |
| 433 | (void)a; /* two validly MACed, chain-valid records for the same generation, |
| 434 | * each claiming one of the two envelopes once, so only the |
| 435 | * one-record-per-generation check can refuse the pair */ |
| 436 | ss_workspace *w = s->ws; |
| 437 | uint8_t *e1 = w->tx + SS_TX_HDR + SS_TX_ENTRY; |
| 438 | m5_commit c; |
| 439 | m5_commit_open(e1 + 32, M5_COMMIT_LEN, K.k_root_auth, K.identity_class, &c); |
| 440 | c.object_sequence = 0; |
| 441 | size_t len1 = SS_TX_HDR + SS_TX_ENTRY + 32; |
| 442 | memcpy(dup_tx, w->tx, SS_TX_HDR); |
| 443 | memcpy(dup_tx + SS_TX_HDR, e1, 32); |
| 444 | m5_commit_seal(&c, K.k_root_auth, dup_tx + SS_TX_HDR + 32); |
| 445 | dup_tx[12] = 1; |
| 446 | dup_tx[13] = 0; |
| 447 | w->tx[12] = 1; |
| 448 | w->tx[13] = 0; |
| 449 | w->tx_len = len1; |
| 450 | ss_tx_remac(s); /* the real record now claims envelope 0 only */ |
| 451 | uint8_t t[SS_TX_MAX]; |
| 452 | memcpy(t, w->tx, len1); |
| 453 | memcpy(w->tx, dup_tx, len1); |
| 454 | ss_tx_remac(s); |
| 455 | memcpy(dup_tx, w->tx, len1); |
| 456 | memcpy(w->tx, t, len1); |
| 457 | w->objs[w->nobjs - 1].len = len1; |
| 458 | w->objs[w->nobjs++] = (st_object){SS_KIND_TXREC, 1, dup_tx, len1}; |
| 459 | } |
| 460 | static uint8_t old_sid[32]; |
| 461 | static m5_commit old_commit; |
| 462 | static void ed_claim_twice(ss_store *s, void *a) |
| 463 | { |
| 464 | (void)a; /* entry 0 re-claims a generation-2 envelope with a fresh, valid commit */ |
| 465 | m5_commit c = old_commit; |
| 466 | c.store_generation = ss_generation(s) + 1; |
| 467 | c.counter = c.store_generation; |
| 468 | c.object_sequence = 0; |
| 469 | uint8_t *e = s->ws->tx + SS_TX_HDR; |
| 470 | memcpy(e, old_sid, 32); |
| 471 | m5_commit_seal(&c, K.k_root_auth, e + 32); |
| 472 | ss_tx_remac(s); |
| 473 | } |
| 474 | |
| 475 | /* re-seal entry i's M5 commit record after an edit, then re-MAC the record */ |
| 476 | static void reseal(ss_store *s, uint32_t i, void (*fix)(m5_commit *)) |
| 477 | { |
| 478 | uint8_t *e = s->ws->tx + SS_TX_HDR + (size_t)i * SS_TX_ENTRY; |
| 479 | m5_commit c; |
| 480 | m5_commit_open(e + 32, M5_COMMIT_LEN, K.k_root_auth, K.identity_class, &c); |
| 481 | fix(&c); |
| 482 | m5_commit_seal(&c, K.k_root_auth, e + 32); |
| 483 | ss_tx_remac(s); |
| 484 | } |
| 485 | static void fx_store(m5_commit *c) { c->obj.store_uuid[0] ^= 1; } |
| 486 | static void fx_gen(m5_commit *c) { c->store_generation += 1; } |
| 487 | static void fx_counter(m5_commit *c) { c->counter += 1; } |
| 488 | static void fx_digest(m5_commit *c) { c->object_id[0] ^= 1; } |
| 489 | static void fx_huge(m5_commit *c) { c->store_generation = c->counter = UINT64_C(1) << 40; } |
| 490 | static void ed_commit_store(ss_store *s, void *a) { (void)a; reseal(s, 0, fx_store); } |
| 491 | static void ed_commit_gen(ss_store *s, void *a) { (void)a; reseal(s, 0, fx_gen); } |
| 492 | static void ed_commit_counter(ss_store *s, void *a) { (void)a; reseal(s, 0, fx_counter); } |
| 493 | static void ed_commit_digest(ss_store *s, void *a) { (void)a; reseal(s, 0, fx_digest); } |
| 494 | static void ed_gen_huge(ss_store *s, void *a) |
| 495 | { |
| 496 | (void)a; /* self-consistent record, generation far beyond the Store's */ |
| 497 | put64le(s->ws->tx + 32, UINT64_C(1) << 40); |
| 498 | put64le(s->ws->tx + 40, UINT64_C(1) << 40); |
| 499 | reseal(s, 0, fx_huge); |
| 500 | reseal(s, 1, fx_huge); |
| 501 | } |
| 502 | static void ed_tx_store(ss_store *s, void *a) { (void)a; s->ws->tx[16] ^= 1; ss_tx_remac(s); } |
| 503 | static void ed_tx_long(ss_store *s, void *a) |
| 504 | { |
| 505 | (void)a; /* 32 trailing bytes inside a valid MAC */ |
| 506 | ss_workspace *w = s->ws; |
| 507 | memset(w->tx + w->tx_len, 0, 32); |
| 508 | w->tx_len += 32; |
| 509 | w->objs[w->nobjs - 1].len = w->tx_len; |
| 510 | ss_tx_remac(s); |
| 511 | } |
| 512 | |
| 513 | static void test_forgery(void) |
| 514 | { |
| 515 | struct { const char *name; forge_fn fn; int want_open; } cases[] = { |
| 516 | {"envelope swapped after sealing", ed_flip_env, SS_E_ENVELOPE}, |
| 517 | {"unclaimed envelope", ed_extra_env, SS_E_ENVELOPE}, |
| 518 | {"plaintext object in sealed store", ed_plain, SS_E_FOREIGN_KIND}, |
| 519 | {"generation skip", ed_gen_skip, SS_E_ORDER}, |
| 520 | {"counter not the generation", ed_counter, SS_E_ORDER}, |
| 521 | {"stale commit record", ed_stale_commit, SS_E_ORDER}, |
| 522 | {"replayed transaction record", ed_replay_record, SS_E_ORDER}, |
| 523 | {"broken record chain", ed_chain, SS_E_ORDER}, |
| 524 | {"record MAC", ed_bad_mac, SS_E_TXREC}, |
| 525 | {"record magic", ed_bad_magic, SS_E_TXREC}, |
| 526 | {"record identity class", ed_tx_class, SS_E_IDENTITY}, |
| 527 | {"commit sequence order", ed_seq, SS_E_ORDER}, |
| 528 | {"two records for one generation", ed_dup_gen, SS_E_DUPLICATE}, |
| 529 | {"envelope claimed twice", ed_claim_twice, SS_E_DUPLICATE}, |
| 530 | {"record longer than its count", ed_tx_long, SS_E_TXREC}, |
| 531 | {"record names another store", ed_tx_store, SS_E_IDENTITY}, |
| 532 | {"commit names another store", ed_commit_store, SS_E_IDENTITY}, |
| 533 | {"commit generation not the record's", ed_commit_gen, SS_E_ORDER}, |
| 534 | {"commit counter not the record's", ed_commit_counter, SS_E_ORDER}, |
| 535 | {"commit digest not the envelope's", ed_commit_digest, SS_E_ENVELOPE}, |
| 536 | {"record generation beyond the Store", ed_gen_huge, SS_E_ORDER}, |
| 537 | }; |
| 538 | uint8_t ids[2][32]; |
| 539 | make_gen2(ids); |
| 540 | { |
| 541 | rig r; |
| 542 | ss_store s; |
| 543 | SCHECK_EQ(sopen(&r, &s, &K), 0, "open gen 2"); |
| 544 | /* the gen 2 record and one of its claims, for replay cases */ |
| 545 | uint32_t n; |
| 546 | const sv1_entry *e = st_catalog(&s.st, &n); |
| 547 | for (uint32_t i = 0; i < n; i++) |
| 548 | if (e[i].kind == SS_KIND_TXREC) |
| 549 | CHECK(st_read_object(&s.st, e[i].object_id, saved_tx, sizeof saved_tx, &saved_tx_len) == 0, "read rec"); |
| 550 | memcpy(old_sid, g_ws.claims[0].sid, 32); |
| 551 | old_commit = g_ws.claims[0].c; |
| 552 | rig_close(&r); |
| 553 | } |
| 554 | CHECK(file_load(g_path, img_a, RIG_BYTES) == 0, "snapshot gen 2"); |
| 555 | for (size_t i = 0; i < sizeof cases / sizeof cases[0]; i++) { |
| 556 | CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); |
| 557 | int rc = forge(cases[i].fn, NULL); |
| 558 | SCHECK_EQ(rc, cases[i].want_open, cases[i].name); |
| 559 | } |
| 560 | /* digest-consistent but tampered ciphertext: mounts, refused at read */ |
| 561 | CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); |
| 562 | SCHECK_EQ(forge(ed_flip_env_rebind, NULL), 0, "tampered ciphertext with rebound digest mounts"); |
| 563 | { |
| 564 | rig r; |
| 565 | ss_store s; |
| 566 | SCHECK_EQ(sopen(&r, &s, &K), 0, "open"); |
| 567 | uint8_t out[SS_MAX_PLAINTEXT]; |
| 568 | size_t len; |
| 569 | int refused = 0, ok = 0; |
| 570 | for (uint32_t k = 0; k < s.nclaims; k++) { |
| 571 | int rc = ss_read(&s, g_ws.claims[k].sid, out, sizeof out, &len, NULL); |
| 572 | if (rc == SS_E_ENVELOPE) |
| 573 | CHECK(len == 0, "refused read reports zero length (claim %u)", k); |
| 574 | refused += rc == SS_E_ENVELOPE; |
| 575 | ok += rc == 0; |
| 576 | } |
| 577 | CHECK(refused == 1 && ok == 3, "AEAD refuses exactly the tampered envelope (%d refused, %d ok)", refused, ok); |
| 578 | rig_close(&r); |
| 579 | } |
| 580 | /* defence in depth at read: a claim whose digest no longer matches the |
| 581 | * envelope (here corrupted in memory after mount) is refused */ |
| 582 | CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); |
| 583 | { |
| 584 | rig r; |
| 585 | ss_store s; |
| 586 | SCHECK_EQ(sopen(&r, &s, &K), 0, "open gen 2"); |
| 587 | uint8_t out[SS_MAX_PLAINTEXT]; |
| 588 | size_t len; |
| 589 | g_ws.claims[0].c.object_id[0] ^= 1; |
| 590 | SCHECK_EQ(ss_read(&s, g_ws.claims[0].sid, out, sizeof out, &len, NULL), SS_E_ENVELOPE, |
| 591 | "read refuses a claim digest mismatch"); |
| 592 | rig_close(&r); |
| 593 | } |
| 594 | /* on-disk envelope corruption. In the newest generation only: the Store |
| 595 | * falls back to the older root and the anchor refuses that as a rollback. |
| 596 | * In both roots: the Store itself refuses. */ |
| 597 | for (int both = 0; both < 2; both++) { |
| 598 | CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); |
| 599 | if (both) advance(&K, 47, P1, 50, NULL, 0); |
| 600 | { |
| 601 | rig r; |
| 602 | ss_store s; |
| 603 | SCHECK_EQ(sopen(&r, &s, &K), 0, "open"); |
| 604 | uint32_t n; |
| 605 | const sv1_entry *e = st_catalog(&s.st, &n); |
| 606 | uint64_t unit = 0; |
| 607 | for (uint32_t i = 0; i < n; i++) |
| 608 | if (e[i].kind == SS_KIND_ENVELOPE && (unit == 0 || e[i].first_unit < unit)) unit = e[i].first_unit; |
| 609 | rig_close(&r); |
| 610 | CHECK(file_load(g_path, img_b, RIG_BYTES) == 0, "load"); |
| 611 | img_b[(RIG_ANCHOR_UNITS + unit) * 4096u + 200] ^= 0x10; |
| 612 | CHECK(file_save(g_path, img_b, RIG_BYTES) == 0, "write"); |
| 613 | } |
| 614 | SCHECK_EQ(open_rc(&K), both ? ST_M_CORRUPT_RECOVERY_REQUIRED : SS_E_ROLLBACK, |
| 615 | both ? "corrupted envelope in both roots" : "corrupted envelope in newest root"); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | static rig *g_cut_rig; |
| 620 | static void cut_before_anchor(void *arg, int cp) |
| 621 | { |
| 622 | (void)arg; |
| 623 | if (cp == SS_CP_BEFORE_ANCHOR) disk_file_arm_cut(&g_cut_rig->f, 0); |
| 624 | } |
| 625 | |
| 626 | |
| 627 | /* ---- KeySlotManifest (kind 21): the one plaintext object, digest-bound ---- */ |
| 628 | |
| 629 | static uint8_t KSM[SS_KEYSLOT_MIN], KSM2[SS_KEYSLOT_MIN], KSM3[SS_KEYSLOT_MIN], KSM_BAD[SS_KEYSLOT_MIN]; |
| 630 | static void ksm_fill(uint8_t *m, uint64_t seq, const uint8_t uuid[16]) |
| 631 | { |
| 632 | memset(m, 0, SS_KEYSLOT_MIN); |
| 633 | memcpy(m, "AIENKSL1", 8); |
| 634 | m[8] = 1; |
| 635 | memcpy(m + 12, uuid, 16); |
| 636 | put64le(m + 28, seq); |
| 637 | m[76] = 2; /* slot_count; descriptors stay Empty: the sealed layer checks shape only */ |
| 638 | } |
| 639 | static void ksm_link(uint8_t *m, const uint8_t prev[32]) { memcpy(m + 36, prev, 32); } |
| 640 | /* Forgery: after prepare, flip the genesis manifest's previous id to nonzero |
| 641 | * and re-derive the Store ObjectId and the commit digest so every other |
| 642 | * guard passes and only the chain rule can refuse it. */ |
| 643 | static void fx_ks_prev_digest(m5_commit *c) { sha256_hash(KSM, sizeof KSM, c->object_id); } |
| 644 | static void ed_ks_prev_forged(ss_store *s, void *a) |
| 645 | { |
| 646 | (void)a; |
| 647 | KSM[36] = 1; |
| 648 | sv1_object_id(SS_KIND_KEYSLOT, 1, KSM, sizeof KSM, s->ws->tx + SS_TX_HDR); |
| 649 | reseal(s, 0, fx_ks_prev_digest); |
| 650 | } |
| 651 | static st_store ks_scratch; |
| 652 | static int bootstrap_rc(uint8_t *out, size_t cap, size_t *len) |
| 653 | { |
| 654 | rig r; |
| 655 | if (rig_open(&r, g_path, g_bs, 0) != 0) return -9999; |
| 656 | int rc = ss_keyslot_bootstrap(&ks_scratch, &r.sdev, &g_ws, out, cap, len); |
| 657 | rig_close(&r); |
| 658 | return rc; |
| 659 | } |
| 660 | static int ks_prepare_rc(const ss_object *o, size_t n) |
| 661 | { |
| 662 | rig r; |
| 663 | ss_store s; |
| 664 | int rc = sopen(&r, &s, &K); |
| 665 | if (rc == 0) rc = ss_prepare(&s, o, n); |
| 666 | rig_close(&r); |
| 667 | return rc; |
| 668 | } |
| 669 | static void ed_ks_digest(ss_store *s, void *a) { (void)a; reseal(s, 0, fx_digest); } |
| 670 | static void ed_ks_second(ss_store *s, void *a) |
| 671 | { |
| 672 | (void)a; |
| 673 | s->ws->objs[s->ws->nobjs++] = (st_object){SS_KIND_KEYSLOT, 1, KSM2, sizeof KSM2}; |
| 674 | } |
| 675 | static void ed_ks_unclaimed(ss_store *s, void *a) |
| 676 | { |
| 677 | (void)a; |
| 678 | s->ws->objs[s->ws->nobjs++] = (st_object){SS_KIND_KEYSLOT, 1, KSM, sizeof KSM}; |
| 679 | } |
| 680 | static void ed_ks_bad_unclaimed(ss_store *s, void *a) |
| 681 | { |
| 682 | (void)a; |
| 683 | s->ws->objs[s->ws->nobjs++] = (st_object){SS_KIND_KEYSLOT, 1, KSM_BAD, sizeof KSM_BAD}; |
| 684 | } |
| 685 | /* Keep both the structural Store id and the keyed claim valid, so a bad |
| 686 | * manifest is rejected by its shape rather than an unrelated digest check. */ |
| 687 | static void ed_ks_bad_shape(ss_store *s, void *a) |
| 688 | { |
| 689 | size_t offset = *(const size_t *)a; |
| 690 | KSM[offset] ^= 1; |
| 691 | sv1_object_id(SS_KIND_KEYSLOT, 1, KSM, sizeof KSM, s->ws->tx + SS_TX_HDR); |
| 692 | reseal(s, 0, fx_ks_prev_digest); |
| 693 | } |
| 694 | |
| 695 | static void test_keyslot(void) |
| 696 | { |
| 697 | uint8_t ids[2][32], out[SS_MAX_PLAINTEXT]; |
| 698 | size_t len = 0; |
| 699 | ksm_fill(KSM, 1, RIG_UUID); |
| 700 | ksm_fill(KSM2, 2, RIG_UUID); |
| 701 | ksm_fill(KSM_BAD, 3, RIG_UUID); |
| 702 | KSM_BAD[0] = 'X'; |
| 703 | make_gen2(ids); |
| 704 | SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), SS_E_KEYSLOT, "no manifest to bootstrap from"); |
| 705 | CHECK(file_load(g_path, img_a, RIG_BYTES) == 0, "snapshot gen 2 (no keyslot)"); |
| 706 | |
| 707 | /* prepare-time refusals: shape, store identity, one per store */ |
| 708 | { |
| 709 | uint8_t m[SS_KEYSLOT_MIN]; |
| 710 | ss_object o[2] = {{SS_KIND_KEYSLOT, 1, m, sizeof m}, {SS_KIND_KEYSLOT, 1, KSM2, sizeof KSM2}}; |
| 711 | memcpy(m, KSM, sizeof m); m[0] = 'X'; |
| 712 | SCHECK_EQ(ks_prepare_rc(o, 1), SS_E_KEYSLOT, "bad magic refused at prepare"); |
| 713 | memcpy(m, KSM, sizeof m); m[8] = 2; |
| 714 | SCHECK_EQ(ks_prepare_rc(o, 1), SS_E_KEYSLOT, "format version 2 refused at prepare"); |
| 715 | memcpy(m, KSM, sizeof m); m[12] ^= 1; |
| 716 | SCHECK_EQ(ks_prepare_rc(o, 1), SS_E_KEYSLOT, "another store's manifest refused at prepare"); |
| 717 | memcpy(m, KSM, sizeof m); o[0].len = SS_KEYSLOT_MIN - 1; |
| 718 | SCHECK_EQ(ks_prepare_rc(o, 1), SS_E_KEYSLOT, "short manifest refused at prepare"); |
| 719 | o[0].len = sizeof m; o[0].version = 2; |
| 720 | SCHECK_EQ(ks_prepare_rc(o, 1), SS_E_KEYSLOT, "manifest version 2 refused at prepare"); |
| 721 | o[0].version = 1; |
| 722 | /* Both independently qualify as the next manifest on this store. |
| 723 | * Sequence 2 here would be rejected even without the count guard. */ |
| 724 | o[1] = o[0]; |
| 725 | SCHECK_EQ(ks_prepare_rc(o, 2), SS_E_KEYSLOT, "two manifests in one transaction refused"); |
| 726 | SCHECK_EQ(ks_prepare_rc(o, 1), 0, "one well-formed manifest prepares"); |
| 727 | } |
| 728 | |
| 729 | /* commit {manifest, object}: bootstrap reads it unkeyed, the keyed open binds it */ |
| 730 | { |
| 731 | rig r; |
| 732 | ss_store s; |
| 733 | SCHECK_EQ(sopen(&r, &s, &K), 0, "open gen 2"); |
| 734 | ss_object o[2] = {{SS_KIND_KEYSLOT, 1, KSM, sizeof KSM}, {42, 1, P4, sizeof P4}}; |
| 735 | uint8_t kid[2][32]; |
| 736 | SCHECK_EQ(ss_transact(&s, o, 2, NULL, NULL, kid), 0, "gen 3 commits the manifest"); |
| 737 | CHECK(s.has_keyslot && memcmp(s.keyslot_sid, kid[0], 32) == 0, "manifest bound after commit"); |
| 738 | rig_close(&r); |
| 739 | SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), 0, "bootstrap reads the manifest"); |
| 740 | CHECK(len == sizeof KSM && memcmp(out, KSM, len) == 0, "bootstrap bytes are the manifest"); |
| 741 | SCHECK_EQ(sopen(&r, &s, &K), 0, "reopen gen 3"); |
| 742 | CHECK(s.has_keyslot && memcmp(s.keyslot_sid, kid[0], 32) == 0, "manifest bound at open"); |
| 743 | uint16_t kind = 0; |
| 744 | SCHECK_EQ(ss_read(&s, kid[0], out, sizeof out, &len, &kind), 0, "keyed read of the manifest"); |
| 745 | CHECK(kind == SS_KIND_KEYSLOT && len == sizeof KSM && memcmp(out, KSM, len) == 0, "read returns plaintext"); |
| 746 | SCHECK_EQ(check_read(&s, kid[1], P4, sizeof P4, 42), 0, "the envelope beside it opens"); |
| 747 | CHECK(s.nkeyslot == 1, "chain length 1"); |
| 748 | /* rotation chain (ADR 0017 section 2.7): the next manifest must be |
| 749 | * sequence count + 1 and name the bound manifest; anything else refused */ |
| 750 | ss_object again = {SS_KIND_KEYSLOT, 1, KSM2, sizeof KSM2}; |
| 751 | SCHECK_EQ(ss_prepare(&s, &again, 1), SS_E_KEYSLOT, "sequence 2 without the link to sequence 1 refused"); |
| 752 | ksm_fill(KSM2, 1, RIG_UUID); |
| 753 | SCHECK_EQ(ss_prepare(&s, &again, 1), SS_E_KEYSLOT, "a second sequence-1 manifest refused"); |
| 754 | ksm_fill(KSM2, 2, RIG_UUID); |
| 755 | ksm_link(KSM2, kid[1]); |
| 756 | SCHECK_EQ(ss_prepare(&s, &again, 1), SS_E_KEYSLOT, "sequence 2 naming an envelope, not the manifest, refused"); |
| 757 | ksm_link(KSM2, kid[0]); |
| 758 | uint8_t kid2[1][32], kid3[1][32]; |
| 759 | SCHECK_EQ(ss_transact(&s, &again, 1, NULL, NULL, kid2), 0, "gen 4 commits the sequence-2 manifest"); |
| 760 | CHECK(s.nkeyslot == 2 && memcmp(s.keyslot_sid, kid2[0], 32) == 0, "the newest manifest is bound after rotation"); |
| 761 | ss_object third = {SS_KIND_KEYSLOT, 1, KSM3, sizeof KSM3}; |
| 762 | ksm_fill(KSM3, 4, RIG_UUID); |
| 763 | ksm_link(KSM3, kid2[0]); |
| 764 | SCHECK_EQ(ss_prepare(&s, &third, 1), SS_E_KEYSLOT, "sequence skip (4 after 2) refused"); |
| 765 | ksm_fill(KSM3, 3, RIG_UUID); |
| 766 | ksm_link(KSM3, kid[0]); |
| 767 | SCHECK_EQ(ss_prepare(&s, &third, 1), SS_E_KEYSLOT, "sequence 3 naming sequence 1 refused"); |
| 768 | ksm_fill(KSM3, 2, RIG_UUID); |
| 769 | ksm_link(KSM3, kid2[0]); |
| 770 | SCHECK_EQ(ss_prepare(&s, &third, 1), SS_E_KEYSLOT, "sequence 2 again refused"); |
| 771 | ksm_fill(KSM3, 3, RIG_UUID); |
| 772 | ksm_link(KSM3, kid2[0]); |
| 773 | SCHECK_EQ(ss_transact(&s, &third, 1, NULL, NULL, kid3), 0, "gen 5 commits the sequence-3 manifest"); |
| 774 | CHECK(s.nkeyslot == 3 && memcmp(s.keyslot_sid, kid3[0], 32) == 0, "sequence 3 bound"); |
| 775 | rig_close(&r); |
| 776 | SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), 0, "bootstrap reads the chain"); |
| 777 | CHECK(len == sizeof KSM3 && memcmp(out, KSM3, len) == 0, "bootstrap returns the newest manifest"); |
| 778 | SCHECK_EQ(sopen(&r, &s, &K), 0, "reopen gen 5"); |
| 779 | CHECK(s.has_keyslot && s.nkeyslot == 3 && memcmp(s.keyslot_sid, kid3[0], 32) == 0, "chain of 3 bound at open"); |
| 780 | SCHECK_EQ(ss_read(&s, kid[0], out, sizeof out, &len, &kind), 0, "the superseded manifest stays readable"); |
| 781 | CHECK(len == sizeof KSM && memcmp(out, KSM, len) == 0, "superseded bytes unchanged"); |
| 782 | /* defence in depth at read: a claim whose digest no longer matches */ |
| 783 | for (uint32_t k = 0; k < s.nclaims; k++) |
| 784 | if (memcmp(g_ws.claims[k].sid, kid[0], 32) == 0) g_ws.claims[k].c.object_id[0] ^= 1; |
| 785 | SCHECK_EQ(ss_read(&s, kid[0], out, sizeof out, &len, NULL), SS_E_KEYSLOT, "manifest read with a wrong digest"); |
| 786 | rig_close(&r); |
| 787 | } |
| 788 | |
| 789 | /* forgeries from the no-keyslot snapshot */ |
| 790 | CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); |
| 791 | { |
| 792 | ss_object o[2] = {{SS_KIND_KEYSLOT, 1, KSM, sizeof KSM}, {44, 1, P1, 300}}; |
| 793 | SCHECK_EQ(forge_objs(ed_ks_digest, NULL, o, 2), SS_E_KEYSLOT, "commit digest not the manifest's"); |
| 794 | } |
| 795 | CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); |
| 796 | { |
| 797 | ss_object o[2] = {{SS_KIND_KEYSLOT, 1, KSM, sizeof KSM}, {44, 1, P1, 300}}; |
| 798 | ksm_fill(KSM2, 2, RIG_UUID); /* sequence 2 with no link */ |
| 799 | SCHECK_EQ(forge_objs(ed_ks_second, NULL, o, 2), SS_E_ENVELOPE, "a second manifest unclaimed in the catalog"); |
| 800 | SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), SS_E_KEYSLOT, "bootstrap refuses a broken chain (sequence 2 without its link)"); |
| 801 | CHECK(len == 0 && sv1_all_zero(out, sizeof out), "broken bootstrap chain scrubs its output"); |
| 802 | } |
| 803 | CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); |
| 804 | { |
| 805 | ss_object o[2] = {{SS_KIND_KEYSLOT, 1, KSM, sizeof KSM}, {44, 1, P1, 300}}; |
| 806 | SCHECK_EQ(forge_objs(ed_ks_prev_forged, NULL, o, 2), SS_E_KEYSLOT, "claimed genesis manifest with a nonzero previous id (chain rule at the keyed open)"); |
| 807 | SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), SS_E_KEYSLOT, "bootstrap refuses it too"); |
| 808 | KSM[36] = 0; |
| 809 | } |
| 810 | CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); |
| 811 | SCHECK_EQ(forge(ed_ks_unclaimed, NULL), SS_E_ENVELOPE, "unclaimed manifest"); |
| 812 | CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); |
| 813 | SCHECK_EQ(forge(ed_ks_bad_unclaimed, NULL), SS_E_ENVELOPE, "unclaimed malformed manifest refused keyed"); |
| 814 | SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), SS_E_KEYSLOT, "bootstrap refuses a malformed manifest"); |
| 815 | CHECK(len == 0, "refused bootstrap reports zero length"); |
| 816 | CHECK(sv1_all_zero(out, sizeof out), "malformed bootstrap scrubs its output"); |
| 817 | /* Magic, version and store identity each have a valid sequence/link and |
| 818 | * freshly sealed claim, isolating the keyed shape check. */ |
| 819 | static const size_t offsets[] = {0, 8, 12}; |
| 820 | for (size_t i = 0; i < sizeof offsets / sizeof offsets[0]; i++) { |
| 821 | CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore for shape forgery"); |
| 822 | ksm_fill(KSM, 1, RIG_UUID); |
| 823 | ss_object o = {SS_KIND_KEYSLOT, 1, KSM, sizeof KSM}; |
| 824 | SCHECK_EQ(forge_objs(ed_ks_bad_shape, (void *)&offsets[i], &o, 1), SS_E_KEYSLOT, |
| 825 | "digest-bound malformed manifest refused keyed"); |
| 826 | if (offsets[i] != 12) { |
| 827 | SCHECK_EQ(bootstrap_rc(out, sizeof out, &len), SS_E_KEYSLOT, |
| 828 | "valid-chain malformed manifest refused at bootstrap"); |
| 829 | CHECK(len == 0 && sv1_all_zero(out, sizeof out), "malformed bootstrap has no output"); |
| 830 | } |
| 831 | ksm_fill(KSM, 1, RIG_UUID); |
| 832 | } |
| 833 | CHECK(file_save(g_path, img_a, RIG_BYTES) == 0, "restore"); |
| 834 | } |
| 835 | |
| 836 | static void test_keyslot_limits(void) |
| 837 | { |
| 838 | /* A structural Store can contain hostile, unclaimed manifests. Bootstrap |
| 839 | * must stop before writing past its bounded chain workspace, and keyed |
| 840 | * open must classify the chain overflow before processing its records. */ |
| 841 | memdev m; |
| 842 | mem_init(&m, 256); |
| 843 | st_dev d = mem_dev(&m); |
| 844 | st_store raw; |
| 845 | static uint8_t manifests[SS_KS_CHAIN_MAX + 1][SS_KEYSLOT_MIN]; |
| 846 | st_object objects[SS_KS_CHAIN_MAX + 1]; |
| 847 | uint8_t prev[32] = {0}; |
| 848 | for (unsigned i = 0; i <= SS_KS_CHAIN_MAX; i++) { |
| 849 | ksm_fill(manifests[i], i + 1, RIG_UUID); |
| 850 | ksm_link(manifests[i], prev); |
| 851 | objects[i] = (st_object){SS_KIND_KEYSLOT, 1, manifests[i], sizeof manifests[i]}; |
| 852 | CHECK_EQ(sv1_object_id(SS_KIND_KEYSLOT, 1, manifests[i], sizeof manifests[i], prev), 0, |
| 853 | "derive raw manifest id"); |
| 854 | } |
| 855 | CHECK_EQ(st_format(&d, RIG_UUID), 0, "format raw chain store"); |
| 856 | CHECK_EQ(st_open(&raw, &d, &g_ws.st), 0, "open raw chain store"); |
| 857 | CHECK_EQ(st_transact(&raw, objects, SS_KS_CHAIN_MAX + 1, NULL, NULL), 0, "write oversized raw chain"); |
| 858 | uint8_t out[SS_KEYSLOT_MIN]; |
| 859 | size_t len = 99; |
| 860 | memset(g_ws.bygen, 0xa5, sizeof g_ws.bygen); |
| 861 | SCHECK_EQ(ss_keyslot_bootstrap(&ks_scratch, &d, &g_ws, out, sizeof out, &len), SS_E_KEYSLOT, |
| 862 | "oversized bootstrap chain refused"); |
| 863 | CHECK(len == 0, "oversized bootstrap returns no manifest"); |
| 864 | int intact = 1; |
| 865 | for (unsigned i = 0; i < ST_CAT_CAP; i++) intact &= g_ws.bygen[i] == UINT32_C(0xa5a5a5a5); |
| 866 | CHECK(intact, "oversized bootstrap does not overwrite the adjacent workspace"); |
| 867 | /* Supply a valid genesis anchor; record verification precedes the |
| 868 | * anti-rollback decision about this unclaimed generation-2 Store. */ |
| 869 | rig r; |
| 870 | CHECK_EQ(rig_open(&r, g_path, g_bs, 1), 0, "create anchor fixture"); |
| 871 | SCHECK_EQ(ss_format(&r.sdev, &r.tdev, 0, RIG_UUID, &K, &g_ws), 0, "format anchor fixture"); |
| 872 | ss_store s; |
| 873 | SCHECK_EQ(ss_open(&s, &d, &r.tdev, 0, &K, &g_ws), SS_E_KEYSLOT, |
| 874 | "oversized keyed chain refused before missing records"); |
| 875 | rig_close(&r); |
| 876 | mem_free(&m); |
| 877 | |
| 878 | /* Exercise the real maximum through the sealed service API, on both |
| 879 | * disk geometries. The usual 64-unit fixture is too small for rotations. */ |
| 880 | disk_file f; |
| 881 | disk_dev disk; |
| 882 | st_disk adapter; |
| 883 | st_dev store; |
| 884 | ts_device anchor; |
| 885 | uint32_t bpu = SV1_UNIT / g_bs; |
| 886 | CHECK_EQ(disk_file_open(&f, &disk, g_path, g_bs, 1028u * bpu, 1), 0, "create rotation disk"); |
| 887 | CHECK_EQ(st_disk_bind(&adapter, &disk, 4u * bpu, 1024, &store), 0, "bind rotation disk"); |
| 888 | ss_ts_device(&disk, &anchor); |
| 889 | SCHECK_EQ(ss_format(&store, &anchor, 0, RIG_UUID, &K, &g_ws), 0, "format rotation store"); |
| 890 | SCHECK_EQ(ss_open(&s, &store, &anchor, 0, &K, &g_ws), 0, "open rotation store"); |
| 891 | memset(prev, 0, sizeof prev); |
| 892 | uint8_t ids[1][32]; |
| 893 | for (unsigned i = 0; i < SS_KS_CHAIN_MAX; i++) { |
| 894 | ksm_fill(KSM, i + 1, RIG_UUID); |
| 895 | ksm_link(KSM, prev); |
| 896 | ss_object o = {SS_KIND_KEYSLOT, 1, KSM, sizeof KSM}; |
| 897 | SCHECK_EQ(ss_transact(&s, &o, 1, NULL, NULL, ids), 0, "commit valid rotation"); |
| 898 | memcpy(prev, ids[0], sizeof prev); |
| 899 | } |
| 900 | SCHECK_EQ(ss_open(&s, &store, &anchor, 0, &K, &g_ws), 0, "reopen full rotation chain"); |
| 901 | CHECK(s.nkeyslot == SS_KS_CHAIN_MAX, "all rotations remain bound after reopen"); |
| 902 | SCHECK_EQ(ss_keyslot_bootstrap(&ks_scratch, &store, &g_ws, out, sizeof out, &len), 0, |
| 903 | "bootstrap full chain succeeds"); |
| 904 | CHECK(len == sizeof KSM && memcmp(out, KSM, len) == 0, "full-chain bootstrap returns newest manifest"); |
| 905 | /* Bootstrap reuses the workspace: reopen before any sealed operation. */ |
| 906 | SCHECK_EQ(ss_open(&s, &store, &anchor, 0, &K, &g_ws), 0, "reopen after bootstrap"); |
| 907 | ksm_fill(KSM, SS_KS_CHAIN_MAX + 1, RIG_UUID); |
| 908 | ksm_link(KSM, prev); |
| 909 | ss_object extra = {SS_KIND_KEYSLOT, 1, KSM, sizeof KSM}; |
| 910 | uint64_t written = f.blocks_written, flushes = f.flushes; |
| 911 | SCHECK_EQ(ss_prepare(&s, &extra, 1), SS_E_KEYSLOT, "rotation past chain limit refused at prepare"); |
| 912 | CHECK(g_ws.nobjs == 0, "refused rotation leaves no prepared transaction"); |
| 913 | CHECK(f.blocks_written == written && f.flushes == flushes, "refused rotation performs no disk effects"); |
| 914 | disk_file_close(&f); |
| 915 | } |
| 916 | |
| 917 | static void test_anchor_crash(void) |
| 918 | { |
| 919 | uint8_t ids[2][32]; |
| 920 | make_gen2(ids); |
| 921 | rig r; |
| 922 | ss_store s; |
| 923 | SCHECK_EQ(sopen(&r, &s, &K), 0, "open"); |
| 924 | g_cut_rig = &r; |
| 925 | ss_object o = {46, 1, P1, 123}; |
| 926 | uint8_t nid[1][32]; |
| 927 | SCHECK_EQ(ss_transact(&s, &o, 1, cut_before_anchor, NULL, nid), SS_E_IO, "anchor write cut"); |
| 928 | disk_file_disarm(&r.f); |
| 929 | SCHECK_EQ(ss_transact(&s, &o, 1, NULL, NULL, NULL), SS_E_NEEDS_REOPEN, "needs reopen"); |
| 930 | rig_close(&r); |
| 931 | SCHECK_EQ(sopen(&r, &s, &K), 0, "reopen after crash between Store and anchor"); |
| 932 | SCHECK_EQ(ss_generation(&s), 3, "Store commit survived"); |
| 933 | SCHECK_EQ(s.rb, SS_RB_PREPARED_ADVANCE, "anchor one behind"); |
| 934 | SCHECK_EQ(check_read(&s, nid[0], P1, 123, 46), 0, "new object readable"); |
| 935 | /* a second crash at the same point: the catch-up keeps the anchor one behind */ |
| 936 | g_cut_rig = &r; |
| 937 | SCHECK_EQ(ss_transact(&s, &o, 1, cut_before_anchor, NULL, NULL), SS_E_IO, "second anchor write cut"); |
| 938 | disk_file_disarm(&r.f); |
| 939 | rig_close(&r); |
| 940 | SCHECK_EQ(sopen(&r, &s, &K), 0, "reopen after second crash"); |
| 941 | SCHECK_EQ(ss_generation(&s), 4, "second Store commit survived"); |
| 942 | SCHECK_EQ(s.rb, SS_RB_PREPARED_ADVANCE, "anchor still only one behind"); |
| 943 | SCHECK_EQ(ss_transact(&s, &o, 1, NULL, NULL, NULL), 0, "next transaction catches up"); |
| 944 | rig_close(&r); |
| 945 | SCHECK_EQ(sopen(&r, &s, &K), 0, "reopen"); |
| 946 | SCHECK_EQ(s.rb, SS_RB_VALID_RESUME, "anchor current again"); |
| 947 | SCHECK_EQ(ss_generation(&s), 5, "generation 5"); |
| 948 | rig_close(&r); |
| 949 | } |
| 950 | |
| 951 | static void test_kinds(void) |
| 952 | { |
| 953 | static const uint16_t taken[] = {1, 2, 16, 17, 18, 19, 20, 21, 22, 23}; |
| 954 | for (size_t i = 0; i < sizeof taken / sizeof taken[0]; i++) |
| 955 | CHECK(taken[i] != SS_KIND_ENVELOPE && taken[i] != SS_KIND_TXREC, "kind collides with %u", taken[i]); |
| 956 | CHECK(SS_KIND_ENVELOPE != SS_KIND_TXREC, "distinct kinds"); |
| 957 | } |
| 958 | |
| 959 | /* ---- nonce safety (Lane 26): envelope id + nonce prefix = V2 rule ---- */ |
| 960 | #define NONCE_ROUNDS 24 |
| 961 | /* (envelope_id || nonce_prefix) of the last envelope sealed into g_ws.env[0] */ |
| 962 | static void last_pair(uint8_t out[24]) |
| 963 | { |
| 964 | memcpy(out, g_ws.env[0] + 28, 16); |
| 965 | memcpy(out + 16, g_ws.env[0] + 44, 8); |
| 966 | } |
| 967 | |
| 968 | /* ---- independent envelope id oracle (#445) ---- |
| 969 | * HMAC-SHA256 (RFC 2104) and HKDF-Expand (RFC 5869) written here over the |
| 970 | * argus SHA-256 alone, pinned by published vectors, so the expected pair is |
| 971 | * computed without envid_derive, aienos_hmac_* or m5_hkdf_expand. The input |
| 972 | * layout is the V2 rule as documented above envid_derive (store_sealed.c): |
| 973 | * uuid 16 || generation u64 || index u32 || kind u16 || version u16 || |
| 974 | * key_generation u64 || identity_class u8 || plaintext_len u64 || |
| 975 | * SHA-256(plaintext), little-endian, after the domain string and its NUL. |
| 976 | * The envelope id (object key input) is bytes 0..15 of the HMAC, the nonce |
| 977 | * prefix bytes 16..23; both live in one key scope, the store's k_domain. */ |
| 978 | static void or_hmac(const uint8_t *key, size_t klen, const uint8_t *a, size_t alen, const uint8_t *b, |
| 979 | size_t blen, uint8_t out[32]) |
| 980 | { |
| 981 | uint8_t k0[64], pad[64], ih[32]; |
| 982 | memset(k0, 0, sizeof k0); |
| 983 | if (klen > 64) sha256_hash(key, klen, k0); |
| 984 | else memcpy(k0, key, klen); |
| 985 | sha256_ctx c; |
| 986 | for (int i = 0; i < 64; i++) pad[i] = k0[i] ^ 0x36; |
| 987 | sha256_init(&c); |
| 988 | sha256_update(&c, pad, 64); |
| 989 | sha256_update(&c, a, alen); |
| 990 | if (blen) sha256_update(&c, b, blen); |
| 991 | sha256_final(&c, ih); |
| 992 | for (int i = 0; i < 64; i++) pad[i] = k0[i] ^ 0x5c; |
| 993 | sha256_init(&c); |
| 994 | sha256_update(&c, pad, 64); |
| 995 | sha256_update(&c, ih, 32); |
| 996 | sha256_final(&c, out); |
| 997 | } |
| 998 | |
| 999 | /* HKDF-Expand, L <= 64, info <= 64 bytes */ |
| 1000 | static void or_hkdf_expand(const uint8_t prk[32], const uint8_t *info, size_t ilen, uint8_t *out, size_t L) |
| 1001 | { |
| 1002 | uint8_t t[32], msg[32 + 64 + 1]; |
| 1003 | size_t got = 0, tl = 0; |
| 1004 | for (uint8_t i = 1; got < L; i++) { |
| 1005 | memcpy(msg, t, tl); |
| 1006 | memcpy(msg + tl, info, ilen); |
| 1007 | msg[tl + ilen] = i; |
| 1008 | or_hmac(prk, 32, msg, tl + ilen + 1, NULL, 0, t); |
| 1009 | tl = 32; |
| 1010 | size_t n = L - got < 32 ? L - got : 32; |
| 1011 | memcpy(out + got, t, n); |
| 1012 | got += n; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | static int hex_eq(const uint8_t *got, const char *hex, size_t n) |
| 1017 | { |
| 1018 | for (size_t i = 0; i < n; i++) { |
| 1019 | unsigned v = 0; |
| 1020 | if (sscanf(hex + 2 * i, "%2x", &v) != 1 || got[i] != (uint8_t)v) return 0; |
| 1021 | } |
| 1022 | return 1; |
| 1023 | } |
| 1024 | |
| 1025 | static void le(uint8_t *p, uint64_t v, int n) |
| 1026 | { |
| 1027 | for (int i = 0; i < n; i++) p[i] = (uint8_t)(v >> (8 * i)); |
| 1028 | } |
| 1029 | |
| 1030 | /* expected (envelope_id || nonce_prefix) of the object at (gen, index) */ |
| 1031 | static void oracle_pair(const ss_keys *k, uint64_t gen, uint32_t index, uint16_t kind, uint16_t version, |
| 1032 | const uint8_t *pt, size_t len, uint8_t out[24]) |
| 1033 | { |
| 1034 | static const char info[] = "AIENOS-SEALED-ENVID-KEY-V2", dom[] = "AIENOS-SEALED-ENVID-V2"; |
| 1035 | uint8_t key[32], m[16 + 8 + 4 + 2 + 2 + 8 + 1 + 8 + 32], h[32]; |
| 1036 | or_hkdf_expand(k->k_domain, (const uint8_t *)info, sizeof info, key, sizeof key); |
| 1037 | memcpy(m, RIG_UUID, 16); |
| 1038 | le(m + 16, gen, 8); |
| 1039 | le(m + 24, index, 4); |
| 1040 | le(m + 28, kind, 2); |
| 1041 | le(m + 30, version, 2); |
| 1042 | le(m + 32, k->key_generation, 8); |
| 1043 | m[40] = k->identity_class; |
| 1044 | le(m + 41, len, 8); |
| 1045 | sha256_hash(pt, len, m + 49); |
| 1046 | or_hmac(key, sizeof key, (const uint8_t *)dom, sizeof dom, m, sizeof m, h); |
| 1047 | memcpy(out, h, 24); |
| 1048 | } |
| 1049 | |
| 1050 | static void test_envid_oracle(void) |
| 1051 | { |
| 1052 | /* the oracle's primitives against published vectors */ |
| 1053 | uint8_t h[42]; |
| 1054 | sha256_hash((const uint8_t *)"abc", 3, h); |
| 1055 | CHECK(hex_eq(h, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", 32), "SHA-256 FIPS 180 'abc'"); |
| 1056 | uint8_t k0b[20]; |
| 1057 | memset(k0b, 0x0b, sizeof k0b); |
| 1058 | or_hmac(k0b, sizeof k0b, (const uint8_t *)"Hi There", 8, NULL, 0, h); |
| 1059 | CHECK(hex_eq(h, "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7", 32), "HMAC RFC 4231 case 1"); |
| 1060 | or_hmac((const uint8_t *)"Jefe", 4, (const uint8_t *)"what do ya want ", 16, (const uint8_t *)"for nothing?", 12, h); |
| 1061 | CHECK(hex_eq(h, "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843", 32), "HMAC RFC 4231 case 2"); |
| 1062 | static const uint8_t prk[32] = {0x07, 0x77, 0x09, 0x36, 0x2c, 0x2e, 0x32, 0xdf, 0x0d, 0xdc, 0x3f, |
| 1063 | 0x0d, 0xc4, 0x7b, 0xba, 0x63, 0x90, 0xb6, 0xc7, 0x3b, 0xb5, 0x0f, |
| 1064 | 0x9c, 0x31, 0x22, 0xec, 0x84, 0x4a, 0xd7, 0xc2, 0xb3, 0xe5}; |
| 1065 | static const uint8_t info[10] = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9}; |
| 1066 | or_hkdf_expand(prk, info, sizeof info, h, 42); |
| 1067 | CHECK(hex_eq(h, "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865", 42), |
| 1068 | "HKDF-Expand RFC 5869 case 1"); |
| 1069 | |
| 1070 | /* gen 2 = {P1 kind 40 v1 at index 0, P2 kind 41 v2 at index 1}: every |
| 1071 | * bound field is nonzero in object 1, so dropping any one changes it */ |
| 1072 | uint8_t ids[2][32], want[24], got[24]; |
| 1073 | make_gen2(ids); |
| 1074 | oracle_pair(&K, 2, 0, 40, 1, P1, sizeof P1, want); |
| 1075 | memcpy(got, g_ws.env[0] + 28, 16); |
| 1076 | memcpy(got + 16, g_ws.env[0] + 44, 8); |
| 1077 | CHECK(memcmp(got, want, 24) == 0, "gen 2 object 0 pair equals the independent oracle"); |
| 1078 | oracle_pair(&K, 2, 1, 41, 2, P2, sizeof P2, want); |
| 1079 | memcpy(got, g_ws.env[1] + 28, 16); |
| 1080 | memcpy(got + 16, g_ws.env[1] + 44, 8); |
| 1081 | CHECK(memcmp(got, want, 24) == 0, "gen 2 object 1 pair equals the independent oracle"); |
| 1082 | if (g_bs == 512) |
| 1083 | printf("envid oracle: key scope k_domain (rig seed 0x11, class %u, key_generation %llu), store RIG_UUID;" |
| 1084 | " gen 2 index 0 kind 40 v1 %zu B = %u chunk(s), index 1 kind 41 v2 %zu B = %u chunk(s) of %u B\n", |
| 1085 | (unsigned)K.identity_class, (unsigned long long)K.key_generation, sizeof P1, |
| 1086 | (unsigned)((sizeof P1 + SS_CHUNK - 1) / SS_CHUNK), sizeof P2, |
| 1087 | (unsigned)((sizeof P2 + SS_CHUNK - 1) / SS_CHUNK), (unsigned)SS_CHUNK); |
| 1088 | /* the same object under another key scope is a different pair; pairs are |
| 1089 | * only required to be new within one scope, never across unrelated keys */ |
| 1090 | uint8_t other[24]; |
| 1091 | oracle_pair(&K_OTHER, 2, 1, 41, 2, P2, sizeof P2, other); |
| 1092 | CHECK(memcmp(other, want, 24) != 0, "oracle: another k_domain gives another pair"); |
| 1093 | } |
| 1094 | |
| 1095 | static void test_nonce(void) |
| 1096 | { |
| 1097 | uint8_t ids[2][32]; |
| 1098 | make_gen2(ids); |
| 1099 | static uint8_t snap[RIG_BYTES], q[SS_MAX_PLAINTEXT]; |
| 1100 | CHECK(file_load(g_path, snap, RIG_BYTES) == 0, "snapshot gen 2"); |
| 1101 | /* gen 3 written, then whole-disk rollback to gen 2 and a different gen 3, |
| 1102 | * NONCE_ROUNDS times: every (key input, nonce) pair must be new */ |
| 1103 | static uint8_t pairs[NONCE_ROUNDS + 1][24]; |
| 1104 | for (int r = 0; r <= NONCE_ROUNDS; r++) { |
| 1105 | CHECK(file_save(g_path, snap, RIG_BYTES) == 0, "whole-disk rollback"); |
| 1106 | fill(q, 600, 100u + (uint32_t)r); |
| 1107 | uint8_t id[32]; |
| 1108 | advance(&K, 42, q, 600, id, 0); |
| 1109 | last_pair(pairs[r]); |
| 1110 | { |
| 1111 | uint8_t want[24]; |
| 1112 | oracle_pair(&K, 3, 0, 42, 1, q, 600, want); |
| 1113 | CHECK(memcmp(pairs[r], want, 24) == 0, "rollback rewrite %d: pair equals the independent oracle", r); |
| 1114 | } |
| 1115 | rig rr; |
| 1116 | ss_store s; |
| 1117 | SCHECK_EQ(sopen(&rr, &s, &K), 0, "rewritten gen 3 mounts"); |
| 1118 | SCHECK_EQ(ss_generation(&s), 3, "same generation after rollback"); |
| 1119 | SCHECK_EQ(check_read(&s, id, q, 600, 42), 0, "rewritten object reads back"); |
| 1120 | rig_close(&rr); |
| 1121 | for (int j = 0; j < r; j++) { |
| 1122 | CHECK(memcmp(pairs[j], pairs[r], 16) != 0, "envelope id (object key input) repeated"); |
| 1123 | CHECK(memcmp(pairs[j] + 16, pairs[r] + 16, 8) != 0, "nonce prefix repeated"); |
| 1124 | } |
| 1125 | /* the old predictable V1 value is not used */ |
| 1126 | uint8_t m[16 + 8 + 4], v1[32]; |
| 1127 | sha256_ctx c; |
| 1128 | sha256_init(&c); |
| 1129 | sha256_update(&c, (const uint8_t *)"AIENOS-SEALED-ENVID-V1", sizeof("AIENOS-SEALED-ENVID-V1")); |
| 1130 | memcpy(m, RIG_UUID, 16); |
| 1131 | for (int b = 0; b < 8; b++) m[16 + b] = (uint8_t)((uint64_t)3 >> (8 * b)); |
| 1132 | memset(m + 24, 0, 4); |
| 1133 | sha256_update(&c, m, sizeof m); |
| 1134 | sha256_final(&c, v1); |
| 1135 | CHECK(memcmp(v1, pairs[r], 24) != 0, "V1 predictable id still used"); |
| 1136 | } |
| 1137 | /* the identical object rewritten at the same place gives the identical |
| 1138 | * envelope (deterministic; reveals nothing the old image did not) */ |
| 1139 | CHECK(file_save(g_path, snap, RIG_BYTES) == 0, "rollback"); |
| 1140 | fill(q, 600, 100u); |
| 1141 | advance(&K, 42, q, 600, NULL, 0); |
| 1142 | uint8_t again[24]; |
| 1143 | last_pair(again); |
| 1144 | CHECK(memcmp(again, pairs[0], 24) == 0, "identical rewrite is deterministic"); |
| 1145 | /* one byte, kind or version changed gives a new pair */ |
| 1146 | CHECK(file_save(g_path, snap, RIG_BYTES) == 0, "rollback"); |
| 1147 | q[599] ^= 1; |
| 1148 | advance(&K, 42, q, 600, NULL, 0); |
| 1149 | last_pair(again); |
| 1150 | CHECK(memcmp(again, pairs[0], 16) != 0 && memcmp(again + 16, pairs[0] + 16, 8) != 0, "one-bit change, new pair"); |
| 1151 | CHECK(file_save(g_path, snap, RIG_BYTES) == 0, "rollback"); |
| 1152 | q[599] ^= 1; |
| 1153 | advance(&K, 43, q, 600, NULL, 0); |
| 1154 | last_pair(again); |
| 1155 | CHECK(memcmp(again, pairs[0], 16) != 0 && memcmp(again + 16, pairs[0] + 16, 8) != 0, "kind change, new pair"); |
| 1156 | |
| 1157 | /* counter going backwards is refused: Store rolled back under the anchor */ |
| 1158 | CHECK(file_save(g_path, snap, RIG_BYTES) == 0, "rollback"); |
| 1159 | advance(&K, 42, q, 600, NULL, 0); /* gen 3 */ |
| 1160 | static uint8_t g3[RIG_BYTES], t[RIG_BYTES]; |
| 1161 | CHECK(file_load(g_path, g3, RIG_BYTES) == 0, "snapshot gen 3"); |
| 1162 | memcpy(t, g3, RIG_BYTES); |
| 1163 | copy_store(t, snap); |
| 1164 | CHECK(file_save(g_path, t, RIG_BYTES) == 0, "write"); |
| 1165 | SCHECK_EQ(open_rc(&K), SS_E_ROLLBACK, "counter went backwards (store 2 under anchor 3)"); |
| 1166 | |
| 1167 | /* a version-1 record (old predictable-id format) is refused, typed */ |
| 1168 | CHECK(file_save(g_path, g3, RIG_BYTES) == 0, "restore gen 3"); |
| 1169 | { |
| 1170 | rig rr; |
| 1171 | ss_store s; |
| 1172 | SCHECK_EQ(sopen(&rr, &s, &K), 0, "open gen 3"); |
| 1173 | ss_object o = {44, 1, P1, sizeof P1}; |
| 1174 | SCHECK_EQ(ss_prepare(&s, &o, 1), 0, "prepare"); |
| 1175 | g_ws.tx[8] = 1; |
| 1176 | ss_tx_remac(&s); |
| 1177 | SCHECK_EQ(ss_commit_prepared(&s, NULL, NULL), 0, "commit forged v1 record"); |
| 1178 | rig_close(&rr); |
| 1179 | SCHECK_EQ(open_rc(&K), SS_E_FORMAT_VERSION, "version-1 record refused"); |
| 1180 | } |
| 1181 | |
| 1182 | /* a writer that sealed with the old predictable id (valid MACs and |
| 1183 | * digests) mounts, but the envelope is refused on read */ |
| 1184 | CHECK(file_save(g_path, g3, RIG_BYTES) == 0, "restore gen 3"); |
| 1185 | { |
| 1186 | rig rr; |
| 1187 | ss_store s; |
| 1188 | SCHECK_EQ(sopen(&rr, &s, &K), 0, "open gen 3"); |
| 1189 | ss_object o = {44, 1, P1, sizeof P1}; |
| 1190 | SCHECK_EQ(ss_prepare(&s, &o, 1), 0, "prepare"); |
| 1191 | uint8_t m[16 + 8 + 4], v1[32]; |
| 1192 | sha256_ctx c; |
| 1193 | sha256_init(&c); |
| 1194 | sha256_update(&c, (const uint8_t *)"AIENOS-SEALED-ENVID-V1", sizeof("AIENOS-SEALED-ENVID-V1")); |
| 1195 | memcpy(m, RIG_UUID, 16); |
| 1196 | for (int b = 0; b < 8; b++) m[16 + b] = (uint8_t)((uint64_t)4 >> (8 * b)); |
| 1197 | memset(m + 24, 0, 4); |
| 1198 | sha256_update(&c, m, sizeof m); |
| 1199 | sha256_final(&c, v1); |
| 1200 | m5_commit mc; |
| 1201 | uint8_t *e = g_ws.tx + SS_TX_HDR; |
| 1202 | CHECK(m5_commit_open(e + 32, M5_COMMIT_LEN, K.k_root_auth, K.identity_class, &mc) == M5_OK, "commit open"); |
| 1203 | memcpy(mc.obj.envelope_id, v1, 16); |
| 1204 | CHECK(m5_envelope_seal(K.k_domain, &mc.obj, v1 + 16, SS_CHUNK, P1, sizeof P1, g_ws.env[0], |
| 1205 | SS_ENV_CAP, &g_ws.env_len[0]) == M5_OK, "legacy seal"); |
| 1206 | g_ws.objs[0].len = g_ws.env_len[0]; |
| 1207 | CHECK(sv1_object_id(SS_KIND_ENVELOPE, 1, g_ws.env[0], g_ws.env_len[0], e) == 0, "object id"); |
| 1208 | sha256_hash(g_ws.env[0], g_ws.env_len[0], mc.object_id); |
| 1209 | CHECK(m5_commit_seal(&mc, K.k_root_auth, e + 32) == M5_OK, "commit seal"); |
| 1210 | ss_tx_remac(&s); |
| 1211 | uint8_t sid[32]; |
| 1212 | memcpy(sid, e, 32); |
| 1213 | SCHECK_EQ(ss_commit_prepared(&s, NULL, NULL), 0, "commit legacy-id envelope"); |
| 1214 | rig_close(&rr); |
| 1215 | SCHECK_EQ(sopen(&rr, &s, &K), 0, "legacy-id envelope mounts (digests valid)"); |
| 1216 | { |
| 1217 | static uint8_t canary[SS_MAX_PLAINTEXT]; |
| 1218 | memset(canary, 0xaa, sizeof canary); |
| 1219 | size_t clen = 999; |
| 1220 | SCHECK_EQ(ss_read(&s, sid, canary, sizeof canary, &clen, NULL), SS_E_ENVELOPE, "legacy-id refused"); |
| 1221 | int z = 1; |
| 1222 | for (size_t b = 0; b < sizeof canary; b++) z &= canary[b] == 0; |
| 1223 | CHECK(clen == 0 && z, "whole output buffer wiped on envelope refusal"); |
| 1224 | } |
| 1225 | SCHECK_EQ(check_read(&s, sid, P1, sizeof P1, 44), SS_E_ENVELOPE, "legacy-id envelope refused on read"); |
| 1226 | rig_close(&rr); |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | int main(int argc, char **argv) |
| 1231 | { |
| 1232 | const char *dir = argc > 1 ? argv[1] : "."; |
| 1233 | fill(P1, sizeof P1, 1); |
| 1234 | fill(P2, sizeof P2, 2); |
| 1235 | fill(P3, sizeof P3, 3); |
| 1236 | P4[0] = 0x5a; |
| 1237 | fill(junk, sizeof junk, 9); |
| 1238 | rig_keys(M5_ID_PRODUCTION, 0x11, &K); |
| 1239 | rig_keys(M5_ID_TEST, 0x11, &K_TEST); |
| 1240 | rig_keys(M5_ID_PRODUCTION, 0x22, &K_OTHER); |
| 1241 | test_kinds(); |
| 1242 | static const uint32_t sizes[2] = {512, 4096}; |
| 1243 | for (int k = 0; k < 2; k++) { |
| 1244 | g_bs = sizes[k]; |
| 1245 | snprintf(g_path, sizeof g_path, "%s/sealed_%u.img", dir, g_bs); |
| 1246 | int before = g_failed; |
| 1247 | test_basic(); |
| 1248 | test_rollback(); |
| 1249 | test_identity(); |
| 1250 | test_forgery(); |
| 1251 | test_keyslot(); |
| 1252 | test_anchor_crash(); |
| 1253 | test_nonce(); |
| 1254 | test_envid_oracle(); |
| 1255 | test_keyslot_limits(); |
| 1256 | printf("sealed store, %u-byte blocks: %s\n", g_bs, g_failed == before ? "ok" : "FAILED"); |
| 1257 | unlink(g_path); |
| 1258 | } |
| 1259 | printf("store sealed: %d checks, %d failed\n", g_checks, g_failed); |
| 1260 | printf("STORE_SEALED: %s\n", g_failed ? "FAIL" : "PASS"); |
| 1261 | return g_failed ? 1 : 0; |
| 1262 | } |