mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-10-30 09:53:10 +00:00
util: Introduce virRandomBytes
Using the existing virUUIDGenerateRandomBytes, move API to virrandom.c rename it to virRandomBytes and add it to libvirt_private.syms. This will be used as a fallback for generating a domain master key.
This commit is contained in:
parent
d125685ad3
commit
6af73f53c6
@ -2094,6 +2094,7 @@ virProcessWait;
|
|||||||
# util/virrandom.h
|
# util/virrandom.h
|
||||||
virRandom;
|
virRandom;
|
||||||
virRandomBits;
|
virRandomBits;
|
||||||
|
virRandomBytes;
|
||||||
virRandomGenerateWWN;
|
virRandomGenerateWWN;
|
||||||
virRandomInt;
|
virRandomInt;
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2012-2015 Red Hat, Inc.
|
* Copyright (C) 2012-2016 Red Hat, Inc.
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
@ -26,12 +26,16 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include "virrandom.h"
|
#include "virrandom.h"
|
||||||
#include "virthread.h"
|
#include "virthread.h"
|
||||||
#include "count-one-bits.h"
|
#include "count-one-bits.h"
|
||||||
#include "virutil.h"
|
#include "virutil.h"
|
||||||
#include "virerror.h"
|
#include "virerror.h"
|
||||||
|
#include "virfile.h"
|
||||||
#include "virlog.h"
|
#include "virlog.h"
|
||||||
#include "virstring.h"
|
#include "virstring.h"
|
||||||
|
|
||||||
@ -156,11 +160,49 @@ uint32_t virRandomInt(uint32_t max)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* virRandomBytes
|
||||||
|
* @buf: Pointer to location to store bytes
|
||||||
|
* @buflen: Number of bytes to store
|
||||||
|
*
|
||||||
|
* Generate a stream of random bytes from /dev/urandom
|
||||||
|
* into @buf of size @buflen
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
virRandomBytes(unsigned char *buf,
|
||||||
|
size_t buflen)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
|
||||||
|
return errno;
|
||||||
|
|
||||||
|
while (buflen > 0) {
|
||||||
|
ssize_t n;
|
||||||
|
|
||||||
|
if ((n = read(fd, buf, buflen)) <= 0) {
|
||||||
|
if (errno == EINTR)
|
||||||
|
continue;
|
||||||
|
VIR_FORCE_CLOSE(fd);
|
||||||
|
return n < 0 ? errno : ENODATA;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf += n;
|
||||||
|
buflen -= n;
|
||||||
|
}
|
||||||
|
|
||||||
|
VIR_FORCE_CLOSE(fd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#define QUMRANET_OUI "001a4a"
|
#define QUMRANET_OUI "001a4a"
|
||||||
#define VMWARE_OUI "000569"
|
#define VMWARE_OUI "000569"
|
||||||
#define MICROSOFT_OUI "0050f2"
|
#define MICROSOFT_OUI "0050f2"
|
||||||
#define XEN_OUI "00163e"
|
#define XEN_OUI "00163e"
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
virRandomGenerateWWN(char **wwn,
|
virRandomGenerateWWN(char **wwn,
|
||||||
const char *virt_type)
|
const char *virt_type)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2012 Red Hat, Inc.
|
* Copyright (C) 2012, 2016 Red Hat, Inc.
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
@ -27,6 +27,8 @@
|
|||||||
uint64_t virRandomBits(int nbits);
|
uint64_t virRandomBits(int nbits);
|
||||||
double virRandom(void);
|
double virRandom(void);
|
||||||
uint32_t virRandomInt(uint32_t max);
|
uint32_t virRandomInt(uint32_t max);
|
||||||
|
int virRandomBytes(unsigned char *buf, size_t buflen)
|
||||||
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
||||||
int virRandomGenerateWWN(char **wwn, const char *virt_type);
|
int virRandomGenerateWWN(char **wwn, const char *virt_type);
|
||||||
|
|
||||||
#endif /* __VIR_RANDOM_H__ */
|
#endif /* __VIR_RANDOM_H__ */
|
||||||
|
@ -52,34 +52,6 @@ VIR_LOG_INIT("util.uuid");
|
|||||||
|
|
||||||
static unsigned char host_uuid[VIR_UUID_BUFLEN];
|
static unsigned char host_uuid[VIR_UUID_BUFLEN];
|
||||||
|
|
||||||
static int
|
|
||||||
virUUIDGenerateRandomBytes(unsigned char *buf,
|
|
||||||
int buflen)
|
|
||||||
{
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
|
|
||||||
return errno;
|
|
||||||
|
|
||||||
while (buflen > 0) {
|
|
||||||
int n;
|
|
||||||
|
|
||||||
if ((n = read(fd, buf, buflen)) <= 0) {
|
|
||||||
if (errno == EINTR)
|
|
||||||
continue;
|
|
||||||
VIR_FORCE_CLOSE(fd);
|
|
||||||
return n < 0 ? errno : ENODATA;
|
|
||||||
}
|
|
||||||
|
|
||||||
buf += n;
|
|
||||||
buflen -= n;
|
|
||||||
}
|
|
||||||
|
|
||||||
VIR_FORCE_CLOSE(fd);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
virUUIDGeneratePseudoRandomBytes(unsigned char *buf,
|
virUUIDGeneratePseudoRandomBytes(unsigned char *buf,
|
||||||
int buflen)
|
int buflen)
|
||||||
@ -108,7 +80,7 @@ virUUIDGenerate(unsigned char *uuid)
|
|||||||
if (uuid == NULL)
|
if (uuid == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if ((err = virUUIDGenerateRandomBytes(uuid, VIR_UUID_BUFLEN))) {
|
if ((err = virRandomBytes(uuid, VIR_UUID_BUFLEN))) {
|
||||||
char ebuf[1024];
|
char ebuf[1024];
|
||||||
VIR_WARN("Falling back to pseudorandom UUID,"
|
VIR_WARN("Falling back to pseudorandom UUID,"
|
||||||
" failed to generate random bytes: %s",
|
" failed to generate random bytes: %s",
|
||||||
|
Loading…
Reference in New Issue
Block a user