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 / core / context_proposal.c

151 lines · 4568 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/* context_proposal.c -- see context_proposal.h. No kernel calls, no libc. */
18#include "context_proposal.h"
19#include "../../argus/sha256.h"
20
21static const char hx[] = "0123456789abcdef";
22
23void ck_prop_clear(ck_prop *p)
24{
25 size_t i;
26 uint8_t *b = (uint8_t *)p;
27 for (i = 0; i < sizeof *p; i++) b[i] = 0;
28}
29
30static void le(sha256_ctx *h, uint64_t v, unsigned n)
31{
32 uint8_t b[8];
33 unsigned i;
34 for (i = 0; i < n; i++) b[i] = (uint8_t)(v >> (8 * i));
35 sha256_update(h, b, n);
36}
37
38void ck_prop_bind(ck_prop *p, const uint8_t agent[32], uint64_t inc, uint64_t seq, uint64_t gen, uint16_t kind,
39 const uint8_t *bytes, size_t len)
40{
41 static const char dom[] = "aienos.commit.key.v1";
42 sha256_ctx h;
43 ck_prop_clear(p);
44 sha256_hash(bytes, len, p->sha);
45 p->inc = inc;
46 p->seq = seq;
47 p->gen = gen;
48 p->kind = kind;
49 p->len = len;
50 sha256_init(&h);
51 sha256_update(&h, (const uint8_t *)dom, sizeof dom); /* with its 0x00 */
52 sha256_update(&h, agent, 32);
53 le(&h, inc, 8);
54 le(&h, seq, 8);
55 le(&h, gen, 8);
56 le(&h, kind, 2);
57 sha256_update(&h, p->sha, 32);
58 sha256_final(&h, p->key);
59 p->active = 1;
60}
61
62int ck_prop_check(const ck_prop *p, uint64_t gen_now, uint16_t kind, const uint8_t *bytes, size_t len)
63{
64 uint8_t d[32];
65 unsigned i, diff = 0;
66 if (!p->active) return CK_PROP_E_ABSENT;
67 if (kind != p->kind || len != p->len) return CK_PROP_E_CHANGED;
68 sha256_hash(bytes, len, d);
69 for (i = 0; i < 32; i++) diff |= (unsigned)(d[i] ^ p->sha[i]);
70 if (diff) return CK_PROP_E_CHANGED;
71 if (gen_now != p->gen) return CK_PROP_E_STALE;
72 return CK_PROP_OK;
73}
74
75static size_t put_s(char *o, size_t n, size_t cap, const char *s)
76{
77 while (*s) {
78 if (n < cap) o[n] = *s;
79 n++;
80 s++;
81 }
82 return n;
83}
84static size_t put_u(char *o, size_t n, size_t cap, uint64_t v)
85{
86 char t[21];
87 int k = 0;
88 do { t[k++] = (char)('0' + v % 10); v /= 10; } while (v);
89 while (k) {
90 if (n < cap) o[n] = t[--k];
91 else k--;
92 n++;
93 }
94 return n;
95}
96static size_t put_h(char *o, size_t n, size_t cap, const uint8_t *b, size_t m)
97{
98 size_t i;
99 for (i = 0; i < m; i++) {
100 if (n < cap) o[n] = hx[b[i] >> 4];
101 n++;
102 if (n < cap) o[n] = hx[b[i] & 15];
103 n++;
104 }
105 return n;
106}
107
108size_t ck_prop_trailer(const ck_prop *p, char *o, size_t cap)
109{
110 size_t n = 0;
111 if (!p->active || !o) return 0;
112 n = put_s(o, n, cap, "\nrequest: inc=");
113 n = put_u(o, n, cap, p->inc);
114 n = put_s(o, n, cap, " seq=");
115 n = put_u(o, n, cap, p->seq);
116 n = put_s(o, n, cap, " gen=");
117 n = put_u(o, n, cap, p->gen);
118 n = put_s(o, n, cap, "\nsha256: ");
119 n = put_h(o, n, cap, p->sha, 32);
120 n = put_s(o, n, cap, "\nkey: ");
121 n = put_h(o, n, cap, p->key, 32);
122 n = put_s(o, n, cap, "\n");
123 return n <= cap ? n : 0;
124}
125
126int ck_prop_has_key(const uint8_t *obj, size_t len, const uint8_t key[32])
127{
128 static const char tag[] = "key: ";
129 const size_t line = sizeof tag - 1 + 64 + 1;
130 size_t i, at;
131 if (!obj || len < line) return 0;
132 at = len - line;
133 if (at > 0 && obj[at - 1] != '\n') return 0; /* the key line starts a line */
134 for (i = 0; i < sizeof tag - 1; i++)
135 if (obj[at + i] != (uint8_t)tag[i]) return 0;
136 at += sizeof tag - 1;
137 for (i = 0; i < 32; i++)
138 if (obj[at + 2 * i] != (uint8_t)hx[key[i] >> 4] || obj[at + 2 * i + 1] != (uint8_t)hx[key[i] & 15]) return 0;
139 return obj[len - 1] == '\n';
140}
141
142const char *ck_prop_strerror(int rc)
143{
144 switch (rc) {
145 case CK_PROP_OK: return "ok";
146 case CK_PROP_E_ABSENT: return "no proposal bound";
147 case CK_PROP_E_CHANGED: return "proposal changed: the bytes differ from the ones shown";
148 case CK_PROP_E_STALE: return "stale proposal: the Store generation moved since it was shown (cancel and resubmit)";
149 }
150 return "?";
151}