Commit dcf9aafa authored by Davide Galassi's avatar Davide Galassi

Removed the system dependent Posix and Windows PRNG

We prefer a system agnostic library!
parent a8edf6bc
Pipeline #52088228 passed with stages
in 1 minute and 22 seconds
#include <cry/prng.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
static int urand_fd = -1;
int cry_prng_init(const unsigned char *seed, size_t seed_siz)
{
urand_fd = open("/dev/urandom", O_RDONLY);
if (urand_fd < 0)
return -1;
return 0;
}
int cry_prng_rand(unsigned char *buf, size_t siz)
{
int n;
if (urand_fd == -1 && cry_prng_init(NULL, 0) != 0)
return -1;
while (siz) {
if ((n = read(urand_fd, buf, siz)) < 0) {
if (errno == EINTR)
continue;
return -1;
}
buf += n;
siz -= n;
}
return 0;
}
#include <windows.h>
#include <wincrypt.h>
static HCRYPTPROV hProvider = 0;
int cry_prng_init(const unsigned char *seed, size_t seed_siz)
{
if (!CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT)) {
return -1;
}
return 0;
}
int cry_prng_rand(unsigned char *buf, size_t siz)
{
if (!hProvider && cry_prng_init(NULL, 0) != 0)
return -1;
if (!CryptGenRandom(hProvider, siz, buf))
return -1;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment