diff options
author | Mattias Andrée <maandree@kth.se> | 2023-06-23 18:08:07 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2023-06-23 18:08:07 +0200 |
commit | 53cf9901f408f2ae69a16912eb90ae45d96e24a7 (patch) | |
tree | 9340d1cc535884791a6a10babbddd8469f6efceb | |
parent | Whitespace fix + do not call srand unless required (diff) | |
download | libar2simplified-53cf9901f408f2ae69a16912eb90ae45d96e24a7.tar.gz libar2simplified-53cf9901f408f2ae69a16912eb90ae45d96e24a7.tar.bz2 libar2simplified-53cf9901f408f2ae69a16912eb90ae45d96e24a7.tar.xz |
Improve srand seed
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r-- | libar2simplified_decode_r.c | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/libar2simplified_decode_r.c b/libar2simplified_decode_r.c index d5df293..3f9dfea 100644 --- a/libar2simplified_decode_r.c +++ b/libar2simplified_decode_r.c @@ -1,7 +1,9 @@ /* See LICENSE file for copyright and license details. */ #include "common.h" #ifdef __linux__ +#include <sys/auxv.h> #include <sys/random.h> +#include <stdint.h> #endif #include <time.h> @@ -37,11 +39,16 @@ random_salt(char *out, size_t n, int (*random_byte_generator)(char *out, size_t #define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" static int srand_called = 0; + unsigned seed; double x; size_t i; int xi; #ifdef __linux__ ssize_t r; +# ifdef AT_RANDOM + uintptr_t raddr_; + uint16_t *raddr; +# endif #endif if (random_byte_generator) { @@ -58,7 +65,23 @@ random_salt(char *out, size_t n, int (*random_byte_generator)(char *out, size_t #endif if (i < n) { if (!srand_called) { - srand((unsigned int)time(NULL) ^ (unsigned int)rand()); + seed = (unsigned)time(NULL) ^ (unsigned)rand(); + seed ^= (unsigned)(uintptr_t)out; +#if defined(__linux__) && defined(AT_RANDOM) + raddr_ = (uintptr_t)getauxval(AT_RANDOM); + if (raddr_) { + raddr = (void *)raddr_; + seed ^= (unsigned)raddr[0]; + seed ^= (unsigned)raddr[1]; + seed ^= (unsigned)raddr[2]; + seed ^= (unsigned)raddr[3]; + seed ^= (unsigned)raddr[4]; + seed ^= (unsigned)raddr[5]; + seed ^= (unsigned)raddr[6]; + seed ^= (unsigned)raddr[7]; + } +#endif + srand(seed); srand_called = 1; } do { |