Skip to content

Commit

Permalink
Merge pull request #28 from libp2p/fix/handle-keygen-errors
Browse files Browse the repository at this point in the history
don't swallow errors from rand.Read
  • Loading branch information
yusefnapora committed Dec 12, 2019
2 parents fc5d113 + 8a5063a commit 8feb08c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
15 changes: 12 additions & 3 deletions p2p/security/noise/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ func TestLibp2pIntegration_NoPipes(t *testing.T) {
func TestLibp2pIntegration_WithPipes(t *testing.T) {
ctx := context.Background()

kpa := GenerateKeypair()
kpa, err := GenerateKeypair()
if err != nil {
t.Fatal(err)
}

ha, err := makeNodePipes(t, 1, 33333, "", [32]byte{}, kpa)
if err != nil {
Expand Down Expand Up @@ -201,7 +204,10 @@ func TestLibp2pIntegration_WithPipes(t *testing.T) {
func TestLibp2pIntegration_XXFallback(t *testing.T) {
ctx := context.Background()

kpa := GenerateKeypair()
kpa, err := GenerateKeypair()
if err != nil {
t.Fatal(err)
}

ha, err := makeNode(t, 1, 33333, kpa)
if err != nil {
Expand Down Expand Up @@ -253,7 +259,10 @@ func TestLibp2pIntegration_XXFallback(t *testing.T) {
}

func TestConstrucingWithMaker(t *testing.T) {
kp := GenerateKeypair()
kp, err := GenerateKeypair()
if err != nil {
t.Fatal(err)
}

ctx := context.Background()
h, err := libp2p.New(ctx,
Expand Down
6 changes: 5 additions & 1 deletion p2p/security/noise/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ func newSecureSession(ctx context.Context, local peer.ID, privKey crypto.PrivKey
}

if kp == nil {
kp = GenerateKeypair()
var err error
kp, err = GenerateKeypair()
if err != nil {
return nil, err
}
}

localPeerInfo := peerInfo{
Expand Down
14 changes: 10 additions & 4 deletions p2p/security/noise/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ type Keypair struct {
}

// GenerateKeypair creates a new ed25519 keypair
func GenerateKeypair() *Keypair {
func GenerateKeypair() (*Keypair, error) {
var public_key [32]byte
var private_key [32]byte
_, _ = rand.Read(private_key[:])
_, err := rand.Read(private_key[:])
if err != nil {
return nil, err
}
curve25519.ScalarBaseMult(&public_key, &private_key)
return &Keypair{public_key, private_key}
return &Keypair{public_key, private_key}, nil
}

// Transport implements the interface sec.SecureTransport
Expand Down Expand Up @@ -102,7 +105,10 @@ func New(privkey crypto.PrivKey, options ...Option) (*Transport, error) {

kp := cfg.NoiseKeypair
if kp == nil {
kp = GenerateKeypair()
kp, err = GenerateKeypair()
if err != nil {
return nil, err
}
}

// the static key cache is only useful if Noise Pipes is enabled
Expand Down
8 changes: 6 additions & 2 deletions p2p/security/noise/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ func TestHandshakeIK(t *testing.T) {
respTransport := newTestTransportPipes(t, crypto.Ed25519, 2048)

// add responder's static key to initiator's key cache
respTransport.NoiseKeypair = GenerateKeypair()
kp, err := GenerateKeypair()
if err != nil {
t.Fatal(err)
}
respTransport.NoiseKeypair = kp
keycache := NewKeyCache()
keycache.Store(respTransport.LocalID, respTransport.NoiseKeypair.public_key)
initTransport.NoiseStaticKeyCache = keycache
Expand All @@ -230,7 +234,7 @@ func TestHandshakeIK(t *testing.T) {
defer respConn.Close()

before := []byte("hello world")
_, err := initConn.Write(before)
_, err = initConn.Write(before)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 8feb08c

Please sign in to comment.