From 9a3c1e351dceffaac780c27d2120754f89164a51 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sat, 25 May 2019 19:49:49 -0400 Subject: [PATCH] Clear Valgrind finding in IncrementCounterByOne The single buffer IncrementCounterByOne generated a Valgrind finding on ARM. This commit uses the same pattern for both overloads in case Valgrind wants to fire on the two-buffer version. --- misc.h | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/misc.h b/misc.h index d836aaa2..2cdabfbe 100644 --- a/misc.h +++ b/misc.h @@ -1225,10 +1225,12 @@ CRYPTOPP_DLL void CRYPTOPP_API CallNewHandler(); /// \note The function is not constant time because it stops processing when the carry is 0. inline void IncrementCounterByOne(byte *inout, unsigned int size) { + CRYPTOPP_ASSERT(inout != NULLPTR); + unsigned int carry=1; while (carry && size != 0) { - // On wrap inout[n] equals 0 + // On carry inout[n] equals 0 carry = ! ++inout[size-1]; size--; } @@ -1243,12 +1245,22 @@ inline void IncrementCounterByOne(byte *inout, unsigned int size) /// \details The function is close to near-constant time because it operates on all the bytes in the blocks. inline void IncrementCounterByOne(byte *output, const byte *input, unsigned int size) { - CRYPTOPP_ASSERT(output != NULLPTR); CRYPTOPP_ASSERT(input != NULLPTR); CRYPTOPP_ASSERT(size < INT_MAX); + CRYPTOPP_ASSERT(output != NULLPTR); + CRYPTOPP_ASSERT(input != NULLPTR); - int i, carry; - for (i=int(size-1), carry=1; i>=0 && carry; i--) - carry = ((output[i] = input[i]+1) == 0); - memcpy_s(output, size, input, size_t(i)+1); + unsigned int carry=1; + while (carry && size != 0) + { + // On carry output[n] equals 0 + carry = ! (output[size-1] = input[size-1] + 1); + size--; + } + + while (size != 0) + { + output[size-1] = input[size-1]; + size--; + } } /// \brief Performs a branchless swap of values a and b if condition c is true