From b0fc484188d1493f308677868cfae72e0b290b42 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Tue, 12 Mar 2019 10:48:18 -0400 Subject: [PATCH 01/10] Add crypto includes when generating errors in generate_errors.pl Adjusted generate_errors to have a configuration option of including crypto files. Turned on by default --- scripts/generate_errors.pl | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/scripts/generate_errors.pl b/scripts/generate_errors.pl index 0c1f7e16ec..037f214954 100755 --- a/scripts/generate_errors.pl +++ b/scripts/generate_errors.pl @@ -3,23 +3,28 @@ # Generate error.c # # Usage: ./generate_errors.pl or scripts/generate_errors.pl without arguments, -# or generate_errors.pl include_dir data_dir error_file +# or generate_errors.pl include_dir data_dir error_file include_crypto use strict; -my ($include_dir, $data_dir, $error_file); +my ($include_dir, $data_dir, $error_file, $include_crypto); +my $crypto_dir = "crypto"; if( @ARGV ) { - die "Invalid number of arguments" if scalar @ARGV != 3; - ($include_dir, $data_dir, $error_file) = @ARGV; + die "Invalid number of arguments" if scalar @ARGV != 4; + ($include_dir, $data_dir, $error_file, $include_crypto) = @ARGV; -d $include_dir or die "No such directory: $include_dir\n"; -d $data_dir or die "No such directory: $data_dir\n"; + if( $include_crypto ) { + -d $crypto_dir or die "Crypto submodule not present\n"; + } } else { $include_dir = 'include/mbedtls'; $data_dir = 'scripts/data_files'; $error_file = 'library/error.c'; - + $include_crypto = 1; + -d $crypto_dir or die "Crypto submodule not present\n"; unless( -d $include_dir && -d $data_dir ) { chdir '..' or die; -d $include_dir && -d $data_dir @@ -48,6 +53,11 @@ close(FORMAT_FILE); $/ = $line_separator; my @files = <$include_dir/*.h>; + +if( $include_crypto ) { + @files = (<$include_dir/*.h>,<$crypto_dir/$include_dir/*.h>); +} + my @matches; foreach my $file (@files) { open(FILE, "$file"); @@ -73,8 +83,15 @@ foreach my $line (@matches) my ($error_name, $error_code) = $line =~ /(MBEDTLS_ERR_\w+)\s+\-(0x\w+)/; my ($description) = $line =~ /\/\*\*< (.*?)\.? \*\//; - die "Duplicated error code: $error_code ($error_name)\n" - if( $error_codes_seen{$error_code}++ ); + if( $error_codes_seen{$error_code}++ ) { + if( $include_crypto ) { + print "Duplicated error code: $error_code ($error_name)\n"; + next; + } + else { + die "Duplicated error code: $error_code ($error_name)\n" ; + } + } $description =~ s/\\/\\\\/g; if ($description eq "") { From ef907604f846958c7e9d8e4527e91988e1eb8b2c Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Fri, 5 Apr 2019 03:33:18 -0400 Subject: [PATCH 02/10] Include crypto config when generating query config Adjusted generate_query_config.pl to have a configuration option of including the crypto config. Turned on by default. --- programs/ssl/query_config.c | 8 +++++ scripts/generate_query_config.pl | 55 ++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/programs/ssl/query_config.c b/programs/ssl/query_config.c index f2f7b46d6d..41535ef4dc 100644 --- a/programs/ssl/query_config.c +++ b/programs/ssl/query_config.c @@ -2586,6 +2586,14 @@ int query_config( const char *config ) } #endif /* MBEDTLS_PLATFORM_GMTIME_R_ALT */ +#if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER) + if( strcmp( "MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER", config ) == 0 ) + { + MACRO_EXPANSION_TO_STR( MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER ); + return( 0 ); + } +#endif /* MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER */ + /* If the symbol is not found, return an error */ return( 1 ); } diff --git a/scripts/generate_query_config.pl b/scripts/generate_query_config.pl index f15e03a358..cc49df4eb9 100755 --- a/scripts/generate_query_config.pl +++ b/scripts/generate_query_config.pl @@ -18,7 +18,9 @@ use strict; +my $include_crypto = 1; my $config_file = "./include/mbedtls/config.h"; +my $crypto_config_file = "./crypto/include/mbedtls/config.h"; my $query_config_format_file = "./scripts/data_files/query_config.fmt"; my $query_config_file = "./programs/ssl/query_config.c"; @@ -33,31 +35,52 @@ MBEDTLS_PARAM_FAILED ); my $excluded_re = join '|', @excluded; -open(CONFIG_FILE, "$config_file") or die "Opening config file '$config_file': $!"; # This variable will contain the string to replace in the CHECK_CONFIG of the # format file my $config_check = ""; +my %defines_seen; +my @files = ($config_file); -while (my $line = ) { - if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) { - my $name = $2; - # Skip over the macro that prevents multiple inclusion - next if "MBEDTLS_CONFIG_H" eq $name; +if( @ARGV ) { + die "Invalid number of arguments" if scalar @ARGV != 1; + ($include_crypto) = @ARGV; +} - # Skip over the macro if it is in the ecluded list - next if $name =~ /$excluded_re/; +if( $include_crypto ) { + push(@files, $crypto_config_file); +} - $config_check .= "#if defined($name)\n"; - $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n"; - $config_check .= " {\n"; - $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n"; - $config_check .= " return( 0 );\n"; - $config_check .= " }\n"; - $config_check .= "#endif /* $name */\n"; - $config_check .= "\n"; +foreach my $file (@files) { + open(FILE, "$file") or die "Opening config file failed: '$file': $!"; + while (my $line = ) { + if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) { + my $name = $2; + + # Skip over the macro that prevents multiple inclusion + next if "MBEDTLS_CONFIG_H" eq $name; + + # Skip over the macro if it is in the excluded list + next if $name =~ /$excluded_re/; + + # Skip if this define is already added + if( $defines_seen{$name}++ ) { + print "Skipping $name, already added. \n"; + next; + } + + $config_check .= "#if defined($name)\n"; + $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n"; + $config_check .= " {\n"; + $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n"; + $config_check .= " return( 0 );\n"; + $config_check .= " }\n"; + $config_check .= "#endif /* $name */\n"; + $config_check .= "\n"; + } } + close(FILE); } # Read the full format file into a string From 79369cd8d91e6a8f3b4b0786743b96ae8b4f7c9b Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Fri, 5 Apr 2019 04:07:40 -0400 Subject: [PATCH 03/10] Add crypto includes when generating features in generate_features.pl Adjusted generate_features to have a configuration option of including crypto config. Turned on by default. --- library/version_features.c | 3 ++ scripts/generate_features.pl | 74 +++++++++++++++++++++++------------- 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/library/version_features.c b/library/version_features.c index 161788ca76..23b2a5a972 100644 --- a/library/version_features.c +++ b/library/version_features.c @@ -780,6 +780,9 @@ static const char *features[] = { #if defined(MBEDTLS_XTEA_C) "MBEDTLS_XTEA_C", #endif /* MBEDTLS_XTEA_C */ +#if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER) + "MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER", +#endif /* MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER */ #endif /* MBEDTLS_VERSION_FEATURES */ NULL }; diff --git a/scripts/generate_features.pl b/scripts/generate_features.pl index 1bd82ca2a7..10aadb63e3 100755 --- a/scripts/generate_features.pl +++ b/scripts/generate_features.pl @@ -3,18 +3,24 @@ use strict; -my ($include_dir, $data_dir, $feature_file); +my ($include_dir, $data_dir, $feature_file, $include_crypto); +my $crypto_include_dir = "crypto/include/mbedtls"; if( @ARGV ) { - die "Invalid number of arguments" if scalar @ARGV != 3; - ($include_dir, $data_dir, $feature_file) = @ARGV; + die "Invalid number of arguments" if scalar @ARGV != 4; + ($include_dir, $data_dir, $feature_file, $include_crypto) = @ARGV; -d $include_dir or die "No such directory: $include_dir\n"; -d $data_dir or die "No such directory: $data_dir\n"; + if( $include_crypto ) { + -d $crypto_include_dir or die "Crypto submodule not present\n"; + } } else { $include_dir = 'include/mbedtls'; $data_dir = 'scripts/data_files'; $feature_file = 'library/version_features.c'; + $include_crypto = 1; + -d $crypto_include_dir or die "Crypto submodule not present\n"; unless( -d $include_dir && -d $data_dir ) { chdir '..' or die; @@ -36,37 +42,53 @@ my $feature_format = ; close(FORMAT_FILE); $/ = $line_separator; +my %defines_seen; +my @files = ("$include_dir/config.h"); -open(CONFIG_H, "$include_dir/config.h") || die("Failure when opening config.h: $!"); +if( $include_crypto ) { + push(@files, "$crypto_include_dir/config.h"); +} my $feature_defines = ""; -my $in_section = 0; -while (my $line = ) -{ - next if ($in_section && $line !~ /#define/ && $line !~ /SECTION/); - next if (!$in_section && $line !~ /SECTION/); +foreach my $file (@files) { + open(FILE, "$file") or die "Opening config file failed: '$file': $!"; - if ($in_section) { - if ($line =~ /SECTION/) { - $in_section = 0; - next; + my $in_section = 0; + + while (my $line = ) + { + next if ($in_section && $line !~ /#define/ && $line !~ /SECTION/); + next if (!$in_section && $line !~ /SECTION/); + + if ($in_section) { + if ($line =~ /SECTION/) { + $in_section = 0; + next; + } + + my ($define) = $line =~ /#define (\w+)/; + + # Skip if this define is already added + if( $defines_seen{$define}++ ) { + print "Skipping $define, already added. \n"; + next; + } + + $feature_defines .= "#if defined(${define})\n"; + $feature_defines .= " \"${define}\",\n"; + $feature_defines .= "#endif /* ${define} */\n"; } - my ($define) = $line =~ /#define (\w+)/; - $feature_defines .= "#if defined(${define})\n"; - $feature_defines .= " \"${define}\",\n"; - $feature_defines .= "#endif /* ${define} */\n"; - } - - if (!$in_section) { - my ($section_name) = $line =~ /SECTION: ([\w ]+)/; - my $found_section = grep $_ eq $section_name, @sections; - - $in_section = 1 if ($found_section); - } -}; + if (!$in_section) { + my ($section_name) = $line =~ /SECTION: ([\w ]+)/; + my $found_section = grep $_ eq $section_name, @sections; + $in_section = 1 if ($found_section); + } + }; + close(FILE); +} $feature_format =~ s/FEATURE_DEFINES\n/$feature_defines/g; open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!"; From b4b1ae193b4a1d138fc6f06cf2b8d9f797b4773e Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Fri, 5 Apr 2019 04:16:12 -0400 Subject: [PATCH 04/10] Add description of generate_query_config.pl argument --- scripts/generate_query_config.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/generate_query_config.pl b/scripts/generate_query_config.pl index cc49df4eb9..af3076be0e 100755 --- a/scripts/generate_query_config.pl +++ b/scripts/generate_query_config.pl @@ -14,7 +14,7 @@ # information is used to automatically generate the body of the query_config() # function by using the template in scripts/data_files/query_config.fmt. # -# Usage: ./scripts/generate_query_config.pl without arguments +# Usage: ./scripts/generate_query_config.pl include_crypto use strict; From 92f91fc9ff1f446da154c465fc8ed54c454b5b22 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Fri, 5 Apr 2019 05:49:53 -0400 Subject: [PATCH 05/10] Add an option to use crypto source files in generated visual c project --- scripts/generate_visualc_files.pl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/scripts/generate_visualc_files.pl b/scripts/generate_visualc_files.pl index 9913976749..37e09a1f6b 100755 --- a/scripts/generate_visualc_files.pl +++ b/scripts/generate_visualc_files.pl @@ -4,7 +4,7 @@ # 2010 # # Must be run from mbedTLS root or scripts directory. -# Takes no argument. +# Takes "include_crypto" as an argument, off by default. use warnings; use strict; @@ -18,10 +18,20 @@ my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext"; my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln"; my $vsx_sln_file = "$vsx_dir/mbedTLS.sln"; +my $include_crypto = 0; +if( @ARGV ) { + die "Invalid number of arguments" if scalar @ARGV != 1; + ($include_crypto) = @ARGV; +} + my $programs_dir = 'programs'; my $header_dir = 'include/mbedtls'; my $source_dir = 'library'; +if( $include_crypto ) { + $source_dir = 'crypto/library'; +} + # Need windows line endings! my $vsx_hdr_tpl = <\r From 91c6030584ab7a30ac4bd250cfca3ba3e250176a Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Tue, 9 Apr 2019 03:28:53 -0400 Subject: [PATCH 06/10] generate_errors.pl: add mbedtls header shadowing by crypto headers Abort script upon encountering a duplicated error --- scripts/generate_errors.pl | 41 +++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/scripts/generate_errors.pl b/scripts/generate_errors.pl index 037f214954..6e04f9bf51 100755 --- a/scripts/generate_errors.pl +++ b/scripts/generate_errors.pl @@ -4,6 +4,7 @@ # # Usage: ./generate_errors.pl or scripts/generate_errors.pl without arguments, # or generate_errors.pl include_dir data_dir error_file include_crypto +# Include crypto can be either 0 (don't include) or 1 (include). On by default. use strict; @@ -16,15 +17,12 @@ if( @ARGV ) { -d $include_dir or die "No such directory: $include_dir\n"; -d $data_dir or die "No such directory: $data_dir\n"; - if( $include_crypto ) { - -d $crypto_dir or die "Crypto submodule not present\n"; - } } else { $include_dir = 'include/mbedtls'; $data_dir = 'scripts/data_files'; $error_file = 'library/error.c'; $include_crypto = 1; - -d $crypto_dir or die "Crypto submodule not present\n"; + unless( -d $include_dir && -d $data_dir ) { chdir '..' or die; -d $include_dir && -d $data_dir @@ -32,6 +30,10 @@ if( @ARGV ) { } } +if( $include_crypto ) { + -d $crypto_include_dir or die "Crypto submodule not present\n"; +} + my $error_format_file = $data_dir.'/error.fmt'; my @low_level_modules = qw( AES ARC4 ARIA ASN1 BASE64 BIGNUM BLOWFISH @@ -52,14 +54,31 @@ close(FORMAT_FILE); $/ = $line_separator; -my @files = <$include_dir/*.h>; +my %files; if( $include_crypto ) { - @files = (<$include_dir/*.h>,<$crypto_dir/$include_dir/*.h>); + my @crypto_headers = <$crypto_dir/$include_dir/*.h>; + my @mbedtls_files = <$include_dir/*.h>; + $files{$_}++ for (@crypto_headers); + + foreach my $file (@mbedtls_files) { + my $stripped_filename = substr($file, rindex($file,"/")+1, length($file)-rindex($file,"/")-1); + my $crypto_counterpart = "$crypto_dir/$include_dir/$stripped_filename"; + if ( exists $files{$crypto_counterpart} ){ + next; + } + else{ + push(@{$files{$file}}); + } + } +} +else{ + my @headers = <$include_dir/*.h>; + $files{$_}++ for (@headers); } my @matches; -foreach my $file (@files) { +foreach my $file (sort keys %files) { open(FILE, "$file"); my @grep_res = grep(/^\s*#define\s+MBEDTLS_ERR_\w+\s+\-0x[0-9A-Fa-f]+/, ); push(@matches, @grep_res); @@ -84,13 +103,7 @@ foreach my $line (@matches) my ($description) = $line =~ /\/\*\*< (.*?)\.? \*\//; if( $error_codes_seen{$error_code}++ ) { - if( $include_crypto ) { - print "Duplicated error code: $error_code ($error_name)\n"; - next; - } - else { - die "Duplicated error code: $error_code ($error_name)\n" ; - } + die "Duplicated error code: $error_code ($error_name)\n"; } $description =~ s/\\/\\\\/g; From 9b11af42e2e6d8c18d2dbdb970c6a0933b1a81c5 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Fri, 12 Apr 2019 09:43:04 -0400 Subject: [PATCH 07/10] revert changes to generate_features.pl and generate_query_config.pl These script should depend solely on the external, mbedtls config --- library/version_features.c | 3 -- programs/ssl/query_config.c | 8 ---- scripts/generate_features.pl | 74 +++++++++++--------------------- scripts/generate_query_config.pl | 57 ++++++++---------------- 4 files changed, 43 insertions(+), 99 deletions(-) diff --git a/library/version_features.c b/library/version_features.c index 23b2a5a972..161788ca76 100644 --- a/library/version_features.c +++ b/library/version_features.c @@ -780,9 +780,6 @@ static const char *features[] = { #if defined(MBEDTLS_XTEA_C) "MBEDTLS_XTEA_C", #endif /* MBEDTLS_XTEA_C */ -#if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER) - "MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER", -#endif /* MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER */ #endif /* MBEDTLS_VERSION_FEATURES */ NULL }; diff --git a/programs/ssl/query_config.c b/programs/ssl/query_config.c index 41535ef4dc..f2f7b46d6d 100644 --- a/programs/ssl/query_config.c +++ b/programs/ssl/query_config.c @@ -2586,14 +2586,6 @@ int query_config( const char *config ) } #endif /* MBEDTLS_PLATFORM_GMTIME_R_ALT */ -#if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER) - if( strcmp( "MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER", config ) == 0 ) - { - MACRO_EXPANSION_TO_STR( MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER ); - return( 0 ); - } -#endif /* MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER */ - /* If the symbol is not found, return an error */ return( 1 ); } diff --git a/scripts/generate_features.pl b/scripts/generate_features.pl index 10aadb63e3..1bd82ca2a7 100755 --- a/scripts/generate_features.pl +++ b/scripts/generate_features.pl @@ -3,24 +3,18 @@ use strict; -my ($include_dir, $data_dir, $feature_file, $include_crypto); -my $crypto_include_dir = "crypto/include/mbedtls"; +my ($include_dir, $data_dir, $feature_file); if( @ARGV ) { - die "Invalid number of arguments" if scalar @ARGV != 4; - ($include_dir, $data_dir, $feature_file, $include_crypto) = @ARGV; + die "Invalid number of arguments" if scalar @ARGV != 3; + ($include_dir, $data_dir, $feature_file) = @ARGV; -d $include_dir or die "No such directory: $include_dir\n"; -d $data_dir or die "No such directory: $data_dir\n"; - if( $include_crypto ) { - -d $crypto_include_dir or die "Crypto submodule not present\n"; - } } else { $include_dir = 'include/mbedtls'; $data_dir = 'scripts/data_files'; $feature_file = 'library/version_features.c'; - $include_crypto = 1; - -d $crypto_include_dir or die "Crypto submodule not present\n"; unless( -d $include_dir && -d $data_dir ) { chdir '..' or die; @@ -42,53 +36,37 @@ my $feature_format = ; close(FORMAT_FILE); $/ = $line_separator; -my %defines_seen; -my @files = ("$include_dir/config.h"); -if( $include_crypto ) { - push(@files, "$crypto_include_dir/config.h"); -} +open(CONFIG_H, "$include_dir/config.h") || die("Failure when opening config.h: $!"); my $feature_defines = ""; +my $in_section = 0; -foreach my $file (@files) { - open(FILE, "$file") or die "Opening config file failed: '$file': $!"; +while (my $line = ) +{ + next if ($in_section && $line !~ /#define/ && $line !~ /SECTION/); + next if (!$in_section && $line !~ /SECTION/); - my $in_section = 0; - - while (my $line = ) - { - next if ($in_section && $line !~ /#define/ && $line !~ /SECTION/); - next if (!$in_section && $line !~ /SECTION/); - - if ($in_section) { - if ($line =~ /SECTION/) { - $in_section = 0; - next; - } - - my ($define) = $line =~ /#define (\w+)/; - - # Skip if this define is already added - if( $defines_seen{$define}++ ) { - print "Skipping $define, already added. \n"; - next; - } - - $feature_defines .= "#if defined(${define})\n"; - $feature_defines .= " \"${define}\",\n"; - $feature_defines .= "#endif /* ${define} */\n"; + if ($in_section) { + if ($line =~ /SECTION/) { + $in_section = 0; + next; } - if (!$in_section) { - my ($section_name) = $line =~ /SECTION: ([\w ]+)/; - my $found_section = grep $_ eq $section_name, @sections; + my ($define) = $line =~ /#define (\w+)/; + $feature_defines .= "#if defined(${define})\n"; + $feature_defines .= " \"${define}\",\n"; + $feature_defines .= "#endif /* ${define} */\n"; + } + + if (!$in_section) { + my ($section_name) = $line =~ /SECTION: ([\w ]+)/; + my $found_section = grep $_ eq $section_name, @sections; + + $in_section = 1 if ($found_section); + } +}; - $in_section = 1 if ($found_section); - } - }; - close(FILE); -} $feature_format =~ s/FEATURE_DEFINES\n/$feature_defines/g; open(ERROR_FILE, ">$feature_file") or die "Opening destination file '$feature_file': $!"; diff --git a/scripts/generate_query_config.pl b/scripts/generate_query_config.pl index af3076be0e..f15e03a358 100755 --- a/scripts/generate_query_config.pl +++ b/scripts/generate_query_config.pl @@ -14,13 +14,11 @@ # information is used to automatically generate the body of the query_config() # function by using the template in scripts/data_files/query_config.fmt. # -# Usage: ./scripts/generate_query_config.pl include_crypto +# Usage: ./scripts/generate_query_config.pl without arguments use strict; -my $include_crypto = 1; my $config_file = "./include/mbedtls/config.h"; -my $crypto_config_file = "./crypto/include/mbedtls/config.h"; my $query_config_format_file = "./scripts/data_files/query_config.fmt"; my $query_config_file = "./programs/ssl/query_config.c"; @@ -35,52 +33,31 @@ MBEDTLS_PARAM_FAILED ); my $excluded_re = join '|', @excluded; +open(CONFIG_FILE, "$config_file") or die "Opening config file '$config_file': $!"; # This variable will contain the string to replace in the CHECK_CONFIG of the # format file my $config_check = ""; -my %defines_seen; -my @files = ($config_file); +while (my $line = ) { + if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) { + my $name = $2; -if( @ARGV ) { - die "Invalid number of arguments" if scalar @ARGV != 1; - ($include_crypto) = @ARGV; -} + # Skip over the macro that prevents multiple inclusion + next if "MBEDTLS_CONFIG_H" eq $name; -if( $include_crypto ) { - push(@files, $crypto_config_file); -} + # Skip over the macro if it is in the ecluded list + next if $name =~ /$excluded_re/; -foreach my $file (@files) { - open(FILE, "$file") or die "Opening config file failed: '$file': $!"; - while (my $line = ) { - if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) { - my $name = $2; - - # Skip over the macro that prevents multiple inclusion - next if "MBEDTLS_CONFIG_H" eq $name; - - # Skip over the macro if it is in the excluded list - next if $name =~ /$excluded_re/; - - # Skip if this define is already added - if( $defines_seen{$name}++ ) { - print "Skipping $name, already added. \n"; - next; - } - - $config_check .= "#if defined($name)\n"; - $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n"; - $config_check .= " {\n"; - $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n"; - $config_check .= " return( 0 );\n"; - $config_check .= " }\n"; - $config_check .= "#endif /* $name */\n"; - $config_check .= "\n"; - } + $config_check .= "#if defined($name)\n"; + $config_check .= " if( strcmp( \"$name\", config ) == 0 )\n"; + $config_check .= " {\n"; + $config_check .= " MACRO_EXPANSION_TO_STR( $name );\n"; + $config_check .= " return( 0 );\n"; + $config_check .= " }\n"; + $config_check .= "#endif /* $name */\n"; + $config_check .= "\n"; } - close(FILE); } # Read the full format file into a string From e90205f9e6f9a8ed71c287c0afabc68c5db3e5a5 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Fri, 12 Apr 2019 09:49:30 -0400 Subject: [PATCH 08/10] generate_errors.pl: typo fix --- scripts/generate_errors.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/generate_errors.pl b/scripts/generate_errors.pl index 6e04f9bf51..ec550689fe 100755 --- a/scripts/generate_errors.pl +++ b/scripts/generate_errors.pl @@ -4,7 +4,7 @@ # # Usage: ./generate_errors.pl or scripts/generate_errors.pl without arguments, # or generate_errors.pl include_dir data_dir error_file include_crypto -# Include crypto can be either 0 (don't include) or 1 (include). On by default. +# include_crypto can be either 0 (don't include) or 1 (include). On by default. use strict; @@ -31,7 +31,7 @@ if( @ARGV ) { } if( $include_crypto ) { - -d $crypto_include_dir or die "Crypto submodule not present\n"; + -d $crypto_dir or die "Crypto submodule not present\n"; } my $error_format_file = $data_dir.'/error.fmt'; From 463f049ef0a2b17745b8b255ee997a879cb32797 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Fri, 12 Apr 2019 10:35:01 -0400 Subject: [PATCH 09/10] generate_errors.pl: refactor and simplify the code --- scripts/generate_errors.pl | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/scripts/generate_errors.pl b/scripts/generate_errors.pl index ec550689fe..2fe202e8d4 100755 --- a/scripts/generate_errors.pl +++ b/scripts/generate_errors.pl @@ -54,31 +54,19 @@ close(FORMAT_FILE); $/ = $line_separator; -my %files; - -if( $include_crypto ) { - my @crypto_headers = <$crypto_dir/$include_dir/*.h>; - my @mbedtls_files = <$include_dir/*.h>; - $files{$_}++ for (@crypto_headers); - - foreach my $file (@mbedtls_files) { - my $stripped_filename = substr($file, rindex($file,"/")+1, length($file)-rindex($file,"/")-1); - my $crypto_counterpart = "$crypto_dir/$include_dir/$stripped_filename"; - if ( exists $files{$crypto_counterpart} ){ - next; - } - else{ - push(@{$files{$file}}); - } +my @headers = (); +if ($include_crypto) { + @headers = <$crypto_dir/$include_dir/*.h>; + foreach my $header (<$include_dir/*.h>) { + my $basename = $header; $basename =~ s!.*/!!; + push @headers, $header unless -e "$crypto_dir/$include_dir/$basename"; } -} -else{ - my @headers = <$include_dir/*.h>; - $files{$_}++ for (@headers); +} else { + @headers = <$include_dir/*.h>; } my @matches; -foreach my $file (sort keys %files) { +foreach my $file (@headers) { open(FILE, "$file"); my @grep_res = grep(/^\s*#define\s+MBEDTLS_ERR_\w+\s+\-0x[0-9A-Fa-f]+/, ); push(@matches, @grep_res); From 021dc3f226daae466d0ee46552d20ebf64bc9022 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Fri, 12 Apr 2019 10:51:27 -0400 Subject: [PATCH 10/10] generate_visualc_files.pl: add mbedtls source shadowing by crypto Running the generation script with "include_crypto" input parameter set to 1 makes the mbedtls sources being overshadowed by crypto sources. In case of any duplicate sources, crypto ones take precedence. --- scripts/generate_visualc_files.pl | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/scripts/generate_visualc_files.pl b/scripts/generate_visualc_files.pl index 37e09a1f6b..e6545bc3ad 100755 --- a/scripts/generate_visualc_files.pl +++ b/scripts/generate_visualc_files.pl @@ -4,7 +4,8 @@ # 2010 # # Must be run from mbedTLS root or scripts directory. -# Takes "include_crypto" as an argument, off by default. +# Takes "include_crypto" as an argument that can be either 0 (don't include) or +# 1 (include). Off by default. use warnings; use strict; @@ -27,10 +28,7 @@ if( @ARGV ) { my $programs_dir = 'programs'; my $header_dir = 'include/mbedtls'; my $source_dir = 'library'; - -if( $include_crypto ) { - $source_dir = 'crypto/library'; -} +my $crypto_dir = 'crypto'; # Need windows line endings! my $vsx_hdr_tpl = <; - my @sources = <$source_dir/*.c>; + + my @sources = (); + if ($include_crypto) { + @sources = <$crypto_dir/$source_dir/*.c>; + foreach my $file (<$source_dir/*.c>) { + my $basename = $file; $basename =~ s!.*/!!; + push @sources, $file unless -e "$crypto_dir/$source_dir/$basename"; + } + } else { + @sources = <$source_dir/*.c>; + } + map { s!/!\\!g } @headers; map { s!/!\\!g } @sources;