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" {
#endif
#include <openssl/types.h>
#include <why2/flags.h>
#include <why2/chat/config.h>
//MACROS
@ -35,13 +31,10 @@ extern "C" {
#define WHY2_CHAT_KEY_LOCATION WHY2_CONFIG_DIR "/keys" //KEYS LOCATION
#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_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_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
//LOCAL
char *base64_encode(char *message, size_t length)
char* base64_encode(char *message)
{
//VARIABLES
BIO *bio;
@ -45,20 +45,18 @@ char *base64_encode(char *message, size_t length)
//INIT BIOs
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_push(b64, bio);
//ENCODE
BIO_write(bio, message, length);
BIO_write(bio, message, strlen(message));
BIO_flush(bio);
BIO_get_mem_ptr(bio, &buffer_ptr);
//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);
sprintf(encoded_message + buffer_ptr -> length, "%c%zu%c", WHY2_CHAT_BASE64_LENGTH_DELIMITER, length, '\0'); //APPEND LENGTH
encoded_message[buffer_ptr -> length] = '\0';
//DEALLOCATION
BIO_free_all(bio);
@ -66,27 +64,21 @@ char *base64_encode(char *message, size_t length)
return encoded_message;
}
char *base64_decode(char *encoded_message, size_t *length)
char* base64_decode(char *encoded_message)
{
//VARIABLES
BIO *bio;
BIO *b64;
char *separator_ptr = strrchr(encoded_message, WHY2_CHAT_BASE64_LENGTH_DELIMITER); //GET THE DELIMITER POINTER
*length = strtoull(separator_ptr + 1, NULL, 10);
char* decoded_message = why2_malloc(*length + 1);
int decoded_length;
size_t length = strlen(encoded_message);
char* decoded_message = why2_malloc(length);
//INIT BIOs
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); //DISABLE NEWLINES
bio = BIO_new_mem_buf(encoded_message, separator_ptr - encoded_message);
bio = BIO_new_mem_buf(encoded_message, length);
bio = BIO_push(b64, bio);
//DECODE
decoded_length = BIO_read(bio, decoded_message, *length);
//NULL-TERM
decoded_message[decoded_length] = '\0';
decoded_message[BIO_read(bio, decoded_message, length)] = '\0';
//DEALLOCATION
BIO_free_all(bio);
@ -156,7 +148,7 @@ char *why2_chat_ecc_sign(char *message)
sig = why2_malloc(siglen); //ALLOCATE SIGNATURE
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
why2_deallocate(sig);
@ -165,30 +157,6 @@ char *why2_chat_ecc_sign(char *message)
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)
{
//DEALLOCATE THE pkey