Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // lsh.h - written and placed in the public domain by Jeffrey Walton

0002 //         Based on the specification and source code provided by

0003 //         Korea Internet & Security Agency (KISA) website. Also

0004 //         see https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do

0005 //         and https://seed.kisa.or.kr/kisa/Board/22/detailView.do.

0006 
0007 // We are hitting some sort of GCC bug in the LSH AVX2 code path.

0008 // Clang is OK on the AVX2 code path. We believe it is GCC Issue

0009 // 82735, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82735. It

0010 // makes using zeroupper a little tricky.

0011 
0012 /// \file lsh.h

0013 /// \brief Classes for the LSH hash functions

0014 /// \since Crypto++ 8.6

0015 /// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>

0016 ///  on the Korea Internet & Security Agency (KISA) website.

0017 #ifndef CRYPTOPP_LSH_H
0018 #define CRYPTOPP_LSH_H
0019 
0020 #include "cryptlib.h"
0021 #include "secblock.h"
0022 
0023 // Enable SSE2 and AVX2 for 64-bit machines.

0024 // 32-bit machines slow down with SSE2.

0025 #if (CRYPTOPP_BOOL_X32) || (CRYPTOPP_BOOL_X64)
0026 # define CRYPTOPP_ENABLE_64BIT_SSE 1
0027 #endif
0028 
0029 NAMESPACE_BEGIN(CryptoPP)
0030 
0031 /// \brief LSH-224 and LSH-256 hash base class

0032 /// \details LSH256_Base is the base class for both LSH-224 and LSH-256

0033 /// \since Crypto++ 8.6

0034 class LSH256_Base : public HashTransformation
0035 {
0036 public:
0037     /// \brief Block size, in bytes

0038     /// \details LSH_256 uses LSH256_MSG_BLK_BYTE_LEN for block size, which is 128

0039     CRYPTOPP_CONSTANT(BLOCKSIZE = 128);
0040 
0041     virtual ~LSH256_Base() {}
0042 
0043     unsigned int BlockSize() const { return BLOCKSIZE; }
0044     unsigned int DigestSize() const { return m_digestSize; }
0045     unsigned int OptimalDataAlignment() const { return GetAlignmentOf<word32>(); }
0046 
0047     void Restart();
0048     void Update(const byte *input, size_t size);
0049     void TruncatedFinal(byte *hash, size_t size);
0050 
0051     std::string AlgorithmProvider() const;
0052 
0053 protected:
0054     LSH256_Base(unsigned int algType, unsigned int digestSize)
0055         : m_digestSize(digestSize) { m_state[80] = algType; }
0056 
0057 protected:
0058     // Working state is:

0059     //   * cv_l = 8 32-bit words

0060     //   * cv_r = 8 32-bit words

0061     //   * submsg_e_l = 8 32-bit words

0062     //   * submsg_e_r = 8 32-bit words

0063     //   * submsg_o_l = 8 32-bit words

0064     //   * submsg_o_r = 8 32-bit words

0065     //   * last_block = 32 32-bit words (128 bytes)

0066     //   * algType

0067     //   * remainingBitLength

0068     FixedSizeSecBlock<word32, 80+2> m_state;
0069     // word32 m_algType, m_remainingBitLength;

0070     word32 m_digestSize;
0071 };
0072 
0073 /// \brief LSH-224 hash function

0074 /// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>

0075 ///  on the Korea Internet & Security Agency (KISA) website.

0076 /// \since Crypto++ 8.6

0077 class LSH224 : public LSH256_Base
0078 {
0079 public:
0080     /// \brief Digest size, in bytes

0081     /// \details LSH_256 uses LSH_GET_HASHBYTE(algType) for digest size, which is 28

0082     CRYPTOPP_CONSTANT(DIGESTSIZE = 28);
0083     /// \brief Block size, in bytes

0084     /// \details LSH_256 uses LSH256_MSG_BLK_BYTE_LEN for block size, which is 128

0085     CRYPTOPP_CONSTANT(BLOCKSIZE = LSH256_Base::BLOCKSIZE);
0086 
0087     /// \brief The algorithm's name

0088     /// \return the standard algorithm name

0089     /// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.

0090     ///  Some algorithms do not have standard names yet. For example, there is no standard

0091     ///  algorithm name for Shoup's ECIES.

0092     /// \note StaticAlgorithmName is not universally implemented yet.

0093     static std::string StaticAlgorithmName() { return "LSH-224"; }
0094 
0095     /// \brief Construct a LSH-224

0096     /// \details LSH_TYPE_224 is the magic value 0x000001C defined in lsh.cpp.

0097     LSH224() : LSH256_Base(0x000001C, DIGESTSIZE) { Restart(); }
0098 
0099     std::string AlgorithmName() const { return StaticAlgorithmName(); }
0100 };
0101 
0102 /// \brief LSH-256 hash function

0103 /// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>

0104 ///  on the Korea Internet & Security Agency (KISA) website.

0105 /// \since Crypto++ 8.6

0106 class LSH256 : public LSH256_Base
0107 {
0108 public:
0109     /// \brief Digest size, in bytes

0110     /// \details LSH_256 uses LSH_GET_HASHBYTE(algType) for digest size, which is 32

0111     CRYPTOPP_CONSTANT(DIGESTSIZE = 32);
0112     /// \brief Block size, in bytes

0113     /// \details LSH_256 uses LSH256_MSG_BLK_BYTE_LEN for block size, which is 128

0114     CRYPTOPP_CONSTANT(BLOCKSIZE = LSH256_Base::BLOCKSIZE);
0115 
0116     /// \brief The algorithm's name

0117     /// \return the standard algorithm name

0118     /// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.

0119     ///  Some algorithms do not have standard names yet. For example, there is no standard

0120     ///  algorithm name for Shoup's ECIES.

0121     /// \note StaticAlgorithmName is not universally implemented yet.

0122     static std::string StaticAlgorithmName() { return "LSH-256"; }
0123 
0124     /// \brief Construct a LSH-256

0125     /// \details LSH_TYPE_256 is the magic value 0x0000020 defined in lsh.cpp.

0126     LSH256() : LSH256_Base(0x0000020, DIGESTSIZE) { Restart(); }
0127 
0128     std::string AlgorithmName() const { return StaticAlgorithmName(); }
0129 };
0130 
0131 /// \brief LSH-384 and LSH-512 hash base class

0132 /// \details LSH512_Base is the base class for both LSH-384 and LSH-512

0133 /// \since Crypto++ 8.6

0134 class LSH512_Base : public HashTransformation
0135 {
0136 public:
0137     /// \brief Block size, in bytes

0138     /// \details LSH_512 uses LSH512_MSG_BLK_BYTE_LEN for block size, which is 256

0139     CRYPTOPP_CONSTANT(BLOCKSIZE = 256);
0140 
0141     virtual ~LSH512_Base() {}
0142 
0143     unsigned int BlockSize() const { return BLOCKSIZE; }
0144     unsigned int DigestSize() const { return m_digestSize; }
0145     unsigned int OptimalDataAlignment() const { return GetAlignmentOf<word64>(); }
0146 
0147     void Restart();
0148     void Update(const byte *input, size_t size);
0149     void TruncatedFinal(byte *hash, size_t size);
0150 
0151     std::string AlgorithmProvider() const;
0152 
0153 protected:
0154     LSH512_Base(unsigned int algType, unsigned int digestSize)
0155         : m_digestSize(digestSize) { m_state[80] = algType; }
0156 
0157 protected:
0158     // Working state is:

0159     //   * cv_l = 8 64-bit words

0160     //   * cv_r = 8 64-bit words

0161     //   * submsg_e_l = 8 64-bit words

0162     //   * submsg_e_r = 8 64-bit words

0163     //   * submsg_o_l = 8 64-bit words

0164     //   * submsg_o_r = 8 64-bit words

0165     //   * last_block = 32 64-bit words (256 bytes)

0166     //   * algType

0167     //   * remainingBitLength

0168     FixedSizeSecBlock<word64, 80+2> m_state;
0169     // word32 m_algType, m_remainingBitLength;

0170     word32 m_digestSize;
0171 };
0172 
0173 /// \brief LSH-384 hash function

0174 /// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>

0175 ///  on the Korea Internet & Security Agency (KISA) website.

0176 /// \since Crypto++ 8.6

0177 class LSH384 : public LSH512_Base
0178 {
0179 public:
0180     /// \brief Digest size, in bytes

0181     /// \details LSH_512 uses LSH_GET_HASHBYTE(algType) for digest size, which is 48

0182     CRYPTOPP_CONSTANT(DIGESTSIZE = 48);
0183     /// \brief Block size, in bytes

0184     /// \details LSH_512 uses LSH512_MSG_BLK_BYTE_LEN for block size, which is 256

0185     CRYPTOPP_CONSTANT(BLOCKSIZE = LSH512_Base::BLOCKSIZE);
0186 
0187     /// \brief The algorithm's name

0188     /// \return the standard algorithm name

0189     /// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.

0190     ///  Some algorithms do not have standard names yet. For example, there is no standard

0191     ///  algorithm name for Shoup's ECIES.

0192     /// \note StaticAlgorithmName is not universally implemented yet.

0193     static std::string StaticAlgorithmName() { return "LSH-384"; }
0194 
0195     /// \brief Construct a LSH-384

0196     /// \details LSH_TYPE_384 is the magic value 0x0010030 defined in lsh.cpp.

0197     LSH384() : LSH512_Base(0x0010030, DIGESTSIZE) { Restart(); }
0198 
0199     std::string AlgorithmName() const { return StaticAlgorithmName(); }
0200 };
0201 
0202 /// \brief LSH-512 hash function

0203 /// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>

0204 ///  on the Korea Internet & Security Agency (KISA) website.

0205 /// \since Crypto++ 8.6

0206 class LSH512 : public LSH512_Base
0207 {
0208 public:
0209     /// \brief Digest size, in bytes

0210     /// \details LSH_512 uses LSH_GET_HASHBYTE(algType) for digest size, which is 64

0211     CRYPTOPP_CONSTANT(DIGESTSIZE = 64);
0212     /// \brief Block size, in bytes

0213     /// \details LSH_512 uses LSH512_MSG_BLK_BYTE_LEN for block size, which is 256

0214     CRYPTOPP_CONSTANT(BLOCKSIZE = LSH512_Base::BLOCKSIZE);
0215 
0216     /// \brief The algorithm's name

0217     /// \return the standard algorithm name

0218     /// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.

0219     ///  Some algorithms do not have standard names yet. For example, there is no standard

0220     ///  algorithm name for Shoup's ECIES.

0221     /// \note StaticAlgorithmName is not universally implemented yet.

0222     static std::string StaticAlgorithmName() { return "LSH-512"; }
0223 
0224     /// \brief Construct a LSH-512

0225     /// \details LSH_TYPE_512 is the magic value 0x0010040 defined in lsh.cpp.

0226     LSH512() : LSH512_Base(0x0010040, DIGESTSIZE) { Restart(); }
0227 
0228     std::string AlgorithmName() const { return StaticAlgorithmName(); }
0229 };
0230 
0231 /// \brief LSH-512-256 hash function

0232 /// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>

0233 ///  on the Korea Internet & Security Agency (KISA) website.

0234 /// \since Crypto++ 8.6

0235 class LSH512_256 : public LSH512_Base
0236 {
0237 public:
0238     /// \brief Digest size, in bytes

0239     /// \details LSH_512 uses LSH_GET_HASHBYTE(algType) for digest size, which is 32

0240     CRYPTOPP_CONSTANT(DIGESTSIZE = 32);
0241     /// \brief Block size, in bytes

0242     /// \details LSH_512 uses LSH512_MSG_BLK_BYTE_LEN for block size, which is 256

0243     CRYPTOPP_CONSTANT(BLOCKSIZE = LSH512_Base::BLOCKSIZE);
0244 
0245     /// \brief The algorithm's name

0246     /// \return the standard algorithm name

0247     /// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.

0248     ///  Some algorithms do not have standard names yet. For example, there is no standard

0249     ///  algorithm name for Shoup's ECIES.

0250     /// \note StaticAlgorithmName is not universally implemented yet.

0251     static std::string StaticAlgorithmName() { return "LSH-512-256"; }
0252 
0253     /// \brief Construct a LSH-512-256

0254     /// \details LSH_TYPE_512_256 is the magic value 0x0010020 defined in lsh.cpp.

0255     LSH512_256() : LSH512_Base(0x0010020, DIGESTSIZE) { Restart(); }
0256 
0257     std::string AlgorithmName() const { return StaticAlgorithmName(); }
0258 };
0259 
0260 NAMESPACE_END
0261 
0262 #endif  // CRYPTOPP_LSH_H