@@ -353,6 +353,51 @@ int test_wc_curve25519_shared_secret_ex(void)
353353 return EXPECT_RESULT ();
354354} /* END test_wc_curve25519_shared_secret_ex */
355355
356+ /*
357+ * Testing that wc_curve25519_shared_secret_ex rejects an all-zero shared
358+ * secret (RFC 7748 section 6.1). This is the default behavior; users that
359+ * need the legacy behavior can opt out with WOLFSSL_NO_ECDHX_SHARED_ZERO_CHECK.
360+ */
361+ int test_wc_curve25519_shared_secret_zero_check (void )
362+ {
363+ EXPECT_DECLS ;
364+ #if defined(HAVE_CURVE25519 ) && defined(HAVE_CURVE25519_KEY_IMPORT ) && \
365+ !defined(WOLFSSL_NO_ECDHX_SHARED_ZERO_CHECK )
366+ curve25519_key private_key ;
367+ curve25519_key public_key ;
368+ WC_RNG rng ;
369+ byte out [CURVE25519_KEYSIZE ];
370+ word32 outLen = sizeof (out );
371+ /* All-zero public key is a low-order point that yields an all-zero
372+ * shared secret for any private key. */
373+ byte zero_pub [CURVE25519_KEYSIZE ];
374+
375+ XMEMSET (& rng , 0 , sizeof (WC_RNG ));
376+ XMEMSET (zero_pub , 0 , sizeof (zero_pub ));
377+
378+ ExpectIntEQ (wc_curve25519_init (& private_key ), 0 );
379+ ExpectIntEQ (wc_curve25519_init (& public_key ), 0 );
380+ ExpectIntEQ (wc_InitRng (& rng ), 0 );
381+ #ifdef WOLFSSL_CURVE25519_BLINDING
382+ ExpectIntEQ (wc_curve25519_set_rng (& private_key , & rng ), 0 );
383+ #endif
384+
385+ ExpectIntEQ (wc_curve25519_make_key (& rng , CURVE25519_KEYSIZE , & private_key ),
386+ 0 );
387+ ExpectIntEQ (wc_curve25519_import_public_ex (zero_pub , sizeof (zero_pub ),
388+ & public_key , EC25519_LITTLE_ENDIAN ), 0 );
389+
390+ ExpectIntEQ (wc_curve25519_shared_secret_ex (& private_key , & public_key , out ,
391+ & outLen , EC25519_BIG_ENDIAN ),
392+ WC_NO_ERR_TRACE (ECC_OUT_OF_RANGE_E ));
393+
394+ DoExpectIntEQ (wc_FreeRng (& rng ), 0 );
395+ wc_curve25519_free (& private_key );
396+ wc_curve25519_free (& public_key );
397+ #endif
398+ return EXPECT_RESULT ();
399+ } /* END test_wc_curve25519_shared_secret_zero_check */
400+
356401/*
357402 * Testing wc_curve25519_make_pub
358403 */
0 commit comments