diff options
Diffstat (limited to 'ecp/util')
-rw-r--r-- | ecp/util/Makefile | 6 | ||||
-rw-r--r-- | ecp/util/keygen.c | 37 | ||||
-rw-r--r-- | ecp/util/mknode.c | 63 | ||||
-rw-r--r-- | ecp/util/util.c | 126 | ||||
-rw-r--r-- | ecp/util/util.h | 11 |
5 files changed, 89 insertions, 154 deletions
diff --git a/ecp/util/Makefile b/ecp/util/Makefile index e65a757..5db7686 100644 --- a/ecp/util/Makefile +++ b/ecp/util/Makefile @@ -8,14 +8,14 @@ dep=../build-posix/*.a ./libecputil.a %.o: %.c %.h $(CC) $(CFLAGS) -c $< -all: libecputil.a mknode +all: libecputil.a keygen libecputil.a: $(obj) $(AR) rcs libecputil.a $(obj) -mknode: mknode.o libecputil.a +keygen: keygen.o libecputil.a $(CC) -o $@ $< $(dep) $(LDFLAGS) clean: rm -f *.o *.a - rm -f mknode + rm -f keygen diff --git a/ecp/util/keygen.c b/ecp/util/keygen.c new file mode 100644 index 0000000..a0a1507 --- /dev/null +++ b/ecp/util/keygen.c @@ -0,0 +1,37 @@ +#include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> +#include <string.h> + +#include <ecp/core.h> + +#include "util.h" + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s <pub> <priv>\n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + ECPDHKey key; + int rv; + + if (argc != 3) usage(argv[0]); + printf("keygen: public=%p, private=%p\n", key.public, key.private); + + rv = ecp_dhkey_gen(&key); + if (rv) goto err; + + rv = ecp_util_save_key(argv[1], &key.public, NULL); + if (rv) goto err; + + rv = ecp_util_save_key(argv[2], &key.public, &key.private); + if (rv) goto err; + + return 0; + +err: + printf("ERR:%d\n", rv); + return 1; +}
\ No newline at end of file diff --git a/ecp/util/mknode.c b/ecp/util/mknode.c deleted file mode 100644 index bc66da0..0000000 --- a/ecp/util/mknode.c +++ /dev/null @@ -1,63 +0,0 @@ -#include <stdlib.h> -#include <fcntl.h> -#include <unistd.h> -#include <stdio.h> -#include <string.h> - -#include <ecp/core.h> - -#include "util.h" - -#define FN_LEN 256 - -static char fn_key[FN_LEN]; -static char fn_node[FN_LEN]; - -static void usage(char *arg) { - fprintf(stderr, "Usage: %s <name> [address]\n", arg); - exit(1); -} - -int main(int argc, char *argv[]) { - char *addr; - ECPDHKey key; - ECPNode node; - int rv; - - if ((argc < 2) || (argc > 3)) usage(argv[0]); - - addr = NULL; - if (argc == 3) addr = argv[2]; - - 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_dhkey_gen(&key); - if (rv) goto err; - - ecp_node_init(&node, &key.public, NULL); - if (addr) { - rv = ecp_node_set_addr(&node, addr); - if (rv) goto err; - } - - rv = ecp_util_save_key(&key.public, &key.private, fn_key); - if (rv) goto err; - - if (addr) { - rv = ecp_util_save_node(&node, fn_node); - if (rv) goto err; - } else { - rv = ecp_util_save_pub(&key.public, fn_node); - if (rv) goto err; - } - - return 0; - -err: - printf("ERR:%d\n", rv); - return 1; -}
\ No newline at end of file diff --git a/ecp/util/util.c b/ecp/util/util.c index efcf152..abe562d 100644 --- a/ecp/util/util.c +++ b/ecp/util/util.c @@ -1,116 +1,80 @@ #include <stdlib.h> +#include <string.h> +#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <sys/stat.h> #include <ecp/core.h> +#include <ecp/cr.h> #include "util.h" -int ecp_util_load_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename) { - int fd; +int ecp_util_read_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private) { + char buffer[ECP_SIZE_ECDH_KEY_BUF]; ssize_t rv; + int _rv; - 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 = read(fd, private, sizeof(ecp_ecdh_private_t)); - if (rv != sizeof(ecp_ecdh_private_t)) { - close(fd); - return ECP_ERR; - } - close(fd); - return ECP_OK; -} + memset(buffer, 0, sizeof(buffer)); + rv = read(fd, buffer, sizeof(buffer)); + if (rv < (ECP_SIZE_ECDH_KEY_BUF - 1)) return ECP_ERR; + if (buffer[ECP_SIZE_ECDH_KEY_BUF - 1] == '\n') buffer[ECP_SIZE_ECDH_KEY_BUF - 1] = '\0'; -int ecp_util_save_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename) { - int fd; - ssize_t rv; + _rv = ecp_str2key((uint8_t *)public, buffer); + if (_rv) return _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; - } - rv = write(fd, private, sizeof(ecp_ecdh_private_t)); - if (rv != sizeof(ecp_ecdh_private_t)) { - close(fd); - return ECP_ERR; + if (private) { + memset(buffer, 0, sizeof(buffer)); + rv = read(fd, buffer, sizeof(buffer)); + if (rv < (ECP_SIZE_ECDH_KEY_BUF - 1)) return ECP_ERR; + if (buffer[ECP_SIZE_ECDH_KEY_BUF - 1] == '\n') buffer[ECP_SIZE_ECDH_KEY_BUF - 1] = '\0'; + + _rv = ecp_str2key((uint8_t *)private, buffer); + if (_rv) return _rv; } - close(fd); + return ECP_OK; } -int ecp_util_load_pub(ecp_ecdh_public_t *public, char *filename) { - int fd; +int ecp_util_write_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private) { + char buffer[ECP_SIZE_ECDH_KEY_BUF]; ssize_t rv; - 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; - } - close(fd); - return ECP_OK; -} + ecp_key2str(buffer, (uint8_t *)public); + buffer[ECP_SIZE_ECDH_KEY_BUF - 1] = '\n'; -int ecp_util_save_pub(ecp_ecdh_public_t *public, char *filename) { - int fd; - ssize_t rv; + rv = write(fd, buffer, sizeof(buffer)); + if (rv != sizeof(buffer)) return ECP_ERR; - 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; + if (private) { + ecp_key2str(buffer, (uint8_t *)private); + buffer[ECP_SIZE_ECDH_KEY_BUF - 1] = '\n'; + + rv = write(fd, buffer, sizeof(buffer)); + if (rv != sizeof(buffer)) return ECP_ERR; } - close(fd); + return ECP_OK; } -int ecp_util_load_node(ECPNode *node, char *filename) { - int fd; - ssize_t rv; +int ecp_util_load_key(char *filename, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private) { + int rv, fd; if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR; - rv = read(fd, &node->key_perma.public, sizeof(node->key_perma.public)); - if (rv != sizeof(node->key_perma.public)) { - close(fd); - return ECP_ERR; - } - rv = read(fd, &node->addr, sizeof(node->addr)); - if (rv != sizeof(node->addr)) { - close(fd); - return ECP_ERR; - } + + rv = ecp_util_read_key(fd, public, private); close(fd); - node->key_perma.valid = 1; - return ECP_OK; + return rv; } -int ecp_util_save_node(ECPNode *node, char *filename) { - int fd; - ssize_t rv; - - if (!node->key_perma.valid) return ECP_ERR; +int ecp_util_save_key(char *filename, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private) { + int rv, fd; 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; - } + + rv = ecp_util_write_key(fd, public, private); close(fd); - return ECP_OK; + + return rv; } diff --git a/ecp/util/util.h b/ecp/util/util.h index 83c4c14..f9a6bb4 100644 --- a/ecp/util/util.h +++ b/ecp/util/util.h @@ -1,8 +1,5 @@ -int ecp_util_load_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename); -int ecp_util_save_key(ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, char *filename); +int ecp_util_read_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private); +int ecp_util_write_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private); -int ecp_util_load_pub(ecp_ecdh_public_t *public, char *filename); -int ecp_util_save_pub(ecp_ecdh_public_t *public, char *filename); - -int ecp_util_load_node(ECPNode *node, char *filename); -int ecp_util_save_node(ECPNode *node, char *filename); +int ecp_util_load_key(char *filename, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private); +int ecp_util_save_key(char *filename, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private);
\ No newline at end of file |