OSDN Git Service

Implemented optional debug logging. Writes to the syslog (Unix) or debugger (Windows).
authorLoRd_MuldeR <mulder2@gmx.de>
Thu, 13 Oct 2022 22:44:19 +0000 (00:44 +0200)
committerLoRd_MuldeR <mulder2@gmx.de>
Thu, 13 Oct 2022 22:44:19 +0000 (00:44 +0200)
frontend/src/crypt.c
frontend/src/crypt.h
frontend/src/main.c
frontend/src/selftest.c
libslunkcrypt/include/slunkcrypt.h
libslunkcrypt/libSlunkCrypt.vcxproj
libslunkcrypt/libSlunkCrypt.vcxproj.filters
libslunkcrypt/src/debug.c [new file with mode: 0644]
libslunkcrypt/src/debug.h [new file with mode: 0644]
libslunkcrypt/src/slunkcrypt.c

index 7b8cc3e..501fc9a 100644 (file)
@@ -68,6 +68,7 @@ static void init_slunk_param(slunkparam_t *const param, const crypt_options_t *c
        param->version = SLUNKCRYPT_PARAM_VERSION;
        param->thread_count = options->thread_count;
        param->legacy_compat = options->legacy_compat;
+       param->debug_logging = options->debug_logging;
 }
 
 #define UPDATE_PROGRESS_INDICATOR(CLK_UPDATE, CURRENT, TOTAL) do \
index 8a8d21b..46798c8 100644 (file)
@@ -12,6 +12,7 @@ typedef struct
 {
        int keep_incomplete;
        int legacy_compat;
+       int debug_logging;
        size_t thread_count;
 }
 crypt_options_t;
index 4ae1252..340767b 100644 (file)
@@ -37,6 +37,7 @@ static const CHR* const ENV_PASSWORD = T("SLUNK_PASSPHRASE");
 static const CHR* const ENV_KEEPFILE = T("SLUNK_KEEP_INCOMPLETE");
 static const CHR* const ENV_NTHREADS = T("SLUNK_THREADS");
 static const CHR* const ENV_LGCYCMPT = T("SLUNK_LEGACY_COMPAT");
+static const CHR* const ENV_DEBUGLOG = T("SLUNK_DEBUG_LOGGING");
 
 static const CHR* const PREFIX_PASS = T("pass:");
 static const CHR* const PREFIX_FILE = T("file:");
@@ -288,7 +289,8 @@ int MAIN(const int argc, CHR *const argv[])
        /* ----------------------------------------------------- */
 
        const uint64_t clk_start = clock_read();
-       const crypt_options_t options = { environ_get_flag(ENV_KEEPFILE), environ_get_flag(ENV_LGCYCMPT), environ_get_uint(ENV_NTHREADS) };
+
+       const crypt_options_t options = { environ_get_flag(ENV_KEEPFILE), environ_get_flag(ENV_LGCYCMPT), environ_get_flag(ENV_DEBUGLOG), environ_get_uint(ENV_NTHREADS) };
 
        switch (slunk_mode)
        {
index 9e2ab9f..14e8710 100644 (file)
@@ -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 };
+                       const slunkparam_t param = { SLUNKCRYPT_PARAM_VERSION, thread_count, (j > 0) ? SLUNKCRYPT_TRUE : SLUNKCRYPT_FALSE, SLUNKCRYPT_TRUE };
                        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 };
+       const slunkparam_t param = { SLUNKCRYPT_PARAM_VERSION, thread_count, SLUNKCRYPT_FALSE, SLUNKCRYPT_TRUE };
        for (size_t i = 0U; i < ITERATIONS; ++i)
        {
                for (size_t j = 0U; j < ARRAY_SIZE(TEST_NONCE); ++j)
index 2739fa9..b0bf1fc 100644 (file)
@@ -90,6 +90,7 @@ typedef struct
        uint16_t version;    /* Must set to SLUNKCRYPT_PARAM_VERSION */
        size_t thread_count; /* Number of threads, set to 0 for auto-detection */
        int legacy_compat;   /* Compatibility with pre-1.3 versions */
+       int debug_logging;   /* Enable debug logging (writes to the syslog) */
 }
 slunkparam_t;
 
index 0a75c84..eaa6650 100644 (file)
@@ -51,6 +51,7 @@
     </ProjectConfiguration>
   </ItemGroup>
   <ItemGroup>
+    <ClCompile Include="src\debug.c" />
     <ClCompile Include="src\junk.c" />
     <ClCompile Include="src\keygen.c" />
     <ClCompile Include="src\slunkcrypt.c" />
@@ -60,6 +61,7 @@
     <ClInclude Include="include\slunkcrypt.h" />
     <ClInclude Include="include\slunkcrypt.hpp" />
     <ClInclude Include="src\compiler.h" />
+    <ClInclude Include="src\debug.h" />
     <ClInclude Include="src\initialize.h" />
     <ClInclude Include="src\keygen.h" />
     <ClInclude Include="src\thread.h" />
index b4106f5..22a17c7 100644 (file)
@@ -27,6 +27,9 @@
     <ClCompile Include="src\thread.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="src\debug.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="include\slunkcrypt.h">
@@ -50,5 +53,8 @@
     <ClInclude Include="src\initialize.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="src\debug.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>
\ No newline at end of file
diff --git a/libslunkcrypt/src/debug.c b/libslunkcrypt/src/debug.c
new file mode 100644 (file)
index 0000000..ee0f06a
--- /dev/null
@@ -0,0 +1,23 @@
+/******************************************************************************/
+/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de>                                */
+/* This work has been released under the CC0 1.0 Universal license!           */
+/******************************************************************************/
+
+/* Internal */
+#include "debug.h"
+
+#if defined(_WIN32)
+#  define WIN32_LEAN_AND_MEAN 1
+#  include <Windows.h>
+#elif defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
+#  include <syslog.h>
+#endif
+
+void slunkcrypt_debug_print(const char* const message)
+{
+#if defined(_WIN32)
+       OutputDebugStringA(message);
+#elif defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
+       syslog(LOG_DEBUG, "%s", message);
+#endif
+}
diff --git a/libslunkcrypt/src/debug.h b/libslunkcrypt/src/debug.h
new file mode 100644 (file)
index 0000000..e0e4159
--- /dev/null
@@ -0,0 +1,11 @@
+/******************************************************************************/
+/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de>                                */
+/* This work has been released under the CC0 1.0 Universal license!           */
+/******************************************************************************/
+
+#ifndef INC_SLUNKCRYPT_DEBUG_H
+#define INC_SLUNKCRYPT_DEBUG_H
+
+void slunkcrypt_debug_print(const char *const message);
+
+#endif
index 5473fbb..f1bf50b 100644 (file)
@@ -3,8 +3,15 @@
 /* This work has been released under the CC0 1.0 Universal license!           */
 /******************************************************************************/
 
+#ifdef _WIN32
+#  define _CRT_SECURE_NO_WARNINGS 1
+#else
+#  define _GNU_SOURCE 1
+#endif
+
 /* Internal */
 #include "slunkcrypt.h"
+#include "debug.h"
 #include "compiler.h"
 #include "keygen.h"
 #include "initialize.h"
@@ -13,6 +20,7 @@
 
 /* CRT */
 #include <string.h>
+#include <stdio.h>
 #include <limits.h>
 
 /* Version */
@@ -58,6 +66,7 @@ crypt_data_t;
 typedef struct
 {
        int legacy_compat;
+       int debug_logging;
        thrdpl_t *thread_pool;
        crypt_data_t data;
 }
@@ -144,7 +153,7 @@ static INLINE void random_seed(rand_state_t *const state, uint64_t salt, const u
 // Initialization
 // ==========================================================================
 
-static int initialize_state(crypt_data_t *const data, const size_t thread_count, const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int mode, const int legacy)
+static int initialize_state(crypt_data_t *const data, const size_t thread_count, const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int mode, const int legacy, const int debug)
 {
        uint8_t temp[256U][256U];
        size_t r, i;
@@ -191,6 +200,22 @@ 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;
@@ -295,7 +320,7 @@ int slunkcrypt_generate_nonce(uint64_t *const nonce)
 
 slunkcrypt_t slunkcrypt_alloc(const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int mode)
 {
-       const slunkparam_t param = { SLUNKCRYPT_PARAM_VERSION, 0U, SLUNKCRYPT_TRUE };
+       const slunkparam_t param = { SLUNKCRYPT_PARAM_VERSION, 0U, SLUNKCRYPT_TRUE, SLUNKCRYPT_FALSE };
        return slunkcrypt_alloc_ext(nonce, passwd, passwd_len, mode, &param);
 }
 
@@ -324,9 +349,10 @@ slunkcrypt_t slunkcrypt_alloc_ext(const uint64_t nonce, const uint8_t *const pas
                }
        }
 
-       state->legacy_compat = (param->version > 1U) ? BOOLIFY(param->legacy_compat) : SLUNKCRYPT_TRUE;
+       state->legacy_compat = (param->version > 1U) ? BOOLIFY(param->legacy_compat) : SLUNKCRYPT_FALSE;
+       state->debug_logging = (param->version > 1U) ? BOOLIFY(param->debug_logging) : SLUNKCRYPT_FALSE;
 
-       if (initialize_state(&state->data, THREAD_COUNT(state), nonce, passwd, passwd_len, mode, state->legacy_compat) == SLUNKCRYPT_SUCCESS)
+       if (initialize_state(&state->data, THREAD_COUNT(state), nonce, passwd, passwd_len, mode, state->legacy_compat, state->debug_logging) == SLUNKCRYPT_SUCCESS)
        {
                return (slunkcrypt_t)state;
        }
@@ -345,7 +371,7 @@ int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t nonce, const uin
                return SLUNKCRYPT_FAILURE;
        }
 
-       if ((result = initialize_state(&state->data, THREAD_COUNT(state), nonce, passwd, passwd_len, mode, state->legacy_compat)) != SLUNKCRYPT_SUCCESS)
+       if ((result = initialize_state(&state->data, THREAD_COUNT(state), nonce, passwd, passwd_len, mode, state->legacy_compat, state->debug_logging)) != SLUNKCRYPT_SUCCESS)
        {
                slunkcrypt_bzero(&state->data, sizeof(crypt_data_t));
        }