From a198d9b247eeccd5d6bab925a9a47c1867230fd3 Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Sun, 13 May 2018 17:53:36 +0300 Subject: [PATCH 1/4] Rewrite changes after rebase Rewrite after a rebase with many conflicts --- benchmark/main.cpp | 289 +++++++++++++++++++++++++-------------------- 1 file changed, 161 insertions(+), 128 deletions(-) diff --git a/benchmark/main.cpp b/benchmark/main.cpp index ae4ef77ac..54040c0bf 100644 --- a/benchmark/main.cpp +++ b/benchmark/main.cpp @@ -300,125 +300,50 @@ typedef struct { rsa, dhm, ecdsa, ecdh; } todo_list; -static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) +static void test_md( const todo_list * todo ) { - int i; unsigned char tmp[200]; - char title[TITLE_LEN]; - todo_list todo; -#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) - unsigned char malloc_buf[HEAP_SIZE] = { 0 }; -#endif - // The call below is used to avoid the "unused parameter" warning. - // The context itself can be used by cryptographic calls which require it. - // Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information. - (void)ctx; - if( argc <= 1 ) - { - memset( &todo, 1, sizeof( todo ) ); - } - else - { - memset( &todo, 0, sizeof( todo ) ); - - for( i = 1; i < argc; i++ ) - { - if( strcmp( argv[i], "md4" ) == 0 ) - todo.md4 = 1; - else if( strcmp( argv[i], "md5" ) == 0 ) - todo.md5 = 1; - else if( strcmp( argv[i], "ripemd160" ) == 0 ) - todo.ripemd160 = 1; - else if( strcmp( argv[i], "sha1" ) == 0 ) - todo.sha1 = 1; - else if( strcmp( argv[i], "sha256" ) == 0 ) - todo.sha256 = 1; - else if( strcmp( argv[i], "sha512" ) == 0 ) - todo.sha512 = 1; - else if( strcmp( argv[i], "arc4" ) == 0 ) - todo.arc4 = 1; - else if( strcmp( argv[i], "des3" ) == 0 ) - todo.des3 = 1; - else if( strcmp( argv[i], "des" ) == 0 ) - todo.des = 1; - else if( strcmp( argv[i], "aes_cbc" ) == 0 ) - todo.aes_cbc = 1; - else if( strcmp( argv[i], "aes_ctr" ) == 0 ) - todo.aes_ctr = 1; - else if( strcmp( argv[i], "aes_gcm" ) == 0 ) - todo.aes_gcm = 1; - else if( strcmp( argv[i], "aes_ccm" ) == 0 ) - todo.aes_ccm = 1; - else if( strcmp( argv[i], "aes_cmac" ) == 0 ) - todo.aes_cmac = 1; - else if( strcmp( argv[i], "des3_cmac" ) == 0 ) - todo.des3_cmac = 1; - else if( strcmp( argv[i], "camellia" ) == 0 ) - todo.camellia = 1; - else if( strcmp( argv[i], "blowfish" ) == 0 ) - todo.blowfish = 1; - else if( strcmp( argv[i], "havege" ) == 0 ) - todo.havege = 1; - else if( strcmp( argv[i], "ctr_drbg" ) == 0 ) - todo.ctr_drbg = 1; - else if( strcmp( argv[i], "hmac_drbg" ) == 0 ) - todo.hmac_drbg = 1; - else if( strcmp( argv[i], "rsa" ) == 0 ) - todo.rsa = 1; - else if( strcmp( argv[i], "dhm" ) == 0 ) - todo.dhm = 1; - else if( strcmp( argv[i], "ecdsa" ) == 0 ) - todo.ecdsa = 1; - else if( strcmp( argv[i], "ecdh" ) == 0 ) - todo.ecdh = 1; - else - { - mbedtls_printf( "Unrecognized option: %s\r\n", argv[i] ); - mbedtls_printf( "Available options: " OPTIONS ); - } - } - } - - mbedtls_printf( "\r\n\r\n" ); - -#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) - mbedtls_memory_buffer_alloc_init( malloc_buf, sizeof( malloc_buf ) ); -#endif - memset( buf, 0xAA, sizeof( buf ) ); memset( tmp, 0xBB, sizeof( tmp ) ); #if defined(MBEDTLS_MD4_C) - if( todo.md4 ) + if( todo->md4 ) TIME_AND_TSC( "MD4", mbedtls_md4( buf, BUFSIZE, tmp ) ); #endif #if defined(MBEDTLS_MD5_C) - if( todo.md5 ) + if( todo->md5 ) TIME_AND_TSC( "MD5", mbedtls_md5( buf, BUFSIZE, tmp ) ); #endif #if defined(MBEDTLS_RIPEMD160_C) - if( todo.ripemd160 ) + if( todo->ripemd160 ) TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160( buf, BUFSIZE, tmp ) ); #endif #if defined(MBEDTLS_SHA1_C) - if( todo.sha1 ) + if( todo->sha1 ) TIME_AND_TSC( "SHA-1", mbedtls_sha1( buf, BUFSIZE, tmp ) ); #endif #if defined(MBEDTLS_SHA256_C) - if( todo.sha256 ) + if( todo->sha256 ) TIME_AND_TSC( "SHA-256", mbedtls_sha256( buf, BUFSIZE, tmp, 0 ) ); #endif #if defined(MBEDTLS_SHA512_C) - if( todo.sha512 ) + if( todo->sha512 ) TIME_AND_TSC( "SHA-512", mbedtls_sha512( buf, BUFSIZE, tmp, 0 ) ); #endif +} + +static void test_crypt( const todo_list * todo ) +{ + unsigned char tmp[200]; + char title[TITLE_LEN]; + memset( tmp, 0xBB, sizeof( tmp ) ); #if defined(MBEDTLS_ARC4_C) - if( todo.arc4 ) + if( todo->arc4 ) { mbedtls_arc4_context arc4; mbedtls_arc4_init( &arc4 ); @@ -429,7 +354,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #endif #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC) - if( todo.des3 ) + if( todo->des3 ) { mbedtls_des3_context des3; mbedtls_des3_init( &des3 ); @@ -439,7 +364,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) mbedtls_des3_free( &des3 ); } - if( todo.des ) + if( todo->des ) { mbedtls_des_context des; mbedtls_des_init( &des ); @@ -449,7 +374,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) mbedtls_des_free( &des ); } #if defined(MBEDTLS_CMAC_C) - if( todo.des3_cmac ) + if( todo->des3_cmac ) { unsigned char output[8]; const mbedtls_cipher_info_t *cipher_info; @@ -468,7 +393,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #if defined(MBEDTLS_AES_C) #if defined(MBEDTLS_CIPHER_MODE_CBC) - if( todo.aes_cbc ) + if( todo->aes_cbc ) { int keysize; mbedtls_aes_context aes; @@ -489,7 +414,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #endif #if defined(MBEDTLS_CIPHER_MODE_CTR) - if( todo.aes_ctr ) + if( todo->aes_ctr ) { int keysize; size_t nc_offset = 0; @@ -512,7 +437,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #endif #if defined(MBEDTLS_GCM_C) - if( todo.aes_gcm ) + if( todo->aes_gcm ) { int keysize; mbedtls_gcm_context gcm; @@ -535,7 +460,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) } #endif #if defined(MBEDTLS_CCM_C) - if( todo.aes_ccm ) + if( todo->aes_ccm ) { int keysize; mbedtls_ccm_context ccm; @@ -558,7 +483,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) } #endif #if defined(MBEDTLS_CMAC_C) - if( todo.aes_cmac ) + if( todo->aes_cmac ) { unsigned char output[16]; const mbedtls_cipher_info_t *cipher_info; @@ -591,7 +516,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #endif #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC) - if( todo.camellia ) + if( todo->camellia ) { int keysize; mbedtls_camellia_context camellia; @@ -613,7 +538,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #endif #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC) - if( todo.blowfish ) + if( todo->blowfish ) { int keysize; mbedtls_blowfish_context blowfish; @@ -636,8 +561,15 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) } #endif +} + +static void test_rng( const todo_list * todo ) +{ + unsigned char tmp[200]; + memset( tmp, 0xBB, sizeof( tmp ) ); + #if defined(MBEDTLS_HAVEGE_C) - if( todo.havege ) + if( todo->havege ) { mbedtls_havege_state hs; mbedtls_havege_init( &hs ); @@ -647,30 +579,30 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #endif #if defined(MBEDTLS_CTR_DRBG_C) - if( todo.ctr_drbg ) + if( todo->ctr_drbg ) { mbedtls_ctr_drbg_context ctr_drbg; mbedtls_ctr_drbg_init( &ctr_drbg ); if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) - return(1); + mbedtls_exit(1); TIME_AND_TSC( "CTR_DRBG (NOPR)", if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) - return(1) ); + mbedtls_exit(1) ); if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) - return(1); + mbedtls_exit(1); mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON ); TIME_AND_TSC( "CTR_DRBG (PR)", if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) - return(1) ); + mbedtls_exit(1) ); mbedtls_ctr_drbg_free( &ctr_drbg ); } #endif #if defined(MBEDTLS_HMAC_DRBG_C) - if( todo.hmac_drbg ) + if( todo->hmac_drbg ) { mbedtls_hmac_drbg_context hmac_drbg; const mbedtls_md_info_t *md_info; @@ -679,51 +611,58 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #if defined(MBEDTLS_SHA1_C) if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) - return(1); + mbedtls_exit(1); if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) return(1); TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)", if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) - return(1) ); + mbedtls_exit(1) ); mbedtls_hmac_drbg_free( &hmac_drbg ); if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) - return(1); + mbedtls_exit(1); mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg, MBEDTLS_HMAC_DRBG_PR_ON ); TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)", if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) - return(1) ); + mbedtls_exit(1) ); mbedtls_hmac_drbg_free( &hmac_drbg ); #endif #if defined(MBEDTLS_SHA256_C) if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL ) - return(1); + mbedtls_exit(1); if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) - return(1); + mbedtls_exit(1); TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)", if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) - return(1) ); + mbedtls_exit(1) ); mbedtls_hmac_drbg_free( &hmac_drbg ); if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) - return(1); + mbedtls_exit(1); mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg, MBEDTLS_HMAC_DRBG_PR_ON ); TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)", if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) - return(1) ); + mbedtls_exit(1) ); mbedtls_hmac_drbg_free( &hmac_drbg ); #endif } #endif +} + +static void test_pk( const todo_list * todo ) +{ + unsigned char tmp[200]; + char title[TITLE_LEN]; + memset( tmp, 0xBB, sizeof( tmp ) ); #if defined(MBEDTLS_RSA_C) && \ defined(MBEDTLS_PEM_PARSE_C) && defined(MBEDTLS_PK_PARSE_C) - if( todo.rsa ) + if( todo->rsa ) { mbedtls_pk_context pk; mbedtls_rsa_context *rsa; @@ -753,7 +692,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #endif #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C) - if( todo.dhm ) + if( todo->dhm ) { int dhm_sizes[] = { 2048, 3072 }; const char *dhm_P[] = { @@ -774,13 +713,13 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) if( mbedtls_mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 || mbedtls_mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 ) { - return( 1 ); + mbedtls_exit( 1 ); } dhm.len = mbedtls_mpi_size( &dhm.P ); mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL ); if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 ) - return( 1 ); + mbedtls_exit( 1 ); mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] ); TIME_PUBLIC( title, "handshake", @@ -798,7 +737,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #endif #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && defined(ENABLE_ECDSA) - if( todo.ecdsa ) + if( todo->ecdsa ) { mbedtls_ecdsa_context ecdsa; const mbedtls_ecp_curve_info *curve_info; @@ -813,7 +752,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) mbedtls_ecdsa_init( &ecdsa ); if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ) - return( 1 ); + mbedtls_exit( 1 ); ecp_clear_precomputed( &ecdsa.grp ); mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s", @@ -835,7 +774,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size, tmp, &sig_len, myrand, NULL ) != 0 ) { - return( 1 ); + mbedtls_exit( 1 ); } ecp_clear_precomputed( &ecdsa.grp ); @@ -851,7 +790,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #endif #if defined(MBEDTLS_ECDH_C) - if( todo.ecdh ) + if( todo->ecdh ) { mbedtls_ecdh_context ecdh; #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) @@ -871,7 +810,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) myrand, NULL ) != 0 || mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ) { - return( 1 ); + mbedtls_exit( 1 ); } ecp_clear_precomputed( &ecdh.grp ); @@ -893,7 +832,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 || mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 ) { - return( 1 ); + mbedtls_exit( 1 ); } TIME_PUBLIC( "ECDHE-Curve25519", "handshake", @@ -919,7 +858,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ), myrand, NULL ) != 0 ) { - return( 1 ); + mbedtls_exit( 1 ); } ecp_clear_precomputed( &ecdh.grp ); @@ -941,7 +880,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) myrand, NULL ) != 0 || mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 ) { - return( 1 ); + mbedtls_exit( 1 ); } TIME_PUBLIC( "ECDH-Curve25519", "handshake", @@ -954,6 +893,100 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) } #endif + +} + +static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) +{ + int i; + unsigned char tmp[200]; + char title[TITLE_LEN]; + todo_list todo; +#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) + unsigned char malloc_buf[HEAP_SIZE] = { 0 }; +#endif + // The call below is used to avoid the "unused parameter" warning. + // The context itself can be used by cryptographic calls which require it. + // Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information. + (void)ctx; + if( argc <= 1 ) + { + memset( &todo, 1, sizeof( todo ) ); + } + else + { + memset( &todo, 0, sizeof( todo ) ); + + for( i = 1; i < argc; i++ ) + { + if( strcmp( argv[i], "md4" ) == 0 ) + todo.md4 = 1; + else if( strcmp( argv[i], "md5" ) == 0 ) + todo.md5 = 1; + else if( strcmp( argv[i], "ripemd160" ) == 0 ) + todo.ripemd160 = 1; + else if( strcmp( argv[i], "sha1" ) == 0 ) + todo.sha1 = 1; + else if( strcmp( argv[i], "sha256" ) == 0 ) + todo.sha256 = 1; + else if( strcmp( argv[i], "sha512" ) == 0 ) + todo.sha512 = 1; + else if( strcmp( argv[i], "arc4" ) == 0 ) + todo.arc4 = 1; + else if( strcmp( argv[i], "des3" ) == 0 ) + todo.des3 = 1; + else if( strcmp( argv[i], "des" ) == 0 ) + todo.des = 1; + else if( strcmp( argv[i], "aes_cbc" ) == 0 ) + todo.aes_cbc = 1; + else if( strcmp( argv[i], "aes_ctr" ) == 0 ) + todo.aes_ctr = 1; + else if( strcmp( argv[i], "aes_gcm" ) == 0 ) + todo.aes_gcm = 1; + else if( strcmp( argv[i], "aes_ccm" ) == 0 ) + todo.aes_ccm = 1; + else if( strcmp( argv[i], "aes_cmac" ) == 0 ) + todo.aes_cmac = 1; + else if( strcmp( argv[i], "des3_cmac" ) == 0 ) + todo.des3_cmac = 1; + else if( strcmp( argv[i], "camellia" ) == 0 ) + todo.camellia = 1; + else if( strcmp( argv[i], "blowfish" ) == 0 ) + todo.blowfish = 1; + else if( strcmp( argv[i], "havege" ) == 0 ) + todo.havege = 1; + else if( strcmp( argv[i], "ctr_drbg" ) == 0 ) + todo.ctr_drbg = 1; + else if( strcmp( argv[i], "hmac_drbg" ) == 0 ) + todo.hmac_drbg = 1; + else if( strcmp( argv[i], "rsa" ) == 0 ) + todo.rsa = 1; + else if( strcmp( argv[i], "dhm" ) == 0 ) + todo.dhm = 1; + else if( strcmp( argv[i], "ecdsa" ) == 0 ) + todo.ecdsa = 1; + else if( strcmp( argv[i], "ecdh" ) == 0 ) + todo.ecdh = 1; + else + { + mbedtls_printf( "Unrecognized option: %s\r\n", argv[i] ); + mbedtls_printf( "Available options: " OPTIONS ); + } + } + } + + mbedtls_printf( "\r\n\r\n" ); + +#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) + mbedtls_memory_buffer_alloc_init( malloc_buf, sizeof( malloc_buf ) ); +#endif + memset( buf, 0xAA, sizeof( buf ) ); + + test_md( & todo ); + test_crypt( &todo ); + test_rng( &todo ); + test_pk( &todo ); + mbedtls_printf("\r\nDONE\r\n"); #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) From 4582a93bb291804ca058e4d83f1d3cc466afc422 Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Sun, 13 May 2018 18:12:09 +0300 Subject: [PATCH 2/4] Some minor modifications after refactoring 1. update copyright year. 2. Enable ecdsa. 3. Change `hlen` parameter in the ecdsa test to byte size, as this is what the API expects. 4. Remove unused variables. --- benchmark/main.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/benchmark/main.cpp b/benchmark/main.cpp index 54040c0bf..fbda59a2d 100644 --- a/benchmark/main.cpp +++ b/benchmark/main.cpp @@ -1,7 +1,7 @@ /* * Benchmark demonstration program * - * Copyright (C) 2006-2016, Arm Limited, All Rights Reserved + * Copyright (C) 2006-2018, Arm Limited, All Rights Reserved * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -145,7 +145,7 @@ /* * Uncomment this line to enable ECDSA benchmark. */ -//#define ENABLE_ECDSA +#define ENABLE_ECDSA /* * For heap usage estimates, we need an estimate of the overhead per allocated @@ -758,7 +758,7 @@ static void test_pk( const todo_list * todo ) mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s", curve_info->name ); TIME_PUBLIC( title, "sign", - ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size, + ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, ( curve_info->bit_size + 7 ) / 8, tmp, &sig_len, myrand, NULL ) ); mbedtls_ecdsa_free( &ecdsa ); @@ -771,7 +771,7 @@ static void test_pk( const todo_list * todo ) mbedtls_ecdsa_init( &ecdsa ); if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 || - mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size, + mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, ( curve_info->bit_size + 7 ) / 8, tmp, &sig_len, myrand, NULL ) != 0 ) { mbedtls_exit( 1 ); @@ -781,7 +781,7 @@ static void test_pk( const todo_list * todo ) mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s", curve_info->name ); TIME_PUBLIC( title, "verify", - ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size, + ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, ( curve_info->bit_size + 7 ) / 8, tmp, sig_len ) ); mbedtls_ecdsa_free( &ecdsa ); @@ -899,8 +899,6 @@ static void test_pk( const todo_list * todo ) static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) { int i; - unsigned char tmp[200]; - char title[TITLE_LEN]; todo_list todo; #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) unsigned char malloc_buf[HEAP_SIZE] = { 0 }; From d86a33b6d8a074cc76b6db9a48477ddd276f7a91 Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Sun, 13 May 2018 18:19:51 +0300 Subject: [PATCH 3/4] Add platform context to the test functions Add the platform context to the test functions, as these are the functions handling the cryptographic modules. --- benchmark/main.cpp | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/benchmark/main.cpp b/benchmark/main.cpp index fbda59a2d..46a43f915 100644 --- a/benchmark/main.cpp +++ b/benchmark/main.cpp @@ -300,9 +300,13 @@ typedef struct { rsa, dhm, ecdsa, ecdh; } todo_list; -static void test_md( const todo_list * todo ) +static void test_md( const todo_list * todo, mbedtls_platform_context* ctx ) { unsigned char tmp[200]; + // The call below is used to avoid the "unused parameter" warning. + // The context itself can be used by cryptographic calls which require it. + // Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information. + (void)ctx; memset( tmp, 0xBB, sizeof( tmp ) ); #if defined(MBEDTLS_MD4_C) @@ -336,10 +340,14 @@ static void test_md( const todo_list * todo ) #endif } -static void test_crypt( const todo_list * todo ) +static void test_crypt( const todo_list * todo, mbedtls_platform_context* ctx ) { unsigned char tmp[200]; char title[TITLE_LEN]; + // The call below is used to avoid the "unused parameter" warning. + // The context itself can be used by cryptographic calls which require it. + // Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information. + (void)ctx; memset( tmp, 0xBB, sizeof( tmp ) ); #if defined(MBEDTLS_ARC4_C) @@ -563,9 +571,13 @@ static void test_crypt( const todo_list * todo ) } -static void test_rng( const todo_list * todo ) +static void test_rng( const todo_list * todo, mbedtls_platform_context* ctx ) { unsigned char tmp[200]; + // The call below is used to avoid the "unused parameter" warning. + // The context itself can be used by cryptographic calls which require it. + // Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information. + (void)ctx; memset( tmp, 0xBB, sizeof( tmp ) ); #if defined(MBEDTLS_HAVEGE_C) @@ -654,10 +666,14 @@ static void test_rng( const todo_list * todo ) #endif } -static void test_pk( const todo_list * todo ) +static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx ) { unsigned char tmp[200]; char title[TITLE_LEN]; + // The call below is used to avoid the "unused parameter" warning. + // The context itself can be used by cryptographic calls which require it. + // Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information. + (void)ctx; memset( tmp, 0xBB, sizeof( tmp ) ); #if defined(MBEDTLS_RSA_C) && \ @@ -903,10 +919,7 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) unsigned char malloc_buf[HEAP_SIZE] = { 0 }; #endif - // The call below is used to avoid the "unused parameter" warning. - // The context itself can be used by cryptographic calls which require it. - // Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information. - (void)ctx; + if( argc <= 1 ) { memset( &todo, 1, sizeof( todo ) ); @@ -980,10 +993,10 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #endif memset( buf, 0xAA, sizeof( buf ) ); - test_md( & todo ); - test_crypt( &todo ); - test_rng( &todo ); - test_pk( &todo ); + test_md( &todo, ctx ); + test_crypt( &todo, ctx ); + test_rng( &todo, ctx ); + test_pk( &todo, ctx ); mbedtls_printf("\r\nDONE\r\n"); From 33786fe5581128a055bc0d2f0f0d1cb85368d22c Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Mon, 14 May 2018 11:42:49 +0300 Subject: [PATCH 4/4] make test function return error code Have the test functions return error code, to avoid resource leak of the platform context, and for cleaner exit. --- benchmark/main.cpp | 67 +++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/benchmark/main.cpp b/benchmark/main.cpp index 46a43f915..c4e1126b8 100644 --- a/benchmark/main.cpp +++ b/benchmark/main.cpp @@ -300,7 +300,7 @@ typedef struct { rsa, dhm, ecdsa, ecdh; } todo_list; -static void test_md( const todo_list * todo, mbedtls_platform_context* ctx ) +static int test_md( const todo_list * todo, mbedtls_platform_context* ctx ) { unsigned char tmp[200]; // The call below is used to avoid the "unused parameter" warning. @@ -338,9 +338,10 @@ static void test_md( const todo_list * todo, mbedtls_platform_context* ctx ) if( todo->sha512 ) TIME_AND_TSC( "SHA-512", mbedtls_sha512( buf, BUFSIZE, tmp, 0 ) ); #endif + return ( 0 ); } -static void test_crypt( const todo_list * todo, mbedtls_platform_context* ctx ) +static int test_crypt( const todo_list * todo, mbedtls_platform_context* ctx ) { unsigned char tmp[200]; char title[TITLE_LEN]; @@ -569,9 +570,10 @@ static void test_crypt( const todo_list * todo, mbedtls_platform_context* ctx ) } #endif + return ( 0 ); } -static void test_rng( const todo_list * todo, mbedtls_platform_context* ctx ) +static int test_rng( const todo_list * todo, mbedtls_platform_context* ctx ) { unsigned char tmp[200]; // The call below is used to avoid the "unused parameter" warning. @@ -598,17 +600,17 @@ static void test_rng( const todo_list * todo, mbedtls_platform_context* ctx ) mbedtls_ctr_drbg_init( &ctr_drbg ); if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) - mbedtls_exit(1); + return(1); TIME_AND_TSC( "CTR_DRBG (NOPR)", if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) - mbedtls_exit(1) ); + return(1) ); if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) - mbedtls_exit(1); + return(1); mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON ); TIME_AND_TSC( "CTR_DRBG (PR)", if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) - mbedtls_exit(1) ); + return(1) ); mbedtls_ctr_drbg_free( &ctr_drbg ); } #endif @@ -623,50 +625,51 @@ static void test_rng( const todo_list * todo, mbedtls_platform_context* ctx ) #if defined(MBEDTLS_SHA1_C) if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) - mbedtls_exit(1); + return(1); if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) return(1); TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)", if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) - mbedtls_exit(1) ); + return(1) ); mbedtls_hmac_drbg_free( &hmac_drbg ); if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) - mbedtls_exit(1); + return(1); mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg, MBEDTLS_HMAC_DRBG_PR_ON ); TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)", if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) - mbedtls_exit(1) ); + return(1) ); mbedtls_hmac_drbg_free( &hmac_drbg ); #endif #if defined(MBEDTLS_SHA256_C) if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL ) - mbedtls_exit(1); + return(1); if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) - mbedtls_exit(1); + return(1); TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)", if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) - mbedtls_exit(1) ); + return(1) ); mbedtls_hmac_drbg_free( &hmac_drbg ); if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) - mbedtls_exit(1); + return(1); mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg, MBEDTLS_HMAC_DRBG_PR_ON ); TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)", if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) - mbedtls_exit(1) ); + return(1) ); mbedtls_hmac_drbg_free( &hmac_drbg ); #endif } #endif + return (0 ); } -static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx ) +static int test_pk( const todo_list * todo, mbedtls_platform_context* ctx ) { unsigned char tmp[200]; char title[TITLE_LEN]; @@ -729,13 +732,13 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx ) if( mbedtls_mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 || mbedtls_mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 ) { - mbedtls_exit( 1 ); + return( 1 ); } dhm.len = mbedtls_mpi_size( &dhm.P ); mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL ); if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 ) - mbedtls_exit( 1 ); + return( 1 ); mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] ); TIME_PUBLIC( title, "handshake", @@ -768,7 +771,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx ) mbedtls_ecdsa_init( &ecdsa ); if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ) - mbedtls_exit( 1 ); + return( 1 ); ecp_clear_precomputed( &ecdsa.grp ); mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s", @@ -790,7 +793,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx ) mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, ( curve_info->bit_size + 7 ) / 8, tmp, &sig_len, myrand, NULL ) != 0 ) { - mbedtls_exit( 1 ); + return( 1 ); } ecp_clear_precomputed( &ecdsa.grp ); @@ -826,7 +829,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx ) myrand, NULL ) != 0 || mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ) { - mbedtls_exit( 1 ); + return( 1 ); } ecp_clear_precomputed( &ecdh.grp ); @@ -848,7 +851,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx ) if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 || mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 ) { - mbedtls_exit( 1 ); + return( 1 ); } TIME_PUBLIC( "ECDHE-Curve25519", "handshake", @@ -874,7 +877,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx ) mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ), myrand, NULL ) != 0 ) { - mbedtls_exit( 1 ); + return( 1 ); } ecp_clear_precomputed( &ecdh.grp ); @@ -896,7 +899,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx ) myrand, NULL ) != 0 || mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 ) { - mbedtls_exit( 1 ); + return( 1 ); } TIME_PUBLIC( "ECDH-Curve25519", "handshake", @@ -908,7 +911,7 @@ static void test_pk( const todo_list * todo, mbedtls_platform_context* ctx ) #endif } #endif - + return ( 0 ); } @@ -993,10 +996,14 @@ static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) #endif memset( buf, 0xAA, sizeof( buf ) ); - test_md( &todo, ctx ); - test_crypt( &todo, ctx ); - test_rng( &todo, ctx ); - test_pk( &todo, ctx ); + if( test_md( &todo, ctx ) != 0) + return ( 1 ); + if( test_crypt( &todo, ctx ) != 0) + return ( 1 ); + if( test_rng( &todo, ctx ) != 0) + return ( 1 ); + if( test_pk( &todo, ctx ) != 0) + return ( 1 ); mbedtls_printf("\r\nDONE\r\n");