Skip to content

fix(random): remixture TRNG on rand_below retry; Linux getrandom - #65

Merged
switck merged 1 commit into
switck:masterfrom
SashaMIT:fix/trng-retry-and-linux-getrandom
Aug 6, 2026
Merged

switck merged 1 commit into
switck:masterfrom
SashaMIT:fix/trng-retry-and-linux-getrandom

Conversation

@SashaMIT

@SashaMIT SashaMIT commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Two related entropy hygiene fixes in ngu/random.c:

  1. _rand_below / uniform: After the first sample, rejection retries previously remixed only my_yasmarang() and never called CHIP_TRNG_32() again. Retries now remixture TRNG each iteration.
  2. Linux CHIP_TRNG_32: Was mapped to glibc random() with no srandom/getrandom. Now uses getrandom(2) (fails hard on error, same pattern as the duplicate-TRNG check in my_random_bytes).

Impact

  • Affects ngu.random.uniform consumers (Coldcard firmware uses this for Key Teleport / PIN scramble / web2fa digits, etc.).
  • Linux/simulator path: critical if anyone generates real keys on that build; STM32/Apple paths unchanged for (2).

Test plan

  • Build makefile.unix / MicroPython unix port
  • Spot-check uniform still returns < mx
  • Confirm Linux no longer links behaviour to unseeded random()

Elacity CodeRED Amber review (portal #51, #53). Coordinating with Coinkite on firmware-side siblings separately.

@SashaMIT

SashaMIT commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up commit: reseed(bytes) now absorbs into full yasmarang state (pad/n/d/dat). Integer reseed(int) kept for compatibility.

Pairs with Coldcard firmware change to pass sha256d(SE1||SE2) bytes instead of truncating to 32 bits (portal #52).

@SashaMIT

SashaMIT commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

Friendly bump — related entropy hardening across libngu#65 and Coldcard firmware#697 (TRNG remix on reject, Linux getrandom, full SE hash reseed). Happy to split or retarget if you want a different shape.

@switck

switck commented Aug 6, 2026

Copy link
Copy Markdown
Owner

rebase, will merge

CHIP_TRNG_32 on Linux mapped to glibc random(), which is not a TRNG and
is typically unseeded in simulator / Linux libngu builds. Every consumer
(drbg_seed_from_chip -> my_random_bytes, including the rejection retry
in _rand_below) therefore derives from a deterministic sequence on
Linux. Map CHIP_TRNG_32 to getrandom(2) instead, failing hard on error.

Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: SashaMIT <sash@ela.city>
@SashaMIT
SashaMIT force-pushed the fix/trng-retry-and-linux-getrandom branch from 59f3dd3 to bb6d30a Compare August 6, 2026 14:23
@SashaMIT

SashaMIT commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current master (e9d5e80). Note: your my_random_bytes() refactor of the _rand_below retry already covers the re-mixture half of this PR, so I've rescoped to the still-live half: CHIP_TRNG_32 on Linux mapped to unseeded glibc random(), which makes drbg_seed_from_chip deterministic on Linux/sim builds. Now maps to getrandom(2), hard-failing on error.

@switck
switck merged commit edda981 into switck:master Aug 6, 2026
SashaMIT added a commit to SashaMIT/oss-contributions that referenced this pull request Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants