Skip to content

Commit 12ba47d

Browse files
committed
Test showing Issue #488, unmarshal keys panics.
1 parent 4bb5601 commit 12ba47d

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

hpke/kem_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
checkIssue488(t, kem)
21+
}
22+
}
23+
24+
func checkIssue488(t *testing.T, kem hpke.KEM) {
25+
scheme := kem.Scheme()
26+
pk, sk, err := scheme.GenerateKeyPair()
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
skBytes, err := sk.MarshalBinary()
31+
test.CheckNoErr(t, err, "marshal private key")
32+
pkBytes, err := pk.MarshalBinary()
33+
test.CheckNoErr(t, err, "marshal public key")
34+
35+
t.Run(fmt.Sprintf("%v/PrivateKey", scheme.Name()), func(t *testing.T) {
36+
N := scheme.PrivateKeySize()
37+
buffer := make([]byte, N+1)
38+
copy(buffer, skBytes)
39+
40+
// passing a buffer larger than the private key size should error (but no panic).
41+
_, err := scheme.UnmarshalBinaryPrivateKey(buffer[:N+1])
42+
test.CheckIsErr(t, err, "unmarshal private key should failed")
43+
44+
// passing a buffer of the exact size must be correct.
45+
gotSk, err := scheme.UnmarshalBinaryPrivateKey(buffer[:N])
46+
test.CheckNoErr(t, err, "unmarshal private key shouldn't fail")
47+
test.CheckOk(sk.Equal(gotSk), "private keys are not equal", t)
48+
})
49+
50+
t.Run(fmt.Sprintf("%v/PublicKey", scheme.Name()), func(t *testing.T) {
51+
N := scheme.PublicKeySize()
52+
buffer := make([]byte, N+1)
53+
copy(buffer, pkBytes)
54+
55+
// passing a buffer larger than the public key size should error (but no panic).
56+
_, err := scheme.UnmarshalBinaryPublicKey(buffer[:N+1])
57+
test.CheckIsErr(t, err, "unmarshal public key should failed")
58+
59+
gotPk, err := scheme.UnmarshalBinaryPublicKey(buffer[:N])
60+
test.CheckNoErr(t, err, "unmarshal public key shouldn't fail")
61+
test.CheckOk(pk.Equal(gotPk), "public keys are not equal", t)
62+
})
63+
}

0 commit comments

Comments
 (0)