2017-01-27 12:05:45 +00:00
|
|
|
// sha.h - originally written and placed in the public domain by Wei Dai
|
2015-11-18 20:32:28 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \file sha.h
|
|
|
|
/// \brief Classes for SHA-1 and SHA-2 family of message digests
|
2018-03-23 02:16:42 +00:00
|
|
|
/// \since SHA1 since Crypto++ 1.0, SHA2 since Crypto++ 4.0, ARMv8 SHA since
|
|
|
|
/// Crypto++ 6.0, Intel SHA since Crypto++ 6.0, Power8 SHA since Crypto++ 6.1
|
2015-11-18 20:32:28 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#ifndef CRYPTOPP_SHA_H
|
|
|
|
#define CRYPTOPP_SHA_H
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "iterhash.h"
|
|
|
|
|
2018-01-28 16:34:57 +00:00
|
|
|
#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32)
|
2017-08-21 01:25:29 +00:00
|
|
|
# define SHA_X86_ALIGN16 true
|
|
|
|
#else
|
|
|
|
# define SHA_X86_ALIGN16 false
|
2015-12-25 08:37:14 +00:00
|
|
|
#endif
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief SHA-1 message digest
|
|
|
|
/// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-1">SHA-1</a>
|
2018-03-23 02:16:42 +00:00
|
|
|
/// \since SHA1 since Crypto++ 1.0, SHA2 since Crypto++ 4.0, ARMv8 SHA since
|
|
|
|
/// Crypto++ 6.0, Intel SHA since Crypto++ 6.0
|
2015-11-05 06:59:46 +00:00
|
|
|
class CRYPTOPP_DLL SHA1 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 20, SHA1>
|
|
|
|
{
|
|
|
|
public:
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Initialize state array
|
|
|
|
/// \param state the state of the hash
|
|
|
|
/// \details InitState sets a state array to SHA1 initial values
|
|
|
|
/// \details Hashes which derive from IteratedHashWithStaticTransform provide static
|
|
|
|
/// member functions InitState and Transform. External classes, like SEAL and MDC,
|
|
|
|
/// can initialize state with a user provided key and operate the hash on the data
|
|
|
|
/// with the user supplied state.
|
|
|
|
/// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
|
2015-11-05 06:59:46 +00:00
|
|
|
static void CRYPTOPP_API InitState(HashWordType *state);
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Operate the hash
|
|
|
|
/// \param digest the state of the hash
|
|
|
|
/// \param data the data to be digested
|
|
|
|
/// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
|
|
|
|
/// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
|
|
|
|
/// or updated state.
|
|
|
|
/// \details Hashes which derive from IteratedHashWithStaticTransform provide static
|
|
|
|
/// member functions InitState and Transform. External classes, like SEAL and MDC,
|
|
|
|
/// can initialize state with a user provided key and operate the hash on the data
|
|
|
|
/// with the user supplied state.
|
|
|
|
/// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
|
2017-08-13 20:05:39 +00:00
|
|
|
static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief The algorithm name
|
|
|
|
/// \returns C-style string "SHA-1"
|
2016-12-01 14:37:04 +00:00
|
|
|
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-1";}
|
2017-08-05 04:24:02 +00:00
|
|
|
|
2017-08-13 20:05:39 +00:00
|
|
|
protected:
|
|
|
|
size_t HashMultipleBlocks(const HashWordType *input, size_t length);
|
2015-11-05 06:59:46 +00:00
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief SHA-256 message digest
|
|
|
|
/// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-256">SHA-256</a>
|
2018-03-23 02:16:42 +00:00
|
|
|
/// \since SHA2 since Crypto++ 4.0, ARMv8 SHA since Crypto++ 6.0,
|
|
|
|
/// Intel SHA since Crypto++ 6.0, Power8 SHA since Crypto++ 6.1
|
2015-11-18 20:32:28 +00:00
|
|
|
class CRYPTOPP_DLL SHA256 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 32, SHA256, 32, true>
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Initialize state array
|
|
|
|
/// \param state the state of the hash
|
|
|
|
/// \details InitState sets a state array to SHA256 initial values
|
|
|
|
/// \details Hashes which derive from IteratedHashWithStaticTransform provide static
|
|
|
|
/// member functions InitState and Transform. External classes, like SEAL and MDC,
|
|
|
|
/// can initialize state with a user provided key and operate the hash on the data
|
|
|
|
/// with the user supplied state.
|
|
|
|
/// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
|
2015-11-05 06:59:46 +00:00
|
|
|
static void CRYPTOPP_API InitState(HashWordType *state);
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Operate the hash
|
|
|
|
/// \param digest the state of the hash
|
|
|
|
/// \param data the data to be digested
|
|
|
|
/// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
|
|
|
|
/// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
|
|
|
|
/// or updated state.
|
|
|
|
/// \details Hashes which derive from IteratedHashWithStaticTransform provide static
|
|
|
|
/// member functions InitState and Transform. External classes, like SEAL and MDC,
|
|
|
|
/// can initialize state with a user provided key and operate the hash on the data
|
|
|
|
/// with the user supplied state.
|
|
|
|
/// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
|
2017-08-13 20:05:39 +00:00
|
|
|
static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief The algorithm name
|
|
|
|
/// \returns C-style string "SHA-256"
|
2016-12-01 14:37:04 +00:00
|
|
|
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-256";}
|
2017-08-05 04:24:02 +00:00
|
|
|
|
2017-08-13 20:05:39 +00:00
|
|
|
protected:
|
|
|
|
size_t HashMultipleBlocks(const HashWordType *input, size_t length);
|
2015-11-05 06:59:46 +00:00
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief SHA-224 message digest
|
|
|
|
/// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-224">SHA-224</a>
|
2018-03-23 02:16:42 +00:00
|
|
|
/// \since SHA2 since Crypto++ 4.0, ARMv8 SHA since Crypto++ 6.0,
|
|
|
|
/// Intel SHA since Crypto++ 6.0, Power8 SHA since Crypto++ 6.1
|
2015-11-18 20:32:28 +00:00
|
|
|
class CRYPTOPP_DLL SHA224 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 32, SHA224, 28, true>
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Initialize state array
|
|
|
|
/// \param state the state of the hash
|
|
|
|
/// \details InitState sets a state array to SHA224 initial values
|
|
|
|
/// \details Hashes which derive from IteratedHashWithStaticTransform provide static
|
|
|
|
/// member functions InitState and Transform. External classes, like SEAL and MDC,
|
|
|
|
/// can initialize state with a user provided key and operate the hash on the data
|
|
|
|
/// with the user supplied state.
|
|
|
|
/// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
|
2015-11-05 06:59:46 +00:00
|
|
|
static void CRYPTOPP_API InitState(HashWordType *state);
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Operate the hash
|
|
|
|
/// \param digest the state of the hash
|
|
|
|
/// \param data the data to be digested
|
|
|
|
/// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
|
|
|
|
/// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
|
|
|
|
/// or updated state.
|
|
|
|
/// \details Hashes which derive from IteratedHashWithStaticTransform provide static
|
|
|
|
/// member functions InitState and Transform. External classes, like SEAL and MDC,
|
|
|
|
/// can initialize state with a user provided key and operate the hash on the data
|
|
|
|
/// with the user supplied state.
|
|
|
|
/// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
|
2017-08-13 20:05:39 +00:00
|
|
|
static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data) {SHA256::Transform(digest, data);}
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief The algorithm name
|
|
|
|
/// \returns C-style string "SHA-224"
|
2016-12-01 14:37:04 +00:00
|
|
|
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-224";}
|
2017-08-05 04:24:02 +00:00
|
|
|
|
2017-08-13 20:05:39 +00:00
|
|
|
protected:
|
|
|
|
size_t HashMultipleBlocks(const HashWordType *input, size_t length);
|
2015-11-05 06:59:46 +00:00
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief SHA-512 message digest
|
|
|
|
/// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-512">SHA-512</a>
|
2018-03-23 02:16:42 +00:00
|
|
|
/// \since SHA2 since Crypto++ 4.0, Power8 SHA since Crypto++ 6.1
|
2017-08-21 01:25:29 +00:00
|
|
|
class CRYPTOPP_DLL SHA512 : public IteratedHashWithStaticTransform<word64, BigEndian, 128, 64, SHA512, 64, SHA_X86_ALIGN16>
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Initialize state array
|
|
|
|
/// \param state the state of the hash
|
|
|
|
/// \details InitState sets a state array to SHA512 initial values
|
|
|
|
/// \details Hashes which derive from IteratedHashWithStaticTransform provide static
|
|
|
|
/// member functions InitState and Transform. External classes, like SEAL and MDC,
|
|
|
|
/// can initialize state with a user provided key and operate the hash on the data
|
|
|
|
/// with the user supplied state.
|
|
|
|
/// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
|
2015-11-05 06:59:46 +00:00
|
|
|
static void CRYPTOPP_API InitState(HashWordType *state);
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Operate the hash
|
|
|
|
/// \param digest the state of the hash
|
|
|
|
/// \param data the data to be digested
|
|
|
|
/// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
|
|
|
|
/// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
|
|
|
|
/// or updated state.
|
|
|
|
/// \details Hashes which derive from IteratedHashWithStaticTransform provide static
|
|
|
|
/// member functions InitState and Transform. External classes, like SEAL and MDC,
|
|
|
|
/// can initialize state with a user provided key and operate the hash on the data
|
|
|
|
/// with the user supplied state.
|
|
|
|
/// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
|
2017-08-13 20:05:39 +00:00
|
|
|
static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief The algorithm name
|
|
|
|
/// \returns C-style string "SHA-512"
|
2016-12-01 14:37:04 +00:00
|
|
|
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-512";}
|
2015-11-05 06:59:46 +00:00
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief SHA-384 message digest
|
|
|
|
/// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-384">SHA-384</a>
|
2018-03-23 02:16:42 +00:00
|
|
|
/// \since SHA2 since Crypto++ 4.0, Power8 SHA since Crypto++ 6.1
|
2017-08-21 01:25:29 +00:00
|
|
|
class CRYPTOPP_DLL SHA384 : public IteratedHashWithStaticTransform<word64, BigEndian, 128, 64, SHA384, 48, SHA_X86_ALIGN16>
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Initialize state array
|
|
|
|
/// \param state the state of the hash
|
|
|
|
/// \details InitState sets a state array to SHA384 initial values
|
|
|
|
/// \details Hashes which derive from IteratedHashWithStaticTransform provide static
|
|
|
|
/// member functions InitState and Transform. External classes, like SEAL and MDC,
|
|
|
|
/// can initialize state with a user provided key and operate the hash on the data
|
|
|
|
/// with the user supplied state.
|
|
|
|
/// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
|
2015-11-05 06:59:46 +00:00
|
|
|
static void CRYPTOPP_API InitState(HashWordType *state);
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Operate the hash
|
|
|
|
/// \param digest the state of the hash
|
|
|
|
/// \param data the data to be digested
|
|
|
|
/// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
|
|
|
|
/// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
|
|
|
|
/// or updated state.
|
|
|
|
/// \details Hashes which derive from IteratedHashWithStaticTransform provide static
|
|
|
|
/// member functions InitState and Transform. External classes, like SEAL and MDC,
|
|
|
|
/// can initialize state with a user provided key and operate the hash on the data
|
|
|
|
/// with the user supplied state.
|
|
|
|
/// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
|
2017-08-13 20:05:39 +00:00
|
|
|
static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data) {SHA512::Transform(digest, data);}
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief The algorithm name
|
|
|
|
/// \returns C-style string "SHA-384"
|
2016-12-01 14:37:04 +00:00
|
|
|
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-384";}
|
2015-11-05 06:59:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|