fixed linux portability, use libsodium random*() stuff

This commit is contained in:
TLINDEN
2013-11-16 14:30:32 +01:00
parent f84d75d500
commit 4ac23de3c7

View File

@@ -57,39 +57,16 @@
#ifndef HAVE_ARC4RANDOM_BUF #ifndef HAVE_ARC4RANDOM_BUF
// shitty OS. we've got to use other stuff // shitty OS. we're using libsodium's implementation
#include <stdlib.h> #include <sodium.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
static inline FILE *__getranddev() {
FILE *R;
if((R = fopen("/dev/urandom", "rb")) == NULL) {
// not even this is here! what a shame
if((R = fopen("/dev/random", "rb")) == NULL) {
// not available or depleted. that's too bad
fprintf(stderr, "ERROR: /dev/urandom not available, /dev/random is depleted.\n");
fprintf(stderr, "That's horrible for you but a nightmare for me. I die. Bye.\n");
exit(2);
}
}
return R;
}
static inline u_int32_t arc4random() { static inline u_int32_t arc4random() {
uint32_t x; return randombytes_random();
FILE *R = __getranddev();
fread(&x, sizeof(uint32_t), 1, R);
fclose(R);
return x;
} }
static inline void arc4random_buf(void *buf, size_t nbytes) { static inline void arc4random_buf(void *buf, size_t nbytes) {
FILE *R = __getranddev(); randombytes(buf, nbytes);
fread(buf, nbytes, 1, R);
fclose(R);
} }