summaryrefslogtreecommitdiff
path: root/keccak.h
diff options
context:
space:
mode:
authorDevJPM <jean-pierre.muench@web.de>2016-09-21 14:16:10 +0200
committerDevJPM <jean-pierre.muench@web.de>2016-09-21 14:16:10 +0200
commit70635865a189450d030cd1a5c3e739021952123f (patch)
treebea1cdbe86d6a1f823912a6e95a062f3e34da490 /keccak.h
parent54557b18275053bbfc34594f7e65808dd92dd1a6 (diff)
downloadcryptopp-git-70635865a189450d030cd1a5c3e739021952123f.tar.gz
Templated Keccak and SHA3
templated Keccak and SHA3 to reduce code-size, added a StaticAlgorithmName() to the base classes and restricted use of constexpr to this new function in the base classes
Diffstat (limited to 'keccak.h')
-rw-r--r--keccak.h62
1 files changed, 25 insertions, 37 deletions
diff --git a/keccak.h b/keccak.h
index d3580ec1..acdc3f3b 100644
--- a/keccak.h
+++ b/keccak.h
@@ -50,12 +50,15 @@ public:
Keccak(unsigned int digestSize) : m_digestSize(digestSize) {Restart();}
unsigned int DigestSize() const {return m_digestSize;}
std::string AlgorithmName() const {return "Keccak-" + IntToString(m_digestSize*8);}
+ CRYPTOPP_CONSTEXPR static const char* StaticAlgorithmName() { return "Keccak"; }
unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word64>();}
void Update(const byte *input, size_t length);
void Restart();
void TruncatedFinal(byte *hash, size_t size);
+ //unsigned int BlockSize() const { return r(); } // that's the idea behind it
+
protected:
inline unsigned int r() const {return 200 - 2 * m_digestSize;}
@@ -64,56 +67,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<unsigned int digestSize>
+class Keccak_Final : public Keccak
{
public:
- CRYPTOPP_CONSTANT(DIGESTSIZE = 28)
-
- //! \brief Construct a Keccak-224 message digest
- Keccak_224() : Keccak(DIGESTSIZE) {}
- CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Keccak-224";}
+ CRYPTOPP_CONSTANT(DIGESTSIZE = digestSize)
+ CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE)
+
+ //! \brief Construct a Keccak-X message digest
+ Keccak_Final() : Keccak(DIGESTSIZE) {}
+ static std::string StaticAlgorithmName() { return "Keccak-" + IntToString(DIGESTSIZE * 8); }
+ 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)
-
- //! \brief Construct a Keccak-256 message digest
- Keccak_256() : Keccak(DIGESTSIZE) {}
- CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Keccak-256";}
-};
-
+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)
-
- //! \brief Construct a Keccak-384 message digest
- Keccak_384() : Keccak(DIGESTSIZE) {}
- CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Keccak-384";}
-};
-
+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)
-
- //! \brief Construct a Keccak-512 message digest
- Keccak_512() : Keccak(DIGESTSIZE) {}
- CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Keccak-512";}
-};
+typedef Keccak_Final<64> Keccak_512;
NAMESPACE_END