Implemented improved SIGINT handling.

This commit is contained in:
LoRd_MuldeR 2020-10-18 20:41:02 +02:00
parent 65ce21963b
commit 895b2812a6
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
3 changed files with 67 additions and 40 deletions

View File

@ -18,7 +18,6 @@
#define BUFF_SIZE 4096U
static const CHR *const ENVV_PASSWD_NAME = T("MCRYPT_PASSWD");
static volatile int g_interrupted = 0;
static char* read_passphrase(const CHR* const file_name)
{
@ -144,7 +143,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
ctx = mcrypt_alloc(seed, (const uint8_t*)passphrase, strlen(passphrase));
if (!ctx)
{
FPUTS(T("\n\nMCrypt error: Failed to initialize encryption!\n\n"), stderr);
FPUTS(g_macrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nMCrypt error: Failed to initialize encryption!\n\n"), stderr);
goto clean_up;
}
@ -164,9 +163,10 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
{
crc_actual = crc64_update(crc_actual, buffer, count);
bytes_read += count;
if (mcrypt_encrypt_inplace(ctx, buffer, count) != MCRYPT_SUCCESS)
const int status = mcrypt_encrypt_inplace(ctx, buffer, count);
if (status != MCRYPT_SUCCESS)
{
FPUTS(T("\n\nMCrypt error: Failed to encrypt data!\n\n"), stderr);
FPUTS((status == MCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nMCrypt error: Failed to encrypt data!\n\n"), stderr);
goto clean_up;
}
if (fwrite(buffer, sizeof(uint8_t), count, file_out) < count)
@ -185,11 +185,6 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co
fflush(stderr);
clk_update = clk_now;
}
if (g_interrupted)
{
FPUTS(T("\n\nProcess interrupted!\n\n"), stderr);
goto clean_up;
}
}
if (ferror(file_in))
@ -275,7 +270,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
ctx = mcrypt_alloc(seed, (const uint8_t*)passphrase, strlen(passphrase));
if (!ctx)
{
FPUTS(T("\n\nMCrypt error: Failed to initialize decryption!\n\n"), stderr);
FPUTS(g_macrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nMCrypt error: Failed to initialize decryption!\n\n"), stderr);
goto clean_up;
}
@ -295,9 +290,10 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
if (count > 0U)
{
bytes_read += count;
if (mcrypt_decrypt_inplace(ctx, buffer, count) != MCRYPT_SUCCESS)
const int status = mcrypt_decrypt_inplace(ctx, buffer, count);
if (status != MCRYPT_SUCCESS)
{
FPUTS(T("\n\nMCrypt error: Failed to decrypt data!\n\n"), stderr);
FPUTS((status == MCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nMCrypt error: Failed to decrypt data!\n\n"), stderr);
goto clean_up;
}
crc_actual = crc64_update(crc_actual, buffer, count);
@ -317,11 +313,6 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co
fflush(stderr);
clk_update = clk_now;
}
if (g_interrupted)
{
FPUTS(T("\n\nProcess interrupted!\n\n"), stderr);
goto clean_up;
}
}
if (ferror(file_in))
@ -384,7 +375,7 @@ static int run_test(const char *const message)
static const char* const passphrase = "OrpheanBeh0lderScry!Doubt";
const size_t length = strlen(message) + 1U;
int result = 1;
int status, result = 1;
mcrypt_t ctx = MCRYPT_NULL;
uint64_t seed;
@ -404,13 +395,14 @@ static int run_test(const char *const message)
ctx = mcrypt_alloc(seed, (const uint8_t*)passphrase, strlen(passphrase));
if (!ctx)
{
FPUTS(T("\n\nnWhoops: Failed to initialize encoder!\n\n"), stderr);
FPUTS(g_macrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize encoder!\n\n"), stderr);
goto clean_up;
}
if (mcrypt_encrypt_inplace(ctx, (uint8_t*)text_temp, length) != MCRYPT_SUCCESS)
status = mcrypt_encrypt_inplace(ctx, (uint8_t*)text_temp, length);
if (status != MCRYPT_SUCCESS)
{
FPUTS(T("\n\nWhoops: Failed to encrypt the message!\n\n"), stderr);
FPUTS((status == MCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to encrypt the message!\n\n"), stderr);
goto clean_up;
}
@ -420,15 +412,17 @@ static int run_test(const char *const message)
goto clean_up;
}
if (mcrypt_reset(ctx, seed, (const uint8_t*)passphrase, strlen(passphrase)) != MCRYPT_SUCCESS)
status = mcrypt_reset(ctx, seed, (const uint8_t*)passphrase, strlen(passphrase));
if (status != MCRYPT_SUCCESS)
{
FPUTS(T("\n\nWhoops: Failed to initialize decoder!\n\n"), stderr);
FPUTS((status == MCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize decoder!\n\n"), stderr);
goto clean_up;
}
if (mcrypt_decrypt_inplace(ctx, (uint8_t*)text_temp, length) != MCRYPT_SUCCESS)
status = mcrypt_decrypt_inplace(ctx, (uint8_t*)text_temp, length);
if (status != MCRYPT_SUCCESS)
{
FPUTS(T("\n\nWhoops: Failed to decrypt the message!\n\n"), stderr);
FPUTS((status == MCRYPT_ABORTED) ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to decrypt the message!\n\n"), stderr);
goto clean_up;
}
@ -475,11 +469,6 @@ static int self_test(void)
{
return 1;
}
if (g_interrupted)
{
FPUTS(T("\n\nProcess interrupted!\n\n"), stderr);
return 1;
}
}
}
@ -493,7 +482,7 @@ static void sigint_handler(const int sig)
{
if (sig == SIGINT)
{
g_interrupted = 1;
g_macrypt_abort_flag = 1;
signal(SIGINT, sigint_handler);
}
}
@ -599,7 +588,7 @@ int MAIN(int argc, CHR* argv[])
goto exiting;
}
if (!g_interrupted)
if (!g_macrypt_abort_flag)
{
FPUTS(T("--------\n\n"), stderr);
fflush(stderr);

View File

@ -15,6 +15,11 @@
extern const char *const LIBMCRYPT_VERSION;
extern const char* const LIBMCRYPT_BUILDNO;
/*
* Abort flag
*/
extern volatile int g_macrypt_abort_flag;
/*
* Opaque handle to internal state
*/
@ -26,6 +31,7 @@ typedef uintptr_t mcrypt_t;
#define MCRYPT_NULL ((mcrypt_t)NULL)
#define MCRYPT_SUCCESS 0
#define MCRYPT_FAILURE (-1)
#define MCRYPT_ABORTED (-2)
/*
* Seed generator

View File

@ -51,6 +51,21 @@ typedef struct
}
rand_state_t;
// ==========================================================================
// Abort flag
// ==========================================================================
volatile int g_macrypt_abort_flag = 0;
#define CHECK_ABORTED() do \
{ \
if (g_macrypt_abort_flag) \
{ \
return MCRYPT_ABORTED; \
} \
} \
while (0)
// ==========================================================================
// Hash function
// ==========================================================================
@ -135,7 +150,7 @@ static void random_seed(rand_state_t* const state, const uint64_t salt, const ui
// Initialization
// ==========================================================================
static void initialize_state(crypt_state_t* const crypt_state, const uint64_t salt, const uint8_t* const passwd, const size_t passwd_len)
static int initialize_state(crypt_state_t* const crypt_state, const uint64_t salt, const uint8_t* const passwd, const size_t passwd_len)
{
mcrypt_bzero(crypt_state, sizeof(crypt_state_t));
@ -160,6 +175,7 @@ static void initialize_state(crypt_state_t* const crypt_state, const uint64_t sa
const size_t j = crypt_state->wheel_fwd[r][i];
crypt_state->wheel_bwd[255U - r][j] = (uint8_t)i;
}
CHECK_ABORTED();
}
/* set up stepping */
@ -177,6 +193,7 @@ static void initialize_state(crypt_state_t* const crypt_state, const uint64_t sa
}
mcrypt_bzero(&rand_state, sizeof(rand_state_t));
return MCRYPT_SUCCESS;
}
// ==========================================================================
@ -243,15 +260,22 @@ mcrypt_t mcrypt_alloc(const uint64_t salt, const uint8_t *const passwd, const si
{
if ((!passwd) || (passwd_len < 1U))
{
return ((mcrypt_t)NULL);
return MCRYPT_NULL;
}
crypt_state_t* const state = (crypt_state_t*)malloc(sizeof(crypt_state_t));
if (!state)
{
return ((mcrypt_t)NULL);
return MCRYPT_NULL;
}
initialize_state(state, salt, passwd, passwd_len);
if (initialize_state(state, salt, passwd, passwd_len) == MCRYPT_SUCCESS)
{
return ((mcrypt_t)state);
}
else
{
mcrypt_bzero(state, sizeof(crypt_state_t));
return MCRYPT_NULL;
}
}
int mcrypt_reset(const mcrypt_t context, const uint64_t salt, const uint8_t *const passwd, const size_t passwd_len)
@ -261,8 +285,12 @@ int mcrypt_reset(const mcrypt_t context, const uint64_t salt, const uint8_t *con
{
return MCRYPT_FAILURE;
}
initialize_state(state, salt, passwd, passwd_len);
return MCRYPT_SUCCESS;
const int result = initialize_state(state, salt, passwd, passwd_len);
if (result != MCRYPT_SUCCESS)
{
mcrypt_bzero(state, sizeof(crypt_state_t));
}
return result;
}
int mcrypt_encrypt(const mcrypt_t context, const uint8_t* const input, uint8_t* const output, size_t length)
@ -275,6 +303,7 @@ int mcrypt_encrypt(const mcrypt_t context, const uint8_t* const input, uint8_t*
for (size_t i = 0; i < length; ++i)
{
output[i] = process_enc(state, input[i]);
CHECK_ABORTED();
}
return MCRYPT_SUCCESS;
}
@ -289,6 +318,7 @@ int mcrypt_encrypt_inplace(const mcrypt_t context, uint8_t* const buffer, size_t
for (size_t i = 0; i < length; ++i)
{
buffer[i] = process_enc(state, buffer[i]);
CHECK_ABORTED();
}
return MCRYPT_SUCCESS;
}
@ -304,6 +334,7 @@ int mcrypt_decrypt(const mcrypt_t context, const uint8_t* const input, uint8_t*
for (size_t i = 0; i < length; ++i)
{
output[i] = process_dec(state, input[i]);
CHECK_ABORTED();
}
return MCRYPT_SUCCESS;
}
@ -318,6 +349,7 @@ int mcrypt_decrypt_inplace(const mcrypt_t context, uint8_t* const buffer, size_t
for (size_t i = 0; i < length; ++i)
{
buffer[i] = process_dec(state, buffer[i]);
CHECK_ABORTED();
}
return MCRYPT_SUCCESS;
}