summaryrefslogtreecommitdiff
path: root/ecp/util
diff options
context:
space:
mode:
Diffstat (limited to 'ecp/util')
-rw-r--r--ecp/util/Makefile22
-rwxr-xr-xecp/util/keydump3
-rw-r--r--ecp/util/mknode.c64
-rw-r--r--ecp/util/util.c92
-rw-r--r--ecp/util/util.h5
5 files changed, 186 insertions, 0 deletions
diff --git a/ecp/util/Makefile b/ecp/util/Makefile
new file mode 100644
index 0000000..b3cbc7c
--- /dev/null
+++ b/ecp/util/Makefile
@@ -0,0 +1,22 @@
+include ../src/Makefile.posix
+CFLAGS=$(CFLAGS_PL) -I../src
+LDFLAGS=$(LDFLAGS_PL)
+
+obj=util.o
+dep=../src/build-posix/*.a ./libecputil.a
+
+
+%.o: %.c %.h
+ $(CC) $(CFLAGS) -c $<
+
+all: libecputil.a mknode
+
+libecputil.a: $(obj)
+ $(AR) rcs libecputil.a $(obj)
+
+mknode: mknode.o libecputil.a
+ $(CC) -o $@ $< $(dep) $(LDFLAGS)
+
+clean:
+ rm -f *.o *.a
+ rm -f mknode
diff --git a/ecp/util/keydump b/ecp/util/keydump
new file mode 100755
index 0000000..9a884dc
--- /dev/null
+++ b/ecp/util/keydump
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+hexdump -e '16/1 "0x%02X, " "\n"' $@
diff --git a/ecp/util/mknode.c b/ecp/util/mknode.c
new file mode 100644
index 0000000..a601a45
--- /dev/null
+++ b/ecp/util/mknode.c
@@ -0,0 +1,64 @@
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "core.h"
+#include "util.h"
+
+#define FN_LEN 256
+
+static char fn_key[FN_LEN];
+static char fn_node[FN_LEN];
+
+static int v_rng(void *buf, size_t bufsize) {
+ int fd;
+
+ if((fd = open("/dev/urandom", O_RDONLY)) < 0) return -1;
+ size_t nb = read(fd, buf, bufsize);
+ close(fd);
+ if (nb != bufsize) return -1;
+ return 0;
+}
+
+static void usage(char *arg) {
+ fprintf(stderr, "Usage: %s <name> [address]\n", arg);
+ exit(1);
+}
+
+int main(int argc, char *argv[]) {
+ int rv;
+ ECPContext ctx;
+ ECPDHKey key;
+ ECPNode node;
+
+ if ((argc < 2) || (argc > 3)) usage(argv[0]);
+
+ if (strlen(argv[1]) > FN_LEN - 6) usage(argv[0]);
+ strcpy(fn_node, argv[1]);
+ strcpy(fn_key, argv[1]);
+ strcat(fn_key, ".priv");
+ strcat(fn_node, ".pub");
+
+ rv = ecp_ctx_create(&ctx);
+ if (rv) goto err;
+ ctx.rng = v_rng;
+
+ rv = ecp_dhkey_generate(&ctx, &key);
+ if (rv) goto err;
+
+ rv = ecp_node_init(&node, &key.public, (argc == 3) ? argv[2] : NULL);
+ if (rv) goto err;
+
+ rv = ecp_util_key_save(&ctx, &key, fn_key);
+ if (rv) goto err;
+
+ rv = ecp_util_node_save(&ctx, &node, fn_node);
+ if (rv) goto err;
+
+ return 0;
+ err:
+ printf("ERR:%d\n", rv);
+ return 1;
+} \ No newline at end of file
diff --git a/ecp/util/util.c b/ecp/util/util.c
new file mode 100644
index 0000000..4f4adc2
--- /dev/null
+++ b/ecp/util/util.c
@@ -0,0 +1,92 @@
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "core.h"
+#include "cr.h"
+#include "util.h"
+
+int ecp_util_key_save(ECPContext *ctx, ECPDHKey *key, char *filename) {
+ int fd;
+ ssize_t rv;
+
+ if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR;
+ rv = write(fd, ecp_cr_dh_pub_get_buf(&key->public), ECP_ECDH_SIZE_KEY);
+ if (rv != ECP_ECDH_SIZE_KEY) {
+ close(fd);
+ return ECP_ERR;
+ }
+ rv = write(fd, &key->private, sizeof(key->private));
+ if (rv != sizeof(key->private)) {
+ close(fd);
+ return ECP_ERR;
+ }
+ close(fd);
+ return ECP_OK;
+}
+
+int ecp_util_key_load(ECPContext *ctx, ECPDHKey *key, char *filename) {
+ int fd;
+ ssize_t rv;
+ unsigned char buf[ECP_ECDH_SIZE_KEY];
+
+ if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR;
+ rv = read(fd, buf, ECP_ECDH_SIZE_KEY);
+ if (rv != ECP_ECDH_SIZE_KEY) {
+ close(fd);
+ return ECP_ERR;
+ }
+ rv = read(fd, &key->private, sizeof(key->private));
+ if (rv != sizeof(key->private)) {
+ close(fd);
+ return ECP_ERR;
+ }
+ close(fd);
+
+ ecp_cr_dh_pub_from_buf(&key->public, buf);
+
+ key->valid = 1;
+ return ECP_OK;
+}
+
+int ecp_util_node_save(ECPContext *ctx, ECPNode *node, char *filename) {
+ int fd;
+ ssize_t rv;
+
+ if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR;
+ rv = write(fd, ecp_cr_dh_pub_get_buf(&node->public), ECP_ECDH_SIZE_KEY);
+ if (rv != ECP_ECDH_SIZE_KEY) {
+ close(fd);
+ return ECP_ERR;
+ }
+ rv = write(fd, &node->addr, sizeof(node->addr));
+ if (rv != sizeof(node->addr)) {
+ close(fd);
+ return ECP_ERR;
+ }
+ close(fd);
+ return ECP_OK;
+}
+
+int ecp_util_node_load(ECPContext *ctx, ECPNode *node, char *filename) {
+ int fd;
+ ssize_t rv;
+ unsigned char buf[ECP_ECDH_SIZE_KEY];
+
+ if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR;
+ rv = read(fd, buf, ECP_ECDH_SIZE_KEY);
+ if (rv != ECP_ECDH_SIZE_KEY) {
+ close(fd);
+ return ECP_ERR;
+ }
+ rv = read(fd, &node->addr, sizeof(node->addr));
+ if (rv != sizeof(node->addr)) {
+ close(fd);
+ return ECP_ERR;
+ }
+ close(fd);
+
+ ecp_cr_dh_pub_from_buf(&node->public, buf);
+
+ return ECP_OK;
+} \ No newline at end of file
diff --git a/ecp/util/util.h b/ecp/util/util.h
new file mode 100644
index 0000000..3d07588
--- /dev/null
+++ b/ecp/util/util.h
@@ -0,0 +1,5 @@
+int ecp_util_key_save(ECPContext *ctx, ECPDHKey *key, char *filename);
+int ecp_util_key_load(ECPContext *ctx, ECPDHKey *key, char *filename);
+
+int ecp_util_node_save(ECPContext *ctx, ECPNode *node, char *filename);
+int ecp_util_node_load(ECPContext *ctx, ECPNode *node, char *filename); \ No newline at end of file