summaryrefslogtreecommitdiff
path: root/ecp/util/util.c
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2020-08-05 03:38:22 +0200
committerUros Majstorovic <majstor@majstor.org>2020-08-05 03:38:22 +0200
commit5cd610a07468137066ea4daa5176c3e7045113b0 (patch)
treea6a5b572572f8f37ec2cb87332fa46e9bcc53aa7 /ecp/util/util.c
parent2473a7d5c51806ab8651cd3c4e07a15b62084eb5 (diff)
ecp moved to root; fixed utils and tests
Diffstat (limited to 'ecp/util/util.c')
-rw-r--r--ecp/util/util.c92
1 files changed, 92 insertions, 0 deletions
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