Skip to content

[PM-44474] perf: Build heavy crypto crates for speed - #1606

Merged
quexten merged 2 commits into
mainfrom
perf/heavy-crypto-build-opt
Oct 13, 2026
Merged

quexten merged 2 commits into
mainfrom
perf/heavy-crypto-build-opt

Conversation

@quexten

@quexten quexten commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

🎟️ 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:

  • Vault decryption: aes, cbc, data-encoding, serde-wasm-bindgen
  • KDFs: sha2, hmac (PBKDF2), argon2, blake2 (Argon2id)
  • ML-DSA: ml-dsa, module-lattice, shake, keccak

RSA (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/hmac built for speed, the memory leak test found the Argon2 case's master password hash in memory after drop on x86_64: derive_master_key_hash left 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's clear_stack helper (moved to util).

📊 Benchmark

Node 25.9, Apple M5 Pro.

Benchmark Base (ms) This PR (ms) Change
V1 vault, encrypt_list 829 764 −8%
V1 vault, list 649 607 −7%
V1 vault, full 707 658 −7%
V2 vault, encrypt_list 845 809 −4%
V2 vault, list 757 735 −3%
V2 vault, full 808 775 −4%
Collections, encrypt_list 22 20 −10%
Collections, decrypt 23 21 −11%
Unlock, PBKDF2-SHA256 600k 191 159 −17%
Unlock, PBKDF2-SHA256 2M 641 535 −17%
Unlock, Argon2id 3 / 64 MiB / 4 161 148 −8%
Unlock, Argon2id 3 / 1 GiB / 4 2,773 2,582 −7%

Stripped wasm: +32 KB (+0.4%).

cd crates/bitwarden-wasm-internal/integration-tests
./perf/build.sh && npm run perf

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).
@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

🔍 SDK Breaking Change Detection

SDK Version: perf/heavy-crypto-build-opt (70afe77)

⚠️ If breaking changes are detected, a corresponding pull request addressing them must be ready for merge in the affected client repository.

Client Status Details
android ✅ No breaking changes detected Compilation passed with new SDK version - View Details
typescript ✅ No breaking changes detected Compilation passed with new SDK version - View Details

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.
Results update as workflows complete.

@codecov

codecov Bot commented Oct 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.49%. Comparing base (7d5b101) to head (70afe77).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@quexten
quexten requested a review from mzieniukbw October 12, 2026 09:08
@quexten
quexten marked this pull request as ready for review October 12, 2026 09:08
@quexten
quexten requested a review from a team as a code owner October 12, 2026 09:08
@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

🤖 Bitwarden Claude Code Review

Overall Assessment: APPROVE

This PR sets opt-level = 3 in the release profile for 12 compute-heavy crypto crates: AES-CBC, base64, the serde-wasm-bindgen reader, the PBKDF2 and Argon2id dependencies, and the ML-DSA dependencies. Every package spec matches a crate in Cargo.lock, so no profile override will be ignored with an unmatched-spec warning. It also moves clear_stack into util, raises its wipe size from 4 KiB to 16 KiB, and calls it after PBKDF2 in derive_master_key_hash, which now holds the hash in a Zeroizing wrapper. The function returns the same value as before. No manifest dependencies were added or changed, and Cargo.lock is unchanged.

#[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(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ideally eventually we we would upstream changes that properly implement zeroize for both argon2 and pbkdf2. This is a hack.

@mzieniukbw mzieniukbw 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.

What's the compilation time penalty ?

@quexten

quexten commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

@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
quexten merged commit eb9e208 into main Oct 13, 2026
83 checks passed
@quexten
quexten deleted the perf/heavy-crypto-build-opt branch October 13, 2026 13:52
@mzieniukbw

Copy link
Copy Markdown
Contributor

@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 ?

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.

2 participants