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@citrix.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
This commit is contained in:
David Vrabel authored and Ian Campbell committed 2014-06-27 14:13:21 +01:00
1 parent 0a9daad380
commit 6a2ba5cb7e
2 files changed
+24

No files matched your search

+2
View File
@@ -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
/*
+22
View File
@@ -1013,6 +1013,28 @@ int libxl_domid_valid_guest(uint32_t domid)
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
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.