11/* SPDX-License-Identifier: GPL-3.0-or-later
2- * Copyright © 2016-2018 The TokTok team.
2+ * Copyright © 2016-2024 The TokTok team.
33 * Copyright © 2013 Tox project.
44 */
55
6- /**
7- * Functions for the core crypto.
8- *
9- * NOTE: This code has to be perfect. We don't mess around with encryption.
10- */
116#include "crypto_core.h"
127
138#include <assert.h>
2015#include "ccompat.h"
2116#include "util.h"
2217
23- #ifndef crypto_box_MACBYTES
24- #define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
25- #endif /* crypto_box_MACBYTES */
26-
27- // Need dht because of ENC_SECRET_KEY_SIZE and ENC_PUBLIC_KEY_SIZE
28- #define ENC_PUBLIC_KEY_SIZE CRYPTO_PUBLIC_KEY_SIZE
29- #define ENC_SECRET_KEY_SIZE CRYPTO_SECRET_KEY_SIZE
30-
3118static_assert (CRYPTO_PUBLIC_KEY_SIZE == crypto_box_PUBLICKEYBYTES ,
3219 "CRYPTO_PUBLIC_KEY_SIZE should be equal to crypto_box_PUBLICKEYBYTES" );
3320static_assert (CRYPTO_SECRET_KEY_SIZE == crypto_box_SECRETKEYBYTES ,
@@ -58,7 +45,7 @@ static_assert(CRYPTO_SIGN_PUBLIC_KEY_SIZE == crypto_sign_PUBLICKEYBYTES,
5845static_assert (CRYPTO_SIGN_SECRET_KEY_SIZE == crypto_sign_SECRETKEYBYTES ,
5946 "CRYPTO_SIGN_SECRET_KEY_SIZE should be equal to crypto_sign_SECRETKEYBYTES" );
6047
61- bool create_extended_keypair (uint8_t * pk , uint8_t * sk )
48+ bool create_extended_keypair (uint8_t pk [ EXT_PUBLIC_KEY_SIZE ] , uint8_t sk [ EXT_SECRET_KEY_SIZE ] )
6249{
6350 /* create signature key pair */
6451 crypto_sign_keypair (pk + ENC_PUBLIC_KEY_SIZE , sk + ENC_SECRET_KEY_SIZE );
@@ -165,7 +152,7 @@ void pk_copy(uint8_t dest[CRYPTO_PUBLIC_KEY_SIZE], const uint8_t src[CRYPTO_PUBL
165152 memcpy (dest , src , CRYPTO_PUBLIC_KEY_SIZE );
166153}
167154
168- bool crypto_sha512_eq (const uint8_t * cksum1 , const uint8_t * cksum2 )
155+ bool crypto_sha512_eq (const uint8_t cksum1 [ CRYPTO_SHA512_SIZE ] , const uint8_t cksum2 [ CRYPTO_SHA512_SIZE ] )
169156{
170157#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION )
171158 // Hope that this is better for the fuzzer
@@ -175,7 +162,7 @@ bool crypto_sha512_eq(const uint8_t *cksum1, const uint8_t *cksum2)
175162#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
176163}
177164
178- bool crypto_sha256_eq (const uint8_t * cksum1 , const uint8_t * cksum2 )
165+ bool crypto_sha256_eq (const uint8_t cksum1 [ CRYPTO_SHA256_SIZE ] , const uint8_t cksum2 [ CRYPTO_SHA256_SIZE ] )
179166{
180167#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
181168 // Hope that this is better for the fuzzer
@@ -218,26 +205,29 @@ uint32_t random_range_u32(const Random *rng, uint32_t upper_bound)
218205 return rng -> funcs -> random_uniform (rng -> obj , upper_bound );
219206}
220207
221- bool crypto_signature_create (uint8_t * signature , const uint8_t * message , uint64_t message_length ,
222- const uint8_t * secret_key )
208+ bool crypto_signature_create (uint8_t signature [CRYPTO_SIGNATURE_SIZE ],
209+ const uint8_t * message , uint64_t message_length ,
210+ const uint8_t secret_key [SIG_SECRET_KEY_SIZE ])
223211{
224212 return crypto_sign_detached (signature , nullptr , message , message_length , secret_key ) == 0 ;
225213}
226214
227- bool crypto_signature_verify (const uint8_t * signature , const uint8_t * message , uint64_t message_length ,
228- const uint8_t * public_key )
215+ bool crypto_signature_verify (const uint8_t signature [CRYPTO_SIGNATURE_SIZE ],
216+ const uint8_t * message , uint64_t message_length ,
217+ const uint8_t public_key [SIG_PUBLIC_KEY_SIZE ])
229218{
230219 return crypto_sign_verify_detached (signature , message , message_length , public_key ) == 0 ;
231220}
232221
233- bool public_key_valid (const uint8_t * public_key )
222+ bool public_key_valid (const uint8_t public_key [ CRYPTO_PUBLIC_KEY_SIZE ] )
234223{
235224 /* Last bit of key is always zero. */
236225 return public_key [31 ] < 128 ;
237226}
238227
239- int32_t encrypt_precompute (const uint8_t * public_key , const uint8_t * secret_key ,
240- uint8_t * shared_key )
228+ int32_t encrypt_precompute (const uint8_t public_key [CRYPTO_PUBLIC_KEY_SIZE ],
229+ const uint8_t secret_key [CRYPTO_SECRET_KEY_SIZE ],
230+ uint8_t shared_key [CRYPTO_SHARED_KEY_SIZE ])
241231{
242232#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
243233 memcpy (shared_key , public_key , CRYPTO_SHARED_KEY_SIZE );
@@ -247,7 +237,8 @@ int32_t encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key,
247237#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
248238}
249239
250- int32_t encrypt_data_symmetric (const uint8_t * shared_key , const uint8_t * nonce ,
240+ int32_t encrypt_data_symmetric (const uint8_t shared_key [CRYPTO_SHARED_KEY_SIZE ],
241+ const uint8_t nonce [CRYPTO_NONCE_SIZE ],
251242 const uint8_t * plain , size_t length , uint8_t * encrypted )
252243{
253244 if (length == 0 || shared_key == nullptr || nonce == nullptr || plain == nullptr || encrypted == nullptr ) {
@@ -299,7 +290,8 @@ int32_t encrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
299290 return (int32_t )(length + crypto_box_MACBYTES );
300291}
301292
302- int32_t decrypt_data_symmetric (const uint8_t * shared_key , const uint8_t * nonce ,
293+ int32_t decrypt_data_symmetric (const uint8_t shared_key [CRYPTO_SHARED_KEY_SIZE ],
294+ const uint8_t nonce [CRYPTO_NONCE_SIZE ],
303295 const uint8_t * encrypted , size_t length , uint8_t * plain )
304296{
305297 if (length <= crypto_box_BOXZEROBYTES || shared_key == nullptr || nonce == nullptr || encrypted == nullptr
@@ -350,7 +342,9 @@ int32_t decrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
350342 return (int32_t )(length - crypto_box_MACBYTES );
351343}
352344
353- int32_t encrypt_data (const uint8_t * public_key , const uint8_t * secret_key , const uint8_t * nonce ,
345+ int32_t encrypt_data (const uint8_t public_key [CRYPTO_PUBLIC_KEY_SIZE ],
346+ const uint8_t secret_key [CRYPTO_SECRET_KEY_SIZE ],
347+ const uint8_t nonce [CRYPTO_NONCE_SIZE ],
354348 const uint8_t * plain , size_t length , uint8_t * encrypted )
355349{
356350 if (public_key == nullptr || secret_key == nullptr ) {
@@ -364,7 +358,9 @@ int32_t encrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const
364358 return ret ;
365359}
366360
367- int32_t decrypt_data (const uint8_t * public_key , const uint8_t * secret_key , const uint8_t * nonce ,
361+ int32_t decrypt_data (const uint8_t public_key [CRYPTO_PUBLIC_KEY_SIZE ],
362+ const uint8_t secret_key [CRYPTO_SECRET_KEY_SIZE ],
363+ const uint8_t nonce [CRYPTO_NONCE_SIZE ],
368364 const uint8_t * encrypted , size_t length , uint8_t * plain )
369365{
370366 if (public_key == nullptr || secret_key == nullptr ) {
@@ -378,7 +374,7 @@ int32_t decrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const
378374 return ret ;
379375}
380376
381- void increment_nonce (uint8_t * nonce )
377+ void increment_nonce (uint8_t nonce [ CRYPTO_NONCE_SIZE ] )
382378{
383379 /* TODO(irungentoo): use `increment_nonce_number(nonce, 1)` or
384380 * sodium_increment (change to little endian).
@@ -397,7 +393,7 @@ void increment_nonce(uint8_t *nonce)
397393 }
398394}
399395
400- void increment_nonce_number (uint8_t * nonce , uint32_t increment )
396+ void increment_nonce_number (uint8_t nonce [ CRYPTO_NONCE_SIZE ] , uint32_t increment )
401397{
402398 /* NOTE don't use breaks inside this loop
403399 * In particular, make sure, as far as possible,
@@ -419,25 +415,28 @@ void increment_nonce_number(uint8_t *nonce, uint32_t increment)
419415 }
420416}
421417
422- void random_nonce (const Random * rng , uint8_t * nonce )
418+ void random_nonce (const Random * rng , uint8_t nonce [ CRYPTO_NONCE_SIZE ] )
423419{
424420 random_bytes (rng , nonce , crypto_box_NONCEBYTES );
425421}
426422
427- void new_symmetric_key (const Random * rng , uint8_t * key )
423+ void new_symmetric_key (const Random * rng , uint8_t key [ CRYPTO_SYMMETRIC_KEY_SIZE ] )
428424{
429425 random_bytes (rng , key , CRYPTO_SYMMETRIC_KEY_SIZE );
430426}
431427
432- int32_t crypto_new_keypair (const Random * rng , uint8_t * public_key , uint8_t * secret_key )
428+ int32_t crypto_new_keypair (const Random * rng ,
429+ uint8_t public_key [CRYPTO_PUBLIC_KEY_SIZE ],
430+ uint8_t secret_key [CRYPTO_SECRET_KEY_SIZE ])
433431{
434432 random_bytes (rng , secret_key , CRYPTO_SECRET_KEY_SIZE );
435433 memzero (public_key , CRYPTO_PUBLIC_KEY_SIZE ); // Make MSAN happy
436434 crypto_derive_public_key (public_key , secret_key );
437435 return 0 ;
438436}
439437
440- void crypto_derive_public_key (uint8_t * public_key , const uint8_t * secret_key )
438+ void crypto_derive_public_key (uint8_t public_key [CRYPTO_PUBLIC_KEY_SIZE ],
439+ const uint8_t secret_key [CRYPTO_SECRET_KEY_SIZE ])
441440{
442441 crypto_scalarmult_curve25519_base (public_key , secret_key );
443442}
@@ -447,8 +446,8 @@ void new_hmac_key(const Random *rng, uint8_t key[CRYPTO_HMAC_KEY_SIZE])
447446 random_bytes (rng , key , CRYPTO_HMAC_KEY_SIZE );
448447}
449448
450- void crypto_hmac (uint8_t auth [CRYPTO_HMAC_SIZE ], const uint8_t key [CRYPTO_HMAC_KEY_SIZE ], const uint8_t * data ,
451- size_t length )
449+ void crypto_hmac (uint8_t auth [CRYPTO_HMAC_SIZE ], const uint8_t key [CRYPTO_HMAC_KEY_SIZE ],
450+ const uint8_t * data , size_t length )
452451{
453452#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
454453 memcpy (auth , key , 16 );
@@ -468,7 +467,7 @@ bool crypto_hmac_verify(const uint8_t auth[CRYPTO_HMAC_SIZE], const uint8_t key[
468467#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
469468}
470469
471- void crypto_sha256 (uint8_t * hash , const uint8_t * data , size_t length )
470+ void crypto_sha256 (uint8_t hash [ CRYPTO_SHA256_SIZE ] , const uint8_t * data , size_t length )
472471{
473472#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
474473 memzero (hash , CRYPTO_SHA256_SIZE );
@@ -478,7 +477,7 @@ void crypto_sha256(uint8_t *hash, const uint8_t *data, size_t length)
478477#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
479478}
480479
481- void crypto_sha512 (uint8_t * hash , const uint8_t * data , size_t length )
480+ void crypto_sha512 (uint8_t hash [ CRYPTO_SHA512_SIZE ] , const uint8_t * data , size_t length )
482481{
483482#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
484483 memzero (hash , CRYPTO_SHA512_SIZE );
0 commit comments