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;
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;
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);
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;
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));
}
{
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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);
}
}