Skip to content

Commit 9b2f6c9

Browse files
committed
Run cargo fmt
1 parent 66c345f commit 9b2f6c9

File tree

3 files changed

+74
-37
lines changed

3 files changed

+74
-37
lines changed

crates/bitwarden-crypto/src/cose.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,20 @@ pub(crate) fn decrypt_xchacha20_poly1305(
125125
.map_err(|_| CryptoError::InvalidPadding)?;
126126
return Ok((data.to_vec(), ContentFormat::Utf8));
127127
}
128-
},
129-
ContentType::Assigned(content_format) if *content_format == CoapContentFormat::Pkcs8 => {
128+
}
129+
ContentType::Assigned(content_format)
130+
if *content_format == CoapContentFormat::Pkcs8 =>
131+
{
130132
return Ok((decrypted_message.to_vec(), ContentFormat::Pkcs8));
131133
}
132-
ContentType::Assigned(content_format) if *content_format == CoapContentFormat::CoseKey => {
134+
ContentType::Assigned(content_format)
135+
if *content_format == CoapContentFormat::CoseKey =>
136+
{
133137
return Ok((decrypted_message.to_vec(), ContentFormat::CoseKey));
134138
}
135-
ContentType::Assigned(content_format) if *content_format == CoapContentFormat::OctetStream => {
139+
ContentType::Assigned(content_format)
140+
if *content_format == CoapContentFormat::OctetStream =>
141+
{
136142
return Ok((decrypted_message.to_vec(), ContentFormat::OctetStream));
137143
}
138144
_ => {}
@@ -241,5 +247,4 @@ mod test {
241247
let decrypted = decrypt_xchacha20_poly1305(&encrypted, key).unwrap();
242248
assert_eq!(decrypted, (plaintext.to_vec(), ContentFormat::CoseKey));
243249
}
244-
245250
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ impl SymmetricCryptoKey {
209209
}
210210

211211
pub(crate) fn try_from_cose(serialized_key: &[u8]) -> Result<Self, CryptoError> {
212-
let cose_key = coset::CoseKey::from_slice(serialized_key)
213-
.map_err(|_| CryptoError::InvalidKey)?;
212+
let cose_key =
213+
coset::CoseKey::from_slice(serialized_key).map_err(|_| CryptoError::InvalidKey)?;
214214
let key = SymmetricCryptoKey::try_from(&cose_key)?;
215215
Ok(key)
216216
}

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

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ impl<Ids: KeyIds> KeyStoreContext<'_, Ids> {
132132
///
133133
/// # Arguments
134134
///
135-
/// * `wrapping_key` - The key id used to decrypt the `wrapped_key`. It must already exist
136-
/// in the context
135+
/// * `wrapping_key` - The key id used to decrypt the `wrapped_key`. It must already exist in
136+
/// the context
137137
/// * `new_key_id` - The key id where the decrypted key will be stored. If it already exists, it
138138
/// will be overwritten
139139
/// * `wrapped_key` - The key to decrypt
@@ -147,30 +147,39 @@ impl<Ids: KeyIds> KeyStoreContext<'_, Ids> {
147147

148148
let key = match (wrapped_key, wrapping_key) {
149149
(EncString::Aes256Cbc_B64 { iv, data }, SymmetricCryptoKey::Aes256CbcKey(key)) => {
150-
SymmetricCryptoKey::try_from(crate::aes::decrypt_aes256(iv, data.clone(), &key.enc_key)?)?
150+
SymmetricCryptoKey::try_from(crate::aes::decrypt_aes256(
151+
iv,
152+
data.clone(),
153+
&key.enc_key,
154+
)?)?
151155
}
152156
(
153157
EncString::Aes256Cbc_HmacSha256_B64 { iv, mac, data },
154158
SymmetricCryptoKey::Aes256CbcHmacKey(key),
159+
) => SymmetricCryptoKey::try_from(crate::aes::decrypt_aes256_hmac(
160+
iv,
161+
mac,
162+
data.clone(),
163+
&key.mac_key,
164+
&key.enc_key,
165+
)?)?,
166+
(
167+
EncString::Cose_Encrypt0_B64 { data },
168+
SymmetricCryptoKey::XChaCha20Poly1305Key(key),
155169
) => {
156-
SymmetricCryptoKey::try_from(crate::aes::decrypt_aes256_hmac(iv, mac, data.clone(), &key.mac_key, &key.enc_key)?)?
157-
}
158-
(EncString::Cose_Encrypt0_B64 { data }, SymmetricCryptoKey::XChaCha20Poly1305Key(key)) => {
159-
let (content_bytes, content_format) = crate::cose::decrypt_xchacha20_poly1305(data, key)?;
170+
let (content_bytes, content_format) =
171+
crate::cose::decrypt_xchacha20_poly1305(data, key)?;
160172
match content_format {
161173
ContentFormat::OctetStream => SymmetricCryptoKey::try_from(content_bytes)?,
162174
ContentFormat::CoseKey => SymmetricCryptoKey::try_from_cose(&content_bytes)?,
163-
_ => return Err(CryptoError::InvalidKey)
175+
_ => return Err(CryptoError::InvalidKey),
164176
}
165177
}
166178
_ => return Err(CryptoError::InvalidKey),
167179
};
168180

169181
#[allow(deprecated)]
170-
self.set_symmetric_key(
171-
new_key_id,
172-
key,
173-
)?;
182+
self.set_symmetric_key(new_key_id, key)?;
174183

175184
// Returning the new key identifier for convenience
176185
Ok(new_key_id)
@@ -199,12 +208,14 @@ impl<Ids: KeyIds> KeyStoreContext<'_, Ids> {
199208
// or `Aes256CbcKey`, or by specifying the content format to be CoseKey, in case the
200209
// wrapped key is a `XChaCha20Poly1305Key`.
201210
match (wrapping_key_instance, key_to_wrap_instance) {
202-
(Aes256CbcHmacKey(_), Aes256CbcHmacKey(_) | Aes256CbcKey(_) | XChaCha20Poly1305Key(_)) => self
203-
.encrypt_data_with_symmetric_key(
204-
wrapping_key,
205-
key_to_wrap_instance.to_encoded().as_slice(),
206-
ContentFormat::OctetStream,
207-
),
211+
(
212+
Aes256CbcHmacKey(_),
213+
Aes256CbcHmacKey(_) | Aes256CbcKey(_) | XChaCha20Poly1305Key(_),
214+
) => self.encrypt_data_with_symmetric_key(
215+
wrapping_key,
216+
key_to_wrap_instance.to_encoded().as_slice(),
217+
ContentFormat::OctetStream,
218+
),
208219
(XChaCha20Poly1305Key(_), Aes256CbcHmacKey(_) | Aes256CbcKey(_)) => self
209220
.encrypt_data_with_symmetric_key(
210221
wrapping_key,
@@ -388,7 +399,10 @@ impl<Ids: KeyIds> KeyStoreContext<'_, Ids> {
388399
EncString::Aes256Cbc_HmacSha256_B64 { iv, mac, data },
389400
SymmetricCryptoKey::Aes256CbcHmacKey(key),
390401
) => crate::aes::decrypt_aes256_hmac(iv, mac, data.clone(), &key.mac_key, &key.enc_key),
391-
(EncString::Cose_Encrypt0_B64 { data }, SymmetricCryptoKey::XChaCha20Poly1305Key(key)) => {
402+
(
403+
EncString::Cose_Encrypt0_B64 { data },
404+
SymmetricCryptoKey::XChaCha20Poly1305Key(key),
405+
) => {
392406
let (data, _) = crate::cose::decrypt_xchacha20_poly1305(data, key)?;
393407
Ok(data)
394408
}
@@ -501,30 +515,48 @@ mod tests {
501515
// Aes256 CBC HMAC keys
502516
let key_aes_1_id = TestSymmKey::A(1);
503517
let key_aes_1 = SymmetricCryptoKey::make_aes256_cbc_hmac_key();
504-
ctx.set_symmetric_key(key_aes_1_id, key_aes_1.clone()).unwrap();
518+
ctx.set_symmetric_key(key_aes_1_id, key_aes_1.clone())
519+
.unwrap();
505520
let key_aes_2_id = TestSymmKey::A(2);
506521
let key_aes_2 = SymmetricCryptoKey::make_aes256_cbc_hmac_key();
507-
ctx.set_symmetric_key(key_aes_2_id, key_aes_2.clone()).unwrap();
522+
ctx.set_symmetric_key(key_aes_2_id, key_aes_2.clone())
523+
.unwrap();
508524

509525
// XChaCha20 Poly1305 keys
510526
let key_xchacha_3_id = TestSymmKey::A(3);
511527
let key_xchacha_3 = SymmetricCryptoKey::make_xchacha20_poly1305_key();
512-
ctx.set_symmetric_key(key_xchacha_3_id, key_xchacha_3.clone()).unwrap();
528+
ctx.set_symmetric_key(key_xchacha_3_id, key_xchacha_3.clone())
529+
.unwrap();
513530
let key_xchacha_4_id = TestSymmKey::A(4);
514531
let key_xchacha_4 = SymmetricCryptoKey::make_xchacha20_poly1305_key();
515-
ctx.set_symmetric_key(key_xchacha_4_id, key_xchacha_4.clone()).unwrap();
532+
ctx.set_symmetric_key(key_xchacha_4_id, key_xchacha_4.clone())
533+
.unwrap();
516534

517535
// Wrap and unwrap the keys
518536
let wrapped_key_1_2 = ctx.wrap_symmetric_key(key_aes_1_id, key_aes_2_id).unwrap();
519-
let wrapped_key_1_3 = ctx.wrap_symmetric_key(key_aes_1_id, key_xchacha_3_id).unwrap();
520-
let wrapped_key_3_1 = ctx.wrap_symmetric_key(key_xchacha_3_id, key_aes_1_id).unwrap();
521-
let wrapped_key_3_4 = ctx.wrap_symmetric_key(key_xchacha_3_id, key_xchacha_4_id).unwrap();
537+
let wrapped_key_1_3 = ctx
538+
.wrap_symmetric_key(key_aes_1_id, key_xchacha_3_id)
539+
.unwrap();
540+
let wrapped_key_3_1 = ctx
541+
.wrap_symmetric_key(key_xchacha_3_id, key_aes_1_id)
542+
.unwrap();
543+
let wrapped_key_3_4 = ctx
544+
.wrap_symmetric_key(key_xchacha_3_id, key_xchacha_4_id)
545+
.unwrap();
522546

523547
// Unwrap the keys
524-
let unwrapped_key_2 = ctx.unwrap_symmetric_key(key_aes_1_id, key_aes_2_id, &wrapped_key_1_2).unwrap();
525-
let unwrapped_key_3 = ctx.unwrap_symmetric_key(key_aes_1_id, key_xchacha_3_id, &wrapped_key_1_3).unwrap();
526-
let unwrapped_key_1 = ctx.unwrap_symmetric_key(key_xchacha_3_id, key_aes_1_id, &wrapped_key_3_1).unwrap();
527-
let unwrapped_key_4 = ctx.unwrap_symmetric_key(key_xchacha_3_id, key_xchacha_4_id, &wrapped_key_3_4).unwrap();
548+
let unwrapped_key_2 = ctx
549+
.unwrap_symmetric_key(key_aes_1_id, key_aes_2_id, &wrapped_key_1_2)
550+
.unwrap();
551+
let unwrapped_key_3 = ctx
552+
.unwrap_symmetric_key(key_aes_1_id, key_xchacha_3_id, &wrapped_key_1_3)
553+
.unwrap();
554+
let unwrapped_key_1 = ctx
555+
.unwrap_symmetric_key(key_xchacha_3_id, key_aes_1_id, &wrapped_key_3_1)
556+
.unwrap();
557+
let unwrapped_key_4 = ctx
558+
.unwrap_symmetric_key(key_xchacha_3_id, key_xchacha_4_id, &wrapped_key_3_4)
559+
.unwrap();
528560

529561
// Assert that the unwrapped keys are the same as the original keys
530562
assert_eq!(unwrapped_key_2, key_aes_2_id);

0 commit comments

Comments
 (0)