From f95608f52a0a82b3a8c005c7f8dad0b9cf93202a Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Wed, 20 Nov 2024 17:56:04 +0100 Subject: [PATCH] moved key's random number generation to why2_random fn --- include/crypto.h | 3 +++ src/core/lib/utils/crypto.c | 7 +++++++ src/core/lib/utils/misc.c | 4 ++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/crypto.h b/include/crypto.h index 8ddedbb..c6ef173 100644 --- a/include/crypto.h +++ b/include/crypto.h @@ -23,6 +23,8 @@ along with this program. If not, see . extern "C" { #endif +#include + //MACROS #define WHY2_SUM_SEGMENT_SIZE 32 //SEGMENT SIZE FOR CALCULATING SUM #define WHY2_SUM_BASE_PRIME 31 //PRIME FOR SUM BASE @@ -30,6 +32,7 @@ extern "C" { //FUNCTIONS unsigned long long why2_sum_segment(char *input); //CALCULATE SUM++ FOR input; USED FOR PADDING SEED +ssize_t why2_random(void *dest, size_t size); //WRITE CRYPTO-SECURE RANDOM NUMBER INTO dest #ifdef __cplusplus } diff --git a/src/core/lib/utils/crypto.c b/src/core/lib/utils/crypto.c index 670f603..a7ddaac 100644 --- a/src/core/lib/utils/crypto.c +++ b/src/core/lib/utils/crypto.c @@ -20,6 +20,8 @@ along with this program. If not, see . #include #include +#include +#include unsigned long long why2_sum_segment(char *input) //THE OUTPUT IS GOING TO GROW A LOT WITH LONG input, BUT IT SHOULDN'T BE A BIG PROBLEM. I TESTED FOR OVERFLOWS UP TO 4096-CHAR input AND ONLY GOT TO (14*10^(-7))% OF FULL ULL RANGE LMAO { @@ -43,4 +45,9 @@ unsigned long long why2_sum_segment(char *input) //THE OUTPUT IS GOING TO GROW A } return output; +} + +ssize_t why2_random(void *dest, size_t size) +{ + return getrandom(dest, size, GRND_NONBLOCK); } \ No newline at end of file diff --git a/src/core/lib/utils/misc.c b/src/core/lib/utils/misc.c index 935d901..06302f7 100644 --- a/src/core/lib/utils/misc.c +++ b/src/core/lib/utils/misc.c @@ -24,13 +24,13 @@ along with this program. If not, see . #include #include #include -#include #include #include #include #include +#include #include #include @@ -422,7 +422,7 @@ char *why2_generate_key(int key_length) 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!"); + if (why2_random(&random_buffer, sizeof(unsigned int)) == -1) why2_die("getrandom fn failed!"); //SET numberBuffer TO RANDOM NUMBER BETWEEN 0 AND 52 number_buffer = (random_buffer % 52) + 1;