@@ -655,86 +655,6 @@ mod test_key_material {
655655 . unwrap ( ) ;
656656 }
657657
658- #[ test]
659- fn test_concatenate ( ) {
660- // intentionally half-full
661- let mut key1 = KeyMaterial256 :: from_bytes ( & [ 1u8 ; 16 ] ) . unwrap ( ) ;
662- let key2 = KeyMaterial256 :: from_bytes ( & [ 2u8 ; 16 ] ) . unwrap ( ) ;
663- assert_eq ! ( key1. key_len( ) , 16 ) ;
664- assert_eq ! ( key2. key_len( ) , 16 ) ;
665-
666- key1. concatenate ( & key2) . unwrap ( ) ;
667- assert_eq ! ( key1. key_len( ) , 32 ) ;
668- assert_eq ! ( key1. ref_to_bytes( ) [ ..16 ] , [ 1u8 ; 16 ] ) ;
669- assert_eq ! ( key1. ref_to_bytes( ) [ 16 ..] , [ 2u8 ; 16 ] ) ;
670-
671- let mut zeroized_key = KeyMaterial256 :: default ( ) ;
672- do_hazardous_operations ( & mut zeroized_key, |zeroized_key| {
673- zeroized_key. set_key_len ( 8 ) . unwrap ( ) ;
674- Ok ( ( ) )
675- } )
676- . unwrap ( ) ;
677- assert_eq ! ( zeroized_key. key_type( ) , KeyType :: Zeroized ) ;
678- assert_eq ! ( zeroized_key. key_len( ) , 8 ) ;
679- zeroized_key. concatenate ( & key2) . unwrap ( ) ;
680- assert_eq ! ( zeroized_key. key_len( ) , 24 ) ;
681- // The result takes the lesser (min) of the two key types: min(Zeroized, BytesLowEntropy).
682- // Folding in zeroized (uninitialized) bytes taints the whole buffer as Zeroized.
683- assert_eq ! ( zeroized_key. key_type( ) , KeyType :: Zeroized ) ;
684- assert_eq ! ( zeroized_key. security_strength( ) , SecurityStrength :: None ) ;
685-
686- // This should be symmetric, so test it in the other direction too.
687- let mut zeroized_key = KeyMaterial256 :: default ( ) ;
688- do_hazardous_operations ( & mut zeroized_key, |zeroized_key| {
689- zeroized_key. set_key_len ( 8 ) . unwrap ( ) ;
690- Ok ( ( ) )
691- } )
692- . unwrap ( ) ;
693- assert_eq ! ( zeroized_key. key_type( ) , KeyType :: Zeroized ) ;
694- assert_eq ! ( zeroized_key. key_len( ) , 8 ) ;
695- let mut key2 = KeyMaterial256 :: from_bytes ( & [ 1u8 ; 16 ] ) . unwrap ( ) ;
696- key2. concatenate ( & zeroized_key) . unwrap ( ) ;
697- assert_eq ! ( key2. key_len( ) , 24 ) ;
698- // The result takes the lesser (min) of the two key types: min(BytesLowEntropy, Zeroized).
699- assert_eq ! ( key2. key_type( ) , KeyType :: Zeroized ) ;
700- assert_eq ! ( key2. security_strength( ) , SecurityStrength :: None ) ;
701-
702- // now try it with keys of different key types
703- let mut low_entropy_key =
704- KeyMaterial256 :: from_bytes_as_type ( & [ 1u8 ; 16 ] , KeyType :: Unknown ) . unwrap ( ) ;
705- let full_entropy_key =
706- KeyMaterial256 :: from_bytes_as_type ( & [ 2u8 ; 16 ] , KeyType :: CryptographicRandom ) . unwrap ( ) ;
707- low_entropy_key. concatenate ( & full_entropy_key) . unwrap ( ) ;
708- // Conservative model: concatenating a full-entropy key with a low-entropy key yields a
709- // low-entropy key. min(BytesLowEntropy, BytesFullEntropy) == BytesLowEntropy.
710- assert_eq ! ( low_entropy_key. key_type( ) , KeyType :: Unknown ) ;
711- // min(None, _128bit) == None (and BytesLowEntropy keys must have strength None anyway).
712- assert_eq ! ( low_entropy_key. security_strength( ) , SecurityStrength :: None ) ;
713-
714- // and in the other direction too
715- let low_entropy_key =
716- KeyMaterial256 :: from_bytes_as_type ( & [ 1u8 ; 16 ] , KeyType :: Unknown ) . unwrap ( ) ;
717- let mut full_entropy_key =
718- KeyMaterial256 :: from_bytes_as_type ( & [ 2u8 ; 16 ] , KeyType :: CryptographicRandom ) . unwrap ( ) ;
719- full_entropy_key. concatenate ( & low_entropy_key) . unwrap ( ) ;
720- // min(BytesFullEntropy, BytesLowEntropy) == BytesLowEntropy.
721- assert_eq ! ( full_entropy_key. key_type( ) , KeyType :: Unknown ) ;
722- // min(_128bit, None) == None.
723- assert_eq ! ( full_entropy_key. security_strength( ) , SecurityStrength :: None ) ;
724-
725- // now with full entropy keys at different security levels
726- let mut full_entropy_key_112 =
727- KeyMaterial512 :: from_bytes_as_type ( & [ 1u8 ; 16 ] , KeyType :: CryptographicRandom ) . unwrap ( ) ;
728- // Now we're gonna explictly tag it at the 112bit security level -- does not require allow_hazardous_operations().
729- full_entropy_key_112. set_security_strength ( SecurityStrength :: _112bit) . unwrap ( ) ;
730- let full_entropy_key =
731- KeyMaterial256 :: from_bytes_as_type ( & [ 2u8 ; 32 ] , KeyType :: CryptographicRandom ) . unwrap ( ) ;
732- full_entropy_key_112. concatenate ( & full_entropy_key) . unwrap ( ) ;
733- assert_eq ! ( full_entropy_key_112. key_type( ) , KeyType :: CryptographicRandom ) ;
734- // The combined key keeps the lower of the two security strengths: min(_112bit, _256bit).
735- assert_eq ! ( full_entropy_key_112. security_strength( ) , SecurityStrength :: _112bit) ;
736- }
737-
738658 #[ test]
739659 fn eq ( ) {
740660 // For context:
0 commit comments