Repository navigation
Draw random bytes through noise-c's stack generator - #247
Merged
Merged
Conversation
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.
Contributor
There was a problem hiding this comment.
🟡 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
noise-c exits when it cannot read the OS source; only the ESP path cannot fail.
7 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
The last abort on an allocation after
start()(Sendspin/backlog#92).platform_random_bytes()created a heapNoiseRandStateper call withnoise_randstate_new()and calledabort()when thatcallocfailed. It is reached afterstart()by the PSK minting inRecordStore, the pairing-code nonce and the CPace scalar, so a small out-of-memory moment during pairing ended the process.Change
platform_random_bytes()calls noise-c's publicnoise_randstate_generate_simple(), which runs the same ChaCha20 generator with its state on the stack: the same starting key, one reseed fromnoise_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 astd::array, so that asserts. noise-c stays the generator; nothing in the entropy path changes.randstate.cis byte-identical in esphome/noise-c 0.1.13 through 0.1.31, so the component's>=0.1.13floor already has the function.std::vectoroverload, which allocated withoperator new.internals.md"Memory" names the aborts that remain, both beforestart()or at static initialization (hashing a PSK while the record store loads, derivingSENTINEL_PSK); the two comments that cited the random-state abort as precedent no longer do.callocchain is gone; the generator state is 96 bytes innoise_randstate_generate_simple()'s frame.Tests