Crypto++ 7.0 Free C++ class library of cryptographic schemes

keccak.h
Go to the documentation of this file.
1 // keccak.h - originally 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 accommodate 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 /// \brief Keccak message digest base class
21 /// \details The Crypto++ Keccak implementation uses F1600 with XOF d=0x01.
22 /// FIPS 202 conformance (XOF d=0x06) is available in SHA3 classes.
23 /// \details Keccak is the base class for Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
24 /// Library users should instantiate a derived class, and only use Keccak
25 /// as a base class reference or pointer.
26 /// \details Keccak will likely change in the future to accommodate extensibility of the
27 /// round function and the XOF functions.
28 /// \details Perform the following to specify a different digest size. The class will use F1600,
29 /// XOF d=0x01, and a new vaue for <tt>r()</tt> (which will be <tt>200-2*24 = 152</tt>).
30 /// <pre> Keccack_192 : public Keccack
31 /// {
32 /// public:
33 /// CRYPTOPP_CONSTANT(DIGESTSIZE = 24)
34 /// Keccack_192() : Keccack(DIGESTSIZE) {}
35 /// };
36 /// </pre>
37 ///
38 /// \sa SHA3, Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
39 /// \since Crypto++ 5.6.4
40 class Keccak : public HashTransformation
41 {
42 public:
43 /// \brief Construct a Keccak
44 /// \param digestSize the digest size, in bytes
45 /// \details Keccak is the base class for Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
46 /// Library users should instantiate a derived class, and only use Keccak
47 /// as a base class reference or pointer.
48 /// \since Crypto++ 5.6.4
49 Keccak(unsigned int digestSize) : m_digestSize(digestSize) {Restart();}
50 unsigned int DigestSize() const {return m_digestSize;}
51 std::string AlgorithmName() const {return "Keccak-" + IntToString(m_digestSize*8);}
52 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() { return "Keccak"; }
53 unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word64>();}
54
55 void Update(const byte *input, size_t length);
56 void Restart();
57 void TruncatedFinal(byte *hash, size_t size);
58
59 //unsigned int BlockSize() const { return r(); } // that's the idea behind it
60
61 protected:
62 inline unsigned int r() const {return 200 - 2 * m_digestSize;}
63
64 FixedSizeSecBlock<word64, 25> m_state;
65 unsigned int m_digestSize, m_counter;
66 };
67
68 /// \brief Keccak message digest template
69 /// \tparam T_DigestSize the size of the digest, in bytes
70 /// \since Crypto++ 6.0
71 template<unsigned int T_DigestSize>
72 class Keccak_Final : public Keccak
73 {
74 public:
75 CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize)
76 CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE)
77
78 /// \brief Construct a Keccak-X message digest
79 Keccak_Final() : Keccak(DIGESTSIZE) {}
80 static std::string StaticAlgorithmName() { return "Keccak-" + IntToString(DIGESTSIZE * 8); }
81 unsigned int BlockSize() const { return BLOCKSIZE; }
82 private:
83 CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200); // ensure there was no underflow in the math
84 CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE > (int)T_DigestSize); // this is a general expectation by HMAC
85 };
86
87 /// \brief Keccak-224 message digest
88 /// \since Crypto++ 5.6.4
89 typedef Keccak_Final<28> Keccak_224;
90
91 /// \brief Keccak-256 message digest
92 /// \since Crypto++ 5.6.4
93 typedef Keccak_Final<32> Keccak_256;
94
95 /// \brief Keccak-384 message digest
96 /// \since Crypto++ 5.6.4
97 typedef Keccak_Final<48> Keccak_384;
98
99 /// \brief Keccak-512 message digest
100 /// \since Crypto++ 5.6.4
101 typedef Keccak_Final<64> Keccak_512;
102
103 NAMESPACE_END
104
105 #endif
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
Keccak_512
Keccak_Final< 64 > Keccak_512
Keccak-512 message digest.
Definition: keccak.h:101
Keccak::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: keccak.h:50
Keccak_224
Keccak_Final< 28 > Keccak_224
Keccak-224 message digest.
Definition: keccak.h:89
secblock.h
Classes and functions for secure memory allocations.
Keccak::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: keccak.h:51
CRYPTOPP_COMPILE_ASSERT
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:144
Keccak::OptimalDataAlignment
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: keccak.h:53
Keccak_Final::BlockSize
unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: keccak.h:81
Keccak::Keccak
Keccak(unsigned int digestSize)
Construct a Keccak.
Definition: keccak.h:49
Keccak
Keccak message digest base class.
Definition: keccak.h:40
FixedSizeSecBlock< word64, 25 >
Keccak_384
Keccak_Final< 48 > Keccak_384
Keccak-384 message digest.
Definition: keccak.h:97
Keccak_256
Keccak_Final< 32 > Keccak_256
Keccak-256 message digest.
Definition: keccak.h:93
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1084
IntToString
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:632
Keccak_Final
Keccak message digest template.
Definition: keccak.h:72
CryptoPP
Crypto++ library namespace.