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 / tools / ck_owner_gen.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 | /* ck_owner_gen.c -- build-time owner provisioning for the C kernel |
| 18 | * hardware staging image (make full CK_HARDWARE_STAGING=1). |
| 19 | * |
| 20 | * ck_owner_gen <owner pubkeys file> <machine id file> <out header> |
| 21 | * |
| 22 | * Reads owner PUBLIC material and the machine identity, checks format and |
| 23 | * length, refuses every known TEST value, and writes a C header with the |
| 24 | * bytes (no labels, no file names, no comments copied from the inputs). |
| 25 | * Nothing secret is ever read here: the Store volume key K_vol is not public |
| 26 | * material and has no build-time source (store_boot.c |
| 27 | * ck_store_production_keys, BLOCKED_OPERATOR on the TRUST-1 key ceremony). |
| 28 | * |
| 29 | * Input format (both files): text lines "<name> <hex>"; '#' starts a comment |
| 30 | * line; blank lines are ignored; every name must appear exactly once; any |
| 31 | * other name is refused. |
| 32 | * owner pubkeys file: owner_root_ed25519 <64 hex> Owner Root public key |
| 33 | * machine id file: machine_id <64 hex> ARGUS machine id (32 B) |
| 34 | * store_uuid <32 hex> boot Store uuid (16 B) |
| 35 | * |
| 36 | * Refused values: all bytes equal (all zero included); the RFC 8032 section |
| 37 | * 7.1 TEST 1/2/3 public keys; the TEST machine id (0xA1 then 31 zero bytes); |
| 38 | * the TEST store uuid "AIEN-TEST-BOOT01". Exit 0 and "CK_OWNER_GEN: OK" on |
| 39 | * success, else exit 1, "CK_OWNER_GEN: REFUSED <reason>" and no header. |
| 40 | * Hosted tool; never part of an image. No outside libraries. */ |
| 41 | #include <stdint.h> |
| 42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
| 45 | |
| 46 | /* Known TEST values. RFC 8032 section 7.1 TEST 1 and TEST 2 equal |
| 47 | * native/kernel/artifact/format.c cka_test1_pk / cka_test2_pk. */ |
| 48 | static const uint8_t RFC8032_T1[32] = { |
| 49 | 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b, 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a, |
| 50 | 0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 0x51, 0x1a}; |
| 51 | static const uint8_t RFC8032_T2[32] = { |
| 52 | 0x3d, 0x40, 0x17, 0xc3, 0xe8, 0x43, 0x89, 0x5a, 0x92, 0xb7, 0x0a, 0xa7, 0x4d, 0x1b, 0x7e, 0xbc, |
| 53 | 0x9c, 0x98, 0x2c, 0xcf, 0x2e, 0xc4, 0x96, 0x8c, 0xc0, 0xcd, 0x55, 0xf1, 0x2a, 0xf4, 0x66, 0x0c}; |
| 54 | static const uint8_t RFC8032_T3[32] = { |
| 55 | 0xfc, 0x51, 0xcd, 0x8e, 0x62, 0x18, 0xa1, 0xa3, 0x8d, 0xa4, 0x7e, 0xd0, 0x02, 0x30, 0xf0, 0x58, |
| 56 | 0x08, 0x16, 0xed, 0x13, 0xba, 0x33, 0x03, 0xac, 0x5d, 0xeb, 0x91, 0x15, 0x48, 0x90, 0x80, 0x25}; |
| 57 | static const uint8_t TEST_MACHINE[32] = {0xA1}; |
| 58 | static const uint8_t TEST_UUID[16] = {'A', 'I', 'E', 'N', '-', 'T', 'E', 'S', |
| 59 | 'T', '-', 'B', 'O', 'O', 'T', '0', '1'}; |
| 60 | |
| 61 | typedef struct { |
| 62 | const char *name; |
| 63 | size_t len; |
| 64 | uint8_t v[32]; |
| 65 | int seen; |
| 66 | } field; |
| 67 | |
| 68 | static void refuse(const char *what, const char *why) |
| 69 | { |
| 70 | printf("CK_OWNER_GEN: REFUSED %s: %s\n", what, why); |
| 71 | exit(1); |
| 72 | } |
| 73 | |
| 74 | static int hexval(int c) |
| 75 | { |
| 76 | if (c >= '0' && c <= '9') return c - '0'; |
| 77 | if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
| 78 | if (c >= 'A' && c <= 'F') return c - 'A' + 10; |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | static void parse(const char *path, field *f, size_t nf) |
| 83 | { |
| 84 | FILE *fp = fopen(path, "r"); |
| 85 | if (!fp) refuse(path, "cannot open"); |
| 86 | char line[512]; |
| 87 | unsigned ln = 0; |
| 88 | while (fgets(line, sizeof line, fp)) { |
| 89 | ln++; |
| 90 | size_t n = strlen(line); |
| 91 | if (n == sizeof line - 1 && line[n - 1] != '\n') refuse(path, "line too long"); |
| 92 | while (n && (line[n - 1] == '\n' || line[n - 1] == '\r' || line[n - 1] == ' ' || line[n - 1] == '\t')) |
| 93 | line[--n] = 0; |
| 94 | char *p = line; |
| 95 | while (*p == ' ' || *p == '\t') p++; |
| 96 | if (!*p || *p == '#') continue; |
| 97 | char *name = p; |
| 98 | while (*p && *p != ' ' && *p != '\t') p++; |
| 99 | if (!*p) refuse(path, "line without a value"); |
| 100 | *p++ = 0; |
| 101 | while (*p == ' ' || *p == '\t') p++; |
| 102 | char *hex = p; |
| 103 | field *t = 0; |
| 104 | for (size_t i = 0; i < nf; i++) |
| 105 | if (!strcmp(f[i].name, name)) t = &f[i]; |
| 106 | if (!t) { |
| 107 | printf("CK_OWNER_GEN: REFUSED %s line %u: unknown name \"%.64s\"\n", path, ln, name); |
| 108 | exit(1); |
| 109 | } |
| 110 | if (t->seen) refuse(t->name, "given twice"); |
| 111 | if (strlen(hex) != 2 * t->len) { |
| 112 | printf("CK_OWNER_GEN: REFUSED %s: need exactly %zu hex digits, got %zu\n", t->name, 2 * t->len, |
| 113 | strlen(hex)); |
| 114 | exit(1); |
| 115 | } |
| 116 | for (size_t i = 0; i < t->len; i++) { |
| 117 | int hi = hexval(hex[2 * i]), lo = hexval(hex[2 * i + 1]); |
| 118 | if (hi < 0 || lo < 0) refuse(t->name, "not hex"); |
| 119 | t->v[i] = (uint8_t)(hi << 4 | lo); |
| 120 | } |
| 121 | t->seen = 1; |
| 122 | } |
| 123 | if (ferror(fp)) refuse(path, "read error"); |
| 124 | fclose(fp); |
| 125 | for (size_t i = 0; i < nf; i++) |
| 126 | if (!f[i].seen) refuse(f[i].name, "missing"); |
| 127 | } |
| 128 | |
| 129 | static void check_value(const field *t) |
| 130 | { |
| 131 | size_t same = 1; |
| 132 | while (same < t->len && t->v[same] == t->v[0]) same++; |
| 133 | if (same == t->len) refuse(t->name, "all bytes equal (placeholder, not an identity)"); |
| 134 | } |
| 135 | |
| 136 | static void emit(FILE *o, const char *sym, const field *t) |
| 137 | { |
| 138 | fprintf(o, "static const uint8_t %s[%zu] __attribute__((unused)) = {", sym, t->len); |
| 139 | for (size_t i = 0; i < t->len; i++) fprintf(o, "%s0x%02x", i ? ", " : "", t->v[i]); |
| 140 | fprintf(o, "};\n"); |
| 141 | } |
| 142 | |
| 143 | int main(int argc, char **argv) |
| 144 | { |
| 145 | if (argc != 4) { |
| 146 | fprintf(stderr, "usage: ck_owner_gen <owner pubkeys file> <machine id file> <out header>\n"); |
| 147 | return 2; |
| 148 | } |
| 149 | field own[1] = {{"owner_root_ed25519", 32, {0}, 0}}; |
| 150 | field mach[2] = {{"machine_id", 32, {0}, 0}, {"store_uuid", 16, {0}, 0}}; |
| 151 | parse(argv[1], own, 1); |
| 152 | parse(argv[2], mach, 2); |
| 153 | check_value(&own[0]); |
| 154 | check_value(&mach[0]); |
| 155 | check_value(&mach[1]); |
| 156 | if (!memcmp(own[0].v, RFC8032_T1, 32) || !memcmp(own[0].v, RFC8032_T2, 32) || !memcmp(own[0].v, RFC8032_T3, 32)) |
| 157 | refuse("owner_root_ed25519", "is an RFC 8032 TEST public key"); |
| 158 | if (!memcmp(mach[0].v, TEST_MACHINE, 32)) refuse("machine_id", "is the TEST machine id (0xA1)"); |
| 159 | if (!memcmp(mach[1].v, TEST_UUID, 16)) refuse("store_uuid", "is the TEST store uuid"); |
| 160 | if (!memcmp(own[0].v, mach[0].v, 32)) refuse("machine_id", "equals the owner root public key"); |
| 161 | |
| 162 | char tmp[4096]; |
| 163 | if (snprintf(tmp, sizeof tmp, "%s.tmp", argv[3]) >= (int)sizeof tmp) refuse(argv[3], "path too long"); |
| 164 | FILE *o = fopen(tmp, "w"); |
| 165 | if (!o) refuse(argv[3], "cannot write"); |
| 166 | fprintf(o, "/* Generated by native/kernel/tools/ck_owner_gen.c. Do not edit or commit. */\n" |
| 167 | "#ifndef AIENOS_CK_OWNER_PROV_H\n#define AIENOS_CK_OWNER_PROV_H\n#include <stdint.h>\n" |
| 168 | "#define CK_OWNER_PROVISIONED 1\n"); |
| 169 | emit(o, "ck_owner_root_pk", &own[0]); |
| 170 | emit(o, "ck_owner_machine_id", &mach[0]); |
| 171 | emit(o, "ck_owner_store_uuid", &mach[1]); |
| 172 | fprintf(o, "#endif\n"); |
| 173 | if (fclose(o) || rename(tmp, argv[3])) { |
| 174 | remove(tmp); |
| 175 | refuse(argv[3], "cannot write"); |
| 176 | } |
| 177 | printf("CK_OWNER_GEN: OK %s\n", argv[3]); |
| 178 | return 0; |
| 179 | } |