20ANONYMOUS_NAMESPACE_BEGIN
24const unsigned int LSH512_MSG_BLK_BYTE_LEN = 256;
27const unsigned int LSH512_HASH_VAL_MAX_BYTE_LEN = 64;
30const unsigned int CV_WORD_LEN = 16;
31const unsigned int CONST_WORD_LEN = 8;
32const unsigned int HASH_VAL_MAX_WORD_LEN = 8;
33const unsigned int NUM_STEPS = 28;
35const unsigned int ROT_EVEN_ALPHA = 23;
36const unsigned int ROT_EVEN_BETA = 59;
37const unsigned int ROT_ODD_ALPHA = 7;
38const unsigned int ROT_ODD_BETA = 3;
40const unsigned int LSH_TYPE_512_512 = 0x0010040;
41const unsigned int LSH_TYPE_512_384 = 0x0010030;
42const unsigned int LSH_TYPE_512_256 = 0x0010020;
43const unsigned int LSH_TYPE_512_224 = 0x001001C;
50const unsigned int LSH_SUCCESS = 0x0;
53const unsigned int LSH_ERR_INVALID_DATABITLEN = 0x2403;
54const unsigned int LSH_ERR_INVALID_STATE = 0x2404;
58const unsigned int AlgorithmType = 80;
59const unsigned int RemainingBits = 81;
63NAMESPACE_BEGIN(CryptoPP)
76CRYPTOPP_ALIGN_DATA(32)
78const word64 LSH512_IV224[CV_WORD_LEN] = {
79 W64LIT(0x0C401E9FE8813A55), W64LIT(0x4A5F446268FD3D35), W64LIT(0xFF13E452334F612A), W64LIT(0xF8227661037E354A),
80 W64LIT(0xA5F223723C9CA29D), W64LIT(0x95D965A11AED3979), W64LIT(0x01E23835B9AB02CC), W64LIT(0x52D49CBAD5B30616),
81 W64LIT(0x9E5C2027773F4ED3), W64LIT(0x66A5C8801925B701), W64LIT(0x22BBC85B4C6779D9), W64LIT(0xC13171A42C559C23),
82 W64LIT(0x31E2B67D25BE3813), W64LIT(0xD522C4DEED8E4D83), W64LIT(0xA79F5509B43FBAFE), W64LIT(0xE00D2CD88B4B6C6A),
85CRYPTOPP_ALIGN_DATA(32)
87const word64 LSH512_IV256[CV_WORD_LEN] = {
88 W64LIT(0x6DC57C33DF989423), W64LIT(0xD8EA7F6E8342C199), W64LIT(0x76DF8356F8603AC4), W64LIT(0x40F1B44DE838223A),
89 W64LIT(0x39FFE7CFC31484CD), W64LIT(0x39C4326CC5281548), W64LIT(0x8A2FF85A346045D8), W64LIT(0xFF202AA46DBDD61E),
90 W64LIT(0xCF785B3CD5FCDB8B), W64LIT(0x1F0323B64A8150BF), W64LIT(0xFF75D972F29EA355), W64LIT(0x2E567F30BF1CA9E1),
91 W64LIT(0xB596875BF8FF6DBA), W64LIT(0xFCCA39B089EF4615), W64LIT(0xECFF4017D020B4B6), W64LIT(0x7E77384C772ED802),
94CRYPTOPP_ALIGN_DATA(32)
96const word64 LSH512_IV384[CV_WORD_LEN] = {
97 W64LIT(0x53156A66292808F6), W64LIT(0xB2C4F362B204C2BC), W64LIT(0xB84B7213BFA05C4E), W64LIT(0x976CEB7C1B299F73),
98 W64LIT(0xDF0CC63C0570AE97), W64LIT(0xDA4441BAA486CE3F), W64LIT(0x6559F5D9B5F2ACC2), W64LIT(0x22DACF19B4B52A16),
99 W64LIT(0xBBCDACEFDE80953A), W64LIT(0xC9891A2879725B3E), W64LIT(0x7C9FE6330237E440), W64LIT(0xA30BA550553F7431),
100 W64LIT(0xBB08043FB34E3E30), W64LIT(0xA0DEC48D54618EAD), W64LIT(0x150317267464BC57), W64LIT(0x32D1501FDE63DC93)
103CRYPTOPP_ALIGN_DATA(32)
105const word64 LSH512_IV512[CV_WORD_LEN] = {
106 W64LIT(0xadd50f3c7f07094e), W64LIT(0xe3f3cee8f9418a4f), W64LIT(0xb527ecde5b3d0ae9), W64LIT(0x2ef6dec68076f501),
107 W64LIT(0x8cb994cae5aca216), W64LIT(0xfbb9eae4bba48cc7), W64LIT(0x650a526174725fea), W64LIT(0x1f9a61a73f8d8085),
108 W64LIT(0xb6607378173b539b), W64LIT(0x1bc99853b0c0b9ed), W64LIT(0xdf727fc19b182d47), W64LIT(0xdbef360cf893a457),
109 W64LIT(0x4981f5e570147e80), W64LIT(0xd00c4490ca7d3e30), W64LIT(0x5d73940c0e4ae1ec), W64LIT(0x894085e2edb2d819)
117const word64 LSH512_StepConstants[CONST_WORD_LEN * NUM_STEPS] = {
118 W64LIT(0x97884283c938982a), W64LIT(0xba1fca93533e2355), W64LIT(0xc519a2e87aeb1c03), W64LIT(0x9a0fc95462af17b1),
119 W64LIT(0xfc3dda8ab019a82b), W64LIT(0x02825d079a895407), W64LIT(0x79f2d0a7ee06a6f7), W64LIT(0xd76d15eed9fdf5fe),
120 W64LIT(0x1fcac64d01d0c2c1), W64LIT(0xd9ea5de69161790f), W64LIT(0xdebc8b6366071fc8), W64LIT(0xa9d91db711c6c94b),
121 W64LIT(0x3a18653ac9c1d427), W64LIT(0x84df64a223dd5b09), W64LIT(0x6cc37895f4ad9e70), W64LIT(0x448304c8d7f3f4d5),
122 W64LIT(0xea91134ed29383e0), W64LIT(0xc4484477f2da88e8), W64LIT(0x9b47eec96d26e8a6), W64LIT(0x82f6d4c8d89014f4),
123 W64LIT(0x527da0048b95fb61), W64LIT(0x644406c60138648d), W64LIT(0x303c0e8aa24c0edc), W64LIT(0xc787cda0cbe8ca19),
124 W64LIT(0x7ba46221661764ca), W64LIT(0x0c8cbc6acd6371ac), W64LIT(0xe336b836940f8f41), W64LIT(0x79cb9da168a50976),
125 W64LIT(0xd01da49021915cb3), W64LIT(0xa84accc7399cf1f1), W64LIT(0x6c4a992cee5aeb0c), W64LIT(0x4f556e6cb4b2e3e0),
126 W64LIT(0x200683877d7c2f45), W64LIT(0x9949273830d51db8), W64LIT(0x19eeeecaa39ed124), W64LIT(0x45693f0a0dae7fef),
127 W64LIT(0xedc234b1b2ee1083), W64LIT(0xf3179400d68ee399), W64LIT(0xb6e3c61b4945f778), W64LIT(0xa4c3db216796c42f),
128 W64LIT(0x268a0b04f9ab7465), W64LIT(0xe2705f6905f2d651), W64LIT(0x08ddb96e426ff53d), W64LIT(0xaea84917bc2e6f34),
129 W64LIT(0xaff6e664a0fe9470), W64LIT(0x0aab94d765727d8c), W64LIT(0x9aa9e1648f3d702e), W64LIT(0x689efc88fe5af3d3),
130 W64LIT(0xb0950ffea51fd98b), W64LIT(0x52cfc86ef8c92833), W64LIT(0xe69727b0b2653245), W64LIT(0x56f160d3ea9da3e2),
131 W64LIT(0xa6dd4b059f93051f), W64LIT(0xb6406c3cd7f00996), W64LIT(0x448b45f3ccad9ec8), W64LIT(0x079b8587594ec73b),
132 W64LIT(0x45a50ea3c4f9653b), W64LIT(0x22983767c1f15b85), W64LIT(0x7dbed8631797782b), W64LIT(0x485234be88418638),
133 W64LIT(0x842850a5329824c5), W64LIT(0xf6aca914c7f9a04c), W64LIT(0xcfd139c07a4c670c), W64LIT(0xa3210ce0a8160242),
134 W64LIT(0xeab3b268be5ea080), W64LIT(0xbacf9f29b34ce0a7), W64LIT(0x3c973b7aaf0fa3a8), W64LIT(0x9a86f346c9c7be80),
135 W64LIT(0xac78f5d7cabcea49), W64LIT(0xa355bddcc199ed42), W64LIT(0xa10afa3ac6b373db), W64LIT(0xc42ded88be1844e5),
136 W64LIT(0x9e661b271cff216a), W64LIT(0x8a6ec8dd002d8861), W64LIT(0xd3d2b629beb34be4), W64LIT(0x217a3a1091863f1a),
137 W64LIT(0x256ecda287a733f5), W64LIT(0xf9139a9e5b872fe5), W64LIT(0xac0535017a274f7c), W64LIT(0xf21b7646d65d2aa9),
138 W64LIT(0x048142441c208c08), W64LIT(0xf937a5dd2db5e9eb), W64LIT(0xa688dfe871ff30b7), W64LIT(0x9bb44aa217c5593b),
139 W64LIT(0x943c702a2edb291a), W64LIT(0x0cae38f9e2b715de), W64LIT(0xb13a367ba176cc28), W64LIT(0x0d91bd1d3387d49b),
140 W64LIT(0x85c386603cac940c), W64LIT(0x30dd830ae39fd5e4), W64LIT(0x2f68c85a712fe85d), W64LIT(0x4ffeecb9dd1e94d6),
141 W64LIT(0xd0ac9a590a0443ae), W64LIT(0xbae732dc99ccf3ea), W64LIT(0xeb70b21d1842f4d9), W64LIT(0x9f4eda50bb5c6fa8),
142 W64LIT(0x4949e69ce940a091), W64LIT(0x0e608dee8375ba14), W64LIT(0x983122cba118458c), W64LIT(0x4eeba696fbb36b25),
143 W64LIT(0x7d46f3630e47f27e), W64LIT(0xa21a0f7666c0dea4), W64LIT(0x5c22cf355b37cec4), W64LIT(0xee292b0c17cc1847),
144 W64LIT(0x9330838629e131da), W64LIT(0x6eee7c71f92fce22), W64LIT(0xc953ee6cb95dd224), W64LIT(0x3a923d92af1e9073),
145 W64LIT(0xc43a5671563a70fb), W64LIT(0xbc2985dd279f8346), W64LIT(0x7ef2049093069320), W64LIT(0x17543723e3e46035),
146 W64LIT(0xc3b409b00b130c6d), W64LIT(0x5d6aee6b28fdf090), W64LIT(0x1d425b26172ff6ed), W64LIT(0xcccfd041cdaf03ad),
147 W64LIT(0xfe90c7c790ab6cbf), W64LIT(0xe5af6304c722ca02), W64LIT(0x70f695239999b39e), W64LIT(0x6b8b5b07c844954c),
148 W64LIT(0x77bdb9bb1e1f7a30), W64LIT(0xc859599426ee80ed), W64LIT(0x5f9d813d4726e40a), W64LIT(0x9ca0120f7cb2b179),
149 W64LIT(0x8f588f583c182cbd), W64LIT(0x951267cbe9eccce7), W64LIT(0x678bb8bd334d520e), W64LIT(0xf6e662d00cd9e1b7),
150 W64LIT(0x357774d93d99aaa7), W64LIT(0x21b2edbb156f6eb5), W64LIT(0xfd1ebe846e0aee69), W64LIT(0x3cb2218c2f642b15),
151 W64LIT(0xe7e7e7945444ea4c), W64LIT(0xa77a33b5d6b9b47c), W64LIT(0xf34475f0809f6075), W64LIT(0xdd4932dce6bb99ad),
152 W64LIT(0xacec4e16d74451dc), W64LIT(0xd4a0a8d084de23d6), W64LIT(0x1bdd42f278f95866), W64LIT(0xeed3adbb938f4051),
153 W64LIT(0xcfcf7be8992f3733), W64LIT(0x21ade98c906e3123), W64LIT(0x37ba66711fffd668), W64LIT(0x267c0fc3a255478a),
154 W64LIT(0x993a64ee1b962e88), W64LIT(0x754979556301faaa), W64LIT(0xf920356b7251be81), W64LIT(0xc281694f22cf923f),
155 W64LIT(0x9f4b6481c8666b02), W64LIT(0xcf97761cfe9f5444), W64LIT(0xf220d7911fd63e9f), W64LIT(0xa28bd365f79cd1b0),
156 W64LIT(0xd39f5309b1c4b721), W64LIT(0xbec2ceb864fca51f), W64LIT(0x1955a0ddc410407a), W64LIT(0x43eab871f261d201),
157 W64LIT(0xeaafe64a2ed16da1), W64LIT(0x670d931b9df39913), W64LIT(0x12f868b0f614de91), W64LIT(0x2e5f395d946e8252),
158 W64LIT(0x72f25cbb767bd8f4), W64LIT(0x8191871d61a1c4dd), W64LIT(0x6ef67ea1d450ba93), W64LIT(0x2ea32a645433d344),
159 W64LIT(0x9a963079003f0f8b), W64LIT(0x74a0aeb9918cac7a), W64LIT(0x0b6119a70af36fa3), W64LIT(0x8d9896f202f0d480),
160 W64LIT(0x654f1831f254cd66), W64LIT(0x1318a47f0366a25e), W64LIT(0x65752076250b4e01), W64LIT(0xd1cd8eb888071772),
161 W64LIT(0x30c6a9793f4e9b25), W64LIT(0x154f684b1e3926ee), W64LIT(0x6c7ac0b1fe6312ae), W64LIT(0x262f88f4f3c5550d),
162 W64LIT(0xb4674a24472233cb), W64LIT(0x2bbd23826a090071), W64LIT(0xda95969b30594f66), W64LIT(0x9f5c47408f1e8a43),
163 W64LIT(0xf77022b88de9c055), W64LIT(0x64b7b36957601503), W64LIT(0xe73b72b06175c11a), W64LIT(0x55b87de8b91a6233),
164 W64LIT(0x1bb16e6b6955ff7f), W64LIT(0xe8e0a5ec7309719c), W64LIT(0x702c31cb89a8b640), W64LIT(0xfba387cfada8cde2),
165 W64LIT(0x6792db4677aa164c), W64LIT(0x1c6b1cc0b7751867), W64LIT(0x22ae2311d736dc01), W64LIT(0x0e3666a1d37c9588),
166 W64LIT(0xcd1fd9d4bf557e9a), W64LIT(0xc986925f7c7b0e84), W64LIT(0x9c5dfd55325ef6b0), W64LIT(0x9f2b577d5676b0dd),
167 W64LIT(0xfa6e21be21c062b3), W64LIT(0x8787dd782c8d7f83), W64LIT(0xd0d134e90e12dd23), W64LIT(0x449d087550121d96),
168 W64LIT(0xecf9ae9414d41967), W64LIT(0x5018f1dbf789934d), W64LIT(0xfa5b52879155a74c), W64LIT(0xca82d4d3cd278e7c),
169 W64LIT(0x688fdfdfe22316ad), W64LIT(0x0f6555a4ba0d030a), W64LIT(0xa2061df720f000f3), W64LIT(0xe1a57dc5622fb3da),
170 W64LIT(0xe6a842a8e8ed8153), W64LIT(0x690acdd3811ce09d), W64LIT(0x55adda18e6fcf446), W64LIT(0x4d57a8a0f4b60b46),
171 W64LIT(0xf86fbfc20539c415), W64LIT(0x74bafa5ec7100d19), W64LIT(0xa824151810f0f495), W64LIT(0x8723432791e38ebb),
172 W64LIT(0x8eeaeb91d66ed539), W64LIT(0x73d8a1549dfd7e06), W64LIT(0x0387f2ffe3f13a9b), W64LIT(0xa5004995aac15193),
173 W64LIT(0x682f81c73efdda0d), W64LIT(0x2fb55925d71d268d), W64LIT(0xcc392d2901e58a3d), W64LIT(0xaa666ab975724a42)
179ANONYMOUS_NAMESPACE_BEGIN
182using CryptoPP::word32;
183using CryptoPP::word64;
184using CryptoPP::rotlFixed;
185using CryptoPP::rotlConstant;
187using CryptoPP::GetBlock;
188using CryptoPP::LittleEndian;
189using CryptoPP::ConditionalByteReverse;
190using CryptoPP::LITTLE_ENDIAN_ORDER;
192using CryptoPP::LSH::LSH512_IV224;
193using CryptoPP::LSH::LSH512_IV256;
194using CryptoPP::LSH::LSH512_IV384;
195using CryptoPP::LSH::LSH512_IV512;
196using CryptoPP::LSH::LSH512_StepConstants;
200typedef word64 lsh_u64;
207 LSH512_Context(word64* state, word64 algType, word64& remainingBitLength) :
208 cv_l(state+0), cv_r(state+8), sub_msgs(state+16),
209 last_block(
reinterpret_cast<byte*
>(state+48)),
210 remain_databitlen(remainingBitLength),
211 alg_type(
static_cast<lsh_type
>(algType)) {}
217 lsh_u64& remain_databitlen;
224 submsg_e_l(state+16), submsg_e_r(state+24),
225 submsg_o_l(state+32), submsg_o_r(state+40) { }
233const lsh_u32 g_gamma512[8] = { 0, 16, 32, 48, 8, 24, 40, 56 };
237inline bool LSH_IS_LSH512(lsh_uint val) {
238 return (val & 0xf0000) == 0x10000;
241inline lsh_uint LSH_GET_SMALL_HASHBIT(lsh_uint val) {
245inline lsh_uint LSH_GET_HASHBYTE(lsh_uint val) {
249inline lsh_uint LSH_GET_HASHBIT(lsh_uint val) {
250 return (LSH_GET_HASHBYTE(val) << 3) - LSH_GET_SMALL_HASHBIT(val);
253inline lsh_u64 loadLE64(lsh_u64 v) {
257lsh_u64 ROTL64(lsh_u64 x, lsh_u32 r) {
262inline void load_msg_blk(
LSH512_Internal* i_state,
const lsh_u8* msgblk)
264 lsh_u64* submsg_e_l = i_state->submsg_e_l;
265 lsh_u64* submsg_e_r = i_state->submsg_e_r;
266 lsh_u64* submsg_o_l = i_state->submsg_o_l;
267 lsh_u64* submsg_o_r = i_state->submsg_o_r;
271 InBlock input(msgblk);
272 input(submsg_e_l[0])(submsg_e_l[1])(submsg_e_l[2])(submsg_e_l[3])
273 (submsg_e_l[4])(submsg_e_l[5])(submsg_e_l[6])(submsg_e_l[7])
274 (submsg_e_r[0])(submsg_e_r[1])(submsg_e_r[2])(submsg_e_r[3])
275 (submsg_e_r[4])(submsg_e_r[5])(submsg_e_r[6])(submsg_e_r[7])
276 (submsg_o_l[0])(submsg_o_l[1])(submsg_o_l[2])(submsg_o_l[3])
277 (submsg_o_l[4])(submsg_o_l[5])(submsg_o_l[6])(submsg_o_l[7])
278 (submsg_o_r[0])(submsg_o_r[1])(submsg_o_r[2])(submsg_o_r[3])
279 (submsg_o_r[4])(submsg_o_r[5])(submsg_o_r[6])(submsg_o_r[7]);
284 CRYPTOPP_ASSERT(i_state != NULLPTR);
286 lsh_u64* submsg_e_l = i_state->submsg_e_l;
287 lsh_u64* submsg_e_r = i_state->submsg_e_r;
288 lsh_u64* submsg_o_l = i_state->submsg_o_l;
289 lsh_u64* submsg_o_r = i_state->submsg_o_r;
292 temp = submsg_e_l[0];
293 submsg_e_l[0] = submsg_o_l[0] + submsg_e_l[3];
294 submsg_e_l[3] = submsg_o_l[3] + submsg_e_l[1];
295 submsg_e_l[1] = submsg_o_l[1] + submsg_e_l[2];
296 submsg_e_l[2] = submsg_o_l[2] + temp;
297 temp = submsg_e_l[4];
298 submsg_e_l[4] = submsg_o_l[4] + submsg_e_l[7];
299 submsg_e_l[7] = submsg_o_l[7] + submsg_e_l[6];
300 submsg_e_l[6] = submsg_o_l[6] + submsg_e_l[5];
301 submsg_e_l[5] = submsg_o_l[5] + temp;
302 temp = submsg_e_r[0];
303 submsg_e_r[0] = submsg_o_r[0] + submsg_e_r[3];
304 submsg_e_r[3] = submsg_o_r[3] + submsg_e_r[1];
305 submsg_e_r[1] = submsg_o_r[1] + submsg_e_r[2];
306 submsg_e_r[2] = submsg_o_r[2] + temp;
307 temp = submsg_e_r[4];
308 submsg_e_r[4] = submsg_o_r[4] + submsg_e_r[7];
309 submsg_e_r[7] = submsg_o_r[7] + submsg_e_r[6];
310 submsg_e_r[6] = submsg_o_r[6] + submsg_e_r[5];
311 submsg_e_r[5] = submsg_o_r[5] + temp;
316 CRYPTOPP_ASSERT(i_state != NULLPTR);
318 lsh_u64* submsg_e_l = i_state->submsg_e_l;
319 lsh_u64* submsg_e_r = i_state->submsg_e_r;
320 lsh_u64* submsg_o_l = i_state->submsg_o_l;
321 lsh_u64* submsg_o_r = i_state->submsg_o_r;
324 temp = submsg_o_l[0];
325 submsg_o_l[0] = submsg_e_l[0] + submsg_o_l[3];
326 submsg_o_l[3] = submsg_e_l[3] + submsg_o_l[1];
327 submsg_o_l[1] = submsg_e_l[1] + submsg_o_l[2];
328 submsg_o_l[2] = submsg_e_l[2] + temp;
329 temp = submsg_o_l[4];
330 submsg_o_l[4] = submsg_e_l[4] + submsg_o_l[7];
331 submsg_o_l[7] = submsg_e_l[7] + submsg_o_l[6];
332 submsg_o_l[6] = submsg_e_l[6] + submsg_o_l[5];
333 submsg_o_l[5] = submsg_e_l[5] + temp;
334 temp = submsg_o_r[0];
335 submsg_o_r[0] = submsg_e_r[0] + submsg_o_r[3];
336 submsg_o_r[3] = submsg_e_r[3] + submsg_o_r[1];
337 submsg_o_r[1] = submsg_e_r[1] + submsg_o_r[2];
338 submsg_o_r[2] = submsg_e_r[2] + temp;
339 temp = submsg_o_r[4];
340 submsg_o_r[4] = submsg_e_r[4] + submsg_o_r[7];
341 submsg_o_r[7] = submsg_e_r[7] + submsg_o_r[6];
342 submsg_o_r[6] = submsg_e_r[6] + submsg_o_r[5];
343 submsg_o_r[5] = submsg_e_r[5] + temp;
346inline void load_sc(
const lsh_u64** p_const_v,
size_t i)
348 *p_const_v = &LSH512_StepConstants[i];
351inline void msg_add_even(lsh_u64 cv_l[8], lsh_u64 cv_r[8],
LSH512_Internal* i_state)
353 CRYPTOPP_ASSERT(i_state != NULLPTR);
355 lsh_u64* submsg_e_l = i_state->submsg_e_l;
356 lsh_u64* submsg_e_r = i_state->submsg_e_r;
358 cv_l[0] ^= submsg_e_l[0]; cv_l[1] ^= submsg_e_l[1];
359 cv_l[2] ^= submsg_e_l[2]; cv_l[3] ^= submsg_e_l[3];
360 cv_l[4] ^= submsg_e_l[4]; cv_l[5] ^= submsg_e_l[5];
361 cv_l[6] ^= submsg_e_l[6]; cv_l[7] ^= submsg_e_l[7];
362 cv_r[0] ^= submsg_e_r[0]; cv_r[1] ^= submsg_e_r[1];
363 cv_r[2] ^= submsg_e_r[2]; cv_r[3] ^= submsg_e_r[3];
364 cv_r[4] ^= submsg_e_r[4]; cv_r[5] ^= submsg_e_r[5];
365 cv_r[6] ^= submsg_e_r[6]; cv_r[7] ^= submsg_e_r[7];
368inline void msg_add_odd(lsh_u64 cv_l[8], lsh_u64 cv_r[8],
LSH512_Internal* i_state)
370 CRYPTOPP_ASSERT(i_state != NULLPTR);
372 lsh_u64* submsg_o_l = i_state->submsg_o_l;
373 lsh_u64* submsg_o_r = i_state->submsg_o_r;
375 cv_l[0] ^= submsg_o_l[0]; cv_l[1] ^= submsg_o_l[1];
376 cv_l[2] ^= submsg_o_l[2]; cv_l[3] ^= submsg_o_l[3];
377 cv_l[4] ^= submsg_o_l[4]; cv_l[5] ^= submsg_o_l[5];
378 cv_l[6] ^= submsg_o_l[6]; cv_l[7] ^= submsg_o_l[7];
379 cv_r[0] ^= submsg_o_r[0]; cv_r[1] ^= submsg_o_r[1];
380 cv_r[2] ^= submsg_o_r[2]; cv_r[3] ^= submsg_o_r[3];
381 cv_r[4] ^= submsg_o_r[4]; cv_r[5] ^= submsg_o_r[5];
382 cv_r[6] ^= submsg_o_r[6]; cv_r[7] ^= submsg_o_r[7];
385inline void add_blk(lsh_u64 cv_l[8], lsh_u64 cv_r[8])
397template <
unsigned int R>
398inline void rotate_blk(lsh_u64 cv[8])
400 cv[0] = rotlConstant<R>(cv[0]);
401 cv[1] = rotlConstant<R>(cv[1]);
402 cv[2] = rotlConstant<R>(cv[2]);
403 cv[3] = rotlConstant<R>(cv[3]);
404 cv[4] = rotlConstant<R>(cv[4]);
405 cv[5] = rotlConstant<R>(cv[5]);
406 cv[6] = rotlConstant<R>(cv[6]);
407 cv[7] = rotlConstant<R>(cv[7]);
410inline void xor_with_const(lsh_u64 cv_l[8],
const lsh_u64* const_v)
412 cv_l[0] ^= const_v[0];
413 cv_l[1] ^= const_v[1];
414 cv_l[2] ^= const_v[2];
415 cv_l[3] ^= const_v[3];
416 cv_l[4] ^= const_v[4];
417 cv_l[5] ^= const_v[5];
418 cv_l[6] ^= const_v[6];
419 cv_l[7] ^= const_v[7];
422inline void rotate_msg_gamma(lsh_u64 cv_r[8])
424 cv_r[1] = ROTL64(cv_r[1], g_gamma512[1]);
425 cv_r[2] = ROTL64(cv_r[2], g_gamma512[2]);
426 cv_r[3] = ROTL64(cv_r[3], g_gamma512[3]);
427 cv_r[4] = ROTL64(cv_r[4], g_gamma512[4]);
428 cv_r[5] = ROTL64(cv_r[5], g_gamma512[5]);
429 cv_r[6] = ROTL64(cv_r[6], g_gamma512[6]);
430 cv_r[7] = ROTL64(cv_r[7], g_gamma512[7]);
433inline void word_perm(lsh_u64 cv_l[8], lsh_u64 cv_r[8])
460template <
unsigned int Alpha,
unsigned int Beta>
461inline void mix(lsh_u64 cv_l[8], lsh_u64 cv_r[8],
const lsh_u64 const_v[8])
464 rotate_blk<Alpha>(cv_l);
465 xor_with_const(cv_l, const_v);
467 rotate_blk<Beta>(cv_r);
469 rotate_msg_gamma(cv_r);
476inline void compress(
LSH512_Context* ctx,
const lsh_u8 pdMsgBlk[LSH512_MSG_BLK_BYTE_LEN])
478 CRYPTOPP_ASSERT(ctx != NULLPTR);
483 const lsh_u64* const_v = NULL;
484 lsh_u64 *cv_l = ctx->cv_l;
485 lsh_u64 *cv_r = ctx->cv_r;
487 load_msg_blk(i_state, pdMsgBlk);
489 msg_add_even(cv_l, cv_r, i_state);
490 load_sc(&const_v, 0);
491 mix<ROT_EVEN_ALPHA, ROT_EVEN_BETA>(cv_l, cv_r, const_v);
492 word_perm(cv_l, cv_r);
494 msg_add_odd(cv_l, cv_r, i_state);
495 load_sc(&const_v, 8);
496 mix<ROT_ODD_ALPHA, ROT_ODD_BETA>(cv_l, cv_r, const_v);
497 word_perm(cv_l, cv_r);
499 for (
size_t i = 1; i < NUM_STEPS / 2; i++)
501 msg_exp_even(i_state);
502 msg_add_even(cv_l, cv_r, i_state);
503 load_sc(&const_v, 16 * i);
504 mix<ROT_EVEN_ALPHA, ROT_EVEN_BETA>(cv_l, cv_r, const_v);
505 word_perm(cv_l, cv_r);
507 msg_exp_odd(i_state);
508 msg_add_odd(cv_l, cv_r, i_state);
509 load_sc(&const_v, 16 * i + 8);
510 mix<ROT_ODD_ALPHA, ROT_ODD_BETA>(cv_l, cv_r, const_v);
511 word_perm(cv_l, cv_r);
514 msg_exp_even(i_state);
515 msg_add_even(cv_l, cv_r, i_state);
520inline void load_iv(lsh_u64 cv_l[8], lsh_u64 cv_r[8],
const lsh_u64 iv[16])
540inline void zero_iv(lsh_u64 cv_l[8], lsh_u64 cv_r[8])
542 memset(cv_l, 0, 8*
sizeof(lsh_u64));
543 memset(cv_r, 0, 8*
sizeof(lsh_u64));
548 lsh_u64* sub_msgs = ctx->sub_msgs;
550 memset(sub_msgs, 0x00, 32*
sizeof(lsh_u64));
555 CRYPTOPP_ASSERT(ctx != NULLPTR);
558 load_iv(ctx->cv_l, ctx->cv_r, LSH512_IV224);
563 CRYPTOPP_ASSERT(ctx != NULLPTR);
566 load_iv(ctx->cv_l, ctx->cv_r, LSH512_IV256);
571 CRYPTOPP_ASSERT(ctx != NULLPTR);
574 load_iv(ctx->cv_l, ctx->cv_r, LSH512_IV384);
579 CRYPTOPP_ASSERT(ctx != NULLPTR);
582 load_iv(ctx->cv_l, ctx->cv_r, LSH512_IV512);
589 CRYPTOPP_ASSERT(ctx != NULLPTR);
591 for (
size_t i = 0; i < HASH_VAL_MAX_WORD_LEN; i++){
592 ctx->cv_l[i] = loadLE64(ctx->cv_l[i] ^ ctx->cv_r[i]);
600 CRYPTOPP_ASSERT(ctx != NULLPTR);
601 CRYPTOPP_ASSERT(ctx->alg_type != 0);
602 CRYPTOPP_ASSERT(pbHashVal != NULLPTR);
604 lsh_uint alg_type = ctx->alg_type;
605 lsh_uint hash_val_byte_len = LSH_GET_HASHBYTE(alg_type);
606 lsh_uint hash_val_bit_len = LSH_GET_SMALL_HASHBIT(alg_type);
609 memcpy(pbHashVal, ctx->cv_l, hash_val_byte_len);
610 if (hash_val_bit_len){
611 pbHashVal[hash_val_byte_len-1] &= (((lsh_u8)0xff) << hash_val_bit_len);
619 CRYPTOPP_ASSERT(ctx != NULLPTR);
620 CRYPTOPP_ASSERT(ctx->alg_type != 0);
622 lsh_u32 alg_type = ctx->alg_type;
623 const lsh_u64* const_v = NULL;
624 ctx->remain_databitlen = 0;
627 case LSH_TYPE_512_512:
630 case LSH_TYPE_512_384:
633 case LSH_TYPE_512_256:
636 case LSH_TYPE_512_224:
643 lsh_u64* cv_l = ctx->cv_l;
644 lsh_u64* cv_r = ctx->cv_r;
647 cv_l[0] = LSH512_HASH_VAL_MAX_BYTE_LEN;
648 cv_l[1] = LSH_GET_HASHBIT(alg_type);
650 for (
size_t i = 0; i < NUM_STEPS / 2; i++)
653 load_sc(&const_v, i * 16);
654 mix<ROT_EVEN_ALPHA, ROT_EVEN_BETA>(cv_l, cv_r, const_v);
655 word_perm(cv_l, cv_r);
657 load_sc(&const_v, i * 16 + 8);
658 mix<ROT_ODD_ALPHA, ROT_ODD_BETA>(cv_l, cv_r, const_v);
659 word_perm(cv_l, cv_r);
665lsh_err lsh512_update(
LSH512_Context* ctx,
const lsh_u8* data,
size_t databitlen)
667 CRYPTOPP_ASSERT(ctx != NULLPTR);
668 CRYPTOPP_ASSERT(data != NULLPTR);
669 CRYPTOPP_ASSERT(databitlen % 8 == 0);
670 CRYPTOPP_ASSERT(ctx->alg_type != 0);
672 if (databitlen == 0){
677 size_t databytelen = databitlen >> 3;
679 const size_t pos2 = 0;
681 size_t remain_msg_byte =
static_cast<size_t>(ctx->remain_databitlen >> 3);
683 const size_t remain_msg_bit = 0;
685 if (remain_msg_byte >= LSH512_MSG_BLK_BYTE_LEN){
686 return LSH_ERR_INVALID_STATE;
688 if (remain_msg_bit > 0){
689 return LSH_ERR_INVALID_DATABITLEN;
692 if (databytelen + remain_msg_byte < LSH512_MSG_BLK_BYTE_LEN){
693 memcpy(ctx->last_block + remain_msg_byte, data, databytelen);
694 ctx->remain_databitlen += (lsh_uint)databitlen;
695 remain_msg_byte += (lsh_uint)databytelen;
697 ctx->last_block[remain_msg_byte] = data[databytelen] & ((0xff >> pos2) ^ 0xff);
702 if (remain_msg_byte > 0){
703 size_t more_byte = LSH512_MSG_BLK_BYTE_LEN - remain_msg_byte;
704 memcpy(ctx->last_block + remain_msg_byte, data, more_byte);
705 compress(ctx, ctx->last_block);
707 databytelen -= more_byte;
709 ctx->remain_databitlen = 0;
712 while (databytelen >= LSH512_MSG_BLK_BYTE_LEN)
718 data += LSH512_MSG_BLK_BYTE_LEN;
719 databytelen -= LSH512_MSG_BLK_BYTE_LEN;
722 if (databytelen > 0){
723 memcpy(ctx->last_block, data, databytelen);
724 ctx->remain_databitlen = (lsh_uint)(databytelen << 3);
728 ctx->last_block[databytelen] = data[databytelen] & ((0xff >> pos2) ^ 0xff);
729 ctx->remain_databitlen += pos2;
736 CRYPTOPP_ASSERT(ctx != NULLPTR);
737 CRYPTOPP_ASSERT(hashval != NULLPTR);
740 size_t remain_msg_byte =
static_cast<size_t>(ctx->remain_databitlen >> 3);
742 const size_t remain_msg_bit = 0;
744 if (remain_msg_byte >= LSH512_MSG_BLK_BYTE_LEN){
745 return LSH_ERR_INVALID_STATE;
749 ctx->last_block[remain_msg_byte] |= (0x1 << (7 - remain_msg_bit));
752 ctx->last_block[remain_msg_byte] = 0x80;
754 memset(ctx->last_block + remain_msg_byte + 1, 0, LSH512_MSG_BLK_BYTE_LEN - remain_msg_byte - 1);
756 compress(ctx, ctx->last_block);
759 get_hash(ctx, hashval);
764ANONYMOUS_NAMESPACE_END
766NAMESPACE_BEGIN(CryptoPP)
768#if defined(CRYPTOPP_ENABLE_64BIT_SSE)
769# if defined(CRYPTOPP_AVX2_AVAILABLE)
770 extern void LSH512_Base_Restart_AVX2(word64* state);
771 extern void LSH512_Base_Update_AVX2(word64* state,
const byte *input,
size_t size);
772 extern void LSH512_Base_TruncatedFinal_AVX2(word64* state,
byte *hash,
size_t size);
774# if defined(CRYPTOPP_SSSE3_AVAILABLE)
775 extern void LSH512_Base_Restart_SSSE3(word64* state);
776 extern void LSH512_Base_Update_SSSE3(word64* state,
const byte *input,
size_t size);
777 extern void LSH512_Base_TruncatedFinal_SSSE3(word64* state,
byte *hash,
size_t size);
783#if defined(CRYPTOPP_ENABLE_64BIT_SSE)
784#if defined(CRYPTOPP_AVX2_AVAILABLE)
789#if defined(CRYPTOPP_SSSE3_AVAILABLE)
799void LSH512_Base_Restart_CXX(word64* state)
801 state[RemainingBits] = 0;
802 LSH512_Context ctx(state, state[AlgorithmType], state[RemainingBits]);
803 lsh_err err = lsh512_init(&ctx);
805 if (err != LSH_SUCCESS)
809void LSH512_Base_Update_CXX(word64* state,
const byte *input,
size_t size)
811 LSH512_Context ctx(state, state[AlgorithmType], state[RemainingBits]);
812 lsh_err err = lsh512_update(&ctx, input, 8*size);
814 if (err != LSH_SUCCESS)
818void LSH512_Base_TruncatedFinal_CXX(word64* state,
byte *hash,
size_t)
820 LSH512_Context ctx(state, state[AlgorithmType], state[RemainingBits]);
821 lsh_err err = lsh512_final(&ctx, hash);
823 if (err != LSH_SUCCESS)
830#if defined(CRYPTOPP_AVX2_AVAILABLE) && defined(CRYPTOPP_ENABLE_64BIT_SSE)
832 LSH512_Base_Restart_AVX2(m_state);
835#if defined(CRYPTOPP_SSSE3_AVAILABLE) && defined(CRYPTOPP_ENABLE_64BIT_SSE)
837 LSH512_Base_Restart_SSSE3(m_state);
841 LSH512_Base_Restart_CXX(m_state);
846 CRYPTOPP_ASSERT(input != NULLPTR);
847 CRYPTOPP_ASSERT(size);
849#if defined(CRYPTOPP_AVX2_AVAILABLE) && defined(CRYPTOPP_ENABLE_64BIT_SSE)
851 LSH512_Base_Update_AVX2(m_state, input, size);
854#if defined(CRYPTOPP_SSSE3_AVAILABLE) && defined(CRYPTOPP_ENABLE_64BIT_SSE)
856 LSH512_Base_Update_SSSE3(m_state, input, size);
860 LSH512_Base_Update_CXX(m_state, input, size);
865 CRYPTOPP_ASSERT(hash != NULLPTR);
871 byte fullHash[LSH512_HASH_VAL_MAX_BYTE_LEN];
874#if defined(CRYPTOPP_AVX2_AVAILABLE) && defined(CRYPTOPP_ENABLE_64BIT_SSE)
876 LSH512_Base_TruncatedFinal_AVX2(m_state, copyOut ? fullHash : hash, size);
879#if defined(CRYPTOPP_SSSE3_AVAILABLE) && defined(CRYPTOPP_ENABLE_64BIT_SSE)
881 LSH512_Base_TruncatedFinal_SSSE3(m_state, copyOut ? fullHash : hash, size);
885 LSH512_Base_TruncatedFinal_CXX(m_state, copyOut ? fullHash : hash, size);
888 memcpy(hash, fullHash, size);
Base class for all exceptions thrown by the library.
@ OTHER_ERROR
Some other error occurred not belonging to other categories.
Access a block of memory.
void Restart()
Restart the hash.
unsigned int DigestSize() const
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
void Update(const byte *input, size_t size)
Updates a hash with additional input.
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Library configuration file.
unsigned int word32
32-bit unsigned datatype
Functions for CPU features and intrinsics.
Classes for the LSH hash functions.
Utility functions for the Crypto++ library.
T ConditionalByteReverse(ByteOrder order, T value)
Reverses bytes in a value depending upon endianness.
T rotlFixed(T x, unsigned int y)
Performs a left rotate.