From 886f5b0a5b88254d34ed1907ab85fea91d1410e7 Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Thu, 14 Nov 2024 09:07:55 +1000 Subject: [PATCH] Kyber: fixes to configure and wolfSSL_get_curve_name Remote original-only option for kyber in configure.ac. Default is ML-KEM only. original is Kyber only. ml-lem is ML-KEM. to have both: all,original,ml-kem. Use WOLFSSL_NO_ML_KEM* instead of WOLFSSL_WC_ML_KEM_* which requires the inclusion of kyber headers. --- configure.ac | 13 +++++++++---- src/ssl.c | 6 +++--- src/tls.c | 24 ++++++++++++------------ tests/api.c | 9 +++++++++ 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/configure.ac b/configure.ac index 16f4cfed8d..4c33e4b0d1 100644 --- a/configure.ac +++ b/configure.ac @@ -1309,7 +1309,7 @@ AC_ARG_ENABLE([kyber], ) ENABLED_WC_KYBER=no -ENABLED_ML_KEM=yes +ENABLED_ML_KEM=unset for v in `echo $ENABLED_KYBER | tr "," " "` do case $v in @@ -1335,9 +1335,8 @@ do original) ENABLED_ORIGINAL=yes ;; - original-only) - ENABLED_ORIGINAL=yes - ENABLED_ML_KEM=no + ml-kem) + ENABLED_ML_KEM=yes ;; *) AC_MSG_ERROR([Invalid choice for KYBER []: $ENABLED_KYBER.]) @@ -1366,6 +1365,12 @@ then if test "$ENABLED_KYBER1024" = ""; then AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_NO_KYBER1024" fi + if test "$ENABLED_ML_KEM" = "unset"; then + ENABLED_ML_KEM=no + fi + fi + if test "$ENABLED_ML_KEM" = "unset"; then + ENABLED_ML_KEM=yes fi if test "$ENABLED_ML_KEM" = "yes"; then if test "$ENABLED_KYBER512" = ""; then diff --git a/src/ssl.c b/src/ssl.c index 5728b399ef..2a568a0f18 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -14572,19 +14572,19 @@ const char* wolfSSL_get_curve_name(WOLFSSL* ssl) case WOLFSSL_P521_ML_KEM_1024: return "P521_ML_KEM_1024"; #elif defined(WOLFSSL_WC_KYBER) - #ifdef WOLFSSL_WC_ML_KEM_512 + #ifndef WOLFSSL_NO_ML_KEM_512 case WOLFSSL_ML_KEM_512: return "ML_KEM_512"; case WOLFSSL_P256_ML_KEM_512: return "P256_ML_KEM_512"; #endif - #ifdef WOLFSSL_WC_ML_KEM_768 + #ifndef WOLFSSL_NO_ML_KEM_768 case WOLFSSL_ML_KEM_768: return "ML_KEM_768"; case WOLFSSL_P384_ML_KEM_768: return "P384_ML_KEM_768"; #endif - #ifdef WOLFSSL_WC_ML_KEM_1024 + #ifndef WOLFSSL_NO_ML_KEM_1024 case WOLFSSL_ML_KEM_1024: return "ML_KEM_1024"; case WOLFSSL_P521_ML_KEM_1024: diff --git a/src/tls.c b/src/tls.c index 1b9adffa55..3c55c6632a 100644 --- a/src/tls.c +++ b/src/tls.c @@ -7984,17 +7984,17 @@ static int kyber_id2type(int id, int *type) switch (id) { #ifndef WOLFSSL_NO_ML_KEM - #ifdef WOLFSSL_WC_ML_KEM_512 + #ifndef WOLFSSL_NO_ML_KEM_512 case WOLFSSL_ML_KEM_512: *type = WC_ML_KEM_512; break; #endif - #ifdef WOLFSSL_WC_ML_KEM_768 + #ifndef WOLFSSL_NO_ML_KEM_768 case WOLFSSL_ML_KEM_768: *type = WC_ML_KEM_768; break; #endif - #ifdef WOLFSSL_WC_ML_KEM_1024 + #ifndef WOLFSSL_NO_ML_KEM_1024 case WOLFSSL_ML_KEM_1024: *type = WC_ML_KEM_1024; break; @@ -9694,15 +9694,15 @@ static int TLSX_KeyShare_IsSupported(int namedGroup) #ifdef WOLFSSL_HAVE_KYBER #ifndef WOLFSSL_NO_ML_KEM #ifdef WOLFSSL_WC_KYBER - #ifdef WOLFSSL_WC_ML_KEM_512 + #ifndef WOLFSSL_NO_ML_KEM_512 case WOLFSSL_ML_KEM_512: case WOLFSSL_P256_ML_KEM_512: #endif - #ifdef WOLFSSL_WC_ML_KEM_768 + #ifndef WOLFSSL_NO_ML_KEM_768 case WOLFSSL_ML_KEM_768: case WOLFSSL_P384_ML_KEM_768: #endif - #ifdef WOLFSSL_WC_ML_KEM_1024 + #ifndef WOLFSSL_NO_ML_KEM_1024 case WOLFSSL_ML_KEM_1024: case WOLFSSL_P521_ML_KEM_1024: #endif @@ -9816,15 +9816,15 @@ static const word16 preferredGroup[] = { #endif #ifndef WOLFSSL_NO_ML_KEM #ifdef WOLFSSL_WC_KYBER - #ifdef WOLFSSL_WC_ML_KEM_512 + #ifndef WOLFSSL_NO_ML_KEM_512 WOLFSSL_ML_KEM_512, WOLFSSL_P256_ML_KEM_512, #endif - #ifdef WOLFSSL_WC_ML_KEM_768 + #ifndef WOLFSSL_NO_ML_KEM_768 WOLFSSL_ML_KEM_768, WOLFSSL_P384_ML_KEM_768, #endif - #ifdef WOLFSSL_WC_ML_KEM_1024 + #ifndef WOLFSSL_NO_ML_KEM_1024 WOLFSSL_ML_KEM_1024, WOLFSSL_P521_ML_KEM_1024, #endif @@ -13484,7 +13484,7 @@ static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions) #ifdef WOLFSSL_HAVE_KYBER #ifndef WOLFSSL_NO_ML_KEM #ifdef WOLFSSL_WC_KYBER -#ifdef WOLFSSL_WC_ML_KEM_512 +#ifndef WOLFSSL_NO_ML_KEM_512 if (ret == WOLFSSL_SUCCESS) ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_ML_KEM_512, ssl->heap); @@ -13492,7 +13492,7 @@ static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions) ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_ML_KEM_512, ssl->heap); #endif -#ifdef WOLFSSL_WC_ML_KEM_768 +#ifndef WOLFSSL_NO_ML_KEM_768 if (ret == WOLFSSL_SUCCESS) ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_ML_KEM_768, ssl->heap); @@ -13500,7 +13500,7 @@ static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions) ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_ML_KEM_768, ssl->heap); #endif -#ifdef WOLFSSL_WC_ML_KEM_1024 +#ifndef WOLFSSL_NO_ML_KEM_1024 if (ret == WOLFSSL_SUCCESS) ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_ML_KEM_1024, ssl->heap); diff --git a/tests/api.c b/tests/api.c index 8136bdbc9b..54937721a6 100644 --- a/tests/api.c +++ b/tests/api.c @@ -95512,7 +95512,11 @@ static int test_dtls13_frag_ch_pq(void) const char *test_str = "test"; int test_str_size; byte buf[255]; +#ifdef WOLFSSL_KYBER_ORIGINAL int group = WOLFSSL_KYBER_LEVEL5; +#else + int group = WOLFSSL_ML_KEM_1024; +#endif XMEMSET(&test_ctx, 0, sizeof(test_ctx)); ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, @@ -95522,8 +95526,13 @@ static int test_dtls13_frag_ch_pq(void) ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS); ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS); ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); +#ifdef WOLFSSL_KYBER_ORIGINAL ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5"); ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5"); +#else + ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "ML_KEM_1024"); + ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "ML_KEM_1024"); +#endif test_str_size = XSTRLEN("test") + 1; ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size); ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);