virCryptoGenerateRandom(unsigned char *buf,
size_t buflen)
{
-#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
- /* If we don't have gnutls_rnd(), we will generate a less cryptographically
- * strong master buf from /dev/urandom.
- */
- if (virRandomBytes(buf, buflen) < 0)
- return -1;
-#endif
-
- return 0;
+ return virRandomBytes(buf, buflen);
}
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
+#ifdef WITH_GNUTLS
+# include <gnutls/gnutls.h>
+# include <gnutls/crypto.h>
+#endif
#include "virrandom.h"
#include "virthread.h"
virRandomBytes(unsigned char *buf,
size_t buflen)
{
+#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 */
+
int fd;
if ((fd = open(RANDOM_SOURCE, O_RDONLY)) < 0) {
}
VIR_FORCE_CLOSE(fd);
+#endif /* !WITH_GNUTLS */
return 0;
}