Crypto++  5.6.5
Free C++ class library of cryptographic schemes
sha3.h
Go to the documentation of this file.
1 // sha3.h - 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 //! \class SHA3
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 public:
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  SHA3(unsigned int digestSize) : m_digestSize(digestSize) {Restart();}
37  unsigned int DigestSize() const {return m_digestSize;}
38  std::string AlgorithmName() const {return "SHA3-" + IntToString(m_digestSize*8);}
39  CRYPTOPP_CONSTEXPR static const char* StaticAlgorithmName() { return "SHA3"; }
40  unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word64>();}
41 
42  void Update(const byte *input, size_t length);
43  void Restart();
44  void TruncatedFinal(byte *hash, size_t size);
45 
46  // unsigned int BlockSize() const { return r(); } // that's the idea behind it
47 protected:
48  inline unsigned int r() const {return 200 - 2 * m_digestSize;}
49 
51  unsigned int m_digestSize, m_counter;
52 };
53 
54 //! \class SHA3_224
55 //! \tparam DigestSize controls the digest size as a template parameter instead of a per-class constant
56 //! \brief SHA3-X message digest, template for more fine-grained typedefs
57 //! \since Crypto++ 5.7.0
58 template<unsigned int T_DigestSize>
59 class SHA3_Final : public SHA3
60 {
61 public:
62  CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize)
63  CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE)
64 
65  //! \brief Construct a SHA3-X message digest
66  SHA3_Final() : SHA3(DIGESTSIZE) {}
67  static std::string StaticAlgorithmName() { return "SHA3-" + IntToString(DIGESTSIZE * 8); }
68  unsigned int BlockSize() const { return BLOCKSIZE; }
69 private:
70  CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200); // ensure there was no underflow in the math
71  CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE > (int)T_DigestSize); // this is a general expectation by HMAC
72 };
73 
74 //! \class SHA3_224
75 //! \brief SHA3-224 message digest
76 //! \since Crypto++ 5.6.2
77 typedef SHA3_Final<28> SHA3_224;
78 //! \class SHA3_256
79 //! \brief SHA3-256 message digest
80 //! \since Crypto++ 5.6.2
81 typedef SHA3_Final<32> SHA3_256;
82 //! \class SHA3_384
83 //! \brief SHA3-384 message digest
84 //! \since Crypto++ 5.6.2
85 typedef SHA3_Final<48> SHA3_384;
86 //! \class SHA3_512
87 //! \brief SHA3-512 message digest
88 //! \since Crypto++ 5.6.2
89 typedef SHA3_Final<64> SHA3_512;
90 
91 NAMESPACE_END
92 
93 #endif
SHA3 message digest base class.
Definition: sha3.h:28
SHA3-256 message digest.
Abstract base classes that provide a uniform interface to this library.
SHA3-X message digest, template for more fine-grained typedefs.
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: sha3.h:38
SHA3(unsigned int digestSize)
Construct a SHA3.
Definition: sha3.h:36
Classes and functions for secure memory allocations.
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: sha3.h:40
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:123
SHA3-512 message digest.
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:931
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: sha3.h:37
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:533
unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: sha3.h:68
Crypto++ library namespace.
SHA3-384 message digest.