Skip to content

refactor: generate the Argon2 KDF salt with getrandom instead of rand - #183

Merged
WingZer0o merged 1 commit into
deps/remove-randfrom
deps/180-argon2-salt-getrandom
Nov 27, 2026
Merged

WingZer0o merged 1 commit into
deps/remove-randfrom
deps/180-argon2-salt-getrandom

Conversation

@WingZer0o

Copy link
Copy Markdown
Member

Summary

CASArgon::generate_salt() drew its salt with rand 0.8 (OsRng.gen::<[u8; KDF_SALT_LENGTH]>()), the last rand use in src/password_hashers. It now uses getrandom 0.4 (already a dependency), and the rand import is gone.

  • No public API change: still pub fn generate_salt() -> Vec<u8>, still exactly KDF_SALT_LENGTH (16) bytes.
  • Still panics if the OS RNG fails (as rand's OsRng did), now with expect("getrandom failure") like get_ed25519_key_pair(), and documented under a new # Panics section.
  • The salt isn't secret, so it isn't zeroized.
  • No unsafe. Cargo.toml / Cargo.lock untouched (removing rand is Remove the rand dependency #182).

CodeQL

rust/hard-coded-cryptographic-value flags a zero-filled buffer later used as crypto input. The salt is never zero-initialised: it is a [MaybeUninit<u8>; KDF_SALT_LENGTH] passed to getrandom::fill_uninit, and the initialised &mut [u8] it returns is copied out with .to_vec().

Tests

  • New argon2_generate_salt_is_random_and_kdf_salt_length: two generate_salt() calls are both KDF_SALT_LENGTH long and differ.
  • argon2_derive_with_salt_known_answer and all other existing tests/vectors are unedited and pass.
  • Windows: cargo build --release --locked OK; cargo test --release --locked: 235 passed, 0 failed (password_hashers: 40, including Wycheproof suites with network). Clippy isn't installed for the pinned toolchain locally, so it wasn't run.

Part of #180

🤖 Generated with Claude Code

CASArgon::generate_salt() drew its salt with rand 0.8's OsRng, the last
rand use in the password hashers. Fill a [MaybeUninit<u8>; KDF_SALT_LENGTH]
with getrandom::fill_uninit instead, which avoids the zeroed-buffer pattern
CodeQL flags as rust/hard-coded-cryptographic-value. The signature stays
infallible and still panics if the OS RNG fails; document that under
# Panics. Add a test that two salts are KDF_SALT_LENGTH long and differ.

Part of #180.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@WingZer0o
WingZer0o merged commit 72979cf into deps/remove-rand Nov 27, 2026
5 checks passed
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.

1 participant