From e37d9f8fb1e235510f2416e26a2a739bb0daf8ef Mon Sep 17 00:00:00 2001 From: Jan Huwald Date: Mon, 21 May 2012 13:05:32 +0200 Subject: hbbp_keygen: add own implementation of randombytes Nacl requires an implementation of randombytes. The version offered by nacl has a strange behaviour (e.g. indefinite blocking in case /dev/urandom is unavailable) and is hard to portably link (.o file in some lib directory). It is thus replaced with an own implementation (that does proper error handling by the way). diff --git a/hbbp_keygen.c b/hbbp_keygen.c index 6bcde5c..f464441 100644 --- a/hbbp_keygen.c +++ b/hbbp_keygen.c @@ -2,9 +2,9 @@ #include #include +#include "common.h" #include "crypto.h" - #define pub_len crypto_box_PUBLICKEYBYTES #define priv_len crypto_box_SECRETKEYBYTES @@ -16,6 +16,27 @@ void error_cleanup(int num) { exit(1); } +// used by nacl's crypto_box_keypair +void randombytes(byte *buf, unsigned long long len) + __attribute__ ((externally_visible)); + +void randombytes(byte *buf, unsigned long long len) { + int fd = open("/dev/urandom", O_RDONLY); + if (fd == -1) goto error; + + while (len > 0) { + int sz = read(fd, buf, len); + if (sz < 1) goto error; + buf += sz; + len -= sz; + } + return; + + error: + perror("failed accessing /dev/urandom"); + error_cleanup(4); +} + int main() { int fd[4], i; byte key_pub [2][pub_len], -- cgit v0.10.1