|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH 2/8] libxl: add libxl__random_bytes() which fills a buffer with random bytes
The random bytes are obtained from /dev/urandom and are suitable for
almost all uses (except for generating long-lived secure keys).
Documentation suggests that /dev/urandom is widely available on Unix-like
systems (such FreeBSD and NetBSD).
A public libxl_random_bytes() (or similar) could be trivially added,
if this required in the future.
Signed-off-by: David Vrabel <david.vrabel@xxxxxxxxxx>
Acked-by: Ian Campbell <ian.campbell@xxxxxxxxxx>
---
tools/libxl/libxl_internal.h | 2 ++
tools/libxl/libxl_utils.c | 22 ++++++++++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index a0d4f24..a9343e8 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -3180,6 +3180,8 @@ int libxl__uint64_parse_json(libxl__gc *gc, const
libxl__json_object *o,
int libxl__string_parse_json(libxl__gc *gc, const libxl__json_object *o,
char **p);
+int libxl__random_bytes(libxl__gc *gc, uint8_t *buf, size_t len);
+
#endif
/*
diff --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c
index 476921e..3be01a5 100644
--- a/tools/libxl/libxl_utils.c
+++ b/tools/libxl/libxl_utils.c
@@ -1014,6 +1014,28 @@ int libxl_domid_valid_guest(uint32_t domid)
}
/*
+ * Fill @buf with @len random bytes.
+ */
+int libxl__random_bytes(libxl__gc *gc, uint8_t *buf, size_t len)
+{
+ static const char *dev = "/dev/urandom";
+ int fd;
+ int ret;
+
+ fd = open(dev, O_RDONLY | O_CLOEXEC);
+ if (fd < 0) {
+ LOGE(ERROR, "failed to open \"%s\"", dev);
+ return ERROR_FAIL;
+ }
+
+ ret = libxl_read_exactly(CTX, fd, buf, len, dev, NULL);
+
+ close(fd);
+
+ return ret;
+}
+
+/*
* Local variables:
* mode: C
* c-basic-offset: 4
--
1.7.10.4
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |