@@ -76,7 +76,7 @@ pub struct XChaCha20Poly1305Key {
76
76
77
77
impl ConstantTimeEq for XChaCha20Poly1305Key {
78
78
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 )
80
80
}
81
81
}
82
82
@@ -215,6 +215,9 @@ impl SymmetricCryptoKey {
215
215
}
216
216
217
217
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.
218
221
fn ct_eq ( & self , other : & SymmetricCryptoKey ) -> Choice {
219
222
use SymmetricCryptoKey :: * ;
220
223
match ( self , other) {
@@ -353,9 +356,14 @@ pub fn derive_symmetric_key(name: &str) -> Aes256CbcHmacKey {
353
356
#[ cfg( test) ]
354
357
mod tests {
355
358
use base64:: { engine:: general_purpose:: STANDARD , Engine } ;
359
+ use generic_array:: GenericArray ;
360
+ use typenum:: U32 ;
356
361
357
362
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
+ } ;
359
367
360
368
#[ test]
361
369
fn test_symmetric_crypto_key ( ) {
@@ -431,4 +439,56 @@ mod tests {
431
439
let unpadded_key = unpad_key ( & key_bytes) . unwrap ( ) ;
432
440
assert_eq ! ( original_key, unpadded_key) ;
433
441
}
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
+ }
434
494
}
0 commit comments