Slightly tweak stepping algorithm to include a counter.

This commit is contained in:
LoRd_MuldeR 2021-04-03 17:02:39 +02:00
parent 80db0ac404
commit 874a54a665
Signed by: mulder
GPG Key ID: 2B5913365F57E03F

View File

@ -57,6 +57,7 @@ typedef struct
{
int reverse_mode;
uint8_t wheel[256U][256U];
uint32_t counter;
rand_state_t random;
}
crypt_state_t;
@ -229,6 +230,10 @@ static int initialize_state(crypt_state_t *const state, const uint64_t nonce, co
slunkcrypt_bzero(state, sizeof(crypt_state_t));
state->reverse_mode = reverse;
/* initialize counter */
random_seed(&state->random, nonce, (uint16_t)(-1), passwd, passwd_len);
state->counter = random_next(&state->random);
/* set up the wheel permutations */
for (r = 0U; r < 256U; ++r)
{
@ -266,7 +271,7 @@ static int initialize_state(crypt_state_t *const state, const uint64_t nonce, co
random_seed(&state->random, nonce, 256U, passwd, passwd_len);
return SLUNKCRYPT_SUCCESS;
/* user abort request */
/* aborted */
aborted:
slunkcrypt_bzero(state, sizeof(crypt_state_t));
return SLUNKCRYPT_ABORTED;
@ -276,28 +281,27 @@ aborted:
// Encrypt / Decrypt
// ==========================================================================
static FORCE_INLINE void calculate_offsets(uint8_t *const offset, rand_state_t *const state, const int reverse)
static FORCE_INLINE void update_offset(uint8_t *const offset, uint32_t seed, rand_state_t *const state, const int reverse)
{
uint32_t temp = 0U;
size_t i;
for (i = 0U; i < 256U; ++i, temp >>= CHAR_BIT)
for (i = 0U; i < 256U; ++i, seed >>= CHAR_BIT)
{
if (!temp)
if (i && (!(i & 3U)))
{
temp = random_next(state);
seed = random_next(state);
}
offset[reverse ? (255U - i) : i] = (uint8_t)temp;
offset[reverse ? (255U - i) : i] = (uint8_t)seed;
}
}
static FORCE_INLINE uint8_t process_next_symbol(crypt_state_t *const crypt_state_t, uint8_t value)
static FORCE_INLINE uint8_t process_next_symbol(crypt_state_t *const state, uint8_t value)
{
uint8_t offset[256U];
size_t i;
calculate_offsets(offset, &crypt_state_t->random, crypt_state_t->reverse_mode);
update_offset(offset, state->counter++, &state->random, state->reverse_mode);
for (i = 0U; i < 256U; ++i)
{
value = (crypt_state_t->wheel[i][(value + offset[i]) & 0xFF] - offset[i]) & 0xFF;
value = (state->wheel[i][(value + offset[i]) & 0xFF] - offset[i]) & 0xFF;
}
return value;
}