|
| 1 | +// lsh.h - written and placed in the public domain by Jeffrey Walton |
| 2 | +// Based on the specification and source code provided by |
| 3 | +// Korea Internet & Security Agency (KISA) website. Also |
| 4 | +// see https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do |
| 5 | +// and https://seed.kisa.or.kr/kisa/Board/22/detailView.do. |
| 6 | + |
| 7 | +/// \file lsh.h |
| 8 | +/// \brief Classes for the LSH hash functions |
| 9 | +/// \since Crypto++ 8.6 |
| 10 | +/// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A> |
| 11 | +/// on the Korea Internet & Security Agency (KISA) website. |
| 12 | +#ifndef CRYPTOPP_LSH_H |
| 13 | +#define CRYPTOPP_LSH_H |
| 14 | + |
| 15 | +#include "cryptlib.h" |
| 16 | +#include "secblock.h" |
| 17 | + |
| 18 | +NAMESPACE_BEGIN(CryptoPP) |
| 19 | + |
| 20 | +/// \brief LSH-224 and LSH-256 hash base class |
| 21 | +/// \details LSH256_Base is the base class for both LSH-224 and LSH-256 |
| 22 | +/// \since Crypto++ 8.6 |
| 23 | +class LSH256_Base : public HashTransformation |
| 24 | +{ |
| 25 | +public: |
| 26 | + virtual ~LSH256_Base() {} |
| 27 | + |
| 28 | + unsigned int BlockSize() const { return m_blockSize; } |
| 29 | + unsigned int DigestSize() const { return m_digestSize; } |
| 30 | + unsigned int OptimalDataAlignment() const { return GetAlignmentOf<word32>(); } |
| 31 | + |
| 32 | + void Restart(); |
| 33 | + void Update(const byte *input, size_t length); |
| 34 | + void TruncatedFinal(byte *hash, size_t size); |
| 35 | + |
| 36 | + std::string AlgorithmProvider() const; |
| 37 | + |
| 38 | +protected: |
| 39 | + LSH256_Base(unsigned int algType, unsigned int digestSize, unsigned int blockSize) |
| 40 | + : m_algType(algType), m_digestSize(digestSize), m_blockSize(blockSize) {} |
| 41 | + |
| 42 | +protected: |
| 43 | + // Working state is: |
| 44 | + // * cv_l = 8 32-bit words |
| 45 | + // * cv_r = 8 32-bit words |
| 46 | + // * submsg_e_l = 8 32-bit words |
| 47 | + // * submsg_e_r = 8 32-bit words |
| 48 | + // * submsg_o_l = 8 32-bit words |
| 49 | + // * submsg_o_r = 8 32-bit words |
| 50 | + // * last_block = 32 32-bit words (128 bytes) |
| 51 | + FixedSizeSecBlock<word32, 80> m_state; |
| 52 | + word32 m_algType, m_remainingBitLength; |
| 53 | + word32 m_digestSize, m_blockSize; |
| 54 | +}; |
| 55 | + |
| 56 | +/// \brief LSH-224 hash function |
| 57 | +/// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A> |
| 58 | +/// on the Korea Internet & Security Agency (KISA) website. |
| 59 | +/// \since Crypto++ 8.6 |
| 60 | +class LSH224 : public LSH256_Base |
| 61 | +{ |
| 62 | +public: |
| 63 | + CRYPTOPP_CONSTANT(DIGESTSIZE = 28); |
| 64 | + CRYPTOPP_CONSTANT(BLOCKSIZE = 64); |
| 65 | + |
| 66 | + static std::string StaticAlgorithmName() { return "LSH-224"; } |
| 67 | + |
| 68 | + /// \brief Construct a LSH-224 |
| 69 | + /// \details LSH_TYPE_224 is the magic value 0x000001C defined in lsh.cpp. |
| 70 | + LSH224() : LSH256_Base(0x000001C, DIGESTSIZE, BLOCKSIZE) { Restart(); } |
| 71 | + |
| 72 | + std::string AlgorithmName() const { return StaticAlgorithmName(); } |
| 73 | +}; |
| 74 | + |
| 75 | +/// \brief LSH-256 hash function |
| 76 | +/// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A> |
| 77 | +/// on the Korea Internet & Security Agency (KISA) website. |
| 78 | +/// \since Crypto++ 8.6 |
| 79 | +class LSH256 : public LSH256_Base |
| 80 | +{ |
| 81 | +public: |
| 82 | + CRYPTOPP_CONSTANT(DIGESTSIZE = 32); |
| 83 | + CRYPTOPP_CONSTANT(BLOCKSIZE = 64); |
| 84 | + |
| 85 | + static std::string StaticAlgorithmName() { return "LSH-256"; } |
| 86 | + |
| 87 | + /// \brief Construct a LSH-256 |
| 88 | + /// \details LSH_TYPE_256 is the magic value 0x0000020 defined in lsh.cpp. |
| 89 | + LSH256() : LSH256_Base(0x0000020, DIGESTSIZE, BLOCKSIZE) { Restart(); } |
| 90 | + |
| 91 | + std::string AlgorithmName() const { return StaticAlgorithmName(); } |
| 92 | +}; |
| 93 | + |
| 94 | +/// \brief LSH-384 and LSH-512 hash base class |
| 95 | +/// \details LSH512_Base is the base class for both LSH-384 and LSH-512 |
| 96 | +/// \since Crypto++ 8.6 |
| 97 | +class LSH512_Base : public HashTransformation |
| 98 | +{ |
| 99 | +public: |
| 100 | + virtual ~LSH512_Base() {} |
| 101 | + |
| 102 | + unsigned int BlockSize() const { return m_blockSize; } |
| 103 | + unsigned int DigestSize() const { return m_digestSize; } |
| 104 | + unsigned int OptimalDataAlignment() const { return GetAlignmentOf<word64>(); } |
| 105 | + |
| 106 | + void Restart(); |
| 107 | + void Update(const byte *input, size_t length); |
| 108 | + void TruncatedFinal(byte *hash, size_t size); |
| 109 | + |
| 110 | + std::string AlgorithmProvider() const; |
| 111 | + |
| 112 | +protected: |
| 113 | + LSH512_Base(unsigned int algType, unsigned int digestSize, unsigned int blockSize) |
| 114 | + : m_algType(algType), m_digestSize(digestSize), m_blockSize(blockSize) {} |
| 115 | + |
| 116 | +protected: |
| 117 | + // Working state is: |
| 118 | + // * cv_l = 8 64-bit words |
| 119 | + // * cv_r = 8 64-bit words |
| 120 | + // * submsg_e_l = 8 64-bit words |
| 121 | + // * submsg_e_r = 8 64-bit words |
| 122 | + // * submsg_o_l = 8 64-bit words |
| 123 | + // * submsg_o_r = 8 64-bit words |
| 124 | + // * last_block = 32 64-bit words (256 bytes) |
| 125 | + FixedSizeSecBlock<word64, 80> m_state; |
| 126 | + word32 m_algType, m_remainingBitLength; |
| 127 | + word32 m_digestSize, m_blockSize; |
| 128 | +}; |
| 129 | + |
| 130 | +/// \brief LSH-384 hash function |
| 131 | +/// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A> |
| 132 | +/// on the Korea Internet & Security Agency (KISA) website. |
| 133 | +/// \since Crypto++ 8.6 |
| 134 | +class LSH384 : public LSH512_Base |
| 135 | +{ |
| 136 | +public: |
| 137 | + CRYPTOPP_CONSTANT(DIGESTSIZE = 48); |
| 138 | + CRYPTOPP_CONSTANT(BLOCKSIZE = 128); |
| 139 | + |
| 140 | + static std::string StaticAlgorithmName() { return "LSH-384"; } |
| 141 | + |
| 142 | + /// \brief Construct a LSH-384 |
| 143 | + /// \details LSH_TYPE_384 is the magic value 0x0010030 defined in lsh.cpp. |
| 144 | + LSH384() : LSH512_Base(0x0010030, DIGESTSIZE, BLOCKSIZE) { Restart(); } |
| 145 | + |
| 146 | + std::string AlgorithmName() const { return StaticAlgorithmName(); } |
| 147 | +}; |
| 148 | + |
| 149 | +/// \brief LSH-512 hash function |
| 150 | +/// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A> |
| 151 | +/// on the Korea Internet & Security Agency (KISA) website. |
| 152 | +/// \since Crypto++ 8.6 |
| 153 | +class LSH512 : public LSH512_Base |
| 154 | +{ |
| 155 | +public: |
| 156 | + CRYPTOPP_CONSTANT(DIGESTSIZE = 64); |
| 157 | + CRYPTOPP_CONSTANT(BLOCKSIZE = 128); |
| 158 | + |
| 159 | + static std::string StaticAlgorithmName() { return "LSH-512"; } |
| 160 | + |
| 161 | + /// \brief Construct a LSH-512 |
| 162 | + /// \details LSH_TYPE_512 is the magic value 0x0010040 defined in lsh.cpp. |
| 163 | + LSH512() : LSH512_Base(0x0010040, DIGESTSIZE, BLOCKSIZE) { Restart(); } |
| 164 | + |
| 165 | + std::string AlgorithmName() const { return StaticAlgorithmName(); } |
| 166 | +}; |
| 167 | + |
| 168 | +/// \brief LSH-512-256 hash function |
| 169 | +/// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A> |
| 170 | +/// on the Korea Internet & Security Agency (KISA) website. |
| 171 | +/// \since Crypto++ 8.6 |
| 172 | +class LSH512_256 : public LSH512_Base |
| 173 | +{ |
| 174 | +public: |
| 175 | + CRYPTOPP_CONSTANT(DIGESTSIZE = 32); |
| 176 | + CRYPTOPP_CONSTANT(BLOCKSIZE = 128); |
| 177 | + |
| 178 | + static std::string StaticAlgorithmName() { return "LSH-512-256"; } |
| 179 | + |
| 180 | + /// \brief Construct a LSH-512-256 |
| 181 | + /// \details LSH_TYPE_512_256 is the magic value 0x0010020 defined in lsh.cpp. |
| 182 | + LSH512_256() : LSH512_Base(0x0010020, DIGESTSIZE, BLOCKSIZE) { Restart(); } |
| 183 | + |
| 184 | + std::string AlgorithmName() const { return StaticAlgorithmName(); } |
| 185 | +}; |
| 186 | + |
| 187 | +NAMESPACE_END |
| 188 | + |
| 189 | +#endif // CRYPTOPP_LSH_H |
0 commit comments