From 7bb6e38a99603993c366ada2509c561eada32494 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Sat, 1 Feb 2025 12:55:39 +0100 Subject: [PATCH] created why2_chat_ecc_serialize_public_key fn --- include/chat/crypto.h | 2 ++ src/chat/crypto.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/chat/crypto.h b/include/chat/crypto.h index affdda5..0230b39 100644 --- a/include/chat/crypto.h +++ b/include/chat/crypto.h @@ -43,6 +43,8 @@ void why2_chat_deallocate_keys(void); //DEALLOCATE :) (NO SLUR HERE) char *why2_chat_ecc_sign(char *message); //SIGN message WITH ECC KEY why2_bool why2_chat_ecc_verify_signature(char *message, char *signature, EVP_PKEY *key); +char *why2_chat_ecc_serialize_public_key(); //GET PUBLIC ECC KEY IN BASE64 + char *why2_sha256(char *input); //HASH input USING SHA256 AND RETURN IN STRING #ifdef __cplusplus diff --git a/src/chat/crypto.c b/src/chat/crypto.c index b2a1b49..802cc68 100644 --- a/src/chat/crypto.c +++ b/src/chat/crypto.c @@ -189,6 +189,37 @@ why2_bool why2_chat_ecc_verify_signature(char *message, char *signature, EVP_PKE return returning; } +//TODO: Remove PEM +char *why2_chat_ecc_serialize_public_key() +{ + //VARIABLES + BIO *bio = BIO_new(BIO_s_mem()); + char *data; + size_t length; + char *pubkey; + char *base64_encoded; + + //SEPARATE PUBKEY + PEM_write_bio_PUBKEY(bio, keypair); + + //ALLOCATE + length = BIO_get_mem_data(bio, &data); + pubkey = why2_malloc(length + 1); + + //COPY + memcpy(pubkey, data, length); + pubkey[length] = '\0'; + + //ENCODE + base64_encoded = base64_encode(pubkey, length); + + //DEALLOCATION + BIO_free(bio); + why2_deallocate(pubkey); + + return base64_encoded; +} + void why2_chat_deallocate_keys(void) { //DEALLOCATE THE pkey