Added data packet padding to toxcore.

Data sent as lossless or lossy is now padded with:
((MAX_CRYPTO_DATA_SIZE - data_length) % CRYPTO_MAX_PADDING) bytes
in order to reduce the possibility of length related attacks.

I set CRYPTO_MAX_PADDING to 8 but it can be changed anytime without
breaking network compatibility between tox cores.
This commit is contained in:
irungentoo 2014-06-04 13:28:56 -04:00
parent de5a33e852
commit b44b58cae4
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
2 changed files with 9 additions and 2 deletions

View File

@ -752,12 +752,17 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *dat
static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint32_t buffer_start, uint32_t num,
uint8_t *data, uint32_t length)
{
if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
return -1;
num = htonl(num);
buffer_start = htonl(buffer_start);
uint8_t packet[sizeof(uint32_t) + sizeof(uint32_t) + length];
uint16_t padding_length = (MAX_CRYPTO_DATA_SIZE - length) % CRYPTO_MAX_PADDING;
uint8_t packet[sizeof(uint32_t) + sizeof(uint32_t) + padding_length + length];
memcpy(packet, &buffer_start, sizeof(uint32_t));
memcpy(packet + sizeof(uint32_t), &num, sizeof(uint32_t));
memcpy(packet + (sizeof(uint32_t) * 2), data, length);
memset(packet + (sizeof(uint32_t) * 2), 0, padding_length);
memcpy(packet + (sizeof(uint32_t) * 2) + padding_length, data, length);
return send_data_packet(c, crypt_connection_id, packet, sizeof(packet));
}

View File

@ -76,6 +76,8 @@
#define PACKET_ID_LOSSY_RANGE_START 192
#define PACKET_ID_LOSSY_RANGE_SIZE 63
#define CRYPTO_MAX_PADDING 8 /* All packets will be padded a number of bytes based on this number. */
typedef struct {
uint64_t time;
uint16_t length;