Skip to content

Commit af06b3b

Browse files
committed
[PM-40158] Use xaes-256-gcm as user key algorithm
for encryption v2 users
1 parent b02a6fb commit af06b3b

9 files changed

Lines changed: 30 additions & 20 deletions

File tree

crates/bitwarden-core/src/client/internal.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,7 @@ impl InternalClient {
280280
.unwrap_v2(user_key_id, &mut ctx)
281281
.map_err(|_| EncryptionSettingsError::InvalidUpgradeToken)?
282282
}
283-
(
284-
SymmetricCryptoKey::XChaCha20Poly1305Key(_) | SymmetricCryptoKey::XAes256GcmKey(_),
285-
Some(_),
286-
) => {
283+
(SymmetricCryptoKey::XAes256GcmKey(_), Some(_)) => {
287284
debug!("V2 user key already present, ignoring upgrade token");
288285
user_key_id
289286
}

crates/bitwarden-core/src/key_management/account_cryptographic_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//!
77
//! V1 users have only a private key protected by an AES256-CBC-HMAC user key.
88
//! V2 users have a private key, a signing key, a signed public key and a signed security state,
9-
//! all protected by a Cose serialized AEAD key, currently XChaCha20-Poly1305.
9+
//! all protected by a COSE-serialized XAES-256-GCM key.
1010
1111
use std::sync::RwLock;
1212

@@ -335,7 +335,7 @@ impl WrappedAccountCryptographicState {
335335
pub fn make(
336336
ctx: &mut KeyStoreContext<KeySlotIds>,
337337
) -> Result<(SymmetricKeySlotId, Self), AccountCryptographyInitializationError> {
338-
let user_key = ctx.make_symmetric_key(SymmetricKeyAlgorithm::XChaCha20Poly1305);
338+
let user_key = ctx.make_symmetric_key(SymmetricKeyAlgorithm::XAes256Gcm);
339339
let private_key = ctx.make_private_key(PublicKeyEncryptionAlgorithm::RsaOaepSha1);
340340
let signing_key = ctx.make_signing_key(SignatureAlgorithm::MlDsa44);
341341
let signed_public_key = ctx.make_signed_public_key(private_key, signing_key)?;

crates/bitwarden-core/src/key_management/crypto.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ pub(crate) fn make_v2_keys_for_v1_user(
926926
let private_key = ctx.dangerous_get_private_key(private_key_id)?.clone();
927927

928928
// New user key
929-
let user_key = SymmetricCryptoKey::make(SymmetricKeyAlgorithm::XChaCha20Poly1305);
929+
let user_key = SymmetricCryptoKey::make(SymmetricKeyAlgorithm::XAes256Gcm);
930930

931931
// New signing key
932932
let signing_key = SigningKey::make(SignatureAlgorithm::Ed25519);
@@ -1239,8 +1239,8 @@ pub(crate) fn make_user_password_registration(
12391239
master_password: String,
12401240
salt: String,
12411241
) -> Result<MakeUserMasterPasswordRegistrationResponse, MakeKeysError> {
1242-
// make_user_v2_crypto_state() - Creates user key (xchacha20-poly1305), RSA keypair, ed25519
1243-
// signature keypair, and signed security state
1242+
// make_user_v2_crypto_state() - Creates user key (XAES-256-GCM), RSA key pair, ML-DSA
1243+
// signing key pair, and signed security state
12441244
let mut ctx = client.internal.get_key_store().context_mut();
12451245
let (user_key_id, wrapped_state) = WrappedAccountCryptographicState::make(&mut ctx)
12461246
.map_err(MakeKeysError::AccountCryptographyInitialization)?;

crates/bitwarden-core/src/key_management/crypto/reinit_user_crypto.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,11 @@ pub(crate) async fn reinit_user_crypto(
117117
debug!("Active user key is already V2, skipping re-initialization.");
118118
return Ok(());
119119
}
120-
SymmetricKeyAlgorithm::Aes256Gcm => {
121-
error!("Unexpected AES-256-GCM user key during reinit_user_crypto");
120+
SymmetricKeyAlgorithm::XChaCha20Poly1305 | SymmetricKeyAlgorithm::Aes256Gcm => {
121+
error!(
122+
?current_algorithm,
123+
"Unexpected user key algorithm during reinit_user_crypto"
124+
);
122125
return Err(ReinitUserCryptoError::CryptoInitialization);
123126
}
124127
};

crates/bitwarden-core/src/key_management/crypto_client.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ impl CryptoClient {
321321

322322
match (algorithm, upgrade_token) {
323323
// Already V2, return current key
324-
(SymmetricKeyAlgorithm::XChaCha20Poly1305 | SymmetricKeyAlgorithm::XAes256Gcm, _) => {
324+
(SymmetricKeyAlgorithm::XAes256Gcm, _) => {
325325
#[allow(deprecated)]
326326
let current_key = ctx
327327
.dangerous_get_symmetric_key(SymmetricKeySlotId::User)
@@ -343,7 +343,9 @@ impl CryptoClient {
343343
(SymmetricKeyAlgorithm::Aes256CbcHmac, None) => {
344344
Err(CryptoClientError::UpgradeTokenRequired)
345345
}
346-
(SymmetricKeyAlgorithm::Aes256Gcm, _) => Err(CryptoClientError::InvalidKeyType),
346+
(SymmetricKeyAlgorithm::XChaCha20Poly1305 | SymmetricKeyAlgorithm::Aes256Gcm, _) => {
347+
Err(CryptoClientError::InvalidKeyType)
348+
}
347349
}
348350
}
349351
}

crates/bitwarden-core/src/key_management/v2_upgrade_token.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ impl V2UpgradeToken {
114114
.unwrap_symmetric_key(v1_key_id, &self.wrapped_user_key_2)
115115
.map_err(|_| V2UpgradeTokenError::DecryptionFailed)?;
116116

117+
if ctx
118+
.get_symmetric_key_algorithm(v2_key_id)
119+
.map_err(|_| V2UpgradeTokenError::KeyMissing)?
120+
!= SymmetricKeyAlgorithm::XAes256Gcm
121+
{
122+
return Err(V2UpgradeTokenError::WrongKeyType);
123+
}
124+
117125
// Validate: unwrapped V2 should be able to decrypt wrapped V1 key
118126
let _: Vec<u8> = self
119127
.wrapped_user_key_1

crates/bitwarden-crypto/src/keys/symmetric_crypto_key.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl TryFrom<wasm_bindgen::JsValue> for SymmetricCryptoKey {
8888
pub enum SymmetricKeyAlgorithm {
8989
/// Used for V1 user keys and data encryption
9090
Aes256CbcHmac,
91-
/// Used for V2 user keys and data envelopes
91+
/// Used by legacy COSE-based envelopes
9292
XChaCha20Poly1305,
9393
/// FIPS-approved AEAD.
9494
/// Used as content encryption key in:
@@ -97,8 +97,8 @@ pub enum SymmetricKeyAlgorithm {
9797
///
9898
/// May not be used for multi-device scoped keys such as the user-key or organization-key
9999
Aes256Gcm,
100-
/// Extended-nonce AES-256-GCM. Safe for random nonces and usable as a
101-
/// general-purpose encryption/wrapping key.
100+
/// Extended-nonce AES-256-GCM. Used for V2 user keys and as a general-purpose
101+
/// encryption/wrapping key.
102102
XAes256Gcm,
103103
}
104104

crates/bitwarden-crypto/src/store/key_rotation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
CoseKeyBytes, CoseSerializable, CryptoError, EncString, KeyEncryptable, KeySlotIds,
33
KeyStoreContext, SignedPublicKey, SignedPublicKeyMessage, SpkiPublicKeyBytes,
4-
SymmetricCryptoKey,
4+
SymmetricCryptoKey, SymmetricKeyAlgorithm,
55
};
66

77
/// Rotated set of account keys
@@ -27,7 +27,7 @@ pub fn dangerous_get_v2_rotated_account_keys<Ids: KeySlotIds>(
2727
current_user_signing_key_id: Ids::Signing,
2828
ctx: &KeyStoreContext<Ids>,
2929
) -> Result<RotatedUserKeys, CryptoError> {
30-
let user_key = SymmetricCryptoKey::make_xchacha20_poly1305_key();
30+
let user_key = SymmetricCryptoKey::make(SymmetricKeyAlgorithm::XAes256Gcm);
3131

3232
let current_private_key = ctx.get_private_key(current_user_private_key_id)?;
3333
let current_signing_key = ctx.get_signing_key(current_user_signing_key_id)?;

crates/bitwarden-user-crypto-management/src/key_rotation/rotation_context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ pub(super) fn make_rotation_context(
8484
);
8585
let current_user_key_id = SymmetricKeySlotId::User;
8686

87-
debug!("Generating new xchacha20-poly1305 user key for key rotation");
88-
let new_user_key_id = ctx.make_symmetric_key(SymmetricKeyAlgorithm::XChaCha20Poly1305);
87+
debug!("Generating new XAES-256-GCM user key for key rotation");
88+
let new_user_key_id = ctx.make_symmetric_key(SymmetricKeyAlgorithm::XAes256Gcm);
8989

9090
Ok(RotationContext {
9191
v1_organization_memberships,

0 commit comments

Comments
 (0)