blob: 02aac225fccebe2e61f71d272830e170d84c1f5b [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 Lvc2e8f3a2023-11-28 17:22:04 +080019
20# `CompoentOutcomes` is a named tuple which is defined as:
21# ComponentOutcomes(
22# successes = {
23# "<suite_case>",
24# ...
25# },
26# failures = {
27# "<suite_case>",
28# ...
29# }
30# )
31# suite_case = "<suite>;<case>"
Pengyu Lv18908ec2023-11-28 12:11:52 +080032ComponentOutcomes = typing.NamedTuple('ComponentOutcomes',
33 [('successes', typing.Set[str]),
34 ('failures', typing.Set[str])])
35
Pengyu Lvc2e8f3a2023-11-28 17:22:04 +080036# `Outcomes` is a representation of the outcomes file,
37# which defined as:
38# Outcomes = {
39# "<component>": ComponentOutcomes,
40# ...
41# }
42Outcomes = typing.Dict[str, ComponentOutcomes]
43
44
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020045class Results:
46 """Process analysis results."""
47
48 def __init__(self):
49 self.error_count = 0
50 self.warning_count = 0
51
Valerio Setti2cff8202023-10-18 14:36:47 +020052 def new_section(self, fmt, *args, **kwargs):
53 self._print_line('\n*** ' + fmt + ' ***\n', *args, **kwargs)
54
Valerio Settiaaef0bc2023-10-10 09:42:13 +020055 def info(self, fmt, *args, **kwargs):
Valerio Setti8070dbe2023-10-17 12:29:30 +020056 self._print_line('Info: ' + fmt, *args, **kwargs)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020057
58 def error(self, fmt, *args, **kwargs):
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020059 self.error_count += 1
Valerio Setti8070dbe2023-10-17 12:29:30 +020060 self._print_line('Error: ' + fmt, *args, **kwargs)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020061
62 def warning(self, fmt, *args, **kwargs):
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020063 self.warning_count += 1
Valerio Setti8070dbe2023-10-17 12:29:30 +020064 self._print_line('Warning: ' + fmt, *args, **kwargs)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020065
Valerio Setti3f339892023-10-17 10:42:11 +020066 @staticmethod
Valerio Setti8070dbe2023-10-17 12:29:30 +020067 def _print_line(fmt, *args, **kwargs):
Valerio Setti735794c2023-10-18 08:05:15 +020068 sys.stderr.write((fmt + '\n').format(*args, **kwargs))
Gilles Peskine15c2cbf2020-06-25 18:36:28 +020069
Pengyu Lvc2e8f3a2023-11-28 17:22:04 +080070def execute_reference_driver_tests(results: Results, ref_component: str, driver_component: str, \
71 outcome_file: str) -> None:
Valerio Setti22992a02023-03-29 11:15:28 +020072 """Run the tests specified in ref_component and driver_component. Results
73 are stored in the output_file and they will be used for the following
Valerio Settia2663322023-03-24 08:20:18 +010074 coverage analysis"""
Pengyu Lv20e3ca32023-11-28 15:30:03 +080075 results.new_section("Test {} and {}", ref_component, driver_component)
Valerio Settia2663322023-03-24 08:20:18 +010076
77 shell_command = "tests/scripts/all.sh --outcome-file " + outcome_file + \
78 " " + ref_component + " " + driver_component
Valerio Setti39d4b9d2023-10-18 14:30:03 +020079 results.info("Running: {}", shell_command)
Valerio Settia2663322023-03-24 08:20:18 +010080 ret_val = subprocess.run(shell_command.split(), check=False).returncode
81
82 if ret_val != 0:
Valerio Settif075e472023-10-17 11:03:16 +020083 results.error("failed to run reference/driver components")
Valerio Settia2663322023-03-24 08:20:18 +010084
Pengyu Lvc2e8f3a2023-11-28 17:22:04 +080085def analyze_coverage(results: Results, outcomes: Outcomes,
86 allow_list: typing.List[str], full_coverage: bool) -> None:
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020087 """Check that all available test cases are executed at least once."""
Gilles Peskine686c2922022-01-07 15:58:38 +010088 available = check_test_cases.collect_available_test_cases()
Pengyu Lv31a9b782023-11-23 14:15:37 +080089 for suite_case in available:
Pengyu Lva4428582023-11-22 19:02:15 +080090 hits = 0
Pengyu Lvdd1d6a72023-11-27 17:57:31 +080091 for comp_outcomes in outcomes.values():
Pengyu Lv18908ec2023-11-28 12:11:52 +080092 if suite_case in comp_outcomes.successes or \
93 suite_case in comp_outcomes.failures:
Pengyu Lva4428582023-11-22 19:02:15 +080094 hits += 1
Pengyu Lvf28cf592023-11-28 10:56:29 +080095 break
Pengyu Lva4428582023-11-22 19:02:15 +080096
Pengyu Lv31a9b782023-11-23 14:15:37 +080097 if hits == 0 and suite_case not in allow_list:
Tomás Gonzálezb401e112023-08-11 15:22:04 +010098 if full_coverage:
Pengyu Lv31a9b782023-11-23 14:15:37 +080099 results.error('Test case not executed: {}', suite_case)
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100100 else:
Pengyu Lv31a9b782023-11-23 14:15:37 +0800101 results.warning('Test case not executed: {}', suite_case)
102 elif hits != 0 and suite_case in allow_list:
Tomás González07bdcc22023-08-11 14:59:03 +0100103 # Test Case should be removed from the allow list.
Tomás González7ebb18f2023-08-22 09:40:23 +0100104 if full_coverage:
Pengyu Lv31a9b782023-11-23 14:15:37 +0800105 results.error('Allow listed test case was executed: {}', suite_case)
Tomás González7ebb18f2023-08-22 09:40:23 +0100106 else:
Pengyu Lv31a9b782023-11-23 14:15:37 +0800107 results.warning('Allow listed test case was executed: {}', suite_case)
Gilles Peskine8d3c70a2020-06-25 18:37:43 +0200108
Pengyu Lvc2e8f3a2023-11-28 17:22:04 +0800109def name_matches_pattern(name: str, str_or_re) -> bool:
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200110 """Check if name matches a pattern, that may be a string or regex.
111 - If the pattern is a string, name must be equal to match.
112 - If the pattern is a regex, name must fully match.
113 """
Manuel Pégourié-Gonnardb2695432023-10-23 09:30:40 +0200114 # The CI's python is too old for re.Pattern
115 #if isinstance(str_or_re, re.Pattern):
116 if not isinstance(str_or_re, str):
Pengyu Lvc2e8f3a2023-11-28 17:22:04 +0800117 return str_or_re.fullmatch(name) is not None
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200118 else:
Manuel Pégourié-Gonnard9d9c2342023-10-26 09:37:40 +0200119 return str_or_re == name
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200120
Pengyu Lvc2e8f3a2023-11-28 17:22:04 +0800121def analyze_driver_vs_reference(results: Results, outcomes: Outcomes,
122 component_ref: str, component_driver: str,
123 ignored_suites: typing.List[str], ignored_tests=None) -> None:
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800124 """Check that all tests passing in the reference component are also
125 passing in the corresponding driver component.
Valerio Setti3002c992023-01-18 17:28:36 +0100126 Skip:
127 - full test suites provided in ignored_suites list
128 - only some specific test inside a test suite, for which the corresponding
129 output string is provided
Przemek Stekiel4e955902022-10-21 13:42:08 +0200130 """
Pengyu Lva4428582023-11-22 19:02:15 +0800131 ref_outcomes = outcomes.get("component_" + component_ref)
132 driver_outcomes = outcomes.get("component_" + component_driver)
133
Pengyu Lv59b9efc2023-11-28 11:15:00 +0800134 if ref_outcomes is None or driver_outcomes is None:
135 results.error("required components are missing: bad outcome file?")
136 return
137
Pengyu Lv18908ec2023-11-28 12:11:52 +0800138 if not ref_outcomes.successes:
Pengyu Lva4428582023-11-22 19:02:15 +0800139 results.error("no passing test in reference component: bad outcome file?")
140 return
141
Pengyu Lv18908ec2023-11-28 12:11:52 +0800142 for suite_case in ref_outcomes.successes:
Pengyu Lv31a9b782023-11-23 14:15:37 +0800143 # suite_case is like "test_suite_foo.bar;Description of test case"
144 (full_test_suite, test_string) = suite_case.split(';')
Valerio Setti00c1ccb2023-02-02 11:33:31 +0100145 test_suite = full_test_suite.split('.')[0] # retrieve main part of test suite name
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200146
147 # Immediately skip fully-ignored test suites
Manuel Pégourié-Gonnard7d381f52023-03-17 15:13:08 +0100148 if test_suite in ignored_suites or full_test_suite in ignored_suites:
Valerio Setti00c1ccb2023-02-02 11:33:31 +0100149 continue
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200150
151 # For ignored test cases inside test suites, just remember and:
152 # don't issue an error if they're skipped with drivers,
153 # but issue an error if they're not (means we have a bad entry).
154 ignored = False
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200155 if full_test_suite in ignored_tests:
Manuel Pégourié-Gonnardd36a37f2023-10-26 09:41:59 +0200156 for str_or_re in ignored_tests[test_suite]:
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200157 if name_matches_pattern(test_string, str_or_re):
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200158 ignored = True
Manuel Pégourié-Gonnard4da369f2023-10-18 09:40:32 +0200159
Pengyu Lv18908ec2023-11-28 12:11:52 +0800160 if not ignored and not suite_case in driver_outcomes.successes:
Pengyu Lv31a9b782023-11-23 14:15:37 +0800161 results.error("PASS -> SKIP/FAIL: {}", suite_case)
Pengyu Lv18908ec2023-11-28 12:11:52 +0800162 if ignored and suite_case in driver_outcomes.successes:
Pengyu Lv31a9b782023-11-23 14:15:37 +0800163 results.error("uselessly ignored: {}", suite_case)
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200164
Pengyu Lvc2e8f3a2023-11-28 17:22:04 +0800165def analyze_outcomes(results: Results, outcomes: Outcomes, args) -> None:
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200166 """Run all analyses on the given outcome collection."""
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100167 analyze_coverage(results, outcomes, args['allow_list'],
168 args['full_coverage'])
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200169
Pengyu Lvc2e8f3a2023-11-28 17:22:04 +0800170def read_outcome_file(outcome_file: str) -> Outcomes:
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200171 """Parse an outcome file and return an outcome collection.
Pengyu Lvc2e8f3a2023-11-28 17:22:04 +0800172 """
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200173 outcomes = {}
174 with open(outcome_file, 'r', encoding='utf-8') as input_file:
175 for line in input_file:
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800176 (_platform, component, suite, case, result, _cause) = line.split(';')
Pengyu Lv451ec8a2023-11-28 17:59:05 +0800177 # Note that `component` is not unique. If a test case passes on Linux
178 # and fails on FreeBSD, it'll end up in both the successes set and
179 # the failures set.
Pengyu Lv31a9b782023-11-23 14:15:37 +0800180 suite_case = ';'.join([suite, case])
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800181 if component not in outcomes:
Pengyu Lv18908ec2023-11-28 12:11:52 +0800182 outcomes[component] = ComponentOutcomes(set(), set())
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200183 if result == 'PASS':
Pengyu Lv18908ec2023-11-28 12:11:52 +0800184 outcomes[component].successes.add(suite_case)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200185 elif result == 'FAIL':
Pengyu Lv18908ec2023-11-28 12:11:52 +0800186 outcomes[component].failures.add(suite_case)
Pengyu Lva4428582023-11-22 19:02:15 +0800187
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200188 return outcomes
189
Pengyu Lvc2e8f3a2023-11-28 17:22:04 +0800190def do_analyze_coverage(results: Results, outcomes: Outcomes, args) -> None:
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100191 """Perform coverage analysis."""
Valerio Setti2cff8202023-10-18 14:36:47 +0200192 results.new_section("Analyze coverage")
Valerio Setti781c2342023-10-17 12:47:35 +0200193 analyze_outcomes(results, outcomes, args)
Przemek Stekiel4e955902022-10-21 13:42:08 +0200194
Pengyu Lvc2e8f3a2023-11-28 17:22:04 +0800195def do_analyze_driver_vs_reference(results: Results, outcomes: Outcomes, args) -> None:
Przemek Stekiel4e955902022-10-21 13:42:08 +0200196 """Perform driver vs reference analyze."""
Valerio Setti2cff8202023-10-18 14:36:47 +0200197 results.new_section("Analyze driver {} vs reference {}",
198 args['component_driver'], args['component_ref'])
Valerio Settib0c618e2023-10-16 14:19:49 +0200199
Valerio Setti3002c992023-01-18 17:28:36 +0100200 ignored_suites = ['test_suite_' + x for x in args['ignored_suites']]
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100201
Valerio Setti781c2342023-10-17 12:47:35 +0200202 analyze_driver_vs_reference(results, outcomes,
203 args['component_ref'], args['component_driver'],
204 ignored_suites, args['ignored_tests'])
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200205
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100206# List of tasks with a function that can handle this task and additional arguments if required
Valerio Settidfd7ca62023-10-09 16:30:11 +0200207KNOWN_TASKS = {
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200208 'analyze_coverage': {
209 'test_function': do_analyze_coverage,
Tomás González07bdcc22023-08-11 14:59:03 +0100210 'args': {
Tomás González358c6c62023-08-14 15:43:46 +0100211 'allow_list': [
Tomás González50223112023-08-22 09:52:06 +0100212 # Algorithm not supported yet
213 'test_suite_psa_crypto_metadata;Asymmetric signature: pure EdDSA',
214 # Algorithm not supported yet
215 'test_suite_psa_crypto_metadata;Cipher: XTS',
Tomás Gonzálezd43cab32023-08-24 09:12:40 +0100216 ],
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100217 'full_coverage': False,
Tomás González07bdcc22023-08-11 14:59:03 +0100218 }
Tomás Gonzálezd43cab32023-08-24 09:12:40 +0100219 },
Valerio Settia2663322023-03-24 08:20:18 +0100220 # There are 2 options to use analyze_driver_vs_reference_xxx locally:
221 # 1. Run tests and then analysis:
222 # - tests/scripts/all.sh --outcome-file "$PWD/out.csv" <component_ref> <component_driver>
223 # - tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
224 # 2. Let this script run both automatically:
225 # - tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200226 'analyze_driver_vs_reference_hash': {
227 'test_function': do_analyze_driver_vs_reference,
228 'args': {
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100229 'component_ref': 'test_psa_crypto_config_reference_hash_use_psa',
230 'component_driver': 'test_psa_crypto_config_accel_hash_use_psa',
Manuel Pégourié-Gonnard10e39632022-12-29 12:29:09 +0100231 'ignored_suites': [
232 'shax', 'mdx', # the software implementations that are being excluded
Manuel Pégourié-Gonnard7d381f52023-03-17 15:13:08 +0100233 'md.psa', # purposefully depends on whether drivers are present
Gilles Peskine35b49c42023-10-04 12:28:41 +0200234 'psa_crypto_low_hash.generated', # testing the builtins
Valerio Setti3002c992023-01-18 17:28:36 +0100235 ],
236 'ignored_tests': {
237 }
238 }
239 },
Valerio Settib6b301f2023-10-04 12:05:05 +0200240 'analyze_driver_vs_reference_cipher_aead': {
241 'test_function': do_analyze_driver_vs_reference,
242 'args': {
243 'component_ref': 'test_psa_crypto_config_reference_cipher_aead',
244 'component_driver': 'test_psa_crypto_config_accel_cipher_aead',
Valerio Setti507e08f2023-10-26 09:44:06 +0200245 # Modules replaced by drivers.
Valerio Settib6b301f2023-10-04 12:05:05 +0200246 'ignored_suites': [
Valerio Setti507e08f2023-10-26 09:44:06 +0200247 # low-level (block/stream) cipher modules
248 'aes', 'aria', 'camellia', 'des', 'chacha20',
249 # AEAD modes
250 'ccm', 'chachapoly', 'cmac', 'gcm',
251 # The Cipher abstraction layer
252 'cipher',
Valerio Settib6b301f2023-10-04 12:05:05 +0200253 ],
254 'ignored_tests': {
Valerio Setti507e08f2023-10-26 09:44:06 +0200255 # PEM decryption is not supported so far.
256 # The rest of PEM (write, unencrypted read) works though.
Valerio Setti7448cee2023-10-04 15:46:42 +0200257 'test_suite_pem': [
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200258 re.compile(r'PEM read .*(AES|DES|\bencrypt).*'),
Valerio Setti7448cee2023-10-04 15:46:42 +0200259 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200260 # Following tests depend on AES_C/DES_C but are not about
261 # them really, just need to know some error code is there.
Valerio Setti7448cee2023-10-04 15:46:42 +0200262 'test_suite_error': [
263 'Low and high error',
264 'Single low error'
265 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200266 # Similar to test_suite_error above.
Valerio Setti7448cee2023-10-04 15:46:42 +0200267 'test_suite_version': [
268 'Check for MBEDTLS_AES_C when already present',
Valerio Setti93941442023-10-13 09:19:52 +0200269 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200270 # The en/decryption part of PKCS#12 is not supported so far.
271 # The rest of PKCS#12 (key derivation) works though.
Valerio Setti93941442023-10-13 09:19:52 +0200272 'test_suite_pkcs12': [
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200273 re.compile(r'PBE Encrypt, .*'),
274 re.compile(r'PBE Decrypt, .*'),
Valerio Setti93941442023-10-13 09:19:52 +0200275 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200276 # The en/decryption part of PKCS#5 is not supported so far.
277 # The rest of PKCS#5 (PBKDF2) works though.
Valerio Setti93941442023-10-13 09:19:52 +0200278 'test_suite_pkcs5': [
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200279 re.compile(r'PBES2 Encrypt, .*'),
280 re.compile(r'PBES2 Decrypt .*'),
Valerio Setti93941442023-10-13 09:19:52 +0200281 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200282 # Encrypted keys are not supported so far.
Valerio Setti5cd18f92023-10-13 15:14:07 +0200283 # pylint: disable=line-too-long
Valerio Setti93941442023-10-13 09:19:52 +0200284 'test_suite_pkparse': [
285 'Key ASN1 (Encrypted key PKCS12, trailing garbage data)',
286 'Key ASN1 (Encrypted key PKCS5, trailing garbage data)',
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200287 re.compile(r'Parse RSA Key .*\(PKCS#8 encrypted .*\)'),
Valerio Setti93941442023-10-13 09:19:52 +0200288 ],
Valerio Settib6b301f2023-10-04 12:05:05 +0200289 }
290 }
291 },
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200292 'analyze_driver_vs_reference_ecp_light_only': {
Valerio Setti42d5f192023-03-20 13:54:41 +0100293 'test_function': do_analyze_driver_vs_reference,
294 'args': {
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200295 'component_ref': 'test_psa_crypto_config_reference_ecc_ecp_light_only',
296 'component_driver': 'test_psa_crypto_config_accel_ecc_ecp_light_only',
Valerio Setti42d5f192023-03-20 13:54:41 +0100297 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200298 # Modules replaced by drivers
299 'ecdsa', 'ecdh', 'ecjpake',
Valerio Setti42d5f192023-03-20 13:54:41 +0100300 ],
301 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200302 # This test wants a legacy function that takes f_rng, p_rng
303 # arguments, and uses legacy ECDSA for that. The test is
304 # really about the wrapper around the PSA RNG, not ECDSA.
Valerio Setti42d5f192023-03-20 13:54:41 +0100305 'test_suite_random': [
306 'PSA classic wrapper: ECDSA signature (SECP256R1)',
307 ],
Valerio Setti0c477d32023-04-07 15:54:20 +0200308 # In the accelerated test ECP_C is not set (only ECP_LIGHT is)
309 # so we must ignore disparities in the tests for which ECP_C
310 # is required.
311 'test_suite_ecp': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200312 re.compile(r'ECP check public-private .*'),
313 re.compile(r'ECP gen keypair .*'),
314 re.compile(r'ECP point muladd .*'),
315 re.compile(r'ECP point multiplication .*'),
316 re.compile(r'ECP test vectors .*'),
Valerio Setti482a0b92023-08-18 15:55:10 +0200317 ],
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200318 'test_suite_ssl': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200319 # This deprecated function is only present when ECP_C is On.
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200320 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
321 ],
Valerio Setti5f540202023-06-30 17:20:49 +0200322 }
Valerio Setti42d5f192023-03-20 13:54:41 +0100323 }
324 },
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200325 'analyze_driver_vs_reference_no_ecp_at_all': {
Valerio Settie618cb02023-04-12 14:59:16 +0200326 'test_function': do_analyze_driver_vs_reference,
327 'args': {
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200328 'component_ref': 'test_psa_crypto_config_reference_ecc_no_ecp_at_all',
329 'component_driver': 'test_psa_crypto_config_accel_ecc_no_ecp_at_all',
Valerio Settie618cb02023-04-12 14:59:16 +0200330 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200331 # Modules replaced by drivers
332 'ecp', 'ecdsa', 'ecdh', 'ecjpake',
Valerio Settie618cb02023-04-12 14:59:16 +0200333 ],
334 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200335 # See ecp_light_only
Valerio Settie618cb02023-04-12 14:59:16 +0200336 'test_suite_random': [
337 'PSA classic wrapper: ECDSA signature (SECP256R1)',
338 ],
Valerio Settiaddeee42023-06-14 10:46:55 +0200339 'test_suite_pkparse': [
Valerio Setti5bd25232023-06-19 19:32:14 +0200340 # When PK_PARSE_C and ECP_C are defined then PK_PARSE_EC_COMPRESSED
341 # is automatically enabled in build_info.h (backward compatibility)
342 # even if it is disabled in config_psa_crypto_no_ecp_at_all(). As a
343 # consequence compressed points are supported in the reference
344 # component but not in the accelerated one, so they should be skipped
345 # while checking driver's coverage.
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200346 re.compile(r'Parse EC Key .*compressed\)'),
347 re.compile(r'Parse Public EC Key .*compressed\)'),
Valerio Settiaddeee42023-06-14 10:46:55 +0200348 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200349 # See ecp_light_only
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200350 'test_suite_ssl': [
351 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
352 ],
Valerio Settie618cb02023-04-12 14:59:16 +0200353 }
354 }
355 },
Valerio Setti307810b2023-08-15 10:12:25 +0200356 'analyze_driver_vs_reference_ecc_no_bignum': {
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200357 'test_function': do_analyze_driver_vs_reference,
358 'args': {
359 'component_ref': 'test_psa_crypto_config_reference_ecc_no_bignum',
360 'component_driver': 'test_psa_crypto_config_accel_ecc_no_bignum',
361 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200362 # Modules replaced by drivers
363 'ecp', 'ecdsa', 'ecdh', 'ecjpake',
364 'bignum_core', 'bignum_random', 'bignum_mod', 'bignum_mod_raw',
365 'bignum.generated', 'bignum.misc',
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200366 ],
367 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200368 # See ecp_light_only
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200369 'test_suite_random': [
370 'PSA classic wrapper: ECDSA signature (SECP256R1)',
371 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200372 # See no_ecp_at_all
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200373 'test_suite_pkparse': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200374 re.compile(r'Parse EC Key .*compressed\)'),
375 re.compile(r'Parse Public EC Key .*compressed\)'),
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200376 ],
Valerio Setti9b3dbcc2023-07-26 18:00:31 +0200377 'test_suite_asn1parse': [
Valerio Setti9b3dbcc2023-07-26 18:00:31 +0200378 'INTEGER too large for mpi',
379 ],
380 'test_suite_asn1write': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200381 re.compile(r'ASN.1 Write mpi.*'),
Valerio Setti9b3dbcc2023-07-26 18:00:31 +0200382 ],
Valerio Settie0be95e2023-08-01 09:07:43 +0200383 'test_suite_debug': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200384 re.compile(r'Debug print mbedtls_mpi.*'),
Valerio Settie0be95e2023-08-01 09:07:43 +0200385 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200386 # See ecp_light_only
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200387 'test_suite_ssl': [
388 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
389 ],
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200390 }
391 }
392 },
Valerio Setti307810b2023-08-15 10:12:25 +0200393 'analyze_driver_vs_reference_ecc_ffdh_no_bignum': {
394 'test_function': do_analyze_driver_vs_reference,
395 'args': {
396 'component_ref': 'test_psa_crypto_config_reference_ecc_ffdh_no_bignum',
397 'component_driver': 'test_psa_crypto_config_accel_ecc_ffdh_no_bignum',
398 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200399 # Modules replaced by drivers
400 'ecp', 'ecdsa', 'ecdh', 'ecjpake', 'dhm',
401 'bignum_core', 'bignum_random', 'bignum_mod', 'bignum_mod_raw',
402 'bignum.generated', 'bignum.misc',
Valerio Setti307810b2023-08-15 10:12:25 +0200403 ],
404 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200405 # See ecp_light_only
Valerio Setti307810b2023-08-15 10:12:25 +0200406 'test_suite_random': [
407 'PSA classic wrapper: ECDSA signature (SECP256R1)',
408 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200409 # See no_ecp_at_all
Valerio Setti307810b2023-08-15 10:12:25 +0200410 'test_suite_pkparse': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200411 re.compile(r'Parse EC Key .*compressed\)'),
412 re.compile(r'Parse Public EC Key .*compressed\)'),
Valerio Setti307810b2023-08-15 10:12:25 +0200413 ],
414 'test_suite_asn1parse': [
Valerio Setti307810b2023-08-15 10:12:25 +0200415 'INTEGER too large for mpi',
416 ],
417 'test_suite_asn1write': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200418 re.compile(r'ASN.1 Write mpi.*'),
Valerio Setti307810b2023-08-15 10:12:25 +0200419 ],
420 'test_suite_debug': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200421 re.compile(r'Debug print mbedtls_mpi.*'),
Valerio Setti307810b2023-08-15 10:12:25 +0200422 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200423 # See ecp_light_only
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200424 'test_suite_ssl': [
425 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
426 ],
Valerio Setti307810b2023-08-15 10:12:25 +0200427 }
428 }
429 },
Przemek Stekiel85b64422023-05-26 09:55:23 +0200430 'analyze_driver_vs_reference_ffdh_alg': {
431 'test_function': do_analyze_driver_vs_reference,
432 'args': {
433 'component_ref': 'test_psa_crypto_config_reference_ffdh',
434 'component_driver': 'test_psa_crypto_config_accel_ffdh',
Przemek Stekiel84f4ff12023-07-04 12:35:31 +0200435 'ignored_suites': ['dhm'],
Przemek Stekiel565353e2023-07-05 11:07:07 +0200436 'ignored_tests': {}
Przemek Stekiel85b64422023-05-26 09:55:23 +0200437 }
438 },
Valerio Settif01d6482023-08-04 13:51:18 +0200439 'analyze_driver_vs_reference_tfm_config': {
440 'test_function': do_analyze_driver_vs_reference,
441 'args': {
442 'component_ref': 'test_tfm_config',
443 'component_driver': 'test_tfm_config_p256m_driver_accel_ec',
Manuel Pégourié-Gonnarde9d97972023-08-08 18:34:47 +0200444 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200445 # Modules replaced by drivers
Yanray Wang57790962023-10-31 13:39:07 +0800446 'asn1parse', 'asn1write',
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200447 'ecp', 'ecdsa', 'ecdh', 'ecjpake',
448 'bignum_core', 'bignum_random', 'bignum_mod', 'bignum_mod_raw',
449 'bignum.generated', 'bignum.misc',
Manuel Pégourié-Gonnarde9d97972023-08-08 18:34:47 +0200450 ],
Valerio Settif01d6482023-08-04 13:51:18 +0200451 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200452 # See ecp_light_only
Manuel Pégourié-Gonnarde9d97972023-08-08 18:34:47 +0200453 'test_suite_random': [
454 'PSA classic wrapper: ECDSA signature (SECP256R1)',
455 ],
Valerio Settif01d6482023-08-04 13:51:18 +0200456 }
457 }
458 }
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200459}
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200460
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200461def main():
Valerio Settif075e472023-10-17 11:03:16 +0200462 main_results = Results()
Valerio Settiaaef0bc2023-10-10 09:42:13 +0200463
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200464 try:
465 parser = argparse.ArgumentParser(description=__doc__)
Przemek Stekiel58bbc232022-10-24 08:10:10 +0200466 parser.add_argument('outcomes', metavar='OUTCOMES.CSV',
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200467 help='Outcome file to analyze')
Valerio Settidfd7ca62023-10-09 16:30:11 +0200468 parser.add_argument('specified_tasks', default='all', nargs='?',
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100469 help='Analysis to be done. By default, run all tasks. '
470 'With one or more TASK, run only those. '
471 'TASK can be the name of a single task or '
Przemek Stekiel85c54ea2022-11-17 11:50:23 +0100472 'comma/space-separated list of tasks. ')
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100473 parser.add_argument('--list', action='store_true',
474 help='List all available tasks and exit.')
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100475 parser.add_argument('--require-full-coverage', action='store_true',
476 dest='full_coverage', help="Require all available "
477 "test cases to be executed and issue an error "
478 "otherwise. This flag is ignored if 'task' is "
479 "neither 'all' nor 'analyze_coverage'")
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200480 options = parser.parse_args()
Przemek Stekiel4e955902022-10-21 13:42:08 +0200481
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100482 if options.list:
Valerio Settidfd7ca62023-10-09 16:30:11 +0200483 for task in KNOWN_TASKS:
Valerio Setti5329ff02023-10-17 09:44:36 +0200484 print(task)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100485 sys.exit(0)
486
Valerio Settidfd7ca62023-10-09 16:30:11 +0200487 if options.specified_tasks == 'all':
488 tasks_list = KNOWN_TASKS.keys()
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100489 else:
Valerio Settidfd7ca62023-10-09 16:30:11 +0200490 tasks_list = re.split(r'[, ]+', options.specified_tasks)
Valerio Settidfd7ca62023-10-09 16:30:11 +0200491 for task in tasks_list:
492 if task not in KNOWN_TASKS:
Manuel Pégourié-Gonnard62d61312023-10-20 10:51:57 +0200493 sys.stderr.write('invalid task: {}\n'.format(task))
Valerio Settifb2750e2023-10-17 10:11:45 +0200494 sys.exit(2)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100495
Valerio Settidfd7ca62023-10-09 16:30:11 +0200496 KNOWN_TASKS['analyze_coverage']['args']['full_coverage'] = options.full_coverage
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100497
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800498 # If the outcome file exists, parse it once and share the result
499 # among tasks to improve performance.
Pengyu Lv20e3ca32023-11-28 15:30:03 +0800500 # Otherwise, it will be generated by execute_reference_driver_tests.
501 if not os.path.exists(options.outcomes):
502 if len(tasks_list) > 1:
503 sys.stderr.write("mutiple tasks found, please provide a valid outcomes file.\n")
504 sys.exit(2)
505
506 task_name = tasks_list[0]
507 task = KNOWN_TASKS[task_name]
Pengyu Lvc2e8f3a2023-11-28 17:22:04 +0800508 if task['test_function'] != do_analyze_driver_vs_reference: # pylint: disable=comparison-with-callable
Pengyu Lv20e3ca32023-11-28 15:30:03 +0800509 sys.stderr.write("please provide valid outcomes file for {}.\n".format(task_name))
510 sys.exit(2)
511
512 execute_reference_driver_tests(main_results,
513 task['args']['component_ref'],
514 task['args']['component_driver'],
515 options.outcomes)
516
517 outcomes = read_outcome_file(options.outcomes)
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800518
Valerio Settifb2750e2023-10-17 10:11:45 +0200519 for task in tasks_list:
520 test_function = KNOWN_TASKS[task]['test_function']
521 test_args = KNOWN_TASKS[task]['args']
Pengyu Lv20e3ca32023-11-28 15:30:03 +0800522 test_function(main_results, outcomes, test_args)
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100523
Valerio Settif6f64cf2023-10-17 12:28:26 +0200524 main_results.info("Overall results: {} warnings and {} errors",
525 main_results.warning_count, main_results.error_count)
Przemek Stekiel4e955902022-10-21 13:42:08 +0200526
Valerio Setti8d178be2023-10-17 12:23:55 +0200527 sys.exit(0 if (main_results.error_count == 0) else 1)
Valerio Settiaaef0bc2023-10-10 09:42:13 +0200528
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200529 except Exception: # pylint: disable=broad-except
530 # Print the backtrace and exit explicitly with our chosen status.
531 traceback.print_exc()
532 sys.exit(120)
533
534if __name__ == '__main__':
535 main()