Skip to content

Commit fa30c33

Browse files
committed
programs: psa: Remove dependency on platform.h
platform.h should only be used internally by the library implementation itself, not the examples. Remove the dependency on platform.h from all PSA programs.
1 parent e23737c commit fa30c33

File tree

2 files changed

+66
-84
lines changed

2 files changed

+66
-84
lines changed

programs/psa/crypto_examples.c

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
#include "psa/crypto.h"
22
#include <string.h>
3-
4-
#if defined(MBEDTLS_PLATFORM_C)
5-
#include "mbedtls/platform.h"
6-
#else
73
#include <stdio.h>
84
#include <stdlib.h>
9-
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
10-
#define mbedtls_printf printf
11-
#define mbedtls_exit exit
12-
#endif
135

146
#define ASSERT( predicate ) \
157
do \
168
{ \
179
if( ! ( predicate ) ) \
1810
{ \
19-
mbedtls_printf( "\tassertion failed at %s:%d - '%s'\r\n", \
20-
__FILE__, __LINE__, #predicate); \
11+
printf( "\tassertion failed at %s:%d - '%s'\r\n", \
12+
__FILE__, __LINE__, #predicate); \
2113
goto exit; \
2214
} \
2315
} while ( 0 )
@@ -27,8 +19,8 @@
2719
{ \
2820
if( ( actual ) != ( expected ) ) \
2921
{ \
30-
mbedtls_printf( "\tassertion failed at %s:%d - " \
31-
"actual:%d expected:%d\r\n", __FILE__, __LINE__, \
22+
printf( "\tassertion failed at %s:%d - " \
23+
"actual:%d expected:%d\r\n", __FILE__, __LINE__, \
3224
(psa_status_t) actual, (psa_status_t) expected ); \
3325
goto exit; \
3426
} \
@@ -39,10 +31,10 @@
3931
!defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
4032
int main( void )
4133
{
42-
mbedtls_printf( "MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_AES_C and/or "
43-
"MBEDTLS_CIPHER_MODE_CBC and/or MBEDTLS_CIPHER_MODE_CTR "
44-
"and/or MBEDTLS_CIPHER_MODE_WITH_PADDING "
45-
"not defined.\r\n" );
34+
printf( "MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_AES_C and/or "
35+
"MBEDTLS_CIPHER_MODE_CBC and/or MBEDTLS_CIPHER_MODE_CTR "
36+
"and/or MBEDTLS_CIPHER_MODE_WITH_PADDING "
37+
"not defined.\r\n" );
4638
return( 0 );
4739
}
4840
#else
@@ -311,20 +303,20 @@ static void cipher_examples( void )
311303
{
312304
psa_status_t status;
313305

314-
mbedtls_printf( "cipher encrypt/decrypt AES CBC no padding:\r\n" );
306+
printf( "cipher encrypt/decrypt AES CBC no padding:\r\n" );
315307
status = cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block( );
316308
if( status == PSA_SUCCESS )
317-
mbedtls_printf( "\tsuccess!\r\n" );
309+
printf( "\tsuccess!\r\n" );
318310

319-
mbedtls_printf( "cipher encrypt/decrypt AES CBC PKCS7 multipart:\r\n" );
311+
printf( "cipher encrypt/decrypt AES CBC PKCS7 multipart:\r\n" );
320312
status = cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi( );
321313
if( status == PSA_SUCCESS )
322-
mbedtls_printf( "\tsuccess!\r\n" );
314+
printf( "\tsuccess!\r\n" );
323315

324-
mbedtls_printf( "cipher encrypt/decrypt AES CTR multipart:\r\n" );
316+
printf( "cipher encrypt/decrypt AES CTR multipart:\r\n" );
325317
status = cipher_example_encrypt_decrypt_aes_ctr_multi( );
326318
if( status == PSA_SUCCESS )
327-
mbedtls_printf( "\tsuccess!\r\n" );
319+
printf( "\tsuccess!\r\n" );
328320
}
329321

330322
#if defined(MBEDTLS_CHECK_PARAMS)
@@ -333,9 +325,9 @@ void mbedtls_param_failed( const char *failure_condition,
333325
const char *file,
334326
int line )
335327
{
336-
mbedtls_printf( "%s:%i: Input param failed - %s\n",
328+
printf( "%s:%i: Input param failed - %s\n",
337329
file, line, failure_condition );
338-
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
330+
exit( EXIT_FAILURE );
339331
}
340332
#endif
341333

programs/psa/key_ladder_demo.c

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,7 @@
5757
#include MBEDTLS_CONFIG_FILE
5858
#endif
5959

60-
#if defined(MBEDTLS_PLATFORM_C)
61-
#include "mbedtls/platform.h"
62-
#else
6360
#include <stdlib.h>
64-
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
65-
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
66-
#define mbedtls_calloc calloc
67-
#define mbedtls_free free
68-
#define mbedtls_printf printf
69-
#define mbedtls_exit exit
70-
#endif
7161
#include <stdio.h>
7262
#include <string.h>
7363

@@ -79,9 +69,9 @@
7969
!defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_FS_IO)
8070
int main( void )
8171
{
82-
mbedtls_printf("MBEDTLS_SHA256_C and/or MBEDTLS_MD_C and/or "
83-
"MBEDTLS_AES_C and/or MBEDTLS_CCM_C and/or "
84-
"MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_FS_IO not defined.\n");
72+
printf("MBEDTLS_SHA256_C and/or MBEDTLS_MD_C and/or "
73+
"MBEDTLS_AES_C and/or MBEDTLS_CCM_C and/or "
74+
"MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_FS_IO not defined.\n");
8575
return( 0 );
8676
}
8777
#else
@@ -112,10 +102,10 @@ int main( void )
112102
status = ( expr ); \
113103
if( status != PSA_SUCCESS ) \
114104
{ \
115-
mbedtls_printf( "Error %d at line %u: %s\n", \
116-
(int) status, \
117-
__LINE__, \
118-
#expr ); \
105+
printf( "Error %d at line %u: %s\n", \
106+
(int) status, \
107+
__LINE__, \
108+
#expr ); \
119109
goto exit; \
120110
} \
121111
} \
@@ -254,8 +244,8 @@ static psa_status_t import_key_from_file( psa_key_usage_t usage,
254244
key_file ) ) != 0 );
255245
if( fread( &extra_byte, 1, 1, key_file ) != 0 )
256246
{
257-
mbedtls_printf( "Key file too large (max: %u).\n",
258-
(unsigned) sizeof( key_data ) );
247+
printf( "Key file too large (max: %u).\n",
248+
(unsigned) sizeof( key_data ) );
259249
status = DEMO_ERROR;
260250
goto exit;
261251
}
@@ -395,7 +385,7 @@ static psa_status_t wrap_data( const char *input_file_name,
395385
#if LONG_MAX > SIZE_MAX
396386
if( input_position > SIZE_MAX )
397387
{
398-
mbedtls_printf( "Input file too large.\n" );
388+
printf( "Input file too large.\n" );
399389
status = DEMO_ERROR;
400390
goto exit;
401391
}
@@ -405,14 +395,14 @@ static psa_status_t wrap_data( const char *input_file_name,
405395
/* Check for integer overflow. */
406396
if( buffer_size < input_size )
407397
{
408-
mbedtls_printf( "Input file too large.\n" );
398+
printf( "Input file too large.\n" );
409399
status = DEMO_ERROR;
410400
goto exit;
411401
}
412402

413403
/* Load the data to wrap. */
414404
SYS_CHECK( fseek( input_file, 0, SEEK_SET ) == 0 );
415-
SYS_CHECK( ( buffer = mbedtls_calloc( 1, buffer_size ) ) != NULL );
405+
SYS_CHECK( ( buffer = calloc( 1, buffer_size ) ) != NULL );
416406
SYS_CHECK( fread( buffer, 1, input_size, input_file ) == input_size );
417407
SYS_CHECK( fclose( input_file ) == 0 );
418408
input_file = NULL;
@@ -447,7 +437,7 @@ static psa_status_t wrap_data( const char *input_file_name,
447437
fclose( output_file );
448438
if( buffer != NULL )
449439
mbedtls_platform_zeroize( buffer, buffer_size );
450-
mbedtls_free( buffer );
440+
free( buffer );
451441
return( status );
452442
}
453443

@@ -471,13 +461,13 @@ static psa_status_t unwrap_data( const char *input_file_name,
471461
if( memcmp( &header.magic, WRAPPED_DATA_MAGIC,
472462
WRAPPED_DATA_MAGIC_LENGTH ) != 0 )
473463
{
474-
mbedtls_printf( "The input does not start with a valid magic header.\n" );
464+
printf( "The input does not start with a valid magic header.\n" );
475465
status = DEMO_ERROR;
476466
goto exit;
477467
}
478468
if( header.ad_size != sizeof( header ) )
479469
{
480-
mbedtls_printf( "The header size is not correct.\n" );
470+
printf( "The header size is not correct.\n" );
481471
status = DEMO_ERROR;
482472
goto exit;
483473
}
@@ -486,18 +476,18 @@ static psa_status_t unwrap_data( const char *input_file_name,
486476
/* Check for integer overflow. */
487477
if( ciphertext_size < header.payload_size )
488478
{
489-
mbedtls_printf( "Input file too large.\n" );
479+
printf( "Input file too large.\n" );
490480
status = DEMO_ERROR;
491481
goto exit;
492482
}
493483

494484
/* Load the payload data. */
495-
SYS_CHECK( ( buffer = mbedtls_calloc( 1, ciphertext_size ) ) != NULL );
485+
SYS_CHECK( ( buffer = calloc( 1, ciphertext_size ) ) != NULL );
496486
SYS_CHECK( fread( buffer, 1, ciphertext_size,
497487
input_file ) == ciphertext_size );
498488
if( fread( &extra_byte, 1, 1, input_file ) != 0 )
499489
{
500-
mbedtls_printf( "Extra garbage after ciphertext\n" );
490+
printf( "Extra garbage after ciphertext\n" );
501491
status = DEMO_ERROR;
502492
goto exit;
503493
}
@@ -513,7 +503,7 @@ static psa_status_t unwrap_data( const char *input_file_name,
513503
&plaintext_size ) );
514504
if( plaintext_size != header.payload_size )
515505
{
516-
mbedtls_printf( "Incorrect payload size in the header.\n" );
506+
printf( "Incorrect payload size in the header.\n" );
517507
status = DEMO_ERROR;
518508
goto exit;
519509
}
@@ -532,7 +522,7 @@ static psa_status_t unwrap_data( const char *input_file_name,
532522
fclose( output_file );
533523
if( buffer != NULL )
534524
mbedtls_platform_zeroize( buffer, ciphertext_size );
535-
mbedtls_free( buffer );
525+
free( buffer );
536526
return( status );
537527
}
538528

@@ -600,23 +590,23 @@ static psa_status_t run( enum program_mode mode,
600590

601591
static void usage( void )
602592
{
603-
mbedtls_printf( "Usage: key_ladder_demo MODE [OPTION=VALUE]...\n" );
604-
mbedtls_printf( "Demonstrate the usage of a key derivation ladder.\n" );
605-
mbedtls_printf( "\n" );
606-
mbedtls_printf( "Modes:\n" );
607-
mbedtls_printf( " generate Generate the master key\n" );
608-
mbedtls_printf( " save Save the derived key\n" );
609-
mbedtls_printf( " unwrap Unwrap (decrypt) input with the derived key\n" );
610-
mbedtls_printf( " wrap Wrap (encrypt) input with the derived key\n" );
611-
mbedtls_printf( "\n" );
612-
mbedtls_printf( "Options:\n" );
613-
mbedtls_printf( " input=FILENAME Input file (required for wrap/unwrap)\n" );
614-
mbedtls_printf( " master=FILENAME File containing the master key (default: master.key)\n" );
615-
mbedtls_printf( " output=FILENAME Output file (required for save/wrap/unwrap)\n" );
616-
mbedtls_printf( " label=TEXT Label for the key derivation.\n" );
617-
mbedtls_printf( " This may be repeated multiple times.\n" );
618-
mbedtls_printf( " To get the same key, you must use the same master key\n" );
619-
mbedtls_printf( " and the same sequence of labels.\n" );
593+
printf( "Usage: key_ladder_demo MODE [OPTION=VALUE]...\n" );
594+
printf( "Demonstrate the usage of a key derivation ladder.\n" );
595+
printf( "\n" );
596+
printf( "Modes:\n" );
597+
printf( " generate Generate the master key\n" );
598+
printf( " save Save the derived key\n" );
599+
printf( " unwrap Unwrap (decrypt) input with the derived key\n" );
600+
printf( " wrap Wrap (encrypt) input with the derived key\n" );
601+
printf( "\n" );
602+
printf( "Options:\n" );
603+
printf( " input=FILENAME Input file (required for wrap/unwrap)\n" );
604+
printf( " master=FILENAME File containing the master key (default: master.key)\n" );
605+
printf( " output=FILENAME Output file (required for save/wrap/unwrap)\n" );
606+
printf( " label=TEXT Label for the key derivation.\n" );
607+
printf( " This may be repeated multiple times.\n" );
608+
printf( " To get the same key, you must use the same master key\n" );
609+
printf( " and the same sequence of labels.\n" );
620610
}
621611

622612
#if defined(MBEDTLS_CHECK_PARAMS)
@@ -625,9 +615,9 @@ void mbedtls_param_failed( const char *failure_condition,
625615
const char *file,
626616
int line )
627617
{
628-
mbedtls_printf( "%s:%i: Input param failed - %s\n",
618+
printf( "%s:%i: Input param failed - %s\n",
629619
file, line, failure_condition );
630-
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
620+
exit( EXIT_FAILURE );
631621
}
632622
#endif
633623

@@ -648,15 +638,15 @@ int main( int argc, char *argv[] )
648638
strcmp( argv[1], "--help" ) == 0 )
649639
{
650640
usage( );
651-
return( MBEDTLS_EXIT_SUCCESS );
641+
return( EXIT_SUCCESS );
652642
}
653643

654644
for( i = 2; i < argc; i++ )
655645
{
656646
char *q = strchr( argv[i], '=' );
657647
if( q == NULL )
658648
{
659-
mbedtls_printf( "Missing argument to option %s\n", argv[i] );
649+
printf( "Missing argument to option %s\n", argv[i] );
660650
goto usage_failure;
661651
}
662652
*q = 0;
@@ -667,9 +657,9 @@ int main( int argc, char *argv[] )
667657
{
668658
if( ladder_depth == MAX_LADDER_DEPTH )
669659
{
670-
mbedtls_printf( "Maximum ladder depth %u exceeded.\n",
660+
printf( "Maximum ladder depth %u exceeded.\n",
671661
(unsigned) MAX_LADDER_DEPTH );
672-
return( MBEDTLS_EXIT_FAILURE );
662+
return( EXIT_FAILURE );
673663
}
674664
ladder[ladder_depth] = q;
675665
++ladder_depth;
@@ -680,7 +670,7 @@ int main( int argc, char *argv[] )
680670
output_file_name = q;
681671
else
682672
{
683-
mbedtls_printf( "Unknown option: %s\n", argv[i] );
673+
printf( "Unknown option: %s\n", argv[i] );
684674
goto usage_failure;
685675
}
686676
}
@@ -695,32 +685,32 @@ int main( int argc, char *argv[] )
695685
mode = MODE_WRAP;
696686
else
697687
{
698-
mbedtls_printf( "Unknown action: %s\n", argv[1] );
688+
printf( "Unknown action: %s\n", argv[1] );
699689
goto usage_failure;
700690
}
701691

702692
if( input_file_name == NULL &&
703693
( mode == MODE_WRAP || mode == MODE_UNWRAP ) )
704694
{
705-
mbedtls_printf( "Required argument missing: input\n" );
695+
printf( "Required argument missing: input\n" );
706696
return( DEMO_ERROR );
707697
}
708698
if( output_file_name == NULL &&
709699
( mode == MODE_SAVE || mode == MODE_WRAP || mode == MODE_UNWRAP ) )
710700
{
711-
mbedtls_printf( "Required argument missing: output\n" );
701+
printf( "Required argument missing: output\n" );
712702
return( DEMO_ERROR );
713703
}
714704

715705
status = run( mode, key_file_name,
716706
ladder, ladder_depth,
717707
input_file_name, output_file_name );
718708
return( status == PSA_SUCCESS ?
719-
MBEDTLS_EXIT_SUCCESS :
720-
MBEDTLS_EXIT_FAILURE );
709+
EXIT_SUCCESS :
710+
EXIT_FAILURE );
721711

722712
usage_failure:
723713
usage( );
724-
return( MBEDTLS_EXIT_FAILURE );
714+
return( EXIT_FAILURE );
725715
}
726716
#endif /* MBEDTLS_SHA256_C && MBEDTLS_MD_C && MBEDTLS_AES_C && MBEDTLS_CCM_C && MBEDTLS_PSA_CRYPTO_C && MBEDTLS_FS_IO */

0 commit comments

Comments
 (0)