20extern const char LSH256_SSE_FNAME[] = __FILE__;
22#if defined(CRYPTOPP_SSSE3_AVAILABLE) && defined(CRYPTOPP_ENABLE_64BIT_SSE)
24#if defined(CRYPTOPP_SSSE3_AVAILABLE)
25# include <emmintrin.h>
26# include <tmmintrin.h>
29#if defined(CRYPTOPP_XOP_AVAILABLE)
30# include <ammintrin.h>
33#if defined(CRYPTOPP_GCC_COMPATIBLE)
34# include <x86intrin.h>
37ANONYMOUS_NAMESPACE_BEGIN
41const unsigned int LSH256_MSG_BLK_BYTE_LEN = 128;
44const unsigned int LSH256_HASH_VAL_MAX_BYTE_LEN = 32;
47const unsigned int CV_WORD_LEN = 16;
48const unsigned int CONST_WORD_LEN = 8;
51const unsigned int NUM_STEPS = 26;
53const unsigned int ROT_EVEN_ALPHA = 29;
54const unsigned int ROT_EVEN_BETA = 1;
55const unsigned int ROT_ODD_ALPHA = 5;
56const unsigned int ROT_ODD_BETA = 17;
58const unsigned int LSH_TYPE_256_256 = 0x0000020;
59const unsigned int LSH_TYPE_256_224 = 0x000001C;
66const unsigned int LSH_SUCCESS = 0x0;
69const unsigned int LSH_ERR_INVALID_DATABITLEN = 0x2403;
70const unsigned int LSH_ERR_INVALID_STATE = 0x2404;
74const unsigned int AlgorithmType = 80;
75const unsigned int RemainingBits = 81;
83extern const word32 LSH256_IV224[CV_WORD_LEN];
84extern const word32 LSH256_IV256[CV_WORD_LEN];
85extern const word32 LSH256_StepConstants[CONST_WORD_LEN * NUM_STEPS];
90ANONYMOUS_NAMESPACE_BEGIN
93using CryptoPP::word32;
94using CryptoPP::rotlFixed;
95using CryptoPP::rotlConstant;
97using CryptoPP::GetBlock;
98using CryptoPP::LittleEndian;
99using CryptoPP::ConditionalByteReverse;
100using CryptoPP::LITTLE_ENDIAN_ORDER;
108using CryptoPP::LSH::LSH256_IV224;
109using CryptoPP::LSH::LSH256_IV256;
110using CryptoPP::LSH::LSH256_StepConstants;
112struct LSH256_SSSE3_Context
115 cv_l(state+0), cv_r(state+8), sub_msgs(state+16),
116 last_block(reinterpret_cast<
byte*>(state+48)),
117 remain_databitlen(remainingBitLength),
118 alg_type(static_cast<lsh_type>(algType)) {}
124 lsh_u32& remain_databitlen;
128struct LSH256_SSSE3_Internal
130 LSH256_SSSE3_Internal(
word32* state) :
131 submsg_e_l(state+16), submsg_e_r(state+24),
132 submsg_o_l(state+32), submsg_o_r(state+40) { }
144inline bool LSH_IS_LSH512(lsh_uint val) {
145 return (val & 0xf0000) == 0;
148inline lsh_uint LSH_GET_SMALL_HASHBIT(lsh_uint val) {
152inline lsh_uint LSH_GET_HASHBYTE(lsh_uint val) {
156inline lsh_uint LSH_GET_HASHBIT(lsh_uint val) {
157 return (LSH_GET_HASHBYTE(val) << 3) - LSH_GET_SMALL_HASHBIT(val);
160inline lsh_u32 loadLE32(lsh_u32 v) {
164lsh_u32 ROTL(lsh_u32 x, lsh_u32 r) {
169inline void load_msg_blk(LSH256_SSSE3_Internal* i_state,
const lsh_u8 msgblk[LSH256_MSG_BLK_BYTE_LEN])
172 lsh_u32* submsg_e_l = i_state->submsg_e_l;
173 lsh_u32* submsg_e_r = i_state->submsg_e_r;
174 lsh_u32* submsg_o_l = i_state->submsg_o_l;
175 lsh_u32* submsg_o_r = i_state->submsg_o_r;
177 _mm_storeu_si128(
M128_CAST(submsg_e_l+0),
179 _mm_storeu_si128(
M128_CAST(submsg_e_l+4),
181 _mm_storeu_si128(
M128_CAST(submsg_e_r+0),
183 _mm_storeu_si128(
M128_CAST(submsg_e_r+4),
185 _mm_storeu_si128(
M128_CAST(submsg_o_l+0),
187 _mm_storeu_si128(
M128_CAST(submsg_o_l+4),
189 _mm_storeu_si128(
M128_CAST(submsg_o_r+0),
191 _mm_storeu_si128(
M128_CAST(submsg_o_r+4),
195inline void msg_exp_even(LSH256_SSSE3_Internal* i_state)
199 lsh_u32* submsg_e_l = i_state->submsg_e_l;
200 lsh_u32* submsg_e_r = i_state->submsg_e_r;
201 lsh_u32* submsg_o_l = i_state->submsg_o_l;
202 lsh_u32* submsg_o_r = i_state->submsg_o_r;
204 _mm_storeu_si128(
M128_CAST(submsg_e_l+0), _mm_add_epi32(
208 _mm_loadu_si128(
CONST_M128_CAST(submsg_e_l+0)), _MM_SHUFFLE(1,0,2,3))));
210 _mm_storeu_si128(
M128_CAST(submsg_e_l+4), _mm_add_epi32(
214 _mm_loadu_si128(
CONST_M128_CAST(submsg_e_l+4)), _MM_SHUFFLE(2,1,0,3))));
216 _mm_storeu_si128(
M128_CAST(submsg_e_r+0), _mm_add_epi32(
220 _mm_loadu_si128(
CONST_M128_CAST(submsg_e_r+0)), _MM_SHUFFLE(1,0,2,3))));
222 _mm_storeu_si128(
M128_CAST(submsg_e_r+4), _mm_add_epi32(
226 _mm_loadu_si128(
CONST_M128_CAST(submsg_e_r+4)), _MM_SHUFFLE(2,1,0,3))));
229inline void msg_exp_odd(LSH256_SSSE3_Internal* i_state)
233 lsh_u32* submsg_e_l = i_state->submsg_e_l;
234 lsh_u32* submsg_e_r = i_state->submsg_e_r;
235 lsh_u32* submsg_o_l = i_state->submsg_o_l;
236 lsh_u32* submsg_o_r = i_state->submsg_o_r;
238 _mm_storeu_si128(
M128_CAST(submsg_o_l+0), _mm_add_epi32(
242 _mm_loadu_si128(
CONST_M128_CAST(submsg_o_l+0)), _MM_SHUFFLE(1,0,2,3))));
244 _mm_storeu_si128(
M128_CAST(submsg_o_l+4), _mm_add_epi32(
248 _mm_loadu_si128(
CONST_M128_CAST(submsg_o_l+4)), _MM_SHUFFLE(2,1,0,3))));
250 _mm_storeu_si128(
M128_CAST(submsg_o_r+0), _mm_add_epi32(
254 _mm_loadu_si128(
CONST_M128_CAST(submsg_o_r+0)), _MM_SHUFFLE(1,0,2,3))));
256 _mm_storeu_si128(
M128_CAST(submsg_o_r+4), _mm_add_epi32(
260 _mm_loadu_si128(
CONST_M128_CAST(submsg_o_r+4)), _MM_SHUFFLE(2,1,0,3))));
263inline void load_sc(
const lsh_u32** p_const_v,
size_t i)
267 *p_const_v = &LSH256_StepConstants[i];
270inline void msg_add_even(lsh_u32 cv_l[8], lsh_u32 cv_r[8], LSH256_SSSE3_Internal* i_state)
274 lsh_u32* submsg_e_l = i_state->submsg_e_l;
275 lsh_u32* submsg_e_r = i_state->submsg_e_r;
277 _mm_storeu_si128(
M128_CAST(cv_l+0), _mm_xor_si128(
280 _mm_storeu_si128(
M128_CAST(cv_l+4), _mm_xor_si128(
283 _mm_storeu_si128(
M128_CAST(cv_r+0), _mm_xor_si128(
286 _mm_storeu_si128(
M128_CAST(cv_r+4), _mm_xor_si128(
291inline void msg_add_odd(lsh_u32 cv_l[8], lsh_u32 cv_r[8], LSH256_SSSE3_Internal* i_state)
295 lsh_u32* submsg_o_l = i_state->submsg_o_l;
296 lsh_u32* submsg_o_r = i_state->submsg_o_r;
298 _mm_storeu_si128(
M128_CAST(cv_l), _mm_xor_si128(
301 _mm_storeu_si128(
M128_CAST(cv_l+4), _mm_xor_si128(
304 _mm_storeu_si128(
M128_CAST(cv_r), _mm_xor_si128(
307 _mm_storeu_si128(
M128_CAST(cv_r+4), _mm_xor_si128(
312inline void add_blk(lsh_u32 cv_l[8],
const lsh_u32 cv_r[8])
314 _mm_storeu_si128(
M128_CAST(cv_l), _mm_add_epi32(
317 _mm_storeu_si128(
M128_CAST(cv_l+4), _mm_add_epi32(
322template <
unsigned int R>
323inline void rotate_blk(lsh_u32 cv[8])
325#if defined(CRYPTOPP_XOP_AVAILABLE)
331 _mm_storeu_si128(
M128_CAST(cv), _mm_or_si128(
334 _mm_storeu_si128(
M128_CAST(cv+4), _mm_or_si128(
340inline void xor_with_const(lsh_u32* cv_l,
const lsh_u32* const_v)
342 _mm_storeu_si128(
M128_CAST(cv_l), _mm_xor_si128(
345 _mm_storeu_si128(
M128_CAST(cv_l+4), _mm_xor_si128(
350inline void rotate_msg_gamma(lsh_u32 cv_r[8])
355 _mm_set_epi8(12,15,14,13, 9,8,11,10, 6,5,4,7, 3,2,1,0)));
358 _mm_set_epi8(15,14,13,12, 10,9,8,11, 5,4,7,6, 0,3,2,1)));
361inline void word_perm(lsh_u32 cv_l[8], lsh_u32 cv_r[8])
363 _mm_storeu_si128(
M128_CAST(cv_l+0), _mm_shuffle_epi32(
365 _mm_storeu_si128(
M128_CAST(cv_l+4), _mm_shuffle_epi32(
367 _mm_storeu_si128(
M128_CAST(cv_r+0), _mm_shuffle_epi32(
369 _mm_storeu_si128(
M128_CAST(cv_r+4), _mm_shuffle_epi32(
379 _mm_storeu_si128(
M128_CAST(cv_r+0), temp);
386template <
unsigned int Alpha,
unsigned int Beta>
387inline void mix(lsh_u32 cv_l[8], lsh_u32 cv_r[8],
const lsh_u32 const_v[8])
390 rotate_blk<Alpha>(cv_l);
391 xor_with_const(cv_l, const_v);
393 rotate_blk<Beta>(cv_r);
395 rotate_msg_gamma(cv_r);
402inline void compress(LSH256_SSSE3_Context* ctx,
const lsh_u8 pdMsgBlk[LSH256_MSG_BLK_BYTE_LEN])
406 LSH256_SSSE3_Internal s_state(ctx->cv_l);
407 LSH256_SSSE3_Internal* i_state = &s_state;
409 const lsh_u32* const_v = NULL;
410 lsh_u32* cv_l = ctx->cv_l;
411 lsh_u32* cv_r = ctx->cv_r;
413 load_msg_blk(i_state, pdMsgBlk);
415 msg_add_even(cv_l, cv_r, i_state);
416 load_sc(&const_v, 0);
417 mix<ROT_EVEN_ALPHA, ROT_EVEN_BETA>(cv_l, cv_r, const_v);
418 word_perm(cv_l, cv_r);
420 msg_add_odd(cv_l, cv_r, i_state);
421 load_sc(&const_v, 8);
422 mix<ROT_ODD_ALPHA, ROT_ODD_BETA>(cv_l, cv_r, const_v);
423 word_perm(cv_l, cv_r);
425 for (
size_t i = 1; i < NUM_STEPS / 2; i++)
427 msg_exp_even(i_state);
428 msg_add_even(cv_l, cv_r, i_state);
429 load_sc(&const_v, 16 * i);
430 mix<ROT_EVEN_ALPHA, ROT_EVEN_BETA>(cv_l, cv_r, const_v);
431 word_perm(cv_l, cv_r);
433 msg_exp_odd(i_state);
434 msg_add_odd(cv_l, cv_r, i_state);
435 load_sc(&const_v, 16 * i + 8);
436 mix<ROT_ODD_ALPHA, ROT_ODD_BETA>(cv_l, cv_r, const_v);
437 word_perm(cv_l, cv_r);
440 msg_exp_even(i_state);
441 msg_add_even(cv_l, cv_r, i_state);
446inline void load_iv(lsh_u32 cv_l[8], lsh_u32 cv_r[8],
const lsh_u32 iv[16])
458inline void zero_iv(lsh_u32 cv_l[8], lsh_u32 cv_r[8])
460 _mm_storeu_si128(
M128_CAST(cv_l+0), _mm_setzero_si128());
461 _mm_storeu_si128(
M128_CAST(cv_l+4), _mm_setzero_si128());
462 _mm_storeu_si128(
M128_CAST(cv_r+0), _mm_setzero_si128());
463 _mm_storeu_si128(
M128_CAST(cv_r+4), _mm_setzero_si128());
466inline void zero_submsgs(LSH256_SSSE3_Context* ctx)
468 lsh_u32* sub_msgs = ctx->sub_msgs;
470 _mm_storeu_si128(
M128_CAST(sub_msgs+ 0), _mm_setzero_si128());
471 _mm_storeu_si128(
M128_CAST(sub_msgs+ 4), _mm_setzero_si128());
472 _mm_storeu_si128(
M128_CAST(sub_msgs+ 8), _mm_setzero_si128());
473 _mm_storeu_si128(
M128_CAST(sub_msgs+12), _mm_setzero_si128());
474 _mm_storeu_si128(
M128_CAST(sub_msgs+16), _mm_setzero_si128());
475 _mm_storeu_si128(
M128_CAST(sub_msgs+20), _mm_setzero_si128());
476 _mm_storeu_si128(
M128_CAST(sub_msgs+24), _mm_setzero_si128());
477 _mm_storeu_si128(
M128_CAST(sub_msgs+28), _mm_setzero_si128());
480inline void init224(LSH256_SSSE3_Context* ctx)
485 load_iv(ctx->cv_l, ctx->cv_r, LSH256_IV224);
488inline void init256(LSH256_SSSE3_Context* ctx)
493 load_iv(ctx->cv_l, ctx->cv_r, LSH256_IV256);
498inline void fin(LSH256_SSSE3_Context* ctx)
502 _mm_storeu_si128(
M128_CAST(ctx->cv_l+0), _mm_xor_si128(
505 _mm_storeu_si128(
M128_CAST(ctx->cv_l+4), _mm_xor_si128(
512inline void get_hash(LSH256_SSSE3_Context* ctx, lsh_u8* pbHashVal)
518 lsh_uint alg_type = ctx->alg_type;
519 lsh_uint hash_val_byte_len = LSH_GET_HASHBYTE(alg_type);
520 lsh_uint hash_val_bit_len = LSH_GET_SMALL_HASHBIT(alg_type);
523 std::memcpy(pbHashVal, ctx->cv_l, hash_val_byte_len);
524 if (hash_val_bit_len){
525 pbHashVal[hash_val_byte_len-1] &= (((lsh_u8)0xff) << hash_val_bit_len);
531lsh_err lsh256_ssse3_init(LSH256_SSSE3_Context* ctx)
536 lsh_u32 alg_type = ctx->alg_type;
537 const lsh_u32* const_v = NULL;
538 ctx->remain_databitlen = 0;
542 case LSH_TYPE_256_256:
545 case LSH_TYPE_256_224:
552 lsh_u32* cv_l = ctx->cv_l;
553 lsh_u32* cv_r = ctx->cv_r;
556 cv_l[0] = LSH256_HASH_VAL_MAX_BYTE_LEN;
557 cv_l[1] = LSH_GET_HASHBIT(alg_type);
559 for (
size_t i = 0; i < NUM_STEPS / 2; i++)
562 load_sc(&const_v, i * 16);
563 mix<ROT_EVEN_ALPHA, ROT_EVEN_BETA>(cv_l, cv_r, const_v);
564 word_perm(cv_l, cv_r);
566 load_sc(&const_v, i * 16 + 8);
567 mix<ROT_ODD_ALPHA, ROT_ODD_BETA>(cv_l, cv_r, const_v);
568 word_perm(cv_l, cv_r);
574lsh_err lsh256_ssse3_update(LSH256_SSSE3_Context* ctx,
const lsh_u8* data,
size_t databitlen)
581 if (databitlen == 0){
586 size_t databytelen = databitlen >> 3;
588 const size_t pos2 = 0;
590 size_t remain_msg_byte = ctx->remain_databitlen >> 3;
592 const size_t remain_msg_bit = 0;
594 if (remain_msg_byte >= LSH256_MSG_BLK_BYTE_LEN){
595 return LSH_ERR_INVALID_STATE;
597 if (remain_msg_bit > 0){
598 return LSH_ERR_INVALID_DATABITLEN;
601 if (databytelen + remain_msg_byte < LSH256_MSG_BLK_BYTE_LEN)
603 std::memcpy(ctx->last_block + remain_msg_byte, data, databytelen);
604 ctx->remain_databitlen += (lsh_uint)databitlen;
605 remain_msg_byte += (lsh_uint)databytelen;
607 ctx->last_block[remain_msg_byte] = data[databytelen] & ((0xff >> pos2) ^ 0xff);
612 if (remain_msg_byte > 0){
613 size_t more_byte = LSH256_MSG_BLK_BYTE_LEN - remain_msg_byte;
614 std::memcpy(ctx->last_block + remain_msg_byte, data, more_byte);
615 compress(ctx, ctx->last_block);
617 databytelen -= more_byte;
619 ctx->remain_databitlen = 0;
622 while (databytelen >= LSH256_MSG_BLK_BYTE_LEN)
628 data += LSH256_MSG_BLK_BYTE_LEN;
629 databytelen -= LSH256_MSG_BLK_BYTE_LEN;
632 if (databytelen > 0){
633 std::memcpy(ctx->last_block, data, databytelen);
634 ctx->remain_databitlen = (lsh_uint)(databytelen << 3);
638 ctx->last_block[databytelen] = data[databytelen] & ((0xff >> pos2) ^ 0xff);
639 ctx->remain_databitlen += pos2;
645lsh_err lsh256_ssse3_final(LSH256_SSSE3_Context* ctx, lsh_u8* hashval)
651 size_t remain_msg_byte = ctx->remain_databitlen >> 3;
653 const size_t remain_msg_bit = 0;
655 if (remain_msg_byte >= LSH256_MSG_BLK_BYTE_LEN){
656 return LSH_ERR_INVALID_STATE;
660 ctx->last_block[remain_msg_byte] |= (0x1 << (7 - remain_msg_bit));
663 ctx->last_block[remain_msg_byte] = 0x80;
665 std::memset(ctx->last_block + remain_msg_byte + 1, 0, LSH256_MSG_BLK_BYTE_LEN - remain_msg_byte - 1);
667 compress(ctx, ctx->last_block);
670 get_hash(ctx, hashval);
675ANONYMOUS_NAMESPACE_END
680void LSH256_Base_Restart_SSSE3(
word32* state)
682 state[RemainingBits] = 0;
683 LSH256_SSSE3_Context ctx(state, state[AlgorithmType], state[RemainingBits]);
684 lsh_err err = lsh256_ssse3_init(&ctx);
686 if (err != LSH_SUCCESS)
691void LSH256_Base_Update_SSSE3(
word32* state,
const byte *input,
size_t size)
693 LSH256_SSSE3_Context ctx(state, state[AlgorithmType], state[RemainingBits]);
694 lsh_err err = lsh256_ssse3_update(&ctx, input, 8*size);
696 if (err != LSH_SUCCESS)
701void LSH256_Base_TruncatedFinal_SSSE3(
word32* state,
byte *hash,
size_t)
703 LSH256_SSSE3_Context ctx(state, state[AlgorithmType], state[RemainingBits]);
704 lsh_err err = lsh256_ssse3_final(&ctx, hash);
706 if (err != LSH_SUCCESS)
#define M128_CAST(x)
Clang workaround.
#define CONST_M128_CAST(x)
Clang workaround.
Base class for all exceptions thrown by the library.
@ OTHER_ERROR
Some other error occurred not belonging to other categories.
Library configuration file.
unsigned char byte
8-bit unsigned datatype
unsigned int word32
32-bit unsigned datatype
Functions for CPU features and intrinsics.
@ LITTLE_ENDIAN_ORDER
byte order is little-endian
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.
Crypto++ library namespace.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.