blob: 2515b309e9c00ae3513bf0c384f0d01e0f0e089f [file] [log] [blame]
Gilles Peskine15c2cbf2020-06-25 18:36:28 +02001#!/usr/bin/env python3
2
3"""Analyze the test outcomes from a full CI run.
4
5This script can also run on outcomes from a partial run, but the results are
6less likely to be useful.
7"""
8
9import argparse
10import sys
11import traceback
Przemek Stekiel85c54ea2022-11-17 11:50:23 +010012import re
Valerio Settia2663322023-03-24 08:20:18 +010013import subprocess
14import os
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020015
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020016import check_test_cases
17
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020018class Results:
19 """Process analysis results."""
20
21 def __init__(self):
22 self.error_count = 0
23 self.warning_count = 0
24
Valerio Setti2cff8202023-10-18 14:36:47 +020025 def new_section(self, fmt, *args, **kwargs):
26 self._print_line('\n*** ' + fmt + ' ***\n', *args, **kwargs)
27
Valerio Settiaaef0bc2023-10-10 09:42:13 +020028 def info(self, fmt, *args, **kwargs):
Valerio Setti8070dbe2023-10-17 12:29:30 +020029 self._print_line('Info: ' + fmt, *args, **kwargs)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020030
31 def error(self, fmt, *args, **kwargs):
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020032 self.error_count += 1
Valerio Setti8070dbe2023-10-17 12:29:30 +020033 self._print_line('Error: ' + fmt, *args, **kwargs)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020034
35 def warning(self, fmt, *args, **kwargs):
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020036 self.warning_count += 1
Valerio Setti8070dbe2023-10-17 12:29:30 +020037 self._print_line('Warning: ' + fmt, *args, **kwargs)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020038
Valerio Setti3f339892023-10-17 10:42:11 +020039 @staticmethod
Valerio Setti8070dbe2023-10-17 12:29:30 +020040 def _print_line(fmt, *args, **kwargs):
Valerio Setti735794c2023-10-18 08:05:15 +020041 sys.stderr.write((fmt + '\n').format(*args, **kwargs))
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020042
Valerio Settif075e472023-10-17 11:03:16 +020043def execute_reference_driver_tests(results: Results, ref_component, driver_component, \
Valerio Setti781c2342023-10-17 12:47:35 +020044 outcome_file):
Valerio Setti22992a02023-03-29 11:15:28 +020045 """Run the tests specified in ref_component and driver_component. Results
46 are stored in the output_file and they will be used for the following
Valerio Settia2663322023-03-24 08:20:18 +010047 coverage analysis"""
48 # If the outcome file already exists, we assume that the user wants to
49 # perform the comparison analysis again without repeating the tests.
50 if os.path.exists(outcome_file):
Valerio Setti39d4b9d2023-10-18 14:30:03 +020051 results.info("Outcome file ({}) already exists. Tests will be skipped.", outcome_file)
Valerio Settia2663322023-03-24 08:20:18 +010052 return
53
54 shell_command = "tests/scripts/all.sh --outcome-file " + outcome_file + \
55 " " + ref_component + " " + driver_component
Valerio Setti39d4b9d2023-10-18 14:30:03 +020056 results.info("Running: {}", shell_command)
Valerio Settia2663322023-03-24 08:20:18 +010057 ret_val = subprocess.run(shell_command.split(), check=False).returncode
58
59 if ret_val != 0:
Valerio Settif075e472023-10-17 11:03:16 +020060 results.error("failed to run reference/driver components")
Valerio Settia2663322023-03-24 08:20:18 +010061
Tomás Gonzálezb401e112023-08-11 15:22:04 +010062def analyze_coverage(results, outcomes, allow_list, full_coverage):
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020063 """Check that all available test cases are executed at least once."""
Gilles Peskine686c2922022-01-07 15:58:38 +010064 available = check_test_cases.collect_available_test_cases()
Pengyu Lv31a9b782023-11-23 14:15:37 +080065 for suite_case in available:
Pengyu Lva4428582023-11-22 19:02:15 +080066 hits = 0
Pengyu Lvdd1d6a72023-11-27 17:57:31 +080067 for comp_outcomes in outcomes.values():
Pengyu Lv31a9b782023-11-23 14:15:37 +080068 if suite_case in comp_outcomes["successes"] or \
69 suite_case in comp_outcomes["failures"]:
Pengyu Lva4428582023-11-22 19:02:15 +080070 hits += 1
Pengyu Lvf28cf592023-11-28 10:56:29 +080071 break
Pengyu Lva4428582023-11-22 19:02:15 +080072
Pengyu Lv31a9b782023-11-23 14:15:37 +080073 if hits == 0 and suite_case not in allow_list:
Tomás Gonzálezb401e112023-08-11 15:22:04 +010074 if full_coverage:
Pengyu Lv31a9b782023-11-23 14:15:37 +080075 results.error('Test case not executed: {}', suite_case)
Tomás Gonzálezb401e112023-08-11 15:22:04 +010076 else:
Pengyu Lv31a9b782023-11-23 14:15:37 +080077 results.warning('Test case not executed: {}', suite_case)
78 elif hits != 0 and suite_case in allow_list:
Tomás González07bdcc22023-08-11 14:59:03 +010079 # Test Case should be removed from the allow list.
Tomás González7ebb18f2023-08-22 09:40:23 +010080 if full_coverage:
Pengyu Lv31a9b782023-11-23 14:15:37 +080081 results.error('Allow listed test case was executed: {}', suite_case)
Tomás González7ebb18f2023-08-22 09:40:23 +010082 else:
Pengyu Lv31a9b782023-11-23 14:15:37 +080083 results.warning('Allow listed test case was executed: {}', suite_case)
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020084
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +020085def name_matches_pattern(name, str_or_re):
86 """Check if name matches a pattern, that may be a string or regex.
87 - If the pattern is a string, name must be equal to match.
88 - If the pattern is a regex, name must fully match.
89 """
Manuel Pégourié-Gonnardb2695432023-10-23 09:30:40 +020090 # The CI's python is too old for re.Pattern
91 #if isinstance(str_or_re, re.Pattern):
92 if not isinstance(str_or_re, str):
Manuel Pégourié-Gonnard9d9c2342023-10-26 09:37:40 +020093 return str_or_re.fullmatch(name)
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +020094 else:
Manuel Pégourié-Gonnard9d9c2342023-10-26 09:37:40 +020095 return str_or_re == name
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +020096
Valerio Settif075e472023-10-17 11:03:16 +020097def analyze_driver_vs_reference(results: Results, outcomes,
Valerio Settiaaef0bc2023-10-10 09:42:13 +020098 component_ref, component_driver,
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +020099 ignored_suites, ignored_tests=None):
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800100 """Check that all tests passing in the reference component are also
101 passing in the corresponding driver component.
Valerio Setti3002c992023-01-18 17:28:36 +0100102 Skip:
103 - full test suites provided in ignored_suites list
104 - only some specific test inside a test suite, for which the corresponding
105 output string is provided
Przemek Stekiel4e955902022-10-21 13:42:08 +0200106 """
Pengyu Lva4428582023-11-22 19:02:15 +0800107 ref_outcomes = outcomes.get("component_" + component_ref)
108 driver_outcomes = outcomes.get("component_" + component_driver)
109
Pengyu Lv59b9efc2023-11-28 11:15:00 +0800110 if ref_outcomes is None or driver_outcomes is None:
111 results.error("required components are missing: bad outcome file?")
112 return
113
114 if not ref_outcomes['successes']:
Pengyu Lva4428582023-11-22 19:02:15 +0800115 results.error("no passing test in reference component: bad outcome file?")
116 return
117
Pengyu Lv31a9b782023-11-23 14:15:37 +0800118 for suite_case in ref_outcomes["successes"]:
119 # suite_case is like "test_suite_foo.bar;Description of test case"
120 (full_test_suite, test_string) = suite_case.split(';')
Valerio Setti00c1ccb2023-02-02 11:33:31 +0100121 test_suite = full_test_suite.split('.')[0] # retrieve main part of test suite name
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200122
123 # Immediately skip fully-ignored test suites
Manuel Pégourié-Gonnard7d381f52023-03-17 15:13:08 +0100124 if test_suite in ignored_suites or full_test_suite in ignored_suites:
Valerio Setti00c1ccb2023-02-02 11:33:31 +0100125 continue
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200126
127 # For ignored test cases inside test suites, just remember and:
128 # don't issue an error if they're skipped with drivers,
129 # but issue an error if they're not (means we have a bad entry).
130 ignored = False
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200131 if full_test_suite in ignored_tests:
Manuel Pégourié-Gonnardd36a37f2023-10-26 09:41:59 +0200132 for str_or_re in ignored_tests[test_suite]:
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200133 if name_matches_pattern(test_string, str_or_re):
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200134 ignored = True
Manuel Pégourié-Gonnard4da369f2023-10-18 09:40:32 +0200135
Pengyu Lv31a9b782023-11-23 14:15:37 +0800136 if not ignored and not suite_case in driver_outcomes['successes']:
137 results.error("PASS -> SKIP/FAIL: {}", suite_case)
138 if ignored and suite_case in driver_outcomes['successes']:
139 results.error("uselessly ignored: {}", suite_case)
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200140
Valerio Setti781c2342023-10-17 12:47:35 +0200141def analyze_outcomes(results: Results, outcomes, args):
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200142 """Run all analyses on the given outcome collection."""
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100143 analyze_coverage(results, outcomes, args['allow_list'],
144 args['full_coverage'])
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200145
146def read_outcome_file(outcome_file):
147 """Parse an outcome file and return an outcome collection.
148
Pengyu Lv31a9b782023-11-23 14:15:37 +0800149An outcome collection is a dictionary presentation of the outcome file:
150```
151outcomes = {
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800152 "<component>": {
Pengyu Lv31a9b782023-11-23 14:15:37 +0800153 "successes": frozenset(["<suite_case>", ... ]),
154 "failures": frozenset(["<suite_case>", ...])
155 }
156 ...
157}
158suite_case = "<suite>;<case>"
159```
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200160"""
161 outcomes = {}
162 with open(outcome_file, 'r', encoding='utf-8') as input_file:
163 for line in input_file:
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800164 (_platform, component, suite, case, result, _cause) = line.split(';')
Pengyu Lv31a9b782023-11-23 14:15:37 +0800165 suite_case = ';'.join([suite, case])
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800166 if component not in outcomes:
167 outcomes[component] = {"successes":[], "failures":[]}
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200168 if result == 'PASS':
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800169 outcomes[component]['successes'].append(suite_case)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200170 elif result == 'FAIL':
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800171 outcomes[component]['failures'].append(suite_case)
Pengyu Lva4428582023-11-22 19:02:15 +0800172
Pengyu Lv31a9b782023-11-23 14:15:37 +0800173 # Convert `list` to `frozenset` to improve search performance
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800174 for component in outcomes:
175 outcomes[component]['successes'] = frozenset(outcomes[component]['successes'])
176 outcomes[component]['failures'] = frozenset(outcomes[component]['failures'])
Pengyu Lva4428582023-11-22 19:02:15 +0800177
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200178 return outcomes
179
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800180def do_analyze_coverage(results: Results, outcomes_or_file, args):
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100181 """Perform coverage analysis."""
Valerio Setti2cff8202023-10-18 14:36:47 +0200182 results.new_section("Analyze coverage")
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800183 outcomes = read_outcome_file(outcomes_or_file) \
184 if isinstance(outcomes_or_file, str) else outcomes_or_file
Valerio Setti781c2342023-10-17 12:47:35 +0200185 analyze_outcomes(results, outcomes, args)
Przemek Stekiel4e955902022-10-21 13:42:08 +0200186
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800187def do_analyze_driver_vs_reference(results: Results, outcomes_or_file, args):
Przemek Stekiel4e955902022-10-21 13:42:08 +0200188 """Perform driver vs reference analyze."""
Valerio Setti2cff8202023-10-18 14:36:47 +0200189 results.new_section("Analyze driver {} vs reference {}",
190 args['component_driver'], args['component_ref'])
Valerio Settib0c618e2023-10-16 14:19:49 +0200191
Valerio Setti3002c992023-01-18 17:28:36 +0100192 ignored_suites = ['test_suite_' + x for x in args['ignored_suites']]
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100193
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800194 if isinstance(outcomes_or_file, str):
195 execute_reference_driver_tests(results, args['component_ref'], \
196 args['component_driver'], outcomes_or_file)
197 outcomes = read_outcome_file(outcomes_or_file)
198 else:
199 outcomes = outcomes_or_file
Valerio Settiaaef0bc2023-10-10 09:42:13 +0200200
Valerio Setti781c2342023-10-17 12:47:35 +0200201 analyze_driver_vs_reference(results, outcomes,
202 args['component_ref'], args['component_driver'],
203 ignored_suites, args['ignored_tests'])
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200204
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100205# List of tasks with a function that can handle this task and additional arguments if required
Valerio Settidfd7ca62023-10-09 16:30:11 +0200206KNOWN_TASKS = {
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200207 'analyze_coverage': {
208 'test_function': do_analyze_coverage,
Tomás González07bdcc22023-08-11 14:59:03 +0100209 'args': {
Tomás González358c6c62023-08-14 15:43:46 +0100210 'allow_list': [
Tomás González50223112023-08-22 09:52:06 +0100211 # Algorithm not supported yet
212 'test_suite_psa_crypto_metadata;Asymmetric signature: pure EdDSA',
213 # Algorithm not supported yet
214 'test_suite_psa_crypto_metadata;Cipher: XTS',
Tomás Gonzálezd43cab32023-08-24 09:12:40 +0100215 ],
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100216 'full_coverage': False,
Tomás González07bdcc22023-08-11 14:59:03 +0100217 }
Tomás Gonzálezd43cab32023-08-24 09:12:40 +0100218 },
Valerio Settia2663322023-03-24 08:20:18 +0100219 # There are 2 options to use analyze_driver_vs_reference_xxx locally:
220 # 1. Run tests and then analysis:
221 # - tests/scripts/all.sh --outcome-file "$PWD/out.csv" <component_ref> <component_driver>
222 # - tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
223 # 2. Let this script run both automatically:
224 # - tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200225 'analyze_driver_vs_reference_hash': {
226 'test_function': do_analyze_driver_vs_reference,
227 'args': {
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100228 'component_ref': 'test_psa_crypto_config_reference_hash_use_psa',
229 'component_driver': 'test_psa_crypto_config_accel_hash_use_psa',
Manuel Pégourié-Gonnard10e39632022-12-29 12:29:09 +0100230 'ignored_suites': [
231 'shax', 'mdx', # the software implementations that are being excluded
Manuel Pégourié-Gonnard7d381f52023-03-17 15:13:08 +0100232 'md.psa', # purposefully depends on whether drivers are present
Gilles Peskine35b49c42023-10-04 12:28:41 +0200233 'psa_crypto_low_hash.generated', # testing the builtins
Valerio Setti3002c992023-01-18 17:28:36 +0100234 ],
235 'ignored_tests': {
236 }
237 }
238 },
Valerio Settib6b301f2023-10-04 12:05:05 +0200239 'analyze_driver_vs_reference_cipher_aead': {
240 'test_function': do_analyze_driver_vs_reference,
241 'args': {
242 'component_ref': 'test_psa_crypto_config_reference_cipher_aead',
243 'component_driver': 'test_psa_crypto_config_accel_cipher_aead',
Valerio Setti507e08f2023-10-26 09:44:06 +0200244 # Modules replaced by drivers.
Valerio Settib6b301f2023-10-04 12:05:05 +0200245 'ignored_suites': [
Valerio Setti507e08f2023-10-26 09:44:06 +0200246 # low-level (block/stream) cipher modules
247 'aes', 'aria', 'camellia', 'des', 'chacha20',
248 # AEAD modes
249 'ccm', 'chachapoly', 'cmac', 'gcm',
250 # The Cipher abstraction layer
251 'cipher',
Valerio Settib6b301f2023-10-04 12:05:05 +0200252 ],
253 'ignored_tests': {
Valerio Setti507e08f2023-10-26 09:44:06 +0200254 # PEM decryption is not supported so far.
255 # The rest of PEM (write, unencrypted read) works though.
Valerio Setti7448cee2023-10-04 15:46:42 +0200256 'test_suite_pem': [
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200257 re.compile(r'PEM read .*(AES|DES|\bencrypt).*'),
Valerio Setti7448cee2023-10-04 15:46:42 +0200258 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200259 # Following tests depend on AES_C/DES_C but are not about
260 # them really, just need to know some error code is there.
Valerio Setti7448cee2023-10-04 15:46:42 +0200261 'test_suite_error': [
262 'Low and high error',
263 'Single low error'
264 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200265 # Similar to test_suite_error above.
Valerio Setti7448cee2023-10-04 15:46:42 +0200266 'test_suite_version': [
267 'Check for MBEDTLS_AES_C when already present',
Valerio Setti93941442023-10-13 09:19:52 +0200268 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200269 # The en/decryption part of PKCS#12 is not supported so far.
270 # The rest of PKCS#12 (key derivation) works though.
Valerio Setti93941442023-10-13 09:19:52 +0200271 'test_suite_pkcs12': [
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200272 re.compile(r'PBE Encrypt, .*'),
273 re.compile(r'PBE Decrypt, .*'),
Valerio Setti93941442023-10-13 09:19:52 +0200274 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200275 # The en/decryption part of PKCS#5 is not supported so far.
276 # The rest of PKCS#5 (PBKDF2) works though.
Valerio Setti93941442023-10-13 09:19:52 +0200277 'test_suite_pkcs5': [
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200278 re.compile(r'PBES2 Encrypt, .*'),
279 re.compile(r'PBES2 Decrypt .*'),
Valerio Setti93941442023-10-13 09:19:52 +0200280 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200281 # Encrypted keys are not supported so far.
Valerio Setti5cd18f92023-10-13 15:14:07 +0200282 # pylint: disable=line-too-long
Valerio Setti93941442023-10-13 09:19:52 +0200283 'test_suite_pkparse': [
284 'Key ASN1 (Encrypted key PKCS12, trailing garbage data)',
285 'Key ASN1 (Encrypted key PKCS5, trailing garbage data)',
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200286 re.compile(r'Parse RSA Key .*\(PKCS#8 encrypted .*\)'),
Valerio Setti93941442023-10-13 09:19:52 +0200287 ],
Valerio Settib6b301f2023-10-04 12:05:05 +0200288 }
289 }
290 },
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200291 'analyze_driver_vs_reference_ecp_light_only': {
Valerio Setti42d5f192023-03-20 13:54:41 +0100292 'test_function': do_analyze_driver_vs_reference,
293 'args': {
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200294 'component_ref': 'test_psa_crypto_config_reference_ecc_ecp_light_only',
295 'component_driver': 'test_psa_crypto_config_accel_ecc_ecp_light_only',
Valerio Setti42d5f192023-03-20 13:54:41 +0100296 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200297 # Modules replaced by drivers
298 'ecdsa', 'ecdh', 'ecjpake',
Valerio Setti42d5f192023-03-20 13:54:41 +0100299 ],
300 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200301 # This test wants a legacy function that takes f_rng, p_rng
302 # arguments, and uses legacy ECDSA for that. The test is
303 # really about the wrapper around the PSA RNG, not ECDSA.
Valerio Setti42d5f192023-03-20 13:54:41 +0100304 'test_suite_random': [
305 'PSA classic wrapper: ECDSA signature (SECP256R1)',
306 ],
Valerio Setti0c477d32023-04-07 15:54:20 +0200307 # In the accelerated test ECP_C is not set (only ECP_LIGHT is)
308 # so we must ignore disparities in the tests for which ECP_C
309 # is required.
310 'test_suite_ecp': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200311 re.compile(r'ECP check public-private .*'),
312 re.compile(r'ECP gen keypair .*'),
313 re.compile(r'ECP point muladd .*'),
314 re.compile(r'ECP point multiplication .*'),
315 re.compile(r'ECP test vectors .*'),
Valerio Setti482a0b92023-08-18 15:55:10 +0200316 ],
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200317 'test_suite_ssl': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200318 # This deprecated function is only present when ECP_C is On.
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200319 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
320 ],
Valerio Setti5f540202023-06-30 17:20:49 +0200321 }
Valerio Setti42d5f192023-03-20 13:54:41 +0100322 }
323 },
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200324 'analyze_driver_vs_reference_no_ecp_at_all': {
Valerio Settie618cb02023-04-12 14:59:16 +0200325 'test_function': do_analyze_driver_vs_reference,
326 'args': {
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200327 'component_ref': 'test_psa_crypto_config_reference_ecc_no_ecp_at_all',
328 'component_driver': 'test_psa_crypto_config_accel_ecc_no_ecp_at_all',
Valerio Settie618cb02023-04-12 14:59:16 +0200329 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200330 # Modules replaced by drivers
331 'ecp', 'ecdsa', 'ecdh', 'ecjpake',
Valerio Settie618cb02023-04-12 14:59:16 +0200332 ],
333 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200334 # See ecp_light_only
Valerio Settie618cb02023-04-12 14:59:16 +0200335 'test_suite_random': [
336 'PSA classic wrapper: ECDSA signature (SECP256R1)',
337 ],
Valerio Settiaddeee42023-06-14 10:46:55 +0200338 'test_suite_pkparse': [
Valerio Setti5bd25232023-06-19 19:32:14 +0200339 # When PK_PARSE_C and ECP_C are defined then PK_PARSE_EC_COMPRESSED
340 # is automatically enabled in build_info.h (backward compatibility)
341 # even if it is disabled in config_psa_crypto_no_ecp_at_all(). As a
342 # consequence compressed points are supported in the reference
343 # component but not in the accelerated one, so they should be skipped
344 # while checking driver's coverage.
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200345 re.compile(r'Parse EC Key .*compressed\)'),
346 re.compile(r'Parse Public EC Key .*compressed\)'),
Valerio Settiaddeee42023-06-14 10:46:55 +0200347 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200348 # See ecp_light_only
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200349 'test_suite_ssl': [
350 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
351 ],
Valerio Settie618cb02023-04-12 14:59:16 +0200352 }
353 }
354 },
Valerio Setti307810b2023-08-15 10:12:25 +0200355 'analyze_driver_vs_reference_ecc_no_bignum': {
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200356 'test_function': do_analyze_driver_vs_reference,
357 'args': {
358 'component_ref': 'test_psa_crypto_config_reference_ecc_no_bignum',
359 'component_driver': 'test_psa_crypto_config_accel_ecc_no_bignum',
360 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200361 # Modules replaced by drivers
362 'ecp', 'ecdsa', 'ecdh', 'ecjpake',
363 'bignum_core', 'bignum_random', 'bignum_mod', 'bignum_mod_raw',
364 'bignum.generated', 'bignum.misc',
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200365 ],
366 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200367 # See ecp_light_only
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200368 'test_suite_random': [
369 'PSA classic wrapper: ECDSA signature (SECP256R1)',
370 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200371 # See no_ecp_at_all
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200372 'test_suite_pkparse': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200373 re.compile(r'Parse EC Key .*compressed\)'),
374 re.compile(r'Parse Public EC Key .*compressed\)'),
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200375 ],
Valerio Setti9b3dbcc2023-07-26 18:00:31 +0200376 'test_suite_asn1parse': [
Valerio Setti9b3dbcc2023-07-26 18:00:31 +0200377 'INTEGER too large for mpi',
378 ],
379 'test_suite_asn1write': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200380 re.compile(r'ASN.1 Write mpi.*'),
Valerio Setti9b3dbcc2023-07-26 18:00:31 +0200381 ],
Valerio Settie0be95e2023-08-01 09:07:43 +0200382 'test_suite_debug': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200383 re.compile(r'Debug print mbedtls_mpi.*'),
Valerio Settie0be95e2023-08-01 09:07:43 +0200384 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200385 # See ecp_light_only
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200386 'test_suite_ssl': [
387 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
388 ],
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200389 }
390 }
391 },
Valerio Setti307810b2023-08-15 10:12:25 +0200392 'analyze_driver_vs_reference_ecc_ffdh_no_bignum': {
393 'test_function': do_analyze_driver_vs_reference,
394 'args': {
395 'component_ref': 'test_psa_crypto_config_reference_ecc_ffdh_no_bignum',
396 'component_driver': 'test_psa_crypto_config_accel_ecc_ffdh_no_bignum',
397 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200398 # Modules replaced by drivers
399 'ecp', 'ecdsa', 'ecdh', 'ecjpake', 'dhm',
400 'bignum_core', 'bignum_random', 'bignum_mod', 'bignum_mod_raw',
401 'bignum.generated', 'bignum.misc',
Valerio Setti307810b2023-08-15 10:12:25 +0200402 ],
403 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200404 # See ecp_light_only
Valerio Setti307810b2023-08-15 10:12:25 +0200405 'test_suite_random': [
406 'PSA classic wrapper: ECDSA signature (SECP256R1)',
407 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200408 # See no_ecp_at_all
Valerio Setti307810b2023-08-15 10:12:25 +0200409 'test_suite_pkparse': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200410 re.compile(r'Parse EC Key .*compressed\)'),
411 re.compile(r'Parse Public EC Key .*compressed\)'),
Valerio Setti307810b2023-08-15 10:12:25 +0200412 ],
413 'test_suite_asn1parse': [
Valerio Setti307810b2023-08-15 10:12:25 +0200414 'INTEGER too large for mpi',
415 ],
416 'test_suite_asn1write': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200417 re.compile(r'ASN.1 Write mpi.*'),
Valerio Setti307810b2023-08-15 10:12:25 +0200418 ],
419 'test_suite_debug': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200420 re.compile(r'Debug print mbedtls_mpi.*'),
Valerio Setti307810b2023-08-15 10:12:25 +0200421 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200422 # See ecp_light_only
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200423 'test_suite_ssl': [
424 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
425 ],
Valerio Setti307810b2023-08-15 10:12:25 +0200426 }
427 }
428 },
Przemek Stekiel85b64422023-05-26 09:55:23 +0200429 'analyze_driver_vs_reference_ffdh_alg': {
430 'test_function': do_analyze_driver_vs_reference,
431 'args': {
432 'component_ref': 'test_psa_crypto_config_reference_ffdh',
433 'component_driver': 'test_psa_crypto_config_accel_ffdh',
Przemek Stekiel84f4ff12023-07-04 12:35:31 +0200434 'ignored_suites': ['dhm'],
Przemek Stekiel565353e2023-07-05 11:07:07 +0200435 'ignored_tests': {}
Przemek Stekiel85b64422023-05-26 09:55:23 +0200436 }
437 },
Valerio Settif01d6482023-08-04 13:51:18 +0200438 'analyze_driver_vs_reference_tfm_config': {
439 'test_function': do_analyze_driver_vs_reference,
440 'args': {
441 'component_ref': 'test_tfm_config',
442 'component_driver': 'test_tfm_config_p256m_driver_accel_ec',
Manuel Pégourié-Gonnarde9d97972023-08-08 18:34:47 +0200443 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200444 # Modules replaced by drivers
Yanray Wang57790962023-10-31 13:39:07 +0800445 'asn1parse', 'asn1write',
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200446 'ecp', 'ecdsa', 'ecdh', 'ecjpake',
447 'bignum_core', 'bignum_random', 'bignum_mod', 'bignum_mod_raw',
448 'bignum.generated', 'bignum.misc',
Manuel Pégourié-Gonnarde9d97972023-08-08 18:34:47 +0200449 ],
Valerio Settif01d6482023-08-04 13:51:18 +0200450 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200451 # See ecp_light_only
Manuel Pégourié-Gonnarde9d97972023-08-08 18:34:47 +0200452 'test_suite_random': [
453 'PSA classic wrapper: ECDSA signature (SECP256R1)',
454 ],
Valerio Settif01d6482023-08-04 13:51:18 +0200455 }
456 }
457 }
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200458}
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200459
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200460def main():
Valerio Settif075e472023-10-17 11:03:16 +0200461 main_results = Results()
Valerio Settiaaef0bc2023-10-10 09:42:13 +0200462
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200463 try:
464 parser = argparse.ArgumentParser(description=__doc__)
Przemek Stekiel58bbc232022-10-24 08:10:10 +0200465 parser.add_argument('outcomes', metavar='OUTCOMES.CSV',
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200466 help='Outcome file to analyze')
Valerio Settidfd7ca62023-10-09 16:30:11 +0200467 parser.add_argument('specified_tasks', default='all', nargs='?',
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100468 help='Analysis to be done. By default, run all tasks. '
469 'With one or more TASK, run only those. '
470 'TASK can be the name of a single task or '
Przemek Stekiel85c54ea2022-11-17 11:50:23 +0100471 'comma/space-separated list of tasks. ')
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100472 parser.add_argument('--list', action='store_true',
473 help='List all available tasks and exit.')
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100474 parser.add_argument('--require-full-coverage', action='store_true',
475 dest='full_coverage', help="Require all available "
476 "test cases to be executed and issue an error "
477 "otherwise. This flag is ignored if 'task' is "
478 "neither 'all' nor 'analyze_coverage'")
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200479 options = parser.parse_args()
Przemek Stekiel4e955902022-10-21 13:42:08 +0200480
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100481 if options.list:
Valerio Settidfd7ca62023-10-09 16:30:11 +0200482 for task in KNOWN_TASKS:
Valerio Setti5329ff02023-10-17 09:44:36 +0200483 print(task)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100484 sys.exit(0)
485
Valerio Settidfd7ca62023-10-09 16:30:11 +0200486 if options.specified_tasks == 'all':
487 tasks_list = KNOWN_TASKS.keys()
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100488 else:
Valerio Settidfd7ca62023-10-09 16:30:11 +0200489 tasks_list = re.split(r'[, ]+', options.specified_tasks)
Valerio Settidfd7ca62023-10-09 16:30:11 +0200490 for task in tasks_list:
491 if task not in KNOWN_TASKS:
Manuel Pégourié-Gonnard62d61312023-10-20 10:51:57 +0200492 sys.stderr.write('invalid task: {}\n'.format(task))
Valerio Settifb2750e2023-10-17 10:11:45 +0200493 sys.exit(2)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100494
Valerio Settidfd7ca62023-10-09 16:30:11 +0200495 KNOWN_TASKS['analyze_coverage']['args']['full_coverage'] = options.full_coverage
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100496
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800497 # If the outcome file exists, parse it once and share the result
498 # among tasks to improve performance.
499 # Otherwise, it will be generated by do_analyze_driver_vs_reference.
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800500 if os.path.exists(options.outcomes):
501 main_results.info("Read outcome file from {}.", options.outcomes)
502 outcomes_or_file = read_outcome_file(options.outcomes)
503 else:
504 outcomes_or_file = options.outcomes
505
Valerio Settifb2750e2023-10-17 10:11:45 +0200506 for task in tasks_list:
507 test_function = KNOWN_TASKS[task]['test_function']
508 test_args = KNOWN_TASKS[task]['args']
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800509 test_function(main_results, outcomes_or_file, test_args)
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100510
Valerio Settif6f64cf2023-10-17 12:28:26 +0200511 main_results.info("Overall results: {} warnings and {} errors",
512 main_results.warning_count, main_results.error_count)
Przemek Stekiel4e955902022-10-21 13:42:08 +0200513
Valerio Setti8d178be2023-10-17 12:23:55 +0200514 sys.exit(0 if (main_results.error_count == 0) else 1)
Valerio Settiaaef0bc2023-10-10 09:42:13 +0200515
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200516 except Exception: # pylint: disable=broad-except
517 # Print the backtrace and exit explicitly with our chosen status.
518 traceback.print_exc()
519 sys.exit(120)
520
521if __name__ == '__main__':
522 main()