Fixed slunkcrypt_reset() when no thread pool is allocated, e.g. when thread count is 1.

This commit is contained in:
LoRd_MuldeR 2022-04-07 00:15:07 +02:00
parent e4f28439c3
commit 70e9d2d3b5
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
4 changed files with 14 additions and 13 deletions

View File

@ -195,7 +195,7 @@ int MAIN(const int argc, CHR *const argv[])
goto clean_up; goto clean_up;
case MODE_TEST: case MODE_TEST:
check_excess_arguments(argc, 2); check_excess_arguments(argc, 2);
result = run_selftest_routine(); result = run_selftest_routine(environ_get_uint(ENV_NTHREADS));
goto clean_up; goto clean_up;
default: 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 unknown!\n\n"), argv[1U]);

View File

@ -26,7 +26,7 @@
// Self-test routines // 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"; 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; 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) if (!ctx)
{ {
FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize encoder!\n\n"), stderr); 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; 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 char* const TEST_PASSPHRASE = "OrpheanBeh0lderScry!Doubt";
static const size_t LENGTH = 134217689U, CHUNKZ_ENC = 8191U, CHUNKZ_DEC = 8179U; 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); 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) if (!ctx)
{ {
FPUTS(g_slunkcrypt_abort_flag ? T("\n\nProcess interrupted!\n\n") : T("\n\nWhoops: Failed to initialize encoder!\n\n"), stderr); 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; return result;
} }
int run_selftest_routine(void) int run_selftest_routine(const size_t thread_count)
{ {
static const size_t ITERATIONS = 2U; static const size_t ITERATIONS = 2U;
static const uint64_t TEST_NONCE[] = { 0x243F6A8885A308D3, 0x13198A2E03707344 }; 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); FPRINTF(stderr, T("Self-test is in progress, please be patient... stage %2u/%2u "), 0U, (unsigned)total);
fflush(stderr); 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 i = 0U; i < ARRAY_SIZE(TEST_STAGE); ++i)
{ {
for (size_t j = 0U; j < ARRAY_SIZE(TEST_NONCE); ++j) 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); FPRINTF(stderr, T("\b\b\b\b\b\b%2u/%2u "), (unsigned)++count, (unsigned)total);
fflush(stderr); 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], &param) != EXIT_SUCCESS)
{ {
return EXIT_FAILURE; 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); FPRINTF(stderr, T("\b\b\b\b\b\b%2u/%2u "), (unsigned)++count, (unsigned)total);
fflush(stderr); fflush(stderr);
if (run_stresstest(TEST_NONCE[j]) != EXIT_SUCCESS) if (run_stresstest(TEST_NONCE[j], &param) != EXIT_SUCCESS)
{ {
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View File

@ -9,6 +9,6 @@
#include "platform.h" #include "platform.h"
#include <stdint.h> #include <stdint.h>
int run_selftest_routine(void); int run_selftest_routine(const size_t thread_count);
#endif #endif

View File

@ -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))) 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; 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]); 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; 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)); 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) if (length > 0U)
{ {
const size_t thread_count = THREAD_COUNT(state); if (THREAD_COUNT(state) > 1U)
if (thread_count > 1U)
{ {
slunkcrypt_thrdpl_exec(state->thread_pool, buffer, length); slunkcrypt_thrdpl_exec(state->thread_pool, buffer, length);
} }