Compare commits

..

No commits in common. "c1e7aba8796b0d511c31fdbf50d3ec9ed9da21c7" and "d764002546419300d087994b4f1414daea4e235b" have entirely different histories.

2 changed files with 10 additions and 49 deletions

View File

@ -23,10 +23,6 @@ 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
@ -35,13 +31,10 @@ 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, size_t length) char* base64_encode(char *message)
{ {
//VARIABLES //VARIABLES
BIO *bio; BIO *bio;
@ -45,20 +45,18 @@ char *base64_encode(char *message, size_t length)
//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, length); BIO_write(bio, message, strlen(message));
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 + why2_count_int_length((int) length) + 2); encoded_message = why2_malloc(buffer_ptr -> length + 1);
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);
@ -66,27 +64,21 @@ char *base64_encode(char *message, size_t length)
return encoded_message; return encoded_message;
} }
char *base64_decode(char *encoded_message, size_t *length) char* base64_decode(char *encoded_message)
{ {
//VARIABLES //VARIABLES
BIO *bio; BIO *bio;
BIO *b64; BIO *b64;
char *separator_ptr = strrchr(encoded_message, WHY2_CHAT_BASE64_LENGTH_DELIMITER); //GET THE DELIMITER POINTER size_t length = strlen(encoded_message);
*length = strtoull(separator_ptr + 1, NULL, 10); char* decoded_message = why2_malloc(length);
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_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); //DISABLE NEWLINES bio = BIO_new_mem_buf(encoded_message, length);
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[decoded_length] = '\0'; decoded_message[BIO_read(bio, decoded_message, length)] = '\0';
//DEALLOCATION //DEALLOCATION
BIO_free_all(bio); BIO_free_all(bio);
@ -156,7 +148,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, siglen); //CONVERT sig TO BASE64 encoded_sig = base64_encode(sig); //CONVERT sig TO BASE64
//DEALLOCATION //DEALLOCATION
why2_deallocate(sig); why2_deallocate(sig);
@ -165,30 +157,6 @@ 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