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

[Minios-devel] [UNIKRAFT PATCH 1/1] lib/ukswrand: Add /dev/random and /dev/urandom



This patch adds the /dev/random and
/dev/urandom devices. Since we
do not currenly have any entropy generating
function they share the same handlers. The
registration of the devices is guarded
by the config option CONFIG_DEV_RANDOM.

The devfs patch series is required.

Signed-off-by: Vlad-Andrei Badoiu <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>
---
 lib/ukswrand/Config.uk |  5 ++++
 lib/ukswrand/mwc.c     | 65 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/lib/ukswrand/Config.uk b/lib/ukswrand/Config.uk
index 44f6ee50..d694b61d 100644
--- a/lib/ukswrand/Config.uk
+++ b/lib/ukswrand/Config.uk
@@ -14,6 +14,11 @@ config LIBUKSWRAND_MWC
                Use multiply-with-carry algorithm
 endchoice
 
+
+config DEV_RANDOM
+       bool "/dev/random device"
+       select LIBDEVFS
+
 config LIBUKSWRAND_INITIALSEED
        int "Initial random seed"
        default 23
diff --git a/lib/ukswrand/mwc.c b/lib/ukswrand/mwc.c
index 199247d3..a276d657 100644
--- a/lib/ukswrand/mwc.c
+++ b/lib/ukswrand/mwc.c
@@ -37,6 +37,7 @@
 #include <uk/print.h>
 #include <uk/assert.h>
 #include <uk/ctors.h>
+#include <uk/config.h>
 
 /* 
https://stackoverflow.com/questions/9492581/c-random-number-generation-pure-c-code-no-libraries-or-functions
 */
 #define PHI 0x9e3779b9
@@ -97,3 +98,67 @@ static void _uk_swrand_ctor(void)
 }
 
 UK_CTOR_FUNC(UK_SWRAND_CTOR_PRIO, _uk_swrand_ctor);
+
+#ifdef CONFIG_DEV_RANDOM
+
+#include <vfscore/device.h>
+#include <vfscore/uio.h>
+#include <stdlib.h>
+
+static const char dev_name[] = "urandom";
+
+int dev_random_read(struct device *dev, struct uio *uio, int flags)
+{
+       char *buf;
+       size_t count;
+
+       buf = uio->uio_iov->iov_base;
+       count = uio->uio_iov->iov_len;
+
+       for (int i = 0; i < count; ++i)
+               buf[i] = uk_swrand_randr_r(&uk_swrand_def);
+
+       uio->uio_resid = 0;
+       return 0;
+}
+
+int dev_random_open(const char *c, int i, struct device **dev)
+{
+       return 0;
+}
+
+
+int dev_random_close(const char *c, int i, struct device **dev)
+{
+       return 0;
+}
+
+static struct devops random_devops = {
+       .read = dev_random_read,
+       .open = dev_random_open,
+       .close = dev_random_close,
+};
+
+static struct driver drv = {
+       .devops = &random_devops,
+       .devsz = 0,
+       .name = dev_name
+};
+
+__constructor_prio(102) static void _uk_dev_swrand_ctor(void)
+{
+       struct device *dev;
+
+       uk_pr_info("Add /dev/random and /dev/urandom\n");
+
+       /* register /dev/urandom */
+       dev = device_create(&drv, dev_name, D_CHR);
+       if (dev == NULL)
+               uk_pr_info("Failed to register /dev/urandom\n");
+
+       /* register /dev/random */
+       dev = device_create(&drv, dev_name + 1, D_CHR);
+       if (dev == NULL)
+               uk_pr_info("Failed to register /dev/random\n");
+}
+#endif
-- 
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®.