2929#define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
3030#endif
3131
32+ //!TOKSTYLE-
33+ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
34+ #include "../testing/fuzzing/fuzz_adapter.h"
35+ #endif
36+ //!TOKSTYLE+
37+
3238static_assert (CRYPTO_PUBLIC_KEY_SIZE == crypto_box_PUBLICKEYBYTES ,
3339 "CRYPTO_PUBLIC_KEY_SIZE should be equal to crypto_box_PUBLICKEYBYTES" );
3440static_assert (CRYPTO_SECRET_KEY_SIZE == crypto_box_SECRETKEYBYTES ,
@@ -48,6 +54,7 @@ static_assert(CRYPTO_SHA512_SIZE == crypto_hash_sha512_BYTES,
4854static_assert (CRYPTO_PUBLIC_KEY_SIZE == 32 ,
4955 "CRYPTO_PUBLIC_KEY_SIZE is required to be 32 bytes for public_key_cmp to work" );
5056
57+ #if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION )
5158static uint8_t * crypto_malloc (size_t bytes )
5259{
5360 return (uint8_t * )malloc (bytes );
@@ -61,10 +68,16 @@ static void crypto_free(uint8_t *ptr, size_t bytes)
6168
6269 free (ptr );
6370}
71+ #endif // !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
6472
6573int32_t public_key_cmp (const uint8_t * pk1 , const uint8_t * pk2 )
6674{
75+ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
76+ // Hope that this is better for the fuzzer
77+ return memcmp (pk1 , pk2 , CRYPTO_PUBLIC_KEY_SIZE ) == 0 ? 0 : -1 ;
78+ #else
6779 return crypto_verify_32 (pk1 , pk2 );
80+ #endif
6881}
6982
7083int32_t crypto_sha512_cmp (const uint8_t * cksum1 , const uint8_t * cksum2 )
@@ -131,6 +144,11 @@ int32_t encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
131144 return -1 ;
132145 }
133146
147+ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
148+ memcpy (encrypted , plain , length ); // Don't encrypt anything
149+ memset (encrypted + length , 0 , crypto_box_MACBYTES ); // Zero MAC to avoid false alarms of uninitialized memory
150+ #else
151+
134152 const size_t size_temp_plain = length + crypto_box_ZEROBYTES ;
135153 const size_t size_temp_encrypted = length + crypto_box_MACBYTES + crypto_box_BOXZEROBYTES ;
136154
@@ -159,7 +177,7 @@ int32_t encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
159177
160178 crypto_free (temp_plain , size_temp_plain );
161179 crypto_free (temp_encrypted , size_temp_encrypted );
162-
180+ #endif
163181 return length + crypto_box_MACBYTES ;
164182}
165183
@@ -170,6 +188,10 @@ int32_t decrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
170188 return -1 ;
171189 }
172190
191+ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
192+ memcpy (plain , encrypted , length - crypto_box_MACBYTES ); // Don't encrypt anything
193+ #else
194+
173195 const size_t size_temp_plain = length + crypto_box_ZEROBYTES ;
174196 const size_t size_temp_encrypted = length + crypto_box_BOXZEROBYTES ;
175197
@@ -197,6 +219,7 @@ int32_t decrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
197219
198220 crypto_free (temp_plain , size_temp_plain );
199221 crypto_free (temp_encrypted , size_temp_encrypted );
222+ #endif
200223 return length - crypto_box_MACBYTES ;
201224}
202225
@@ -295,7 +318,14 @@ void new_symmetric_key(uint8_t *key)
295318
296319int32_t crypto_new_keypair (uint8_t * public_key , uint8_t * secret_key )
297320{
321+ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
322+ random_bytes (secret_key , CRYPTO_SECRET_KEY_SIZE );
323+ memset (public_key , 0 , CRYPTO_PUBLIC_KEY_SIZE ); // Make MSAN happy
324+ crypto_scalarmult_curve25519_base (public_key , secret_key );
325+ return 0 ;
326+ #else
298327 return crypto_box_keypair (public_key , secret_key );
328+ #endif
299329}
300330
301331void crypto_derive_public_key (uint8_t * public_key , const uint8_t * secret_key )
@@ -315,5 +345,9 @@ void crypto_sha512(uint8_t *hash, const uint8_t *data, size_t length)
315345
316346void random_bytes (uint8_t * data , size_t length )
317347{
348+ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
349+ fuzz_random_bytes (data , length );
350+ #else
318351 randombytes (data , length );
352+ #endif
319353}
0 commit comments