diff options
author | iphydf <iphydf@users.noreply.github.com> | 2016-12-19 02:47:42 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2016-12-22 10:26:59 +0000 |
commit | ce29c8e7ec91d95167b2dea3aee9fd1ae1aac254 (patch) | |
tree | a288df55c44e8edf816e6abbde19a70faef73394 /toxcore/crypto_core.api.h | |
parent | 7122d2e862e028a730478d88cd61557fbed16ebf (diff) |
Wrap all sodium/nacl functions in crypto_core.c.
Diffstat (limited to 'toxcore/crypto_core.api.h')
-rw-r--r-- | toxcore/crypto_core.api.h | 83 |
1 files changed, 60 insertions, 23 deletions
diff --git a/toxcore/crypto_core.api.h b/toxcore/crypto_core.api.h index 0c72471c..78a17280 100644 --- a/toxcore/crypto_core.api.h +++ b/toxcore/crypto_core.api.h | |||
@@ -25,29 +25,59 @@ | |||
25 | #define CORE_CRYPTO_H | 25 | #define CORE_CRYPTO_H |
26 | 26 | ||
27 | #include "network.h" | 27 | #include "network.h" |
28 | %} | ||
28 | 29 | ||
29 | #ifndef VANILLA_NACL | 30 | /** |
30 | /* We use libsodium by default. */ | 31 | * The number of bytes in a Tox public key. |
31 | #include <sodium.h> | 32 | */ |
32 | #else | 33 | const CRYPTO_PUBLIC_KEY_SIZE = 32; |
33 | #include <crypto_box.h> | ||
34 | #include <crypto_hash_sha256.h> | ||
35 | #include <crypto_hash_sha512.h> | ||
36 | #include <crypto_scalarmult_curve25519.h> | ||
37 | #include <crypto_verify_16.h> | ||
38 | #include <crypto_verify_32.h> | ||
39 | #include <randombytes.h> | ||
40 | #define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) | ||
41 | /* I know */ | ||
42 | #define sodium_memcmp(a, b, c) memcmp(a, b, c) | ||
43 | #define sodium_memzero(a, c) memset(a, 0, c) | ||
44 | #endif | ||
45 | 34 | ||
46 | #define crypto_box_KEYBYTES (crypto_box_BEFORENMBYTES) | 35 | /** |
47 | %} | 36 | * The number of bytes in a Tox secret key. |
37 | */ | ||
38 | const CRYPTO_SECRET_KEY_SIZE = 32; | ||
39 | |||
40 | /** | ||
41 | * The number of bytes in a shared key computed from public and secret key. | ||
42 | */ | ||
43 | const CRYPTO_SHARED_KEY_SIZE = 32; | ||
44 | |||
45 | /** | ||
46 | * The number of bytes in a random symmetric key. | ||
47 | */ | ||
48 | const CRYPTO_SYMMETRIC_KEY_SIZE = CRYPTO_SHARED_KEY_SIZE; | ||
49 | |||
50 | /** | ||
51 | * The number of bytes needed for the MAC (message authentication code) in an | ||
52 | * encrypted message. | ||
53 | */ | ||
54 | const CRYPTO_MAC_SIZE = 16; | ||
48 | 55 | ||
49 | /** | 56 | /** |
50 | * compare 2 public keys of length crypto_box_PUBLICKEYBYTES, not vulnerable to timing attacks. | 57 | * The number of bytes in a nonce used for encryption/decryption. |
58 | */ | ||
59 | const CRYPTO_NONCE_SIZE = 24; | ||
60 | |||
61 | /** | ||
62 | * The number of bytes in a SHA256 hash. | ||
63 | */ | ||
64 | const CRYPTO_SHA256_SIZE = 32; | ||
65 | |||
66 | /** | ||
67 | * The number of bytes in a SHA512 hash. | ||
68 | */ | ||
69 | const CRYPTO_SHA512_SIZE = 64; | ||
70 | |||
71 | static int32_t crypto_memcmp(const void *p1, const void *p2, size_t length); | ||
72 | static void crypto_memzero(void *data, size_t length); | ||
73 | |||
74 | static void crypto_sha256(uint8_t *hash, const uint8_t[length] data); | ||
75 | static void crypto_sha512(uint8_t *hash, const uint8_t[length] data); | ||
76 | |||
77 | static void crypto_derive_public_key(uint8_t *public_key, uint8_t *secret_key); | ||
78 | |||
79 | /** | ||
80 | * compare 2 public keys of length CRYPTO_PUBLIC_KEY_SIZE, not vulnerable to timing attacks. | ||
51 | * returns 0 if both mem locations of length are equal, | 81 | * returns 0 if both mem locations of length are equal, |
52 | * return -1 if they are not. | 82 | * return -1 if they are not. |
53 | */ | 83 | */ |
@@ -64,7 +94,7 @@ static uint32_t random_int(); | |||
64 | static uint64_t random_64b(); | 94 | static uint64_t random_64b(); |
65 | 95 | ||
66 | /** | 96 | /** |
67 | * Check if a Tox public key crypto_box_PUBLICKEYBYTES is valid or not. | 97 | * Check if a Tox public key CRYPTO_PUBLIC_KEY_SIZE is valid or not. |
68 | * This should only be used for input validation. | 98 | * This should only be used for input validation. |
69 | * | 99 | * |
70 | * return 0 if it isn't. | 100 | * return 0 if it isn't. |
@@ -72,6 +102,8 @@ static uint64_t random_64b(); | |||
72 | */ | 102 | */ |
73 | static int32_t public_key_valid(const uint8_t *public_key); | 103 | static int32_t public_key_valid(const uint8_t *public_key); |
74 | 104 | ||
105 | static int32_t crypto_new_keypair(uint8_t *public_key, uint8_t *secret_key); | ||
106 | |||
75 | /** | 107 | /** |
76 | * Encrypts plain of length length to encrypted of length + 16 using the | 108 | * Encrypts plain of length length to encrypted of length + 16 using the |
77 | * public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce. | 109 | * public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce. |
@@ -105,7 +137,7 @@ static int32_t encrypt_precompute( | |||
105 | 137 | ||
106 | /** | 138 | /** |
107 | * Encrypts plain of length length to encrypted of length + 16 using a | 139 | * Encrypts plain of length length to encrypted of length + 16 using a |
108 | * secret key crypto_box_KEYBYTES big and a 24 byte nonce. | 140 | * secret key CRYPTO_SYMMETRIC_KEY_SIZE big and a 24 byte nonce. |
109 | * | 141 | * |
110 | * return -1 if there was a problem. | 142 | * return -1 if there was a problem. |
111 | * return length of encrypted data if everything was fine. | 143 | * return length of encrypted data if everything was fine. |
@@ -116,7 +148,7 @@ static int32_t encrypt_data_symmetric( | |||
116 | 148 | ||
117 | /** | 149 | /** |
118 | * Decrypts encrypted of length length to plain of length length - 16 using a | 150 | * Decrypts encrypted of length length to plain of length length - 16 using a |
119 | * secret key crypto_box_KEYBYTES big and a 24 byte nonce. | 151 | * secret key CRYPTO_SYMMETRIC_KEY_SIZE big and a 24 byte nonce. |
120 | * | 152 | * |
121 | * return -1 if there was a problem (decryption failed). | 153 | * return -1 if there was a problem (decryption failed). |
122 | * return length of plain data if everything was fine. | 154 | * return length of plain data if everything was fine. |
@@ -141,10 +173,15 @@ static void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num); | |||
141 | static void random_nonce(uint8_t *nonce); | 173 | static void random_nonce(uint8_t *nonce); |
142 | 174 | ||
143 | /** | 175 | /** |
144 | * Fill a key crypto_box_KEYBYTES big with random bytes. | 176 | * Fill a key CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes. |
145 | */ | 177 | */ |
146 | static void new_symmetric_key(uint8_t *key); | 178 | static void new_symmetric_key(uint8_t *key); |
147 | 179 | ||
180 | /** | ||
181 | * Fill an array of bytes with random values. | ||
182 | */ | ||
183 | static void random_bytes(uint8_t[length] bytes); | ||
184 | |||
148 | %{ | 185 | %{ |
149 | #endif | 186 | #endif |
150 | %} | 187 | %} |