Message ID | 1452624982-19332-2-git-send-email-berrange@redhat.com |
---|---|
State | New |
Headers | show |
On Tue, 01/12 18:56, Daniel P. Berrange wrote: > Signed-off-by: Daniel P. Berrange <berrange@redhat.com> > --- > 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. s/2015/2016/ > + * > + * 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 <http://www.gnu.org/licenses/>. > + * > + */ > + > +#include <config-host.h> > + > +#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. s/2015/2016/ > + * > + * 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 <http://www.gnu.org/licenses/>. > + * > + */ > + > +#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 s/objet/object/, but more importantly @errp must be NULL, so it's not exact to say it "uninitialized". > + * > + * 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 > >
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 <http://www.gnu.org/licenses/>. + * + */ + +#include <config-host.h> + +#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 <http://www.gnu.org/licenses/>. + * + */ + +#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__ */
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- 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