Add EnumToInt conversion macro for enum-enum conversion warnings (GH #1016)

This commit is contained in:
Jeffrey Walton 2021-03-09 22:51:19 -05:00
parent 5c4c631e92
commit f7e6af6344
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
10 changed files with 66 additions and 55 deletions

View File

@ -112,13 +112,13 @@ inline size_t AdvancedProcessBlocks128_6x1_NEON(F1 func1, F6 func6,
const size_t blockSize = 16;
// const size_t neonBlockSize = 16;
size_t inIncrement = (flags & (static_cast<int>(BT_InBlockIsCounter)|static_cast<int>(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t inIncrement = (flags & (EnumToInt(BT_InBlockIsCounter)|EnumToInt(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t xorIncrement = (xorBlocks != NULLPTR) ? blockSize : 0;
size_t outIncrement = (flags & static_cast<int>(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
size_t outIncrement = (flags & EnumToInt(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
// Clang and Coverity are generating findings using xorBlocks as a flag.
const bool xorInput = (xorBlocks != NULLPTR) && (flags & static_cast<int>(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast<int>(BT_XorInput));
const bool xorInput = (xorBlocks != NULLPTR) && (flags & EnumToInt(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & EnumToInt(BT_XorInput));
if (flags & BT_ReverseDirection)
{
@ -266,13 +266,13 @@ inline size_t AdvancedProcessBlocks128_4x1_NEON(F1 func1, F4 func4,
const size_t blockSize = 16;
// const size_t neonBlockSize = 16;
size_t inIncrement = (flags & (static_cast<int>(BT_InBlockIsCounter)|static_cast<int>(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t inIncrement = (flags & (EnumToInt(BT_InBlockIsCounter)|EnumToInt(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t xorIncrement = (xorBlocks != NULLPTR) ? blockSize : 0;
size_t outIncrement = (flags & static_cast<int>(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
size_t outIncrement = (flags & EnumToInt(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
// Clang and Coverity are generating findings using xorBlocks as a flag.
const bool xorInput = (xorBlocks != NULLPTR) && (flags & static_cast<int>(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast<int>(BT_XorInput));
const bool xorInput = (xorBlocks != NULLPTR) && (flags & EnumToInt(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & EnumToInt(BT_XorInput));
if (flags & BT_ReverseDirection)
{
@ -400,13 +400,13 @@ inline size_t AdvancedProcessBlocks128_6x2_NEON(F2 func2, F6 func6,
const size_t blockSize = 16;
// const size_t neonBlockSize = 16;
size_t inIncrement = (flags & (static_cast<int>(BT_InBlockIsCounter)|static_cast<int>(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t inIncrement = (flags & (EnumToInt(BT_InBlockIsCounter)|EnumToInt(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t xorIncrement = (xorBlocks != NULLPTR) ? blockSize : 0;
size_t outIncrement = (flags & static_cast<int>(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
size_t outIncrement = (flags & EnumToInt(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
// Clang and Coverity are generating findings using xorBlocks as a flag.
const bool xorInput = (xorBlocks != NULLPTR) && (flags & static_cast<int>(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast<int>(BT_XorInput));
const bool xorInput = (xorBlocks != NULLPTR) && (flags & EnumToInt(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & EnumToInt(BT_XorInput));
if (flags & BT_ReverseDirection)
{
@ -644,13 +644,13 @@ inline size_t AdvancedProcessBlocks128_6x2_SSE(F2 func2, F6 func6,
const size_t blockSize = 16;
// const size_t xmmBlockSize = 16;
size_t inIncrement = (flags & (static_cast<int>(BT_InBlockIsCounter)|static_cast<int>(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t inIncrement = (flags & (EnumToInt(BT_InBlockIsCounter)|EnumToInt(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t xorIncrement = (xorBlocks != NULLPTR) ? blockSize : 0;
size_t outIncrement = (flags & static_cast<int>(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
size_t outIncrement = (flags & EnumToInt(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
// Clang and Coverity are generating findings using xorBlocks as a flag.
const bool xorInput = (xorBlocks != NULLPTR) && (flags & static_cast<int>(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast<int>(BT_XorInput));
const bool xorInput = (xorBlocks != NULLPTR) && (flags & EnumToInt(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & EnumToInt(BT_XorInput));
if (flags & BT_ReverseDirection)
{
@ -839,13 +839,13 @@ inline size_t AdvancedProcessBlocks128_4x1_SSE(F1 func1, F4 func4,
const size_t blockSize = 16;
// const size_t xmmBlockSize = 16;
size_t inIncrement = (flags & (static_cast<int>(BT_InBlockIsCounter)|static_cast<int>(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t inIncrement = (flags & (EnumToInt(BT_InBlockIsCounter)|EnumToInt(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t xorIncrement = (xorBlocks != NULLPTR) ? blockSize : 0;
size_t outIncrement = (flags & static_cast<int>(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
size_t outIncrement = (flags & EnumToInt(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
// Clang and Coverity are generating findings using xorBlocks as a flag.
const bool xorInput = (xorBlocks != NULLPTR) && (flags & static_cast<int>(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast<int>(BT_XorInput));
const bool xorInput = (xorBlocks != NULLPTR) && (flags & EnumToInt(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & EnumToInt(BT_XorInput));
if (flags & BT_ReverseDirection)
{
@ -986,13 +986,13 @@ inline size_t AdvancedProcessBlocks128_4x1_ALTIVEC(F1 func1, F4 func4,
const size_t blockSize = 16;
// const size_t simdBlockSize = 16;
size_t inIncrement = (flags & (static_cast<int>(BT_InBlockIsCounter)|static_cast<int>(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t inIncrement = (flags & (EnumToInt(BT_InBlockIsCounter)|EnumToInt(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t xorIncrement = (xorBlocks != NULLPTR) ? blockSize : 0;
size_t outIncrement = (flags & static_cast<int>(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
size_t outIncrement = (flags & EnumToInt(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
// Clang and Coverity are generating findings using xorBlocks as a flag.
const bool xorInput = (xorBlocks != NULLPTR) && (flags & static_cast<int>(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast<int>(BT_XorInput));
const bool xorInput = (xorBlocks != NULLPTR) && (flags & EnumToInt(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & EnumToInt(BT_XorInput));
if (flags & BT_ReverseDirection)
{
@ -1131,13 +1131,13 @@ inline size_t AdvancedProcessBlocks128_6x1_ALTIVEC(F1 func1, F6 func6,
const size_t blockSize = 16;
// const size_t simdBlockSize = 16;
size_t inIncrement = (flags & (static_cast<int>(BT_InBlockIsCounter)|static_cast<int>(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t inIncrement = (flags & (EnumToInt(BT_InBlockIsCounter)|EnumToInt(BT_DontIncrementInOutPointers))) ? 0 : blockSize;
size_t xorIncrement = (xorBlocks != NULLPTR) ? blockSize : 0;
size_t outIncrement = (flags & static_cast<int>(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
size_t outIncrement = (flags & EnumToInt(BT_DontIncrementInOutPointers)) ? 0 : blockSize;
// Clang and Coverity are generating findings using xorBlocks as a flag.
const bool xorInput = (xorBlocks != NULLPTR) && (flags & static_cast<int>(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast<int>(BT_XorInput));
const bool xorInput = (xorBlocks != NULLPTR) && (flags & EnumToInt(BT_XorInput));
const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & EnumToInt(BT_XorInput));
if (flags & BT_ReverseDirection)
{

12
asn.h
View File

@ -380,7 +380,7 @@ class CRYPTOPP_DLL BERGeneralDecoder : public Store
{
public:
/// \brief Default ASN.1 tag
enum {DefaultTag = SEQUENCE | static_cast<int>(CONSTRUCTED)};
enum {DefaultTag = SEQUENCE | EnumToInt(CONSTRUCTED)};
virtual ~BERGeneralDecoder();
@ -491,7 +491,7 @@ class CRYPTOPP_DLL DERGeneralEncoder : public ByteQueue
{
public:
/// \brief Default ASN.1 tag
enum {DefaultTag = SEQUENCE | static_cast<int>(CONSTRUCTED)};
enum {DefaultTag = SEQUENCE | EnumToInt(CONSTRUCTED)};
virtual ~DERGeneralEncoder();
@ -525,7 +525,7 @@ class CRYPTOPP_DLL BERSequenceDecoder : public BERGeneralDecoder
{
public:
/// \brief Default ASN.1 tag
enum {DefaultTag = SEQUENCE | static_cast<int>(CONSTRUCTED)};
enum {DefaultTag = SEQUENCE | EnumToInt(CONSTRUCTED)};
/// \brief Construct an ASN.1 decoder
/// \param inQueue input byte queue
@ -557,7 +557,7 @@ class CRYPTOPP_DLL DERSequenceEncoder : public DERGeneralEncoder
{
public:
/// \brief Default ASN.1 tag
enum {DefaultTag = SEQUENCE | static_cast<int>(CONSTRUCTED)};
enum {DefaultTag = SEQUENCE | EnumToInt(CONSTRUCTED)};
/// \brief Construct an ASN.1 encoder
/// \param outQueue output byte queue
@ -589,7 +589,7 @@ class CRYPTOPP_DLL BERSetDecoder : public BERGeneralDecoder
{
public:
/// \brief Default ASN.1 tag
enum {DefaultTag = SET | static_cast<int>(CONSTRUCTED)};
enum {DefaultTag = SET | EnumToInt(CONSTRUCTED)};
/// \brief Construct an ASN.1 decoder
/// \param inQueue input byte queue
@ -621,7 +621,7 @@ class CRYPTOPP_DLL DERSetEncoder : public DERGeneralEncoder
{
public:
/// \brief Default ASN.1 tag
enum {DefaultTag = SET | static_cast<int>(CONSTRUCTED)};
enum {DefaultTag = SET | EnumToInt(CONSTRUCTED)};
/// \brief Construct an ASN.1 encoder
/// \param outQueue output byte queue

View File

@ -102,7 +102,7 @@ void ChaCha_OperateKeystream(KeystreamOperation operation,
{
while (iterationCount >= 8 && MultiBlockSafe(state[12], 8))
{
const bool xorInput = (operation & static_cast<int>(INPUT_NULL)) != static_cast<int>(INPUT_NULL);
const bool xorInput = (operation & EnumToInt(INPUT_NULL)) != EnumToInt(INPUT_NULL);
ChaCha_OperateKeystream_AVX2(state, xorInput ? input : NULLPTR, output, rounds);
// MultiBlockSafe avoids overflow on the counter words
@ -120,7 +120,7 @@ void ChaCha_OperateKeystream(KeystreamOperation operation,
{
while (iterationCount >= 4 && MultiBlockSafe(state[12], 4))
{
const bool xorInput = (operation & static_cast<int>(INPUT_NULL)) != static_cast<int>(INPUT_NULL);
const bool xorInput = (operation & EnumToInt(INPUT_NULL)) != EnumToInt(INPUT_NULL);
ChaCha_OperateKeystream_SSE2(state, xorInput ? input : NULLPTR, output, rounds);
// MultiBlockSafe avoids overflow on the counter words
@ -138,7 +138,7 @@ void ChaCha_OperateKeystream(KeystreamOperation operation,
{
while (iterationCount >= 4 && MultiBlockSafe(state[12], 4))
{
const bool xorInput = (operation & static_cast<int>(INPUT_NULL)) != static_cast<int>(INPUT_NULL);
const bool xorInput = (operation & EnumToInt(INPUT_NULL)) != EnumToInt(INPUT_NULL);
ChaCha_OperateKeystream_NEON(state, xorInput ? input : NULLPTR, output, rounds);
// MultiBlockSafe avoids overflow on the counter words
@ -156,7 +156,7 @@ void ChaCha_OperateKeystream(KeystreamOperation operation,
{
while (iterationCount >= 4 && MultiBlockSafe(state[12], 4))
{
const bool xorInput = (operation & static_cast<int>(INPUT_NULL)) != static_cast<int>(INPUT_NULL);
const bool xorInput = (operation & EnumToInt(INPUT_NULL)) != EnumToInt(INPUT_NULL);
ChaCha_OperateKeystream_ALTIVEC(state, xorInput ? input : NULLPTR, output, rounds);
// MultiBlockSafe avoids overflow on the counter words

View File

@ -73,8 +73,8 @@ static void GenerateKeyIV(const byte *passphrase, size_t passphraseLength, const
memcpy(temp+passphraseLength, salt, saltLength);
// OK. Derived params, cannot be NULL
SecByteBlock keyIV(static_cast<int>(Info::KEYLENGTH)+static_cast<int>(+Info::BLOCKSIZE));
Mash<H>(temp, passphraseLength + saltLength, keyIV, static_cast<int>(Info::KEYLENGTH)+static_cast<int>(+Info::BLOCKSIZE), iterations);
SecByteBlock keyIV(EnumToInt(Info::KEYLENGTH)+EnumToInt(+Info::BLOCKSIZE));
Mash<H>(temp, passphraseLength + saltLength, keyIV, EnumToInt(Info::KEYLENGTH)+EnumToInt(+Info::BLOCKSIZE), iterations);
memcpy(key, keyIV, Info::KEYLENGTH);
memcpy(IV, keyIV+Info::KEYLENGTH, Info::BLOCKSIZE);
}
@ -140,7 +140,7 @@ void DataEncryptor<BC,H,Info>::LastPut(const byte *inString, size_t length)
template <class BC, class H, class Info>
DataDecryptor<BC,H,Info>::DataDecryptor(const char *p, BufferedTransformation *attachment, bool throwException)
: ProxyFilter(NULLPTR, static_cast<int>(SALTLENGTH)+static_cast<int>(BLOCKSIZE), 0, attachment)
: ProxyFilter(NULLPTR, EnumToInt(SALTLENGTH)+EnumToInt(BLOCKSIZE), 0, attachment)
, m_state(WAITING_FOR_KEYCHECK)
, m_passphrase((const byte *)p, strlen(p))
, m_throwException(throwException)
@ -151,7 +151,7 @@ DataDecryptor<BC,H,Info>::DataDecryptor(const char *p, BufferedTransformation *a
template <class BC, class H, class Info>
DataDecryptor<BC,H,Info>::DataDecryptor(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment, bool throwException)
: ProxyFilter(NULLPTR, static_cast<int>(SALTLENGTH)+static_cast<int>(BLOCKSIZE), 0, attachment)
: ProxyFilter(NULLPTR, EnumToInt(SALTLENGTH)+EnumToInt(BLOCKSIZE), 0, attachment)
, m_state(WAITING_FOR_KEYCHECK)
, m_passphrase(passphrase, passphraseLength)
, m_throwException(throwException)
@ -202,11 +202,11 @@ void DataDecryptor<BC,H,Info>::CheckKey(const byte *salt, const byte *keyCheck)
decryptor->Put(keyCheck, BLOCKSIZE);
decryptor->ForceNextPut();
decryptor->Get(check+static_cast<int>(BLOCKSIZE), BLOCKSIZE);
decryptor->Get(check+EnumToInt(BLOCKSIZE), BLOCKSIZE);
SetFilter(decryptor.release());
if (!VerifyBufsEqual(check, check+static_cast<int>(BLOCKSIZE), BLOCKSIZE))
if (!VerifyBufsEqual(check, check+EnumToInt(BLOCKSIZE), BLOCKSIZE))
{
m_state = KEY_BAD;
if (m_throwException)

View File

@ -229,7 +229,7 @@ void HC128Policy::OperateKeystream(KeystreamOperation operation, byte *output, c
// writing the result to the output buffer. In all cases the
// keystream is written to the output buffer. The optional part is
// adding the input buffer and keystream.
if ((operation & static_cast<int>(INPUT_NULL)) != static_cast<int>(INPUT_NULL))
if ((operation & EnumToInt(INPUT_NULL)) != EnumToInt(INPUT_NULL))
{
xorbuf(output, input, BYTES_PER_ITERATION);
input += BYTES_PER_ITERATION;

View File

@ -107,7 +107,7 @@ void HC256Policy::OperateKeystream(KeystreamOperation operation, byte *output, c
// writing the result to the output buffer. In all cases the
// keystream is written to the output buffer. The optional part is
// adding the input buffer and keystream.
if ((operation & static_cast<int>(INPUT_NULL)) != static_cast<int>(INPUT_NULL))
if ((operation & EnumToInt(INPUT_NULL)) != EnumToInt(INPUT_NULL))
{
xorbuf(output, input, BYTES_PER_ITERATION);
input += BYTES_PER_ITERATION;

11
misc.h
View File

@ -473,6 +473,7 @@ const byte* ConstBytePtr(const SecByteBlock& str);
/// \brief Size of a string
/// \param str std::string
/// \return size of a string
/// \since Crypto++ 8.3
inline size_t BytePtrSize(const std::string& str)
{
return str.size();
@ -481,8 +482,18 @@ inline size_t BytePtrSize(const std::string& str)
/// \brief Size of a string
/// \param str SecByteBlock
/// \return size of a string
/// \since Crypto++ 8.3
size_t BytePtrSize(const SecByteBlock& str);
/// \brief Integer value
/// \details EnumToInt is a convenience macro to convert enums
/// to integers. The cast avoids C++20 enum-enum conversion
/// warnings.
/// \details A macro is used due to [lack of] constexpr-ness in
/// early versions of C++.
/// \since Crypto++ 8.6
#define EnumToInt(v) static_cast<int>(v)
#if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB)
/// \brief Bounds checking replacement for memcpy()

View File

@ -245,7 +245,7 @@ void AutoSeededX917RNG<BLOCK_CIPHER>::Reseed(bool blocking, const byte *input, s
{
enum {BlockSize=BLOCK_CIPHER::BLOCKSIZE};
enum {KeyLength=BLOCK_CIPHER::DEFAULT_KEYLENGTH};
enum {SeedSize=static_cast<int>(BlockSize)+static_cast<int>(KeyLength)};
enum {SeedSize=EnumToInt(BlockSize)+EnumToInt(KeyLength)};
SecByteBlock seed(SeedSize), temp(SeedSize);
const byte label[] = "X9.17 key generation";

View File

@ -150,7 +150,7 @@ void RabbitPolicy::OperateKeystream(KeystreamOperation operation, byte *output,
// writing the result to the output buffer. In all cases the
// keystream is written to the output buffer. The optional part is
// adding the input buffer and keystream.
if ((operation & static_cast<int>(INPUT_NULL)) != static_cast<int>(INPUT_NULL))
if ((operation & EnumToInt(INPUT_NULL)) != EnumToInt(INPUT_NULL))
xorbuf(output, input, GetBytesPerIteration() * iterationCount);
}
@ -252,7 +252,7 @@ void RabbitWithIVPolicy::OperateKeystream(KeystreamOperation operation, byte *ou
// writing the result to the output buffer. In all cases the
// keystream is written to the output buffer. The optional part is
// adding the input buffer and keystream.
if ((operation & static_cast<int>(INPUT_NULL)) != static_cast<int>(INPUT_NULL))
if ((operation & EnumToInt(INPUT_NULL)) != EnumToInt(INPUT_NULL))
xorbuf(output, input, GetBytesPerIteration() * iterationCount);
}

View File

@ -269,25 +269,25 @@ struct CRYPTOPP_NO_VTABLE AdditiveCipherConcretePolicy : public BASE
switch (operation) \
{ \
case WRITE_KEYSTREAM: \
x(static_cast<int>(WRITE_KEYSTREAM)) \
x(EnumToInt(WRITE_KEYSTREAM)) \
break; \
case XOR_KEYSTREAM: \
x(static_cast<int>(XOR_KEYSTREAM)) \
x(EnumToInt(XOR_KEYSTREAM)) \
input += y; \
break; \
case XOR_KEYSTREAM_INPUT_ALIGNED: \
x(static_cast<int>(XOR_KEYSTREAM_INPUT_ALIGNED)) \
x(EnumToInt(XOR_KEYSTREAM_INPUT_ALIGNED)) \
input += y; \
break; \
case XOR_KEYSTREAM_OUTPUT_ALIGNED: \
x(static_cast<int>(XOR_KEYSTREAM_OUTPUT_ALIGNED)) \
x(EnumToInt(XOR_KEYSTREAM_OUTPUT_ALIGNED)) \
input += y; \
break; \
case WRITE_KEYSTREAM_ALIGNED: \
x(static_cast<int>(WRITE_KEYSTREAM_ALIGNED)) \
x(EnumToInt(WRITE_KEYSTREAM_ALIGNED)) \
break; \
case XOR_KEYSTREAM_BOTH_ALIGNED: \
x(static_cast<int>(XOR_KEYSTREAM_BOTH_ALIGNED)) \
x(EnumToInt(XOR_KEYSTREAM_BOTH_ALIGNED)) \
input += y; \
break; \
} \