@@ -121,19 +121,15 @@ impl CipherAesCmHmacSha1 {
121121 /// - Authenticated portion of the packet is everything BEFORE MKI
122122 /// - k_a is the session message authentication key
123123 /// - n_tag is the bit-length of the output authentication tag
124- fn generate_srtp_auth_tag ( & mut self , buf : & [ u8 ] , roc : u32 ) -> Vec < u8 > {
125- self . srtp_session_auth . reset ( ) ;
124+ fn generate_srtp_auth_tag ( & self , buf : & [ u8 ] , roc : u32 ) -> [ u8 ; 20 ] {
125+ let mut signer = self . srtp_session_auth . clone ( ) ;
126126
127- self . srtp_session_auth . update ( buf) ;
127+ signer . update ( buf) ;
128128
129129 // For SRTP only, we need to hash the rollover counter as well.
130- self . srtp_session_auth . update ( & roc. to_be_bytes ( ) ) ;
130+ signer . update ( & roc. to_be_bytes ( ) ) ;
131131
132- let result = self . srtp_session_auth . clone ( ) . finalize ( ) ;
133- let code_bytes = result. into_bytes ( ) ;
134-
135- // Truncate the hash to the first AUTH_TAG_SIZE bytes.
136- code_bytes[ 0 ..self . auth_tag_len ( ) ] . to_vec ( )
132+ signer. finalize ( ) . into_bytes ( ) . into ( )
137133 }
138134
139135 /// https://tools.ietf.org/html/rfc3711#section-4.2
@@ -147,13 +143,12 @@ impl CipherAesCmHmacSha1 {
147143 /// - Authenticated portion of the packet is everything BEFORE MKI
148144 /// - k_a is the session message authentication key
149145 /// - n_tag is the bit-length of the output authentication tag
150- fn generate_srtcp_auth_tag ( & mut self , buf : & [ u8 ] ) -> Vec < u8 > {
151- self . srtcp_session_auth . reset ( ) ;
146+ fn generate_srtcp_auth_tag ( & self , buf : & [ u8 ] ) -> Vec < u8 > {
147+ let mut signer = self . srtcp_session_auth . clone ( ) ;
152148
153- self . srtcp_session_auth . update ( buf) ;
149+ signer . update ( buf) ;
154150
155- let result = self . srtcp_session_auth . clone ( ) . finalize ( ) ;
156- let code_bytes = result. into_bytes ( ) ;
151+ let code_bytes = signer. finalize ( ) . into_bytes ( ) ;
157152
158153 // Truncate the hash to the first AUTH_TAG_SIZE bytes.
159154 code_bytes[ 0 ..self . auth_tag_len ( ) ] . to_vec ( )
@@ -179,26 +174,26 @@ impl Cipher for CipherAesCmHmacSha1 {
179174 ) -> Result < Bytes > {
180175 let header_len = header. marshal_size ( ) ;
181176 let mut writer =
182- BytesMut :: with_capacity ( header_len + payload. len ( ) + self . auth_tag_len ( ) ) ;
177+ Vec :: with_capacity ( payload. len ( ) + self . auth_tag_len ( ) ) ;
183178
184179 // Copy the header unencrypted.
185- writer. extend ( header. marshal ( ) ) ;
180+ writer. extend_from_slice ( & payload[ ..header_len] ) ;
181+
186182 // Encrypt the payload
187183 let nonce = generate_counter (
188184 header. sequence_number ,
189185 roc,
190186 header. ssrc ,
191187 & self . srtp_session_salt ,
192188 ) ;
193-
194- writer. put_bytes ( 0 , payload. len ( ) ) ;
189+ writer. resize ( payload. len ( ) , 0 ) ;
195190 self . ctx . encrypt_init ( None , None , Some ( & nonce) ) . unwrap ( ) ;
196- let count = self . ctx . cipher_update ( & payload, Some ( & mut writer[ header_len..] ) ) . unwrap ( ) ;
191+ let count = self . ctx . cipher_update ( & payload[ header_len.. ] , Some ( & mut writer[ header_len..] ) ) . unwrap ( ) ;
197192 self . ctx . cipher_final ( & mut writer[ count..] ) . unwrap ( ) ;
198193
199- // Generate the auth tag.
200- let auth_tag = self . generate_srtp_auth_tag ( & writer, roc) ;
201- writer. extend ( auth_tag) ;
194+ // Generate and write the auth tag.
195+ let auth_tag = & self . generate_srtp_auth_tag ( & writer, roc) [ .. self . auth_tag_len ( ) ] ;
196+ writer. extend_from_slice ( auth_tag) ;
202197
203198 Ok ( Bytes :: from ( writer) )
204199 }
@@ -220,11 +215,11 @@ impl Cipher for CipherAesCmHmacSha1 {
220215 let cipher_text = & encrypted[ ..encrypted. len ( ) - self . auth_tag_len ( ) ] ;
221216
222217 // Generate the auth tag we expect to see from the ciphertext.
223- let expected_tag = self . generate_srtp_auth_tag ( cipher_text, roc) ;
218+ let expected_tag = & self . generate_srtp_auth_tag ( cipher_text, roc) [ .. self . auth_tag_len ( ) ] ;
224219
225220 // See if the auth tag actually matches.
226221 // We use a constant time comparison to prevent timing attacks.
227- if actual_tag. ct_eq ( & expected_tag) . unwrap_u8 ( ) != 1 {
222+ if actual_tag. ct_eq ( expected_tag) . unwrap_u8 ( ) != 1 {
228223 return Err ( Error :: RtpFailedToVerifyAuthTag ) ;
229224 }
230225
0 commit comments