2017-01-27 12:05:45 +00:00
|
|
|
// keccak.h - originally written and placed in the public domain by Wei Dai
|
2016-09-09 00:24:25 +00:00
|
|
|
|
|
|
|
//! \file keccak.h
|
|
|
|
//! \brief Classes for Keccak message digests
|
2016-09-10 22:22:00 +00:00
|
|
|
//! \details The Crypto++ Keccak implementation uses F1600 with XOF d=0x01.
|
2016-09-20 09:31:52 +00:00
|
|
|
//! FIPS 202 conformance (XOF d=0x06) is available in SHA3 classes.
|
2016-12-27 17:34:57 +00:00
|
|
|
//! \details Keccak will likely change in the future to accommodate extensibility of the
|
2016-09-09 08:45:20 +00:00
|
|
|
//! round function and the XOF functions.
|
2016-09-09 00:24:25 +00:00
|
|
|
//! \sa <a href="http://en.wikipedia.org/wiki/Keccak">Keccak</a>
|
2016-09-09 09:19:29 +00:00
|
|
|
//! \since Crypto++ 5.6.4
|
2016-09-09 00:24:25 +00:00
|
|
|
|
|
|
|
#ifndef CRYPTOPP_KECCAK_H
|
|
|
|
#define CRYPTOPP_KECCAK_H
|
|
|
|
|
|
|
|
#include "cryptlib.h"
|
|
|
|
#include "secblock.h"
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
|
|
|
//! \class Keccak
|
|
|
|
//! \brief Keccak message digest base class
|
2016-09-10 22:59:43 +00:00
|
|
|
//! \details The Crypto++ Keccak implementation uses F1600 with XOF d=0x01.
|
|
|
|
//! FIPS 202 conformance (XOF d=0x06) is available in SHA3 classes.
|
2016-09-09 09:04:36 +00:00
|
|
|
//! \details Keccak is the base class for Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
|
|
|
|
//! Library users should instantiate a derived class, and only use Keccak
|
|
|
|
//! as a base class reference or pointer.
|
2016-12-27 17:34:57 +00:00
|
|
|
//! \details Keccak will likely change in the future to accommodate extensibility of the
|
2016-09-09 08:45:20 +00:00
|
|
|
//! round function and the XOF functions.
|
2016-09-10 22:59:43 +00:00
|
|
|
//! \details Perform the following to specify a different digest size. The class will use F1600,
|
|
|
|
//! XOF d=0x01, and a new vaue for <tt>r()</tt> (which will be <tt>200-2*24 = 152</tt>).
|
2016-09-09 08:53:15 +00:00
|
|
|
//! <pre> Keccack_192 : public Keccack
|
2016-09-09 08:45:20 +00:00
|
|
|
//! {
|
|
|
|
//! public:
|
|
|
|
//! CRYPTOPP_CONSTANT(DIGESTSIZE = 24)
|
|
|
|
//! Keccack_192() : Keccack(DIGESTSIZE) {}
|
|
|
|
//! };
|
|
|
|
//! </pre>
|
|
|
|
//!
|
2016-09-20 09:31:52 +00:00
|
|
|
//! \sa SHA3, Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
|
2016-09-09 09:19:29 +00:00
|
|
|
//! \since Crypto++ 5.6.4
|
2016-09-09 00:24:25 +00:00
|
|
|
class Keccak : public HashTransformation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
//! \brief Construct a Keccak
|
|
|
|
//! \param digestSize the digest size, in bytes
|
|
|
|
//! \details Keccak is the base class for Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
|
2016-09-09 09:04:36 +00:00
|
|
|
//! Library users should instantiate a derived class, and only use Keccak
|
2016-09-09 00:24:25 +00:00
|
|
|
//! as a base class reference or pointer.
|
2016-09-09 09:19:29 +00:00
|
|
|
//! \since Crypto++ 5.6.4
|
2016-09-09 00:24:25 +00:00
|
|
|
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);}
|
2016-12-01 14:37:04 +00:00
|
|
|
CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() { return "Keccak"; }
|
2016-09-09 00:24:25 +00:00
|
|
|
unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word64>();}
|
|
|
|
|
|
|
|
void Update(const byte *input, size_t length);
|
|
|
|
void Restart();
|
|
|
|
void TruncatedFinal(byte *hash, size_t size);
|
|
|
|
|
2016-09-21 12:16:10 +00:00
|
|
|
//unsigned int BlockSize() const { return r(); } // that's the idea behind it
|
|
|
|
|
2016-09-09 00:24:25 +00:00
|
|
|
protected:
|
|
|
|
inline unsigned int r() const {return 200 - 2 * m_digestSize;}
|
|
|
|
|
|
|
|
FixedSizeSecBlock<word64, 25> m_state;
|
|
|
|
unsigned int m_digestSize, m_counter;
|
|
|
|
};
|
|
|
|
|
2016-09-20 23:01:38 +00:00
|
|
|
//! \class Keccak_224
|
2017-07-27 23:15:21 +00:00
|
|
|
//! \tparam T_DigestSize controls the digest size as a template parameter instead of a per-class constant
|
2016-09-21 12:16:10 +00:00
|
|
|
//! \brief Keccak-X message digest, template for more fine-grained typedefs
|
2017-02-21 17:59:20 +00:00
|
|
|
//! \since Crypto++ 6.0.0
|
2016-10-01 08:42:42 +00:00
|
|
|
template<unsigned int T_DigestSize>
|
2016-09-21 12:16:10 +00:00
|
|
|
class Keccak_Final : public Keccak
|
2016-09-09 00:24:25 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-10-01 08:42:42 +00:00
|
|
|
CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize)
|
2016-09-21 12:16:10 +00:00
|
|
|
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
|
2016-10-01 09:24:45 +00:00
|
|
|
CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE > (int)T_DigestSize); // this is a general expectation by HMAC
|
2016-09-09 00:24:25 +00:00
|
|
|
};
|
|
|
|
|
2016-09-21 12:16:10 +00:00
|
|
|
//! \brief Keccak-224 message digest
|
|
|
|
//! \since Crypto++ 5.6.4
|
|
|
|
typedef Keccak_Final<28> Keccak_224;
|
2017-07-27 23:15:21 +00:00
|
|
|
|
2016-09-09 00:24:25 +00:00
|
|
|
//! \brief Keccak-256 message digest
|
2016-09-09 09:19:29 +00:00
|
|
|
//! \since Crypto++ 5.6.4
|
2016-09-21 12:16:10 +00:00
|
|
|
typedef Keccak_Final<32> Keccak_256;
|
2017-07-27 23:15:21 +00:00
|
|
|
|
2016-09-09 00:24:25 +00:00
|
|
|
//! \brief Keccak-384 message digest
|
2016-09-09 09:19:29 +00:00
|
|
|
//! \since Crypto++ 5.6.4
|
2016-09-21 12:16:10 +00:00
|
|
|
typedef Keccak_Final<48> Keccak_384;
|
2017-07-27 23:15:21 +00:00
|
|
|
|
2016-09-09 00:24:25 +00:00
|
|
|
//! \brief Keccak-512 message digest
|
2016-09-09 09:19:29 +00:00
|
|
|
//! \since Crypto++ 5.6.4
|
2016-09-21 12:16:10 +00:00
|
|
|
typedef Keccak_Final<64> Keccak_512;
|
2016-09-09 00:24:25 +00:00
|
|
|
|
|
|
|
NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|