Skip to content

Commit d2e442e

Browse files
Merge pull request #59 from nats-io/empty
[FIXED] Make sure to use byte slice to receive proper copy
2 parents 3e454c8 + 58fb9d6 commit d2e442e

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

xkeys.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 The NATS Authors
1+
// Copyright 2022-2023 The NATS Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -38,12 +38,13 @@ type ckp struct {
3838
seed [curveKeyLen]byte // Private raw key.
3939
}
4040

41-
// CreateUser will create a User typed KeyPair.
41+
// CreateCurveKeys will create a Curve typed KeyPair.
4242
func CreateCurveKeys() (KeyPair, error) {
4343
return CreateCurveKeysWithRand(rand.Reader)
4444
}
4545

46-
// CreateUser will create a User typed KeyPair with specified rand source.
46+
// CreateCurveKeysWithRand will create a Curve typed KeyPair
47+
// with specified rand source.
4748
func CreateCurveKeysWithRand(rr io.Reader) (KeyPair, error) {
4849
var kp ckp
4950
_, err := io.ReadFull(rr, kp.seed[:])
@@ -85,7 +86,7 @@ func (pair *ckp) PrivateKey() ([]byte, error) {
8586
return Encode(PrefixBytePrivate, pair.seed[:])
8687
}
8788

88-
func decodePubCurveKey(src string, dest [curveKeyLen]byte) error {
89+
func decodePubCurveKey(src string, dest []byte) error {
8990
var raw [curveDecodeLen]byte // should always be 35
9091
n, err := b32Enc.Decode(raw[:], []byte(src))
9192
if err != nil {
@@ -112,7 +113,7 @@ func decodePubCurveKey(src string, dest [curveKeyLen]byte) error {
112113
}
113114

114115
// Copy over, ignore prefix byte.
115-
copy(dest[:], raw[1:end])
116+
copy(dest, raw[1:end])
116117
return nil
117118
}
118119

@@ -134,7 +135,7 @@ func (pair *ckp) SealWithRand(input []byte, recipient string, rr io.Reader) ([]b
134135
err error
135136
)
136137

137-
if err = decodePubCurveKey(recipient, rpub); err != nil {
138+
if err = decodePubCurveKey(recipient, rpub[:]); err != nil {
138139
return nil, ErrInvalidRecipient
139140
}
140141
if _, err := io.ReadFull(rr, nonce[:]); err != nil {
@@ -159,7 +160,7 @@ func (pair *ckp) Open(input []byte, sender string) ([]byte, error) {
159160
}
160161
copy(nonce[:], input[vlen:vlen+curveNonceLen])
161162

162-
if err = decodePubCurveKey(sender, spub); err != nil {
163+
if err = decodePubCurveKey(sender, spub[:]); err != nil {
163164
return nil, ErrInvalidSender
164165
}
165166

xkeys_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 The NATS Authors
1+
// Copyright 2022-2023 The NATS Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -154,3 +154,32 @@ func TestCurvePublic(t *testing.T) {
154154
t.Fatalf("Expected %v but got %v", ErrCannotSeal, err)
155155
}
156156
}
157+
158+
func TestCurvePublicEmptyBug(t *testing.T) {
159+
kp, _ := CreateCurveKeys()
160+
pub, _ := kp.PublicKey()
161+
162+
rkp, _ := CreateCurveKeys()
163+
rpub, _ := rkp.PublicKey()
164+
165+
msg := []byte("Empty public better not work!")
166+
encrypted, err := kp.Seal(msg, rpub)
167+
if err != nil {
168+
t.Fatalf("Received an error on Seal: %v", err)
169+
}
170+
decrypted, err := rkp.Open(encrypted, pub)
171+
if err != nil {
172+
t.Fatalf("Received an error on Open: %v", err)
173+
}
174+
if !bytes.Equal(decrypted, msg) {
175+
t.Fatalf("Expected %q to be %q", decrypted, msg)
176+
}
177+
// Check an empty pub key.
178+
var empty [curveKeyLen]byte
179+
epub, _ := Encode(PrefixByteCurve, empty[:])
180+
181+
_, err = rkp.Open(encrypted, string(epub))
182+
if err == nil {
183+
t.Fatalf("Expected a failure with empty pub key")
184+
}
185+
}

0 commit comments

Comments
 (0)