#include "config.h"
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <stdio.h>
#include <l_stdlib.h>
#include <ntp_random.h>
#include "safecast.h"
#ifdef USE_OPENSSL_CRYPTO_RAND
#include <openssl/err.h>
#include <openssl/rand.h>
int crypto_rand_init = 0;
#else
# ifndef HAVE_ARC4RANDOM_BUF
static void
arc4random_buf(void *buf, size_t nbytes);
void
evutil_secure_rng_get_bytes(void *buf, size_t nbytes);
static void
arc4random_buf(void *buf, size_t nbytes)
{
evutil_secure_rng_get_bytes(buf, nbytes);
return;
}
# endif
#endif
void
ntp_crypto_srandom(
void
)
{
#ifdef USE_OPENSSL_CRYPTO_RAND
if (!crypto_rand_init) {
RAND_poll();
crypto_rand_init = 1;
}
#else
#endif
}
int
ntp_crypto_random_buf(
void *buf,
size_t nbytes
)
{
#ifdef USE_OPENSSL_CRYPTO_RAND
int rc;
rc = RAND_bytes(buf, size2int_chk(nbytes));
if (1 != rc) {
unsigned long err;
char *err_str;
err = ERR_get_error();
err_str = ERR_error_string(err, NULL);
(void)&err_str;
return -1;
}
return 0;
#else
arc4random_buf(buf, nbytes);
return 0;
#endif
}