@@ -28,6 +28,8 @@ static_assert(CRYPTO_MAC_SIZE == crypto_box_MACBYTES,
2828 "CRYPTO_MAC_SIZE should be equal to crypto_box_MACBYTES" );
2929static_assert (CRYPTO_NONCE_SIZE == crypto_box_NONCEBYTES ,
3030 "CRYPTO_NONCE_SIZE should be equal to crypto_box_NONCEBYTES" );
31+ static_assert (CRYPTO_NOISE_NONCE_SIZE == crypto_stream_chacha20_ietf_NONCEBYTES ,
32+ "CRYPTO_NOISE_NONCE_SIZE should be equal to crypto_stream_chacha20_ietf_NONCEBYTES" );
3133static_assert (CRYPTO_HMAC_SIZE == crypto_auth_BYTES ,
3234 "CRYPTO_HMAC_SIZE should be equal to crypto_auth_BYTES" );
3335static_assert (CRYPTO_HMAC_KEY_SIZE == crypto_auth_KEYBYTES ,
@@ -46,6 +48,12 @@ static_assert(CRYPTO_SIGN_PUBLIC_KEY_SIZE == crypto_sign_PUBLICKEYBYTES,
4648static_assert (CRYPTO_SIGN_SECRET_KEY_SIZE == crypto_sign_SECRETKEYBYTES ,
4749 "CRYPTO_SIGN_SECRET_KEY_SIZE should be equal to crypto_sign_SECRETKEYBYTES" );
4850
51+
52+ static_assert (CRYPTO_MAC_SIZE == crypto_aead_chacha20poly1305_IETF_ABYTES ,
53+ "CRYPTO_MAC_SIZE should be equal to crypto_aead_chacha20poly1305_IETF_ABYTES" );
54+ static_assert (CRYPTO_SHARED_KEY_SIZE == CRYPTO_SYMMETRIC_KEY_SIZE ,
55+ "CRYPTO_SHARED_KEY_SIZE should be equal to CRYPTO_SYMMETRIC_KEY_SIZE" );
56+
4957bool create_extended_keypair (Extended_Public_Key * pk , Extended_Secret_Key * sk , const Random * rng )
5058{
5159 /* create signature key pair */
@@ -235,7 +243,7 @@ int32_t encrypt_data_symmetric(const Memory *mem,
235243 const uint8_t nonce [CRYPTO_NONCE_SIZE ],
236244 const uint8_t * plain , size_t length , uint8_t * encrypted )
237245{
238- if (length == 0 || shared_key == nullptr || nonce == nullptr || plain == nullptr || encrypted == nullptr ) {
246+ if (length == 0 || length >= INT32_MAX - crypto_box_MACBYTES || shared_key == nullptr || nonce == nullptr || plain == nullptr || encrypted == nullptr ) {
239247 return -1 ;
240248 }
241249
@@ -280,7 +288,6 @@ int32_t encrypt_data_symmetric(const Memory *mem,
280288 crypto_free (mem , temp_plain , size_temp_plain );
281289 crypto_free (mem , temp_encrypted , size_temp_encrypted );
282290#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
283- assert (length < INT32_MAX - crypto_box_MACBYTES );
284291 return (int32_t )(length + crypto_box_MACBYTES );
285292}
286293
@@ -289,13 +296,12 @@ int32_t decrypt_data_symmetric(const Memory *mem,
289296 const uint8_t nonce [CRYPTO_NONCE_SIZE ],
290297 const uint8_t * encrypted , size_t length , uint8_t * plain )
291298{
292- if (length <= crypto_box_BOXZEROBYTES || shared_key == nullptr || nonce == nullptr || encrypted == nullptr
299+ if (length <= crypto_box_BOXZEROBYTES || length >= INT32_MAX || shared_key == nullptr || nonce == nullptr || encrypted == nullptr
293300 || plain == nullptr ) {
294301 return -1 ;
295302 }
296303
297304#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
298- assert (length >= crypto_box_MACBYTES );
299305 memcpy (plain , encrypted , length - crypto_box_MACBYTES ); // Don't encrypt anything
300306#else
301307
@@ -332,8 +338,6 @@ int32_t decrypt_data_symmetric(const Memory *mem,
332338 crypto_free (mem , temp_plain , size_temp_plain );
333339 crypto_free (mem , temp_encrypted , size_temp_encrypted );
334340#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
335- assert (length > crypto_box_MACBYTES );
336- assert (length < INT32_MAX );
337341 return (int32_t )(length - crypto_box_MACBYTES );
338342}
339343
@@ -373,10 +377,7 @@ int32_t decrypt_data(const Memory *mem,
373377
374378void increment_nonce (uint8_t nonce [CRYPTO_NONCE_SIZE ])
375379{
376- /* TODO(irungentoo): use `increment_nonce_number(nonce, 1)` or
377- * sodium_increment (change to little endian).
378- *
379- * NOTE don't use breaks inside this loop.
380+ /* NOTE don't use breaks inside this loop.
380381 * In particular, make sure, as far as possible,
381382 * that loop bounds and their potential underflow or overflow
382383 * are independent of user-controlled input (you may have heard of the Heartbleed bug).
@@ -488,3 +489,92 @@ void random_bytes(const Random *rng, uint8_t *bytes, size_t length)
488489{
489490 rng_bytes (rng , bytes , length );
490491}
492+
493+ // Necessary functions for Noise, cf. https://noiseprotocol.org/noise.html (Revision 34)
494+
495+ int32_t encrypt_data_symmetric_aead (const uint8_t shared_key [CRYPTO_SHARED_KEY_SIZE ], const uint8_t nonce [CRYPTO_NOISE_NONCE_SIZE ],
496+ const uint8_t * plain , size_t plain_length , uint8_t * encrypted ,
497+ const uint8_t * ad , size_t ad_length )
498+ {
499+ if (plain_length == 0 || plain_length >= INT32_MAX - crypto_aead_chacha20poly1305_IETF_ABYTES
500+ || shared_key == nullptr || nonce == nullptr || plain == nullptr || encrypted == nullptr ) {
501+ return -1 ;
502+ }
503+
504+ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
505+ memcpy (encrypted , plain , plain_length );
506+ memzero (encrypted + plain_length , crypto_aead_chacha20poly1305_IETF_ABYTES );
507+ #else
508+ if (crypto_aead_chacha20poly1305_ietf_encrypt (encrypted , nullptr , plain , plain_length ,
509+ ad , ad_length , nullptr , nonce , shared_key ) != 0 ) {
510+ return -1 ;
511+ }
512+ #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
513+
514+ return (int32_t )(plain_length + crypto_aead_chacha20poly1305_IETF_ABYTES );
515+ }
516+
517+ int32_t decrypt_data_symmetric_aead (const uint8_t shared_key [CRYPTO_SHARED_KEY_SIZE ], const uint8_t nonce [CRYPTO_NOISE_NONCE_SIZE ],
518+ const uint8_t * encrypted , size_t encrypted_length , uint8_t * plain ,
519+ const uint8_t * ad , size_t ad_length )
520+ {
521+ if (encrypted_length <= crypto_aead_chacha20poly1305_IETF_ABYTES || encrypted_length >= INT32_MAX
522+ || shared_key == nullptr || nonce == nullptr || encrypted == nullptr || plain == nullptr ) {
523+ return -1 ;
524+ }
525+
526+ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
527+ memcpy (plain , encrypted , encrypted_length - crypto_aead_chacha20poly1305_IETF_ABYTES );
528+ #else
529+ if (crypto_aead_chacha20poly1305_ietf_decrypt (plain , nullptr , nullptr , encrypted ,
530+ encrypted_length , ad , ad_length , nonce , shared_key ) != 0 ) {
531+ return -1 ;
532+ }
533+ #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
534+
535+ return (int32_t )(encrypted_length - crypto_aead_chacha20poly1305_IETF_ABYTES );
536+ }
537+
538+ int32_t encrypt_data_symmetric_xaead (const uint8_t shared_key [CRYPTO_SHARED_KEY_SIZE ], const uint8_t nonce [CRYPTO_NONCE_SIZE ],
539+ const uint8_t * plain , size_t plain_length , uint8_t * encrypted ,
540+ const uint8_t * ad , size_t ad_length )
541+ {
542+ if (plain_length == 0 || plain_length >= INT32_MAX - crypto_aead_xchacha20poly1305_ietf_ABYTES
543+ || shared_key == nullptr || nonce == nullptr || plain == nullptr || encrypted == nullptr ) {
544+ return -1 ;
545+ }
546+
547+ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
548+ memcpy (encrypted , plain , plain_length );
549+ memzero (encrypted + plain_length , crypto_aead_xchacha20poly1305_ietf_ABYTES );
550+ #else
551+ if (crypto_aead_xchacha20poly1305_ietf_encrypt (encrypted , nullptr , plain , plain_length ,
552+ ad , ad_length , nullptr , nonce , shared_key ) != 0 ) {
553+ return -1 ;
554+ }
555+ #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
556+
557+ return (int32_t )(plain_length + crypto_aead_xchacha20poly1305_ietf_ABYTES );
558+ }
559+
560+ int32_t decrypt_data_symmetric_xaead (const uint8_t shared_key [CRYPTO_SHARED_KEY_SIZE ], const uint8_t nonce [CRYPTO_NONCE_SIZE ],
561+ const uint8_t * encrypted , size_t encrypted_length , uint8_t * plain ,
562+ const uint8_t * ad , size_t ad_length )
563+ {
564+ if (encrypted_length <= crypto_aead_xchacha20poly1305_ietf_ABYTES || encrypted_length >= INT32_MAX
565+ || shared_key == nullptr || nonce == nullptr || encrypted == nullptr || plain == nullptr ) {
566+ return -1 ;
567+ }
568+
569+ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
570+ memcpy (plain , encrypted , encrypted_length - crypto_aead_xchacha20poly1305_ietf_ABYTES );
571+ #else
572+ if (crypto_aead_xchacha20poly1305_ietf_decrypt (plain , nullptr , nullptr , encrypted ,
573+ encrypted_length , ad , ad_length , nonce , shared_key ) != 0 ) {
574+ return -1 ;
575+ }
576+ #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
577+
578+ return (int32_t )(encrypted_length - crypto_aead_xchacha20poly1305_ietf_ABYTES );
579+ }
580+
0 commit comments