Randomize stepping.
This commit is contained in:
parent
46b3e15594
commit
1639e130db
@ -60,7 +60,7 @@ static int weak_passphrase(const CHR *str)
|
||||
|
||||
static int encrypt(const char* const passphrase, const CHR* const input, const CHR* const output)
|
||||
{
|
||||
mcrypt_t ctx = NULL;
|
||||
mcrypt_t ctx = MCRYPT_NULL;
|
||||
FILE *fin = NULL, *fout = NULL;
|
||||
int result = 1;
|
||||
|
||||
@ -181,7 +181,7 @@ clean_up:
|
||||
|
||||
static int decrypt(const char* const passphrase, const CHR* const input, const CHR* const output)
|
||||
{
|
||||
mcrypt_t ctx = NULL;
|
||||
mcrypt_t ctx = MCRYPT_NULL;
|
||||
FILE *fin = NULL, *fout = NULL;
|
||||
int result = 1;
|
||||
|
||||
@ -312,7 +312,7 @@ static int self_test(void)
|
||||
|
||||
const size_t length = strlen(text_plain) + 1U;
|
||||
int result = 1;
|
||||
mcrypt_t ctx_enc = NULL, ctx_dec = NULL;
|
||||
mcrypt_t ctx_enc = MCRYPT_NULL, ctx_dec = MCRYPT_NULL;
|
||||
|
||||
FPUTS(T("Self-test is running, please be patient... "), stderr);
|
||||
|
||||
|
@ -15,7 +15,8 @@ extern const char* const LIBMCRYPT_BUILDNO;
|
||||
/*
|
||||
* Opaque handle to internal state
|
||||
*/
|
||||
typedef void* mcrypt_t;
|
||||
typedef uintptr_t mcrypt_t;
|
||||
#define MCRYPT_NULL ((mcrypt_t)NULL)
|
||||
|
||||
/*
|
||||
* Seed generator
|
||||
@ -23,9 +24,10 @@ typedef void* mcrypt_t;
|
||||
int mcrypt_generate_seed(uint64_t* const seed);
|
||||
|
||||
/*
|
||||
* Allocate or free state
|
||||
* Allocate, reset or free state
|
||||
*/
|
||||
mcrypt_t mcrypt_alloc(const uint64_t salt, const char* const passphrase);
|
||||
int mcrypt_reset(const mcrypt_t context, const uint64_t salt, const char* const passphrase);
|
||||
void mcrypt_free(const mcrypt_t context);
|
||||
|
||||
/*
|
||||
|
@ -15,10 +15,11 @@ const char* const LIBMCRYPT_BUILDNO = __DATE__", "__TIME__;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t box[256U][256U];
|
||||
uint8_t inv[256U][256U];
|
||||
uint8_t off[256U];
|
||||
uint8_t pos;
|
||||
uint8_t wheel_fwd[256U][256U];
|
||||
uint8_t wheel_bwd[256U][256U];
|
||||
uint8_t rot[256U];
|
||||
uint8_t step[256U];
|
||||
uint8_t counter;
|
||||
}
|
||||
crypt_state_t;
|
||||
|
||||
@ -95,28 +96,37 @@ static void random_seed(rand_state_t* const state, const uint64_t salt, const ui
|
||||
|
||||
static void initialize_state(crypt_state_t* const crypt_state, const uint64_t salt, const uint8_t* const key, const size_t key_len)
|
||||
{
|
||||
crypt_state->counter = 0U;
|
||||
rand_state_t rand_state;
|
||||
for (size_t r = 0U; r < 256U; ++r)
|
||||
{
|
||||
random_seed(&rand_state, salt, (uint16_t)r, key, key_len);
|
||||
crypt_state->off[r] = (uint8_t)random_next(&rand_state);
|
||||
crypt_state->rot[r] = (uint8_t)random_next(&rand_state);
|
||||
for (size_t i = 0U; i < 256U; ++i)
|
||||
{
|
||||
const size_t j = random_next(&rand_state) % (i + 1U);
|
||||
if (j != i)
|
||||
{
|
||||
crypt_state->box[r][i] = crypt_state->box[r][j];
|
||||
crypt_state->wheel_fwd[r][i] = crypt_state->wheel_fwd[r][j];
|
||||
}
|
||||
crypt_state->box[r][j] = (uint8_t)i;
|
||||
crypt_state->wheel_fwd[r][j] = (uint8_t)i;
|
||||
}
|
||||
for (size_t i = 0U; i < 256U; ++i)
|
||||
{
|
||||
const size_t j = crypt_state->box[r][i];
|
||||
crypt_state->inv[r][j] = (uint8_t)i;
|
||||
const size_t j = crypt_state->wheel_fwd[r][i];
|
||||
crypt_state->wheel_bwd[r][j] = (uint8_t)i;
|
||||
}
|
||||
}
|
||||
random_seed(&rand_state, salt, 0x0100, key, key_len);
|
||||
crypt_state->pos = (uint8_t)random_next(&rand_state);
|
||||
for (size_t i = 0U; i < 256U; ++i)
|
||||
{
|
||||
const size_t j = random_next(&rand_state) % (i + 1U);
|
||||
if (j != i)
|
||||
{
|
||||
crypt_state->step[i] = crypt_state->step[j];
|
||||
}
|
||||
crypt_state->step[j] = (uint8_t)i;
|
||||
}
|
||||
mcrypt_bzero(&rand_state, sizeof(rand_state_t));
|
||||
}
|
||||
|
||||
@ -128,9 +138,9 @@ static uint8_t process_enc(crypt_state_t* const crypt_state, uint8_t value)
|
||||
{
|
||||
for (size_t i = 0U; i < 256U; ++i)
|
||||
{
|
||||
value = crypt_state->box[i][(value + crypt_state->off[i]) & 0xFF];
|
||||
value = crypt_state->wheel_fwd[i][(value + crypt_state->rot[i]) & 0xFF];
|
||||
}
|
||||
++crypt_state->off[crypt_state->pos++];
|
||||
++crypt_state->rot[crypt_state->step[crypt_state->counter++]];
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -139,9 +149,9 @@ static uint8_t process_dec(crypt_state_t* const crypt_state, uint8_t value)
|
||||
size_t i = 256U;
|
||||
while (i--)
|
||||
{
|
||||
value = (crypt_state->inv[i][value] - crypt_state->off[i]) & 0xFF;
|
||||
value = (crypt_state->wheel_bwd[i][value] - crypt_state->rot[i]) & 0xFF;
|
||||
}
|
||||
++crypt_state->off[crypt_state->pos++];
|
||||
++crypt_state->rot[crypt_state->step[crypt_state->counter++]];
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -173,10 +183,21 @@ mcrypt_t mcrypt_alloc(const uint64_t salt, const char* const passphrase)
|
||||
return ((mcrypt_t)state);
|
||||
}
|
||||
|
||||
int mcrypt_reset(const mcrypt_t context, const uint64_t salt, const char* const passphrase)
|
||||
{
|
||||
crypt_state_t* const state = (crypt_state_t*)context;
|
||||
if ((!state) || (!passphrase))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
initialize_state(state, salt, (uint8_t*)passphrase, strlen(passphrase));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mcrypt_enc_process(const mcrypt_t context, const uint8_t* const input, uint8_t* const output, size_t length)
|
||||
{
|
||||
crypt_state_t* const state = (crypt_state_t*)context;
|
||||
if (!context)
|
||||
if (!state)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
@ -190,7 +211,7 @@ int mcrypt_enc_process(const mcrypt_t context, const uint8_t* const input, uint8
|
||||
int mcrypt_enc_process_inplace(const mcrypt_t context, uint8_t* const buffer, size_t length)
|
||||
{
|
||||
crypt_state_t* const state = (crypt_state_t*)context;
|
||||
if (!context)
|
||||
if (!state)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
@ -205,7 +226,7 @@ int mcrypt_enc_process_inplace(const mcrypt_t context, uint8_t* const buffer, si
|
||||
int mcrypt_dec_process(const mcrypt_t context, const uint8_t* const input, uint8_t* const output, size_t length)
|
||||
{
|
||||
crypt_state_t* const state = (crypt_state_t*)context;
|
||||
if (!context)
|
||||
if (!state)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
@ -219,7 +240,7 @@ int mcrypt_dec_process(const mcrypt_t context, const uint8_t* const input, uint8
|
||||
int mcrypt_dec_process_inplace(const mcrypt_t context, uint8_t* const buffer, size_t length)
|
||||
{
|
||||
crypt_state_t* const state = (crypt_state_t*)context;
|
||||
if (!context)
|
||||
if (!state)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
@ -233,9 +254,9 @@ int mcrypt_dec_process_inplace(const mcrypt_t context, uint8_t* const buffer, si
|
||||
void mcrypt_free(const mcrypt_t context)
|
||||
{
|
||||
crypt_state_t* const state = (crypt_state_t*)context;
|
||||
if (context)
|
||||
if (state)
|
||||
{
|
||||
mcrypt_bzero((void*)context, sizeof(crypt_state_t));
|
||||
free(context);
|
||||
mcrypt_bzero(state, sizeof(crypt_state_t));
|
||||
free(state);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user