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

sha3.h
Go to the documentation of this file.
1 // sha3.h - originally written and placed in the public domain by Wei Dai
2
3 /// \file sha3.h
4 /// \brief Classes for SHA3 message digests
5 /// \details The Crypto++ implementation conforms to the FIPS 202 version of SHA3 using F1600 with XOF d=0x06.
6 /// Previous behavior (XOF d=0x01) is available in Keccak classes.
7 /// \sa <a href="http://en.wikipedia.org/wiki/SHA-3">SHA-3</a>,
8 /// <A HREF="http://csrc.nist.gov/groups/ST/hash/sha-3/fips202_standard_2015.html">SHA-3 STANDARD (FIPS 202)</A>.
9 /// \since Crypto++ 5.6.2
10
11 #ifndef CRYPTOPP_SHA3_H
12 #define CRYPTOPP_SHA3_H
13
14 #include "cryptlib.h"
15 #include "secblock.h"
16
17 NAMESPACE_BEGIN(CryptoPP)
18
19 /// \brief SHA3 message digest base class
20 /// \details The Crypto++ implementation conforms to FIPS 202 version of SHA3 using F1600 with XOF d=0x06.
21 /// Previous behavior (XOF d=0x01) is available in Keccak classes.
22 /// \details SHA3 is the base class for SHA3_224, SHA3_256, SHA3_384 and SHA3_512.
23 /// Library users should instantiate a derived class, and only use SHA3
24 /// as a base class reference or pointer.
25 /// \sa Keccak, SHA3_224, SHA3_256, SHA3_384 and SHA3_512.
26 /// \since Crypto++ 5.6.2
27 class SHA3 : public HashTransformation
28 {
29 public:
30 /// \brief Construct a SHA3
31 /// \param digestSize the digest size, in bytes
32 /// \details SHA3 is the base class for SHA3_224, SHA3_256, SHA3_384 and SHA3_512.
33 /// Library users should instantiate a derived class, and only use SHA3
34 /// as a base class reference or pointer.
35 SHA3(unsigned int digestSize) : m_digestSize(digestSize) {Restart();}
36 unsigned int DigestSize() const {return m_digestSize;}
37 std::string AlgorithmName() const {return "SHA3-" + IntToString(m_digestSize*8);}
38 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() { return "SHA3"; }
39 unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word64>();}
40
41 void Update(const byte *input, size_t length);
42 void Restart();
43 void TruncatedFinal(byte *hash, size_t size);
44
45 // unsigned int BlockSize() const { return r(); } // that's the idea behind it
46 protected:
47 inline unsigned int r() const {return 200 - 2 * m_digestSize;}
48
49 FixedSizeSecBlock<word64, 25> m_state;
50 unsigned int m_digestSize, m_counter;
51 };
52
53 /// \brief SHA3 message digest template
54 /// \tparam T_DigestSize the size of the digest, in bytes
55 /// \since Crypto++ 5.6.2
56 template<unsigned int T_DigestSize>
57 class SHA3_Final : public SHA3
58 {
59 public:
60 CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize)
61 CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE)
62
63 /// \brief Construct a SHA3-X message digest
64 SHA3_Final() : SHA3(DIGESTSIZE) {}
65 static std::string StaticAlgorithmName() { return "SHA3-" + IntToString(DIGESTSIZE * 8); }
66 unsigned int BlockSize() const { return BLOCKSIZE; }
67 private:
68 #if !defined(__BORLANDC__)
69 CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200); // ensure there was no underflow in the math
70 CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE > (int)T_DigestSize); // this is a general expectation by HMAC
71 #endif
72 };
73
74 /// \brief SHA3-224 message digest
75 /// \since Crypto++ 5.6.2
76 // typedef SHA3_Final<28> SHA3_224;
77 class SHA3_224 : public SHA3_Final<28>
78 {
79 };
80
81 /// \brief SHA3-256 message digest
82 /// \since Crypto++ 5.6.2
83 // typedef SHA3_Final<32> SHA3_256;
84 class SHA3_256 : public SHA3_Final<32>
85 {
86 };
87
88 /// \brief SHA3-384 message digest
89 /// \since Crypto++ 5.6.2
90 // typedef SHA3_Final<48> SHA3_384;
91 class SHA3_384 : public SHA3_Final<48>
92 {
93 };
94
95 /// \brief SHA3-512 message digest
96 /// \since Crypto++ 5.6.2
97 // typedef SHA3_Final<64> SHA3_512;
98 class SHA3_512 : public SHA3_Final<64>
99 {
100 };
101
102 NAMESPACE_END
103
104 #endif
SHA3
SHA3 message digest base class.
Definition: sha3.h:27
SHA3_Final
SHA3 message digest template.
Definition: sha3.h:57
SHA3_256
SHA3-256 message digest.
Definition: sha3.h:84
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
SHA3_224
SHA3-224 message digest.
Definition: sha3.h:77
SHA3::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: sha3.h:37
SHA3::SHA3
SHA3(unsigned int digestSize)
Construct a SHA3.
Definition: sha3.h:35
secblock.h
Classes and functions for secure memory allocations.
SHA3::OptimalDataAlignment
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: sha3.h:39
CRYPTOPP_COMPILE_ASSERT
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:144
SHA3_512
SHA3-512 message digest.
Definition: sha3.h:98
FixedSizeSecBlock< word64, 25 >
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1084
SHA3::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: sha3.h:36
IntToString
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:632
SHA3_Final::BlockSize
unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: sha3.h:66
CryptoPP
Crypto++ library namespace.
SHA3_384
SHA3-384 message digest.
Definition: sha3.h:91