G.2 (v2.0.0): unified rng_get_bytes() entropy entry point; virtio-rng sole backend

The QEMU-verifiable slice of the real-hardware RNG driver (per FABRIC-3.md
§G.2). New include/starkernel/rng.h + src/starkernel/rng/rng.c provide the
single entropy entry point: rng_init() probes the backend set (v2.0.0:
virtio-rng only) and, on no backend, prints a loud boot-time warning while
rng_get_bytes() returns RNG_ERR_NO_BACKEND - never silently degrading to a
deterministic seed. The backend-selection switch in rng.c is the exact seam
v2.5.0's per-arch drivers (amd64 RDRAND, riscv64 Zkr, aarch64 peripheral) plug
into without touching the call path.

Consumers route through the unified layer instead of virtio-rng directly:
capsule_mint.c (identity seed + drive_uuid) and kernel_main.c phase 8
(rng_init()). virtio_rng.c stays as the sole backend. Built clean on
amd64/aarch64/riscv64. QEMU amd64 boot: POST 1012/0/0 + ok>, "rng: backend =
virtio-rng" + "entropy: ready", Zuse identity confirmed from thumbdrive -
mint/cert behavior unchanged.

FABRIC-3.md §G.2 v2.0.0 slice marked BUILT+VERIFIED.
This commit is contained in:
Robert Allan James
2026-08-29 09:57:17 -04:00
parent aee1ecaa8f
commit 09857b7228
8 changed files with 157 additions and 17 deletions
+60
View File
@@ -0,0 +1,60 @@
/*
StarKernel — Unified entropy layer (rng_get_bytes)
Single entropy entry point for the kernel; see include/starkernel/rng.h for
the contract and the probe-and-refuse-loudly discipline this implements.
v2.0.0: virtio-rng is the sole active backend (QEMU-only, uniform across all
three arches). The backend-selection switch in rng_get_bytes() is the exact
seam where the v2.5.0 real per-arch drivers (amd64 RDRAND, riscv64 Zkr,
aarch64 peripheral RNG) plug in without touching the call path.
*/
#ifndef __STARKERNEL__
#error "rng.c is kernel-only"
#endif
#include "starkernel/rng.h"
#include "starkernel/virtio_rng.h"
#include "starkernel/console.h"
/* Number of entropy backends known to this build. Each maps to one case in
* rng_init() and rng_get_bytes(). v2.0.0 has exactly one: virtio-rng. */
enum {
RNG_BACKEND_NONE = 0,
RNG_BACKEND_VIRTIO, /* virtio-rng (QEMU, all three arches) */
};
static int g_rng_backend = RNG_BACKEND_NONE;
int rng_init(void) {
/* Probe backends in priority order; first success wins. virtio-rng is
* the sole backend at v2.0.0; v2.5.0 adds real per-arch drivers here. */
if (virtio_rng_init() == 0) {
g_rng_backend = RNG_BACKEND_VIRTIO;
console_println("rng: backend = virtio-rng");
return 0;
}
/* Refuse loudly: never fall through to a deterministic seed. */
g_rng_backend = RNG_BACKEND_NONE;
console_println(
"rng: WARNING — no entropy backend available; rng_get_bytes() "
"will refuse (no deterministic seed fallback)");
return -1;
}
int rng_ready(void) {
return g_rng_backend != RNG_BACKEND_NONE;
}
int rng_get_bytes(uint8_t *buf, size_t n) {
switch (g_rng_backend) {
case RNG_BACKEND_VIRTIO:
return virtio_rng_get_bytes(buf, n);
default:
/* v2.5.0 real per-arch cases land here. No backend: refuse loudly,
* never return a deterministic throwaway. */
return RNG_ERR_NO_BACKEND;
}
}