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

[Minios-devel] [UNIKRAFT PATCH] lib/ukswrand: Add getrandom() function



This function is used for filling buffers with random data. We introduce it now
because it is used by Python 3 interpreter. Although we define the GRND_NONBLOCK
and GRND_RANDOM flags, they are not used internally because we use the same
"source" as the uk_swrand_randr() function. Given that the reading from
'/dev/random' implied the same core logic, we also refactor a bit and use a
common internal function for filling buffers: uk_swrand_fill_buffer().

Signed-off-by: Costin Lupu <costin.lupu@xxxxxxxxx>
---
 lib/ukswrand/Makefile.uk          |  1 +
 lib/ukswrand/dev.c                | 12 +-------
 lib/ukswrand/exportsyms.uk        |  1 +
 lib/ukswrand/getrandom.c          | 44 ++++++++++++++++++++++++++
 lib/ukswrand/include/sys/random.h | 51 +++++++++++++++++++++++++++++++
 lib/ukswrand/include/uk/swrand.h  |  3 ++
 lib/ukswrand/mwc.c                | 20 ++++++++++++
 7 files changed, 121 insertions(+), 11 deletions(-)
 create mode 100644 lib/ukswrand/getrandom.c
 create mode 100644 lib/ukswrand/include/sys/random.h

diff --git a/lib/ukswrand/Makefile.uk b/lib/ukswrand/Makefile.uk
index 055699de..6cf1223e 100644
--- a/lib/ukswrand/Makefile.uk
+++ b/lib/ukswrand/Makefile.uk
@@ -5,3 +5,4 @@ CXXINCLUDES-$(CONFIG_LIBUKSWRAND) += 
-I$(LIBUKSWRAND_BASE)/include
 
 LIBUKSWRAND_SRCS-$(CONFIG_LIBUKSWRAND_MWC) += $(LIBUKSWRAND_BASE)/mwc.c
 LIBUKSWRAND_SRCS-$(CONFIG_LIBUKSWRAND_DEVFS) += $(LIBUKSWRAND_BASE)/dev.c
+LIBUKSWRAND_SRCS-y += $(LIBUKSWRAND_BASE)/getrandom.c
diff --git a/lib/ukswrand/dev.c b/lib/ukswrand/dev.c
index adca6566..fe232942 100644
--- a/lib/ukswrand/dev.c
+++ b/lib/ukswrand/dev.c
@@ -56,17 +56,7 @@ int dev_random_read(struct device *dev __unused, struct uio 
*uio,
        buf = uio->uio_iov->iov_base;
        count = uio->uio_iov->iov_len;
 
-       step = sizeof(__u32);
-       chunk_size = count % step;
-
-       for (i = 0; i < count - chunk_size; i += step)
-               *(buf + i) = uk_swrand_randr();
-
-       /* fill the remaining bytes of the buffer */
-       if (chunk_size > 0) {
-               rd = uk_swrand_randr();
-               memcpy(buf + i, &rd, chunk_size);
-       }
+       uk_swrand_fill_buffer(buf, count);
 
        uio->uio_resid = 0;
        return 0;
diff --git a/lib/ukswrand/exportsyms.uk b/lib/ukswrand/exportsyms.uk
index aedc6daf..49744046 100644
--- a/lib/ukswrand/exportsyms.uk
+++ b/lib/ukswrand/exportsyms.uk
@@ -1,3 +1,4 @@
+getrandom
 uk_swrand_def
 uk_swrand_init_r
 uk_swrand_randr_r
diff --git a/lib/ukswrand/getrandom.c b/lib/ukswrand/getrandom.c
new file mode 100644
index 00000000..f4567385
--- /dev/null
+++ b/lib/ukswrand/getrandom.c
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Authors: Costin Lupu <costin.lupu@xxxxxxxxx>
+ *
+ * Copyright (c) 2019, University Politehnica of Bucharest. All rights 
reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
+ */
+
+#include <string.h>
+#include <sys/random.h>
+#include <uk/essentials.h>
+#include <uk/swrand.h>
+
+
+ssize_t getrandom(void *buf, size_t buflen, unsigned int flags __unused)
+{
+       return uk_swrand_fill_buffer(buf, buflen);
+}
diff --git a/lib/ukswrand/include/sys/random.h 
b/lib/ukswrand/include/sys/random.h
new file mode 100644
index 00000000..4ccee1cc
--- /dev/null
+++ b/lib/ukswrand/include/sys/random.h
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Authors: Costin Lupu <costin.lupu@xxxxxxxxx>
+ *
+ * Copyright (c) 2019, University Politehnica of Bucharest. All rights 
reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
+ */
+
+#ifndef _SYS_RANDOM_H
+#define _SYS_RANDOM_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <sys/types.h>
+
+#define GRND_NONBLOCK     0x01
+#define GRND_RANDOM       0x02
+
+ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/lib/ukswrand/include/uk/swrand.h b/lib/ukswrand/include/uk/swrand.h
index 9a93e878..8523e207 100644
--- a/lib/ukswrand/include/uk/swrand.h
+++ b/lib/ukswrand/include/uk/swrand.h
@@ -35,6 +35,7 @@
 #ifndef __UK_SWRAND__
 #define __UK_SWRAND__
 
+#include <sys/types.h>
 #include <uk/arch/types.h>
 #include <uk/plat/lcpu.h>
 #include <uk/config.h>
@@ -71,6 +72,8 @@ static inline __u32 uk_swrand_randr(void)
        return ret;
 }
 
+ssize_t uk_swrand_fill_buffer(void *buf, size_t buflen);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/ukswrand/mwc.c b/lib/ukswrand/mwc.c
index 39c3daaf..427d7f92 100644
--- a/lib/ukswrand/mwc.c
+++ b/lib/ukswrand/mwc.c
@@ -90,6 +90,26 @@ __u32 uk_swrand_randr_r(struct uk_swrand *r)
        return (r->Q[i] = y - x);
 }
 
+ssize_t uk_swrand_fill_buffer(void *buf, size_t buflen)
+{
+       size_t step, chunk_size, i;
+       __u32 rd;
+
+       step = sizeof(__u32);
+       chunk_size = buflen % step;
+
+       for (i = 0; i < buflen - chunk_size; i += step)
+               *((char *) buf + i) = uk_swrand_randr();
+
+       /* fill the remaining bytes of the buffer */
+       if (chunk_size > 0) {
+               rd = uk_swrand_randr();
+               memcpy(buf + i, &rd, chunk_size);
+       }
+
+       return buflen;
+}
+
 static void _uk_swrand_ctor(void)
 {
        uk_pr_info("Initialize random number generator...\n");
-- 
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®.