Skip to content

Commit

Permalink
Use speedy XOR from pion/transport
Browse files Browse the repository at this point in the history
Now that we have a speedy version of xorBytes in pion/transport,
we might as well use it.  Benchmarks indicate an improvement
of 10% to 20% in CTR encryption speed for large blocks, depending
on the architecture.

Also remove TestXorBytesBuffer, which is no longer relevant.
  • Loading branch information
jech authored and adriancable committed Jun 22, 2022
1 parent 88859d2 commit 497faca
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 47 deletions.
22 changes: 3 additions & 19 deletions crypto.go
Expand Up @@ -2,25 +2,9 @@ package srtp

import (
"crypto/cipher"
)

// xorBytes computes the exclusive-or of src1 and src2 and stores it in dst.
// It returns the number of bytes written.
func xorBytes(dst, src1, src2 []byte) int {
n := len(src1)
if len(src2) < n {
n = len(src2)
}
if len(dst) < n {
n = len(dst)
}

for i := 0; i < n; i++ {
dst[i] = src1[i] ^ src2[i]
}

return n
}
"github.com/pion/transport/utils/xor"
)

// incrementCTR increments a big-endian integer of arbitrary size.
func incrementCTR(ctr []byte) {
Expand Down Expand Up @@ -48,7 +32,7 @@ func xorBytesCTR(block cipher.Block, iv []byte, dst, src []byte) error {
for i < len(src) {
block.Encrypt(stream, ctr)
incrementCTR(ctr)
n := xorBytes(dst[i:], src[i:], stream)
n := xor.XorBytes(dst[i:], src[i:], stream)
if n == 0 {
break
}
Expand Down
19 changes: 0 additions & 19 deletions crypto_test.go
Expand Up @@ -62,22 +62,3 @@ func TestXorBytesCTRInvalidIvLength(t *testing.T) {
test(make([]byte, block.BlockSize()-1))
test(make([]byte, block.BlockSize()+1))
}

func TestXorBytesBufferSize(t *testing.T) {
a := []byte{3}
b := []byte{5, 6}
dst := make([]byte, 3)

xorBytes(dst, a, b)
require.Equal(t, dst, []byte{6, 0, 0})

xorBytes(dst, b, a)
require.Equal(t, dst, []byte{6, 0, 0})

a = []byte{1, 1, 1, 1}
b = []byte{2, 2, 2, 2}
dst = make([]byte, 3)

xorBytes(dst, a, b)
require.Equal(t, dst, []byte{3, 3, 3})
}
3 changes: 2 additions & 1 deletion go.mod
Expand Up @@ -6,6 +6,7 @@ require (
github.com/pion/logging v0.2.2
github.com/pion/rtcp v1.2.9
github.com/pion/rtp v1.7.13
github.com/pion/transport v0.13.0
github.com/pion/transport v0.13.1
github.com/stretchr/testify v1.7.1
golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664 // indirect
)
19 changes: 11 additions & 8 deletions go.sum
Expand Up @@ -8,20 +8,23 @@ github.com/pion/rtcp v1.2.9 h1:1ujStwg++IOLIEoOiIQ2s+qBuJ1VN81KW+9pMPsif+U=
github.com/pion/rtcp v1.2.9/go.mod h1:qVPhiCzAm4D/rxb6XzKeyZiQK69yJpbUDJSF7TgrqNo=
github.com/pion/rtp v1.7.13 h1:qcHwlmtiI50t1XivvoawdCGTP4Uiypzfrsap+bijcoA=
github.com/pion/rtp v1.7.13/go.mod h1:bDb5n+BFZxXx0Ea7E5qe+klMuqiBrP+w8XSjiWtCUko=
github.com/pion/transport v0.13.0 h1:KWTA5ZrQogizzYwPEciGtHPLwpAjE91FgXnyu+Hv2uY=
github.com/pion/transport v0.13.0/go.mod h1:yxm9uXpK9bpBBWkITk13cLo1y5/ur5VQpG22ny6EP7g=
github.com/pion/transport v0.13.1 h1:/UH5yLeQtwm2VZIPjxwnNFxjS4DFhyLfS4GlfuKUzfA=
github.com/pion/transport v0.13.1/go.mod h1:EBxbqzyv+ZrmDb82XswEE0BjfQFtuw1Nu6sjnjWCsGg=
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=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.0.0-20211201190559-0a0e4e1bb54c h1:WtYZ93XtWSO5KlOMgPZu7hXY9WhMZpprvlm5VwvAl8c=
golang.org/x/net v0.0.0-20211201190559-0a0e4e1bb54c/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/net v0.0.0-20220531201128-c960675eff93 h1:MYimHLfoXEpOhqd/zgoA/uoXzHB86AEky4LAx5ij9xA=
golang.org/x/net v0.0.0-20220531201128-c960675eff93/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220608164250-635b8c9b7f68/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664 h1:wEZYwx+kK+KlZ0hpvP2Ls1Xr4+RWnlzGFwPP0aiDjIU=
golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down

0 comments on commit 497faca

Please sign in to comment.