From ccd59710a3f1d06bc165de5e276f9280fa8a1dc9 Mon Sep 17 00:00:00 2001 From: DevJPM Date: Tue, 20 Sep 2016 11:27:25 +0200 Subject: [PATCH] converted derived Keccak, SHA3 classes into typedefs removed the old derived class (8 in total) and replaced them by typedefs of {SHA3|Keccak}_Final<> to optimize engineering efforts --- keccak.h | 73 +++++++++++++++----------------------------------------- sha3.h | 69 +++++++++++++--------------------------------------- 2 files changed, 36 insertions(+), 106 deletions(-) diff --git a/keccak.h b/keccak.h index 0349b464..05d6fc3a 100644 --- a/keccak.h +++ b/keccak.h @@ -3,7 +3,7 @@ //! \file keccak.h //! \brief Classes for Keccak message digests //! \details The Crypto++ Keccak implementation uses F1600 with XOF d=0x01. -//! FIPS 202 conformance (XOF d=0x06) is available in SHA3 classes. +//! FIPS 202 conformance (XOF d=0x06) is available in Keccak classes. //! \details Keccak will likely change in the future to accomodate extensibility of the //! round function and the XOF functions. //! \sa Keccak @@ -36,7 +36,7 @@ NAMESPACE_BEGIN(CryptoPP) //! }; //! //! -//! \sa SHA3, Keccak_224, Keccak_256, Keccak_384 and Keccak_512. +//! \sa SHA3, SHA3_224, SHA3_256, SHA3_384 and SHA3_512. //! \since Crypto++ 5.6.4 class Keccak : public HashTransformation { @@ -66,76 +66,41 @@ protected: }; //! \class Keccak_224 -//! \brief Keccak-224 message digest -//! \since Crypto++ 5.6.4 -class Keccak_224 : public Keccak +//! \tparam DigestSize controls the digest size as a template parameter instead of a per-class constant +//! \brief Keccak-X message digest, template for more fine-grained typedefs +//! \since Crypto++ 5.7.0 +template +class Keccak_Final : public Keccak { public: - CRYPTOPP_CONSTANT(DIGESTSIZE = 28) + CRYPTOPP_CONSTANT(DIGESTSIZE = digestSize) CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE) - //! \brief Construct a Keccak-224 message digest - Keccak_224() : Keccak(DIGESTSIZE) {} - CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Keccak-224";} - unsigned int BlockSize() const { return BLOCKSIZE; } + //! \brief Construct a Keccak-X message digest + Keccak_Final() : Keccak(DIGESTSIZE) {} + CRYPTOPP_CONSTEXPR static std::string StaticAlgorithmName() { return "Keccak-" + IntToString(DIGESTSIZE * 8); } + CRYPTOPP_CONSTEXPR unsigned int BlockSize() const { return BLOCKSIZE; } private: CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200); // ensure there was no underflow in the math CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE > DIGESTSIZE); // this is a general expectation by HMAC }; +//! \class Keccak_224 +//! \brief Keccak-224 message digest +//! \since Crypto++ 5.6.4 +typedef Keccak_Final<28> Keccak_224; //! \class Keccak_256 //! \brief Keccak-256 message digest //! \since Crypto++ 5.6.4 -class Keccak_256 : public Keccak -{ -public: - CRYPTOPP_CONSTANT(DIGESTSIZE = 32) - CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE) - - //! \brief Construct a Keccak-256 message digest - Keccak_256() : Keccak(DIGESTSIZE) {} - CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Keccak-256";} - unsigned int BlockSize() const { return BLOCKSIZE; } -private: - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200); // ensure there was no underflow in the math - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE > DIGESTSIZE); // this is a general expectation by HMAC -}; - +typedef Keccak_Final<32> Keccak_256; //! \class Keccak_384 //! \brief Keccak-384 message digest //! \since Crypto++ 5.6.4 -class Keccak_384 : public Keccak -{ -public: - CRYPTOPP_CONSTANT(DIGESTSIZE = 48) - CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE) - - //! \brief Construct a Keccak-384 message digest - Keccak_384() : Keccak(DIGESTSIZE) {} - CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Keccak-384";} - unsigned int BlockSize() const { return BLOCKSIZE; } -private: - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200); // ensure there was no underflow in the math - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE > DIGESTSIZE); // this is a general expectation by HMAC -}; - +typedef Keccak_Final<48> Keccak_384; //! \class Keccak_512 //! \brief Keccak-512 message digest //! \since Crypto++ 5.6.4 -class Keccak_512 : public Keccak -{ -public: - CRYPTOPP_CONSTANT(DIGESTSIZE = 64) - CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE) - - //! \brief Construct a Keccak-512 message digest - Keccak_512() : Keccak(DIGESTSIZE) {} - CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Keccak-512";} - unsigned int BlockSize() const { return BLOCKSIZE; } -private: - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200); // ensure there was no underflow in the math - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE > DIGESTSIZE); // this is a general expectation by HMAC -}; +typedef Keccak_Final<64> Keccak_512; NAMESPACE_END diff --git a/sha3.h b/sha3.h index f2d3b742..8b988ab1 100644 --- a/sha3.h +++ b/sha3.h @@ -51,76 +51,41 @@ protected: }; //! \class SHA3_224 -//! \brief SHA3-224 message digest -//! \since Crypto++ 5.6.2 -class SHA3_224 : public SHA3 +//! \tparam DigestSize controls the digest size as a template parameter instead of a per-class constant +//! \brief SHA3-X message digest, template for more fine-grained typedefs +//! \since Crypto++ 5.7.0 +template +class SHA3_Final : public SHA3 { public: - CRYPTOPP_CONSTANT(DIGESTSIZE = 28) + CRYPTOPP_CONSTANT(DIGESTSIZE = digestSize) CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE) - //! \brief Construct a SHA3-224 message digest - SHA3_224() : SHA3(DIGESTSIZE) {} - CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "SHA3-224";} - unsigned int BlockSize() const { return BLOCKSIZE; } + //! \brief Construct a SHA3-X message digest + SHA3_Final() : SHA3(DIGESTSIZE) {} + CRYPTOPP_CONSTEXPR static std::string StaticAlgorithmName() { return "SHA3-" + IntToString(DIGESTSIZE * 8); } + CRYPTOPP_CONSTEXPR unsigned int BlockSize() const { return BLOCKSIZE; } private: CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200); // ensure there was no underflow in the math CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE > DIGESTSIZE); // this is a general expectation by HMAC }; +//! \class SHA3_224 +//! \brief SHA3-224 message digest +//! \since Crypto++ 5.6.2 +typedef SHA3_Final<28> SHA3_224; //! \class SHA3_256 //! \brief SHA3-256 message digest //! \since Crypto++ 5.6.2 -class SHA3_256 : public SHA3 -{ -public: - CRYPTOPP_CONSTANT(DIGESTSIZE = 32) - CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE) - - //! \brief Construct a SHA3-256 message digest - SHA3_256() : SHA3(DIGESTSIZE) {} - CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "SHA3-256";} - unsigned int BlockSize() const { return BLOCKSIZE; } -private: - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200); // ensure there was no underflow in the math - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE > DIGESTSIZE); // this is a general expectation by HMAC -}; - +typedef SHA3_Final<32> SHA3_256; //! \class SHA3_384 //! \brief SHA3-384 message digest //! \since Crypto++ 5.6.2 -class SHA3_384 : public SHA3 -{ -public: - CRYPTOPP_CONSTANT(DIGESTSIZE = 48) - CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE) - - //! \brief Construct a SHA3-384 message digest - SHA3_384() : SHA3(DIGESTSIZE) {} - CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "SHA3-384";} - unsigned int BlockSize() const { return BLOCKSIZE; } -private: - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200); // ensure there was no underflow in the math - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE > DIGESTSIZE); // this is a general expectation by HMAC -}; - +typedef SHA3_Final<48> SHA3_384; //! \class SHA3_512 //! \brief SHA3-512 message digest //! \since Crypto++ 5.6.2 -class SHA3_512 : public SHA3 -{ -public: - CRYPTOPP_CONSTANT(DIGESTSIZE = 64) - CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE) - - //! \brief Construct a SHA3-512 message digest - SHA3_512() : SHA3(DIGESTSIZE) {} - CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "SHA3-512";} - unsigned int BlockSize() const { return BLOCKSIZE; } -private: - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200); // ensure there was no underflow in the math - CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE > DIGESTSIZE); // this is a general expectation by HMAC -}; +typedef SHA3_Final<64> SHA3_512; NAMESPACE_END