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/util/util.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 code/util/util.c (limited to 'code/util/util.c') 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 -- cgit v1.2.3