Crypto++ 8.6 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 protected:
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 /// \details This constructor was moved to protected at Crypto++ 8.1
49 /// because users were attempting to create Keccak objects with it.
50 /// \since Crypto++ 5.6.4
51 Keccak(unsigned int digestSize) : m_digestSize(digestSize) {Restart();}
52
53 public:
54 unsigned int DigestSize() const {return m_digestSize;}
55 unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word64>();}
56
57 void Update(const byte *input, size_t length);
58 void Restart();
59 void TruncatedFinal(byte *hash, size_t size);
60
61 protected:
62 inline unsigned int r() const {return BlockSize();}
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 static std::string StaticAlgorithmName()
78 { return "Keccak-" + IntToString(DIGESTSIZE * 8); }
79
80 /// \brief Construct a Keccak-X message digest
81 Keccak_Final() : Keccak(DIGESTSIZE) {}
82
83 /// \brief Provides the block size of the compression function
84 /// \return block size of the compression function, in bytes
85 /// \details BlockSize() will return 0 if the hash is not block based
86 /// or does not have an equivalent block size. For example, Keccak
87 /// and SHA-3 do not have a block size, but they do have an equivalent
88 /// block size called rate expressed as <tt>r</tt>.
89 unsigned int BlockSize() const { return BLOCKSIZE; }
90
91 std::string AlgorithmName() const { return StaticAlgorithmName(); }
92
93 private:
94 #if !defined(__BORLANDC__)
95 // ensure there was no underflow in the math
96 CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200);
97 #endif
98 };
99
100 /// \brief Keccak-224 message digest
101 /// \since Crypto++ 5.6.4
102 DOCUMENTED_TYPEDEF(Keccak_Final<28>, Keccak_224);
103
104 /// \brief Keccak-256 message digest
105 /// \since Crypto++ 5.6.4
106 DOCUMENTED_TYPEDEF(Keccak_Final<32>, Keccak_256);
107
108 /// \brief Keccak-384 message digest
109 /// \since Crypto++ 5.6.4
110 DOCUMENTED_TYPEDEF(Keccak_Final<48>, Keccak_384);
111
112 /// \brief Keccak-512 message digest
113 /// \since Crypto++ 5.6.4
114 DOCUMENTED_TYPEDEF(Keccak_Final<64>, Keccak_512);
115
116 NAMESPACE_END
117
118 #endif
Keccak::OptimalDataAlignment
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: keccak.h:55
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1112
secblock.h
Classes and functions for secure memory allocations.
Keccak_Final::Keccak_Final
Keccak_Final()
Construct a Keccak-X message digest.
Definition: keccak.h:81
Keccak_224
Keccak-224 message digest.
Definition: keccak.h:102
CRYPTOPP_COMPILE_ASSERT
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:151
Keccak_384
Keccak-384 message digest.
Definition: keccak.h:110
Keccak
Keccak message digest base class.
Definition: keccak.h:40
IntToString
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:724
Keccak::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: keccak.h:54
Keccak_Final::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: keccak.h:91
Keccak_256
Keccak-256 message digest.
Definition: keccak.h:106
FixedSizeSecBlock< word64, 25 >
Keccak_Final::BlockSize
unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: keccak.h:89
CryptoPP
Crypto++ library namespace.
Keccak_512
Keccak-512 message digest.
Definition: keccak.h:114
Keccak_Final
Keccak message digest template.
Definition: keccak.h:72
cryptlib.h
Abstract base classes that provide a uniform interface to this library.