From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43328) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aJ47P-0000qW-II for qemu-devel@nongnu.org; Tue, 12 Jan 2016 13:56:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aJ47O-0003MV-FT for qemu-devel@nongnu.org; Tue, 12 Jan 2016 13:56:35 -0500 From: "Daniel P. Berrange" Date: Tue, 12 Jan 2016 18:56:08 +0000 Message-Id: <1452624982-19332-2-git-send-email-berrange@redhat.com> In-Reply-To: <1452624982-19332-1-git-send-email-berrange@redhat.com> References: <1452624982-19332-1-git-send-email-berrange@redhat.com> Subject: [Qemu-devel] [PATCH v1 01/15] crypto: add cryptographic random byte source List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org Signed-off-by: Daniel P. Berrange --- crypto/Makefile.objs | 1 + crypto/random.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ include/crypto/random.h | 43 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 crypto/random.c create mode 100644 include/crypto/random.h diff --git a/crypto/Makefile.objs b/crypto/Makefile.objs index a3135f1..5f38d2d 100644 --- a/crypto/Makefile.objs +++ b/crypto/Makefile.objs @@ -8,6 +8,7 @@ crypto-obj-y += tlscredsanon.o crypto-obj-y += tlscredsx509.o crypto-obj-y += tlssession.o crypto-obj-y += secret.o +crypto-obj-y += random.o # Let the userspace emulators avoid linking gnutls/etc crypto-aes-obj-y = aes.o diff --git a/crypto/random.c b/crypto/random.c new file mode 100644 index 0000000..8257d24 --- /dev/null +++ b/crypto/random.c @@ -0,0 +1,50 @@ +/* + * QEMU Crypto random number provider + * + * Copyright (c) 2015 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + * + */ + +#include + +#include "crypto/random.h" + +int qcrypto_random_bytes(uint8_t *buf, + size_t buflen, + Error **errp) +{ + ssize_t ret; + int fd = open("/dev/random", O_RDONLY); + if (fd < 0) { + error_setg_errno(errp, errno, + "Unable to open /dev/random"); + return -1; + } + + while (buflen) { + ret = read(fd, buf, buflen); + if (ret < 0) { + error_setg_errno(errp, errno, + "Unable to read random bytes"); + close(fd); + return -1; + } + buflen -= ret; + } + + close(fd); + return 0; +} diff --git a/include/crypto/random.h b/include/crypto/random.h new file mode 100644 index 0000000..ce1626b --- /dev/null +++ b/include/crypto/random.h @@ -0,0 +1,43 @@ +/* + * QEMU Crypto random number provider + * + * Copyright (c) 2015 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + * + */ + +#ifndef QCRYPTO_RANDOM_H__ +#define QCRYPTO_RANDOM_H__ + +#include "qemu-common.h" +#include "qapi/error.h" + + +/** + * qcrypto_random_bytes: + * @buf: the buffer to fill + * @buflen: length of @buf in bytes + * @errp: pointer to uninitialized error objet + * + * Fill @buf with @buflen bytes of random data + * + * Returns 0 on sucess, -1 on error + */ +int qcrypto_random_bytes(uint8_t *buf, + size_t buflen, + Error **errp); + + +#endif /* QCRYPTO_RANDOM_H__ */ -- 2.5.0