blob: 346f6ff710d5cb0ea2ff662dafe026f51b9d75f0 [file] [log] [blame]
Gilles Peskinef5ea1972019-01-29 08:50:20 +01001#!/usr/bin/env python3
2
Dave Rodgman7ff79652023-11-03 12:04:52 +00003# Copyright The Mbed TLS Contributors
4# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinef5ea1972019-01-29 08:50:20 +01005
Gilles Peskinef5ea1972019-01-29 08:50:20 +01006"""
Andrzej Kurek110fc482022-10-09 05:29:44 -04007Test Mbed TLS with a subset of algorithms.
Gilles Peskinef5ea1972019-01-29 08:50:20 +01008
Andrzej Kurek110fc482022-10-09 05:29:44 -04009This script can be divided into several steps:
10
Andrzej Kurek467a0f22022-10-20 06:15:06 -040011First, include/mbedtls/config.h or a different config file passed
Andrzej Kurekcf394062023-02-15 05:42:02 -050012in the arguments is parsed to extract any configuration options (using config.py).
Andrzej Kurek110fc482022-10-09 05:29:44 -040013
14Then, test domains (groups of jobs, tests) are built based on predefined data
15collected in the DomainData class. Here, each domain has five major traits:
Andrzej Kurekffbc8f52022-10-17 08:34:40 -040016- domain name, can be used to run only specific tests via command-line;
Andrzej Kurek110fc482022-10-09 05:29:44 -040017- configuration building method, described in detail below;
18- list of symbols passed to the configuration building method;
19- commands to be run on each job (only build, build and test, or any other custom);
20- optional list of symbols to be excluded from testing.
21
22The configuration building method can be one of the three following:
23
24- ComplementaryDomain - build a job for each passed symbol by disabling a single
25 symbol and its reverse dependencies (defined in REVERSE_DEPENDENCIES);
26
27- ExclusiveDomain - build a job where, for each passed symbol, only this particular
28 one is defined and other symbols from the list are unset. For each job look for
29 any non-standard symbols to set/unset in EXCLUSIVE_GROUPS. These are usually not
30 direct dependencies, but rather non-trivial results of other configs missing. Then
31 look for any unset symbols and handle their reverse dependencies.
Andrzej Kurek110fc482022-10-09 05:29:44 -040032
33- DualDomain - combination of the two above - both complementary and exclusive domain
34 job generation code will be run. Currently only used for hashes.
35
36Lastly, the collected jobs are executed and (optionally) tested, with
37error reporting and coloring as configured in options. Each test starts with
38a full config without a couple of slowing down or unnecessary options
39(see set_reference_config), then the specific job config is derived.
40"""
Gilles Peskinef5ea1972019-01-29 08:50:20 +010041import argparse
42import os
43import re
44import shutil
45import subprocess
46import sys
47import traceback
Andrzej Kurek2432dc22023-01-24 07:40:42 -050048from typing import Union
49
Andrzej Kurek2e1aeb12023-01-23 07:19:22 -050050# Add the Mbed TLS Python library directory to the module search path
Andrzej Kurekcf394062023-02-15 05:42:02 -050051import scripts_path # pylint: disable=unused-import
52import config
53
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -040054class Colors: # pylint: disable=too-few-public-methods
Gilles Peskinefd1d69c2019-01-29 18:48:48 +010055 """Minimalistic support for colored output.
56Each field of an object of this class is either None if colored output
57is not possible or not desired, or a pair of strings (start, stop) such
58that outputting start switches the text color to the desired color and
59stop switches the text color back to the default."""
60 red = None
61 green = None
Andrzej Kurekcf394062023-02-15 05:42:02 -050062 cyan = None
Gilles Peskinefd1d69c2019-01-29 18:48:48 +010063 bold_red = None
64 bold_green = None
65 def __init__(self, options=None):
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -040066 """Initialize color profile according to passed options."""
Gilles Peskinefd1d69c2019-01-29 18:48:48 +010067 if not options or options.color in ['no', 'never']:
68 want_color = False
69 elif options.color in ['yes', 'always']:
70 want_color = True
71 else:
72 want_color = sys.stderr.isatty()
73 if want_color:
74 # Assume ANSI compatible terminal
75 normal = '\033[0m'
76 self.red = ('\033[31m', normal)
77 self.green = ('\033[32m', normal)
Andrzej Kurekcf394062023-02-15 05:42:02 -050078 self.cyan = ('\033[36m', normal)
Gilles Peskinefd1d69c2019-01-29 18:48:48 +010079 self.bold_red = ('\033[1;31m', normal)
80 self.bold_green = ('\033[1;32m', normal)
81NO_COLORS = Colors(None)
82
83def log_line(text, prefix='depends.py:', suffix='', color=None):
Gilles Peskinef5ea1972019-01-29 08:50:20 +010084 """Print a status message."""
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -040085 if color is not None:
Gilles Peskinefd1d69c2019-01-29 18:48:48 +010086 prefix = color[0] + prefix
87 suffix = suffix + color[1]
88 sys.stderr.write(prefix + ' ' + text + suffix + '\n')
Gilles Peskinee6a60db2019-01-29 18:42:55 +010089 sys.stderr.flush()
Gilles Peskinef5ea1972019-01-29 08:50:20 +010090
Gilles Peskined43ce2b2019-01-29 18:46:34 +010091def log_command(cmd):
92 """Print a trace of the specified command.
93cmd is a list of strings: a command name and its arguments."""
94 log_line(' '.join(cmd), prefix='+')
95
Gilles Peskinef5ea1972019-01-29 08:50:20 +010096def backup_config(options):
Andrzej Kurek467a0f22022-10-20 06:15:06 -040097 """Back up the library configuration file (config.h).
Gilles Peskine88e8dd62019-01-29 18:52:16 +010098If the backup file already exists, it is presumed to be the desired backup,
99so don't make another backup."""
100 if os.path.exists(options.config_backup):
101 options.own_backup = False
102 else:
103 options.own_backup = True
104 shutil.copy(options.config, options.config_backup)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100105
Gilles Peskine88e8dd62019-01-29 18:52:16 +0100106def restore_config(options):
Andrzej Kurek467a0f22022-10-20 06:15:06 -0400107 """Restore the library configuration file (config.h).
Gilles Peskine88e8dd62019-01-29 18:52:16 +0100108Remove the backup file if it was saved earlier."""
109 if options.own_backup:
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100110 shutil.move(options.config_backup, options.config)
111 else:
112 shutil.copy(options.config_backup, options.config)
Gilles Peskine88e8dd62019-01-29 18:52:16 +0100113
Andrzej Kurekcf394062023-02-15 05:42:02 -0500114def option_exists(conf, option):
Andrzej Kurek3ebe7d62023-02-06 10:48:43 +0100115 return option in conf.settings
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100116
Andrzej Kurek2432dc22023-01-24 07:40:42 -0500117def set_config_option_value(conf, option, colors, value: Union[bool, str]):
118 """Set/unset a configuration option, optionally specifying a value.
119value can be either True/False (set/unset config option), or a string,
120which will make a symbol defined with a certain value."""
Andrzej Kurekcf394062023-02-15 05:42:02 -0500121 if not option_exists(conf, option):
122 log_line('Symbol {} was not found in {}'.format(option, conf.filename), color=colors.red)
123 return False
Andrzej Kurekcf394062023-02-15 05:42:02 -0500124
Andrzej Kurek2e1aeb12023-01-23 07:19:22 -0500125 if value is False:
126 log_command(['config.py', 'unset', option])
127 conf.unset(option)
Andrzej Kurek3e7666b2023-02-06 10:49:46 +0100128 elif value is True:
129 log_command(['config.py', 'set', option])
130 conf.set(option)
Andrzej Kurek2e1aeb12023-01-23 07:19:22 -0500131 else:
Andrzej Kurek3e7666b2023-02-06 10:49:46 +0100132 log_command(['config.py', 'set', option, value])
133 conf.set(option, value)
Andrzej Kurekcf394062023-02-15 05:42:02 -0500134 return True
135
136def set_reference_config(conf, options, colors):
Andrzej Kurek467a0f22022-10-20 06:15:06 -0400137 """Change the library configuration file (config.h) to the reference state.
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -0400138The reference state is the one from which the tested configurations are
139derived."""
Andrzej Kurek8b7a1572022-10-14 07:06:43 -0400140 # Turn off options that are not relevant to the tests and slow them down.
Andrzej Kurekcf394062023-02-15 05:42:02 -0500141 log_command(['config.py', 'full'])
142 conf.adapt(config.full_adapter)
Andrzej Kurek2e1aeb12023-01-23 07:19:22 -0500143 set_config_option_value(conf, 'MBEDTLS_TEST_HOOKS', colors, False)
Andrzej Kurek2b44a922022-10-24 10:41:20 -0400144 if options.unset_use_psa:
Andrzej Kurek2e1aeb12023-01-23 07:19:22 -0500145 set_config_option_value(conf, 'MBEDTLS_USE_PSA_CRYPTO', colors, False)
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -0400146
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100147class Job:
148 """A job builds the library in a specific configuration and runs some tests."""
149 def __init__(self, name, config_settings, commands):
150 """Build a job object.
151The job uses the configuration described by config_settings. This is a
152dictionary where the keys are preprocessor symbols and the values are
153booleans or strings. A boolean indicates whether or not to #define the
154symbol. With a string, the symbol is #define'd to that value.
155After setting the configuration, the job runs the programs specified by
156commands. This is a list of lists of strings; each list of string is a
157command name and its arguments and is passed to subprocess.call with
158shell=False."""
159 self.name = name
160 self.config_settings = config_settings
161 self.commands = commands
162
Gilles Peskinefd1d69c2019-01-29 18:48:48 +0100163 def announce(self, colors, what):
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100164 '''Announce the start or completion of a job.
165If what is None, announce the start of the job.
166If what is True, announce that the job has passed.
167If what is False, announce that the job has failed.'''
168 if what is True:
Gilles Peskinefd1d69c2019-01-29 18:48:48 +0100169 log_line(self.name + ' PASSED', color=colors.green)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100170 elif what is False:
Gilles Peskinefd1d69c2019-01-29 18:48:48 +0100171 log_line(self.name + ' FAILED', color=colors.red)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100172 else:
Andrzej Kurekcf394062023-02-15 05:42:02 -0500173 log_line('starting ' + self.name, color=colors.cyan)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100174
Andrzej Kurekcf394062023-02-15 05:42:02 -0500175 def configure(self, conf, options, colors):
Andrzej Kurek8b7a1572022-10-14 07:06:43 -0400176 '''Set library configuration options as required for the job.'''
Andrzej Kurekcf394062023-02-15 05:42:02 -0500177 set_reference_config(conf, options, colors)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100178 for key, value in sorted(self.config_settings.items()):
Andrzej Kurek2e1aeb12023-01-23 07:19:22 -0500179 ret = set_config_option_value(conf, key, colors, value)
Andrzej Kurekcf394062023-02-15 05:42:02 -0500180 if ret is False:
181 return False
182 return True
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100183
184 def test(self, options):
185 '''Run the job's build and test commands.
186Return True if all the commands succeed and False otherwise.
187If options.keep_going is false, stop as soon as one command fails. Otherwise
188run all the commands, except that if the first command fails, none of the
189other commands are run (typically, the first command is a build command
190and subsequent commands are tests that cannot run if the build failed).'''
191 built = False
192 success = True
193 for command in self.commands:
Gilles Peskined43ce2b2019-01-29 18:46:34 +0100194 log_command(command)
Gilles Peskine0af7a902024-02-12 14:16:05 +0100195 env = os.environ.copy()
196 if 'MBEDTLS_TEST_CONFIGURATION' in env:
197 env['MBEDTLS_TEST_CONFIGURATION'] += '-' + self.name
198 ret = subprocess.call(command, env=env)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100199 if ret != 0:
200 if command[0] not in ['make', options.make_command]:
201 log_line('*** [{}] Error {}'.format(' '.join(command), ret))
202 if not options.keep_going or not built:
203 return False
204 success = False
205 built = True
206 return success
207
208# SSL/TLS versions up to 1.1 and corresponding options. These require
209# both MD5 and SHA-1.
Andrzej Kurekfb3e27e2022-10-04 16:22:22 -0400210SSL_PRE_1_2_DEPENDENCIES = ['MBEDTLS_SSL_CBC_RECORD_SPLITTING',
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100211 'MBEDTLS_SSL_PROTO_SSL3',
212 'MBEDTLS_SSL_PROTO_TLS1',
213 'MBEDTLS_SSL_PROTO_TLS1_1']
214
215# If the configuration option A requires B, make sure that
Andrzej Kurekfb3e27e2022-10-04 16:22:22 -0400216# B in REVERSE_DEPENDENCIES[A].
Gilles Peskineb81f4062019-01-29 19:30:40 +0100217# All the information here should be contained in check_config.h. This
218# file includes a copy because it changes rarely and it would be a pain
219# to extract automatically.
Andrzej Kurekfb3e27e2022-10-04 16:22:22 -0400220REVERSE_DEPENDENCIES = {
Gilles Peskine3ce0e322019-01-29 23:12:28 +0100221 'MBEDTLS_AES_C': ['MBEDTLS_CTR_DRBG_C',
Andrzej Kurek90686252022-09-28 03:17:56 -0400222 'MBEDTLS_NIST_KW_C'],
Gilles Peskine3ce0e322019-01-29 23:12:28 +0100223 'MBEDTLS_CHACHA20_C': ['MBEDTLS_CHACHAPOLY_C'],
Andrzej Kurek90686252022-09-28 03:17:56 -0400224 'MBEDTLS_ECDSA_C': ['MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED',
225 'MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED'],
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100226 'MBEDTLS_ECP_C': ['MBEDTLS_ECDSA_C',
227 'MBEDTLS_ECDH_C',
228 'MBEDTLS_ECJPAKE_C',
Manuel Pégourié-Gonnard3dc7f232022-12-06 13:20:06 +0100229 'MBEDTLS_ECP_RESTARTABLE',
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100230 'MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED',
231 'MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED',
232 'MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED',
233 'MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED',
Andrzej Kurek90686252022-09-28 03:17:56 -0400234 'MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED',
235 'MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED'],
Gilles Peskineb81f4062019-01-29 19:30:40 +0100236 'MBEDTLS_ECP_DP_SECP256R1_ENABLED': ['MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED'],
Andrzej Kurekfb3e27e2022-10-04 16:22:22 -0400237 'MBEDTLS_MD5_C': SSL_PRE_1_2_DEPENDENCIES,
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100238 'MBEDTLS_PKCS1_V21': ['MBEDTLS_X509_RSASSA_PSS_SUPPORT'],
239 'MBEDTLS_PKCS1_V15': ['MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED',
240 'MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED',
241 'MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED',
242 'MBEDTLS_KEY_EXCHANGE_RSA_ENABLED'],
243 'MBEDTLS_RSA_C': ['MBEDTLS_X509_RSASSA_PSS_SUPPORT',
244 'MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED',
245 'MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED',
246 'MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED',
Andrzej Kurek90686252022-09-28 03:17:56 -0400247 'MBEDTLS_KEY_EXCHANGE_RSA_ENABLED',
248 'MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED'],
Andrzej Kurekfb3e27e2022-10-04 16:22:22 -0400249 'MBEDTLS_SHA1_C': SSL_PRE_1_2_DEPENDENCIES,
Gilles Peskineb81f4062019-01-29 19:30:40 +0100250 'MBEDTLS_SHA256_C': ['MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED',
Andrzej Kurekb790c932023-02-15 15:19:37 -0500251 'MBEDTLS_ENTROPY_FORCE_SHA256'],
Andrzej Kurek90686252022-09-28 03:17:56 -0400252 'MBEDTLS_X509_RSASSA_PSS_SUPPORT': []
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100253}
254
Andrzej Kurek90686252022-09-28 03:17:56 -0400255# If an option is tested in an exclusive test, alter the following defines.
Andrzej Kurek110fc482022-10-09 05:29:44 -0400256# These are not necessarily dependencies, but just minimal required changes
Andrzej Kurek90686252022-09-28 03:17:56 -0400257# if a given define is the only one enabled from an exclusive group.
Andrzej Kurekfb3e27e2022-10-04 16:22:22 -0400258EXCLUSIVE_GROUPS = {
Andrzej Kurek467a0f22022-10-20 06:15:06 -0400259 'MBEDTLS_SHA512_C': ['-MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL'],
Andrzej Kurekf53d0ba2022-11-23 05:54:46 -0500260 'MBEDTLS_SHA512_NO_SHA384': ['+MBEDTLS_SHA512_C',
261 '-MBEDTLS_SSL_PROTO_TLS1_2',
262 '-MBEDTLS_SSL_PROTO_DTLS',
263 '-MBEDTLS_SSL_TLS_C',
264 '-MBEDTLS_SSL_CLI_C',
265 '-MBEDTLS_SSL_SRV_C',
266 '-MBEDTLS_SSL_DTLS_HELLO_VERIFY',
267 '-MBEDTLS_SSL_DTLS_ANTI_REPLAY',
268 '-MBEDTLS_SSL_DTLS_CONNECTION_ID',
269 '-MBEDTLS_SSL_DTLS_BADMAC_LIMIT',
270 '-MBEDTLS_SSL_ENCRYPT_THEN_MAC',
271 '-MBEDTLS_SSL_EXTENDED_MASTER_SECRET',
272 '-MBEDTLS_SSL_DTLS_SRTP',
273 '-MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE'],
Andrzej Kurek9cbdf102022-10-14 08:09:16 -0400274 'MBEDTLS_ECP_DP_CURVE448_ENABLED': ['-MBEDTLS_ECDSA_C',
275 '-MBEDTLS_ECDSA_DETERMINISTIC',
276 '-MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED',
277 '-MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED',
278 '-MBEDTLS_ECJPAKE_C',
279 '-MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED'],
280 'MBEDTLS_ECP_DP_CURVE25519_ENABLED': ['-MBEDTLS_ECDSA_C',
281 '-MBEDTLS_ECDSA_DETERMINISTIC',
282 '-MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED',
283 '-MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED',
284 '-MBEDTLS_ECJPAKE_C',
285 '-MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED'],
286 'MBEDTLS_ARIA_C': ['-MBEDTLS_CMAC_C'],
Andrzej Kurek467a0f22022-10-20 06:15:06 -0400287 'MBEDTLS_ARC4_C': ['-MBEDTLS_CMAC_C',
288 '-MBEDTLS_CCM_C',
289 '-MBEDTLS_SSL_TICKET_C',
290 '-MBEDTLS_SSL_CONTEXT_SERIALIZATION',
291 '-MBEDTLS_GCM_C'],
292 'MBEDTLS_BLOWFISH_C': ['-MBEDTLS_CMAC_C',
293 '-MBEDTLS_CCM_C',
294 '-MBEDTLS_SSL_TICKET_C',
295 '-MBEDTLS_SSL_CONTEXT_SERIALIZATION',
296 '-MBEDTLS_GCM_C'],
Andrzej Kurek9cbdf102022-10-14 08:09:16 -0400297 'MBEDTLS_CAMELLIA_C': ['-MBEDTLS_CMAC_C'],
298 'MBEDTLS_CHACHA20_C': ['-MBEDTLS_CMAC_C', '-MBEDTLS_CCM_C', '-MBEDTLS_GCM_C'],
299 'MBEDTLS_DES_C': ['-MBEDTLS_CCM_C',
300 '-MBEDTLS_GCM_C',
301 '-MBEDTLS_SSL_TICKET_C',
302 '-MBEDTLS_SSL_CONTEXT_SERIALIZATION'],
Andrzej Kurek90686252022-09-28 03:17:56 -0400303}
304def handle_exclusive_groups(config_settings, symbol):
305 """For every symbol tested in an exclusive group check if there are other
306defines to be altered. """
Andrzej Kurekfb3e27e2022-10-04 16:22:22 -0400307 for dep in EXCLUSIVE_GROUPS.get(symbol, []):
Andrzej Kurek9cbdf102022-10-14 08:09:16 -0400308 unset = dep.startswith('-')
309 dep = dep[1:]
Andrzej Kurek90686252022-09-28 03:17:56 -0400310 config_settings[dep] = not unset
311
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100312def turn_off_dependencies(config_settings):
313 """For every option turned off config_settings, also turn off what depends on it.
314An option O is turned off if config_settings[O] is False."""
315 for key, value in sorted(config_settings.items()):
316 if value is not False:
317 continue
Andrzej Kurekfb3e27e2022-10-04 16:22:22 -0400318 for dep in REVERSE_DEPENDENCIES.get(key, []):
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100319 config_settings[dep] = False
320
Andrzej Kurek068a73f2022-10-06 18:52:44 -0400321class BaseDomain: # pylint: disable=too-few-public-methods, unused-argument
322 """A base class for all domains."""
323 def __init__(self, symbols, commands, exclude):
324 """Initialize the jobs container"""
325 self.jobs = []
326
327class ExclusiveDomain(BaseDomain): # pylint: disable=too-few-public-methods
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100328 """A domain consisting of a set of conceptually-equivalent settings.
329Establish a list of configuration symbols. For each symbol, run a test job
Andrzej Kurek2e105b52022-10-06 16:57:38 -0400330with this symbol set and the others unset."""
Gilles Peskine3dd0dab2019-01-29 18:56:03 +0100331 def __init__(self, symbols, commands, exclude=None):
332 """Build a domain for the specified list of configuration symbols.
Andrzej Kurek2e105b52022-10-06 16:57:38 -0400333The domain contains a set of jobs that enable one of the elements
334of symbols and disable the others.
Gilles Peskine3dd0dab2019-01-29 18:56:03 +0100335Each job runs the specified commands.
336If exclude is a regular expression, skip generated jobs whose description
337would match this regular expression."""
Andrzej Kurek068a73f2022-10-06 18:52:44 -0400338 super().__init__(symbols, commands, exclude)
Andrzej Kurek2e105b52022-10-06 16:57:38 -0400339 base_config_settings = {}
340 for symbol in symbols:
341 base_config_settings[symbol] = False
342 for symbol in symbols:
343 description = symbol
344 if exclude and re.match(exclude, description):
345 continue
346 config_settings = base_config_settings.copy()
347 config_settings[symbol] = True
348 handle_exclusive_groups(config_settings, symbol)
349 turn_off_dependencies(config_settings)
350 job = Job(description, config_settings, commands)
351 self.jobs.append(job)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100352
Andrzej Kurek068a73f2022-10-06 18:52:44 -0400353class ComplementaryDomain(BaseDomain): # pylint: disable=too-few-public-methods
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100354 """A domain consisting of a set of loosely-related settings.
355Establish a list of configuration symbols. For each symbol, run a test job
Andrzej Kurek8b7a1572022-10-14 07:06:43 -0400356with this symbol unset.
357If exclude is a regular expression, skip generated jobs whose description
358would match this regular expression."""
Andrzej Kurek068a73f2022-10-06 18:52:44 -0400359 def __init__(self, symbols, commands, exclude=None):
Gilles Peskine3dd0dab2019-01-29 18:56:03 +0100360 """Build a domain for the specified list of configuration symbols.
361Each job in the domain disables one of the specified symbols.
362Each job runs the specified commands."""
Andrzej Kurek068a73f2022-10-06 18:52:44 -0400363 super().__init__(symbols, commands, exclude)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100364 for symbol in symbols:
365 description = '!' + symbol
Andrzej Kurek068a73f2022-10-06 18:52:44 -0400366 if exclude and re.match(exclude, description):
367 continue
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100368 config_settings = {symbol: False}
369 turn_off_dependencies(config_settings)
370 job = Job(description, config_settings, commands)
371 self.jobs.append(job)
372
Andrzej Kurek068a73f2022-10-06 18:52:44 -0400373class DualDomain(ExclusiveDomain, ComplementaryDomain): # pylint: disable=too-few-public-methods
Andrzej Kurek8b7a1572022-10-14 07:06:43 -0400374 """A domain that contains both the ExclusiveDomain and BaseDomain tests.
Andrzej Kurek0325ced2022-10-18 09:37:59 -0400375Both parent class __init__ calls are performed in any order and
Andrzej Kurek8b7a1572022-10-14 07:06:43 -0400376each call adds respective jobs. The job array initialization is done once in
377BaseDomain, before the parent __init__ calls."""
Andrzej Kurek068a73f2022-10-06 18:52:44 -0400378
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -0400379class CipherInfo: # pylint: disable=too-few-public-methods
Gilles Peskine3ce0e322019-01-29 23:12:28 +0100380 """Collect data about cipher.h."""
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -0400381 def __init__(self):
Gilles Peskine3ce0e322019-01-29 23:12:28 +0100382 self.base_symbols = set()
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -0400383 with open('include/mbedtls/cipher.h', encoding="utf-8") as fh:
Gilles Peskine3ce0e322019-01-29 23:12:28 +0100384 for line in fh:
385 m = re.match(r' *MBEDTLS_CIPHER_ID_(\w+),', line)
386 if m and m.group(1) not in ['NONE', 'NULL', '3DES']:
387 self.base_symbols.add('MBEDTLS_' + m.group(1) + '_C')
388
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100389class DomainData:
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -0400390 """A container for domains and jobs, used to structurize testing."""
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100391 def config_symbols_matching(self, regexp):
Andrzej Kurek467a0f22022-10-20 06:15:06 -0400392 """List the config.h settings matching regexp."""
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100393 return [symbol for symbol in self.all_config_symbols
394 if re.match(regexp, symbol)]
395
Andrzej Kurekcf394062023-02-15 05:42:02 -0500396 def __init__(self, options, conf):
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100397 """Gather data about the library and establish a list of domains to test."""
Dave Rodgmanb046b9a2023-12-19 11:33:55 +0000398 build_command = [options.make_command, 'CFLAGS=-Werror -O2']
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100399 build_and_test = [build_command, [options.make_command, 'test']]
Andrzej Kurekcf394062023-02-15 05:42:02 -0500400 self.all_config_symbols = set(conf.settings.keys())
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100401 # Find hash modules by name.
402 hash_symbols = self.config_symbols_matching(r'MBEDTLS_(MD|RIPEMD|SHA)[0-9]+_C\Z')
Andrzej Kurekaa112812022-11-22 08:13:45 -0500403 hash_symbols.append("MBEDTLS_SHA512_NO_SHA384")
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100404 # Find elliptic curve enabling macros by name.
405 curve_symbols = self.config_symbols_matching(r'MBEDTLS_ECP_DP_\w+_ENABLED\Z')
406 # Find key exchange enabling macros by name.
407 key_exchange_symbols = self.config_symbols_matching(r'MBEDTLS_KEY_EXCHANGE_\w+_ENABLED\Z')
Gilles Peskine3ce0e322019-01-29 23:12:28 +0100408 # Find cipher IDs (block permutations and stream ciphers --- chaining
409 # and padding modes are exercised separately) information by parsing
Andrzej Kurek467a0f22022-10-20 06:15:06 -0400410 # cipher.h, as the information is not readily available in config.h.
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -0400411 cipher_info = CipherInfo()
Gilles Peskine3ce0e322019-01-29 23:12:28 +0100412 # Find block cipher chaining and padding mode enabling macros by name.
413 cipher_chaining_symbols = self.config_symbols_matching(r'MBEDTLS_CIPHER_MODE_\w+\Z')
414 cipher_padding_symbols = self.config_symbols_matching(r'MBEDTLS_CIPHER_PADDING_\w+\Z')
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100415 self.domains = {
Gilles Peskine3ce0e322019-01-29 23:12:28 +0100416 # Cipher IDs, chaining modes and padding modes. Run the test suites.
417 'cipher_id': ExclusiveDomain(cipher_info.base_symbols,
418 build_and_test),
419 'cipher_chaining': ExclusiveDomain(cipher_chaining_symbols,
420 build_and_test),
421 'cipher_padding': ExclusiveDomain(cipher_padding_symbols,
422 build_and_test),
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100423 # Elliptic curves. Run the test suites.
424 'curves': ExclusiveDomain(curve_symbols, build_and_test),
Andrzej Kurek467a0f22022-10-20 06:15:06 -0400425 # Hash algorithms. Exclude exclusive domain of MD, RIPEMD, SHA1 (obsolete)
Andrzej Kurek068a73f2022-10-06 18:52:44 -0400426 'hashes': DualDomain(hash_symbols, build_and_test,
Andrzej Kurekaa112812022-11-22 08:13:45 -0500427 exclude=r'MBEDTLS_(MD|RIPEMD|SHA1_)'\
428 '|!MBEDTLS_*_NO_SHA'),
Andrzej Kurekddf62602023-01-23 06:19:14 -0500429 # Key exchange types.
Andrzej Kurekde416fc2022-11-02 04:50:16 -0400430 'kex': ExclusiveDomain(key_exchange_symbols, build_and_test),
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100431 'pkalgs': ComplementaryDomain(['MBEDTLS_ECDSA_C',
432 'MBEDTLS_ECP_C',
433 'MBEDTLS_PKCS1_V21',
434 'MBEDTLS_PKCS1_V15',
435 'MBEDTLS_RSA_C',
436 'MBEDTLS_X509_RSASSA_PSS_SUPPORT'],
437 build_and_test),
438 }
439 self.jobs = {}
440 for domain in self.domains.values():
441 for job in domain.jobs:
442 self.jobs[job.name] = job
443
444 def get_jobs(self, name):
445 """Return the list of jobs identified by the given name.
446A name can either be the name of a domain or the name of one specific job."""
447 if name in self.domains:
448 return sorted(self.domains[name].jobs, key=lambda job: job.name)
449 else:
450 return [self.jobs[name]]
451
Andrzej Kurekcf394062023-02-15 05:42:02 -0500452def run(options, job, conf, colors=NO_COLORS):
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100453 """Run the specified job (a Job instance)."""
454 subprocess.check_call([options.make_command, 'clean'])
Gilles Peskinefd1d69c2019-01-29 18:48:48 +0100455 job.announce(colors, None)
Andrzej Kurekcf394062023-02-15 05:42:02 -0500456 if not job.configure(conf, options, colors):
457 job.announce(colors, False)
458 return False
459 conf.write()
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100460 success = job.test(options)
Gilles Peskinefd1d69c2019-01-29 18:48:48 +0100461 job.announce(colors, success)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100462 return success
463
Andrzej Kurekcf394062023-02-15 05:42:02 -0500464def run_tests(options, domain_data, conf):
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100465 """Run the desired jobs.
466domain_data should be a DomainData instance that describes the available
467domains and jobs.
Andrzej Kurek113952d2022-10-17 08:39:09 -0400468Run the jobs listed in options.tasks."""
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100469 if not hasattr(options, 'config_backup'):
470 options.config_backup = options.config + '.bak'
Gilles Peskinefd1d69c2019-01-29 18:48:48 +0100471 colors = Colors(options)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100472 jobs = []
473 failures = []
474 successes = []
Andrzej Kurek113952d2022-10-17 08:39:09 -0400475 for name in options.tasks:
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100476 jobs += domain_data.get_jobs(name)
477 backup_config(options)
478 try:
479 for job in jobs:
Andrzej Kurekcf394062023-02-15 05:42:02 -0500480 success = run(options, job, conf, colors=colors)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100481 if not success:
482 if options.keep_going:
483 failures.append(job.name)
484 else:
485 return False
486 else:
487 successes.append(job.name)
Gilles Peskine88e8dd62019-01-29 18:52:16 +0100488 restore_config(options)
489 except:
490 # Restore the configuration, except in stop-on-error mode if there
491 # was an error, where we leave the failing configuration up for
492 # developer convenience.
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100493 if options.keep_going:
Gilles Peskine88e8dd62019-01-29 18:52:16 +0100494 restore_config(options)
495 raise
Gilles Peskinedc68f612019-01-29 18:50:03 +0100496 if successes:
497 log_line('{} passed'.format(' '.join(successes)), color=colors.bold_green)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100498 if failures:
Gilles Peskinedc68f612019-01-29 18:50:03 +0100499 log_line('{} FAILED'.format(' '.join(failures)), color=colors.bold_red)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100500 return False
501 else:
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100502 return True
503
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -0400504def main():
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100505 try:
Andrzej Kurek110fc482022-10-09 05:29:44 -0400506 parser = argparse.ArgumentParser(
507 formatter_class=argparse.RawDescriptionHelpFormatter,
508 description=
509 "Test Mbed TLS with a subset of algorithms.\n\n"
510 "Example usage:\n"
Andrzej Kurekffbc8f52022-10-17 08:34:40 -0400511 r"./tests/scripts/depends.py \!MBEDTLS_SHA1_C MBEDTLS_SHA256_C""\n"
Andrzej Kurek110fc482022-10-09 05:29:44 -0400512 "./tests/scripts/depends.py MBEDTLS_AES_C hashes\n"
513 "./tests/scripts/depends.py cipher_id cipher_chaining\n")
Gilles Peskinefd1d69c2019-01-29 18:48:48 +0100514 parser.add_argument('--color', metavar='WHEN',
515 help='Colorize the output (always/auto/never)',
516 choices=['always', 'auto', 'never'], default='auto')
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100517 parser.add_argument('-c', '--config', metavar='FILE',
518 help='Configuration file to modify',
Andrzej Kurek467a0f22022-10-20 06:15:06 -0400519 default='include/mbedtls/config.h')
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100520 parser.add_argument('-C', '--directory', metavar='DIR',
521 help='Change to this directory before anything else',
522 default='.')
523 parser.add_argument('-k', '--keep-going',
524 help='Try all configurations even if some fail (default)',
525 action='store_true', dest='keep_going', default=True)
526 parser.add_argument('-e', '--no-keep-going',
527 help='Stop as soon as a configuration fails',
528 action='store_false', dest='keep_going')
529 parser.add_argument('--list-jobs',
530 help='List supported jobs and exit',
531 action='append_const', dest='list', const='jobs')
532 parser.add_argument('--list-domains',
533 help='List supported domains and exit',
534 action='append_const', dest='list', const='domains')
535 parser.add_argument('--make-command', metavar='CMD',
536 help='Command to run instead of make (e.g. gmake)',
537 action='store', default='make')
Andrzej Kurek2b44a922022-10-24 10:41:20 -0400538 parser.add_argument('--unset-use-psa',
539 help='Unset MBEDTLS_USE_PSA_CRYPTO before any test',
540 action='store_true', dest='unset_use_psa')
Andrzej Kurek113952d2022-10-17 08:39:09 -0400541 parser.add_argument('tasks', metavar='TASKS', nargs='*',
542 help='The domain(s) or job(s) to test (default: all).',
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100543 default=True)
544 options = parser.parse_args()
545 os.chdir(options.directory)
Andrzej Kurekcf394062023-02-15 05:42:02 -0500546 conf = config.ConfigFile(options.config)
547 domain_data = DomainData(options, conf)
548
Andrzej Kurek113952d2022-10-17 08:39:09 -0400549 if options.tasks is True:
550 options.tasks = sorted(domain_data.domains.keys())
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100551 if options.list:
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -0400552 for arg in options.list:
553 for domain_name in sorted(getattr(domain_data, arg).keys()):
554 print(domain_name)
555 sys.exit(0)
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100556 else:
Andrzej Kurekcf394062023-02-15 05:42:02 -0500557 sys.exit(0 if run_tests(options, domain_data, conf) else 1)
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -0400558 except Exception: # pylint: disable=broad-except
Gilles Peskinef5ea1972019-01-29 08:50:20 +0100559 traceback.print_exc()
Andrzej Kurekb95ba9a2022-10-04 15:02:41 -0400560 sys.exit(3)
561
562if __name__ == '__main__':
563 main()