blob: b52952458b5716571d5621dbaa60f486f818bed3 [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
Pengyu Lv18908ec2023-11-28 12:11:52 +080015import typing
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020016
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020017import check_test_cases
18
Pengyu Lv18908ec2023-11-28 12:11:52 +080019ComponentOutcomes = typing.NamedTuple('ComponentOutcomes',
20 [('successes', typing.Set[str]),
21 ('failures', typing.Set[str])])
22
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020023class Results:
24 """Process analysis results."""
25
26 def __init__(self):
27 self.error_count = 0
28 self.warning_count = 0
29
Valerio Setti2cff8202023-10-18 14:36:47 +020030 def new_section(self, fmt, *args, **kwargs):
31 self._print_line('\n*** ' + fmt + ' ***\n', *args, **kwargs)
32
Valerio Settiaaef0bc2023-10-10 09:42:13 +020033 def info(self, fmt, *args, **kwargs):
Valerio Setti8070dbe2023-10-17 12:29:30 +020034 self._print_line('Info: ' + fmt, *args, **kwargs)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020035
36 def error(self, fmt, *args, **kwargs):
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020037 self.error_count += 1
Valerio Setti8070dbe2023-10-17 12:29:30 +020038 self._print_line('Error: ' + fmt, *args, **kwargs)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020039
40 def warning(self, fmt, *args, **kwargs):
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020041 self.warning_count += 1
Valerio Setti8070dbe2023-10-17 12:29:30 +020042 self._print_line('Warning: ' + fmt, *args, **kwargs)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020043
Valerio Setti3f339892023-10-17 10:42:11 +020044 @staticmethod
Valerio Setti8070dbe2023-10-17 12:29:30 +020045 def _print_line(fmt, *args, **kwargs):
Valerio Setti735794c2023-10-18 08:05:15 +020046 sys.stderr.write((fmt + '\n').format(*args, **kwargs))
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020047
Valerio Settif075e472023-10-17 11:03:16 +020048def execute_reference_driver_tests(results: Results, ref_component, driver_component, \
Valerio Setti781c2342023-10-17 12:47:35 +020049 outcome_file):
Valerio Setti22992a02023-03-29 11:15:28 +020050 """Run the tests specified in ref_component and driver_component. Results
51 are stored in the output_file and they will be used for the following
Valerio Settia2663322023-03-24 08:20:18 +010052 coverage analysis"""
53 # If the outcome file already exists, we assume that the user wants to
54 # perform the comparison analysis again without repeating the tests.
55 if os.path.exists(outcome_file):
Valerio Setti39d4b9d2023-10-18 14:30:03 +020056 results.info("Outcome file ({}) already exists. Tests will be skipped.", outcome_file)
Valerio Settia2663322023-03-24 08:20:18 +010057 return
58
59 shell_command = "tests/scripts/all.sh --outcome-file " + outcome_file + \
60 " " + ref_component + " " + driver_component
Valerio Setti39d4b9d2023-10-18 14:30:03 +020061 results.info("Running: {}", shell_command)
Valerio Settia2663322023-03-24 08:20:18 +010062 ret_val = subprocess.run(shell_command.split(), check=False).returncode
63
64 if ret_val != 0:
Valerio Settif075e472023-10-17 11:03:16 +020065 results.error("failed to run reference/driver components")
Valerio Settia2663322023-03-24 08:20:18 +010066
Tomás Gonzálezb401e112023-08-11 15:22:04 +010067def analyze_coverage(results, outcomes, allow_list, full_coverage):
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020068 """Check that all available test cases are executed at least once."""
Gilles Peskine686c2922022-01-07 15:58:38 +010069 available = check_test_cases.collect_available_test_cases()
Pengyu Lv31a9b782023-11-23 14:15:37 +080070 for suite_case in available:
Pengyu Lva4428582023-11-22 19:02:15 +080071 hits = 0
Pengyu Lvdd1d6a72023-11-27 17:57:31 +080072 for comp_outcomes in outcomes.values():
Pengyu Lv18908ec2023-11-28 12:11:52 +080073 if suite_case in comp_outcomes.successes or \
74 suite_case in comp_outcomes.failures:
Pengyu Lva4428582023-11-22 19:02:15 +080075 hits += 1
Pengyu Lvf28cf592023-11-28 10:56:29 +080076 break
Pengyu Lva4428582023-11-22 19:02:15 +080077
Pengyu Lv31a9b782023-11-23 14:15:37 +080078 if hits == 0 and suite_case not in allow_list:
Tomás Gonzálezb401e112023-08-11 15:22:04 +010079 if full_coverage:
Pengyu Lv31a9b782023-11-23 14:15:37 +080080 results.error('Test case not executed: {}', suite_case)
Tomás Gonzálezb401e112023-08-11 15:22:04 +010081 else:
Pengyu Lv31a9b782023-11-23 14:15:37 +080082 results.warning('Test case not executed: {}', suite_case)
83 elif hits != 0 and suite_case in allow_list:
Tomás González07bdcc22023-08-11 14:59:03 +010084 # Test Case should be removed from the allow list.
Tomás González7ebb18f2023-08-22 09:40:23 +010085 if full_coverage:
Pengyu Lv31a9b782023-11-23 14:15:37 +080086 results.error('Allow listed test case was executed: {}', suite_case)
Tomás González7ebb18f2023-08-22 09:40:23 +010087 else:
Pengyu Lv31a9b782023-11-23 14:15:37 +080088 results.warning('Allow listed test case was executed: {}', suite_case)
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020089
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +020090def name_matches_pattern(name, str_or_re):
91 """Check if name matches a pattern, that may be a string or regex.
92 - If the pattern is a string, name must be equal to match.
93 - If the pattern is a regex, name must fully match.
94 """
Manuel Pégourié-Gonnardb2695432023-10-23 09:30:40 +020095 # The CI's python is too old for re.Pattern
96 #if isinstance(str_or_re, re.Pattern):
97 if not isinstance(str_or_re, str):
Manuel Pégourié-Gonnard9d9c2342023-10-26 09:37:40 +020098 return str_or_re.fullmatch(name)
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +020099 else:
Manuel Pégourié-Gonnard9d9c2342023-10-26 09:37:40 +0200100 return str_or_re == name
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200101
Valerio Settif075e472023-10-17 11:03:16 +0200102def analyze_driver_vs_reference(results: Results, outcomes,
Valerio Settiaaef0bc2023-10-10 09:42:13 +0200103 component_ref, component_driver,
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200104 ignored_suites, ignored_tests=None):
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800105 """Check that all tests passing in the reference component are also
106 passing in the corresponding driver component.
Valerio Setti3002c992023-01-18 17:28:36 +0100107 Skip:
108 - full test suites provided in ignored_suites list
109 - only some specific test inside a test suite, for which the corresponding
110 output string is provided
Przemek Stekiel4e955902022-10-21 13:42:08 +0200111 """
Pengyu Lva4428582023-11-22 19:02:15 +0800112 ref_outcomes = outcomes.get("component_" + component_ref)
113 driver_outcomes = outcomes.get("component_" + component_driver)
114
Pengyu Lv59b9efc2023-11-28 11:15:00 +0800115 if ref_outcomes is None or driver_outcomes is None:
116 results.error("required components are missing: bad outcome file?")
117 return
118
Pengyu Lv18908ec2023-11-28 12:11:52 +0800119 if not ref_outcomes.successes:
Pengyu Lva4428582023-11-22 19:02:15 +0800120 results.error("no passing test in reference component: bad outcome file?")
121 return
122
Pengyu Lv18908ec2023-11-28 12:11:52 +0800123 for suite_case in ref_outcomes.successes:
Pengyu Lv31a9b782023-11-23 14:15:37 +0800124 # suite_case is like "test_suite_foo.bar;Description of test case"
125 (full_test_suite, test_string) = suite_case.split(';')
Valerio Setti00c1ccb2023-02-02 11:33:31 +0100126 test_suite = full_test_suite.split('.')[0] # retrieve main part of test suite name
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200127
128 # Immediately skip fully-ignored test suites
Manuel Pégourié-Gonnard7d381f52023-03-17 15:13:08 +0100129 if test_suite in ignored_suites or full_test_suite in ignored_suites:
Valerio Setti00c1ccb2023-02-02 11:33:31 +0100130 continue
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200131
132 # For ignored test cases inside test suites, just remember and:
133 # don't issue an error if they're skipped with drivers,
134 # but issue an error if they're not (means we have a bad entry).
135 ignored = False
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200136 if full_test_suite in ignored_tests:
Manuel Pégourié-Gonnardd36a37f2023-10-26 09:41:59 +0200137 for str_or_re in ignored_tests[test_suite]:
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200138 if name_matches_pattern(test_string, str_or_re):
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200139 ignored = True
Manuel Pégourié-Gonnard4da369f2023-10-18 09:40:32 +0200140
Pengyu Lv18908ec2023-11-28 12:11:52 +0800141 if not ignored and not suite_case in driver_outcomes.successes:
Pengyu Lv31a9b782023-11-23 14:15:37 +0800142 results.error("PASS -> SKIP/FAIL: {}", suite_case)
Pengyu Lv18908ec2023-11-28 12:11:52 +0800143 if ignored and suite_case in driver_outcomes.successes:
Pengyu Lv31a9b782023-11-23 14:15:37 +0800144 results.error("uselessly ignored: {}", suite_case)
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200145
Valerio Setti781c2342023-10-17 12:47:35 +0200146def analyze_outcomes(results: Results, outcomes, args):
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200147 """Run all analyses on the given outcome collection."""
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100148 analyze_coverage(results, outcomes, args['allow_list'],
149 args['full_coverage'])
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200150
151def read_outcome_file(outcome_file):
152 """Parse an outcome file and return an outcome collection.
153
Pengyu Lv31a9b782023-11-23 14:15:37 +0800154An outcome collection is a dictionary presentation of the outcome file:
155```
156outcomes = {
Pengyu Lv18908ec2023-11-28 12:11:52 +0800157 "<component>": ComponentOutcomes,
Pengyu Lv31a9b782023-11-23 14:15:37 +0800158 ...
159}
Pengyu Lv18908ec2023-11-28 12:11:52 +0800160
161CompoentOutcomes is a named tuple which is defined as:
162
163ComponentOutcomes(
164 successes = {
165 <suite_case>,
166 ...
167 },
168 failures = {
169 <suite_case>,
170 ...
171 }
172)
173
Pengyu Lv31a9b782023-11-23 14:15:37 +0800174suite_case = "<suite>;<case>"
175```
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200176"""
177 outcomes = {}
178 with open(outcome_file, 'r', encoding='utf-8') as input_file:
179 for line in input_file:
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800180 (_platform, component, suite, case, result, _cause) = line.split(';')
Pengyu Lv31a9b782023-11-23 14:15:37 +0800181 suite_case = ';'.join([suite, case])
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800182 if component not in outcomes:
Pengyu Lv18908ec2023-11-28 12:11:52 +0800183 outcomes[component] = ComponentOutcomes(set(), set())
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200184 if result == 'PASS':
Pengyu Lv18908ec2023-11-28 12:11:52 +0800185 outcomes[component].successes.add(suite_case)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200186 elif result == 'FAIL':
Pengyu Lv18908ec2023-11-28 12:11:52 +0800187 outcomes[component].failures.add(suite_case)
Pengyu Lva4428582023-11-22 19:02:15 +0800188
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200189 return outcomes
190
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800191def do_analyze_coverage(results: Results, outcomes_or_file, args):
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100192 """Perform coverage analysis."""
Valerio Setti2cff8202023-10-18 14:36:47 +0200193 results.new_section("Analyze coverage")
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800194 outcomes = read_outcome_file(outcomes_or_file) \
195 if isinstance(outcomes_or_file, str) else outcomes_or_file
Valerio Setti781c2342023-10-17 12:47:35 +0200196 analyze_outcomes(results, outcomes, args)
Przemek Stekiel4e955902022-10-21 13:42:08 +0200197
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800198def do_analyze_driver_vs_reference(results: Results, outcomes_or_file, args):
Przemek Stekiel4e955902022-10-21 13:42:08 +0200199 """Perform driver vs reference analyze."""
Valerio Setti2cff8202023-10-18 14:36:47 +0200200 results.new_section("Analyze driver {} vs reference {}",
201 args['component_driver'], args['component_ref'])
Valerio Settib0c618e2023-10-16 14:19:49 +0200202
Valerio Setti3002c992023-01-18 17:28:36 +0100203 ignored_suites = ['test_suite_' + x for x in args['ignored_suites']]
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100204
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800205 if isinstance(outcomes_or_file, str):
206 execute_reference_driver_tests(results, args['component_ref'], \
207 args['component_driver'], outcomes_or_file)
208 outcomes = read_outcome_file(outcomes_or_file)
209 else:
210 outcomes = outcomes_or_file
Valerio Settiaaef0bc2023-10-10 09:42:13 +0200211
Valerio Setti781c2342023-10-17 12:47:35 +0200212 analyze_driver_vs_reference(results, outcomes,
213 args['component_ref'], args['component_driver'],
214 ignored_suites, args['ignored_tests'])
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200215
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100216# List of tasks with a function that can handle this task and additional arguments if required
Valerio Settidfd7ca62023-10-09 16:30:11 +0200217KNOWN_TASKS = {
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200218 'analyze_coverage': {
219 'test_function': do_analyze_coverage,
Tomás González07bdcc22023-08-11 14:59:03 +0100220 'args': {
Tomás González358c6c62023-08-14 15:43:46 +0100221 'allow_list': [
Tomás González50223112023-08-22 09:52:06 +0100222 # Algorithm not supported yet
223 'test_suite_psa_crypto_metadata;Asymmetric signature: pure EdDSA',
224 # Algorithm not supported yet
225 'test_suite_psa_crypto_metadata;Cipher: XTS',
Tomás Gonzálezd43cab32023-08-24 09:12:40 +0100226 ],
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100227 'full_coverage': False,
Tomás González07bdcc22023-08-11 14:59:03 +0100228 }
Tomás Gonzálezd43cab32023-08-24 09:12:40 +0100229 },
Valerio Settia2663322023-03-24 08:20:18 +0100230 # There are 2 options to use analyze_driver_vs_reference_xxx locally:
231 # 1. Run tests and then analysis:
232 # - tests/scripts/all.sh --outcome-file "$PWD/out.csv" <component_ref> <component_driver>
233 # - tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
234 # 2. Let this script run both automatically:
235 # - tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200236 'analyze_driver_vs_reference_hash': {
237 'test_function': do_analyze_driver_vs_reference,
238 'args': {
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100239 'component_ref': 'test_psa_crypto_config_reference_hash_use_psa',
240 'component_driver': 'test_psa_crypto_config_accel_hash_use_psa',
Manuel Pégourié-Gonnard10e39632022-12-29 12:29:09 +0100241 'ignored_suites': [
242 'shax', 'mdx', # the software implementations that are being excluded
Manuel Pégourié-Gonnard7d381f52023-03-17 15:13:08 +0100243 'md.psa', # purposefully depends on whether drivers are present
Gilles Peskine35b49c42023-10-04 12:28:41 +0200244 'psa_crypto_low_hash.generated', # testing the builtins
Valerio Setti3002c992023-01-18 17:28:36 +0100245 ],
246 'ignored_tests': {
247 }
248 }
249 },
Valerio Settib6b301f2023-10-04 12:05:05 +0200250 'analyze_driver_vs_reference_cipher_aead': {
251 'test_function': do_analyze_driver_vs_reference,
252 'args': {
253 'component_ref': 'test_psa_crypto_config_reference_cipher_aead',
254 'component_driver': 'test_psa_crypto_config_accel_cipher_aead',
Valerio Setti507e08f2023-10-26 09:44:06 +0200255 # Modules replaced by drivers.
Valerio Settib6b301f2023-10-04 12:05:05 +0200256 'ignored_suites': [
Valerio Setti507e08f2023-10-26 09:44:06 +0200257 # low-level (block/stream) cipher modules
258 'aes', 'aria', 'camellia', 'des', 'chacha20',
259 # AEAD modes
260 'ccm', 'chachapoly', 'cmac', 'gcm',
261 # The Cipher abstraction layer
262 'cipher',
Valerio Settib6b301f2023-10-04 12:05:05 +0200263 ],
264 'ignored_tests': {
Valerio Setti507e08f2023-10-26 09:44:06 +0200265 # PEM decryption is not supported so far.
266 # The rest of PEM (write, unencrypted read) works though.
Valerio Setti7448cee2023-10-04 15:46:42 +0200267 'test_suite_pem': [
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200268 re.compile(r'PEM read .*(AES|DES|\bencrypt).*'),
Valerio Setti7448cee2023-10-04 15:46:42 +0200269 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200270 # Following tests depend on AES_C/DES_C but are not about
271 # them really, just need to know some error code is there.
Valerio Setti7448cee2023-10-04 15:46:42 +0200272 'test_suite_error': [
273 'Low and high error',
274 'Single low error'
275 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200276 # Similar to test_suite_error above.
Valerio Setti7448cee2023-10-04 15:46:42 +0200277 'test_suite_version': [
278 'Check for MBEDTLS_AES_C when already present',
Valerio Setti93941442023-10-13 09:19:52 +0200279 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200280 # The en/decryption part of PKCS#12 is not supported so far.
281 # The rest of PKCS#12 (key derivation) works though.
Valerio Setti93941442023-10-13 09:19:52 +0200282 'test_suite_pkcs12': [
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200283 re.compile(r'PBE Encrypt, .*'),
284 re.compile(r'PBE Decrypt, .*'),
Valerio Setti93941442023-10-13 09:19:52 +0200285 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200286 # The en/decryption part of PKCS#5 is not supported so far.
287 # The rest of PKCS#5 (PBKDF2) works though.
Valerio Setti93941442023-10-13 09:19:52 +0200288 'test_suite_pkcs5': [
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200289 re.compile(r'PBES2 Encrypt, .*'),
290 re.compile(r'PBES2 Decrypt .*'),
Valerio Setti93941442023-10-13 09:19:52 +0200291 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200292 # Encrypted keys are not supported so far.
Valerio Setti5cd18f92023-10-13 15:14:07 +0200293 # pylint: disable=line-too-long
Valerio Setti93941442023-10-13 09:19:52 +0200294 'test_suite_pkparse': [
295 'Key ASN1 (Encrypted key PKCS12, trailing garbage data)',
296 'Key ASN1 (Encrypted key PKCS5, trailing garbage data)',
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200297 re.compile(r'Parse RSA Key .*\(PKCS#8 encrypted .*\)'),
Valerio Setti93941442023-10-13 09:19:52 +0200298 ],
Valerio Settib6b301f2023-10-04 12:05:05 +0200299 }
300 }
301 },
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200302 'analyze_driver_vs_reference_ecp_light_only': {
Valerio Setti42d5f192023-03-20 13:54:41 +0100303 'test_function': do_analyze_driver_vs_reference,
304 'args': {
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200305 'component_ref': 'test_psa_crypto_config_reference_ecc_ecp_light_only',
306 'component_driver': 'test_psa_crypto_config_accel_ecc_ecp_light_only',
Valerio Setti42d5f192023-03-20 13:54:41 +0100307 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200308 # Modules replaced by drivers
309 'ecdsa', 'ecdh', 'ecjpake',
Valerio Setti42d5f192023-03-20 13:54:41 +0100310 ],
311 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200312 # This test wants a legacy function that takes f_rng, p_rng
313 # arguments, and uses legacy ECDSA for that. The test is
314 # really about the wrapper around the PSA RNG, not ECDSA.
Valerio Setti42d5f192023-03-20 13:54:41 +0100315 'test_suite_random': [
316 'PSA classic wrapper: ECDSA signature (SECP256R1)',
317 ],
Valerio Setti0c477d32023-04-07 15:54:20 +0200318 # In the accelerated test ECP_C is not set (only ECP_LIGHT is)
319 # so we must ignore disparities in the tests for which ECP_C
320 # is required.
321 'test_suite_ecp': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200322 re.compile(r'ECP check public-private .*'),
323 re.compile(r'ECP gen keypair .*'),
324 re.compile(r'ECP point muladd .*'),
325 re.compile(r'ECP point multiplication .*'),
326 re.compile(r'ECP test vectors .*'),
Valerio Setti482a0b92023-08-18 15:55:10 +0200327 ],
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200328 'test_suite_ssl': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200329 # This deprecated function is only present when ECP_C is On.
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200330 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
331 ],
Valerio Setti5f540202023-06-30 17:20:49 +0200332 }
Valerio Setti42d5f192023-03-20 13:54:41 +0100333 }
334 },
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200335 'analyze_driver_vs_reference_no_ecp_at_all': {
Valerio Settie618cb02023-04-12 14:59:16 +0200336 'test_function': do_analyze_driver_vs_reference,
337 'args': {
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200338 'component_ref': 'test_psa_crypto_config_reference_ecc_no_ecp_at_all',
339 'component_driver': 'test_psa_crypto_config_accel_ecc_no_ecp_at_all',
Valerio Settie618cb02023-04-12 14:59:16 +0200340 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200341 # Modules replaced by drivers
342 'ecp', 'ecdsa', 'ecdh', 'ecjpake',
Valerio Settie618cb02023-04-12 14:59:16 +0200343 ],
344 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200345 # See ecp_light_only
Valerio Settie618cb02023-04-12 14:59:16 +0200346 'test_suite_random': [
347 'PSA classic wrapper: ECDSA signature (SECP256R1)',
348 ],
Valerio Settiaddeee42023-06-14 10:46:55 +0200349 'test_suite_pkparse': [
Valerio Setti5bd25232023-06-19 19:32:14 +0200350 # When PK_PARSE_C and ECP_C are defined then PK_PARSE_EC_COMPRESSED
351 # is automatically enabled in build_info.h (backward compatibility)
352 # even if it is disabled in config_psa_crypto_no_ecp_at_all(). As a
353 # consequence compressed points are supported in the reference
354 # component but not in the accelerated one, so they should be skipped
355 # while checking driver's coverage.
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200356 re.compile(r'Parse EC Key .*compressed\)'),
357 re.compile(r'Parse Public EC Key .*compressed\)'),
Valerio Settiaddeee42023-06-14 10:46:55 +0200358 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200359 # See ecp_light_only
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200360 'test_suite_ssl': [
361 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
362 ],
Valerio Settie618cb02023-04-12 14:59:16 +0200363 }
364 }
365 },
Valerio Setti307810b2023-08-15 10:12:25 +0200366 'analyze_driver_vs_reference_ecc_no_bignum': {
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200367 'test_function': do_analyze_driver_vs_reference,
368 'args': {
369 'component_ref': 'test_psa_crypto_config_reference_ecc_no_bignum',
370 'component_driver': 'test_psa_crypto_config_accel_ecc_no_bignum',
371 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200372 # Modules replaced by drivers
373 'ecp', 'ecdsa', 'ecdh', 'ecjpake',
374 'bignum_core', 'bignum_random', 'bignum_mod', 'bignum_mod_raw',
375 'bignum.generated', 'bignum.misc',
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200376 ],
377 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200378 # See ecp_light_only
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200379 'test_suite_random': [
380 'PSA classic wrapper: ECDSA signature (SECP256R1)',
381 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200382 # See no_ecp_at_all
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200383 'test_suite_pkparse': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200384 re.compile(r'Parse EC Key .*compressed\)'),
385 re.compile(r'Parse Public EC Key .*compressed\)'),
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200386 ],
Valerio Setti9b3dbcc2023-07-26 18:00:31 +0200387 'test_suite_asn1parse': [
Valerio Setti9b3dbcc2023-07-26 18:00:31 +0200388 'INTEGER too large for mpi',
389 ],
390 'test_suite_asn1write': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200391 re.compile(r'ASN.1 Write mpi.*'),
Valerio Setti9b3dbcc2023-07-26 18:00:31 +0200392 ],
Valerio Settie0be95e2023-08-01 09:07:43 +0200393 'test_suite_debug': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200394 re.compile(r'Debug print mbedtls_mpi.*'),
Valerio Settie0be95e2023-08-01 09:07:43 +0200395 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200396 # See ecp_light_only
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200397 'test_suite_ssl': [
398 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
399 ],
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200400 }
401 }
402 },
Valerio Setti307810b2023-08-15 10:12:25 +0200403 'analyze_driver_vs_reference_ecc_ffdh_no_bignum': {
404 'test_function': do_analyze_driver_vs_reference,
405 'args': {
406 'component_ref': 'test_psa_crypto_config_reference_ecc_ffdh_no_bignum',
407 'component_driver': 'test_psa_crypto_config_accel_ecc_ffdh_no_bignum',
408 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200409 # Modules replaced by drivers
410 'ecp', 'ecdsa', 'ecdh', 'ecjpake', 'dhm',
411 'bignum_core', 'bignum_random', 'bignum_mod', 'bignum_mod_raw',
412 'bignum.generated', 'bignum.misc',
Valerio Setti307810b2023-08-15 10:12:25 +0200413 ],
414 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200415 # See ecp_light_only
Valerio Setti307810b2023-08-15 10:12:25 +0200416 'test_suite_random': [
417 'PSA classic wrapper: ECDSA signature (SECP256R1)',
418 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200419 # See no_ecp_at_all
Valerio Setti307810b2023-08-15 10:12:25 +0200420 'test_suite_pkparse': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200421 re.compile(r'Parse EC Key .*compressed\)'),
422 re.compile(r'Parse Public EC Key .*compressed\)'),
Valerio Setti307810b2023-08-15 10:12:25 +0200423 ],
424 'test_suite_asn1parse': [
Valerio Setti307810b2023-08-15 10:12:25 +0200425 'INTEGER too large for mpi',
426 ],
427 'test_suite_asn1write': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200428 re.compile(r'ASN.1 Write mpi.*'),
Valerio Setti307810b2023-08-15 10:12:25 +0200429 ],
430 'test_suite_debug': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200431 re.compile(r'Debug print mbedtls_mpi.*'),
Valerio Setti307810b2023-08-15 10:12:25 +0200432 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200433 # See ecp_light_only
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200434 'test_suite_ssl': [
435 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
436 ],
Valerio Setti307810b2023-08-15 10:12:25 +0200437 }
438 }
439 },
Przemek Stekiel85b64422023-05-26 09:55:23 +0200440 'analyze_driver_vs_reference_ffdh_alg': {
441 'test_function': do_analyze_driver_vs_reference,
442 'args': {
443 'component_ref': 'test_psa_crypto_config_reference_ffdh',
444 'component_driver': 'test_psa_crypto_config_accel_ffdh',
Przemek Stekiel84f4ff12023-07-04 12:35:31 +0200445 'ignored_suites': ['dhm'],
Przemek Stekiel565353e2023-07-05 11:07:07 +0200446 'ignored_tests': {}
Przemek Stekiel85b64422023-05-26 09:55:23 +0200447 }
448 },
Valerio Settif01d6482023-08-04 13:51:18 +0200449 'analyze_driver_vs_reference_tfm_config': {
450 'test_function': do_analyze_driver_vs_reference,
451 'args': {
452 'component_ref': 'test_tfm_config',
453 'component_driver': 'test_tfm_config_p256m_driver_accel_ec',
Manuel Pégourié-Gonnarde9d97972023-08-08 18:34:47 +0200454 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200455 # Modules replaced by drivers
Yanray Wang57790962023-10-31 13:39:07 +0800456 'asn1parse', 'asn1write',
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200457 'ecp', 'ecdsa', 'ecdh', 'ecjpake',
458 'bignum_core', 'bignum_random', 'bignum_mod', 'bignum_mod_raw',
459 'bignum.generated', 'bignum.misc',
Manuel Pégourié-Gonnarde9d97972023-08-08 18:34:47 +0200460 ],
Valerio Settif01d6482023-08-04 13:51:18 +0200461 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200462 # See ecp_light_only
Manuel Pégourié-Gonnarde9d97972023-08-08 18:34:47 +0200463 'test_suite_random': [
464 'PSA classic wrapper: ECDSA signature (SECP256R1)',
465 ],
Valerio Settif01d6482023-08-04 13:51:18 +0200466 }
467 }
468 }
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200469}
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200470
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200471def main():
Valerio Settif075e472023-10-17 11:03:16 +0200472 main_results = Results()
Valerio Settiaaef0bc2023-10-10 09:42:13 +0200473
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200474 try:
475 parser = argparse.ArgumentParser(description=__doc__)
Przemek Stekiel58bbc232022-10-24 08:10:10 +0200476 parser.add_argument('outcomes', metavar='OUTCOMES.CSV',
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200477 help='Outcome file to analyze')
Valerio Settidfd7ca62023-10-09 16:30:11 +0200478 parser.add_argument('specified_tasks', default='all', nargs='?',
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100479 help='Analysis to be done. By default, run all tasks. '
480 'With one or more TASK, run only those. '
481 'TASK can be the name of a single task or '
Przemek Stekiel85c54ea2022-11-17 11:50:23 +0100482 'comma/space-separated list of tasks. ')
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100483 parser.add_argument('--list', action='store_true',
484 help='List all available tasks and exit.')
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100485 parser.add_argument('--require-full-coverage', action='store_true',
486 dest='full_coverage', help="Require all available "
487 "test cases to be executed and issue an error "
488 "otherwise. This flag is ignored if 'task' is "
489 "neither 'all' nor 'analyze_coverage'")
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200490 options = parser.parse_args()
Przemek Stekiel4e955902022-10-21 13:42:08 +0200491
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100492 if options.list:
Valerio Settidfd7ca62023-10-09 16:30:11 +0200493 for task in KNOWN_TASKS:
Valerio Setti5329ff02023-10-17 09:44:36 +0200494 print(task)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100495 sys.exit(0)
496
Valerio Settidfd7ca62023-10-09 16:30:11 +0200497 if options.specified_tasks == 'all':
498 tasks_list = KNOWN_TASKS.keys()
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100499 else:
Valerio Settidfd7ca62023-10-09 16:30:11 +0200500 tasks_list = re.split(r'[, ]+', options.specified_tasks)
Valerio Settidfd7ca62023-10-09 16:30:11 +0200501 for task in tasks_list:
502 if task not in KNOWN_TASKS:
Manuel Pégourié-Gonnard62d61312023-10-20 10:51:57 +0200503 sys.stderr.write('invalid task: {}\n'.format(task))
Valerio Settifb2750e2023-10-17 10:11:45 +0200504 sys.exit(2)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100505
Valerio Settidfd7ca62023-10-09 16:30:11 +0200506 KNOWN_TASKS['analyze_coverage']['args']['full_coverage'] = options.full_coverage
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100507
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800508 # If the outcome file exists, parse it once and share the result
509 # among tasks to improve performance.
510 # Otherwise, it will be generated by do_analyze_driver_vs_reference.
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800511 if os.path.exists(options.outcomes):
512 main_results.info("Read outcome file from {}.", options.outcomes)
513 outcomes_or_file = read_outcome_file(options.outcomes)
514 else:
515 outcomes_or_file = options.outcomes
516
Valerio Settifb2750e2023-10-17 10:11:45 +0200517 for task in tasks_list:
518 test_function = KNOWN_TASKS[task]['test_function']
519 test_args = KNOWN_TASKS[task]['args']
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800520 test_function(main_results, outcomes_or_file, test_args)
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100521
Valerio Settif6f64cf2023-10-17 12:28:26 +0200522 main_results.info("Overall results: {} warnings and {} errors",
523 main_results.warning_count, main_results.error_count)
Przemek Stekiel4e955902022-10-21 13:42:08 +0200524
Valerio Setti8d178be2023-10-17 12:23:55 +0200525 sys.exit(0 if (main_results.error_count == 0) else 1)
Valerio Settiaaef0bc2023-10-10 09:42:13 +0200526
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200527 except Exception: # pylint: disable=broad-except
528 # Print the backtrace and exit explicitly with our chosen status.
529 traceback.print_exc()
530 sys.exit(120)
531
532if __name__ == '__main__':
533 main()