Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/tests/test_artifact.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / tests / test_artifact.c` 189 lines ยท 6850 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_artifact.c -- host test of the C artifact library (format, TEST-key 18 * verification, admission policy, receipts) that the kernel loader uses. 19 * The pack/sign helpers come from the in-tree tool (its main is renamed). */ 20 #define main ck_artifact_tool_main 21 #include "../tools/ck_artifact_tool.c" 22 #undef main 23 24 #include "ck_test.h" 25 26 static struct cka_limits plenty(void) 27 { 28 struct cka_limits l = {1000, 1000, 1000, 16, 0, 0, 1000000000ull, 1000000000ull, 65536}; 29 return l; 30 } 31 32 static void test_verify_and_admit(void) 33 { 34 struct buf b = signed_art(1, seed_read(1), res(1, 1, 1, 1, 8), exit0_w, 4, "BASELINE"); 35 struct cka_parsed p; 36 struct cka_ident id; 37 uint8_t fp[32], pkfp[32]; 38 CHECK(cka_identify(b.p, b.n, &p, &id) == 0); 39 CHECK(verify_test(&b, &p, &id, fp) == 0); 40 sha256_hash(cka_test1_pk, 32, pkfp); 41 CHECK(memcmp(fp, pkfp, 32) == 0); 42 43 /* Production trust (no anchors): the same artifact is refused. */ 44 CHECK(cka_verify(b.p, b.n, 0, 0, &p, &id, fp) == CKA_UNTRUSTED_SIGNER); 45 46 /* Qualification policy admits it with the requested read grant. */ 47 struct cka_policy pol; 48 struct cka_decision d; 49 struct cka_limits avail = plenty(); 50 CHECK(verify_test(&b, &p, &id, fp) == 0); 51 cka_boot_policy(&pol, &cka_test1_pk, 1); 52 CHECK(cka_evaluate(&pol, &p, fp, &avail, &d) == 0); 53 CHECK(d.ngrants == 1 && d.grants[0].rights == 1 && d.grants[0].id == 1); 54 55 /* No frames available: ResourceLimit, never a partial admission. */ 56 avail.code_pages = avail.data_pages = avail.stack_pages = 0; 57 CHECK(cka_evaluate(&pol, &p, fp, &avail, &d) == CKA_RESOURCE_LIMIT); 58 59 /* A policy without signers refuses the TEST signer. */ 60 struct cka_policy prod; 61 cka_boot_policy(&prod, 0, 0); 62 avail = plenty(); 63 CHECK(cka_evaluate(&prod, &p, fp, &avail, &d) != 0); 64 uint8_t d1[32], d2[32]; 65 cka_policy_digest(&pol, d1); 66 cka_policy_digest(&prod, d2); 67 CHECK(memcmp(d1, d2, 32) != 0); 68 69 /* Any payload byte flipped: refused at verification. */ 70 struct buf t = clone(&b, 0); 71 t.p[p.payload_off] ^= 1; 72 CHECK(cka_verify(t.p, t.n, &cka_test1_pk, 1, &p, &id, fp) != 0); 73 /* Truncated: refused before any signature work. */ 74 CHECK(cka_identify(b.p, 100, &p, &id) != 0); 75 free(t.p); 76 free(b.p); 77 } 78 79 static void test_receipt(void) 80 { 81 struct cka_receipt r, back; 82 uint8_t rec[CKA_RECEIPT_SIZE], dg[32], dg2[32], ver[32]; 83 memset(&r, 0, sizeof r); 84 cka_verifier_identity("`0123456789abcdef0123456789abcdef01234567`", 1, ver); 85 r.decision = CKR_ADMITTED; 86 r.tier = CKR_TIER_SEED0B_QEMU; 87 r.seq = 1; 88 memcpy(r.verifier, ver, 32); 89 cka_receipt_nonce(ver, 1, r.nonce); 90 r.status = CKR_EXITED; 91 r.result_flags = 0xff; 92 r.syscalls = 9; 93 r.reads = 3; 94 r.denials = 5; 95 r.frames = 9; 96 memset(r.id, 0x11, 32); 97 memset(r.payload, 0x22, 32); 98 memset(r.signer, 0x33, 32); 99 memset(r.policy, 0x44, 32); 100 memset(r.requested, 0x55, 32); 101 memset(r.granted, 0x66, 32); 102 memset(r.resources, 0x77, 32); 103 CHECK(cka_receipt_validate(&r) == 0); 104 cka_receipt_encode(&r, rec); 105 CHECK(cka_receipt_decode(rec, &back) == 0); 106 CHECK(back.seq == 1 && back.syscalls == 9 && back.result_flags == 0xff); 107 cka_receipt_digest(rec, dg); 108 cka_receipt_digest(rec, dg2); 109 CHECK(memcmp(dg, dg2, 32) == 0); 110 CHECK(!cka_receipt_is_signed(rec)); 111 CHECK(cka_receipt_sign(rec, test2_seed) == 0); 112 CHECK(cka_receipt_verify(rec, &cka_test2_pk, 1) == 0); 113 CHECK(cka_receipt_verify(rec, &cka_test1_pk, 1) != 0); 114 rec[20] ^= 1; 115 CHECK(cka_receipt_verify(rec, &cka_test2_pk, 1) != 0); 116 117 /* An admitted receipt cannot carry a rejection stage. */ 118 r.stage = CKS_VERIFIED; 119 r.reason = CKA_BAD_SIGNATURE; 120 CHECK(cka_receipt_validate(&r) != 0); 121 { 122 uint8_t bad[CKA_RECEIPT_SIZE]; 123 struct cka_receipt out; 124 cka_receipt_encode(&r, bad); 125 CHECK(cka_receipt_decode(bad, &out) != 0); /* decode validates */ 126 } 127 } 128 129 /* Receipt tier by image: a Pi image (build with -DCK_PI5) writes SEED-0B-PI5, any other image SEED-0B-QEMU; the 130 * hosted verifier decodes both, round-trips the Pi tier, signs and verifies it, and refuses any other value. */ 131 static void test_tier(void) 132 { 133 struct cka_receipt r, back; 134 uint8_t rec[CKA_RECEIPT_SIZE], ver[32]; 135 #ifdef CK_PI5 136 CHECK(CKA_IMAGE_TIER == CKR_TIER_SEED0B_PI5); 137 #else 138 CHECK(CKA_IMAGE_TIER == CKR_TIER_SEED0B_QEMU); 139 #endif 140 memset(&r, 0, sizeof r); 141 cka_verifier_identity("`0123456789abcdef0123456789abcdef01234567`", 1, ver); 142 r.decision = CKR_ADMITTED; 143 r.tier = CKA_IMAGE_TIER; 144 r.seq = 1; 145 memcpy(r.verifier, ver, 32); 146 cka_receipt_nonce(ver, 1, r.nonce); 147 r.status = CKR_EXITED; 148 r.result_flags = 0xff; 149 memset(r.id, 0x11, 32); 150 memset(r.payload, 0x22, 32); 151 memset(r.signer, 0x33, 32); 152 memset(r.policy, 0x44, 32); 153 memset(r.requested, 0x55, 32); 154 memset(r.granted, 0x66, 32); 155 memset(r.resources, 0x77, 32); 156 cka_receipt_encode(&r, rec); 157 CHECK(cka_receipt_decode(rec, &back) == 0 && back.tier == CKA_IMAGE_TIER); 158 CHECK(rec[22] == (uint8_t)CKA_IMAGE_TIER && rec[23] == 0); 159 r.tier = CKR_TIER_SEED0B_PI5; 160 cka_receipt_encode(&r, rec); 161 CHECK(cka_receipt_decode(rec, &back) == 0 && back.tier == CKR_TIER_SEED0B_PI5); 162 CHECK(cka_receipt_sign(rec, test2_seed) == 0); 163 CHECK(cka_receipt_verify(rec, &cka_test2_pk, 1) == 0); 164 rec[22] = 1; /* the tier is covered by the signature */ 165 CHECK(cka_receipt_verify(rec, &cka_test2_pk, 1) != 0); 166 r.tier = 2; 167 cka_receipt_encode(&r, rec); 168 CHECK(cka_receipt_decode(rec, &back) != 0); 169 r.tier = 4; 170 cka_receipt_encode(&r, rec); 171 CHECK(cka_receipt_decode(rec, &back) != 0); 172 } 173 174 static void test_names(void) 175 { 176 CHECK(strcmp(cka_error_name(CKA_UNTRUSTED_SIGNER), "UntrustedSigner") == 0); 177 CHECK(strcmp(cka_error_name(CKL_WX_AUDIT), "WxAudit") == 0); 178 CHECK(strcmp(cka_stage_name(CKS_VERIFIED), "verified") == 0 || 179 strcmp(cka_stage_name(CKS_VERIFIED), "Verified") == 0); 180 } 181 182 int main(void) 183 { 184 test_verify_and_admit(); 185 test_receipt(); 186 test_tier(); 187 test_names(); 188 return ck_t_verdict("test_artifact"); 189 }