SFML community forums

Help => Network => Topic started by: Mourzor on December 27, 2022, 10:07:16 pm

Title: Trouble with conforming to sf::Packet::onReceive and onSend char/void return
Post by: Mourzor on December 27, 2022, 10:07:16 pm
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.
Title: Re: Trouble with conforming to sf::Packet::onReceive and onSend char/void return
Post by: Mourzor on December 28, 2022, 11:59:12 am
Managed to solve everything I think. Also realized I was returning a pointer to something getting deleted right away so I have just made it apply to an outside variable.

std::vector<unsigned char> ciphertext;

char* encrypt(const char* data, std::size_t& length)
{
        ciphertext.resize(length + (16 - (length % 16)));

        int ciphertext_len = gcm_encrypt((unsigned char*)data, strlen((char*)data),
                NULL, NULL,
                key,
                iv, 16,
                &ciphertext[0], tag);

        length = ciphertext_len;

        return (char*)&ciphertext[0];
}
Title: Re: Trouble with conforming to sf::Packet::onReceive and onSend char/void return
Post by: eXpl0it3r on December 28, 2022, 01:50:31 pm
For OpenSSL questions you're probably better off over at StackOverflow or similar.
If you're just sending plain bytes, you could also directly use the sockets instead of putting it on a packet.
Title: Re: Trouble with conforming to sf::Packet::onReceive and onSend char/void return
Post by: Mourzor on December 28, 2022, 03:20:07 pm
I see. That does make sense. I did manage to solve the issue anyway I think so I should be fine.