From 6bd0f2e9a131c6db2cdf7d1480162f087468e23c Mon Sep 17 00:00:00 2001 From: Jan Huwald Date: Mon, 21 May 2012 09:35:14 +0200 Subject: add key generator hbbp_keygen generates all four keys private, public for sender and receiver with the file names used in hbbp[cd] in the current working directory. diff --git a/Makefile b/Makefile index 87a65e8..0149285 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ -BIN=hbbpd hbbpc - +ifndef BIN +BIN=hbbpd hbbpc hbbp_keygen +endif + ifndef EXTERNAL_NACL CFLAGS += -Inacl/include/ LDFLAGS += -Lnacl/lib/ diff --git a/hbbp_keygen.c b/hbbp_keygen.c new file mode 100644 index 0000000..6bcde5c --- /dev/null +++ b/hbbp_keygen.c @@ -0,0 +1,48 @@ +#include +#include +#include + +#include "crypto.h" + + +#define pub_len crypto_box_PUBLICKEYBYTES +#define priv_len crypto_box_SECRETKEYBYTES + +const char *name[4] = {"recv.pub", "recv.priv", "send.pub", "send.priv"}; + +void error_cleanup(int num) { + for (int i = 0; i < num; i++) + unlink(name[i]); + exit(1); +} + +int main() { + int fd[4], i; + byte key_pub [2][pub_len], + key_priv[2][priv_len]; + + /* open files */ + for (i=0; i<4; i++) { + if ((fd[i] = open(name[i], O_WRONLY|O_CREAT|O_EXCL, + (i%2) ? 0400 : 0444)) == -1) { + fprintf(stderr, + (errno == EEXIST) + ? "%s already exists\n" : "could not create file %s\n", + name[i]); + error_cleanup(i); + } + } + + /* generate keys */ + for (i=0; i<2; i++) { + crypto_box_keypair(key_pub[i], key_priv[i]); + if ((write(fd[2*i ], key_pub[i], pub_len) != pub_len) + || (write(fd[2*i+1], key_priv[i], priv_len) != priv_len) + || (close(fd[2*i ]) == -1) + || (close(fd[2*i+1]) == -1)) { + error_cleanup(4); + } + } + + return 0; +} -- cgit v0.10.1