| 1 |
#ifndef _WIN32 |
| 2 |
#include <sys/types.h> |
| 3 |
#include <sys/stat.h> |
| 4 |
#include <fcntl.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
static int ed25519_random_fd = -1; |
| 10 |
|
| 11 |
static inline void ed25519_randombytes(unsigned char *x,unsigned long long xlen) |
| 12 |
{ |
| 13 |
int i; |
| 14 |
|
| 15 |
if (ed25519_random_fd == -1) { |
| 16 |
for (;;) { |
| 17 |
ed25519_random_fd = open("/dev/urandom",O_RDONLY); |
| 18 |
if (ed25519_random_fd != -1) break; |
| 19 |
sleep(1); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
while (xlen > 0) { |
| 24 |
if (xlen < 1048576) i = xlen; else i = 1048576; |
| 25 |
|
| 26 |
i = read(ed25519_random_fd,x,i); |
| 27 |
if (i < 1) { |
| 28 |
sleep(1); |
| 29 |
continue; |
| 30 |
} |
| 31 |
|
| 32 |
x += i; |
| 33 |
xlen -= i; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
#else |
| 38 |
#include <windows.h> |
| 39 |
#include <wincrypt.h> |
| 40 |
|
| 41 |
static inline void ed25519_randombytes(unsigned char *x,unsigned long long xlen) |
| 42 |
{ |
| 43 |
HCRYPTPROV prov = 0; |
| 44 |
|
| 45 |
CryptAcquireContextW(&prov, NULL, NULL, |
| 46 |
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT); |
| 47 |
|
| 48 |
CryptGenRandom(prov, xlen, x); |
| 49 |
CryptReleaseContext(prov, 0); |
| 50 |
} |
| 51 |
|
| 52 |
#endif |