Repository navigation
refactor: generate AEAD keys and nonces with getrandom instead of rand - #184
Merged
Merged
Conversation
random_bytes (behind every AEAD's generate_key / generate_nonce and the AES-KW generate_kek) and the CASAeadSeal::seal nonce now come from getrandom::fill_uninit on an uninitialised MaybeUninit buffer instead of rand 0.8's OsRng. Drawing into uninitialised memory keeps CodeQL's rust/hard-coded-cryptographic-value rule quiet, and the stack buffer in random_bytes is wrapped in Zeroizing so the returned Vec is the only copy. Public API is unchanged: the generators stay infallible and panic if the OS RNG fails, as rand's OsRng did; this is now documented with # Panics sections. seal already returns CasResult, so an RNG failure there returns CasError::EncryptionFailed instead of panicking. Adds tests that consecutive generate_key / generate_nonce (and generate_kek) calls have the right lengths and differ. Part of #179. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
src/symmetric/aead_helpers.rsno longer usesrand. Both RNG draws now usegetrandom0.4, which the crate already depends on:random_bytes::<N>()backsgenerate_key/generate_nonceon AES-GCM, AES-GCM-SIV, ChaCha20-Poly1305 and Ascon-AEAD128, andgenerate_kekon AES-KW. It now callsgetrandom::fill_uniniton aZeroizing<[MaybeUninit<u8>; N]>and copies out the initialised slice. TheStandard: Distribution<[u8; N]>bound is gone.sealnonce now callsgetrandom::fill_uniniton anArray::<MaybeUninit<u8>, A::NonceSize>::uninit(), replacingOsRng.fill_byteson a defaultedNonce<A>.I checked every
random_bytescaller (aes.rs,aes_gcm_siv.rs,chacha20poly1305.rs,aes_kw.rs,sponges/ascon_aead.rs). None of them usesrandelsewhere. Outside this chain,randis still used only inargon2.rsandslh_dsa.rs(#180, #181).Cargo.toml/Cargo.lockare unchanged; removing the dependency is #182.CodeQL (
rust/hard-coded-cryptographic-value)Neither path creates a zero-filled buffer that later becomes a key or nonce. Both draw into uninitialised
MaybeUninitmemory, and the code only uses the&mut [u8]thatfill_uninitreturns. The oldNonce::<A>::default()insealwas a zeroed buffer, and that is now gone too. Nounsafeis added.Zeroization
The stack buffer in
random_bytesis wrapped inZeroizing, so once the function returns, the caller'sVecis the only copy of a key. Nonces are not secret, so thesealnonce buffer is not zeroized.Panic / error behaviour
generate_key/generate_nonce/generate_kekstay infallible and returnVec<u8>, so the public API is unchanged. If the OS RNG fails they panic withgetrandom failure: the OS random number generator is unavailable, the same behaviour asrand'sOsRng. Each trait declaration and impl now has a# Panicssection, asget_ed25519_key_pair()does.sealalready returnsCasResult, so an RNG failure there now returnsCasError::EncryptionFailedinstead of panicking. That is the closest existing variant, and no variant was added or renumbered. TheCASAeadSeal::sealdocs say so.The
OsRngmentions in the trait docs now refer to the operating system's random number generator.Tests
tests/aead_seal.rs: new testgenerate_key_and_nonce_have_right_length_and_differcovers all six AEADs (the test table gains akey_lencolumn).tests/aes_kw.rs: new testgenerate_kek_has_right_length_and_differs.Results, run locally on Windows:
cargo build --release --locked: OK.cargo test --release --locked: all 236 pass, including the Wycheproof tests that need the network.cargo doc --no-deps: no warnings.Part of #179
🤖 Generated with Claude Code