From b5366d1434c3afe5e33ed7743c8f738062dd3329 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 03:57:01 -0500 Subject: [PATCH 1/2] native/store: sealed envelope ids and nonces keyed on the plaintext (record v2) Version 1 derived each envelope id and AES-GCM-SIV nonce prefix from SHA-256(uuid || generation || index). A whole-disk rollback (undetected without TPM NV) followed by a rewrite of the same generation with different bytes therefore reused the (object key, nonce) pair. Version 2 takes the first 24 bytes of HMAC(k_envid, uuid || generation || index || kind || version || key_generation || class || length || SHA-256(plaintext)), k_envid = HKDF-Expand(k_domain, ENVID-KEY-V2). The envelope id feeds the per-object key, so (key, nonce) changes whenever the plaintext changes; only the identical object at the same place repeats. No entropy source is needed and the kernel call sites are unchanged. Transaction records carry version 2; a version-1 record is refused with the new typed error SS_E_FORMAT_VERSION. ss_read recomputes the rule after decrypting and refuses a mismatching envelope. Tests: 24 whole-disk rollback-then-rewrite rounds never repeat an envelope id or nonce prefix; identical rewrite is deterministic; counter going backwards refused; v1 record refused; legacy-id envelope refused on read. The two-records-per-generation forgery is rebuilt so that only the tx-duplicate guard can refuse it (it previously relied on catalog order). Co-Authored-By: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- native/store/README.md | 29 ++++- native/store/store_sealed.c | 72 +++++++++-- native/store/store_sealed.h | 5 + native/store/tests/store_sealed_test.c | 163 +++++++++++++++++++++++-- 4 files changed, 243 insertions(+), 26 deletions(-) diff --git a/native/store/README.md b/native/store/README.md index 5b1b8ffb..c21c75a3 100644 --- a/native/store/README.md +++ b/native/store/README.md @@ -52,15 +52,32 @@ buffer is a caller-provided workspace (`st_workspace` about 0.9 MiB, * Kind `0x0510` = M5 envelope (AES-256-GCM-SIV, 4096-byte chunks, at most 16 KiB plaintext, 8 objects per transaction). Kind `0x0511` = sealed transaction record. Existing kinds are 1, 2 and 16 to 23; no collision. -* Record `AIENSTX1`: version, identity class, count, store uuid, Store +* Record `AIENSTX1`: version (2; a version-1 record is refused with + `SS_E_FORMAT_VERSION`, reformat), identity class, count, store uuid, Store generation, counter (= generation), key generation, SHA-256 of the previous record (zero at generation 2), then per object the Store ObjectId and the 144-byte M5 commit record (binds uuid, kind, envelope id, generation, counter, sequence, SHA-256 of the envelope), then HMAC(k_root_auth, "AIENOS-STORE-SEALED-TX-V1\0" || body). -* Envelope id and nonce prefix = SHA-256("AIENOS-SEALED-ENVID-V1\0" || uuid - || generation || index). A retried generation after a crash reuses them; - GCM-SIV is nonce-misuse resistant, which is why this is safe. +* Envelope id (16 bytes) and nonce prefix (8 bytes), record version 2 = + the first 24 bytes of HMAC(k_envid, "AIENOS-SEALED-ENVID-V2\0" || uuid || + generation u64 || index u32 || kind u16 || version u16 || key_generation + u64 || identity_class u8 || plaintext_len u64 || SHA-256(plaintext)), + k_envid = HKDF-Expand(k_domain, "AIENOS-SEALED-ENVID-KEY-V2\0"). The + envelope id also feeds the per-object key, so the AES-GCM-SIV (key, nonce) + pair of every chunk is a keyed function of the plaintext. + Why (Lane 26, 2026-10-01): version 1 used SHA-256(uuid || generation || + index), so after a whole-disk rollback (undetected without TPM NV) a + rewrite of the same generation with different bytes reused the (key, + nonce) pair. GCM-SIV keeps confidentiality under that reuse except for + equal messages, but the store promised unique nonces and the margin was + not worth keeping. Now only the identical object at the same place gives + the identical envelope (a retried generation after a crash, or a rewrite + after rollback), which shows nothing the old image did not already show. + No entropy source is needed, so the library stays clock-, heap- and + RNG-free and the kernel call sites are unchanged. `ss_read` recomputes + the rule after decrypting and refuses an envelope whose id or nonce + prefix does not match it (`SS_E_ENVELOPE`). * Anchor record `AIENSAN1`: class, uuid, then the 120-byte `m5_anchor` (counter = Store generation, digest = SHA-256 of the newest record). @@ -80,7 +97,9 @@ Refused: Store rolled back under the anchor, anchor two behind, a missing anchor, a forked anchor, two anchor slots with one sequence, PRODUCTION/TEST mix, another store's anchor, other volume key, swapped / unclaimed / tampered envelopes, plaintext objects, generation skip, stale commit record, replayed -record, broken chain, two records for one generation, an envelope claimed twice. +record, broken chain, two records for one generation, an envelope claimed twice, +a version-1 transaction record (`SS_E_FORMAT_VERSION`) and, at read, an +envelope whose id or nonce prefix does not follow the version-2 rule. ## Tests diff --git a/native/store/store_sealed.c b/native/store/store_sealed.c index 040ebf8c..6b936695 100644 --- a/native/store/store_sealed.c +++ b/native/store/store_sealed.c @@ -9,7 +9,7 @@ #include "store_v1.h" /* ---- sealed transaction record, kind SS_KIND_TXREC ---- - * 0 "AIENSTX1" | 8 version u16 = 1 | 10 identity_class | 11 reserved = 0 + * 0 "AIENSTX1" | 8 version u16 = 2 (1 refused: SS_E_FORMAT_VERSION) | 10 identity_class | 11 reserved = 0 * | 12 count u16 (1..SS_MAX_OBJECTS) | 14 reserved u16 = 0 | 16 store_uuid[16] * | 32 store_generation u64 | 40 counter u64 (= store_generation) * | 48 key_generation u64 | 56 previous_record_digest[32] (zero at generation 2) @@ -20,7 +20,9 @@ * 0 "AIENSAN1" | 8 version u16 = 1 | 10 identity_class | 11..16 zero * | 16 store_uuid[16] | 32 m5 anchor[120] | 152..4096 zero */ #define TX_DOMAIN "AIENOS-STORE-SEALED-TX-V1" -#define ENVID_DOMAIN "AIENOS-SEALED-ENVID-V1" +#define ENVID_DOMAIN "AIENOS-SEALED-ENVID-V2" +#define ENVID_KEY_INFO "AIENOS-SEALED-ENVID-KEY-V2" +#define TX_VERSION 2u /* 1 = predictable envelope ids (refused, SS_E_FORMAT_VERSION) */ #define AN_OFF 32u static void put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } @@ -63,6 +65,7 @@ const char *ss_strerror(int e) case SS_E_FOREIGN_KIND: return "SealedForeignKind"; case SS_E_CRYPTO: return "SealedCryptoFailure"; case SS_E_NEEDS_REOPEN: return "SealedNeedsReopen"; + case SS_E_FORMAT_VERSION: return "SealedFormatVersion"; default: return st_strerror(e); } } @@ -160,7 +163,9 @@ static int tx_open(ss_store *s, const uint8_t *b, size_t len, ss_txinfo *info, ss_claim *claims, uint32_t room, uint32_t *cnt) { if (len < SS_TX_HDR + SS_TX_ENTRY + 32 || len > SS_TX_MAX) return SS_E_TXREC; - if (memcmp(b, "AIENSTX1", 8) != 0 || get16(b + 8) != 1 || b[11] != 0 || get16(b + 14) != 0) + if (memcmp(b, "AIENSTX1", 8) != 0) return SS_E_TXREC; + if (get16(b + 8) == 1) return SS_E_FORMAT_VERSION; /* GUARD:tx-version-1 */ + if (get16(b + 8) != TX_VERSION || b[11] != 0 || get16(b + 14) != 0) return SS_E_TXREC; uint32_t n = get16(b + 12); if (n == 0 || n > SS_MAX_OBJECTS) return SS_E_TXREC; @@ -325,6 +330,44 @@ void ss_tx_remac(ss_store *s) tx_mac(s->keys.k_root_auth, b, len - 32, b + len - 32); } +/* Envelope id (16 bytes) and nonce prefix (8 bytes) of the object at + * (generation, index), V2 rule: the first 24 bytes of + * HMAC(k_envid, "AIENOS-SEALED-ENVID-V2\0" || uuid || generation u64 || + * index u32 || kind u16 || version u16 || key_generation u64 || + * identity_class u8 || plaintext_len u64 || SHA-256(plaintext)), + * k_envid = HKDF-Expand(k_domain, "AIENOS-SEALED-ENVID-KEY-V2\0", 32). + * The envelope id also feeds the per-object key, so the AES-GCM-SIV + * (key, nonce) pair of a chunk changes whenever the plaintext changes. + * After a whole-disk rollback a rewrite of the same generation and index + * with different bytes therefore never repeats a (key, nonce) pair; only a + * rewrite of the identical object reproduces the identical envelope, which + * shows an attacker nothing the old image did not already show. */ +static int envid_derive(const ss_store *s, uint64_t gen, uint32_t index, uint16_t kind, + uint16_t version, const uint8_t *pt, size_t len, uint8_t out[32]) +{ + uint8_t k[32], ph[32], m[16 + 8 + 4 + 2 + 2 + 8 + 1 + 8 + 32]; + if (m5_hkdf_expand(s->keys.k_domain, (const uint8_t *)ENVID_KEY_INFO, sizeof(ENVID_KEY_INFO), k, + sizeof k) != M5_OK) + return SS_E_CRYPTO; + sha256_hash(pt, len, ph); + memcpy(m, s->uuid, 16); + put64(m + 16, gen); + put32(m + 24, index); + put16(m + 28, kind); + put16(m + 30, version); + put64(m + 32, s->keys.key_generation); + m[40] = s->keys.identity_class; + put64(m + 41, (uint64_t)len); + memcpy(m + 49, ph, 32); + aienos_hmac_sha256_ctx c; + aienos_hmac_sha256_init(&c, k); + aienos_hmac_sha256_update(&c, (const uint8_t *)ENVID_DOMAIN, sizeof(ENVID_DOMAIN)); + aienos_hmac_sha256_update(&c, m, sizeof m); + aienos_hmac_sha256_final(&c, out); + memset(k, 0, sizeof k); + return 0; +} + int ss_prepare(ss_store *s, const ss_object *o, size_t n) { if (!s || !s->ws || (!o && n)) return SS_E_ARG; @@ -341,15 +384,9 @@ int ss_prepare(ss_store *s, const ss_object *o, size_t n) for (size_t i = 0; i < n; i++) { if (o[i].len > SS_MAX_PLAINTEXT || (!o[i].bytes && o[i].len)) return SS_E_ARG; /* GUARD:sealed-size */ if (o[i].kind == 0 || o[i].version == 0) return SS_E_ARG; - uint8_t h[32], m[16 + 8 + 4]; - sha256_ctx c; - sha256_init(&c); - sha256_update(&c, (const uint8_t *)ENVID_DOMAIN, sizeof(ENVID_DOMAIN)); - memcpy(m, s->uuid, 16); - put64(m + 16, ng); - put32(m + 24, (uint32_t)i); - sha256_update(&c, m, sizeof m); - sha256_final(&c, h); + uint8_t h[32]; + if (envid_derive(s, ng, (uint32_t)i, o[i].kind, o[i].version, o[i].bytes, o[i].len, h) != 0) + return SS_E_CRYPTO; m5_object_binding b; memset(&b, 0, sizeof b); memcpy(b.store_uuid, s->uuid, 16); @@ -377,7 +414,7 @@ int ss_prepare(ss_store *s, const ss_object *o, size_t n) ws->objs[i].len = ws->env_len[i]; } memcpy(tx, "AIENSTX1", 8); - put16(tx + 8, 1); + put16(tx + 8, TX_VERSION); tx[10] = s->keys.identity_class; put16(tx + 12, (uint16_t)n); memcpy(tx + 16, s->uuid, 16); @@ -456,6 +493,15 @@ int ss_read(ss_store *s, const uint8_t sid[32], uint8_t *out, size_t cap, size_t out, cap, len); if (rc == M5_ERR_SPACE) return SS_E_ARG; if (rc != M5_OK) return SS_E_ENVELOPE; + /* the envelope id and nonce prefix must follow the V2 rule for these bytes */ + m5_env_header eh; + uint8_t h[32]; + int bad = m5_envelope_parse_header(s->ws->rd, elen, &eh) != M5_OK || + c->c.object_sequence > UINT32_MAX || + envid_derive(s, c->c.store_generation, (uint32_t)c->c.object_sequence, + c->c.obj.object_kind, c->c.obj.object_version, out, *len, h) != 0; + if (!bad) bad = memcmp(h, c->c.obj.envelope_id, 16) != 0 || memcmp(h + 16, eh.nonce_prefix, 8) != 0; + if (bad) { memset(out, 0, *len); *len = 0; return SS_E_ENVELOPE; } /* GUARD:read-envid-rule */ if (kind) *kind = c->c.obj.object_kind; return 0; } diff --git a/native/store/store_sealed.h b/native/store/store_sealed.h index 33bf2e1d..c280c5e7 100644 --- a/native/store/store_sealed.h +++ b/native/store/store_sealed.h @@ -19,6 +19,10 @@ * anchor one behind: mount reports SS_RB_PREPARED_ADVANCE and the next * transaction first catches the anchor up. Deterministic either way. * + * Envelope ids and nonce prefixes are a keyed function of the plaintext + * (record version 2, see README), so a rewrite after a whole-disk rollback + * never reuses an AES-GCM-SIV (key, nonce) pair for different bytes. + * * Limitation: the anchor sits on the same disk as the Store. It refuses a * Store-only rollback, a stale anchor more than one step behind and any * forged or replayed record, but an attacker who rolls back BOTH regions @@ -61,6 +65,7 @@ enum { SS_E_FOREIGN_KIND = -311, /* plaintext (non-sealed) object inside a sealed store */ SS_E_CRYPTO = -312, /* sealing failed */ SS_E_NEEDS_REOPEN = -313, /* an anchor write failed; reopen */ + SS_E_FORMAT_VERSION = -314, /* version-1 record (predictable envelope ids): reformat */ }; const char *ss_strerror(int e); diff --git a/native/store/tests/store_sealed_test.c b/native/store/tests/store_sealed_test.c index 99deb50d..414743b3 100644 --- a/native/store/tests/store_sealed_test.c +++ b/native/store/tests/store_sealed_test.c @@ -400,16 +400,32 @@ static void ed_seq(ss_store *s, void *a) static uint8_t dup_tx[SS_TX_MAX]; static void ed_dup_gen(ss_store *s, void *a) { - (void)a; /* a second, different, validly MACed record for the same generation */ + (void)a; /* two validly MACed, chain-valid records for the same generation, + * each claiming one of the two envelopes once, so only the + * one-record-per-generation check can refuse the pair */ ss_workspace *w = s->ws; - memcpy(dup_tx, w->tx, w->tx_len); - w->tx[56] ^= 1; - ss_tx_remac(s); + uint8_t *e1 = w->tx + SS_TX_HDR + SS_TX_ENTRY; + m5_commit c; + m5_commit_open(e1 + 32, M5_COMMIT_LEN, K.k_root_auth, K.identity_class, &c); + c.object_sequence = 0; + size_t len1 = SS_TX_HDR + SS_TX_ENTRY + 32; + memcpy(dup_tx, w->tx, SS_TX_HDR); + memcpy(dup_tx + SS_TX_HDR, e1, 32); + m5_commit_seal(&c, K.k_root_auth, dup_tx + SS_TX_HDR + 32); + dup_tx[12] = 1; + dup_tx[13] = 0; + w->tx[12] = 1; + w->tx[13] = 0; + w->tx_len = len1; + ss_tx_remac(s); /* the real record now claims envelope 0 only */ uint8_t t[SS_TX_MAX]; - memcpy(t, w->tx, w->tx_len); - memcpy(w->tx, dup_tx, w->tx_len); - memcpy(dup_tx, t, w->tx_len); - w->objs[w->nobjs++] = (st_object){SS_KIND_TXREC, 1, dup_tx, w->tx_len}; + memcpy(t, w->tx, len1); + memcpy(w->tx, dup_tx, len1); + ss_tx_remac(s); + memcpy(dup_tx, w->tx, len1); + memcpy(w->tx, t, len1); + w->objs[w->nobjs - 1].len = len1; + w->objs[w->nobjs++] = (st_object){SS_KIND_TXREC, 1, dup_tx, len1}; } static uint8_t old_sid[32]; static m5_commit old_commit; @@ -619,6 +635,136 @@ static void test_kinds(void) CHECK(SS_KIND_ENVELOPE != SS_KIND_TXREC, "distinct kinds"); } +/* ---- nonce safety (Lane 26): envelope id + nonce prefix = V2 rule ---- */ +#define NONCE_ROUNDS 24 +/* (envelope_id || nonce_prefix) of the last envelope sealed into g_ws.env[0] */ +static void last_pair(uint8_t out[24]) +{ + memcpy(out, g_ws.env[0] + 28, 16); + memcpy(out + 16, g_ws.env[0] + 44, 8); +} + +static void test_nonce(void) +{ + uint8_t ids[2][32]; + make_gen2(ids); + static uint8_t snap[RIG_BYTES], q[SS_MAX_PLAINTEXT]; + CHECK(file_load(g_path, snap, RIG_BYTES) == 0, "snapshot gen 2"); + /* gen 3 written, then whole-disk rollback to gen 2 and a different gen 3, + * NONCE_ROUNDS times: every (key input, nonce) pair must be new */ + static uint8_t pairs[NONCE_ROUNDS + 1][24]; + for (int r = 0; r <= NONCE_ROUNDS; r++) { + CHECK(file_save(g_path, snap, RIG_BYTES) == 0, "whole-disk rollback"); + fill(q, 600, 100u + (uint32_t)r); + uint8_t id[32]; + advance(&K, 42, q, 600, id, 0); + last_pair(pairs[r]); + rig rr; + ss_store s; + SCHECK_EQ(sopen(&rr, &s, &K), 0, "rewritten gen 3 mounts"); + SCHECK_EQ(ss_generation(&s), 3, "same generation after rollback"); + SCHECK_EQ(check_read(&s, id, q, 600, 42), 0, "rewritten object reads back"); + rig_close(&rr); + for (int j = 0; j < r; j++) { + CHECK(memcmp(pairs[j], pairs[r], 16) != 0, "envelope id (object key input) repeated"); + CHECK(memcmp(pairs[j] + 16, pairs[r] + 16, 8) != 0, "nonce prefix repeated"); + } + /* the old predictable V1 value is not used */ + uint8_t m[16 + 8 + 4], v1[32]; + sha256_ctx c; + sha256_init(&c); + sha256_update(&c, (const uint8_t *)"AIENOS-SEALED-ENVID-V1", sizeof("AIENOS-SEALED-ENVID-V1")); + memcpy(m, RIG_UUID, 16); + for (int b = 0; b < 8; b++) m[16 + b] = (uint8_t)((uint64_t)3 >> (8 * b)); + memset(m + 24, 0, 4); + sha256_update(&c, m, sizeof m); + sha256_final(&c, v1); + CHECK(memcmp(v1, pairs[r], 24) != 0, "V1 predictable id still used"); + } + /* the identical object rewritten at the same place gives the identical + * envelope (deterministic; reveals nothing the old image did not) */ + CHECK(file_save(g_path, snap, RIG_BYTES) == 0, "rollback"); + fill(q, 600, 100u); + advance(&K, 42, q, 600, NULL, 0); + uint8_t again[24]; + last_pair(again); + CHECK(memcmp(again, pairs[0], 24) == 0, "identical rewrite is deterministic"); + /* one byte, kind or version changed gives a new pair */ + CHECK(file_save(g_path, snap, RIG_BYTES) == 0, "rollback"); + q[599] ^= 1; + advance(&K, 42, q, 600, NULL, 0); + last_pair(again); + CHECK(memcmp(again, pairs[0], 16) != 0 && memcmp(again + 16, pairs[0] + 16, 8) != 0, "one-bit change, new pair"); + CHECK(file_save(g_path, snap, RIG_BYTES) == 0, "rollback"); + q[599] ^= 1; + advance(&K, 43, q, 600, NULL, 0); + last_pair(again); + CHECK(memcmp(again, pairs[0], 16) != 0 && memcmp(again + 16, pairs[0] + 16, 8) != 0, "kind change, new pair"); + + /* counter going backwards is refused: Store rolled back under the anchor */ + CHECK(file_save(g_path, snap, RIG_BYTES) == 0, "rollback"); + advance(&K, 42, q, 600, NULL, 0); /* gen 3 */ + static uint8_t g3[RIG_BYTES], t[RIG_BYTES]; + CHECK(file_load(g_path, g3, RIG_BYTES) == 0, "snapshot gen 3"); + memcpy(t, g3, RIG_BYTES); + copy_store(t, snap); + CHECK(file_save(g_path, t, RIG_BYTES) == 0, "write"); + SCHECK_EQ(open_rc(&K), SS_E_ROLLBACK, "counter went backwards (store 2 under anchor 3)"); + + /* a version-1 record (old predictable-id format) is refused, typed */ + CHECK(file_save(g_path, g3, RIG_BYTES) == 0, "restore gen 3"); + { + rig rr; + ss_store s; + SCHECK_EQ(sopen(&rr, &s, &K), 0, "open gen 3"); + ss_object o = {44, 1, P1, sizeof P1}; + SCHECK_EQ(ss_prepare(&s, &o, 1), 0, "prepare"); + g_ws.tx[8] = 1; + ss_tx_remac(&s); + SCHECK_EQ(ss_commit_prepared(&s, NULL, NULL), 0, "commit forged v1 record"); + rig_close(&rr); + SCHECK_EQ(open_rc(&K), SS_E_FORMAT_VERSION, "version-1 record refused"); + } + + /* a writer that sealed with the old predictable id (valid MACs and + * digests) mounts, but the envelope is refused on read */ + CHECK(file_save(g_path, g3, RIG_BYTES) == 0, "restore gen 3"); + { + rig rr; + ss_store s; + SCHECK_EQ(sopen(&rr, &s, &K), 0, "open gen 3"); + ss_object o = {44, 1, P1, sizeof P1}; + SCHECK_EQ(ss_prepare(&s, &o, 1), 0, "prepare"); + uint8_t m[16 + 8 + 4], v1[32]; + sha256_ctx c; + sha256_init(&c); + sha256_update(&c, (const uint8_t *)"AIENOS-SEALED-ENVID-V1", sizeof("AIENOS-SEALED-ENVID-V1")); + memcpy(m, RIG_UUID, 16); + for (int b = 0; b < 8; b++) m[16 + b] = (uint8_t)((uint64_t)4 >> (8 * b)); + memset(m + 24, 0, 4); + sha256_update(&c, m, sizeof m); + sha256_final(&c, v1); + m5_commit mc; + uint8_t *e = g_ws.tx + SS_TX_HDR; + CHECK(m5_commit_open(e + 32, M5_COMMIT_LEN, K.k_root_auth, K.identity_class, &mc) == M5_OK, "commit open"); + memcpy(mc.obj.envelope_id, v1, 16); + CHECK(m5_envelope_seal(K.k_domain, &mc.obj, v1 + 16, SS_CHUNK, P1, sizeof P1, g_ws.env[0], + SS_ENV_CAP, &g_ws.env_len[0]) == M5_OK, "legacy seal"); + g_ws.objs[0].len = g_ws.env_len[0]; + CHECK(sv1_object_id(SS_KIND_ENVELOPE, 1, g_ws.env[0], g_ws.env_len[0], e) == 0, "object id"); + sha256_hash(g_ws.env[0], g_ws.env_len[0], mc.object_id); + CHECK(m5_commit_seal(&mc, K.k_root_auth, e + 32) == M5_OK, "commit seal"); + ss_tx_remac(&s); + uint8_t sid[32]; + memcpy(sid, e, 32); + SCHECK_EQ(ss_commit_prepared(&s, NULL, NULL), 0, "commit legacy-id envelope"); + rig_close(&rr); + SCHECK_EQ(sopen(&rr, &s, &K), 0, "legacy-id envelope mounts (digests valid)"); + SCHECK_EQ(check_read(&s, sid, P1, sizeof P1, 44), SS_E_ENVELOPE, "legacy-id envelope refused on read"); + rig_close(&rr); + } +} + int main(int argc, char **argv) { const char *dir = argc > 1 ? argv[1] : "."; @@ -641,6 +787,7 @@ int main(int argc, char **argv) test_identity(); test_forgery(); test_anchor_crash(); + test_nonce(); printf("sealed store, %u-byte blocks: %s\n", g_bs, g_failed == before ? "ok" : "FAILED"); unlink(g_path); } From 2c8f2c1e8717cf24a6e480b8b0eb3cd9bed24ce5 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 04:11:53 -0500 Subject: [PATCH 2/2] native/store: wipe derivation scratch, constant-time id compare, wipe whole output on refusal (review) Co-Authored-By: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- native/store/store_sealed.c | 8 +++++--- native/store/tests/store_sealed_test.c | 9 +++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/native/store/store_sealed.c b/native/store/store_sealed.c index 6b936695..541cfcea 100644 --- a/native/store/store_sealed.c +++ b/native/store/store_sealed.c @@ -364,7 +364,9 @@ static int envid_derive(const ss_store *s, uint64_t gen, uint32_t index, uint16_ aienos_hmac_sha256_update(&c, (const uint8_t *)ENVID_DOMAIN, sizeof(ENVID_DOMAIN)); aienos_hmac_sha256_update(&c, m, sizeof m); aienos_hmac_sha256_final(&c, out); - memset(k, 0, sizeof k); + aienos_wipe(k, sizeof k); + aienos_wipe(ph, sizeof ph); + aienos_wipe(m, sizeof m); return 0; } @@ -500,8 +502,8 @@ int ss_read(ss_store *s, const uint8_t sid[32], uint8_t *out, size_t cap, size_t c->c.object_sequence > UINT32_MAX || envid_derive(s, c->c.store_generation, (uint32_t)c->c.object_sequence, c->c.obj.object_kind, c->c.obj.object_version, out, *len, h) != 0; - if (!bad) bad = memcmp(h, c->c.obj.envelope_id, 16) != 0 || memcmp(h + 16, eh.nonce_prefix, 8) != 0; - if (bad) { memset(out, 0, *len); *len = 0; return SS_E_ENVELOPE; } /* GUARD:read-envid-rule */ + if (!bad) bad = !aienos_ct_equal(h, c->c.obj.envelope_id, 16) || !aienos_ct_equal(h + 16, eh.nonce_prefix, 8); + if (bad) { aienos_wipe(out, cap); *len = 0; return SS_E_ENVELOPE; } /* GUARD:read-envid-rule */ if (kind) *kind = c->c.obj.object_kind; return 0; } diff --git a/native/store/tests/store_sealed_test.c b/native/store/tests/store_sealed_test.c index 414743b3..da9335da 100644 --- a/native/store/tests/store_sealed_test.c +++ b/native/store/tests/store_sealed_test.c @@ -760,6 +760,15 @@ static void test_nonce(void) SCHECK_EQ(ss_commit_prepared(&s, NULL, NULL), 0, "commit legacy-id envelope"); rig_close(&rr); SCHECK_EQ(sopen(&rr, &s, &K), 0, "legacy-id envelope mounts (digests valid)"); + { + static uint8_t canary[SS_MAX_PLAINTEXT]; + memset(canary, 0xaa, sizeof canary); + size_t clen = 999; + SCHECK_EQ(ss_read(&s, sid, canary, sizeof canary, &clen, NULL), SS_E_ENVELOPE, "legacy-id refused"); + int z = 1; + for (size_t b = 0; b < sizeof canary; b++) z &= canary[b] == 0; + CHECK(clen == 0 && z, "whole output buffer wiped on envelope refusal"); + } SCHECK_EQ(check_read(&s, sid, P1, sizeof P1, 44), SS_E_ENVELOPE, "legacy-id envelope refused on read"); rig_close(&rr); }