@@ -32,7 +32,8 @@ pub(crate) struct CipherAesCmHmacSha1 {
32
32
srtcp_session_auth : HmacSha1 ,
33
33
//srtcp_session_auth_tag: Vec<u8>,
34
34
35
- ctx : CipherCtx
35
+ rtp_ctx : CipherCtx ,
36
+ rtcp_ctx : CipherCtx
36
37
}
37
38
38
39
impl CipherAesCmHmacSha1 {
@@ -90,8 +91,13 @@ impl CipherAesCmHmacSha1 {
90
91
. map_err ( |e| Error :: Other ( e. to_string ( ) ) ) ?;
91
92
92
93
let t = openssl:: cipher:: Cipher :: aes_128_ctr ( ) ;
93
- let mut ctx = CipherCtx :: new ( ) . expect ( "a reusable cipher context" ) ;
94
- ctx. encrypt_init ( Some ( t) , Some ( & srtp_session_key[ ..] ) , None )
94
+ let mut rtp_ctx = CipherCtx :: new ( ) . expect ( "a reusable cipher context" ) ;
95
+ rtp_ctx. encrypt_init ( Some ( t) , Some ( & srtp_session_key[ ..] ) , None )
96
+ . expect ( "enc init" ) ;
97
+
98
+ let t = openssl:: cipher:: Cipher :: aes_128_ctr ( ) ;
99
+ let mut rtcp_ctx = CipherCtx :: new ( ) . expect ( "a reusable cipher context" ) ;
100
+ rtcp_ctx. encrypt_init ( Some ( t) , Some ( & srtcp_session_key[ ..] ) , None )
95
101
. expect ( "enc init" ) ;
96
102
97
103
Ok ( CipherAesCmHmacSha1 {
@@ -103,7 +109,8 @@ impl CipherAesCmHmacSha1 {
103
109
srtcp_session_salt,
104
110
srtcp_session_auth,
105
111
//srtcp_session_auth_tag,
106
- ctx,
112
+ rtp_ctx,
113
+ rtcp_ctx
107
114
} )
108
115
}
109
116
@@ -187,9 +194,9 @@ impl Cipher for CipherAesCmHmacSha1 {
187
194
& self . srtp_session_salt ,
188
195
) ;
189
196
writer. resize ( payload. len ( ) , 0 ) ;
190
- self . ctx . encrypt_init ( None , None , Some ( & nonce) ) . unwrap ( ) ;
191
- let count = self . ctx . cipher_update ( & payload[ header_len..] , Some ( & mut writer[ header_len..] ) ) . unwrap ( ) ;
192
- self . ctx . cipher_final ( & mut writer[ count..] ) . unwrap ( ) ;
197
+ self . rtp_ctx . encrypt_init ( None , None , Some ( & nonce) ) . unwrap ( ) ;
198
+ let count = self . rtp_ctx . cipher_update ( & payload[ header_len..] , Some ( & mut writer[ header_len..] ) ) . unwrap ( ) ;
199
+ self . rtp_ctx . cipher_final ( & mut writer[ count..] ) . unwrap ( ) ;
193
200
194
201
// Generate and write the auth tag.
195
202
let auth_tag = & self . generate_srtp_auth_tag ( & writer, roc) [ ..self . auth_tag_len ( ) ] ;
@@ -236,9 +243,9 @@ impl Cipher for CipherAesCmHmacSha1 {
236
243
) ;
237
244
238
245
writer. put_bytes ( 0 , encrypted. len ( ) - header_len - self . auth_tag_len ( ) ) ;
239
- self . ctx . decrypt_init ( None , None , Some ( & nonce) ) . unwrap ( ) ;
240
- let count = self . ctx . cipher_update ( & cipher_text[ header_len..] , Some ( & mut writer[ header_len..] ) ) . unwrap ( ) ;
241
- self . ctx . cipher_final ( & mut writer[ count..] ) . unwrap ( ) ;
246
+ self . rtp_ctx . decrypt_init ( None , None , Some ( & nonce) ) . unwrap ( ) ;
247
+ let count = self . rtp_ctx . cipher_update ( & cipher_text[ header_len..] , Some ( & mut writer[ header_len..] ) ) . unwrap ( ) ;
248
+ self . rtp_ctx . cipher_final ( & mut writer[ count..] ) . unwrap ( ) ;
242
249
243
250
Ok ( writer. freeze ( ) )
244
251
}
@@ -248,23 +255,20 @@ impl Cipher for CipherAesCmHmacSha1 {
248
255
BytesMut :: with_capacity ( decrypted. len ( ) + SRTCP_INDEX_SIZE + self . auth_tag_len ( ) ) ;
249
256
250
257
// Write the decrypted to the destination buffer.
251
- writer. extend_from_slice ( decrypted) ;
258
+ writer. extend_from_slice ( & decrypted[ ..rtcp :: header :: HEADER_LENGTH + rtcp :: header :: SSRC_LENGTH ] ) ;
252
259
253
260
// Encrypt everything after header
254
- let counter = generate_counter (
261
+ let nonce = generate_counter (
255
262
( srtcp_index & 0xFFFF ) as u16 ,
256
263
( srtcp_index >> 16 ) as u32 ,
257
264
ssrc,
258
265
& self . srtcp_session_salt ,
259
266
) ;
260
267
261
- let key = GenericArray :: from_slice ( & self . srtcp_session_key ) ;
262
- let nonce = GenericArray :: from_slice ( & counter) ;
263
- let mut stream = Aes128Ctr :: new ( key, nonce) ;
264
-
265
- stream. apply_keystream (
266
- & mut writer[ rtcp:: header:: HEADER_LENGTH + rtcp:: header:: SSRC_LENGTH ..] ,
267
- ) ;
268
+ writer. resize ( decrypted. len ( ) - rtcp:: header:: HEADER_LENGTH - rtcp:: header:: SSRC_LENGTH , 0 ) ;
269
+ self . rtcp_ctx . encrypt_init ( None , None , Some ( & nonce) ) . unwrap ( ) ;
270
+ let count = self . rtcp_ctx . cipher_update ( & decrypted[ rtcp:: header:: HEADER_LENGTH + rtcp:: header:: SSRC_LENGTH ..] , Some ( & mut writer[ rtcp:: header:: HEADER_LENGTH + rtcp:: header:: SSRC_LENGTH ..] ) ) . unwrap ( ) ;
271
+ self . rtcp_ctx . cipher_final ( & mut writer[ rtcp:: header:: HEADER_LENGTH + rtcp:: header:: SSRC_LENGTH + count..] ) . unwrap ( ) ;
268
272
269
273
// Add SRTCP index and set Encryption bit
270
274
writer. put_u32 ( srtcp_index as u32 | ( 1u32 << 31 ) ) ;
0 commit comments