Skip to content

Commit 76832ce

Browse files
authored
Merge pull request #165 from ARMmbed/feature-platform-init
Add platform setup and teardown support
2 parents b4f6045 + 4bd4bca commit 76832ce

File tree

8 files changed

+109
-44
lines changed

8 files changed

+109
-44
lines changed

authcrypt/authcrypt.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
#include "mbedtls/debug.h"
2929
#endif
3030

31-
#include "mbedtls/platform.h"
32-
3331
#include <string.h>
3432

3533
const unsigned char Authcrypt::secret_key[16] = {
@@ -41,8 +39,11 @@ const char Authcrypt::message[] = "Some things are better left unread";
4139

4240
const char Authcrypt::metadata[] = "eg sequence number, routing info";
4341

44-
Authcrypt::Authcrypt()
42+
Authcrypt::Authcrypt(mbedtls_platform_context* platform_ctx)
4543
{
44+
// The platform context can be used by cryptographic calls which require it.
45+
// Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information.
46+
_platform_ctx = platform_ctx;
4647
memset(ciphertext, 0, sizeof(ciphertext));
4748
memset(decrypted, 0, sizeof(decrypted));
4849

authcrypt/authcrypt.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "mbedtls/cipher.h"
2424
#include "mbedtls/entropy.h"
2525
#include "mbedtls/ctr_drbg.h"
26+
#include "mbedtls/platform.h"
2627

2728
/**
2829
* This class implements the logic to demonstrate authenticated encryption using
@@ -34,7 +35,7 @@ class Authcrypt
3435
/**
3536
* Construct an Authcrypt instance
3637
*/
37-
Authcrypt();
38+
Authcrypt(mbedtls_platform_context* platform_ctx);
3839

3940
/**
4041
* Free any allocated resources
@@ -103,6 +104,11 @@ class Authcrypt
103104
* The block cipher configuration
104105
*/
105106
mbedtls_cipher_context_t cipher;
107+
108+
/**
109+
* The platform context
110+
*/
111+
mbedtls_platform_context* _platform_ctx;
106112
};
107113

108114
#endif /* _AUTHCRYPT_H_ */

authcrypt/main.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,23 @@
2424
#include "mbedtls/platform.h"
2525

2626
int main() {
27-
int exit_code = MBEDTLS_EXIT_SUCCESS;
28-
Authcrypt *authcrypt = new Authcrypt();
27+
mbedtls_platform_context platform_ctx;
28+
int exit_code = MBEDTLS_EXIT_FAILURE;
2929

30-
if (authcrypt->run() != 0) {
30+
if((exit_code = mbedtls_platform_setup(&platform_ctx)) != 0) {
31+
printf("Platform initialization failed with error %d\r\n", exit_code);
32+
return MBEDTLS_EXIT_FAILURE;
33+
}
34+
35+
Authcrypt *authcrypt = new Authcrypt(&platform_ctx);
36+
37+
if ((exit_code = authcrypt->run()) != 0) {
38+
mbedtls_printf("Example failed with error %d\r\n", exit_code);
3139
exit_code = MBEDTLS_EXIT_FAILURE;
32-
mbedtls_printf("\r\nFAIL\r\n");
3340
}
3441

3542
delete authcrypt;
3643

44+
mbedtls_platform_teardown(&platform_ctx);
3745
return exit_code;
3846
}

benchmark/main.cpp

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ typedef struct {
300300
rsa, dhm, ecdsa, ecdh;
301301
} todo_list;
302302

303-
static int benchmark( int argc, char *argv[] )
303+
static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
304304
{
305305
int i;
306306
unsigned char tmp[200];
@@ -309,7 +309,10 @@ static int benchmark( int argc, char *argv[] )
309309
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
310310
unsigned char malloc_buf[HEAP_SIZE] = { 0 };
311311
#endif
312-
312+
// The call below is used to avoid the "unused parameter" warning.
313+
// The context itself can be used by cryptographic calls which require it.
314+
// Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information.
315+
(void)ctx;
313316
if( argc <= 1 )
314317
{
315318
memset( &todo, 1, sizeof( todo ) );
@@ -651,17 +654,17 @@ static int benchmark( int argc, char *argv[] )
651654
mbedtls_ctr_drbg_init( &ctr_drbg );
652655

653656
if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
654-
mbedtls_exit(1);
657+
return(1);
655658
TIME_AND_TSC( "CTR_DRBG (NOPR)",
656659
if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
657-
mbedtls_exit(1) );
660+
return(1) );
658661

659662
if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
660-
mbedtls_exit(1);
663+
return(1);
661664
mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
662665
TIME_AND_TSC( "CTR_DRBG (PR)",
663666
if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
664-
mbedtls_exit(1) );
667+
return(1) );
665668
mbedtls_ctr_drbg_free( &ctr_drbg );
666669
}
667670
#endif
@@ -676,43 +679,43 @@ static int benchmark( int argc, char *argv[] )
676679

677680
#if defined(MBEDTLS_SHA1_C)
678681
if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
679-
mbedtls_exit(1);
682+
return(1);
680683

681684
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
682-
mbedtls_exit(1);
685+
return(1);
683686
TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
684687
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
685-
mbedtls_exit(1) );
688+
return(1) );
686689
mbedtls_hmac_drbg_free( &hmac_drbg );
687690

688691
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
689-
mbedtls_exit(1);
692+
return(1);
690693
mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
691694
MBEDTLS_HMAC_DRBG_PR_ON );
692695
TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
693696
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
694-
mbedtls_exit(1) );
697+
return(1) );
695698
mbedtls_hmac_drbg_free( &hmac_drbg );
696699
#endif
697700

698701
#if defined(MBEDTLS_SHA256_C)
699702
if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
700-
mbedtls_exit(1);
703+
return(1);
701704

702705
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
703-
mbedtls_exit(1);
706+
return(1);
704707
TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
705708
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
706-
mbedtls_exit(1) );
709+
return(1) );
707710
mbedtls_hmac_drbg_free( &hmac_drbg );
708711

709712
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
710-
mbedtls_exit(1);
713+
return(1);
711714
mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
712715
MBEDTLS_HMAC_DRBG_PR_ON );
713716
TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
714717
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
715-
mbedtls_exit(1) );
718+
return(1) );
716719
mbedtls_hmac_drbg_free( &hmac_drbg );
717720
#endif
718721
}
@@ -771,13 +774,13 @@ static int benchmark( int argc, char *argv[] )
771774
if( mbedtls_mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
772775
mbedtls_mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
773776
{
774-
mbedtls_exit( 1 );
777+
return( 1 );
775778
}
776779

777780
dhm.len = mbedtls_mpi_size( &dhm.P );
778781
mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
779782
if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
780-
mbedtls_exit( 1 );
783+
return( 1 );
781784

782785
mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
783786
TIME_PUBLIC( title, "handshake",
@@ -810,7 +813,7 @@ static int benchmark( int argc, char *argv[] )
810813
mbedtls_ecdsa_init( &ecdsa );
811814

812815
if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
813-
mbedtls_exit( 1 );
816+
return( 1 );
814817
ecp_clear_precomputed( &ecdsa.grp );
815818

816819
mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
@@ -832,7 +835,7 @@ static int benchmark( int argc, char *argv[] )
832835
mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
833836
tmp, &sig_len, myrand, NULL ) != 0 )
834837
{
835-
mbedtls_exit( 1 );
838+
return( 1 );
836839
}
837840
ecp_clear_precomputed( &ecdsa.grp );
838841

@@ -868,7 +871,7 @@ static int benchmark( int argc, char *argv[] )
868871
myrand, NULL ) != 0 ||
869872
mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
870873
{
871-
mbedtls_exit( 1 );
874+
return( 1 );
872875
}
873876
ecp_clear_precomputed( &ecdh.grp );
874877

@@ -890,7 +893,7 @@ static int benchmark( int argc, char *argv[] )
890893
if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
891894
mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
892895
{
893-
mbedtls_exit( 1 );
896+
return( 1 );
894897
}
895898

896899
TIME_PUBLIC( "ECDHE-Curve25519", "handshake",
@@ -916,7 +919,7 @@ static int benchmark( int argc, char *argv[] )
916919
mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ),
917920
myrand, NULL ) != 0 )
918921
{
919-
mbedtls_exit( 1 );
922+
return( 1 );
920923
}
921924
ecp_clear_precomputed( &ecdh.grp );
922925

@@ -938,7 +941,7 @@ static int benchmark( int argc, char *argv[] )
938941
myrand, NULL ) != 0 ||
939942
mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
940943
{
941-
mbedtls_exit( 1 );
944+
return( 1 );
942945
}
943946

944947
TIME_PUBLIC( "ECDH-Curve25519", "handshake",
@@ -961,8 +964,20 @@ static int benchmark( int argc, char *argv[] )
961964
}
962965

963966
int main(void) {
964-
int ret = benchmark(0, NULL);
965-
if (ret != 0) {
966-
mbedtls_printf("Benchmark failed with error %d\r\n", ret);
967+
mbedtls_platform_context platform_ctx;
968+
int exit_code = MBEDTLS_EXIT_FAILURE;
969+
970+
if((exit_code = mbedtls_platform_setup(&platform_ctx)) != 0) {
971+
printf("Platform initialization failed with error %d\r\n", exit_code);
972+
return MBEDTLS_EXIT_FAILURE;
967973
}
974+
975+
exit_code = benchmark(0, NULL, &platform_ctx);
976+
if (exit_code != 0) {
977+
mbedtls_printf("Benchmark failed with error %d\r\n", exit_code);
978+
exit_code = MBEDTLS_EXIT_FAILURE;
979+
}
980+
981+
mbedtls_platform_teardown(&platform_ctx);
982+
return exit_code;
968983
}

hashing/main.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ static const char hello_str[] = "Hello, world!";
4747
static const unsigned char *hello_buffer = (const unsigned char *) hello_str;
4848
static const size_t hello_len = strlen(hello_str);
4949

50-
static int example(void)
50+
static int example(mbedtls_platform_context* ctx)
5151
{
52+
// The call below is used to avoid the "unused parameter" warning.
53+
// The context itself can be used by cryptographic calls which require it.
54+
// Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more information.
55+
(void)ctx;
56+
5257
mbedtls_printf("\r\n\r\n");
5358

5459
/*
@@ -152,8 +157,20 @@ static int example(void)
152157
}
153158

154159
int main() {
155-
int ret = example();
156-
if (ret != 0) {
157-
mbedtls_printf("Example failed with error %d\r\n", ret);
160+
mbedtls_platform_context platform_ctx;
161+
int exit_code = MBEDTLS_EXIT_FAILURE;
162+
163+
if((exit_code = mbedtls_platform_setup(&platform_ctx)) != 0) {
164+
printf("Platform initialization failed with error %d\r\n", exit_code);
165+
return MBEDTLS_EXIT_FAILURE;
158166
}
167+
168+
exit_code = example(&platform_ctx);
169+
if (exit_code != 0) {
170+
mbedtls_printf("Example failed with error %d\r\n", exit_code);
171+
exit_code = MBEDTLS_EXIT_FAILURE;
172+
}
173+
174+
mbedtls_platform_teardown(&platform_ctx);
175+
return exit_code;
159176
}

tls-client/HelloHttpsClient.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,15 @@ const char *HelloHttpsClient::HTTP_HELLO_STR = "Hello world!";
7070
const char *HelloHttpsClient::HTTP_OK_STR = "200 OK";
7171

7272
HelloHttpsClient::HelloHttpsClient(const char *in_server_name,
73-
const uint16_t in_server_port) :
73+
const uint16_t in_server_port,
74+
mbedtls_platform_context* in_platform_ctx) :
7475
socket(),
7576
server_name(in_server_name),
76-
server_port(in_server_port)
77+
server_port(in_server_port),
78+
/* The platform context is passed just in case any crypto calls need it.
79+
* Please refer to https://github.com/ARMmbed/mbedtls/issues/1200 for more
80+
* information. */
81+
platform_ctx(in_platform_ctx)
7782
{
7883
mbedtls_entropy_init(&entropy);
7984
mbedtls_ctr_drbg_init(&ctr_drbg);

tls-client/HelloHttpsClient.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "TCPSocket.h"
2626

2727
#include "mbedtls/config.h"
28+
#include "mbedtls/platform.h"
2829
#include "mbedtls/ssl.h"
2930
#include "mbedtls/entropy.h"
3031
#include "mbedtls/ctr_drbg.h"
@@ -60,7 +61,8 @@ class HelloHttpsClient
6061
* The server port
6162
*/
6263
HelloHttpsClient(const char *in_server_name,
63-
const uint16_t in_server_port);
64+
const uint16_t in_server_port,
65+
mbedtls_platform_context* in_platform_ctx);
6466

6567
/**
6668
* Free any allocated resources
@@ -223,6 +225,8 @@ class HelloHttpsClient
223225
* The TLS configuration in use
224226
*/
225227
mbedtls_ssl_config ssl_conf;
228+
229+
mbedtls_platform_context* platform_ctx;
226230
};
227231

228232
#endif /* _HELLOHTTPSCLIENT_H_ */

tls-client/main.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,19 @@ const int SERVER_PORT = 443;
4949
*/
5050
int main()
5151
{
52+
mbedtls_platform_context platform_ctx;
53+
int exit_code = MBEDTLS_EXIT_FAILURE;
54+
55+
if((exit_code = mbedtls_platform_setup(&platform_ctx)) != 0) {
56+
printf("Platform initialization failed with error %d\r\n", exit_code);
57+
return MBEDTLS_EXIT_FAILURE;
58+
}
5259
/*
5360
* The default 9600 bps is too slow to print full TLS debug info and could
5461
* cause the other party to time out.
5562
*/
5663

5764
HelloHttpsClient *client;
58-
int exit_code = MBEDTLS_EXIT_FAILURE;
5965

6066
mbedtls_printf("Starting mbed-os-example-tls/tls-client\n");
6167

@@ -67,10 +73,12 @@ int main()
6773
#endif /* MBEDTLS_MAJOR_VERSION */
6874

6975
/* Allocate a HTTPS client */
70-
client = new (std::nothrow) HelloHttpsClient(SERVER_NAME, SERVER_PORT);
76+
client = new (std::nothrow) HelloHttpsClient(SERVER_NAME, SERVER_PORT,
77+
&platform_ctx);
7178
if (client == NULL) {
7279
mbedtls_printf("Failed to allocate HelloHttpsClient object\n"
7380
"\nFAIL\n");
81+
mbedtls_platform_teardown(&platform_ctx);
7482
return exit_code;
7583
}
7684

@@ -84,5 +92,6 @@ int main()
8492

8593
delete client;
8694

95+
mbedtls_platform_teardown(&platform_ctx);
8796
return exit_code;
8897
}

0 commit comments

Comments
 (0)