moved base64 fns to crypto header
This commit is contained in:
parent
06b1c8728c
commit
0a9a716ffe
@ -43,6 +43,9 @@ extern "C" {
|
|||||||
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_base64_encode(char *message, size_t length); //ENCODE message OF length INTO BASE64 WITH LENGTH DELIMITER (WHY2_CHAT_BASE64_LENGTH_DELIMITER)
|
||||||
|
char *why2_chat_base64_decode(char *encoded_message, size_t *length); //DECODE encoded_message AND SET length TO OUTPUT LENGTH
|
||||||
|
|
||||||
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);
|
why2_bool why2_chat_ecc_verify_signature(char *message, char *signature, EVP_PKEY *key);
|
||||||
|
|
||||||
|
@ -36,68 +36,6 @@ 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)
|
|
||||||
{
|
|
||||||
//VARIABLES
|
|
||||||
BIO *bio;
|
|
||||||
BIO *b64;
|
|
||||||
BUF_MEM *buffer_ptr;
|
|
||||||
char* encoded_message;
|
|
||||||
|
|
||||||
//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_flush(bio);
|
|
||||||
BIO_get_mem_ptr(bio, &buffer_ptr);
|
|
||||||
|
|
||||||
//COPY
|
|
||||||
encoded_message = why2_malloc(buffer_ptr -> length + why2_count_int_length((int) length) + 2);
|
|
||||||
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
|
|
||||||
|
|
||||||
//DEALLOCATION
|
|
||||||
BIO_free_all(bio);
|
|
||||||
|
|
||||||
return encoded_message;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *base64_decode(char *encoded_message, size_t *length)
|
|
||||||
{
|
|
||||||
//VARIABLES
|
|
||||||
BIO *bio;
|
|
||||||
BIO *b64;
|
|
||||||
char *separator_ptr = strrchr(encoded_message, WHY2_CHAT_BASE64_LENGTH_DELIMITER); //GET THE DELIMITER POINTER
|
|
||||||
size_t length_local = strtoull(separator_ptr + 1, NULL, 10);
|
|
||||||
char* decoded_message = why2_malloc(length_local + 1);
|
|
||||||
int decoded_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_push(b64, bio);
|
|
||||||
|
|
||||||
//DECODE
|
|
||||||
decoded_length = BIO_read(bio, decoded_message, length_local);
|
|
||||||
|
|
||||||
//NULL-TERM
|
|
||||||
decoded_message[decoded_length] = '\0';
|
|
||||||
|
|
||||||
//DEALLOCATION
|
|
||||||
BIO_free_all(bio);
|
|
||||||
|
|
||||||
//SET length
|
|
||||||
if (length != NULL) *length = length_local;
|
|
||||||
|
|
||||||
return decoded_message;
|
|
||||||
}
|
|
||||||
|
|
||||||
void calculate_ecdh_secret(EVP_PKEY *pri, EVP_PKEY *pub, char **secret, size_t *len)
|
void calculate_ecdh_secret(EVP_PKEY *pri, EVP_PKEY *pub, char **secret, size_t *len)
|
||||||
{
|
{
|
||||||
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pri, NULL);
|
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pri, NULL);
|
||||||
@ -158,6 +96,68 @@ void why2_chat_init_keys(void)
|
|||||||
why2_deallocate(key);
|
why2_deallocate(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *why2_chat_base64_encode(char *message, size_t length)
|
||||||
|
{
|
||||||
|
//VARIABLES
|
||||||
|
BIO *bio;
|
||||||
|
BIO *b64;
|
||||||
|
BUF_MEM *buffer_ptr;
|
||||||
|
char* encoded_message;
|
||||||
|
|
||||||
|
//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_flush(bio);
|
||||||
|
BIO_get_mem_ptr(bio, &buffer_ptr);
|
||||||
|
|
||||||
|
//COPY
|
||||||
|
encoded_message = why2_malloc(buffer_ptr -> length + why2_count_int_length((int) length) + 2);
|
||||||
|
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
|
||||||
|
|
||||||
|
//DEALLOCATION
|
||||||
|
BIO_free_all(bio);
|
||||||
|
|
||||||
|
return encoded_message;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *why2_chat_base64_decode(char *encoded_message, size_t *length)
|
||||||
|
{
|
||||||
|
//VARIABLES
|
||||||
|
BIO *bio;
|
||||||
|
BIO *b64;
|
||||||
|
char *separator_ptr = strrchr(encoded_message, WHY2_CHAT_BASE64_LENGTH_DELIMITER); //GET THE DELIMITER POINTER
|
||||||
|
size_t length_local = strtoull(separator_ptr + 1, NULL, 10);
|
||||||
|
char* decoded_message = why2_malloc(length_local + 1);
|
||||||
|
int decoded_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_push(b64, bio);
|
||||||
|
|
||||||
|
//DECODE
|
||||||
|
decoded_length = BIO_read(bio, decoded_message, length_local);
|
||||||
|
|
||||||
|
//NULL-TERM
|
||||||
|
decoded_message[decoded_length] = '\0';
|
||||||
|
|
||||||
|
//DEALLOCATION
|
||||||
|
BIO_free_all(bio);
|
||||||
|
|
||||||
|
//SET length
|
||||||
|
if (length != NULL) *length = length_local;
|
||||||
|
|
||||||
|
return decoded_message;
|
||||||
|
}
|
||||||
|
|
||||||
char *why2_chat_ecc_sign(char *message)
|
char *why2_chat_ecc_sign(char *message)
|
||||||
{
|
{
|
||||||
//VARIABLES
|
//VARIABLES
|
||||||
@ -177,7 +177,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 = why2_chat_base64_encode(sig, siglen); //CONVERT sig TO BASE64
|
||||||
|
|
||||||
//DEALLOCATION
|
//DEALLOCATION
|
||||||
why2_deallocate(sig);
|
why2_deallocate(sig);
|
||||||
@ -232,7 +232,7 @@ char *why2_chat_ecc_serialize_public_key()
|
|||||||
pubkey[length] = '\0';
|
pubkey[length] = '\0';
|
||||||
|
|
||||||
//ENCODE
|
//ENCODE
|
||||||
base64_encoded = base64_encode(pubkey, length);
|
base64_encoded = why2_chat_base64_encode(pubkey, length);
|
||||||
|
|
||||||
//DEALLOCATION
|
//DEALLOCATION
|
||||||
BIO_free(bio);
|
BIO_free(bio);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user