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

Update GenerateKeyEd25519 to only generate a private key #124

Merged
merged 1 commit into from
Oct 17, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,27 @@ func (k *PrivateKeyEd25519) Bytes() ([]byte, error) {
return priv, nil
}

// GenerateKeyEd25519 generates a public/private key pair.
func GenerateKeyEd25519() (*PublicKeyEd25519, *PrivateKeyEd25519, error) {
pkeyPriv, err := generateEVPPKey(C.GO_EVP_PKEY_ED25519, 0, "")
if err != nil {
return nil, nil, err
}
func (k *PrivateKeyEd25519) Public() (*PublicKeyEd25519, error) {
pub := make([]byte, publicKeySizeEd25519)
if err := extractPKEYPubEd25519(pkeyPriv, pub); err != nil {
C.go_openssl_EVP_PKEY_free(pkeyPriv)
return nil, nil, err
if err := extractPKEYPubEd25519(k._pkey, pub); err != nil {
return nil, err
}
pubk, err := NewPublicKeyEd25119(pub)
if err != nil {
C.go_openssl_EVP_PKEY_free(pkeyPriv)
return nil, nil, err
return nil, err
}
privk := &PrivateKeyEd25519{_pkey: pkeyPriv}
runtime.SetFinalizer(privk, (*PrivateKeyEd25519).finalize)
return pubk, privk, nil
return pubk, nil
}

// GenerateKeyEd25519 generates a private key.
func GenerateKeyEd25519() (*PrivateKeyEd25519, error) {
pkeyPriv, err := generateEVPPKey(C.GO_EVP_PKEY_ED25519, 0, "")
if err != nil {
return nil, err
}
priv := &PrivateKeyEd25519{_pkey: pkeyPriv}
runtime.SetFinalizer(priv, (*PrivateKeyEd25519).finalize)
return priv, nil
}

func NewPrivateKeyEd25119(priv []byte) (*PrivateKeyEd25519, error) {
Expand Down
16 changes: 12 additions & 4 deletions ed25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func TestEd25519SignVerify(t *testing.T) {
if !openssl.SupportsEd25519() {
t.Skip("Ed25519 not supported")
}
public, private, err := openssl.GenerateKeyEd25519()
private, err := openssl.GenerateKeyEd25519()
if err != nil {
t.Fatal(err)
}
public, err := private.Public()
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -93,7 +97,7 @@ func BenchmarkEd25519GenerateKey(b *testing.B) {
b.Skip("Ed25519 not supported")
}
for i := 0; i < b.N; i++ {
_, _, err := openssl.GenerateKeyEd25519()
_, err := openssl.GenerateKeyEd25519()
if err != nil {
b.Fatal(err)
}
Expand All @@ -117,7 +121,7 @@ func BenchmarkEd25519Signing(b *testing.B) {
if !openssl.SupportsEd25519() {
b.Skip("Ed25519 not supported")
}
_, priv, err := openssl.GenerateKeyEd25519()
priv, err := openssl.GenerateKeyEd25519()
if err != nil {
b.Fatal(err)
}
Expand All @@ -132,7 +136,11 @@ func BenchmarkEd25519Verification(b *testing.B) {
if !openssl.SupportsEd25519() {
b.Skip("Ed25519 not supported")
}
pub, priv, err := openssl.GenerateKeyEd25519()
priv, err := openssl.GenerateKeyEd25519()
if err != nil {
b.Fatal(err)
}
pub, err := priv.Public()
if err != nil {
b.Fatal(err)
}
Expand Down