interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / tools / ck_manifest_sig.c

172 lines · 7154 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/* ck_manifest_sig.c -- PI5-8: owner signature over a Pi receipts manifest (scripts/pi5_receipts_manifest.sh),
18 * using the in-tree Ed25519 (native/sig, RFC 8032 pure Ed25519) and SHA-256 (native/argus).
19 *
20 * ck_manifest_sig statement MANIFEST OUT writes the fixed signed statement for MANIFEST
21 * ck_manifest_sig verify MANIFEST SIG PUBKEYHEX exit 0 only if SIG (64 raw bytes) is a valid Ed25519
22 * signature over the statement rebuilt from MANIFEST
23 * ck_manifest_sig sign-test MANIFEST SIGOUT signs that statement with the PUBLIC RFC 8032 TEST 1 secret key
24 *
25 * WHAT IS SIGNED (design, docs/native/PI5-8-OWNER-CEREMONY.md): the owner key lives in a YubiKey PIV slot. The PIV
26 * Ed25519 operation signs the message it is handed (pure Ed25519) but the tool and the card take only a small
27 * message (yubico-piv-tool: under 3072 bytes), and a manifest can be longer. So the owner signs a fixed ASCII
28 * statement of four LF-terminated lines that binds the manifest by its length and sha256:
29 *
30 * AIENOS-OWNER-STATEMENT-V1
31 * purpose pi5-8-receipts-manifest
32 * manifest_bytes <decimal length>
33 * manifest_sha256 <64 lowercase hex>
34 *
35 * The verifier rebuilds those bytes from the manifest it is given and checks a plain Ed25519 signature over them,
36 * so any change to the manifest changes the statement and the signature fails. There is deliberately no way to
37 * sign with any other key: no key file, no key argument. An owner signature is made by the YubiKey; this tool only
38 * verifies it. "sign-test" exists to prove the statement is signable and verifiable, and its output says TEST ONLY. */
39#include <stdint.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include "aienos_sig.h"
44#include "sha256.h"
45
46/* RFC 8032 section 7.1 TEST 1 (public, published in the RFC; protects nothing). */
47static const char *const TEST1_SK = "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60";
48static const char *const TEST1_PK = "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a";
49
50static int unhex(const char *s, uint8_t *o, size_t n)
51{
52 if (strlen(s) != 2 * n)
53 return -1;
54 for (size_t i = 0; i < n; i++) {
55 unsigned v = 0;
56 for (int k = 0; k < 2; k++) {
57 char c = s[2 * i + k];
58 unsigned d = c >= '0' && c <= '9' ? (unsigned)(c - '0')
59 : c >= 'a' && c <= 'f' ? (unsigned)(c - 'a' + 10)
60 : c >= 'A' && c <= 'F' ? (unsigned)(c - 'A' + 10) : 99u;
61 if (d > 15)
62 return -1;
63 v = v * 16 + d;
64 }
65 o[i] = (uint8_t)v;
66 }
67 return 0;
68}
69
70static uint8_t *readf(const char *path, size_t *n)
71{
72 FILE *f = fopen(path, "rb");
73 if (!f) {
74 fprintf(stderr, "error: cannot read %s\n", path);
75 exit(2);
76 }
77 size_t cap = 1 << 16, len = 0;
78 uint8_t *p = malloc(cap);
79 size_t r;
80 while (p && (r = fread(p + len, 1, cap - len, f)) > 0) {
81 len += r;
82 if (len == cap) {
83 cap *= 2;
84 p = realloc(p, cap);
85 }
86 }
87 fclose(f);
88 if (!p) {
89 fprintf(stderr, "error: out of memory\n");
90 exit(2);
91 }
92 *n = len;
93 return p;
94}
95
96/* Rebuild the signed statement for a manifest. Returns its length (fixed size for a given manifest length class). */
97static size_t statement_of(const uint8_t *m, size_t mn, char *out, size_t cap)
98{
99 uint8_t d[SHA256_DIGEST_SIZE];
100 char hex[2 * SHA256_DIGEST_SIZE + 1];
101 sha256_hash(m, mn, d);
102 for (size_t i = 0; i < SHA256_DIGEST_SIZE; i++)
103 snprintf(hex + 2 * i, 3, "%02x", d[i]);
104 int n = snprintf(out, cap,
105 "AIENOS-OWNER-STATEMENT-V1\npurpose pi5-8-receipts-manifest\nmanifest_bytes %zu\nmanifest_sha256 %s\n", mn,
106 hex);
107 if (n <= 0 || (size_t)n >= cap) {
108 fprintf(stderr, "error: statement does not fit\n");
109 exit(2);
110 }
111 return (size_t)n;
112}
113
114static int writef(const char *path, const void *p, size_t n)
115{
116 FILE *f = fopen(path, "wb");
117 if (!f || fwrite(p, 1, n, f) != n || fclose(f)) {
118 fprintf(stderr, "error: cannot write %s\n", path);
119 return -1;
120 }
121 return 0;
122}
123
124int main(int argc, char **argv)
125{
126 uint8_t sig[64], pk[32], sk[32];
127 size_t mn, sn;
128 char st[256];
129 if (argc == 4 && !strcmp(argv[1], "statement")) {
130 uint8_t *m = readf(argv[2], &mn);
131 size_t n = statement_of(m, mn, st, sizeof st);
132 return writef(argv[3], st, n) ? 2 : 0;
133 }
134 if (argc == 5 && !strcmp(argv[1], "verify")) {
135 uint8_t *m = readf(argv[2], &mn);
136 uint8_t *s = readf(argv[3], &sn);
137 if (sn != 64 || unhex(argv[4], pk, 32)) {
138 fprintf(stderr, "error: SIG must be 64 raw bytes and PUBKEYHEX 64 hex digits\n");
139 return 2;
140 }
141 memcpy(sig, s, 64);
142 size_t n = statement_of(m, mn, st, sizeof st);
143 if (aienos_ed25519_verify(sig, (const uint8_t *)st, n, pk) != AIENOS_SIG_OK) {
144 printf("MANIFEST_SIG: FAIL signature does not verify over the statement for these %zu manifest bytes\n", mn);
145 return 1;
146 }
147 printf("MANIFEST_SIG: PASS signature verifies over the statement binding %zu manifest bytes, public key %s%s\n", mn,
148 argv[4], strcmp(argv[4], TEST1_PK) ? "" : " (RFC 8032 TEST 1 key: TEST ONLY, not an owner or release key)");
149 return 0;
150 }
151 if (argc == 4 && !strcmp(argv[1], "sign-test")) {
152 uint8_t *m = readf(argv[2], &mn);
153 uint8_t pk2[32];
154 if (unhex(TEST1_SK, sk, 32) || unhex(TEST1_PK, pk, 32) || aienos_ed25519_public_key(pk2, sk) != 0 ||
155 memcmp(pk, pk2, 32)) {
156 fprintf(stderr, "error: RFC 8032 TEST 1 key self-check failed\n");
157 return 2;
158 }
159 size_t n = statement_of(m, mn, st, sizeof st);
160 if (aienos_ed25519_sign(sig, (const uint8_t *)st, n, sk) != AIENOS_SIG_OK) {
161 fprintf(stderr, "error: sign failed\n");
162 return 2;
163 }
164 if (writef(argv[3], sig, 64))
165 return 2;
166 printf("MANIFEST_SIGN: TEST ONLY, signed the statement for %zu manifest bytes with the public RFC 8032 TEST 1 key %s "
167 "(not an owner or release key)\n", mn, TEST1_PK);
168 return 0;
169 }
170 fprintf(stderr, "usage: ck_manifest_sig statement MANIFEST OUT | verify MANIFEST SIG PUBKEYHEX | sign-test MANIFEST SIGOUT\n");
171 return 2;
172}