X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fcrypto.c;h=b5309b0fee4d61092abf10f0fef2dcd2b471c288;hb=516bfe0557afe6adaa3ea73494b5fe1cf5587b64;hp=2e2fa5386555c02b1e8c5632ee597c6c52bd513d;hpb=7593adb9ad9e6ff73afe29e9bd445cea8615a151;p=meshlink diff --git a/src/crypto.c b/src/crypto.c index 2e2fa538..b5309b0f 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -29,8 +29,11 @@ static int random_fd = -1; void crypto_init(void) { random_fd = open("/dev/urandom", O_RDONLY); - if(random_fd < 0) + + if(random_fd < 0) { random_fd = open("/dev/random", O_RDONLY); + } + if(random_fd < 0) { fprintf(stderr, "Could not open source of random numbers: %s\n", strerror(errno)); abort(); @@ -41,6 +44,24 @@ void crypto_exit(void) { close(random_fd); } +void randomize(void *out, size_t outlen) { + while(outlen) { + size_t len = read(random_fd, out, outlen); + + if(len <= 0) { + if(errno == EAGAIN || errno == EINTR) { + continue; + } + + fprintf(stderr, "Could not read random numbers: %s\n", strerror(errno)); + abort(); + } + + out += len; + outlen -= len; + } +} + #else #include