Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build with only montgomery curves (+ DJB configuration) #2013

53 changes: 47 additions & 6 deletions programs/ssl/ssl_client2.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,22 @@

#if !defined(MBEDTLS_ENTROPY_C) || \
!defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
!defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
!defined(MBEDTLS_NET_C)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does Support HMAC_DRBG in the TLS test programs have to do with Build with only montgomery curves? Shouldn't this commit be in a different PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to enable the SSL test programs to at least build in the “DJB-only” configuration. DJB-only means no AES and therefore no CTR_DRBG.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

int main( void )
{
mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
"MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
"MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
"MBEDTLS_NET_C not defined.\n");
return( 0 );
}
#elif !( defined(MBEDTLS_CTR_DRBG_C) || \
( defined(MBEDTLS_HMAC_DRBG_C) && ( defined(MBEDTLS_SHA256_C) || \
defined(MBEDTLS_SHA512_C) ) ) )
int main( void )
{
mbedtls_printf("MBEDTLS_CTR_DRBG_C and MBEDTLS_HMAC_DRBG_C not defined, "
"or MBEDTLS_HMAC_DRBG_C defined without "
"MBEDTLS_SHA256_C or MBEDTLS_512_C.\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: SHA512

return( 0 );
}
#else
Expand All @@ -53,6 +63,7 @@ int main( void )
#include "mbedtls/ssl.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/hmac_drbg.h"
#include "mbedtls/certs.h"
#include "mbedtls/x509.h"
#include "mbedtls/error.h"
Expand Down Expand Up @@ -530,7 +541,11 @@ int main( int argc, char *argv[] )
mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
#endif
mbedtls_entropy_context entropy;
#if defined(MBEDTLS_CTR_DRBG_C)
mbedtls_ctr_drbg_context ctr_drbg;
#else
mbedtls_hmac_drbg_context hmac_drbg;
#endif
mbedtls_ssl_context ssl;
mbedtls_ssl_config conf;
mbedtls_ssl_session saved_session;
Expand All @@ -553,7 +568,11 @@ int main( int argc, char *argv[] )
mbedtls_ssl_init( &ssl );
mbedtls_ssl_config_init( &conf );
memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
#if defined(MBEDTLS_CTR_DRBG_C)
mbedtls_ctr_drbg_init( &ctr_drbg );
#else
mbedtls_hmac_drbg_init( &hmac_drbg );
#endif
#if defined(MBEDTLS_X509_CRT_PARSE_C)
mbedtls_x509_crt_init( &cacert );
mbedtls_x509_crt_init( &clicert );
Expand Down Expand Up @@ -1165,11 +1184,24 @@ int main( int argc, char *argv[] )
fflush( stdout );

mbedtls_entropy_init( &entropy );
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
&entropy, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 )
#if defined(MBEDTLS_CTR_DRBG_C)
ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
&entropy, (const unsigned char *) pers,
strlen( pers ) );
#else
ret = mbedtls_hmac_drbg_seed( &hmac_drbg,
#if defined(MBEDTLS_SHA256_C)
mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
#else
mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 ),
#endif
mbedtls_entropy_func,
&entropy, (const unsigned char *) pers,
strlen( pers ) );
#endif
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
mbedtls_printf( " failed\n ! mbedtls_xxx_drbg_seed returned -0x%x\n",
-ret );
goto exit;
}
Expand Down Expand Up @@ -1403,7 +1435,12 @@ int main( int argc, char *argv[] )
}
#endif

#if defined(MBEDTLS_CTR_DRBG_C)
mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
#else
mbedtls_ssl_conf_rng( &conf, mbedtls_hmac_drbg_random, &hmac_drbg );
#endif

mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );

mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
Expand Down Expand Up @@ -2054,7 +2091,11 @@ int main( int argc, char *argv[] )
mbedtls_ssl_session_free( &saved_session );
mbedtls_ssl_free( &ssl );
mbedtls_ssl_config_free( &conf );
#if defined(MBEDTLS_CTR_DRBG_C)
mbedtls_ctr_drbg_free( &ctr_drbg );
#else
mbedtls_hmac_drbg_free( &hmac_drbg );
#endif
mbedtls_entropy_free( &entropy );

#if defined(_WIN32)
Expand Down
65 changes: 54 additions & 11 deletions programs/ssl/ssl_server2.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,22 @@

#if !defined(MBEDTLS_ENTROPY_C) || \
!defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_SRV_C) || \
!defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
!defined(MBEDTLS_NET_C)
int main( void )
{
mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
"MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
"MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
"MBEDTLS_NET_C not defined.\n");
return( 0 );
}
#elif !( defined(MBEDTLS_CTR_DRBG_C) || \
( defined(MBEDTLS_HMAC_DRBG_C) && ( defined(MBEDTLS_SHA256_C) || \
defined(MBEDTLS_SHA512_C) ) ) )
int main( void )
{
mbedtls_printf("MBEDTLS_CTR_DRBG_C and MBEDTLS_HMAC_DRBG_C not defined, "
"or MBEDTLS_HMAC_DRBG_C defined without "
"MBEDTLS_SHA256_C or MBEDTLS_512_C.\n");
return( 0 );
}
#else
Expand All @@ -54,6 +64,7 @@ int main( void )
#include "mbedtls/ssl.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/hmac_drbg.h"
#include "mbedtls/certs.h"
#include "mbedtls/x509.h"
#include "mbedtls/error.h"
Expand Down Expand Up @@ -1183,7 +1194,17 @@ int main( int argc, char *argv[] )
mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
#endif
mbedtls_entropy_context entropy;
#if defined(MBEDTLS_CTR_DRBG_C)
mbedtls_ctr_drbg_context ctr_drbg;
void *const drbg_ctx = &ctr_drbg;
int ( *drbg_func )( void *, unsigned char *, size_t ) =
mbedtls_ctr_drbg_random;
#else
mbedtls_hmac_drbg_context hmac_drbg;
void *const drbg_ctx = &hmac_drbg;
int ( *drbg_func )( void *, unsigned char *, size_t ) =
mbedtls_hmac_drbg_random;
#endif
mbedtls_ssl_context ssl;
mbedtls_ssl_config conf;
#if defined(MBEDTLS_TIMING_C)
Expand Down Expand Up @@ -1242,7 +1263,11 @@ int main( int argc, char *argv[] )
mbedtls_net_init( &listen_fd );
mbedtls_ssl_init( &ssl );
mbedtls_ssl_config_init( &conf );
#if defined(MBEDTLS_CTR_DRBG_C)
mbedtls_ctr_drbg_init( &ctr_drbg );
#else
mbedtls_hmac_drbg_init( &hmac_drbg );
#endif
#if defined(MBEDTLS_X509_CRT_PARSE_C)
mbedtls_x509_crt_init( &cacert );
mbedtls_x509_crt_init( &srvcert );
Expand Down Expand Up @@ -1925,11 +1950,24 @@ int main( int argc, char *argv[] )
fflush( stdout );

mbedtls_entropy_init( &entropy );
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
&entropy, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 )
#if defined(MBEDTLS_CTR_DRBG_C)
ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
&entropy, (const unsigned char *) pers,
strlen( pers ) );
#else
ret = mbedtls_hmac_drbg_seed( &hmac_drbg,
#if defined(MBEDTLS_SHA256_C)
mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
#else
mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 ),
#endif
mbedtls_entropy_func,
&entropy, (const unsigned char *) pers,
strlen( pers ) );
#endif
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
mbedtls_printf( " failed\n ! mbedtls_xxx_drbg_seed returned -0x%x\n",
-ret );
goto exit;
}
Expand Down Expand Up @@ -2217,7 +2255,8 @@ int main( int argc, char *argv[] )
}
#endif

mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
mbedtls_ssl_conf_rng( &conf, drbg_func, drbg_ctx );

mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );

#if defined(MBEDTLS_SSL_CACHE_C)
Expand All @@ -2236,7 +2275,7 @@ int main( int argc, char *argv[] )
if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
{
if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
mbedtls_ctr_drbg_random, &ctr_drbg,
drbg_func, drbg_ctx,
MBEDTLS_CIPHER_AES_256_GCM,
opt.ticket_timeout ) ) != 0 )
{
Expand All @@ -2258,7 +2297,7 @@ int main( int argc, char *argv[] )
if( opt.cookies > 0 )
{
if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
drbg_func, drbg_ctx ) ) != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
goto exit;
Expand Down Expand Up @@ -2405,8 +2444,8 @@ int main( int argc, char *argv[] )
ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
- opt.async_private_error :
opt.async_private_error );
ssl_async_keys.f_rng = mbedtls_ctr_drbg_random;
ssl_async_keys.p_rng = &ctr_drbg;
ssl_async_keys.f_rng = drbg_func;
ssl_async_keys.p_rng = drbg_ctx;
mbedtls_ssl_conf_async_private_cb( &conf,
sign,
decrypt,
Expand Down Expand Up @@ -3083,7 +3122,11 @@ int main( int argc, char *argv[] )

mbedtls_ssl_free( &ssl );
mbedtls_ssl_config_free( &conf );
#if defined(MBEDTLS_CTR_DRBG_C)
mbedtls_ctr_drbg_free( &ctr_drbg );
#else
mbedtls_hmac_drbg_free( &hmac_drbg );
#endif
mbedtls_entropy_free( &entropy );

#if defined(MBEDTLS_SSL_CACHE_C)
Expand Down