code wiki / _hdl_build / nx_rsa_keygen.nx

nx_rsa_keygen.nx

buildroot/runtime/_hdl_build/nx_rsa_keygen.nx

15879 B362 linesdepth 8pulls 12 transitivereach 4 importersview sourcekind tooltopic rsa
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_u2048.nx nx_u2048_mul.nx nx_rsa2048_mod.nx nx_rsa2048_mod_exp.nx nx_rsa2048_mod_exp_big.nx nx_u2048_millerrabin.nx nx_u2048_smallops.nx nx_csprng.nx nx_rsa_keygen.nx nx_authenticode_sign.nx nx_efivars_enrol.nx nx_rsa_pkcs1_sign.nx nx_x509_emit.nx

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

main kg_eq_str kg_selftest sys_mmap nxa_die sys_write sys_exit nxa_lock_take nxa_lock_addr sys_write ↻ nxa_lock_give nxa_lock_addr ↻ nxa_report_overrun sys_write ↻ nxa_dump_printable sys_write ↻ nxa_dump_sizes sys_write ↻ kg_p sys_write ↻ u2048_alloc sys_mmap ↻ kg_keygen sys_mmap ↻ u2048_alloc ↻ u2048_one u2048_zero u2048_wide_alloc sys_mmap ↻ kg_gen_prime sys_mmap ↻ kg_fill nx_csprng_fill sys_mmap ↻ nx_csprng_urandom_path sys_openat_rd sys_read sys_close kg_rng_next u2048_load_be

structs

none

consts

39const KG_MAGIC_88172645463325252: i64 = 88172645463325252
40const KG_MAGIC_20260808: i64 = 20260808
41const KG_MAGIC_123456789012345: i64 = 123456789012345
42const KG_MAGIC_65537: i64 = 65537
43const KG_MAGIC_1024: i64 = 1024
45const KG_E: i64 = 65537
46const KG_WIDE: i64 = 128
47const KG_MAXTRY: i64 = 20000

functions

49func 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 }
called by 2: kg_selftestmain calls 1: sys_write
50func 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 }
called by 1: kg_selftest calls 1: sys_write
51func kg_fn(fd: i64, v: i64) -> i64
called by 2: kg_selftestmain calls 2: sys_mmapsys_write
61func kg_hex(x: *i64) -> i64
74func kg_eq_str(a: *u8, b: *u8) -> i64
called by 1: main
83func kg_mul_small_n(out: *i64, a: *i64, k: i64, nl: i64) -> i64
called by 1: kg_keygen
94func kg_div_small_n(out: *i64, a: *i64, m: i64, nl: i64) -> i64
106func kg_mod_small_n(a: *i64, m: i64, nl: i64) -> i64
117func kg_add_small_n(out: *i64, a: *i64, k: i64, nl: i64) -> i64
called by 1: kg_keygen
128func kg_wide_zero(x: *i64) -> i64 { var i: i64 = 0; while i < KG_WIDE { x[i] = 0; i = i + 1 } return 0 }
131func kg_rng_next(st: *i64) -> i64
called by 1: kg_fill
140func kg_fill(dst: *u8, n: i64, st: *i64, deterministic: i64) -> i64
148func kg_trial_reject(c: *i64) -> i64
163func kg_gen_prime(out: *i64, bits: i64, st: *i64, deterministic: i64, bases: *i64, nb: i64) -> i64
183func kg_keygen(bits: i64, p: *i64, q: *i64, n: *i64, d: *i64, st: *i64, deterministic: i64) -> i64
227func kg_roundtrip_ok(n: *i64, d: *i64, m: *i64) -> i64
236func kg_selftest() -> i64
339func main(argc: i64, argv: *i64) -> i64