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 / tests / test_entropy.c

183 lines · 6319 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/* test_entropy.c -- host tests of core/entropy.c (RNDR-or-refuse) against a
18 * mocked RNDR: FEAT_RNG absent, RNDR always failing, failing then
19 * succeeding (inside and at the retry bound), failing mid-fill, a stuck
20 * (repeating) source, and the latched refusal. */
21#include <string.h>
22#include "ck_test.h"
23#include "entropy.h"
24
25struct mock {
26 int present;
27 int fail_next; /* fail this many reads, then succeed */
28 long ok_left; /* successes before failing forever (-1 = never) */
29 int constant; /* return the same word every time */
30 uint64_t ctr;
31 unsigned reads, present_calls;
32};
33
34static int m_present(void *c)
35{
36 struct mock *m = c;
37 m->present_calls++;
38 return m->present;
39}
40
41static int m_read(void *c, uint64_t *out)
42{
43 struct mock *m = c;
44 m->reads++;
45 *out = 0x1111111111111111ull; /* what a failed RNDR leaves (Arm: 0) */
46 if (m->fail_next > 0) {
47 m->fail_next--;
48 *out = 0;
49 return 0;
50 }
51 if (m->ok_left == 0) {
52 *out = 0;
53 return 0;
54 }
55 if (m->ok_left > 0) m->ok_left--;
56 *out = m->constant ? 0x0123456789abcdefull : (0x9e3779b97f4a7c15ull * ++m->ctr);
57 return 1;
58}
59
60static int all(const uint8_t *p, size_t n, uint8_t v)
61{
62 for (size_t i = 0; i < n; i++)
63 if (p[i] != v) return 0;
64 return 1;
65}
66
67static void setup(struct mock *m, struct ck_rng_ops *ops)
68{
69 memset(m, 0, sizeof *m);
70 m->present = 1;
71 m->ok_left = -1;
72 ops->present = m_present;
73 ops->read = m_read;
74 ops->ctx = m;
75}
76
77int main(void)
78{
79 struct mock m;
80 struct ck_rng_ops ops;
81 struct ck_rng r;
82 uint8_t buf[40];
83
84 /* Never probed (zeroed state): refuse, zero the buffer. */
85 memset(&r, 0, sizeof r);
86 memset(buf, 0xaa, sizeof buf);
87 CHECK(ck_rng_fill(&r, buf, sizeof buf) == CK_RNG_UNPROBED);
88 CHECK(all(buf, sizeof buf, 0));
89 CHECK(ck_rng_probe(&r, NULL) == CK_RNG_EARG);
90
91 /* 1. FEAT_RNG absent: refused without a single RNDR read. */
92 setup(&m, &ops);
93 m.present = 0;
94 CHECK(ck_rng_probe(&r, &ops) == CK_RNG_ABSENT);
95 CHECK(m.reads == 0);
96 memset(buf, 0xaa, sizeof buf);
97 CHECK(ck_rng_fill(&r, buf, sizeof buf) == CK_RNG_ABSENT);
98 CHECK(all(buf, sizeof buf, 0));
99 CHECK(m.reads == 0);
100 CHECK(strcmp(ck_rng_reason(r.state), "absent") == 0);
101
102 /* 2. RNDR always fails: exactly CK_RNG_RETRIES reads, then refused and
103 * latched (no further reads). */
104 setup(&m, &ops);
105 m.ok_left = 0;
106 CHECK(ck_rng_probe(&r, &ops) == CK_RNG_FAILED);
107 CHECK(m.reads == CK_RNG_RETRIES);
108 memset(buf, 0xaa, sizeof buf);
109 CHECK(ck_rng_fill(&r, buf, sizeof buf) == CK_RNG_FAILED);
110 CHECK(all(buf, sizeof buf, 0));
111 CHECK(m.reads == CK_RNG_RETRIES);
112 CHECK(strcmp(ck_rng_reason(r.state), "failed") == 0);
113
114 /* 3a. Fails CK_RNG_RETRIES - 1 times, then succeeds: usable. */
115 setup(&m, &ops);
116 m.fail_next = (int)CK_RNG_RETRIES - 1;
117 CHECK(ck_rng_probe(&r, &ops) == CK_RNG_OK);
118 CHECK(r.retries == CK_RNG_RETRIES - 1);
119 CHECK(strcmp(ck_rng_reason(r.state), "rndr") == 0);
120 /* 3b. Fails exactly CK_RNG_RETRIES times: refused (bound is inclusive). */
121 setup(&m, &ops);
122 m.fail_next = (int)CK_RNG_RETRIES;
123 CHECK(ck_rng_probe(&r, &ops) == CK_RNG_FAILED);
124
125 /* 4. Normal fill, length not a multiple of 8: little-endian words, in order. */
126 setup(&m, &ops);
127 CHECK(ck_rng_probe(&r, &ops) == CK_RNG_OK);
128 CHECK(r.words == 2 && m.reads == 2);
129 memset(buf, 0, sizeof buf);
130 CHECK(ck_rng_fill(&r, buf, 13) == CK_RNG_OK);
131 uint64_t w3 = 0x9e3779b97f4a7c15ull * 3, w4 = 0x9e3779b97f4a7c15ull * 4;
132 int match = 1;
133 for (int k = 0; k < 8; k++) match &= buf[k] == (uint8_t)(w3 >> (8 * k));
134 for (int k = 0; k < 5; k++) match &= buf[8 + k] == (uint8_t)(w4 >> (8 * k));
135 CHECK(match);
136 CHECK(all(buf + 13, sizeof buf - 13, 0));
137 CHECK(ck_rng_fill(&r, buf, 0) == CK_RNG_OK);
138
139 /* 5. Transient failure during a fill is retried and recovered. */
140 m.fail_next = 5;
141 uint64_t before = r.retries;
142 CHECK(ck_rng_fill(&r, buf, 16) == CK_RNG_OK);
143 CHECK(r.retries == before + 5);
144
145 /* 6. Source dies mid-fill: whole buffer zeroed (no partial bytes),
146 * refusal latched even after RNDR would recover. */
147 setup(&m, &ops);
148 CHECK(ck_rng_probe(&r, &ops) == CK_RNG_OK);
149 m.ok_left = 2; /* two more words, then dead */
150 memset(buf, 0xaa, sizeof buf);
151 CHECK(ck_rng_fill(&r, buf, sizeof buf) == CK_RNG_FAILED);
152 CHECK(all(buf, sizeof buf, 0));
153 m.ok_left = -1;
154 unsigned reads = m.reads;
155 memset(buf, 0xaa, sizeof buf);
156 CHECK(ck_rng_fill(&r, buf, 8) == CK_RNG_FAILED);
157 CHECK(all(buf, 8, 0));
158 CHECK(m.reads == reads);
159
160 /* 7. Stuck source: two equal words refuse at probe. */
161 setup(&m, &ops);
162 m.constant = 1;
163 CHECK(ck_rng_probe(&r, &ops) == CK_RNG_STUCK);
164 CHECK(strcmp(ck_rng_reason(r.state), "stuck") == 0);
165 memset(buf, 0xaa, sizeof buf);
166 CHECK(ck_rng_fill(&r, buf, sizeof buf) == CK_RNG_STUCK);
167 CHECK(all(buf, sizeof buf, 0));
168 /* 7b. Source sticks mid-fill: refused, buffer zeroed. */
169 setup(&m, &ops);
170 CHECK(ck_rng_probe(&r, &ops) == CK_RNG_OK);
171 m.constant = 1; /* next word 0x0123.. differs from the last; the one after repeats */
172 memset(buf, 0xaa, sizeof buf);
173 CHECK(ck_rng_fill(&r, buf, 24) == CK_RNG_STUCK);
174 CHECK(all(buf, 24, 0));
175
176 /* 8. Re-probe after a refusal starts fresh (a new boot). */
177 setup(&m, &ops);
178 CHECK(ck_rng_probe(&r, &ops) == CK_RNG_OK);
179 CHECK(ck_rng_fill(NULL, buf, 1) == CK_RNG_EARG);
180 CHECK(ck_rng_fill(&r, NULL, 1) == CK_RNG_EARG);
181
182 return ck_t_verdict("test_entropy");
183}