diff --git a/ed25519.go b/ed25519.go index 81961da8..33e26a55 100644 --- a/ed25519.go +++ b/ed25519.go @@ -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) { diff --git a/ed25519_test.go b/ed25519_test.go index e34ead2c..e4dbb896 100644 --- a/ed25519_test.go +++ b/ed25519_test.go @@ -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) } @@ -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) } @@ -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) } @@ -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) }