Skip to content

Commit

Permalink
Use sync.OnceValue for SupportsEd25519
Browse files Browse the repository at this point in the history
  • Loading branch information
dagood committed Oct 8, 2024
1 parent c50cd9e commit cca75db
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,32 @@ const (
// TODO: Add support for Ed25519ph and Ed25519ctx when OpenSSL supports them,
// which will probably be in 3.2.0 (https://github.com/openssl/openssl/issues/20418).

var (
onceSupportsEd25519 sync.Once
supportsEd25519 bool
)
var supportsEd25519 = sync.OnceValue(func() bool {
switch vMajor {
case 1:
if versionAtOrAbove(1, 1, 1) {
ctx := C.go_openssl_EVP_PKEY_CTX_new_id(C.GO_EVP_PKEY_ED25519, nil)
if ctx != nil {
C.go_openssl_EVP_PKEY_CTX_free(ctx)
return true
}
}
case 3:
name := C.CString("ED25519")
defer C.free(unsafe.Pointer(name))
sig := C.go_openssl_EVP_SIGNATURE_fetch(nil, name, nil)
if sig != nil {
C.go_openssl_EVP_SIGNATURE_free(sig)
return true
}
}
return false
})

// SupportsEd25519 returns true if the current OpenSSL version supports
// GenerateKeyEd25519, NewKeyFromSeedEd25519, SignEd25519 and VerifyEd25519.
func SupportsEd25519() bool {
onceSupportsEd25519.Do(func() {
switch vMajor {
case 1:
if versionAtOrAbove(1, 1, 1) {
ctx := C.go_openssl_EVP_PKEY_CTX_new_id(C.GO_EVP_PKEY_ED25519, nil)
if ctx != nil {
C.go_openssl_EVP_PKEY_CTX_free(ctx)
supportsEd25519 = true
}
}
case 3:
name := C.CString("ED25519")
defer C.free(unsafe.Pointer(name))
sig := C.go_openssl_EVP_SIGNATURE_fetch(nil, name, nil)
if sig != nil {
C.go_openssl_EVP_SIGNATURE_free(sig)
supportsEd25519 = true
}
}
})
return supportsEd25519
return supportsEd25519()
}

type PublicKeyEd25519 struct {
Expand Down

0 comments on commit cca75db

Please sign in to comment.