Skip to content

Commit

Permalink
Remove pkg/errors dependency
Browse files Browse the repository at this point in the history
Everything we used it for is provided by stdlib
  • Loading branch information
Sean-Der committed Aug 24, 2019
1 parent 0741c01 commit 131c412
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 25 deletions.
7 changes: 3 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"crypto/hmac"
"crypto/sha1" // #nosec
"encoding/binary"
"fmt"
"hash"

"github.com/pkg/errors"
)

// ProtectionProfile specifies Cipher and AuthTag details, similar to TLS cipher suite
Expand Down Expand Up @@ -71,9 +70,9 @@ type Context struct {
// CreateContext creates a new SRTP Context
func CreateContext(masterKey, masterSalt []byte, profile ProtectionProfile) (c *Context, err error) {
if masterKeyLen := len(masterKey); masterKeyLen != keyLen {
return c, errors.Errorf("SRTP Master Key must be len %d, got %d", masterKey, keyLen)
return c, fmt.Errorf("SRTP Master Key must be len %d, got %d", masterKey, keyLen)
} else if masterSaltLen := len(masterSalt); masterSaltLen != saltLen {
return c, errors.Errorf("SRTP Salt must be len %d, got %d", saltLen, masterSaltLen)
return c, fmt.Errorf("SRTP Salt must be len %d, got %d", saltLen, masterSaltLen)
}

c = &Context{
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ require (
github.com/pion/rtcp v1.2.1
github.com/pion/rtp v1.1.3
github.com/pion/transport v0.6.0
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ github.com/pion/rtp v1.1.3 h1:GTYSTsSLF5vH+UqShGYQEBdoYasWjTTC9UeYglnUO+o=
github.com/pion/rtp v1.1.3/go.mod h1:/l4cvcKd0D3u9JLs2xSVI95YkfXW87a3br3nqmVtSlE=
github.com/pion/transport v0.6.0 h1:WAoyJg/6OI8dhCVFl/0JHTMd1iu2iHgGUXevptMtJ3U=
github.com/pion/transport v0.6.0/go.mod h1:iWZ07doqOosSLMhZ+FXUTq+TamDoXSllxpbGcfkCmbE=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
13 changes: 6 additions & 7 deletions srtcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/pion/rtcp"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

Expand All @@ -21,12 +20,12 @@ func TestRTCPLifecycle(t *testing.T) {
assert := assert.New(t)
encryptContext, err := CreateContext(rtcpTestMasterKey, rtcpTestMasterSalt, cipherContextAlgo)
if err != nil {
t.Error(errors.Wrap(err, "CreateContext failed"))
t.Errorf("CreateContext failed: %v", err)
}

decryptContext, err := CreateContext(rtcpTestMasterKey, rtcpTestMasterSalt, cipherContextAlgo)
if err != nil {
t.Error(errors.Wrap(err, "CreateContext failed"))
t.Errorf("CreateContext failed: %v", err)
}

decryptResult, err := decryptContext.DecryptRTCP(nil, rtcpTestEncrypted, nil)
Expand All @@ -48,13 +47,13 @@ func TestRTCPLifecycleInPlace(t *testing.T) {
encryptHeader := &rtcp.Header{}
encryptContext, err := CreateContext(rtcpTestMasterKey, rtcpTestMasterSalt, cipherContextAlgo)
if err != nil {
t.Error(errors.Wrap(err, "CreateContext failed"))
t.Errorf("CreateContext failed: %v", err)
}

decryptHeader := &rtcp.Header{}
decryptContext, err := CreateContext(rtcpTestMasterKey, rtcpTestMasterSalt, cipherContextAlgo)
if err != nil {
t.Error(errors.Wrap(err, "CreateContext failed"))
t.Errorf("CreateContext failed: %v", err)
}

// Copy packet, asserts that everything was done in place
Expand Down Expand Up @@ -93,13 +92,13 @@ func TestRTCPLifecyclePartialAllocation(t *testing.T) {
encryptHeader := &rtcp.Header{}
encryptContext, err := CreateContext(rtcpTestMasterKey, rtcpTestMasterSalt, cipherContextAlgo)
if err != nil {
t.Error(errors.Wrap(err, "CreateContext failed"))
t.Errorf("CreateContext failed: %v", err)
}

decryptHeader := &rtcp.Header{}
decryptContext, err := CreateContext(rtcpTestMasterKey, rtcpTestMasterSalt, cipherContextAlgo)
if err != nil {
t.Error(errors.Wrap(err, "CreateContext failed"))
t.Errorf("CreateContext failed: %v", err)
}

// Copy packet, asserts that partial buffers can be used
Expand Down
4 changes: 2 additions & 2 deletions srtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package srtp
import (
"crypto/cipher"
"crypto/subtle"
"fmt"

"github.com/pion/rtp"
"github.com/pkg/errors"
)

func (c *Context) decryptRTP(dst []byte, ciphertext []byte, header *rtp.Header) ([]byte, error) {
Expand All @@ -27,7 +27,7 @@ func (c *Context) decryptRTP(dst []byte, ciphertext []byte, header *rtp.Header)
// 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, errors.Errorf("failed to verify auth tag")
return nil, fmt.Errorf("failed to verify auth tag")
}

// Write the plaintext header to the destination buffer.
Expand Down
18 changes: 9 additions & 9 deletions srtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/pion/rtp"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

Expand All @@ -27,7 +26,7 @@ func TestKeyLen(t *testing.T) {
}

if _, err := CreateContext(make([]byte, keyLen), make([]byte, saltLen), cipherContextAlgo); err != nil {
t.Error(errors.Wrap(err, "CreateContext failed with a valid length key and salt"))
t.Errorf("CreateContext failed with a valid length key and salt: %v", err)
}
}

Expand All @@ -41,26 +40,27 @@ func TestValidSessionKeys(t *testing.T) {

c, err := CreateContext(masterKey, masterSalt, cipherContextAlgo)
if err != nil {
t.Error(errors.Wrap(err, "CreateContext failed"))
t.Errorf("CreateContext failed: %v", err)
}

sessionKey, err := c.generateSessionKey(labelSRTPEncryption)
if err != nil {
t.Error(errors.Wrap(err, "generateSessionKey failed"))
t.Errorf("generateSessionKey failed: %v", err)

} else if !bytes.Equal(sessionKey, expectedSessionKey) {
t.Errorf("Session Key % 02x does not match expected % 02x", sessionKey, expectedSessionKey)
}

sessionSalt, err := c.generateSessionSalt(labelSRTPSalt)
if err != nil {
t.Error(errors.Wrap(err, "generateSessionSalt failed"))
t.Errorf("generateSessionSalt failed: %v", err)
} else if !bytes.Equal(sessionSalt, expectedSessionSalt) {
t.Errorf("Session Salt % 02x does not match expected % 02x", sessionSalt, expectedSessionSalt)
}

sessionAuthTag, err := c.generateSessionAuthTag(labelSRTPAuthenticationTag)
if err != nil {
t.Error(errors.Wrap(err, "generateSessionAuthTag failed"))
t.Errorf("generateSessionAuthTag failed: %v", err)
} else if !bytes.Equal(sessionAuthTag, expectedSessionAuthTag) {
t.Errorf("Session Auth Tag % 02x does not match expected % 02x", sessionAuthTag, expectedSessionAuthTag)
}
Expand All @@ -73,7 +73,7 @@ func TestValidPacketCounter(t *testing.T) {

c, err := CreateContext(masterKey, masterSalt, cipherContextAlgo)
if err != nil {
t.Error(errors.Wrap(err, "CreateContext failed"))
t.Errorf("CreateContext failed: %v", err)
}

s := &ssrcState{ssrc: 4160032510}
Expand All @@ -90,7 +90,7 @@ func TestRolloverCount(t *testing.T) {

c, err := CreateContext(masterKey, masterSalt, cipherContextAlgo)
if err != nil {
t.Error(errors.Wrap(err, "CreateContext failed"))
t.Errorf("CreateContext failed: %v", err)
}

s := &ssrcState{ssrc: defaultSsrc}
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestRTPInvalidAuth(t *testing.T) {

invalidContext, err := CreateContext(masterKey, invalidSalt, cipherContextAlgo)
if err != nil {
t.Fatal(errors.Wrap(err, "CreateContext failed"))
t.Errorf("CreateContext failed: %v", err)
}

for _, testCase := range rtpTestCases {
Expand Down

0 comments on commit 131c412

Please sign in to comment.