diff options
author | Uros Majstorovic <majstor@majstor.org> | 2020-08-05 03:38:22 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2020-08-05 03:38:22 +0200 |
commit | 5cd610a07468137066ea4daa5176c3e7045113b0 (patch) | |
tree | a6a5b572572f8f37ec2cb87332fa46e9bcc53aa7 /ecp/util/mknode.c | |
parent | 2473a7d5c51806ab8651cd3c4e07a15b62084eb5 (diff) |
ecp moved to root; fixed utils and tests
Diffstat (limited to 'ecp/util/mknode.c')
-rw-r--r-- | ecp/util/mknode.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/ecp/util/mknode.c b/ecp/util/mknode.c new file mode 100644 index 0000000..a601a45 --- /dev/null +++ b/ecp/util/mknode.c @@ -0,0 +1,64 @@ +#include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> +#include <string.h> + +#include "core.h" +#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 <name> [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(&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 |