From 8fe73fafe9653e3f274e3a5b847f6bfb9e36509e Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Thu, 22 Feb 2024 22:04:48 +0100 Subject: [PATCH] implemented getrandom instead of rand also made why2_generate_key's variables snake_case --- src/core/lib/utils/misc.c | 50 ++++++++++----------------------------- 1 file changed, 12 insertions(+), 38 deletions(-) diff --git a/src/core/lib/utils/misc.c b/src/core/lib/utils/misc.c index 0e13738..03e0762 100644 --- a/src/core/lib/utils/misc.c +++ b/src/core/lib/utils/misc.c @@ -24,7 +24,7 @@ along with this program. If not, see . #include #include #include -#include +#include #include #include @@ -34,8 +34,6 @@ along with this program. If not, see . #include #include -why2_bool seedSet = 0; //DO NOT FUCKING TOUCH THIS!!! - int multiply_cb(int a, int b) { return a * b; } int subtract_cb(int a, int b) { return a - b; } int sum_cb(int a, int b) { return a + b; } @@ -393,54 +391,30 @@ unsigned long why2_compare_time_micro(struct timeval startTime, struct timeval f char *why2_generate_key(int key_length) { - int numberBuffer; + int number_buffer; + unsigned int random_buffer; char *key; - if (!seedSet) - { - why2_set_memory_identifier("core_key_generation_random"); - - //TRY TO MAKE RANDOM GENERATION REALLY "RANDOM" - FILE *fileBuffer; - - fileBuffer = why2_fopen("/dev/urandom", "r"); - - if (fread(&numberBuffer, sizeof(numberBuffer), 1, fileBuffer) != 1) - { - if (!why2_get_flags().no_output) fprintf(stderr, "Reading file failed!\n"); - - why2_clean_memory("core_key_generation_random"); - return NULL; - } - - numberBuffer = abs(numberBuffer); //MAKE numberBuffer POSITIVE - srand(numberBuffer); - - why2_deallocate(fileBuffer); - - seedSet = 1; - - why2_reset_memory_identifier(); - } - key = why2_malloc(key_length + 1); for (int i = 0; i < key_length; i++) { + //GET RANDOM NUMBER + if (getrandom(&random_buffer, sizeof(unsigned int), GRND_NONBLOCK) == -1) why2_die("getrandom fn failed!"); + //SET numberBuffer TO RANDOM NUMBER BETWEEN 0 AND 52 - numberBuffer = (rand() % 52) + 1; + number_buffer = (random_buffer % 52) + 1; //GET CHAR FROM numberBuffer - if (numberBuffer > 26) + if (number_buffer > 26) { - numberBuffer += 70; - } - else + number_buffer += 70; + } else { - numberBuffer += 64; + number_buffer += 64; } - key[i] = (char) numberBuffer; + key[i] = (char) number_buffer; } key[key_length] = '\0';