From 3d30e8a3898c33ac633d5a8071eddbb11079b32b Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sat, 30 Dec 2023 21:40:55 -0600 Subject: [PATCH] Progress: Further work in TacocaT. I previously just jammed on a random seed based on time as a quick short term solution. I created an f_random FLL project to provide some basic random entropy processing code. This change set focuses on utilizing these to get set the random entropy. The setup of the seed is set to non-blocking with a single delayed retry if blocked. On failure, this still falls back to the time() based setup. The fallback also must be cast t an unsigned int rather than a long. --- data/build/tacocat/dependencies | 1 + data/build/tacocat/settings | 2 +- sources/c/tacocat/main/common/define.h | 7 +++++++ sources/c/tacocat/main/tacocat.c | 22 ++++++++++++++++++++-- sources/c/tacocat/main/tacocat.h | 1 + 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/data/build/tacocat/dependencies b/data/build/tacocat/dependencies index 9b4c10e..94519c6 100644 --- a/data/build/tacocat/dependencies +++ b/data/build/tacocat/dependencies @@ -16,6 +16,7 @@ f_network f_path f_pipe f_print +f_random f_signal f_socket f_status_string diff --git a/data/build/tacocat/settings b/data/build/tacocat/settings index 3366b94..05f59ef 100644 --- a/data/build/tacocat/settings +++ b/data/build/tacocat/settings @@ -32,7 +32,7 @@ build_indexer_arguments rcs build_language c build_libraries -lc -build_libraries-individual -lfll_error -lfll_fss -lfll_print -lfll_program -lfl_conversion -lfl_fss -lfl_print -lf_color -lf_compare -lf_console -lf_conversion -lf_file -lf_fss -lf_memory -lf_network -lf_path -lf_pipe -lf_print -lf_signal -lf_socket -lf_status_string -lf_string -lf_time -lf_type_array -lf_utf +build_libraries-individual -lfll_error -lfll_fss -lfll_print -lfll_program -lfl_conversion -lfl_fss -lfl_print -lf_color -lf_compare -lf_console -lf_conversion -lf_file -lf_fss -lf_memory -lf_network -lf_path -lf_pipe -lf_print -lf_random -lf_signal -lf_socket -lf_status_string -lf_string -lf_time -lf_type_array -lf_utf build_libraries-individual_thread -lf_thread build_libraries-level -lfll_2 -lfll_1 -lfll_0 build_libraries-monolithic -lfll diff --git a/sources/c/tacocat/main/common/define.h b/sources/c/tacocat/main/common/define.h index f8ca80e..1e59c7f 100644 --- a/sources/c/tacocat/main/common/define.h +++ b/sources/c/tacocat/main/common/define.h @@ -56,6 +56,10 @@ extern "C" { * - retry_delay_second: The delay in seconds to wait between each retry. * - retry_delay_millisecond: The delay in milliseconds to wait between each retry. * - retry_max: During start up, the maximum number of retries to perform when trying to establish the initial connection before giving up. + * + * kt_tacocat_startup_seed_*_d: + * - delay_second: The delay in seconds to wait if the first non-blocking random seed setup fails. + * - delay_nanosecond: The delay in nanoseconds to wait if the first non-blocking random seed setup fails. */ #ifndef _di_kt_tacocat_d_ #define kt_tacocat_allocation_console_d 0x4 @@ -87,6 +91,9 @@ extern "C" { #define kt_tacocat_startup_retry_delay_second_d 3 #define kt_tacocat_startup_retry_delay_millisecond_d 0 #define kt_tacocat_startup_retry_max_d 24 + + #define kt_tacocat_startup_seed_delay_second_d 0 + #define kt_tacocat_startup_seed_delay_nanosecond_d 100 #endif // _di_kt_tacocat_d_ /** diff --git a/sources/c/tacocat/main/tacocat.c b/sources/c/tacocat/main/tacocat.c index a0e259a..4ff0112 100644 --- a/sources/c/tacocat/main/tacocat.c +++ b/sources/c/tacocat/main/tacocat.c @@ -39,8 +39,26 @@ extern "C" { return; } - // Establish random seed. - srandom((long) time(0)); + main->setting.state.status = f_random_seed(GRND_NONBLOCK); + + // Try again, but only once if blocked. + if (main->setting.state.status == F_status_set_error(F_again)) { + { + const struct timespec time = { .tv_sec = kt_tacocat_startup_seed_delay_second_d, .tv_nsec = kt_tacocat_startup_seed_delay_nanosecond_d }; + + nanosleep(&time, 0); + } + + main->setting.state.status = f_random_seed(GRND_NONBLOCK); + } + + if (F_status_is_error(main->setting.state.status)) { + + // Fall back to the far less secure but better than nothing method of using time as the seed. + f_random_seed_set((unsigned int) time(0)); + + main->setting.state.status = F_okay; + } kt_tacocat_process_main(main); diff --git a/sources/c/tacocat/main/tacocat.h b/sources/c/tacocat/main/tacocat.h index 7cf37c4..b15ea95 100644 --- a/sources/c/tacocat/main/tacocat.h +++ b/sources/c/tacocat/main/tacocat.h @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include -- 1.8.3.1