Skip to content

Commit fdb936e

Browse files
committed
crypto/ecies: make authenticated shared data work
The s2 parameter was not actually written to the MAC.
1 parent b05e472 commit fdb936e

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

‎crypto/ecies/ecies.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,9 @@ func concatKDF(hash hash.Hash, z, s1 []byte, kdLen int) (k []byte, err error) {
192192
// messageTag computes the MAC of a message (called the tag) as per
193193
// SEC 1, 3.5.
194194
func messageTag(hash func() hash.Hash, km, msg, shared []byte) []byte {
195-
if shared == nil {
196-
shared = make([]byte, 0)
197-
}
198195
mac := hmac.New(hash, km)
199196
mac.Write(msg)
197+
mac.Write(shared)
200198
tag := mac.Sum(nil)
201199
return tag
202200
}
@@ -243,9 +241,11 @@ func symDecrypt(rand io.Reader, params *ECIESParams, key, ct []byte) (m []byte,
243241
return
244242
}
245243

246-
// Encrypt encrypts a message using ECIES as specified in SEC 1, 5.1. If
247-
// the shared information parameters aren't being used, they should be
248-
// nil.
244+
// Encrypt encrypts a message using ECIES as specified in SEC 1, 5.1.
245+
//
246+
// s1 and s2 contain shared information that is not part of the resulting
247+
// ciphertext. s1 is fed into key derivation, s2 is fed into the MAC. If the
248+
// shared information parameters aren't being used, they should be nil.
249249
func Encrypt(rand io.Reader, pub *PublicKey, m, s1, s2 []byte) (ct []byte, err error) {
250250
params := pub.Params
251251
if params == nil {

‎crypto/ecies/ecies_test.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,36 @@ func TestEncryptDecrypt(t *testing.T) {
408408
}
409409
}
410410

411+
func TestDecryptShared2(t *testing.T) {
412+
prv, err := GenerateKey(rand.Reader, DefaultCurve, nil)
413+
if err != nil {
414+
t.Fatal(err)
415+
}
416+
message := []byte("Hello, world.")
417+
shared2 := []byte("shared data 2")
418+
ct, err := Encrypt(rand.Reader, &prv.PublicKey, message, nil, shared2)
419+
if err != nil {
420+
t.Fatal(err)
421+
}
422+
423+
// Check that decrypting with correct shared data works.
424+
pt, err := prv.Decrypt(rand.Reader, ct, nil, shared2)
425+
if err != nil {
426+
t.Fatal(err)
427+
}
428+
if !bytes.Equal(pt, message) {
429+
t.Fatal("ecies: plaintext doesn't match message")
430+
}
431+
432+
// Decrypting without shared data or incorrect shared data fails.
433+
if _, err = prv.Decrypt(rand.Reader, ct, nil, nil); err == nil {
434+
t.Fatal("ecies: decrypting without shared data didn't fail")
435+
}
436+
if _, err = prv.Decrypt(rand.Reader, ct, nil, []byte("garbage")); err == nil {
437+
t.Fatal("ecies: decrypting with incorrect shared data didn't fail")
438+
}
439+
}
440+
411441
// TestMarshalEncryption validates the encode/decode produces a valid
412442
// ECIES encryption key.
413443
func TestMarshalEncryption(t *testing.T) {

0 commit comments

Comments
 (0)