summaryrefslogtreecommitdiff
path: root/ecp/src
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2024-04-23 18:07:21 +0200
committerUros Majstorovic <majstor@majstor.org>2024-04-23 18:07:21 +0200
commit503ac614ea91b4fdd9d5f6f467a2efcad900a7e1 (patch)
tree43db3f29292cbce58ab32667f722e3eed316ca7f /ecp/src
parent40f4f88f04e834a9b9849dd6bcda78c1a1893506 (diff)
added string key utilities
Diffstat (limited to 'ecp/src')
-rw-r--r--ecp/src/ecp/cr.h3
-rw-r--r--ecp/src/ecp/crypto/crypto.c37
-rw-r--r--ecp/src/ecp/crypto/crypto.h2
3 files changed, 42 insertions, 0 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];