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 / kernel / tests / test_artifact_card.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 | /* test_artifact_card.c -- core/artifact_card.c: the card artifact bundle of the EL0 artifact qualification image |
| 18 | * (CK_ARTIFACT_CARD, PI5-7 cut 3). The parser is strict (magic, count, name bytes, uniqueness, lengths, exact |
| 19 | * end of file) and a refused file leaves nothing behind for the loader. The layout is the one |
| 20 | * tools/ck_artifact_tool.c "bundle" writes: "AIENBND\0", u32 count, count x {u16 name_len, name, u32 len, bytes}. */ |
| 21 | #include <string.h> |
| 22 | #include "ck_test.h" |
| 23 | #include "artifact_card.h" |
| 24 | |
| 25 | static size_t put(uint8_t *b, size_t at, const char *name, uint32_t len, uint8_t fill) |
| 26 | { |
| 27 | size_t nl = strlen(name); |
| 28 | b[at] = (uint8_t)nl; |
| 29 | b[at + 1] = (uint8_t)(nl >> 8); |
| 30 | memcpy(b + at + 2, name, nl); |
| 31 | at += 2 + nl; |
| 32 | b[at] = (uint8_t)len; |
| 33 | b[at + 1] = (uint8_t)(len >> 8); |
| 34 | b[at + 2] = (uint8_t)(len >> 16); |
| 35 | b[at + 3] = (uint8_t)(len >> 24); |
| 36 | memset(b + at + 4, fill, len); |
| 37 | return at + 4 + len; |
| 38 | } |
| 39 | |
| 40 | static size_t head(uint8_t *b, uint32_t count) |
| 41 | { |
| 42 | memset(b, 0, 12); |
| 43 | memcpy(b, "AIENBND", 8); |
| 44 | b[8] = (uint8_t)count; |
| 45 | b[9] = (uint8_t)(count >> 8); |
| 46 | return 12; |
| 47 | } |
| 48 | |
| 49 | static int refused(const uint8_t *b, size_t n, const char *want) |
| 50 | { |
| 51 | struct ck_acard o; |
| 52 | const char *why = 0; |
| 53 | int rc = ck_acard_parse(b, n, &o, &why); |
| 54 | int clean = o.count == 0 && o.e[0].name[0] == 0 && o.e[0].bytes == 0 && o.e[0].len == 0; |
| 55 | return rc != 0 && why && strstr(why, want) != 0 && clean; |
| 56 | } |
| 57 | |
| 58 | int main(void) |
| 59 | { |
| 60 | static uint8_t b[CK_ACARD_FILE_MAX + 64]; |
| 61 | struct ck_acard o; |
| 62 | const char *why = (const char *)1; |
| 63 | size_t n; |
| 64 | |
| 65 | /* a well formed two-entry bundle */ |
| 66 | n = head(b, 2); |
| 67 | n = put(b, n, "P25EXEC.AIEN", 100, 0xa1); |
| 68 | n = put(b, n, "P25TAMP.AIEN", 7, 0xb2); |
| 69 | CHECK(ck_acard_parse(b, n, &o, &why) == 0); |
| 70 | CHECK(why == 0 && o.count == 2); |
| 71 | CHECK(strcmp(o.e[0].name, "P25EXEC.AIEN") == 0 && o.e[0].len == 100 && o.e[0].bytes[0] == 0xa1 && o.e[0].bytes[99] == 0xa1); |
| 72 | CHECK(strcmp(o.e[1].name, "P25TAMP.AIEN") == 0 && o.e[1].len == 7 && o.e[1].bytes == b + (n - 7)); |
| 73 | /* entry bytes are offered in place, never copied or reordered */ |
| 74 | CHECK(o.e[0].bytes + 100 + 2 + 12 + 4 == o.e[1].bytes); |
| 75 | |
| 76 | /* one-byte exact-end rule: a trailing byte is refused */ |
| 77 | b[n] = 0; |
| 78 | CHECK(refused(b, n + 1, "bytes after the last entry")); |
| 79 | /* a missing last byte is refused */ |
| 80 | CHECK(refused(b, n - 1, "past the end")); |
| 81 | |
| 82 | /* header */ |
| 83 | n = head(b, 1); |
| 84 | n = put(b, n, "A", 4, 1); |
| 85 | CHECK(ck_acard_parse(b, n, &o, &why) == 0); |
| 86 | CHECK(refused(b, 11, "file size")); |
| 87 | CHECK(refused(b, 0, "file size")); |
| 88 | CHECK(ck_acard_parse(0, 12, &o, &why) != 0); |
| 89 | CHECK(ck_acard_parse(b, n, 0, &why) != 0); |
| 90 | b[0] = 'X'; |
| 91 | CHECK(refused(b, n, "bad magic")); |
| 92 | b[0] = 'A'; |
| 93 | b[7] = 1; /* the NUL after AIENBND */ |
| 94 | CHECK(refused(b, n, "bad magic")); |
| 95 | b[7] = 0; |
| 96 | b[8] = 0; |
| 97 | CHECK(refused(b, n, "entry count")); |
| 98 | b[8] = 17; |
| 99 | CHECK(refused(b, n, "entry count")); |
| 100 | b[8] = 2; /* promises a second entry that is not there */ |
| 101 | CHECK(refused(b, n, "truncated")); |
| 102 | b[8] = 1; |
| 103 | b[11] = 1; /* count above 16 through the high bytes */ |
| 104 | CHECK(refused(b, n, "entry count")); |
| 105 | b[11] = 0; |
| 106 | |
| 107 | /* names */ |
| 108 | n = head(b, 1); |
| 109 | n = put(b, n, "", 4, 1); |
| 110 | CHECK(refused(b, n, "name length")); |
| 111 | n = head(b, 1); |
| 112 | n = put(b, n, "123456789012345678901234567890123", 4, 1); /* 33 */ |
| 113 | CHECK(refused(b, n, "name length")); |
| 114 | n = head(b, 1); |
| 115 | n = put(b, n, "123456789012345678901234567890AB", 4, 1); /* 32 is fine */ |
| 116 | CHECK(ck_acard_parse(b, n, &o, &why) == 0 && strlen(o.e[0].name) == 32); |
| 117 | n = head(b, 1); |
| 118 | n = put(b, n, "A B", 4, 1); |
| 119 | CHECK(refused(b, n, "printable")); |
| 120 | n = head(b, 1); |
| 121 | n = put(b, n, "A\x7f", 4, 1); |
| 122 | CHECK(refused(b, n, "printable")); |
| 123 | n = head(b, 2); |
| 124 | n = put(b, n, "SAME", 4, 1); |
| 125 | n = put(b, n, "SAME", 5, 2); |
| 126 | CHECK(refused(b, n, "duplicate")); |
| 127 | n = head(b, 2); |
| 128 | n = put(b, n, "SAME", 4, 1); |
| 129 | n = put(b, n, "SAMEX", 5, 2); /* a name that only starts like an earlier one is not a duplicate */ |
| 130 | CHECK(ck_acard_parse(b, n, &o, &why) == 0 && o.count == 2); |
| 131 | |
| 132 | /* lengths */ |
| 133 | n = head(b, 1); |
| 134 | n = put(b, n, "A", 1, 9); |
| 135 | CHECK(ck_acard_parse(b, n, &o, &why) == 0 && o.e[0].len == 1); |
| 136 | n = head(b, 1); |
| 137 | n = put(b, n, "A", 0, 9); |
| 138 | CHECK(refused(b, n, "artifact length")); |
| 139 | n = head(b, 1); |
| 140 | n = put(b, n, "A", 4, 9); |
| 141 | b[2 + 12 + 1] = 0xff; |
| 142 | b[2 + 12 + 2] = 0xff; |
| 143 | b[2 + 12 + 3] = 0xff; |
| 144 | b[2 + 12 + 4] = 0xff; /* length 0xffffffff */ |
| 145 | CHECK(refused(b, n, "artifact length")); |
| 146 | n = head(b, 1); |
| 147 | n = put(b, n, "A", 40, 9); |
| 148 | b[2 + 12 + 1] = 41; /* promises one byte more than the file holds */ |
| 149 | CHECK(refused(b, n, "past the end")); |
| 150 | |
| 151 | /* limits: 16 entries pass, a file of exactly the maximum size passes, one byte more is refused */ |
| 152 | n = head(b, 16); |
| 153 | for (int i = 0; i < 16; i++) { |
| 154 | char nm[4] = {'E', (char)('A' + i), 0, 0}; |
| 155 | n = put(b, n, nm, 3, (uint8_t)i); |
| 156 | } |
| 157 | CHECK(ck_acard_parse(b, n, &o, &why) == 0 && o.count == 16 && strcmp(o.e[15].name, "EP") == 0); |
| 158 | n = head(b, 1); |
| 159 | n = put(b, n, "BIG", CK_ACARD_FILE_MAX - 12 - 2 - 3 - 4, 5); |
| 160 | CHECK(n == CK_ACARD_FILE_MAX); |
| 161 | CHECK(ck_acard_parse(b, n, &o, &why) == 0 && o.e[0].len == CK_ACARD_FILE_MAX - 21); |
| 162 | n = head(b, 1); |
| 163 | n = put(b, n, "BIG", CK_ACARD_FILE_MAX - 12 - 2 - 3 - 4 + 1, 5); |
| 164 | CHECK(refused(b, n, "file size")); |
| 165 | |
| 166 | return ck_t_verdict("test_artifact_card"); |
| 167 | } |