diff --git a/frontend/src/main.c b/frontend/src/main.c index 770a796..5d306c4 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -17,12 +17,11 @@ #define BUFFER_SIZE 4096U -#define OP_MODE_NONE 0 -#define OP_MODE_HELP 1 -#define OP_MODE_VERS 2 -#define OP_MODE_ENCR 3 -#define OP_MODE_DECR 4 -#define OP_MODE_TEST 5 +#define OP_MODE_HELP 0 +#define OP_MODE_VERS 1 +#define OP_MODE_ENCR 2 +#define OP_MODE_DECR 3 +#define OP_MODE_TEST 4 static const CHR *const ENVV_PASSWD_NAME = T("SLUNK_PASSPHRASE"); @@ -55,7 +54,7 @@ static int parse_mode(const CHR* const command) else { FPRINTF(stderr, T("Error: The specified command \"%") T(PRISTR) T("\" is unknown!\n\n"), command); - return -1; + exit(EXIT_FAILURE); } } @@ -70,7 +69,7 @@ static void print_manpage(const CHR *const program) FPRINTF(stderr, T(" %") T(PRISTR) T(" --decrypt [[@][:]] \n\n"), program); } -static char* read_passphrase(const CHR* const file_name) +static char *read_passphrase(const CHR* const file_name) { const size_t max_len = SLUNKCRYPT_PWDLEN_MAX + 2U; char *buffer = (char*) malloc(max_len * sizeof(char)); @@ -138,7 +137,7 @@ static int open_files(FILE **const file_in, FILE **const file_out, const CHR* co if (!(*file_in)) { FPUTS(T("Error: Failed to open input file for reading!\n\n"), stderr); - return 1; + return EXIT_FAILURE; } *file_out = FOPEN(output_path, T("wb")); @@ -146,10 +145,10 @@ static int open_files(FILE **const file_in, FILE **const file_out, const CHR* co { FPUTS(T("Error: Failed to open output file for writing!\n\n"), stderr); fclose(*file_out); - return 1; + return EXIT_FAILURE; } - return 0; + return EXIT_SUCCESS; } static void sigint_handler(const int sig) @@ -168,9 +167,9 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co { slunkcrypt_t ctx = SLUNKCRYPT_NULL; FILE * file_in = NULL, * file_out = NULL; - int result = 1; + int result = EXIT_FAILURE; - if (open_files(&file_in, &file_out, input_path, output_path) != 0) + if (open_files(&file_in, &file_out, input_path, output_path) != EXIT_SUCCESS) { goto clean_up;; } @@ -263,16 +262,24 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co } crc_actual = crc64_finish(crc_actual); - FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%%\n\n"), 100.0); - fflush(stderr); + + const int status = slunkcrypt_encrypt_inplace(ctx, (uint8_t*)&crc_actual, sizeof(uint64_t)); + if (status != 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; + } if (fwrite(&crc_actual, sizeof(uint64_t), 1U, file_out) < 1U) { - FPUTS(T("I/O error: Failed to write CRC checksum!\n\n"), stderr); + FPUTS(T("\n\nI/O error: Failed to write CRC checksum!\n\n"), stderr); goto clean_up; } - result = 0; + FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%%\n\n"), 100.0); + fflush(stderr); + + result = EXIT_SUCCESS; FPUTS(T("All is done.\n\n"), stderr); fflush(stderr); @@ -305,9 +312,9 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co { slunkcrypt_t ctx = SLUNKCRYPT_NULL; FILE *file_in = NULL, *file_out = NULL; - int result = 1; + int result = EXIT_FAILURE; - if (open_files(&file_in, &file_out, input_path, output_path) != 0) + if (open_files(&file_in, &file_out, input_path, output_path) != EXIT_SUCCESS) { goto clean_up; } @@ -395,16 +402,24 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co } crc_actual = crc64_finish(crc_actual); - FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%%\n\n"), 100.0); - fflush(stderr); uint64_t crc_expected; if (fread(&crc_expected, sizeof(uint64_t), 1U, file_in) < 1U) { - FPUTS(T("I/O error: Failed to read CRC checksum!\n\n"), stderr); + FPUTS(T("\n\nI/O error: Failed to read CRC checksum!\n\n"), stderr); goto clean_up; } + const int status = slunkcrypt_decrypt_inplace(ctx, (uint8_t*)&crc_expected, sizeof(uint64_t)); + if (status != 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; + } + + FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%%\n\n"), 100.0); + fflush(stderr); + if (crc_actual != crc_expected) { FPRINTF(stderr, T("CRC error: Checksum mismatch detected! [expected: 0x%016") T(PRIX64) T(", actual: 0x%016") T(PRIX64) T("]\n\n"), crc_actual, crc_expected); @@ -412,7 +427,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co goto clean_up; } - result = 0; + result = EXIT_SUCCESS; FPUTS(T("CRC checksum is correct.\n\n"), stderr); fflush(stderr); @@ -445,15 +460,15 @@ static int run_test_case(const char *const message) { static const char* const passphrase = "OrpheanBeh0lderScry!Doubt"; + int status, result = EXIT_FAILURE; const size_t length = strlen(message) + 1U; - int status, result = 1; slunkcrypt_t ctx = SLUNKCRYPT_NULL; uint64_t seed; if (slunkcrypt_generate_seed(&seed) != SLUNKCRYPT_SUCCESS) { FPUTS(T("\n\nWhoops: Failed to generate seed!\n\n"), stderr); - return 1; + return EXIT_FAILURE; } char* const text_temp = strdup(message); @@ -503,7 +518,7 @@ static int run_test_case(const char *const message) goto clean_up; } - result = 0; + result = EXIT_SUCCESS; clean_up: @@ -536,9 +551,9 @@ static int run_self_test(void) { FPRINTF(stderr, T("\b\b\b\b\b\b%2u/%2u "), (unsigned int)++completed, (unsigned int)total); fflush(stderr); - if (run_test_case(test_data[j])) + if (run_test_case(test_data[j]) != EXIT_SUCCESS) { - return 1; + return EXIT_FAILURE; } } } @@ -546,7 +561,7 @@ static int run_self_test(void) FPRINTF(stderr, T("\b\b\b\b\b\b%2u/%2u\n\nCompleted successfully.\n\n"), (unsigned int)total, (unsigned int)total); fflush(stderr); - return 0; + return EXIT_SUCCESS; } // ========================================================================== @@ -557,7 +572,7 @@ int MAIN(const int argc, CHR *const argv[]) { init_terminal(); setup_signal_handler(SIGINT, sigint_handler); - int result = -1; + int result = EXIT_FAILURE; FPRINTF(stderr, T("SlunkCrypt Utility (%") T(PRIstr) T("-%") T(PRIstr) T("), by LoRd_MuldeR \n"), OS_TYPE, CPU_ARCH); FPRINTF(stderr, T("Using libSlunkCrypt v%u.%u.%u [%") T(PRIstr) T("]\n\n"), SLUNKCRYPT_VERSION_MAJOR, SLUNKCRYPT_VERSION_MINOR, SLUNKCRYPT_VERSION_PATCH, SLUNKCRYPT_BUILD); @@ -566,28 +581,27 @@ int MAIN(const int argc, CHR *const argv[]) /* Parse arguments */ /* ----------------------------------------------------- */ - const int mode = (argc > 1) ? parse_mode(argv[1U]) : OP_MODE_NONE; + if (argc < 2) + { + FPRINTF(stderr, T("Error: Nothing to do. Please type '%") T(PRISTR) T(" --help' for details!\n\n"), get_file_name(argv[0U])); + return EXIT_FAILURE; + } + + const int mode = parse_mode(argv[1U]); switch (mode) { - case OP_MODE_NONE: - FPRINTF(stderr, T("Error: Nothing to do. Please type '%") T(PRISTR) T(" --help' for details!\n\n"), get_file_name(argv[0U])); - return 1; case OP_MODE_HELP: print_manpage(get_file_name(argv[0U])); - return 0; - case OP_MODE_ENCR: - case OP_MODE_DECR: - break; + case OP_MODE_VERS: + return EXIT_SUCCESS; case OP_MODE_TEST: return run_self_test(); - default: - return -1; } if (argc < 4) { FPRINTF(stderr, T("Error: Required argument is missing. Please type '%") T(PRISTR) T(" --help' for details!\n\n"), get_file_name(argv[0U])); - return 1; + return EXIT_FAILURE; } const CHR* const passphrase = (argc > 4) ? argv[2U] : GETENV(ENVV_PASSWD_NAME); @@ -596,13 +610,13 @@ int MAIN(const int argc, CHR *const argv[]) if ((!passphrase) || (!passphrase[0U]) || (((passphrase[0U] == T('@')) || (passphrase[0U] == T(':'))) && (!passphrase[1U]))) { FPUTS(T("Error: The specified passphrase must not be empty!\n\n"), stderr); - return 1; + return EXIT_FAILURE; } if ((!input_file[0U]) || (!output_file[0U])) { FPUTS(T("Error: The input file and/or output file must not be empty!\n\n"), stderr); - return 1; + return EXIT_FAILURE; } /* ----------------------------------------------------- */ @@ -613,7 +627,7 @@ int MAIN(const int argc, CHR *const argv[]) if (!passphrase_buffer) { FPUTS(T("Error: Failed to read the passphrase!\n\n"), stderr); - return 1; + return EXIT_FAILURE; } slunkcrypt_bzero((CHR*)passphrase, STRLEN(passphrase) * sizeof(CHR)); @@ -654,7 +668,7 @@ int MAIN(const int argc, CHR *const argv[]) result = decrypt(passphrase_buffer, input_file, output_file); break; default: - abort(); /*not supposed to happen!*/ + FPUTS(T("Unexpected mode encountered!\n\n"), stderr); } if (!g_slunkcrypt_abort_flag)