Skip to content

Commit c96fdff

Browse files
authored
Merge pull request #113 from golang-fips/derivopt
Add noescape and nocallback directive to EVP_PKEY_derive
2 parents 24808fc + d534947 commit c96fdff

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

cgo_go122.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build go1.22 && !cmd_go_bootstrap
2+
3+
package openssl
4+
5+
/*
6+
// The following noescape and nocallback directives are used to
7+
// prevent the Go compiler from allocating function parameters on the
8+
// heap. This is just a performance optimization. Only add those
9+
// functions that are known to allocate.
10+
#cgo noescape go_openssl_EVP_PKEY_derive
11+
#cgo nocallback go_openssl_EVP_PKEY_derive
12+
*/
13+
import "C"

ecdh_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,28 @@ func hexDecode(t *testing.T, s string) []byte {
146146
}
147147
return b
148148
}
149+
150+
func BenchmarkECDH(b *testing.B) {
151+
const curve = "P-256"
152+
aliceKey, _, err := openssl.GenerateKeyECDH(curve)
153+
if err != nil {
154+
b.Fatal(err)
155+
}
156+
bobKey, _, err := openssl.GenerateKeyECDH(curve)
157+
if err != nil {
158+
b.Fatal(err)
159+
}
160+
161+
alicePubKey, err := aliceKey.PublicKey()
162+
if err != nil {
163+
b.Fatal(err)
164+
}
165+
b.ResetTimer()
166+
b.ReportAllocs()
167+
for i := 0; i < b.N; i++ {
168+
_, err := openssl.ECDH(bobKey, alicePubKey)
169+
if err != nil {
170+
b.Fatal(err)
171+
}
172+
}
173+
}

0 commit comments

Comments
 (0)