From 68ac0772d9b9055da5f17023f965518bdcd6bf90 Mon Sep 17 00:00:00 2001 From: redraincatching <99604494+redraincatching@users.noreply.github.com> Date: Fri, 11 Jul 2025 11:42:28 +0100 Subject: [PATCH] reduce calls to random (#1710) implemented change suggested in TODO to speed up aes key generation without, hopefully, negatively impacting the overall randomness of the function --------- Co-authored-by: Gustavo Garcia Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/apps/relay/mainrelay.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/apps/relay/mainrelay.c b/src/apps/relay/mainrelay.c index efe6a18..3fafebd 100644 --- a/src/apps/relay/mainrelay.c +++ b/src/apps/relay/mainrelay.c @@ -37,6 +37,7 @@ #include "prom_server.h" #include +#include #if defined(WINDOWS) #include @@ -1761,29 +1762,26 @@ void encrypt_aes_128(unsigned char *in, const unsigned char *mykey) { printf("%s\n", base64_encoded); } static void generate_aes_128_key(char *filePath, unsigned char *returnedKey) { - char key[16]; + unsigned char key[16]; // TODO: Document why this is called...? turn_srandom(); +// generate two 64-bit random values +#if LONG_MAX > 0xffffffff + uint64_t random_value_0 = (uint64_t)turn_random(); + uint64_t random_value_1 = (uint64_t)turn_random(); +#else + uint64_t random_value_0 = (((uint64_t)turn_random()) << 32) | (uint64_t)turn_random(); + uint64_t random_value_1 = (((uint64_t)turn_random()) << 32) | (uint64_t)turn_random(); +#endif + for (size_t i = 0; i < 16; ++i) { - // TODO: This could be sped up by breaking the - // returned random value into multiple 8bit values - // instead of getting a new multi-byte random value - // for each key index. - switch (turn_random() % 3) { - case 0: - key[i] = (turn_random() % 10) + 48; - continue; - case 1: - key[i] = (turn_random() % 26) + 65; - continue; - default: - key[i] = (turn_random() % 26) + 97; - continue; - } + // store the 128 random bits in the key array + key[i] = (i < 8) ? (random_value_0 >> (i * 8)) & 0xff : (random_value_1 >> ((i - 8) * 8)) & 0xff; } - FILE *fptr = fopen(filePath, "w"); + + FILE *fptr = fopen(filePath, "wb"); if (!fptr) { return; }