interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / checker2 / test_c2_receipt.c

427 lines · 18751 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/* test_c2_receipt: signed realization receipts (spec/one-layer-receipts.md).
18 *
19 * Every refusal case starts from a receipt that this same test shows is accepted under the same
20 * policy, changes one thing, and requires the exact named reason. tests/checker2/mutants.sh removes
21 * each GUARD line of src/checker2/c2_receipt.c in turn and requires this test to fail (negative
22 * control: the test can see each check).
23 *
24 * Argument 1 (optional): directory holding owner/ceremony-statement and owner/ceremony-signature
25 * (default tests/checker2/fixtures). */
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include "c2_receipt.h"
31#include "sha256.h"
32#include "searchtrace/sig/aienos_sig.h"
33
34static int fails, checks;
35#define CHECK(cond, ...) \
36 do { \
37 checks++; \
38 if (!(cond)) { \
39 fails++; \
40 fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \
41 fprintf(stderr, __VA_ARGS__); \
42 fputc('\n', stderr); \
43 } \
44 } while (0)
45#define EXPECT(got, want, what) \
46 CHECK((got) == (want), "%s: got %s, want %s", what, c2r_reason(got), c2r_reason(want))
47
48static const char *fixdir = "tests/checker2/fixtures";
49
50static size_t slurp(const char *rel, uint8_t *buf, size_t cap)
51{
52 char path[512];
53 snprintf(path, sizeof path, "%s/%s", fixdir, rel);
54 FILE *f = fopen(path, "rb");
55 if (!f) { fprintf(stderr, "cannot open %s\n", path); exit(2); }
56 size_t n = fread(buf, 1, cap, f);
57 fclose(f);
58 return n;
59}
60
61static void fill(uint8_t out[32], const char *label)
62{
63 sha256_hash((const uint8_t *)label, strlen(label), out);
64}
65
66static C2Receipt base(void)
67{
68 C2Receipt r;
69 memset(&r, 0, sizeof r);
70 r.version = C2R_FORMAT_VERSION;
71 r.signer_class = C2R_CLASS_TEST;
72 r.alg = C2R_ALG_ED25519;
73 r.evidence_class = C2R_EV_IR_CHECKED;
74 fill(r.requirements, "requirements");
75 fill(r.program, "program bytes");
76 fill(r.executable, "executable bytes");
77 c2r_deps_digest(NULL, 0, r.dependencies);
78 fill(r.checker, "checker2");
79 fill(r.producer_checker, "oscc");
80 c2r_target_id("aarch64-linux-host", r.target);
81 /* firmware all zero: host */
82 fill(r.input, "input");
83 r.policy_epoch = 5;
84 r.expiry_epoch = 9;
85 return r;
86}
87
88static uint8_t seed[32];
89static uint8_t checker_ok[32]; /* fill(.., "checker2"): the identity base() puts in a receipt */
90static const uint8_t (*test_anchor)[32] = &c2r_test_pk;
91static const uint8_t (*owner_anchor)[32] = &c2r_owner_root_pk;
92
93static C2ReceiptPolicy qual(void)
94{
95 C2ReceiptPolicy p;
96 memset(&p, 0, sizeof p);
97 p.production = 0;
98 p.test_anchors = test_anchor; p.n_test = 1;
99 p.owner_anchors = owner_anchor; p.n_owner = 1;
100 p.checkers = (const uint8_t (*)[32])checker_ok; p.n_checkers = 1;
101 p.epoch_floor = 5;
102 p.current_epoch = 7;
103 return p;
104}
105
106static C2ReceiptExpect expect_of(const C2Receipt *r)
107{
108 C2ReceiptExpect e = {r->target, r->firmware, r->program, r->executable, r->producer_checker, r->dependencies};
109 return e;
110}
111
112static void test_keys(void)
113{
114 uint8_t pk[32];
115 c2r_test_seed(seed);
116 CHECK(aienos_ed25519_public_key(pk, seed) == AIENOS_SIG_OK, "derive test pk");
117 CHECK(memcmp(pk, c2r_test_pk, 32) == 0, "c2r_test_pk is the public key of c2r_test_seed");
118 CHECK(memcmp(c2r_test_pk, c2r_owner_root_pk, 32) != 0, "test key differs from owner root");
119
120 /* The owner root is Drake's real key: its ceremony signature (made on the YubiKey) verifies
121 * under the compiled-in constant, and a one-byte change of the statement does not. */
122 uint8_t msg[4096], sig[64];
123 size_t n = slurp("owner/ceremony-statement", msg, sizeof msg);
124 CHECK(slurp("owner/ceremony-signature", sig, sizeof sig) == 64, "ceremony signature is 64 bytes");
125 CHECK(aienos_ed25519_verify(sig, msg, n, c2r_owner_root_pk) == AIENOS_SIG_OK,
126 "real owner ceremony signature verifies under c2r_owner_root_pk");
127 msg[n - 1] ^= 1;
128 CHECK(aienos_ed25519_verify(sig, msg, n, c2r_owner_root_pk) != AIENOS_SIG_OK,
129 "control: altered ceremony statement is refused");
130 msg[n - 1] ^= 1;
131 CHECK(aienos_ed25519_verify(sig, msg, n, c2r_test_pk) != AIENOS_SIG_OK,
132 "control: ceremony signature does not verify under the TEST key");
133}
134
135static void test_valid_and_deterministic(void)
136{
137 C2Receipt r = base(), r2 = base(), got;
138 uint8_t a[C2R_SIZE], b[C2R_SIZE], ida[32], idb[32];
139 EXPECT(c2r_sign_test(&r, seed, a), C2R_OK, "sign");
140 EXPECT(c2r_sign_test(&r2, seed, b), C2R_OK, "sign again");
141 CHECK(memcmp(a, b, C2R_SIZE) == 0, "identical inputs give identical receipt bytes");
142 c2r_id(a, ida);
143 c2r_id(b, idb);
144 CHECK(memcmp(ida, idb, 32) == 0, "identical ids");
145 C2ReceiptPolicy p = qual();
146 C2ReceiptExpect e = expect_of(&r);
147 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, &got), C2R_OK, "valid TEST receipt, qualification policy");
148 CHECK(memcmp(got.program, r.program, 32) == 0 && got.policy_epoch == 5 && got.expiry_epoch == 9,
149 "decoded fields round-trip");
150 /* epoch window edges are inclusive */
151 p.current_epoch = 5;
152 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_OK, "current == policy epoch");
153 p.current_epoch = 9;
154 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_OK, "current == expiry epoch");
155}
156
157/* Altered: every single-bit change anywhere in the 416 bytes is refused. */
158static void test_altered(void)
159{
160 C2Receipt r = base();
161 uint8_t a[C2R_SIZE];
162 c2r_sign_test(&r, seed, a);
163 C2ReceiptPolicy p = qual();
164 C2ReceiptExpect e = expect_of(&r);
165 unsigned accepted = 0;
166 for (unsigned i = 0; i < C2R_SIZE * 8; i++) {
167 uint8_t m[C2R_SIZE];
168 memcpy(m, a, C2R_SIZE);
169 m[i / 8] ^= (uint8_t)(1u << (i % 8));
170 if (c2r_verify(m, C2R_SIZE, &p, &e, NULL) == C2R_OK) accepted++;
171 }
172 CHECK(accepted == 0, "every one of %u single-bit alterations refused (accepted %u)", C2R_SIZE * 8, accepted);
173
174 /* named reasons for the structural fields */
175 uint8_t m[C2R_SIZE];
176 memcpy(m, a, C2R_SIZE); m[0] ^= 1;
177 EXPECT(c2r_verify(m, C2R_SIZE, &p, &e, NULL), C2R_E_MAGIC, "altered magic");
178 memcpy(m, a, C2R_SIZE); m[8] = 2;
179 EXPECT(c2r_verify(m, C2R_SIZE, &p, &e, NULL), C2R_E_VERSION, "altered version");
180 memcpy(m, a, C2R_SIZE); m[10] = 3;
181 EXPECT(c2r_verify(m, C2R_SIZE, &p, &e, NULL), C2R_E_CLASS, "altered class");
182 memcpy(m, a, C2R_SIZE); m[12] = 2;
183 EXPECT(c2r_verify(m, C2R_SIZE, &p, &e, NULL), C2R_E_ALG, "altered algorithm");
184 memcpy(m, a, C2R_SIZE); m[14] = 9;
185 EXPECT(c2r_verify(m, C2R_SIZE, &p, &e, NULL), C2R_E_EVIDENCE, "altered evidence class");
186 memcpy(m, a, C2R_SIZE); m[48] ^= 0x80;
187 EXPECT(c2r_verify(m, C2R_SIZE, &p, NULL, NULL), C2R_E_BAD_SIGNATURE, "altered program hash");
188 memcpy(m, a, C2R_SIZE); m[400] ^= 0x01;
189 EXPECT(c2r_verify(m, C2R_SIZE, &p, &e, NULL), C2R_E_BAD_SIGNATURE, "altered signature");
190 EXPECT(c2r_verify(a, C2R_SIZE - 1, &p, &e, NULL), C2R_E_SIZE, "truncated");
191 uint8_t big[C2R_SIZE + 1];
192 memcpy(big, a, C2R_SIZE); big[C2R_SIZE] = 0;
193 EXPECT(c2r_verify(big, sizeof big, &p, &e, NULL), C2R_E_SIZE, "trailing byte");
194
195 /* epoch order: an expiry before the issue epoch is refused at decode and at signing */
196 C2Receipt bad = base();
197 bad.expiry_epoch = 4;
198 EXPECT(c2r_sign_test(&bad, seed, m), C2R_E_EPOCH_ORDER, "sign refuses expiry < policy epoch");
199}
200
201static void test_wrong_key(void)
202{
203 C2Receipt r = base();
204 uint8_t a[C2R_SIZE], other_seed[32], other_pk[32];
205 fill(other_seed, "some other key");
206 aienos_ed25519_public_key(other_pk, other_seed);
207 EXPECT(c2r_sign_test(&r, other_seed, a), C2R_E_KEY_CLASS, "signing refuses a key that is not the TEST key");
208
209 /* a receipt signed by an unknown key (attached signature) is refused: not an anchor */
210 C2Receipt r2 = base();
211 uint8_t tbs[C2R_SIZE], msg[C2R_SIGNED_MSG_LEN], sig[64];
212 sha256_hash(other_pk, 32, r2.key_fp);
213 c2r_encode(&r2, tbs);
214 c2r_signed_message(tbs, msg);
215 aienos_ed25519_sign(sig, msg, sizeof msg, other_seed);
216 EXPECT(c2r_attach(&r2, other_pk, sig, a), C2R_OK, "attach a valid foreign signature");
217 C2ReceiptPolicy p = qual();
218 C2ReceiptExpect e = expect_of(&r2);
219 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_UNTRUSTED_SIGNER, "wrong key");
220
221 /* the right key fingerprint with a signature made by another key */
222 C2Receipt r3 = base();
223 sha256_hash(c2r_test_pk, 32, r3.key_fp);
224 c2r_encode(&r3, tbs);
225 c2r_signed_message(tbs, msg);
226 aienos_ed25519_sign(sig, msg, sizeof msg, other_seed);
227 EXPECT(c2r_attach(&r3, c2r_test_pk, sig, a), C2R_E_BAD_SIGNATURE, "attach refuses a signature by the wrong key");
228 memcpy(a + 352, sig, 64);
229 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_BAD_SIGNATURE, "TEST fingerprint, foreign signature");
230
231 /* swap two equally valid receipts' signatures: refused */
232 C2Receipt x = base(), y = base();
233 y.policy_epoch = 6;
234 uint8_t bx[C2R_SIZE], by[C2R_SIZE];
235 c2r_sign_test(&x, seed, bx);
236 c2r_sign_test(&y, seed, by);
237 memcpy(bx + 352, by + 352, 64);
238 EXPECT(c2r_verify(bx, C2R_SIZE, &p, &e, NULL), C2R_E_BAD_SIGNATURE, "signature of another receipt");
239}
240
241static void test_production(void)
242{
243 C2Receipt r = base();
244 uint8_t a[C2R_SIZE];
245 c2r_sign_test(&r, seed, a);
246 C2ReceiptPolicy p = qual();
247 C2ReceiptExpect e = expect_of(&r);
248 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_OK, "control: accepted in qualification");
249 p.production = 1;
250 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_TEST_IN_PRODUCTION, "TEST receipt in production");
251
252 /* the TEST key relabelled OWNER class: production refuses it even if a careless policy lists
253 * the TEST key as an owner anchor */
254 C2Receipt o = base();
255 o.signer_class = C2R_CLASS_OWNER;
256 EXPECT(c2r_sign_test(&o, seed, a), C2R_E_KEY_CLASS, "TEST key cannot sign OWNER class");
257 uint8_t tbs[C2R_SIZE], msg[C2R_SIGNED_MSG_LEN], sig[64];
258 sha256_hash(c2r_test_pk, 32, o.key_fp);
259 c2r_encode(&o, tbs);
260 c2r_signed_message(tbs, msg);
261 aienos_ed25519_sign(sig, msg, sizeof msg, seed);
262 EXPECT(c2r_attach(&o, c2r_test_pk, sig, a), C2R_OK, "attach (OWNER class, TEST key)");
263 const uint8_t sloppy[2][32] = {{0}, {0}};
264 uint8_t (*sl)[32] = (uint8_t (*)[32])sloppy;
265 memcpy(sl[0], c2r_owner_root_pk, 32);
266 memcpy(sl[1], c2r_test_pk, 32);
267 C2ReceiptPolicy q = qual();
268 q.production = 1;
269 q.owner_anchors = (const uint8_t (*)[32])sl;
270 q.n_owner = 2;
271 q.test_anchors = NULL;
272 q.n_test = 0;
273 EXPECT(c2r_verify(a, C2R_SIZE, &q, &e, NULL), C2R_E_UNTRUSTED_SIGNER,
274 "production pins the owner root: TEST key listed as owner anchor is refused");
275 /* control for the pin: the same sloppy policy outside production takes it */
276 q.production = 0;
277 EXPECT(c2r_verify(a, C2R_SIZE, &q, &e, NULL), C2R_OK, "control: non-production takes listed owner anchor");
278 /* cross-class: the same key may not be anchored in both classes */
279 q.test_anchors = test_anchor;
280 q.n_test = 1;
281 EXPECT(c2r_verify(a, C2R_SIZE, &q, &e, NULL), C2R_E_UNTRUSTED_SIGNER, "key anchored in both classes");
282
283 /* an OWNER-class receipt claiming the owner fingerprint but signed by the TEST key */
284 C2Receipt f = base();
285 f.signer_class = C2R_CLASS_OWNER;
286 sha256_hash(c2r_owner_root_pk, 32, f.key_fp);
287 c2r_encode(&f, tbs);
288 c2r_signed_message(tbs, msg);
289 aienos_ed25519_sign(sig, msg, sizeof msg, seed);
290 memcpy(tbs + 352, sig, 64);
291 EXPECT(c2r_attach(&f, c2r_owner_root_pk, sig, a), C2R_E_BAD_SIGNATURE, "attach refuses forged owner signature");
292 p = qual();
293 p.production = 1;
294 EXPECT(c2r_verify(tbs, C2R_SIZE, &p, &e, NULL), C2R_E_BAD_SIGNATURE, "forged owner receipt in production");
295}
296
297static void test_revoked_replayed_fresh(void)
298{
299 C2Receipt r = base();
300 uint8_t a[C2R_SIZE], id[32], fp[32], other[32];
301 c2r_sign_test(&r, seed, a);
302 c2r_id(a, id);
303 sha256_hash(c2r_test_pk, 32, fp);
304 fill(other, "unrelated id");
305 C2ReceiptExpect e = expect_of(&r);
306 C2ReceiptPolicy p = qual();
307 const uint8_t (*ids)[32] = (const uint8_t (*)[32])id;
308 const uint8_t (*oth)[32] = (const uint8_t (*)[32])other;
309 const uint8_t (*fps)[32] = (const uint8_t (*)[32])fp;
310 p.revoked_ids = oth; p.n_revoked_ids = 1;
311 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_OK, "control: unrelated revocation");
312 p.revoked_ids = ids;
313 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_REVOKED, "revoked receipt id");
314 p = qual();
315 p.revoked_keys = fps; p.n_revoked_keys = 1;
316 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_REVOKED, "revoked signer key");
317
318 /* replay of a superseded receipt: policy moved its floor past the receipt's epoch */
319 p = qual();
320 p.epoch_floor = 6;
321 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_STALE, "replayed after epoch floor moved");
322 p = qual();
323 p.current_epoch = 10;
324 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_EXPIRED, "expired");
325 p = qual();
326 p.epoch_floor = 0;
327 p.current_epoch = 4;
328 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_NOT_YET_VALID, "not yet valid");
329}
330
331static void test_binding(void)
332{
333 C2Receipt r = base();
334 uint8_t a[C2R_SIZE], x[32];
335 c2r_sign_test(&r, seed, a);
336 C2ReceiptPolicy p = qual();
337 C2ReceiptExpect e = expect_of(&r);
338 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_OK, "control: all bindings match");
339
340 c2r_target_id("gb10-aienos", x);
341 e = expect_of(&r); e.target = x;
342 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_WRONG_TARGET, "wrong target");
343 fill(x, "firmware v2");
344 e = expect_of(&r); e.firmware = x;
345 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_WRONG_FIRMWARE, "wrong firmware");
346 fill(x, "other program");
347 e = expect_of(&r); e.program = x;
348 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_PROGRAM_MISMATCH, "program mismatch");
349 fill(x, "other executable");
350 e = expect_of(&r); e.executable = x;
351 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_EXECUTABLE_MISMATCH, "executable mismatch");
352}
353
354/* Checker identity is a policy anchor list: a receipt judged by any other checker build is refused,
355 * an empty list refuses everything (fail closed). Producer and dependency digests are bound like
356 * the other fields. */
357static void test_checker_identity(void)
358{
359 C2Receipt r = base(), o = base();
360 uint8_t a[C2R_SIZE], b[C2R_SIZE], x[32], two[2][32];
361 fill(o.checker, "checker2 built from other sources");
362 c2r_sign_test(&r, seed, a);
363 c2r_sign_test(&o, seed, b);
364 C2ReceiptPolicy p = qual();
365 C2ReceiptExpect e = expect_of(&r);
366 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_OK, "control: receipt from the anchored checker");
367 EXPECT(c2r_verify(b, C2R_SIZE, &p, &e, NULL), C2R_E_UNTRUSTED_CHECKER, "receipt from a different checker identity");
368 EXPECT(c2r_verify(b, C2R_SIZE, &p, NULL, NULL), C2R_E_UNTRUSTED_CHECKER, "checker identity checked without expectations");
369 memcpy(two[0], checker_ok, 32);
370 memcpy(two[1], o.checker, 32);
371 p.checkers = (const uint8_t (*)[32])two;
372 p.n_checkers = 2;
373 EXPECT(c2r_verify(b, C2R_SIZE, &p, &e, NULL), C2R_OK, "control: second identity anchored");
374 p.n_checkers = 0;
375 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_UNTRUSTED_CHECKER, "empty checker list refuses everything");
376 p.checkers = NULL;
377 p.n_checkers = 1;
378 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_UNTRUSTED_CHECKER, "NULL checker list refuses everything");
379
380 p = qual();
381 fill(x, "another oscc binary");
382 e = expect_of(&r); e.producer_checker = x;
383 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_WRONG_PRODUCER, "wrong producer checker");
384 uint8_t ids[1][32];
385 fill(ids[0], "some dependency receipt");
386 c2r_deps_digest((const uint8_t (*)[32])ids, 1, x);
387 e = expect_of(&r); e.dependencies = x;
388 EXPECT(c2r_verify(a, C2R_SIZE, &p, &e, NULL), C2R_E_DEPS_MISMATCH, "dependency digest mismatch");
389}
390
391static void test_deps(void)
392{
393 uint8_t ids[3][32], d1[32], d2[32];
394 fill(ids[0], "a"); fill(ids[1], "b"); fill(ids[2], "c");
395 /* sort ascending */
396 for (int i = 0; i < 3; i++)
397 for (int j = i + 1; j < 3; j++)
398 if (memcmp(ids[i], ids[j], 32) > 0) {
399 uint8_t t[32];
400 memcpy(t, ids[i], 32); memcpy(ids[i], ids[j], 32); memcpy(ids[j], t, 32);
401 }
402 CHECK(c2r_deps_digest((const uint8_t (*)[32])ids, 3, d1) == 0, "sorted deps accepted");
403 CHECK(c2r_deps_digest((const uint8_t (*)[32])ids, 2, d2) == 0 && memcmp(d1, d2, 32) != 0,
404 "dependency count is bound");
405 uint8_t swapped[2][32];
406 memcpy(swapped[0], ids[1], 32); memcpy(swapped[1], ids[0], 32);
407 CHECK(c2r_deps_digest((const uint8_t (*)[32])swapped, 2, d2) == -1, "unsorted deps refused");
408 memcpy(swapped[1], ids[1], 32);
409 CHECK(c2r_deps_digest((const uint8_t (*)[32])swapped, 2, d2) == -1, "duplicate deps refused");
410}
411
412int main(int argc, char **argv)
413{
414 if (argc > 1) fixdir = argv[1];
415 fill(checker_ok, "checker2");
416 test_keys();
417 test_valid_and_deterministic();
418 test_altered();
419 test_wrong_key();
420 test_production();
421 test_revoked_replayed_fresh();
422 test_binding();
423 test_checker_identity();
424 test_deps();
425 printf("test_c2_receipt: %d checks, %d failed\n", checks, fails);
426 return fails ? 1 : 0;
427}