From 753f927e31a059b7d42a4cb66204f85a932113b9 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 31 Mar 2020 14:51:51 +0200 Subject: [PATCH] crypto/ecies: avoid big integer math in overflow check --- crypto/ecies/ecies.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/crypto/ecies/ecies.go b/crypto/ecies/ecies.go index 7df3d2618a..90d5d66ceb 100644 --- a/crypto/ecies/ecies.go +++ b/crypto/ecies/ecies.go @@ -143,11 +143,6 @@ var ( ErrInvalidMessage = fmt.Errorf("ecies: invalid message") ) -var ( - big2To32 = new(big.Int).Exp(big.NewInt(2), big.NewInt(32), nil) - big2To32M1 = new(big.Int).Sub(big2To32, big.NewInt(1)) -) - func incCounter(ctr []byte) { if ctr[3]++; ctr[3] != 0 { return @@ -169,8 +164,11 @@ func concatKDF(hash hash.Hash, z, s1 []byte, kdLen int) (k []byte, err error) { s1 = make([]byte, 0) } - reps := ((kdLen + 7) * 8) / (hash.Size() * 8) - if big.NewInt(int64(reps)).Cmp(big2To32M1) > 0 { + // reps is the maximum number of iterations of the + // counter hashing loop. This is capped to 32 bits to + // prevent overflow of the counter. + reps := (int64(kdLen) + 7) * 8 / int64(hash.Size()*8) + if reps > int64(^uint32(0)) { return nil, ErrKeyDataTooLong }