From 70e9d2d3b5f5b77ac72f18c00d2d024ea969ae5b Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Thu, 7 Apr 2022 00:15:07 +0200 Subject: [PATCH] Fixed slunkcrypt_reset() when no thread pool is allocated, e.g. when thread count is 1. --- frontend/src/main.c | 2 +- frontend/src/selftest.c | 15 ++++++++------- frontend/src/selftest.h | 2 +- libslunkcrypt/src/slunkcrypt.c | 8 ++++---- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/frontend/src/main.c b/frontend/src/main.c index 2dfed86..b3882eb 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -195,7 +195,7 @@ int MAIN(const int argc, CHR *const argv[]) goto clean_up; case MODE_TEST: check_excess_arguments(argc, 2); - result = run_selftest_routine(); + result = run_selftest_routine(environ_get_uint(ENV_NTHREADS)); goto clean_up; default: FPRINTF(stderr, T("Error: The specified command \"%") T(PRISTR) T("\" is unknown!\n\n"), argv[1U]); diff --git a/frontend/src/selftest.c b/frontend/src/selftest.c index cefe7bb..499b307 100644 --- a/frontend/src/selftest.c +++ b/frontend/src/selftest.c @@ -26,7 +26,7 @@ // Self-test routines // ========================================================================== -static int run_testcase(const char* const message, const uint64_t nonce, const uint64_t checksum_message, const uint64_t checksum_expected) +static int run_testcase(const char* const message, const uint64_t nonce, const uint64_t checksum_message, const uint64_t checksum_expected, const slunkparam_t *const param) { static const char* const TEST_PASSPHRASE = "OrpheanBeh0lderScry!Doubt"; @@ -48,7 +48,7 @@ static int run_testcase(const char* const message, const uint64_t nonce, const u goto clean_up; } - ctx = slunkcrypt_alloc(nonce, (const uint8_t*)TEST_PASSPHRASE, strlen(TEST_PASSPHRASE), SLUNKCRYPT_ENCRYPT); + ctx = slunkcrypt_alloc_ext(nonce, (const uint8_t*)TEST_PASSPHRASE, strlen(TEST_PASSPHRASE), SLUNKCRYPT_ENCRYPT, param); if (!ctx) { FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize encoder!\n\n"), stderr); @@ -117,7 +117,7 @@ clean_up: return result; } -static int run_stresstest(const uint64_t nonce) +static int run_stresstest(const uint64_t nonce, const slunkparam_t *const param) { static const char* const TEST_PASSPHRASE = "OrpheanBeh0lderScry!Doubt"; static const size_t LENGTH = 134217689U, CHUNKZ_ENC = 8191U, CHUNKZ_DEC = 8179U; @@ -141,7 +141,7 @@ static int run_stresstest(const uint64_t nonce) const uint64_t checksum_original = blake2s_compute(buffer, LENGTH); - ctx = slunkcrypt_alloc(nonce, (const uint8_t*)TEST_PASSPHRASE, strlen(TEST_PASSPHRASE), SLUNKCRYPT_ENCRYPT); + ctx = slunkcrypt_alloc_ext(nonce, (const uint8_t*)TEST_PASSPHRASE, strlen(TEST_PASSPHRASE), SLUNKCRYPT_ENCRYPT, param); if (!ctx) { FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize encoder!\n\n"), stderr); @@ -199,7 +199,7 @@ clean_up: return result; } -int run_selftest_routine(void) +int run_selftest_routine(const size_t thread_count) { static const size_t ITERATIONS = 2U; static const uint64_t TEST_NONCE[] = { 0x243F6A8885A308D3, 0x13198A2E03707344 }; @@ -223,13 +223,14 @@ int run_selftest_routine(void) FPRINTF(stderr, T("Self-test is in progress, please be patient... stage %2u/%2u "), 0U, (unsigned)total); fflush(stderr); + const slunkparam_t param = { SLUNKCRYPT_PARAM_VERSION, thread_count }; for (size_t i = 0U; i < ARRAY_SIZE(TEST_STAGE); ++i) { for (size_t j = 0U; j < ARRAY_SIZE(TEST_NONCE); ++j) { FPRINTF(stderr, T("\b\b\b\b\b\b%2u/%2u "), (unsigned)++count, (unsigned)total); fflush(stderr); - if (run_testcase(TEST_STAGE[i].text, TEST_NONCE[j], TEST_STAGE[i].check_orig, TEST_STAGE[i].check_encr[j]) != EXIT_SUCCESS) + if (run_testcase(TEST_STAGE[i].text, TEST_NONCE[j], TEST_STAGE[i].check_orig, TEST_STAGE[i].check_encr[j], ¶m) != EXIT_SUCCESS) { return EXIT_FAILURE; } @@ -242,7 +243,7 @@ int run_selftest_routine(void) { FPRINTF(stderr, T("\b\b\b\b\b\b%2u/%2u "), (unsigned)++count, (unsigned)total); fflush(stderr); - if (run_stresstest(TEST_NONCE[j]) != EXIT_SUCCESS) + if (run_stresstest(TEST_NONCE[j], ¶m) != EXIT_SUCCESS) { return EXIT_FAILURE; } diff --git a/frontend/src/selftest.h b/frontend/src/selftest.h index cd44249..f796b6d 100644 --- a/frontend/src/selftest.h +++ b/frontend/src/selftest.h @@ -9,6 +9,6 @@ #include "platform.h" #include -int run_selftest_routine(void); +int run_selftest_routine(const size_t thread_count); #endif diff --git a/libslunkcrypt/src/slunkcrypt.c b/libslunkcrypt/src/slunkcrypt.c index 1a07540..7fa8383 100644 --- a/libslunkcrypt/src/slunkcrypt.c +++ b/libslunkcrypt/src/slunkcrypt.c @@ -311,8 +311,9 @@ slunkcrypt_t slunkcrypt_alloc_ext(const uint64_t nonce, const uint8_t *const pas if ((state->thread_pool = slunkcrypt_thrdpl_create(param->thread_count, thread_worker))) { + const size_t thread_count = slunkcrypt_thrdpl_count(state->thread_pool); size_t i; - for (i = 0U; i < slunkcrypt_thrdpl_count(state->thread_pool); ++i) + for (i = 0U; i < thread_count; ++i) { slunkcrypt_thrdpl_init(state->thread_pool, i, &state->data.thread_data[i]); } @@ -336,7 +337,7 @@ int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t nonce, const uin { return SLUNKCRYPT_FAILURE; } - if ((result = initialize_state(&state->data, slunkcrypt_thrdpl_count(state->thread_pool), nonce, passwd, passwd_len, mode)) != SLUNKCRYPT_SUCCESS) + if ((result = initialize_state(&state->data, THREAD_COUNT(state), nonce, passwd, passwd_len, mode)) != SLUNKCRYPT_SUCCESS) { slunkcrypt_bzero(&state->data, sizeof(crypt_data_t)); } @@ -370,8 +371,7 @@ int slunkcrypt_inplace(const slunkcrypt_t context, uint8_t *const buffer, size_t if (length > 0U) { - const size_t thread_count = THREAD_COUNT(state); - if (thread_count > 1U) + if (THREAD_COUNT(state) > 1U) { slunkcrypt_thrdpl_exec(state->thread_pool, buffer, length); }