Skip to content

Commit 8cb69b9

Browse files
fjlobscuren
authored andcommitted
[release/1.3.4] crypto/ecies: make authenticated shared data work
The s2 parameter was not actually written to the MAC.
1 parent c541b38 commit 8cb69b9

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

crypto/ecies/ecies.go

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

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

crypto/ecies/ecies_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,36 @@ func TestEncryptDecrypt(t *testing.T) {
353353
}
354354
}
355355

356+
func TestDecryptShared2(t *testing.T) {
357+
prv, err := GenerateKey(rand.Reader, DefaultCurve, nil)
358+
if err != nil {
359+
t.Fatal(err)
360+
}
361+
message := []byte("Hello, world.")
362+
shared2 := []byte("shared data 2")
363+
ct, err := Encrypt(rand.Reader, &prv.PublicKey, message, nil, shared2)
364+
if err != nil {
365+
t.Fatal(err)
366+
}
367+
368+
// Check that decrypting with correct shared data works.
369+
pt, err := prv.Decrypt(rand.Reader, ct, nil, shared2)
370+
if err != nil {
371+
t.Fatal(err)
372+
}
373+
if !bytes.Equal(pt, message) {
374+
t.Fatal("ecies: plaintext doesn't match message")
375+
}
376+
377+
// Decrypting without shared data or incorrect shared data fails.
378+
if _, err = prv.Decrypt(rand.Reader, ct, nil, nil); err == nil {
379+
t.Fatal("ecies: decrypting without shared data didn't fail")
380+
}
381+
if _, err = prv.Decrypt(rand.Reader, ct, nil, []byte("garbage")); err == nil {
382+
t.Fatal("ecies: decrypting with incorrect shared data didn't fail")
383+
}
384+
}
385+
356386
// TestMarshalEncryption validates the encode/decode produces a valid
357387
// ECIES encryption key.
358388
func TestMarshalEncryption(t *testing.T) {

0 commit comments

Comments
 (0)