Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 1 | #! /usr/bin/env perl |
| 2 | |
| 3 | # Generate query_config.c |
| 4 | # |
| 5 | # The file query_config.c contains a C function that can be used to check if |
| 6 | # a configuration macro is defined and to retrieve its expansion in string |
| 7 | # form (if any). This facilitates querying the compile time configuration of |
| 8 | # the library, for example, for testing. |
| 9 | # |
Ronald Cron | eab2055 | 2023-08-30 17:36:25 +0200 | [diff] [blame^] | 10 | # The query_config.c is generated from the default configuration files |
| 11 | # include/mbedtls/mbedtls_config.h and include/psa/crypto_config.h. |
| 12 | # The idea is that mbedtls_config.h and crypto_config.h contain ALL the |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 13 | # compile time configurations available in Mbed TLS (commented or uncommented). |
Ronald Cron | eab2055 | 2023-08-30 17:36:25 +0200 | [diff] [blame^] | 14 | # This script extracts the configuration macros from the two files and this |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 15 | # information is used to automatically generate the body of the query_config() |
| 16 | # function by using the template in scripts/data_files/query_config.fmt. |
| 17 | # |
Manuel Pégourié-Gonnard | 3a8413d | 2021-05-14 09:23:57 +0200 | [diff] [blame] | 18 | # Usage: scripts/generate_query_config.pl without arguments, or |
Ronald Cron | eab2055 | 2023-08-30 17:36:25 +0200 | [diff] [blame^] | 19 | # generate_query_config.pl mbedtls_config_file psa_crypto_config_file template_file output_file |
Bence Szépkúti | 700ee44 | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 20 | # |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 21 | # Copyright The Mbed TLS Contributors |
Bence Szépkúti | c7da1fe | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 22 | # SPDX-License-Identifier: Apache-2.0 |
| 23 | # |
| 24 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 25 | # not use this file except in compliance with the License. |
| 26 | # You may obtain a copy of the License at |
| 27 | # |
| 28 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 29 | # |
| 30 | # Unless required by applicable law or agreed to in writing, software |
| 31 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 32 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 33 | # See the License for the specific language governing permissions and |
| 34 | # limitations under the License. |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 35 | |
| 36 | use strict; |
| 37 | |
Ronald Cron | eab2055 | 2023-08-30 17:36:25 +0200 | [diff] [blame^] | 38 | my ($mbedtls_config_file, $psa_crypto_config_file, $query_config_format_file, $query_config_file); |
Tom Cosgrove | ef83b83 | 2022-07-25 11:42:38 +0100 | [diff] [blame] | 39 | |
| 40 | my $default_mbedtls_config_file = "./include/mbedtls/mbedtls_config.h"; |
Ronald Cron | eab2055 | 2023-08-30 17:36:25 +0200 | [diff] [blame^] | 41 | my $default_psa_crypto_config_file = "./include/psa/crypto_config.h"; |
Tom Cosgrove | ef83b83 | 2022-07-25 11:42:38 +0100 | [diff] [blame] | 42 | my $default_query_config_format_file = "./scripts/data_files/query_config.fmt"; |
| 43 | my $default_query_config_file = "./programs/test/query_config.c"; |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | 3a8413d | 2021-05-14 09:23:57 +0200 | [diff] [blame] | 45 | if( @ARGV ) { |
Ronald Cron | eab2055 | 2023-08-30 17:36:25 +0200 | [diff] [blame^] | 46 | die "Invalid number of arguments - usage: $0 [CONFIG_FILE TEMPLATE_FILE OUTPUT_FILE]" if scalar @ARGV != 4; |
| 47 | ($mbedtls_config_file, $psa_crypto_config_file, $query_config_format_file, $query_config_file) = @ARGV; |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 48 | |
Tom Cosgrove | ef83b83 | 2022-07-25 11:42:38 +0100 | [diff] [blame] | 49 | -f $mbedtls_config_file or die "No such file: $mbedtls_config_file"; |
Ronald Cron | eab2055 | 2023-08-30 17:36:25 +0200 | [diff] [blame^] | 50 | -f $psa_crypto_config_file or die "No such file: $psa_crypto_config_file"; |
Manuel Pégourié-Gonnard | 3a8413d | 2021-05-14 09:23:57 +0200 | [diff] [blame] | 51 | -f $query_config_format_file or die "No such file: $query_config_format_file"; |
| 52 | } else { |
Tom Cosgrove | ef83b83 | 2022-07-25 11:42:38 +0100 | [diff] [blame] | 53 | $mbedtls_config_file = $default_mbedtls_config_file; |
Ronald Cron | eab2055 | 2023-08-30 17:36:25 +0200 | [diff] [blame^] | 54 | $psa_crypto_config_file = $default_psa_crypto_config_file; |
Tom Cosgrove | ef83b83 | 2022-07-25 11:42:38 +0100 | [diff] [blame] | 55 | $query_config_format_file = $default_query_config_format_file; |
| 56 | $query_config_file = $default_query_config_file; |
Manuel Pégourié-Gonnard | 3a8413d | 2021-05-14 09:23:57 +0200 | [diff] [blame] | 57 | |
Tom Cosgrove | ff3c6c1 | 2022-07-25 12:19:35 +0100 | [diff] [blame] | 58 | unless(-f $mbedtls_config_file && -f $query_config_format_file && -f $psa_crypto_config_file) { |
Manuel Pégourié-Gonnard | 3a8413d | 2021-05-14 09:23:57 +0200 | [diff] [blame] | 59 | chdir '..' or die; |
Tom Cosgrove | ff3c6c1 | 2022-07-25 12:19:35 +0100 | [diff] [blame] | 60 | -f $mbedtls_config_file && -f $query_config_format_file && -f $psa_crypto_config_file |
David Horstmann | d64f4b2 | 2021-10-20 12:29:47 +0100 | [diff] [blame] | 61 | or die "No arguments supplied, must be run from project root or a first-level subdirectory\n"; |
Manuel Pégourié-Gonnard | 3a8413d | 2021-05-14 09:23:57 +0200 | [diff] [blame] | 62 | } |
Gilles Peskine | c86f20a | 2021-04-22 00:20:47 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Andres Amaya Garcia | ef672f0 | 2019-01-03 20:16:43 +0000 | [diff] [blame] | 65 | # Excluded macros from the generated query_config.c. For example, macros that |
| 66 | # have commas or function-like macros cannot be transformed into strings easily |
| 67 | # using the preprocessor, so they should be excluded or the preprocessor will |
| 68 | # throw errors. |
| 69 | my @excluded = qw( |
| 70 | MBEDTLS_SSL_CIPHERSUITES |
| 71 | ); |
| 72 | my $excluded_re = join '|', @excluded; |
| 73 | |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 74 | # This variable will contain the string to replace in the CHECK_CONFIG of the |
| 75 | # format file |
| 76 | my $config_check = ""; |
Jerry Yu | 84e63a7 | 2021-12-06 13:40:37 +0800 | [diff] [blame] | 77 | my $list_config = ""; |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 78 | |
Tom Cosgrove | ff3c6c1 | 2022-07-25 12:19:35 +0100 | [diff] [blame] | 79 | for my $config_file ($mbedtls_config_file, $psa_crypto_config_file) { |
Tom Cosgrove | ef83b83 | 2022-07-25 11:42:38 +0100 | [diff] [blame] | 80 | |
Tom Cosgrove | ff3c6c1 | 2022-07-25 12:19:35 +0100 | [diff] [blame] | 81 | next unless defined($config_file); # we might not have been given a PSA crypto config file |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 82 | |
Tom Cosgrove | ff3c6c1 | 2022-07-25 12:19:35 +0100 | [diff] [blame] | 83 | open(CONFIG_FILE, "<", $config_file) or die "Opening config file '$config_file': $!"; |
Andres Amaya Garcia | ef672f0 | 2019-01-03 20:16:43 +0000 | [diff] [blame] | 84 | |
Tom Cosgrove | ff3c6c1 | 2022-07-25 12:19:35 +0100 | [diff] [blame] | 85 | while (my $line = <CONFIG_FILE>) { |
| 86 | if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+|PSA_WANT_\w+).*/) { |
| 87 | my $name = $2; |
| 88 | |
Tom Cosgrove | 5900c1d | 2022-07-27 08:55:03 +0100 | [diff] [blame] | 89 | # Skip over the macro if it is in the excluded list |
Tom Cosgrove | ff3c6c1 | 2022-07-25 12:19:35 +0100 | [diff] [blame] | 90 | next if $name =~ /$excluded_re/; |
| 91 | |
| 92 | $config_check .= <<EOT; |
Tom Cosgrove | ef83b83 | 2022-07-25 11:42:38 +0100 | [diff] [blame] | 93 | #if defined($name) |
| 94 | if( strcmp( "$name", config ) == 0 ) |
| 95 | { |
| 96 | MACRO_EXPANSION_TO_STR( $name ); |
| 97 | return( 0 ); |
| 98 | } |
| 99 | #endif /* $name */ |
Jerry Yu | 84e63a7 | 2021-12-06 13:40:37 +0800 | [diff] [blame] | 100 | |
Tom Cosgrove | ef83b83 | 2022-07-25 11:42:38 +0100 | [diff] [blame] | 101 | EOT |
| 102 | |
Tom Cosgrove | ff3c6c1 | 2022-07-25 12:19:35 +0100 | [diff] [blame] | 103 | $list_config .= <<EOT; |
Tom Cosgrove | ef83b83 | 2022-07-25 11:42:38 +0100 | [diff] [blame] | 104 | #if defined($name) |
| 105 | OUTPUT_MACRO_NAME_VALUE($name); |
| 106 | #endif /* $name */ |
| 107 | |
| 108 | EOT |
Tom Cosgrove | ff3c6c1 | 2022-07-25 12:19:35 +0100 | [diff] [blame] | 109 | } |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 110 | } |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 111 | |
Tom Cosgrove | ff3c6c1 | 2022-07-25 12:19:35 +0100 | [diff] [blame] | 112 | close(CONFIG_FILE); |
| 113 | } |
Tom Cosgrove | ef83b83 | 2022-07-25 11:42:38 +0100 | [diff] [blame] | 114 | |
Andres Amaya Garcia | 109f8b6 | 2018-10-23 19:53:14 +0100 | [diff] [blame] | 115 | # Read the full format file into a string |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 116 | local $/; |
Tom Cosgrove | ef83b83 | 2022-07-25 11:42:38 +0100 | [diff] [blame] | 117 | open(FORMAT_FILE, "<", $query_config_format_file) or die "Opening query config format file '$query_config_format_file': $!"; |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 118 | my $query_config_format = <FORMAT_FILE>; |
| 119 | close(FORMAT_FILE); |
| 120 | |
| 121 | # Replace the body of the query_config() function with the code we just wrote |
| 122 | $query_config_format =~ s/CHECK_CONFIG/$config_check/g; |
Jerry Yu | 84e63a7 | 2021-12-06 13:40:37 +0800 | [diff] [blame] | 123 | $query_config_format =~ s/LIST_CONFIG/$list_config/g; |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 124 | |
| 125 | # Rewrite the query_config.c file |
Tom Cosgrove | ef83b83 | 2022-07-25 11:42:38 +0100 | [diff] [blame] | 126 | open(QUERY_CONFIG_FILE, ">", $query_config_file) or die "Opening destination file '$query_config_file': $!"; |
Andres Amaya Garcia | 88121a9 | 2018-10-16 22:00:13 +0100 | [diff] [blame] | 127 | print QUERY_CONFIG_FILE $query_config_format; |
| 128 | close(QUERY_CONFIG_FILE); |