2020-10-12 19:10:19 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/* MCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
|
|
|
/* This work has been released under the CC0 1.0 Universal license! */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define _CRT_RAND_S 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <mcrypt.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-10-15 21:56:36 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define FORCE_INLINE __forceinline
|
|
|
|
#else
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define FORCE_INLINE __attribute__((always_inline)) inline
|
|
|
|
#else
|
|
|
|
#define FORCE_INLINE inline
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2020-10-12 19:10:19 +02:00
|
|
|
const char* const LIBMCRYPT_VERSION = "1.0.0";
|
2020-10-13 17:42:22 +02:00
|
|
|
const char* const LIBMCRYPT_BUILDNO = __DATE__", "__TIME__;
|
2020-10-12 19:10:19 +02:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2020-10-14 13:39:12 +02:00
|
|
|
uint8_t wheel_fwd[256U][256U];
|
|
|
|
uint8_t wheel_bwd[256U][256U];
|
2020-10-14 14:02:05 +02:00
|
|
|
uint8_t step_fwd[256U];
|
|
|
|
uint8_t step_bwd[256U];
|
2020-10-15 21:56:36 +02:00
|
|
|
uint8_t rotation_fwd[2U][256U];
|
|
|
|
uint8_t rotation_bwd[2U][256U];
|
2020-10-14 13:39:12 +02:00
|
|
|
uint8_t counter;
|
2020-10-12 19:10:19 +02:00
|
|
|
}
|
|
|
|
crypt_state_t;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint32_t a, b, c, d;
|
|
|
|
uint32_t counter;
|
|
|
|
}
|
|
|
|
rand_state_t;
|
|
|
|
|
|
|
|
// ==========================================================================
|
|
|
|
// Hash function
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
static void hash_update(uint64_t* const h, const uint8_t* const data, const size_t data_len)
|
|
|
|
{
|
|
|
|
for (size_t i = 0U; i < data_len; ++i)
|
|
|
|
{
|
|
|
|
*h ^= data[i];
|
|
|
|
*h *= 0x00000100000001B3ull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 23:10:47 +02:00
|
|
|
static uint64_t hash_code(const uint64_t salt, const uint16_t pepper, const uint8_t* const data, const size_t data_len)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
|
|
|
uint64_t h = 0xCBF29CE484222325ull;
|
2020-10-14 14:02:05 +02:00
|
|
|
hash_update(&h, (uint8_t*)&salt, sizeof(uint64_t));
|
2020-10-12 23:10:47 +02:00
|
|
|
hash_update(&h, (uint8_t*)&pepper, sizeof(uint16_t));
|
2020-10-12 19:10:19 +02:00
|
|
|
hash_update(&h, data, data_len);
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==========================================================================
|
|
|
|
// PRNG
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
static void random_init(rand_state_t* const state, const uint64_t seed_0, const uint64_t seed_1)
|
|
|
|
{
|
|
|
|
state->counter = 0U;
|
|
|
|
state->a = (uint32_t)(seed_0 & 0xFFFFFFFF);
|
|
|
|
state->b = (uint32_t)((seed_0 >> 32) & 0xFFFFFFFF);
|
|
|
|
state->c = (uint32_t)(seed_1 & 0xFFFFFFFF);
|
|
|
|
state->d = (uint32_t)((seed_1 >> 32) & 0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t random_next(rand_state_t* const state)
|
|
|
|
{
|
|
|
|
uint32_t t = state->d;
|
|
|
|
const uint32_t s = state->a;
|
|
|
|
state->d = state->c;
|
|
|
|
state->c = state->b;
|
|
|
|
state->b = s;
|
|
|
|
t ^= t >> 2;
|
|
|
|
t ^= t << 1;
|
|
|
|
t ^= s ^ (s << 4);
|
|
|
|
state->a = t;
|
|
|
|
return t + (state->counter += 362437U);
|
|
|
|
}
|
|
|
|
|
2020-10-12 23:10:47 +02:00
|
|
|
static void random_seed(rand_state_t* const state, const uint64_t salt, const uint16_t pepper, const uint8_t* const key, const size_t key_len)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
2020-10-13 15:04:59 +02:00
|
|
|
const uint64_t hash_code_0 = hash_code(salt, pepper & 0x7FFF, key, key_len);
|
|
|
|
const uint64_t hash_code_1 = hash_code(salt, pepper | 0x8000, key, key_len);
|
2020-10-12 23:10:47 +02:00
|
|
|
random_init(state, hash_code_0, hash_code_1);
|
2020-10-12 19:10:19 +02:00
|
|
|
for (size_t i = 0U; i < 13U; ++i)
|
|
|
|
{
|
|
|
|
random_next(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==========================================================================
|
|
|
|
// Initialization
|
|
|
|
// ==========================================================================
|
|
|
|
|
2020-10-12 23:10:47 +02:00
|
|
|
static void initialize_state(crypt_state_t* const crypt_state, const uint64_t salt, const uint8_t* const key, const size_t key_len)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
2020-10-14 14:02:05 +02:00
|
|
|
/* set up wheels and initial rotation */
|
2020-10-12 19:10:19 +02:00
|
|
|
rand_state_t rand_state;
|
|
|
|
for (size_t r = 0U; r < 256U; ++r)
|
|
|
|
{
|
2020-10-12 23:10:47 +02:00
|
|
|
random_seed(&rand_state, salt, (uint16_t)r, key, key_len);
|
2020-10-15 21:56:36 +02:00
|
|
|
crypt_state->rotation_bwd[0U][255U - r] = crypt_state->rotation_fwd[0U][r] = (uint8_t)random_next(&rand_state);
|
|
|
|
crypt_state->rotation_bwd[1U][255U - r] = crypt_state->rotation_fwd[1U][r] = 0U;
|
2020-10-12 19:10:19 +02:00
|
|
|
for (size_t i = 0U; i < 256U; ++i)
|
|
|
|
{
|
|
|
|
const size_t j = random_next(&rand_state) % (i + 1U);
|
|
|
|
if (j != i)
|
|
|
|
{
|
2020-10-14 13:39:12 +02:00
|
|
|
crypt_state->wheel_fwd[r][i] = crypt_state->wheel_fwd[r][j];
|
2020-10-12 19:10:19 +02:00
|
|
|
}
|
2020-10-14 13:39:12 +02:00
|
|
|
crypt_state->wheel_fwd[r][j] = (uint8_t)i;
|
2020-10-12 19:10:19 +02:00
|
|
|
}
|
|
|
|
for (size_t i = 0U; i < 256U; ++i)
|
|
|
|
{
|
2020-10-14 13:39:12 +02:00
|
|
|
const size_t j = crypt_state->wheel_fwd[r][i];
|
2020-10-14 14:02:05 +02:00
|
|
|
crypt_state->wheel_bwd[255U - r][j] = (uint8_t)i;
|
2020-10-12 19:10:19 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-14 14:02:05 +02:00
|
|
|
|
|
|
|
/* set up stepping */
|
2020-10-12 23:10:47 +02:00
|
|
|
random_seed(&rand_state, salt, 0x0100, key, key_len);
|
2020-10-14 13:39:12 +02:00
|
|
|
for (size_t i = 0U; i < 256U; ++i)
|
|
|
|
{
|
|
|
|
const size_t j = random_next(&rand_state) % (i + 1U);
|
|
|
|
if (j != i)
|
|
|
|
{
|
2020-10-14 14:02:05 +02:00
|
|
|
crypt_state->step_fwd[i] = crypt_state->step_fwd[j];
|
|
|
|
crypt_state->step_bwd[i] = crypt_state->step_bwd[j];
|
2020-10-14 13:39:12 +02:00
|
|
|
}
|
2020-10-14 14:02:05 +02:00
|
|
|
crypt_state->step_fwd[j] = (uint8_t)i;
|
|
|
|
crypt_state->step_bwd[j] = (uint8_t)(255U - i);
|
2020-10-14 13:39:12 +02:00
|
|
|
}
|
2020-10-14 14:02:05 +02:00
|
|
|
|
|
|
|
crypt_state->counter = 0U;
|
2020-10-13 19:33:01 +02:00
|
|
|
mcrypt_bzero(&rand_state, sizeof(rand_state_t));
|
2020-10-12 19:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ==========================================================================
|
|
|
|
// Encrypt / Decrypt
|
|
|
|
// ==========================================================================
|
|
|
|
|
2020-10-15 21:56:36 +02:00
|
|
|
static FORCE_INLINE void increment(uint8_t *const arr, const int rev)
|
|
|
|
{
|
|
|
|
for (size_t i = 0U; i < 256U; ++i)
|
|
|
|
{
|
|
|
|
if (++arr[rev ? (255U - i) : i] != 0U)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static FORCE_INLINE uint8_t process_enc(crypt_state_t* const crypt_state, uint8_t value)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
|
|
|
for (size_t i = 0U; i < 256U; ++i)
|
|
|
|
{
|
2020-10-15 21:56:36 +02:00
|
|
|
const uint8_t offset = crypt_state->rotation_fwd[0U][i] + crypt_state->rotation_fwd[1U][i];
|
|
|
|
value = crypt_state->wheel_fwd[i][(value + offset) & 0xFF];
|
2020-10-12 19:10:19 +02:00
|
|
|
}
|
2020-10-15 21:56:36 +02:00
|
|
|
++crypt_state->rotation_fwd[0U][crypt_state->step_fwd[crypt_state->counter++]];
|
|
|
|
increment(crypt_state->rotation_fwd[1U], 0);
|
2020-10-12 19:10:19 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-10-15 21:56:36 +02:00
|
|
|
static FORCE_INLINE uint8_t process_dec(crypt_state_t* const crypt_state, uint8_t value)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
2020-10-14 14:02:05 +02:00
|
|
|
for (size_t i = 0U; i < 256U; ++i)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
2020-10-15 21:56:36 +02:00
|
|
|
const uint8_t offset = crypt_state->rotation_bwd[0U][i] + crypt_state->rotation_bwd[1U][i];
|
|
|
|
value = (crypt_state->wheel_bwd[i][value] - offset) & 0xFF;
|
2020-10-12 19:10:19 +02:00
|
|
|
}
|
2020-10-15 21:56:36 +02:00
|
|
|
++crypt_state->rotation_bwd[0U][crypt_state->step_bwd[crypt_state->counter++]];
|
|
|
|
increment(crypt_state->rotation_bwd[1U], 1);
|
2020-10-12 19:10:19 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==========================================================================
|
|
|
|
// Public API
|
|
|
|
// ==========================================================================
|
|
|
|
|
2020-10-12 23:10:47 +02:00
|
|
|
int mcrypt_generate_seed(uint64_t* const seed)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
2020-10-14 14:02:05 +02:00
|
|
|
if (!seed)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
2020-10-14 14:02:05 +02:00
|
|
|
return -1;
|
2020-10-12 19:10:19 +02:00
|
|
|
}
|
2020-10-14 14:02:05 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (mcrypt_random_bytes((uint8_t*)seed, sizeof(uint64_t)) != 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!(*seed));
|
|
|
|
return 0;
|
2020-10-12 19:10:19 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 00:43:57 +02:00
|
|
|
mcrypt_t mcrypt_alloc(const uint64_t salt, const char* const passphrase)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
|
|
|
if (!passphrase)
|
|
|
|
{
|
|
|
|
return ((mcrypt_t)NULL);
|
|
|
|
}
|
2020-10-12 23:10:47 +02:00
|
|
|
crypt_state_t* const state = (crypt_state_t*)malloc(sizeof(crypt_state_t));
|
2020-10-12 19:10:19 +02:00
|
|
|
if (!state)
|
|
|
|
{
|
|
|
|
return ((mcrypt_t)NULL);
|
|
|
|
}
|
|
|
|
initialize_state(state, salt, (uint8_t*)passphrase, strlen(passphrase));
|
|
|
|
return ((mcrypt_t)state);
|
|
|
|
}
|
|
|
|
|
2020-10-14 13:39:12 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-14 17:57:40 +02:00
|
|
|
int mcrypt_encrypt(const mcrypt_t context, const uint8_t* const input, uint8_t* const output, size_t length)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
|
|
|
crypt_state_t* const state = (crypt_state_t*)context;
|
2020-10-14 13:39:12 +02:00
|
|
|
if (!state)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
output[i] = process_enc(state, input[i]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-14 17:57:40 +02:00
|
|
|
int mcrypt_encrypt_inplace(const mcrypt_t context, uint8_t* const buffer, size_t length)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
|
|
|
crypt_state_t* const state = (crypt_state_t*)context;
|
2020-10-14 13:39:12 +02:00
|
|
|
if (!state)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
buffer[i] = process_enc(state, buffer[i]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-14 17:57:40 +02:00
|
|
|
int mcrypt_decrypt(const mcrypt_t context, const uint8_t* const input, uint8_t* const output, size_t length)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
|
|
|
crypt_state_t* const state = (crypt_state_t*)context;
|
2020-10-14 13:39:12 +02:00
|
|
|
if (!state)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
output[i] = process_dec(state, input[i]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-14 17:57:40 +02:00
|
|
|
int mcrypt_decrypt_inplace(const mcrypt_t context, uint8_t* const buffer, size_t length)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
|
|
|
crypt_state_t* const state = (crypt_state_t*)context;
|
2020-10-14 13:39:12 +02:00
|
|
|
if (!state)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
buffer[i] = process_dec(state, buffer[i]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mcrypt_free(const mcrypt_t context)
|
|
|
|
{
|
|
|
|
crypt_state_t* const state = (crypt_state_t*)context;
|
2020-10-14 13:39:12 +02:00
|
|
|
if (state)
|
2020-10-12 19:10:19 +02:00
|
|
|
{
|
2020-10-14 13:39:12 +02:00
|
|
|
mcrypt_bzero(state, sizeof(crypt_state_t));
|
|
|
|
free(state);
|
2020-10-12 19:10:19 +02:00
|
|
|
}
|
|
|
|
}
|