-
Notifications
You must be signed in to change notification settings - Fork 51
Add platform setup and teardown support #165
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
Changes from all commits
f9d17b3
7c78056
9a36b87
93b8992
3781f08
4bd4bca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
#include "mbedtls/cipher.h" | ||
#include "mbedtls/entropy.h" | ||
#include "mbedtls/ctr_drbg.h" | ||
#include "mbedtls/platform.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case it's better to have |
||
|
||
/** | ||
* This class implements the logic to demonstrate authenticated encryption using | ||
|
@@ -34,7 +35,7 @@ class Authcrypt | |
/** | ||
* Construct an Authcrypt instance | ||
*/ | ||
Authcrypt(); | ||
Authcrypt(mbedtls_platform_context* platform_ctx); | ||
|
||
/** | ||
* Free any allocated resources | ||
|
@@ -103,6 +104,11 @@ class Authcrypt | |
* The block cipher configuration | ||
*/ | ||
mbedtls_cipher_context_t cipher; | ||
|
||
/** | ||
* The platform context | ||
*/ | ||
mbedtls_platform_context* _platform_ctx; | ||
}; | ||
|
||
#endif /* _AUTHCRYPT_H_ */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -300,7 +300,7 @@ typedef struct { | |
rsa, dhm, ecdsa, ecdh; | ||
} todo_list; | ||
|
||
static int benchmark( int argc, char *argv[] ) | ||
static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
int i; | ||
unsigned char tmp[200]; | ||
|
@@ -309,7 +309,10 @@ static int benchmark( int argc, char *argv[] ) | |
#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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An application like this test will not directly call a cryptographic function. The cryptographic functions should have a way of accessing the context like a |
||
// Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information. | ||
(void)ctx; | ||
if( argc <= 1 ) | ||
{ | ||
memset( &todo, 1, sizeof( todo ) ); | ||
|
@@ -651,17 +654,17 @@ static int benchmark( int argc, char *argv[] ) | |
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 | ||
|
@@ -676,43 +679,43 @@ static int benchmark( int argc, char *argv[] ) | |
|
||
#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 ) | ||
mbedtls_exit(1); | ||
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 | ||
} | ||
|
@@ -771,13 +774,13 @@ static int benchmark( int argc, char *argv[] ) | |
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", | ||
|
@@ -810,7 +813,7 @@ static int benchmark( int argc, char *argv[] ) | |
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", | ||
|
@@ -832,7 +835,7 @@ static int benchmark( int argc, char *argv[] ) | |
mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size, | ||
tmp, &sig_len, myrand, NULL ) != 0 ) | ||
{ | ||
mbedtls_exit( 1 ); | ||
return( 1 ); | ||
} | ||
ecp_clear_precomputed( &ecdsa.grp ); | ||
|
||
|
@@ -868,7 +871,7 @@ static int benchmark( int argc, char *argv[] ) | |
myrand, NULL ) != 0 || | ||
mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ) | ||
{ | ||
mbedtls_exit( 1 ); | ||
return( 1 ); | ||
} | ||
ecp_clear_precomputed( &ecdh.grp ); | ||
|
||
|
@@ -890,7 +893,7 @@ static int benchmark( int argc, char *argv[] ) | |
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", | ||
|
@@ -916,7 +919,7 @@ static int benchmark( int argc, char *argv[] ) | |
mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ), | ||
myrand, NULL ) != 0 ) | ||
{ | ||
mbedtls_exit( 1 ); | ||
return( 1 ); | ||
} | ||
ecp_clear_precomputed( &ecdh.grp ); | ||
|
||
|
@@ -938,7 +941,7 @@ static int benchmark( int argc, char *argv[] ) | |
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", | ||
|
@@ -961,8 +964,20 @@ static int benchmark( int argc, char *argv[] ) | |
} | ||
|
||
int main(void) { | ||
int ret = benchmark(0, NULL); | ||
if (ret != 0) { | ||
mbedtls_printf("Benchmark failed with error %d\r\n", ret); | ||
mbedtls_platform_context platform_ctx; | ||
int exit_code = MBEDTLS_EXIT_FAILURE; | ||
|
||
if((exit_code = mbedtls_platform_setup(&platform_ctx)) != 0) { | ||
printf("Platform initialization failed with error %d\r\n", exit_code); | ||
return MBEDTLS_EXIT_FAILURE; | ||
} | ||
|
||
exit_code = benchmark(0, NULL, &platform_ctx); | ||
if (exit_code != 0) { | ||
mbedtls_printf("Benchmark failed with error %d\r\n", exit_code); | ||
exit_code = MBEDTLS_EXIT_FAILURE; | ||
} | ||
|
||
mbedtls_platform_teardown(&platform_ctx); | ||
return exit_code; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,8 +47,13 @@ static const char hello_str[] = "Hello, world!"; | |
static const unsigned char *hello_buffer = (const unsigned char *) hello_str; | ||
static const size_t hello_len = strlen(hello_str); | ||
|
||
static int example(void) | ||
static int example(mbedtls_platform_context* ctx) | ||
{ | ||
// 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; | ||
|
||
mbedtls_printf("\r\n\r\n"); | ||
|
||
/* | ||
|
@@ -152,8 +157,20 @@ static int example(void) | |
} | ||
|
||
int main() { | ||
int ret = example(); | ||
if (ret != 0) { | ||
mbedtls_printf("Example failed with error %d\r\n", ret); | ||
mbedtls_platform_context platform_ctx; | ||
int exit_code = MBEDTLS_EXIT_FAILURE; | ||
|
||
if((exit_code = mbedtls_platform_setup(&platform_ctx)) != 0) { | ||
printf("Platform initialization failed with error %d\r\n", exit_code); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mbedtls_printf may be a substituted for a custom printer function that may not work if the platform setup fails. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. |
||
return MBEDTLS_EXIT_FAILURE; | ||
} | ||
|
||
exit_code = example(&platform_ctx); | ||
if (exit_code != 0) { | ||
mbedtls_printf("Example failed with error %d\r\n", exit_code); | ||
exit_code = MBEDTLS_EXIT_FAILURE; | ||
} | ||
|
||
mbedtls_platform_teardown(&platform_ctx); | ||
return exit_code; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it required to pass
platform_ctx
to the application(test)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not required, but we do so as a way of showing the user how to access it. It's an approach that we have chosen with Ron after a discussion that can be followed above, on this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be limited to the tests that actually test things from the platform context.