Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:09

0001 // sha3.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file sha3.h

0004 /// \brief Classes for SHA3 message digests

0005 /// \details The Crypto++ implementation conforms to the FIPS 202 version of SHA3 using F1600 with XOF d=0x06.

0006 ///   Previous behavior (XOF d=0x01) is available in Keccak classes.

0007 /// \sa <a href="http://en.wikipedia.org/wiki/SHA-3">SHA-3</a>,

0008 ///   <A HREF="http://csrc.nist.gov/groups/ST/hash/sha-3/fips202_standard_2015.html">SHA-3 STANDARD (FIPS 202)</A>.

0009 /// \since Crypto++ 5.6.2

0010 
0011 #ifndef CRYPTOPP_SHA3_H
0012 #define CRYPTOPP_SHA3_H
0013 
0014 #include "cryptlib.h"
0015 #include "secblock.h"
0016 #include "misc.h"
0017 
0018 NAMESPACE_BEGIN(CryptoPP)
0019 
0020 /// \brief SHA3 message digest base class

0021 /// \details The Crypto++ implementation conforms to FIPS 202 version of SHA3 using F1600 with XOF d=0x06.

0022 ///   Previous behavior (XOF d=0x01) is available in Keccak classes.

0023 /// \details SHA3 is the base class for SHA3_224, SHA3_256, SHA3_384 and SHA3_512.

0024 ///   Library users should instantiate a derived class, and only use SHA3

0025 ///   as a base class reference or pointer.

0026 /// \sa Keccak, SHA3_224, SHA3_256, SHA3_384 and SHA3_512.

0027 /// \since Crypto++ 5.6.2

0028 class SHA3 : public HashTransformation
0029 {
0030 protected:
0031     /// \brief Construct a SHA3

0032     /// \param digestSize the digest size, in bytes

0033     /// \details SHA3 is the base class for SHA3_224, SHA3_256, SHA3_384 and SHA3_512.

0034     ///   Library users should instantiate a derived class, and only use SHA3

0035     ///   as a base class reference or pointer.

0036     /// \details This constructor was moved to protected at Crypto++ 8.1

0037     ///   because users were attempting to create Keccak objects with it.

0038     /// \since Crypto++ 5.6.2

0039     SHA3(unsigned int digestSize) : m_digestSize(digestSize) {Restart();}
0040 
0041 public:
0042     unsigned int DigestSize() const {return m_digestSize;}
0043     unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word64>();}
0044 
0045     void Update(const byte *input, size_t length);
0046     void Restart();
0047     void TruncatedFinal(byte *hash, size_t size);
0048 
0049 protected:
0050     inline unsigned int r() const {return BlockSize();}
0051 
0052     FixedSizeSecBlock<word64, 25> m_state;
0053     unsigned int m_digestSize, m_counter;
0054 };
0055 
0056 /// \brief SHA3 message digest template

0057 /// \tparam T_DigestSize the size of the digest, in bytes

0058 /// \since Crypto++ 5.6.2

0059 template<unsigned int T_DigestSize>
0060 class SHA3_Final : public SHA3
0061 {
0062 public:
0063     CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize);
0064     CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE);
0065     static std::string StaticAlgorithmName()
0066         { return "SHA3-" + IntToString(DIGESTSIZE * 8); }
0067 
0068     /// \brief Construct a SHA3-X message digest

0069     SHA3_Final() : SHA3(DIGESTSIZE) {}
0070 
0071     /// \brief Provides the block size of the compression function

0072     /// \return block size of the compression function, in bytes

0073     /// \details BlockSize() will return 0 if the hash is not block based

0074     ///   or does not have an equivalent block size. For example, Keccak

0075     ///   and SHA-3 do not have a block size, but they do have an equivalent

0076     ///   block size called rate expressed as <tt>r</tt>.

0077     unsigned int BlockSize() const { return BLOCKSIZE; }
0078 
0079     std::string AlgorithmName() const { return StaticAlgorithmName(); }
0080 
0081 private:
0082 #if !defined(__BORLANDC__)
0083     // ensure there was no underflow in the math

0084     CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200);
0085 #endif
0086 };
0087 
0088 /// \brief SHA3-224 message digest

0089 /// \since Crypto++ 5.6.2

0090 class SHA3_224 : public SHA3_Final<28> {};
0091 
0092 /// \brief SHA3-256 message digest

0093 /// \since Crypto++ 5.6.2

0094 class SHA3_256 : public SHA3_Final<32> {};
0095 
0096 /// \brief SHA3-384 message digest

0097 /// \since Crypto++ 5.6.2

0098 class SHA3_384 : public SHA3_Final<48> {};
0099 
0100 /// \brief SHA3-512 message digest

0101 /// \since Crypto++ 5.6.2

0102 class SHA3_512 : public SHA3_Final<64> {};
0103 
0104 NAMESPACE_END
0105 
0106 #endif