Some simplification.
This commit is contained in:
parent
1639e130db
commit
270c8b4ecd
@ -308,7 +308,7 @@ clean_up:
|
||||
static int self_test(void)
|
||||
{
|
||||
static const char* const passphrase = "OrpheanBeh0lderScryDoubt!";
|
||||
static const char* const text_plain = "The greatest glory in living lies not in never falling, but in rising every time we fall.";
|
||||
static const char* const text_plain = "Would you like me to give you a formula for success? It's quite simple, really: Double your rate of failure. You are thinking of failure as the enemy of success. But it isn't at all. You can be discouraged by failure or you can learn from it, so go ahead and make mistakes. Make all you can. Because remember that's where you will find success.";
|
||||
|
||||
const size_t length = strlen(text_plain) + 1U;
|
||||
int result = 1;
|
||||
|
@ -17,8 +17,10 @@ typedef struct
|
||||
{
|
||||
uint8_t wheel_fwd[256U][256U];
|
||||
uint8_t wheel_bwd[256U][256U];
|
||||
uint8_t rot[256U];
|
||||
uint8_t step[256U];
|
||||
uint8_t step_fwd[256U];
|
||||
uint8_t step_bwd[256U];
|
||||
uint8_t rotation_fwd[256U];
|
||||
uint8_t rotation_bwd[256U];
|
||||
uint8_t counter;
|
||||
}
|
||||
crypt_state_t;
|
||||
@ -96,12 +98,12 @@ 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;
|
||||
/* set up wheels and initial rotation */
|
||||
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->rot[r] = (uint8_t)random_next(&rand_state);
|
||||
crypt_state->rotation_bwd[255U - r] = crypt_state->rotation_fwd[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);
|
||||
@ -114,19 +116,25 @@ static void initialize_state(crypt_state_t* const crypt_state, const uint64_t sa
|
||||
for (size_t i = 0U; i < 256U; ++i)
|
||||
{
|
||||
const size_t j = crypt_state->wheel_fwd[r][i];
|
||||
crypt_state->wheel_bwd[r][j] = (uint8_t)i;
|
||||
crypt_state->wheel_bwd[255U - r][j] = (uint8_t)i;
|
||||
}
|
||||
}
|
||||
|
||||
/* set up stepping */
|
||||
random_seed(&rand_state, salt, 0x0100, key, key_len);
|
||||
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_fwd[i] = crypt_state->step_fwd[j];
|
||||
crypt_state->step_bwd[i] = crypt_state->step_bwd[j];
|
||||
}
|
||||
crypt_state->step[j] = (uint8_t)i;
|
||||
crypt_state->step_fwd[j] = (uint8_t)i;
|
||||
crypt_state->step_bwd[j] = (uint8_t)(255U - i);
|
||||
}
|
||||
|
||||
crypt_state->counter = 0U;
|
||||
mcrypt_bzero(&rand_state, sizeof(rand_state_t));
|
||||
}
|
||||
|
||||
@ -138,20 +146,19 @@ 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->wheel_fwd[i][(value + crypt_state->rot[i]) & 0xFF];
|
||||
value = crypt_state->wheel_fwd[i][(value + crypt_state->rotation_fwd[i]) & 0xFF];
|
||||
}
|
||||
++crypt_state->rot[crypt_state->step[crypt_state->counter++]];
|
||||
++crypt_state->rotation_fwd[crypt_state->step_fwd[crypt_state->counter++]];
|
||||
return value;
|
||||
}
|
||||
|
||||
static uint8_t process_dec(crypt_state_t* const crypt_state, uint8_t value)
|
||||
{
|
||||
size_t i = 256U;
|
||||
while (i--)
|
||||
for (size_t i = 0U; i < 256U; ++i)
|
||||
{
|
||||
value = (crypt_state->wheel_bwd[i][value] - crypt_state->rot[i]) & 0xFF;
|
||||
value = (crypt_state->wheel_bwd[i][value] - crypt_state->rotation_bwd[i]) & 0xFF;
|
||||
}
|
||||
++crypt_state->rot[crypt_state->step[crypt_state->counter++]];
|
||||
++crypt_state->rotation_bwd[crypt_state->step_bwd[crypt_state->counter++]];
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -161,11 +168,19 @@ static uint8_t process_dec(crypt_state_t* const crypt_state, uint8_t value)
|
||||
|
||||
int mcrypt_generate_seed(uint64_t* const seed)
|
||||
{
|
||||
if (seed)
|
||||
if (!seed)
|
||||
{
|
||||
return mcrypt_random_bytes((uint8_t*)seed, sizeof(uint64_t));
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
do
|
||||
{
|
||||
if (mcrypt_random_bytes((uint8_t*)seed, sizeof(uint64_t)) != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
while (!(*seed));
|
||||
return 0;
|
||||
}
|
||||
|
||||
mcrypt_t mcrypt_alloc(const uint64_t salt, const char* const passphrase)
|
||||
|
Loading…
Reference in New Issue
Block a user