diff options
Diffstat (limited to 'ecp/util/util.c')
-rw-r--r-- | ecp/util/util.c | 80 |
1 files changed, 52 insertions, 28 deletions
diff --git a/ecp/util/util.c b/ecp/util/util.c index ee51c73..2b67127 100644 --- a/ecp/util/util.c +++ b/ecp/util/util.c @@ -3,24 +3,22 @@ #include <unistd.h> #include <sys/stat.h> -#include "core.h" -#include "cr.h" +#include <core.h> + #include "util.h" -int ecp_util_key_save(ECPDHKey *key, char *filename) { +int ecp_util_load_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename) { int fd; ssize_t rv; - if (!key->valid) return ECP_ERR; - - if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; - rv = write(fd, &key->public, sizeof(key->public)); - if (rv != sizeof(key->public)) { + if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR; + rv = read(fd, public, sizeof(ecp_ecdh_public_t)); + if (rv != sizeof(ecp_ecdh_public_t)) { close(fd); return ECP_ERR; } - rv = write(fd, &key->private, sizeof(key->private)); - if (rv != sizeof(key->private)) { + rv = read(fd, private, sizeof(ecp_ecdh_private_t)); + if (rv != sizeof(ecp_ecdh_private_t)) { close(fd); return ECP_ERR; } @@ -28,41 +26,46 @@ int ecp_util_key_save(ECPDHKey *key, char *filename) { return ECP_OK; } -int ecp_util_key_load(ECPDHKey *key, char *filename) { +int ecp_util_save_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename) { int fd; ssize_t rv; - if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR; - rv = read(fd, &key->public, sizeof(key->public)); - if (rv != sizeof(key->public)) { + if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; + rv = write(fd, public, sizeof(ecp_ecdh_public_t)); + if (rv != sizeof(ecp_ecdh_public_t)) { close(fd); return ECP_ERR; } - rv = read(fd, &key->private, sizeof(key->private)); - if (rv != sizeof(key->private)) { + rv = write(fd, private, sizeof(ecp_ecdh_private_t)); + if (rv != sizeof(ecp_ecdh_private_t)) { close(fd); return ECP_ERR; } close(fd); - - key->valid = 1; return ECP_OK; } -int ecp_util_node_save(ECPNode *node, char *filename) { +int ecp_util_load_pub(ecp_ecdh_public_t *public, char *filename) { int fd; ssize_t rv; - if (!node->key_perma.valid) return ECP_ERR; - - if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; - rv = write(fd, &node->key_perma.public, sizeof(node->key_perma.public)); - if (rv != sizeof(node->key_perma.public)) { + if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR; + rv = read(fd, public, sizeof(ecp_ecdh_public_t)); + if (rv != sizeof(ecp_ecdh_public_t)) { close(fd); return ECP_ERR; } - rv = write(fd, &node->addr, sizeof(node->addr)); - if (rv != sizeof(node->addr)) { + close(fd); + return ECP_OK; +} + +int ecp_util_save_pub(ecp_ecdh_public_t *public, 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, public, sizeof(ecp_ecdh_public_t)); + if (rv != sizeof(ecp_ecdh_public_t)) { close(fd); return ECP_ERR; } @@ -70,7 +73,7 @@ int ecp_util_node_save(ECPNode *node, char *filename) { return ECP_OK; } -int ecp_util_node_load(ECPNode *node, char *filename) { +int ecp_util_load_node(ECPNode *node, char *filename) { int fd; ssize_t rv; @@ -89,4 +92,25 @@ int ecp_util_node_load(ECPNode *node, char *filename) { node->key_perma.valid = 1; return ECP_OK; -}
\ No newline at end of file +} + +int ecp_util_save_node(ECPNode *node, char *filename) { + int fd; + ssize_t rv; + + if (!node->key_perma.valid) return ECP_ERR; + + if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; + rv = write(fd, &node->key_perma.public, sizeof(node->key_perma.public)); + if (rv != sizeof(node->key_perma.public)) { + 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; +} |