summaryrefslogtreecommitdiff
path: root/ecp/util
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2022-03-12 13:08:52 +0100
committerUros Majstorovic <majstor@majstor.org>2022-03-12 13:08:52 +0100
commit3436ce5d3ffbc2777e785310bac971c830cbb4ff (patch)
treec3ffb0d18bb992e8b97818e4d0b369844979012d /ecp/util
parentd359dd9981f4137e8873a7a571e5c54c5b2449c4 (diff)
new rev compile OK
Diffstat (limited to 'ecp/util')
-rw-r--r--ecp/util/mknode.c23
-rw-r--r--ecp/util/util.c35
-rw-r--r--ecp/util/util.h8
3 files changed, 25 insertions, 41 deletions
diff --git a/ecp/util/mknode.c b/ecp/util/mknode.c
index 0c262fe..885a4c0 100644
--- a/ecp/util/mknode.c
+++ b/ecp/util/mknode.c
@@ -12,26 +12,15 @@
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;
+ int rv;
if ((argc < 2) || (argc > 3)) usage(argv[0]);
@@ -41,20 +30,16 @@ int main(int argc, char *argv[]) {
strcat(fn_key, ".priv");
strcat(fn_node, ".pub");
- rv = ecp_ctx_init(&ctx);
- if (rv) goto err;
- ctx.rng = v_rng;
-
- rv = ecp_dhkey_gen(&ctx, &key);
+ rv = ecp_dhkey_gen(&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);
+ rv = ecp_util_key_save(&key, fn_key);
if (rv) goto err;
- rv = ecp_util_node_save(&ctx, &node, fn_node);
+ rv = ecp_util_node_save(&node, fn_node);
if (rv) goto err;
return 0;
diff --git a/ecp/util/util.c b/ecp/util/util.c
index 7ce8da5..ee51c73 100644
--- a/ecp/util/util.c
+++ b/ecp/util/util.c
@@ -7,13 +7,15 @@
#include "cr.h"
#include "util.h"
-int ecp_util_key_save(ECPContext *ctx, ECPDHKey *key, char *filename) {
+int ecp_util_key_save(ECPDHKey *key, 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, ecp_cr_dh_pub_get_buf(&key->public), ECP_ECDH_SIZE_KEY);
- if (rv != ECP_ECDH_SIZE_KEY) {
+ rv = write(fd, &key->public, sizeof(key->public));
+ if (rv != sizeof(key->public)) {
close(fd);
return ECP_ERR;
}
@@ -26,14 +28,13 @@ int ecp_util_key_save(ECPContext *ctx, ECPDHKey *key, char *filename) {
return ECP_OK;
}
-int ecp_util_key_load(ECPContext *ctx, ECPDHKey *key, char *filename) {
+int ecp_util_key_load(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) {
+ rv = read(fd, &key->public, sizeof(key->public));
+ if (rv != sizeof(key->public)) {
close(fd);
return ECP_ERR;
}
@@ -44,19 +45,19 @@ int ecp_util_key_load(ECPContext *ctx, ECPDHKey *key, char *filename) {
}
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 ecp_util_node_save(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, ecp_cr_dh_pub_get_buf(&node->public), ECP_ECDH_SIZE_KEY);
- if (rv != ECP_ECDH_SIZE_KEY) {
+ rv = write(fd, &node->key_perma.public, sizeof(node->key_perma.public));
+ if (rv != sizeof(node->key_perma.public)) {
close(fd);
return ECP_ERR;
}
@@ -69,14 +70,13 @@ int ecp_util_node_save(ECPContext *ctx, ECPNode *node, char *filename) {
return ECP_OK;
}
-int ecp_util_node_load(ECPContext *ctx, ECPNode *node, char *filename) {
+int ecp_util_node_load(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) {
+ rv = read(fd, &node->key_perma.public, sizeof(node->key_perma.public));
+ if (rv != sizeof(node->key_perma.public)) {
close(fd);
return ECP_ERR;
}
@@ -87,7 +87,6 @@ int ecp_util_node_load(ECPContext *ctx, ECPNode *node, char *filename) {
}
close(fd);
- ecp_cr_dh_pub_from_buf(&node->public, buf);
-
+ node->key_perma.valid = 1;
return ECP_OK;
} \ No newline at end of file
diff --git a/ecp/util/util.h b/ecp/util/util.h
index 3d07588..631a808 100644
--- a/ecp/util/util.h
+++ b/ecp/util/util.h
@@ -1,5 +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_key_save(ECPDHKey *key, char *filename);
+int ecp_util_key_load(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
+int ecp_util_node_save(ECPNode *node, char *filename);
+int ecp_util_node_load(ECPNode *node, char *filename); \ No newline at end of file