blob: ca2a6fb9f6a2f891a6006f4129e5ee5f37945cd0 [file] [log] [blame]
Gilles Peskine09940492021-01-26 22:16:30 +01001#!/usr/bin/env python3
2"""Generate test data for PSA cryptographic mechanisms.
Gilles Peskine0298bda2021-03-10 02:34:37 +01003
4With no arguments, generate all test data. With non-option arguments,
5generate only the specified files.
Gilles Peskine09940492021-01-26 22:16:30 +01006"""
7
8# Copyright The Mbed TLS Contributors
Tomás González5fae5602023-11-13 11:45:12 +00009# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine09940492021-01-26 22:16:30 +010010
Gilles Peskinef8b6b502022-03-15 17:26:33 +010011import enum
Gilles Peskine14e428f2021-01-26 22:19:21 +010012import re
Gilles Peskine09940492021-01-26 22:16:30 +010013import sys
Werner Lewisdcad1e92022-08-24 11:30:03 +010014from typing import Callable, Dict, FrozenSet, Iterable, Iterator, List, Optional
Gilles Peskine09940492021-01-26 22:16:30 +010015
16import scripts_path # pylint: disable=unused-import
Tomás González2bff1bf2023-10-30 15:29:23 +000017from mbedtls_dev import crypto_data_tests
Gilles Peskine14e428f2021-01-26 22:19:21 +010018from mbedtls_dev import crypto_knowledge
Tomás González734d22c2023-10-30 15:15:45 +000019from mbedtls_dev import macro_collector #pylint: disable=unused-import
20from mbedtls_dev import psa_information
Gilles Peskine897dff92021-03-10 15:03:44 +010021from mbedtls_dev import psa_storage
Gilles Peskine14e428f2021-01-26 22:19:21 +010022from mbedtls_dev import test_case
Gilles Peskine69feebd2022-09-16 21:41:47 +020023from mbedtls_dev import test_data_generation
Gilles Peskine09940492021-01-26 22:16:30 +010024
Gilles Peskine14e428f2021-01-26 22:19:21 +010025
Przemyslaw Stekield6ead7c2021-10-11 10:15:25 +020026def test_case_for_key_type_not_supported(
Gilles Peskineb94ea512021-03-10 02:12:08 +010027 verb: str, key_type: str, bits: int,
28 dependencies: List[str],
29 *args: str,
30 param_descr: str = ''
31) -> test_case.TestCase:
32 """Return one test case exercising a key creation method
33 for an unsupported key type or size.
34 """
Tomás González734d22c2023-10-30 15:15:45 +000035 psa_information.hack_dependencies_not_implemented(dependencies)
Gilles Peskineb94ea512021-03-10 02:12:08 +010036 tc = test_case.TestCase()
Gilles Peskined79aef52022-03-17 23:42:25 +010037 short_key_type = crypto_knowledge.short_expression(key_type)
Gilles Peskineb94ea512021-03-10 02:12:08 +010038 adverb = 'not' if dependencies else 'never'
39 if param_descr:
40 adverb = param_descr + ' ' + adverb
Przemyslaw Stekield6ead7c2021-10-11 10:15:25 +020041 tc.set_description('PSA {} {} {}-bit {} supported'
42 .format(verb, short_key_type, bits, adverb))
Gilles Peskinec3b261a2024-04-10 17:19:04 +020043 tc.set_dependencies(sorted(dependencies))
Przemyslaw Stekield6ead7c2021-10-11 10:15:25 +020044 tc.set_function(verb + '_not_supported')
45 tc.set_arguments([key_type] + list(args))
46 return tc
47
Gilles Peskine4fa76bd2022-12-15 22:14:28 +010048class KeyTypeNotSupported:
49 """Generate test cases for when a key type is not supported."""
Gilles Peskineb94ea512021-03-10 02:12:08 +010050
Tomás González734d22c2023-10-30 15:15:45 +000051 def __init__(self, info: psa_information.Information) -> None:
Gilles Peskineb94ea512021-03-10 02:12:08 +010052 self.constructors = info.constructors
Gilles Peskine14e428f2021-01-26 22:19:21 +010053
Gilles Peskine60b29fe2021-02-16 14:06:50 +010054 ALWAYS_SUPPORTED = frozenset([
55 'PSA_KEY_TYPE_DERIVE',
56 'PSA_KEY_TYPE_RAW_DATA',
57 ])
Gilles Peskine14e428f2021-01-26 22:19:21 +010058 def test_cases_for_key_type_not_supported(
Gilles Peskine60b29fe2021-02-16 14:06:50 +010059 self,
Gilles Peskineaf172842021-01-27 18:24:48 +010060 kt: crypto_knowledge.KeyType,
61 param: Optional[int] = None,
62 param_descr: str = '',
Gilles Peskine3d778392021-02-17 15:11:05 +010063 ) -> Iterator[test_case.TestCase]:
Przemyslaw Stekiel32a8b842021-10-18 14:58:20 +020064 """Return test cases exercising key creation when the given type is unsupported.
Gilles Peskineaf172842021-01-27 18:24:48 +010065
66 If param is present and not None, emit test cases conditioned on this
67 parameter not being supported. If it is absent or None, emit test cases
Przemyslaw Stekiel32a8b842021-10-18 14:58:20 +020068 conditioned on the base type not being supported.
Gilles Peskineaf172842021-01-27 18:24:48 +010069 """
Gilles Peskine60b29fe2021-02-16 14:06:50 +010070 if kt.name in self.ALWAYS_SUPPORTED:
71 # Don't generate test cases for key types that are always supported.
72 # They would be skipped in all configurations, which is noise.
Gilles Peskine3d778392021-02-17 15:11:05 +010073 return
Gilles Peskineaf172842021-01-27 18:24:48 +010074 import_dependencies = [('!' if param is None else '') +
Tomás González734d22c2023-10-30 15:15:45 +000075 psa_information.psa_want_symbol(kt.name)]
Gilles Peskineaf172842021-01-27 18:24:48 +010076 if kt.params is not None:
77 import_dependencies += [('!' if param == i else '') +
Tomás González734d22c2023-10-30 15:15:45 +000078 psa_information.psa_want_symbol(sym)
Gilles Peskineaf172842021-01-27 18:24:48 +010079 for i, sym in enumerate(kt.params)]
Gilles Peskine14e428f2021-01-26 22:19:21 +010080 if kt.name.endswith('_PUBLIC_KEY'):
81 generate_dependencies = []
82 else:
83 generate_dependencies = import_dependencies
Gilles Peskine14e428f2021-01-26 22:19:21 +010084 for bits in kt.sizes_to_test():
Przemyslaw Stekield6ead7c2021-10-11 10:15:25 +020085 yield test_case_for_key_type_not_supported(
Gilles Peskine7f756872021-02-16 12:13:12 +010086 'import', kt.expression, bits,
Tomás González734d22c2023-10-30 15:15:45 +000087 psa_information.finish_family_dependencies(import_dependencies, bits),
Gilles Peskineaf172842021-01-27 18:24:48 +010088 test_case.hex_string(kt.key_material(bits)),
89 param_descr=param_descr,
Gilles Peskine3d778392021-02-17 15:11:05 +010090 )
Gilles Peskineaf172842021-01-27 18:24:48 +010091 if not generate_dependencies and param is not None:
92 # If generation is impossible for this key type, rather than
93 # supported or not depending on implementation capabilities,
94 # only generate the test case once.
95 continue
Przemyslaw Stekiel1ab3a5c2021-11-02 10:50:44 +010096 # For public key we expect that key generation fails with
97 # INVALID_ARGUMENT. It is handled by KeyGenerate class.
Gilles Peskine989c13d2022-03-17 12:52:24 +010098 if not kt.is_public():
Przemyslaw Stekield6ead7c2021-10-11 10:15:25 +020099 yield test_case_for_key_type_not_supported(
100 'generate', kt.expression, bits,
Tomás González734d22c2023-10-30 15:15:45 +0000101 psa_information.finish_family_dependencies(generate_dependencies, bits),
Przemyslaw Stekield6ead7c2021-10-11 10:15:25 +0200102 str(bits),
103 param_descr=param_descr,
104 )
Gilles Peskine14e428f2021-01-26 22:19:21 +0100105 # To be added: derive
Gilles Peskine14e428f2021-01-26 22:19:21 +0100106
Gilles Peskineb93f8542021-04-19 13:50:25 +0200107 ECC_KEY_TYPES = ('PSA_KEY_TYPE_ECC_KEY_PAIR',
108 'PSA_KEY_TYPE_ECC_PUBLIC_KEY')
109
Gilles Peskine3d778392021-02-17 15:11:05 +0100110 def test_cases_for_not_supported(self) -> Iterator[test_case.TestCase]:
Gilles Peskine14e428f2021-01-26 22:19:21 +0100111 """Generate test cases that exercise the creation of keys of unsupported types."""
Gilles Peskine14e428f2021-01-26 22:19:21 +0100112 for key_type in sorted(self.constructors.key_types):
Gilles Peskineb93f8542021-04-19 13:50:25 +0200113 if key_type in self.ECC_KEY_TYPES:
114 continue
Gilles Peskine14e428f2021-01-26 22:19:21 +0100115 kt = crypto_knowledge.KeyType(key_type)
Gilles Peskine3d778392021-02-17 15:11:05 +0100116 yield from self.test_cases_for_key_type_not_supported(kt)
Gilles Peskineaf172842021-01-27 18:24:48 +0100117 for curve_family in sorted(self.constructors.ecc_curves):
Gilles Peskineb93f8542021-04-19 13:50:25 +0200118 for constr in self.ECC_KEY_TYPES:
Gilles Peskineaf172842021-01-27 18:24:48 +0100119 kt = crypto_knowledge.KeyType(constr, [curve_family])
Gilles Peskine3d778392021-02-17 15:11:05 +0100120 yield from self.test_cases_for_key_type_not_supported(
Gilles Peskineaf172842021-01-27 18:24:48 +0100121 kt, param_descr='type')
Gilles Peskine3d778392021-02-17 15:11:05 +0100122 yield from self.test_cases_for_key_type_not_supported(
Gilles Peskineaf172842021-01-27 18:24:48 +0100123 kt, 0, param_descr='curve')
Gilles Peskineb94ea512021-03-10 02:12:08 +0100124
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200125def test_case_for_key_generation(
126 key_type: str, bits: int,
127 dependencies: List[str],
128 *args: str,
Przemyslaw Stekielc03b7c52021-10-20 11:59:50 +0200129 result: str = ''
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200130) -> test_case.TestCase:
131 """Return one test case exercising a key generation.
132 """
Tomás González734d22c2023-10-30 15:15:45 +0000133 psa_information.hack_dependencies_not_implemented(dependencies)
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200134 tc = test_case.TestCase()
Gilles Peskined79aef52022-03-17 23:42:25 +0100135 short_key_type = crypto_knowledge.short_expression(key_type)
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200136 tc.set_description('PSA {} {}-bit'
Przemyslaw Stekielc03b7c52021-10-20 11:59:50 +0200137 .format(short_key_type, bits))
Gilles Peskinec3b261a2024-04-10 17:19:04 +0200138 tc.set_dependencies(sorted(dependencies))
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200139 tc.set_function('generate_key')
Przemyslaw Stekiel1ab3a5c2021-11-02 10:50:44 +0100140 tc.set_arguments([key_type] + list(args) + [result])
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200141
142 return tc
143
144class KeyGenerate:
145 """Generate positive and negative (invalid argument) test cases for key generation."""
146
Tomás González734d22c2023-10-30 15:15:45 +0000147 def __init__(self, info: psa_information.Information) -> None:
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200148 self.constructors = info.constructors
149
Przemyslaw Stekielc03b7c52021-10-20 11:59:50 +0200150 ECC_KEY_TYPES = ('PSA_KEY_TYPE_ECC_KEY_PAIR',
151 'PSA_KEY_TYPE_ECC_PUBLIC_KEY')
152
Przemyslaw Stekiel1ab3a5c2021-11-02 10:50:44 +0100153 @staticmethod
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200154 def test_cases_for_key_type_key_generation(
Przemyslaw Stekielc03b7c52021-10-20 11:59:50 +0200155 kt: crypto_knowledge.KeyType
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200156 ) -> Iterator[test_case.TestCase]:
157 """Return test cases exercising key generation.
158
159 All key types can be generated except for public keys. For public key
160 PSA_ERROR_INVALID_ARGUMENT status is expected.
161 """
162 result = 'PSA_SUCCESS'
163
Tomás González734d22c2023-10-30 15:15:45 +0000164 import_dependencies = [psa_information.psa_want_symbol(kt.name)]
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200165 if kt.params is not None:
Tomás González734d22c2023-10-30 15:15:45 +0000166 import_dependencies += [psa_information.psa_want_symbol(sym)
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200167 for i, sym in enumerate(kt.params)]
168 if kt.name.endswith('_PUBLIC_KEY'):
Przemyslaw Stekiel1ab3a5c2021-11-02 10:50:44 +0100169 # The library checks whether the key type is a public key generically,
170 # before it reaches a point where it needs support for the specific key
171 # type, so it returns INVALID_ARGUMENT for unsupported public key types.
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200172 generate_dependencies = []
173 result = 'PSA_ERROR_INVALID_ARGUMENT'
174 else:
175 generate_dependencies = import_dependencies
Przemyslaw Stekiel1ab3a5c2021-11-02 10:50:44 +0100176 if kt.name == 'PSA_KEY_TYPE_RSA_KEY_PAIR':
Przemyslaw Stekiel08101082021-10-22 10:39:56 +0200177 generate_dependencies.append("MBEDTLS_GENPRIME")
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200178 for bits in kt.sizes_to_test():
179 yield test_case_for_key_generation(
180 kt.expression, bits,
Tomás González734d22c2023-10-30 15:15:45 +0000181 psa_information.finish_family_dependencies(generate_dependencies, bits),
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200182 str(bits),
Przemyslaw Stekielc03b7c52021-10-20 11:59:50 +0200183 result
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200184 )
185
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200186 def test_cases_for_key_generation(self) -> Iterator[test_case.TestCase]:
187 """Generate test cases that exercise the generation of keys."""
188 for key_type in sorted(self.constructors.key_types):
189 if key_type in self.ECC_KEY_TYPES:
190 continue
191 kt = crypto_knowledge.KeyType(key_type)
192 yield from self.test_cases_for_key_type_key_generation(kt)
193 for curve_family in sorted(self.constructors.ecc_curves):
194 for constr in self.ECC_KEY_TYPES:
195 kt = crypto_knowledge.KeyType(constr, [curve_family])
Przemyslaw Stekielc03b7c52021-10-20 11:59:50 +0200196 yield from self.test_cases_for_key_type_key_generation(kt)
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200197
Gilles Peskinec05158b2021-04-27 20:40:10 +0200198class OpFail:
199 """Generate test cases for operations that must fail."""
200 #pylint: disable=too-few-public-methods
201
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100202 class Reason(enum.Enum):
203 NOT_SUPPORTED = 0
204 INVALID = 1
205 INCOMPATIBLE = 2
Gilles Peskinec2fc2412021-04-29 21:56:59 +0200206 PUBLIC = 3
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100207
Tomás González734d22c2023-10-30 15:15:45 +0000208 def __init__(self, info: psa_information.Information) -> None:
Gilles Peskinec05158b2021-04-27 20:40:10 +0200209 self.constructors = info.constructors
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100210 key_type_expressions = self.constructors.generate_expressions(
211 sorted(self.constructors.key_types)
212 )
213 self.key_types = [crypto_knowledge.KeyType(kt_expr)
214 for kt_expr in key_type_expressions]
Gilles Peskinec05158b2021-04-27 20:40:10 +0200215
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100216 def make_test_case(
217 self,
218 alg: crypto_knowledge.Algorithm,
219 category: crypto_knowledge.AlgorithmCategory,
220 reason: 'Reason',
221 kt: Optional[crypto_knowledge.KeyType] = None,
222 not_deps: FrozenSet[str] = frozenset(),
223 ) -> test_case.TestCase:
224 """Construct a failure test case for a one-key or keyless operation."""
225 #pylint: disable=too-many-arguments,too-many-locals
Gilles Peskinea2180472021-04-27 21:03:43 +0200226 tc = test_case.TestCase()
Gilles Peskined79aef52022-03-17 23:42:25 +0100227 pretty_alg = alg.short_expression()
Gilles Peskined0964452021-04-29 21:35:03 +0200228 if reason == self.Reason.NOT_SUPPORTED:
229 short_deps = [re.sub(r'PSA_WANT_ALG_', r'', dep)
230 for dep in not_deps]
231 pretty_reason = '!' + '&'.join(sorted(short_deps))
232 else:
233 pretty_reason = reason.name.lower()
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100234 if kt:
235 key_type = kt.expression
Gilles Peskined79aef52022-03-17 23:42:25 +0100236 pretty_type = kt.short_expression()
Gilles Peskinea2180472021-04-27 21:03:43 +0200237 else:
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100238 key_type = ''
239 pretty_type = ''
240 tc.set_description('PSA {} {}: {}{}'
241 .format(category.name.lower(),
242 pretty_alg,
243 pretty_reason,
244 ' with ' + pretty_type if pretty_type else ''))
Tomás González734d22c2023-10-30 15:15:45 +0000245 dependencies = psa_information.automatic_dependencies(alg.base_expression, key_type)
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100246 for i, dep in enumerate(dependencies):
247 if dep in not_deps:
248 dependencies[i] = '!' + dep
Gilles Peskinec3b261a2024-04-10 17:19:04 +0200249 tc.set_dependencies(sorted(dependencies))
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100250 tc.set_function(category.name.lower() + '_fail')
David Horstmann4fc7e0e2023-01-24 18:53:15 +0000251 arguments = [] # type: List[str]
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100252 if kt:
253 key_material = kt.key_material(kt.sizes_to_test()[0])
254 arguments += [key_type, test_case.hex_string(key_material)]
255 arguments.append(alg.expression)
Gilles Peskinec2fc2412021-04-29 21:56:59 +0200256 if category.is_asymmetric():
257 arguments.append('1' if reason == self.Reason.PUBLIC else '0')
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100258 error = ('NOT_SUPPORTED' if reason == self.Reason.NOT_SUPPORTED else
259 'INVALID_ARGUMENT')
260 arguments.append('PSA_ERROR_' + error)
261 tc.set_arguments(arguments)
262 return tc
Gilles Peskinea2180472021-04-27 21:03:43 +0200263
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100264 def no_key_test_cases(
265 self,
266 alg: crypto_knowledge.Algorithm,
267 category: crypto_knowledge.AlgorithmCategory,
268 ) -> Iterator[test_case.TestCase]:
269 """Generate failure test cases for keyless operations with the specified algorithm."""
Gilles Peskine23cb12e2021-04-29 20:54:40 +0200270 if alg.can_do(category):
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100271 # Compatible operation, unsupported algorithm
Tomás González734d22c2023-10-30 15:15:45 +0000272 for dep in psa_information.automatic_dependencies(alg.base_expression):
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100273 yield self.make_test_case(alg, category,
274 self.Reason.NOT_SUPPORTED,
275 not_deps=frozenset([dep]))
276 else:
277 # Incompatible operation, supported algorithm
278 yield self.make_test_case(alg, category, self.Reason.INVALID)
279
280 def one_key_test_cases(
281 self,
282 alg: crypto_knowledge.Algorithm,
283 category: crypto_knowledge.AlgorithmCategory,
284 ) -> Iterator[test_case.TestCase]:
285 """Generate failure test cases for one-key operations with the specified algorithm."""
286 for kt in self.key_types:
287 key_is_compatible = kt.can_do(alg)
Gilles Peskine23cb12e2021-04-29 20:54:40 +0200288 if key_is_compatible and alg.can_do(category):
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100289 # Compatible key and operation, unsupported algorithm
Tomás González734d22c2023-10-30 15:15:45 +0000290 for dep in psa_information.automatic_dependencies(alg.base_expression):
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100291 yield self.make_test_case(alg, category,
292 self.Reason.NOT_SUPPORTED,
293 kt=kt, not_deps=frozenset([dep]))
Gilles Peskinec2fc2412021-04-29 21:56:59 +0200294 # Public key for a private-key operation
295 if category.is_asymmetric() and kt.is_public():
296 yield self.make_test_case(alg, category,
297 self.Reason.PUBLIC,
298 kt=kt)
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100299 elif key_is_compatible:
300 # Compatible key, incompatible operation, supported algorithm
301 yield self.make_test_case(alg, category,
302 self.Reason.INVALID,
303 kt=kt)
Gilles Peskine23cb12e2021-04-29 20:54:40 +0200304 elif alg.can_do(category):
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100305 # Incompatible key, compatible operation, supported algorithm
306 yield self.make_test_case(alg, category,
307 self.Reason.INCOMPATIBLE,
308 kt=kt)
309 else:
310 # Incompatible key and operation. Don't test cases where
311 # multiple things are wrong, to keep the number of test
312 # cases reasonable.
313 pass
314
315 def test_cases_for_algorithm(
316 self,
317 alg: crypto_knowledge.Algorithm,
318 ) -> Iterator[test_case.TestCase]:
Gilles Peskinea2180472021-04-27 21:03:43 +0200319 """Generate operation failure test cases for the specified algorithm."""
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100320 for category in crypto_knowledge.AlgorithmCategory:
321 if category == crypto_knowledge.AlgorithmCategory.PAKE:
322 # PAKE operations are not implemented yet
323 pass
324 elif category.requires_key():
325 yield from self.one_key_test_cases(alg, category)
326 else:
327 yield from self.no_key_test_cases(alg, category)
Gilles Peskinea2180472021-04-27 21:03:43 +0200328
Gilles Peskinec05158b2021-04-27 20:40:10 +0200329 def all_test_cases(self) -> Iterator[test_case.TestCase]:
330 """Generate all test cases for operations that must fail."""
Gilles Peskinea2180472021-04-27 21:03:43 +0200331 algorithms = sorted(self.constructors.algorithms)
Gilles Peskinef8b6b502022-03-15 17:26:33 +0100332 for expr in self.constructors.generate_expressions(algorithms):
333 alg = crypto_knowledge.Algorithm(expr)
Gilles Peskinea2180472021-04-27 21:03:43 +0200334 yield from self.test_cases_for_algorithm(alg)
Gilles Peskinec05158b2021-04-27 20:40:10 +0200335
336
Gilles Peskine897dff92021-03-10 15:03:44 +0100337class StorageKey(psa_storage.Key):
338 """Representation of a key for storage format testing."""
339
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200340 IMPLICIT_USAGE_FLAGS = {
341 'PSA_KEY_USAGE_SIGN_HASH': 'PSA_KEY_USAGE_SIGN_MESSAGE',
342 'PSA_KEY_USAGE_VERIFY_HASH': 'PSA_KEY_USAGE_VERIFY_MESSAGE'
343 } #type: Dict[str, str]
344 """Mapping of usage flags to the flags that they imply."""
345
346 def __init__(
347 self,
Gilles Peskined9af9782022-03-17 22:32:59 +0100348 usage: Iterable[str],
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200349 without_implicit_usage: Optional[bool] = False,
350 **kwargs
351 ) -> None:
352 """Prepare to generate a key.
353
354 * `usage` : The usage flags used for the key.
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000355 * `without_implicit_usage`: Flag to define to apply the usage extension
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200356 """
Gilles Peskined9af9782022-03-17 22:32:59 +0100357 usage_flags = set(usage)
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200358 if not without_implicit_usage:
Gilles Peskined9af9782022-03-17 22:32:59 +0100359 for flag in sorted(usage_flags):
360 if flag in self.IMPLICIT_USAGE_FLAGS:
361 usage_flags.add(self.IMPLICIT_USAGE_FLAGS[flag])
362 if usage_flags:
363 usage_expression = ' | '.join(sorted(usage_flags))
364 else:
365 usage_expression = '0'
366 super().__init__(usage=usage_expression, **kwargs)
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200367
368class StorageTestData(StorageKey):
369 """Representation of test case data for storage format testing."""
370
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200371 def __init__(
372 self,
373 description: str,
Gilles Peskined9af9782022-03-17 22:32:59 +0100374 expected_usage: Optional[List[str]] = None,
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200375 **kwargs
376 ) -> None:
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200377 """Prepare to generate test data
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200378
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200379 * `description` : used for the the test case names
380 * `expected_usage`: the usage flags generated as the expected usage flags
381 in the test cases. CAn differ from the usage flags
382 stored in the keys because of the usage flags extension.
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200383 """
Gilles Peskine897dff92021-03-10 15:03:44 +0100384 super().__init__(**kwargs)
385 self.description = description #type: str
Gilles Peskined9af9782022-03-17 22:32:59 +0100386 if expected_usage is None:
387 self.expected_usage = self.usage #type: psa_storage.Expr
388 elif expected_usage:
389 self.expected_usage = psa_storage.Expr(' | '.join(expected_usage))
390 else:
391 self.expected_usage = psa_storage.Expr(0)
gabor-mezei-arm15c1f032021-06-24 10:04:38 +0200392
Gilles Peskine897dff92021-03-10 15:03:44 +0100393class StorageFormat:
394 """Storage format stability test cases."""
395
Tomás González734d22c2023-10-30 15:15:45 +0000396 def __init__(self, info: psa_information.Information, version: int, forward: bool) -> None:
Gilles Peskine897dff92021-03-10 15:03:44 +0100397 """Prepare to generate test cases for storage format stability.
398
Tomás González734d22c2023-10-30 15:15:45 +0000399 * `info`: information about the API. See the `psa_information.Information` class.
Gilles Peskine897dff92021-03-10 15:03:44 +0100400 * `version`: the storage format version to generate test cases for.
401 * `forward`: if true, generate forward compatibility test cases which
402 save a key and check that its representation is as intended. Otherwise
403 generate backward compatibility test cases which inject a key
404 representation and check that it can be read and used.
405 """
gabor-mezei-arm0bdb84e2021-06-23 17:01:44 +0200406 self.constructors = info.constructors #type: macro_collector.PSAMacroEnumerator
407 self.version = version #type: int
408 self.forward = forward #type: bool
Gilles Peskine897dff92021-03-10 15:03:44 +0100409
Gilles Peskine32611242022-03-19 12:09:13 +0100410 RSA_OAEP_RE = re.compile(r'PSA_ALG_RSA_OAEP\((.*)\)\Z')
Gilles Peskine8ddced52022-03-19 15:36:09 +0100411 BRAINPOOL_RE = re.compile(r'PSA_KEY_TYPE_\w+\(PSA_ECC_FAMILY_BRAINPOOL_\w+\)\Z')
Gilles Peskine32611242022-03-19 12:09:13 +0100412 @classmethod
Gilles Peskine8ddced52022-03-19 15:36:09 +0100413 def exercise_key_with_algorithm(
Gilles Peskine32611242022-03-19 12:09:13 +0100414 cls,
415 key_type: psa_storage.Expr, bits: int,
416 alg: psa_storage.Expr
417 ) -> bool:
Gilles Peskine1efe7fd2022-12-15 23:03:19 +0100418 """Whether to exercise the given key with the given algorithm.
Gilles Peskine32611242022-03-19 12:09:13 +0100419
420 Normally only the type and algorithm matter for compatibility, and
421 this is handled in crypto_knowledge.KeyType.can_do(). This function
422 exists to detect exceptional cases. Exceptional cases detected here
423 are not tested in OpFail and should therefore have manually written
424 test cases.
425 """
Gilles Peskine8ddced52022-03-19 15:36:09 +0100426 # Some test keys have the RAW_DATA type and attributes that don't
427 # necessarily make sense. We do this to validate numerical
428 # encodings of the attributes.
429 # Raw data keys have no useful exercise anyway so there is no
430 # loss of test coverage.
431 if key_type.string == 'PSA_KEY_TYPE_RAW_DATA':
432 return False
Gilles Peskinec7686002022-04-20 16:31:37 +0200433 # Mbed TLS only supports 128-bit keys for RC4.
434 if key_type.string == 'PSA_KEY_TYPE_ARC4' and bits != 128:
435 return False
Gilles Peskine32611242022-03-19 12:09:13 +0100436 # OAEP requires room for two hashes plus wrapping
437 m = cls.RSA_OAEP_RE.match(alg.string)
438 if m:
439 hash_alg = m.group(1)
440 hash_length = crypto_knowledge.Algorithm.hash_length(hash_alg)
441 key_length = (bits + 7) // 8
442 # Leave enough room for at least one byte of plaintext
443 return key_length > 2 * hash_length + 2
Gilles Peskine8ddced52022-03-19 15:36:09 +0100444 # There's nothing wrong with ECC keys on Brainpool curves,
445 # but operations with them are very slow. So we only exercise them
446 # with a single algorithm, not with all possible hashes. We do
447 # exercise other curves with all algorithms so test coverage is
448 # perfectly adequate like this.
449 m = cls.BRAINPOOL_RE.match(key_type.string)
450 if m and alg.string != 'PSA_ALG_ECDSA_ANY':
451 return False
Gilles Peskine32611242022-03-19 12:09:13 +0100452 return True
453
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200454 def make_test_case(self, key: StorageTestData) -> test_case.TestCase:
Gilles Peskine897dff92021-03-10 15:03:44 +0100455 """Construct a storage format test case for the given key.
456
457 If ``forward`` is true, generate a forward compatibility test case:
458 create a key and validate that it has the expected representation.
459 Otherwise generate a backward compatibility test case: inject the
460 key representation into storage and validate that it can be read
461 correctly.
462 """
463 verb = 'save' if self.forward else 'read'
Gilles Peskinec7b58d52024-04-10 15:55:39 +0200464 tc = psa_information.TestCase()
Gilles Peskine930ccef2022-03-18 00:02:15 +0100465 tc.set_description(verb + ' ' + key.description)
Gilles Peskinec7b58d52024-04-10 15:55:39 +0200466 tc.set_key_bits(key.bits)
Gilles Peskine897dff92021-03-10 15:03:44 +0100467 tc.set_function('key_storage_' + verb)
468 if self.forward:
469 extra_arguments = []
470 else:
Gilles Peskine643eb832021-04-21 20:11:33 +0200471 flags = []
Gilles Peskine8ddced52022-03-19 15:36:09 +0100472 if self.exercise_key_with_algorithm(key.type, key.bits, key.alg):
Gilles Peskine643eb832021-04-21 20:11:33 +0200473 flags.append('TEST_FLAG_EXERCISE')
474 if 'READ_ONLY' in key.lifetime.string:
475 flags.append('TEST_FLAG_READ_ONLY')
476 extra_arguments = [' | '.join(flags) if flags else '0']
Gilles Peskine897dff92021-03-10 15:03:44 +0100477 tc.set_arguments([key.lifetime.string,
478 key.type.string, str(key.bits),
Gilles Peskined9af9782022-03-17 22:32:59 +0100479 key.expected_usage.string,
480 key.alg.string, key.alg2.string,
Gilles Peskine897dff92021-03-10 15:03:44 +0100481 '"' + key.material.hex() + '"',
482 '"' + key.hex() + '"',
483 *extra_arguments])
484 return tc
485
Gilles Peskineefb584d2021-04-21 22:05:34 +0200486 def key_for_lifetime(
487 self,
488 lifetime: str,
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200489 ) -> StorageTestData:
Gilles Peskineefb584d2021-04-21 22:05:34 +0200490 """Construct a test key for the given lifetime."""
491 short = lifetime
492 short = re.sub(r'PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION',
493 r'', short)
Gilles Peskined79aef52022-03-17 23:42:25 +0100494 short = crypto_knowledge.short_expression(short)
Gilles Peskineefb584d2021-04-21 22:05:34 +0200495 description = 'lifetime: ' + short
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200496 key = StorageTestData(version=self.version,
497 id=1, lifetime=lifetime,
498 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
Gilles Peskined9af9782022-03-17 22:32:59 +0100499 usage=['PSA_KEY_USAGE_EXPORT'], alg=0, alg2=0,
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200500 material=b'L',
501 description=description)
502 return key
Gilles Peskineefb584d2021-04-21 22:05:34 +0200503
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200504 def all_keys_for_lifetimes(self) -> Iterator[StorageTestData]:
Gilles Peskineefb584d2021-04-21 22:05:34 +0200505 """Generate test keys covering lifetimes."""
506 lifetimes = sorted(self.constructors.lifetimes)
507 expressions = self.constructors.generate_expressions(lifetimes)
508 for lifetime in expressions:
509 # Don't attempt to create or load a volatile key in storage
510 if 'VOLATILE' in lifetime:
511 continue
512 # Don't attempt to create a read-only key in storage,
513 # but do attempt to load one.
514 if 'READ_ONLY' in lifetime and self.forward:
515 continue
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200516 yield self.key_for_lifetime(lifetime)
Gilles Peskineefb584d2021-04-21 22:05:34 +0200517
Gilles Peskinea296e482022-02-24 18:58:08 +0100518 def key_for_usage_flags(
Gilles Peskine897dff92021-03-10 15:03:44 +0100519 self,
520 usage_flags: List[str],
gabor-mezei-armd71659f2021-06-24 09:42:02 +0200521 short: Optional[str] = None,
Gilles Peskinea296e482022-02-24 18:58:08 +0100522 test_implicit_usage: Optional[bool] = True
523 ) -> StorageTestData:
Gilles Peskine897dff92021-03-10 15:03:44 +0100524 """Construct a test key for the given key usage."""
Gilles Peskinea296e482022-02-24 18:58:08 +0100525 extra_desc = ' without implication' if test_implicit_usage else ''
Gilles Peskined9af9782022-03-17 22:32:59 +0100526 description = 'usage' + extra_desc + ': '
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200527 key1 = StorageTestData(version=self.version,
528 id=1, lifetime=0x00000001,
529 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
Gilles Peskined9af9782022-03-17 22:32:59 +0100530 expected_usage=usage_flags,
Gilles Peskinea296e482022-02-24 18:58:08 +0100531 without_implicit_usage=not test_implicit_usage,
Gilles Peskined9af9782022-03-17 22:32:59 +0100532 usage=usage_flags, alg=0, alg2=0,
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200533 material=b'K',
534 description=description)
Gilles Peskined9af9782022-03-17 22:32:59 +0100535 if short is None:
Gilles Peskined79aef52022-03-17 23:42:25 +0100536 usage_expr = key1.expected_usage.string
537 key1.description += crypto_knowledge.short_expression(usage_expr)
Gilles Peskined9af9782022-03-17 22:32:59 +0100538 else:
539 key1.description += short
Gilles Peskinea296e482022-02-24 18:58:08 +0100540 return key1
Gilles Peskine897dff92021-03-10 15:03:44 +0100541
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200542 def generate_keys_for_usage_flags(self, **kwargs) -> Iterator[StorageTestData]:
Gilles Peskine897dff92021-03-10 15:03:44 +0100543 """Generate test keys covering usage flags."""
544 known_flags = sorted(self.constructors.key_usage_flags)
Gilles Peskinea296e482022-02-24 18:58:08 +0100545 yield self.key_for_usage_flags(['0'], **kwargs)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200546 for usage_flag in known_flags:
Gilles Peskinea296e482022-02-24 18:58:08 +0100547 yield self.key_for_usage_flags([usage_flag], **kwargs)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200548 for flag1, flag2 in zip(known_flags,
549 known_flags[1:] + [known_flags[0]]):
Gilles Peskinea296e482022-02-24 18:58:08 +0100550 yield self.key_for_usage_flags([flag1, flag2], **kwargs)
gabor-mezei-armbce85272021-06-24 14:38:51 +0200551
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200552 def generate_key_for_all_usage_flags(self) -> Iterator[StorageTestData]:
gabor-mezei-armbce85272021-06-24 14:38:51 +0200553 known_flags = sorted(self.constructors.key_usage_flags)
Gilles Peskinea296e482022-02-24 18:58:08 +0100554 yield self.key_for_usage_flags(known_flags, short='all known')
gabor-mezei-armbce85272021-06-24 14:38:51 +0200555
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200556 def all_keys_for_usage_flags(self) -> Iterator[StorageTestData]:
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200557 yield from self.generate_keys_for_usage_flags()
558 yield from self.generate_key_for_all_usage_flags()
Gilles Peskine897dff92021-03-10 15:03:44 +0100559
Gilles Peskine6213a002021-04-29 22:28:07 +0200560 def key_for_type_and_alg(
561 self,
562 kt: crypto_knowledge.KeyType,
563 bits: int,
564 alg: Optional[crypto_knowledge.Algorithm] = None,
565 ) -> StorageTestData:
566 """Construct a test key of the given type.
567
568 If alg is not None, this key allows it.
569 """
Gilles Peskined9af9782022-03-17 22:32:59 +0100570 usage_flags = ['PSA_KEY_USAGE_EXPORT']
Gilles Peskine0de11432022-03-18 09:58:09 +0100571 alg1 = 0 #type: psa_storage.Exprable
Gilles Peskine6213a002021-04-29 22:28:07 +0200572 alg2 = 0
Gilles Peskine0de11432022-03-18 09:58:09 +0100573 if alg is not None:
574 alg1 = alg.expression
575 usage_flags += alg.usage_flags(public=kt.is_public())
Gilles Peskine6213a002021-04-29 22:28:07 +0200576 key_material = kt.key_material(bits)
Gilles Peskine930ccef2022-03-18 00:02:15 +0100577 description = 'type: {} {}-bit'.format(kt.short_expression(1), bits)
Gilles Peskine6213a002021-04-29 22:28:07 +0200578 if alg is not None:
Gilles Peskine930ccef2022-03-18 00:02:15 +0100579 description += ', ' + alg.short_expression(1)
Gilles Peskine6213a002021-04-29 22:28:07 +0200580 key = StorageTestData(version=self.version,
581 id=1, lifetime=0x00000001,
582 type=kt.expression, bits=bits,
583 usage=usage_flags, alg=alg1, alg2=alg2,
584 material=key_material,
585 description=description)
586 return key
587
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100588 def keys_for_type(
589 self,
590 key_type: str,
Gilles Peskine6213a002021-04-29 22:28:07 +0200591 all_algorithms: List[crypto_knowledge.Algorithm],
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200592 ) -> Iterator[StorageTestData]:
Gilles Peskine6213a002021-04-29 22:28:07 +0200593 """Generate test keys for the given key type."""
594 kt = crypto_knowledge.KeyType(key_type)
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100595 for bits in kt.sizes_to_test():
Gilles Peskine6213a002021-04-29 22:28:07 +0200596 # Test a non-exercisable key, as well as exercisable keys for
597 # each compatible algorithm.
598 # To do: test reading a key from storage with an incompatible
599 # or unsupported algorithm.
600 yield self.key_for_type_and_alg(kt, bits)
601 compatible_algorithms = [alg for alg in all_algorithms
602 if kt.can_do(alg)]
603 for alg in compatible_algorithms:
604 yield self.key_for_type_and_alg(kt, bits, alg)
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100605
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200606 def all_keys_for_types(self) -> Iterator[StorageTestData]:
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100607 """Generate test keys covering key types and their representations."""
Gilles Peskineb93f8542021-04-19 13:50:25 +0200608 key_types = sorted(self.constructors.key_types)
Gilles Peskine6213a002021-04-29 22:28:07 +0200609 all_algorithms = [crypto_knowledge.Algorithm(alg)
610 for alg in self.constructors.generate_expressions(
611 sorted(self.constructors.algorithms)
612 )]
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200613 for key_type in self.constructors.generate_expressions(key_types):
Gilles Peskine6213a002021-04-29 22:28:07 +0200614 yield from self.keys_for_type(key_type, all_algorithms)
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100615
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200616 def keys_for_algorithm(self, alg: str) -> Iterator[StorageTestData]:
Gilles Peskine6213a002021-04-29 22:28:07 +0200617 """Generate test keys for the encoding of the specified algorithm."""
618 # These test cases only validate the encoding of algorithms, not
619 # whether the key read from storage is suitable for an operation.
620 # `keys_for_types` generate read tests with an algorithm and a
621 # compatible key.
Gilles Peskine930ccef2022-03-18 00:02:15 +0100622 descr = crypto_knowledge.short_expression(alg, 1)
Gilles Peskined9af9782022-03-17 22:32:59 +0100623 usage = ['PSA_KEY_USAGE_EXPORT']
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200624 key1 = StorageTestData(version=self.version,
625 id=1, lifetime=0x00000001,
626 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
627 usage=usage, alg=alg, alg2=0,
628 material=b'K',
629 description='alg: ' + descr)
630 yield key1
631 key2 = StorageTestData(version=self.version,
632 id=1, lifetime=0x00000001,
633 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
634 usage=usage, alg=0, alg2=alg,
635 material=b'L',
636 description='alg2: ' + descr)
637 yield key2
Gilles Peskined86bc522021-03-10 15:08:57 +0100638
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200639 def all_keys_for_algorithms(self) -> Iterator[StorageTestData]:
Gilles Peskined86bc522021-03-10 15:08:57 +0100640 """Generate test keys covering algorithm encodings."""
Gilles Peskineb93f8542021-04-19 13:50:25 +0200641 algorithms = sorted(self.constructors.algorithms)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200642 for alg in self.constructors.generate_expressions(algorithms):
643 yield from self.keys_for_algorithm(alg)
Gilles Peskined86bc522021-03-10 15:08:57 +0100644
gabor-mezei-armea840de2021-06-29 15:42:57 +0200645 def generate_all_keys(self) -> Iterator[StorageTestData]:
gabor-mezei-arm8b0c91c2021-06-24 09:49:50 +0200646 """Generate all keys for the test cases."""
gabor-mezei-armea840de2021-06-29 15:42:57 +0200647 yield from self.all_keys_for_lifetimes()
648 yield from self.all_keys_for_usage_flags()
649 yield from self.all_keys_for_types()
650 yield from self.all_keys_for_algorithms()
gabor-mezei-arm8b0c91c2021-06-24 09:49:50 +0200651
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200652 def all_test_cases(self) -> Iterator[test_case.TestCase]:
Gilles Peskine897dff92021-03-10 15:03:44 +0100653 """Generate all storage format test cases."""
Gilles Peskineae9f14b2021-04-12 14:43:05 +0200654 # First build a list of all keys, then construct all the corresponding
655 # test cases. This allows all required information to be obtained in
656 # one go, which is a significant performance gain as the information
657 # includes numerical values obtained by compiling a C program.
Gilles Peskine3008c582021-07-06 21:05:52 +0200658 all_keys = list(self.generate_all_keys())
659 for key in all_keys:
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200660 if key.location_value() != 0:
661 # Skip keys with a non-default location, because they
662 # require a driver and we currently have no mechanism to
663 # determine whether a driver is available.
664 continue
665 yield self.make_test_case(key)
Gilles Peskine897dff92021-03-10 15:03:44 +0100666
gabor-mezei-arm4d9fb732021-06-24 09:53:26 +0200667class StorageFormatForward(StorageFormat):
668 """Storage format stability test cases for forward compatibility."""
669
Tomás González734d22c2023-10-30 15:15:45 +0000670 def __init__(self, info: psa_information.Information, version: int) -> None:
gabor-mezei-arm4d9fb732021-06-24 09:53:26 +0200671 super().__init__(info, version, True)
672
673class StorageFormatV0(StorageFormat):
674 """Storage format stability test cases for version 0 compatibility."""
675
Tomás González734d22c2023-10-30 15:15:45 +0000676 def __init__(self, info: psa_information.Information) -> None:
gabor-mezei-arm4d9fb732021-06-24 09:53:26 +0200677 super().__init__(info, 0, False)
Gilles Peskine897dff92021-03-10 15:03:44 +0100678
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200679 def all_keys_for_usage_flags(self) -> Iterator[StorageTestData]:
gabor-mezei-arm15c1f032021-06-24 10:04:38 +0200680 """Generate test keys covering usage flags."""
Gilles Peskinea296e482022-02-24 18:58:08 +0100681 yield from super().all_keys_for_usage_flags()
682 yield from self.generate_keys_for_usage_flags(test_implicit_usage=False)
gabor-mezei-arm15c1f032021-06-24 10:04:38 +0200683
gabor-mezei-armacfcc182021-06-28 17:40:32 +0200684 def keys_for_implicit_usage(
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200685 self,
gabor-mezei-arme84d3212021-06-28 16:54:11 +0200686 implyer_usage: str,
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200687 alg: str,
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200688 key_type: crypto_knowledge.KeyType
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200689 ) -> StorageTestData:
gabor-mezei-armb92d61b2021-06-24 14:38:25 +0200690 # pylint: disable=too-many-locals
gabor-mezei-arm927742e2021-06-28 16:27:29 +0200691 """Generate test keys for the specified implicit usage flag,
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200692 algorithm and key type combination.
693 """
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200694 bits = key_type.sizes_to_test()[0]
gabor-mezei-arme84d3212021-06-28 16:54:11 +0200695 implicit_usage = StorageKey.IMPLICIT_USAGE_FLAGS[implyer_usage]
Gilles Peskined9af9782022-03-17 22:32:59 +0100696 usage_flags = ['PSA_KEY_USAGE_EXPORT']
697 material_usage_flags = usage_flags + [implyer_usage]
698 expected_usage_flags = material_usage_flags + [implicit_usage]
gabor-mezei-arm47812632021-06-28 16:35:48 +0200699 alg2 = 0
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200700 key_material = key_type.key_material(bits)
Gilles Peskine930ccef2022-03-18 00:02:15 +0100701 usage_expression = crypto_knowledge.short_expression(implyer_usage, 1)
702 alg_expression = crypto_knowledge.short_expression(alg, 1)
703 key_type_expression = key_type.short_expression(1)
gabor-mezei-armacfcc182021-06-28 17:40:32 +0200704 description = 'implied by {}: {} {} {}-bit'.format(
gabor-mezei-arm47812632021-06-28 16:35:48 +0200705 usage_expression, alg_expression, key_type_expression, bits)
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200706 key = StorageTestData(version=self.version,
707 id=1, lifetime=0x00000001,
708 type=key_type.expression, bits=bits,
709 usage=material_usage_flags,
710 expected_usage=expected_usage_flags,
711 without_implicit_usage=True,
712 alg=alg, alg2=alg2,
713 material=key_material,
714 description=description)
715 return key
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200716
717 def gather_key_types_for_sign_alg(self) -> Dict[str, List[str]]:
gabor-mezei-armb92d61b2021-06-24 14:38:25 +0200718 # pylint: disable=too-many-locals
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200719 """Match possible key types for sign algorithms."""
Shaun Case0e7791f2021-12-20 21:14:10 -0800720 # To create a valid combination both the algorithms and key types
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200721 # must be filtered. Pair them with keywords created from its names.
722 incompatible_alg_keyword = frozenset(['RAW', 'ANY', 'PURE'])
723 incompatible_key_type_keywords = frozenset(['MONTGOMERY'])
724 keyword_translation = {
725 'ECDSA': 'ECC',
726 'ED[0-9]*.*' : 'EDWARDS'
727 }
728 exclusive_keywords = {
729 'EDWARDS': 'ECC'
730 }
gabor-mezei-armb92d61b2021-06-24 14:38:25 +0200731 key_types = set(self.constructors.generate_expressions(self.constructors.key_types))
732 algorithms = set(self.constructors.generate_expressions(self.constructors.sign_algorithms))
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200733 alg_with_keys = {} #type: Dict[str, List[str]]
734 translation_table = str.maketrans('(', '_', ')')
735 for alg in algorithms:
736 # Generate keywords from the name of the algorithm
737 alg_keywords = set(alg.partition('(')[0].split(sep='_')[2:])
738 # Translate keywords for better matching with the key types
739 for keyword in alg_keywords.copy():
740 for pattern, replace in keyword_translation.items():
741 if re.match(pattern, keyword):
742 alg_keywords.remove(keyword)
743 alg_keywords.add(replace)
Shaun Case0e7791f2021-12-20 21:14:10 -0800744 # Filter out incompatible algorithms
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200745 if not alg_keywords.isdisjoint(incompatible_alg_keyword):
746 continue
747
748 for key_type in key_types:
749 # Generate keywords from the of the key type
750 key_type_keywords = set(key_type.translate(translation_table).split(sep='_')[3:])
751
Shaun Case0e7791f2021-12-20 21:14:10 -0800752 # Remove ambiguous keywords
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200753 for keyword1, keyword2 in exclusive_keywords.items():
754 if keyword1 in key_type_keywords:
755 key_type_keywords.remove(keyword2)
756
757 if key_type_keywords.isdisjoint(incompatible_key_type_keywords) and\
758 not key_type_keywords.isdisjoint(alg_keywords):
759 if alg in alg_with_keys:
760 alg_with_keys[alg].append(key_type)
761 else:
762 alg_with_keys[alg] = [key_type]
763 return alg_with_keys
764
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200765 def all_keys_for_implicit_usage(self) -> Iterator[StorageTestData]:
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200766 """Generate test keys for usage flag extensions."""
767 # Generate a key type and algorithm pair for each extendable usage
768 # flag to generate a valid key for exercising. The key is generated
Shaun Case0e7791f2021-12-20 21:14:10 -0800769 # without usage extension to check the extension compatibility.
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200770 alg_with_keys = self.gather_key_types_for_sign_alg()
gabor-mezei-arm7d2ec9a2021-06-24 16:35:01 +0200771
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200772 for usage in sorted(StorageKey.IMPLICIT_USAGE_FLAGS, key=str):
773 for alg in sorted(alg_with_keys):
774 for key_type in sorted(alg_with_keys[alg]):
775 # The key types must be filtered to fit the specific usage flag.
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200776 kt = crypto_knowledge.KeyType(key_type)
Gilles Peskine989c13d2022-03-17 12:52:24 +0100777 if kt.is_public() and '_SIGN_' in usage:
778 # Can't sign with a public key
779 continue
780 yield self.keys_for_implicit_usage(usage, alg, kt)
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200781
gabor-mezei-armea840de2021-06-29 15:42:57 +0200782 def generate_all_keys(self) -> Iterator[StorageTestData]:
783 yield from super().generate_all_keys()
784 yield from self.all_keys_for_implicit_usage()
gabor-mezei-arm15c1f032021-06-24 10:04:38 +0200785
Tomás González734d22c2023-10-30 15:15:45 +0000786
Gilles Peskine69feebd2022-09-16 21:41:47 +0200787class PSATestGenerator(test_data_generation.TestGenerator):
Werner Lewisdcad1e92022-08-24 11:30:03 +0100788 """Test generator subclass including PSA targets and info."""
Dave Rodgmanbeb5ad72022-04-22 14:52:41 +0100789 # Note that targets whose names contain 'test_format' have their content
Gilles Peskinecfd4fae2021-04-23 16:37:12 +0200790 # validated by `abi_check.py`.
Werner Lewis0d07e862022-09-02 11:56:34 +0100791 targets = {
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200792 'test_suite_psa_crypto_generate_key.generated':
793 lambda info: KeyGenerate(info).test_cases_for_key_generation(),
Gilles Peskine0298bda2021-03-10 02:34:37 +0100794 'test_suite_psa_crypto_not_supported.generated':
Gilles Peskine4fa76bd2022-12-15 22:14:28 +0100795 lambda info: KeyTypeNotSupported(info).test_cases_for_not_supported(),
Tomás González2bff1bf2023-10-30 15:29:23 +0000796 'test_suite_psa_crypto_low_hash.generated':
797 lambda info: crypto_data_tests.HashPSALowLevel(info).all_test_cases(),
Gilles Peskinec05158b2021-04-27 20:40:10 +0200798 'test_suite_psa_crypto_op_fail.generated':
799 lambda info: OpFail(info).all_test_cases(),
Gilles Peskine897dff92021-03-10 15:03:44 +0100800 'test_suite_psa_crypto_storage_format.current':
gabor-mezei-arm4d9fb732021-06-24 09:53:26 +0200801 lambda info: StorageFormatForward(info, 0).all_test_cases(),
Gilles Peskine897dff92021-03-10 15:03:44 +0100802 'test_suite_psa_crypto_storage_format.v0':
gabor-mezei-arm4d9fb732021-06-24 09:53:26 +0200803 lambda info: StorageFormatV0(info).all_test_cases(),
Tomás González734d22c2023-10-30 15:15:45 +0000804 } #type: Dict[str, Callable[[psa_information.Information], Iterable[test_case.TestCase]]]
Gilles Peskine0298bda2021-03-10 02:34:37 +0100805
Werner Lewisdcad1e92022-08-24 11:30:03 +0100806 def __init__(self, options):
807 super().__init__(options)
Tomás González734d22c2023-10-30 15:15:45 +0000808 self.info = psa_information.Information()
Gilles Peskine14e428f2021-01-26 22:19:21 +0100809
Werner Lewisdcad1e92022-08-24 11:30:03 +0100810 def generate_target(self, name: str, *target_args) -> None:
811 super().generate_target(name, self.info)
Gilles Peskine09940492021-01-26 22:16:30 +0100812
Tomás González734d22c2023-10-30 15:15:45 +0000813
Gilles Peskine09940492021-01-26 22:16:30 +0100814if __name__ == '__main__':
Gilles Peskine69feebd2022-09-16 21:41:47 +0200815 test_data_generation.main(sys.argv[1:], __doc__, PSATestGenerator)