From 8b1b8aec64c9dede498485419ec270df99c4e036 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sat, 9 Apr 2022 16:40:16 +0200 Subject: [PATCH] Added built-time option SLUNKBUILD_NOTHREADS to disable threading + added simple "testbed" script. --- Makefile | 10 +++++++++ README.md | 17 +++++++++++++-- etc/test/testbed.sh | 52 ++++++++++++++++++++++++++++++++++++++++++++++ libslunkcrypt/src/thread.c | 4 ++++ libslunkcrypt/src/thread.h | 19 ++++++++++++++--- 5 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 etc/test/testbed.sh diff --git a/Makefile b/Makefile index ac0f02e..a2b5a1c 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ STRIP ?= 0 CPU ?= 0 MARCH ?= 0 MTUNE ?= 0 +THREAD ?= 1 # --------------------------------------------------------------------------- # Directories @@ -31,6 +32,11 @@ CFLAGS = -I$(SUBDIR_LIB)/include -std=gnu99 -Wall ifneq ($(CPU),0) CFLAGS += -m$(firstword $(CPU)) endif +ifneq ($(TARGET),) + CFLAGS += --target=$(firstword $(TARGET)) + LDFLGS += --target=$(firstword $(TARGET)) +endif + ifneq ($(MARCH),0) CFLAGS += -march=$(firstword $(MARCH)) endif @@ -81,6 +87,10 @@ ifeq ($(MACHINE),$(filter %-w64-mingw32 %w64-windows-gnu,$(MACHINE))) LDFLGS += -mconsole -municode endif +ifeq ($(THREAD),0) + CFLAGS += -DSLUNKBUILD_NOTHREADS +endif + # --------------------------------------------------------------------------- # File names # --------------------------------------------------------------------------- diff --git a/README.md b/README.md index 4c3d6b9..a1d5bc4 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,9 @@ The SlunkCrypt library and the command-line application currently run on the fol * **Microsoft Windows** (Windows XP SP-3, or later) — 32-Bit (i686) and 64-Bit (AMD64) * **Linux** (kernel version 3.17, or later) — 32-Bit (i686) and 64-Bit (AMD64) -* **Various BSD flavors** (tested on FreeBSD 13 and OpenBSD 7) — 32-Bit (i686) and 64-Bit (AMD64) -* **Oracle Solaris** (tested on version 11.4) — 32-Bit (i686) and 64-Bit (AMD64) +* **Various BSD flavors** (tested on NetBSD 9.2, FreeBSD 13.0 and OpenBSD 7.0) — 32-Bit (i686) and 64-Bit (AMD64) +* **Solaris** (tested on Solaris 11.4 and OmniOS/illumos) — 32-Bit (i686) and 64-Bit (AMD64) +* **Mac OS X** (tested on macOS 11 “Big Sur”) — Intel x86-64 (AMD64) and Apple Silicon (AArch64) The SlunkCrypt GUI application currently runs on the following platforms: @@ -327,6 +328,8 @@ Encrypt the next message chunk, using separate input/output buffers. * `length` The length of the plaintext chunk contained in the *input* buffer given by the `input` parameter, in bytes. At the same time, this determines the minimum required size of the *output* buffer given by the `output` parameters, in bytes. + *Note:* It is recommended to process chunks of at least ∼64 KB each, in order to take full advantage of multi-threading. + ***Return value:*** * If successful, `true` is returned; otherwise `false` is returned. @@ -373,6 +376,8 @@ Encrypt the next message chunk, using a single buffer. * `length` The length of the plaintext chunk initially contained in the input/output buffer given by the `buffer` parameter, in bytes. At the same time, this determines the portion of the input/output buffer that will be overwritten with encrypted data, in bytes. + *Note:* It is recommended to process chunks of at least ∼64 KB each, in order to take full advantage of multi-threading. + ***Return value:*** * If successful, `true` is returned; otherwise `false` is returned. @@ -464,6 +469,8 @@ Decrypt the next message chunk, using separate input/output buffers. * `length` The length of the ciphertext chunk contained in the *input* buffer given by the `input` parameter, in bytes. At the same time, this determines the minimum required size of the *output* buffer given by the `output` parameters, in bytes. + *Note:* It is recommended to process chunks of at least ∼64 KB each, in order to take full advantage of multi-threading. + ***Return value:*** * If successful, `true` is returned; otherwise `false` is returned. @@ -510,6 +517,8 @@ Decrypt the next message chunk, using a single buffer. * `length` The length of the ciphertext chunk initially contained in the input/output buffer given by the `buffer` parameter, in bytes. At the same time, this determines the portion of the input/output buffer that will be overwritten with decrypted data, in bytes. + *Note:* It is recommended to process chunks of at least ∼64 KB each, in order to take full advantage of multi-threading. + ***Return value:*** * If successful, `true` is returned; otherwise `false` is returned. @@ -701,6 +710,8 @@ Encrypt or decrypt the next message chunk, using separate input/output buffers. * `length` The length of the given plaintext chunk (*encryption* mode), or the length of the given ciphertext chunk (*decryption* mode) in the `input` buffer, in bytes. At the same time, this parameter determines the minimum required size of the `output` buffer, in bytes. If this parameter is set to *zero*, the function does nothing; this is *not* considered an error. + *Note:* It is recommended to process chunks of at least ∼64 KB each, in order to take full advantage of multi-threading. + ***Return value:*** * If successful, `SLUNKCRYPT_SUCCESS` is returned; otherwise `SLUNKCRYPT_FAILURE` or `SLUNKCRYPT_ABORTED` is returned. @@ -730,6 +741,8 @@ Encrypt or decrypt the next message chunk, using a *single* input/output buffer. * `length` The length of the plaintext chunk (*encryption* mode), or the length of the ciphertext chunk (*decryption* mode) initially contained in the input/output buffer, in bytes. + *Note:* It is recommended to process chunks of at least ∼64 KB each, in order to take full advantage of multi-threading. + ***Return value:*** * If successful, `SLUNKCRYPT_SUCCESS` is returned; otherwise `SLUNKCRYPT_FAILURE` or `SLUNKCRYPT_ABORTED` is returned. diff --git a/etc/test/testbed.sh b/etc/test/testbed.sh new file mode 100644 index 0000000..732adb3 --- /dev/null +++ b/etc/test/testbed.sh @@ -0,0 +1,52 @@ +#!/bin/bash +cd -- "$(dirname -- "${BASH_SOURCE[0]}")" + +rm -rf "enc" "out" +mkdir -p "bin" "org" "enc" "out" + +readonly PASSWD="T25na1i{XYuUMFDi2fRqk258" + +for file in org/*; do + name="$(basename -- "$file")" + + # Encrypt -A- + echo -e "\n\033[1;36m---===[ ${name} ]===---\033[0m\n" + if ! ${BASH} -x -c "bin/slunkcrypt-a -e \"pass:${PASSWD}\" \"${file}\" \"enc/${name}~~A\""; then + echo -e "\n\033[1;31mError: File could not be encoded !!!\033[0m" + exit 1 + fi + + # Encrypt -B- + if ! ${BASH} -x -c "bin/slunkcrypt-b -e \"pass:${PASSWD}\" \"${file}\" \"enc/${name}~~B\""; then + echo -e "\n\033[1;31mError: File could not be encoded !!!\033[0m" + exit 1 + fi + + # Print hash + sha256sum "enc/${name}~~A" "enc/${name}~~B" && echo "" + + # Decrypt -A/B- + if ! ${BASH} -x -c "bin/slunkcrypt-a -d \"pass:${PASSWD}\" \"enc/${name}~~B\" \"out/${name}~~A\""; then + echo -e "\n\033[1;31mError: File could not be decoded !!!\033[0m" + exit 1 + fi + if ! cmp "out/${name}~~A" "${file}"; then + echo -e "\n\033[1;31mError: Decoded file does *not* match original !!!\033[0m" + exit 1 + fi + + # Decrypt -B/A- + if ! ${BASH} -x -c "bin/slunkcrypt-b -d \"pass:${PASSWD}\" \"enc/${name}~~A\" \"out/${name}~~B\""; then + echo -e "\n\033[1;31mError: File could not be decoded !!!\033[0m" + exit + fi + if ! cmp "out/${name}~~A" "out/${name}~~B"; then + echo -e "\n\033[1;31mError: Decoded files are *not* the same !!!\033[0m" + exit 1 + fi + + # Print hash + sha256sum "out/${name}~~A" "out/${name}~~B" && echo "" +done + +echo -e "\n\033[1;32mAll tests have completed successfully!\033[0m" diff --git a/libslunkcrypt/src/thread.c b/libslunkcrypt/src/thread.c index 89ac88e..cb92046 100644 --- a/libslunkcrypt/src/thread.c +++ b/libslunkcrypt/src/thread.c @@ -3,6 +3,8 @@ /* This work has been released under the CC0 1.0 Universal license! */ /******************************************************************************/ +#ifndef SLUNKBUILD_NOTHREADS + #ifdef _WIN32 # define _CRT_SECURE_NO_WARNINGS 1 #else @@ -318,3 +320,5 @@ void slunkcrypt_thrdpl_destroy(thrdpl_t *const thrdpl) free(thrdpl); } + +#endif /*SLUNKBUILD_NOTHREADS*/ diff --git a/libslunkcrypt/src/thread.h b/libslunkcrypt/src/thread.h index a2ae84c..c59742a 100644 --- a/libslunkcrypt/src/thread.h +++ b/libslunkcrypt/src/thread.h @@ -9,10 +9,12 @@ #include #include -#define MAX_THREADS 32U - -typedef void (*thrdpl_worker_t)(const size_t thread_count, void *const context, uint8_t *const buffer, const size_t length); typedef struct thrdpl_data_t thrdpl_t; +typedef void(*thrdpl_worker_t)(const size_t thread_count, void *const context, uint8_t *const buffer, const size_t length); + +#ifndef SLUNKBUILD_NOTHREADS + +#define MAX_THREADS 32U thrdpl_t *slunkcrypt_thrdpl_create(const size_t count, const thrdpl_worker_t worker); size_t slunkcrypt_thrdpl_count(const thrdpl_t *const thrdpl); @@ -20,4 +22,15 @@ void slunkcrypt_thrdpl_init(thrdpl_t *const thrdpl, const size_t index, void *co void slunkcrypt_thrdpl_exec(thrdpl_t *const thrdpl, uint8_t *const buffer, const size_t length); void slunkcrypt_thrdpl_destroy(thrdpl_t *const thrdpl); +#else + +#define MAX_THREADS 1U + +#define slunkcrypt_thrdpl_create(X,Y) NULL +#define slunkcrypt_thrdpl_count(X) 0U +#define slunkcrypt_thrdpl_init(X,Y,Z) do { abort(); } while(0) +#define slunkcrypt_thrdpl_exec(X,Y,Z) do { abort(); } while(0) +#define slunkcrypt_thrdpl_destroy(X) do { abort(); } while(0) + +#endif #endif -- 2.11.0