Skip to content

Commit 11eee26

Browse files
authoredApr 16, 2021
Add LSH-256 and LSH-512 hash functions (GH weidai11#1025, PR weidai11#1026)
Add South Korea's LSH-256 and LSH-512 families of hash functions.
1 parent bb3776e commit 11eee26

16 files changed

+2298
-7
lines changed
 

‎Filelist.txt

+7
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ lubyrack.h
204204
lea.cpp
205205
lea_simd.cpp
206206
lea.h
207+
lsh256.cpp
208+
lsh512.cpp
209+
lsh.h
207210
luc.cpp
208211
luc.h
209212
mars.cpp
@@ -525,6 +528,10 @@ TestVectors/hmac.txt
525528
TestVectors/kalyna.txt
526529
TestVectors/keccak.txt
527530
TestVectors/lea.txt
531+
TestVectors/lsh.txt
532+
TestVectors/lsh256.txt
533+
TestVectors/lsh512.txt
534+
TestVectors/lsh512_256.txt
528535
TestVectors/mars.txt
529536
TestVectors/nr.txt
530537
TestVectors/panama.txt

‎Readme.txt

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ Currently the library contains the following algorithms:
2828
Poly1305, Poly1305 (IETF), SipHash, Two-Track-MAC,
2929
VMAC
3030

31-
BLAKE2s, BLAKE2b, Keccack (F1600), SHA-1,
32-
hash functions SHA-2 (224/256/384/512), SHA-3 (224/256/384/512),
33-
SHAKE (128/256), SipHash, SM3, Tiger,
31+
BLAKE2s, BLAKE2b, Keccack (F1600), LSH (256/512),
32+
hash functions SHA-1, SHA-2 (224/256/384/512), SHA-3 (224/256),
33+
SHA-3 (384/512), SHAKE (128/256), SipHash, SM3, Tiger,
3434
RIPEMD (128/160/256/320), WHIRLPOOL
3535

3636
RSA, DSA, Deterministic DSA, ElGamal,
@@ -76,8 +76,8 @@ Other features include:
7676
* A high level interface for most of the above, using a filter/pipeline
7777
metaphor
7878
* benchmarks and validation testing
79-
* x86, x64 (x86-64), x32 (ILP32), ARM-32, Aarch32, Aarch64 and Power8 in-core code
80-
for the commonly used algorithms
79+
* x86, x64 (x86-64), x32 (ILP32), ARM-32, Aarch32, Aarch64 and Power8
80+
in-core code for the commonly used algorithms
8181
+ run-time CPU feature detection and code selection
8282
+ supports GCC-style and MSVC-style inline assembly, and MASM for x64
8383
+ x86, x64 (x86-64), x32 provides MMX, SSE2, and SSE4 implementations

‎TestVectors/all.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Test: TestVectors/hmac.txt
2727
Test: TestVectors/kalyna.txt
2828
Test: TestVectors/keccak.txt
2929
Test: TestVectors/lea.txt
30+
Test: TestVectors/lsh256.txt
31+
Test: TestVectors/lsh512.txt
3032
Test: TestVectors/mars.txt
3133
Test: TestVectors/nr.txt
3234
Test: TestVectors/panama.txt
@@ -61,4 +63,3 @@ Test: TestVectors/vmac.txt
6163
Test: TestVectors/wake.txt
6264
Test: TestVectors/whrlpool.txt
6365
Test: TestVectors/xts.txt
64-

‎TestVectors/lsh.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
AlgorithmType: FileList
2+
Name: LSH test vectors
3+
Test: TestVectors/lsh256.txt
4+
Test: TestVectors/lsh512.txt
5+
Test: TestVectors/lsh512_256.txt

‎TestVectors/lsh256.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
AlgorithmType: MessageDigest
2+
Name: LSH-224
3+
Source: https://en.wikipedia.org/wiki/LSH_(hash_function)
4+
Message: "abc"
5+
Digest: F7 C5 3B A4 03 4E 70 8E 74 FB A4 2E 55 99 7C A5 12 6B B7 62 36 88 F8 53 42 F7 37 32
6+
Test: Verify
7+
#
8+
Name: LSH-256
9+
Source: https://en.wikipedia.org/wiki/LSH_(hash_function)
10+
Message: "abc"
11+
Digest: 5F BF 36 5D AE A5 44 6A 70 53 C5 2B 57 40 4D 77 A0 7A 5F 48 A1 F7 C1 96 3A 08 98 BA 1B 71 47 41
12+
Test: Verify

‎TestVectors/lsh512.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
AlgorithmType: MessageDigest
2+
Name: LSH-384
3+
Source: https://en.wikipedia.org/wiki/LSH_(hash_function)
4+
Message: "abc"
5+
Digest: 5F 34 4E FA A0 E4 3C CD 2E 5E 19 4D 60 39 79 4B 4F B4 31 F1 0F B4 B6 5F D4 5E 9D A4 EC DE 0F 27 B6 6E 8D BD FA 47 25 2E 0D 0B 74 1B FD 91 F9 FE
6+
Test: Verify
7+
#
8+
Name: LSH-512
9+
Source: https://en.wikipedia.org/wiki/LSH_(hash_function)
10+
Message: "abc"
11+
Digest: A3 D9 3C FE 60 DC 1A AC DD 3B D4 BE F0 A6 98 53 81 A3 96 C7 D4 9D 9F D1 77 79 56 97 C3 53 52 08 B5 C5 72 24 BE F2 10 84 D4 20 83 E9 5A 4B D8 EB 33 E8 69 81 2B 65 03 1C 42 88 19 A1 E7 CE 59 6D
12+
Test: Verify

‎TestVectors/lsh512_256.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
AlgorithmType: MessageDigest
2+
Name: LSH-512-256
3+
Source: https://en.wikipedia.org/wiki/LSH_(hash_function)
4+
Message: "abc"
5+
Digest: CD 89 23 10 53 26 02 33 2B 61 3F 1E C1 1A 69 62 FC A6 1E A0 9E CF FC D4 BC F7 58 58 D8 02 ED EC
6+
Test: Verify

‎bench1.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,8 @@ void BenchmarkUnkeyedAlgorithms(double t, double hertz)
509509
BenchMarkByNameKeyLess<HashTransformation>("SM3");
510510
BenchMarkByNameKeyLess<HashTransformation>("BLAKE2s");
511511
BenchMarkByNameKeyLess<HashTransformation>("BLAKE2b");
512+
BenchMarkByNameKeyLess<HashTransformation>("LSH-256");
513+
BenchMarkByNameKeyLess<HashTransformation>("LSH-512");
512514
}
513515

514516
std::cout << "\n</TABLE>" << std::endl;

‎lsh.h

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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

‎lsh256.cpp

+913
Large diffs are not rendered by default.

‎lsh512.cpp

+1,125
Large diffs are not rendered by default.

‎regtest1.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "ripemd.h"
2626
#include "panama.h"
2727
#include "whrlpool.h"
28+
#include "lsh.h"
2829

2930
#include "osrng.h"
3031
#include "drbg.h"
@@ -115,6 +116,11 @@ void RegisterFactories1()
115116
RegisterDefaultFactoryFor<HashTransformation, SM3>();
116117
RegisterDefaultFactoryFor<HashTransformation, BLAKE2s>();
117118
RegisterDefaultFactoryFor<HashTransformation, BLAKE2b>();
119+
RegisterDefaultFactoryFor<HashTransformation, LSH224>();
120+
RegisterDefaultFactoryFor<HashTransformation, LSH256>();
121+
RegisterDefaultFactoryFor<HashTransformation, LSH384>();
122+
RegisterDefaultFactoryFor<HashTransformation, LSH512>();
123+
RegisterDefaultFactoryFor<HashTransformation, LSH512_256>();
118124

119125
#ifdef BLOCKING_RNG_AVAILABLE
120126
RegisterDefaultFactoryFor<RandomNumberGenerator, BlockingRng>();

‎test.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,8 @@ bool Validate(int alg, bool thorough)
10091009
case 65: result = ValidateARIA(); break;
10101010
case 66: result = ValidateCamellia(); break;
10111011
case 67: result = ValidateWhirlpool(); break;
1012-
case 68: result = ValidateTTMAC(); break;
1012+
case 68: result = ValidateLSH(); break;
1013+
case 69: result = ValidateTTMAC(); break;
10131014
case 70: result = ValidateSalsa(); break;
10141015
case 71: result = ValidateChaCha(); break;
10151016
case 72: result = ValidateChaChaTLS(); break;

‎validat3.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ bool ValidateAll(bool thorough)
107107
pass=ValidateSHAKE() && pass;
108108
pass=ValidateSHAKE_XOF() && pass;
109109

110+
pass=ValidateLSH() && pass;
111+
110112
pass=ValidateHashDRBG() && pass;
111113
pass=ValidateHmacDRBG() && pass;
112114

‎validat5.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "siphash.h"
2929
#include "poly1305.h"
3030
#include "whrlpool.h"
31+
#include "lsh.h"
3132

3233
#include "pssr.h"
3334
#include "hkdf.h"
@@ -514,14 +515,22 @@ bool ValidateHAVAL()
514515

515516
bool ValidatePanama()
516517
{
518+
std::cout << "\nPanama validation suite running...\n";
517519
return RunTestDataFile("TestVectors/panama.txt");
518520
}
519521

520522
bool ValidateWhirlpool()
521523
{
524+
std::cout << "\nWhirlpool validation suite running...\n";
522525
return RunTestDataFile("TestVectors/whrlpool.txt");
523526
}
524527

528+
bool ValidateLSH()
529+
{
530+
std::cout << "\nLSH validation suite running...\n";
531+
return RunTestDataFile("TestVectors/lsh.txt");
532+
}
533+
525534
#ifdef CRYPTOPP_REMOVED
526535
bool ValidateMD5MAC()
527536
{

‎validate.h

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ bool ValidateTiger();
6060
bool ValidateRIPEMD();
6161
bool ValidatePanama();
6262
bool ValidateWhirlpool();
63+
bool ValidateLSH();
6364

6465
bool ValidateSM3();
6566
bool ValidateBLAKE2s();

0 commit comments

Comments
 (0)
Please sign in to comment.