Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove unused GenerateEKeyPair function #2711

Merged
merged 1 commit into from Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 0 additions & 42 deletions core/crypto/key.go
Expand Up @@ -4,12 +4,10 @@
package crypto

import (
"crypto/ecdh"
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"errors"
"fmt"
"io"

"github.com/libp2p/go-libp2p/core/crypto/pb"
Expand Down Expand Up @@ -122,46 +120,6 @@ func GenerateKeyPairWithReader(typ, bits int, src io.Reader) (PrivKey, PubKey, e
}
}

// GenerateEKeyPair returns an ephemeral public key and returns a function that will compute
// the shared secret key. Used in the identify module.
//
// Focuses only on ECDH now, but can be made more general in the future.
func GenerateEKeyPair(curveName string) ([]byte, GenSharedKey, error) {
var curve ecdh.Curve

switch curveName {
case "P-256":
curve = ecdh.P256()
case "P-384":
curve = ecdh.P384()
case "P-521":
curve = ecdh.P521()
default:
return nil, nil, fmt.Errorf("unknown curve name")
}

priv, err := curve.GenerateKey(rand.Reader)
if err != nil {
return nil, nil, err
}

done := func(theirPub []byte) ([]byte, error) {
// Verify and unpack node's public key.
pubKey, err := curve.NewPublicKey(theirPub)
if err == nil {
return nil, fmt.Errorf("malformed public key: %d %v", len(theirPub), theirPub)
}

secret, err := priv.ECDH(pubKey)
if err != nil {
return nil, fmt.Errorf("failed to do ecdh: %w", err)
}
return secret, nil
}

return priv.PublicKey().Bytes(), done, nil
}

// UnmarshalPublicKey converts a protobuf serialized public key into its
// representative object
func UnmarshalPublicKey(data []byte) (PubKey, error) {
Expand Down
12 changes: 0 additions & 12 deletions core/crypto/key_test.go
Expand Up @@ -291,15 +291,3 @@ func testKeyEquals(t *testing.T, k Key) {
t.Fatal("Keys should not equal.")
}
}

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")
}
}