{
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;
static uint64_t hash_code(const uint64_t salt, const uint16_t pepper, const uint8_t* const data, const size_t data_len)
{
uint64_t h = 0xCBF29CE484222325ull;
- hash_update(&h, (uint8_t*)&salt, sizeof(uint64_t));
+ hash_update(&h, (uint8_t*)&salt, sizeof(uint64_t));
hash_update(&h, (uint8_t*)&pepper, sizeof(uint16_t));
hash_update(&h, data, data_len);
return h;
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);
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));
}
{
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;
}
int mcrypt_generate_seed(uint64_t* const seed)
{
- if (seed)
+ if (!seed)
{
- return mcrypt_random_bytes((uint8_t*)seed, sizeof(uint64_t));
+ return -1;
}
- 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)