crypto/ecies: move hash.Reset call to end of loop

This makes a difference because the hash is re-used in Encrypt and
Decrypt without an additional Reset.
This commit is contained in:
Felix Lange 2020-04-01 12:04:11 +02:00
parent 870f40f7ec
commit 3b81617e94

View file

@ -143,18 +143,17 @@ var (
ErrInvalidMessage = fmt.Errorf("ecies: invalid message") ErrInvalidMessage = fmt.Errorf("ecies: invalid message")
) )
// NIST SP 800-56 Concatenation Key Derivation Function (see section 5.8.1). // NIST SP 800-56 Concatenation Key Derivation Function (see section 5.8.1).
func concatKDF(hash hash.Hash, z, s1 []byte, kdLen int) []byte { func concatKDF(hash hash.Hash, z, s1 []byte, kdLen int) []byte {
counterBytes := make([]byte, 4) counterBytes := make([]byte, 4)
k := make([]byte, 0, kdLen+hash.Size()) k := make([]byte, 0, kdLen+hash.Size())
for counter := uint32(1); len(k) < kdLen; counter++ { for counter := uint32(1); len(k) < kdLen; counter++ {
hash.Reset()
binary.BigEndian.PutUint32(counterBytes, counter) binary.BigEndian.PutUint32(counterBytes, counter)
hash.Write(counterBytes) hash.Write(counterBytes)
hash.Write(z) hash.Write(z)
hash.Write(s1) hash.Write(s1)
k = hash.Sum(k) k = hash.Sum(k)
hash.Reset()
} }
return k[:kdLen] return k[:kdLen]
} }