Skip to content
This repository was archived by the owner on May 26, 2022. It is now read-only.

Commit 69090b2

Browse files
authored
Merge pull request #58 from libp2p/raul-review
2 parents abd5989 + 1ad9313 commit 69090b2

File tree

13 files changed

+338
-309
lines changed

13 files changed

+338
-309
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
66
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
77
[![GoDoc](https://godoc.org/github.com/libp2p/go-libp2p-noise?status.svg)](https://godoc.org/github.com/libp2p/go-libp2p-noise)
8-
[![Build Status](https://travis-ci.org/libp2p/go-libp2p-noise.svg?branch=master)](https://travis-ci.org/libp2p/go-libp2p-noise)
8+
[![Build Status](https://travis-ci.com/libp2p/go-libp2p-noise.svg?branch=master)](https://travis-ci.com/libp2p/go-libp2p-noise)
99

1010
> go-libp2p's noise encrypted transport
1111

benchmark_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ func pipeRandom(src rand.Source, w io.WriteCloser, r io.Reader, n int64) error {
124124
func benchDataTransfer(b *benchenv, size int64) {
125125
var totalBytes int64
126126
var totalTime time.Duration
127+
128+
b.ResetTimer()
129+
b.ReportAllocs()
130+
127131
for i := 0; i < b.N; i++ {
128132
initSession, respSession := b.connect(true)
129133

@@ -153,6 +157,9 @@ func BenchmarkTransfer500Mb(b *testing.B) {
153157
}
154158

155159
func (b benchenv) benchHandshake() {
160+
b.ResetTimer()
161+
b.ReportAllocs()
162+
156163
for i := 0; i < b.N; i++ {
157164
i, r := b.connect(false)
158165
b.StopTimer()

crypto.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,46 @@
11
package noise
22

3-
import "errors"
3+
import (
4+
"errors"
5+
)
46

5-
func (s *secureSession) encrypt(plaintext []byte) (ciphertext []byte, err error) {
7+
// encrypt calls the cipher's encryption. It encrypts the provided plaintext,
8+
// slice-appending the ciphertext on out.
9+
//
10+
// Usually you want to pass a 0-len slice to this method, with enough capacity
11+
// to accommodate the ciphertext in order to spare allocs.
12+
//
13+
// encrypt returns a new slice header, whose len is the length of the resulting
14+
// ciphertext, including the authentication tag.
15+
//
16+
// This method will not allocate if the supplied slice is large enough to
17+
// accommodate the encrypted data + authentication tag. If so, the returned
18+
// slice header should be a view of the original slice.
19+
//
20+
// With the poly1305 MAC function that noise-libp2p uses, the authentication tag
21+
// adds an overhead of 16 bytes.
22+
func (s *secureSession) encrypt(out, plaintext []byte) ([]byte, error) {
623
if s.enc == nil {
724
return nil, errors.New("cannot encrypt, handshake incomplete")
825
}
9-
10-
// TODO: use pre-allocated buffers
11-
ciphertext = s.enc.Encrypt(nil, nil, plaintext)
12-
return ciphertext, nil
26+
return s.enc.Encrypt(out, nil, plaintext), nil
1327
}
1428

15-
func (s *secureSession) decrypt(ciphertext []byte) (plaintext []byte, err error) {
29+
// decrypt calls the cipher's decryption. It decrypts the provided ciphertext,
30+
// slice-appending the plaintext on out.
31+
//
32+
// Usually you want to pass a 0-len slice to this method, with enough capacity
33+
// to accommodate the plaintext in order to spare allocs.
34+
//
35+
// decrypt returns a new slice header, whose len is the length of the resulting
36+
// plaintext, without the authentication tag.
37+
//
38+
// This method will not allocate if the supplied slice is large enough to
39+
// accommodate the plaintext. If so, the returned slice header should be a view
40+
// of the original slice.
41+
func (s *secureSession) decrypt(out, ciphertext []byte) ([]byte, error) {
1642
if s.dec == nil {
1743
return nil, errors.New("cannot decrypt, handshake incomplete")
1844
}
19-
20-
// TODO: use pre-allocated buffers
21-
return s.dec.Decrypt(nil, nil, ciphertext)
45+
return s.dec.Decrypt(out, nil, ciphertext)
2246
}

crypto_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@ func TestEncryptAndDecrypt_InitToResp(t *testing.T) {
1818
defer respConn.Close()
1919

2020
plaintext := []byte("helloworld")
21-
ciphertext, err := initConn.encrypt(plaintext)
21+
ciphertext, err := initConn.encrypt(nil, plaintext)
2222
if err != nil {
2323
t.Fatal(err)
2424
}
2525

26-
result, err := respConn.decrypt(ciphertext)
26+
result, err := respConn.decrypt(nil, ciphertext)
2727
if !bytes.Equal(plaintext, result) {
2828
t.Fatalf("got %x expected %x", result, plaintext)
2929
} else if err != nil {
3030
t.Fatal(err)
3131
}
3232

3333
plaintext = []byte("goodbye")
34-
ciphertext, err = initConn.encrypt(plaintext)
34+
ciphertext, err = initConn.encrypt(nil, plaintext)
3535
if err != nil {
3636
t.Fatal(err)
3737
}
3838

39-
result, err = respConn.decrypt(ciphertext)
39+
result, err = respConn.decrypt(nil, ciphertext)
4040
if !bytes.Equal(plaintext, result) {
4141
t.Fatalf("got %x expected %x", result, plaintext)
4242
} else if err != nil {
@@ -53,12 +53,12 @@ func TestEncryptAndDecrypt_RespToInit(t *testing.T) {
5353
defer respConn.Close()
5454

5555
plaintext := []byte("helloworld")
56-
ciphertext, err := respConn.encrypt(plaintext)
56+
ciphertext, err := respConn.encrypt(nil, plaintext)
5757
if err != nil {
5858
t.Fatal(err)
5959
}
6060

61-
result, err := initConn.decrypt(ciphertext)
61+
result, err := initConn.decrypt(nil, ciphertext)
6262
if !bytes.Equal(plaintext, result) {
6363
t.Fatalf("got %x expected %x", result, plaintext)
6464
} else if err != nil {
@@ -75,14 +75,14 @@ func TestCryptoFailsIfCiphertextIsAltered(t *testing.T) {
7575
defer respConn.Close()
7676

7777
plaintext := []byte("helloworld")
78-
ciphertext, err := respConn.encrypt(plaintext)
78+
ciphertext, err := respConn.encrypt(nil, plaintext)
7979
if err != nil {
8080
t.Fatal(err)
8181
}
8282

8383
ciphertext[0] = ^ciphertext[0]
8484

85-
_, err = initConn.decrypt(ciphertext)
85+
_, err = initConn.decrypt(nil, ciphertext)
8686
if err == nil {
8787
t.Fatal("expected decryption to fail when ciphertext altered")
8888
}
@@ -94,11 +94,11 @@ func TestCryptoFailsIfHandshakeIncomplete(t *testing.T) {
9494
_ = resp.Close()
9595

9696
session, _ := newSecureSession(initTransport, context.TODO(), init, "remote-peer", true)
97-
_, err := session.encrypt([]byte("hi"))
97+
_, err := session.encrypt(nil, []byte("hi"))
9898
if err == nil {
9999
t.Error("expected encryption error when handshake incomplete")
100100
}
101-
_, err = session.decrypt([]byte("it's a secret"))
101+
_, err = session.decrypt(nil, []byte("it's a secret"))
102102
if err == nil {
103103
t.Error("expected decryption error when handshake incomplete")
104104
}

go-libp2p-noise-ethberlin-1a.pdf

-642 KB
Binary file not shown.

go.mod

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
module github.com/libp2p/go-libp2p-noise
22

3-
go 1.12
3+
go 1.14
44

55
require (
6-
github.com/ChainSafe/log15 v1.0.0
76
github.com/flynn/noise v0.0.0-20180327030543-2492fe189ae6
87
github.com/gogo/protobuf v1.3.1
9-
github.com/golang/protobuf v1.3.4
10-
github.com/ipfs/go-log v1.0.4
11-
github.com/libp2p/go-libp2p v0.7.2
12-
github.com/libp2p/go-libp2p-core v0.3.1
13-
github.com/libp2p/go-msgio v0.0.4
8+
github.com/libp2p/go-buffer-pool v0.0.2
9+
github.com/libp2p/go-libp2p v0.8.1
10+
github.com/libp2p/go-libp2p-core v0.5.1
1411
github.com/multiformats/go-multiaddr v0.2.1
15-
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
12+
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5
1613
)

0 commit comments

Comments
 (0)