Skip to content

Commit

Permalink
Merge pull request #124 from golang-fips/ed25519-2
Browse files Browse the repository at this point in the history
Update GenerateKeyEd25519 to only generate a private key
  • Loading branch information
qmuntal authored Oct 17, 2023
2 parents fbf9598 + f867d4a commit 08f07a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
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

0 comments on commit 08f07a7

Please sign in to comment.