Revamped the stepping algorithm.

This commit is contained in:
LoRd_MuldeR 2021-03-27 16:19:26 +01:00
parent bdab06cd80
commit 73f2e71342
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
2 changed files with 36 additions and 28 deletions

View File

@ -57,6 +57,8 @@ static const char PASSWD_SYMBOLS[] =
static const size_t RCMD_PWDLEN_LENGTH = 12U; static const size_t RCMD_PWDLEN_LENGTH = 12U;
static const size_t DFLT_PWDLEN_LENGTH = 20U; static const size_t DFLT_PWDLEN_LENGTH = 20U;
static const clock_t UPDATE_INTERVAL = (clock_t)(1.5708 * CLOCKS_PER_SEC);
static const uint64_t MAGIC_NUMBER = 0x243F6A8885A308D3ull; static const uint64_t MAGIC_NUMBER = 0x243F6A8885A308D3ull;
// ========================================================================== // ==========================================================================
@ -349,7 +351,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
{ {
break; /*EOF*/ break; /*EOF*/
} }
if ((clk_now = clock()) - clk_update > CLOCKS_PER_SEC) if ((clk_now = clock()) - clk_update > UPDATE_INTERVAL)
{ {
FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%% "), (bytes_read / ((double)file_size)) * 100.0); FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%% "), (bytes_read / ((double)file_size)) * 100.0);
fflush(stderr); fflush(stderr);
@ -521,7 +523,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
{ {
break; /*EOF*/ break; /*EOF*/
} }
if ((clk_now = clock()) - clk_update > CLOCKS_PER_SEC) if ((clk_now = clock()) - clk_update > UPDATE_INTERVAL)
{ {
FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%% "), (bytes_read / ((double)read_limit)) * 100.0); FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%% "), (bytes_read / ((double)read_limit)) * 100.0);
fflush(stderr); fflush(stderr);

View File

@ -54,10 +54,10 @@ typedef struct
{ {
uint8_t wheel_fwd[256U][256U]; uint8_t wheel_fwd[256U][256U];
uint8_t wheel_bwd[256U][256U]; uint8_t wheel_bwd[256U][256U];
uint8_t step_fwd[256U]; uint8_t step_fwd[247U];
uint8_t step_bwd[256U]; uint8_t step_bwd[247U];
uint8_t rotation_fwd[2U][256U]; uint8_t rotation_fwd[256U];
uint8_t rotation_bwd[2U][256U]; uint8_t rotation_bwd[256U];
uint8_t counter; uint8_t counter;
} }
crypt_state_t; crypt_state_t;
@ -230,8 +230,7 @@ static int initialize_state(crypt_state_t* const crypt_state, const uint64_t non
for (r = 0U; r < 256U; ++r) for (r = 0U; r < 256U; ++r)
{ {
random_seed(&rand_state, nonce, (uint16_t)r, passwd, passwd_len); random_seed(&rand_state, nonce, (uint16_t)r, passwd, passwd_len);
crypt_state->rotation_bwd[0U][255U - r] = crypt_state->rotation_fwd[0U][r] = (uint8_t)random_next(&rand_state); crypt_state->rotation_bwd[255U - r] = crypt_state->rotation_fwd[r] = (uint8_t)random_next(&rand_state);
crypt_state->rotation_bwd[1U][255U - r] = crypt_state->rotation_fwd[1U][r] = 0U;
for (i = 0U; i < 256U; ++i) for (i = 0U; i < 256U; ++i)
{ {
const size_t j = random_next(&rand_state) % (i + 1U); const size_t j = random_next(&rand_state) % (i + 1U);
@ -251,7 +250,7 @@ static int initialize_state(crypt_state_t* const crypt_state, const uint64_t non
/* set up stepping */ /* set up stepping */
random_seed(&rand_state, nonce, 256U, passwd, passwd_len); random_seed(&rand_state, nonce, 256U, passwd, passwd_len);
for (i = 0U; i < 256U; ++i) for (i = 0U; i < 247U; ++i)
{ {
const size_t j = random_next(&rand_state) % (i + 1U); const size_t j = random_next(&rand_state) % (i + 1U);
if (j != i) if (j != i)
@ -259,8 +258,8 @@ static int initialize_state(crypt_state_t* const crypt_state, const uint64_t non
crypt_state->step_fwd[i] = crypt_state->step_fwd[j]; crypt_state->step_fwd[i] = crypt_state->step_fwd[j];
crypt_state->step_bwd[i] = crypt_state->step_bwd[j]; crypt_state->step_bwd[i] = crypt_state->step_bwd[j];
} }
crypt_state->step_fwd[j] = (uint8_t)i; crypt_state->step_fwd[j] = (uint8_t)( 6U + i);
crypt_state->step_bwd[j] = (uint8_t)(255U - i); crypt_state->step_bwd[j] = (uint8_t)(249U - i);
} }
slunkcrypt_bzero(&rand_state, sizeof(rand_state_t)); slunkcrypt_bzero(&rand_state, sizeof(rand_state_t));
@ -271,41 +270,48 @@ static int initialize_state(crypt_state_t* const crypt_state, const uint64_t non
// Encrypt / Decrypt // Encrypt / Decrypt
// ========================================================================== // ==========================================================================
static FORCE_INLINE void increment(uint8_t *const arr, const int rev) static FORCE_INLINE void odometer_step(uint8_t *const arr, const int bwd)
{ {
size_t i; size_t i;
for (i = 0U; i < 256U; ++i) for (i = 0U; i < 6U; ++i)
{ {
if (++arr[rev ? (255U - i) : i] != 0U) if (++arr[bwd ? (255U - i) : i] != 0U)
{
break;
}
}
for (i = 0U; i < 3U; ++i)
{
if (++arr[bwd ? i : (255U - i)] != 0U)
{ {
break; break;
} }
} }
} }
static FORCE_INLINE uint8_t process_enc(crypt_state_t* const crypt_state, uint8_t value) static FORCE_INLINE uint8_t process_encrypt(crypt_state_t* const crypt_state, uint8_t value)
{ {
size_t i; size_t i;
for (i = 0U; i < 256U; ++i) for (i = 0U; i < 256U; ++i)
{ {
const uint8_t offset = crypt_state->rotation_fwd[0U][i] + crypt_state->rotation_fwd[1U][i]; value = crypt_state->wheel_fwd[i][(value + crypt_state->rotation_fwd[i]) & 0xFF];
value = crypt_state->wheel_fwd[i][(value + offset) & 0xFF];
} }
++crypt_state->rotation_fwd[0U][crypt_state->step_fwd[crypt_state->counter++]]; ++crypt_state->rotation_fwd[crypt_state->step_fwd[crypt_state->counter]];
increment(crypt_state->rotation_fwd[1U], 0); odometer_step(crypt_state->rotation_fwd, 0);
crypt_state->counter = (crypt_state->counter + 1U) % 247U;
return value; return value;
} }
static FORCE_INLINE uint8_t process_dec(crypt_state_t* const crypt_state, uint8_t value) static FORCE_INLINE uint8_t process_decrypt(crypt_state_t* const crypt_state, uint8_t value)
{ {
size_t i; size_t i;
for (i = 0U; i < 256U; ++i) for (i = 0U; i < 256U; ++i)
{ {
const uint8_t offset = crypt_state->rotation_bwd[0U][i] + crypt_state->rotation_bwd[1U][i]; value = (crypt_state->wheel_bwd[i][value] - crypt_state->rotation_bwd[i]) & 0xFF;
value = (crypt_state->wheel_bwd[i][value] - offset) & 0xFF;
} }
++crypt_state->rotation_bwd[0U][crypt_state->step_bwd[crypt_state->counter++]]; ++crypt_state->rotation_bwd[crypt_state->step_bwd[crypt_state->counter]];
increment(crypt_state->rotation_bwd[1U], 1); odometer_step(crypt_state->rotation_bwd, 1);
crypt_state->counter = (crypt_state->counter + 1U) % 247U;
return value; return value;
} }
@ -379,7 +385,7 @@ int slunkcrypt_encrypt(const slunkcrypt_t context, const uint8_t* const input, u
size_t i; size_t i;
for (i = 0; i < length; ++i) for (i = 0; i < length; ++i)
{ {
output[i] = process_enc(state, input[i]); output[i] = process_encrypt(state, input[i]);
CHECK_ABORTED(); CHECK_ABORTED();
} }
} }
@ -398,7 +404,7 @@ int slunkcrypt_encrypt_inplace(const slunkcrypt_t context, uint8_t* const buffer
size_t i; size_t i;
for (i = 0; i < length; ++i) for (i = 0; i < length; ++i)
{ {
buffer[i] = process_enc(state, buffer[i]); buffer[i] = process_encrypt(state, buffer[i]);
CHECK_ABORTED(); CHECK_ABORTED();
} }
} }
@ -418,7 +424,7 @@ int slunkcrypt_decrypt(const slunkcrypt_t context, const uint8_t* const input, u
size_t i; size_t i;
for (i = 0; i < length; ++i) for (i = 0; i < length; ++i)
{ {
output[i] = process_dec(state, input[i]); output[i] = process_decrypt(state, input[i]);
CHECK_ABORTED(); CHECK_ABORTED();
} }
} }
@ -437,7 +443,7 @@ int slunkcrypt_decrypt_inplace(const slunkcrypt_t context, uint8_t* const buffer
size_t i; size_t i;
for (i = 0; i < length; ++i) for (i = 0; i < length; ++i)
{ {
buffer[i] = process_dec(state, buffer[i]); buffer[i] = process_decrypt(state, buffer[i]);
CHECK_ABORTED(); CHECK_ABORTED();
} }
} }