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 / entropy.h

73 lines · 3079 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/* entropy.h -- kernel entropy: the Arm RNDR instruction or a refusal.
18 *
19 * Pure selection logic over two callbacks (is FEAT_RNG present, read one
20 * RNDR word), so host tests drive it with a mocked RNDR. The kernel glue
21 * (arch/rndr.c) supplies the real ID_AA64ISAR0_EL1 / RNDR accessors.
22 *
23 * There is no fallback source. Rules:
24 * - FEAT_RNG is checked first (ID_AA64ISAR0_EL1.RNDR >= 1); absent means
25 * every request is refused.
26 * - RNDR reports failure by setting NZCV.Z; each 64-bit word is retried at
27 * most CK_RNG_RETRIES times, then the source is refused.
28 * - Stuck-output test: a word equal to the previous word refuses the source
29 * (FIPS-style continuous test; false-positive odds 2^-64 per word).
30 * - A refusal is latched for the rest of the boot and the caller's buffer
31 * is zeroed, so no partial or stale bytes ever leave as "random".
32 * QEMU is not hardware: nothing built on this is physically qualified. */
33#ifndef AIENOS_CK_ENTROPY_H
34#define AIENOS_CK_ENTROPY_H
35
36#include <stddef.h>
37#include <stdint.h>
38
39#define CK_RNG_RETRIES 64u
40
41enum {
42 CK_RNG_OK = 0,
43 CK_RNG_UNPROBED = -1, /* ck_rng_probe not called yet */
44 CK_RNG_ABSENT = -2, /* ID_AA64ISAR0_EL1.RNDR == 0 */
45 CK_RNG_FAILED = -3, /* RNDR failed CK_RNG_RETRIES times in a row */
46 CK_RNG_STUCK = -4, /* two consecutive words were equal */
47 CK_RNG_EARG = -5, /* bad arguments (state unchanged) */
48};
49
50struct ck_rng_ops {
51 int (*present)(void *ctx); /* 1 if FEAT_RNG is implemented */
52 int (*read)(void *ctx, uint64_t *out); /* 1 = valid word; 0 = RNDR failed (Z set) */
53 void *ctx;
54};
55
56struct ck_rng {
57 const struct ck_rng_ops *ops;
58 int state; /* CK_RNG_OK or the latched refusal */
59 uint64_t last; /* previous word, for the stuck-output test */
60 uint64_t words; /* words delivered (probe included) */
61 uint64_t retries; /* failed RNDR reads that a retry recovered */
62};
63
64/* Check FEAT_RNG, then draw two words (retry + stuck test). Returns the new
65 * state: CK_RNG_OK or a refusal. */
66int ck_rng_probe(struct ck_rng *r, const struct ck_rng_ops *ops);
67/* Fill buf with RNDR output. 0 on success; otherwise the (latched) refusal
68 * and buf is zeroed. */
69int ck_rng_fill(struct ck_rng *r, void *buf, size_t len);
70/* Short reason word for a state: "rndr", "absent", "failed", "stuck", ... */
71const char *ck_rng_reason(int state);
72
73#endif