Coverage Report

Created: 2025-10-08 19:34

/work/toxcore/crypto_core.h
Line
Count
Source
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2016-2025 The TokTok team.
3
 * Copyright © 2013 Tox project.
4
 */
5
6
/** @file
7
 * @brief Functions for the core crypto.
8
 *
9
 * @note This code has to be perfect. We don't mess around with encryption.
10
 */
11
#ifndef C_TOXCORE_TOXCORE_CRYPTO_CORE_H
12
#define C_TOXCORE_TOXCORE_CRYPTO_CORE_H
13
14
#include <stdbool.h>
15
#include <stddef.h>
16
#include <stdint.h>
17
18
#include "attributes.h"
19
#include "mem.h"
20
#include "tox_random.h"
21
22
#ifdef __cplusplus
23
extern "C" {
24
#endif
25
26
/**
27
 * @brief The number of bytes in a signature.
28
 */
29
15.5k
#define CRYPTO_SIGNATURE_SIZE          64
30
31
/**
32
 * @brief The number of bytes in a Tox public key used for signatures.
33
 */
34
89.5k
#define CRYPTO_SIGN_PUBLIC_KEY_SIZE    32
35
36
/**
37
 * @brief The number of bytes in a Tox secret key used for signatures.
38
 */
39
449
#define CRYPTO_SIGN_SECRET_KEY_SIZE    64
40
41
/**
42
 * @brief The number of bytes in a Tox public key used for encryption.
43
 */
44
608M
#define CRYPTO_PUBLIC_KEY_SIZE         32
45
46
/**
47
 * @brief The number of bytes in a Tox secret key used for encryption.
48
 */
49
53.0k
#define CRYPTO_SECRET_KEY_SIZE         32
50
51
/**
52
 * @brief The number of bytes in a shared key computed from public and secret keys.
53
 */
54
265k
#define CRYPTO_SHARED_KEY_SIZE         32
55
56
/**
57
 * @brief The number of bytes in a symmetric key.
58
 */
59
36.8k
#define CRYPTO_SYMMETRIC_KEY_SIZE      CRYPTO_SHARED_KEY_SIZE
60
61
/**
62
 * @brief The number of bytes needed for the MAC (message authentication code) in an
63
 *   encrypted message.
64
 */
65
11.1M
#define CRYPTO_MAC_SIZE                16
66
67
/**
68
 * @brief The number of bytes in a nonce used for encryption/decryption.
69
 */
70
12.3M
#define CRYPTO_NONCE_SIZE              24
71
72
/**
73
 * @brief The number of bytes in a SHA256 hash.
74
 */
75
17.6k
#define CRYPTO_SHA256_SIZE             32
76
77
/**
78
 * @brief The number of bytes in a SHA512 hash.
79
 */
80
11.1k
#define CRYPTO_SHA512_SIZE             64
81
82
/**
83
 * @brief The number of bytes in an encryption public key used by DHT group chats.
84
 */
85
656k
#define ENC_PUBLIC_KEY_SIZE            CRYPTO_PUBLIC_KEY_SIZE
86
87
/**
88
 * @brief The number of bytes in an encryption secret key used by DHT group chats.
89
 */
90
#define ENC_SECRET_KEY_SIZE            CRYPTO_SECRET_KEY_SIZE
91
92
/**
93
 * @brief The number of bytes in a signature public key.
94
 */
95
89.5k
#define SIG_PUBLIC_KEY_SIZE            CRYPTO_SIGN_PUBLIC_KEY_SIZE
96
97
/**
98
 * @brief The number of bytes in a signature secret key.
99
 */
100
449
#define SIG_SECRET_KEY_SIZE            CRYPTO_SIGN_SECRET_KEY_SIZE
101
102
/**
103
 * @brief The number of bytes in a DHT group chat public key identifier.
104
 */
105
31.1k
#define CHAT_ID_SIZE                   SIG_PUBLIC_KEY_SIZE
106
107
/**
108
 * @brief The number of bytes in an extended public key used by DHT group chats.
109
 */
110
3.61k
#define EXT_PUBLIC_KEY_SIZE            (ENC_PUBLIC_KEY_SIZE + SIG_PUBLIC_KEY_SIZE)
111
112
/**
113
 * @brief The number of bytes in an extended secret key used by DHT group chats.
114
 */
115
#define EXT_SECRET_KEY_SIZE            (ENC_SECRET_KEY_SIZE + SIG_SECRET_KEY_SIZE)
116
117
/**
118
 * @brief The number of bytes in an HMAC authenticator.
119
 */
120
1.28M
#define CRYPTO_HMAC_SIZE               32
121
122
/**
123
 * @brief The number of bytes in an HMAC secret key.
124
 */
125
14.4k
#define CRYPTO_HMAC_KEY_SIZE           32
126
127
/**
128
 * @brief A `bzero`-like function which won't be optimised away by the compiler.
129
 *
130
 * Some compilers will inline `bzero` or `memset` if they can prove that there
131
 * will be no reads to the written data. Use this function if you want to be
132
 * sure the memory is indeed zeroed.
133
 */
134
void crypto_memzero(void *_Nonnull data, size_t length);
135
136
/**
137
 * @brief Compute a SHA256 hash (32 bytes).
138
 *
139
 * @param[out] hash The SHA256 hash of @p data will be written to this byte array.
140
 */
141
void crypto_sha256(uint8_t hash[_Nonnull CRYPTO_SHA256_SIZE], const uint8_t *_Nonnull data, size_t length);
142
143
/**
144
 * @brief Compute a SHA512 hash (64 bytes).
145
 *
146
 * @param[out] hash The SHA512 hash of @p data will be written to this byte array.
147
 */
148
void crypto_sha512(uint8_t hash[_Nonnull CRYPTO_SHA512_SIZE], const uint8_t *_Nonnull data, size_t length);
149
150
/**
151
 * @brief Compute an HMAC authenticator (32 bytes).
152
 *
153
 * @param[out] auth Resulting authenticator.
154
 * @param key Secret key, as generated by `new_hmac_key()`.
155
 */
156
void crypto_hmac(uint8_t auth[_Nonnull CRYPTO_HMAC_SIZE], const uint8_t key[_Nonnull CRYPTO_HMAC_KEY_SIZE], const uint8_t *_Nonnull data, size_t length);
157
158
/**
159
 * @brief Verify an HMAC authenticator.
160
 */
161
bool crypto_hmac_verify(const uint8_t auth[_Nonnull CRYPTO_HMAC_SIZE], const uint8_t key[_Nonnull CRYPTO_HMAC_KEY_SIZE], const uint8_t *_Nonnull data, size_t length);
162
163
/**
164
 * @brief Compare 2 public keys of length @ref CRYPTO_PUBLIC_KEY_SIZE, not vulnerable to
165
 *   timing attacks.
166
 *
167
 * @retval true if both mem locations of length are equal
168
 * @retval false if they are not
169
 */
170
bool pk_equal(const uint8_t pk1[_Nonnull CRYPTO_PUBLIC_KEY_SIZE], const uint8_t pk2[_Nonnull CRYPTO_PUBLIC_KEY_SIZE]);
171
172
/**
173
 * @brief Copy a public key from `src` to `dest`.
174
 */
175
void pk_copy(uint8_t dest[_Nonnull CRYPTO_PUBLIC_KEY_SIZE], const uint8_t src[_Nonnull CRYPTO_PUBLIC_KEY_SIZE]);
176
177
/**
178
 * @brief Compare 2 SHA512 checksums of length CRYPTO_SHA512_SIZE, not vulnerable to
179
 *   timing attacks.
180
 *
181
 * @return true if both mem locations of length are equal, false if they are not.
182
 */
183
bool crypto_sha512_eq(const uint8_t cksum1[_Nonnull CRYPTO_SHA512_SIZE], const uint8_t cksum2[_Nonnull CRYPTO_SHA512_SIZE]);
184
185
/**
186
 * @brief Compare 2 SHA256 checksums of length CRYPTO_SHA256_SIZE, not vulnerable to
187
 *   timing attacks.
188
 *
189
 * @return true if both mem locations of length are equal, false if they are not.
190
 */
191
bool crypto_sha256_eq(const uint8_t cksum1[_Nonnull CRYPTO_SHA256_SIZE], const uint8_t cksum2[_Nonnull CRYPTO_SHA256_SIZE]);
192
193
/**
194
 * @brief Shorter internal name for the RNG type.
195
 */
196
typedef Tox_Random Random;
197
198
/**
199
 * @brief Return a random 8 bit integer.
200
 */
201
uint8_t random_u08(const Random *_Nonnull rng);
202
203
/**
204
 * @brief Return a random 16 bit integer.
205
 */
206
uint16_t random_u16(const Random *_Nonnull rng);
207
208
/**
209
 * @brief Return a random 32 bit integer.
210
 */
211
uint32_t random_u32(const Random *_Nonnull rng);
212
213
/**
214
 * @brief Return a random 64 bit integer.
215
 */
216
uint64_t random_u64(const Random *_Nonnull rng);
217
218
/**
219
 * @brief Return a random 32 bit integer between 0 and upper_bound (excluded).
220
 *
221
 * This function guarantees a uniform distribution of possible outputs.
222
 */
223
uint32_t random_range_u32(const Random *_Nonnull rng, uint32_t upper_bound);
224
225
/**
226
 * @brief Cryptographically signs a message using the supplied secret key and puts the resulting signature
227
 *   in the supplied buffer.
228
 *
229
 * @param[out] signature The buffer for the resulting signature, which must have room for at
230
 *   least CRYPTO_SIGNATURE_SIZE bytes.
231
 * @param message The message being signed.
232
 * @param message_length The length in bytes of the message being signed.
233
 * @param secret_key The secret key used to create the signature. The key should be
234
 *   produced by either `create_extended_keypair` or the libsodium function `crypto_sign_keypair`.
235
 *
236
 * @retval true on success.
237
 */
238
bool crypto_signature_create(uint8_t signature[_Nonnull CRYPTO_SIGNATURE_SIZE], const uint8_t *_Nonnull message, uint64_t message_length, const uint8_t secret_key[_Nonnull SIG_SECRET_KEY_SIZE]);
239
240
/** @brief Verifies that the given signature was produced by a given message and public key.
241
 *
242
 * @param signature The signature we wish to verify.
243
 * @param message The message we wish to verify.
244
 * @param message_length The length of the message.
245
 * @param public_key The public key counterpart of the secret key that was used to
246
 *   create the signature.
247
 *
248
 * @retval true on success.
249
 */
250
bool crypto_signature_verify(const uint8_t signature[_Nonnull CRYPTO_SIGNATURE_SIZE], const uint8_t *_Nonnull message, uint64_t message_length,
251
                             const uint8_t public_key[_Nonnull SIG_PUBLIC_KEY_SIZE]);
252
253
/**
254
 * @brief Fill the given nonce with random bytes.
255
 */
256
void random_nonce(const Random *_Nonnull rng, uint8_t nonce[_Nonnull CRYPTO_NONCE_SIZE]);
257
258
/**
259
 * @brief Fill an array of bytes with random values.
260
 */
261
void random_bytes(const Random *_Nonnull rng, uint8_t *_Nonnull bytes, size_t length);
262
263
/**
264
 * @brief Check if a Tox public key CRYPTO_PUBLIC_KEY_SIZE is valid or not.
265
 *
266
 * This should only be used for input validation.
267
 *
268
 * @return false if it isn't, true if it is.
269
 */
270
bool public_key_valid(const uint8_t public_key[_Nonnull CRYPTO_PUBLIC_KEY_SIZE]);
271
272
typedef struct Extended_Public_Key {
273
    uint8_t enc[CRYPTO_PUBLIC_KEY_SIZE];
274
    uint8_t sig[CRYPTO_SIGN_PUBLIC_KEY_SIZE];
275
} Extended_Public_Key;
276
277
typedef struct Extended_Secret_Key {
278
    uint8_t enc[CRYPTO_SECRET_KEY_SIZE];
279
    uint8_t sig[CRYPTO_SIGN_SECRET_KEY_SIZE];
280
} Extended_Secret_Key;
281
282
/**
283
 * @brief Creates an extended keypair: curve25519 and ed25519 for encryption and signing
284
 *   respectively. The Encryption keys are derived from the signature keys.
285
 *
286
 * NOTE: This does *not* use Random, so any code using this will not be fuzzable.
287
 * TODO: Make it use Random.
288
 *
289
 * @param[out] pk The buffer where the public key will be stored. Must have room for EXT_PUBLIC_KEY_SIZE bytes.
290
 * @param[out] sk The buffer where the secret key will be stored. Must have room for EXT_SECRET_KEY_SIZE bytes.
291
 * @param rng The random number generator to use for the key generator seed.
292
 *
293
 * @retval true on success.
294
 */
295
bool create_extended_keypair(Extended_Public_Key *_Nonnull pk, Extended_Secret_Key *_Nonnull sk, const Random *_Nonnull rng);
296
297
/** Functions for groupchat extended keys */
298
const uint8_t *_Nonnull get_enc_key(const Extended_Public_Key *_Nonnull key);
299
const uint8_t *_Nonnull get_sig_pk(const Extended_Public_Key *_Nonnull key);
300
void set_sig_pk(Extended_Public_Key *_Nonnull key, const uint8_t *_Nonnull sig_pk);
301
const uint8_t *_Nonnull get_sig_sk(const Extended_Secret_Key *_Nonnull key);
302
const uint8_t *_Nonnull get_chat_id(const Extended_Public_Key *_Nonnull key);
303
304
/**
305
 * @brief Generate a new random keypair.
306
 *
307
 * Every call to this function is likely to generate a different keypair.
308
 */
309
int32_t crypto_new_keypair(const Random *_Nonnull rng, uint8_t public_key[_Nonnull CRYPTO_PUBLIC_KEY_SIZE], uint8_t secret_key[_Nonnull CRYPTO_SECRET_KEY_SIZE]);
310
311
/**
312
 * @brief Derive the public key from a given secret key.
313
 */
314
void crypto_derive_public_key(uint8_t public_key[_Nonnull CRYPTO_PUBLIC_KEY_SIZE], const uint8_t secret_key[_Nonnull CRYPTO_SECRET_KEY_SIZE]);
315
316
/**
317
 * @brief Encrypt message to send from secret key to public key.
318
 *
319
 * Encrypt plain text of the given length to encrypted of
320
 * `length + CRYPTO_MAC_SIZE` using the public key (@ref CRYPTO_PUBLIC_KEY_SIZE
321
 * bytes) of the receiver and the secret key of the sender and a
322
 * @ref CRYPTO_NONCE_SIZE byte nonce.
323
 *
324
 * @retval -1 if there was a problem.
325
 * @return length of encrypted data if everything was fine.
326
 */
327
int32_t encrypt_data(const Memory *_Nonnull mem, const uint8_t public_key[_Nonnull CRYPTO_PUBLIC_KEY_SIZE], const uint8_t secret_key[_Nonnull CRYPTO_SECRET_KEY_SIZE],
328
                     const uint8_t nonce[_Nonnull CRYPTO_NONCE_SIZE], const uint8_t *_Nonnull plain, size_t length, uint8_t *_Nonnull encrypted);
329
330
/**
331
 * @brief Decrypt message from public key to secret key.
332
 *
333
 * Decrypt encrypted text of the given @p length to plain text of the given
334
 * `length - CRYPTO_MAC_SIZE` using the public key (@ref CRYPTO_PUBLIC_KEY_SIZE
335
 * bytes) of the sender, the secret key of the receiver and a
336
 * @ref CRYPTO_NONCE_SIZE byte nonce.
337
 *
338
 * @retval -1 if there was a problem (decryption failed).
339
 * @return length of plain text data if everything was fine.
340
 */
341
int32_t decrypt_data(const Memory *_Nonnull mem, const uint8_t public_key[_Nonnull CRYPTO_PUBLIC_KEY_SIZE], const uint8_t secret_key[_Nonnull CRYPTO_SECRET_KEY_SIZE],
342
                     const uint8_t nonce[_Nonnull CRYPTO_NONCE_SIZE], const uint8_t *_Nonnull encrypted, size_t length, uint8_t *_Nonnull plain);
343
344
/**
345
 * @brief Fast encrypt/decrypt operations.
346
 *
347
 * Use if this is not a one-time communication. `encrypt_precompute` does the
348
 * shared-key generation once so it does not have to be performed on every
349
 * encrypt/decrypt.
350
 */
351
int32_t encrypt_precompute(const uint8_t public_key[_Nonnull CRYPTO_PUBLIC_KEY_SIZE], const uint8_t secret_key[_Nonnull CRYPTO_SECRET_KEY_SIZE],
352
                           uint8_t shared_key[_Nonnull CRYPTO_SHARED_KEY_SIZE]);
353
354
/**
355
 * @brief Encrypt message with precomputed shared key.
356
 *
357
 * Encrypts plain of length length to encrypted of length + @ref CRYPTO_MAC_SIZE
358
 * using a shared key @ref CRYPTO_SYMMETRIC_KEY_SIZE big and a @ref CRYPTO_NONCE_SIZE
359
 * byte nonce.
360
 *
361
 * @retval -1 if there was a problem.
362
 * @return length of encrypted data if everything was fine.
363
 */
364
int32_t encrypt_data_symmetric(const Memory *_Nonnull mem, const uint8_t shared_key[_Nonnull CRYPTO_SHARED_KEY_SIZE], const uint8_t nonce[_Nonnull CRYPTO_NONCE_SIZE],
365
                               const uint8_t *_Nonnull plain, size_t length, uint8_t *_Nonnull encrypted);
366
367
/**
368
 * @brief Decrypt message with precomputed shared key.
369
 *
370
 * Decrypts encrypted of length length to plain of length
371
 * `length - CRYPTO_MAC_SIZE` using a shared key @ref CRYPTO_SYMMETRIC_KEY_SIZE
372
 * big and a @ref CRYPTO_NONCE_SIZE byte nonce.
373
 *
374
 * @retval -1 if there was a problem (decryption failed).
375
 * @return length of plain data if everything was fine.
376
 */
377
int32_t decrypt_data_symmetric(const Memory *_Nonnull mem, const uint8_t shared_key[_Nonnull CRYPTO_SHARED_KEY_SIZE], const uint8_t nonce[_Nonnull CRYPTO_NONCE_SIZE],
378
                               const uint8_t *_Nonnull encrypted, size_t length, uint8_t *_Nonnull plain);
379
380
/**
381
 * @brief Increment the given nonce by 1 in big endian (rightmost byte incremented first).
382
 */
383
void increment_nonce(uint8_t nonce[_Nonnull CRYPTO_NONCE_SIZE]);
384
385
/**
386
 * @brief Increment the given nonce by a given number.
387
 *
388
 * The number should be in host byte order.
389
 */
390
void increment_nonce_number(uint8_t nonce[_Nonnull CRYPTO_NONCE_SIZE], uint32_t increment);
391
392
/**
393
 * @brief Fill a key @ref CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes.
394
 */
395
void new_symmetric_key(const Random *_Nonnull rng, uint8_t key[_Nonnull CRYPTO_SYMMETRIC_KEY_SIZE]);
396
397
/**
398
 * @brief Locks `length` bytes of memory pointed to by `data`.
399
 *
400
 * This will attempt to prevent the specified memory region from being swapped
401
 * to disk.
402
 *
403
 * @return true on success.
404
 */
405
bool crypto_memlock(void *_Nonnull data, size_t length);
406
407
/**
408
 * @brief Unlocks `length` bytes of memory pointed to by `data`.
409
 *
410
 * This allows the specified memory region to be swapped to disk.
411
 *
412
 * This function call has the side effect of zeroing the specified memory region
413
 * whether or not it succeeds. Therefore it should only be used once the memory
414
 * is no longer in use.
415
 *
416
 * @return true on success.
417
 */
418
bool crypto_memunlock(void *_Nonnull data, size_t length);
419
420
/**
421
 * @brief Generate a random secret HMAC key.
422
 */
423
void new_hmac_key(const Random *_Nonnull rng, uint8_t key[_Nonnull CRYPTO_HMAC_KEY_SIZE]);
424
425
#ifdef __cplusplus
426
} /* extern "C" */
427
#endif
428
429
#endif /* C_TOXCORE_TOXCORE_CRYPTO_CORE_H */