Added self-test mode.

This commit is contained in:
LoRd_MuldeR 2020-10-14 13:14:47 +02:00
parent dc9fe30673
commit 46b3e15594
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
3 changed files with 109 additions and 10 deletions

View File

@ -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 <MuldeR2@GMX.de>\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)
{

View File

@ -97,4 +97,8 @@
#define T(X) _T(X)
#ifdef _MSC_VER
#define strdup(X) _strdup((X))
#endif
#endif

View File

@ -77,6 +77,8 @@ 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);
@ -92,3 +94,4 @@ void mcrypt_bzero(void* const ptr, const size_t length)
#endif
#endif
}
}