Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
use buffer pool for envelope signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
yusefnapora committed Nov 15, 2019
1 parent 0adf617 commit 1a01f08
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
49 changes: 38 additions & 11 deletions crypto/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"errors"
"github.com/golang/protobuf/proto"
"github.com/libp2p/go-buffer-pool"
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
)

Expand Down Expand Up @@ -42,7 +43,10 @@ func MakeEnvelope(privateKey PrivKey, domain string, payloadType []byte, payload
if len(domain) == 0 {
return nil, errEmptyDomain
}
toSign := makeSigBuffer(domain, payloadType, payload)
toSign, err := makeSigBuffer(domain, payloadType, payload)
if err != nil {
return nil, err
}
sig, err := privateKey.Sign(toSign)
if err != nil {
return nil, err
Expand Down Expand Up @@ -129,7 +133,10 @@ func (e *SignedEnvelope) Equals(other *SignedEnvelope) bool {
// validate returns true if the envelope signature is valid for the given 'domain',
// or false if it is invalid. May return an error if signature validation fails.
func (e *SignedEnvelope) validate(domain string) error {
toVerify := makeSigBuffer(domain, e.payloadType, e.payload)
toVerify, err := makeSigBuffer(domain, e.payloadType, e.payload)
if err != nil {
return err
}
valid, err := e.publicKey.Verify(toVerify, e.signature)
if err != nil {
return err
Expand All @@ -141,16 +148,36 @@ func (e *SignedEnvelope) validate(domain string) error {
}

// makeSigBuffer is a helper function that prepares a buffer to sign or verify.
func makeSigBuffer(domain string, typeHint []byte, content []byte) []byte {
b := bytes.Buffer{}
func makeSigBuffer(domain string, payloadType []byte, payload []byte) ([]byte, error) {
domainBytes := []byte(domain)
b.Write(encodedSize(domainBytes))
b.Write(domainBytes)
b.Write(encodedSize(typeHint))
b.Write(typeHint)
b.Write(encodedSize(content))
b.Write(content)
return b.Bytes()
fields := [][]byte{domainBytes, payloadType, payload}

const lengthPrefixSize = 8
size := 0
for _, f := range fields {
size += len(f) + lengthPrefixSize
}

b := pool.NewBuffer(nil)
b.Grow(size)

for _, f := range fields {
err := writeField(b, f)
if err != nil {
return nil, err
}
}

return b.Bytes(), nil
}

func writeField(b *pool.Buffer, f []byte) error {
_, err := b.Write(encodedSize(f))
if err != nil {
return err
}
_, err = b.Write(f)
return err
}

func encodedSize(content []byte) []byte {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/golang/protobuf v1.3.1
github.com/ipfs/go-cid v0.0.3
github.com/jbenet/goprocess v0.1.3
github.com/libp2p/go-buffer-pool v0.0.1
github.com/libp2p/go-flow-metrics v0.0.2
github.com/libp2p/go-msgio v0.0.4
github.com/libp2p/go-openssl v0.0.3
Expand Down

0 comments on commit 1a01f08

Please sign in to comment.