diff options
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 17d2e8ff..ae3a69a1 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -163,16 +163,40 @@ void increment_nonce(uint8_t *nonce) | |||
163 | { | 163 | { |
164 | uint32_t i; | 164 | uint32_t i; |
165 | 165 | ||
166 | for (i = 0; i < crypto_box_NONCEBYTES; ++i) { | 166 | for (i = crypto_box_NONCEBYTES; i != 0; --i) { |
167 | ++nonce[i]; | 167 | ++nonce[i - 1]; |
168 | 168 | ||
169 | if (nonce[i] != 0) | 169 | if (nonce[i - 1] != 0) |
170 | break; | 170 | break; |
171 | } | 171 | } |
172 | } | 172 | } |
173 | /* increment the given nonce by num */ | ||
174 | void increment_nonce_number(uint8_t *nonce, uint32_t num) | ||
175 | { | ||
176 | uint32_t num1, num2; | ||
177 | memcpy(&num1, nonce + (crypto_box_NONCEBYTES - sizeof(num1)), sizeof(num1)); | ||
178 | num1 = ntohl(num1); | ||
179 | num2 = num + num1; | ||
180 | |||
181 | if (num2 < num1) { | ||
182 | uint32_t i; | ||
183 | |||
184 | for (i = crypto_box_NONCEBYTES - sizeof(num1); i != 0; --i) { | ||
185 | ++nonce[i - 1]; | ||
186 | |||
187 | if (nonce[i - 1] != 0) | ||
188 | break; | ||
189 | } | ||
190 | } | ||
191 | |||
192 | num2 = htonl(num2); | ||
193 | memcpy(nonce + (crypto_box_NONCEBYTES - sizeof(num2)), &num2, sizeof(num2)); | ||
194 | } | ||
173 | 195 | ||
174 | #if crypto_box_NONCEBYTES != crypto_secretbox_NONCEBYTES | 196 | #if crypto_box_NONCEBYTES != crypto_secretbox_NONCEBYTES |
175 | /*if they no longer equal each other, this function must be split into two.*/ | 197 | /*if they no longer equal each other, this function and the previous ones |
198 | *must be split into two. | ||
199 | */ | ||
176 | #error random_nonce(): crypto_box_NONCEBYTES must equal crypto_secretbox_NONCEBYTES. | 200 | #error random_nonce(): crypto_box_NONCEBYTES must equal crypto_secretbox_NONCEBYTES. |
177 | #endif | 201 | #endif |
178 | /* Fill the given nonce with random bytes. */ | 202 | /* Fill the given nonce with random bytes. */ |