I am having trouble with encrypting data in a way that conforms to the onReceive and onSend functions. I am trying to encrypt and decrypt data using OpenSSL. My problem is that I can't figure out how to get OpenSSL to take in an input of variable size. What I mean by this is that my code works perfectly well when OpenSSL takes in something such as unsigned char ciphertext[128]; - however this is of a fixed length and packets in networks of course can be of varying sizes.
I am basically just trying to figure out how to pass in ciphertext as I am now but with a varying length. I did manage to get an encryption code working that returned a string as opposed to a void (char*? I've seen that SecureSFML for example returns/appends a char* in its' onReceive/onSend functions) as onReceive and onSend do.
Below is a simplified bit of the code I am using specifically for encrypting and not decrypting since my problem is the same in both encrypting and decrypting.
unsigned char* key;
unsigned char* iv;
unsigned char tag[16];
int gcm_encrypt(unsigned char* plaintext, int plaintext_len,
unsigned char* aad, int aad_len,
unsigned char* key,
unsigned char* iv, int iv_len,
unsigned char* ciphertext,
unsigned char* tag);
char* encrypt(const char* data, std::size_t& length)
{
unsigned char ciphertext[128];
int ciphertext_len = gcm_encrypt((unsigned char*)data, strlen((char*)data),
NULL, NULL,
key,
iv, 16,
ciphertext, tag);
length = ciphertext_len;
return (char*)ciphertext;
}
This code works perfectly aside from the problem with ciphertext being of a fixed length.