Skip to content

[PM-40815] feat: Support AES-256-CBC-HMAC wrapping keys in SymmetricKeyEnvelope - #1616

Merged
quexten merged 4 commits into
mainfrom
km/pm-40815/symmetric-key-envelope-cbc-hmac-aead
Oct 29, 2026
Merged

quexten merged 4 commits into
mainfrom
km/pm-40815/symmetric-key-envelope-cbc-hmac-aead

Conversation

@luke-bitwarden

Copy link
Copy Markdown
Contributor

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-40815

📔 Objective

Allow SymmetricKeyEnvelope to seal and unseal with an AES-256-CBC-HMAC wrapping key, using the
AES-256-CBC-HMAC-SHA256-AEAD COSE construction from #1376. Many keys that act as
key-encryption-keys today, such as V1 user keys and cipher keys, are AES-256-CBC-HMAC keys. This
lets them adopt safe envelopes before they are rotated, as planned in the SAFE adoption RFC.

Changes:

  • KeyEncryptionKey::is_key_algorithm_valid, which returned a bool, is replaced by
    KeyEncryptionKey::get_cose_key_view. It returns the key as a CoseKeyView if it is an
    XAES-256-GCM or AES-256-CBC-HMAC key, and a KeyEncryptionKeyError otherwise. seal and
    unseal both get the wrapping key through it, so neither can use a key without the check.
    Previously unseal didn't call the KEK check and relied on its own XAES-only match.
  • seal and unseal take the algorithm, key bytes and key ID from the CoseKeyView instead of
    hard-coding XAES-256-GCM.
  • KeyEncryptionKey::make still generates only XAES-256-GCM keys. The key hierarchy README now
    notes that existing AES-256-CBC-HMAC keys are also accepted as KEKs.

Behavior change

seal with a missing wrapping key now returns SymmetricKeyEnvelopeError::KeyMissing instead of
WrongKeyType, matching unseal. No code in this repository matches on these variants.

Tests

  • New AES-256-CBC-HMAC tests: round trip (including the alg header), derived key IDs, wrong key,
    wrong namespace, and an envelope sealed with one wrapping key algorithm failing to unseal with
    the other, in both directions.
  • test_missing_wrapping_key covers the KeyMissing behavior.
  • New test vector: an AES-256-CBC-HMAC key sealed with an AES-256-CBC-HMAC wrapping key, the V1
    user key and cipher key case. The existing XAES-256-GCM test vector is unchanged and still
    decrypts.

🚨 Breaking Changes

None. get_cose_key_view and KeyEncryptionKeyError are crate-private, as was the removed
is_key_algorithm_valid. The public API of SymmetricKeyEnvelope is unchanged.

…eyEnvelope

Replace KeyEncryptionKey::is_key_algorithm_valid with get_cose_key_view, which
returns the wrapping key as a CoseKeyView when it is an XAES-256-GCM or
AES-256-CBC-HMAC key. seal and unseal both get the wrapping key through it and
take the algorithm, key bytes and key ID from the view instead of hard-coding
XAES-256-GCM.

Add AES-256-CBC-HMAC tests and a test vector.
@luke-bitwarden
luke-bitwarden requested a review from a team as a code owner October 14, 2026 14:31
@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

🤖 Bitwarden Claude Code Review

Overall Assessment: APPROVE

This PR lets SymmetricKeyEnvelope seal and unseal with AES-256-CBC-HMAC wrapping keys as well as XAES-256-GCM ones. It replaces KeyEncryptionKey::is_key_algorithm_valid with get_cose_key_view, which returns a CoseKeyView. seal and unseal now both go through that one KEK check, and unseal still uses CoseAlgorithmPolicy::Exactly to bind the envelope's protected alg to the wrapping key's type. Swapping the algorithm both ways is tested, and the existing XAES-256-GCM test vector still decrypts, so envelopes that already exist stay readable. I found no security, correctness, or compatibility issues in the changed code.

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

🔍 SDK Breaking Change Detection

SDK Version: km/pm-40815/symmetric-key-envelope-cbc-hmac-aead (a474df1)

⚠️ 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 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.20690% with 28 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.62%. Comparing base (5c0b59a) to head (6ca8d5d).

Files with missing lines Patch % Lines
...itwarden-crypto/src/safe/symmetric_key_envelope.rs 83.62% 28 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1616      +/-   ##
==========================================
+ Coverage   87.60%   87.62%   +0.01%     
==========================================
  Files         682      682              
  Lines      105107   105194      +87     
==========================================
+ Hits        92080    92172      +92     
+ Misses      13027    13022       -5     

☔ 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.

@luke-bitwarden

Copy link
Copy Markdown
Contributor Author

The Android breaking-change failure is already on main. It comes from the organization state change in #1588 (SyncResponseJsonExtensions.kt and SdkRepositoryFactoryImpl.kt in bitwarden/android), not from this PR, which doesn't change any public or UniFFI types. #1588 and #1610 show the same failure.

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

I have few concerns about docs and tests and it's placement

@@ -324,12 +315,21 @@ impl ContentNamespace for SymmetricKeyEnvelopeNamespace {}
#[cfg(test)]
mod tests {

@mzieniukbw mzieniukbw Oct 15, 2026 •

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.

❓ I wonder whether we should rename existing tests that asserts XAes256Gcm to say they test this key type ?
Alternatively (my preference), we would have one test function, for every algorithm, since they are identical compared to existing ones, like so:

fn test_x() {
    for sealing_key in [
        SymmetricKeyAlgorithm::Aes256CbcHmac,
        SymmetricKeyAlgorithm::XAes256Gcm,
    ] {
       ...
    }
}

Thoughts from @bitwarden/team-key-management-dev ?

@mzieniukbw mzieniukbw Oct 15, 2026 •

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.

Update: This is discouraged, because of concerns over slow builds due to costly macro generation.
Other idea: Bring rstest dependency:

#[rstest]
#[case::aes256_cbc_hmac(SymmetricKeyAlgorithm::Aes256CbcHmac)]
#[case::xaes256_gcm(SymmetricKeyAlgorithm::XAes256Gcm)]
fn test_wrong_key(#[case] sealing_key: SymmetricKeyAlgorithm) {

(also supports passing the test vectors)

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.

I think combining tests into such a loop makes sense, and we've done so in the past.

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.

Done, thanks. The round-trip, key-ID, wrong-key and test-vector tests now loop over both key types with plain for loops. The round trip also loops over the key being sealed (a COSE key and a legacy 64-byte key), since the legacy format had no envelope test. Tests that don't depend on the key type are unchanged. This needed Clone, Copy on SymmetricKeyAlgorithm.

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.

legacy 64-byte key

It's legacy encoding (format). The symmetric key itself is not legacy.

- Loop the round-trip, key ID, wrong key and test vector tests over both
  wrapping key types instead of keeping separate AES-256-CBC-HMAC copies.
  The round trip also covers a COSE key and a legacy 64-byte key as the
  key being sealed.
- Remove the redundant AES-256-CBC-HMAC wrong-namespace and cross-algorithm
  tests.
- Split the missing wrapping key test into separate seal and unseal tests.
- Rename the XAES-256-GCM test vector constants, the key-to-seal variable
  and the key ID test.
- Move the AES-256-CBC-HMAC KEK rationale into the KeyEncryptionKey docs.
- Derive Clone and Copy for SymmetricKeyAlgorithm.
@luke-bitwarden

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews. Pushed ac713f91: the tests now loop over both key types instead of having separate CBC copies, two redundant tests are removed, the vector constants and the key-ID test are renamed, and the KEK rationale moved into the KeyEncryptionKey docs. Ready for another look.

@@ -324,12 +315,21 @@ impl ContentNamespace for SymmetricKeyEnvelopeNamespace {}
#[cfg(test)]
mod tests {

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.

legacy 64-byte key

It's legacy encoding (format). The symmetric key itself is not legacy.

jlf0dev and others added 2 commits October 28, 2026 13:18
…overage

- Describe AES-256-CBC-HMAC keys as using the legacy (non-COSE) encoding
  rather than as legacy keys.
- Seal every symmetric key algorithm in the round-trip test, including
  AES-256-GCM and XAES-256-GCM.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quexten
quexten merged commit f2f0534 into main Oct 29, 2026
81 checks passed
@quexten
quexten deleted the km/pm-40815/symmetric-key-envelope-cbc-hmac-aead branch October 29, 2026 07:06
@quexten

quexten commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

@jlf0dev I went ahead and merged this so the blocked PR can be rebased.

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.

4 participants