From ab0325ae7906230f1ea82f08b27c72b075e9a13d Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Tue, 23 May 2017 17:28:12 +0200 Subject: build fixed; added lib util --- code/build.sh | 8 +++ code/core/Makefile | 4 +- code/core/core.c | 10 +++- code/core/core.h | 2 +- code/core/crypto/Makefile | 2 + code/core/crypto/chacha/Makefile | 2 + code/core/crypto/compat/Makefile | 2 + code/core/crypto/curve25519/Makefile | 2 + code/core/crypto/poly1305/Makefile | 2 + code/core/crypto/sha/Makefile | 2 + code/core/crypto/test/Makefile | 4 +- code/core/htable/Makefile | 2 + code/core/posix/Makefile | 2 + code/proxy/Makefile | 13 ++++ code/proxy/proxy.c | 21 ++++++- code/proxy/proxy.h | 4 +- code/test/Makefile | 15 ++--- code/test/basic.c | 113 +++++++++++++++++++++++++++++++++++ code/test/server.c | 113 ----------------------------------- code/test/stress.c | 2 +- code/util/Makefile | 21 +++++++ code/util/mknode.c | 63 +++++++++++++++++++ code/util/util.c | 91 ++++++++++++++++++++++++++++ code/util/util.h | 5 ++ 24 files changed, 375 insertions(+), 130 deletions(-) create mode 100755 code/build.sh create mode 100644 code/proxy/Makefile create mode 100644 code/test/basic.c delete mode 100644 code/test/server.c create mode 100644 code/util/Makefile create mode 100644 code/util/mknode.c create mode 100644 code/util/util.c create mode 100644 code/util/util.h (limited to 'code') diff --git a/code/build.sh b/code/build.sh new file mode 100755 index 0000000..96f26e3 --- /dev/null +++ b/code/build.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +subdirs="core proxy util test" + +for i in $subdirs; do + (cd $i && make $1 && cd ..) || exit; +done + diff --git a/code/core/Makefile b/code/core/Makefile index d3610bf..2cae048 100644 --- a/code/core/Makefile +++ b/code/core/Makefile @@ -1,8 +1,10 @@ MAKE=make -CFLAGS = -I. -pthread -O3 -DECP_DEBUG +CFLAGS = -I. -pthread -O3 $(PIC) -DECP_DEBUG + obj = core.o timer.o msgq.o subdirs = crypto posix htable + %.o: %.c %.h $(CC) $(CFLAGS) -c $< diff --git a/code/core/core.c b/code/core/core.c index ed083a0..93495d0 100644 --- a/code/core/core.c +++ b/code/core/core.c @@ -14,11 +14,15 @@ int ecp_dhkey_generate(ECPContext *ctx, ECPDHKey *key) { return ECP_OK; } -int ecp_node_init(ECPContext *ctx, ECPNode *node, void *addr, ecp_dh_public_t *public) { - int rv = ctx->tr.addr_set(&node->addr, addr); +int ecp_node_init(ECPContext *ctx, ECPNode *node, ecp_dh_public_t *public, void *addr) { + int rv = ECP_OK; + + memset(node, 0, sizeof(ECPNode)); + memcpy(&node->public, public, sizeof(node->public)); + + if (addr) rv = ctx->tr.addr_set(&node->addr, addr); if (rv) return ECP_ERR_NET_ADDR; - memcpy(&node->public, public, sizeof(node->public)); return ECP_OK; } diff --git a/code/core/core.h b/code/core/core.h index 33dd6d2..4c2b74c 100644 --- a/code/core/core.h +++ b/code/core/core.h @@ -263,7 +263,7 @@ int ecp_transport_init(ECPTransportIface *t); int ecp_time_init(ECPTimeIface *t); int ecp_dhkey_generate(ECPContext *ctx, ECPDHKey *key); -int ecp_node_init(ECPContext *ctx, ECPNode *node, void *addr, ecp_dh_public_t *public); +int ecp_node_init(ECPContext *ctx, ECPNode *node, ecp_dh_public_t *public, void *addr); int ecp_ctx_create(ECPContext *ctx); int ecp_ctx_destroy(ECPContext *ctx); diff --git a/code/core/crypto/Makefile b/code/core/crypto/Makefile index cfd3bf8..2fabea2 100644 --- a/code/core/crypto/Makefile +++ b/code/core/crypto/Makefile @@ -1,11 +1,13 @@ MAKE=make CFLAGS=-Iinclude -I.. -D__BEGIN_HIDDEN_DECLS= -D__END_HIDDEN_DECLS= -O3 $(PIC) + obj = e_chacha20poly1305.o crypto.o obj_dep = compat/explicit_bzero.o compat/timingsafe_memcmp.o compat/timingsafe_bcmp.o \ chacha/chacha.o poly1305/poly1305.o curve25519/curve25519.o curve25519/curve25519-generic.o \ sha/sha256.o sha/sha512.o subdirs = compat curve25519 chacha poly1305 sha + %.o: %.c $(CC) $(CFLAGS) -c $< diff --git a/code/core/crypto/chacha/Makefile b/code/core/crypto/chacha/Makefile index dae3373..071a1b1 100644 --- a/code/core/crypto/chacha/Makefile +++ b/code/core/crypto/chacha/Makefile @@ -1,6 +1,8 @@ CFLAGS=-I../include -D__BEGIN_HIDDEN_DECLS= -D__END_HIDDEN_DECLS= -O3 $(PIC) + obj = chacha.o + all: libchacha.a dep: all diff --git a/code/core/crypto/compat/Makefile b/code/core/crypto/compat/Makefile index 2c0fa5c..a0dec2d 100644 --- a/code/core/crypto/compat/Makefile +++ b/code/core/crypto/compat/Makefile @@ -1,7 +1,9 @@ CFLAGS=-I../include -D__BEGIN_HIDDEN_DECLS= -D__END_HIDDEN_DECLS= -O3 $(PIC) + getentropy = getentropy_osx obj = explicit_bzero.o timingsafe_memcmp.o timingsafe_bcmp.o # arc4random.o arc4random_uniform.o $(getentropy).o + all: libcompat.a dep: all diff --git a/code/core/crypto/curve25519/Makefile b/code/core/crypto/curve25519/Makefile index 470d31e..1a96045 100644 --- a/code/core/crypto/curve25519/Makefile +++ b/code/core/crypto/curve25519/Makefile @@ -1,6 +1,8 @@ CFLAGS=-I../include -D__BEGIN_HIDDEN_DECLS= -D__END_HIDDEN_DECLS= -DED25519 -O3 $(PIC) + obj = curve25519.o curve25519-generic.o + all: libcurve25519.a dep: all diff --git a/code/core/crypto/poly1305/Makefile b/code/core/crypto/poly1305/Makefile index 6ddec33..e780491 100644 --- a/code/core/crypto/poly1305/Makefile +++ b/code/core/crypto/poly1305/Makefile @@ -1,6 +1,8 @@ CFLAGS=-I../include -D__BEGIN_HIDDEN_DECLS= -D__END_HIDDEN_DECLS= -O3 $(PIC) + obj = poly1305.o + all: libpoly1305.a dep: all diff --git a/code/core/crypto/sha/Makefile b/code/core/crypto/sha/Makefile index 8a50b28..f078644 100644 --- a/code/core/crypto/sha/Makefile +++ b/code/core/crypto/sha/Makefile @@ -1,6 +1,8 @@ CFLAGS=-I../include -D__BEGIN_HIDDEN_DECLS= -D__END_HIDDEN_DECLS= -O3 $(PIC) + obj = sha1dgst.o sha1_one.o sha256.o sha512.o + all: libsha.a dep: all diff --git a/code/core/crypto/test/Makefile b/code/core/crypto/test/Makefile index b6a92f2..f274209 100644 --- a/code/core/crypto/test/Makefile +++ b/code/core/crypto/test/Makefile @@ -1,9 +1,11 @@ -CFLAGS=-I.. -I../include -O3 +CFLAGS=-I.. -I../include -O3 + aead_dep=../compat/explicit_bzero.o ../compat/timingsafe_memcmp.o ../compat/timingsafe_bcmp.o \ ../chacha/chacha.o ../poly1305/poly1305.o ../curve25519/curve25519.o ../curve25519/curve25519-generic.o \ ../sha/sha512.o ../e_chacha20poly1305.o dsa_dep=../*/*.a + %.o: %.c $(CC) $(CFLAGS) -c $< diff --git a/code/core/htable/Makefile b/code/core/htable/Makefile index af329b4..af8d7c1 100644 --- a/code/core/htable/Makefile +++ b/code/core/htable/Makefile @@ -1,6 +1,8 @@ CFLAGS=-I.. -std=gnu89 $(PIC) + obj = htable.o hashtable.o hashtable_itr.o + %.o: %.c $(CC) $(CFLAGS) -c $< diff --git a/code/core/posix/Makefile b/code/core/posix/Makefile index 64cf5f6..00c8048 100644 --- a/code/core/posix/Makefile +++ b/code/core/posix/Makefile @@ -1,7 +1,9 @@ CFLAGS=-I.. $(PIC) + obj_tr = transport.o obj_tm = time.o + %.o: %.c $(CC) $(CFLAGS) -c $< diff --git a/code/proxy/Makefile b/code/proxy/Makefile new file mode 100644 index 0000000..9a8490c --- /dev/null +++ b/code/proxy/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -I../core -pthread -O3 $(PIC) + +obj = proxy.o + + +%.o: %.c %.h + $(CC) $(CFLAGS) -c $< + +all: $(obj) + $(AR) rcs libecpproxy.a $(obj) + +clean: + rm -f *.o *.a \ No newline at end of file diff --git a/code/proxy/proxy.c b/code/proxy/proxy.c index ae66318..3d6ffec 100644 --- a/code/proxy/proxy.c +++ b/code/proxy/proxy.c @@ -1,4 +1,4 @@ -#include "core.h" +#include #include "proxy.h" #ifdef ECP_WITH_PTHREAD @@ -409,7 +409,8 @@ int ecp_proxy_init(ECPContext *ctx) { return ECP_OK; } -int ecp_conn_proxy_init(ECPConnection *conn, ECPNode *conn_node, ECPConnProxy *proxy[], ECPNode *proxy_node[], int size, ECPSocket *sock) { +int ecp_conn_proxy_init(ECPConnection *conn, ECPNode *conn_node, ECPConnProxy *proxy[], ECPNode *proxy_node[], int size) { + ECPSocket *sock = conn->sock; int i, rv; rv = ecp_conn_init(conn, conn_node); @@ -438,3 +439,19 @@ int ecp_conn_proxy_init(ECPConnection *conn, ECPNode *conn_node, ECPConnProxy *p return ECP_OK; } +static ssize_t _proxy_send_kget(ECPConnection *conn, ECPTimerItem *ti) { + unsigned char payload[ECP_SIZE_PLD(0)]; + + ecp_pld_set_type(payload, ECP_MTYPE_KGET); + return ecp_pld_send_wkey(conn, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, payload, sizeof(payload)); +} + +int ecp_conn_proxy_open(ECPConnection *conn, ECPNode *conn_node, ECPConnProxy *proxy[], ECPNode *proxy_node[], int size) { + int rv = ecp_conn_proxy_init(conn, conn_node, proxy, proxy_node, size); + if (rv) return rv; + + ssize_t _rv = ecp_timer_send((ECPConnection *)proxy[0], _proxy_send_kget, ECP_MTYPE_KGET, 3, 500); + if (_rv < 0) return _rv; + + return ECP_OK; +} \ No newline at end of file diff --git a/code/proxy/proxy.h b/code/proxy/proxy.h index c6d5422..f3bc58a 100644 --- a/code/proxy/proxy.h +++ b/code/proxy/proxy.h @@ -16,4 +16,6 @@ typedef struct ECPConnProxyF { } ECPConnProxyF; int ecp_proxy_init(ECPContext *ctx); -int ecp_conn_proxy_init(ECPConnection *conn, ECPNode *conn_node, ECPConnProxy *proxy[], ECPNode *proxy_node[], int size, ECPSocket *sock); + +int ecp_conn_proxy_init(ECPConnection *conn, ECPNode *conn_node, ECPConnProxy *proxy[], ECPNode *proxy_node[], int size); +int ecp_conn_proxy_open(ECPConnection *conn, ECPNode *conn_node, ECPConnProxy *proxy[], ECPNode *proxy_node[], int size); diff --git a/code/test/Makefile b/code/test/Makefile index 5c25b5e..ddfc095 100644 --- a/code/test/Makefile +++ b/code/test/Makefile @@ -1,18 +1,19 @@ CFLAGS=-I../core -O3 -Wno-int-to-void-pointer-cast LDFLAGS=-lm -pthread -dep=../core/libecpcore.a ../core/crypto/libecpcr.a ../core/htable/libecpht.a ../core/posix/libecptr.a ../core/posix/libecptm.a init.o + +dep=../core/libecpcore.a ../core/crypto/libecpcr.a ../core/htable/libecpht.a ../core/posix/libecptr.a ../core/posix/libecptm.a %.o: %.c $(CC) $(CFLAGS) -c $< -all: server stress +all: basic stress -server: server.o $(dep) - $(CC) $< $(dep) $(LDFLAGS) -o $@ +basic: basic.o init.o $(dep) + $(CC) -o $@ $(LDFLAGS) $< init.o $(dep) -stress: stress.o $(dep) - $(CC) $< $(dep) $(LDFLAGS) -o $@ +stress: stress.o init.o $(dep) + $(CC) -o $@ $(LDFLAGS) $< init.o $(dep) clean: rm -f *.o - rm -f server stress + rm -f basic stress diff --git a/code/test/basic.c b/code/test/basic.c new file mode 100644 index 0000000..bcd65bc --- /dev/null +++ b/code/test/basic.c @@ -0,0 +1,113 @@ +#include +#include +#include + +#include + +ECPContext ctx_s; +ECPSocket sock_s; +ECPDHKey key_perma_s; +ECPConnHandler handler_s; + +ECPContext ctx_c; +ECPSocket sock_c; +ECPDHKey key_perma_c; +ECPConnHandler handler_c; + +ECPNode node; +ECPConnection conn; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 8 + +ssize_t handle_open_c(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) { + uint32_t seq = 0; + + ecp_conn_handle_open(conn, t, p, s); + if (s < 0) { + printf("OPEN ERR:%ld\n", s); + return 0; + } + + unsigned char payload[ECP_SIZE_PLD(1000)]; + unsigned char *buf = ecp_pld_get_buf(payload); + char *msg = "PERA JE CAR!"; + + ecp_pld_set_type(payload, MTYPE_MSG); + strcpy((char *)buf, msg); + ssize_t _rv = ecp_send(conn, payload, sizeof(payload)); + return 0; +} + +ssize_t handle_msg_c(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) { + printf("MSG C:%s size:%ld\n", p, s); + return s; +} + +ssize_t handle_msg_s(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) { + printf("MSG S:%s size:%ld\n", p, s); + + unsigned char payload[ECP_SIZE_PLD(1000)]; + unsigned char *buf = ecp_pld_get_buf(payload); + char *msg = "VAISTINU JE CAR!"; + + ecp_pld_set_type(payload, MTYPE_MSG); + strcpy((char *)buf, msg); + ssize_t _rv = ecp_send(conn, payload, sizeof(payload)); + + return s; +} + +int main(int argc, char *argv[]) { + int rv; + + rv = ecp_init(&ctx_s); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_conn_handler_init(&handler_s); + handler_s.msg[MTYPE_MSG] = handle_msg_s; + ctx_s.handler[CTYPE_TEST] = &handler_s; + + rv = ecp_dhkey_generate(&ctx_s, &key_perma_s); + printf("ecp_dhkey_generate RV:%d\n", rv); + + rv = ecp_sock_create(&sock_s, &ctx_s, &key_perma_s); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock_s, "0.0.0.0:3000"); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock_s); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_init(&ctx_c); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_conn_handler_init(&handler_c); + handler_c.msg[ECP_MTYPE_OPEN] = handle_open_c; + handler_c.msg[MTYPE_MSG] = handle_msg_c; + ctx_c.handler[CTYPE_TEST] = &handler_c; + + rv = ecp_dhkey_generate(&ctx_c, &key_perma_c); + printf("ecp_dhkey_generate RV:%d\n", rv); + + rv = ecp_sock_create(&sock_c, &ctx_c, &key_perma_c); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock_c, NULL); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock_c); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_node_init(&ctx_c, &node, &key_perma_s.public, "127.0.0.1:3000"); + printf("ecp_node_init RV:%d\n", rv); + + rv = ecp_conn_create(&conn, &sock_c, CTYPE_TEST); + printf("ecp_conn_create RV:%d\n", rv); + + rv = ecp_conn_open(&conn, &node); + printf("ecp_conn_open RV:%d\n", rv); + + while (1) sleep(1); +} \ No newline at end of file diff --git a/code/test/server.c b/code/test/server.c deleted file mode 100644 index 5bb36ef..0000000 --- a/code/test/server.c +++ /dev/null @@ -1,113 +0,0 @@ -#include -#include -#include - -#include - -ECPContext ctx_s; -ECPSocket sock_s; -ECPDHKey key_perma_s; -ECPConnHandler handler_s; - -ECPContext ctx_c; -ECPSocket sock_c; -ECPDHKey key_perma_c; -ECPConnHandler handler_c; - -ECPNode node; -ECPConnection conn; - -#define CTYPE_TEST 0 -#define MTYPE_MSG 8 - -ssize_t handle_open_c(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) { - uint32_t seq = 0; - - ecp_conn_handle_open(conn, t, p, s); - if (s < 0) { - printf("OPEN ERR:%ld\n", s); - return 0; - } - - unsigned char payload[ECP_SIZE_PLD(1000)]; - unsigned char *buf = ecp_pld_get_buf(payload); - char *msg = "PERA JE CAR!"; - - ecp_pld_set_type(payload, MTYPE_MSG); - strcpy((char *)buf, msg); - ssize_t _rv = ecp_send(conn, payload, sizeof(payload)); - return 0; -} - -ssize_t handle_msg_c(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) { - printf("MSG C:%s size:%ld\n", p, s); - return s; -} - -ssize_t handle_msg_s(ECPConnection *conn, unsigned char t, unsigned char *p, ssize_t s) { - printf("MSG S:%s size:%ld\n", p, s); - - unsigned char payload[ECP_SIZE_PLD(1000)]; - unsigned char *buf = ecp_pld_get_buf(payload); - char *msg = "VAISTINU JE CAR!"; - - ecp_pld_set_type(payload, MTYPE_MSG); - strcpy((char *)buf, msg); - ssize_t _rv = ecp_send(conn, payload, sizeof(payload)); - - return s; -} - -int main(int argc, char *argv[]) { - int rv; - - rv = ecp_init(&ctx_s); - printf("ecp_init RV:%d\n", rv); - - rv = ecp_conn_handler_init(&handler_s); - handler_s.msg[MTYPE_MSG] = handle_msg_s; - ctx_s.handler[CTYPE_TEST] = &handler_s; - - rv = ecp_dhkey_generate(&ctx_s, &key_perma_s); - printf("ecp_dhkey_generate RV:%d\n", rv); - - rv = ecp_sock_create(&sock_s, &ctx_s, &key_perma_s); - printf("ecp_sock_create RV:%d\n", rv); - - rv = ecp_sock_open(&sock_s, "0.0.0.0:3000"); - printf("ecp_sock_open RV:%d\n", rv); - - rv = ecp_start_receiver(&sock_s); - printf("ecp_start_receiver RV:%d\n", rv); - - rv = ecp_init(&ctx_c); - printf("ecp_init RV:%d\n", rv); - - rv = ecp_conn_handler_init(&handler_c); - handler_c.msg[ECP_MTYPE_OPEN] = handle_open_c; - handler_c.msg[MTYPE_MSG] = handle_msg_c; - ctx_c.handler[CTYPE_TEST] = &handler_c; - - rv = ecp_dhkey_generate(&ctx_c, &key_perma_c); - printf("ecp_dhkey_generate RV:%d\n", rv); - - rv = ecp_sock_create(&sock_c, &ctx_c, &key_perma_c); - printf("ecp_sock_create RV:%d\n", rv); - - rv = ecp_sock_open(&sock_c, NULL); - printf("ecp_sock_open RV:%d\n", rv); - - rv = ecp_start_receiver(&sock_c); - printf("ecp_start_receiver RV:%d\n", rv); - - rv = ecp_node_init(&ctx_c, &node, "127.0.0.1:3000", &key_perma_s.public); - printf("ecp_node_init RV:%d\n", rv); - - rv = ecp_conn_create(&conn, &sock_c, CTYPE_TEST); - printf("ecp_conn_create RV:%d\n", rv); - - rv = ecp_conn_open(&conn, &node); - printf("ecp_conn_open RV:%d\n", rv); - - while (1) sleep(1); -} \ No newline at end of file diff --git a/code/test/stress.c b/code/test/stress.c index 808611e..41883c2 100644 --- a/code/test/stress.c +++ b/code/test/stress.c @@ -209,7 +209,7 @@ int main(int argc, char *argv[]) { strcpy(addr, "127.0.0.1:"); sprintf(addr+strlen(addr), "%d", 3000 + (i % num_s)); - if (!rv) rv = ecp_node_init(&ctx_c[i], &node[i], addr, &key_perma_s.public); + if (!rv) rv = ecp_node_init(&ctx_c[i], &node[i], &key_perma_s.public, addr); if (!rv) rv = ecp_conn_create(&conn[i], &sock_c[i], CTYPE_TEST); conn[i].conn_data = (void *)i; diff --git a/code/util/Makefile b/code/util/Makefile new file mode 100644 index 0000000..c87a1dd --- /dev/null +++ b/code/util/Makefile @@ -0,0 +1,21 @@ +CFLAGS = -I../core -pthread -O3 $(PIC) +LDFLAGS=-lm -pthread + +obj=util.o +dep=../core/libecpcore.a ../core/crypto/libecpcr.a ../core/htable/libecpht.a ../core/posix/libecptr.a ../core/posix/libecptm.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 $@ $(LDFLAGS) $< $(dep) + +clean: + rm -f *.o *.a + rm -f mknode diff --git a/code/util/mknode.c b/code/util/mknode.c new file mode 100644 index 0000000..0c389dc --- /dev/null +++ b/code/util/mknode.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include + +#include +#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 [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(&ctx, &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/code/util/util.c b/code/util/util.c new file mode 100644 index 0000000..e225ea1 --- /dev/null +++ b/code/util/util.c @@ -0,0 +1,91 @@ +#include +#include +#include + +#include +#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, ctx->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); + + ctx->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, ctx->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); + + ctx->cr.dh_pub_from_buf(&node->public, buf); + + return ECP_OK; +} \ No newline at end of file diff --git a/code/util/util.h b/code/util/util.h new file mode 100644 index 0000000..3d07588 --- /dev/null +++ b/code/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 -- cgit v1.2.3