1+ #[ cfg( not( feature = "std" ) ) ]
2+ use alloc:: boxed:: Box ;
3+
4+ use super :: { DecryptBufferAdapter , EncryptBufferAdapter } ;
5+
16use chacha20poly1305:: { AeadInPlace , KeyInit , KeySizeUser } ;
2- #[ cfg( feature = "tls12" ) ]
3- use rustls:: crypto:: cipher:: NONCE_LEN ;
4- use rustls:: {
5- crypto:: cipher:: { self , AeadKey , Iv , UnsupportedOperationError } ,
6- ConnectionTrafficSecrets , ContentType , ProtocolVersion ,
7+ use rustls:: crypto:: cipher:: {
8+ self , AeadKey , InboundOpaqueMessage , InboundPlainMessage , Iv , MessageDecrypter ,
9+ MessageEncrypter , OutboundOpaqueMessage , OutboundPlainMessage , PrefixedPayload ,
10+ Tls13AeadAlgorithm , UnsupportedOperationError ,
711} ;
12+ use rustls:: { ConnectionTrafficSecrets , ContentType , ProtocolVersion } ;
13+
14+ #[ cfg( feature = "tls12" ) ]
15+ use rustls:: crypto:: cipher:: { KeyBlockShape , Tls12AeadAlgorithm , NONCE_LEN } ;
816
917pub struct Chacha20Poly1305 ;
1018
11- impl cipher :: Tls13AeadAlgorithm for Chacha20Poly1305 {
12- fn encrypter ( & self , key : cipher :: AeadKey , iv : cipher :: Iv ) -> Box < dyn cipher :: MessageEncrypter > {
19+ impl Tls13AeadAlgorithm for Chacha20Poly1305 {
20+ fn encrypter ( & self , key : AeadKey , iv : Iv ) -> Box < dyn MessageEncrypter > {
1321 Box :: new ( Tls13Cipher (
1422 chacha20poly1305:: ChaCha20Poly1305 :: new_from_slice ( key. as_ref ( ) ) . unwrap ( ) ,
1523 iv,
1624 ) )
1725 }
1826
19- fn decrypter ( & self , key : cipher :: AeadKey , iv : cipher :: Iv ) -> Box < dyn cipher :: MessageDecrypter > {
27+ fn decrypter ( & self , key : AeadKey , iv : Iv ) -> Box < dyn MessageDecrypter > {
2028 Box :: new ( Tls13Cipher (
2129 chacha20poly1305:: ChaCha20Poly1305 :: new_from_slice ( key. as_ref ( ) ) . unwrap ( ) ,
2230 iv,
@@ -37,28 +45,23 @@ impl cipher::Tls13AeadAlgorithm for Chacha20Poly1305 {
3745}
3846
3947#[ cfg( feature = "tls12" ) ]
40- impl cipher:: Tls12AeadAlgorithm for Chacha20Poly1305 {
41- fn encrypter (
42- & self ,
43- key : cipher:: AeadKey ,
44- iv : & [ u8 ] ,
45- _: & [ u8 ] ,
46- ) -> Box < dyn cipher:: MessageEncrypter > {
48+ impl Tls12AeadAlgorithm for Chacha20Poly1305 {
49+ fn encrypter ( & self , key : AeadKey , iv : & [ u8 ] , _: & [ u8 ] ) -> Box < dyn MessageEncrypter > {
4750 Box :: new ( Tls12Cipher (
4851 chacha20poly1305:: ChaCha20Poly1305 :: new_from_slice ( key. as_ref ( ) ) . unwrap ( ) ,
49- cipher :: Iv :: copy ( iv) ,
52+ Iv :: copy ( iv) ,
5053 ) )
5154 }
5255
53- fn decrypter ( & self , key : cipher :: AeadKey , iv : & [ u8 ] ) -> Box < dyn cipher :: MessageDecrypter > {
56+ fn decrypter ( & self , key : AeadKey , iv : & [ u8 ] ) -> Box < dyn MessageDecrypter > {
5457 Box :: new ( Tls12Cipher (
5558 chacha20poly1305:: ChaCha20Poly1305 :: new_from_slice ( key. as_ref ( ) ) . unwrap ( ) ,
56- cipher :: Iv :: copy ( iv) ,
59+ Iv :: copy ( iv) ,
5760 ) )
5861 }
5962
60- fn key_block_shape ( & self ) -> cipher :: KeyBlockShape {
61- cipher :: KeyBlockShape {
63+ fn key_block_shape ( & self ) -> KeyBlockShape {
64+ KeyBlockShape {
6265 enc_key_len : 32 ,
6366 fixed_iv_len : 12 ,
6467 explicit_nonce_len : 0 ,
@@ -81,29 +84,28 @@ impl cipher::Tls12AeadAlgorithm for Chacha20Poly1305 {
8184 }
8285}
8386
84- struct Tls13Cipher ( chacha20poly1305:: ChaCha20Poly1305 , cipher :: Iv ) ;
87+ struct Tls13Cipher ( chacha20poly1305:: ChaCha20Poly1305 , Iv ) ;
8588
86- impl cipher :: MessageEncrypter for Tls13Cipher {
89+ impl MessageEncrypter for Tls13Cipher {
8790 fn encrypt (
8891 & mut self ,
89- m : cipher :: BorrowedPlainMessage ,
92+ m : OutboundPlainMessage ,
9093 seq : u64 ,
91- ) -> Result < cipher :: OpaqueMessage , rustls:: Error > {
94+ ) -> Result < OutboundOpaqueMessage , rustls:: Error > {
9295 let total_len = self . encrypted_payload_len ( m. payload . len ( ) ) ;
96+ let mut payload = PrefixedPayload :: with_capacity ( total_len) ;
9397
94- // construct a TLSInnerPlaintext
95- let mut payload = Vec :: with_capacity ( total_len) ;
96- payload. extend_from_slice ( m. payload ) ;
97- payload. push ( m. typ . get_u8 ( ) ) ;
98+ payload. extend_from_chunks ( & m. payload ) ;
99+ payload. extend_from_slice ( & m. typ . to_array ( ) ) ;
98100
99- let nonce = chacha20poly1305:: Nonce :: from ( cipher:: Nonce :: new ( & self . 1 , seq) . 0 ) ;
101+ let nonce: chacha20poly1305:: Nonce = cipher:: Nonce :: new ( & self . 1 , seq) . 0 . into ( ) ;
100102 let aad = cipher:: make_tls13_aad ( total_len) ;
101103
102104 self . 0
103- . encrypt_in_place ( & nonce, & aad, & mut payload)
105+ . encrypt_in_place ( & nonce, & aad, & mut EncryptBufferAdapter ( & mut payload) )
104106 . map_err ( |_| rustls:: Error :: EncryptError )
105107 . map ( |( ) | {
106- cipher :: OpaqueMessage :: new (
108+ OutboundOpaqueMessage :: new (
107109 ContentType :: ApplicationData ,
108110 ProtocolVersion :: TLSv1_2 ,
109111 payload,
@@ -116,46 +118,46 @@ impl cipher::MessageEncrypter for Tls13Cipher {
116118 }
117119}
118120
119- impl cipher :: MessageDecrypter for Tls13Cipher {
120- fn decrypt (
121+ impl MessageDecrypter for Tls13Cipher {
122+ fn decrypt < ' a > (
121123 & mut self ,
122- mut m : cipher :: OpaqueMessage ,
124+ mut m : InboundOpaqueMessage < ' a > ,
123125 seq : u64 ,
124- ) -> Result < cipher :: PlainMessage , rustls:: Error > {
125- let payload = m . payload_mut ( ) ;
126- let nonce = chacha20poly1305:: Nonce :: from ( cipher:: Nonce :: new ( & self . 1 , seq) . 0 ) ;
126+ ) -> Result < InboundPlainMessage < ' a > , rustls:: Error > {
127+ let payload = & mut m . payload ;
128+ let nonce: chacha20poly1305:: Nonce = cipher:: Nonce :: new ( & self . 1 , seq) . 0 . into ( ) ;
127129 let aad = cipher:: make_tls13_aad ( payload. len ( ) ) ;
128130
129131 self . 0
130- . decrypt_in_place ( & nonce, & aad, payload)
132+ . decrypt_in_place ( & nonce, & aad, & mut DecryptBufferAdapter ( payload) )
131133 . map_err ( |_| rustls:: Error :: DecryptError ) ?;
132134
133135 m. into_tls13_unpadded_message ( )
134136 }
135137}
136138
137139#[ cfg( feature = "tls12" ) ]
138- struct Tls12Cipher ( chacha20poly1305:: ChaCha20Poly1305 , cipher :: Iv ) ;
140+ struct Tls12Cipher ( chacha20poly1305:: ChaCha20Poly1305 , Iv ) ;
139141
140142#[ cfg( feature = "tls12" ) ]
141- impl cipher :: MessageEncrypter for Tls12Cipher {
143+ impl MessageEncrypter for Tls12Cipher {
142144 fn encrypt (
143145 & mut self ,
144- m : cipher :: BorrowedPlainMessage ,
146+ m : OutboundPlainMessage ,
145147 seq : u64 ,
146- ) -> Result < cipher :: OpaqueMessage , rustls:: Error > {
148+ ) -> Result < OutboundOpaqueMessage , rustls:: Error > {
147149 let total_len = self . encrypted_payload_len ( m. payload . len ( ) ) ;
150+ let mut payload = PrefixedPayload :: with_capacity ( total_len) ;
148151
149- let mut payload = Vec :: with_capacity ( total_len) ;
150- payload. extend_from_slice ( m. payload ) ;
152+ payload. extend_from_chunks ( & m. payload ) ;
151153
152- let nonce = chacha20poly1305:: Nonce :: from ( cipher:: Nonce :: new ( & self . 1 , seq) . 0 ) ;
153- let aad = cipher:: make_tls12_aad ( seq, m. typ , m. version , payload. len ( ) ) ;
154+ let nonce: chacha20poly1305:: Nonce = cipher:: Nonce :: new ( & self . 1 , seq) . 0 . into ( ) ;
155+ let aad = cipher:: make_tls12_aad ( seq, m. typ , m. version , m . payload . len ( ) ) ;
154156
155157 self . 0
156- . encrypt_in_place ( & nonce, & aad, & mut payload)
158+ . encrypt_in_place ( & nonce, & aad, & mut EncryptBufferAdapter ( & mut payload) )
157159 . map_err ( |_| rustls:: Error :: EncryptError )
158- . map ( |_| cipher :: OpaqueMessage :: new ( m. typ , m. version , payload) )
160+ . map ( |_| OutboundOpaqueMessage :: new ( m. typ , m. version , payload) )
159161 }
160162
161163 fn encrypted_payload_len ( & self , payload_len : usize ) -> usize {
@@ -164,24 +166,24 @@ impl cipher::MessageEncrypter for Tls12Cipher {
164166}
165167
166168#[ cfg( feature = "tls12" ) ]
167- impl cipher :: MessageDecrypter for Tls12Cipher {
168- fn decrypt (
169+ impl MessageDecrypter for Tls12Cipher {
170+ fn decrypt < ' a > (
169171 & mut self ,
170- mut m : cipher :: OpaqueMessage ,
172+ mut m : InboundOpaqueMessage < ' a > ,
171173 seq : u64 ,
172- ) -> Result < cipher :: PlainMessage , rustls:: Error > {
173- let payload = m. payload ( ) ;
174- let nonce = chacha20poly1305:: Nonce :: from ( cipher:: Nonce :: new ( & self . 1 , seq) . 0 ) ;
174+ ) -> Result < InboundPlainMessage < ' a > , rustls:: Error > {
175+ let payload = & m. payload ;
176+ let nonce: chacha20poly1305:: Nonce = cipher:: Nonce :: new ( & self . 1 , seq) . 0 . into ( ) ;
175177 let aad = cipher:: make_tls12_aad (
176178 seq,
177179 m. typ ,
178180 m. version ,
179181 payload. len ( ) - CHACHAPOLY1305_OVERHEAD ,
180182 ) ;
181183
182- let payload = m . payload_mut ( ) ;
184+ let payload = & mut m . payload ;
183185 self . 0
184- . decrypt_in_place ( & nonce, & aad, payload)
186+ . decrypt_in_place ( & nonce, & aad, & mut DecryptBufferAdapter ( payload) )
185187 . map_err ( |_| rustls:: Error :: DecryptError ) ?;
186188
187189 Ok ( m. into_plain_message ( ) )
0 commit comments