code wiki / _hdl_build / nx_rsa_keygen.nx
nx_rsa_keygen.nx
buildroot/runtime/_hdl_build/nx_rsa_keygen.nx
about
nx_rsa_keygen.nx -- F103e RUNG 7: RSA key generation.
The credential F103e has been walking toward. Real EDK2 with MS keys refuses our unsigned .efi
(Access Denied) while the SAME secboot firmware in SETUP mode runs it, so the blocker is the
enrolled key set (debt 1786237435). Enrolling OUR key requires HAVING one -- and it must be
GENERATED, never recited: an RSA modulus quoted from memory is a fabricated constant.
Dependencies, both built earlier in this same arc because neither existed:
nx_u2048_millerrabin -- primality (needs a^d mod n with d ~1024 bits)
nx_rsa2048_mod_exp_big -- that modexp (the incumbent takes the exponent as an i64)
nx_u2048_smallops -- d = (k*phi + 1)/e without a general extended GCD
⚠THE WIDTH TRAP, found by arithmetic and not by testing: for 1024-bit primes phi is ~2048 bits, so
phi*k with k < e = 65537 is ~2065 bits and OVERFLOWS the u2048 width. The d-recovery therefore
runs in the WIDE (128-limb) form. ★★★★★**AN INTERMEDIATE THAT OVERFLOWS ONLY AT PRODUCTION SIZE
IS INVISIBLE TO EVERY SMALL-PARAMETER TEST** -- so the wide helpers are cross-checked against the
shipped 64-limb smallops on 64-limb inputs (T2), making them the same ruler at two widths.
★THE CORRECTNESS PROOF NEEDS NO EXTERNAL ORACLE: a keypair is valid iff (m^e)^d == m mod n for
messages we choose. That is checked for several m, INCLUDING m=1 and m=n-1 which any broken key
still satisfies -- so a non-trivial m is required for the tooth to mean anything.
Usage: nx_rsa_keygen selftest (fast, small parameters -- proves the algorithm)
nx_rsa_keygen gen <bits> (real key from the CSPRNG, printed as hex)
⚠the selftest uses a DETERMINISTIC generator so the teeth are reproducible. That path prints
TEST-KEY-DO-NOT-USE and is unreachable from `gen`. ★A DETERMINISTIC KEY IS A TEST FIXTURE AND A
SECURITY DEFECT WEARING THE SAME BYTES -- the two paths must never share an exit.
Exit: 0 GREEN | 1 RED. Log -> knowledge/status/nishi_os.log, verdict= LAST.
license_tier: ORIGINAL
dependencies 9 imports · 4 importers
imports: nx_syscalls.nxnx_u2048.nxnx_u2048_mul.nxnx_rsa2048_mod.nxnx_rsa2048_mod_exp.nxnx_rsa2048_mod_exp_big.nxnx_u2048_millerrabin.nxnx_u2048_smallops.nxnx_csprng.nx
imported by: nx_authenticode_sign.nxnx_efivars_enrol.nxnx_rsa_pkcs1_sign.nxnx_x509_emit.nx
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 39 | const KG_MAGIC_88172645463325252: i64 = 88172645463325252 |
| 40 | const KG_MAGIC_20260808: i64 = 20260808 |
| 41 | const KG_MAGIC_123456789012345: i64 = 123456789012345 |
| 42 | const KG_MAGIC_65537: i64 = 65537 |
| 43 | const KG_MAGIC_1024: i64 = 1024 |
| 45 | const KG_E: i64 = 65537 |
| 46 | const KG_WIDE: i64 = 128 |
| 47 | const KG_MAXTRY: i64 = 20000 |
functions
| 49 | func kg_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } |
| 50 | func kg_fp(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } |
| 51 | func kg_fn(fd: i64, v: i64) -> i64 |
| 61 | func kg_hex(x: *i64) -> i64 |
| 74 | func kg_eq_str(a: *u8, b: *u8) -> i64 called by 1: main |
| 83 | func kg_mul_small_n(out: *i64, a: *i64, k: i64, nl: i64) -> i64 called by 1: kg_keygen |
| 94 | func kg_div_small_n(out: *i64, a: *i64, m: i64, nl: i64) -> i64 |
| 106 | func kg_mod_small_n(a: *i64, m: i64, nl: i64) -> i64 |
| 117 | func kg_add_small_n(out: *i64, a: *i64, k: i64, nl: i64) -> i64 called by 1: kg_keygen |
| 128 | func kg_wide_zero(x: *i64) -> i64 { var i: i64 = 0; while i < KG_WIDE { x[i] = 0; i = i + 1 } return 0 } |
| 131 | func kg_rng_next(st: *i64) -> i64 called by 1: kg_fill |
| 140 | func kg_fill(dst: *u8, n: i64, st: *i64, deterministic: i64) -> i64 |
| 148 | func kg_trial_reject(c: *i64) -> i64 |
| 163 | func kg_gen_prime(out: *i64, bits: i64, st: *i64, deterministic: i64, bases: *i64, nb: i64) -> i64 |
| 183 | func kg_keygen(bits: i64, p: *i64, q: *i64, n: *i64, d: *i64, st: *i64, deterministic: i64) -> i64 |
| 227 | func kg_roundtrip_ok(n: *i64, d: *i64, m: *i64) -> i64 |
| 236 | func kg_selftest() -> i64 |
| 339 | func main(argc: i64, argv: *i64) -> i64 |