Skip to content

Commit

Permalink
noise: use golang.org/x/crypto/chacha20poly1305 instead of golang.org…
Browse files Browse the repository at this point in the history
…/x/crypto/poly1305
  • Loading branch information
marten-seemann committed Apr 27, 2022
1 parent 9359057 commit 04b43c8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
4 changes: 2 additions & 2 deletions p2p/security/noise/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
"time"

"golang.org/x/crypto/poly1305"
"golang.org/x/crypto/chacha20poly1305"

"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/sec"
Expand Down Expand Up @@ -174,7 +174,7 @@ func benchDataTransfer(b *benchenv, dataSize int64, m testMode) {
plainTextBufs[i] = make([]byte, (i+2)*1024)
switch m {
case readBufferGtEncMsg:
rbuf = make([]byte, len(plainTextBufs[i])+poly1305.TagSize+1)
rbuf = make([]byte, len(plainTextBufs[i])+chacha20poly1305.Overhead+1)
case readBufferLtPlainText:
rbuf = make([]byte, len(plainTextBufs[i])-2)
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/security/noise/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"runtime/debug"
"time"

"golang.org/x/crypto/poly1305"
"golang.org/x/crypto/chacha20poly1305"

"github.com/libp2p/go-libp2p/p2p/security/noise/pb"

Expand Down Expand Up @@ -73,7 +73,7 @@ func (s *secureSession) runHandshake(ctx context.Context) (err error) {
// will be the size of the maximum handshake message for the Noise XX pattern.
// Also, since we prefix every noise handshake message with its length, we need to account for
// it when we fetch the buffer from the pool
maxMsgSize := 2*noise.DH25519.DHLen() + len(payload) + 2*poly1305.TagSize
maxMsgSize := 2*noise.DH25519.DHLen() + len(payload) + 2*chacha20poly1305.Overhead
hbuf := pool.Get(maxMsgSize + LengthPrefixLength)
defer pool.Put(hbuf)

Expand Down
7 changes: 3 additions & 4 deletions p2p/security/noise/rw.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"io"

pool "github.com/libp2p/go-buffer-pool"

"golang.org/x/crypto/poly1305"
"golang.org/x/crypto/chacha20poly1305"
)

// MaxTransportMsgLength is the Noise-imposed maximum transport message length,
Expand All @@ -15,7 +14,7 @@ const MaxTransportMsgLength = 0xffff

// MaxPlaintextLength is the maximum payload size. It is MaxTransportMsgLength
// minus the MAC size. Payloads over this size will be automatically chunked.
const MaxPlaintextLength = MaxTransportMsgLength - poly1305.TagSize
const MaxPlaintextLength = MaxTransportMsgLength - chacha20poly1305.Overhead

// LengthPrefixLength is the length of the length prefix itself, which precedes
// all transport messages in order to delimit them. In bytes.
Expand Down Expand Up @@ -100,7 +99,7 @@ func (s *secureSession) Write(data []byte) (int, error) {
)

if total < MaxPlaintextLength {
cbuf = pool.Get(total + poly1305.TagSize + LengthPrefixLength)
cbuf = pool.Get(total + chacha20poly1305.Overhead + LengthPrefixLength)
} else {
cbuf = pool.Get(MaxTransportMsgLength + LengthPrefixLength)
}
Expand Down
7 changes: 3 additions & 4 deletions p2p/security/noise/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"

"golang.org/x/crypto/poly1305"
"golang.org/x/crypto/chacha20poly1305"

"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/sec"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -302,7 +301,7 @@ func TestBufferEqEncPayload(t *testing.T) {
_, err := initConn.Write(before)
require.NoError(t, err)

after := make([]byte, len(before)+poly1305.TagSize)
after := make([]byte, len(before)+chacha20poly1305.Overhead)
afterLen, err := respConn.Read(after)
require.NoError(t, err)

Expand Down

0 comments on commit 04b43c8

Please sign in to comment.