Compare commits

..

6 Commits

Author SHA1 Message Date
c1e7aba879
defined why2_chat_ecc_verify_signature
All checks were successful
Codacy Scan / Codacy Security Scan (push) Successful in 10s
Build WHY2-chat / test-why2 (./out/why2-chat-client, ./configure.sh, ubuntu-latest, ./out/why2-chat-server) (push) Successful in 1m45s
Test Project / test-project (./configure.sh, gdb -ex "run" -ex "quit" --batch, ubuntu-latest, ./test) (push) Successful in 1m42s
Test WHY2-core / test-why2 (why2, ./configure.sh, gdb -ex "run" -ex "quit" --batch, ubuntu-latest, ./out/why2-core-test, valgrind --leak-check=full --show-leak-kinds=reachable --track-origins=yes -s) (push) Successful in 2m18s
Test WHY2-logger / test-why2 (why2-logger, ./configure.sh, gdb -ex "run" -ex "quit" --batch, ubuntu-latest, ./out/why2-logger-test, valgrind --leak-check=full --show-leak-kinds=reachable --track-origins=yes -s) (push) Successful in 2m28s
one commit, 8 hours of going crazy together with AI
2025-01-30 20:07:19 +01:00
ac88620d99
added key parameter to why2_chat_ecc_verify_signature declaration 2025-01-30 20:06:20 +01:00
c8d197f6b9
returning length in base64_decode 2025-01-30 19:58:44 +01:00
210d23369b
implemented base64 delimiter 2025-01-30 19:38:47 +01:00
b859c88dcc
declared why2_chat_ecc_verify_signature 2025-01-30 19:38:02 +01:00
7c2c8b9138
created WHY2_CHAT_BASE64_LENGTH_DELIMITER macro 2025-01-30 19:37:49 +01:00
2 changed files with 49 additions and 10 deletions

View File

@ -23,6 +23,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
extern "C" { extern "C" {
#endif #endif
#include <openssl/types.h>
#include <why2/flags.h>
#include <why2/chat/config.h> #include <why2/chat/config.h>
//MACROS //MACROS
@ -31,10 +35,13 @@ extern "C" {
#define WHY2_CHAT_KEY_LOCATION WHY2_CONFIG_DIR "/keys" //KEYS LOCATION #define WHY2_CHAT_KEY_LOCATION WHY2_CONFIG_DIR "/keys" //KEYS LOCATION
#define WHY2_CHAT_KEY "secp521r1.pem" #define WHY2_CHAT_KEY "secp521r1.pem"
#define WHY2_CHAT_BASE64_LENGTH_DELIMITER ':' //SEPARATES BASE64 FROM LENGTH (YnJhbWJvcmFrCg==:9)
void why2_chat_init_keys(void); //INIT (POSSIBLY GENERATE) ECC KEYS void why2_chat_init_keys(void); //INIT (POSSIBLY GENERATE) ECC KEYS
void why2_chat_deallocate_keys(void); //DEALLOCATE :) (NO SLUR HERE) void why2_chat_deallocate_keys(void); //DEALLOCATE :) (NO SLUR HERE)
char *why2_chat_ecc_sign(char *message); //SIGN message WITH ECC KEY 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_sha256(char *input); //HASH input USING SHA256 AND RETURN IN STRING char *why2_sha256(char *input); //HASH input USING SHA256 AND RETURN IN STRING

View File

@ -35,7 +35,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
EVP_PKEY *keypair = NULL; //KEYPAIR EVP_PKEY *keypair = NULL; //KEYPAIR
//LOCAL //LOCAL
char* base64_encode(char *message) char *base64_encode(char *message, size_t length)
{ {
//VARIABLES //VARIABLES
BIO *bio; BIO *bio;
@ -45,18 +45,20 @@ char* base64_encode(char *message)
//INIT BIOs //INIT BIOs
b64 = BIO_new(BIO_f_base64()); b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); //DISABLE NEWLINES
bio = BIO_new(BIO_s_mem()); bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio); bio = BIO_push(b64, bio);
//ENCODE //ENCODE
BIO_write(bio, message, strlen(message)); BIO_write(bio, message, length);
BIO_flush(bio); BIO_flush(bio);
BIO_get_mem_ptr(bio, &buffer_ptr); BIO_get_mem_ptr(bio, &buffer_ptr);
//COPY //COPY
encoded_message = why2_malloc(buffer_ptr -> length + 1); encoded_message = why2_malloc(buffer_ptr -> length + why2_count_int_length((int) length) + 2);
memcpy(encoded_message, buffer_ptr -> data, buffer_ptr -> length); memcpy(encoded_message, buffer_ptr -> data, buffer_ptr -> length);
encoded_message[buffer_ptr -> length] = '\0';
sprintf(encoded_message + buffer_ptr -> length, "%c%zu%c", WHY2_CHAT_BASE64_LENGTH_DELIMITER, length, '\0'); //APPEND LENGTH
//DEALLOCATION //DEALLOCATION
BIO_free_all(bio); BIO_free_all(bio);
@ -64,21 +66,27 @@ char* base64_encode(char *message)
return encoded_message; return encoded_message;
} }
char* base64_decode(char *encoded_message) char *base64_decode(char *encoded_message, size_t *length)
{ {
//VARIABLES //VARIABLES
BIO *bio; BIO *bio;
BIO *b64; BIO *b64;
size_t length = strlen(encoded_message); char *separator_ptr = strrchr(encoded_message, WHY2_CHAT_BASE64_LENGTH_DELIMITER); //GET THE DELIMITER POINTER
char* decoded_message = why2_malloc(length); *length = strtoull(separator_ptr + 1, NULL, 10);
char* decoded_message = why2_malloc(*length + 1);
int decoded_length;
//INIT BIOs //INIT BIOs
b64 = BIO_new(BIO_f_base64()); b64 = BIO_new(BIO_f_base64());
bio = BIO_new_mem_buf(encoded_message, length); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); //DISABLE NEWLINES
bio = BIO_new_mem_buf(encoded_message, separator_ptr - encoded_message);
bio = BIO_push(b64, bio); bio = BIO_push(b64, bio);
//DECODE
decoded_length = BIO_read(bio, decoded_message, *length);
//NULL-TERM //NULL-TERM
decoded_message[BIO_read(bio, decoded_message, length)] = '\0'; decoded_message[decoded_length] = '\0';
//DEALLOCATION //DEALLOCATION
BIO_free_all(bio); BIO_free_all(bio);
@ -148,7 +156,7 @@ char *why2_chat_ecc_sign(char *message)
sig = why2_malloc(siglen); //ALLOCATE SIGNATURE sig = why2_malloc(siglen); //ALLOCATE SIGNATURE
EVP_DigestSignFinal(mdctx, (unsigned char*) sig, &siglen); EVP_DigestSignFinal(mdctx, (unsigned char*) sig, &siglen);
encoded_sig = base64_encode(sig); //CONVERT sig TO BASE64 encoded_sig = base64_encode(sig, siglen); //CONVERT sig TO BASE64
//DEALLOCATION //DEALLOCATION
why2_deallocate(sig); why2_deallocate(sig);
@ -157,6 +165,30 @@ char *why2_chat_ecc_sign(char *message)
return encoded_sig; return encoded_sig;
} }
why2_bool why2_chat_ecc_verify_signature(char *message, char *signature, EVP_PKEY *key)
{
//VARIABLES
size_t length;
char *decoded_signature = base64_decode(signature, &length); //DECODE SIGNATURE
why2_bool returning;
//INIT CONTEXT
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
//INIT VERIFICATION CONTEXT
EVP_DigestVerifyInit(ctx, NULL, EVP_sha256(), NULL, key);
//VERIFY MESSAGE
EVP_DigestVerifyUpdate(ctx, message, strlen(message));
returning = EVP_DigestVerifyFinal(ctx, (unsigned char*) decoded_signature, length) == 1;
//DEALLOCATION
EVP_MD_CTX_free(ctx);
why2_deallocate(decoded_signature);
return returning;
}
void why2_chat_deallocate_keys(void) void why2_chat_deallocate_keys(void)
{ {
//DEALLOCATE THE pkey //DEALLOCATE THE pkey