From 38cfba2f71bfa9bdea562cb6465b9dc0155fc467 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Mon, 8 Mar 2021 19:30:48 +0100 Subject: Better randomization (soon: random memory locs) --- libc/random.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'libc/random.c') diff --git a/libc/random.c b/libc/random.c index 8c8076f..cfd082d 100644 --- a/libc/random.c +++ b/libc/random.c @@ -1,5 +1,6 @@ // MIT License, Copyright (c) 2020 Marvin Borner +#include #include #include #include @@ -11,6 +12,34 @@ void srand(u32 seed) g_seed = seed; } +u32 rdrand(void) +{ +#ifdef kernel + if (!cpu_has_cfeature(CPUID_FEAT_ECX_RDRND)) + return rand(); + + u32 rd; + __asm__ volatile("rdrand %%eax" : "=a"(rd)); + return rd; +#else + return rand(); +#endif +} + +u32 rdseed(void) +{ +#ifdef kernel + if (!cpu_has_cfeature(CPUID_FEAT_ECX_RDRND)) + return rand(); + + u32 rd; + __asm__ volatile("rdseed %%eax" : "=a"(rd)); + return rd; +#else + return rand(); +#endif +} + u32 rand(void) { g_seed = g_seed * 1103515245 + 12345; -- cgit v1.2.3