mirror of
https://github.com/shadps4-emu/ext-cryptopp.git
synced 2025-02-17 03:48:38 +00:00
Tighten use of AVX_Cleanup
This commit is contained in:
parent
551c428166
commit
b49d7c8baf
44
lsh256.cpp
44
lsh256.cpp
@ -120,8 +120,12 @@ struct LSH256_Internal
|
||||
};
|
||||
|
||||
#if defined(CRYPTOPP_LSH256_AVX_AVAILABLE)
|
||||
// Clear upper bits on entry and exit
|
||||
struct AVX_Cleanup
|
||||
{
|
||||
AVX_Cleanup() {
|
||||
_mm256_zeroupper();
|
||||
}
|
||||
~AVX_Cleanup() {
|
||||
_mm256_zeroupper();
|
||||
}
|
||||
@ -782,6 +786,10 @@ inline void compress(LSH256_Context* ctx, const lsh_u8 pdMsgBlk[LSH256_MSG_BLK_B
|
||||
lsh_u32* cv_l = ctx->cv_l;
|
||||
lsh_u32* cv_r = ctx->cv_r;
|
||||
|
||||
#if defined(CRYPTOPP_LSH256_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
load_msg_blk(i_state, pdMsgBlk);
|
||||
|
||||
msg_add_even(cv_l, cv_r, i_state);
|
||||
@ -873,6 +881,10 @@ inline void init224(LSH256_Context* ctx)
|
||||
{
|
||||
CRYPTOPP_ASSERT(ctx != NULLPTR);
|
||||
|
||||
#if defined(CRYPTOPP_LSH256_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
zero_submsgs(ctx);
|
||||
load_iv(ctx->cv_l, ctx->cv_r, g_IV224);
|
||||
}
|
||||
@ -881,6 +893,10 @@ inline void init256(LSH256_Context* ctx)
|
||||
{
|
||||
CRYPTOPP_ASSERT(ctx != NULLPTR);
|
||||
|
||||
#if defined(CRYPTOPP_LSH256_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
zero_submsgs(ctx);
|
||||
load_iv(ctx->cv_l, ctx->cv_r, g_IV256);
|
||||
}
|
||||
@ -891,6 +907,10 @@ inline void fin(LSH256_Context* ctx)
|
||||
{
|
||||
CRYPTOPP_ASSERT(ctx != NULLPTR);
|
||||
|
||||
#if defined(CRYPTOPP_LSH256_AVX2_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
#if defined(CRYPTOPP_LSH256_AVX2_AVAILABLE)
|
||||
_mm256_storeu_si256(M256_CAST(ctx->cv_l+0), _mm256_xor_si256(
|
||||
_mm256_loadu_si256(CONST_M256_CAST(ctx->cv_l+0)),
|
||||
@ -935,13 +955,8 @@ lsh_err lsh256_init(LSH256_Context* ctx)
|
||||
CRYPTOPP_ASSERT(ctx != NULLPTR);
|
||||
CRYPTOPP_ASSERT(ctx->algtype != 0);
|
||||
|
||||
#if defined(CRYPTOPP_LSH256_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
lsh_u32 algtype = ctx->algtype;
|
||||
const lsh_u32* const_v = NULL;
|
||||
|
||||
ctx->remain_databitlen = 0;
|
||||
|
||||
switch (algtype)
|
||||
@ -965,6 +980,10 @@ lsh_err lsh256_init(LSH256_Context* ctx)
|
||||
ctx->cv_l[0] = LSH256_HASH_VAL_MAX_BYTE_LEN;
|
||||
ctx->cv_l[1] = LSH_GET_HASHBIT(algtype);
|
||||
|
||||
#if defined(CRYPTOPP_LSH256_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
for (size_t i = 0; i < NUM_STEPS / 2; i++)
|
||||
{
|
||||
//Mix
|
||||
@ -991,17 +1010,13 @@ lsh_err lsh256_update(LSH256_Context* ctx, const lsh_u8* data, size_t databitlen
|
||||
return LSH_SUCCESS;
|
||||
}
|
||||
|
||||
#if defined(CRYPTOPP_LSH256_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
size_t databytelen = databitlen >> 3;
|
||||
lsh_uint pos2 = databitlen & 0x7;
|
||||
|
||||
// We are byte oriented. remain_msg_bit will always be 0.
|
||||
lsh_uint remain_msg_byte = ctx->remain_databitlen >> 3;
|
||||
// lsh_uint remain_msg_bit = ctx->remain_databitlen & 7;
|
||||
lsh_uint remain_msg_bit = 0;
|
||||
const lsh_uint remain_msg_bit = 0;
|
||||
|
||||
if (remain_msg_byte >= LSH256_MSG_BLK_BYTE_LEN){
|
||||
return LSH_ERR_INVALID_STATE;
|
||||
@ -1033,6 +1048,9 @@ lsh_err lsh256_update(LSH256_Context* ctx, const lsh_u8* data, size_t databitlen
|
||||
|
||||
while (databytelen >= LSH256_MSG_BLK_BYTE_LEN)
|
||||
{
|
||||
// This call to compress caused some trouble.
|
||||
// The data pointer can become unaligned in the
|
||||
// previous block.
|
||||
compress(ctx, data);
|
||||
data += LSH256_MSG_BLK_BYTE_LEN;
|
||||
databytelen -= LSH256_MSG_BLK_BYTE_LEN;
|
||||
@ -1059,7 +1077,7 @@ lsh_err lsh256_final(LSH256_Context* ctx, lsh_u8* hashval)
|
||||
// We are byte oriented. remain_msg_bit will always be 0.
|
||||
lsh_uint remain_msg_byte = ctx->remain_databitlen >> 3;
|
||||
// lsh_uint remain_msg_bit = ctx->remain_databitlen & 7;
|
||||
lsh_uint remain_msg_bit = 0;
|
||||
const lsh_uint remain_msg_bit = 0;
|
||||
|
||||
if (remain_msg_byte >= LSH256_MSG_BLK_BYTE_LEN){
|
||||
return LSH_ERR_INVALID_STATE;
|
||||
@ -1073,10 +1091,6 @@ lsh_err lsh256_final(LSH256_Context* ctx, lsh_u8* hashval)
|
||||
}
|
||||
memset(ctx->last_block + remain_msg_byte + 1, 0, LSH256_MSG_BLK_BYTE_LEN - remain_msg_byte - 1);
|
||||
|
||||
#if defined(CRYPTOPP_LSH256_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
compress(ctx, ctx->last_block);
|
||||
|
||||
fin(ctx);
|
||||
|
49
lsh512.cpp
49
lsh512.cpp
@ -122,8 +122,12 @@ struct LSH512_Internal
|
||||
};
|
||||
|
||||
#if defined(CRYPTOPP_LSH512_AVX_AVAILABLE)
|
||||
// Clear upper bits on entry and exit
|
||||
struct AVX_Cleanup
|
||||
{
|
||||
AVX_Cleanup() {
|
||||
_mm256_zeroupper();
|
||||
}
|
||||
~AVX_Cleanup() {
|
||||
_mm256_zeroupper();
|
||||
}
|
||||
@ -1034,6 +1038,10 @@ inline void compress(LSH512_Context* ctx, const lsh_u8 pdMsgBlk[LSH512_MSG_BLK_B
|
||||
lsh_u64 *cv_l = ctx->cv_l;
|
||||
lsh_u64 *cv_r = ctx->cv_r;
|
||||
|
||||
#if defined(CRYPTOPP_LSH512_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
load_msg_blk(i_state, pdMsgBlk);
|
||||
|
||||
msg_add_even(cv_l, cv_r, i_state);
|
||||
@ -1157,6 +1165,10 @@ inline void init224(LSH512_Context* ctx)
|
||||
{
|
||||
CRYPTOPP_ASSERT(ctx != NULLPTR);
|
||||
|
||||
#if defined(CRYPTOPP_LSH512_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
zero_submsgs(ctx);
|
||||
load_iv(ctx->cv_l, ctx->cv_r, g_IV224);
|
||||
}
|
||||
@ -1165,6 +1177,10 @@ inline void init256(LSH512_Context* ctx)
|
||||
{
|
||||
CRYPTOPP_ASSERT(ctx != NULLPTR);
|
||||
|
||||
#if defined(CRYPTOPP_LSH512_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
zero_submsgs(ctx);
|
||||
load_iv(ctx->cv_l, ctx->cv_r, g_IV256);
|
||||
}
|
||||
@ -1173,6 +1189,10 @@ inline void init384(LSH512_Context* ctx)
|
||||
{
|
||||
CRYPTOPP_ASSERT(ctx != NULLPTR);
|
||||
|
||||
#if defined(CRYPTOPP_LSH512_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
zero_submsgs(ctx);
|
||||
load_iv(ctx->cv_l, ctx->cv_r, g_IV384);
|
||||
}
|
||||
@ -1195,6 +1215,10 @@ inline void fin(LSH512_Context* ctx)
|
||||
{
|
||||
CRYPTOPP_ASSERT(ctx != NULLPTR);
|
||||
|
||||
#if defined(CRYPTOPP_LSH512_AVX2_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
#if defined(CRYPTOPP_LSH512_AVX2_AVAILABLE)
|
||||
_mm256_storeu_si256(M256_CAST(ctx->cv_l+0), _mm256_xor_si256(
|
||||
_mm256_loadu_si256(CONST_M256_CAST(ctx->cv_l+0)),
|
||||
@ -1249,13 +1273,8 @@ lsh_err lsh512_init(LSH512_Context* ctx)
|
||||
CRYPTOPP_ASSERT(ctx != NULLPTR);
|
||||
CRYPTOPP_ASSERT(ctx->algtype != 0);
|
||||
|
||||
#if defined(CRYPTOPP_LSH512_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
lsh_u32 algtype = ctx->algtype;
|
||||
const lsh_u64* const_v = NULL;
|
||||
|
||||
ctx->remain_databitlen = 0;
|
||||
|
||||
switch (algtype){
|
||||
@ -1280,9 +1299,14 @@ lsh_err lsh512_init(LSH512_Context* ctx)
|
||||
|
||||
memset(cv_l, 0, 8 * sizeof(lsh_u64));
|
||||
memset(cv_r, 0, 8 * sizeof(lsh_u64));
|
||||
|
||||
cv_l[0] = LSH512_HASH_VAL_MAX_BYTE_LEN;
|
||||
cv_l[1] = LSH_GET_HASHBIT(algtype);
|
||||
|
||||
#if defined(CRYPTOPP_LSH512_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
for (size_t i = 0; i < NUM_STEPS / 2; i++)
|
||||
{
|
||||
//Mix
|
||||
@ -1309,17 +1333,13 @@ lsh_err lsh512_update(LSH512_Context* ctx, const lsh_u8* data, size_t databitlen
|
||||
return LSH_SUCCESS;
|
||||
}
|
||||
|
||||
#if defined(CRYPTOPP_LSH512_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
size_t databytelen = databitlen >> 3;
|
||||
lsh_uint pos2 = databitlen & 0x7;
|
||||
|
||||
// We are byte oriented. remain_msg_bit will always be 0.
|
||||
lsh_uint remain_msg_byte = ctx->remain_databitlen >> 3;
|
||||
// remain_msg_bit = ctx->remain_databitlen & 7;
|
||||
lsh_uint remain_msg_bit = 0;
|
||||
const lsh_uint remain_msg_bit = 0;
|
||||
|
||||
if (remain_msg_byte >= LSH512_MSG_BLK_BYTE_LEN){
|
||||
return LSH_ERR_INVALID_STATE;
|
||||
@ -1350,6 +1370,9 @@ lsh_err lsh512_update(LSH512_Context* ctx, const lsh_u8* data, size_t databitlen
|
||||
|
||||
while (databytelen >= LSH512_MSG_BLK_BYTE_LEN)
|
||||
{
|
||||
// This call to compress caused some trouble.
|
||||
// The data pointer can become unaligned in the
|
||||
// previous block.
|
||||
compress(ctx, data);
|
||||
data += LSH512_MSG_BLK_BYTE_LEN;
|
||||
databytelen -= LSH512_MSG_BLK_BYTE_LEN;
|
||||
@ -1375,7 +1398,7 @@ lsh_err lsh512_final(LSH512_Context* ctx, lsh_u8* hashval)
|
||||
// We are byte oriented. remain_msg_bit will always be 0.
|
||||
lsh_uint remain_msg_byte = ctx->remain_databitlen >> 3;
|
||||
// lsh_uint remain_msg_bit = ctx->remain_databitlen & 7;
|
||||
lsh_uint remain_msg_bit = 0;
|
||||
const lsh_uint remain_msg_bit = 0;
|
||||
|
||||
if (remain_msg_byte >= LSH512_MSG_BLK_BYTE_LEN){
|
||||
return LSH_ERR_INVALID_STATE;
|
||||
@ -1389,10 +1412,6 @@ lsh_err lsh512_final(LSH512_Context* ctx, lsh_u8* hashval)
|
||||
}
|
||||
memset(ctx->last_block + remain_msg_byte + 1, 0, LSH512_MSG_BLK_BYTE_LEN - remain_msg_byte - 1);
|
||||
|
||||
#if defined(CRYPTOPP_LSH512_AVX_AVAILABLE)
|
||||
AVX_Cleanup cleanup;
|
||||
#endif
|
||||
|
||||
compress(ctx, ctx->last_block);
|
||||
|
||||
fin(ctx);
|
||||
|
Loading…
x
Reference in New Issue
Block a user