From 46b3e1559423addfcda875e40522352e1949d82c Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Wed, 14 Oct 2020 13:14:47 +0200 Subject: [PATCH] Added self-test mode. --- frontend/src/main.c | 96 +++++++++++++++++++++++++++++++++++++++- frontend/src/platform.h | 4 ++ libMCrypt/src/internal.c | 19 ++++---- 3 files changed, 109 insertions(+), 10 deletions(-) diff --git a/frontend/src/main.c b/frontend/src/main.c index 00d97f3..6299c11 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -62,7 +62,7 @@ static int encrypt(const char* const passphrase, const CHR* const input, const C { mcrypt_t ctx = NULL; FILE *fin = NULL, *fout = NULL; - int result = -1; + int result = 1; if (open_files(&fin, &fout, input, output) != 0) { @@ -183,7 +183,7 @@ static int decrypt(const char* const passphrase, const CHR* const input, const C { mcrypt_t ctx = NULL; FILE *fin = NULL, *fout = NULL; - int result = -1; + int result = 1; if (open_files(&fin, &fout, input, output) != 0) { @@ -305,6 +305,93 @@ clean_up: return result; } +static int self_test(void) +{ + static const char* const passphrase = "OrpheanBeh0lderScryDoubt!"; + static const char* const text_plain = "The greatest glory in living lies not in never falling, but in rising every time we fall."; + + const size_t length = strlen(text_plain) + 1U; + int result = 1; + mcrypt_t ctx_enc = NULL, ctx_dec = NULL; + + FPUTS(T("Self-test is running, please be patient... "), stderr); + + uint64_t seed; + if (mcrypt_generate_seed(&seed) != 0) + { + FPUTS(T("error!\n\nWhoops: Failed to generate seed!\n\n"), stderr); + goto clean_up; + } + + char *const text_temp = strdup(text_plain); + if (!text_temp) + { + FPUTS(T("error!\n\nWhoops: Failed to allocate text buffer!\n\n"), stderr); + goto clean_up; + } + + ctx_enc = mcrypt_alloc(seed, passphrase); + if (!ctx_enc) + { + FPUTS(T("error!\n\nnWhoops: Failed to initialize encoder!\n\n"), stderr); + goto clean_up; + } + + if (mcrypt_enc_process_inplace(ctx_enc, text_temp, length) != 0) + { + FPUTS(T("error!\n\nWhoops: Failed to encrypt the message!\n\n"), stderr); + goto clean_up; + } + + if (strncmp(text_plain, text_temp, length) == 0) + { + FPUTS(T("error!\n\nWhoops: Encrypted message equals the original message!\n\n"), stderr); + goto clean_up; + } + + ctx_dec = mcrypt_alloc(seed, passphrase); + if (!ctx_dec) + { + FPUTS(T("error!\n\nWhoops: Failed to initialize decoder!\n\n"), stderr); + goto clean_up; + } + + if (mcrypt_dec_process_inplace(ctx_dec, text_temp, length) != 0) + { + FPUTS(T("error!\n\nWhoops: Failed to decrypt the message!\n\n"), stderr); + goto clean_up; + } + + if (strncmp(text_plain, text_temp, length) != 0) + { + FPUTS(T("error!\n\nWhoops: Decrypted message does *not* match the original message!\n\n"), stderr); + goto clean_up; + } + + result = 0; + FPUTS(T("done\n\nCompleted successfully.\n\n"), stderr); + +clean_up: + + if (ctx_enc) + { + mcrypt_free(ctx_enc); + } + + if (ctx_dec) + { + mcrypt_free(ctx_dec); + } + + if (text_temp) + { + mcrypt_bzero(text_temp, strlen(text_temp)); + free(text_temp); + } + + return result; +} + int MAIN(int argc, CHR* argv[]) { init_terminal(); @@ -312,6 +399,11 @@ int MAIN(int argc, CHR* argv[]) FPRINTF(stderr, T("MCrypt Utility (%") T(PRIstr) T("-%") T(PRIstr) T("), by LoRd_MuldeR \n"), OS_TYPE, CPU_ARCH); FPRINTF(stderr, T("Using libMCrypt v%") T(PRIstr) T(" [%") T(PRIstr) T("]\n\n"), LIBMCRYPT_VERSION, LIBMCRYPT_BUILDNO); + if ((argc > 1) && (!STRICMP(argv[1U], T("--self-test")))) + { + return self_test(); /*only self-test!*/ + } + const int help_requested = (argc > 1) && ((!STRICMP(argv[1U], T("/?"))) || (!STRICMP(argv[1U], T("--help"))) || (!STRICMP(argv[1U], T("--version")))); if ((argc < 5) || help_requested) { diff --git a/frontend/src/platform.h b/frontend/src/platform.h index 8014945..0ce1508 100644 --- a/frontend/src/platform.h +++ b/frontend/src/platform.h @@ -97,4 +97,8 @@ #define T(X) _T(X) +#ifdef _MSC_VER +#define strdup(X) _strdup((X)) +#endif + #endif diff --git a/libMCrypt/src/internal.c b/libMCrypt/src/internal.c index 0796d3b..27a7408 100644 --- a/libMCrypt/src/internal.c +++ b/libMCrypt/src/internal.c @@ -78,17 +78,20 @@ int mcrypt_random_bytes(uint8_t* const buffer, const size_t length) void mcrypt_bzero(void* const ptr, const size_t length) { + if ((ptr) && (length > 0U)) + { #ifdef _WIN32 - SecureZeroMemory(ptr, length); + SecureZeroMemory(ptr, length); #else #ifdef HAVE_EXPLICIT_BZERO - explicit_bzero(ptr, length); + explicit_bzero(ptr, length); #else - volatile uint8_t* buffer = ptr; - for (size_t i = 0U; i < length; ++i) - { - buffer[i] = 0U; + volatile uint8_t* buffer = ptr; + for (size_t i = 0U; i < length; ++i) + { + buffer[i] = 0U; + } +#endif +#endif } -#endif -#endif }