From mboxrd@z Thu Jan 1 00:00:00 1970 From: David Vrabel Subject: [PATCH 2/8] libxl: add libxl__random_bytes() which fills a buffer with random bytes Date: Thu, 12 Jun 2014 16:04:32 +0100 Message-ID: <1402585478-3389-3-git-send-email-david.vrabel@citrix.com> References: <1402585478-3389-1-git-send-email-david.vrabel@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1Wv6ZY-0002ly-8O for xen-devel@lists.xenproject.org; Thu, 12 Jun 2014 15:05:48 +0000 In-Reply-To: <1402585478-3389-1-git-send-email-david.vrabel@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xenproject.org Cc: David Vrabel , Ian Jackson , Ian Campbell , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org 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 --- 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