Security Scol plugin
lsh.h
Go to the documentation of this file.
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// We are hitting some sort of GCC bug in the LSH AVX2 code path.
8// Clang is OK on the AVX2 code path. We believe it is GCC Issue
9// 82735, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82735. It
10// makes using zeroupper a little tricky.
11
17#ifndef CRYPTOPP_LSH_H
18#define CRYPTOPP_LSH_H
19
20#include "cryptlib.h"
21#include "secblock.h"
22
23// Enable SSE2 and AVX2 for 64-bit machines.
24// 32-bit machines slow down with SSE2.
25#if (CRYPTOPP_BOOL_X32) || (CRYPTOPP_BOOL_X64)
26# define CRYPTOPP_ENABLE_64BIT_SSE 1
27#endif
28
29NAMESPACE_BEGIN(CryptoPP)
30
31
35{
36public:
39 CRYPTOPP_CONSTANT(BLOCKSIZE = 128);
40
41 virtual ~LSH256_Base() {}
42
43 unsigned int BlockSize() const { return BLOCKSIZE; }
44 unsigned int DigestSize() const { return m_digestSize; }
45 unsigned int OptimalDataAlignment() const { return GetAlignmentOf<word32>(); }
46
47 void Restart();
48 void Update(const byte *input, size_t size);
49 void TruncatedFinal(byte *hash, size_t size);
50
51 std::string AlgorithmProvider() const;
52
53protected:
54 LSH256_Base(unsigned int algType, unsigned int digestSize)
55 : m_digestSize(digestSize) { m_state[80] = algType; }
56
57protected:
58 // Working state is:
59 // * cv_l = 8 32-bit words
60 // * cv_r = 8 32-bit words
61 // * submsg_e_l = 8 32-bit words
62 // * submsg_e_r = 8 32-bit words
63 // * submsg_o_l = 8 32-bit words
64 // * submsg_o_r = 8 32-bit words
65 // * last_block = 32 32-bit words (128 bytes)
66 // * algType
67 // * remainingBitLength
69 // word32 m_algType, m_remainingBitLength;
70 word32 m_digestSize;
71};
72
77class LSH224 : public LSH256_Base
78{
79public:
82 CRYPTOPP_CONSTANT(DIGESTSIZE = 28);
85 CRYPTOPP_CONSTANT(BLOCKSIZE = LSH256_Base::BLOCKSIZE);
86
93 static std::string StaticAlgorithmName() { return "LSH-224"; }
94
97 LSH224() : LSH256_Base(0x000001C, DIGESTSIZE) { Restart(); }
98
99 std::string AlgorithmName() const { return StaticAlgorithmName(); }
100};
101
106class LSH256 : public LSH256_Base
107{
108public:
111 CRYPTOPP_CONSTANT(DIGESTSIZE = 32);
114 CRYPTOPP_CONSTANT(BLOCKSIZE = LSH256_Base::BLOCKSIZE);
115
122 static std::string StaticAlgorithmName() { return "LSH-256"; }
123
126 LSH256() : LSH256_Base(0x0000020, DIGESTSIZE) { Restart(); }
127
128 std::string AlgorithmName() const { return StaticAlgorithmName(); }
129};
130
135{
136public:
139 CRYPTOPP_CONSTANT(BLOCKSIZE = 256);
140
141 virtual ~LSH512_Base() {}
142
143 unsigned int BlockSize() const { return BLOCKSIZE; }
144 unsigned int DigestSize() const { return m_digestSize; }
145 unsigned int OptimalDataAlignment() const { return GetAlignmentOf<word64>(); }
146
147 void Restart();
148 void Update(const byte *input, size_t size);
149 void TruncatedFinal(byte *hash, size_t size);
150
151 std::string AlgorithmProvider() const;
152
153protected:
154 LSH512_Base(unsigned int algType, unsigned int digestSize)
155 : m_digestSize(digestSize) { m_state[80] = algType; }
156
157protected:
158 // Working state is:
159 // * cv_l = 8 64-bit words
160 // * cv_r = 8 64-bit words
161 // * submsg_e_l = 8 64-bit words
162 // * submsg_e_r = 8 64-bit words
163 // * submsg_o_l = 8 64-bit words
164 // * submsg_o_r = 8 64-bit words
165 // * last_block = 32 64-bit words (256 bytes)
166 // * algType
167 // * remainingBitLength
169 // word32 m_algType, m_remainingBitLength;
170 word32 m_digestSize;
171};
172
177class LSH384 : public LSH512_Base
178{
179public:
182 CRYPTOPP_CONSTANT(DIGESTSIZE = 48);
185 CRYPTOPP_CONSTANT(BLOCKSIZE = LSH512_Base::BLOCKSIZE);
186
193 static std::string StaticAlgorithmName() { return "LSH-384"; }
194
197 LSH384() : LSH512_Base(0x0010030, DIGESTSIZE) { Restart(); }
198
199 std::string AlgorithmName() const { return StaticAlgorithmName(); }
200};
201
206class LSH512 : public LSH512_Base
207{
208public:
211 CRYPTOPP_CONSTANT(DIGESTSIZE = 64);
214 CRYPTOPP_CONSTANT(BLOCKSIZE = LSH512_Base::BLOCKSIZE);
215
222 static std::string StaticAlgorithmName() { return "LSH-512"; }
223
226 LSH512() : LSH512_Base(0x0010040, DIGESTSIZE) { Restart(); }
227
228 std::string AlgorithmName() const { return StaticAlgorithmName(); }
229};
230
236{
237public:
240 CRYPTOPP_CONSTANT(DIGESTSIZE = 32);
243 CRYPTOPP_CONSTANT(BLOCKSIZE = LSH512_Base::BLOCKSIZE);
244
251 static std::string StaticAlgorithmName() { return "LSH-512-256"; }
252
255 LSH512_256() : LSH512_Base(0x0010020, DIGESTSIZE) { Restart(); }
256
257 std::string AlgorithmName() const { return StaticAlgorithmName(); }
258};
259
260NAMESPACE_END
261
262#endif // CRYPTOPP_LSH_H
Fixed size stack-based SecBlock.
Definition secblock.h:1246
Interface for hash functions and data processing part of MACs.
Definition cryptlib.h:1113
LSH-224 hash function.
Definition lsh.h:78
CRYPTOPP_CONSTANT(DIGESTSIZE=28)
Digest size, in bytes.
CRYPTOPP_CONSTANT(BLOCKSIZE=LSH256_Base::BLOCKSIZE)
Block size, in bytes.
static std::string StaticAlgorithmName()
The algorithm's name.
Definition lsh.h:93
LSH224()
Construct a LSH-224.
Definition lsh.h:97
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition lsh.h:99
LSH-224 and LSH-256 hash base class.
Definition lsh.h:35
CRYPTOPP_CONSTANT(BLOCKSIZE=128)
Block size, in bytes.
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition lsh.h:45
void Restart()
Restart the hash.
Definition lsh256.cpp:752
unsigned int BlockSize() const
Provides the block size of the compression function.
Definition lsh.h:43
unsigned int DigestSize() const
Definition lsh.h:44
LSH-256 hash function.
Definition lsh.h:107
CRYPTOPP_CONSTANT(BLOCKSIZE=LSH256_Base::BLOCKSIZE)
Block size, in bytes.
static std::string StaticAlgorithmName()
The algorithm's name.
Definition lsh.h:122
LSH256()
Construct a LSH-256.
Definition lsh.h:126
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition lsh.h:128
CRYPTOPP_CONSTANT(DIGESTSIZE=32)
Digest size, in bytes.
LSH-384 hash function.
Definition lsh.h:178
LSH384()
Construct a LSH-384.
Definition lsh.h:197
CRYPTOPP_CONSTANT(BLOCKSIZE=LSH512_Base::BLOCKSIZE)
Block size, in bytes.
CRYPTOPP_CONSTANT(DIGESTSIZE=48)
Digest size, in bytes.
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition lsh.h:199
static std::string StaticAlgorithmName()
The algorithm's name.
Definition lsh.h:193
LSH-512-256 hash function.
Definition lsh.h:236
CRYPTOPP_CONSTANT(BLOCKSIZE=LSH512_Base::BLOCKSIZE)
Block size, in bytes.
CRYPTOPP_CONSTANT(DIGESTSIZE=32)
Digest size, in bytes.
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition lsh.h:257
LSH512_256()
Construct a LSH-512-256.
Definition lsh.h:255
static std::string StaticAlgorithmName()
The algorithm's name.
Definition lsh.h:251
LSH-384 and LSH-512 hash base class.
Definition lsh.h:135
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition lsh.h:145
void Restart()
Restart the hash.
Definition lsh512.cpp:828
unsigned int DigestSize() const
Definition lsh.h:144
CRYPTOPP_CONSTANT(BLOCKSIZE=256)
Block size, in bytes.
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition lsh512.cpp:863
void Update(const byte *input, size_t size)
Updates a hash with additional input.
Definition lsh512.cpp:844
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition lsh512.cpp:781
unsigned int BlockSize() const
Provides the block size of the compression function.
Definition lsh.h:143
LSH-512 hash function.
Definition lsh.h:207
CRYPTOPP_CONSTANT(BLOCKSIZE=LSH512_Base::BLOCKSIZE)
Block size, in bytes.
LSH512()
Construct a LSH-512.
Definition lsh.h:226
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition lsh.h:228
static std::string StaticAlgorithmName()
The algorithm's name.
Definition lsh.h:222
CRYPTOPP_CONSTANT(DIGESTSIZE=64)
Digest size, in bytes.
unsigned int word32
32-bit unsigned datatype
Definition config_int.h:62
Abstract base classes that provide a uniform interface to this library.
Classes and functions for secure memory allocations.