diff --git a/adv_simd.h b/adv_simd.h index 402d0ee4..0b65691d 100644 --- a/adv_simd.h +++ b/adv_simd.h @@ -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(BT_InBlockIsCounter)|static_cast(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(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(BT_XorInput)); - const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast(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(BT_InBlockIsCounter)|static_cast(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(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(BT_XorInput)); - const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast(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(BT_InBlockIsCounter)|static_cast(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(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(BT_XorInput)); - const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast(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(BT_InBlockIsCounter)|static_cast(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(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(BT_XorInput)); - const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast(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(BT_InBlockIsCounter)|static_cast(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(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(BT_XorInput)); - const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast(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(BT_InBlockIsCounter)|static_cast(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(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(BT_XorInput)); - const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast(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(BT_InBlockIsCounter)|static_cast(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(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(BT_XorInput)); - const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & static_cast(BT_XorInput)); + const bool xorInput = (xorBlocks != NULLPTR) && (flags & EnumToInt(BT_XorInput)); + const bool xorOutput = (xorBlocks != NULLPTR) && !(flags & EnumToInt(BT_XorInput)); if (flags & BT_ReverseDirection) { diff --git a/asn.h b/asn.h index 84a739c5..d021bb63 100644 --- a/asn.h +++ b/asn.h @@ -380,7 +380,7 @@ class CRYPTOPP_DLL BERGeneralDecoder : public Store { public: /// \brief Default ASN.1 tag - enum {DefaultTag = SEQUENCE | static_cast(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(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(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(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(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(CONSTRUCTED)}; + enum {DefaultTag = SET | EnumToInt(CONSTRUCTED)}; /// \brief Construct an ASN.1 encoder /// \param outQueue output byte queue diff --git a/chacha.cpp b/chacha.cpp index c1b0f559..5c50d52c 100644 --- a/chacha.cpp +++ b/chacha.cpp @@ -102,7 +102,7 @@ void ChaCha_OperateKeystream(KeystreamOperation operation, { while (iterationCount >= 8 && MultiBlockSafe(state[12], 8)) { - const bool xorInput = (operation & static_cast(INPUT_NULL)) != static_cast(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(INPUT_NULL)) != static_cast(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(INPUT_NULL)) != static_cast(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(INPUT_NULL)) != static_cast(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 diff --git a/default.cpp b/default.cpp index a318f07b..1723a015 100644 --- a/default.cpp +++ b/default.cpp @@ -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(Info::KEYLENGTH)+static_cast(+Info::BLOCKSIZE)); - Mash(temp, passphraseLength + saltLength, keyIV, static_cast(Info::KEYLENGTH)+static_cast(+Info::BLOCKSIZE), iterations); + SecByteBlock keyIV(EnumToInt(Info::KEYLENGTH)+EnumToInt(+Info::BLOCKSIZE)); + Mash(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::LastPut(const byte *inString, size_t length) template DataDecryptor::DataDecryptor(const char *p, BufferedTransformation *attachment, bool throwException) - : ProxyFilter(NULLPTR, static_cast(SALTLENGTH)+static_cast(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::DataDecryptor(const char *p, BufferedTransformation *a template DataDecryptor::DataDecryptor(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment, bool throwException) - : ProxyFilter(NULLPTR, static_cast(SALTLENGTH)+static_cast(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::CheckKey(const byte *salt, const byte *keyCheck) decryptor->Put(keyCheck, BLOCKSIZE); decryptor->ForceNextPut(); - decryptor->Get(check+static_cast(BLOCKSIZE), BLOCKSIZE); + decryptor->Get(check+EnumToInt(BLOCKSIZE), BLOCKSIZE); SetFilter(decryptor.release()); - if (!VerifyBufsEqual(check, check+static_cast(BLOCKSIZE), BLOCKSIZE)) + if (!VerifyBufsEqual(check, check+EnumToInt(BLOCKSIZE), BLOCKSIZE)) { m_state = KEY_BAD; if (m_throwException) diff --git a/hc128.cpp b/hc128.cpp index 8590b866..74f67c17 100644 --- a/hc128.cpp +++ b/hc128.cpp @@ -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(INPUT_NULL)) != static_cast(INPUT_NULL)) + if ((operation & EnumToInt(INPUT_NULL)) != EnumToInt(INPUT_NULL)) { xorbuf(output, input, BYTES_PER_ITERATION); input += BYTES_PER_ITERATION; diff --git a/hc256.cpp b/hc256.cpp index b2fc592c..9259b87a 100644 --- a/hc256.cpp +++ b/hc256.cpp @@ -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(INPUT_NULL)) != static_cast(INPUT_NULL)) + if ((operation & EnumToInt(INPUT_NULL)) != EnumToInt(INPUT_NULL)) { xorbuf(output, input, BYTES_PER_ITERATION); input += BYTES_PER_ITERATION; diff --git a/misc.h b/misc.h index 5f9e6eed..b2272ae9 100644 --- a/misc.h +++ b/misc.h @@ -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(v) + #if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB) /// \brief Bounds checking replacement for memcpy() diff --git a/osrng.h b/osrng.h index 60b02cfd..bd534d58 100644 --- a/osrng.h +++ b/osrng.h @@ -245,7 +245,7 @@ void AutoSeededX917RNG::Reseed(bool blocking, const byte *input, s { enum {BlockSize=BLOCK_CIPHER::BLOCKSIZE}; enum {KeyLength=BLOCK_CIPHER::DEFAULT_KEYLENGTH}; - enum {SeedSize=static_cast(BlockSize)+static_cast(KeyLength)}; + enum {SeedSize=EnumToInt(BlockSize)+EnumToInt(KeyLength)}; SecByteBlock seed(SeedSize), temp(SeedSize); const byte label[] = "X9.17 key generation"; diff --git a/rabbit.cpp b/rabbit.cpp index 0b6d8273..4f655111 100644 --- a/rabbit.cpp +++ b/rabbit.cpp @@ -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(INPUT_NULL)) != static_cast(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(INPUT_NULL)) != static_cast(INPUT_NULL)) + if ((operation & EnumToInt(INPUT_NULL)) != EnumToInt(INPUT_NULL)) xorbuf(output, input, GetBytesPerIteration() * iterationCount); } diff --git a/strciphr.h b/strciphr.h index 677ee808..db52b494 100644 --- a/strciphr.h +++ b/strciphr.h @@ -269,25 +269,25 @@ struct CRYPTOPP_NO_VTABLE AdditiveCipherConcretePolicy : public BASE switch (operation) \ { \ case WRITE_KEYSTREAM: \ - x(static_cast(WRITE_KEYSTREAM)) \ + x(EnumToInt(WRITE_KEYSTREAM)) \ break; \ case XOR_KEYSTREAM: \ - x(static_cast(XOR_KEYSTREAM)) \ + x(EnumToInt(XOR_KEYSTREAM)) \ input += y; \ break; \ case XOR_KEYSTREAM_INPUT_ALIGNED: \ - x(static_cast(XOR_KEYSTREAM_INPUT_ALIGNED)) \ + x(EnumToInt(XOR_KEYSTREAM_INPUT_ALIGNED)) \ input += y; \ break; \ case XOR_KEYSTREAM_OUTPUT_ALIGNED: \ - x(static_cast(XOR_KEYSTREAM_OUTPUT_ALIGNED)) \ + x(EnumToInt(XOR_KEYSTREAM_OUTPUT_ALIGNED)) \ input += y; \ break; \ case WRITE_KEYSTREAM_ALIGNED: \ - x(static_cast(WRITE_KEYSTREAM_ALIGNED)) \ + x(EnumToInt(WRITE_KEYSTREAM_ALIGNED)) \ break; \ case XOR_KEYSTREAM_BOTH_ALIGNED: \ - x(static_cast(XOR_KEYSTREAM_BOTH_ALIGNED)) \ + x(EnumToInt(XOR_KEYSTREAM_BOTH_ALIGNED)) \ input += y; \ break; \ } \