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 / core / 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 | /* artifact_card.c -- see artifact_card.h. No kernel calls, no libc (host-tested by tests/test_artifact_card.c). */ |
| 18 | #include "artifact_card.h" |
| 19 | |
| 20 | struct ck_acard_boot ck_acard_boot; |
| 21 | uint8_t ck_acard_buf[CK_ACARD_FILE_MAX + 1u] __attribute__((aligned(64))); |
| 22 | |
| 23 | static void zero(struct ck_acard *o) |
| 24 | { |
| 25 | unsigned char *p = (unsigned char *)o; |
| 26 | for (size_t i = 0; i < sizeof *o; i++) p[i] = 0; |
| 27 | } |
| 28 | |
| 29 | static uint32_t rd16(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8; } |
| 30 | static uint32_t rd32(const uint8_t *p) { return rd16(p) | rd16(p + 2) << 16; } |
| 31 | |
| 32 | int ck_acard_parse(const uint8_t *buf, size_t n, struct ck_acard *out, const char **why) |
| 33 | { |
| 34 | static const char magic[8] = {'A', 'I', 'E', 'N', 'B', 'N', 'D', 0}; |
| 35 | const char *w = 0; |
| 36 | size_t at = 12, i; |
| 37 | uint32_t count = 0, c; |
| 38 | if (out) zero(out); |
| 39 | if (!buf || !out) { |
| 40 | w = "no bundle"; |
| 41 | goto fail; |
| 42 | } |
| 43 | if (n < 12 || n > CK_ACARD_FILE_MAX) { |
| 44 | w = "file size must be 12..131072 bytes"; |
| 45 | goto fail; |
| 46 | } |
| 47 | for (i = 0; i < 8; i++) |
| 48 | if (buf[i] != (uint8_t)magic[i]) { |
| 49 | w = "bad magic (want AIENBND)"; |
| 50 | goto fail; |
| 51 | } |
| 52 | count = rd32(buf + 8); |
| 53 | if (count < 1 || count > CK_ACARD_MAX_ENTRIES) { |
| 54 | w = "entry count must be 1..16"; |
| 55 | goto fail; |
| 56 | } |
| 57 | for (c = 0; c < count; c++) { |
| 58 | uint32_t nl, bl; |
| 59 | if (n - at < 2) { |
| 60 | w = "truncated entry header"; |
| 61 | goto fail; |
| 62 | } |
| 63 | nl = rd16(buf + at); |
| 64 | at += 2; |
| 65 | if (nl < 1 || nl > CK_ACARD_NAME_MAX) { |
| 66 | w = "name length must be 1..32"; |
| 67 | goto fail; |
| 68 | } |
| 69 | if (n - at < (size_t)nl + 4u) { |
| 70 | w = "truncated entry"; |
| 71 | goto fail; |
| 72 | } |
| 73 | for (i = 0; i < nl; i++) |
| 74 | if (buf[at + i] < 0x21 || buf[at + i] > 0x7e) { |
| 75 | w = "name byte is not printable ASCII"; |
| 76 | goto fail; |
| 77 | } |
| 78 | for (uint32_t k = 0; k < c; k++) { |
| 79 | size_t j = 0; |
| 80 | while (j < nl && out->e[k].name[j] == (char)buf[at + j]) j++; |
| 81 | if (j == nl && out->e[k].name[nl] == 0) { |
| 82 | w = "duplicate name"; |
| 83 | goto fail; |
| 84 | } |
| 85 | } |
| 86 | for (i = 0; i < nl; i++) out->e[c].name[i] = (char)buf[at + i]; |
| 87 | out->e[c].name[nl] = 0; |
| 88 | at += nl; |
| 89 | bl = rd32(buf + at); |
| 90 | at += 4; |
| 91 | if (bl < 1 || bl > CK_ACARD_ENTRY_MAX) { |
| 92 | w = "artifact length must be 1..655360"; |
| 93 | goto fail; |
| 94 | } |
| 95 | if (n - at < bl) { |
| 96 | w = "artifact bytes run past the end of the file"; |
| 97 | goto fail; |
| 98 | } |
| 99 | out->e[c].len = bl; |
| 100 | out->e[c].bytes = buf + at; |
| 101 | at += bl; |
| 102 | } |
| 103 | #ifndef CK_ACARD_MUTANT_TRAILING |
| 104 | if (at != n) { |
| 105 | w = "bytes after the last entry"; |
| 106 | goto fail; |
| 107 | } |
| 108 | #endif |
| 109 | out->count = count; |
| 110 | if (why) *why = 0; |
| 111 | return 0; |
| 112 | fail: |
| 113 | if (out) zero(out); |
| 114 | if (why) *why = w; |
| 115 | return -1; |
| 116 | } |