Skip to content

Commit

Permalink
noise: use a 2 kb buffer for all handshake operations
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Sep 15, 2022
1 parent d27a209 commit 4f8a466
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions p2p/security/noise/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"runtime/debug"
"time"

"golang.org/x/crypto/chacha20poly1305"

"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/security/noise/pb"
Expand Down Expand Up @@ -73,12 +71,8 @@ func (s *secureSession) runHandshake(ctx context.Context) (err error) {
}
}

// We can re-use this buffer for all handshake messages as its size
// 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() + 2*chacha20poly1305.Overhead + 1024 /* payload */
hbuf := pool.Get(maxMsgSize + LengthPrefixLength)
// We can re-use this buffer for all handshake messages.
hbuf := pool.Get(2 << 10)
defer pool.Put(hbuf)

if s.initiator {
Expand Down

0 comments on commit 4f8a466

Please sign in to comment.