Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Copyright (c) 2018, Arm Limited, All Rights Reserved. |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | # This file is part of Mbed TLS (https://tls.mbed.org) |
| 19 | |
| 20 | """Test Mbed TLS with a subset of algorithms. |
| 21 | """ |
| 22 | |
| 23 | import argparse |
| 24 | import os |
| 25 | import re |
| 26 | import shutil |
| 27 | import subprocess |
| 28 | import sys |
| 29 | import traceback |
| 30 | |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 31 | class Colors: # pylint: disable=too-few-public-methods |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 32 | """Minimalistic support for colored output. |
| 33 | Each field of an object of this class is either None if colored output |
| 34 | is not possible or not desired, or a pair of strings (start, stop) such |
| 35 | that outputting start switches the text color to the desired color and |
| 36 | stop switches the text color back to the default.""" |
| 37 | red = None |
| 38 | green = None |
| 39 | bold_red = None |
| 40 | bold_green = None |
| 41 | def __init__(self, options=None): |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 42 | """Initialize color profile according to passed options.""" |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 43 | if not options or options.color in ['no', 'never']: |
| 44 | want_color = False |
| 45 | elif options.color in ['yes', 'always']: |
| 46 | want_color = True |
| 47 | else: |
| 48 | want_color = sys.stderr.isatty() |
| 49 | if want_color: |
| 50 | # Assume ANSI compatible terminal |
| 51 | normal = '\033[0m' |
| 52 | self.red = ('\033[31m', normal) |
| 53 | self.green = ('\033[32m', normal) |
| 54 | self.bold_red = ('\033[1;31m', normal) |
| 55 | self.bold_green = ('\033[1;32m', normal) |
| 56 | NO_COLORS = Colors(None) |
| 57 | |
| 58 | def log_line(text, prefix='depends.py:', suffix='', color=None): |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 59 | """Print a status message.""" |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 60 | if color is not None: |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 61 | prefix = color[0] + prefix |
| 62 | suffix = suffix + color[1] |
| 63 | sys.stderr.write(prefix + ' ' + text + suffix + '\n') |
Gilles Peskine | 46c8256 | 2019-01-29 18:42:55 +0100 | [diff] [blame] | 64 | sys.stderr.flush() |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 65 | |
Gilles Peskine | 54aa5c6 | 2019-01-29 18:46:34 +0100 | [diff] [blame] | 66 | def log_command(cmd): |
| 67 | """Print a trace of the specified command. |
| 68 | cmd is a list of strings: a command name and its arguments.""" |
| 69 | log_line(' '.join(cmd), prefix='+') |
| 70 | |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 71 | def backup_config(options): |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 72 | """Back up the library configuration file (mbedtls_config.h). |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 73 | If the backup file already exists, it is presumed to be the desired backup, |
| 74 | so don't make another backup.""" |
| 75 | if os.path.exists(options.config_backup): |
| 76 | options.own_backup = False |
| 77 | else: |
| 78 | options.own_backup = True |
| 79 | shutil.copy(options.config, options.config_backup) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 80 | |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 81 | def restore_config(options): |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 82 | """Restore the library configuration file (mbedtls_config.h). |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 83 | Remove the backup file if it was saved earlier.""" |
| 84 | if options.own_backup: |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 85 | shutil.move(options.config_backup, options.config) |
| 86 | else: |
| 87 | shutil.copy(options.config_backup, options.config) |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 88 | |
Gilles Peskine | 54aa5c6 | 2019-01-29 18:46:34 +0100 | [diff] [blame] | 89 | def run_config_pl(options, args): |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 90 | """Run scripts/config.py with the specified arguments.""" |
| 91 | cmd = ['scripts/config.py'] |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 92 | if options.config != 'include/mbedtls/mbedtls_config.h': |
Gilles Peskine | 54aa5c6 | 2019-01-29 18:46:34 +0100 | [diff] [blame] | 93 | cmd += ['--file', options.config] |
| 94 | cmd += args |
| 95 | log_command(cmd) |
| 96 | subprocess.check_call(cmd) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 97 | |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 98 | def set_reference_config(options): |
| 99 | """Change the library configuration file (mbedtls_config.h) to the reference state. |
| 100 | The reference state is the one from which the tested configurations are |
| 101 | derived.""" |
| 102 | # Turn off memory management options that are not relevant to |
| 103 | # the tests and slow them down. |
| 104 | run_config_pl(options, ['full']) |
| 105 | run_config_pl(options, ['unset', 'MBEDTLS_MEMORY_BACKTRACE']) |
| 106 | run_config_pl(options, ['unset', 'MBEDTLS_MEMORY_BUFFER_ALLOC_C']) |
| 107 | run_config_pl(options, ['unset', 'MBEDTLS_MEMORY_DEBUG']) |
| 108 | |
| 109 | def collect_config_symbols(options): |
| 110 | """Read the list of settings from mbedtls_config.h. |
| 111 | Return them in a generator.""" |
| 112 | with open(options.config, encoding="utf-8") as config_file: |
| 113 | rx = re.compile(r'\s*(?://\s*)?#define\s+(\w+)\s*(?:$|/[/*])') |
| 114 | for line in config_file: |
| 115 | m = re.match(rx, line) |
| 116 | if m: |
| 117 | yield m.group(1) |
| 118 | |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 119 | class Job: |
| 120 | """A job builds the library in a specific configuration and runs some tests.""" |
| 121 | def __init__(self, name, config_settings, commands): |
| 122 | """Build a job object. |
| 123 | The job uses the configuration described by config_settings. This is a |
| 124 | dictionary where the keys are preprocessor symbols and the values are |
| 125 | booleans or strings. A boolean indicates whether or not to #define the |
| 126 | symbol. With a string, the symbol is #define'd to that value. |
| 127 | After setting the configuration, the job runs the programs specified by |
| 128 | commands. This is a list of lists of strings; each list of string is a |
| 129 | command name and its arguments and is passed to subprocess.call with |
| 130 | shell=False.""" |
| 131 | self.name = name |
| 132 | self.config_settings = config_settings |
| 133 | self.commands = commands |
| 134 | |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 135 | def announce(self, colors, what): |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 136 | '''Announce the start or completion of a job. |
| 137 | If what is None, announce the start of the job. |
| 138 | If what is True, announce that the job has passed. |
| 139 | If what is False, announce that the job has failed.''' |
| 140 | if what is True: |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 141 | log_line(self.name + ' PASSED', color=colors.green) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 142 | elif what is False: |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 143 | log_line(self.name + ' FAILED', color=colors.red) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 144 | else: |
| 145 | log_line('starting ' + self.name) |
| 146 | |
Gilles Peskine | 54aa5c6 | 2019-01-29 18:46:34 +0100 | [diff] [blame] | 147 | def configure(self, options): |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 148 | '''Set library configuration options as required for the job. |
| 149 | config_file_name indicates which file to modify.''' |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 150 | set_reference_config(options) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 151 | for key, value in sorted(self.config_settings.items()): |
| 152 | if value is True: |
| 153 | args = ['set', key] |
| 154 | elif value is False: |
| 155 | args = ['unset', key] |
| 156 | else: |
| 157 | args = ['set', key, value] |
Gilles Peskine | 54aa5c6 | 2019-01-29 18:46:34 +0100 | [diff] [blame] | 158 | run_config_pl(options, args) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 159 | |
| 160 | def test(self, options): |
| 161 | '''Run the job's build and test commands. |
| 162 | Return True if all the commands succeed and False otherwise. |
| 163 | If options.keep_going is false, stop as soon as one command fails. Otherwise |
| 164 | run all the commands, except that if the first command fails, none of the |
| 165 | other commands are run (typically, the first command is a build command |
| 166 | and subsequent commands are tests that cannot run if the build failed).''' |
| 167 | built = False |
| 168 | success = True |
| 169 | for command in self.commands: |
Gilles Peskine | 54aa5c6 | 2019-01-29 18:46:34 +0100 | [diff] [blame] | 170 | log_command(command) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 171 | ret = subprocess.call(command) |
| 172 | if ret != 0: |
| 173 | if command[0] not in ['make', options.make_command]: |
| 174 | log_line('*** [{}] Error {}'.format(' '.join(command), ret)) |
| 175 | if not options.keep_going or not built: |
| 176 | return False |
| 177 | success = False |
| 178 | built = True |
| 179 | return success |
| 180 | |
| 181 | # SSL/TLS versions up to 1.1 and corresponding options. These require |
| 182 | # both MD5 and SHA-1. |
Andrzej Kurek | 202932f | 2022-10-04 16:22:22 -0400 | [diff] [blame] | 183 | SSL_PRE_1_2_DEPENDENCIES = ['MBEDTLS_SSL_CBC_RECORD_SPLITTING', |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 184 | 'MBEDTLS_SSL_PROTO_SSL3', |
| 185 | 'MBEDTLS_SSL_PROTO_TLS1', |
| 186 | 'MBEDTLS_SSL_PROTO_TLS1_1'] |
| 187 | |
| 188 | # If the configuration option A requires B, make sure that |
Andrzej Kurek | 202932f | 2022-10-04 16:22:22 -0400 | [diff] [blame] | 189 | # B in REVERSE_DEPENDENCIES[A]. |
Gilles Peskine | 584c24a | 2019-01-29 19:30:40 +0100 | [diff] [blame] | 190 | # All the information here should be contained in check_config.h. This |
| 191 | # file includes a copy because it changes rarely and it would be a pain |
| 192 | # to extract automatically. |
Andrzej Kurek | 202932f | 2022-10-04 16:22:22 -0400 | [diff] [blame] | 193 | REVERSE_DEPENDENCIES = { |
Gilles Peskine | 34a1557 | 2019-01-29 23:12:28 +0100 | [diff] [blame] | 194 | 'MBEDTLS_AES_C': ['MBEDTLS_CTR_DRBG_C', |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 195 | 'MBEDTLS_NIST_KW_C'], |
Gilles Peskine | 34a1557 | 2019-01-29 23:12:28 +0100 | [diff] [blame] | 196 | 'MBEDTLS_CHACHA20_C': ['MBEDTLS_CHACHAPOLY_C'], |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 197 | 'MBEDTLS_ECDSA_C': ['MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED', |
| 198 | 'MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED'], |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 199 | 'MBEDTLS_ECP_C': ['MBEDTLS_ECDSA_C', |
| 200 | 'MBEDTLS_ECDH_C', |
| 201 | 'MBEDTLS_ECJPAKE_C', |
| 202 | 'MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED', |
| 203 | 'MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED', |
| 204 | 'MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED', |
| 205 | 'MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED', |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 206 | 'MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED', |
| 207 | 'MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED'], |
Gilles Peskine | 584c24a | 2019-01-29 19:30:40 +0100 | [diff] [blame] | 208 | 'MBEDTLS_ECP_DP_SECP256R1_ENABLED': ['MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED'], |
Andrzej Kurek | 202932f | 2022-10-04 16:22:22 -0400 | [diff] [blame] | 209 | 'MBEDTLS_MD5_C': SSL_PRE_1_2_DEPENDENCIES, |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 210 | 'MBEDTLS_PKCS1_V21': ['MBEDTLS_X509_RSASSA_PSS_SUPPORT'], |
| 211 | 'MBEDTLS_PKCS1_V15': ['MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED', |
| 212 | 'MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED', |
| 213 | 'MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED', |
| 214 | 'MBEDTLS_KEY_EXCHANGE_RSA_ENABLED'], |
| 215 | 'MBEDTLS_RSA_C': ['MBEDTLS_X509_RSASSA_PSS_SUPPORT', |
| 216 | 'MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED', |
| 217 | 'MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED', |
| 218 | 'MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED', |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 219 | 'MBEDTLS_KEY_EXCHANGE_RSA_ENABLED', |
| 220 | 'MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED'], |
Andrzej Kurek | 202932f | 2022-10-04 16:22:22 -0400 | [diff] [blame] | 221 | 'MBEDTLS_SHA1_C': SSL_PRE_1_2_DEPENDENCIES, |
Gilles Peskine | 584c24a | 2019-01-29 19:30:40 +0100 | [diff] [blame] | 222 | 'MBEDTLS_SHA256_C': ['MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED', |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 223 | 'MBEDTLS_ENTROPY_FORCE_SHA256', |
| 224 | 'MBEDTLS_SHA224_C', |
| 225 | 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', |
| 226 | 'MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY', |
| 227 | 'MBEDTLS_SSL_PROTO_TLS1_3'], |
| 228 | 'MBEDTLS_SHA512_C': ['MBEDTLS_SHA384_C', |
| 229 | 'MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT', |
| 230 | 'MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY'], |
| 231 | 'MBEDTLS_SHA224_C': ['MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED', |
| 232 | 'MBEDTLS_ENTROPY_FORCE_SHA256', |
| 233 | 'MBEDTLS_SHA256_C', |
| 234 | 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', |
| 235 | 'MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY'], |
| 236 | 'MBEDTLS_SHA384_C': ['MBEDTLS_SSL_PROTO_TLS1_3'], |
| 237 | 'MBEDTLS_X509_RSASSA_PSS_SUPPORT': [] |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 240 | # If an option is tested in an exclusive test, alter the following defines. |
| 241 | # These are not neccesarily dependencies, but just minimal required changes |
| 242 | # if a given define is the only one enabled from an exclusive group. |
Andrzej Kurek | 202932f | 2022-10-04 16:22:22 -0400 | [diff] [blame] | 243 | EXCLUSIVE_GROUPS = { |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 244 | 'MBEDTLS_SHA224_C': ['MBEDTLS_SHA256_C'], |
| 245 | 'MBEDTLS_SHA384_C': ['MBEDTLS_SHA512_C'], |
| 246 | 'MBEDTLS_ECP_DP_CURVE448_ENABLED': ['!MBEDTLS_ECDSA_C', |
Andrzej Kurek | 0e8b2d7 | 2022-10-04 11:14:59 -0400 | [diff] [blame] | 247 | '!MBEDTLS_ECDSA_DETERMINISTIC', |
| 248 | '!MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED', |
| 249 | '!MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED', |
| 250 | '!MBEDTLS_ECJPAKE_C', |
| 251 | '!MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED'], |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 252 | 'MBEDTLS_ECP_DP_CURVE25519_ENABLED': ['!MBEDTLS_ECDSA_C', |
Andrzej Kurek | 0e8b2d7 | 2022-10-04 11:14:59 -0400 | [diff] [blame] | 253 | '!MBEDTLS_ECDSA_DETERMINISTIC', |
| 254 | '!MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED', |
| 255 | '!MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED', |
| 256 | '!MBEDTLS_ECJPAKE_C', |
| 257 | '!MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED'], |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 258 | 'MBEDTLS_ARIA_C': ['!MBEDTLS_CMAC_C'], |
| 259 | 'MBEDTLS_CAMELLIA_C': ['!MBEDTLS_CMAC_C'], |
| 260 | 'MBEDTLS_CHACHA20_C': ['!MBEDTLS_CMAC_C', '!MBEDTLS_CCM_C', '!MBEDTLS_GCM_C'], |
| 261 | 'MBEDTLS_DES_C': ['!MBEDTLS_CCM_C', '!MBEDTLS_GCM_C'], |
| 262 | } |
| 263 | def handle_exclusive_groups(config_settings, symbol): |
| 264 | """For every symbol tested in an exclusive group check if there are other |
| 265 | defines to be altered. """ |
Andrzej Kurek | 202932f | 2022-10-04 16:22:22 -0400 | [diff] [blame] | 266 | for dep in EXCLUSIVE_GROUPS.get(symbol, []): |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 267 | unset = dep.startswith('!') |
| 268 | if unset: |
Andrzej Kurek | 0e8b2d7 | 2022-10-04 11:14:59 -0400 | [diff] [blame] | 269 | dep = dep[1:] |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 270 | config_settings[dep] = not unset |
| 271 | |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 272 | def turn_off_dependencies(config_settings): |
| 273 | """For every option turned off config_settings, also turn off what depends on it. |
| 274 | An option O is turned off if config_settings[O] is False.""" |
| 275 | for key, value in sorted(config_settings.items()): |
| 276 | if value is not False: |
| 277 | continue |
Andrzej Kurek | 202932f | 2022-10-04 16:22:22 -0400 | [diff] [blame] | 278 | for dep in REVERSE_DEPENDENCIES.get(key, []): |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 279 | config_settings[dep] = False |
| 280 | |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 281 | class ExclusiveDomain: # pylint: disable=too-few-public-methods |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 282 | """A domain consisting of a set of conceptually-equivalent settings. |
| 283 | Establish a list of configuration symbols. For each symbol, run a test job |
| 284 | with this symbol set and the others unset, and a test job with this symbol |
| 285 | unset and the others set.""" |
Gilles Peskine | b1284cf | 2019-01-29 18:56:03 +0100 | [diff] [blame] | 286 | def __init__(self, symbols, commands, exclude=None): |
| 287 | """Build a domain for the specified list of configuration symbols. |
| 288 | The domain contains two sets of jobs: jobs that enable one of the elements |
| 289 | of symbols and disable the others, and jobs that disable one of the elements |
| 290 | of symbols and enable the others. |
| 291 | Each job runs the specified commands. |
| 292 | If exclude is a regular expression, skip generated jobs whose description |
| 293 | would match this regular expression.""" |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 294 | self.jobs = [] |
| 295 | for invert in [False, True]: |
| 296 | base_config_settings = {} |
| 297 | for symbol in symbols: |
| 298 | base_config_settings[symbol] = invert |
| 299 | for symbol in symbols: |
| 300 | description = '!' + symbol if invert else symbol |
Gilles Peskine | b1284cf | 2019-01-29 18:56:03 +0100 | [diff] [blame] | 301 | if exclude and re.match(exclude, description): |
| 302 | continue |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 303 | config_settings = base_config_settings.copy() |
| 304 | config_settings[symbol] = not invert |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 305 | if not invert: |
| 306 | handle_exclusive_groups(config_settings, symbol) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 307 | turn_off_dependencies(config_settings) |
| 308 | job = Job(description, config_settings, commands) |
| 309 | self.jobs.append(job) |
| 310 | |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 311 | class ComplementaryDomain: # pylint: disable=too-few-public-methods |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 312 | """A domain consisting of a set of loosely-related settings. |
| 313 | Establish a list of configuration symbols. For each symbol, run a test job |
| 314 | with this symbol unset.""" |
| 315 | def __init__(self, symbols, commands): |
Gilles Peskine | b1284cf | 2019-01-29 18:56:03 +0100 | [diff] [blame] | 316 | """Build a domain for the specified list of configuration symbols. |
| 317 | Each job in the domain disables one of the specified symbols. |
| 318 | Each job runs the specified commands.""" |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 319 | self.jobs = [] |
| 320 | for symbol in symbols: |
| 321 | description = '!' + symbol |
| 322 | config_settings = {symbol: False} |
| 323 | turn_off_dependencies(config_settings) |
| 324 | job = Job(description, config_settings, commands) |
| 325 | self.jobs.append(job) |
| 326 | |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 327 | class CipherInfo: # pylint: disable=too-few-public-methods |
Gilles Peskine | 34a1557 | 2019-01-29 23:12:28 +0100 | [diff] [blame] | 328 | """Collect data about cipher.h.""" |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 329 | def __init__(self): |
Gilles Peskine | 34a1557 | 2019-01-29 23:12:28 +0100 | [diff] [blame] | 330 | self.base_symbols = set() |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 331 | with open('include/mbedtls/cipher.h', encoding="utf-8") as fh: |
Gilles Peskine | 34a1557 | 2019-01-29 23:12:28 +0100 | [diff] [blame] | 332 | for line in fh: |
| 333 | m = re.match(r' *MBEDTLS_CIPHER_ID_(\w+),', line) |
| 334 | if m and m.group(1) not in ['NONE', 'NULL', '3DES']: |
| 335 | self.base_symbols.add('MBEDTLS_' + m.group(1) + '_C') |
| 336 | |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 337 | class DomainData: |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 338 | """A container for domains and jobs, used to structurize testing.""" |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 339 | def config_symbols_matching(self, regexp): |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 340 | """List the mbedtls_config.h settings matching regexp.""" |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 341 | return [symbol for symbol in self.all_config_symbols |
| 342 | if re.match(regexp, symbol)] |
| 343 | |
| 344 | def __init__(self, options): |
| 345 | """Gather data about the library and establish a list of domains to test.""" |
| 346 | build_command = [options.make_command, 'CFLAGS=-Werror'] |
| 347 | build_and_test = [build_command, [options.make_command, 'test']] |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 348 | self.all_config_symbols = set(collect_config_symbols(options)) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 349 | # Find hash modules by name. |
| 350 | hash_symbols = self.config_symbols_matching(r'MBEDTLS_(MD|RIPEMD|SHA)[0-9]+_C\Z') |
| 351 | # Find elliptic curve enabling macros by name. |
| 352 | curve_symbols = self.config_symbols_matching(r'MBEDTLS_ECP_DP_\w+_ENABLED\Z') |
| 353 | # Find key exchange enabling macros by name. |
| 354 | key_exchange_symbols = self.config_symbols_matching(r'MBEDTLS_KEY_EXCHANGE_\w+_ENABLED\Z') |
Gilles Peskine | 34a1557 | 2019-01-29 23:12:28 +0100 | [diff] [blame] | 355 | # Find cipher IDs (block permutations and stream ciphers --- chaining |
| 356 | # and padding modes are exercised separately) information by parsing |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 357 | # cipher.h, as the information is not readily available in mbedtls_config.h. |
| 358 | |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 359 | cipher_info = CipherInfo() |
Gilles Peskine | 34a1557 | 2019-01-29 23:12:28 +0100 | [diff] [blame] | 360 | # Find block cipher chaining and padding mode enabling macros by name. |
| 361 | cipher_chaining_symbols = self.config_symbols_matching(r'MBEDTLS_CIPHER_MODE_\w+\Z') |
| 362 | cipher_padding_symbols = self.config_symbols_matching(r'MBEDTLS_CIPHER_PADDING_\w+\Z') |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 363 | self.domains = { |
Gilles Peskine | 34a1557 | 2019-01-29 23:12:28 +0100 | [diff] [blame] | 364 | # Cipher IDs, chaining modes and padding modes. Run the test suites. |
| 365 | 'cipher_id': ExclusiveDomain(cipher_info.base_symbols, |
| 366 | build_and_test), |
| 367 | 'cipher_chaining': ExclusiveDomain(cipher_chaining_symbols, |
| 368 | build_and_test), |
| 369 | 'cipher_padding': ExclusiveDomain(cipher_padding_symbols, |
| 370 | build_and_test), |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 371 | # Elliptic curves. Run the test suites. |
| 372 | 'curves': ExclusiveDomain(curve_symbols, build_and_test), |
| 373 | # Hash algorithms. Exclude configurations with only one |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 374 | # hash which is obsolete. Run the test suites. Exclude |
| 375 | # SHA512 and SHA256, as these are tested with SHA384 and SHA224. |
Gilles Peskine | b1284cf | 2019-01-29 18:56:03 +0100 | [diff] [blame] | 376 | 'hashes': ExclusiveDomain(hash_symbols, build_and_test, |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 377 | exclude=r'MBEDTLS_(MD|RIPEMD|SHA1_|SHA256_|SHA512_)\ |
| 378 | |!MBEDTLS_(SHA256_|SHA512_)'), |
Gilles Peskine | c3b4dee | 2019-01-29 19:33:05 +0100 | [diff] [blame] | 379 | # Key exchange types. Only build the library and the sample |
| 380 | # programs. |
| 381 | 'kex': ExclusiveDomain(key_exchange_symbols, |
| 382 | [build_command + ['lib'], |
| 383 | build_command + ['-C', 'programs']]), |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 384 | 'pkalgs': ComplementaryDomain(['MBEDTLS_ECDSA_C', |
| 385 | 'MBEDTLS_ECP_C', |
| 386 | 'MBEDTLS_PKCS1_V21', |
| 387 | 'MBEDTLS_PKCS1_V15', |
| 388 | 'MBEDTLS_RSA_C', |
| 389 | 'MBEDTLS_X509_RSASSA_PSS_SUPPORT'], |
| 390 | build_and_test), |
| 391 | } |
| 392 | self.jobs = {} |
| 393 | for domain in self.domains.values(): |
| 394 | for job in domain.jobs: |
| 395 | self.jobs[job.name] = job |
| 396 | |
| 397 | def get_jobs(self, name): |
| 398 | """Return the list of jobs identified by the given name. |
| 399 | A name can either be the name of a domain or the name of one specific job.""" |
| 400 | if name in self.domains: |
| 401 | return sorted(self.domains[name].jobs, key=lambda job: job.name) |
| 402 | else: |
| 403 | return [self.jobs[name]] |
| 404 | |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 405 | def run(options, job, colors=NO_COLORS): |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 406 | """Run the specified job (a Job instance).""" |
| 407 | subprocess.check_call([options.make_command, 'clean']) |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 408 | job.announce(colors, None) |
Gilles Peskine | 54aa5c6 | 2019-01-29 18:46:34 +0100 | [diff] [blame] | 409 | job.configure(options) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 410 | success = job.test(options) |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 411 | job.announce(colors, success) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 412 | return success |
| 413 | |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 414 | def run_tests(options, domain_data): |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 415 | """Run the desired jobs. |
| 416 | domain_data should be a DomainData instance that describes the available |
| 417 | domains and jobs. |
| 418 | Run the jobs listed in options.domains.""" |
| 419 | if not hasattr(options, 'config_backup'): |
| 420 | options.config_backup = options.config + '.bak' |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 421 | colors = Colors(options) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 422 | jobs = [] |
| 423 | failures = [] |
| 424 | successes = [] |
| 425 | for name in options.domains: |
| 426 | jobs += domain_data.get_jobs(name) |
| 427 | backup_config(options) |
| 428 | try: |
| 429 | for job in jobs: |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 430 | success = run(options, job, colors=colors) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 431 | if not success: |
| 432 | if options.keep_going: |
| 433 | failures.append(job.name) |
| 434 | else: |
| 435 | return False |
| 436 | else: |
| 437 | successes.append(job.name) |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 438 | restore_config(options) |
| 439 | except: |
| 440 | # Restore the configuration, except in stop-on-error mode if there |
| 441 | # was an error, where we leave the failing configuration up for |
| 442 | # developer convenience. |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 443 | if options.keep_going: |
Gilles Peskine | bf7537d | 2019-01-29 18:52:16 +0100 | [diff] [blame] | 444 | restore_config(options) |
| 445 | raise |
Gilles Peskine | e85163b | 2019-01-29 18:50:03 +0100 | [diff] [blame] | 446 | if successes: |
| 447 | log_line('{} passed'.format(' '.join(successes)), color=colors.bold_green) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 448 | if failures: |
Gilles Peskine | e85163b | 2019-01-29 18:50:03 +0100 | [diff] [blame] | 449 | log_line('{} FAILED'.format(' '.join(failures)), color=colors.bold_red) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 450 | return False |
| 451 | else: |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 452 | return True |
| 453 | |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 454 | def main(): |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 455 | try: |
| 456 | parser = argparse.ArgumentParser(description=__doc__) |
Gilles Peskine | 0fa7cbe | 2019-01-29 18:48:48 +0100 | [diff] [blame] | 457 | parser.add_argument('--color', metavar='WHEN', |
| 458 | help='Colorize the output (always/auto/never)', |
| 459 | choices=['always', 'auto', 'never'], default='auto') |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 460 | parser.add_argument('-c', '--config', metavar='FILE', |
| 461 | help='Configuration file to modify', |
Andrzej Kurek | e05b17f | 2022-09-28 03:17:56 -0400 | [diff] [blame] | 462 | default='include/mbedtls/mbedtls_config.h') |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 463 | parser.add_argument('-C', '--directory', metavar='DIR', |
| 464 | help='Change to this directory before anything else', |
| 465 | default='.') |
| 466 | parser.add_argument('-k', '--keep-going', |
| 467 | help='Try all configurations even if some fail (default)', |
| 468 | action='store_true', dest='keep_going', default=True) |
| 469 | parser.add_argument('-e', '--no-keep-going', |
| 470 | help='Stop as soon as a configuration fails', |
| 471 | action='store_false', dest='keep_going') |
| 472 | parser.add_argument('--list-jobs', |
| 473 | help='List supported jobs and exit', |
| 474 | action='append_const', dest='list', const='jobs') |
| 475 | parser.add_argument('--list-domains', |
| 476 | help='List supported domains and exit', |
| 477 | action='append_const', dest='list', const='domains') |
| 478 | parser.add_argument('--make-command', metavar='CMD', |
| 479 | help='Command to run instead of make (e.g. gmake)', |
| 480 | action='store', default='make') |
| 481 | parser.add_argument('domains', metavar='DOMAIN', nargs='*', |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 482 | help='The domain(s) to test (default: all). This can \ |
| 483 | be also a list of jobs to run.', |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 484 | default=True) |
| 485 | options = parser.parse_args() |
| 486 | os.chdir(options.directory) |
| 487 | domain_data = DomainData(options) |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 488 | if options.domains is True: |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 489 | options.domains = sorted(domain_data.domains.keys()) |
| 490 | if options.list: |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 491 | for arg in options.list: |
| 492 | for domain_name in sorted(getattr(domain_data, arg).keys()): |
| 493 | print(domain_name) |
| 494 | sys.exit(0) |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 495 | else: |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 496 | sys.exit(0 if run_tests(options, domain_data) else 1) |
| 497 | except Exception: # pylint: disable=broad-except |
Gilles Peskine | b39e3ec | 2019-01-29 08:50:20 +0100 | [diff] [blame] | 498 | traceback.print_exc() |
Andrzej Kurek | 3322c22 | 2022-10-04 15:02:41 -0400 | [diff] [blame] | 499 | sys.exit(3) |
| 500 | |
| 501 | if __name__ == '__main__': |
| 502 | main() |