From b0cd820fdf3237a4545bea0603edf557ba5a89da Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Fri, 14 Oct 2022 01:13:37 +0200 Subject: [PATCH] Make debug logging optional (disabled by default) in self-test mode. --- frontend/src/crypt.c | 2 +- frontend/src/main.c | 6 +++--- frontend/src/selftest.c | 6 +++--- frontend/src/selftest.h | 2 +- libslunkcrypt/src/debug.c | 27 ++++++++++++++++++++----- libslunkcrypt/src/debug.h | 3 ++- libslunkcrypt/src/slunkcrypt.c | 36 +++++++++++++++++++--------------- 7 files changed, 52 insertions(+), 30 deletions(-) diff --git a/frontend/src/crypt.c b/frontend/src/crypt.c index 501fc9a..d51bc5d 100644 --- a/frontend/src/crypt.c +++ b/frontend/src/crypt.c @@ -66,9 +66,9 @@ static void init_slunk_param(slunkparam_t *const param, const crypt_options_t *c { slunkcrypt_bzero(param, sizeof(slunkparam_t)); param->version = SLUNKCRYPT_PARAM_VERSION; - param->thread_count = options->thread_count; param->legacy_compat = options->legacy_compat; param->debug_logging = options->debug_logging; + param->thread_count = options->thread_count; } #define UPDATE_PROGRESS_INDICATOR(CLK_UPDATE, CURRENT, TOTAL) do \ diff --git a/frontend/src/main.c b/frontend/src/main.c index 340767b..de81813 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -156,7 +156,7 @@ int MAIN(const int argc, CHR *const argv[]) setup_signal_handler(SIGINT, sigint_handler); FPRINTF(stderr, T("SlunkCrypt Utility (%") T(PRIstr) T("-%") T(PRIstr) T("), by LoRd_MuldeR \n"), OS_TYPE_NAME, CPU_ARCH); - FPRINTF(stderr, T("Using libSlunkCrypt-%") T(PRIstr) T(" v%u.%u.%u [%") T(PRIstr) T("]\n\n"), + FPRINTF(stderr, T("Using libSlunkCrypt-%") T(PRIstr) T(" v%u.%u.%u [%.18") T(PRIstr) T("]\n\n"), SLUNKCRYPT_HAVE_THREADS ? "MT" : "ST", SLUNKCRYPT_VERSION_MAJOR, SLUNKCRYPT_VERSION_MINOR, SLUNKCRYPT_VERSION_PATCH, SLUNKCRYPT_BUILD); fflush(stderr); @@ -194,10 +194,10 @@ int MAIN(const int argc, CHR *const argv[]) goto clean_up; case MODE_TEST: check_excess_arguments(argc, 2); - result = run_selftest_routine(environ_get_uint(ENV_NTHREADS)); + result = selftest_routine(environ_get_uint(ENV_NTHREADS), environ_get_flag(ENV_DEBUGLOG)); goto clean_up; default: - FPRINTF(stderr, T("Error: The specified command \"%") T(PRISTR) T("\" is unknown!\n\n"), argv[1U]); + FPRINTF(stderr, T("Error: The specified command \"%") T(PRISTR) T("\" is unrecognized!\n\n"), argv[1U]); goto clean_up; } diff --git a/frontend/src/selftest.c b/frontend/src/selftest.c index 14e8710..7c4f506 100644 --- a/frontend/src/selftest.c +++ b/frontend/src/selftest.c @@ -196,7 +196,7 @@ clean_up: return result; } -int run_selftest_routine(const size_t thread_count) +int selftest_routine(const size_t thread_count, const int debug) { static const size_t ITERATIONS = 2U; static const uint64_t TEST_NONCE[] = { 0x243F6A8885A308D3, 0x13198A2E03707344 }; @@ -224,7 +224,7 @@ int run_selftest_routine(const size_t thread_count) { for (size_t j = 0U; j < ITERATIONS; ++j) { - const slunkparam_t param = { SLUNKCRYPT_PARAM_VERSION, thread_count, (j > 0) ? SLUNKCRYPT_TRUE : SLUNKCRYPT_FALSE, SLUNKCRYPT_TRUE }; + const slunkparam_t param = { SLUNKCRYPT_PARAM_VERSION, thread_count, (j > 0) ? SLUNKCRYPT_TRUE : SLUNKCRYPT_FALSE, debug }; for (size_t k = 0U; k < ARRAY_SIZE(TEST_NONCE); ++k) { FPRINTF(stderr, T("\b\b\b\b\b\b%2u/%2u "), (unsigned)++count, (unsigned)total); @@ -237,7 +237,7 @@ int run_selftest_routine(const size_t thread_count) } } - const slunkparam_t param = { SLUNKCRYPT_PARAM_VERSION, thread_count, SLUNKCRYPT_FALSE, SLUNKCRYPT_TRUE }; + const slunkparam_t param = { SLUNKCRYPT_PARAM_VERSION, thread_count, SLUNKCRYPT_FALSE, debug }; for (size_t i = 0U; i < ITERATIONS; ++i) { for (size_t j = 0U; j < ARRAY_SIZE(TEST_NONCE); ++j) diff --git a/frontend/src/selftest.h b/frontend/src/selftest.h index 3297b84..01a9d08 100644 --- a/frontend/src/selftest.h +++ b/frontend/src/selftest.h @@ -8,6 +8,6 @@ #include "platform.h" -int run_selftest_routine(const size_t thread_count); +int selftest_routine(const size_t thread_count, const int debug); #endif diff --git a/libslunkcrypt/src/debug.c b/libslunkcrypt/src/debug.c index ee0f06a..d0df4dc 100644 --- a/libslunkcrypt/src/debug.c +++ b/libslunkcrypt/src/debug.c @@ -6,18 +6,35 @@ /* Internal */ #include "debug.h" +/* CRT */ +#include +#include + +/* syslog */ #if defined(_WIN32) # define WIN32_LEAN_AND_MEAN 1 # include -#elif defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) +#elif defined(__unix__) || defined(__HAIKU__) # include #endif -void slunkcrypt_debug_print(const char* const message) +void slunkcrypt_debug_write(const char* const text) { #if defined(_WIN32) - OutputDebugStringA(message); -#elif defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) - syslog(LOG_DEBUG, "%s", message); + OutputDebugStringA(text); +#elif defined(__unix__) || defined(__HAIKU__) + syslog(LOG_DEBUG, "%s", text); #endif } + +void slunkcrypt_debug_print(const char* const text, ...) +{ + char buffer[100U]; + va_list ap; + va_start(ap, text); + if (vsnprintf(buffer, 100U, text, ap) > 0) + { + slunkcrypt_debug_write(buffer); + } + va_end(ap); +} diff --git a/libslunkcrypt/src/debug.h b/libslunkcrypt/src/debug.h index e0e4159..8e082a9 100644 --- a/libslunkcrypt/src/debug.h +++ b/libslunkcrypt/src/debug.h @@ -6,6 +6,7 @@ #ifndef INC_SLUNKCRYPT_DEBUG_H #define INC_SLUNKCRYPT_DEBUG_H -void slunkcrypt_debug_print(const char *const message); +void slunkcrypt_debug_write(const char *const text); +void slunkcrypt_debug_print(const char *const text, ...); #endif diff --git a/libslunkcrypt/src/slunkcrypt.c b/libslunkcrypt/src/slunkcrypt.c index f1bf50b..72fccad 100644 --- a/libslunkcrypt/src/slunkcrypt.c +++ b/libslunkcrypt/src/slunkcrypt.c @@ -200,22 +200,6 @@ static int initialize_state(crypt_data_t *const data, const size_t thread_count, CHECK_ABORTED(); } - /* dump final wheel configuration */ - if (debug) - { - char message[772U]; - for (r = 0U; r < 256U; ++r) - { - size_t off = sprintf(message, "%02X:", (unsigned)r); - for (i = 0U; i < 256U; ++i) - { - off += sprintf(message + off, ",%02X", data->wheel[r][i]); - } - message[3U] = '\x20'; - slunkcrypt_debug_print(message); - } - } - /* initialize thread state */ data->thread_data[0].reverse_mode = reverse_mode; data->thread_data[0].wheel = (const uint8_t(*)[256]) data->wheel; @@ -232,6 +216,26 @@ static int initialize_state(crypt_data_t *const data, const size_t thread_count, CHECK_ABORTED(); } + /* dump the final configuration */ + if (debug) + { + slunkcrypt_debug_print("cntr = %08X", data->thread_data[0].counter); + slunkcrypt_debug_print("drbg = %08X %08X %08X %08X %08X %08X", + data->thread_data[0].random.d, data->thread_data[0].random.v, data->thread_data[0].random.w, + data->thread_data[0].random.x, data->thread_data[0].random.y, data->thread_data[0].random.z); + for (r = 0U; r < 256U; ++r) + { + char text[775U]; + size_t off = sprintf(text, "[%02X] =", (unsigned)r); + for (i = 0U; i < 256U; ++i) + { + off += sprintf(text + off, " %02X", data->wheel[r][i]); + } + CHECK_ABORTED(); + slunkcrypt_debug_write(text); + } + } + return SLUNKCRYPT_SUCCESS; /* aborted */