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

Commit

Permalink
Merge pull request #58 from libp2p/raul-review
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk committed Apr 24, 2020
2 parents abd5989 + 1ad9313 commit 69090b2
Show file tree
Hide file tree
Showing 13 changed files with 338 additions and 309 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
[![GoDoc](https://godoc.org/github.com/libp2p/go-libp2p-noise?status.svg)](https://godoc.org/github.com/libp2p/go-libp2p-noise)
[![Build Status](https://travis-ci.org/libp2p/go-libp2p-noise.svg?branch=master)](https://travis-ci.org/libp2p/go-libp2p-noise)
[![Build Status](https://travis-ci.com/libp2p/go-libp2p-noise.svg?branch=master)](https://travis-ci.com/libp2p/go-libp2p-noise)

> go-libp2p's noise encrypted transport
Expand Down
7 changes: 7 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ func pipeRandom(src rand.Source, w io.WriteCloser, r io.Reader, n int64) error {
func benchDataTransfer(b *benchenv, size int64) {
var totalBytes int64
var totalTime time.Duration

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
initSession, respSession := b.connect(true)

Expand Down Expand Up @@ -153,6 +157,9 @@ func BenchmarkTransfer500Mb(b *testing.B) {
}

func (b benchenv) benchHandshake() {
b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
i, r := b.connect(false)
b.StopTimer()
Expand Down
44 changes: 34 additions & 10 deletions crypto.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
package noise

import "errors"
import (
"errors"
)

func (s *secureSession) encrypt(plaintext []byte) (ciphertext []byte, err error) {
// encrypt calls the cipher's encryption. It encrypts the provided plaintext,
// slice-appending the ciphertext on out.
//
// Usually you want to pass a 0-len slice to this method, with enough capacity
// to accommodate the ciphertext in order to spare allocs.
//
// encrypt returns a new slice header, whose len is the length of the resulting
// ciphertext, including the authentication tag.
//
// This method will not allocate if the supplied slice is large enough to
// accommodate the encrypted data + authentication tag. If so, the returned
// slice header should be a view of the original slice.
//
// With the poly1305 MAC function that noise-libp2p uses, the authentication tag
// adds an overhead of 16 bytes.
func (s *secureSession) encrypt(out, plaintext []byte) ([]byte, error) {
if s.enc == nil {
return nil, errors.New("cannot encrypt, handshake incomplete")
}

// TODO: use pre-allocated buffers
ciphertext = s.enc.Encrypt(nil, nil, plaintext)
return ciphertext, nil
return s.enc.Encrypt(out, nil, plaintext), nil
}

func (s *secureSession) decrypt(ciphertext []byte) (plaintext []byte, err error) {
// decrypt calls the cipher's decryption. It decrypts the provided ciphertext,
// slice-appending the plaintext on out.
//
// Usually you want to pass a 0-len slice to this method, with enough capacity
// to accommodate the plaintext in order to spare allocs.
//
// decrypt returns a new slice header, whose len is the length of the resulting
// plaintext, without the authentication tag.
//
// This method will not allocate if the supplied slice is large enough to
// accommodate the plaintext. If so, the returned slice header should be a view
// of the original slice.
func (s *secureSession) decrypt(out, ciphertext []byte) ([]byte, error) {
if s.dec == nil {
return nil, errors.New("cannot decrypt, handshake incomplete")
}

// TODO: use pre-allocated buffers
return s.dec.Decrypt(nil, nil, ciphertext)
return s.dec.Decrypt(out, nil, ciphertext)
}
20 changes: 10 additions & 10 deletions crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ func TestEncryptAndDecrypt_InitToResp(t *testing.T) {
defer respConn.Close()

plaintext := []byte("helloworld")
ciphertext, err := initConn.encrypt(plaintext)
ciphertext, err := initConn.encrypt(nil, plaintext)
if err != nil {
t.Fatal(err)
}

result, err := respConn.decrypt(ciphertext)
result, err := respConn.decrypt(nil, ciphertext)
if !bytes.Equal(plaintext, result) {
t.Fatalf("got %x expected %x", result, plaintext)
} else if err != nil {
t.Fatal(err)
}

plaintext = []byte("goodbye")
ciphertext, err = initConn.encrypt(plaintext)
ciphertext, err = initConn.encrypt(nil, plaintext)
if err != nil {
t.Fatal(err)
}

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

plaintext := []byte("helloworld")
ciphertext, err := respConn.encrypt(plaintext)
ciphertext, err := respConn.encrypt(nil, plaintext)
if err != nil {
t.Fatal(err)
}

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

plaintext := []byte("helloworld")
ciphertext, err := respConn.encrypt(plaintext)
ciphertext, err := respConn.encrypt(nil, plaintext)
if err != nil {
t.Fatal(err)
}

ciphertext[0] = ^ciphertext[0]

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

session, _ := newSecureSession(initTransport, context.TODO(), init, "remote-peer", true)
_, err := session.encrypt([]byte("hi"))
_, err := session.encrypt(nil, []byte("hi"))
if err == nil {
t.Error("expected encryption error when handshake incomplete")
}
_, err = session.decrypt([]byte("it's a secret"))
_, err = session.decrypt(nil, []byte("it's a secret"))
if err == nil {
t.Error("expected decryption error when handshake incomplete")
}
Expand Down
Binary file removed go-libp2p-noise-ethberlin-1a.pdf
Binary file not shown.
13 changes: 5 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
module github.com/libp2p/go-libp2p-noise

go 1.12
go 1.14

require (
github.com/ChainSafe/log15 v1.0.0
github.com/flynn/noise v0.0.0-20180327030543-2492fe189ae6
github.com/gogo/protobuf v1.3.1
github.com/golang/protobuf v1.3.4
github.com/ipfs/go-log v1.0.4
github.com/libp2p/go-libp2p v0.7.2
github.com/libp2p/go-libp2p-core v0.3.1
github.com/libp2p/go-msgio v0.0.4
github.com/libp2p/go-buffer-pool v0.0.2
github.com/libp2p/go-libp2p v0.8.1
github.com/libp2p/go-libp2p-core v0.5.1
github.com/multiformats/go-multiaddr v0.2.1
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5
)
Loading

0 comments on commit 69090b2

Please sign in to comment.