Skip to content

Commit

Permalink
chore: remove unused GenerateEKeyPair function (#2711)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Feb 19, 2024
1 parent c8024ff commit 98be190
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 54 deletions.
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")
}
}

0 comments on commit 98be190

Please sign in to comment.