@@ -25,8 +25,9 @@ type srtpCipherAeadAesGcm struct {
2525
2626 useCryptex bool
2727
28- // Pre-allocated buffer for IV to avoid heap allocation in hot path
29- rtpIV [12 ]byte
28+ // Pre-allocated buffers for IV to avoid heap allocation in hot path
29+ rtpIV [12 ]byte
30+ rtcpIV [12 ]byte
3031}
3132
3233func newSrtpCipherAeadAesGcm (
@@ -258,7 +259,7 @@ func (s *srtpCipherAeadAesGcm) encryptRTCP(dst, decrypted []byte, srtcpIndex uin
258259 dst = growBufferSize (dst , aadPos + srtcpIndexSize + len (s .mki ))
259260 sameBuffer := isSameBuffer (dst , decrypted )
260261
261- iv := s .rtcpInitializationVector (srtcpIndex , ssrc )
262+ s .rtcpInitializationVector (srtcpIndex , ssrc )
262263 if s .srtcpEncrypted {
263264 aad := s .rtcpAdditionalAuthenticatedData (decrypted , srtcpIndex )
264265 if ! sameBuffer {
@@ -267,7 +268,7 @@ func (s *srtpCipherAeadAesGcm) encryptRTCP(dst, decrypted []byte, srtcpIndex uin
267268 }
268269 // Copy index to the proper place.
269270 copy (dst [aadPos :aadPos + srtcpIndexSize ], aad [8 :12 ])
270- s .srtcpCipher .Seal (dst [srtcpHeaderSize :srtcpHeaderSize ], iv [:], decrypted [srtcpHeaderSize :], aad [:])
271+ s .srtcpCipher .Seal (dst [srtcpHeaderSize :srtcpHeaderSize ], s . rtcpIV [:], decrypted [srtcpHeaderSize :], aad [:])
271272 } else {
272273 // Copy the packet unencrypted.
273274 if ! sameBuffer {
@@ -277,7 +278,7 @@ func (s *srtpCipherAeadAesGcm) encryptRTCP(dst, decrypted []byte, srtcpIndex uin
277278 binary .BigEndian .PutUint32 (dst [len (decrypted ):], srtcpIndex )
278279 // Generate the authentication tag.
279280 tag := make ([]byte , authTagLen )
280- s .srtcpCipher .Seal (tag [0 :0 ], iv [:], nil , dst [:len (decrypted )+ srtcpIndexSize ])
281+ s .srtcpCipher .Seal (tag [0 :0 ], s . rtcpIV [:], nil , dst [:len (decrypted )+ srtcpIndexSize ])
281282 // Copy index to the proper place.
282283 copy (dst [aadPos :], dst [len (decrypted ):len (decrypted )+ srtcpIndexSize ])
283284 // Copy the auth tag after RTCP payload.
@@ -305,10 +306,10 @@ func (s *srtpCipherAeadAesGcm) decryptRTCP(dst, encrypted []byte, srtcpIndex, ss
305306 sameBuffer := isSameBuffer (dst , encrypted )
306307
307308 isEncrypted := encrypted [aadPos ]& srtcpEncryptionFlag != 0
308- iv := s .rtcpInitializationVector (srtcpIndex , ssrc )
309+ s .rtcpInitializationVector (srtcpIndex , ssrc )
309310 if isEncrypted {
310311 aad := s .rtcpAdditionalAuthenticatedData (encrypted , srtcpIndex )
311- if _ , err := s .srtcpCipher .Open (dst [srtcpHeaderSize :srtcpHeaderSize ], iv [:], encrypted [srtcpHeaderSize :aadPos ],
312+ if _ , err := s .srtcpCipher .Open (dst [srtcpHeaderSize :srtcpHeaderSize ], s . rtcpIV [:], encrypted [srtcpHeaderSize :aadPos ],
312313 aad [:]); err != nil {
313314 return nil , fmt .Errorf ("%w: %w" , ErrFailedToVerifyAuthTag , err )
314315 }
@@ -319,7 +320,7 @@ func (s *srtpCipherAeadAesGcm) decryptRTCP(dst, encrypted []byte, srtcpIndex, ss
319320 copy (aad , encrypted [:dataEnd ])
320321 copy (aad [dataEnd :], encrypted [aadPos :aadPos + 4 ])
321322 // Verify the auth tag.
322- if _ , err := s .srtcpCipher .Open (nil , iv [:], encrypted [dataEnd :aadPos ], aad ); err != nil {
323+ if _ , err := s .srtcpCipher .Open (nil , s . rtcpIV [:], encrypted [dataEnd :aadPos ], aad ); err != nil {
323324 return nil , fmt .Errorf ("%w: %w" , ErrFailedToVerifyAuthTag , err )
324325 }
325326 // Copy the unencrypted payload.
@@ -359,17 +360,13 @@ func (s *srtpCipherAeadAesGcm) rtpInitializationVector(header *rtp.Header, roc u
359360// form the 12-octet IV.
360361//
361362// https://tools.ietf.org/html/rfc7714#section-9.1
362- func (s * srtpCipherAeadAesGcm ) rtcpInitializationVector (srtcpIndex uint32 , ssrc uint32 ) [12 ]byte {
363- var iv [12 ]byte
363+ func (s * srtpCipherAeadAesGcm ) rtcpInitializationVector (srtcpIndex uint32 , ssrc uint32 ) {
364+ binary .BigEndian .PutUint32 (s .rtcpIV [2 :], ssrc )
365+ binary .BigEndian .PutUint32 (s .rtcpIV [8 :], srtcpIndex )
364366
365- binary .BigEndian .PutUint32 (iv [2 :], ssrc )
366- binary .BigEndian .PutUint32 (iv [8 :], srtcpIndex )
367-
368- for i := range iv {
369- iv [i ] ^= s .srtcpSessionSalt [i ]
367+ for i := range s .rtcpIV {
368+ s .rtcpIV [i ] ^= s .srtcpSessionSalt [i ]
370369 }
371-
372- return iv
373370}
374371
375372// In an SRTCP packet, a 1-bit Encryption flag is prepended to the
0 commit comments