57
57
#include MBEDTLS_CONFIG_FILE
58
58
#endif
59
59
60
- #if defined(MBEDTLS_PLATFORM_C )
61
- #include "mbedtls/platform.h"
62
- #else
63
60
#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
71
61
#include <stdio.h>
72
62
#include <string.h>
73
63
79
69
!defined(MBEDTLS_PSA_CRYPTO_C ) || !defined(MBEDTLS_FS_IO )
80
70
int main ( void )
81
71
{
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" );
85
75
return ( 0 );
86
76
}
87
77
#else
@@ -112,10 +102,10 @@ int main( void )
112
102
status = ( expr ); \
113
103
if( status != PSA_SUCCESS ) \
114
104
{ \
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 ); \
119
109
goto exit; \
120
110
} \
121
111
} \
@@ -254,8 +244,8 @@ static psa_status_t import_key_from_file( psa_key_usage_t usage,
254
244
key_file ) ) != 0 );
255
245
if ( fread ( & extra_byte , 1 , 1 , key_file ) != 0 )
256
246
{
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 ) );
259
249
status = DEMO_ERROR ;
260
250
goto exit ;
261
251
}
@@ -395,7 +385,7 @@ static psa_status_t wrap_data( const char *input_file_name,
395
385
#if LONG_MAX > SIZE_MAX
396
386
if ( input_position > SIZE_MAX )
397
387
{
398
- mbedtls_printf ( "Input file too large.\n" );
388
+ printf ( "Input file too large.\n" );
399
389
status = DEMO_ERROR ;
400
390
goto exit ;
401
391
}
@@ -405,14 +395,14 @@ static psa_status_t wrap_data( const char *input_file_name,
405
395
/* Check for integer overflow. */
406
396
if ( buffer_size < input_size )
407
397
{
408
- mbedtls_printf ( "Input file too large.\n" );
398
+ printf ( "Input file too large.\n" );
409
399
status = DEMO_ERROR ;
410
400
goto exit ;
411
401
}
412
402
413
403
/* Load the data to wrap. */
414
404
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 );
416
406
SYS_CHECK ( fread ( buffer , 1 , input_size , input_file ) == input_size );
417
407
SYS_CHECK ( fclose ( input_file ) == 0 );
418
408
input_file = NULL ;
@@ -447,7 +437,7 @@ static psa_status_t wrap_data( const char *input_file_name,
447
437
fclose ( output_file );
448
438
if ( buffer != NULL )
449
439
mbedtls_platform_zeroize ( buffer , buffer_size );
450
- mbedtls_free ( buffer );
440
+ free ( buffer );
451
441
return ( status );
452
442
}
453
443
@@ -471,13 +461,13 @@ static psa_status_t unwrap_data( const char *input_file_name,
471
461
if ( memcmp ( & header .magic , WRAPPED_DATA_MAGIC ,
472
462
WRAPPED_DATA_MAGIC_LENGTH ) != 0 )
473
463
{
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" );
475
465
status = DEMO_ERROR ;
476
466
goto exit ;
477
467
}
478
468
if ( header .ad_size != sizeof ( header ) )
479
469
{
480
- mbedtls_printf ( "The header size is not correct.\n" );
470
+ printf ( "The header size is not correct.\n" );
481
471
status = DEMO_ERROR ;
482
472
goto exit ;
483
473
}
@@ -486,18 +476,18 @@ static psa_status_t unwrap_data( const char *input_file_name,
486
476
/* Check for integer overflow. */
487
477
if ( ciphertext_size < header .payload_size )
488
478
{
489
- mbedtls_printf ( "Input file too large.\n" );
479
+ printf ( "Input file too large.\n" );
490
480
status = DEMO_ERROR ;
491
481
goto exit ;
492
482
}
493
483
494
484
/* Load the payload data. */
495
- SYS_CHECK ( ( buffer = mbedtls_calloc ( 1 , ciphertext_size ) ) != NULL );
485
+ SYS_CHECK ( ( buffer = calloc ( 1 , ciphertext_size ) ) != NULL );
496
486
SYS_CHECK ( fread ( buffer , 1 , ciphertext_size ,
497
487
input_file ) == ciphertext_size );
498
488
if ( fread ( & extra_byte , 1 , 1 , input_file ) != 0 )
499
489
{
500
- mbedtls_printf ( "Extra garbage after ciphertext\n" );
490
+ printf ( "Extra garbage after ciphertext\n" );
501
491
status = DEMO_ERROR ;
502
492
goto exit ;
503
493
}
@@ -513,7 +503,7 @@ static psa_status_t unwrap_data( const char *input_file_name,
513
503
& plaintext_size ) );
514
504
if ( plaintext_size != header .payload_size )
515
505
{
516
- mbedtls_printf ( "Incorrect payload size in the header.\n" );
506
+ printf ( "Incorrect payload size in the header.\n" );
517
507
status = DEMO_ERROR ;
518
508
goto exit ;
519
509
}
@@ -532,7 +522,7 @@ static psa_status_t unwrap_data( const char *input_file_name,
532
522
fclose ( output_file );
533
523
if ( buffer != NULL )
534
524
mbedtls_platform_zeroize ( buffer , ciphertext_size );
535
- mbedtls_free ( buffer );
525
+ free ( buffer );
536
526
return ( status );
537
527
}
538
528
@@ -600,23 +590,23 @@ static psa_status_t run( enum program_mode mode,
600
590
601
591
static void usage ( void )
602
592
{
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" );
620
610
}
621
611
622
612
#if defined(MBEDTLS_CHECK_PARAMS )
@@ -625,9 +615,9 @@ void mbedtls_param_failed( const char *failure_condition,
625
615
const char * file ,
626
616
int line )
627
617
{
628
- mbedtls_printf ( "%s:%i: Input param failed - %s\n" ,
618
+ printf ( "%s:%i: Input param failed - %s\n" ,
629
619
file , line , failure_condition );
630
- mbedtls_exit ( MBEDTLS_EXIT_FAILURE );
620
+ exit ( EXIT_FAILURE );
631
621
}
632
622
#endif
633
623
@@ -648,15 +638,15 @@ int main( int argc, char *argv[] )
648
638
strcmp ( argv [1 ], "--help" ) == 0 )
649
639
{
650
640
usage ( );
651
- return ( MBEDTLS_EXIT_SUCCESS );
641
+ return ( EXIT_SUCCESS );
652
642
}
653
643
654
644
for ( i = 2 ; i < argc ; i ++ )
655
645
{
656
646
char * q = strchr ( argv [i ], '=' );
657
647
if ( q == NULL )
658
648
{
659
- mbedtls_printf ( "Missing argument to option %s\n" , argv [i ] );
649
+ printf ( "Missing argument to option %s\n" , argv [i ] );
660
650
goto usage_failure ;
661
651
}
662
652
* q = 0 ;
@@ -667,9 +657,9 @@ int main( int argc, char *argv[] )
667
657
{
668
658
if ( ladder_depth == MAX_LADDER_DEPTH )
669
659
{
670
- mbedtls_printf ( "Maximum ladder depth %u exceeded.\n" ,
660
+ printf ( "Maximum ladder depth %u exceeded.\n" ,
671
661
(unsigned ) MAX_LADDER_DEPTH );
672
- return ( MBEDTLS_EXIT_FAILURE );
662
+ return ( EXIT_FAILURE );
673
663
}
674
664
ladder [ladder_depth ] = q ;
675
665
++ ladder_depth ;
@@ -680,7 +670,7 @@ int main( int argc, char *argv[] )
680
670
output_file_name = q ;
681
671
else
682
672
{
683
- mbedtls_printf ( "Unknown option: %s\n" , argv [i ] );
673
+ printf ( "Unknown option: %s\n" , argv [i ] );
684
674
goto usage_failure ;
685
675
}
686
676
}
@@ -695,32 +685,32 @@ int main( int argc, char *argv[] )
695
685
mode = MODE_WRAP ;
696
686
else
697
687
{
698
- mbedtls_printf ( "Unknown action: %s\n" , argv [1 ] );
688
+ printf ( "Unknown action: %s\n" , argv [1 ] );
699
689
goto usage_failure ;
700
690
}
701
691
702
692
if ( input_file_name == NULL &&
703
693
( mode == MODE_WRAP || mode == MODE_UNWRAP ) )
704
694
{
705
- mbedtls_printf ( "Required argument missing: input\n" );
695
+ printf ( "Required argument missing: input\n" );
706
696
return ( DEMO_ERROR );
707
697
}
708
698
if ( output_file_name == NULL &&
709
699
( mode == MODE_SAVE || mode == MODE_WRAP || mode == MODE_UNWRAP ) )
710
700
{
711
- mbedtls_printf ( "Required argument missing: output\n" );
701
+ printf ( "Required argument missing: output\n" );
712
702
return ( DEMO_ERROR );
713
703
}
714
704
715
705
status = run ( mode , key_file_name ,
716
706
ladder , ladder_depth ,
717
707
input_file_name , output_file_name );
718
708
return ( status == PSA_SUCCESS ?
719
- MBEDTLS_EXIT_SUCCESS :
720
- MBEDTLS_EXIT_FAILURE );
709
+ EXIT_SUCCESS :
710
+ EXIT_FAILURE );
721
711
722
712
usage_failure :
723
713
usage ( );
724
- return ( MBEDTLS_EXIT_FAILURE );
714
+ return ( EXIT_FAILURE );
725
715
}
726
716
#endif /* MBEDTLS_SHA256_C && MBEDTLS_MD_C && MBEDTLS_AES_C && MBEDTLS_CCM_C && MBEDTLS_PSA_CRYPTO_C && MBEDTLS_FS_IO */
0 commit comments