@@ -8,6 +8,7 @@ use util::marshal::*;
88use super :: { Cipher , CipherInner } ;
99use crate :: error:: { Error , Result } ;
1010use crate :: key_derivation:: * ;
11+ use crate :: protection_profile:: ProtectionProfile ;
1112
1213type Aes128Ctr = ctr:: Ctr128BE < aes:: Aes128 > ;
1314
@@ -18,8 +19,8 @@ pub(crate) struct CipherAesCmHmacSha1 {
1819}
1920
2021impl CipherAesCmHmacSha1 {
21- pub fn new ( master_key : & [ u8 ] , master_salt : & [ u8 ] ) -> Result < Self > {
22- let inner = CipherInner :: new ( master_key, master_salt) ?;
22+ pub fn new ( profile : ProtectionProfile , master_key : & [ u8 ] , master_salt : & [ u8 ] ) -> Result < Self > {
23+ let inner = CipherInner :: new ( profile , master_key, master_salt) ?;
2324
2425 let srtp_session_key = aes_cm_key_derivation (
2526 LABEL_SRTP_ENCRYPTION ,
@@ -45,8 +46,19 @@ impl CipherAesCmHmacSha1 {
4546}
4647
4748impl Cipher for CipherAesCmHmacSha1 {
48- fn auth_tag_len ( & self ) -> usize {
49- self . inner . auth_tag_len ( )
49+ /// Get RTP authenticated tag length.
50+ fn rtp_auth_tag_len ( & self ) -> usize {
51+ self . inner . profile . rtp_auth_tag_len ( )
52+ }
53+
54+ /// Get RTCP authenticated tag length.
55+ fn rtcp_auth_tag_len ( & self ) -> usize {
56+ self . inner . profile . rtcp_auth_tag_len ( )
57+ }
58+
59+ /// Get AEAD auth key length of the cipher.
60+ fn aead_auth_tag_len ( & self ) -> usize {
61+ self . inner . profile . aead_auth_tag_len ( )
5062 }
5163
5264 fn get_rtcp_index ( & self , input : & [ u8 ] ) -> usize {
@@ -59,7 +71,7 @@ impl Cipher for CipherAesCmHmacSha1 {
5971 header : & rtp:: header:: Header ,
6072 roc : u32 ,
6173 ) -> Result < Bytes > {
62- let mut writer = Vec :: with_capacity ( plaintext. len ( ) + self . auth_tag_len ( ) ) ;
74+ let mut writer = Vec :: with_capacity ( plaintext. len ( ) + self . rtp_auth_tag_len ( ) ) ;
6375
6476 // Write the plaintext to the destination buffer.
6577 writer. extend_from_slice ( plaintext) ;
@@ -77,7 +89,7 @@ impl Cipher for CipherAesCmHmacSha1 {
7789 stream. apply_keystream ( & mut writer[ header. marshal_size ( ) ..] ) ;
7890
7991 // Generate the auth tag.
80- let auth_tag = & self . inner . generate_srtp_auth_tag ( & writer, roc) [ ..self . auth_tag_len ( ) ] ;
92+ let auth_tag = & self . inner . generate_srtp_auth_tag ( & writer, roc) [ ..self . rtp_auth_tag_len ( ) ] ;
8193 writer. extend ( auth_tag) ;
8294
8395 Ok ( Bytes :: from ( writer) )
@@ -90,19 +102,19 @@ impl Cipher for CipherAesCmHmacSha1 {
90102 roc : u32 ,
91103 ) -> Result < Bytes > {
92104 let encrypted_len = encrypted. len ( ) ;
93- if encrypted_len < self . auth_tag_len ( ) {
94- return Err ( Error :: SrtpTooSmall ( encrypted_len, self . auth_tag_len ( ) ) ) ;
105+ if encrypted_len < self . rtp_auth_tag_len ( ) {
106+ return Err ( Error :: SrtpTooSmall ( encrypted_len, self . rtp_auth_tag_len ( ) ) ) ;
95107 }
96108
97- let mut writer = Vec :: with_capacity ( encrypted_len - self . auth_tag_len ( ) ) ;
109+ let mut writer = Vec :: with_capacity ( encrypted_len - self . rtp_auth_tag_len ( ) ) ;
98110
99111 // Split the auth tag and the cipher text into two parts.
100- let actual_tag = & encrypted[ encrypted_len - self . auth_tag_len ( ) ..] ;
101- let cipher_text = & encrypted[ ..encrypted_len - self . auth_tag_len ( ) ] ;
112+ let actual_tag = & encrypted[ encrypted_len - self . rtp_auth_tag_len ( ) ..] ;
113+ let cipher_text = & encrypted[ ..encrypted_len - self . rtp_auth_tag_len ( ) ] ;
102114
103115 // Generate the auth tag we expect to see from the ciphertext.
104116 let expected_tag =
105- & self . inner . generate_srtp_auth_tag ( cipher_text, roc) [ ..self . auth_tag_len ( ) ] ;
117+ & self . inner . generate_srtp_auth_tag ( cipher_text, roc) [ ..self . rtp_auth_tag_len ( ) ] ;
106118
107119 // See if the auth tag actually matches.
108120 // We use a constant time comparison to prevent timing attacks.
@@ -132,7 +144,7 @@ impl Cipher for CipherAesCmHmacSha1 {
132144
133145 fn encrypt_rtcp ( & mut self , decrypted : & [ u8 ] , srtcp_index : usize , ssrc : u32 ) -> Result < Bytes > {
134146 let mut writer =
135- Vec :: with_capacity ( decrypted. len ( ) + SRTCP_INDEX_SIZE + self . auth_tag_len ( ) ) ;
147+ Vec :: with_capacity ( decrypted. len ( ) + SRTCP_INDEX_SIZE + self . rtcp_auth_tag_len ( ) ) ;
136148
137149 // Write the decrypted to the destination buffer.
138150 writer. extend_from_slice ( decrypted) ;
@@ -155,22 +167,22 @@ impl Cipher for CipherAesCmHmacSha1 {
155167 writer. put_u32 ( srtcp_index as u32 | ( 1u32 << 31 ) ) ;
156168
157169 // Generate the auth tag.
158- let auth_tag = & self . inner . generate_srtcp_auth_tag ( & writer) [ ..self . auth_tag_len ( ) ] ;
170+ let auth_tag = & self . inner . generate_srtcp_auth_tag ( & writer) [ ..self . rtcp_auth_tag_len ( ) ] ;
159171 writer. extend ( auth_tag) ;
160172
161173 Ok ( Bytes :: from ( writer) )
162174 }
163175
164176 fn decrypt_rtcp ( & mut self , encrypted : & [ u8 ] , srtcp_index : usize , ssrc : u32 ) -> Result < Bytes > {
165177 let encrypted_len = encrypted. len ( ) ;
166- if encrypted_len < self . auth_tag_len ( ) + SRTCP_INDEX_SIZE {
178+ if encrypted_len < self . rtcp_auth_tag_len ( ) + SRTCP_INDEX_SIZE {
167179 return Err ( Error :: SrtcpTooSmall (
168180 encrypted_len,
169- self . auth_tag_len ( ) + SRTCP_INDEX_SIZE ,
181+ self . rtcp_auth_tag_len ( ) + SRTCP_INDEX_SIZE ,
170182 ) ) ;
171183 }
172184
173- let tail_offset = encrypted_len - ( self . auth_tag_len ( ) + SRTCP_INDEX_SIZE ) ;
185+ let tail_offset = encrypted_len - ( self . rtcp_auth_tag_len ( ) + SRTCP_INDEX_SIZE ) ;
174186
175187 let mut writer = Vec :: with_capacity ( tail_offset) ;
176188
@@ -182,18 +194,18 @@ impl Cipher for CipherAesCmHmacSha1 {
182194 }
183195
184196 // Split the auth tag and the cipher text into two parts.
185- let actual_tag = & encrypted[ encrypted_len - self . auth_tag_len ( ) ..] ;
186- if actual_tag. len ( ) != self . auth_tag_len ( ) {
197+ let actual_tag = & encrypted[ encrypted_len - self . rtcp_auth_tag_len ( ) ..] ;
198+ if actual_tag. len ( ) != self . rtcp_auth_tag_len ( ) {
187199 return Err ( Error :: RtcpInvalidLengthAuthTag (
188200 actual_tag. len ( ) ,
189- self . auth_tag_len ( ) ,
201+ self . rtcp_auth_tag_len ( ) ,
190202 ) ) ;
191203 }
192204
193- let cipher_text = & encrypted[ ..encrypted_len - self . auth_tag_len ( ) ] ;
205+ let cipher_text = & encrypted[ ..encrypted_len - self . rtcp_auth_tag_len ( ) ] ;
194206
195207 // Generate the auth tag we expect to see from the ciphertext.
196- let expected_tag = & self . inner . generate_srtcp_auth_tag ( cipher_text) [ ..self . auth_tag_len ( ) ] ;
208+ let expected_tag = & self . inner . generate_srtcp_auth_tag ( cipher_text) [ ..self . rtcp_auth_tag_len ( ) ] ;
197209
198210 // See if the auth tag actually matches.
199211 // We use a constant time comparison to prevent timing attacks.
0 commit comments