Skip to content

Commit

Permalink
Merge pull request #33 from libp2p/bug/curve-name
Browse files Browse the repository at this point in the history
Return error rather than panic in GenerateEKeyPair
  • Loading branch information
bigs authored Jul 12, 2019
2 parents 243249c + a9b8c56 commit 566bfe9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/crypto/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ func GenerateEKeyPair(curveName string) ([]byte, GenSharedKey, error) {
curve = elliptic.P384()
case "P-521":
curve = elliptic.P521()
default:
return nil, nil, fmt.Errorf("unknown curve name")
}

priv, x, y, err := elliptic.GenerateKey(curve, rand.Reader)
Expand Down
12 changes: 12 additions & 0 deletions core/crypto/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,15 @@ func (pk testkey) Raw() ([]byte, error) {
func (pk testkey) Equals(k Key) bool {
return KeyEqual(pk, k)
}

func TestUnknownCurveErrors(t *testing.T) {
_, _, err := GenerateEKeyPair("P-256")
if err != nil {
t.Fatal(err)
}

_, _, err = GenerateEKeyPair("error-please")
if err == nil {
t.Fatal("expected invalid key type to error")
}
}

0 comments on commit 566bfe9

Please sign in to comment.