summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/net_crypto.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 9b0f0443..a37d99da 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -125,12 +125,13 @@ int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
125 125
126int encrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *plain, uint32_t length, uint8_t *encrypted) 126int encrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *plain, uint32_t length, uint8_t *encrypted)
127{ 127{
128 if (length + crypto_secretbox_MACBYTES > MAX_DATA_SIZE || length == 0) 128 if (length == 0)
129 return -1; 129 return -1;
130 130
131 uint8_t temp_plain[MAX_DATA_SIZE + crypto_secretbox_ZEROBYTES] = {0}; 131 uint8_t temp_plain[length + crypto_secretbox_ZEROBYTES];
132 uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_secretbox_BOXZEROBYTES]; 132 uint8_t temp_encrypted[length + crypto_secretbox_MACBYTES + crypto_secretbox_BOXZEROBYTES];
133 133
134 memset(temp_plain, 0, crypto_secretbox_ZEROBYTES);
134 memcpy(temp_plain + crypto_secretbox_ZEROBYTES, plain, length); // Pad the message with 32 0 bytes. 135 memcpy(temp_plain + crypto_secretbox_ZEROBYTES, plain, length); // Pad the message with 32 0 bytes.
135 136
136 crypto_secretbox(temp_encrypted, temp_plain, length + crypto_secretbox_ZEROBYTES, nonce, secret_key); 137 crypto_secretbox(temp_encrypted, temp_plain, length + crypto_secretbox_ZEROBYTES, nonce, secret_key);
@@ -141,12 +142,13 @@ int encrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *plain,
141 142
142int decrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *encrypted, uint32_t length, uint8_t *plain) 143int decrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *encrypted, uint32_t length, uint8_t *plain)
143{ 144{
144 if (length > MAX_DATA_SIZE || length <= crypto_secretbox_BOXZEROBYTES) 145 if (length <= crypto_secretbox_BOXZEROBYTES)
145 return -1; 146 return -1;
146 147
147 uint8_t temp_plain[MAX_DATA_SIZE + crypto_secretbox_ZEROBYTES]; 148 uint8_t temp_plain[length + crypto_secretbox_ZEROBYTES];
148 uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_secretbox_BOXZEROBYTES] = {0}; 149 uint8_t temp_encrypted[length + crypto_secretbox_BOXZEROBYTES];
149 150
151 memset(temp_plain, 0, crypto_secretbox_BOXZEROBYTES);
150 memcpy(temp_encrypted + crypto_secretbox_BOXZEROBYTES, encrypted, length); // Pad the message with 16 0 bytes. 152 memcpy(temp_encrypted + crypto_secretbox_BOXZEROBYTES, encrypted, length); // Pad the message with 16 0 bytes.
151 153
152 if (crypto_secretbox_open(temp_plain, temp_encrypted, length + crypto_secretbox_BOXZEROBYTES, nonce, secret_key) == -1) 154 if (crypto_secretbox_open(temp_plain, temp_encrypted, length + crypto_secretbox_BOXZEROBYTES, nonce, secret_key) == -1)