2012-01-25 15:17:46 +00:00
|
|
|
/*
|
2016-03-29 22:15:33 +00:00
|
|
|
* Copyright (C) 2012-2016 Red Hat, Inc.
|
2012-01-25 15:17:46 +00:00
|
|
|
*
|
|
|
|
* 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.1 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2012-01-25 15:17:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2012-02-10 04:51:47 +00:00
|
|
|
#include <inttypes.h>
|
2012-08-10 13:01:23 +00:00
|
|
|
#include <math.h>
|
2012-08-14 17:36:38 +00:00
|
|
|
#include <strings.h>
|
2015-04-14 19:15:06 +00:00
|
|
|
#include <time.h>
|
2016-03-29 22:15:33 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2018-05-29 07:43:26 +00:00
|
|
|
#ifdef WITH_GNUTLS
|
|
|
|
# include <gnutls/gnutls.h>
|
|
|
|
# include <gnutls/crypto.h>
|
|
|
|
#endif
|
2012-01-25 15:17:46 +00:00
|
|
|
|
|
|
|
#include "virrandom.h"
|
2012-12-13 15:49:48 +00:00
|
|
|
#include "virthread.h"
|
2012-01-25 15:17:46 +00:00
|
|
|
#include "count-one-bits.h"
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2016-03-29 22:15:33 +00:00
|
|
|
#include "virfile.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2012-02-10 04:51:47 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.random");
|
|
|
|
|
2018-05-29 06:58:46 +00:00
|
|
|
#define RANDOM_SOURCE "/dev/urandom"
|
|
|
|
|
2012-01-25 16:38:37 +00:00
|
|
|
/**
|
2012-01-25 15:17:46 +00:00
|
|
|
* virRandomBits:
|
2018-12-04 17:08:14 +00:00
|
|
|
* @nbits: Number of bits of randomness required
|
2012-01-25 15:17:46 +00:00
|
|
|
*
|
|
|
|
* Generate an evenly distributed random number between [0,2^nbits), where
|
|
|
|
* @nbits must be in the range (0,64].
|
|
|
|
*
|
|
|
|
* Return: a random number with @nbits entropy
|
|
|
|
*/
|
|
|
|
uint64_t virRandomBits(int nbits)
|
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
|
2018-05-29 06:26:18 +00:00
|
|
|
if (virRandomBytes((unsigned char *) &ret, sizeof(ret)) < 0) {
|
2012-08-03 23:15:00 +00:00
|
|
|
/* You're already hosed, so this particular non-random value
|
|
|
|
* isn't any worse. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-02 07:29:38 +00:00
|
|
|
if (nbits < 64)
|
|
|
|
ret &= (1ULL << nbits) - 1;
|
|
|
|
|
2012-01-25 15:17:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2012-02-10 04:51:47 +00:00
|
|
|
|
2012-08-10 13:01:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virRandom:
|
|
|
|
*
|
|
|
|
* Generate an evenly distributed random number between [0.0,1.0)
|
|
|
|
*
|
|
|
|
* Return: a random number with 48 bits of entropy
|
|
|
|
*/
|
|
|
|
double virRandom(void)
|
|
|
|
{
|
|
|
|
uint64_t val = virRandomBits(48);
|
|
|
|
|
|
|
|
return ldexp(val, -48);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virRandomInt:
|
|
|
|
* @max: upper limit
|
|
|
|
*
|
|
|
|
* Generate an evenly distributed random integer between [0, @max)
|
|
|
|
*
|
|
|
|
* Return: a random number between [0,@max)
|
|
|
|
*/
|
|
|
|
uint32_t virRandomInt(uint32_t max)
|
|
|
|
{
|
2012-08-14 17:36:38 +00:00
|
|
|
if ((max & (max - 1)) == 0)
|
|
|
|
return virRandomBits(ffs(max) - 1);
|
|
|
|
|
2012-08-10 13:01:23 +00:00
|
|
|
double val = virRandom();
|
|
|
|
return val * max;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-29 22:15:33 +00:00
|
|
|
/**
|
|
|
|
* virRandomBytes
|
|
|
|
* @buf: Pointer to location to store bytes
|
|
|
|
* @buflen: Number of bytes to store
|
|
|
|
*
|
2018-05-29 06:58:46 +00:00
|
|
|
* Generate a stream of random bytes from RANDOM_SOURCE
|
2016-03-29 22:15:33 +00:00
|
|
|
* into @buf of size @buflen
|
2016-06-07 11:24:31 +00:00
|
|
|
*
|
2018-05-29 06:58:46 +00:00
|
|
|
* Returns 0 on success or -1 (with error reported)
|
2016-03-29 22:15:33 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
virRandomBytes(unsigned char *buf,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
2018-05-29 07:43:26 +00:00
|
|
|
#if WITH_GNUTLS
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
/* Generate the byte stream using gnutls_rnd() if possible */
|
|
|
|
if ((rv = gnutls_rnd(GNUTLS_RND_RANDOM, buf, buflen)) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to generate byte stream: %s"),
|
|
|
|
gnutls_strerror(rv));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !WITH_GNUTLS */
|
|
|
|
|
2016-03-29 22:15:33 +00:00
|
|
|
int fd;
|
|
|
|
|
2018-05-29 06:58:46 +00:00
|
|
|
if ((fd = open(RANDOM_SOURCE, O_RDONLY)) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("unable to open %s"),
|
|
|
|
RANDOM_SOURCE);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-03-29 22:15:33 +00:00
|
|
|
|
|
|
|
while (buflen > 0) {
|
|
|
|
ssize_t n;
|
|
|
|
|
2018-05-29 06:55:28 +00:00
|
|
|
if ((n = saferead(fd, buf, buflen)) <= 0) {
|
2018-05-29 06:58:46 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("unable to read from %s"),
|
|
|
|
RANDOM_SOURCE);
|
2016-03-29 22:15:33 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2018-05-29 07:02:57 +00:00
|
|
|
return n < 0 ? -errno : -ENODATA;
|
2016-03-29 22:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf += n;
|
|
|
|
buflen -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
2018-05-29 07:43:26 +00:00
|
|
|
#endif /* !WITH_GNUTLS */
|
2016-03-29 22:15:33 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-10 04:51:47 +00:00
|
|
|
#define QUMRANET_OUI "001a4a"
|
|
|
|
#define VMWARE_OUI "000569"
|
|
|
|
#define MICROSOFT_OUI "0050f2"
|
|
|
|
#define XEN_OUI "00163e"
|
|
|
|
|
2016-03-29 22:15:33 +00:00
|
|
|
|
2012-02-10 04:51:47 +00:00
|
|
|
int
|
|
|
|
virRandomGenerateWWN(char **wwn,
|
2014-03-18 08:14:35 +00:00
|
|
|
const char *virt_type)
|
|
|
|
{
|
2012-02-10 04:51:47 +00:00
|
|
|
const char *oui = NULL;
|
|
|
|
|
|
|
|
if (!virt_type) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INVALID_ARG, "%s",
|
2012-02-10 04:51:47 +00:00
|
|
|
_("argument virt_type must not be NULL"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STREQ(virt_type, "QEMU")) {
|
|
|
|
oui = QUMRANET_OUI;
|
|
|
|
} else if (STREQ(virt_type, "Xen") ||
|
|
|
|
STREQ(virt_type, "xenlight") ||
|
|
|
|
STREQ(virt_type, "XenAPI")) {
|
|
|
|
oui = XEN_OUI;
|
|
|
|
} else if (STREQ(virt_type, "ESX") ||
|
|
|
|
STREQ(virt_type, "VMWARE")) {
|
|
|
|
oui = VMWARE_OUI;
|
|
|
|
} else if (STREQ(virt_type, "HYPER-V")) {
|
|
|
|
oui = MICROSOFT_OUI;
|
|
|
|
} else {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2012-02-10 04:51:47 +00:00
|
|
|
_("Unsupported virt type"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-18 10:13:46 +00:00
|
|
|
if (virAsprintf(wwn, "5" "%s%09llx", oui,
|
|
|
|
(unsigned long long)virRandomBits(36)) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2012-02-10 04:51:47 +00:00
|
|
|
}
|