|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Minios-devel] [UNIKRAFT PATCH v3 2/4] lib/ukswrand: Add seed generating function
This patch adds a new function, _get_random_seed32 for seed generaton. If the
pre-compiled config option is not selected, then rdrand is used on x86 and
ukplat_wall_clock on ARM.
Signed-off-by: Vlad-Andrei Badoiu <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>
---
lib/ukswrand/Config.uk | 22 +++++++++++++++++++---
lib/ukswrand/include/uk/swrand.h | 2 ++
lib/ukswrand/mwc.c | 1 -
lib/ukswrand/swrand.c | 22 +++++++++++++++++++++-
4 files changed, 42 insertions(+), 5 deletions(-)
diff --git a/lib/ukswrand/Config.uk b/lib/ukswrand/Config.uk
index c58371bb..e056b4c5 100644
--- a/lib/ukswrand/Config.uk
+++ b/lib/ukswrand/Config.uk
@@ -14,9 +14,25 @@ config LIBUKSWRAND_MWC
Use multiply-with-carry algorithm
endchoice
-config LIBUKSWRAND_INITIALSEED
- int "Initial random seed"
- default 23
+choice
+ prompt "Initial seed"
+ default LIBUKSWRANDR_INITIALSEED_TIME
+
+config LIBUKSWRAND_INITIALSEED_TIME
+ bool "Platform timestamp"
+
+config LIBUKSWRAND_INITIALSEED_RDRAND
+ depends on ARCH_X86_64 && (MARCH_X86_64_COREI7AVXI)
+ bool "`rdrand` instruction"
+
+config LIBUKSWRAND_INITIALSEED_USECONSTANT
+ bool "Compiled-in constant"
+endchoice
+
+config LIBUKSWRAND_INITIALSEED_CONSTANT
+ int "Initial seed constant"
+ depends on LIBUKSWRAND_INITIALSEED_USECONSTANT
+ default 23
config LIBUKSWRAND_DEVFS
bool "Register random and urandom device to devfs"
diff --git a/lib/ukswrand/include/uk/swrand.h b/lib/ukswrand/include/uk/swrand.h
index 35f3e2fe..7f5eeed2 100644
--- a/lib/ukswrand/include/uk/swrand.h
+++ b/lib/ukswrand/include/uk/swrand.h
@@ -39,6 +39,7 @@
#include <uk/arch/types.h>
#include <uk/plat/lcpu.h>
#include <uk/config.h>
+#include <uk/plat/time.h>
#ifdef __cplusplus
extern "C" {
@@ -51,6 +52,7 @@ extern struct uk_swrand uk_swrand_def;
void uk_swrand_init_r(struct uk_swrand *r, __u32 seed);
__u32 uk_swrand_randr_r(struct uk_swrand *r);
+__u32 _get_random_seed32(void);
/* Uses the pre-initialized default generator */
/* TODO: Add assertion when we can test if we are in interrupt context */
/* TODO: Revisit with multi-CPU support */
diff --git a/lib/ukswrand/mwc.c b/lib/ukswrand/mwc.c
index 3ee07051..19166c4b 100644
--- a/lib/ukswrand/mwc.c
+++ b/lib/ukswrand/mwc.c
@@ -93,4 +93,3 @@ __u32 uk_swrand_randr_r(struct uk_swrand *r)
r->c = c;
return (r->Q[i] = y - x);
}
-
diff --git a/lib/ukswrand/swrand.c b/lib/ukswrand/swrand.c
index d9497bf8..16ad3496 100644
--- a/lib/ukswrand/swrand.c
+++ b/lib/ukswrand/swrand.c
@@ -37,6 +37,26 @@
#define UK_SWRAND_CTOR_PRIO 1
+__u32 _get_random_seed32(void)
+{
+ __u32 val;
+
+#ifdef CONFIG_LIBUKSWRAND_INITIALSEED_TIME
+ val = (__u32)ukplat_wall_clock();
+#endif
+
+#ifdef CONFIG_LIBUKSWRAND_INITIALSEED_RDRAND
+ asm volatile ("rdrand %%eax;"
+ : "=a" (val));
+#endif
+
+#ifdef CONFIG_LIBUKSWRAND_INITIALSEED_USECONSTANT
+ val = CONFIG_LIBUKSWRAND_INITIALSEED_CONSTANT;
+#endif
+
+ return val;
+}
+
ssize_t uk_swrand_fill_buffer(void *buf, size_t buflen)
{
size_t step, chunk_size, i;
@@ -60,7 +80,7 @@ ssize_t uk_swrand_fill_buffer(void *buf, size_t buflen)
static void _uk_swrand_ctor(void)
{
uk_pr_info("Initialize random number generator...\n");
- uk_swrand_init_r(&uk_swrand_def, CONFIG_LIBUKSWRAND_INITIALSEED);
+ uk_swrand_init_r(&uk_swrand_def, _get_random_seed32());
}
UK_CTOR_FUNC(UK_SWRAND_CTOR_PRIO, _uk_swrand_ctor);
--
2.20.1
_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |