Some improvements to the PRNG code.

This commit is contained in:
LoRd_MuldeR 2021-03-18 23:02:55 +01:00
parent 11951fc33b
commit 1b049d5291
Signed by: mulder
GPG Key ID: 2B5913365F57E03F

View File

@ -40,10 +40,16 @@ const char* const SLUNKCRYPT_BUILD = __DATE__ " " __TIME__;
typedef struct typedef struct
{ {
uint64_t a, b; uint64_t a, b, c;
} }
key_data_t; key_data_t;
typedef struct
{
uint32_t x, y, z, w, v, d;
}
rand_state_t;
typedef struct typedef struct
{ {
uint8_t wheel_fwd[256U][256U]; uint8_t wheel_fwd[256U][256U];
@ -56,13 +62,6 @@ typedef struct
} }
crypt_state_t; crypt_state_t;
typedef struct
{
uint32_t a, b, c, d;
uint32_t counter;
}
rand_state_t;
// ========================================================================== // ==========================================================================
// Abort flag // Abort flag
// ========================================================================== // ==========================================================================
@ -156,11 +155,11 @@ static uint64_t hash_code_next(const uint64_t salt, const uint8_t* const data, c
// Key derivation // Key derivation
// ========================================================================== // ==========================================================================
static FORCE_INLINE uint64_t keygen_loop(uint64_t salt, const uint16_t pepper, const uint8_t* const passwd, const size_t passwd_len) static FORCE_INLINE uint64_t keygen_loop(uint64_t salt, const uint16_t i, const uint8_t* const passwd, const size_t passwd_len)
{ {
uint64_t result = salt = hash_code_init(salt, pepper, passwd, passwd_len); size_t u;
size_t i; uint64_t result = salt = hash_code_init(salt, i, passwd, passwd_len);
for (i = 1U; i < 99971U; ++i) for (u = 1U; u < 99971U; ++u)
{ {
result ^= salt = hash_code_next(salt, passwd, passwd_len); result ^= salt = hash_code_next(salt, passwd, passwd_len);
} }
@ -169,35 +168,35 @@ static FORCE_INLINE uint64_t keygen_loop(uint64_t salt, const uint16_t pepper, c
static void generate_key(key_data_t *const key, const uint64_t salt, const uint16_t pepper, const uint8_t* const passwd, const size_t passwd_len) static void generate_key(key_data_t *const key, const uint64_t salt, const uint16_t pepper, const uint8_t* const passwd, const size_t passwd_len)
{ {
key->a = keygen_loop(salt, pepper & 0x7FFF, passwd, passwd_len); key->a = keygen_loop(salt, (pepper & 0x3FFF) | 0x0000, passwd, passwd_len);
key->b = keygen_loop(salt, pepper | 0x8000, passwd, passwd_len); key->b = keygen_loop(salt, (pepper & 0x3FFF) | 0x4000, passwd, passwd_len);
key->c = keygen_loop(salt, (pepper & 0x3FFF) | 0x8000, passwd, passwd_len);
} }
// ========================================================================== // ==========================================================================
// PRNG // PRNG
// ========================================================================== // ==========================================================================
static void random_init(rand_state_t* const state, const uint64_t seed_0, const uint64_t seed_1) static void random_init(rand_state_t *const state, const key_data_t *const key)
{ {
slunkcrypt_bzero(state, sizeof(rand_state_t)); slunkcrypt_bzero(state, sizeof(rand_state_t));
state->a = lower_u64(seed_0); state->x = lower_u64(key->a);
state->b = upper_u64(seed_0); state->y = upper_u64(key->a);
state->c = lower_u64(seed_1); state->z = lower_u64(key->b);
state->d = upper_u64(seed_1); state->w = upper_u64(key->b);
state->v = lower_u64(key->c);
state->d = upper_u64(key->c);
} }
static uint32_t random_next(rand_state_t *const state) static uint32_t random_next(rand_state_t *const state)
{ {
uint32_t t = state->d; const uint32_t t = state->x ^ (state->x >> 2);
const uint32_t s = state->a; state->x = state->y;
state->d = state->c; state->y = state->z;
state->c = state->b; state->z = state->w;
state->b = s; state->w = state->v;
t ^= t >> 2; state->v ^= (state->v << 4) ^ t ^ (t << 1);
t ^= t << 1; return (state->d += 0x000587C5) + state->v;
t ^= s ^ (s << 4);
state->a = t;
return t + (state->counter += 362437U);
} }
static void random_seed(rand_state_t* const state, const uint64_t salt, const uint16_t pepper, const uint8_t *const passwd, const size_t passwd_len) static void random_seed(rand_state_t* const state, const uint64_t salt, const uint16_t pepper, const uint8_t *const passwd, const size_t passwd_len)
@ -205,11 +204,11 @@ static void random_seed(rand_state_t* const state, const uint64_t salt, const ui
key_data_t key; key_data_t key;
size_t i; size_t i;
generate_key(&key, salt, pepper, passwd, passwd_len); generate_key(&key, salt, pepper, passwd, passwd_len);
random_init(state, key.a, key.b); random_init(state, &key);
slunkcrypt_bzero(&key, sizeof(key_data_t)); slunkcrypt_bzero(&key, sizeof(key_data_t));
for (i = 0U; i < 97U; ++i) for (i = 0U; i < 97U; ++i)
{ {
UNUSED volatile uint32_t u = random_next(state); UNUSED volatile uint32_t q = random_next(state);
} }
} }