Skip to content

Draw random bytes through noise-c's stack generator - #247

Merged
kahrendt merged 2 commits into
mainfrom
rng-stack-state
Nov 4, 2026
Merged

kahrendt merged 2 commits into
mainfrom
rng-stack-state

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Summary

The last abort on an allocation after start() (Sendspin/backlog#92). platform_random_bytes() created a heap NoiseRandState per call with noise_randstate_new() and called abort() when that calloc failed. It is reached after start() by the PSK minting in RecordStore, the pairing-code nonce and the CPace scalar, so a small out-of-memory moment during pairing ended the process.

Change

  • Stack generator: platform_random_bytes() calls noise-c's public noise_randstate_generate_simple(), which runs the same ChaCha20 generator with its state on the stack: the same starting key, one reseed from noise_rand_bytes() (esp_fill_random() on ESP, the OS source on host), the same generate-and-rekey, and the state wiped after. It fails only for a null buffer, which every caller rules out with a std::array, so that asserts. noise-c stays the generator; nothing in the entropy path changes.
  • ESP noise-c: randstate.c is byte-identical in esphome/noise-c 0.1.13 through 0.1.31, so the component's >=0.1.13 floor already has the function.
  • Removed: the unused std::vector overload, which allocated with operator new.
  • Docs: internals.md "Memory" names the aborts that remain, both before start() or at static initialization (hashing a PSK while the record store loads, deriving SENTINEL_PSK); the two comments that cited the random-state abort as precedent no longer do.
  • Stack: the protocol task's bound and reachable figures are unchanged. A draw's own subtree drops from about 2.5 KB to 0.8 KB, because the calloc chain is gone; the generator state is 96 bytes in noise_randstate_generate_simple()'s frame.

Tests

  • No new test. A generator that writes nothing fails about 40 existing tests across the record store, the handshake, pairing and the lifecycle suites.
  • ESP build: IDF 5.5.4, default config.

platform_random_bytes() created a heap NoiseRandState per call and
aborted when its calloc failed, the library's last abort on an
allocation after start() (PSK minting, the pairing-code nonce, the
CPace scalar). It now calls noise_randstate_generate_simple(), which
runs the same ChaCha20 generator with its state on the stack, seeded
from the same source and wiped after; it fails only for a null buffer,
which asserts. The unused std::vector overload is removed, and the
protocol task's stack figures are unchanged.
@kahrendt
kahrendt marked this pull request as ready for review November 4, 2026 20:35
@kahrendt
kahrendt requested a max review from Copilot November 4, 2026 20:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The success guarantee is too broad: removing generator-state allocation failure does not prevent entropy-source failures from terminating the host process.

1 open issue
What changed in this PR

Removes the random-state heap allocation from the library’s cryptographic random-byte generation, eliminating that allocation-failure abort path during pairing after start().

Changes:

  • Uses noise-c’s stack-backed generator and removes the unused allocating vector overload.
  • Updates memory documentation and removes obsolete random-state-abort references from comments.
File Description
src/​platform/​crypto.h Switches random-byte generation to the stack-backed API and removes the vector overload.
src/​crypto/​keys.cpp Comment-only: removes the obsolete random-state-abort reference.
src/​crypto/​constants.cpp Comment-only: removes the same obsolete reference from the sentinel failure rationale.
docs/​internals.md Updates the memory model’s description of remaining allocation-failure aborts.

Review effort: Max

Comment thread src/platform/crypto.h Resolved
noise-c exits when it cannot read the OS source; only the ESP path
cannot fail.
@kahrendt
kahrendt merged commit 1773d9c into main Nov 4, 2026
16 checks passed
@kahrendt
kahrendt deleted the rng-stack-state branch November 4, 2026 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants