diff options
author | irungentoo <irungentoo@gmail.com> | 2014-04-18 20:36:52 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-04-18 20:36:52 -0400 |
commit | 9f6b17de55a31cf7aeb8370345849c30683f0c0c (patch) | |
tree | 7a6822495739552bd988e31f1c623cb6e385e426 /toxcore | |
parent | 47fb2e8067429c3ad142e9375ce2c5469218ad96 (diff) |
Added function to increment nonce by specified number.
Nonces now behave like big endian numbers.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/net_crypto.c | 32 | ||||
-rw-r--r-- | toxcore/net_crypto.h | 3 |
2 files changed, 31 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. */ |
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index da776527..3c70c4d3 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h | |||
@@ -135,6 +135,9 @@ int decrypt_data_symmetric(uint8_t *secret_key, uint8_t *nonce, uint8_t *encrypt | |||
135 | /* Increment the given nonce by 1. */ | 135 | /* Increment the given nonce by 1. */ |
136 | void increment_nonce(uint8_t *nonce); | 136 | void increment_nonce(uint8_t *nonce); |
137 | 137 | ||
138 | /* increment the given nonce by num */ | ||
139 | void increment_nonce_number(uint8_t *nonce, uint32_t num); | ||
140 | |||
138 | /* Fill the given nonce with random bytes. */ | 141 | /* Fill the given nonce with random bytes. */ |
139 | void random_nonce(uint8_t *nonce); | 142 | void random_nonce(uint8_t *nonce); |
140 | 143 | ||