crypto/ecies: avoid overallocation of key buffer

This commit is contained in:
Felix Lange 2020-04-01 12:42:23 +02:00
parent 3b81617e94
commit 81823d4757

View file

@ -146,7 +146,7 @@ var (
// 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, roundup(kdLen, hash.Size()))
for counter := uint32(1); len(k) < kdLen; counter++ { for counter := uint32(1); len(k) < kdLen; counter++ {
binary.BigEndian.PutUint32(counterBytes, counter) binary.BigEndian.PutUint32(counterBytes, counter)
hash.Write(counterBytes) hash.Write(counterBytes)
@ -158,6 +158,11 @@ func concatKDF(hash hash.Hash, z, s1 []byte, kdLen int) []byte {
return k[:kdLen] return k[:kdLen]
} }
// roundup rounds size up to the nearest multiple of blocksize.
func roundup(size, blocksize int) int {
return size + blocksize - (size % blocksize)
}
// messageTag computes the MAC of a message (called the tag) as per // messageTag computes the MAC of a message (called the tag) as per
// SEC 1, 3.5. // SEC 1, 3.5.
func messageTag(hash func() hash.Hash, km, msg, shared []byte) []byte { func messageTag(hash func() hash.Hash, km, msg, shared []byte) []byte {