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

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 c3f7bb2 + c7c0a1c commit 1d45af2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 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 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 1d45af2

Please sign in to comment.