Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hpke: fix encapsulation seed in test for xyber #428

Merged
merged 1 commit into from Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion hpke/testdata/hybrid-x25119-kyber768-test-vectors.json

Large diffs are not rendered by default.

26 changes: 14 additions & 12 deletions hpke/vectors_test.go
Expand Up @@ -228,16 +228,17 @@ type vector struct {
KdfID uint16 `json:"kdf_id"`
AeadID uint16 `json:"aead_id"`
Info string `json:"info"`
Ier string `json:"ier,omitempty"`
IkmR string `json:"ikmR"`
IkmE string `json:"ikmE"`
IkmE string `json:"ikmE,omitempty"`
bwesterb marked this conversation as resolved.
Show resolved Hide resolved
SkRm string `json:"skRm"`
SkEm string `json:"skEm"`
SkEm string `json:"skEm,omitempty"`
bwesterb marked this conversation as resolved.
Show resolved Hide resolved
SkSm string `json:"skSm,omitempty"`
Psk string `json:"psk,omitempty"`
PskID string `json:"psk_id,omitempty"`
PkSm string `json:"pkSm,omitempty"`
PkRm string `json:"pkRm"`
PkEm string `json:"pkEm"`
PkEm string `json:"pkEm,omitempty"`
bwesterb marked this conversation as resolved.
Show resolved Hide resolved
Enc string `json:"enc"`
SharedSecret string `json:"shared_secret"`
KeyScheduleContext string `json:"key_schedule_context"`
Expand Down Expand Up @@ -344,10 +345,8 @@ func TestHybridKemRoundTrip(t *testing.T) {
t.Error(err)
}

ikmE, pkE, skE, err := generateHybridKeyPair(rnd, kemID.Scheme())
if err != nil {
t.Error(err)
}
ier := make([]byte, 64)
_, _ = rnd.Read(ier)

receiver, err := suite.NewReceiver(skR, info)
if err != nil {
Expand All @@ -366,9 +365,10 @@ func TestHybridKemRoundTrip(t *testing.T) {
opener Opener
enc []byte
)
rnd2 := bytes.NewBuffer(ier)
switch mode {
case modeBase:
enc, sealer, err2 = sender.Setup(rnd)
enc, sealer, err2 = sender.Setup(rnd2)
if err2 != nil {
t.Error(err2)
}
Expand All @@ -377,7 +377,7 @@ func TestHybridKemRoundTrip(t *testing.T) {
t.Error(err2)
}
case modePSK:
enc, sealer, err2 = sender.SetupPSK(rnd, psk, pskid)
enc, sealer, err2 = sender.SetupPSK(rnd2, psk, pskid)
if err2 != nil {
t.Error(err2)
}
Expand All @@ -389,6 +389,10 @@ func TestHybridKemRoundTrip(t *testing.T) {
panic("unsupported mode")
}

if rnd2.Len() != 0 {
t.Fatal()
}

Comment on lines +392 to +395
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if rnd2.Len() != 0 {
t.Fatal()
}

It doesn't seem like this is needed since we always create it as 64 bytes up above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is defensive programming, to make sure we don't have a mismatch between the amount of randomness in the buffer and the actual amount consumed. It's not necessary now, but it might be once we check the upstream test vectors and copy-paste this code.

innerSealer := sealer.(*sealContext)

encryptions, err2 := generateEncryptions(sealer, opener, msg)
Expand All @@ -405,13 +409,11 @@ func TestHybridKemRoundTrip(t *testing.T) {
KemID: uint16(kemID),
KdfID: uint16(kdfID),
AeadID: uint16(aeadID),
Ier: hex.EncodeToString(ier),
Info: hex.EncodeToString(info),
IkmR: hex.EncodeToString(ikmR),
IkmE: hex.EncodeToString(ikmE),
SkRm: hex.EncodeToString(mustEncodePrivateKey(skR)),
SkEm: hex.EncodeToString(mustEncodePrivateKey(skE)),
PkRm: hex.EncodeToString(mustEncodePublicKey(pkR)),
PkEm: hex.EncodeToString(mustEncodePublicKey(pkE)),
Enc: hex.EncodeToString(enc),
SharedSecret: hex.EncodeToString(innerSealer.sharedSecret),
KeyScheduleContext: hex.EncodeToString(innerSealer.keyScheduleContext),
Expand Down