Skip to content

Commit e3e6d11

Browse files
committed
Make ProtectionProfile methods public
This allows to get values like key length and use them with key management protocols like MIKEY, instead of creating own constants for this. Resolves #258
1 parent e9fc319 commit e3e6d11

15 files changed

+56
-49
lines changed

context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ type Context struct {
6666
//
6767
// decCtx, err := srtp.CreateContext(key, salt, profile, srtp.SRTPReplayProtection(256))
6868
func CreateContext(masterKey, masterSalt []byte, profile ProtectionProfile, opts ...ContextOption) (c *Context, err error) {
69-
keyLen, err := profile.keyLen()
69+
keyLen, err := profile.KeyLen()
7070
if err != nil {
7171
return nil, err
7272
}
7373

74-
saltLen, err := profile.saltLen()
74+
saltLen, err := profile.SaltLen()
7575
if err != nil {
7676
return nil, err
7777
}

key_derivation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestValidSessionKeys(t *testing.T) {
3232
t.Errorf("Session Salt % 02x does not match expected % 02x", sessionSalt, expectedSessionSalt)
3333
}
3434

35-
authKeyLen, err := ProtectionProfileAes128CmHmacSha1_80.authKeyLen()
35+
authKeyLen, err := ProtectionProfileAes128CmHmacSha1_80.AuthKeyLen()
3636
assert.NoError(t, err)
3737

3838
sessionAuthTag, err := aesCmKeyDerivation(labelSRTPAuthenticationTag, masterKey, masterSalt, 0, authKeyLen)

keying.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ type KeyingMaterialExporter interface {
1414
// extracting them from DTLS. This behavior is defined in RFC5764:
1515
// https://tools.ietf.org/html/rfc5764
1616
func (c *Config) ExtractSessionKeysFromDTLS(exporter KeyingMaterialExporter, isClient bool) error {
17-
keyLen, err := c.Profile.keyLen()
17+
keyLen, err := c.Profile.KeyLen()
1818
if err != nil {
1919
return err
2020
}
2121

22-
saltLen, err := c.Profile.saltLen()
22+
saltLen, err := c.Profile.SaltLen()
2323
if err != nil {
2424
return err
2525
}

protection_profile.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const (
1717
ProtectionProfileAeadAes256Gcm ProtectionProfile = 0x0008
1818
)
1919

20-
func (p ProtectionProfile) keyLen() (int, error) {
20+
// KeyLen returns length of encryption key in bytes.
21+
func (p ProtectionProfile) KeyLen() (int, error) {
2122
switch p {
2223
case ProtectionProfileAes128CmHmacSha1_32, ProtectionProfileAes128CmHmacSha1_80, ProtectionProfileAeadAes128Gcm:
2324
return 16, nil
@@ -28,7 +29,8 @@ func (p ProtectionProfile) keyLen() (int, error) {
2829
}
2930
}
3031

31-
func (p ProtectionProfile) saltLen() (int, error) {
32+
// SaltLen returns length of salt key in bytes.
33+
func (p ProtectionProfile) SaltLen() (int, error) {
3234
switch p {
3335
case ProtectionProfileAes128CmHmacSha1_32, ProtectionProfileAes128CmHmacSha1_80:
3436
return 14, nil
@@ -39,7 +41,8 @@ func (p ProtectionProfile) saltLen() (int, error) {
3941
}
4042
}
4143

42-
func (p ProtectionProfile) rtpAuthTagLen() (int, error) {
44+
// AuthTagRTPLen returns length of RTP authentication tag in bytes for AES protection profiles. For AEAD ones it returns zero.
45+
func (p ProtectionProfile) AuthTagRTPLen() (int, error) {
4346
switch p {
4447
case ProtectionProfileAes128CmHmacSha1_80:
4548
return 10, nil
@@ -52,7 +55,8 @@ func (p ProtectionProfile) rtpAuthTagLen() (int, error) {
5255
}
5356
}
5457

55-
func (p ProtectionProfile) rtcpAuthTagLen() (int, error) {
58+
// AuthTagRTCPLen returns length of RTCP authentication tag in bytes for AES protection profiles. For AEAD ones it returns zero.
59+
func (p ProtectionProfile) AuthTagRTCPLen() (int, error) {
5660
switch p {
5761
case ProtectionProfileAes128CmHmacSha1_32, ProtectionProfileAes128CmHmacSha1_80:
5862
return 10, nil
@@ -63,7 +67,8 @@ func (p ProtectionProfile) rtcpAuthTagLen() (int, error) {
6367
}
6468
}
6569

66-
func (p ProtectionProfile) aeadAuthTagLen() (int, error) {
70+
// AEADAuthTagLen returns length of authentication tag in bytes for AEAD protection profiles. For AES ones it returns zero.
71+
func (p ProtectionProfile) AEADAuthTagLen() (int, error) {
6772
switch p {
6873
case ProtectionProfileAes128CmHmacSha1_32, ProtectionProfileAes128CmHmacSha1_80:
6974
return 0, nil
@@ -74,7 +79,8 @@ func (p ProtectionProfile) aeadAuthTagLen() (int, error) {
7479
}
7580
}
7681

77-
func (p ProtectionProfile) authKeyLen() (int, error) {
82+
// AuthKeyLen returns length of authentication key in bytes for AES protection profiles. For AEAD ones it returns zero.
83+
func (p ProtectionProfile) AuthKeyLen() (int, error) {
7884
switch p {
7985
case ProtectionProfileAes128CmHmacSha1_32, ProtectionProfileAes128CmHmacSha1_80:
8086
return 20, nil
@@ -85,6 +91,7 @@ func (p ProtectionProfile) authKeyLen() (int, error) {
8591
}
8692
}
8793

94+
// String returns the name of the protection profile.
8895
func (p ProtectionProfile) String() string {
8996
switch p {
9097
case ProtectionProfileAes128CmHmacSha1_80:

protection_profile_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
func TestInvalidProtectionProfile(t *testing.T) {
1313
var invalidProtectionProfile ProtectionProfile
1414

15-
_, err := invalidProtectionProfile.keyLen()
15+
_, err := invalidProtectionProfile.KeyLen()
1616
assert.Error(t, err)
1717

18-
_, err = invalidProtectionProfile.saltLen()
18+
_, err = invalidProtectionProfile.SaltLen()
1919
assert.Error(t, err)
2020
}

session_srtcp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func TestSessionSRTCPAcceptStreamTimeout(t *testing.T) {
341341
}
342342

343343
func getSenderSSRC(t *testing.T, stream *ReadStreamSRTCP) (ssrc uint32, err error) {
344-
authTagSize, err := ProtectionProfileAes128CmHmacSha1_80.rtcpAuthTagLen()
344+
authTagSize, err := ProtectionProfileAes128CmHmacSha1_80.AuthTagRTCPLen()
345345
if err != nil {
346346
return 0, err
347347
}

srtcp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ const maxSRTCPIndex = 0x7FFFFFFF
1515
func (c *Context) decryptRTCP(dst, encrypted []byte) ([]byte, error) {
1616
out := allocateIfMismatch(dst, encrypted)
1717

18-
authTagLen, err := c.cipher.rtcpAuthTagLen()
18+
authTagLen, err := c.cipher.AuthTagRTCPLen()
1919
if err != nil {
2020
return nil, err
2121
}
22-
aeadAuthTagLen, err := c.cipher.aeadAuthTagLen()
22+
aeadAuthTagLen, err := c.cipher.AEADAuthTagLen()
2323
if err != nil {
2424
return nil, err
2525
}

srtcp_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ func TestRTCPLifecycleInPlace(t *testing.T) {
164164
testCase := testCase
165165
t.Run(caseName, func(t *testing.T) {
166166
assert := assert.New(t)
167-
authTagLen, err := testCase.algo.rtcpAuthTagLen()
167+
authTagLen, err := testCase.algo.AuthTagRTCPLen()
168168
assert.NoError(err)
169169

170-
aeadAuthTagLen, err := testCase.algo.aeadAuthTagLen()
170+
aeadAuthTagLen, err := testCase.algo.AEADAuthTagLen()
171171
assert.NoError(err)
172172

173173
encryptHeader := &rtcp.Header{}
@@ -272,10 +272,10 @@ func TestRTCPInvalidAuthTag(t *testing.T) {
272272
testCase := testCase
273273
t.Run(caseName, func(t *testing.T) {
274274
assert := assert.New(t)
275-
authTagLen, err := testCase.algo.rtcpAuthTagLen()
275+
authTagLen, err := testCase.algo.AuthTagRTCPLen()
276276
assert.NoError(err)
277277

278-
aeadAuthTagLen, err := testCase.algo.aeadAuthTagLen()
278+
aeadAuthTagLen, err := testCase.algo.AEADAuthTagLen()
279279
assert.NoError(err)
280280

281281
decryptContext, err := CreateContext(testCase.masterKey, testCase.masterSalt, testCase.algo)
@@ -354,7 +354,7 @@ func TestEncryptRTCPSeparation(t *testing.T) {
354354
encryptContext, err := CreateContext(testCase.masterKey, testCase.masterSalt, testCase.algo)
355355
assert.NoError(err)
356356

357-
authTagLen, err := testCase.algo.rtcpAuthTagLen()
357+
authTagLen, err := testCase.algo.AuthTagRTCPLen()
358358
assert.NoError(err)
359359

360360
decryptContext, err := CreateContext(

srtp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func (c *Context) decryptRTP(dst, ciphertext []byte, header *rtp.Header, headerLen int) ([]byte, error) {
12-
authTagLen, err := c.cipher.rtpAuthTagLen()
12+
authTagLen, err := c.cipher.AuthTagRTPLen()
1313
if err != nil {
1414
return nil, err
1515
}

srtp_cipher.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import "github.com/pion/rtp"
88
// cipher represents a implementation of one
99
// of the SRTP Specific ciphers
1010
type srtpCipher interface {
11-
// authTagLen returns auth key length of the cipher.
11+
// AuthTagRTPLen/AuthTagRTCPLen return auth key length of the cipher.
1212
// See the note below.
13-
rtpAuthTagLen() (int, error)
14-
rtcpAuthTagLen() (int, error)
15-
// aeadAuthTagLen returns AEAD auth key length of the cipher.
13+
AuthTagRTPLen() (int, error)
14+
AuthTagRTCPLen() (int, error)
15+
// AEADAuthTagLen returns AEAD auth key length of the cipher.
1616
// See the note below.
17-
aeadAuthTagLen() (int, error)
17+
AEADAuthTagLen() (int, error)
1818
getRTCPIndex([]byte) uint32
1919

2020
encryptRTP([]byte, *rtp.Header, []byte, uint32) ([]byte, error)

0 commit comments

Comments
 (0)