From d53494757ba6358bf0930cc1d4103150523c93e4 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Wed, 30 Aug 2023 16:53:53 +0200 Subject: [PATCH] add noescape and nocallback directive to EVP_PKEY_derive --- cgo_go122.go | 13 +++++++++++++ ecdh_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 cgo_go122.go diff --git a/cgo_go122.go b/cgo_go122.go new file mode 100644 index 00000000..555f58c5 --- /dev/null +++ b/cgo_go122.go @@ -0,0 +1,13 @@ +//go:build go1.22 && !cmd_go_bootstrap + +package openssl + +/* +// The following noescape and nocallback directives are used to +// prevent the Go compiler from allocating function parameters on the +// heap. This is just a performance optimization. Only add those +// functions that are known to allocate. +#cgo noescape go_openssl_EVP_PKEY_derive +#cgo nocallback go_openssl_EVP_PKEY_derive +*/ +import "C" diff --git a/ecdh_test.go b/ecdh_test.go index 80bf85a0..c83a347b 100644 --- a/ecdh_test.go +++ b/ecdh_test.go @@ -146,3 +146,28 @@ func hexDecode(t *testing.T, s string) []byte { } return b } + +func BenchmarkECDH(b *testing.B) { + const curve = "P-256" + aliceKey, _, err := openssl.GenerateKeyECDH(curve) + if err != nil { + b.Fatal(err) + } + bobKey, _, err := openssl.GenerateKeyECDH(curve) + if err != nil { + b.Fatal(err) + } + + alicePubKey, err := aliceKey.PublicKey() + if err != nil { + b.Fatal(err) + } + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + _, err := openssl.ECDH(bobKey, alicePubKey) + if err != nil { + b.Fatal(err) + } + } +}