diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-08-26 17:58:27 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-08-26 19:15:44 +0000 |
commit | acc19a202faad8fb6cbc576b8f4d906ee80e6e16 (patch) | |
tree | 6244c8a8532a4e653f5cbe65ad2b71c3c2363219 /toxcore/crypto_core.c | |
parent | 473cde24d81526bb23ae1c535be590f53078f2f4 (diff) |
Format crypto_core.c.
Changes:
* 100 columns maximum (not strict, can be a bit more sometimes).
* No space after cast.
Diffstat (limited to 'toxcore/crypto_core.c')
-rw-r--r-- | toxcore/crypto_core.c | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index 975a3b97..1fd62866 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c | |||
@@ -129,13 +129,14 @@ bool public_key_valid(const uint8_t *public_key) | |||
129 | * encrypt/decrypt operation. | 129 | * encrypt/decrypt operation. |
130 | * shared_key has to be crypto_box_BEFORENMBYTES bytes long. | 130 | * shared_key has to be crypto_box_BEFORENMBYTES bytes long. |
131 | */ | 131 | */ |
132 | int32_t encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key, uint8_t *shared_key) | 132 | int32_t encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key, |
133 | uint8_t *shared_key) | ||
133 | { | 134 | { |
134 | return crypto_box_beforenm(shared_key, public_key, secret_key); | 135 | return crypto_box_beforenm(shared_key, public_key, secret_key); |
135 | } | 136 | } |
136 | 137 | ||
137 | int32_t encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce, const uint8_t *plain, size_t length, | 138 | int32_t encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce, |
138 | uint8_t *encrypted) | 139 | const uint8_t *plain, size_t length, uint8_t *encrypted) |
139 | { | 140 | { |
140 | if (length == 0 || !secret_key || !nonce || !plain || !encrypted) { | 141 | if (length == 0 || !secret_key || !nonce || !plain || !encrypted) { |
141 | return -1; | 142 | return -1; |
@@ -145,19 +146,21 @@ int32_t encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce, | |||
145 | VLA(uint8_t, temp_encrypted, length + crypto_box_MACBYTES + crypto_box_BOXZEROBYTES); | 146 | VLA(uint8_t, temp_encrypted, length + crypto_box_MACBYTES + crypto_box_BOXZEROBYTES); |
146 | 147 | ||
147 | memset(temp_plain, 0, crypto_box_ZEROBYTES); | 148 | memset(temp_plain, 0, crypto_box_ZEROBYTES); |
148 | memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); // Pad the message with 32 0 bytes. | 149 | // Pad the message with 32 0 bytes. |
150 | memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); | ||
149 | 151 | ||
150 | if (crypto_box_afternm(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, secret_key) != 0) { | 152 | if (crypto_box_afternm(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, |
153 | secret_key) != 0) { | ||
151 | return -1; | 154 | return -1; |
152 | } | 155 | } |
153 | 156 | ||
154 | /* Unpad the encrypted message. */ | 157 | // Unpad the encrypted message. |
155 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES); | 158 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES); |
156 | return length + crypto_box_MACBYTES; | 159 | return length + crypto_box_MACBYTES; |
157 | } | 160 | } |
158 | 161 | ||
159 | int32_t decrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce, const uint8_t *encrypted, size_t length, | 162 | int32_t decrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce, |
160 | uint8_t *plain) | 163 | const uint8_t *encrypted, size_t length, uint8_t *plain) |
161 | { | 164 | { |
162 | if (length <= crypto_box_BOXZEROBYTES || !secret_key || !nonce || !encrypted || !plain) { | 165 | if (length <= crypto_box_BOXZEROBYTES || !secret_key || !nonce || !encrypted || !plain) { |
163 | return -1; | 166 | return -1; |
@@ -167,9 +170,11 @@ int32_t decrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce, | |||
167 | VLA(uint8_t, temp_encrypted, length + crypto_box_BOXZEROBYTES); | 170 | VLA(uint8_t, temp_encrypted, length + crypto_box_BOXZEROBYTES); |
168 | 171 | ||
169 | memset(temp_encrypted, 0, crypto_box_BOXZEROBYTES); | 172 | memset(temp_encrypted, 0, crypto_box_BOXZEROBYTES); |
170 | memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); // Pad the message with 16 0 bytes. | 173 | // Pad the message with 16 0 bytes. |
174 | memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); | ||
171 | 175 | ||
172 | if (crypto_box_open_afternm(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, nonce, secret_key) != 0) { | 176 | if (crypto_box_open_afternm(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, nonce, |
177 | secret_key) != 0) { | ||
173 | return -1; | 178 | return -1; |
174 | } | 179 | } |
175 | 180 | ||
@@ -205,12 +210,13 @@ int32_t decrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const | |||
205 | return ret; | 210 | return ret; |
206 | } | 211 | } |
207 | 212 | ||
208 | |||
209 | /* Increment the given nonce by 1. */ | 213 | /* Increment the given nonce by 1. */ |
210 | void increment_nonce(uint8_t *nonce) | 214 | void increment_nonce(uint8_t *nonce) |
211 | { | 215 | { |
212 | /* TODO(irungentoo): use increment_nonce_number(nonce, 1) or sodium_increment (change to little endian) | 216 | /* TODO(irungentoo): use increment_nonce_number(nonce, 1) or |
213 | * NOTE don't use breaks inside this loop | 217 | * sodium_increment (change to little endian). |
218 | * | ||
219 | * NOTE don't use breaks inside this loop. | ||
214 | * In particular, make sure, as far as possible, | 220 | * In particular, make sure, as far as possible, |
215 | * that loop bounds and their potential underflow or overflow | 221 | * that loop bounds and their potential underflow or overflow |
216 | * are independent of user-controlled input (you may have heard of the Heartbleed bug). | 222 | * are independent of user-controlled input (you may have heard of the Heartbleed bug). |
@@ -219,8 +225,8 @@ void increment_nonce(uint8_t *nonce) | |||
219 | uint_fast16_t carry = 1U; | 225 | uint_fast16_t carry = 1U; |
220 | 226 | ||
221 | for (; i != 0; --i) { | 227 | for (; i != 0; --i) { |
222 | carry += (uint_fast16_t) nonce[i - 1]; | 228 | carry += (uint_fast16_t)nonce[i - 1]; |
223 | nonce[i - 1] = (uint8_t) carry; | 229 | nonce[i - 1] = (uint8_t)carry; |
224 | carry >>= 8; | 230 | carry >>= 8; |
225 | } | 231 | } |
226 | } | 232 | } |
@@ -228,11 +234,10 @@ void increment_nonce(uint8_t *nonce) | |||
228 | static uint32_t host_to_network(uint32_t x) | 234 | static uint32_t host_to_network(uint32_t x) |
229 | { | 235 | { |
230 | #if !defined(BYTE_ORDER) || BYTE_ORDER == LITTLE_ENDIAN | 236 | #if !defined(BYTE_ORDER) || BYTE_ORDER == LITTLE_ENDIAN |
231 | return | 237 | return ((x >> 24) & 0x000000FF) | // move byte 3 to byte 0 |
232 | ((x >> 24) & 0x000000FF) | // move byte 3 to byte 0 | 238 | ((x >> 8) & 0x0000FF00) | // move byte 2 to byte 1 |
233 | ((x >> 8) & 0x0000FF00) | // move byte 2 to byte 1 | 239 | ((x << 8) & 0x00FF0000) | // move byte 1 to byte 2 |
234 | ((x << 8) & 0x00FF0000) | // move byte 1 to byte 2 | 240 | ((x << 24) & 0xFF000000); // move byte 0 to byte 3 |
235 | ((x << 24) & 0xFF000000); // move byte 0 to byte 3 | ||
236 | #else | 241 | #else |
237 | return x; | 242 | return x; |
238 | #endif | 243 | #endif |
@@ -247,7 +252,7 @@ void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num) | |||
247 | * are independent of user-controlled input (you may have heard of the Heartbleed bug). | 252 | * are independent of user-controlled input (you may have heard of the Heartbleed bug). |
248 | */ | 253 | */ |
249 | const uint32_t big_endian_num = host_to_network(host_order_num); | 254 | const uint32_t big_endian_num = host_to_network(host_order_num); |
250 | const uint8_t *const num_vec = (const uint8_t *) &big_endian_num; | 255 | const uint8_t *const num_vec = (const uint8_t *)&big_endian_num; |
251 | uint8_t num_as_nonce[crypto_box_NONCEBYTES] = {0}; | 256 | uint8_t num_as_nonce[crypto_box_NONCEBYTES] = {0}; |
252 | num_as_nonce[crypto_box_NONCEBYTES - 4] = num_vec[0]; | 257 | num_as_nonce[crypto_box_NONCEBYTES - 4] = num_vec[0]; |
253 | num_as_nonce[crypto_box_NONCEBYTES - 3] = num_vec[1]; | 258 | num_as_nonce[crypto_box_NONCEBYTES - 3] = num_vec[1]; |
@@ -258,7 +263,7 @@ void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num) | |||
258 | uint_fast16_t carry = 0U; | 263 | uint_fast16_t carry = 0U; |
259 | 264 | ||
260 | for (; i != 0; --i) { | 265 | for (; i != 0; --i) { |
261 | carry += (uint_fast16_t) nonce[i - 1] + (uint_fast16_t) num_as_nonce[i - 1]; | 266 | carry += (uint_fast16_t)nonce[i - 1] + (uint_fast16_t)num_as_nonce[i - 1]; |
262 | nonce[i - 1] = (uint8_t)carry; | 267 | nonce[i - 1] = (uint8_t)carry; |
263 | carry >>= 8; | 268 | carry >>= 8; |
264 | } | 269 | } |