@@ -18,8 +18,8 @@ use enum_map::EnumMap;
1818use neqo_common:: { Buffer , Encoder , Role , hex, hex_snip_middle, qdebug, qinfo, qtrace} ;
1919pub use nss:: Epoch ;
2020use nss:: {
21- Agent , AntiReplay , Cipher , Error as CryptoError , HandshakeState , PrivateKey , PublicKey , Record ,
22- RecordList , RecordProtection as Aead , ResumptionToken , SymKey , TLS_AES_128_GCM_SHA256 ,
21+ Agent , AntiReplay , Cipher , Error as CryptoError , HandshakeState , Mode , PrivateKey , PublicKey ,
22+ Record , RecordList , RecordProtection as Aead , ResumptionToken , SymKey , TLS_AES_128_GCM_SHA256 ,
2323 TLS_AES_256_GCM_SHA384 , TLS_CHACHA20_POLY1305_SHA256 , TLS_CT_HANDSHAKE , TLS_GRP_EC_SECP256R1 ,
2424 TLS_GRP_EC_SECP384R1 , TLS_GRP_EC_SECP521R1 , TLS_GRP_EC_X25519 , TLS_GRP_KEM_MLKEM768X25519 ,
2525 TLS_VERSION_1_3 , ZeroRttChecker , hkdf, hp, random,
@@ -444,6 +444,15 @@ pub enum CryptoDxDirection {
444444 Write ,
445445}
446446
447+ impl From < CryptoDxDirection > for Mode {
448+ fn from ( dir : CryptoDxDirection ) -> Self {
449+ match dir {
450+ CryptoDxDirection :: Read => Self :: Decrypt ,
451+ CryptoDxDirection :: Write => Self :: Encrypt ,
452+ }
453+ }
454+ }
455+
447456#[ derive( Debug ) ]
448457pub struct CryptoDxState {
449458 /// The QUIC version.
@@ -489,7 +498,13 @@ impl CryptoDxState {
489498 version,
490499 direction,
491500 epoch : usize:: from ( epoch) ,
492- aead : Aead :: new ( TLS_VERSION_1_3 , cipher, secret, version. label_prefix ( ) ) ?,
501+ aead : Aead :: new (
502+ TLS_VERSION_1_3 ,
503+ cipher,
504+ secret,
505+ version. label_prefix ( ) ,
506+ direction. into ( ) ,
507+ ) ?,
493508 hpkey : hp:: Key :: extract ( TLS_VERSION_1_3 , cipher, secret, & hplabel) ?,
494509 used_pn : min_pn..min_pn,
495510 min_pn,
@@ -581,6 +596,7 @@ impl CryptoDxState {
581596 cipher,
582597 next_secret,
583598 self . version . label_prefix ( ) ,
599+ self . direction . into ( ) ,
584600 ) ?,
585601 hpkey : self . hpkey . try_clone ( ) ?,
586602 used_pn : pn..pn,
@@ -708,6 +724,13 @@ impl CryptoDxState {
708724 }
709725
710726 #[ must_use]
727+ #[ cfg( not( feature = "disable-encryption" ) ) ]
728+ pub const fn expansion ( & self ) -> usize {
729+ self . aead . expansion ( )
730+ }
731+
732+ #[ must_use]
733+ #[ cfg( feature = "disable-encryption" ) ]
711734 pub fn expansion ( & self ) -> usize {
712735 self . aead . expansion ( )
713736 }
@@ -734,21 +757,35 @@ impl CryptoDxState {
734757 #[ cfg( not( feature = "disable-encryption" ) ) ]
735758 #[ cfg( test) ]
736759 pub ( crate ) fn test_default ( ) -> Self {
760+ Self :: test_default_with_direction ( CryptoDxDirection :: Write )
761+ }
762+
763+ #[ cfg( not( feature = "disable-encryption" ) ) ]
764+ #[ cfg( test) ]
765+ pub ( crate ) fn test_default_read ( ) -> Self {
766+ Self :: test_default_with_direction ( CryptoDxDirection :: Read )
767+ }
768+
769+ #[ cfg( not( feature = "disable-encryption" ) ) ]
770+ #[ cfg( test) ]
771+ fn test_default_with_direction ( direction : CryptoDxDirection ) -> Self {
737772 // This matches the value in packet.rs
738773 const CLIENT_CID : & [ u8 ] = & [ 0x83 , 0x94 , 0xc8 , 0xf0 , 0x3e , 0x51 , 0x57 , 0x08 ] ;
739- Self :: new_initial (
740- Version :: default ( ) ,
741- CryptoDxDirection :: Write ,
742- "server in" ,
743- CLIENT_CID ,
744- 0 ,
745- )
746- . unwrap ( )
774+ Self :: new_initial ( Version :: default ( ) , direction, "server in" , CLIENT_CID , 0 ) . unwrap ( )
775+ }
776+
777+ /// Get the amount of extra padding packets protected with this profile need.
778+ /// This is the difference between the size of the header protection sample
779+ /// and the AEAD expansion.
780+ #[ cfg( not( feature = "disable-encryption" ) ) ]
781+ pub const fn extra_padding ( & self ) -> usize {
782+ hp:: Key :: SAMPLE_SIZE . saturating_sub ( self . expansion ( ) )
747783 }
748784
749785 /// Get the amount of extra padding packets protected with this profile need.
750786 /// This is the difference between the size of the header protection sample
751787 /// and the AEAD expansion.
788+ #[ cfg( feature = "disable-encryption" ) ]
752789 pub fn extra_padding ( & self ) -> usize {
753790 hp:: Key :: SAMPLE_SIZE . saturating_sub ( self . expansion ( ) )
754791 }
@@ -1340,8 +1377,7 @@ impl CryptoStates {
13401377 #[ cfg( test) ]
13411378 pub ( crate ) fn test_default ( ) -> Self {
13421379 let read = |epoch| {
1343- let mut dx = CryptoDxState :: test_default ( ) ;
1344- dx. direction = CryptoDxDirection :: Read ;
1380+ let mut dx = CryptoDxState :: test_default_read ( ) ;
13451381 dx. epoch = epoch;
13461382 dx
13471383 } ;
@@ -1390,6 +1426,7 @@ impl CryptoStates {
13901426 TLS_CHACHA20_POLY1305_SHA256 ,
13911427 & secret,
13921428 "quic " , // This is a v1 test so hard-code the label.
1429+ Mode :: Decrypt ,
13931430 )
13941431 . unwrap ( ) ,
13951432 hpkey : hp:: Key :: extract (
0 commit comments