diff options
Diffstat (limited to 'toxcore/crypto_core.c')
-rw-r--r-- | toxcore/crypto_core.c | 95 |
1 files changed, 89 insertions, 6 deletions
diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index a71b3cb8..d4f7c562 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c | |||
@@ -29,8 +29,53 @@ | |||
29 | 29 | ||
30 | #include "crypto_core.h" | 30 | #include "crypto_core.h" |
31 | 31 | ||
32 | #if crypto_box_PUBLICKEYBYTES != 32 | 32 | #ifndef VANILLA_NACL |
33 | #error crypto_box_PUBLICKEYBYTES is required to be 32 bytes for public_key_cmp to work, | 33 | /* We use libsodium by default. */ |
34 | #include <sodium.h> | ||
35 | #else | ||
36 | #include <crypto_box.h> | ||
37 | #include <crypto_hash_sha256.h> | ||
38 | #include <crypto_hash_sha512.h> | ||
39 | #include <crypto_scalarmult_curve25519.h> | ||
40 | #include <crypto_verify_16.h> | ||
41 | #include <crypto_verify_32.h> | ||
42 | #include <randombytes.h> | ||
43 | #define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) | ||
44 | /* I know */ | ||
45 | #define sodium_memcmp(a, b, c) memcmp(a, b, c) | ||
46 | #define sodium_memzero(a, c) memset(a, 0, c) | ||
47 | #endif | ||
48 | |||
49 | #if CRYPTO_PUBLIC_KEY_SIZE != crypto_box_PUBLICKEYBYTES | ||
50 | #error CRYPTO_PUBLIC_KEY_SIZE should be equal to crypto_box_PUBLICKEYBYTES | ||
51 | #endif | ||
52 | |||
53 | #if CRYPTO_SECRET_KEY_SIZE != crypto_box_SECRETKEYBYTES | ||
54 | #error CRYPTO_SECRET_KEY_SIZE should be equal to crypto_box_SECRETKEYBYTES | ||
55 | #endif | ||
56 | |||
57 | #if CRYPTO_SHARED_KEY_SIZE != crypto_box_BEFORENMBYTES | ||
58 | #error CRYPTO_SHARED_KEY_SIZE should be equal to crypto_box_BEFORENMBYTES | ||
59 | #endif | ||
60 | |||
61 | #if CRYPTO_SYMMETRIC_KEY_SIZE != crypto_box_BEFORENMBYTES | ||
62 | #error CRYPTO_SYMMETRIC_KEY_SIZE should be equal to crypto_box_BEFORENMBYTES | ||
63 | #endif | ||
64 | |||
65 | #if CRYPTO_MAC_SIZE != crypto_box_MACBYTES | ||
66 | #error CRYPTO_MAC_SIZE should be equal to crypto_box_MACBYTES | ||
67 | #endif | ||
68 | |||
69 | #if CRYPTO_NONCE_SIZE != crypto_box_NONCEBYTES | ||
70 | #error CRYPTO_NONCE_SIZE should be equal to crypto_box_NONCEBYTES | ||
71 | #endif | ||
72 | |||
73 | #if CRYPTO_SHA256_SIZE != crypto_hash_sha256_BYTES | ||
74 | #error CRYPTO_SHA256_SIZE should be equal to crypto_hash_sha256_BYTES | ||
75 | #endif | ||
76 | |||
77 | #if CRYPTO_SHA512_SIZE != crypto_hash_sha512_BYTES | ||
78 | #error CRYPTO_SHA512_SIZE should be equal to crypto_hash_sha512_BYTES | ||
34 | #endif | 79 | #endif |
35 | 80 | ||
36 | /* compare 2 public keys of length crypto_box_PUBLICKEYBYTES, not vulnerable to timing attacks. | 81 | /* compare 2 public keys of length crypto_box_PUBLICKEYBYTES, not vulnerable to timing attacks. |
@@ -38,6 +83,9 @@ | |||
38 | return -1 if they are not. */ | 83 | return -1 if they are not. */ |
39 | int public_key_cmp(const uint8_t *pk1, const uint8_t *pk2) | 84 | int public_key_cmp(const uint8_t *pk1, const uint8_t *pk2) |
40 | { | 85 | { |
86 | #if crypto_box_PUBLICKEYBYTES != 32 | ||
87 | #error crypto_box_PUBLICKEYBYTES is required to be 32 bytes for public_key_cmp to work, | ||
88 | #endif | ||
41 | return crypto_verify_32(pk1, pk2); | 89 | return crypto_verify_32(pk1, pk2); |
42 | } | 90 | } |
43 | 91 | ||
@@ -135,7 +183,7 @@ int encrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uin | |||
135 | uint8_t k[crypto_box_BEFORENMBYTES]; | 183 | uint8_t k[crypto_box_BEFORENMBYTES]; |
136 | encrypt_precompute(public_key, secret_key, k); | 184 | encrypt_precompute(public_key, secret_key, k); |
137 | int ret = encrypt_data_symmetric(k, nonce, plain, length, encrypted); | 185 | int ret = encrypt_data_symmetric(k, nonce, plain, length, encrypted); |
138 | sodium_memzero(k, sizeof k); | 186 | crypto_memzero(k, sizeof k); |
139 | return ret; | 187 | return ret; |
140 | } | 188 | } |
141 | 189 | ||
@@ -149,7 +197,7 @@ int decrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uin | |||
149 | uint8_t k[crypto_box_BEFORENMBYTES]; | 197 | uint8_t k[crypto_box_BEFORENMBYTES]; |
150 | encrypt_precompute(public_key, secret_key, k); | 198 | encrypt_precompute(public_key, secret_key, k); |
151 | int ret = decrypt_data_symmetric(k, nonce, encrypted, length, plain); | 199 | int ret = decrypt_data_symmetric(k, nonce, encrypted, length, plain); |
152 | sodium_memzero(k, sizeof k); | 200 | crypto_memzero(k, sizeof k); |
153 | return ret; | 201 | return ret; |
154 | } | 202 | } |
155 | 203 | ||
@@ -204,8 +252,43 @@ void random_nonce(uint8_t *nonce) | |||
204 | randombytes(nonce, crypto_box_NONCEBYTES); | 252 | randombytes(nonce, crypto_box_NONCEBYTES); |
205 | } | 253 | } |
206 | 254 | ||
207 | /* Fill a key crypto_box_KEYBYTES big with random bytes */ | 255 | /* Fill a key CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes */ |
208 | void new_symmetric_key(uint8_t *key) | 256 | void new_symmetric_key(uint8_t *key) |
209 | { | 257 | { |
210 | randombytes(key, crypto_box_KEYBYTES); | 258 | randombytes(key, CRYPTO_SYMMETRIC_KEY_SIZE); |
259 | } | ||
260 | |||
261 | int32_t crypto_new_keypair(uint8_t *public_key, uint8_t *secret_key) | ||
262 | { | ||
263 | return crypto_box_keypair(public_key, secret_key); | ||
264 | } | ||
265 | |||
266 | void crypto_derive_public_key(uint8_t *public_key, uint8_t *secret_key) | ||
267 | { | ||
268 | crypto_scalarmult_curve25519_base(public_key, secret_key); | ||
269 | } | ||
270 | |||
271 | void crypto_sha256(uint8_t *hash, const uint8_t *data, size_t length) | ||
272 | { | ||
273 | crypto_hash_sha256(hash, data, length); | ||
274 | } | ||
275 | |||
276 | void crypto_sha512(uint8_t *hash, const uint8_t *data, size_t length) | ||
277 | { | ||
278 | crypto_hash_sha512(hash, data, length); | ||
279 | } | ||
280 | |||
281 | void crypto_memzero(void *data, size_t length) | ||
282 | { | ||
283 | sodium_memzero(data, length); | ||
284 | } | ||
285 | |||
286 | int32_t crypto_memcmp(const void *p1, const void *p2, size_t length) | ||
287 | { | ||
288 | return sodium_memcmp(p1, p2, length); | ||
289 | } | ||
290 | |||
291 | void random_bytes(uint8_t *data, size_t length) | ||
292 | { | ||
293 | randombytes(data, length); | ||
211 | } | 294 | } |