From 874a54a66540e10cc6a30f3295955228e7e32cb9 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sat, 3 Apr 2021 17:02:39 +0200 Subject: [PATCH] Slightly tweak stepping algorithm to include a counter. --- libslunkcrypt/src/slunkcrypt.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/libslunkcrypt/src/slunkcrypt.c b/libslunkcrypt/src/slunkcrypt.c index 4cdc647..7378bab 100644 --- a/libslunkcrypt/src/slunkcrypt.c +++ b/libslunkcrypt/src/slunkcrypt.c @@ -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; }