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>
|
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>
|
2020-01-22 16:21:35 +00:00
|
|
|
#include <gnutls/gnutls.h>
|
|
|
|
#include <gnutls/crypto.h>
|
2012-01-25 15:17:46 +00:00
|
|
|
|
|
|
|
#include "virrandom.h"
|
2012-12-13 15:49:48 +00:00
|
|
|
#include "virthread.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");
|
|
|
|
|
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)
|
|
|
|
{
|
2020-11-04 18:41:27 +00:00
|
|
|
if (VIR_IS_POW2(max))
|
2019-10-03 14:51:30 +00:00
|
|
|
return virRandomBits(__builtin_ffs(max) - 1);
|
2012-08-14 17:36:38 +00:00
|
|
|
|
2020-07-28 18:09:31 +00:00
|
|
|
return virRandom() * max;
|
2012-08-10 13:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-29 22:15:33 +00:00
|
|
|
/**
|
|
|
|
* virRandomBytes
|
|
|
|
* @buf: Pointer to location to store bytes
|
|
|
|
* @buflen: Number of bytes to store
|
|
|
|
*
|
2020-01-22 16:21:35 +00:00
|
|
|
* Generate a stream of random bytes using gnutls_rnd()
|
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
|
|
|
int rv;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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") ||
|
2019-09-03 04:24:00 +00:00
|
|
|
STREQ(virt_type, "xenlight")) {
|
2012-02-10 04:51:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
*wwn = g_strdup_printf("5" "%s%09llx", oui,
|
|
|
|
(unsigned long long)virRandomBits(36));
|
2013-07-18 10:13:46 +00:00
|
|
|
return 0;
|
2012-02-10 04:51:47 +00:00
|
|
|
}
|