diff options
Diffstat (limited to 'toxcore/crypto_core.h')
-rw-r--r-- | toxcore/crypto_core.h | 100 |
1 files changed, 78 insertions, 22 deletions
diff --git a/toxcore/crypto_core.h b/toxcore/crypto_core.h index 5a1b153a..a5cea019 100644 --- a/toxcore/crypto_core.h +++ b/toxcore/crypto_core.h | |||
@@ -25,27 +25,75 @@ | |||
25 | 25 | ||
26 | #include "network.h" | 26 | #include "network.h" |
27 | 27 | ||
28 | #ifndef VANILLA_NACL | 28 | /** |
29 | /* We use libsodium by default. */ | 29 | * The number of bytes in a Tox public key. |
30 | #include <sodium.h> | 30 | */ |
31 | #else | 31 | #define CRYPTO_PUBLIC_KEY_SIZE 32 |
32 | #include <crypto_box.h> | 32 | |
33 | #include <crypto_hash_sha256.h> | 33 | uint32_t crypto_public_key_size(void); |
34 | #include <crypto_hash_sha512.h> | 34 | |
35 | #include <crypto_scalarmult_curve25519.h> | 35 | /** |
36 | #include <crypto_verify_16.h> | 36 | * The number of bytes in a Tox secret key. |
37 | #include <crypto_verify_32.h> | 37 | */ |
38 | #include <randombytes.h> | 38 | #define CRYPTO_SECRET_KEY_SIZE 32 |
39 | #define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) | 39 | |
40 | /* I know */ | 40 | uint32_t crypto_secret_key_size(void); |
41 | #define sodium_memcmp(a, b, c) memcmp(a, b, c) | 41 | |
42 | #define sodium_memzero(a, c) memset(a, 0, c) | 42 | /** |
43 | #endif | 43 | * The number of bytes in a shared key computed from public and secret key. |
44 | */ | ||
45 | #define CRYPTO_SHARED_KEY_SIZE 32 | ||
44 | 46 | ||
45 | #define crypto_box_KEYBYTES (crypto_box_BEFORENMBYTES) | 47 | uint32_t crypto_shared_key_size(void); |
46 | 48 | ||
47 | /** | 49 | /** |
48 | * compare 2 public keys of length crypto_box_PUBLICKEYBYTES, not vulnerable to timing attacks. | 50 | * The number of bytes in a random symmetric key. |
51 | */ | ||
52 | #define CRYPTO_SYMMETRIC_KEY_SIZE CRYPTO_SHARED_KEY_SIZE | ||
53 | |||
54 | uint32_t crypto_symmetric_key_size(void); | ||
55 | |||
56 | /** | ||
57 | * The number of bytes needed for the MAC (message authentication code) in an | ||
58 | * encrypted message. | ||
59 | */ | ||
60 | #define CRYPTO_MAC_SIZE 16 | ||
61 | |||
62 | uint32_t crypto_mac_size(void); | ||
63 | |||
64 | /** | ||
65 | * The number of bytes in a nonce used for encryption/decryption. | ||
66 | */ | ||
67 | #define CRYPTO_NONCE_SIZE 24 | ||
68 | |||
69 | uint32_t crypto_nonce_size(void); | ||
70 | |||
71 | /** | ||
72 | * The number of bytes in a SHA256 hash. | ||
73 | */ | ||
74 | #define CRYPTO_SHA256_SIZE 32 | ||
75 | |||
76 | uint32_t crypto_sha256_size(void); | ||
77 | |||
78 | /** | ||
79 | * The number of bytes in a SHA512 hash. | ||
80 | */ | ||
81 | #define CRYPTO_SHA512_SIZE 64 | ||
82 | |||
83 | uint32_t crypto_sha512_size(void); | ||
84 | |||
85 | int32_t crypto_memcmp(const void *p1, const void *p2, size_t length); | ||
86 | |||
87 | void crypto_memzero(void *data, size_t length); | ||
88 | |||
89 | void crypto_sha256(uint8_t *hash, const uint8_t *data, size_t length); | ||
90 | |||
91 | void crypto_sha512(uint8_t *hash, const uint8_t *data, size_t length); | ||
92 | |||
93 | void crypto_derive_public_key(uint8_t *public_key, uint8_t *secret_key); | ||
94 | |||
95 | /** | ||
96 | * compare 2 public keys of length CRYPTO_PUBLIC_KEY_SIZE, not vulnerable to timing attacks. | ||
49 | * returns 0 if both mem locations of length are equal, | 97 | * returns 0 if both mem locations of length are equal, |
50 | * return -1 if they are not. | 98 | * return -1 if they are not. |
51 | */ | 99 | */ |
@@ -62,7 +110,7 @@ uint32_t random_int(void); | |||
62 | uint64_t random_64b(void); | 110 | uint64_t random_64b(void); |
63 | 111 | ||
64 | /** | 112 | /** |
65 | * Check if a Tox public key crypto_box_PUBLICKEYBYTES is valid or not. | 113 | * Check if a Tox public key CRYPTO_PUBLIC_KEY_SIZE is valid or not. |
66 | * This should only be used for input validation. | 114 | * This should only be used for input validation. |
67 | * | 115 | * |
68 | * return 0 if it isn't. | 116 | * return 0 if it isn't. |
@@ -70,6 +118,8 @@ uint64_t random_64b(void); | |||
70 | */ | 118 | */ |
71 | int32_t public_key_valid(const uint8_t *public_key); | 119 | int32_t public_key_valid(const uint8_t *public_key); |
72 | 120 | ||
121 | int32_t crypto_new_keypair(uint8_t *public_key, uint8_t *secret_key); | ||
122 | |||
73 | /** | 123 | /** |
74 | * Encrypts plain of length length to encrypted of length + 16 using the | 124 | * Encrypts plain of length length to encrypted of length + 16 using the |
75 | * public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce. | 125 | * public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce. |
@@ -99,7 +149,7 @@ int32_t encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key, | |||
99 | 149 | ||
100 | /** | 150 | /** |
101 | * Encrypts plain of length length to encrypted of length + 16 using a | 151 | * Encrypts plain of length length to encrypted of length + 16 using a |
102 | * secret key crypto_box_KEYBYTES big and a 24 byte nonce. | 152 | * secret key CRYPTO_SYMMETRIC_KEY_SIZE big and a 24 byte nonce. |
103 | * | 153 | * |
104 | * return -1 if there was a problem. | 154 | * return -1 if there was a problem. |
105 | * return length of encrypted data if everything was fine. | 155 | * return length of encrypted data if everything was fine. |
@@ -109,7 +159,7 @@ int32_t encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce, | |||
109 | 159 | ||
110 | /** | 160 | /** |
111 | * Decrypts encrypted of length length to plain of length length - 16 using a | 161 | * Decrypts encrypted of length length to plain of length length - 16 using a |
112 | * secret key crypto_box_KEYBYTES big and a 24 byte nonce. | 162 | * secret key CRYPTO_SYMMETRIC_KEY_SIZE big and a 24 byte nonce. |
113 | * | 163 | * |
114 | * return -1 if there was a problem (decryption failed). | 164 | * return -1 if there was a problem (decryption failed). |
115 | * return length of plain data if everything was fine. | 165 | * return length of plain data if everything was fine. |
@@ -133,8 +183,14 @@ void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num); | |||
133 | void random_nonce(uint8_t *nonce); | 183 | void random_nonce(uint8_t *nonce); |
134 | 184 | ||
135 | /** | 185 | /** |
136 | * Fill a key crypto_box_KEYBYTES big with random bytes. | 186 | * Fill a key CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes. |
137 | */ | 187 | */ |
138 | void new_symmetric_key(uint8_t *key); | 188 | void new_symmetric_key(uint8_t *key); |
139 | 189 | ||
190 | /** | ||
191 | * Fill an array of bytes with random values. | ||
192 | */ | ||
193 | void random_bytes(uint8_t *bytes, size_t length); | ||
194 | |||
140 | #endif | 195 | #endif |
196 | |||