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@citrix.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
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
/*
return domid > 0 && domid < DOMID_FIRST_RESERVED;
}
+/*
+ * 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