@@ -2702,3 +2702,162 @@ int test_wolfSSL_EVP_PKEY_ed448(void)
27022702 return EXPECT_RESULT ();
27032703}
27042704
2705+ int test_wolfSSL_EVP_PKEY_x25519 (void )
2706+ {
2707+ EXPECT_DECLS ;
2708+ #if defined(OPENSSL_EXTRA ) && defined(HAVE_CURVE25519 )
2709+ EVP_PKEY * pkey = NULL ;
2710+ EVP_PKEY * peer = NULL ;
2711+ EVP_PKEY_CTX * genCtx = NULL ;
2712+ EVP_PKEY_CTX * ctx = NULL ;
2713+ unsigned char rawPriv [32 ];
2714+ unsigned char rawPub [32 ];
2715+ unsigned char secretA [32 ];
2716+ unsigned char secretB [32 ];
2717+ size_t secretLen ;
2718+ int i ;
2719+
2720+ for (i = 0 ; i < 32 ; i ++ ) {
2721+ rawPriv [i ] = (unsigned char )i ;
2722+ rawPub [i ] = (unsigned char )(0x40 + i );
2723+ }
2724+
2725+ /* Raw import with the correct length reports the X25519 type. */
2726+ ExpectNotNull (pkey = EVP_PKEY_new_raw_public_key (
2727+ EVP_PKEY_X25519 , NULL , rawPub , sizeof (rawPub )));
2728+ ExpectIntEQ (EVP_PKEY_id (pkey ), EVP_PKEY_X25519 );
2729+ EVP_PKEY_free (pkey );
2730+ pkey = NULL ;
2731+
2732+ ExpectNotNull (pkey = EVP_PKEY_new_raw_private_key (
2733+ EVP_PKEY_X25519 , NULL , rawPriv , sizeof (rawPriv )));
2734+ ExpectIntEQ (EVP_PKEY_id (pkey ), EVP_PKEY_X25519 );
2735+
2736+ /* X25519 is key-agreement only: signing must be rejected. */
2737+ ExpectNotNull (ctx = EVP_PKEY_CTX_new (pkey , NULL ));
2738+ ExpectIntNE (EVP_PKEY_sign_init (ctx ), WOLFSSL_SUCCESS );
2739+ EVP_PKEY_CTX_free (ctx );
2740+ ctx = NULL ;
2741+ EVP_PKEY_free (pkey );
2742+ pkey = NULL ;
2743+
2744+ /* Wrong raw lengths are rejected. */
2745+ ExpectNull (EVP_PKEY_new_raw_public_key (
2746+ EVP_PKEY_X25519 , NULL , rawPub , 16 ));
2747+ ExpectNull (EVP_PKEY_new_raw_private_key (
2748+ EVP_PKEY_X25519 , NULL , rawPriv , 16 ));
2749+
2750+ /* Generate two key pairs and confirm ECDH agreement is symmetric. This
2751+ * also exercises the little-endian convention used on import/derive. */
2752+ ExpectNotNull (genCtx = EVP_PKEY_CTX_new_id (EVP_PKEY_X25519 , NULL ));
2753+ ExpectIntEQ (EVP_PKEY_keygen_init (genCtx ), WOLFSSL_SUCCESS );
2754+ ExpectIntEQ (EVP_PKEY_keygen (genCtx , & pkey ), WOLFSSL_SUCCESS );
2755+ ExpectIntEQ (EVP_PKEY_keygen (genCtx , & peer ), WOLFSSL_SUCCESS );
2756+ EVP_PKEY_CTX_free (genCtx );
2757+ genCtx = NULL ;
2758+
2759+ ExpectNotNull (ctx = EVP_PKEY_CTX_new (pkey , NULL ));
2760+ ExpectIntEQ (EVP_PKEY_derive_init (ctx ), WOLFSSL_SUCCESS );
2761+ ExpectIntEQ (EVP_PKEY_derive_set_peer (ctx , peer ), WOLFSSL_SUCCESS );
2762+ secretLen = sizeof (secretA );
2763+ ExpectIntEQ (EVP_PKEY_derive (ctx , secretA , & secretLen ), WOLFSSL_SUCCESS );
2764+ ExpectIntEQ ((int )secretLen , 32 );
2765+ EVP_PKEY_CTX_free (ctx );
2766+ ctx = NULL ;
2767+
2768+ ExpectNotNull (ctx = EVP_PKEY_CTX_new (peer , NULL ));
2769+ ExpectIntEQ (EVP_PKEY_derive_init (ctx ), WOLFSSL_SUCCESS );
2770+ ExpectIntEQ (EVP_PKEY_derive_set_peer (ctx , pkey ), WOLFSSL_SUCCESS );
2771+ secretLen = sizeof (secretB );
2772+ ExpectIntEQ (EVP_PKEY_derive (ctx , secretB , & secretLen ), WOLFSSL_SUCCESS );
2773+ ExpectIntEQ ((int )secretLen , 32 );
2774+ EVP_PKEY_CTX_free (ctx );
2775+ ctx = NULL ;
2776+
2777+ ExpectIntEQ (XMEMCMP (secretA , secretB , 32 ), 0 );
2778+
2779+ EVP_PKEY_free (peer );
2780+ EVP_PKEY_free (pkey );
2781+ #endif
2782+ return EXPECT_RESULT ();
2783+ }
2784+
2785+ int test_wolfSSL_EVP_PKEY_x448 (void )
2786+ {
2787+ EXPECT_DECLS ;
2788+ #if defined(OPENSSL_EXTRA ) && defined(HAVE_CURVE448 )
2789+ EVP_PKEY * pkey = NULL ;
2790+ EVP_PKEY * peer = NULL ;
2791+ EVP_PKEY_CTX * genCtx = NULL ;
2792+ EVP_PKEY_CTX * ctx = NULL ;
2793+ unsigned char rawPriv [56 ];
2794+ unsigned char rawPub [56 ];
2795+ unsigned char secretA [56 ];
2796+ unsigned char secretB [56 ];
2797+ size_t secretLen ;
2798+ int i ;
2799+
2800+ for (i = 0 ; i < 56 ; i ++ ) {
2801+ rawPriv [i ] = (unsigned char )i ;
2802+ rawPub [i ] = (unsigned char )(0x40 + i );
2803+ }
2804+
2805+ /* Raw import with the correct length reports the X448 type. */
2806+ ExpectNotNull (pkey = EVP_PKEY_new_raw_public_key (
2807+ EVP_PKEY_X448 , NULL , rawPub , sizeof (rawPub )));
2808+ ExpectIntEQ (EVP_PKEY_id (pkey ), EVP_PKEY_X448 );
2809+ EVP_PKEY_free (pkey );
2810+ pkey = NULL ;
2811+
2812+ ExpectNotNull (pkey = EVP_PKEY_new_raw_private_key (
2813+ EVP_PKEY_X448 , NULL , rawPriv , sizeof (rawPriv )));
2814+ ExpectIntEQ (EVP_PKEY_id (pkey ), EVP_PKEY_X448 );
2815+
2816+ /* X448 is key-agreement only: signing must be rejected. */
2817+ ExpectNotNull (ctx = EVP_PKEY_CTX_new (pkey , NULL ));
2818+ ExpectIntNE (EVP_PKEY_sign_init (ctx ), WOLFSSL_SUCCESS );
2819+ EVP_PKEY_CTX_free (ctx );
2820+ ctx = NULL ;
2821+ EVP_PKEY_free (pkey );
2822+ pkey = NULL ;
2823+
2824+ /* Wrong raw lengths are rejected. */
2825+ ExpectNull (EVP_PKEY_new_raw_public_key (
2826+ EVP_PKEY_X448 , NULL , rawPub , 16 ));
2827+ ExpectNull (EVP_PKEY_new_raw_private_key (
2828+ EVP_PKEY_X448 , NULL , rawPriv , 16 ));
2829+
2830+ /* Generate two key pairs and confirm ECDH agreement is symmetric. */
2831+ ExpectNotNull (genCtx = EVP_PKEY_CTX_new_id (EVP_PKEY_X448 , NULL ));
2832+ ExpectIntEQ (EVP_PKEY_keygen_init (genCtx ), WOLFSSL_SUCCESS );
2833+ ExpectIntEQ (EVP_PKEY_keygen (genCtx , & pkey ), WOLFSSL_SUCCESS );
2834+ ExpectIntEQ (EVP_PKEY_keygen (genCtx , & peer ), WOLFSSL_SUCCESS );
2835+ EVP_PKEY_CTX_free (genCtx );
2836+ genCtx = NULL ;
2837+
2838+ ExpectNotNull (ctx = EVP_PKEY_CTX_new (pkey , NULL ));
2839+ ExpectIntEQ (EVP_PKEY_derive_init (ctx ), WOLFSSL_SUCCESS );
2840+ ExpectIntEQ (EVP_PKEY_derive_set_peer (ctx , peer ), WOLFSSL_SUCCESS );
2841+ secretLen = sizeof (secretA );
2842+ ExpectIntEQ (EVP_PKEY_derive (ctx , secretA , & secretLen ), WOLFSSL_SUCCESS );
2843+ ExpectIntEQ ((int )secretLen , 56 );
2844+ EVP_PKEY_CTX_free (ctx );
2845+ ctx = NULL ;
2846+
2847+ ExpectNotNull (ctx = EVP_PKEY_CTX_new (peer , NULL ));
2848+ ExpectIntEQ (EVP_PKEY_derive_init (ctx ), WOLFSSL_SUCCESS );
2849+ ExpectIntEQ (EVP_PKEY_derive_set_peer (ctx , pkey ), WOLFSSL_SUCCESS );
2850+ secretLen = sizeof (secretB );
2851+ ExpectIntEQ (EVP_PKEY_derive (ctx , secretB , & secretLen ), WOLFSSL_SUCCESS );
2852+ ExpectIntEQ ((int )secretLen , 56 );
2853+ EVP_PKEY_CTX_free (ctx );
2854+ ctx = NULL ;
2855+
2856+ ExpectIntEQ (XMEMCMP (secretA , secretB , 56 ), 0 );
2857+
2858+ EVP_PKEY_free (peer );
2859+ EVP_PKEY_free (pkey );
2860+ #endif
2861+ return EXPECT_RESULT ();
2862+ }
2863+
0 commit comments