ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
secure_trng.c
Go to the documentation of this file.
1
17
18#include <stdint.h>
19
20#include "ra8_check.h"
21#include "ra8_err.h"
23
24static const char* s_tag = "SECTRNG";
25
26/*
27 * Fail-closed stub-crypto gate (issue #180). The xorshift64* core below is a
28 * DETERMINISTIC PRNG masquerading as a TRNG -- predictable "random" bytes were
29 * the #1 severity finding in the security audit (predictable keys). It is only
30 * safe under an off-target build or an explicitly-declared insecure dev/eval image.
31 * A real production/HIL image (neither flag set) compiles the #else branch,
32 * where every entry point hard-errors so predictable entropy can never be
33 * drawn. scripts/checks/check_stub_crypto_guarded.py enforces that this guard
34 * stays wrapped around the insecure body.
35 */
36#if defined(RA8_INSECURE_STUB_CRYPTO) || defined(RA8_OFF_TARGET)
37
46typedef enum : uint64_t {
47 k_xorshift_seed = 0x9E3779B97F4A7C15ULL,
48 k_xorshift_multiplier = 0x2545F4914F6CDD1DULL,
49} ra8_secure_trng_consts64_t;
50
51typedef enum : uint8_t {
52 k_xorshift_shift_a = 12U,
53 k_xorshift_shift_b = 25U,
54 k_xorshift_shift_c = 27U,
55 k_byte_bits = 8U,
56 k_bytes_per_u64 = 8U,
57} ra8_secure_trng_consts8_t;
58
59typedef enum : uint32_t {
60 k_byte_mask = 0xFFU,
61} ra8_secure_trng_consts32_t;
62
71static uint64_t s_state = k_xorshift_seed;
72
92static uint64_t internal_xorshift64(void)
93{
94 uint64_t x = s_state;
95 x ^= x >> k_xorshift_shift_a;
96 x ^= x << k_xorshift_shift_b;
97 x ^= x >> k_xorshift_shift_c;
98 s_state = x;
99 return x * k_xorshift_multiplier;
100}
101
121{
122 s_state = k_xorshift_seed;
123 return k_ra8_ok;
124}
125
146ra8_err_t priv_ra8_secure_trng_read(uint8_t* out, uint32_t len)
147{
148 RA8_CHECK_NULL_PTR(out, s_tag, "trng_read: out");
149 if ((len == 0U) || (len > (uint32_t)k_ra8_secure_trng_max_bytes)) {
151 }
152 uint32_t written = 0U;
153 /* Loop bound is the per-call cap (NASA Rule 2). */
154 while (written < len) {
155 const uint64_t word = internal_xorshift64();
156 /* Inner loop bound is constant 8. */
157 for (uint32_t b = 0U; (b < (uint32_t)k_bytes_per_u64) && (written < len); ++b) {
158 out[written] = (uint8_t)((word >> (b * (uint32_t)k_byte_bits)) & k_byte_mask);
159 ++written;
160 }
161 }
162 return k_ra8_ok;
163}
164
165#else /* production build: neither RA8_INSECURE_STUB_CRYPTO nor RA8_OFF_TARGET */
166
167/*
168 * Fail-closed production variant. Without a real RSIP TRNG backend the
169 * deterministic PRNG above must never run, so both entry points return a hard
170 * error (never k_ra8_ok). A production image that forgot to provide real
171 * entropy therefore cannot silently draw predictable "random" bytes.
172 */
173
178
179ra8_err_t priv_ra8_secure_trng_read(uint8_t* out, uint32_t len)
180{
181 RA8_CHECK_NULL_PTR(out, s_tag, "trng_read: out");
182 (void)len;
184}
185
186#endif /* RA8_INSECURE_STUB_CRYPTO || RA8_OFF_TARGET */
@ k_byte_bits
Bits per byte (SHPR field width).
Definition emu_exc.h:99
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ k_ra8_err_not_supported
Requested feature not compiled in, not wired, or not supported by this MCU variant.
Definition ra8_err.h:180
@ k_ra8_err_invalid_arg
Invalid function argument.
Definition ra8_err.h:152
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
@ k_byte_mask
Byte mask.
static uint32_t s_state
ra8_err_t priv_ra8_secure_trng_read(uint8_t *out, uint32_t len)
Fill [out, out+len) with TRNG-quality entropy.
ra8_err_t priv_ra8_secure_trng_reset(void)
Reset the TRNG seed (test-only; production reseeds from RSIP).
Secure-side TRNG read API (RSIP-backed entropy).
@ k_ra8_secure_trng_max_bytes
Max bytes per call.