blob: 4e925a18e4a968c141b54f2ec8332c5cdd337362 [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"""
Pengyu Lv20e3ca32023-11-28 15:30:03 +080053 results.new_section("Test {} and {}", ref_component, driver_component)
Valerio Settia2663322023-03-24 08:20:18 +010054
55 shell_command = "tests/scripts/all.sh --outcome-file " + outcome_file + \
56 " " + ref_component + " " + driver_component
Valerio Setti39d4b9d2023-10-18 14:30:03 +020057 results.info("Running: {}", shell_command)
Valerio Settia2663322023-03-24 08:20:18 +010058 ret_val = subprocess.run(shell_command.split(), check=False).returncode
59
60 if ret_val != 0:
Valerio Settif075e472023-10-17 11:03:16 +020061 results.error("failed to run reference/driver components")
Valerio Settia2663322023-03-24 08:20:18 +010062
Tomás Gonzálezb401e112023-08-11 15:22:04 +010063def analyze_coverage(results, outcomes, allow_list, full_coverage):
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020064 """Check that all available test cases are executed at least once."""
Gilles Peskine686c2922022-01-07 15:58:38 +010065 available = check_test_cases.collect_available_test_cases()
Pengyu Lv31a9b782023-11-23 14:15:37 +080066 for suite_case in available:
Pengyu Lva4428582023-11-22 19:02:15 +080067 hits = 0
Pengyu Lvdd1d6a72023-11-27 17:57:31 +080068 for comp_outcomes in outcomes.values():
Pengyu Lv18908ec2023-11-28 12:11:52 +080069 if suite_case in comp_outcomes.successes or \
70 suite_case in comp_outcomes.failures:
Pengyu Lva4428582023-11-22 19:02:15 +080071 hits += 1
Pengyu Lvf28cf592023-11-28 10:56:29 +080072 break
Pengyu Lva4428582023-11-22 19:02:15 +080073
Pengyu Lv31a9b782023-11-23 14:15:37 +080074 if hits == 0 and suite_case not in allow_list:
Tomás Gonzálezb401e112023-08-11 15:22:04 +010075 if full_coverage:
Pengyu Lv31a9b782023-11-23 14:15:37 +080076 results.error('Test case not executed: {}', suite_case)
Tomás Gonzálezb401e112023-08-11 15:22:04 +010077 else:
Pengyu Lv31a9b782023-11-23 14:15:37 +080078 results.warning('Test case not executed: {}', suite_case)
79 elif hits != 0 and suite_case in allow_list:
Tomás González07bdcc22023-08-11 14:59:03 +010080 # Test Case should be removed from the allow list.
Tomás González7ebb18f2023-08-22 09:40:23 +010081 if full_coverage:
Pengyu Lv31a9b782023-11-23 14:15:37 +080082 results.error('Allow listed test case was executed: {}', suite_case)
Tomás González7ebb18f2023-08-22 09:40:23 +010083 else:
Pengyu Lv31a9b782023-11-23 14:15:37 +080084 results.warning('Allow listed test case was executed: {}', suite_case)
Gilles Peskine8d3c70a2020-06-25 18:37:43 +020085
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +020086def name_matches_pattern(name, str_or_re):
87 """Check if name matches a pattern, that may be a string or regex.
88 - If the pattern is a string, name must be equal to match.
89 - If the pattern is a regex, name must fully match.
90 """
Manuel Pégourié-Gonnardb2695432023-10-23 09:30:40 +020091 # The CI's python is too old for re.Pattern
92 #if isinstance(str_or_re, re.Pattern):
93 if not isinstance(str_or_re, str):
Manuel Pégourié-Gonnard9d9c2342023-10-26 09:37:40 +020094 return str_or_re.fullmatch(name)
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +020095 else:
Manuel Pégourié-Gonnard9d9c2342023-10-26 09:37:40 +020096 return str_or_re == name
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +020097
Valerio Settif075e472023-10-17 11:03:16 +020098def analyze_driver_vs_reference(results: Results, outcomes,
Valerio Settiaaef0bc2023-10-10 09:42:13 +020099 component_ref, component_driver,
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200100 ignored_suites, ignored_tests=None):
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800101 """Check that all tests passing in the reference component are also
102 passing in the corresponding driver component.
Valerio Setti3002c992023-01-18 17:28:36 +0100103 Skip:
104 - full test suites provided in ignored_suites list
105 - only some specific test inside a test suite, for which the corresponding
106 output string is provided
Przemek Stekiel4e955902022-10-21 13:42:08 +0200107 """
Pengyu Lva4428582023-11-22 19:02:15 +0800108 ref_outcomes = outcomes.get("component_" + component_ref)
109 driver_outcomes = outcomes.get("component_" + component_driver)
110
Pengyu Lv59b9efc2023-11-28 11:15:00 +0800111 if ref_outcomes is None or driver_outcomes is None:
112 results.error("required components are missing: bad outcome file?")
113 return
114
Pengyu Lv18908ec2023-11-28 12:11:52 +0800115 if not ref_outcomes.successes:
Pengyu Lva4428582023-11-22 19:02:15 +0800116 results.error("no passing test in reference component: bad outcome file?")
117 return
118
Pengyu Lv18908ec2023-11-28 12:11:52 +0800119 for suite_case in ref_outcomes.successes:
Pengyu Lv31a9b782023-11-23 14:15:37 +0800120 # suite_case is like "test_suite_foo.bar;Description of test case"
121 (full_test_suite, test_string) = suite_case.split(';')
Valerio Setti00c1ccb2023-02-02 11:33:31 +0100122 test_suite = full_test_suite.split('.')[0] # retrieve main part of test suite name
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200123
124 # Immediately skip fully-ignored test suites
Manuel Pégourié-Gonnard7d381f52023-03-17 15:13:08 +0100125 if test_suite in ignored_suites or full_test_suite in ignored_suites:
Valerio Setti00c1ccb2023-02-02 11:33:31 +0100126 continue
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200127
128 # For ignored test cases inside test suites, just remember and:
129 # don't issue an error if they're skipped with drivers,
130 # but issue an error if they're not (means we have a bad entry).
131 ignored = False
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200132 if full_test_suite in ignored_tests:
Manuel Pégourié-Gonnardd36a37f2023-10-26 09:41:59 +0200133 for str_or_re in ignored_tests[test_suite]:
Manuel Pégourié-Gonnard881ce012023-10-18 10:22:07 +0200134 if name_matches_pattern(test_string, str_or_re):
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200135 ignored = True
Manuel Pégourié-Gonnard4da369f2023-10-18 09:40:32 +0200136
Pengyu Lv18908ec2023-11-28 12:11:52 +0800137 if not ignored and not suite_case in driver_outcomes.successes:
Pengyu Lv31a9b782023-11-23 14:15:37 +0800138 results.error("PASS -> SKIP/FAIL: {}", suite_case)
Pengyu Lv18908ec2023-11-28 12:11:52 +0800139 if ignored and suite_case in driver_outcomes.successes:
Pengyu Lv31a9b782023-11-23 14:15:37 +0800140 results.error("uselessly ignored: {}", suite_case)
Manuel Pégourié-Gonnard371165a2023-10-18 12:44:54 +0200141
Valerio Setti781c2342023-10-17 12:47:35 +0200142def analyze_outcomes(results: Results, outcomes, args):
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200143 """Run all analyses on the given outcome collection."""
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100144 analyze_coverage(results, outcomes, args['allow_list'],
145 args['full_coverage'])
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200146
147def read_outcome_file(outcome_file):
148 """Parse an outcome file and return an outcome collection.
149
Pengyu Lv31a9b782023-11-23 14:15:37 +0800150An outcome collection is a dictionary presentation of the outcome file:
151```
152outcomes = {
Pengyu Lv18908ec2023-11-28 12:11:52 +0800153 "<component>": ComponentOutcomes,
Pengyu Lv31a9b782023-11-23 14:15:37 +0800154 ...
155}
Pengyu Lv18908ec2023-11-28 12:11:52 +0800156
157CompoentOutcomes is a named tuple which is defined as:
158
159ComponentOutcomes(
160 successes = {
161 <suite_case>,
162 ...
163 },
164 failures = {
165 <suite_case>,
166 ...
167 }
168)
169
Pengyu Lv31a9b782023-11-23 14:15:37 +0800170suite_case = "<suite>;<case>"
171```
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200172"""
173 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 Lv31a9b782023-11-23 14:15:37 +0800177 suite_case = ';'.join([suite, case])
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800178 if component not in outcomes:
Pengyu Lv18908ec2023-11-28 12:11:52 +0800179 outcomes[component] = ComponentOutcomes(set(), set())
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200180 if result == 'PASS':
Pengyu Lv18908ec2023-11-28 12:11:52 +0800181 outcomes[component].successes.add(suite_case)
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200182 elif result == 'FAIL':
Pengyu Lv18908ec2023-11-28 12:11:52 +0800183 outcomes[component].failures.add(suite_case)
Pengyu Lva4428582023-11-22 19:02:15 +0800184
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200185 return outcomes
186
Pengyu Lv20e3ca32023-11-28 15:30:03 +0800187def do_analyze_coverage(results: Results, outcomes, args):
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100188 """Perform coverage analysis."""
Valerio Setti2cff8202023-10-18 14:36:47 +0200189 results.new_section("Analyze coverage")
Valerio Setti781c2342023-10-17 12:47:35 +0200190 analyze_outcomes(results, outcomes, args)
Przemek Stekiel4e955902022-10-21 13:42:08 +0200191
Pengyu Lv20e3ca32023-11-28 15:30:03 +0800192def do_analyze_driver_vs_reference(results: Results, outcomes, args):
Przemek Stekiel4e955902022-10-21 13:42:08 +0200193 """Perform driver vs reference analyze."""
Valerio Setti2cff8202023-10-18 14:36:47 +0200194 results.new_section("Analyze driver {} vs reference {}",
195 args['component_driver'], args['component_ref'])
Valerio Settib0c618e2023-10-16 14:19:49 +0200196
Valerio Setti3002c992023-01-18 17:28:36 +0100197 ignored_suites = ['test_suite_' + x for x in args['ignored_suites']]
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100198
Valerio Setti781c2342023-10-17 12:47:35 +0200199 analyze_driver_vs_reference(results, outcomes,
200 args['component_ref'], args['component_driver'],
201 ignored_suites, args['ignored_tests'])
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200202
Przemek Stekiel6856f4c2022-11-09 10:50:29 +0100203# List of tasks with a function that can handle this task and additional arguments if required
Valerio Settidfd7ca62023-10-09 16:30:11 +0200204KNOWN_TASKS = {
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200205 'analyze_coverage': {
206 'test_function': do_analyze_coverage,
Tomás González07bdcc22023-08-11 14:59:03 +0100207 'args': {
Tomás González358c6c62023-08-14 15:43:46 +0100208 'allow_list': [
Tomás González50223112023-08-22 09:52:06 +0100209 # Algorithm not supported yet
210 'test_suite_psa_crypto_metadata;Asymmetric signature: pure EdDSA',
211 # Algorithm not supported yet
212 'test_suite_psa_crypto_metadata;Cipher: XTS',
Tomás Gonzálezd43cab32023-08-24 09:12:40 +0100213 ],
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100214 'full_coverage': False,
Tomás González07bdcc22023-08-11 14:59:03 +0100215 }
Tomás Gonzálezd43cab32023-08-24 09:12:40 +0100216 },
Valerio Settia2663322023-03-24 08:20:18 +0100217 # There are 2 options to use analyze_driver_vs_reference_xxx locally:
218 # 1. Run tests and then analysis:
219 # - tests/scripts/all.sh --outcome-file "$PWD/out.csv" <component_ref> <component_driver>
220 # - tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
221 # 2. Let this script run both automatically:
222 # - tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200223 'analyze_driver_vs_reference_hash': {
224 'test_function': do_analyze_driver_vs_reference,
225 'args': {
Przemek Stekiel51f30ff2022-11-09 12:07:29 +0100226 'component_ref': 'test_psa_crypto_config_reference_hash_use_psa',
227 'component_driver': 'test_psa_crypto_config_accel_hash_use_psa',
Manuel Pégourié-Gonnard10e39632022-12-29 12:29:09 +0100228 'ignored_suites': [
229 'shax', 'mdx', # the software implementations that are being excluded
Manuel Pégourié-Gonnard7d381f52023-03-17 15:13:08 +0100230 'md.psa', # purposefully depends on whether drivers are present
Gilles Peskine35b49c42023-10-04 12:28:41 +0200231 'psa_crypto_low_hash.generated', # testing the builtins
Valerio Setti3002c992023-01-18 17:28:36 +0100232 ],
233 'ignored_tests': {
234 }
235 }
236 },
Valerio Settib6b301f2023-10-04 12:05:05 +0200237 'analyze_driver_vs_reference_cipher_aead': {
238 'test_function': do_analyze_driver_vs_reference,
239 'args': {
240 'component_ref': 'test_psa_crypto_config_reference_cipher_aead',
241 'component_driver': 'test_psa_crypto_config_accel_cipher_aead',
Valerio Setti507e08f2023-10-26 09:44:06 +0200242 # Modules replaced by drivers.
Valerio Settib6b301f2023-10-04 12:05:05 +0200243 'ignored_suites': [
Valerio Setti507e08f2023-10-26 09:44:06 +0200244 # low-level (block/stream) cipher modules
245 'aes', 'aria', 'camellia', 'des', 'chacha20',
246 # AEAD modes
247 'ccm', 'chachapoly', 'cmac', 'gcm',
248 # The Cipher abstraction layer
249 'cipher',
Valerio Settib6b301f2023-10-04 12:05:05 +0200250 ],
251 'ignored_tests': {
Valerio Setti507e08f2023-10-26 09:44:06 +0200252 # PEM decryption is not supported so far.
253 # The rest of PEM (write, unencrypted read) works though.
Valerio Setti7448cee2023-10-04 15:46:42 +0200254 'test_suite_pem': [
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200255 re.compile(r'PEM read .*(AES|DES|\bencrypt).*'),
Valerio Setti7448cee2023-10-04 15:46:42 +0200256 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200257 # Following tests depend on AES_C/DES_C but are not about
258 # them really, just need to know some error code is there.
Valerio Setti7448cee2023-10-04 15:46:42 +0200259 'test_suite_error': [
260 'Low and high error',
261 'Single low error'
262 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200263 # Similar to test_suite_error above.
Valerio Setti7448cee2023-10-04 15:46:42 +0200264 'test_suite_version': [
265 'Check for MBEDTLS_AES_C when already present',
Valerio Setti93941442023-10-13 09:19:52 +0200266 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200267 # The en/decryption part of PKCS#12 is not supported so far.
268 # The rest of PKCS#12 (key derivation) works though.
Valerio Setti93941442023-10-13 09:19:52 +0200269 'test_suite_pkcs12': [
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200270 re.compile(r'PBE Encrypt, .*'),
271 re.compile(r'PBE Decrypt, .*'),
Valerio Setti93941442023-10-13 09:19:52 +0200272 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200273 # The en/decryption part of PKCS#5 is not supported so far.
274 # The rest of PKCS#5 (PBKDF2) works though.
Valerio Setti93941442023-10-13 09:19:52 +0200275 'test_suite_pkcs5': [
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200276 re.compile(r'PBES2 Encrypt, .*'),
277 re.compile(r'PBES2 Decrypt .*'),
Valerio Setti93941442023-10-13 09:19:52 +0200278 ],
Valerio Setti507e08f2023-10-26 09:44:06 +0200279 # Encrypted keys are not supported so far.
Valerio Setti5cd18f92023-10-13 15:14:07 +0200280 # pylint: disable=line-too-long
Valerio Setti93941442023-10-13 09:19:52 +0200281 'test_suite_pkparse': [
282 'Key ASN1 (Encrypted key PKCS12, trailing garbage data)',
283 'Key ASN1 (Encrypted key PKCS5, trailing garbage data)',
Manuel Pégourié-Gonnardcd84a292023-10-27 09:24:44 +0200284 re.compile(r'Parse RSA Key .*\(PKCS#8 encrypted .*\)'),
Valerio Setti93941442023-10-13 09:19:52 +0200285 ],
Valerio Settib6b301f2023-10-04 12:05:05 +0200286 }
287 }
288 },
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200289 'analyze_driver_vs_reference_ecp_light_only': {
Valerio Setti42d5f192023-03-20 13:54:41 +0100290 'test_function': do_analyze_driver_vs_reference,
291 'args': {
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200292 'component_ref': 'test_psa_crypto_config_reference_ecc_ecp_light_only',
293 'component_driver': 'test_psa_crypto_config_accel_ecc_ecp_light_only',
Valerio Setti42d5f192023-03-20 13:54:41 +0100294 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200295 # Modules replaced by drivers
296 'ecdsa', 'ecdh', 'ecjpake',
Valerio Setti42d5f192023-03-20 13:54:41 +0100297 ],
298 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200299 # This test wants a legacy function that takes f_rng, p_rng
300 # arguments, and uses legacy ECDSA for that. The test is
301 # really about the wrapper around the PSA RNG, not ECDSA.
Valerio Setti42d5f192023-03-20 13:54:41 +0100302 'test_suite_random': [
303 'PSA classic wrapper: ECDSA signature (SECP256R1)',
304 ],
Valerio Setti0c477d32023-04-07 15:54:20 +0200305 # In the accelerated test ECP_C is not set (only ECP_LIGHT is)
306 # so we must ignore disparities in the tests for which ECP_C
307 # is required.
308 'test_suite_ecp': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200309 re.compile(r'ECP check public-private .*'),
310 re.compile(r'ECP gen keypair .*'),
311 re.compile(r'ECP point muladd .*'),
312 re.compile(r'ECP point multiplication .*'),
313 re.compile(r'ECP test vectors .*'),
Valerio Setti482a0b92023-08-18 15:55:10 +0200314 ],
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200315 'test_suite_ssl': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200316 # This deprecated function is only present when ECP_C is On.
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200317 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
318 ],
Valerio Setti5f540202023-06-30 17:20:49 +0200319 }
Valerio Setti42d5f192023-03-20 13:54:41 +0100320 }
321 },
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200322 'analyze_driver_vs_reference_no_ecp_at_all': {
Valerio Settie618cb02023-04-12 14:59:16 +0200323 'test_function': do_analyze_driver_vs_reference,
324 'args': {
Valerio Setti4d25a8d2023-06-14 10:33:10 +0200325 'component_ref': 'test_psa_crypto_config_reference_ecc_no_ecp_at_all',
326 'component_driver': 'test_psa_crypto_config_accel_ecc_no_ecp_at_all',
Valerio Settie618cb02023-04-12 14:59:16 +0200327 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200328 # Modules replaced by drivers
329 'ecp', 'ecdsa', 'ecdh', 'ecjpake',
Valerio Settie618cb02023-04-12 14:59:16 +0200330 ],
331 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200332 # See ecp_light_only
Valerio Settie618cb02023-04-12 14:59:16 +0200333 'test_suite_random': [
334 'PSA classic wrapper: ECDSA signature (SECP256R1)',
335 ],
Valerio Settiaddeee42023-06-14 10:46:55 +0200336 'test_suite_pkparse': [
Valerio Setti5bd25232023-06-19 19:32:14 +0200337 # When PK_PARSE_C and ECP_C are defined then PK_PARSE_EC_COMPRESSED
338 # is automatically enabled in build_info.h (backward compatibility)
339 # even if it is disabled in config_psa_crypto_no_ecp_at_all(). As a
340 # consequence compressed points are supported in the reference
341 # component but not in the accelerated one, so they should be skipped
342 # while checking driver's coverage.
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200343 re.compile(r'Parse EC Key .*compressed\)'),
344 re.compile(r'Parse Public EC Key .*compressed\)'),
Valerio Settiaddeee42023-06-14 10:46:55 +0200345 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200346 # See ecp_light_only
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200347 'test_suite_ssl': [
348 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
349 ],
Valerio Settie618cb02023-04-12 14:59:16 +0200350 }
351 }
352 },
Valerio Setti307810b2023-08-15 10:12:25 +0200353 'analyze_driver_vs_reference_ecc_no_bignum': {
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200354 'test_function': do_analyze_driver_vs_reference,
355 'args': {
356 'component_ref': 'test_psa_crypto_config_reference_ecc_no_bignum',
357 'component_driver': 'test_psa_crypto_config_accel_ecc_no_bignum',
358 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200359 # Modules replaced by drivers
360 'ecp', 'ecdsa', 'ecdh', 'ecjpake',
361 'bignum_core', 'bignum_random', 'bignum_mod', 'bignum_mod_raw',
362 'bignum.generated', 'bignum.misc',
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200363 ],
364 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200365 # See ecp_light_only
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200366 'test_suite_random': [
367 'PSA classic wrapper: ECDSA signature (SECP256R1)',
368 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200369 # See no_ecp_at_all
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200370 'test_suite_pkparse': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200371 re.compile(r'Parse EC Key .*compressed\)'),
372 re.compile(r'Parse Public EC Key .*compressed\)'),
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200373 ],
Valerio Setti9b3dbcc2023-07-26 18:00:31 +0200374 'test_suite_asn1parse': [
Valerio Setti9b3dbcc2023-07-26 18:00:31 +0200375 'INTEGER too large for mpi',
376 ],
377 'test_suite_asn1write': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200378 re.compile(r'ASN.1 Write mpi.*'),
Valerio Setti9b3dbcc2023-07-26 18:00:31 +0200379 ],
Valerio Settie0be95e2023-08-01 09:07:43 +0200380 'test_suite_debug': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200381 re.compile(r'Debug print mbedtls_mpi.*'),
Valerio Settie0be95e2023-08-01 09:07:43 +0200382 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200383 # See ecp_light_only
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200384 'test_suite_ssl': [
385 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
386 ],
Manuel Pégourié-Gonnardabd00d02023-06-12 17:51:33 +0200387 }
388 }
389 },
Valerio Setti307810b2023-08-15 10:12:25 +0200390 'analyze_driver_vs_reference_ecc_ffdh_no_bignum': {
391 'test_function': do_analyze_driver_vs_reference,
392 'args': {
393 'component_ref': 'test_psa_crypto_config_reference_ecc_ffdh_no_bignum',
394 'component_driver': 'test_psa_crypto_config_accel_ecc_ffdh_no_bignum',
395 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200396 # Modules replaced by drivers
397 'ecp', 'ecdsa', 'ecdh', 'ecjpake', 'dhm',
398 'bignum_core', 'bignum_random', 'bignum_mod', 'bignum_mod_raw',
399 'bignum.generated', 'bignum.misc',
Valerio Setti307810b2023-08-15 10:12:25 +0200400 ],
401 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200402 # See ecp_light_only
Valerio Setti307810b2023-08-15 10:12:25 +0200403 'test_suite_random': [
404 'PSA classic wrapper: ECDSA signature (SECP256R1)',
405 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200406 # See no_ecp_at_all
Valerio Setti307810b2023-08-15 10:12:25 +0200407 'test_suite_pkparse': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200408 re.compile(r'Parse EC Key .*compressed\)'),
409 re.compile(r'Parse Public EC Key .*compressed\)'),
Valerio Setti307810b2023-08-15 10:12:25 +0200410 ],
411 'test_suite_asn1parse': [
Valerio Setti307810b2023-08-15 10:12:25 +0200412 'INTEGER too large for mpi',
413 ],
414 'test_suite_asn1write': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200415 re.compile(r'ASN.1 Write mpi.*'),
Valerio Setti307810b2023-08-15 10:12:25 +0200416 ],
417 'test_suite_debug': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200418 re.compile(r'Debug print mbedtls_mpi.*'),
Valerio Setti307810b2023-08-15 10:12:25 +0200419 ],
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200420 # See ecp_light_only
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +0200421 'test_suite_ssl': [
422 'Test configuration of groups for DHE through mbedtls_ssl_conf_curves()',
423 ],
Valerio Setti307810b2023-08-15 10:12:25 +0200424 }
425 }
426 },
Przemek Stekiel85b64422023-05-26 09:55:23 +0200427 'analyze_driver_vs_reference_ffdh_alg': {
428 'test_function': do_analyze_driver_vs_reference,
429 'args': {
430 'component_ref': 'test_psa_crypto_config_reference_ffdh',
431 'component_driver': 'test_psa_crypto_config_accel_ffdh',
Przemek Stekiel84f4ff12023-07-04 12:35:31 +0200432 'ignored_suites': ['dhm'],
Przemek Stekiel565353e2023-07-05 11:07:07 +0200433 'ignored_tests': {}
Przemek Stekiel85b64422023-05-26 09:55:23 +0200434 }
435 },
Valerio Settif01d6482023-08-04 13:51:18 +0200436 'analyze_driver_vs_reference_tfm_config': {
437 'test_function': do_analyze_driver_vs_reference,
438 'args': {
439 'component_ref': 'test_tfm_config',
440 'component_driver': 'test_tfm_config_p256m_driver_accel_ec',
Manuel Pégourié-Gonnarde9d97972023-08-08 18:34:47 +0200441 'ignored_suites': [
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200442 # Modules replaced by drivers
Yanray Wang57790962023-10-31 13:39:07 +0800443 'asn1parse', 'asn1write',
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200444 'ecp', 'ecdsa', 'ecdh', 'ecjpake',
445 'bignum_core', 'bignum_random', 'bignum_mod', 'bignum_mod_raw',
446 'bignum.generated', 'bignum.misc',
Manuel Pégourié-Gonnarde9d97972023-08-08 18:34:47 +0200447 ],
Valerio Settif01d6482023-08-04 13:51:18 +0200448 'ignored_tests': {
Manuel Pégourié-Gonnard4fd5a6a2023-10-20 10:21:09 +0200449 # See ecp_light_only
Manuel Pégourié-Gonnarde9d97972023-08-08 18:34:47 +0200450 'test_suite_random': [
451 'PSA classic wrapper: ECDSA signature (SECP256R1)',
452 ],
Valerio Settif01d6482023-08-04 13:51:18 +0200453 }
454 }
455 }
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200456}
Przemek Stekiel4d13c832022-10-26 16:11:26 +0200457
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200458def main():
Valerio Settif075e472023-10-17 11:03:16 +0200459 main_results = Results()
Valerio Settiaaef0bc2023-10-10 09:42:13 +0200460
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200461 try:
462 parser = argparse.ArgumentParser(description=__doc__)
Przemek Stekiel58bbc232022-10-24 08:10:10 +0200463 parser.add_argument('outcomes', metavar='OUTCOMES.CSV',
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200464 help='Outcome file to analyze')
Valerio Settidfd7ca62023-10-09 16:30:11 +0200465 parser.add_argument('specified_tasks', default='all', nargs='?',
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100466 help='Analysis to be done. By default, run all tasks. '
467 'With one or more TASK, run only those. '
468 'TASK can be the name of a single task or '
Przemek Stekiel85c54ea2022-11-17 11:50:23 +0100469 'comma/space-separated list of tasks. ')
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100470 parser.add_argument('--list', action='store_true',
471 help='List all available tasks and exit.')
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100472 parser.add_argument('--require-full-coverage', action='store_true',
473 dest='full_coverage', help="Require all available "
474 "test cases to be executed and issue an error "
475 "otherwise. This flag is ignored if 'task' is "
476 "neither 'all' nor 'analyze_coverage'")
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200477 options = parser.parse_args()
Przemek Stekiel4e955902022-10-21 13:42:08 +0200478
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100479 if options.list:
Valerio Settidfd7ca62023-10-09 16:30:11 +0200480 for task in KNOWN_TASKS:
Valerio Setti5329ff02023-10-17 09:44:36 +0200481 print(task)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100482 sys.exit(0)
483
Valerio Settidfd7ca62023-10-09 16:30:11 +0200484 if options.specified_tasks == 'all':
485 tasks_list = KNOWN_TASKS.keys()
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100486 else:
Valerio Settidfd7ca62023-10-09 16:30:11 +0200487 tasks_list = re.split(r'[, ]+', options.specified_tasks)
Valerio Settidfd7ca62023-10-09 16:30:11 +0200488 for task in tasks_list:
489 if task not in KNOWN_TASKS:
Manuel Pégourié-Gonnard62d61312023-10-20 10:51:57 +0200490 sys.stderr.write('invalid task: {}\n'.format(task))
Valerio Settifb2750e2023-10-17 10:11:45 +0200491 sys.exit(2)
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100492
Valerio Settidfd7ca62023-10-09 16:30:11 +0200493 KNOWN_TASKS['analyze_coverage']['args']['full_coverage'] = options.full_coverage
Przemek Stekiel992de3c2022-11-09 13:54:49 +0100494
Pengyu Lvdd1d6a72023-11-27 17:57:31 +0800495 # If the outcome file exists, parse it once and share the result
496 # among tasks to improve performance.
Pengyu Lv20e3ca32023-11-28 15:30:03 +0800497 # Otherwise, it will be generated by execute_reference_driver_tests.
498 if not os.path.exists(options.outcomes):
499 if len(tasks_list) > 1:
500 sys.stderr.write("mutiple tasks found, please provide a valid outcomes file.\n")
501 sys.exit(2)
502
503 task_name = tasks_list[0]
504 task = KNOWN_TASKS[task_name]
505 if task['test_function'] != do_analyze_driver_vs_reference:
506 sys.stderr.write("please provide valid outcomes file for {}.\n".format(task_name))
507 sys.exit(2)
508
509 execute_reference_driver_tests(main_results,
510 task['args']['component_ref'],
511 task['args']['component_driver'],
512 options.outcomes)
513
514 outcomes = read_outcome_file(options.outcomes)
Pengyu Lva6cf5d62023-11-22 11:35:21 +0800515
Valerio Settifb2750e2023-10-17 10:11:45 +0200516 for task in tasks_list:
517 test_function = KNOWN_TASKS[task]['test_function']
518 test_args = KNOWN_TASKS[task]['args']
Pengyu Lv20e3ca32023-11-28 15:30:03 +0800519 test_function(main_results, outcomes, test_args)
Tomás Gonzálezb401e112023-08-11 15:22:04 +0100520
Valerio Settif6f64cf2023-10-17 12:28:26 +0200521 main_results.info("Overall results: {} warnings and {} errors",
522 main_results.warning_count, main_results.error_count)
Przemek Stekiel4e955902022-10-21 13:42:08 +0200523
Valerio Setti8d178be2023-10-17 12:23:55 +0200524 sys.exit(0 if (main_results.error_count == 0) else 1)
Valerio Settiaaef0bc2023-10-10 09:42:13 +0200525
Gilles Peskine15c2cbf2020-06-25 18:36:28 +0200526 except Exception: # pylint: disable=broad-except
527 # Print the backtrace and exit explicitly with our chosen status.
528 traceback.print_exc()
529 sys.exit(120)
530
531if __name__ == '__main__':
532 main()