Skip to content

Commit f7d5b36

Browse files
committed
Add constant time eq tests and comment
1 parent 4e00b0f commit f7d5b36

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

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

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub struct XChaCha20Poly1305Key {
7676

7777
impl ConstantTimeEq for XChaCha20Poly1305Key {
7878
fn ct_eq(&self, other: &Self) -> Choice {
79-
self.enc_key.ct_eq(&other.enc_key)
79+
self.enc_key.ct_eq(&other.enc_key) & self.key_id.ct_eq(&other.key_id)
8080
}
8181
}
8282

@@ -215,6 +215,9 @@ impl SymmetricCryptoKey {
215215
}
216216

217217
impl ConstantTimeEq for SymmetricCryptoKey {
218+
/// Note: This is constant time with respect to comparing two keys of the same type, but not
219+
/// constant type with respect to the fact that different keys are compared. If two types of
220+
/// different keys are compared, then this does have different timing.
218221
fn ct_eq(&self, other: &SymmetricCryptoKey) -> Choice {
219222
use SymmetricCryptoKey::*;
220223
match (self, other) {
@@ -353,9 +356,14 @@ pub fn derive_symmetric_key(name: &str) -> Aes256CbcHmacKey {
353356
#[cfg(test)]
354357
mod tests {
355358
use base64::{engine::general_purpose::STANDARD, Engine};
359+
use generic_array::GenericArray;
360+
use typenum::U32;
356361

357362
use super::{derive_symmetric_key, SymmetricCryptoKey};
358-
use crate::keys::symmetric_crypto_key::{pad_key, unpad_key};
363+
use crate::{
364+
keys::symmetric_crypto_key::{pad_key, unpad_key},
365+
Aes256CbcKey, XChaCha20Poly1305Key,
366+
};
359367

360368
#[test]
361369
fn test_symmetric_crypto_key() {
@@ -431,4 +439,56 @@ mod tests {
431439
let unpadded_key = unpad_key(&key_bytes).unwrap();
432440
assert_eq!(original_key, unpadded_key);
433441
}
442+
443+
#[test]
444+
fn test_eq_aes_cbc_hmac() {
445+
let key1 = SymmetricCryptoKey::make_aes256_cbc_hmac_key();
446+
let key2 = SymmetricCryptoKey::make_aes256_cbc_hmac_key();
447+
assert_ne!(key1, key2);
448+
let key3 = SymmetricCryptoKey::try_from(key1.to_base64()).unwrap();
449+
assert_eq!(key1, key3);
450+
}
451+
452+
#[test]
453+
fn test_eq_aes_cbc() {
454+
let key1 = SymmetricCryptoKey::try_from(vec![1u8; 32]).unwrap();
455+
let key2 = SymmetricCryptoKey::try_from(vec![2u8; 32]).unwrap();
456+
assert_ne!(key1, key2);
457+
let key3 = SymmetricCryptoKey::try_from(key1.to_base64()).unwrap();
458+
assert_eq!(key1, key3);
459+
}
460+
461+
#[test]
462+
fn test_eq_xchacha20_poly1305() {
463+
let key1 = SymmetricCryptoKey::make_xchacha20_poly1305_key();
464+
let key2 = SymmetricCryptoKey::make_xchacha20_poly1305_key();
465+
assert_ne!(key1, key2);
466+
let key3 = SymmetricCryptoKey::try_from(key1.to_base64()).unwrap();
467+
assert_eq!(key1, key3);
468+
}
469+
470+
#[test]
471+
fn test_neq_different_key_types() {
472+
let key1 = SymmetricCryptoKey::Aes256CbcKey(Aes256CbcKey {
473+
enc_key: Box::pin(GenericArray::<u8, U32>::default()),
474+
});
475+
let key2 = SymmetricCryptoKey::XChaCha20Poly1305Key(XChaCha20Poly1305Key {
476+
enc_key: Box::pin(GenericArray::<u8, U32>::default()),
477+
key_id: [0; 16],
478+
});
479+
assert_ne!(key1, key2);
480+
}
481+
482+
#[test]
483+
fn test_neq_different_key_id() {
484+
let key1 = SymmetricCryptoKey::XChaCha20Poly1305Key(XChaCha20Poly1305Key {
485+
enc_key: Box::pin(GenericArray::<u8, U32>::default()),
486+
key_id: [0; 16],
487+
});
488+
let key2 = SymmetricCryptoKey::XChaCha20Poly1305Key(XChaCha20Poly1305Key {
489+
enc_key: Box::pin(GenericArray::<u8, U32>::default()),
490+
key_id: [1; 16],
491+
});
492+
assert_ne!(key1, key2);
493+
}
434494
}

0 commit comments

Comments
 (0)