From 503ac614ea91b4fdd9d5f6f467a2efcad900a7e1 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Tue, 23 Apr 2024 18:07:21 +0200 Subject: added string key utilities --- ecp/src/ecp/cr.h | 3 +++ ecp/src/ecp/crypto/crypto.c | 37 +++++++++++++++++++++++++++++++++++++ ecp/src/ecp/crypto/crypto.h | 2 ++ 3 files changed, 42 insertions(+) (limited to 'ecp/src') 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 #include +#include #include #include @@ -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]; -- cgit v1.2.3