Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Trouble with conforming to sf::Packet::onReceive and onSend char/void return  (Read 233 times)

0 Members and 1 Guest are viewing this topic.

Mourzor

  • Newbie
  • *
  • Posts: 3
    • View Profile
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.
« Last Edit: December 27, 2022, 10:16:12 pm by Mourzor »

Mourzor

  • Newbie
  • *
  • Posts: 3
    • View Profile
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];
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10390
    • View Profile
    • development blog
    • Email
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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

Mourzor

  • Newbie
  • *
  • Posts: 3
    • View Profile
I see. That does make sense. I did manage to solve the issue anyway I think so I should be fine.