MCPcopy
hub / github.com/OpenNHP/opennhp / FuzzECDHFromKey

Function FuzzECDHFromKey

nhp/test/fuzz_test.go:11–38  ·  view source on GitHub ↗

FuzzECDHFromKey tests ECDH key creation with random inputs. This is important for security as malformed keys should be handled gracefully.

(f *testing.F)

Source from the content-addressed store, hash-verified

9// FuzzECDHFromKey tests ECDH key creation with random inputs.
10// This is important for security as malformed keys should be handled gracefully.
11func FuzzECDHFromKey(f *testing.F) {
12 // Seed corpus with valid key sizes
13 f.Add([]byte{}, int(core.ECC_CURVE25519))
14 f.Add(make([]byte, 32), int(core.ECC_CURVE25519))
15 f.Add(make([]byte, 64), int(core.ECC_SM2))
16 f.Add(make([]byte, 16), int(core.ECC_CURVE25519))
17 f.Add(make([]byte, 48), int(core.ECC_SM2))
18
19 f.Fuzz(func(t *testing.T, data []byte, eccType int) {
20 // Normalize eccType to valid range
21 var eType core.EccTypeEnum
22 switch eccType % 2 {
23 case 0:
24 eType = core.ECC_CURVE25519
25 case 1:
26 eType = core.ECC_SM2
27 }
28
29 // ECDHFromKey should not panic on any input
30 // It should return nil for invalid keys
31 e := core.ECDHFromKey(eType, data)
32 if e != nil {
33 // If key was accepted, verify basic operations don't panic
34 _ = e.PublicKey()
35 _ = e.PublicKeyBase64()
36 }
37 })
38}
39
40// FuzzAESDecrypt tests AES decryption with random inputs.
41// Ensures malformed ciphertext is handled without panics.

Callers

nothing calls this directly

Calls 4

ECDHFromKeyMethod · 0.80
AddMethod · 0.65
PublicKeyMethod · 0.65
PublicKeyBase64Method · 0.65

Tested by

no test coverage detected