summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Huwald <jh@sotun.de>2012-05-21 11:05:32 (GMT)
committerJan Huwald <jh@sotun.de>2012-05-21 11:05:32 (GMT)
commite37d9f8fb1e235510f2416e26a2a739bb0daf8ef (patch)
treef8fc6773592fca518d1a18cc5c027811b5c0b13e
parent94ff4ad0f6b71c662f48c88308046f267dd74907 (diff)
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).
-rw-r--r--hbbp_keygen.c23
1 files changed, 22 insertions, 1 deletions
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 <sys/stat.h>
#include <fcntl.h>
+#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],
contact: Jan Huwald // Impressum