Crypto++  5.6.5
Free C++ class library of cryptographic schemes
keccak.h
Go to the documentation of this file.
1 // keccak.h - written and placed in the public domain by Wei Dai
2 
3 //! \file keccak.h
4 //! \brief Classes for Keccak message digests
5 //! \details The Crypto++ Keccak implementation uses F1600 with XOF d=0x01.
6 //! FIPS 202 conformance (XOF d=0x06) is available in SHA3 classes.
7 //! \details Keccak will likely change in the future to accomodate extensibility of the
8 //! round function and the XOF functions.
9 //! \sa <a href="http://en.wikipedia.org/wiki/Keccak">Keccak</a>
10 //! \since Crypto++ 5.6.4
11 
12 #ifndef CRYPTOPP_KECCAK_H
13 #define CRYPTOPP_KECCAK_H
14 
15 #include "cryptlib.h"
16 #include "secblock.h"
17 
18 NAMESPACE_BEGIN(CryptoPP)
19 
20 //! \class Keccak
21 //! \brief Keccak message digest base class
22 //! \details The Crypto++ Keccak implementation uses F1600 with XOF d=0x01.
23 //! FIPS 202 conformance (XOF d=0x06) is available in SHA3 classes.
24 //! \details Keccak is the base class for Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
25 //! Library users should instantiate a derived class, and only use Keccak
26 //! as a base class reference or pointer.
27 //! \details Keccak will likely change in the future to accomodate extensibility of the
28 //! round function and the XOF functions.
29 //! \details Perform the following to specify a different digest size. The class will use F1600,
30 //! XOF d=0x01, and a new vaue for <tt>r()</tt> (which will be <tt>200-2*24 = 152</tt>).
31 //! <pre> Keccack_192 : public Keccack
32 //! {
33 //! public:
34 //! CRYPTOPP_CONSTANT(DIGESTSIZE = 24)
35 //! Keccack_192() : Keccack(DIGESTSIZE) {}
36 //! };
37 //! </pre>
38 //!
39 //! \sa SHA3, Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
40 //! \since Crypto++ 5.6.4
41 class Keccak : public HashTransformation
42 {
43 public:
44  //! \brief Construct a Keccak
45  //! \param digestSize the digest size, in bytes
46  //! \details Keccak is the base class for Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
47  //! Library users should instantiate a derived class, and only use Keccak
48  //! as a base class reference or pointer.
49  //! \since Crypto++ 5.6.4
50  Keccak(unsigned int digestSize) : m_digestSize(digestSize) {Restart();}
51  unsigned int DigestSize() const {return m_digestSize;}
52  std::string AlgorithmName() const {return "Keccak-" + IntToString(m_digestSize*8);}
53  CRYPTOPP_CONSTEXPR static const char* StaticAlgorithmName() { return "Keccak"; }
54  unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word64>();}
55 
56  void Update(const byte *input, size_t length);
57  void Restart();
58  void TruncatedFinal(byte *hash, size_t size);
59 
60  //unsigned int BlockSize() const { return r(); } // that's the idea behind it
61 
62 protected:
63  inline unsigned int r() const {return 200 - 2 * m_digestSize;}
64 
66  unsigned int m_digestSize, m_counter;
67 };
68 
69 //! \class Keccak_224
70 //! \tparam DigestSize controls the digest size as a template parameter instead of a per-class constant
71 //! \brief Keccak-X message digest, template for more fine-grained typedefs
72 //! \since Crypto++ 5.7.0
73 template<unsigned int T_DigestSize>
74 class Keccak_Final : public Keccak
75 {
76 public:
77  CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize)
78  CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE)
79 
80  //! \brief Construct a Keccak-X message digest
81  Keccak_Final() : Keccak(DIGESTSIZE) {}
82  static std::string StaticAlgorithmName() { return "Keccak-" + IntToString(DIGESTSIZE * 8); }
83  unsigned int BlockSize() const { return BLOCKSIZE; }
84 private:
85  CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200); // ensure there was no underflow in the math
86  CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE > (int)T_DigestSize); // this is a general expectation by HMAC
87 };
88 
89 //! \class Keccak_224
90 //! \brief Keccak-224 message digest
91 //! \since Crypto++ 5.6.4
93 //! \class Keccak_256
94 //! \brief Keccak-256 message digest
95 //! \since Crypto++ 5.6.4
97 //! \class Keccak_384
98 //! \brief Keccak-384 message digest
99 //! \since Crypto++ 5.6.4
101 //! \class Keccak_512
102 //! \brief Keccak-512 message digest
103 //! \since Crypto++ 5.6.4
105 
106 NAMESPACE_END
107 
108 #endif
Abstract base classes that provide a uniform interface to this library.
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: keccak.h:51
Classes and functions for secure memory allocations.
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: keccak.h:52
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:123
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: keccak.h:54
unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: keccak.h:83
Keccak(unsigned int digestSize)
Construct a Keccak.
Definition: keccak.h:50
Keccak message digest base class.
Definition: keccak.h:41
Keccak-X message digest, template for more fine-grained typedefs.
Keccak-384 message digest.
Keccak-512 message digest.
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:931
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:533
Keccak-256 message digest.
Crypto++ library namespace.