Crypto++ 8.2 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 #include "misc.h"
17
18 NAMESPACE_BEGIN(CryptoPP)
19
20 /// \brief SHA3 message digest base class
21 /// \details The Crypto++ implementation conforms to FIPS 202 version of SHA3 using F1600 with XOF d=0x06.
22 /// Previous behavior (XOF d=0x01) is available in Keccak classes.
23 /// \details SHA3 is the base class for SHA3_224, SHA3_256, SHA3_384 and SHA3_512.
24 /// Library users should instantiate a derived class, and only use SHA3
25 /// as a base class reference or pointer.
26 /// \sa Keccak, SHA3_224, SHA3_256, SHA3_384 and SHA3_512.
27 /// \since Crypto++ 5.6.2
28 class SHA3 : public HashTransformation
29 {
30 protected:
31 /// \brief Construct a SHA3
32 /// \param digestSize the digest size, in bytes
33 /// \details SHA3 is the base class for SHA3_224, SHA3_256, SHA3_384 and SHA3_512.
34 /// Library users should instantiate a derived class, and only use SHA3
35 /// as a base class reference or pointer.
36 /// \details This constructor was moved to protected at Crypto++ 8.1
37 /// because users were attempting to create Keccak objects with it.
38 /// \since Crypto++ 5.6.2
39 SHA3(unsigned int digestSize) : m_digestSize(digestSize) {Restart();}
40
41 public:
42 unsigned int DigestSize() const {return m_digestSize;}
43 unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word64>();}
44
45 void Update(const byte *input, size_t length);
46 void Restart();
47 void TruncatedFinal(byte *hash, size_t size);
48
49 protected:
50 inline unsigned int r() const {return BlockSize();}
51
52 FixedSizeSecBlock<word64, 25> m_state;
53 unsigned int m_digestSize, m_counter;
54 };
55
56 /// \brief SHA3 message digest template
57 /// \tparam T_DigestSize the size of the digest, in bytes
58 /// \since Crypto++ 5.6.2
59 template<unsigned int T_DigestSize>
60 class SHA3_Final : public SHA3
61 {
62 public:
63 CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize);
64 CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE);
65 static std::string StaticAlgorithmName()
66 { return "SHA3-" + IntToString(DIGESTSIZE * 8); }
67
68 /// \brief Construct a SHA3-X message digest
69 SHA3_Final() : SHA3(DIGESTSIZE) {}
70
71 /// \brief Provides the block size of the compression function
72 /// \return block size of the compression function, in bytes
73 /// \details BlockSize() will return 0 if the hash is not block based
74 /// or does not have an equivalent block size. For example, Keccak
75 /// and SHA-3 do not have a block size, but they do have an equivalent
76 /// block size called rate expressed as <tt>r</tt>.
77 unsigned int BlockSize() const { return BLOCKSIZE; }
78
79 std::string AlgorithmName() const { return StaticAlgorithmName(); }
80
81 private:
82 #if !defined(__BORLANDC__)
83 // ensure there was no underflow in the math
84 CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200);
85 #endif
86 };
87
88 /// \brief SHA3-224 message digest
89 /// \since Crypto++ 5.6.2
90 class SHA3_224 : public SHA3_Final<28> {};
91
92 /// \brief SHA3-256 message digest
93 /// \since Crypto++ 5.6.2
94 class SHA3_256 : public SHA3_Final<32> {};
95
96 /// \brief SHA3-384 message digest
97 /// \since Crypto++ 5.6.2
98 class SHA3_384 : public SHA3_Final<48> {};
99
100 /// \brief SHA3-512 message digest
101 /// \since Crypto++ 5.6.2
102 class SHA3_512 : public SHA3_Final<64> {};
103
104 NAMESPACE_END
105
106 #endif
misc.h
Utility functions for the Crypto++ library.
SHA3
SHA3 message digest base class.
Definition: sha3.h:28
SHA3_Final::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: sha3.h:79
SHA3_Final
SHA3 message digest template.
Definition: sha3.h:60
SHA3_256
SHA3-256 message digest.
Definition: sha3.h:94
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
SHA3_Final::SHA3_Final
SHA3_Final()
Construct a SHA3-X message digest.
Definition: sha3.h:69
SHA3_224
SHA3-224 message digest.
Definition: sha3.h:90
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:43
CRYPTOPP_COMPILE_ASSERT
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:149
SHA3_512
SHA3-512 message digest.
Definition: sha3.h:102
FixedSizeSecBlock< word64, 25 >
Name::BlockSize
const char * BlockSize()
int, in bytes
Definition: argnames.h:27
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1112
SHA3::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: sha3.h:42
IntToString
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:704
SHA3_Final::BlockSize
unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: sha3.h:77
CryptoPP
Crypto++ library namespace.
SHA3_384
SHA3-384 message digest.
Definition: sha3.h:98