Repository navigation
[PM-44474] perf: Build heavy crypto crates for speed - #1606
Conversation
The release profile optimizes every crate for size. Vault decryption (aes, cbc, data-encoding, serde-wasm-bindgen), the KDFs (sha2, hmac, argon2, blake2) and ML-DSA (ml-dsa, module-lattice, shake, keccak) spend their time in these crates. Building them at opt-level 3 makes unlock, decryption and signing faster for ~31 KB. The RSA crates are not included: at opt-level 3 they added ~70 KB without a measurable gain, as RSA's generic code is instantiated in the calling crates.
derive_master_key_hash left the PBKDF2 output in an unzeroized local and in the popped HMAC/SHA-256 stack frames. With sha2 and hmac built for speed, the memory leak test found the Argon2 case's hash still in memory after drop on x86_64: the frames are laid out differently and later calls no longer happened to overwrite them. Zeroize the local and clear the stack after the derivation, reusing the clear_stack helper Argon2 already used (now shared in util and sized for both).
🔍 SDK Breaking Change DetectionSDK Version:
Breaking change detection uses the build of the SDK from this branch, including any incompatibities pre-existing on or merged into this branch. Check the workflow logs to confirm. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1606 +/- ##
=======================================
Coverage 87.48% 87.49%
=======================================
Files 609 609
Lines 86982 86987 +5
=======================================
+ Hits 76100 76105 +5
Misses 10882 10882 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
🤖 Bitwarden Claude Code ReviewOverall Assessment: APPROVE This PR sets |
| #[cfg_attr(feature = "dangerous-crypto-debug", tracing::instrument)] | ||
| pub fn derive_master_key_hash(&self, password: &[u8], purpose: HashPurpose) -> B64 { | ||
| let hash = util::pbkdf2(self.inner_bytes().as_slice(), password, purpose as u32); | ||
| let hash = Zeroizing::new(util::pbkdf2( |
There was a problem hiding this comment.
Ideally eventually we we would upstream changes that properly implement zeroize for both argon2 and pbkdf2. This is a hack.
mzieniukbw
left a comment
There was a problem hiding this comment.
What's the compilation time penalty ?
|
@mzieniukbw not very much, at least, i did not notice it. The related SIMD PR is much worse, and should have a follow-up that fixes it (since it effecively doubles the comp time) |
|
@quexten i haven't asked, but we are confident this does not impact any of the functionality on archs (x86, arm) that we support today ? |
…Build heavy crypto crates for speed (bitwarden/sdk-internal#1606)
🎟️ Tracking
https://bitwarden.atlassian.net/browse/PM-44474
📔 Objective
Split from #1569.
Build heavy crypto crates for speed. The release profile builds every crate for size. This PR builds compute-heavy crates at
opt-level = 3:aes,cbc,data-encoding,serde-wasm-bindgensha2,hmac(PBKDF2),argon2,blake2(Argon2id)ml-dsa,module-lattice,shake,keccakRSA (
rsa,crypto-bigint,crypto-primes) is left out: at opt-level 3 it added ~70 KB with no measurable gain.Wipe the stack after deriving the master key hash. With
sha2/hmacbuilt for speed, the memory leak test found the Argon2 case's master password hash in memory after drop on x86_64:derive_master_key_hashleft the PBKDF2 output in an unzeroized local and in popped HMAC/SHA-256 stack frames, which size-optimized code happened to overwrite. The local is now zeroized and the stack cleared, reusing Argon2'sclear_stackhelper (moved toutil).📊 Benchmark
Node 25.9, Apple M5 Pro.
Stripped wasm: +32 KB (+0.4%).