/
srtp.go
107 lines (84 loc) · 3.38 KB
/
srtp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Package srtp implements Secure Real-time Transport Protocol
package srtp
import (
"crypto/cipher"
"crypto/subtle"
"fmt"
"github.com/pion/rtp"
)
func (c *Context) decryptRTP(dst []byte, ciphertext []byte, header *rtp.Header) ([]byte, error) {
dst = growBufferSize(dst, len(ciphertext)-authTagSize)
s := c.getSSRCState(header.SSRC)
c.updateRolloverCount(header.SequenceNumber, s)
// Split the auth tag and the cipher text into two parts.
actualTag := ciphertext[len(ciphertext)-authTagSize:]
ciphertext = ciphertext[:len(ciphertext)-authTagSize]
// Generate the auth tag we expect to see from the ciphertext.
expectedTag, err := c.generateSrtpAuthTag(ciphertext, s.rolloverCounter)
if err != nil {
return nil, err
}
// See if the auth tag actually matches.
// We use a constant time comparison to prevent timing attacks.
if subtle.ConstantTimeCompare(actualTag, expectedTag) != 1 {
return nil, fmt.Errorf("failed to verify auth tag")
}
// Write the plaintext header to the destination buffer.
copy(dst, ciphertext[:header.PayloadOffset])
// Decrypt the ciphertext for the payload.
counter := c.generateCounter(header.SequenceNumber, s.rolloverCounter, s.ssrc, c.srtpSessionSalt)
stream := cipher.NewCTR(c.srtpBlock, counter)
stream.XORKeyStream(dst[header.PayloadOffset:], ciphertext[header.PayloadOffset:])
return dst, nil
}
// DecryptRTP decrypts a RTP packet with an encrypted payload
func (c *Context) DecryptRTP(dst, encrypted []byte, header *rtp.Header) ([]byte, error) {
if header == nil {
header = &rtp.Header{}
}
if err := header.Unmarshal(encrypted); err != nil {
return nil, err
}
return c.decryptRTP(dst, encrypted, header)
}
// EncryptRTP marshals and encrypts an RTP packet, writing to the dst buffer provided.
// If the dst buffer does not have the capacity to hold `len(plaintext) + 10` bytes, a new one will be allocated and returned.
// If a rtp.Header is provided, it will be Unmarshaled using the plaintext.
func (c *Context) EncryptRTP(dst []byte, plaintext []byte, header *rtp.Header) ([]byte, error) {
if header == nil {
header = &rtp.Header{}
}
err := header.Unmarshal(plaintext)
if err != nil {
return nil, err
}
return c.encryptRTP(dst, header, plaintext[header.PayloadOffset:])
}
// encryptRTP marshals and encrypts an RTP packet, writing to the dst buffer provided.
// If the dst buffer does not have the capacity to hold `len(plaintext) + 10` bytes, a new one will be allocated and returned.
// Similar to above but faster because it can avoid unmarshaling the header and marshaling the payload.
func (c *Context) encryptRTP(dst []byte, header *rtp.Header, payload []byte) (ciphertext []byte, err error) {
// Grow the given buffer to fit the output.
// authTag = 10 bytes
dst = growBufferSize(dst, header.MarshalSize()+len(payload)+10)
s := c.getSSRCState(header.SSRC)
c.updateRolloverCount(header.SequenceNumber, s)
// Copy the header unencrypted.
n, err := header.MarshalTo(dst)
if err != nil {
return nil, err
}
// Encrypt the payload
counter := c.generateCounter(header.SequenceNumber, s.rolloverCounter, s.ssrc, c.srtpSessionSalt)
stream := cipher.NewCTR(c.srtpBlock, counter)
stream.XORKeyStream(dst[n:], payload)
n += len(payload)
// Generate the auth tag.
authTag, err := c.generateSrtpAuthTag(dst[:n], s.rolloverCounter)
if err != nil {
return nil, err
}
// Write the auth tag to the dest.
copy(dst[n:], authTag)
return dst, nil
}