Replaced separate encrypt() and decrypt() functions with a single process() function + added 'mode' parameter to alloc() and reset() functions.

This commit is contained in:
LoRd_MuldeR 2021-04-01 01:58:39 +02:00
parent 75b10acd6a
commit 471e08737b
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
6 changed files with 64 additions and 97 deletions

View File

@ -74,7 +74,7 @@ static int decrypt_main(int argc, char *argv[])
const std::streamsize count = file_src.gcount();
if (count > 0)
{
if (!slunk_decrypt.decrypt_inplace(buffer, (size_t)count))
if (!slunk_decrypt.process_inplace(buffer, (size_t)count))
{
std::cerr << "failed!\n\nError: SlunkCrypt decryption has failed!" << std::endl;
return -1;

View File

@ -72,7 +72,7 @@ static int encrypt_main(int argc, char *argv[])
const std::streamsize count = file_src.gcount();
if (count > 0)
{
if (!slunk_encrypt.encrypt_inplace(buffer, (size_t)count))
if (!slunk_encrypt.process_inplace(buffer, (size_t)count))
{
std::cerr << "failed!\n\nError: SlunkCrypt encryption has failed!" << std::endl;
return -1;

View File

@ -304,7 +304,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
goto clean_up;
}
ctx = slunkcrypt_alloc(nonce, (const uint8_t*)passphrase, strlen(passphrase));
ctx = slunkcrypt_alloc(nonce, (const uint8_t*)passphrase, strlen(passphrase), SLUNKCRYPT_ENCRYPT);
if (!ctx)
{
FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to initialize encryption!\n\n"), stderr);
@ -337,7 +337,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
{
blake2s_update(&blake2s_state, buffer, count);
bytes_read += count;
if ((status = slunkcrypt_encrypt_inplace(ctx, buffer, count)) != SLUNKCRYPT_SUCCESS)
if ((status = slunkcrypt_process_inplace(ctx, buffer, count)) != SLUNKCRYPT_SUCCESS)
{
FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to encrypt data!\n\n"), stderr);
goto clean_up;
@ -385,7 +385,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
}
SET_LOWBITS(buffer[padding - 1U], padding - 1U);
if ((status = slunkcrypt_encrypt_inplace(ctx, buffer, padding)) != SLUNKCRYPT_SUCCESS)
if ((status = slunkcrypt_process_inplace(ctx, buffer, padding)) != SLUNKCRYPT_SUCCESS)
{
FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to encrypt data!\n\n"), stderr);
goto clean_up;
@ -400,7 +400,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
uint8_t checksum_buffer[sizeof(uint64_t)];
store_ui64(checksum_buffer, blake2s_final(&blake2s_state));
if ((status = slunkcrypt_encrypt_inplace(ctx, checksum_buffer, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS)
if ((status = slunkcrypt_process_inplace(ctx, checksum_buffer, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS)
{
FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to encrypt checksum!\n\n"), stderr);
goto clean_up;
@ -486,7 +486,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
goto clean_up;
}
ctx = slunkcrypt_alloc(nonce ^ MAGIC_NUMBER, (const uint8_t*)passphrase, strlen(passphrase));
ctx = slunkcrypt_alloc(nonce ^ MAGIC_NUMBER, (const uint8_t*)passphrase, strlen(passphrase), SLUNKCRYPT_DECRYPT);
if (!ctx)
{
FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to initialize decryption!\n\n"), stderr);
@ -514,7 +514,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
if (count > 0U)
{
bytes_read += count;
if ((status = slunkcrypt_decrypt_inplace(ctx, buffer, count)) != SLUNKCRYPT_SUCCESS)
if ((status = slunkcrypt_process_inplace(ctx, buffer, count)) != SLUNKCRYPT_SUCCESS)
{
FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to decrypt data!\n\n"), stderr);
goto clean_up;
@ -560,7 +560,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
goto clean_up;
}
if ((status = slunkcrypt_decrypt_inplace(ctx, buffer, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS)
if ((status = slunkcrypt_process_inplace(ctx, buffer, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS)
{
FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to decrypt data!\n\n"), stderr);
goto clean_up;
@ -588,7 +588,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
goto clean_up;
}
if ((status = slunkcrypt_decrypt_inplace(ctx, checksum_buffer, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS)
if ((status = slunkcrypt_process_inplace(ctx, checksum_buffer, sizeof(uint64_t))) != SLUNKCRYPT_SUCCESS)
{
FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nSlunkCrypt error: Failed to decrypt checksum!\n\n"), stderr);
goto clean_up;
@ -670,14 +670,14 @@ static int run_test_case(const char *const message, const uint64_t checksum_mess
goto clean_up;
}
ctx = slunkcrypt_alloc(nonce, (const uint8_t*)passphrase, strlen(passphrase));
ctx = slunkcrypt_alloc(nonce, (const uint8_t*)passphrase, strlen(passphrase), SLUNKCRYPT_ENCRYPT);
if (!ctx)
{
FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize encoder!\n\n"), stderr);
goto clean_up;
}
status = slunkcrypt_encrypt_inplace(ctx, (uint8_t*)text_temp, length);
status = slunkcrypt_process_inplace(ctx, (uint8_t*)text_temp, length);
if (status != SLUNKCRYPT_SUCCESS)
{
FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to encrypt the message!\n\n"), stderr);
@ -690,14 +690,14 @@ static int run_test_case(const char *const message, const uint64_t checksum_mess
goto clean_up;
}
status = slunkcrypt_reset(ctx, nonce, (const uint8_t*)passphrase, strlen(passphrase));
status = slunkcrypt_reset(ctx, nonce, (const uint8_t*)passphrase, strlen(passphrase), SLUNKCRYPT_DECRYPT);
if (status != SLUNKCRYPT_SUCCESS)
{
FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize decoder!\n\n"), stderr);
goto clean_up;
}
status = slunkcrypt_decrypt_inplace(ctx, (uint8_t*)text_temp, length);
status = slunkcrypt_process_inplace(ctx, (uint8_t*)text_temp, length);
if (status != SLUNKCRYPT_SUCCESS)
{
FPUTS((status == SLUNKCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to decrypt the message!\n\n"), stderr);

View File

@ -56,6 +56,12 @@ extern "C" {
typedef uintptr_t slunkcrypt_t;
#define SLUNKCRYPT_NULL ((slunkcrypt_t)NULL)
/*
* Mode of operation
*/
static const int SLUNKCRYPT_ENCRYPT = 0;
static const int SLUNKCRYPT_DECRYPT = 1;
/*
* Error codes
*/
@ -94,21 +100,15 @@ SLUNKCRYPT_API int slunkcrypt_generate_nonce(uint64_t* const nonce);
/*
* Allocate, reset or free state
*/
SLUNKCRYPT_API slunkcrypt_t slunkcrypt_alloc(const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len);
SLUNKCRYPT_API int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len);
SLUNKCRYPT_API slunkcrypt_t slunkcrypt_alloc(const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int mode);
SLUNKCRYPT_API int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int mode);
SLUNKCRYPT_API void slunkcrypt_free(const slunkcrypt_t context);
/*
* Encryption routines
*/
SLUNKCRYPT_API int slunkcrypt_encrypt(const slunkcrypt_t context, const uint8_t *const input, uint8_t* const output, size_t length);
SLUNKCRYPT_API int slunkcrypt_encrypt_inplace(const slunkcrypt_t context, uint8_t *const buffer, size_t length);
/*
* Decryption routines
*/
SLUNKCRYPT_API int slunkcrypt_decrypt(const slunkcrypt_t context, const uint8_t *const input, uint8_t *const output, size_t length);
SLUNKCRYPT_API int slunkcrypt_decrypt_inplace(const slunkcrypt_t context, uint8_t* const buffer, size_t length);
SLUNKCRYPT_API int slunkcrypt_process(const slunkcrypt_t context, const uint8_t *const input, uint8_t* const output, size_t length);
SLUNKCRYPT_API int slunkcrypt_process_inplace(const slunkcrypt_t context, uint8_t *const buffer, size_t length);
/*
* Auxiliary functions

View File

@ -38,7 +38,7 @@ namespace slunkcrypt
{
throw std::runtime_error("SlunkCryptEncr: Failed to generate the seed value!");
}
if ((m_instance = slunkcrypt_alloc(m_nonce, (const uint8_t*)passwd.c_str(), passwd.length())) == SLUNKCRYPT_NULL)
if ((m_instance = slunkcrypt_alloc(m_nonce, (const uint8_t*)passwd.c_str(), passwd.length(), SLUNKCRYPT_ENCRYPT)) == SLUNKCRYPT_NULL)
{
throw std::runtime_error("SlunkCryptEncr: Failed to create encoder instance!");
}
@ -60,28 +60,28 @@ namespace slunkcrypt
}
}
bool encrypt(const uint8_t* const input, uint8_t* const output, size_t length)
bool process(const uint8_t* const input, uint8_t* const output, size_t length)
{
return (slunkcrypt_encrypt(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS);
return (slunkcrypt_process(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS);
}
bool encrypt(const std::vector<uint8_t>& input, std::vector<uint8_t>& output)
bool process(const std::vector<uint8_t>& input, std::vector<uint8_t>& output)
{
if (output.size() >= input.size())
{
return (slunkcrypt_encrypt(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS);
return (slunkcrypt_process(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS);
}
return false;
}
bool encrypt_inplace(uint8_t* const buffer, size_t length)
bool process_inplace(uint8_t* const buffer, size_t length)
{
return (slunkcrypt_encrypt_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS);
return (slunkcrypt_process_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS);
}
bool encrypt_inplace(std::vector<uint8_t>& buffer)
bool process_inplace(std::vector<uint8_t>& buffer)
{
return (slunkcrypt_encrypt_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS);
return (slunkcrypt_process_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS);
}
uint64_t get_nonce(void) const
@ -104,7 +104,7 @@ namespace slunkcrypt
public:
Decryptor(const uint64_t nonce, const std::string& passwd)
{
if ((m_instance = slunkcrypt_alloc(nonce, (const uint8_t*)passwd.c_str(), passwd.length())) == SLUNKCRYPT_NULL)
if ((m_instance = slunkcrypt_alloc(nonce, (const uint8_t*)passwd.c_str(), passwd.length(), SLUNKCRYPT_DECRYPT)) == SLUNKCRYPT_NULL)
{
throw std::runtime_error("SlunkCryptDecr: Failed to create decoder instance!");
}
@ -124,28 +124,28 @@ namespace slunkcrypt
}
}
bool decrypt(const uint8_t* const input, uint8_t* const output, size_t length)
bool process(const uint8_t* const input, uint8_t* const output, size_t length)
{
return (slunkcrypt_decrypt(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS);
return (slunkcrypt_process(m_instance, input, output, length) == SLUNKCRYPT_SUCCESS);
}
bool decrypt(const std::vector<uint8_t>& input, std::vector<uint8_t>& output)
bool process(const std::vector<uint8_t>& input, std::vector<uint8_t>& output)
{
if (output.size() >= input.size())
{
return (slunkcrypt_decrypt(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS);
return (slunkcrypt_process(m_instance, input.data(), output.data(), input.size()) == SLUNKCRYPT_SUCCESS);
}
return false;
}
bool decrypt_inplace(uint8_t* const buffer, size_t length)
bool process_inplace(uint8_t* const buffer, size_t length)
{
return (slunkcrypt_decrypt_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS);
return (slunkcrypt_process_inplace(m_instance, buffer, length) == SLUNKCRYPT_SUCCESS);
}
bool decrypt_inplace(std::vector<uint8_t>& buffer)
bool process_inplace(std::vector<uint8_t>& buffer)
{
return (slunkcrypt_decrypt_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS);
return (slunkcrypt_process_inplace(m_instance, buffer.data(), buffer.size()) == SLUNKCRYPT_SUCCESS);
}
private:

View File

@ -24,9 +24,6 @@
# define UNUSED
#endif
/* Utilities */
#define LOGICAL_XOR(X,Y) ((Y) ? (!(X)) : (X))
/* Version info */
const uint16_t SLUNKCRYPT_VERSION_MAJOR = MY_VERSION_MAJOR;
const uint16_t SLUNKCRYPT_VERSION_MINOR = MY_VERSION_MINOR;
@ -37,6 +34,10 @@ const char* const SLUNKCRYPT_BUILD = __DATE__ " " __TIME__;
#define HASH_MAGIC_PRIME 0x00000100000001B3ull
#define HASH_OFFSET_BASE 0xCBF29CE484222325ull
/* Utils */
#define BOOLIFY(X) (!!(X))
#define LOGICAL_XOR(X,Y) ((Y) ? (!(X)) : (X))
// ==========================================================================
// Data structures
// ==========================================================================
@ -55,6 +56,7 @@ rand_state_t;
typedef struct
{
uint8_t reverse_mode;
uint8_t wheel_fwd[256U][256U];
uint8_t wheel_bwd[256U][256U];
uint8_t step_fwd[241U];
@ -223,11 +225,14 @@ static void random_seed(rand_state_t *const state, uint64_t salt, const uint16_t
// Initialization
// ==========================================================================
static int initialize_state(crypt_state_t* const crypt_state, const uint64_t nonce, const uint8_t* const passwd, const size_t passwd_len)
static int initialize_state(crypt_state_t *const crypt_state, const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int reverse)
{
rand_state_t rand_state;
size_t r, i;
/* initialize state */
slunkcrypt_bzero(crypt_state, sizeof(crypt_state_t));
crypt_state->reverse_mode = BOOLIFY(reverse);
/* set up wheels and initial rotation */
for (r = 0U; r < 256U; ++r)
@ -264,6 +269,7 @@ static int initialize_state(crypt_state_t* const crypt_state, const uint64_t non
crypt_state->step_bwd[j] = (uint8_t)(249U - i);
}
/* final clean-up */
slunkcrypt_bzero(&rand_state, sizeof(rand_state_t));
return SLUNKCRYPT_SUCCESS;
}
@ -292,7 +298,7 @@ static FORCE_INLINE void odometer_step(uint8_t *const arr, const int bwd)
increment(arr, 6U, 9U, LOGICAL_XOR(bwd, 1));
}
static FORCE_INLINE uint8_t process_encrypt(crypt_state_t* const crypt_state, uint8_t value)
static FORCE_INLINE uint8_t process_encrypt(crypt_state_t *const crypt_state, uint8_t value)
{
size_t i;
for (i = 0U; i < 256U; ++i)
@ -306,7 +312,7 @@ static FORCE_INLINE uint8_t process_encrypt(crypt_state_t* const crypt_state, ui
return value;
}
static FORCE_INLINE uint8_t process_decrypt(crypt_state_t* const crypt_state, uint8_t value)
static FORCE_INLINE uint8_t process_decrypt(crypt_state_t *const crypt_state, uint8_t value)
{
size_t i;
for (i = 0U; i < 256U; ++i)
@ -341,10 +347,10 @@ int slunkcrypt_generate_nonce(uint64_t* const nonce)
return SLUNKCRYPT_SUCCESS;
}
slunkcrypt_t slunkcrypt_alloc(const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len)
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))
if ((!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX) || (mode < SLUNKCRYPT_ENCRYPT) || (mode > SLUNKCRYPT_DECRYPT))
{
return SLUNKCRYPT_NULL;
}
@ -352,7 +358,7 @@ slunkcrypt_t slunkcrypt_alloc(const uint64_t nonce, const uint8_t *const passwd,
{
return SLUNKCRYPT_NULL;
}
if (initialize_state(state, nonce, passwd, passwd_len) == SLUNKCRYPT_SUCCESS)
if (initialize_state(state, nonce, passwd, passwd_len, mode) == SLUNKCRYPT_SUCCESS)
{
return ((slunkcrypt_t)state);
}
@ -363,22 +369,22 @@ slunkcrypt_t slunkcrypt_alloc(const uint64_t nonce, const uint8_t *const passwd,
}
}
int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len)
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))
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)) != SLUNKCRYPT_SUCCESS)
if ((result = initialize_state(state, nonce, passwd, passwd_len, mode)) != SLUNKCRYPT_SUCCESS)
{
slunkcrypt_bzero(state, sizeof(crypt_state_t));
}
return result;
}
int slunkcrypt_encrypt(const slunkcrypt_t context, const uint8_t* const input, uint8_t* const output, size_t length)
int slunkcrypt_process(const slunkcrypt_t context, const uint8_t* const input, uint8_t* const output, size_t length)
{
crypt_state_t* const state = (crypt_state_t*)context;
if (!state)
@ -390,14 +396,14 @@ int slunkcrypt_encrypt(const slunkcrypt_t context, const uint8_t* const input, u
size_t i;
for (i = 0; i < length; ++i)
{
output[i] = process_encrypt(state, input[i]);
output[i] = state->reverse_mode ? process_decrypt(state, input[i]) : process_encrypt(state, input[i]);
CHECK_ABORTED();
}
}
return SLUNKCRYPT_SUCCESS;
}
int slunkcrypt_encrypt_inplace(const slunkcrypt_t context, uint8_t* const buffer, size_t length)
int slunkcrypt_process_inplace(const slunkcrypt_t context, uint8_t* const buffer, size_t length)
{
crypt_state_t* const state = (crypt_state_t*)context;
if (!state)
@ -409,46 +415,7 @@ int slunkcrypt_encrypt_inplace(const slunkcrypt_t context, uint8_t* const buffer
size_t i;
for (i = 0; i < length; ++i)
{
buffer[i] = process_encrypt(state, buffer[i]);
CHECK_ABORTED();
}
}
return SLUNKCRYPT_SUCCESS;
}
int slunkcrypt_decrypt(const slunkcrypt_t context, const uint8_t* const input, uint8_t* const output, size_t length)
{
crypt_state_t* const state = (crypt_state_t*)context;
if (!state)
{
return SLUNKCRYPT_FAILURE;
}
if (length > 0U)
{
size_t i;
for (i = 0; i < length; ++i)
{
output[i] = process_decrypt(state, input[i]);
CHECK_ABORTED();
}
}
return SLUNKCRYPT_SUCCESS;
}
int slunkcrypt_decrypt_inplace(const slunkcrypt_t context, uint8_t* const buffer, size_t length)
{
crypt_state_t* const state = (crypt_state_t*)context;
if (!state)
{
return SLUNKCRYPT_FAILURE;
}
if (length > 0U)
{
size_t i;
for (i = 0; i < length; ++i)
{
buffer[i] = process_decrypt(state, buffer[i]);
buffer[i] = state->reverse_mode ? process_decrypt(state, buffer[i]) : process_encrypt(state, buffer[i]);
CHECK_ABORTED();
}
}