[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Minios-devel] [UNIKRAFT PATCH v5 2/4] lib/ukswrand: Add seed generating function


  • To: "minios-devel@xxxxxxxxxxxxx" <minios-devel@xxxxxxxxxxxxx>
  • From: Vlad-Andrei BĂDOIU (78692) <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>
  • Date: Wed, 27 Nov 2019 12:41:49 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=stud.acs.upb.ro; dmarc=pass action=none header.from=stud.acs.upb.ro; dkim=pass header.d=stud.acs.upb.ro; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=iZ0/jYeUnHZRegJ9i6kvybOFyrnVBbNZwIXGcT94zJc=; b=B03SCnPC8clfmdkgJijwR295oyf4p0sRG7+yUbOXtu3p3XcEsYtjqIxo+uSfMzFuo6aWK/flK1iH9y/GtUPS49qFoKt7IMRUZAwDJ18FUYaSWYW0sZsNcDiujfs47Vkdk6oxbLFohpXlge/2cGdcslzfuuIAue18Xu5W70fFMjThEWvzmGVBZHAcaIK0TBNdWWO66SILM+0ulkVq5XC/K/UvQ1ltXW1Sbhdn/vRgmoFkYB3v18fpRlfx/uLmZspuT47qTLSk9AxctA9HF55LxUKdo2Tr9sc+RqEcEsPpIRpHwmnO2aobvIK2bgZPVbXnrSCzL29C8OrTcjItBTLBjw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PNsBPjQYIXNR0B66rs2Yaa03EsP7+NWzhY71p4kSaz6pjhGbDpUpFzg4jfYT0a8boCunPUD4d4r5IT2CDCLYwjEL86o/ZNrBdBiNN/wr4pF6grMbzyv5XKNej5W8onb0e7MPiGQOEkGOLtxceRmRF3l3aIBCrfUtPVzhMVhL0nbT0xfdwKcuQtr0cntu4+7QzVz8iIr9ee/yr2rCWtotZH2c+KGJC13kQGCLbL+y8ffzs5iBye5yX/PehpE2e37H6oaw5njss/MrHG74TCa7AuoGJL8ymRSh6JIRFs5rEg70M7zzXtojXky68fEUGi6EzNgr6AHt4JssZc+EFtHwVQ==
  • Authentication-results: spf=none (sender IP is ) smtp.mailfrom=vlad_andrei.badoiu@xxxxxxxxxxxxxxx;
  • Cc: "felipe.huici@xxxxxxxxx" <felipe.huici@xxxxxxxxx>, "simon.kuenzer@xxxxxxxxx" <simon.kuenzer@xxxxxxxxx>, Vlad-Andrei BĂDOIU (78692) <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>
  • Delivery-date: Wed, 27 Nov 2019 12:42:03 +0000
  • List-id: Mini-os development list <minios-devel.lists.xenproject.org>
  • Thread-index: AQHVpSAJkX+ku5GRNEurQDNZO8Gz+w==
  • Thread-topic: [UNIKRAFT PATCH v5 2/4] lib/ukswrand: Add seed generating function

From: Vlad-Andrei BĂDOIU (78692) <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>

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/exportsyms.uk       |  1 +
 lib/ukswrand/include/uk/swrand.h |  5 ++++-
 lib/ukswrand/mwc.c               | 11 ++++++-----
 lib/ukswrand/swrand.c            | 29 ++++++++++++++++++++++++++++-
 5 files changed, 58 insertions(+), 10 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/exportsyms.uk b/lib/ukswrand/exportsyms.uk
index 49744046..40739365 100644
--- a/lib/ukswrand/exportsyms.uk
+++ b/lib/ukswrand/exportsyms.uk
@@ -2,3 +2,4 @@ getrandom
 uk_swrand_def
 uk_swrand_init_r
 uk_swrand_randr_r
+uk_swrandr_gen_seed32
diff --git a/lib/ukswrand/include/uk/swrand.h b/lib/ukswrand/include/uk/swrand.h
index 35f3e2fe..29be8a9b 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" {
@@ -48,9 +49,11 @@ struct uk_swrand;
 
 extern struct uk_swrand uk_swrand_def;
 
-void uk_swrand_init_r(struct uk_swrand *r, __u32 seed);
+void uk_swrand_init_r(struct uk_swrand *r, unsigned int seedc,
+                       const __u32 seedv[]);
 __u32 uk_swrand_randr_r(struct uk_swrand *r);
 
+__u32 uk_swrandr_gen_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 8b4cca30..f032e950 100644
--- a/lib/ukswrand/mwc.c
+++ b/lib/ukswrand/mwc.c
@@ -48,15 +48,17 @@ struct uk_swrand {
 
 struct uk_swrand uk_swrand_def;
 
-void uk_swrand_init_r(struct uk_swrand *r, __u32 seed)
+void uk_swrand_init_r(struct uk_swrand *r, unsigned int seedc,
+               const __u32 seedv[])
 {
        __u32 i;
 
        UK_ASSERT(r);
+       UK_ASSERT(seedc > 0);
 
-       r->Q[0] = seed;
-       r->Q[1] = seed + PHI;
-       r->Q[2] = seed + PHI + PHI;
+       r->Q[0] = seedv[0];
+       r->Q[1] = seedv[0] + PHI;
+       r->Q[2] = seedv[0] + PHI + PHI;
        for (i = 3; i < 4096; i++)
                r->Q[i] = r->Q[i - 3] ^ r->Q[i - 2] ^ PHI ^ i;
 
@@ -88,4 +90,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..b3f410fb 100644
--- a/lib/ukswrand/swrand.c
+++ b/lib/ukswrand/swrand.c
@@ -37,6 +37,26 @@
 
 #define UK_SWRAND_CTOR_PRIO    1
 
+__u32 uk_swrandr_gen_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;
@@ -59,8 +79,15 @@ ssize_t uk_swrand_fill_buffer(void *buf, size_t buflen)
 
 static void _uk_swrand_ctor(void)
 {
+       unsigned int i;
+       unsigned int seedc = 2;
+       __u32 seedv[2];
+
+       for (i = 0; i < seedc; i++)
+               seedv[i] = uk_swrandr_gen_seed32();
+
        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, seedc, seedv);
 }
 
 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

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.