Custom encryption

As an optional feature, the TeamSpeak 3 SDK allows users to implement custom encryption and decryption for all network traffic. Custom encryption replaces the default AES encryption implemented by the TeamSpeak 3 SDK. A possible reason to apply own encryption might be to make ones TeamSpeak 3 client/server incompatible to other SDK implementations.

Important

Custom encryption must be implemented the same way in both the client and server.

Note

If you do not want to use this feature, just don’t implement the two encryption callbacks.

Encryption

To encrypt outgoing data, implement the callback

struct ServerLibFunctions

Server callbacks. Zero initialize and set members to functions that are to be called when the event in question happens. Every callback you use should exit quickly to avoid stalling the server. If you need any expensive activity upon receiving callbacks, consider starting the activity in a new thread and allow the callback to exit quickly.

Public Members

void (*onCustomPacketEncryptEvent)(char **dataToSend, unsigned int *sizeOfData)

called when a packet needs to be encrypted to be sent over the wire.

Used to implement custom encryption of server communication. This needs to be implemented the same in the client and server, otherwise clients cannot communicate with the server. Only implement this callback when you need custom encryption.

Param dataToSend:

pointer to an array of bytes that need to be encrypted. Must not be freed. Encrypt the data in place in this array if the size of your encrypted data is smaller than indicated in the sizeOfData parameter. Otherwise allocate your own memory and replace the pointer to point to your own allocated memory. In this case you need to take care of freeing the memory.

Param sizeOfData:

size in byte of the dataToSend array.

Important

The original memory pointed to by dataToSend must not be freed regardless of whether or not you replace the pointer!

Decryption

To decrypt incoming data, implement the callback

struct ServerLibFunctions

Server callbacks. Zero initialize and set members to functions that are to be called when the event in question happens. Every callback you use should exit quickly to avoid stalling the server. If you need any expensive activity upon receiving callbacks, consider starting the activity in a new thread and allow the callback to exit quickly.

Public Members

void (*onCustomPacketDecryptEvent)(char **dataReceived, unsigned int *dataReceivedSize)

called when a packet needs to be decrypted after it has been received.

Used to implement custom encryption of server communication. This needs to be implemented the same in the client and server, otherwise clients cannot communicate with the server. Only implement this callback when you need custom encryption.

Param dataReceived:

pointer to an array of bytes that need to be decrypted. Must not be freed. Decrypt the data in place in this array if the size of your decrypted data is smaller than indicated by the dataReceivedSize parameter. Otherwise allocate your own memory and replace the pointer to point to your own allocated memory. In this case you need to take care of freeing the memory

Param dataReceivedSize:

size in byte of the dataReceived array.

Important

The original memory pointed to by dataReceived must not be freed regardless of whether or not you replace the pointer!

Example

Example code implementing a very simple XOR custom encryption and decryption (also see the SDK examples):

 1void onCustomPacketEncryptEvent(char** dataToSend, unsigned int* sizeOfData) {
 2    unsigned int i;
 3    for (i = 0; i < *sizeOfData; i++) {
 4        (*dataToSend)[i] ^= CUSTOM_CRYPT_KEY;
 5    }
 6}
 7
 8void onCustomPacketDecryptEvent(char** dataReceived, unsigned int* dataReceivedSize) {
 9    unsigned int i;
10    for (i = 0; i < *dataReceivedSize; i++) {
11        (*dataReceived)[i] ^= CUSTOM_CRYPT_KEY;
12    }
13}