From 19deccf3ba64a50c97c29d519ca9bfaec234c267 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sat, 16 Dec 2017 18:18:53 -0500 Subject: [PATCH] Fix Clang 5.0 "runtime error: addition of unsigned offset to 0xXXXX overflowed to 0xYYYY" (GH #549) --- adv-simd.h | 97 ++++++++++++++++++++++++++-------------------------- cryptlib.cpp | 13 +++---- seed.cpp | 17 ++++++--- 3 files changed, 69 insertions(+), 58 deletions(-) diff --git a/adv-simd.h b/adv-simd.h index 2b360b7f..cfb904d5 100644 --- a/adv-simd.h +++ b/adv-simd.h @@ -24,6 +24,7 @@ #include "config.h" #include "misc.h" +#include "stdcpp.h" #if (CRYPTOPP_ARM_NEON_AVAILABLE) # include @@ -88,18 +89,18 @@ inline size_t AdvancedProcessBlocks64_NEON2x6(F2 func2, F6 func6, CRYPTOPP_ASSERT(outBlocks); CRYPTOPP_ASSERT(length >= 8); - const size_t blockSize = 8; - const size_t neonBlockSize = 16; + const ptrdiff_t blockSize = 8; + const ptrdiff_t neonBlockSize = 16; - size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : neonBlockSize; - size_t xorIncrement = xorBlocks ? neonBlockSize : 0; - size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : neonBlockSize; + ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : neonBlockSize; + ptrdiff_t xorIncrement = xorBlocks ? neonBlockSize : 0; + ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : neonBlockSize; if (flags & BT_ReverseDirection) { - inBlocks += length - neonBlockSize; - xorBlocks += length - neonBlockSize; - outBlocks += length - neonBlockSize; + inBlocks += static_cast(length) - neonBlockSize; + xorBlocks += static_cast(length) - neonBlockSize; + outBlocks += static_cast(length) - neonBlockSize; inIncrement = 0-inIncrement; xorIncrement = 0-xorIncrement; outIncrement = 0-outIncrement; @@ -316,18 +317,18 @@ size_t AdvancedProcessBlocks128_NEON1x6(F1 func1, F6 func6, CRYPTOPP_ASSERT(outBlocks); CRYPTOPP_ASSERT(length >= 16); - const size_t blockSize = 16; - // const size_t neonBlockSize = 16; + const ptrdiff_t blockSize = 16; + // const ptrdiff_t neonBlockSize = 16; - size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; - size_t xorIncrement = xorBlocks ? blockSize : 0; - size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; + ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; + ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0; + ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; if (flags & BT_ReverseDirection) { - inBlocks += length - blockSize; - xorBlocks += length - blockSize; - outBlocks += length - blockSize; + inBlocks += static_cast(length) - blockSize; + xorBlocks += static_cast(length) - blockSize; + outBlocks += static_cast(length) - blockSize; inIncrement = 0-inIncrement; xorIncrement = 0-xorIncrement; outIncrement = 0-outIncrement; @@ -455,18 +456,18 @@ size_t AdvancedProcessBlocks128_NEON2x6(F2 func2, F6 func6, CRYPTOPP_ASSERT(outBlocks); CRYPTOPP_ASSERT(length >= 16); - const size_t blockSize = 16; - // const size_t neonBlockSize = 16; + const ptrdiff_t blockSize = 16; + // const ptrdiff_t neonBlockSize = 16; - size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; - size_t xorIncrement = xorBlocks ? blockSize : 0; - size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; + ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; + ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0; + ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; if (flags & BT_ReverseDirection) { - inBlocks += length - blockSize; - xorBlocks += length - blockSize; - outBlocks += length - blockSize; + inBlocks += static_cast(length) - blockSize; + xorBlocks += static_cast(length) - blockSize; + outBlocks += static_cast(length) - blockSize; inIncrement = 0-inIncrement; xorIncrement = 0-xorIncrement; outIncrement = 0-outIncrement; @@ -682,18 +683,18 @@ inline size_t AdvancedProcessBlocks64_SSE2x6(F2 func2, F6 func6, CRYPTOPP_ASSERT(outBlocks); CRYPTOPP_ASSERT(length >= 8); - const size_t blockSize = 8; - const size_t xmmBlockSize = 16; + const ptrdiff_t blockSize = 8; + const ptrdiff_t xmmBlockSize = 16; - size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : xmmBlockSize; - size_t xorIncrement = xorBlocks ? xmmBlockSize : 0; - size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : xmmBlockSize; + ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : xmmBlockSize; + ptrdiff_t xorIncrement = xorBlocks ? xmmBlockSize : 0; + ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : xmmBlockSize; if (flags & BT_ReverseDirection) { - inBlocks += length - xmmBlockSize; - xorBlocks += length - xmmBlockSize; - outBlocks += length - xmmBlockSize; + inBlocks += static_cast(length) - xmmBlockSize; + xorBlocks += static_cast(length) - xmmBlockSize; + outBlocks += static_cast(length) - xmmBlockSize; inIncrement = 0-inIncrement; xorIncrement = 0-xorIncrement; outIncrement = 0-outIncrement; @@ -916,18 +917,18 @@ inline size_t AdvancedProcessBlocks128_SSE2x6(F2 func2, F6 func6, CRYPTOPP_ASSERT(outBlocks); CRYPTOPP_ASSERT(length >= 16); - const size_t blockSize = 16; - // const size_t xmmBlockSize = 16; + const ptrdiff_t blockSize = 16; + // const ptrdiff_t xmmBlockSize = 16; - size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; - size_t xorIncrement = xorBlocks ? blockSize : 0; - size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; + ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; + ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0; + ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; if (flags & BT_ReverseDirection) { - inBlocks += length - blockSize; - xorBlocks += length - blockSize; - outBlocks += length - blockSize; + inBlocks += static_cast(length) - blockSize; + xorBlocks += static_cast(length) - blockSize; + outBlocks += static_cast(length) - blockSize; inIncrement = 0-inIncrement; xorIncrement = 0-xorIncrement; outIncrement = 0-outIncrement; @@ -1101,18 +1102,18 @@ inline size_t AdvancedProcessBlocks128_SSE1x4(F1 func1, F4 func4, CRYPTOPP_ASSERT(outBlocks); CRYPTOPP_ASSERT(length >= 16); - const size_t blockSize = 16; - // const size_t xmmBlockSize = 16; + const ptrdiff_t blockSize = 16; + // const ptrdiff_t xmmBlockSize = 16; - size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; - size_t xorIncrement = xorBlocks ? blockSize : 0; - size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; + ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; + ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0; + ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; if (flags & BT_ReverseDirection) { - inBlocks += length - blockSize; - xorBlocks += length - blockSize; - outBlocks += length - blockSize; + inBlocks += static_cast(length) - blockSize; + xorBlocks += static_cast(length) - blockSize; + outBlocks += static_cast(length) - blockSize; inIncrement = 0-inIncrement; xorIncrement = 0-xorIncrement; outIncrement = 0-outIncrement; diff --git a/cryptlib.cpp b/cryptlib.cpp index 03f0f0ff..55df7134 100644 --- a/cryptlib.cpp +++ b/cryptlib.cpp @@ -26,6 +26,7 @@ #include "osrng.h" #include "secblock.h" #include "smartptr.h" +#include "stdcpp.h" // http://www.cygwin.com/faq.html#faq.api.winsock #if (defined(__CYGWIN__) || defined(__CYGWIN32__)) && defined(PREFER_WINDOWS_STYLE_SOCKETS) @@ -147,14 +148,13 @@ size_t BlockTransformation::AdvancedProcessBlocks(const byte *inBlocks, const by CRYPTOPP_ASSERT(outBlocks); CRYPTOPP_ASSERT(length); - size_t blockSize = BlockSize(); - size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; - size_t xorIncrement = xorBlocks ? blockSize : 0; - size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; + ptrdiff_t blockSize = static_cast(BlockSize()); + ptrdiff_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize; + ptrdiff_t xorIncrement = xorBlocks ? blockSize : 0; + ptrdiff_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize; if (flags & BT_ReverseDirection) { - CRYPTOPP_ASSERT(length % blockSize == 0); inBlocks += length - blockSize; xorBlocks += length - blockSize; outBlocks += length - blockSize; @@ -164,7 +164,7 @@ size_t BlockTransformation::AdvancedProcessBlocks(const byte *inBlocks, const by } // Coverity finding. - bool xorFlag = xorBlocks && (flags & BT_XorInput); + const bool xorFlag = xorBlocks && (flags & BT_XorInput); while (length >= blockSize) { if (xorFlag) @@ -181,6 +181,7 @@ size_t BlockTransformation::AdvancedProcessBlocks(const byte *inBlocks, const by if (flags & BT_InBlockIsCounter) const_cast(inBlocks)[blockSize-1]++; + inBlocks += inIncrement; outBlocks += outIncrement; xorBlocks += xorIncrement; diff --git a/seed.cpp b/seed.cpp index a610b3e6..4e99d759 100644 --- a/seed.cpp +++ b/seed.cpp @@ -3,14 +3,18 @@ #include "pch.h" #include "seed.h" #include "misc.h" +#include "stdcpp.h" -NAMESPACE_BEGIN(CryptoPP) +ANONYMOUS_NAMESPACE_BEGIN -static const word32 s_kc[16] = { +using CryptoPP::byte; +using CryptoPP::word32; + +const word32 s_kc[16] = { 0x9e3779b9, 0x3c6ef373, 0x78dde6e6, 0xf1bbcdcc, 0xe3779b99, 0xc6ef3733, 0x8dde6e67, 0x1bbcdccf, 0x3779b99e, 0x6ef3733c, 0xdde6e678, 0xbbcdccf1, 0x779b99e3, 0xef3733c6, 0xde6e678d, 0xbcdccf1b}; -static const byte s_s0[256] = { +const byte s_s0[256] = { 0xA9, 0x85, 0xD6, 0xD3, 0x54, 0x1D, 0xAC, 0x25, 0x5D, 0x43, 0x18, 0x1E, 0x51, 0xFC, 0xCA, 0x63, 0x28, 0x44, 0x20, 0x9D, 0xE0, 0xE2, 0xC8, 0x17, 0xA5, 0x8F, 0x03, 0x7B, 0xBB, 0x13, 0xD2, 0xEE, 0x70, 0x8C, 0x3F, 0xA8, 0x32, 0xDD, 0xF6, 0x74, 0xEC, 0x95, 0x0B, 0x57, 0x5C, 0x5B, 0xBD, 0x01, 0x24, 0x1C, 0x73, @@ -52,6 +56,10 @@ static const byte s_s1[256] = { #define SS3(x) ((s_s1[x]*0x01010101UL) & 0xCFF3FC3F) #define G(x) (SS0(GETBYTE(x, 0)) ^ SS1(GETBYTE(x, 1)) ^ SS2(GETBYTE(x, 2)) ^ SS3(GETBYTE(x, 3))) +ANONYMOUS_NAMESPACE_END + +NAMESPACE_BEGIN(CryptoPP) + void SEED::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs& /*params*/) { AssertValidKeyLength(length); @@ -60,7 +68,8 @@ void SEED::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const GetBlock get(userKey); get(key01)(key23); word32 *k = m_k; - size_t kInc = 2; + ptrdiff_t kInc = 2; + if (!IsForwardTransformation()) { k = k+30;