diff options
author | Uros Majstorovic <majstor@majstor.org> | 2024-04-23 18:07:21 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2024-04-23 18:07:21 +0200 |
commit | 503ac614ea91b4fdd9d5f6f467a2efcad900a7e1 (patch) | |
tree | 43db3f29292cbce58ab32667f722e3eed316ca7f /ecp | |
parent | 40f4f88f04e834a9b9849dd6bcda78c1a1893506 (diff) |
added string key utilities
Diffstat (limited to 'ecp')
-rw-r--r-- | ecp/src/ecp/cr.h | 3 | ||||
-rw-r--r-- | ecp/src/ecp/crypto/crypto.c | 37 | ||||
-rw-r--r-- | ecp/src/ecp/crypto/crypto.h | 2 | ||||
-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 |
8 files changed, 131 insertions, 154 deletions
diff --git a/ecp/src/ecp/cr.h b/ecp/src/ecp/cr.h index f5a617d..84f9bf1 100644 --- a/ecp/src/ecp/cr.h +++ b/ecp/src/ecp/cr.h @@ -16,3 +16,6 @@ int ecp_ecdsa_sign(ecp_ecdsa_signature_t *sig, unsigned char *m, size_t ml, ecp_ int ecp_ecdsa_verify(unsigned char *m, size_t ml, ecp_ecdsa_signature_t *sig, ecp_ecdsa_public_t *p); void ecp_hmac(unsigned char *hd, ecp_hmac_key_t *k, unsigned char *m, size_t ml); + +int ecp_str2key(uint8_t *key, char *str); +void ecp_key2str(char *str, uint8_t *key); diff --git a/ecp/src/ecp/crypto/crypto.c b/ecp/src/ecp/crypto/crypto.c index 2909f47..1b801ab 100644 --- a/ecp/src/ecp/crypto/crypto.c +++ b/ecp/src/ecp/crypto/crypto.c @@ -1,5 +1,6 @@ #include <stdlib.h> #include <string.h> +#include <stdio.h> #include <ecp/core.h> #include <ecp/cr.h> @@ -131,3 +132,39 @@ void ecp_hmac(unsigned char *hd, ecp_hmac_key_t *k, unsigned char *m, size_t ml) SHA1_Update(&ctx, d, sizeof(d)); SHA1_Final(hd, &ctx); } + +int ecp_str2key(uint8_t *key, char *str) { + unsigned int u[8]; + int i, rv; + + if (str[ECP_SIZE_ECDH_KEY_BUF - 1] != '\0') return ECP_ERR; + + rv = sscanf(str, "%X:%X:%X:%X:%X:%X:%X:%X", &u[0], &u[1], &u[2], &u[3], &u[4], &u[5], &u[6], &u[7]); + if (rv != 8) return ECP_ERR; + + for (i=0; i<8; i++) { + key[0] = u[i] >> 24; + key[1] = u[i] >> 16; + key[2] = u[i] >> 8; + key[3] = u[i]; + key += 4; + } + + return ECP_OK; +} + +void ecp_key2str(char *str, uint8_t *key) { + unsigned int u[8]; + int i; + + for (i=0; i<8; i++) { + u[i] = (unsigned int)key[0] << 24; + u[i] |= (unsigned int)key[1] << 16; + u[i] |= (unsigned int)key[2] << 8; + u[i] |= (unsigned int)key[3]; + key += 4; + } + + sprintf(str, "%.8X:%.8X:%.8X:%.8X:%.8X:%.8X:%.8X:%.8X", u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7]); + str[ECP_SIZE_ECDH_KEY_BUF - 1] = '\0'; +} diff --git a/ecp/src/ecp/crypto/crypto.h b/ecp/src/ecp/crypto/crypto.h index 1320fb8..1fd63a2 100644 --- a/ecp/src/ecp/crypto/crypto.h +++ b/ecp/src/ecp/crypto/crypto.h @@ -18,6 +18,8 @@ #define ECP_SIZE_HMAC_KEY 32 #define ECP_SIZE_HMAC_DIGEST SHA_DIGEST_LENGTH +#define ECP_SIZE_ECDH_KEY_BUF 72 + typedef uint8_t ecp_ecdh_public_t[ECP_SIZE_ECDH_PUB]; typedef uint8_t ecp_ecdh_private_t[ECP_SIZE_ECDH_SEC]; typedef uint8_t ecp_aead_key_t[ECP_SIZE_AEAD_KEY]; 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 |