Skip to content

Commit 3507683

Browse files
committed
Test showing Issue #488, unmarshal keys panics.
1 parent bba8f1a commit 3507683

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

hpke/kem_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package hpke_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/cloudflare/circl/hpke"
8+
"github.com/cloudflare/circl/internal/test"
9+
)
10+
11+
func TestKemKeysMarshal(t *testing.T) {
12+
for _, kem := range []hpke.KEM{
13+
hpke.KEM_P256_HKDF_SHA256,
14+
hpke.KEM_P384_HKDF_SHA384,
15+
hpke.KEM_P521_HKDF_SHA512,
16+
hpke.KEM_X25519_HKDF_SHA256,
17+
hpke.KEM_X448_HKDF_SHA512,
18+
hpke.KEM_X25519_KYBER768_DRAFT00,
19+
} {
20+
fixIssue488(t, kem)
21+
}
22+
}
23+
24+
func fixIssue488(t *testing.T, kem hpke.KEM) {
25+
scheme := kem.Scheme()
26+
// Passing larger slices to UnmarshlBinary on keys causes panic.
27+
pk, sk, err := scheme.GenerateKeyPair()
28+
if err != nil {
29+
t.Fatal(err)
30+
}
31+
32+
t.Run(fmt.Sprintf("%v/PrivateKey", scheme.Name()), func(t *testing.T) {
33+
// setting a buffer larger than the private key.
34+
buffer := make([]byte, scheme.PrivateKeySize()+100)
35+
36+
skBytes, err := sk.MarshalBinary()
37+
test.CheckNoErr(t, err, "marshal private key")
38+
39+
copy(buffer, skBytes)
40+
41+
gotSk, err := scheme.UnmarshalBinaryPrivateKey(buffer)
42+
test.CheckNoErr(t, err, "unmarshal private key")
43+
test.CheckOk(sk.Equal(gotSk), "private keys are not equal", t)
44+
})
45+
46+
t.Run(fmt.Sprintf("%v/PublicKey", scheme.Name()), func(t *testing.T) {
47+
// setting a buffer larger than the public key.
48+
buffer := make([]byte, scheme.PublicKeySize()+100)
49+
50+
pkBytes, err := pk.MarshalBinary()
51+
test.CheckNoErr(t, err, "marshal public key")
52+
53+
copy(buffer, pkBytes)
54+
55+
gotPk, err := scheme.UnmarshalBinaryPublicKey(buffer)
56+
test.CheckNoErr(t, err, "unmarshal public key")
57+
test.CheckOk(pk.Equal(gotPk), "public keys are not equal", t)
58+
})
59+
}

0 commit comments

Comments
 (0)