1
+ #[ cfg( not( feature = "std" ) ) ]
2
+ use alloc:: boxed:: Box ;
3
+
4
+ use super :: { DecryptBufferAdapter , EncryptBufferAdapter } ;
5
+
1
6
use 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 ,
7
11
} ;
12
+ use rustls:: { ConnectionTrafficSecrets , ContentType , ProtocolVersion } ;
13
+
14
+ #[ cfg( feature = "tls12" ) ]
15
+ use rustls:: crypto:: cipher:: { KeyBlockShape , Tls12AeadAlgorithm , NONCE_LEN } ;
8
16
9
17
pub struct Chacha20Poly1305 ;
10
18
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 > {
13
21
Box :: new ( Tls13Cipher (
14
22
chacha20poly1305:: ChaCha20Poly1305 :: new_from_slice ( key. as_ref ( ) ) . unwrap ( ) ,
15
23
iv,
16
24
) )
17
25
}
18
26
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 > {
20
28
Box :: new ( Tls13Cipher (
21
29
chacha20poly1305:: ChaCha20Poly1305 :: new_from_slice ( key. as_ref ( ) ) . unwrap ( ) ,
22
30
iv,
@@ -37,28 +45,23 @@ impl cipher::Tls13AeadAlgorithm for Chacha20Poly1305 {
37
45
}
38
46
39
47
#[ 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 > {
47
50
Box :: new ( Tls12Cipher (
48
51
chacha20poly1305:: ChaCha20Poly1305 :: new_from_slice ( key. as_ref ( ) ) . unwrap ( ) ,
49
- cipher :: Iv :: copy ( iv) ,
52
+ Iv :: copy ( iv) ,
50
53
) )
51
54
}
52
55
53
- fn decrypter ( & self , key : cipher :: AeadKey , iv : & [ u8 ] ) -> Box < dyn cipher :: MessageDecrypter > {
56
+ fn decrypter ( & self , key : AeadKey , iv : & [ u8 ] ) -> Box < dyn MessageDecrypter > {
54
57
Box :: new ( Tls12Cipher (
55
58
chacha20poly1305:: ChaCha20Poly1305 :: new_from_slice ( key. as_ref ( ) ) . unwrap ( ) ,
56
- cipher :: Iv :: copy ( iv) ,
59
+ Iv :: copy ( iv) ,
57
60
) )
58
61
}
59
62
60
- fn key_block_shape ( & self ) -> cipher :: KeyBlockShape {
61
- cipher :: KeyBlockShape {
63
+ fn key_block_shape ( & self ) -> KeyBlockShape {
64
+ KeyBlockShape {
62
65
enc_key_len : 32 ,
63
66
fixed_iv_len : 12 ,
64
67
explicit_nonce_len : 0 ,
@@ -81,29 +84,28 @@ impl cipher::Tls12AeadAlgorithm for Chacha20Poly1305 {
81
84
}
82
85
}
83
86
84
- struct Tls13Cipher ( chacha20poly1305:: ChaCha20Poly1305 , cipher :: Iv ) ;
87
+ struct Tls13Cipher ( chacha20poly1305:: ChaCha20Poly1305 , Iv ) ;
85
88
86
- impl cipher :: MessageEncrypter for Tls13Cipher {
89
+ impl MessageEncrypter for Tls13Cipher {
87
90
fn encrypt (
88
91
& mut self ,
89
- m : cipher :: BorrowedPlainMessage ,
92
+ m : OutboundPlainMessage ,
90
93
seq : u64 ,
91
- ) -> Result < cipher :: OpaqueMessage , rustls:: Error > {
94
+ ) -> Result < OutboundOpaqueMessage , rustls:: Error > {
92
95
let total_len = self . encrypted_payload_len ( m. payload . len ( ) ) ;
96
+ let mut payload = PrefixedPayload :: with_capacity ( total_len) ;
93
97
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 ( ) ) ;
98
100
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 ( ) ;
100
102
let aad = cipher:: make_tls13_aad ( total_len) ;
101
103
102
104
self . 0
103
- . encrypt_in_place ( & nonce, & aad, & mut payload)
105
+ . encrypt_in_place ( & nonce, & aad, & mut EncryptBufferAdapter ( & mut payload) )
104
106
. map_err ( |_| rustls:: Error :: EncryptError )
105
107
. map ( |( ) | {
106
- cipher :: OpaqueMessage :: new (
108
+ OutboundOpaqueMessage :: new (
107
109
ContentType :: ApplicationData ,
108
110
ProtocolVersion :: TLSv1_2 ,
109
111
payload,
@@ -116,46 +118,46 @@ impl cipher::MessageEncrypter for Tls13Cipher {
116
118
}
117
119
}
118
120
119
- impl cipher :: MessageDecrypter for Tls13Cipher {
120
- fn decrypt (
121
+ impl MessageDecrypter for Tls13Cipher {
122
+ fn decrypt < ' a > (
121
123
& mut self ,
122
- mut m : cipher :: OpaqueMessage ,
124
+ mut m : InboundOpaqueMessage < ' a > ,
123
125
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 ( ) ;
127
129
let aad = cipher:: make_tls13_aad ( payload. len ( ) ) ;
128
130
129
131
self . 0
130
- . decrypt_in_place ( & nonce, & aad, payload)
132
+ . decrypt_in_place ( & nonce, & aad, & mut DecryptBufferAdapter ( payload) )
131
133
. map_err ( |_| rustls:: Error :: DecryptError ) ?;
132
134
133
135
m. into_tls13_unpadded_message ( )
134
136
}
135
137
}
136
138
137
139
#[ cfg( feature = "tls12" ) ]
138
- struct Tls12Cipher ( chacha20poly1305:: ChaCha20Poly1305 , cipher :: Iv ) ;
140
+ struct Tls12Cipher ( chacha20poly1305:: ChaCha20Poly1305 , Iv ) ;
139
141
140
142
#[ cfg( feature = "tls12" ) ]
141
- impl cipher :: MessageEncrypter for Tls12Cipher {
143
+ impl MessageEncrypter for Tls12Cipher {
142
144
fn encrypt (
143
145
& mut self ,
144
- m : cipher :: BorrowedPlainMessage ,
146
+ m : OutboundPlainMessage ,
145
147
seq : u64 ,
146
- ) -> Result < cipher :: OpaqueMessage , rustls:: Error > {
148
+ ) -> Result < OutboundOpaqueMessage , rustls:: Error > {
147
149
let total_len = self . encrypted_payload_len ( m. payload . len ( ) ) ;
150
+ let mut payload = PrefixedPayload :: with_capacity ( total_len) ;
148
151
149
- let mut payload = Vec :: with_capacity ( total_len) ;
150
- payload. extend_from_slice ( m. payload ) ;
152
+ payload. extend_from_chunks ( & m. payload ) ;
151
153
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 ( ) ) ;
154
156
155
157
self . 0
156
- . encrypt_in_place ( & nonce, & aad, & mut payload)
158
+ . encrypt_in_place ( & nonce, & aad, & mut EncryptBufferAdapter ( & mut payload) )
157
159
. map_err ( |_| rustls:: Error :: EncryptError )
158
- . map ( |_| cipher :: OpaqueMessage :: new ( m. typ , m. version , payload) )
160
+ . map ( |_| OutboundOpaqueMessage :: new ( m. typ , m. version , payload) )
159
161
}
160
162
161
163
fn encrypted_payload_len ( & self , payload_len : usize ) -> usize {
@@ -164,24 +166,24 @@ impl cipher::MessageEncrypter for Tls12Cipher {
164
166
}
165
167
166
168
#[ cfg( feature = "tls12" ) ]
167
- impl cipher :: MessageDecrypter for Tls12Cipher {
168
- fn decrypt (
169
+ impl MessageDecrypter for Tls12Cipher {
170
+ fn decrypt < ' a > (
169
171
& mut self ,
170
- mut m : cipher :: OpaqueMessage ,
172
+ mut m : InboundOpaqueMessage < ' a > ,
171
173
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 ( ) ;
175
177
let aad = cipher:: make_tls12_aad (
176
178
seq,
177
179
m. typ ,
178
180
m. version ,
179
181
payload. len ( ) - CHACHAPOLY1305_OVERHEAD ,
180
182
) ;
181
183
182
- let payload = m . payload_mut ( ) ;
184
+ let payload = & mut m . payload ;
183
185
self . 0
184
- . decrypt_in_place ( & nonce, & aad, payload)
186
+ . decrypt_in_place ( & nonce, & aad, & mut DecryptBufferAdapter ( payload) )
185
187
. map_err ( |_| rustls:: Error :: DecryptError ) ?;
186
188
187
189
Ok ( m. into_plain_message ( ) )
0 commit comments