From fea45591b219e91e68b4a588d004b27a3a8c629a Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 4 Sep 2016 02:29:32 -0400 Subject: [PATCH 01/28] Add constexpr to size_max() methods for C++11 --- secblock.h | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/secblock.h b/secblock.h index 442fd096..862f28bd 100644 --- a/secblock.h +++ b/secblock.h @@ -52,8 +52,8 @@ public: //! because the latter is \a not a \a constexpr. Some compilers, like Clang, do not //! optimize it well under all circumstances. Compilers like GCC, ICC and MSVC appear //! to optimize it well in either form. - size_type max_size() const {return (SIZE_MAX/sizeof(T));} - + CRYPTOPP_CONSTEXPR size_type max_size() const {return (SIZE_MAX/sizeof(T));} + #if defined(CRYPTOPP_CXX11_VARIADIC_TEMPLATES) || defined(CRYPTOPP_DOXYGEN_PROCESSING) //! \brief Constructs a new U using variadic arguments @@ -65,7 +65,7 @@ public: //! is defined. The define is controlled by compiler versions detected in config.h. template void construct(U* ptr, Args&&... args) {::new ((void*)ptr) U(std::forward(args)...);} - + //! \brief Destroys an U constructed with variadic arguments //! \tparam U the type to be forwarded //! \details This is a C++11 feature. It is available when CRYPTOPP_CXX11_VARIADIC_TEMPLATES @@ -76,11 +76,11 @@ public: #endif protected: - + //! \brief Verifies the allocator can satisfy a request based on size //! \param size the size of the allocation, in elements //! \throws InvalidArgument - //! \details CheckSize verifies the number of elements requested is valid. + //! \details CheckSize verifies the number of elements requested is valid. //! \details If size is greater than max_size(), then InvalidArgument is thrown. //! The library throws InvalidArgument if the size is too large to satisfy. //! \details Internally, preprocessor macros are used rather than std::numeric_limits @@ -126,7 +126,7 @@ typename A::pointer StandardReallocate(A& alloc, T *oldPtr, typename A::size_typ { typename A::pointer newPointer = alloc.allocate(newSize, NULL); const size_t copySize = STDMIN(oldSize, newSize) * sizeof(T); - + if (oldPtr && newPointer) {memcpy_s(newPointer, copySize, oldPtr, copySize);} alloc.deallocate(oldPtr, oldSize); return newPointer; @@ -140,7 +140,7 @@ typename A::pointer StandardReallocate(A& alloc, T *oldPtr, typename A::size_typ //! \class AllocatorWithCleanup //! \brief Allocates a block of memory with cleanup -//! \tparam T class or type +//! \tparam T class or type //! \tparam T_Align16 boolean that determines whether allocations should be aligned on 16-byte boundaries //! \details If T_Align16 is true, then AllocatorWithCleanup calls AlignedAllocate() //! for memory allocations. If T_Align16 is false, then AllocatorWithCleanup() calls @@ -162,10 +162,10 @@ public: //! and less than max_size(), then an attempt is made to fulfill the request using either //! AlignedAllocate() or UnalignedAllocate(). //! \details AlignedAllocate() is used if T_Align16 is true. - //! UnalignedAllocate() used if T_Align16 is false. + //! UnalignedAllocate() used if T_Align16 is false. //! \details This is the C++ *Placement New* operator. ptr is not used, and the function //! asserts in Debug builds if ptr is non-NULL. - //! \sa CallNewHandler() for the methods used to recover from a failed + //! \sa CallNewHandler() for the methods used to recover from a failed //! allocation attempt. //! \note size is the count of elements, and not the number of bytes pointer allocate(size_type size, const void *ptr = NULL) @@ -188,10 +188,10 @@ public: //! \param ptr the pointer for the allocation //! \param size the size of the allocation, in elements //! \details Internally, SecureWipeArray() is called before deallocating the memory. - //! Once the memory block is wiped or zeroized, AlignedDeallocate() or + //! Once the memory block is wiped or zeroized, AlignedDeallocate() or //! UnalignedDeallocate() is called. //! \details AlignedDeallocate() is used if T_Align16 is true. - //! UnalignedDeallocate() used if T_Align16 is false. + //! UnalignedDeallocate() used if T_Align16 is false. void deallocate(void *ptr, size_type size) { assert((ptr && size) || !(ptr || size)); @@ -278,7 +278,7 @@ public: assert(false); } - size_type max_size() const {return 0;} + CRYPTOPP_CONSTEXPR size_type max_size() const {return 0;} //LCOV_EXCL_STOP }; @@ -410,7 +410,7 @@ public: return newPointer; } - size_type max_size() const {return STDMAX(m_fallbackAllocator.max_size(), S);} + CRYPTOPP_CONSTEXPR size_type max_size() const {return STDMAX(m_fallbackAllocator.max_size(), S);} private: @@ -446,7 +446,7 @@ public: //! \note size is the count of elements, and not the number of bytes explicit SecBlock(size_type size=0) : m_size(size), m_ptr(m_alloc.allocate(size, NULL)) { } - + //! \brief Copy construct a SecBlock from another SecBlock //! \param t the other SecBlock //! \throws std::bad_alloc @@ -460,7 +460,7 @@ public: //! \param ptr a pointer to an array of T //! \param len the number of elements in the memory block //! \throws std::bad_alloc - //! \details If ptr!=NULL and len!=0, then the block is initialized from the pointer ptr. + //! \details If ptr!=NULL and len!=0, then the block is initialized from the pointer ptr. //! If ptr==NULL and len!=0, then the block is initialized to 0. //! Otherwise, the block is empty and \a not initialized. //! \note size is the count of elements, and not the number of bytes @@ -802,7 +802,7 @@ __stl_alloc_rebind(CryptoPP::AllocatorWithCleanup<_Tp1>& __a, const _Tp2*) #endif NAMESPACE_END - + #if CRYPTOPP_MSC_VERSION # pragma warning(pop) #endif From a534ccb1bc7e5e2cfa7a51974c77da24e3745948 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 4 Sep 2016 06:44:33 -0400 Subject: [PATCH 02/28] Stub-out a rotlImmediate and rotrImmediate providing shift/rotate amount as a template parameter. Also see http://stackoverflow.com/q/39314690 and http://stackoverflow.com/q/39284065 --- misc.h | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/misc.h b/misc.h index 689b5ed3..5ff4105c 100644 --- a/misc.h +++ b/misc.h @@ -1293,6 +1293,33 @@ CRYPTOPP_DLL void CRYPTOPP_API UnalignedDeallocate(void *ptr); // ************** rotate functions *************** //! \brief Performs a left rotate +//! \tparam T the word type +//! \tparam Y the number of bit positions to rotate the value +//! \param x the value to rotate +//! \details This is a portable C/C++ implementation which attempts to take advantage of the +//! constexpr-ness of a template parameter in hopes of achieving better code +//! generation under Clang and VC++. If a specialization is not available, then +//! rotlImmediate simply calls rotlFixed. +template inline T rotlImmediate(T x) +{ + return rotlFixed(x, Y); +} + +//! \brief Performs a right rotate +//! \tparam T the word type +//! \tparam Y the number of bit positions to rotate the value +//! \param x the value to rotate +//! \details This is a portable C/C++ implementation which attempts to take advantage of the +//! constexpr-ness of a template parameter in hopes of achieving better code +//! generation under Clang and VC++. If a specialization is not available, then +//! rotrImmediate simply calls rotlFixed. +template inline T rotrFixed(T x) +{ + return rotrFixed(x, Y); +} + +//! \brief Performs a left rotate +//! \tparam T the word type //! \param x the value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a portable C/C++ implementation. The value x to be rotated can be 8 to 64-bits. @@ -1314,6 +1341,7 @@ template inline T rotlFixed(T x, unsigned int y) } //! \brief Performs a right rotate +//! \tparam T the word type //! \param x the value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a portable C/C++ implementation. The value x to be rotated can be 8 to 64-bits. @@ -1335,6 +1363,7 @@ template inline T rotrFixed(T x, unsigned int y) } //! \brief Performs a left rotate +//! \tparam T the word type //! \param x the value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a portable C/C++ implementation. The value x to be rotated can be 8 to 64-bits. @@ -1352,6 +1381,7 @@ template inline T rotlVariable(T x, unsigned int y) } //! \brief Performs a right rotate +//! \tparam T the word type //! \param x the value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a portable C/C++ implementation. The value x to be rotated can be 8 to 64-bits. @@ -1369,6 +1399,7 @@ template inline T rotrVariable(T x, unsigned int y) } //! \brief Performs a left rotate +//! \tparam T the word type //! \param x the value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a portable C/C++ implementation. The value x to be rotated can be 8 to 64-bits. @@ -1382,6 +1413,7 @@ template inline T rotlMod(T x, unsigned int y) } //! \brief Performs a right rotate +//! \tparam T the word type //! \param x the value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a portable C/C++ implementation. The value x to be rotated can be 8 to 64-bits. @@ -1394,9 +1426,39 @@ template inline T rotrMod(T x, unsigned int y) return T((x>>(y&MASK))|(x<<(-y&MASK))); } +#if defined(__GNUC__) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32) && 0 +template +inline word32 rotlImmediate(word32 x) +{ + __asm__ ("roll %1, %0" : "+mq" (x) : "I" ((unsigned char)Y)); + return x; +} +template +inline T rotlImmediate(word32 x) +{ + __asm__ ("rorl %1, %0" : "+mq" (x) : "I" ((unsigned char)Y)); + return x; +} +# if (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32) +template <> +inline word64 rotlImmediate(word64 x) +{ + __asm__ ("rolq %1, %0" : "+mq" (x) : "J" ((unsigned char)Y)); + return x; +} +template <> +inline T rotlImmediate(word64 x) +{ + __asm__ ("rorq %1, %0" : "+mq" (x) : "J" ((unsigned char)Y)); + return x; +} +# endif +#endif + #ifdef _MSC_VER //! \brief Performs a left rotate +//! \tparam T the word type //! \param x the 32-bit value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a Microsoft specific implementation using _lrotl provided by @@ -1411,6 +1473,7 @@ template<> inline word32 rotlFixed(word32 x, unsigned int y) } //! \brief Performs a right rotate +//! \tparam T the word type //! \param x the 32-bit value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a Microsoft specific implementation using _lrotr provided by @@ -1425,6 +1488,7 @@ template<> inline word32 rotrFixed(word32 x, unsigned int y) } //! \brief Performs a left rotate +//! \tparam T the word type //! \param x the 32-bit value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a Microsoft specific implementation using _lrotl provided by @@ -1438,6 +1502,7 @@ template<> inline word32 rotlVariable(word32 x, unsigned int y) } //! \brief Performs a right rotate +//! \tparam T the word type //! \param x the 32-bit value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a Microsoft specific implementation using _lrotr provided by @@ -1451,6 +1516,7 @@ template<> inline word32 rotrVariable(word32 x, unsigned int y) } //! \brief Performs a left rotate +//! \tparam T the word type //! \param x the 32-bit value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a Microsoft specific implementation using _lrotl provided by @@ -1463,6 +1529,7 @@ template<> inline word32 rotlMod(word32 x, unsigned int y) } //! \brief Performs a right rotate +//! \tparam T the word type //! \param x the 32-bit value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a Microsoft specific implementation using _lrotr provided by @@ -1480,6 +1547,7 @@ template<> inline word32 rotrMod(word32 x, unsigned int y) // Intel C++ Compiler 10.0 calls a function instead of using the rotate instruction when using these instructions //! \brief Performs a left rotate +//! \tparam T the word type //! \param x the 64-bit value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a Microsoft specific implementation using _lrotl provided by @@ -1494,6 +1562,7 @@ template<> inline word64 rotlFixed(word64 x, unsigned int y) } //! \brief Performs a right rotate +//! \tparam T the word type //! \param x the 64-bit value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a Microsoft specific implementation using _lrotr provided by @@ -1508,6 +1577,7 @@ template<> inline word64 rotrFixed(word64 x, unsigned int y) } //! \brief Performs a left rotate +//! \tparam T the word type //! \param x the 64-bit value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a Microsoft specific implementation using _lrotl provided by @@ -1521,6 +1591,7 @@ template<> inline word64 rotlVariable(word64 x, unsigned int y) } //! \brief Performs a right rotate +//! \tparam T the word type //! \param x the 64-bit value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a Microsoft specific implementation using _lrotr provided by @@ -1534,6 +1605,7 @@ template<> inline word64 rotrVariable(word64 x, unsigned int y) } //! \brief Performs a left rotate +//! \tparam T the word type //! \param x the 64-bit value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a Microsoft specific implementation using _lrotl provided by @@ -1546,6 +1618,7 @@ template<> inline word64 rotlMod(word64 x, unsigned int y) } //! \brief Performs a right rotate +//! \tparam T the word type //! \param x the 64-bit value to rotate //! \param y the number of bit positions to rotate the value //! \details This is a Microsoft specific implementation using _lrotr provided by From cf81d8a09998a879969e6096c68c068f971e9784 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 5 Sep 2016 03:13:45 -0400 Subject: [PATCH 03/28] Add constexpr-ness to seckey.h classes. Coarse grained benchmarking with GCC 4.8 at -O2 using 'time' and 'cryptest.exe' shows we shaved about 100ms to 150ms off the running time. We are ready to break the 1-second mark for the running time (its elluded me for some time now) --- seckey.h | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/seckey.h b/seckey.h index 8a801306..46099f00 100644 --- a/seckey.h +++ b/seckey.h @@ -66,18 +66,20 @@ public: CRYPTOPP_CONSTANT(MIN_ROUNDS = N) //! \brief The maximum number of rounds for the algorithm provided as a constant. CRYPTOPP_CONSTANT(MAX_ROUNDS = M) - //! \brief The default number of rounds for the algorithm based on key length + //! \brief The default number of rounds for the algorithm based on key length //! provided by a static function. //! \param keylength the size of the key, in bytes //! \details keylength is unused in the default implementation. - static unsigned int StaticGetDefaultRounds(size_t keylength) - {CRYPTOPP_UNUSED(keylength); return DEFAULT_ROUNDS;} + CRYPTOPP_CONSTEXPR static unsigned int StaticGetDefaultRounds(size_t keylength) + {return CRYPTOPP_UNUSED(keylength), DEFAULT_ROUNDS;} protected: //! \brief Validates the number of rounds for an algorithm. //! \param rounds the canddiate number of rounds //! \param alg an Algorithm object used if the number of rounds are invalid //! \throws InvalidRounds if the number of rounds are invalid + //! \details ThrowIfInvalidRounds() validates the number of rounds and throws if invalid. + //! The function is not a C++11 constexpr due to the potential throw operation. inline void ThrowIfInvalidRounds(int rounds, const Algorithm *alg) { if (M == INT_MAX) // Coverity and result_independent_of_operands @@ -97,6 +99,8 @@ protected: //! \param alg an Algorithm object used if the number of rounds are invalid //! \returns the number of rounds for the algorithm //! \throws InvalidRounds if the number of rounds are invalid + //! \details GetRoundsAndThrowIfInvalid() validates the number of rounds and throws if invalid. + //! The function is not a C++11 constexpr due to the potential throw operation. inline unsigned int GetRoundsAndThrowIfInvalid(const NameValuePairs ¶m, const Algorithm *alg) { int rounds = param.GetIntValueWithDefault("Rounds", DEFAULT_ROUNDS); @@ -140,8 +144,8 @@ public: //! \param keylength the size of the key, in bytes //! \details The default implementation returns KEYLENGTH. keylength is unused //! in the default implementation. - static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength) - {CRYPTOPP_UNUSED(keylength); return KEYLENGTH;} + CRYPTOPP_CONSTEXPR static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength) + {return CRYPTOPP_UNUSED(keylength), KEYLENGTH;} }; //! \class VariableKeyLength @@ -192,6 +196,7 @@ public: //! then keylength is returned. Otherwise, the function returns keylength rounded //! \a down to the next smaller multiple of KEYLENGTH_MULTIPLE. //! \details keylength is provided in bytes, not bits. + // TODO: Figure out how to make this CRYPTOPP_CONSTEXPR static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength) { if (keylength < (size_t)MIN_KEYLENGTH) @@ -240,7 +245,7 @@ public: //! then keylength is returned. Otherwise, the function returns keylength rounded //! \a down to the next smaller multiple of KEYLENGTH_MULTIPLE. //! \details keylength is provided in bytes, not bits. - static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength) + CRYPTOPP_CONSTEXPR static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength) {return T::StaticGetValidKeyLength(keylength);} }; @@ -257,19 +262,19 @@ class CRYPTOPP_NO_VTABLE SimpleKeyingInterfaceImpl : public BASE public: //! \brief The minimum key length used by the algorithm //! \returns minimum key length used by the algorithm, in bytes - size_t MinKeyLength() const + CRYPTOPP_CONSTEXPR size_t MinKeyLength() const {return INFO::MIN_KEYLENGTH;} //! \brief The maximum key length used by the algorithm //! \returns maximum key length used by the algorithm, in bytes - size_t MaxKeyLength() const + CRYPTOPP_CONSTEXPR size_t MaxKeyLength() const {return (size_t)INFO::MAX_KEYLENGTH;} - + //! \brief The default key length used by the algorithm //! \returns default key length used by the algorithm, in bytes - size_t DefaultKeyLength() const + CRYPTOPP_CONSTEXPR size_t DefaultKeyLength() const {return INFO::DEFAULT_KEYLENGTH;} - + //! \brief Provides a valid key length for the algorithm //! \param keylength the size of the key, in bytes //! \returns the valid key lenght, in bytes @@ -278,17 +283,17 @@ public: //! then the function returns MAX_KEYLENGTH. if If keylength is a multiple of KEYLENGTH_MULTIPLE, //! then keylength is returned. Otherwise, the function returns a \a lower multiple of //! KEYLENGTH_MULTIPLE. - size_t GetValidKeyLength(size_t keylength) const {return INFO::StaticGetValidKeyLength(keylength);} + CRYPTOPP_CONSTEXPR size_t GetValidKeyLength(size_t keylength) const {return INFO::StaticGetValidKeyLength(keylength);} //! \brief The default IV requirements for the algorithm //! \details The default value is NOT_RESYNCHRONIZABLE. See IV_Requirement //! in cryptlib.h for allowed values. - SimpleKeyingInterface::IV_Requirement IVRequirement() const + CRYPTOPP_CONSTEXPR SimpleKeyingInterface::IV_Requirement IVRequirement() const {return (SimpleKeyingInterface::IV_Requirement)INFO::IV_REQUIREMENT;} - + //! \brief The default initialization vector length for the algorithm //! \details IVSize is provided in bytes, not bits. The default implementation uses IV_LENGTH, which is 0. - unsigned int IVSize() const + CRYPTOPP_CONSTEXPR unsigned int IVSize() const {return INFO::IV_LENGTH;} }; @@ -323,7 +328,7 @@ public: //! SimpleKeyingInterface::SetKey. BlockCipherFinal(const byte *key) {this->SetKey(key, this->DEFAULT_KEYLENGTH);} - + //! \brief Construct a BlockCipherFinal //! \param key a byte array used to key the cipher //! \param length the length of the byte array @@ -331,7 +336,7 @@ public: //! SimpleKeyingInterface::SetKey. BlockCipherFinal(const byte *key, size_t length) {this->SetKey(key, length);} - + //! \brief Construct a BlockCipherFinal //! \param key a byte array used to key the cipher //! \param length the length of the byte array @@ -344,7 +349,7 @@ public: //! \brief Provides the direction of the cipher //! \returns true if DIR is ENCRYPTION, false otherwise //! \sa GetCipherDirection(), IsPermutation() - bool IsForwardTransformation() const {return DIR == ENCRYPTION;} + CRYPTOPP_CONSTEXPR bool IsForwardTransformation() const {return DIR == ENCRYPTION;} }; //! \class MessageAuthenticationCodeImpl @@ -388,7 +393,7 @@ public: //! \brief Provides Encryption and Decryption typedefs used by derived classes to //! implement a block cipher //! \details These objects usually should not be used directly. See CipherModeDocumentation -//! instead. Each class derived from this one defines two types, Encryption and Decryption, +//! instead. Each class derived from this one defines two types, Encryption and Decryption, //! both of which implement the BlockCipher interface. struct BlockCipherDocumentation { @@ -401,7 +406,7 @@ struct BlockCipherDocumentation //! \class SymmetricCipherDocumentation //! \brief Provides Encryption and Decryption typedefs used by derived classes to //! implement a symmetric cipher -//! \details Each class derived from this one defines two types, Encryption and Decryption, +//! \details Each class derived from this one defines two types, Encryption and Decryption, //! both of which implement the SymmetricCipher interface. Two types of classes derive //! from this class: stream ciphers and block cipher modes. Stream ciphers can be used //! alone, cipher mode classes need to be used with a block cipher. See CipherModeDocumentation @@ -417,7 +422,7 @@ struct SymmetricCipherDocumentation //! \class AuthenticatedSymmetricCipherDocumentation //! \brief Provides Encryption and Decryption typedefs used by derived classes to //! implement an authenticated encryption cipher -//! \details Each class derived from this one defines two types, Encryption and Decryption, +//! \details Each class derived from this one defines two types, Encryption and Decryption, //! both of which implement the AuthenticatedSymmetricCipher interface. struct AuthenticatedSymmetricCipherDocumentation { @@ -428,7 +433,7 @@ struct AuthenticatedSymmetricCipherDocumentation }; NAMESPACE_END - + #if CRYPTOPP_MSC_VERSION # pragma warning(pop) #endif From 60be5a672a87b27cc7cd8aec08c826cb0dd04257 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 5 Sep 2016 04:36:08 -0400 Subject: [PATCH 04/28] Fixed compile under SunCC 5.14 and SimpleKeyingInterfaceImpl (with virtual functions) using constexpr. Updated documentation --- seckey.h | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/seckey.h b/seckey.h index 46099f00..1013e8dc 100644 --- a/seckey.h +++ b/seckey.h @@ -255,24 +255,26 @@ public: //! \brief Provides a base implementation of SimpleKeyingInterface //! \tparam BASE a SimpleKeyingInterface derived class //! \tparam INFO a SimpleKeyingInterface derived class -//! \sa SimpleKeyingInterface +//! \details SimpleKeyingInterfaceImpl() provides a default implementation for ciphers providing a keying interface. +//! Functions are virtual and not subject to C++11 constexpr. +//! \sa Algorithm(), SimpleKeyingInterface() template class CRYPTOPP_NO_VTABLE SimpleKeyingInterfaceImpl : public BASE { public: //! \brief The minimum key length used by the algorithm //! \returns minimum key length used by the algorithm, in bytes - CRYPTOPP_CONSTEXPR size_t MinKeyLength() const + size_t MinKeyLength() const {return INFO::MIN_KEYLENGTH;} //! \brief The maximum key length used by the algorithm //! \returns maximum key length used by the algorithm, in bytes - CRYPTOPP_CONSTEXPR size_t MaxKeyLength() const + size_t MaxKeyLength() const {return (size_t)INFO::MAX_KEYLENGTH;} //! \brief The default key length used by the algorithm //! \returns default key length used by the algorithm, in bytes - CRYPTOPP_CONSTEXPR size_t DefaultKeyLength() const + size_t DefaultKeyLength() const {return INFO::DEFAULT_KEYLENGTH;} //! \brief Provides a valid key length for the algorithm @@ -283,17 +285,17 @@ public: //! then the function returns MAX_KEYLENGTH. if If keylength is a multiple of KEYLENGTH_MULTIPLE, //! then keylength is returned. Otherwise, the function returns a \a lower multiple of //! KEYLENGTH_MULTIPLE. - CRYPTOPP_CONSTEXPR size_t GetValidKeyLength(size_t keylength) const {return INFO::StaticGetValidKeyLength(keylength);} + size_t GetValidKeyLength(size_t keylength) const {return INFO::StaticGetValidKeyLength(keylength);} //! \brief The default IV requirements for the algorithm //! \details The default value is NOT_RESYNCHRONIZABLE. See IV_Requirement //! in cryptlib.h for allowed values. - CRYPTOPP_CONSTEXPR SimpleKeyingInterface::IV_Requirement IVRequirement() const + SimpleKeyingInterface::IV_Requirement IVRequirement() const {return (SimpleKeyingInterface::IV_Requirement)INFO::IV_REQUIREMENT;} //! \brief The default initialization vector length for the algorithm //! \details IVSize is provided in bytes, not bits. The default implementation uses IV_LENGTH, which is 0. - CRYPTOPP_CONSTEXPR unsigned int IVSize() const + unsigned int IVSize() const {return INFO::IV_LENGTH;} }; @@ -301,6 +303,9 @@ public: //! \brief Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers //! \tparam INFO a SimpleKeyingInterface derived class //! \tparam BASE a SimpleKeyingInterface derived class +//! \details BlockCipherImpl() provides a default implementation for block ciphers using AlgorithmImpl() +//! and SimpleKeyingInterfaceImpl(). Functions are virtual and not subject to C++11 constexpr. +//! \sa Algorithm(), SimpleKeyingInterface(), AlgorithmImpl(), SimpleKeyingInterfaceImpl() template class CRYPTOPP_NO_VTABLE BlockCipherImpl : public AlgorithmImpl > > { @@ -349,13 +354,17 @@ public: //! \brief Provides the direction of the cipher //! \returns true if DIR is ENCRYPTION, false otherwise //! \sa GetCipherDirection(), IsPermutation() - CRYPTOPP_CONSTEXPR bool IsForwardTransformation() const {return DIR == ENCRYPTION;} + bool IsForwardTransformation() const {return DIR == ENCRYPTION;} }; //! \class MessageAuthenticationCodeImpl //! \brief Provides a base implementation of Algorithm and SimpleKeyingInterface for message authentication codes //! \tparam INFO a SimpleKeyingInterface derived class //! \tparam BASE a SimpleKeyingInterface derived class +//! \details MessageAuthenticationCodeImpl() provides a default implementation for message authentication codes +//! using AlgorithmImpl() and SimpleKeyingInterfaceImpl(). Functions are virtual and not subject to C++11 +//! constexpr. +//! \sa Algorithm(), SimpleKeyingInterface(), AlgorithmImpl(), SimpleKeyingInterfaceImpl() template class MessageAuthenticationCodeImpl : public AlgorithmImpl, INFO> { From 5057991a31bae69d0ff4bcfe3f9c47f2fb210242 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 5 Sep 2016 09:10:12 -0400 Subject: [PATCH 05/28] Add StaticGetValidKeyLength tests for RijndaelEncryption, RijndaelDecryption, TwofishEncryption and TwofishDecryption --- validat1.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/validat1.cpp b/validat1.cpp index 4a798dde..88507451 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -1998,26 +1998,62 @@ bool ValidateMARS() bool ValidateRijndael() { cout << "\nRijndael (AES) validation suite running...\n\n"; + bool pass1 = true, pass2 = true, pass3 = true; + + RijndaelEncryption enc; + pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1; + pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1; + cout << (pass1 ? "passed:" : "FAILED:") << " RijndaelEncryption StaticGetValidKeyLength\n"; + + RijndaelDecryption dec; + pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2; + pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2; + cout << (pass2 ? "passed:" : "FAILED:") << " RijndaelDecryption StaticGetValidKeyLength\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/rijndael.dat", true, new HexDecoder); - bool pass = true; - pass = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 4) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(24), valdata, 3) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(32), valdata, 2) && pass; - pass = RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/aes.txt") && pass; - return pass; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 4) && pass3; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(24), valdata, 3) && pass3; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(32), valdata, 2) && pass3; + pass3 = RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/aes.txt") && pass3; + return pass1 && pass2 && pass3; } bool ValidateTwofish() { cout << "\nTwofish validation suite running...\n\n"; + bool pass1 = true, pass2 = true, pass3 = true; + + TwofishEncryption enc; + // pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1; + pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1; + // pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1; + // pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1; + cout << (pass1 ? "passed:" : "FAILED:") << " TwofishEncryption StaticGetValidKeyLength\n"; + + TwofishDecryption dec; + // pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2; + pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2; + // pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2; + // pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2; + cout << (pass2 ? "passed:" : "FAILED:") << " TwofishDecryption StaticGetValidKeyLength\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/twofishv.dat", true, new HexDecoder); - bool pass = true; - pass = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 4) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(24), valdata, 3) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(32), valdata, 2) && pass; - return pass; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 4) && pass3; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(24), valdata, 3) && pass3; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(32), valdata, 2) && pass3; + return pass1 && pass2 && pass3; } bool ValidateSerpent() From 0bc85ca42fe6849189fa8033357b65f71eea81ec Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 5 Sep 2016 09:35:19 -0400 Subject: [PATCH 06/28] Fix Twofish VariableKeyLength information (Issue 252) --- twofish.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twofish.h b/twofish.h index 5ebc2440..f6d75ca8 100644 --- a/twofish.h +++ b/twofish.h @@ -13,7 +13,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \class Twofish_Info //! \brief Twofish block cipher information -struct Twofish_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 0, 32>, FixedRounds<16> +struct Twofish_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 8>, FixedRounds<16> { static const char *StaticAlgorithmName() {return "Twofish";} }; From 88bc98fa2507eef8597c2030be9643c9f55ad004 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 5 Sep 2016 09:37:51 -0400 Subject: [PATCH 07/28] Modify "Algorithm key lengths" output format --- validat1.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/validat1.cpp b/validat1.cpp index 88507451..92f582c9 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -2007,7 +2007,6 @@ bool ValidateRijndael() pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1; pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1; pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1; - cout << (pass1 ? "passed:" : "FAILED:") << " RijndaelEncryption StaticGetValidKeyLength\n"; RijndaelDecryption dec; pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; @@ -2016,7 +2015,7 @@ bool ValidateRijndael() pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2; pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2; pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2; - cout << (pass2 ? "passed:" : "FAILED:") << " RijndaelDecryption StaticGetValidKeyLength\n"; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/rijndael.dat", true, new HexDecoder); pass3 = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 4) && pass3; @@ -2032,22 +2031,21 @@ bool ValidateTwofish() bool pass1 = true, pass2 = true, pass3 = true; TwofishEncryption enc; - // pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1; pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1; pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1; pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1; - // pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1; - // pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1; - cout << (pass1 ? "passed:" : "FAILED:") << " TwofishEncryption StaticGetValidKeyLength\n"; + pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1; TwofishDecryption dec; - // pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2; pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2; pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2; - // pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2; - // pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2; - cout << (pass2 ? "passed:" : "FAILED:") << " TwofishDecryption StaticGetValidKeyLength\n"; + pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/twofishv.dat", true, new HexDecoder); pass3 = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 4) && pass3; From ea02dc0b945237d8743ebf0d549f103612be63d6 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 5 Sep 2016 09:50:26 -0400 Subject: [PATCH 08/28] Fix Serpent VariableKeyLength information (Issue 252) --- serpent.cpp | 22 +++++++++++----------- serpent.h | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/serpent.cpp b/serpent.cpp index 88a7d19b..3f8b2751 100644 --- a/serpent.cpp +++ b/serpent.cpp @@ -52,7 +52,7 @@ typedef BlockGetAndPut Block; void Serpent::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const { word32 a, b, c, d, e; - + Block::Get(inBlock)(a)(b)(c)(d); const word32 *k = m_key; @@ -84,14 +84,14 @@ void Serpent::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, while (true); afterS7(KX); - + Block::Put(xorBlock, outBlock)(d)(e)(b)(a); } void Serpent::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const { word32 a, b, c, d, e; - + Block::Get(inBlock)(a)(b)(c)(d); const word32 *k = m_key + 96; @@ -108,17 +108,17 @@ void Serpent::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, k -= 32; beforeI7(ILT); start: - beforeI7(I7); afterI7(KX); - afterI7(ILT); afterI7(I6); afterI6(KX); - afterI6(ILT); afterI6(I5); afterI5(KX); - afterI5(ILT); afterI5(I4); afterI4(KX); - afterI4(ILT); afterI4(I3); afterI3(KX); - afterI3(ILT); afterI3(I2); afterI2(KX); - afterI2(ILT); afterI2(I1); afterI1(KX); + beforeI7(I7); afterI7(KX); + afterI7(ILT); afterI7(I6); afterI6(KX); + afterI6(ILT); afterI6(I5); afterI5(KX); + afterI5(ILT); afterI5(I4); afterI4(KX); + afterI4(ILT); afterI4(I3); afterI3(KX); + afterI3(ILT); afterI3(I2); afterI2(KX); + afterI2(ILT); afterI2(I1); afterI1(KX); afterI1(ILT); afterI1(I0); afterI0(KX); } while (--i != 0); - + Block::Put(xorBlock, outBlock)(a)(d)(b)(e); } diff --git a/serpent.h b/serpent.h index 64fd0e5c..888d70af 100644 --- a/serpent.h +++ b/serpent.h @@ -13,7 +13,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \class Serpent_Info //! \brief Serpent block cipher information -struct Serpent_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 0, 32>, public FixedRounds<32> +struct Serpent_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 8>, public FixedRounds<32> { static const char *StaticAlgorithmName() {return "Serpent";} }; From b472b446fb38d4d2467e59820311619b6d52b902 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 5 Sep 2016 10:52:53 -0400 Subject: [PATCH 09/28] Fix SHARK VariableKeyLength information (Issue 252) --- shark.cpp | 32 ++++++++++++++++---------------- shark.h | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/shark.cpp b/shark.cpp index 8721574b..99d63d21 100644 --- a/shark.cpp +++ b/shark.cpp @@ -15,20 +15,20 @@ NAMESPACE_BEGIN(CryptoPP) static word64 SHARKTransform(word64 a) { static const byte iG[8][8] = { - 0xe7, 0x30, 0x90, 0x85, 0xd0, 0x4b, 0x91, 0x41, - 0x53, 0x95, 0x9b, 0xa5, 0x96, 0xbc, 0xa1, 0x68, - 0x02, 0x45, 0xf7, 0x65, 0x5c, 0x1f, 0xb6, 0x52, - 0xa2, 0xca, 0x22, 0x94, 0x44, 0x63, 0x2a, 0xa2, - 0xfc, 0x67, 0x8e, 0x10, 0x29, 0x75, 0x85, 0x71, - 0x24, 0x45, 0xa2, 0xcf, 0x2f, 0x22, 0xc1, 0x0e, - 0xa1, 0xf1, 0x71, 0x40, 0x91, 0x27, 0x18, 0xa5, - 0x56, 0xf4, 0xaf, 0x32, 0xd2, 0xa4, 0xdc, 0x71, + 0xe7, 0x30, 0x90, 0x85, 0xd0, 0x4b, 0x91, 0x41, + 0x53, 0x95, 0x9b, 0xa5, 0x96, 0xbc, 0xa1, 0x68, + 0x02, 0x45, 0xf7, 0x65, 0x5c, 0x1f, 0xb6, 0x52, + 0xa2, 0xca, 0x22, 0x94, 0x44, 0x63, 0x2a, 0xa2, + 0xfc, 0x67, 0x8e, 0x10, 0x29, 0x75, 0x85, 0x71, + 0x24, 0x45, 0xa2, 0xcf, 0x2f, 0x22, 0xc1, 0x0e, + 0xa1, 0xf1, 0x71, 0x40, 0x91, 0x27, 0x18, 0xa5, + 0x56, 0xf4, 0xaf, 0x32, 0xd2, 0xa4, 0xdc, 0x71, }; word64 result=0; GF256 gf256(0xf5); for (unsigned int i=0; i<8; i++) - for(unsigned int j=0; j<8; j++) + for(unsigned int j=0; j<8; j++) result ^= word64(gf256.Multiply(iG[i][j], GF256::Element(a>>(56-8*j)))) << (56-8*i); return result; } @@ -100,17 +100,17 @@ inline SharkProcessAndXorBlock(const word64 *roundKeys, unsigned int rounds, con word64 tmp = *(word64 *)(void *)inBlock ^ roundKeys[0]; ByteOrder order = GetNativeByteOrder(); - tmp = cbox[0][GetByte(order, tmp, 0)] ^ cbox[1][GetByte(order, tmp, 1)] - ^ cbox[2][GetByte(order, tmp, 2)] ^ cbox[3][GetByte(order, tmp, 3)] - ^ cbox[4][GetByte(order, tmp, 4)] ^ cbox[5][GetByte(order, tmp, 5)] + tmp = cbox[0][GetByte(order, tmp, 0)] ^ cbox[1][GetByte(order, tmp, 1)] + ^ cbox[2][GetByte(order, tmp, 2)] ^ cbox[3][GetByte(order, tmp, 3)] + ^ cbox[4][GetByte(order, tmp, 4)] ^ cbox[5][GetByte(order, tmp, 5)] ^ cbox[6][GetByte(order, tmp, 6)] ^ cbox[7][GetByte(order, tmp, 7)] ^ roundKeys[1]; - for(unsigned int i=2; i, public VariableKeyLength<16, 1, 16>, public VariableRounds<6, 2> +struct SHARK_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public VariableRounds<6, 2> { static const char *StaticAlgorithmName() {return "SHARK-E";} }; From 0ee3ae136d58abc240457dbb75b0aa6d1065c95b Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 5 Sep 2016 11:18:10 -0400 Subject: [PATCH 10/28] Fix CAST-128 and CAST-256 VariableKeyLength information (Issue 252) --- cast.cpp | 86 ++++++++++++++++++++++++++++---------------------------- cast.h | 2 +- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/cast.cpp b/cast.cpp index c6787ea2..cc62b915 100644 --- a/cast.cpp +++ b/cast.cpp @@ -151,48 +151,48 @@ void CAST128::Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, // The following CAST-256 implementation was contributed by Leonard Janke const word32 CAST256::Base::t_m[8][24]={ -{ 0x5a827999, 0xd151d6a1, 0x482133a9, 0xbef090b1, 0x35bfedb9, 0xac8f4ac1, - 0x235ea7c9, 0x9a2e04d1, 0x10fd61d9, 0x87ccbee1, 0xfe9c1be9, 0x756b78f1, - 0xec3ad5f9, 0x630a3301, 0xd9d99009, 0x50a8ed11, 0xc7784a19, 0x3e47a721, - 0xb5170429, 0x2be66131, 0xa2b5be39, 0x19851b41, 0x90547849, 0x0723d551}, -{ 0xc95c653a, 0x402bc242, 0xb6fb1f4a, 0x2dca7c52, 0xa499d95a, 0x1b693662, - 0x9238936a, 0x0907f072, 0x7fd74d7a, 0xf6a6aa82, 0x6d76078a, 0xe4456492, - 0x5b14c19a, 0xd1e41ea2, 0x48b37baa, 0xbf82d8b2, 0x365235ba, 0xad2192c2, - 0x23f0efca, 0x9ac04cd2, 0x118fa9da, 0x885f06e2, 0xff2e63ea, 0x75fdc0f2}, -{ 0x383650db, 0xaf05ade3, 0x25d50aeb, 0x9ca467f3, 0x1373c4fb, 0x8a432203, - 0x01127f0b, 0x77e1dc13, 0xeeb1391b, 0x65809623, 0xdc4ff32b, 0x531f5033, - 0xc9eead3b, 0x40be0a43, 0xb78d674b, 0x2e5cc453, 0xa52c215b, 0x1bfb7e63, - 0x92cadb6b, 0x099a3873, 0x8069957b, 0xf738f283, 0x6e084f8b, 0xe4d7ac93}, -{ 0xa7103c7c, 0x1ddf9984, 0x94aef68c, 0x0b7e5394, 0x824db09c, 0xf91d0da4, - 0x6fec6aac, 0xe6bbc7b4, 0x5d8b24bc, 0xd45a81c4, 0x4b29decc, 0xc1f93bd4, - 0x38c898dc, 0xaf97f5e4, 0x266752ec, 0x9d36aff4, 0x14060cfc, 0x8ad56a04, - 0x01a4c70c, 0x78742414, 0xef43811c, 0x6612de24, 0xdce23b2c, 0x53b19834}, -{ 0x15ea281d, 0x8cb98525, 0x0388e22d, 0x7a583f35, 0xf1279c3d, 0x67f6f945, - 0xdec6564d, 0x5595b355, 0xcc65105d, 0x43346d65, 0xba03ca6d, 0x30d32775, - 0xa7a2847d, 0x1e71e185, 0x95413e8d, 0x0c109b95, 0x82dff89d, 0xf9af55a5, - 0x707eb2ad, 0xe74e0fb5, 0x5e1d6cbd, 0xd4ecc9c5, 0x4bbc26cd, 0xc28b83d5}, -{ 0x84c413be, 0xfb9370c6, 0x7262cdce, 0xe9322ad6, 0x600187de, 0xd6d0e4e6, - 0x4da041ee, 0xc46f9ef6, 0x3b3efbfe, 0xb20e5906, 0x28ddb60e, 0x9fad1316, - 0x167c701e, 0x8d4bcd26, 0x041b2a2e, 0x7aea8736, 0xf1b9e43e, 0x68894146, - 0xdf589e4e, 0x5627fb56, 0xccf7585e, 0x43c6b566, 0xba96126e, 0x31656f76}, -{ 0xf39dff5f, 0x6a6d5c67, 0xe13cb96f, 0x580c1677, 0xcedb737f, 0x45aad087, - 0xbc7a2d8f, 0x33498a97, 0xaa18e79f, 0x20e844a7, 0x97b7a1af, 0x0e86feb7, - 0x85565bbf, 0xfc25b8c7, 0x72f515cf, 0xe9c472d7, 0x6093cfdf, 0xd7632ce7, - 0x4e3289ef, 0xc501e6f7, 0x3bd143ff, 0xb2a0a107, 0x296ffe0f, 0xa03f5b17}, -{ 0x6277eb00, 0xd9474808, 0x5016a510, 0xc6e60218, 0x3db55f20, 0xb484bc28, - 0x2b541930, 0xa2237638, 0x18f2d340, 0x8fc23048, 0x06918d50, 0x7d60ea58, - 0xf4304760, 0x6affa468, 0xe1cf0170, 0x589e5e78, 0xcf6dbb80, 0x463d1888, - 0xbd0c7590, 0x33dbd298, 0xaaab2fa0, 0x217a8ca8, 0x9849e9b0, 0x0f1946b8} +{ 0x5a827999, 0xd151d6a1, 0x482133a9, 0xbef090b1, 0x35bfedb9, 0xac8f4ac1, + 0x235ea7c9, 0x9a2e04d1, 0x10fd61d9, 0x87ccbee1, 0xfe9c1be9, 0x756b78f1, + 0xec3ad5f9, 0x630a3301, 0xd9d99009, 0x50a8ed11, 0xc7784a19, 0x3e47a721, + 0xb5170429, 0x2be66131, 0xa2b5be39, 0x19851b41, 0x90547849, 0x0723d551}, +{ 0xc95c653a, 0x402bc242, 0xb6fb1f4a, 0x2dca7c52, 0xa499d95a, 0x1b693662, + 0x9238936a, 0x0907f072, 0x7fd74d7a, 0xf6a6aa82, 0x6d76078a, 0xe4456492, + 0x5b14c19a, 0xd1e41ea2, 0x48b37baa, 0xbf82d8b2, 0x365235ba, 0xad2192c2, + 0x23f0efca, 0x9ac04cd2, 0x118fa9da, 0x885f06e2, 0xff2e63ea, 0x75fdc0f2}, +{ 0x383650db, 0xaf05ade3, 0x25d50aeb, 0x9ca467f3, 0x1373c4fb, 0x8a432203, + 0x01127f0b, 0x77e1dc13, 0xeeb1391b, 0x65809623, 0xdc4ff32b, 0x531f5033, + 0xc9eead3b, 0x40be0a43, 0xb78d674b, 0x2e5cc453, 0xa52c215b, 0x1bfb7e63, + 0x92cadb6b, 0x099a3873, 0x8069957b, 0xf738f283, 0x6e084f8b, 0xe4d7ac93}, +{ 0xa7103c7c, 0x1ddf9984, 0x94aef68c, 0x0b7e5394, 0x824db09c, 0xf91d0da4, + 0x6fec6aac, 0xe6bbc7b4, 0x5d8b24bc, 0xd45a81c4, 0x4b29decc, 0xc1f93bd4, + 0x38c898dc, 0xaf97f5e4, 0x266752ec, 0x9d36aff4, 0x14060cfc, 0x8ad56a04, + 0x01a4c70c, 0x78742414, 0xef43811c, 0x6612de24, 0xdce23b2c, 0x53b19834}, +{ 0x15ea281d, 0x8cb98525, 0x0388e22d, 0x7a583f35, 0xf1279c3d, 0x67f6f945, + 0xdec6564d, 0x5595b355, 0xcc65105d, 0x43346d65, 0xba03ca6d, 0x30d32775, + 0xa7a2847d, 0x1e71e185, 0x95413e8d, 0x0c109b95, 0x82dff89d, 0xf9af55a5, + 0x707eb2ad, 0xe74e0fb5, 0x5e1d6cbd, 0xd4ecc9c5, 0x4bbc26cd, 0xc28b83d5}, +{ 0x84c413be, 0xfb9370c6, 0x7262cdce, 0xe9322ad6, 0x600187de, 0xd6d0e4e6, + 0x4da041ee, 0xc46f9ef6, 0x3b3efbfe, 0xb20e5906, 0x28ddb60e, 0x9fad1316, + 0x167c701e, 0x8d4bcd26, 0x041b2a2e, 0x7aea8736, 0xf1b9e43e, 0x68894146, + 0xdf589e4e, 0x5627fb56, 0xccf7585e, 0x43c6b566, 0xba96126e, 0x31656f76}, +{ 0xf39dff5f, 0x6a6d5c67, 0xe13cb96f, 0x580c1677, 0xcedb737f, 0x45aad087, + 0xbc7a2d8f, 0x33498a97, 0xaa18e79f, 0x20e844a7, 0x97b7a1af, 0x0e86feb7, + 0x85565bbf, 0xfc25b8c7, 0x72f515cf, 0xe9c472d7, 0x6093cfdf, 0xd7632ce7, + 0x4e3289ef, 0xc501e6f7, 0x3bd143ff, 0xb2a0a107, 0x296ffe0f, 0xa03f5b17}, +{ 0x6277eb00, 0xd9474808, 0x5016a510, 0xc6e60218, 0x3db55f20, 0xb484bc28, + 0x2b541930, 0xa2237638, 0x18f2d340, 0x8fc23048, 0x06918d50, 0x7d60ea58, + 0xf4304760, 0x6affa468, 0xe1cf0170, 0x589e5e78, 0xcf6dbb80, 0x463d1888, + 0xbd0c7590, 0x33dbd298, 0xaaab2fa0, 0x217a8ca8, 0x9849e9b0, 0x0f1946b8} }; -const unsigned int CAST256::Base::t_r[8][24]={ - {19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11}, - {4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28}, - {21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13}, - {6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30}, - {23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15}, - {8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0}, - {25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17}, +const unsigned int CAST256::Base::t_r[8][24]={ + {19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11}, + {4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28}, + {21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13}, + {6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30}, + {23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15}, + {8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0}, + {25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17}, {10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2} }; @@ -262,7 +262,7 @@ void CAST256::Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, { Omega(2*i,kappa); Omega(2*i+1,kappa); - + K[8*i]=kappa[0] & 31; K[8*i+1]=kappa[2] & 31; K[8*i+2]=kappa[4] & 31; @@ -284,8 +284,8 @@ void CAST256::Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, assert(i1, public VariableKeyLength<16, 16, 32> +struct CAST256_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 4> { static const char *StaticAlgorithmName() {return "CAST-256";} }; From 022c33a172e1b2625a9ebf7658566d399600913f Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 5 Sep 2016 11:35:00 -0400 Subject: [PATCH 11/28] Add StaticGetValidKeyLength tests for block ciphers (Issue 252) --- validat1.cpp | 193 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 175 insertions(+), 18 deletions(-) diff --git a/validat1.cpp b/validat1.cpp index 92f582c9..0b57d5aa 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -2057,25 +2057,72 @@ bool ValidateTwofish() bool ValidateSerpent() { cout << "\nSerpent validation suite running...\n\n"; + bool pass1 = true, pass2 = true, pass3 = true; + + SerpentEncryption enc; + pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1; + pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1; + + SerpentDecryption dec; + pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2; + pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/serpentv.dat", true, new HexDecoder); bool pass = true; pass = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 5) && pass; pass = BlockTransformationTest(FixedRoundsCipherFactory(24), valdata, 4) && pass; pass = BlockTransformationTest(FixedRoundsCipherFactory(32), valdata, 3) && pass; - return pass; + return pass1 && pass2 && pass3; } bool ValidateBlowfish() { cout << "\nBlowfish validation suite running...\n\n"; + bool pass1 = true, pass2 = true, pass3 = true, fail; + + BlowfishEncryption enc; // 32 to 448-bits (4 to 56-bytes) + pass1 = enc.StaticGetValidKeyLength(3) == 4 && pass1; + pass1 = enc.StaticGetValidKeyLength(4) == 4 && pass1; + pass1 = enc.StaticGetValidKeyLength(5) == 5 && pass1; + pass1 = enc.StaticGetValidKeyLength(8) == 8 && pass1; + pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1; + pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(56) == 56 && pass1; + pass1 = enc.StaticGetValidKeyLength(57) == 56 && pass1; + pass1 = enc.StaticGetValidKeyLength(60) == 56 && pass1; + pass1 = enc.StaticGetValidKeyLength(64) == 56 && pass1; + pass1 = enc.StaticGetValidKeyLength(128) == 56 && pass1; + + BlowfishDecryption dec; // 32 to 448-bits (4 to 56-bytes) + pass2 = dec.StaticGetValidKeyLength(3) == 4 && pass2; + pass2 = dec.StaticGetValidKeyLength(4) == 4 && pass2; + pass2 = dec.StaticGetValidKeyLength(5) == 5 && pass2; + pass2 = dec.StaticGetValidKeyLength(8) == 8 && pass2; + pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2; + pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(56) == 56 && pass2; + pass2 = dec.StaticGetValidKeyLength(57) == 56 && pass2; + pass2 = dec.StaticGetValidKeyLength(60) == 56 && pass2; + pass2 = dec.StaticGetValidKeyLength(64) == 56 && pass2; + pass2 = dec.StaticGetValidKeyLength(128) == 56 && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; HexEncoder output(new FileSink(cout)); const char *key[]={"abcdefghijklmnopqrstuvwxyz", "Who is John Galt?"}; byte *plain[]={(byte *)"BLOWFISH", (byte *)"\xfe\xdc\xba\x98\x76\x54\x32\x10"}; byte *cipher[]={(byte *)"\x32\x4e\xd0\xfe\xf4\x13\xa2\x03", (byte *)"\xcc\x91\x73\x2b\x80\x22\xf6\x84"}; byte out[8], outplain[8]; - bool pass=true, fail; for (int i=0; i<2; i++) { @@ -2086,7 +2133,7 @@ bool ValidateBlowfish() ECB_Mode::Decryption dec((byte *)key[i], strlen(key[i])); dec.ProcessData(outplain, cipher[i], 8); fail = fail || memcmp(outplain, plain[i], 8); - pass = pass && !fail; + pass3 = pass3 && !fail; cout << (fail ? "FAILED " : "passed "); cout << '\"' << key[i] << '\"'; @@ -2097,68 +2144,178 @@ bool ValidateBlowfish() output.Put(out, 8); cout << endl; } - return pass; + return pass1 && pass2 && pass3; } bool ValidateThreeWay() { cout << "\n3-WAY validation suite running...\n\n"; + bool pass1 = true, pass2 = true; + + ThreeWayEncryption enc; // 96-bit only + pass1 = enc.StaticGetValidKeyLength(8) == 12 && pass1; + pass1 = enc.StaticGetValidKeyLength(12) == 12 && pass1; + pass1 = enc.StaticGetValidKeyLength(16) == 12 && pass1; + + ThreeWayDecryption dec; // 96-bit only + pass2 = dec.StaticGetValidKeyLength(8) == 12 && pass2; + pass2 = dec.StaticGetValidKeyLength(12) == 12 && pass2; + pass2 = dec.StaticGetValidKeyLength(16) == 12 && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/3wayval.dat", true, new HexDecoder); - return BlockTransformationTest(FixedRoundsCipherFactory(), valdata); + return BlockTransformationTest(FixedRoundsCipherFactory(), valdata) && pass1 && pass2; } bool ValidateGOST() { cout << "\nGOST validation suite running...\n\n"; + bool pass1 = true, pass2 = true; + + GOSTEncryption enc; // 256-bit only + pass1 = enc.StaticGetValidKeyLength(16) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(24) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(40) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1; + + GOSTDecryption dec; // 256-bit only + pass2 = dec.StaticGetValidKeyLength(16) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(24) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(40) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/gostval.dat", true, new HexDecoder); - return BlockTransformationTest(FixedRoundsCipherFactory(), valdata); + return BlockTransformationTest(FixedRoundsCipherFactory(), valdata) && pass1 && pass2; } bool ValidateSHARK() { cout << "\nSHARK validation suite running...\n\n"; + bool pass1 = true, pass2 = true; + + SHARKEncryption enc; // 128-bit only + pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(15) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(17) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(32) == 16 && pass1; + + SHARKDecryption dec; // 128-bit only + pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(15) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(17) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(32) == 16 && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/sharkval.dat", true, new HexDecoder); - return BlockTransformationTest(FixedRoundsCipherFactory(), valdata); + return BlockTransformationTest(FixedRoundsCipherFactory(), valdata) && pass1 && pass2; } bool ValidateCAST() { - bool pass = true; - cout << "\nCAST-128 validation suite running...\n\n"; + bool pass1 = true, pass2 = true, pass3 = true; + + CAST128Encryption enc1; // 40 to 128-bits (5 to 16-bytes) + pass1 = enc1.StaticGetValidKeyLength(4) == 5 && pass1; + pass1 = enc1.StaticGetValidKeyLength(5) == 5 && pass1; + pass1 = enc1.StaticGetValidKeyLength(15) == 15 && pass1; + pass1 = enc1.StaticGetValidKeyLength(16) == 16 && pass1; + pass1 = enc1.StaticGetValidKeyLength(17) == 16 && pass1; + + CAST128Decryption dec1; // 40 to 128-bits (5 to 16-bytes) + pass2 = dec1.StaticGetValidKeyLength(4) == 5 && pass2; + pass2 = dec1.StaticGetValidKeyLength(5) == 5 && pass2; + pass2 = dec1.StaticGetValidKeyLength(15) == 15 && pass2; + pass2 = dec1.StaticGetValidKeyLength(16) == 16 && pass2; + pass2 = dec1.StaticGetValidKeyLength(17) == 16 && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource val128(CRYPTOPP_DATA_DIR "TestData/cast128v.dat", true, new HexDecoder); - pass = BlockTransformationTest(FixedRoundsCipherFactory(16), val128, 1) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(10), val128, 1) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(5), val128, 1) && pass; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(16), val128, 1) && pass3; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(10), val128, 1) && pass3; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(5), val128, 1) && pass3; cout << "\nCAST-256 validation suite running...\n\n"; + bool pass4 = true, pass5 = true, pass6 = true; + + CAST256Encryption enc2; // 128, 160, 192, 224, or 256-bits (16 to 32-bytes, step 4) + pass4 = enc2.StaticGetValidKeyLength(15) == 16 && pass4; + pass4 = enc2.StaticGetValidKeyLength(16) == 16 && pass4; + pass4 = enc2.StaticGetValidKeyLength(17) == 20 && pass4; + pass4 = enc2.StaticGetValidKeyLength(20) == 20 && pass4; + pass4 = enc2.StaticGetValidKeyLength(24) == 24 && pass4; + pass4 = enc2.StaticGetValidKeyLength(28) == 28 && pass4; + pass4 = enc2.StaticGetValidKeyLength(31) == 32 && pass4; + pass4 = enc2.StaticGetValidKeyLength(32) == 32 && pass4; + pass4 = enc2.StaticGetValidKeyLength(33) == 32 && pass4; + + CAST256Decryption dec2; // 128, 160, 192, 224, or 256-bits (16 to 32-bytes, step 4) + pass5 = dec2.StaticGetValidKeyLength(15) == 16 && pass5; + pass5 = dec2.StaticGetValidKeyLength(16) == 16 && pass5; + pass5 = dec2.StaticGetValidKeyLength(17) == 20 && pass5; + pass5 = dec2.StaticGetValidKeyLength(20) == 20 && pass5; + pass5 = dec2.StaticGetValidKeyLength(24) == 24 && pass5; + pass5 = dec2.StaticGetValidKeyLength(28) == 28 && pass5; + pass5 = dec2.StaticGetValidKeyLength(31) == 32 && pass5; + pass5 = dec2.StaticGetValidKeyLength(32) == 32 && pass5; + pass5 = dec2.StaticGetValidKeyLength(33) == 32 && pass5; + cout << (pass4 && pass5 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource val256(CRYPTOPP_DATA_DIR "TestData/cast256v.dat", true, new HexDecoder); - pass = BlockTransformationTest(FixedRoundsCipherFactory(16), val256, 1) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(24), val256, 1) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(32), val256, 1) && pass; + pass6 = BlockTransformationTest(FixedRoundsCipherFactory(16), val256, 1) && pass6; + pass6 = BlockTransformationTest(FixedRoundsCipherFactory(24), val256, 1) && pass6; + pass6 = BlockTransformationTest(FixedRoundsCipherFactory(32), val256, 1) && pass6; - return pass; + return pass1 && pass2 && pass3 && pass4 && pass5 && pass6; } bool ValidateSquare() { cout << "\nSquare validation suite running...\n\n"; + bool pass1 = true, pass2 = true; + + SquareEncryption enc; // 128-bits only + pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(15) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(17) == 16 && pass1; + + SquareDecryption dec; // 128-bits only + pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(15) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(17) == 16 && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/squareva.dat", true, new HexDecoder); - return BlockTransformationTest(FixedRoundsCipherFactory(), valdata); + return BlockTransformationTest(FixedRoundsCipherFactory(), valdata) && pass1 && pass2; } bool ValidateSKIPJACK() { cout << "\nSKIPJACK validation suite running...\n\n"; + bool pass1 = true, pass2 = true; + + SKIPJACKEncryption enc; // 80-bits only + pass1 = enc.StaticGetValidKeyLength(8) == 10 && pass1; + pass1 = enc.StaticGetValidKeyLength(9) == 10 && pass1; + pass1 = enc.StaticGetValidKeyLength(10) == 10 && pass1; + pass1 = enc.StaticGetValidKeyLength(16) == 10 && pass1; + + SKIPJACKDecryption dec; // 80-bits only + pass2 = dec.StaticGetValidKeyLength(8) == 10 && pass2; + pass2 = dec.StaticGetValidKeyLength(9) == 10 && pass2; + pass2 = dec.StaticGetValidKeyLength(10) == 10 && pass2; + pass2 = dec.StaticGetValidKeyLength(16) == 10 && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/skipjack.dat", true, new HexDecoder); - return BlockTransformationTest(FixedRoundsCipherFactory(), valdata); + return BlockTransformationTest(FixedRoundsCipherFactory(), valdata) && pass1 && pass2; } bool ValidateSEAL() From 0d14a527906b4f1dc5700c0e0b5913a7aad444e4 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 5 Sep 2016 13:01:44 -0400 Subject: [PATCH 12/28] Fix RC6 VariableKeyLength information (Issue 252) --- rc6.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rc6.h b/rc6.h index 13b7bfa9..9ac55d80 100644 --- a/rc6.h +++ b/rc6.h @@ -13,7 +13,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \class RC6_Info //! \brief RC6 block cipher information -struct RC6_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 0, 255>, public VariableRounds<20> +struct RC6_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 8>, public VariableRounds<20> { static const char *StaticAlgorithmName() {return "RC6";} typedef word32 RC6_WORD; From 9767221125a27a5bca8b98b4a67a86aac05162a0 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 5 Sep 2016 13:06:26 -0400 Subject: [PATCH 13/28] Fix MARS VariableKeyLength information (Issue 252) --- mars.cpp | 4 ++-- mars.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mars.cpp b/mars.cpp index aa7693e6..af7e12fd 100644 --- a/mars.cpp +++ b/mars.cpp @@ -58,7 +58,7 @@ void MARS::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, by unsigned int i; word32 a, b, c, d, l, m, r, t; const word32 *k = m_k; - + Block::Get(inBlock)(a)(b)(c)(d); a += k[0]; b += k[1]; c += k[2]; d += k[3]; @@ -109,7 +109,7 @@ void MARS::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, by const word32 *k = m_k; Block::Get(inBlock)(d)(c)(b)(a); - + d += k[36]; c += k[37]; b += k[38]; a += k[39]; for (i=0; i<8; i++) diff --git a/mars.h b/mars.h index 3a59c1e8..b8b4e2c3 100644 --- a/mars.h +++ b/mars.h @@ -13,7 +13,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \class MARS_Info //! \brief MARS block cipher information -struct MARS_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 56, 4> +struct MARS_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 8> { static const char *StaticAlgorithmName() {return "MARS";} }; From edf7bd8fa6e1f7d97f2f8630943fc8dc7296a29b Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 5 Sep 2016 13:08:15 -0400 Subject: [PATCH 14/28] Add StaticGetValidKeyLength tests for MARS, RC5 and RC6 block ciphers (Issue 252) --- validat1.cpp | 146 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 126 insertions(+), 20 deletions(-) diff --git a/validat1.cpp b/validat1.cpp index 0b57d5aa..9dd0848f 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -1966,33 +1966,93 @@ bool ValidateARC4() bool ValidateRC5() { cout << "\nRC5 validation suite running...\n\n"; + bool pass1 = true, pass2 = true; + + RC5Encryption enc; // 0 to 2040-bits (255-bytes) + pass1 = enc.StaticGetValidKeyLength(0) == 0 && pass1; + pass1 = enc.StaticGetValidKeyLength(254) == 254 && pass1; + pass1 = enc.StaticGetValidKeyLength(255) == 255 && pass1; + pass1 = enc.StaticGetValidKeyLength(256) == 255 && pass1; + pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1; + pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1; + + RC5Decryption dec; + pass2 = dec.StaticGetValidKeyLength(0) == 0 && pass2; + pass2 = dec.StaticGetValidKeyLength(254) == 254 && pass2; + pass2 = dec.StaticGetValidKeyLength(255) == 255 && pass2; + pass2 = dec.StaticGetValidKeyLength(256) == 255 && pass2; + pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2; + pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/rc5val.dat", true, new HexDecoder); - return BlockTransformationTest(VariableRoundsCipherFactory(16, 12), valdata); + return BlockTransformationTest(VariableRoundsCipherFactory(16, 12), valdata) && pass1 && pass2; } bool ValidateRC6() { cout << "\nRC6 validation suite running...\n\n"; + bool pass1 = true, pass2 = true, pass3 = true; + + RC6Encryption enc; + pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1; + pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1; + pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1; + + RC6Decryption dec; + pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2; + pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2; + pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/rc6val.dat", true, new HexDecoder); - bool pass = true; - pass = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 2) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(24), valdata, 2) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(32), valdata, 2) && pass; - return pass; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 2) && pass3; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(24), valdata, 2) && pass3; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(32), valdata, 2) && pass3; + return pass1 && pass2 && pass3; } bool ValidateMARS() { cout << "\nMARS validation suite running...\n\n"; + bool pass1 = true, pass2 = true, pass3 = true; + + MARSEncryption enc; + pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1; + pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1; + pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1; + + MARSDecryption dec; + pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2; + pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2; + pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/marsval.dat", true, new HexDecoder); - bool pass = true; - pass = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 4) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(24), valdata, 3) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(32), valdata, 2) && pass; - return pass; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 4) && pass3; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(24), valdata, 3) && pass3; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(32), valdata, 2) && pass3; + return pass1 && pass2 && pass3; } bool ValidateRijndael() @@ -2007,6 +2067,8 @@ bool ValidateRijndael() pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1; pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1; pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1; + pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1; RijndaelDecryption dec; pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; @@ -2015,6 +2077,8 @@ bool ValidateRijndael() pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2; pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2; pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2; + pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2; cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/rijndael.dat", true, new HexDecoder); @@ -2416,24 +2480,66 @@ bool ValidateBaseCode() bool ValidateSHACAL2() { cout << "\nSHACAL-2 validation suite running...\n\n"; + bool pass1 = true, pass2 = true, pass3 = true; + + SHACAL2Encryption enc; // 128 to 512-bits (16 to 64-bytes) + pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(15) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(64) == 64 && pass1; + pass1 = enc.StaticGetValidKeyLength(65) == 64 && pass1; + pass1 = enc.StaticGetValidKeyLength(128) == 64 && pass1; + pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1; + pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1; + + SHACAL2Decryption dec; // 128 to 512-bits (16 to 64-bytes) + pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(15) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(64) == 64 && pass2; + pass2 = dec.StaticGetValidKeyLength(65) == 64 && pass2; + pass2 = dec.StaticGetValidKeyLength(128) == 64 && pass2; + pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2; + pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; - bool pass = true; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/shacal2v.dat", true, new HexDecoder); - pass = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 4) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(64), valdata, 10) && pass; - return pass; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 4) && pass3; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(64), valdata, 10) && pass3; + return pass1 && pass2 && pass3; } bool ValidateCamellia() { cout << "\nCamellia validation suite running...\n\n"; + bool pass1 = true, pass2 = true, pass3 = true; + + CamelliaEncryption enc; + pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1; + pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1; + pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1; + pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1; + pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1; + + CamelliaDecryption dec; + pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2; + pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2; + pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2; + pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2; + pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2; + cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; - bool pass = true; FileSource valdata(CRYPTOPP_DATA_DIR "TestData/camellia.dat", true, new HexDecoder); - pass = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 15) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(24), valdata, 15) && pass; - pass = BlockTransformationTest(FixedRoundsCipherFactory(32), valdata, 15) && pass; - return pass; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(16), valdata, 15) && pass3; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(24), valdata, 15) && pass3; + pass3 = BlockTransformationTest(FixedRoundsCipherFactory(32), valdata, 15) && pass3; + return pass1 && pass2 && pass3; } bool ValidateSalsa() From 33522b39b1de5806798bded49e481baecb56dc58 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 5 Sep 2016 13:57:33 -0400 Subject: [PATCH 15/28] Updated documentation --- seckey.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/seckey.h b/seckey.h index 1013e8dc..31286ce6 100644 --- a/seckey.h +++ b/seckey.h @@ -79,7 +79,6 @@ protected: //! \param alg an Algorithm object used if the number of rounds are invalid //! \throws InvalidRounds if the number of rounds are invalid //! \details ThrowIfInvalidRounds() validates the number of rounds and throws if invalid. - //! The function is not a C++11 constexpr due to the potential throw operation. inline void ThrowIfInvalidRounds(int rounds, const Algorithm *alg) { if (M == INT_MAX) // Coverity and result_independent_of_operands @@ -100,7 +99,6 @@ protected: //! \returns the number of rounds for the algorithm //! \throws InvalidRounds if the number of rounds are invalid //! \details GetRoundsAndThrowIfInvalid() validates the number of rounds and throws if invalid. - //! The function is not a C++11 constexpr due to the potential throw operation. inline unsigned int GetRoundsAndThrowIfInvalid(const NameValuePairs ¶m, const Algorithm *alg) { int rounds = param.GetIntValueWithDefault("Rounds", DEFAULT_ROUNDS); @@ -256,7 +254,7 @@ public: //! \tparam BASE a SimpleKeyingInterface derived class //! \tparam INFO a SimpleKeyingInterface derived class //! \details SimpleKeyingInterfaceImpl() provides a default implementation for ciphers providing a keying interface. -//! Functions are virtual and not subject to C++11 constexpr. +//! Functions are virtual and not eligible for C++11 constexpr-ness. //! \sa Algorithm(), SimpleKeyingInterface() template class CRYPTOPP_NO_VTABLE SimpleKeyingInterfaceImpl : public BASE @@ -304,7 +302,7 @@ public: //! \tparam INFO a SimpleKeyingInterface derived class //! \tparam BASE a SimpleKeyingInterface derived class //! \details BlockCipherImpl() provides a default implementation for block ciphers using AlgorithmImpl() -//! and SimpleKeyingInterfaceImpl(). Functions are virtual and not subject to C++11 constexpr. +//! and SimpleKeyingInterfaceImpl(). Functions are virtual and not eligible for C++11 constexpr-ness. //! \sa Algorithm(), SimpleKeyingInterface(), AlgorithmImpl(), SimpleKeyingInterfaceImpl() template class CRYPTOPP_NO_VTABLE BlockCipherImpl : public AlgorithmImpl > > From 45323bddd83982a183714d91167194f73bab1440 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Tue, 6 Sep 2016 02:51:16 -0400 Subject: [PATCH 16/28] Initial fix for older Apple ld's non_lazy_ptr missing symbols (Issue 255) --- 3way.cpp | 9 +++++-- des.cpp | 14 ++++++++--- des.h | 10 +++++++- gost.cpp | 7 ++++-- gost.h | 6 +++++ idea.cpp | 23 ++++++++++-------- mdc.h | 18 +++++++++----- panama.cpp | 37 +++++++++++++++------------- panama.h | 24 ++++++++++++------ salsa.cpp | 69 ++++++++++++++++++++++++++++------------------------ salsa.h | 10 +++++--- seal.cpp | 7 ++++-- seal.h | 12 +++++++-- seed.cpp | 3 +++ shark.cpp | 3 +++ shark.h | 6 +++++ skipjack.cpp | 7 ++++-- skipjack.h | 6 +++++ square.cpp | 21 +++++++++------- tea.cpp | 15 +++++++----- tea.h | 18 ++++++++++++++ ttmac.cpp | 3 +++ ttmac.h | 13 +++++++--- wake.cpp | 5 +++- wake.h | 11 +++++++-- 25 files changed, 245 insertions(+), 112 deletions(-) diff --git a/3way.cpp b/3way.cpp index ad57edf5..c6784ee8 100644 --- a/3way.cpp +++ b/3way.cpp @@ -15,6 +15,11 @@ void ThreeWay_TestInstantiations() } #endif +// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +static const size_t s_unused1 = ThreeWay::KEYLENGTH; +static const size_t s_unused2 = ThreeWayEncryption::KEYLENGTH; +static const size_t s_unused3 = ThreeWayDecryption::KEYLENGTH; + static const word32 START_E = 0x0b0b; // round constant of first encryption round static const word32 START_D = 0xb1b1; // round constant of first decryption round #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 @@ -57,13 +62,13 @@ static inline word32 reverseBits(word32 a) a0 ^= c ^ b0; \ a1 ^= c ^ b1; \ a2 ^= c ^ (b0 >> 16) ^ (b1 << 16); \ -} +} #define rho(a0, a1, a2) \ { \ theta(a0, a1, a2); \ pi_gamma_pi(a0, a1, a2); \ -} +} void ThreeWay::Base::UncheckedSetKey(const byte *uk, unsigned int length, const NameValuePairs ¶ms) { diff --git a/des.cpp b/des.cpp index 7e6e45fa..c468403c 100644 --- a/des.cpp +++ b/des.cpp @@ -20,6 +20,12 @@ NAMESPACE_BEGIN(CryptoPP) +// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +static const size_t s_unused1 = DES::KEYLENGTH; +static const size_t s_unused2 = DES_EDE2::KEYLENGTH; +static const size_t s_unused3 = DES_EDE3::KEYLENGTH; +static const size_t s_unused4 = DES_XEX3::KEYLENGTH; + typedef BlockGetAndPut Block; // Richard Outerbridge's initial permutation algorithm @@ -70,8 +76,8 @@ inline void FPERM(word32 &left, word32 &right) } */ -// Wei Dai's modification to Richard Outerbridge's initial permutation -// algorithm, this one is faster if you have access to rotate instructions +// Wei Dai's modification to Richard Outerbridge's initial permutation +// algorithm, this one is faster if you have access to rotate instructions // (like in MSVC) static inline void IPERM(word32 &left, word32 &right) { @@ -283,7 +289,7 @@ void RawDES::RawSetKey(CipherDir dir, const byte *key) byte *const ks=pcr+56; register int i,j,l; int m; - + for (j=0; j<56; j++) { /* convert pc1 to bits of key */ l=pc1[j]-1; /* integer bit location */ m = l & 07; /* find bit */ @@ -314,7 +320,7 @@ void RawDES::RawSetKey(CipherDir dir, const byte *key) | ((word32)ks[5] << 8) | ((word32)ks[7]); } - + if (dir==DECRYPTION) // reverse key schedule order for (i=0; i<16; i+=2) { diff --git a/des.h b/des.h index 429d2e49..5ada3513 100644 --- a/des.h +++ b/des.h @@ -35,12 +35,14 @@ struct DES_Info : public FixedBlockSize<8>, public FixedKeyLength<8> //! \class DES //! \brief DES block cipher -//! \details The DES implementation in Crypto++ ignores the parity bits +//! \details The DES implementation in Crypto++ ignores the parity bits //! (the least significant bits of each byte) in the key. However you can use CheckKeyParityBits() //! and CorrectKeyParityBits() to check or correct the parity bits if you wish. //! \sa DES class DES : public DES_Info, public BlockCipherDocumentation { + //! \class Base + //! \brief DES block cipher default operation class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl, public RawDES { public: @@ -70,6 +72,8 @@ struct DES_EDE2_Info : public FixedBlockSize<8>, public FixedKeyLength<16> /// \sa DES-EDE2 class DES_EDE2 : public DES_EDE2_Info, public BlockCipherDocumentation { + //! \class Base + //! \brief DES_EDE2 block cipher default operation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl { public: @@ -97,6 +101,8 @@ struct DES_EDE3_Info : public FixedBlockSize<8>, public FixedKeyLength<24> //! \sa DES-EDE3 class DES_EDE3 : public DES_EDE3_Info, public BlockCipherDocumentation { + //! \class Base + //! \brief DES_EDE3 block cipher default operation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl { public: @@ -124,6 +130,8 @@ struct DES_XEX3_Info : public FixedBlockSize<8>, public FixedKeyLength<24> //! \sa DES-XEX3, AKA DESX class DES_XEX3 : public DES_XEX3_Info, public BlockCipherDocumentation { + //! \class Base + //! \brief DES_XEX3 block cipher default operation class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl { public: diff --git a/gost.cpp b/gost.cpp index f502f8a1..f60cf031 100644 --- a/gost.cpp +++ b/gost.cpp @@ -4,6 +4,9 @@ NAMESPACE_BEGIN(CryptoPP) +// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +static const size_t s_unused = GOST::KEYLENGTH; + // these are the S-boxes given in Applied Cryptography 2nd Ed., p. 333 const byte GOST::Base::sBox[8][16]={ {4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3}, @@ -24,7 +27,7 @@ const byte GOST::Base::sBox[8][16]={ { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 }, {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 }, {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 }, - {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 }}; + {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 }}; */ volatile bool GOST::Base::sTableCalculated = false; @@ -44,7 +47,7 @@ void GOST::Base::PrecalculateSTable() if (!sTableCalculated) { for (unsigned i = 0; i < 4; i++) - for (unsigned j = 0; j < 256; j++) + for (unsigned j = 0; j < 256; j++) { word32 temp = sBox[2*i][j%16] | (sBox[2*i+1][j/16] << 4); sTable[i][j] = rotlMod(temp, 11+8*i); diff --git a/gost.h b/gost.h index 12dbb344..116e3710 100644 --- a/gost.h +++ b/gost.h @@ -23,6 +23,8 @@ struct GOST_Info : public FixedBlockSize<8>, public FixedKeyLength<32> //! \sa GOST class GOST : public GOST_Info, public BlockCipherDocumentation { + //! \class Base + //! \brief GOST block cipher default operation class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl { public: @@ -38,12 +40,16 @@ class GOST : public GOST_Info, public BlockCipherDocumentation FixedSizeSecBlock key; }; + //! \class Enc + //! \brief GOST block cipher encryption operation class CRYPTOPP_NO_VTABLE Enc : public Base { public: void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; }; + //! \class Dec + //! \brief GOST block cipher decryption operation class CRYPTOPP_NO_VTABLE Dec : public Base { public: diff --git a/idea.cpp b/idea.cpp index fe961743..96f946af 100644 --- a/idea.cpp +++ b/idea.cpp @@ -7,6 +7,9 @@ NAMESPACE_BEGIN(CryptoPP) +// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +static const size_t s_unused = IDEA::KEYLENGTH; + static const int IDEA_KEYLEN=(6*IDEA::ROUNDS+4); // key schedule length in # of word16s #define low16(x) ((x)&0xffff) // compiler should be able to optimize this away if word is 16 bits @@ -42,16 +45,16 @@ void IDEA::Base::BuildLogTables() else { tablesBuilt = true; - + IDEA::Word x=1; word32 i; - + for (i=0; i<0x10000; i++) { antilog[i] = (word16)x; DirectMUL(x, 3); } - + for (i=0; i<0x10000; i++) log[antilog[i]] = (word16)i; } @@ -82,16 +85,16 @@ inline void IDEA::Base::LookupMUL(IDEA::Word &a, IDEA::Word b) void IDEA::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &) { AssertValidKeyLength(length); - + #ifdef IDEA_LARGECACHE BuildLogTables(); #endif - + EnKey(userKey); - + if (!IsForwardTransformation()) DeKey(); - + #ifdef IDEA_LARGECACHE LookupKeyLogs(); #endif @@ -100,10 +103,10 @@ void IDEA::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const void IDEA::Base::EnKey (const byte *userKey) { unsigned int i; - + for (i=0; i<8; i++) m_key[i] = ((IDEA::Word)userKey[2*i]<<8) | userKey[2*i+1]; - + for (; i struct MDC_Info : public FixedBlockSize, public FixedKeyLength { static std::string StaticAlgorithmName() {return std::string("MDC/")+T::StaticAlgorithmName();} }; -//! MDC -/*! a construction by Peter Gutmann to turn an iterated hash function into a PRF */ + +//! \class MDC +//! \brief MDC cipher +//! \details MDC() is a construction by Peter Gutmann to turn an iterated hash function into a PRF +//! \sa MDC template class MDC : public MDC_Info { + //! \class Enc + //! \brief MDC cipher encryption operation class CRYPTOPP_NO_VTABLE Enc : public BlockCipherImpl > { typedef typename T::HashWordType HashWordType; diff --git a/panama.cpp b/panama.cpp index 1631e02c..2c76c934 100644 --- a/panama.cpp +++ b/panama.cpp @@ -12,11 +12,14 @@ #include "cpu.h" NAMESPACE_BEGIN(CryptoPP) - + #if CRYPTOPP_MSC_VERSION # pragma warning(disable: 4731) #endif +// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +static const size_t s_unused = PanamaCipher<>::KEYLENGTH; + template void Panama::Reset() { @@ -385,22 +388,22 @@ void Panama::Iterate(size_t count, const word32 *p, byte *output, const byte UL(0); UL(1); UL(2); UL(3); UL(4); UL(5); UL(6); UL(7); } - GP(0); - GP(1); - GP(2); - GP(3); - GP(4); - GP(5); - GP(6); + GP(0); + GP(1); + GP(2); + GP(3); + GP(4); + GP(5); + GP(6); GP(7); - GP(8); - GP(9); - GP(10); - GP(11); - GP(12); - GP(13); - GP(14); - GP(15); + GP(8); + GP(9); + GP(10); + GP(11); + GP(12); + GP(13); + GP(14); + GP(15); GP(16); T(0,1); @@ -434,7 +437,7 @@ void PanamaHash::TruncatedFinal(byte *hash, size_t size) this->ThrowIfInvalidTruncatedSize(size); this->PadLastBlock(this->BLOCKSIZE, 0x01); - + HashEndianCorrectedBlock(this->m_data); this->Iterate(32); // pull diff --git a/panama.h b/panama.h index 18e60338..b1a41e61 100644 --- a/panama.h +++ b/panama.h @@ -1,7 +1,7 @@ // panama.h - written and placed in the public domain by Wei Dai //! \file panama.h -//! \brief Classes for Panama stream cipher +//! \brief Classes for Panama hash and stream cipher #ifndef CRYPTOPP_PANAMA_H #define CRYPTOPP_PANAMA_H @@ -17,7 +17,7 @@ NAMESPACE_BEGIN(CryptoPP) -/// base class, do not use directly +// Base class, do not use directly template class CRYPTOPP_NO_VTABLE Panama { @@ -33,7 +33,9 @@ protected: }; namespace Weak { -/// Panama Hash +//! \class PanamaHash +//! \brief Panama hash +//! \sa Panama Hash template class PanamaHash : protected Panama, public AlgorithmImpl, PanamaHash > { @@ -52,7 +54,8 @@ protected: }; } -//! MAC construction using a hermetic hash function +//! \class HermeticHashFunctionMAC +//! \brief MAC construction using a hermetic hash function template class HermeticHashFunctionMAC : public AlgorithmImpl > >, T_Info> { @@ -108,7 +111,8 @@ protected: }; namespace Weak { -/// Panama MAC +//! \class PanamaMAC +//! \brief Panama message authentication code template class PanamaMAC : public HermeticHashFunctionMAC > { @@ -119,14 +123,16 @@ public: }; } -//! algorithm info +//! \class PanamaCipherInfo +//! \brief Panama stream cipher information template struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32> { static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";} }; -//! _ +//! \class PanamaCipherPolicy +//! \brief Panama stream cipher operation template class PanamaCipherPolicy : public AdditiveCipherConcretePolicy, public PanamaCipherInfo, @@ -142,7 +148,9 @@ protected: FixedSizeSecBlock m_key; }; -//! Panama Stream Cipher +//! \class PanamaCipher +//! \brief Panama stream cipher +//! \sa Panama Stream Cipher template struct PanamaCipher : public PanamaCipherInfo, public SymmetricCipherDocumentation { diff --git a/salsa.cpp b/salsa.cpp index d13e4e4d..603ae521 100644 --- a/salsa.cpp +++ b/salsa.cpp @@ -35,10 +35,15 @@ NAMESPACE_BEGIN(CryptoPP) #if !defined(NDEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING) void Salsa20_TestInstantiations() { - Salsa20::Encryption x; + Salsa20::Encryption x1; + XSalsa20::Encryption x2; } #endif +// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +// static const size_t s_unused1 = Salsa20::KEYLENGTH; +static const size_t s_unused2 = XSalsa20::KEYLENGTH; + void Salsa20_Policy::CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length) { m_rounds = params.GetIntValueWithDefault(Name::Rounds(), 20); @@ -247,37 +252,37 @@ void Salsa20_Policy::OperateKeystream(KeystreamOperation operation, byte *output AS2( pxor xmm##b, xmm5) #define L01(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##A, [SSE2_WORKSPACE + d*16 + i*256]) /* y3 */ -#define L02(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##C, [SSE2_WORKSPACE + a*16 + i*256]) /* y0 */ -#define L03(A,B,C,D,a,b,c,d,i) AS2( paddd xmm##A, xmm##C) /* y0+y3 */ -#define L04(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##B, xmm##A) -#define L05(A,B,C,D,a,b,c,d,i) AS2( pslld xmm##A, 7) -#define L06(A,B,C,D,a,b,c,d,i) AS2( psrld xmm##B, 32-7) -#define L07(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, [SSE2_WORKSPACE + b*16 + i*256]) -#define L08(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, xmm##B) /* z1 */ -#define L09(A,B,C,D,a,b,c,d,i) AS2( movdqa [SSE2_WORKSPACE + b*16], xmm##A) -#define L10(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##B, xmm##A) -#define L11(A,B,C,D,a,b,c,d,i) AS2( paddd xmm##A, xmm##C) /* z1+y0 */ -#define L12(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##D, xmm##A) -#define L13(A,B,C,D,a,b,c,d,i) AS2( pslld xmm##A, 9) -#define L14(A,B,C,D,a,b,c,d,i) AS2( psrld xmm##D, 32-9) -#define L15(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, [SSE2_WORKSPACE + c*16 + i*256]) -#define L16(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, xmm##D) /* z2 */ -#define L17(A,B,C,D,a,b,c,d,i) AS2( movdqa [SSE2_WORKSPACE + c*16], xmm##A) -#define L18(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##D, xmm##A) -#define L19(A,B,C,D,a,b,c,d,i) AS2( paddd xmm##A, xmm##B) /* z2+z1 */ -#define L20(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##B, xmm##A) -#define L21(A,B,C,D,a,b,c,d,i) AS2( pslld xmm##A, 13) -#define L22(A,B,C,D,a,b,c,d,i) AS2( psrld xmm##B, 32-13) -#define L23(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, [SSE2_WORKSPACE + d*16 + i*256]) -#define L24(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, xmm##B) /* z3 */ -#define L25(A,B,C,D,a,b,c,d,i) AS2( movdqa [SSE2_WORKSPACE + d*16], xmm##A) -#define L26(A,B,C,D,a,b,c,d,i) AS2( paddd xmm##A, xmm##D) /* z3+z2 */ -#define L27(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##D, xmm##A) -#define L28(A,B,C,D,a,b,c,d,i) AS2( pslld xmm##A, 18) -#define L29(A,B,C,D,a,b,c,d,i) AS2( psrld xmm##D, 32-18) -#define L30(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, xmm##C) /* xor y0 */ -#define L31(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, xmm##D) /* z0 */ -#define L32(A,B,C,D,a,b,c,d,i) AS2( movdqa [SSE2_WORKSPACE + a*16], xmm##A) +#define L02(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##C, [SSE2_WORKSPACE + a*16 + i*256]) /* y0 */ +#define L03(A,B,C,D,a,b,c,d,i) AS2( paddd xmm##A, xmm##C) /* y0+y3 */ +#define L04(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##B, xmm##A) +#define L05(A,B,C,D,a,b,c,d,i) AS2( pslld xmm##A, 7) +#define L06(A,B,C,D,a,b,c,d,i) AS2( psrld xmm##B, 32-7) +#define L07(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, [SSE2_WORKSPACE + b*16 + i*256]) +#define L08(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, xmm##B) /* z1 */ +#define L09(A,B,C,D,a,b,c,d,i) AS2( movdqa [SSE2_WORKSPACE + b*16], xmm##A) +#define L10(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##B, xmm##A) +#define L11(A,B,C,D,a,b,c,d,i) AS2( paddd xmm##A, xmm##C) /* z1+y0 */ +#define L12(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##D, xmm##A) +#define L13(A,B,C,D,a,b,c,d,i) AS2( pslld xmm##A, 9) +#define L14(A,B,C,D,a,b,c,d,i) AS2( psrld xmm##D, 32-9) +#define L15(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, [SSE2_WORKSPACE + c*16 + i*256]) +#define L16(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, xmm##D) /* z2 */ +#define L17(A,B,C,D,a,b,c,d,i) AS2( movdqa [SSE2_WORKSPACE + c*16], xmm##A) +#define L18(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##D, xmm##A) +#define L19(A,B,C,D,a,b,c,d,i) AS2( paddd xmm##A, xmm##B) /* z2+z1 */ +#define L20(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##B, xmm##A) +#define L21(A,B,C,D,a,b,c,d,i) AS2( pslld xmm##A, 13) +#define L22(A,B,C,D,a,b,c,d,i) AS2( psrld xmm##B, 32-13) +#define L23(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, [SSE2_WORKSPACE + d*16 + i*256]) +#define L24(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, xmm##B) /* z3 */ +#define L25(A,B,C,D,a,b,c,d,i) AS2( movdqa [SSE2_WORKSPACE + d*16], xmm##A) +#define L26(A,B,C,D,a,b,c,d,i) AS2( paddd xmm##A, xmm##D) /* z3+z2 */ +#define L27(A,B,C,D,a,b,c,d,i) AS2( movdqa xmm##D, xmm##A) +#define L28(A,B,C,D,a,b,c,d,i) AS2( pslld xmm##A, 18) +#define L29(A,B,C,D,a,b,c,d,i) AS2( psrld xmm##D, 32-18) +#define L30(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, xmm##C) /* xor y0 */ +#define L31(A,B,C,D,a,b,c,d,i) AS2( pxor xmm##A, xmm##D) /* z0 */ +#define L32(A,B,C,D,a,b,c,d,i) AS2( movdqa [SSE2_WORKSPACE + a*16], xmm##A) #define SSE2_QUARTER_ROUND_X8(i, a, b, c, d, e, f, g, h) \ L01(0,1,2,3, a,b,c,d, i) L01(4,5,6,7, e,f,g,h, i) \ diff --git a/salsa.h b/salsa.h index 54d1ec69..6a70af6e 100644 --- a/salsa.h +++ b/salsa.h @@ -19,12 +19,14 @@ NAMESPACE_BEGIN(CryptoPP) //! \class Salsa20_Info -//! \brief Salsa stream cipher information +//! \brief Salsa20 stream cipher information struct Salsa20_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8> { static const char *StaticAlgorithmName() {return "Salsa20";} }; +//! \class Salsa20_Policy +//! \brief Salsa20 stream cipher operation class CRYPTOPP_NO_VTABLE Salsa20_Policy : public AdditiveCipherConcretePolicy { protected: @@ -43,7 +45,7 @@ protected: }; //! \class Salsa20 -//! \brief Salsa20 stream cipher information +//! \brief Salsa20 stream cipher //! \details Salsa20 provides a variable number of rounds: 8, 12 or 20. The default number of rounds is 20. //! \sa XSalsa20 struct Salsa20 : public Salsa20_Info, public SymmetricCipherDocumentation @@ -59,6 +61,8 @@ struct XSalsa20_Info : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_I static const char *StaticAlgorithmName() {return "XSalsa20";} }; +//! \class XSalsa20_Policy +//! \brief XSalsa20 stream cipher operation class CRYPTOPP_NO_VTABLE XSalsa20_Policy : public Salsa20_Policy { public: @@ -70,7 +74,7 @@ protected: }; //! \class XSalsa20 -//! \brief XSalsa20 stream cipher information +//! \brief XSalsa20 stream cipher //! \details XSalsa20 provides a variable number of rounds: 8, 12 or 20. The default number of rounds is 20. //! \sa XSalsa20 struct XSalsa20 : public XSalsa20_Info, public SymmetricCipherDocumentation diff --git a/seal.cpp b/seal.cpp index 15392ac0..641a3207 100644 --- a/seal.cpp +++ b/seal.cpp @@ -17,6 +17,9 @@ void SEAL_TestInstantiations() } #endif +// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +static const size_t s_unused = SEAL<>::KEYLENGTH; + struct SEAL_Gamma { SEAL_Gamma(const byte *key) @@ -139,7 +142,7 @@ void SEAL_Policy::OperateKeystream(KeystreamOperation operation, byte *output p = d & 0x7fc; a += Ttab(p); d = rotrFixed(d, 9U); - + // generate 8192 bits for (unsigned int i=0; i<64; i++) { @@ -197,7 +200,7 @@ void SEAL_Policy::OperateKeystream(KeystreamOperation operation, byte *output else { a += n1; - b += n2; + b += n2; c ^= n1; d ^= n2; } diff --git a/seal.h b/seal.h index cbf5edb1..9c898e52 100644 --- a/seal.h +++ b/seal.h @@ -11,13 +11,18 @@ NAMESPACE_BEGIN(CryptoPP) -//! _ +//! \class SEAL_Info +//! \brief SEAL stream cipher information +//! \tparam B Endianess of the stream cipher template struct SEAL_Info : public FixedKeyLength<20, SimpleKeyingInterface::INTERNALLY_GENERATED_IV, 4> { static const char *StaticAlgorithmName() {return B::ToEnum() == LITTLE_ENDIAN_ORDER ? "SEAL-3.0-LE" : "SEAL-3.0-BE";} }; +//! \class SEAL_Policy +//! \brief SEAL stream cipher operation +//! \tparam B Endianess of the stream cipher template class CRYPTOPP_NO_VTABLE SEAL_Policy : public AdditiveCipherConcretePolicy, public SEAL_Info { @@ -37,7 +42,10 @@ private: word32 m_outsideCounter, m_insideCounter; }; -//! SEAL +//! \class SEAL +//! \brief SEAL stream cipher +//! \tparam B Endianess of the stream cipher +//! \sa SEAL template struct SEAL : public SEAL_Info, public SymmetricCipherDocumentation { diff --git a/seed.cpp b/seed.cpp index f6a9690f..58905fe3 100644 --- a/seed.cpp +++ b/seed.cpp @@ -6,6 +6,9 @@ NAMESPACE_BEGIN(CryptoPP) +// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +static const size_t s_unused = SEED::KEYLENGTH; + static const word32 s_kc[16] = { 0x9e3779b9, 0x3c6ef373, 0x78dde6e6, 0xf1bbcdcc, 0xe3779b99, 0xc6ef3733, 0x8dde6e67, 0x1bbcdccf, 0x3779b99e, 0x6ef3733c, 0xdde6e678, 0xbbcdccf1, 0x779b99e3, 0xef3733c6, 0xde6e678d, 0xbcdccf1b}; diff --git a/shark.cpp b/shark.cpp index 99d63d21..503d00b6 100644 --- a/shark.cpp +++ b/shark.cpp @@ -12,6 +12,9 @@ NAMESPACE_BEGIN(CryptoPP) +// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +static const size_t s_unused = SHARK::KEYLENGTH; + static word64 SHARKTransform(word64 a) { static const byte iG[8][8] = { diff --git a/shark.h b/shark.h index 0998011a..bf413c92 100644 --- a/shark.h +++ b/shark.h @@ -24,6 +24,8 @@ struct SHARK_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public /// SHARK-E class SHARK : public SHARK_Info, public BlockCipherDocumentation { + //! \class Base + //! \brief SHARK block cipher default operation class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl { public: @@ -34,6 +36,8 @@ class SHARK : public SHARK_Info, public BlockCipherDocumentation SecBlock m_roundKeys; }; + //! \class Enc + //! \brief SHARK block cipher encryption operation class CRYPTOPP_NO_VTABLE Enc : public Base { public: @@ -47,6 +51,8 @@ class SHARK : public SHARK_Info, public BlockCipherDocumentation static const word64 cbox[8][256]; }; + //! \class Dec + //! \brief SHARK block cipher decryption operation class CRYPTOPP_NO_VTABLE Dec : public Base { public: diff --git a/skipjack.cpp b/skipjack.cpp index 7b3afdfa..c52baf49 100644 --- a/skipjack.cpp +++ b/skipjack.cpp @@ -7,7 +7,7 @@ #include "skipjack.h" -/* +/* * Optimized implementation of SKIPJACK algorithm * * originally written by Panu Rissanen 1998.06.24 @@ -17,10 +17,13 @@ NAMESPACE_BEGIN(CryptoPP) +// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +static const size_t s_unused = SKIPJACK::KEYLENGTH; + /** * The F-table byte permutation (see description of the G-box permutation) */ -const byte SKIPJACK::Base::fTable[256] = { +const byte SKIPJACK::Base::fTable[256] = { 0xa3,0xd7,0x09,0x83,0xf8,0x48,0xf6,0xf4,0xb3,0x21,0x15,0x78,0x99,0xb1,0xaf,0xf9, 0xe7,0x2d,0x4d,0x8a,0xce,0x4c,0xca,0x2e,0x52,0x95,0xd9,0x1e,0x4e,0x38,0x44,0x28, 0x0a,0xdf,0x02,0xa0,0x17,0xf1,0x60,0x68,0x12,0xb7,0x7a,0xc3,0xe9,0xfa,0x3d,0x53, diff --git a/skipjack.h b/skipjack.h index 1e214c26..c2142ef5 100644 --- a/skipjack.h +++ b/skipjack.h @@ -23,6 +23,8 @@ struct SKIPJACK_Info : public FixedBlockSize<8>, public FixedKeyLength<10> //! \sa SKIPJACK class SKIPJACK : public SKIPJACK_Info, public BlockCipherDocumentation { + //! \class Base + //! \brief SKIPJACK block cipher default operation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl { public: @@ -35,6 +37,8 @@ class SKIPJACK : public SKIPJACK_Info, public BlockCipherDocumentation FixedSizeSecBlock tab; }; + //! \class Enc + //! \brief SKIPJACK block cipher encryption operation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Enc : public Base { public: @@ -44,6 +48,8 @@ class SKIPJACK : public SKIPJACK_Info, public BlockCipherDocumentation static const word32 Te[4][256]; }; + //! \class Dec + //! \brief SKIPJACK block cipher decryption operation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Dec : public Base { public: diff --git a/square.cpp b/square.cpp index c976cb38..b868200a 100644 --- a/square.cpp +++ b/square.cpp @@ -18,14 +18,17 @@ NAMESPACE_BEGIN(CryptoPP) +// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +static const size_t s_unused = Square::KEYLENGTH; + // apply theta to a roundkey static void SquareTransform (word32 in[4], word32 out[4]) { - static const byte G[4][4] = + static const byte G[4][4] = { - 0x02U, 0x01U, 0x01U, 0x03U, - 0x03U, 0x02U, 0x01U, 0x01U, - 0x01U, 0x03U, 0x02U, 0x01U, + 0x02U, 0x01U, 0x01U, 0x03U, + 0x03U, 0x02U, 0x01U, 0x01U, + 0x01U, 0x03U, 0x02U, 0x01U, 0x01U, 0x01U, 0x03U, 0x02U }; @@ -62,7 +65,7 @@ void Square::Base::UncheckedSetKey(const byte *userKey, unsigned int length, con roundkeys(i, 1) = roundkeys(i-1, 1) ^ roundkeys(i, 0); roundkeys(i, 2) = roundkeys(i-1, 2) ^ roundkeys(i, 1); roundkeys(i, 3) = roundkeys(i-1, 3) ^ roundkeys(i, 2); - } + } /* produce the round keys */ if (IsForwardTransformation()) @@ -138,13 +141,13 @@ void Square::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, { word32 text[4], temp[4]; Block::Get(inBlock)(text[0])(text[1])(text[2])(text[3]); - + /* initial key addition */ text[0] ^= roundkeys(0, 0); text[1] ^= roundkeys(0, 1); text[2] ^= roundkeys(0, 2); text[3] ^= roundkeys(0, 3); - + /* ROUNDS - 1 full rounds */ for (int i=1; i+1 Block; @@ -24,7 +27,7 @@ void TEA::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byt word32 sum = 0; while (sum != m_limit) - { + { sum += DELTA; y += ((z << 4) + m_k[0]) ^ (z + sum) ^ ((z >> 5) + m_k[1]); z += ((y << 4) + m_k[2]) ^ (y + sum) ^ ((y >> 5) + m_k[3]); @@ -41,7 +44,7 @@ void TEA::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byt word32 sum = m_limit; while (sum != 0) { - z -= ((y << 4) + m_k[2]) ^ (y + sum) ^ ((y >> 5) + m_k[3]); + z -= ((y << 4) + m_k[2]) ^ (y + sum) ^ ((y >> 5) + m_k[3]); y -= ((z << 4) + m_k[0]) ^ (z + sum) ^ ((z >> 5) + m_k[1]); sum -= DELTA; } @@ -70,7 +73,7 @@ void XTEA::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, by word32 sum = 0; while (sum != m_limit) #endif - { + { y += ((z<<4 ^ z>>5) + z) ^ (sum + m_k[sum&3]); sum += DELTA; z += ((y<<4 ^ y>>5) + y) ^ (sum + m_k[sum>>11 & 3]); @@ -116,9 +119,9 @@ void BTEA::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, by word32 y = v[0], z = v[n-1], e; word32 p, q = 6+52/n; word32 sum = 0; - + while (q-- > 0) - { + { sum += DELTA; e = sum>>2 & 3; for (p = 0; p < n-1; p++) @@ -148,7 +151,7 @@ void BTEA::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, by word32 sum = q * DELTA; while (sum != 0) - { + { e = sum>>2 & 3; for (p = n-1; p > 0; p--) { diff --git a/tea.h b/tea.h index 6c933b7e..e1d0da87 100644 --- a/tea.h +++ b/tea.h @@ -24,6 +24,8 @@ struct TEA_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public Va //! \sa TEA class TEA : public TEA_Info, public BlockCipherDocumentation { + //! \class Base + //! \brief TEA block cipher default operation class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl { public: @@ -34,12 +36,16 @@ class TEA : public TEA_Info, public BlockCipherDocumentation word32 m_limit; }; + //! \class Enc + //! \brief TEA block cipher encryption operation class CRYPTOPP_NO_VTABLE Enc : public Base { public: void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; }; + //! \class Dec + //! \brief TEA block cipher decryption operation class CRYPTOPP_NO_VTABLE Dec : public Base { public: @@ -66,6 +72,8 @@ struct XTEA_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public V //! \sa XTEA class XTEA : public XTEA_Info, public BlockCipherDocumentation { + //! \class Base + //! \brief XTEA block cipher default operation class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl { public: @@ -76,12 +84,16 @@ class XTEA : public XTEA_Info, public BlockCipherDocumentation word32 m_limit; }; + //! \class Enc + //! \brief XTEA block cipher encryption operation class CRYPTOPP_NO_VTABLE Enc : public Base { public: void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; }; + //! \class Dec + //! \brief XTEA block cipher decryption operation class CRYPTOPP_NO_VTABLE Dec : public Base { public: @@ -106,6 +118,8 @@ struct BTEA_Info : public FixedKeyLength<16> //! \sa Corrected Block TEA. class BTEA : public BTEA_Info, public BlockCipherDocumentation { + //! \class Base + //! \brief BTEA block cipher default operation class CRYPTOPP_NO_VTABLE Base : public AlgorithmImpl, BTEA_Info>, public BTEA_Info { public: @@ -123,12 +137,16 @@ class BTEA : public BTEA_Info, public BlockCipherDocumentation unsigned int m_blockSize; }; + //! \class Enc + //! \brief BTEA block cipher encryption operation class CRYPTOPP_NO_VTABLE Enc : public Base { public: void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; }; + //! \class Dec + //! \brief BTEA block cipher decryption operation class CRYPTOPP_NO_VTABLE Dec : public Base { public: diff --git a/ttmac.cpp b/ttmac.cpp index 98954370..b7513e8b 100644 --- a/ttmac.cpp +++ b/ttmac.cpp @@ -6,6 +6,9 @@ NAMESPACE_BEGIN(CryptoPP) +// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +static const size_t s_unused = TTMAC::KEYLENGTH; + void TTMAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &) { AssertValidKeyLength(keylength); diff --git a/ttmac.h b/ttmac.h index 206ee293..47b32ca2 100644 --- a/ttmac.h +++ b/ttmac.h @@ -1,5 +1,8 @@ // ttmac.h - written and placed in the public domain by Kevin Springle +//! \file ttmac.h +//! \brief Classes for the TTMAC message authentication code + #ifndef CRYPTOPP_TTMAC_H #define CRYPTOPP_TTMAC_H @@ -9,7 +12,8 @@ NAMESPACE_BEGIN(CryptoPP) -//! _ +//! \class TTMAC_Base +//! \brief TTMAC message authentication code information class CRYPTOPP_NO_VTABLE TTMAC_Base : public FixedKeyLength<20>, public IteratedHash { public: @@ -30,8 +34,11 @@ protected: FixedSizeSecBlock m_key; }; -//! Two-Track-MAC -/*! 160 Bit MAC with 160 Bit Key */ +//! \class TTMAC +//! \brief Two-Track-MAC message authentication code +//! \tparam T HashTransformation class +//! \details 160-bit MAC with 160-bit key +//! \sa MessageAuthenticationCode(), Two-Track-MAC DOCUMENTED_TYPEDEF(MessageAuthenticationCodeFinal, TTMAC) NAMESPACE_END diff --git a/wake.cpp b/wake.cpp index 4a9bc340..4725947f 100644 --- a/wake.cpp +++ b/wake.cpp @@ -15,6 +15,9 @@ void WAKE_TestInstantiations() } #endif +// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +static const size_t s_unused = WAKE_OFB<>::KEYLENGTH; + inline word32 WAKE_Base::M(word32 x, word32 y) { word32 w = x+y; @@ -24,7 +27,7 @@ inline word32 WAKE_Base::M(word32 x, word32 y) void WAKE_Base::GenKey(word32 k0, word32 k1, word32 k2, word32 k3) { // this code is mostly copied from David Wheeler's paper "A Bulk Data Encryption Algorithm" - signed int x, z, p; + signed int x, z, p; // x and z were declared as "long" in Wheeler's paper, which is a signed type. I don't know if that was intentional, but it's too late to change it now. -- Wei 7/4/2010 CRYPTOPP_COMPILE_ASSERT(sizeof(x) == 4); static unsigned int tt[10]= { diff --git a/wake.h b/wake.h index 28c00e02..9629e9f9 100644 --- a/wake.h +++ b/wake.h @@ -12,7 +12,9 @@ NAMESPACE_BEGIN(CryptoPP) -//! _ +//! \class WAKE_OFB_Info +//! \brief WAKE stream cipher information +//! \tparam B Endianess of the stream cipher template struct WAKE_OFB_Info : public FixedKeyLength<32> { @@ -29,6 +31,9 @@ protected: word32 r3, r4, r5, r6; }; +//! \class WAKE_Policy +//! \brief WAKE stream cipher operation +//! \tparam B Endianess of the stream cipher template class CRYPTOPP_NO_VTABLE WAKE_Policy : public AdditiveCipherConcretePolicy, protected WAKE_Base { @@ -39,7 +44,9 @@ protected: bool CipherIsRandomAccess() const {return false;} }; -//! WAKE-OFB +//! \class WAKE_OFB +//! \brief WAKE stream cipher +//! \tparam B Endianess of the stream cipher template struct WAKE_OFB : public WAKE_OFB_Info, public SymmetricCipherDocumentation { From a62aee441fabfda28d581cee45d8ad0e6e812342 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Tue, 6 Sep 2016 04:04:03 -0400 Subject: [PATCH 17/28] Backed out use of "static const" to declare constant; switch to "enum" (Issue 255) --- 3way.cpp | 5 ----- cast.h | 10 ++++++++++ config.h | 5 +++-- config.recommend | 5 +++-- des.cpp | 6 ------ gost.cpp | 3 --- idea.cpp | 3 --- modes.cpp | 4 ++-- modes.h | 44 +++++++++++++++++++++++++++++++++++--------- panama.cpp | 3 --- safer.h | 10 ++++++++-- salsa.cpp | 4 ---- seal.cpp | 3 --- seed.cpp | 3 --- shark.cpp | 3 --- skipjack.cpp | 3 --- skipjack.h | 2 +- square.cpp | 3 --- tea.cpp | 3 --- ttmac.cpp | 3 --- wake.cpp | 3 --- 21 files changed, 62 insertions(+), 66 deletions(-) diff --git a/3way.cpp b/3way.cpp index c6784ee8..cd0d8a7b 100644 --- a/3way.cpp +++ b/3way.cpp @@ -15,11 +15,6 @@ void ThreeWay_TestInstantiations() } #endif -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -static const size_t s_unused1 = ThreeWay::KEYLENGTH; -static const size_t s_unused2 = ThreeWayEncryption::KEYLENGTH; -static const size_t s_unused3 = ThreeWayDecryption::KEYLENGTH; - static const word32 START_E = 0x0b0b; // round constant of first encryption round static const word32 START_D = 0xb1b1; // round constant of first decryption round #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 diff --git a/cast.h b/cast.h index 63f1e763..52ae53c9 100644 --- a/cast.h +++ b/cast.h @@ -11,6 +11,8 @@ NAMESPACE_BEGIN(CryptoPP) +//! \class CAST +//! \brief CAST block cipher base class CAST { protected: @@ -29,6 +31,8 @@ struct CAST128_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 5, //! \sa CAST-128 class CAST128 : public CAST128_Info, public BlockCipherDocumentation { + //! \class Base + //! \brief CAST128 block cipher default operation class CRYPTOPP_NO_VTABLE Base : public CAST, public BlockCipherImpl { public: @@ -39,12 +43,16 @@ class CAST128 : public CAST128_Info, public BlockCipherDocumentation FixedSizeSecBlock K; }; + //! \class Enc + //! \brief CAST128 block cipher encryption operation class CRYPTOPP_NO_VTABLE Enc : public Base { public: void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; }; + //! \class Dec + //! \brief CAST128 block cipher decryption operation class CRYPTOPP_NO_VTABLE Dec : public Base { public: @@ -68,6 +76,8 @@ struct CAST256_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16 //! \sa CAST-256 class CAST256 : public CAST256_Info, public BlockCipherDocumentation { + //! \class Base + //! \brief CAST256 block cipher default operation class CRYPTOPP_NO_VTABLE Base : public CAST, public BlockCipherImpl { public: diff --git a/config.h b/config.h index 332bcc43..a25d1430 100644 --- a/config.h +++ b/config.h @@ -544,8 +544,9 @@ NAMESPACE_END # define CRYPTOPP_NOINLINE #endif -// how to declare class constants -#if (defined(_MSC_VER) && _MSC_VER <= 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) +// How to declare class constants +// Use enum for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +#if (defined(_MSC_VER) && _MSC_VER <= 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) || (defined(__APPLE__) && (__GNUC__ == 4) && (__GNUC_MINOR__ <= 2)) # define CRYPTOPP_CONSTANT(x) enum {x}; #else # define CRYPTOPP_CONSTANT(x) static const int x; diff --git a/config.recommend b/config.recommend index ff04befe..c9a1c9db 100644 --- a/config.recommend +++ b/config.recommend @@ -544,8 +544,9 @@ NAMESPACE_END # define CRYPTOPP_NOINLINE #endif -// how to declare class constants -#if (defined(_MSC_VER) && _MSC_VER <= 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) +// How to declare class constants +// Use enum for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 +#if (defined(_MSC_VER) && _MSC_VER <= 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) || (defined(__APPLE__) && (__GNUC__ == 4) && (__GNUC_MINOR__ <= 2)) # define CRYPTOPP_CONSTANT(x) enum {x}; #else # define CRYPTOPP_CONSTANT(x) static const int x; diff --git a/des.cpp b/des.cpp index c468403c..6e9a393a 100644 --- a/des.cpp +++ b/des.cpp @@ -20,12 +20,6 @@ NAMESPACE_BEGIN(CryptoPP) -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -static const size_t s_unused1 = DES::KEYLENGTH; -static const size_t s_unused2 = DES_EDE2::KEYLENGTH; -static const size_t s_unused3 = DES_EDE3::KEYLENGTH; -static const size_t s_unused4 = DES_XEX3::KEYLENGTH; - typedef BlockGetAndPut Block; // Richard Outerbridge's initial permutation algorithm diff --git a/gost.cpp b/gost.cpp index f60cf031..53de81a8 100644 --- a/gost.cpp +++ b/gost.cpp @@ -4,9 +4,6 @@ NAMESPACE_BEGIN(CryptoPP) -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -static const size_t s_unused = GOST::KEYLENGTH; - // these are the S-boxes given in Applied Cryptography 2nd Ed., p. 333 const byte GOST::Base::sBox[8][16]={ {4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3}, diff --git a/idea.cpp b/idea.cpp index 96f946af..d0b8836f 100644 --- a/idea.cpp +++ b/idea.cpp @@ -7,9 +7,6 @@ NAMESPACE_BEGIN(CryptoPP) -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -static const size_t s_unused = IDEA::KEYLENGTH; - static const int IDEA_KEYLEN=(6*IDEA::ROUNDS+4); // key schedule length in # of word16s #define low16(x) ((x)&0xffff) // compiler should be able to optimize this away if word is 16 bits diff --git a/modes.cpp b/modes.cpp index 23f6afb1..ed2b3224 100644 --- a/modes.cpp +++ b/modes.cpp @@ -7,9 +7,9 @@ #include "modes.h" #include "misc.h" -#ifndef NDEBUG +//#ifndef NDEBUG #include "des.h" -#endif +//#endif NAMESPACE_BEGIN(CryptoPP) diff --git a/modes.h b/modes.h index df773116..202210c7 100644 --- a/modes.h +++ b/modes.h @@ -16,8 +16,8 @@ NAMESPACE_BEGIN(CryptoPP) //! \class CipherModeDocumentation -//! \brief Classes for operating block cipher modes of operation -//! \details Each class derived from this one defines two types, Encryption and Decryption, +//! \brief Block cipher mode of operation information +//! \details Each class derived from this one defines two types, Encryption and Decryption, //! both of which implement the SymmetricCipher interface. //! For each mode there are two classes, one of which is a template class, //! and the other one has a name that ends in "_ExternalCipher". @@ -31,6 +31,8 @@ struct CipherModeDocumentation : public SymmetricCipherDocumentation { }; +//! \class CipherModeBase +//! \brief Block cipher mode of operation information class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CipherModeBase : public SymmetricCipher { public: @@ -70,7 +72,7 @@ protected: if (!(feedbackSize == 0 || feedbackSize == BlockSize())) throw InvalidArgument("CipherModeBase: feedback size cannot be specified for this cipher mode"); } - + // Thanks to Zireael, http://github.com/weidai11/cryptopp/pull/46 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual void ResizeBuffers(); @@ -85,6 +87,9 @@ protected: AlignedSecByteBlock m_register; }; +//! \class ModePolicyCommonTemplate +//! \brief Block cipher mode of operation common operations +//! \tparam POLICY_INTERFACE common operations template class CRYPTOPP_NO_VTABLE ModePolicyCommonTemplate : public CipherModeBase, public POLICY_INTERFACE { @@ -101,6 +106,8 @@ void ModePolicyCommonTemplate::CipherSetKey(const NameValuePai SetFeedbackSize(feedbackSize); } +//! \class CFB_ModePolicy +//! \brief CFB block cipher mode of operation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CFB_ModePolicy : public ModePolicyCommonTemplate { public: @@ -129,6 +136,8 @@ inline void CopyOrZero(void *dest, const void *src, size_t s) memset(dest, 0, s); } +//! \class OFB_ModePolicy +//! \brief OFB block cipher mode of operation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE OFB_ModePolicy : public ModePolicyCommonTemplate { public: @@ -143,6 +152,8 @@ private: void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length); }; +//! \class CTR_ModePolicy +//! \brief CTR block cipher mode of operation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CTR_ModePolicy : public ModePolicyCommonTemplate { public: @@ -166,6 +177,8 @@ protected: AlignedSecByteBlock m_counterArray; }; +//! \class BlockOrientedCipherModeBase +//! \brief Block cipher mode of operation default implementation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE BlockOrientedCipherModeBase : public CipherModeBase { public: @@ -178,7 +191,7 @@ public: protected: bool RequireAlignedInput() const {return true;} - + // Thanks to Zireael, http://github.com/weidai11/cryptopp/pull/46 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 void ResizeBuffers(); @@ -193,6 +206,8 @@ protected: SecByteBlock m_buffer; }; +//! \class ECB_OneWay +//! \brief ECB block cipher mode of operation default implementation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE ECB_OneWay : public BlockOrientedCipherModeBase { public: @@ -204,6 +219,8 @@ public: static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECB";} }; +//! \class CBC_ModeBase +//! \brief CBC block cipher mode of operation default implementation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_ModeBase : public BlockOrientedCipherModeBase { public: @@ -213,12 +230,16 @@ public: static const char * CRYPTOPP_API StaticAlgorithmName() {return "CBC";} }; +//! \class CBC_Encryption +//! \brief CBC block cipher mode of operation encryption operation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Encryption : public CBC_ModeBase { public: void ProcessData(byte *outString, const byte *inString, size_t length); }; +//! \class CBC_CTS_Encryption +//! \brief CBC-CTS block cipher mode of operation encryption operation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Encryption : public CBC_Encryption { public: @@ -237,13 +258,15 @@ protected: byte *m_stolenIV; }; +//! \class CBC_Decryption +//! \brief CBC block cipher mode of operation decryption operation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Decryption : public CBC_ModeBase { public: void ProcessData(byte *outString, const byte *inString, size_t length); - + protected: - + // Thanks to Zireael, http://github.com/weidai11/cryptopp/pull/46 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 void ResizeBuffers(); @@ -258,6 +281,8 @@ protected: AlignedSecByteBlock m_temp; }; +//! \class CBC_CTS_Decryption +//! \brief CBC-CTS block cipher mode of operation decryption operation class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Decryption : public CBC_Decryption { public: @@ -265,7 +290,8 @@ public: void ProcessLastBlock(byte *outString, const byte *inString, size_t length); }; -//! _ +//! \class CipherModeFinalTemplate_CipherHolder +//! \brief Block cipher mode of operation aggregate template class CipherModeFinalTemplate_CipherHolder : protected ObjectHolder, public AlgorithmImpl > { @@ -296,8 +322,8 @@ public: }; //! \class CipherModeFinalTemplate_ExternalCipher -//! \tparam BASE CipherModeFinalTemplate_CipherHolder class -//! \brief OFB block cipher mode of operation. +//! \tparam BASE CipherModeFinalTemplate_CipherHolder base class +//! \details template class CipherModeFinalTemplate_ExternalCipher : public BASE { diff --git a/panama.cpp b/panama.cpp index 2c76c934..f10edbdc 100644 --- a/panama.cpp +++ b/panama.cpp @@ -17,9 +17,6 @@ NAMESPACE_BEGIN(CryptoPP) # pragma warning(disable: 4731) #endif -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -static const size_t s_unused = PanamaCipher<>::KEYLENGTH; - template void Panama::Reset() { diff --git a/safer.h b/safer.h index 3828d8e3..a9150284 100644 --- a/safer.h +++ b/safer.h @@ -1,7 +1,7 @@ // safer.h - written and placed in the public domain by Wei Dai //! \file safer.h -//! \brief Classes for the SAFER block cipher +//! \brief Classes for the SAFER and SAFER-K block ciphers #ifndef CRYPTOPP_SAFER_H #define CRYPTOPP_SAFER_H @@ -12,10 +12,12 @@ NAMESPACE_BEGIN(CryptoPP) //! \class SAFER -//! \brief SAFER base class +//! \brief SAFER block cipher class SAFER { public: + //! \class Base + //! \brief SAFER block cipher default operation class CRYPTOPP_NO_VTABLE Base : public BlockCipher { public: @@ -30,12 +32,16 @@ public: static const byte log_tab[256]; }; + //! \class Enc + //! \brief SAFER block cipher encryption operation class CRYPTOPP_NO_VTABLE Enc : public Base { public: void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; }; + //! \class Dec + //! \brief SAFER block cipher decryption operation class CRYPTOPP_NO_VTABLE Dec : public Base { public: diff --git a/salsa.cpp b/salsa.cpp index 603ae521..ddf779ae 100644 --- a/salsa.cpp +++ b/salsa.cpp @@ -40,10 +40,6 @@ void Salsa20_TestInstantiations() } #endif -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -// static const size_t s_unused1 = Salsa20::KEYLENGTH; -static const size_t s_unused2 = XSalsa20::KEYLENGTH; - void Salsa20_Policy::CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length) { m_rounds = params.GetIntValueWithDefault(Name::Rounds(), 20); diff --git a/seal.cpp b/seal.cpp index 641a3207..9babbfa1 100644 --- a/seal.cpp +++ b/seal.cpp @@ -17,9 +17,6 @@ void SEAL_TestInstantiations() } #endif -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -static const size_t s_unused = SEAL<>::KEYLENGTH; - struct SEAL_Gamma { SEAL_Gamma(const byte *key) diff --git a/seed.cpp b/seed.cpp index 58905fe3..f6a9690f 100644 --- a/seed.cpp +++ b/seed.cpp @@ -6,9 +6,6 @@ NAMESPACE_BEGIN(CryptoPP) -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -static const size_t s_unused = SEED::KEYLENGTH; - static const word32 s_kc[16] = { 0x9e3779b9, 0x3c6ef373, 0x78dde6e6, 0xf1bbcdcc, 0xe3779b99, 0xc6ef3733, 0x8dde6e67, 0x1bbcdccf, 0x3779b99e, 0x6ef3733c, 0xdde6e678, 0xbbcdccf1, 0x779b99e3, 0xef3733c6, 0xde6e678d, 0xbcdccf1b}; diff --git a/shark.cpp b/shark.cpp index 503d00b6..99d63d21 100644 --- a/shark.cpp +++ b/shark.cpp @@ -12,9 +12,6 @@ NAMESPACE_BEGIN(CryptoPP) -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -static const size_t s_unused = SHARK::KEYLENGTH; - static word64 SHARKTransform(word64 a) { static const byte iG[8][8] = { diff --git a/skipjack.cpp b/skipjack.cpp index c52baf49..d3f59ff7 100644 --- a/skipjack.cpp +++ b/skipjack.cpp @@ -17,9 +17,6 @@ NAMESPACE_BEGIN(CryptoPP) -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -static const size_t s_unused = SKIPJACK::KEYLENGTH; - /** * The F-table byte permutation (see description of the G-box permutation) */ diff --git a/skipjack.h b/skipjack.h index c2142ef5..3378931b 100644 --- a/skipjack.h +++ b/skipjack.h @@ -19,7 +19,7 @@ struct SKIPJACK_Info : public FixedBlockSize<8>, public FixedKeyLength<10> }; //! \class SKIPJACK -//! \brief SKIPJACK block cipher information +//! \brief SKIPJACK block cipher //! \sa SKIPJACK class SKIPJACK : public SKIPJACK_Info, public BlockCipherDocumentation { diff --git a/square.cpp b/square.cpp index b868200a..b0912900 100644 --- a/square.cpp +++ b/square.cpp @@ -18,9 +18,6 @@ NAMESPACE_BEGIN(CryptoPP) -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -static const size_t s_unused = Square::KEYLENGTH; - // apply theta to a roundkey static void SquareTransform (word32 in[4], word32 out[4]) { diff --git a/tea.cpp b/tea.cpp index f1a82e8d..dba159f6 100644 --- a/tea.cpp +++ b/tea.cpp @@ -6,9 +6,6 @@ NAMESPACE_BEGIN(CryptoPP) -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -static const size_t s_unused = TEA::KEYLENGTH; - static const word32 DELTA = 0x9e3779b9; typedef BlockGetAndPut Block; diff --git a/ttmac.cpp b/ttmac.cpp index b7513e8b..98954370 100644 --- a/ttmac.cpp +++ b/ttmac.cpp @@ -6,9 +6,6 @@ NAMESPACE_BEGIN(CryptoPP) -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -static const size_t s_unused = TTMAC::KEYLENGTH; - void TTMAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &) { AssertValidKeyLength(keylength); diff --git a/wake.cpp b/wake.cpp index 4725947f..888b2e09 100644 --- a/wake.cpp +++ b/wake.cpp @@ -15,9 +15,6 @@ void WAKE_TestInstantiations() } #endif -// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -static const size_t s_unused = WAKE_OFB<>::KEYLENGTH; - inline word32 WAKE_Base::M(word32 x, word32 y) { word32 w = x+y; From 2a51576adc665121921b56e011ae437cb0d69cfa Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Tue, 6 Sep 2016 05:03:17 -0400 Subject: [PATCH 18/28] Updated documentation --- safer.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/safer.h b/safer.h index a9150284..0a6cb064 100644 --- a/safer.h +++ b/safer.h @@ -49,6 +49,12 @@ public: }; }; +//! \class SAFER_Impl +//! \brief SAFER block cipher default implementation +//! \tparam BASE SAFER::Enc or SAFER::Dec derived base class +//! \tparam INFO SAFER_Info derived class +//! \tparam STR flag indicating a strengthened implementation +//! \details SAFER-K is not strengthened; while SAFER-SK is strengthened. template class CRYPTOPP_NO_VTABLE SAFER_Impl : public BlockCipherImpl { From 9205efda02a788084a332bff06733572a338b132 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Tue, 6 Sep 2016 08:53:55 -0400 Subject: [PATCH 19/28] Add constexpr to CRYPTOPP_CONSTANT when CRYPTOPP_CXX11_CONSTEXPR is in effect --- config.h | 6 ++++++ config.recommend | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/config.h b/config.h index a25d1430..ab00dc7a 100644 --- a/config.h +++ b/config.h @@ -902,6 +902,12 @@ NAMESPACE_END # define CRYPTOPP_ALIGN_DATA(x) alignas(x) #endif // CRYPTOPP_CXX11_ALIGNAS +// Hack... CRYPTOPP_CONSTANT is defined earlier, before C++11 constexpr availability is determined +#if defined(CRYPTOPP_CXX11_CONSTEXPR) +# undef CRYPTOPP_CONSTANT +# define CRYPTOPP_CONSTANT(x) constexpr static int x; +#endif + // OK to comment the following out, but please report it so we can fix it. // C++17 value taken from http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4567.pdf. #if (defined(__cplusplus) && (__cplusplus >= 199711L) && (__cplusplus < 201402L)) && !defined(CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE) diff --git a/config.recommend b/config.recommend index c9a1c9db..61fcb1df 100644 --- a/config.recommend +++ b/config.recommend @@ -902,6 +902,12 @@ NAMESPACE_END # define CRYPTOPP_ALIGN_DATA(x) alignas(x) #endif // CRYPTOPP_CXX11_ALIGNAS +// Hack... CRYPTOPP_CONSTANT is defined earlier, before C++11 constexpr availability is determined +#if defined(CRYPTOPP_CXX11_CONSTEXPR) +# undef CRYPTOPP_CONSTANT +# define CRYPTOPP_CONSTANT(x) constexpr static int x; +#endif + // OK to comment the following out, but please report it so we can fix it. // C++17 value taken from http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4567.pdf. #if (defined(__cplusplus) && (__cplusplus >= 199711L) && (__cplusplus < 201402L)) && !defined(CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE) From 59aad798ebb5302c9b62285c3a9a0d2878cb68e3 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Tue, 6 Sep 2016 08:59:31 -0400 Subject: [PATCH 20/28] Cleared "local variable is initialized but not referenced" under VS2015 --- rw.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rw.cpp b/rw.cpp index 15aa6646..784a0368 100644 --- a/rw.cpp +++ b/rw.cpp @@ -207,7 +207,7 @@ Integer InvertibleRWFunction::CalculateInverse(RandomNumberGenerator &rng, const Integer re = modn.Square(r); re = modn.Multiply(re, x); // blind - const Integer &h = re, &p = m_p, &q = m_q, &n = m_n; + const Integer &h = re, &p = m_p, &q = m_q; Integer e, f; const Integer U = modq.Exponentiate(h, (q+1)/8); From 622e95809862320b1f71afec2f78d17b27df33f8 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Tue, 6 Sep 2016 22:57:46 -0400 Subject: [PATCH 21/28] Cleared "declaration hides other declaration" under VS2015 --- validat1.cpp | 60 ++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/validat1.cpp b/validat1.cpp index 9dd0848f..273bd427 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -2153,33 +2153,33 @@ bool ValidateBlowfish() cout << "\nBlowfish validation suite running...\n\n"; bool pass1 = true, pass2 = true, pass3 = true, fail; - BlowfishEncryption enc; // 32 to 448-bits (4 to 56-bytes) - pass1 = enc.StaticGetValidKeyLength(3) == 4 && pass1; - pass1 = enc.StaticGetValidKeyLength(4) == 4 && pass1; - pass1 = enc.StaticGetValidKeyLength(5) == 5 && pass1; - pass1 = enc.StaticGetValidKeyLength(8) == 8 && pass1; - pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1; - pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1; - pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1; - pass1 = enc.StaticGetValidKeyLength(56) == 56 && pass1; - pass1 = enc.StaticGetValidKeyLength(57) == 56 && pass1; - pass1 = enc.StaticGetValidKeyLength(60) == 56 && pass1; - pass1 = enc.StaticGetValidKeyLength(64) == 56 && pass1; - pass1 = enc.StaticGetValidKeyLength(128) == 56 && pass1; + BlowfishEncryption enc1; // 32 to 448-bits (4 to 56-bytes) + pass1 = enc1.StaticGetValidKeyLength(3) == 4 && pass1; + pass1 = enc1.StaticGetValidKeyLength(4) == 4 && pass1; + pass1 = enc1.StaticGetValidKeyLength(5) == 5 && pass1; + pass1 = enc1.StaticGetValidKeyLength(8) == 8 && pass1; + pass1 = enc1.StaticGetValidKeyLength(16) == 16 && pass1; + pass1 = enc1.StaticGetValidKeyLength(24) == 24 && pass1; + pass1 = enc1.StaticGetValidKeyLength(32) == 32 && pass1; + pass1 = enc1.StaticGetValidKeyLength(56) == 56 && pass1; + pass1 = enc1.StaticGetValidKeyLength(57) == 56 && pass1; + pass1 = enc1.StaticGetValidKeyLength(60) == 56 && pass1; + pass1 = enc1.StaticGetValidKeyLength(64) == 56 && pass1; + pass1 = enc1.StaticGetValidKeyLength(128) == 56 && pass1; - BlowfishDecryption dec; // 32 to 448-bits (4 to 56-bytes) - pass2 = dec.StaticGetValidKeyLength(3) == 4 && pass2; - pass2 = dec.StaticGetValidKeyLength(4) == 4 && pass2; - pass2 = dec.StaticGetValidKeyLength(5) == 5 && pass2; - pass2 = dec.StaticGetValidKeyLength(8) == 8 && pass2; - pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2; - pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2; - pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2; - pass2 = dec.StaticGetValidKeyLength(56) == 56 && pass2; - pass2 = dec.StaticGetValidKeyLength(57) == 56 && pass2; - pass2 = dec.StaticGetValidKeyLength(60) == 56 && pass2; - pass2 = dec.StaticGetValidKeyLength(64) == 56 && pass2; - pass2 = dec.StaticGetValidKeyLength(128) == 56 && pass2; + BlowfishDecryption dec1; // 32 to 448-bits (4 to 56-bytes) + pass2 = dec1.StaticGetValidKeyLength(3) == 4 && pass2; + pass2 = dec1.StaticGetValidKeyLength(4) == 4 && pass2; + pass2 = dec1.StaticGetValidKeyLength(5) == 5 && pass2; + pass2 = dec1.StaticGetValidKeyLength(8) == 8 && pass2; + pass2 = dec1.StaticGetValidKeyLength(16) == 16 && pass2; + pass2 = dec1.StaticGetValidKeyLength(24) == 24 && pass2; + pass2 = dec1.StaticGetValidKeyLength(32) == 32 && pass2; + pass2 = dec1.StaticGetValidKeyLength(56) == 56 && pass2; + pass2 = dec1.StaticGetValidKeyLength(57) == 56 && pass2; + pass2 = dec1.StaticGetValidKeyLength(60) == 56 && pass2; + pass2 = dec1.StaticGetValidKeyLength(64) == 56 && pass2; + pass2 = dec1.StaticGetValidKeyLength(128) == 56 && pass2; cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n"; HexEncoder output(new FileSink(cout)); @@ -2190,12 +2190,12 @@ bool ValidateBlowfish() for (int i=0; i<2; i++) { - ECB_Mode::Encryption enc((byte *)key[i], strlen(key[i])); - enc.ProcessData(out, plain[i], 8); + ECB_Mode::Encryption enc2((byte *)key[i], strlen(key[i])); + enc2.ProcessData(out, plain[i], 8); fail = memcmp(out, cipher[i], 8) != 0; - ECB_Mode::Decryption dec((byte *)key[i], strlen(key[i])); - dec.ProcessData(outplain, cipher[i], 8); + ECB_Mode::Decryption dec2((byte *)key[i], strlen(key[i])); + dec2.ProcessData(outplain, cipher[i], 8); fail = fail || memcmp(outplain, plain[i], 8); pass3 = pass3 && !fail; From cdf659a277937771f75fe6e9848af1c98d4e7f11 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Tue, 6 Sep 2016 23:04:42 -0400 Subject: [PATCH 22/28] Const-ify input, ky and iv --- validat1.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/validat1.cpp b/validat1.cpp index 273bd427..fbe097ce 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -2384,10 +2384,10 @@ bool ValidateSKIPJACK() bool ValidateSEAL() { - byte input[] = {0x37,0xa0,0x05,0x95,0x9b,0x84,0xc4,0x9c,0xa4,0xbe,0x1e,0x05,0x06,0x73,0x53,0x0f,0x5f,0xb0,0x97,0xfd,0xf6,0xa1,0x3f,0xbd,0x6c,0x2c,0xde,0xcd,0x81,0xfd,0xee,0x7c}; + static const byte input[] = {0x37,0xa0,0x05,0x95,0x9b,0x84,0xc4,0x9c,0xa4,0xbe,0x1e,0x05,0x06,0x73,0x53,0x0f,0x5f,0xb0,0x97,0xfd,0xf6,0xa1,0x3f,0xbd,0x6c,0x2c,0xde,0xcd,0x81,0xfd,0xee,0x7c}; + static const byte key[] = {0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x89, 0x98, 0xba, 0xdc, 0xfe, 0x10, 0x32, 0x54, 0x76, 0xc3, 0xd2, 0xe1, 0xf0}; + static const byte iv[] = {0x01, 0x35, 0x77, 0xaf}; byte output[32]; - byte key[] = {0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x89, 0x98, 0xba, 0xdc, 0xfe, 0x10, 0x32, 0x54, 0x76, 0xc3, 0xd2, 0xe1, 0xf0}; - byte iv[] = {0x01, 0x35, 0x77, 0xaf}; cout << "\nSEAL validation suite running...\n\n"; From c1556295e60e435883c6976aeaf1a2be6606c738 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Wed, 7 Sep 2016 06:16:46 -0400 Subject: [PATCH 23/28] Add constexpr-ness to StaticAlgorithmName member function --- 3way.h | 2 +- adler32.h | 4 ++-- arc4.h | 6 +++--- blake2.h | 4 ++-- blowfish.h | 2 +- camellia.h | 2 +- cast.h | 4 ++-- crc.h | 4 ++-- des.h | 4 ++-- eccrypto.h | 4 ++-- elgamal.h | 8 ++++---- emsa2.h | 2 +- esign.h | 6 +++--- gfpcrypt.h | 4 ++-- gost.h | 2 +- idea.h | 2 +- luc.h | 14 +++++++------- mars.h | 2 +- md2.h | 2 +- md4.h | 2 +- md5.h | 2 +- mersenne.h | 34 +++++++++++++++++----------------- modes.h | 12 ++++++------ panama.h | 4 ++-- pkcspad.h | 6 +++--- pubkey.h | 4 ++-- rc2.h | 2 +- rc5.h | 2 +- rc6.h | 2 +- ripemd.h | 8 ++++---- rsa.h | 4 ++-- safer.h | 4 ++-- salsa.h | 4 ++-- seal.h | 2 +- seed.h | 2 +- serpent.h | 2 +- sha.h | 10 +++++----- sha3.h | 8 ++++---- shacal2.h | 2 +- shark.h | 2 +- sosemanuk.h | 2 +- square.h | 2 +- tea.h | 6 +++--- tiger.h | 2 +- twofish.h | 2 +- wake.h | 2 +- whrlpool.h | 2 +- 47 files changed, 107 insertions(+), 107 deletions(-) diff --git a/3way.h b/3way.h index 16affa6c..19068350 100644 --- a/3way.h +++ b/3way.h @@ -16,7 +16,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief ThreeWay block cipher information struct ThreeWay_Info : public FixedBlockSize<12>, public FixedKeyLength<12>, public VariableRounds<11> { - static const char *StaticAlgorithmName() {return "3-Way";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "3-Way";} }; //! \class ThreeWay diff --git a/adler32.h b/adler32.h index e8caa897..3224ad99 100644 --- a/adler32.h +++ b/adler32.h @@ -11,7 +11,7 @@ NAMESPACE_BEGIN(CryptoPP) -//! ADLER-32 checksum calculations +//! ADLER-32 checksum calculations class Adler32 : public HashTransformation { public: @@ -20,7 +20,7 @@ public: void Update(const byte *input, size_t length); void TruncatedFinal(byte *hash, size_t size); unsigned int DigestSize() const {return DIGESTSIZE;} - static const char * StaticAlgorithmName() {return "Adler32";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Adler32";} std::string AlgorithmName() const {return StaticAlgorithmName();} private: diff --git a/arc4.h b/arc4.h index 2b46af02..6c02524c 100644 --- a/arc4.h +++ b/arc4.h @@ -23,13 +23,13 @@ class CRYPTOPP_NO_VTABLE ARC4_Base : public VariableKeyLength<16, 1, 256>, publi public: ~ARC4_Base(); - static const char *StaticAlgorithmName() {return "ARC4";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "ARC4";} void GenerateBlock(byte *output, size_t size); void DiscardBytes(size_t n); void ProcessData(byte *outString, const byte *inString, size_t length); - + bool IsRandomAccess() const {return false;} bool IsSelfInverting() const {return true;} bool IsForwardTransformation() const {return true;} @@ -55,7 +55,7 @@ DOCUMENTED_TYPEDEF(SymmetricCipherFinal, ARC4) class CRYPTOPP_NO_VTABLE MARC4_Base : public ARC4_Base { public: - static const char *StaticAlgorithmName() {return "MARC4";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "MARC4";} typedef SymmetricCipherFinal Encryption; typedef SymmetricCipherFinal Decryption; diff --git a/blake2.h b/blake2.h index 0bbee89a..ef7819df 100644 --- a/blake2.h +++ b/blake2.h @@ -39,7 +39,7 @@ struct BLAKE2_Info : public VariableKeyLength<(T_64bit ? 64 : 32),0,(T_64bit ? 6 CRYPTOPP_CONSTANT(SALTSIZE = (T_64bit ? 16 : 8)) CRYPTOPP_CONSTANT(PERSONALIZATIONSIZE = (T_64bit ? 16 : 8)) - static const char *StaticAlgorithmName() {return (T_64bit ? "BLAKE2b" : "BLAKE2s");} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return (T_64bit ? "BLAKE2b" : "BLAKE2s");} }; //! \class BLAKE2_ParameterBlock @@ -175,7 +175,7 @@ public: //! \brief Retrieve the static algorithm name //! \returns the algorithm name (BLAKE2s or BLAKE2b) - static const char *StaticAlgorithmName() {return BLAKE2_Info::StaticAlgorithmName();} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return BLAKE2_Info::StaticAlgorithmName();} //! \brief Retrieve the object's name //! \returns the object's algorithm name following RFC 7693 diff --git a/blowfish.h b/blowfish.h index 41497a66..dc770c9e 100644 --- a/blowfish.h +++ b/blowfish.h @@ -15,7 +15,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief Blowfish block cipher information struct Blowfish_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 4, 56>, public FixedRounds<16> { - static const char *StaticAlgorithmName() {return "Blowfish";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Blowfish";} }; // Blowfish diff --git a/camellia.h b/camellia.h index b6c4535d..9fa9e3b4 100644 --- a/camellia.h +++ b/camellia.h @@ -16,7 +16,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief Camellia block cipher information struct Camellia_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 8> { - static const char *StaticAlgorithmName() {return "Camellia";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Camellia";} }; //! \class Camellia diff --git a/cast.h b/cast.h index 52ae53c9..2e212897 100644 --- a/cast.h +++ b/cast.h @@ -23,7 +23,7 @@ protected: //! \brief CAST128 block cipher information struct CAST128_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 5, 16> { - static const char *StaticAlgorithmName() {return "CAST-128";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "CAST-128";} }; //! \class CAST128 @@ -68,7 +68,7 @@ public: //! \brief CAST256 block cipher information struct CAST256_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 4> { - static const char *StaticAlgorithmName() {return "CAST-256";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "CAST-256";} }; //! \class CAST256 diff --git a/crc.h b/crc.h index 89b68629..44e0e2f7 100644 --- a/crc.h +++ b/crc.h @@ -31,7 +31,7 @@ public: void Update(const byte *input, size_t length); void TruncatedFinal(byte *hash, size_t size); unsigned int DigestSize() const {return DIGESTSIZE;} - static const char * StaticAlgorithmName() {return "CRC32";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "CRC32";} std::string AlgorithmName() const {return StaticAlgorithmName();} void UpdateByte(byte b) {m_crc = m_tab[CRC32_INDEX(m_crc) ^ b] ^ CRC32_SHIFTED(m_crc);} @@ -55,7 +55,7 @@ public: void Update(const byte *input, size_t length); void TruncatedFinal(byte *hash, size_t size); unsigned int DigestSize() const {return DIGESTSIZE;} - static const char * StaticAlgorithmName() {return "CRC32C";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "CRC32C";} std::string AlgorithmName() const {return StaticAlgorithmName();} void UpdateByte(byte b) {m_crc = m_tab[CRC32_INDEX(m_crc) ^ b] ^ CRC32_SHIFTED(m_crc);} diff --git a/des.h b/des.h index 5ada3513..3e686dc0 100644 --- a/des.h +++ b/des.h @@ -30,7 +30,7 @@ protected: struct DES_Info : public FixedBlockSize<8>, public FixedKeyLength<8> { // disable DES in DLL version by not exporting this function - static const char * StaticAlgorithmName() {return "DES";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "DES";} }; //! \class DES @@ -122,7 +122,7 @@ public: //! \brief DESX block cipher information struct DES_XEX3_Info : public FixedBlockSize<8>, public FixedKeyLength<24> { - static const char *StaticAlgorithmName() {return "DES-XEX3";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "DES-XEX3";} }; //! \class DES_XEX3 diff --git a/eccrypto.h b/eccrypto.h index cba037fb..18572b7b 100644 --- a/eccrypto.h +++ b/eccrypto.h @@ -245,7 +245,7 @@ template class DL_Algorithm_ECDSA : public DL_Algorithm_GDSA { public: - static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECDSA";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECDSA";} #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_Algorithm_ECDSA() {} @@ -257,7 +257,7 @@ template class DL_Algorithm_ECNR : public DL_Algorithm_NR { public: - static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECNR";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECNR";} #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_Algorithm_ECNR() {} diff --git a/elgamal.h b/elgamal.h index 4e7ef7f0..93eccd7f 100644 --- a/elgamal.h +++ b/elgamal.h @@ -16,8 +16,8 @@ NAMESPACE_BEGIN(CryptoPP) -class CRYPTOPP_NO_VTABLE ElGamalBase : public DL_KeyAgreementAlgorithm_DH, - public DL_KeyDerivationAlgorithm, +class CRYPTOPP_NO_VTABLE ElGamalBase : public DL_KeyAgreementAlgorithm_DH, + public DL_KeyDerivationAlgorithm, public DL_SymmetricEncryptionAlgorithm { public: @@ -86,7 +86,7 @@ public: } virtual const DL_GroupParameters_GFP & GetGroupParameters() const =0; - + #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~ElGamalBase() {} #endif @@ -127,7 +127,7 @@ struct ElGamal { typedef DL_CryptoSchemeOptions SchemeOptions; - static const char * StaticAlgorithmName() {return "ElgamalEnc/Crypto++Padding";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "ElgamalEnc/Crypto++Padding";} typedef SchemeOptions::GroupParameters GroupParameters; //! implements PK_Encryptor interface diff --git a/emsa2.h b/emsa2.h index 3f3e2c17..7c376e4a 100644 --- a/emsa2.h +++ b/emsa2.h @@ -61,7 +61,7 @@ CRYPTOPP_DLL_TEMPLATE_CLASS EMSA2HashId; class CRYPTOPP_DLL EMSA2Pad : public EMSA2HashIdLookup { public: - static const char * CRYPTOPP_API StaticAlgorithmName() {return "EMSA2";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "EMSA2";} size_t MinRepresentativeBitLength(size_t hashIdentifierLength, size_t digestLength) const {CRYPTOPP_UNUSED(hashIdentifierLength); return 8*digestLength + 31;} diff --git a/esign.h b/esign.h index e9969d3e..a55d6035 100644 --- a/esign.h +++ b/esign.h @@ -90,9 +90,9 @@ template class EMSA5Pad : public PK_DeterministicSignatureMessageEncodingMethod { public: - static const char *StaticAlgorithmName() {return "EMSA5";} - - void ComputeMessageRepresentative(RandomNumberGenerator &rng, + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "EMSA5";} + + void ComputeMessageRepresentative(RandomNumberGenerator &rng, const byte *recoverableMessage, size_t recoverableMessageLength, HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty, byte *representative, size_t representativeBitLength) const diff --git a/gfpcrypt.h b/gfpcrypt.h index 114fe8da..ed37bf4a 100644 --- a/gfpcrypt.h +++ b/gfpcrypt.h @@ -185,7 +185,7 @@ template class DL_Algorithm_GDSA : public DL_ElgamalLikeSignatureAlgorithm { public: - static const char * CRYPTOPP_API StaticAlgorithmName() {return "DSA-1363";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "DSA-1363";} void Sign(const DL_GroupParameters ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const { @@ -221,7 +221,7 @@ template class DL_Algorithm_NR : public DL_ElgamalLikeSignatureAlgorithm { public: - static const char * CRYPTOPP_API StaticAlgorithmName() {return "NR";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "NR";} void Sign(const DL_GroupParameters ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const { diff --git a/gost.h b/gost.h index 116e3710..b2de14af 100644 --- a/gost.h +++ b/gost.h @@ -15,7 +15,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief GOST block cipher information struct GOST_Info : public FixedBlockSize<8>, public FixedKeyLength<32> { - static const char *StaticAlgorithmName() {return "GOST";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "GOST";} }; //! \class GOST diff --git a/idea.h b/idea.h index a2b50673..501308b2 100644 --- a/idea.h +++ b/idea.h @@ -15,7 +15,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief IDEA block cipher information struct IDEA_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public FixedRounds<8> { - static const char *StaticAlgorithmName() {return "IDEA";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "IDEA";} }; //! \class IDEA diff --git a/luc.h b/luc.h index bd04dee5..bac96f2b 100644 --- a/luc.h +++ b/luc.h @@ -205,7 +205,7 @@ public: { return GetValueHelper(this, name, valueType, pValue).Assignable(); } - + #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_GroupParameters_LUC() {} #endif @@ -219,7 +219,7 @@ class DL_GroupParameters_LUC_DefaultSafePrime : public DL_GroupParameters_LUC { public: typedef NoCofactorMultiplication DefaultCofactorOption; - + #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_GroupParameters_LUC_DefaultSafePrime() {} #endif @@ -232,14 +232,14 @@ protected: class DL_Algorithm_LUC_HMP : public DL_ElgamalLikeSignatureAlgorithm { public: - static const char * StaticAlgorithmName() {return "LUC-HMP";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "LUC-HMP";} void Sign(const DL_GroupParameters ¶ms, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const; bool Verify(const DL_GroupParameters ¶ms, const DL_PublicKey &publicKey, const Integer &e, const Integer &r, const Integer &s) const; size_t RLen(const DL_GroupParameters ¶ms) const {return params.GetGroupOrder().ByteCount();} - + #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_Algorithm_LUC_HMP() {} #endif @@ -251,7 +251,7 @@ struct DL_SignatureKeys_LUC typedef DL_GroupParameters_LUC GroupParameters; typedef DL_PublicKey_GFP PublicKey; typedef DL_PrivateKey_GFP PrivateKey; - + #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_SignatureKeys_LUC() {} #endif @@ -269,7 +269,7 @@ struct DL_CryptoKeys_LUC typedef DL_GroupParameters_LUC_DefaultSafePrime GroupParameters; typedef DL_PublicKey_GFP PublicKey; typedef DL_PrivateKey_GFP PrivateKey; - + #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_CryptoKeys_LUC() {} #endif @@ -286,7 +286,7 @@ struct LUC_IES LUC_IES<> > { static std::string StaticAlgorithmName() {return "LUC-IES";} // non-standard name - + #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~LUC_IES() {} #endif diff --git a/mars.h b/mars.h index b8b4e2c3..590f9adf 100644 --- a/mars.h +++ b/mars.h @@ -15,7 +15,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief MARS block cipher information struct MARS_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 8> { - static const char *StaticAlgorithmName() {return "MARS";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "MARS";} }; //! \class MARS diff --git a/md2.h b/md2.h index 760edec0..296c523d 100644 --- a/md2.h +++ b/md2.h @@ -17,7 +17,7 @@ public: void TruncatedFinal(byte *hash, size_t size); unsigned int DigestSize() const {return DIGESTSIZE;} unsigned int BlockSize() const {return BLOCKSIZE;} - static const char * StaticAlgorithmName() {return "MD2";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "MD2";} CRYPTOPP_CONSTANT(DIGESTSIZE = 16) CRYPTOPP_CONSTANT(BLOCKSIZE = 16) diff --git a/md4.h b/md4.h index ac98f946..bf6a4f68 100644 --- a/md4.h +++ b/md4.h @@ -15,7 +15,7 @@ class MD4 : public IteratedHashWithStaticTransformword32 size //! \details If n is not a multiple of word32, then unused bytes are @@ -120,7 +120,7 @@ public: for(size_t i=0; i < RoundUpToMultipleOf(n, 4U); i++) NextMersenneWord(); } - + protected: //! \brief Returns the next 32-bit word from the state array @@ -130,19 +130,19 @@ protected: word32 NextMersenneWord() { if (m_idx >= N) { Twist(); } - + word32 temp = m_state[m_idx++]; temp ^= (temp >> 11); temp ^= (temp << 7) & 0x9D2C5680; // 0x9D2C5680 (2636928640) temp ^= (temp << 15) & 0xEFC60000; // 0xEFC60000 (4022730752) - + return temp ^ (temp >> 18); } //! \brief Performs the twist operaton on the state array void Twist() - { + { static const unsigned long magic[2]={0x0UL, K}; word32 kk, temp; @@ -152,19 +152,19 @@ protected: temp = (m_state[kk] & 0x80000000)|(m_state[kk+1] & 0x7FFFFFFF); m_state[kk] = m_state[kk+M] ^ (temp >> 1) ^ magic[temp & 0x1UL]; } - + for (;kk> 1) ^ magic[temp & 0x1UL]; } - + temp = (m_state[N-1] & 0x80000000)|(m_state[0] & 0x7FFFFFFF); m_state[N-1] = m_state[M-1] ^ (temp >> 1) ^ magic[temp & 0x1UL]; - + // Reset index m_idx = 0; - + // Wipe temp *((volatile word32*)&temp) = 0; } @@ -179,7 +179,7 @@ private: unsigned int m_idx; }; -//! \brief Original MT19937 generator provided in the ACM paper. +//! \brief Original MT19937 generator provided in the ACM paper. //! \details Also see http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/ARTICLES/mt.pdf; uses 4537 as default initial seed. typedef MersenneTwister<0x9908B0DF /*2567483615*/, 397, 624, 0x10DCD /*69069*/, 4537> MT19937; @@ -191,4 +191,4 @@ typedef MersenneTwister<0x9908B0DF /*2567483615*/, 397, 624, 0x6C078965 /*181243 NAMESPACE_END #endif // CRYPTOPP_MERSENNE_TWISTER_H - \ No newline at end of file + diff --git a/modes.h b/modes.h index 202210c7..01ea4bfe 100644 --- a/modes.h +++ b/modes.h @@ -112,7 +112,7 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CFB_ModePolicy : public ModePolicyCommonTe { public: IV_Requirement IVRequirement() const {return RANDOM_IV;} - static const char * CRYPTOPP_API StaticAlgorithmName() {return "CFB";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "CFB";} protected: unsigned int GetBytesPerIteration() const {return m_feedbackSize;} @@ -143,7 +143,7 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE OFB_ModePolicy : public ModePolicyCommonTe public: bool CipherIsRandomAccess() const {return false;} IV_Requirement IVRequirement() const {return UNIQUE_IV;} - static const char * CRYPTOPP_API StaticAlgorithmName() {return "OFB";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "OFB";} private: unsigned int GetBytesPerIteration() const {return BlockSize();} @@ -159,7 +159,7 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CTR_ModePolicy : public ModePolicyCommonTe public: bool CipherIsRandomAccess() const {return true;} IV_Requirement IVRequirement() const {return RANDOM_IV;} - static const char * CRYPTOPP_API StaticAlgorithmName() {return "CTR";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "CTR";} protected: virtual void IncrementCounterBy256(); @@ -216,7 +216,7 @@ public: IV_Requirement IVRequirement() const {return NOT_RESYNCHRONIZABLE;} unsigned int OptimalBlockSize() const {return BlockSize() * m_cipher->OptimalNumberOfParallelBlocks();} void ProcessData(byte *outString, const byte *inString, size_t length); - static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECB";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECB";} }; //! \class CBC_ModeBase @@ -227,7 +227,7 @@ public: IV_Requirement IVRequirement() const {return UNPREDICTABLE_RANDOM_IV;} bool RequireAlignedInput() const {return false;} unsigned int MinLastBlockSize() const {return 0;} - static const char * CRYPTOPP_API StaticAlgorithmName() {return "CBC";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "CBC";} }; //! \class CBC_Encryption @@ -246,7 +246,7 @@ public: void SetStolenIV(byte *iv) {m_stolenIV = iv;} unsigned int MinLastBlockSize() const {return BlockSize()+1;} void ProcessLastBlock(byte *outString, const byte *inString, size_t length); - static const char * CRYPTOPP_API StaticAlgorithmName() {return "CBC/CTS";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "CBC/CTS";} protected: void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms) diff --git a/panama.h b/panama.h index b1a41e61..8276c7d5 100644 --- a/panama.h +++ b/panama.h @@ -44,7 +44,7 @@ public: PanamaHash() {Panama::Reset();} unsigned int DigestSize() const {return DIGESTSIZE;} void TruncatedFinal(byte *hash, size_t size); - static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";} protected: void Init() {Panama::Reset();} @@ -128,7 +128,7 @@ public: template struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32> { - static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";} }; //! \class PanamaCipherPolicy diff --git a/pkcspad.h b/pkcspad.h index 1af7c0ce..a400e988 100644 --- a/pkcspad.h +++ b/pkcspad.h @@ -19,7 +19,7 @@ NAMESPACE_BEGIN(CryptoPP) class PKCS_EncryptionPaddingScheme : public PK_EncryptionMessageEncodingMethod { public: - static const char * StaticAlgorithmName() {return "EME-PKCS1-v1_5";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "EME-PKCS1-v1_5";} size_t MaxUnpaddedLength(size_t paddedLength) const; void Pad(RandomNumberGenerator &rng, const byte *raw, size_t inputLength, byte *padded, size_t paddedLength, const NameValuePairs ¶meters) const; @@ -60,12 +60,12 @@ CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration; class CRYPTOPP_DLL PKCS1v15_SignatureMessageEncodingMethod : public PK_DeterministicSignatureMessageEncodingMethod { public: - static const char * CRYPTOPP_API StaticAlgorithmName() {return "EMSA-PKCS1-v1_5";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "EMSA-PKCS1-v1_5";} size_t MinRepresentativeBitLength(size_t hashIdentifierSize, size_t digestSize) const {return 8 * (digestSize + hashIdentifierSize + 10);} - void ComputeMessageRepresentative(RandomNumberGenerator &rng, + void ComputeMessageRepresentative(RandomNumberGenerator &rng, const byte *recoverableMessage, size_t recoverableMessageLength, HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty, byte *representative, size_t representativeBitLength) const; diff --git a/pubkey.h b/pubkey.h index a91df165..78f3fa8a 100644 --- a/pubkey.h +++ b/pubkey.h @@ -712,7 +712,7 @@ CRYPTOPP_DLL void CRYPTOPP_API P1363_MGF1KDF2_Common(HashTransformation &hash, b class P1363_MGF1 : public MaskGeneratingFunction { public: - static const char * CRYPTOPP_API StaticAlgorithmName() {return "MGF1";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "MGF1";} void GenerateAndMask(HashTransformation &hash, byte *output, size_t outputLength, const byte *input, size_t inputLength, bool mask = true) const { P1363_MGF1KDF2_Common(hash, output, outputLength, input, inputLength, NULL, 0, mask, 0); @@ -1978,7 +1978,7 @@ public: virtual ~DL_KeyAgreementAlgorithm_DH() {} #endif - static const char * CRYPTOPP_API StaticAlgorithmName() + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return COFACTOR_OPTION::ToEnum() == INCOMPATIBLE_COFACTOR_MULTIPLICTION ? "DHC" : "DH";} Element AgreeWithEphemeralPrivateKey(const DL_GroupParameters ¶ms, const DL_FixedBasePrecomputation &publicPrecomputation, const Integer &privateExponent) const diff --git a/rc2.h b/rc2.h index b917abfb..760d665b 100644 --- a/rc2.h +++ b/rc2.h @@ -18,7 +18,7 @@ struct RC2_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 1, 128> { CRYPTOPP_CONSTANT(DEFAULT_EFFECTIVE_KEYLENGTH = 1024) CRYPTOPP_CONSTANT(MAX_EFFECTIVE_KEYLENGTH = 1024) - static const char *StaticAlgorithmName() {return "RC2";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "RC2";} }; //! \class RC2 diff --git a/rc5.h b/rc5.h index b1012737..b76a5217 100644 --- a/rc5.h +++ b/rc5.h @@ -15,7 +15,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief RC5 block cipher information struct RC5_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 0, 255>, public VariableRounds<16> { - static const char *StaticAlgorithmName() {return "RC5";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "RC5";} typedef word32 RC5_WORD; }; diff --git a/rc6.h b/rc6.h index 9ac55d80..5b84a0bd 100644 --- a/rc6.h +++ b/rc6.h @@ -15,7 +15,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief RC6 block cipher information struct RC6_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 8>, public VariableRounds<20> { - static const char *StaticAlgorithmName() {return "RC6";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "RC6";} typedef word32 RC6_WORD; }; diff --git a/ripemd.h b/ripemd.h index 818c4284..b93b1614 100644 --- a/ripemd.h +++ b/ripemd.h @@ -17,7 +17,7 @@ class RIPEMD160 : public IteratedHashWithStaticTransform //! \brief RSA algorithm struct CRYPTOPP_DLL RSA_ISO { - static const char * CRYPTOPP_API StaticAlgorithmName() {return "RSA-ISO";} + CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "RSA-ISO";} typedef RSAFunction_ISO PublicKey; typedef InvertibleRSAFunction_ISO PrivateKey; }; diff --git a/safer.h b/safer.h index 0a6cb064..6be48474 100644 --- a/safer.h +++ b/safer.h @@ -66,7 +66,7 @@ protected: //! \brief SAFER-K block cipher information struct SAFER_K_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13> { - static const char *StaticAlgorithmName() {return "SAFER-K";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "SAFER-K";} }; //! \class SAFER_K @@ -83,7 +83,7 @@ public: //! \brief SAFER-SK block cipher information struct SAFER_SK_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13> { - static const char *StaticAlgorithmName() {return "SAFER-SK";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "SAFER-SK";} }; //! \class SAFER_SK diff --git a/salsa.h b/salsa.h index 6a70af6e..623b36f7 100644 --- a/salsa.h +++ b/salsa.h @@ -22,7 +22,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief Salsa20 stream cipher information struct Salsa20_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8> { - static const char *StaticAlgorithmName() {return "Salsa20";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Salsa20";} }; //! \class Salsa20_Policy @@ -58,7 +58,7 @@ struct Salsa20 : public Salsa20_Info, public SymmetricCipherDocumentation //! \brief XSalsa20 stream cipher information struct XSalsa20_Info : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 24> { - static const char *StaticAlgorithmName() {return "XSalsa20";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "XSalsa20";} }; //! \class XSalsa20_Policy diff --git a/seal.h b/seal.h index 9c898e52..92e0321e 100644 --- a/seal.h +++ b/seal.h @@ -17,7 +17,7 @@ NAMESPACE_BEGIN(CryptoPP) template struct SEAL_Info : public FixedKeyLength<20, SimpleKeyingInterface::INTERNALLY_GENERATED_IV, 4> { - static const char *StaticAlgorithmName() {return B::ToEnum() == LITTLE_ENDIAN_ORDER ? "SEAL-3.0-LE" : "SEAL-3.0-BE";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return B::ToEnum() == LITTLE_ENDIAN_ORDER ? "SEAL-3.0-LE" : "SEAL-3.0-BE";} }; //! \class SEAL_Policy diff --git a/seed.h b/seed.h index f62f6626..96158293 100644 --- a/seed.h +++ b/seed.h @@ -15,7 +15,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief SEED block cipher information struct SEED_Info : public FixedBlockSize<16>, public FixedKeyLength<16>, public FixedRounds<16> { - static const char *StaticAlgorithmName() {return "SEED";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "SEED";} }; //! \class SEED diff --git a/serpent.h b/serpent.h index 888d70af..501ef0a9 100644 --- a/serpent.h +++ b/serpent.h @@ -15,7 +15,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief Serpent block cipher information struct Serpent_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 8>, public FixedRounds<32> { - static const char *StaticAlgorithmName() {return "Serpent";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Serpent";} }; //! \class Serpent diff --git a/sha.h b/sha.h index c70d9d1f..7c145115 100644 --- a/sha.h +++ b/sha.h @@ -23,7 +23,7 @@ class CRYPTOPP_DLL SHA1 : public IteratedHashWithStaticTransform, public VariableKeyLength<16, 16, 64> { - static const char *StaticAlgorithmName() {return "SHACAL-2";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "SHACAL-2";} }; //! \class SHACAL2 diff --git a/shark.h b/shark.h index bf413c92..dbb79a6d 100644 --- a/shark.h +++ b/shark.h @@ -16,7 +16,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief SHARK block cipher information struct SHARK_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public VariableRounds<6, 2> { - static const char *StaticAlgorithmName() {return "SHARK-E";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "SHARK-E";} }; //! \class SHARK diff --git a/sosemanuk.h b/sosemanuk.h index 5b32ee85..a64d92c2 100644 --- a/sosemanuk.h +++ b/sosemanuk.h @@ -20,7 +20,7 @@ NAMESPACE_BEGIN(CryptoPP) //! algorithm info struct SosemanukInfo : public VariableKeyLength<16, 1, 32, 1, SimpleKeyingInterface::UNIQUE_IV, 16> { - static const char * StaticAlgorithmName() {return "Sosemanuk";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Sosemanuk";} }; //! _ diff --git a/square.h b/square.h index cf058a3d..1c5c5498 100644 --- a/square.h +++ b/square.h @@ -15,7 +15,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief Square block cipher information struct Square_Info : public FixedBlockSize<16>, public FixedKeyLength<16>, FixedRounds<8> { - static const char *StaticAlgorithmName() {return "Square";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Square";} }; //! \class Square diff --git a/tea.h b/tea.h index e1d0da87..a019326b 100644 --- a/tea.h +++ b/tea.h @@ -16,7 +16,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief TEA block cipher information struct TEA_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public VariableRounds<32> { - static const char *StaticAlgorithmName() {return "TEA";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "TEA";} }; //! \class TEA @@ -64,7 +64,7 @@ typedef TEA::Decryption TEADecryption; //! \brief XTEA block cipher information struct XTEA_Info : public FixedBlockSize<8>, public FixedKeyLength<16>, public VariableRounds<32> { - static const char *StaticAlgorithmName() {return "XTEA";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "XTEA";} }; //! \class XTEA @@ -109,7 +109,7 @@ public: //! \brief BTEA block cipher information struct BTEA_Info : public FixedKeyLength<16> { - static const char *StaticAlgorithmName() {return "BTEA";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "BTEA";} }; //! \class BTEA diff --git a/tiger.h b/tiger.h index 2c203af7..395d7805 100644 --- a/tiger.h +++ b/tiger.h @@ -13,7 +13,7 @@ public: static void InitState(HashWordType *state); static void Transform(word64 *digest, const word64 *data); void TruncatedFinal(byte *hash, size_t size); - static const char * StaticAlgorithmName() {return "Tiger";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Tiger";} protected: static const word64 table[4*256+3]; diff --git a/twofish.h b/twofish.h index f6d75ca8..99c31374 100644 --- a/twofish.h +++ b/twofish.h @@ -15,7 +15,7 @@ NAMESPACE_BEGIN(CryptoPP) //! \brief Twofish block cipher information struct Twofish_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 8>, FixedRounds<16> { - static const char *StaticAlgorithmName() {return "Twofish";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Twofish";} }; //! \class Twofish diff --git a/wake.h b/wake.h index 9629e9f9..972216d3 100644 --- a/wake.h +++ b/wake.h @@ -18,7 +18,7 @@ NAMESPACE_BEGIN(CryptoPP) template struct WAKE_OFB_Info : public FixedKeyLength<32> { - static const char *StaticAlgorithmName() {return B::ToEnum() == LITTLE_ENDIAN_ORDER ? "WAKE-OFB-LE" : "WAKE-OFB-BE";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return B::ToEnum() == LITTLE_ENDIAN_ORDER ? "WAKE-OFB-LE" : "WAKE-OFB-BE";} }; class CRYPTOPP_NO_VTABLE WAKE_Base diff --git a/whrlpool.h b/whrlpool.h index 68b43204..58dbf22d 100644 --- a/whrlpool.h +++ b/whrlpool.h @@ -13,7 +13,7 @@ public: static void InitState(HashWordType *state); static void Transform(word64 *digest, const word64 *data); void TruncatedFinal(byte *hash, size_t size); - static const char * StaticAlgorithmName() {return "Whirlpool";} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Whirlpool";} }; NAMESPACE_END From e2f2ace688167fe71d574b0c1b3305eacaeb9077 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Wed, 7 Sep 2016 06:39:52 -0400 Subject: [PATCH 24/28] Add constexpr-ness to ChaCha StaticAlgorithmName member function --- chacha.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/chacha.h b/chacha.h index 28b8c7af..f62606b7 100644 --- a/chacha.h +++ b/chacha.h @@ -19,7 +19,9 @@ NAMESPACE_BEGIN(CryptoPP) template struct ChaCha_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>, public FixedRounds { - static const char *StaticAlgorithmName() {static const std::string name = "ChaCha" + IntToString(R); return name.c_str();} + CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() { + return (R==8?"ChaCha8":(R==12?"ChaCha12":(R==20?"ChaCha20":"ChaCha"))); + } }; //! \class ChaCha_Policy From f0e7b45bcbcc5b979d9d8df9d151f5951996e6e9 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Wed, 7 Sep 2016 09:32:06 -0400 Subject: [PATCH 25/28] Remove comma operator from return values for StaticGetDefaultRounds and StaticGetValidKeyLength in non-constexpr builds (Issue 255) --- config.h | 2 +- config.recommend | 2 +- seckey.h | 26 ++++++++++++++++++++++---- validat1.cpp | 4 ++++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/config.h b/config.h index ab00dc7a..0d99df57 100644 --- a/config.h +++ b/config.h @@ -546,7 +546,7 @@ NAMESPACE_END // How to declare class constants // Use enum for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -#if (defined(_MSC_VER) && _MSC_VER <= 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) || (defined(__APPLE__) && (__GNUC__ == 4) && (__GNUC_MINOR__ <= 2)) +#if (defined(_MSC_VER) && _MSC_VER <= 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) # define CRYPTOPP_CONSTANT(x) enum {x}; #else # define CRYPTOPP_CONSTANT(x) static const int x; diff --git a/config.recommend b/config.recommend index 61fcb1df..966a6da8 100644 --- a/config.recommend +++ b/config.recommend @@ -546,7 +546,7 @@ NAMESPACE_END // How to declare class constants // Use enum for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255 -#if (defined(_MSC_VER) && _MSC_VER <= 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) || (defined(__APPLE__) && (__GNUC__ == 4) && (__GNUC_MINOR__ <= 2)) +#if (defined(_MSC_VER) && _MSC_VER <= 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) # define CRYPTOPP_CONSTANT(x) enum {x}; #else # define CRYPTOPP_CONSTANT(x) static const int x; diff --git a/seckey.h b/seckey.h index 31286ce6..bbf1ea7a 100644 --- a/seckey.h +++ b/seckey.h @@ -71,11 +71,20 @@ public: //! \param keylength the size of the key, in bytes //! \details keylength is unused in the default implementation. CRYPTOPP_CONSTEXPR static unsigned int StaticGetDefaultRounds(size_t keylength) - {return CRYPTOPP_UNUSED(keylength), DEFAULT_ROUNDS;} + { + // Comma operator breaks Debug builds with GCC 4.0 - 4.6. + // Also see http://github.com/weidai11/cryptopp/issues/255 +#if defined(CRYPTOPP_CXX11_CONSTEXPR) + return CRYPTOPP_UNUSED(keylength), static_cast(DEFAULT_ROUNDS); +#else + CRYPTOPP_UNUSED(keylength); + return static_cast(DEFAULT_ROUNDS); +#endif + } protected: //! \brief Validates the number of rounds for an algorithm. - //! \param rounds the canddiate number of rounds + //! \param rounds the candidate number of rounds //! \param alg an Algorithm object used if the number of rounds are invalid //! \throws InvalidRounds if the number of rounds are invalid //! \details ThrowIfInvalidRounds() validates the number of rounds and throws if invalid. @@ -94,7 +103,7 @@ protected: } //! \brief Validates the number of rounds for an algorithm - //! \param param the canddiate number of rounds + //! \param param the candidate number of rounds //! \param alg an Algorithm object used if the number of rounds are invalid //! \returns the number of rounds for the algorithm //! \throws InvalidRounds if the number of rounds are invalid @@ -143,7 +152,16 @@ public: //! \details The default implementation returns KEYLENGTH. keylength is unused //! in the default implementation. CRYPTOPP_CONSTEXPR static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength) - {return CRYPTOPP_UNUSED(keylength), KEYLENGTH;} + { + // Comma operator breaks Debug builds with GCC 4.0 - 4.6. + // Also see http://github.com/weidai11/cryptopp/issues/255 +#if defined(CRYPTOPP_CXX11_CONSTEXPR) + return CRYPTOPP_UNUSED(keylength), static_cast(KEYLENGTH); +#else + CRYPTOPP_UNUSED(keylength); + return static_cast(KEYLENGTH); +#endif + } }; //! \class VariableKeyLength diff --git a/validat1.cpp b/validat1.cpp index afda0d94..66285929 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -2436,6 +2436,7 @@ bool ValidateRC5() bool pass1 = true, pass2 = true; RC5Encryption enc; // 0 to 2040-bits (255-bytes) + pass1 = RC5Encryption::DEFAULT_KEYLENGTH == 16 && pass1; pass1 = enc.StaticGetValidKeyLength(0) == 0 && pass1; pass1 = enc.StaticGetValidKeyLength(254) == 254 && pass1; pass1 = enc.StaticGetValidKeyLength(255) == 255 && pass1; @@ -2444,6 +2445,7 @@ bool ValidateRC5() pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1; RC5Decryption dec; + pass2 = RC5Decryption::DEFAULT_KEYLENGTH == 16 && pass2; pass2 = dec.StaticGetValidKeyLength(0) == 0 && pass2; pass2 = dec.StaticGetValidKeyLength(254) == 254 && pass2; pass2 = dec.StaticGetValidKeyLength(255) == 255 && pass2; @@ -2684,11 +2686,13 @@ bool ValidateThreeWay() bool pass1 = true, pass2 = true; ThreeWayEncryption enc; // 96-bit only + pass1 = ThreeWayEncryption::KEYLENGTH == 12 && pass1; pass1 = enc.StaticGetValidKeyLength(8) == 12 && pass1; pass1 = enc.StaticGetValidKeyLength(12) == 12 && pass1; pass1 = enc.StaticGetValidKeyLength(16) == 12 && pass1; ThreeWayDecryption dec; // 96-bit only + pass2 = ThreeWayEncryption::KEYLENGTH == 12 && pass2; pass2 = dec.StaticGetValidKeyLength(8) == 12 && pass2; pass2 = dec.StaticGetValidKeyLength(12) == 12 && pass2; pass2 = dec.StaticGetValidKeyLength(16) == 12 && pass2; From 416605c31135f21f6e8c73359764ee286e0ab0f3 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Wed, 7 Sep 2016 10:10:27 -0400 Subject: [PATCH 26/28] Add additional KEYLENGTH and DEFAULT_KEYLENGTH tests --- validat1.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/validat1.cpp b/validat1.cpp index 66285929..7655656e 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -2692,7 +2692,7 @@ bool ValidateThreeWay() pass1 = enc.StaticGetValidKeyLength(16) == 12 && pass1; ThreeWayDecryption dec; // 96-bit only - pass2 = ThreeWayEncryption::KEYLENGTH == 12 && pass2; + pass2 = ThreeWayDecryption::KEYLENGTH == 12 && pass2; pass2 = dec.StaticGetValidKeyLength(8) == 12 && pass2; pass2 = dec.StaticGetValidKeyLength(12) == 12 && pass2; pass2 = dec.StaticGetValidKeyLength(16) == 12 && pass2; @@ -2708,6 +2708,7 @@ bool ValidateGOST() bool pass1 = true, pass2 = true; GOSTEncryption enc; // 256-bit only + pass1 = GOSTEncryption::KEYLENGTH == 32 && pass1; pass1 = enc.StaticGetValidKeyLength(16) == 32 && pass1; pass1 = enc.StaticGetValidKeyLength(24) == 32 && pass1; pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1; @@ -2715,6 +2716,7 @@ bool ValidateGOST() pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1; GOSTDecryption dec; // 256-bit only + pass2 = GOSTDecryption::KEYLENGTH == 32 && pass2; pass2 = dec.StaticGetValidKeyLength(16) == 32 && pass2; pass2 = dec.StaticGetValidKeyLength(24) == 32 && pass2; pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2; @@ -2732,6 +2734,7 @@ bool ValidateSHARK() bool pass1 = true, pass2 = true; SHARKEncryption enc; // 128-bit only + pass1 = SHARKEncryption::KEYLENGTH == 16 && pass1; pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1; pass1 = enc.StaticGetValidKeyLength(15) == 16 && pass1; pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1; @@ -2739,6 +2742,7 @@ bool ValidateSHARK() pass1 = enc.StaticGetValidKeyLength(32) == 16 && pass1; SHARKDecryption dec; // 128-bit only + pass2 = SHARKDecryption::KEYLENGTH == 16 && pass2; pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2; pass2 = dec.StaticGetValidKeyLength(15) == 16 && pass2; pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2; @@ -2756,6 +2760,7 @@ bool ValidateCAST() bool pass1 = true, pass2 = true, pass3 = true; CAST128Encryption enc1; // 40 to 128-bits (5 to 16-bytes) + pass1 = CAST128Encryption::DEFAULT_KEYLENGTH == 16 && pass1; pass1 = enc1.StaticGetValidKeyLength(4) == 5 && pass1; pass1 = enc1.StaticGetValidKeyLength(5) == 5 && pass1; pass1 = enc1.StaticGetValidKeyLength(15) == 15 && pass1; @@ -2763,6 +2768,7 @@ bool ValidateCAST() pass1 = enc1.StaticGetValidKeyLength(17) == 16 && pass1; CAST128Decryption dec1; // 40 to 128-bits (5 to 16-bytes) + pass2 = CAST128Decryption::DEFAULT_KEYLENGTH == 16 && pass2; pass2 = dec1.StaticGetValidKeyLength(4) == 5 && pass2; pass2 = dec1.StaticGetValidKeyLength(5) == 5 && pass2; pass2 = dec1.StaticGetValidKeyLength(15) == 15 && pass2; @@ -2779,6 +2785,7 @@ bool ValidateCAST() bool pass4 = true, pass5 = true, pass6 = true; CAST256Encryption enc2; // 128, 160, 192, 224, or 256-bits (16 to 32-bytes, step 4) + pass1 = CAST128Encryption::DEFAULT_KEYLENGTH == 16 && pass1; pass4 = enc2.StaticGetValidKeyLength(15) == 16 && pass4; pass4 = enc2.StaticGetValidKeyLength(16) == 16 && pass4; pass4 = enc2.StaticGetValidKeyLength(17) == 20 && pass4; @@ -2790,6 +2797,7 @@ bool ValidateCAST() pass4 = enc2.StaticGetValidKeyLength(33) == 32 && pass4; CAST256Decryption dec2; // 128, 160, 192, 224, or 256-bits (16 to 32-bytes, step 4) + pass2 = CAST256Decryption::DEFAULT_KEYLENGTH == 16 && pass2; pass5 = dec2.StaticGetValidKeyLength(15) == 16 && pass5; pass5 = dec2.StaticGetValidKeyLength(16) == 16 && pass5; pass5 = dec2.StaticGetValidKeyLength(17) == 20 && pass5; From d317881da9b2e3ecbba86fa55dfd861c07fc9606 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 31 Jul 2016 03:02:45 -0400 Subject: [PATCH 27/28] Fix GCC compile error under LP64 data model (Issue 236) --- rdrand.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/rdrand.cpp b/rdrand.cpp index 637f5cab..cc18cc43 100644 --- a/rdrand.cpp +++ b/rdrand.cpp @@ -175,7 +175,8 @@ static int ALL_RRI_GenerateBlock(byte *output, size_t size, unsigned int safety) #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 if (_rdrand32_step((word32*)output)) #else - if (_rdrand64_step((word64*)output)) + // Cast due to GCC, http://github.com/weidai11/cryptopp/issues/236 + if (_rdrand64_step(reinterpret_cast(output))) #endif { output += sizeof(val); @@ -196,7 +197,8 @@ static int ALL_RRI_GenerateBlock(byte *output, size_t size, unsigned int safety) #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 if (_rdrand32_step(&val)) #else - if (_rdrand64_step(&val)) + // Cast due to GCC, http://github.com/weidai11/cryptopp/issues/236 + if (_rdrand64_step(reinterpret_cast(&val))) #endif { memcpy(output, &val, size); @@ -348,7 +350,8 @@ static int ALL_RSI_GenerateBlock(byte *output, size_t size, unsigned int safety) #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 if (_rdseed32_step((word32*)output)) #else - if (_rdseed64_step((word64*)output)) + // Cast due to GCC, http://github.com/weidai11/cryptopp/issues/236 + if (_rdseed64_step(reinterpret_cast(output))) #endif { output += sizeof(val); @@ -369,7 +372,8 @@ static int ALL_RSI_GenerateBlock(byte *output, size_t size, unsigned int safety) #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 if (_rdseed32_step(&val)) #else - if (_rdseed64_step(&val)) + // Cast due to GCC, http://github.com/weidai11/cryptopp/issues/236 + if (_rdseed64_step(reinterpret_cast(&val))) #endif { memcpy(output, &val, size); From 89809b7d59dd2505cd1eda89777394850665f990 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Wed, 7 Sep 2016 15:02:23 -0400 Subject: [PATCH 28/28] Removed experimental rotlImmediate and rotrImmediate --- misc.h | 55 ------------------------------------------------------- 1 file changed, 55 deletions(-) diff --git a/misc.h b/misc.h index 5ff4105c..5120b96c 100644 --- a/misc.h +++ b/misc.h @@ -1292,32 +1292,6 @@ CRYPTOPP_DLL void CRYPTOPP_API UnalignedDeallocate(void *ptr); // ************** rotate functions *************** -//! \brief Performs a left rotate -//! \tparam T the word type -//! \tparam Y the number of bit positions to rotate the value -//! \param x the value to rotate -//! \details This is a portable C/C++ implementation which attempts to take advantage of the -//! constexpr-ness of a template parameter in hopes of achieving better code -//! generation under Clang and VC++. If a specialization is not available, then -//! rotlImmediate simply calls rotlFixed. -template inline T rotlImmediate(T x) -{ - return rotlFixed(x, Y); -} - -//! \brief Performs a right rotate -//! \tparam T the word type -//! \tparam Y the number of bit positions to rotate the value -//! \param x the value to rotate -//! \details This is a portable C/C++ implementation which attempts to take advantage of the -//! constexpr-ness of a template parameter in hopes of achieving better code -//! generation under Clang and VC++. If a specialization is not available, then -//! rotrImmediate simply calls rotlFixed. -template inline T rotrFixed(T x) -{ - return rotrFixed(x, Y); -} - //! \brief Performs a left rotate //! \tparam T the word type //! \param x the value to rotate @@ -1426,35 +1400,6 @@ template inline T rotrMod(T x, unsigned int y) return T((x>>(y&MASK))|(x<<(-y&MASK))); } -#if defined(__GNUC__) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32) && 0 -template -inline word32 rotlImmediate(word32 x) -{ - __asm__ ("roll %1, %0" : "+mq" (x) : "I" ((unsigned char)Y)); - return x; -} -template -inline T rotlImmediate(word32 x) -{ - __asm__ ("rorl %1, %0" : "+mq" (x) : "I" ((unsigned char)Y)); - return x; -} -# if (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32) -template <> -inline word64 rotlImmediate(word64 x) -{ - __asm__ ("rolq %1, %0" : "+mq" (x) : "J" ((unsigned char)Y)); - return x; -} -template <> -inline T rotlImmediate(word64 x) -{ - __asm__ ("rorq %1, %0" : "+mq" (x) : "J" ((unsigned char)Y)); - return x; -} -# endif -#endif - #ifdef _MSC_VER //! \brief Performs a left rotate