/******************************************************************************/ /* SlunkCrypt, by LoRd_MuldeR */ /* This work has been released under the CC0 1.0 Universal license! */ /******************************************************************************/ /* Internal */ #include "slunkcrypt.h" #include "compiler.h" #include "keygen.h" #include "thread.h" #include "version.h" /* CRT */ #include #include /* Version */ const uint16_t SLUNKCRYPT_VERSION_MAJOR = LIB_VERSION_MAJOR; const uint16_t SLUNKCRYPT_VERSION_MINOR = LIB_VERSION_MINOR; const uint16_t SLUNKCRYPT_VERSION_PATCH = LIB_VERSION_PATCH; const char *const SLUNKCRYPT_BUILD = __DATE__ ", " __TIME__; /* Utilities */ #define BOOLIFY(X) (!!(X)) // ========================================================================== // Data structures // ========================================================================== typedef struct { uint32_t x, y, z, w, v, d; } rand_state_t; typedef struct { int reverse_mode; const uint8_t (*wheel)[256U]; uint32_t counter; rand_state_t random; uint8_t *data; } thread_state_t; typedef struct { uint8_t wheel[256U][256U]; thrdpl_t thread_pool; size_t thread_idx; thread_state_t thread_data[MAX_THREADS]; } crypt_state_t; #define THREAD_COUNT 1U // ========================================================================== // Abort flag // ========================================================================== volatile int g_slunkcrypt_abort_flag = 0; #define CHECK_ABORTED() do \ { \ if (g_slunkcrypt_abort_flag) \ { \ goto aborted; \ } \ } \ while (0) // ========================================================================== // Byte access (endianness agnostic) // ========================================================================== static INLINE uint32_t lower_u64(const uint64_t value) { return (uint32_t)(value & 0xFFFFFFFF); } static INLINE uint32_t upper_u64(const uint64_t value) { return (uint32_t)(value >> 32U); } // ========================================================================== // Deterministic random bit generator // ========================================================================== static INLINE void random_init(rand_state_t *const state, const keydata_t *const key) { slunkcrypt_bzero(state, sizeof(rand_state_t)); state->x = lower_u64(key->a); state->y = upper_u64(key->a); state->z = lower_u64(key->b); state->w = upper_u64(key->b); state->v = lower_u64(key->c); state->d = upper_u64(key->c); } static INLINE uint32_t random_next(rand_state_t *const state) { const uint32_t t = state->x ^ (state->x >> 2); state->x = state->y; state->y = state->z; state->z = state->w; state->w = state->v; state->v ^= (state->v << 4) ^ t ^ (t << 1); return (state->d += 0x000587C5) + state->v; } static INLINE void random_seed(rand_state_t *const state, uint64_t salt, const uint16_t pepper, const uint8_t *const passwd, const size_t passwd_len) { size_t i; keydata_t key; do { slunkcrypt_keygen(&key, salt++, pepper, passwd, passwd_len); random_init(state, &key); slunkcrypt_bzero(&key, sizeof(keydata_t)); } while (!(state->x || state->y || state->z || state->w || state->v)); for (i = 0U; i < 97U; ++i) { UNUSED volatile uint32_t q = random_next(state); } } // ========================================================================== // Initialization // ========================================================================== static int initialize_state(crypt_state_t *const state, const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int mode, const int reset) { uint8_t temp[256U][256U]; size_t r, i; rand_state_t random; uint32_t counter; const int reverse_mode = BOOLIFY(mode); /* backup previous value */ const thrdpl_t thread_pool = reset ? state->thread_pool : THRDPL_NULL; /* initialize state */ slunkcrypt_bzero(state, sizeof(crypt_state_t)); /* create thread-pool */ if ((state->thread_pool = reset ? thread_pool : thrdpl_create(THREAD_COUNT)) == THRDPL_NULL) { return SLUNKCRYPT_FAILURE; } /* initialize counter */ random_seed(&random, nonce, (uint16_t)(-1), passwd, passwd_len); counter = random_next(&random); /* set up the wheel permutations */ for (r = 0U; r < 256U; ++r) { random_seed(&random, nonce, (uint16_t)r, passwd, passwd_len); for (i = 0U; i < 256U; ++i) { const size_t j = random_next(&random) % (i + 1U); if (j != i) { state->wheel[r][i] = state->wheel[r][j]; } state->wheel[r][j] = (uint8_t)i; } CHECK_ABORTED(); } /* reverse the wheels, if requested */ if (reverse_mode) { for (r = 0U; r < 256U; ++r) { for (i = 0U; i < 256U; ++i) { temp[r][state->wheel[r][i]] = (uint8_t)i; } } for (r = 0U; r < 256U; ++r) { memcpy(state->wheel[255U - r], temp[r], 256U); } slunkcrypt_bzero(temp, sizeof(temp)); CHECK_ABORTED(); } /* set up thread state */ random_seed(&random, nonce, 256U, passwd, passwd_len); for (i = 0U; i < THREAD_COUNT; ++i) { state->thread_data[i].reverse_mode = reverse_mode; state->thread_data[i].wheel = state->wheel; state->thread_data[i].counter = counter + ((uint32_t)i); memcpy(&state->thread_data[i].random, &random, sizeof(rand_state_t)); for (r = 0U; r < i * 63U; ++r) { random_next(&state->thread_data[i].random); } CHECK_ABORTED(); } slunkcrypt_bzero(&counter, sizeof(uint32_t)); slunkcrypt_bzero(&random, sizeof(rand_state_t)); return SLUNKCRYPT_SUCCESS; /* aborted */ aborted: thrdpl_destroy(state->thread_pool); slunkcrypt_bzero(state, sizeof(crypt_state_t)); slunkcrypt_bzero(&counter, sizeof(uint32_t)); slunkcrypt_bzero(&random, sizeof(rand_state_t)); return SLUNKCRYPT_ABORTED; } // ========================================================================== // Encrypt / Decrypt // ========================================================================== static INLINE void update_offset(uint8_t *const offset, uint32_t seed, rand_state_t *const state, const int reverse) { size_t i; for (i = 0U; i < 256U; ++i, seed >>= CHAR_BIT) { if (i && (!(i & 3U))) { seed = random_next(state); } offset[reverse ? (255U - i) : i] = (uint8_t)seed; } for (i = 0U; i < 63U * (THREAD_COUNT - 1U); ++i) { random_next(state); } } static INLINE void process_next_symbol(thread_state_t *const state) { uint8_t offset[256U]; size_t i; update_offset(offset, state->counter, &state->random, state->reverse_mode); for (i = 0U; i < 256U; ++i) { *state->data = (state->wheel[i][(*state->data + offset[i]) & 0xFF] - offset[i]) & 0xFF; } state->counter += THREAD_COUNT; } // ========================================================================== // Public API // ========================================================================== int slunkcrypt_generate_nonce(uint64_t *const nonce) { if (!nonce) { return SLUNKCRYPT_FAILURE; } do { if (slunkcrypt_random_bytes((uint8_t*)nonce, sizeof(uint64_t)) != sizeof(uint64_t)) { return SLUNKCRYPT_FAILURE; } } while (!(*nonce)); return SLUNKCRYPT_SUCCESS; } slunkcrypt_t slunkcrypt_alloc(const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int mode) { crypt_state_t* state = NULL; if ((!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX) || (mode < SLUNKCRYPT_ENCRYPT) || (mode > SLUNKCRYPT_DECRYPT)) { return SLUNKCRYPT_NULL; } if (!(state = (crypt_state_t*)malloc(sizeof(crypt_state_t)))) { return SLUNKCRYPT_NULL; } if (initialize_state(state, nonce, passwd, passwd_len, mode, 0) == SLUNKCRYPT_SUCCESS) { return ((slunkcrypt_t)state); } else { slunkcrypt_bzero(state, sizeof(crypt_state_t)); return SLUNKCRYPT_NULL; } } int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int mode) { crypt_state_t *const state = (crypt_state_t*)context; int result = SLUNKCRYPT_FAILURE; if ((!state) || (!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX) || (mode < SLUNKCRYPT_ENCRYPT) || (mode > SLUNKCRYPT_DECRYPT)) { return SLUNKCRYPT_FAILURE; } if ((result = initialize_state(state, nonce, passwd, passwd_len, mode, 1)) != SLUNKCRYPT_SUCCESS) { slunkcrypt_bzero(state, sizeof(crypt_state_t)); } return result; } int slunkcrypt_process(const slunkcrypt_t context, const uint8_t *const input, uint8_t *const output, size_t length) { size_t i; crypt_state_t *const state = (crypt_state_t*)context; if (!state) { return SLUNKCRYPT_FAILURE; } if (length > 0U) { memcpy(output, input, length * sizeof(uint8_t)); for (i = 0; i < length; ++i) { abort(); //process_next_symbol(state, output + i); CHECK_ABORTED(); } } thrdpl_await(state->thread_pool); return SLUNKCRYPT_SUCCESS; aborted: thrdpl_await(state->thread_pool); slunkcrypt_bzero(state, sizeof(crypt_state_t)); return SLUNKCRYPT_ABORTED; } int slunkcrypt_inplace(const slunkcrypt_t context, uint8_t *const buffer, size_t length) { size_t i; crypt_state_t *const state = (crypt_state_t*)context; if (!state) { return SLUNKCRYPT_FAILURE; } if (length > 0U) { for (i = 0; i < length; ++i) { state->thread_data[state->thread_idx].data = buffer + i; //process_next_symbol(&state->thread_data[state->thread_idx]); thrdpl_submit(state->thread_pool, process_next_symbol, &state->thread_data[state->thread_idx]); if (++state->thread_idx >= THREAD_COUNT) { state->thread_idx = 0U; } CHECK_ABORTED(); } } thrdpl_await(state->thread_pool); return SLUNKCRYPT_SUCCESS; aborted: thrdpl_await(state->thread_pool); slunkcrypt_bzero(state, sizeof(crypt_state_t)); return SLUNKCRYPT_ABORTED; } void slunkcrypt_free(const slunkcrypt_t context) { crypt_state_t *const state = (crypt_state_t*)context; if (state) { thrdpl_destroy(state->thread_pool); slunkcrypt_bzero(state, sizeof(crypt_state_t)); free(state); } }