blob: 0bca19a146db267181156cc6732dd4f41a2815ee [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
9# SPDX-License-Identifier: Apache-2.0
10#
11# Licensed under the Apache License, Version 2.0 (the "License"); you may
12# not use this file except in compliance with the License.
13# You may obtain a copy of the License at
14#
15# http://www.apache.org/licenses/LICENSE-2.0
16#
17# Unless required by applicable law or agreed to in writing, software
18# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20# See the License for the specific language governing permissions and
21# limitations under the License.
22
23import argparse
Gilles Peskine14e428f2021-01-26 22:19:21 +010024import os
25import re
Gilles Peskine09940492021-01-26 22:16:30 +010026import sys
Gilles Peskine3d778392021-02-17 15:11:05 +010027from typing import Callable, Dict, FrozenSet, Iterable, Iterator, List, Optional, TypeVar
Gilles Peskine09940492021-01-26 22:16:30 +010028
29import scripts_path # pylint: disable=unused-import
Gilles Peskine14e428f2021-01-26 22:19:21 +010030from mbedtls_dev import crypto_knowledge
Gilles Peskine09940492021-01-26 22:16:30 +010031from mbedtls_dev import macro_collector
Gilles Peskine897dff92021-03-10 15:03:44 +010032from mbedtls_dev import psa_storage
Gilles Peskine14e428f2021-01-26 22:19:21 +010033from mbedtls_dev import test_case
Gilles Peskine09940492021-01-26 22:16:30 +010034
35T = TypeVar('T') #pylint: disable=invalid-name
36
Gilles Peskine14e428f2021-01-26 22:19:21 +010037
Gilles Peskine7f756872021-02-16 12:13:12 +010038def psa_want_symbol(name: str) -> str:
Gilles Peskineaf172842021-01-27 18:24:48 +010039 """Return the PSA_WANT_xxx symbol associated with a PSA crypto feature."""
40 if name.startswith('PSA_'):
41 return name[:4] + 'WANT_' + name[4:]
42 else:
43 raise ValueError('Unable to determine the PSA_WANT_ symbol for ' + name)
44
Gilles Peskine7f756872021-02-16 12:13:12 +010045def finish_family_dependency(dep: str, bits: int) -> str:
46 """Finish dep if it's a family dependency symbol prefix.
47
48 A family dependency symbol prefix is a PSA_WANT_ symbol that needs to be
49 qualified by the key size. If dep is such a symbol, finish it by adjusting
50 the prefix and appending the key size. Other symbols are left unchanged.
51 """
52 return re.sub(r'_FAMILY_(.*)', r'_\1_' + str(bits), dep)
53
54def finish_family_dependencies(dependencies: List[str], bits: int) -> List[str]:
55 """Finish any family dependency symbol prefixes.
56
57 Apply `finish_family_dependency` to each element of `dependencies`.
58 """
59 return [finish_family_dependency(dep, bits) for dep in dependencies]
Gilles Peskineaf172842021-01-27 18:24:48 +010060
Gilles Peskine8a55b432021-04-20 23:23:45 +020061SYMBOLS_WITHOUT_DEPENDENCY = frozenset([
62 'PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG', # modifier, only in policies
63 'PSA_ALG_AEAD_WITH_SHORTENED_TAG', # modifier
64 'PSA_ALG_ANY_HASH', # only in policies
65 'PSA_ALG_AT_LEAST_THIS_LENGTH_MAC', # modifier, only in policies
66 'PSA_ALG_KEY_AGREEMENT', # chaining
67 'PSA_ALG_TRUNCATED_MAC', # modifier
68])
Gilles Peskinef8223ab2021-03-10 15:07:16 +010069def automatic_dependencies(*expressions: str) -> List[str]:
70 """Infer dependencies of a test case by looking for PSA_xxx symbols.
71
72 The arguments are strings which should be C expressions. Do not use
73 string literals or comments as this function is not smart enough to
74 skip them.
75 """
76 used = set()
77 for expr in expressions:
78 used.update(re.findall(r'PSA_(?:ALG|ECC_FAMILY|KEY_TYPE)_\w+', expr))
Gilles Peskine8a55b432021-04-20 23:23:45 +020079 used.difference_update(SYMBOLS_WITHOUT_DEPENDENCY)
Gilles Peskinef8223ab2021-03-10 15:07:16 +010080 return sorted(psa_want_symbol(name) for name in used)
81
Gilles Peskined169d602021-02-16 14:16:25 +010082# A temporary hack: at the time of writing, not all dependency symbols
83# are implemented yet. Skip test cases for which the dependency symbols are
84# not available. Once all dependency symbols are available, this hack must
85# be removed so that a bug in the dependency symbols proprely leads to a test
86# failure.
87def read_implemented_dependencies(filename: str) -> FrozenSet[str]:
88 return frozenset(symbol
89 for line in open(filename)
90 for symbol in re.findall(r'\bPSA_WANT_\w+\b', line))
91IMPLEMENTED_DEPENDENCIES = read_implemented_dependencies('include/psa/crypto_config.h')
92def hack_dependencies_not_implemented(dependencies: List[str]) -> None:
93 if not all(dep.lstrip('!') in IMPLEMENTED_DEPENDENCIES
94 for dep in dependencies):
95 dependencies.append('DEPENDENCY_NOT_IMPLEMENTED_YET')
96
Gilles Peskine14e428f2021-01-26 22:19:21 +010097
Gilles Peskineb94ea512021-03-10 02:12:08 +010098class Information:
99 """Gather information about PSA constructors."""
Gilles Peskine09940492021-01-26 22:16:30 +0100100
Gilles Peskineb94ea512021-03-10 02:12:08 +0100101 def __init__(self) -> None:
Gilles Peskine09940492021-01-26 22:16:30 +0100102 self.constructors = self.read_psa_interface()
103
104 @staticmethod
Gilles Peskine09940492021-01-26 22:16:30 +0100105 def remove_unwanted_macros(
Gilles Peskineb93f8542021-04-19 13:50:25 +0200106 constructors: macro_collector.PSAMacroEnumerator
Gilles Peskine09940492021-01-26 22:16:30 +0100107 ) -> None:
Gilles Peskineb93f8542021-04-19 13:50:25 +0200108 # Mbed TLS doesn't support finite-field DH yet and will not support
109 # finite-field DSA. Don't attempt to generate any related test case.
110 constructors.key_types.discard('PSA_KEY_TYPE_DH_KEY_PAIR')
111 constructors.key_types.discard('PSA_KEY_TYPE_DH_PUBLIC_KEY')
Gilles Peskine09940492021-01-26 22:16:30 +0100112 constructors.key_types.discard('PSA_KEY_TYPE_DSA_KEY_PAIR')
113 constructors.key_types.discard('PSA_KEY_TYPE_DSA_PUBLIC_KEY')
Gilles Peskine09940492021-01-26 22:16:30 +0100114
Gilles Peskineb93f8542021-04-19 13:50:25 +0200115 def read_psa_interface(self) -> macro_collector.PSAMacroEnumerator:
Gilles Peskine09940492021-01-26 22:16:30 +0100116 """Return the list of known key types, algorithms, etc."""
Gilles Peskined6d2d6a2021-03-30 21:46:35 +0200117 constructors = macro_collector.InputsForTest()
Gilles Peskine09940492021-01-26 22:16:30 +0100118 header_file_names = ['include/psa/crypto_values.h',
119 'include/psa/crypto_extra.h']
Gilles Peskineb93f8542021-04-19 13:50:25 +0200120 test_suites = ['tests/suites/test_suite_psa_crypto_metadata.data']
Gilles Peskine09940492021-01-26 22:16:30 +0100121 for header_file_name in header_file_names:
Gilles Peskineb93f8542021-04-19 13:50:25 +0200122 constructors.parse_header(header_file_name)
123 for test_cases in test_suites:
124 constructors.parse_test_cases(test_cases)
Gilles Peskine09940492021-01-26 22:16:30 +0100125 self.remove_unwanted_macros(constructors)
Gilles Peskined6d2d6a2021-03-30 21:46:35 +0200126 constructors.gather_arguments()
Gilles Peskine09940492021-01-26 22:16:30 +0100127 return constructors
128
Gilles Peskine14e428f2021-01-26 22:19:21 +0100129
Przemyslaw Stekield6ead7c2021-10-11 10:15:25 +0200130def test_case_for_key_type_not_supported(
Gilles Peskineb94ea512021-03-10 02:12:08 +0100131 verb: str, key_type: str, bits: int,
132 dependencies: List[str],
133 *args: str,
134 param_descr: str = ''
135) -> test_case.TestCase:
136 """Return one test case exercising a key creation method
137 for an unsupported key type or size.
138 """
139 hack_dependencies_not_implemented(dependencies)
140 tc = test_case.TestCase()
141 short_key_type = re.sub(r'PSA_(KEY_TYPE|ECC_FAMILY)_', r'', key_type)
142 adverb = 'not' if dependencies else 'never'
143 if param_descr:
144 adverb = param_descr + ' ' + adverb
Przemyslaw Stekield6ead7c2021-10-11 10:15:25 +0200145 tc.set_description('PSA {} {} {}-bit {} supported'
146 .format(verb, short_key_type, bits, adverb))
147 tc.set_dependencies(dependencies)
148 tc.set_function(verb + '_not_supported')
149 tc.set_arguments([key_type] + list(args))
150 return tc
151
Gilles Peskineb94ea512021-03-10 02:12:08 +0100152class NotSupported:
Przemyslaw Stekiel32a8b842021-10-18 14:58:20 +0200153 """Generate test cases for when something is not supported."""
Gilles Peskineb94ea512021-03-10 02:12:08 +0100154
155 def __init__(self, info: Information) -> None:
156 self.constructors = info.constructors
Gilles Peskine14e428f2021-01-26 22:19:21 +0100157
Gilles Peskine60b29fe2021-02-16 14:06:50 +0100158 ALWAYS_SUPPORTED = frozenset([
159 'PSA_KEY_TYPE_DERIVE',
160 'PSA_KEY_TYPE_RAW_DATA',
161 ])
Gilles Peskine14e428f2021-01-26 22:19:21 +0100162 def test_cases_for_key_type_not_supported(
Gilles Peskine60b29fe2021-02-16 14:06:50 +0100163 self,
Gilles Peskineaf172842021-01-27 18:24:48 +0100164 kt: crypto_knowledge.KeyType,
165 param: Optional[int] = None,
166 param_descr: str = '',
Gilles Peskine3d778392021-02-17 15:11:05 +0100167 ) -> Iterator[test_case.TestCase]:
Przemyslaw Stekiel32a8b842021-10-18 14:58:20 +0200168 """Return test cases exercising key creation when the given type is unsupported.
Gilles Peskineaf172842021-01-27 18:24:48 +0100169
170 If param is present and not None, emit test cases conditioned on this
171 parameter not being supported. If it is absent or None, emit test cases
Przemyslaw Stekiel32a8b842021-10-18 14:58:20 +0200172 conditioned on the base type not being supported.
Gilles Peskineaf172842021-01-27 18:24:48 +0100173 """
Gilles Peskine60b29fe2021-02-16 14:06:50 +0100174 if kt.name in self.ALWAYS_SUPPORTED:
175 # Don't generate test cases for key types that are always supported.
176 # They would be skipped in all configurations, which is noise.
Gilles Peskine3d778392021-02-17 15:11:05 +0100177 return
Gilles Peskineaf172842021-01-27 18:24:48 +0100178 import_dependencies = [('!' if param is None else '') +
179 psa_want_symbol(kt.name)]
180 if kt.params is not None:
181 import_dependencies += [('!' if param == i else '') +
182 psa_want_symbol(sym)
183 for i, sym in enumerate(kt.params)]
Gilles Peskine14e428f2021-01-26 22:19:21 +0100184 if kt.name.endswith('_PUBLIC_KEY'):
185 generate_dependencies = []
186 else:
187 generate_dependencies = import_dependencies
Gilles Peskine14e428f2021-01-26 22:19:21 +0100188 for bits in kt.sizes_to_test():
Przemyslaw Stekield6ead7c2021-10-11 10:15:25 +0200189 yield test_case_for_key_type_not_supported(
Gilles Peskine7f756872021-02-16 12:13:12 +0100190 'import', kt.expression, bits,
191 finish_family_dependencies(import_dependencies, bits),
Gilles Peskineaf172842021-01-27 18:24:48 +0100192 test_case.hex_string(kt.key_material(bits)),
193 param_descr=param_descr,
Gilles Peskine3d778392021-02-17 15:11:05 +0100194 )
Gilles Peskineaf172842021-01-27 18:24:48 +0100195 if not generate_dependencies and param is not None:
196 # If generation is impossible for this key type, rather than
197 # supported or not depending on implementation capabilities,
198 # only generate the test case once.
199 continue
Przemyslaw Stekiel32a8b842021-10-18 14:58:20 +0200200 # Public key cannot be generated
201 if not kt.name.endswith('_PUBLIC_KEY'):
Przemyslaw Stekield6ead7c2021-10-11 10:15:25 +0200202 yield test_case_for_key_type_not_supported(
203 'generate', kt.expression, bits,
204 finish_family_dependencies(generate_dependencies, bits),
205 str(bits),
206 param_descr=param_descr,
207 )
Gilles Peskine14e428f2021-01-26 22:19:21 +0100208 # To be added: derive
Gilles Peskine14e428f2021-01-26 22:19:21 +0100209
Gilles Peskineb93f8542021-04-19 13:50:25 +0200210 ECC_KEY_TYPES = ('PSA_KEY_TYPE_ECC_KEY_PAIR',
211 'PSA_KEY_TYPE_ECC_PUBLIC_KEY')
212
Gilles Peskine3d778392021-02-17 15:11:05 +0100213 def test_cases_for_not_supported(self) -> Iterator[test_case.TestCase]:
Gilles Peskine14e428f2021-01-26 22:19:21 +0100214 """Generate test cases that exercise the creation of keys of unsupported types."""
Gilles Peskine14e428f2021-01-26 22:19:21 +0100215 for key_type in sorted(self.constructors.key_types):
Gilles Peskineb93f8542021-04-19 13:50:25 +0200216 if key_type in self.ECC_KEY_TYPES:
217 continue
Gilles Peskine14e428f2021-01-26 22:19:21 +0100218 kt = crypto_knowledge.KeyType(key_type)
Gilles Peskine3d778392021-02-17 15:11:05 +0100219 yield from self.test_cases_for_key_type_not_supported(kt)
Gilles Peskineaf172842021-01-27 18:24:48 +0100220 for curve_family in sorted(self.constructors.ecc_curves):
Gilles Peskineb93f8542021-04-19 13:50:25 +0200221 for constr in self.ECC_KEY_TYPES:
Gilles Peskineaf172842021-01-27 18:24:48 +0100222 kt = crypto_knowledge.KeyType(constr, [curve_family])
Gilles Peskine3d778392021-02-17 15:11:05 +0100223 yield from self.test_cases_for_key_type_not_supported(
Gilles Peskineaf172842021-01-27 18:24:48 +0100224 kt, param_descr='type')
Gilles Peskine3d778392021-02-17 15:11:05 +0100225 yield from self.test_cases_for_key_type_not_supported(
Gilles Peskineaf172842021-01-27 18:24:48 +0100226 kt, 0, param_descr='curve')
Gilles Peskineb94ea512021-03-10 02:12:08 +0100227
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200228def test_case_for_key_generation(
229 key_type: str, bits: int,
230 dependencies: List[str],
231 *args: str,
232 result: str = '',
Przemyslaw Stekiel32a8b842021-10-18 14:58:20 +0200233 param_descr: str = ''
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200234) -> test_case.TestCase:
235 """Return one test case exercising a key generation.
236 """
237 hack_dependencies_not_implemented(dependencies)
238 tc = test_case.TestCase()
239 short_key_type = re.sub(r'PSA_(KEY_TYPE|ECC_FAMILY)_', r'', key_type)
240 tc.set_description('PSA {} {}-bit'
241 .format( short_key_type, bits))
242 tc.set_dependencies(dependencies)
243 tc.set_function('generate_key')
244 tc.set_arguments([key_type] + list(args))
245 tc.set_result(result)
246
247 return tc
248
249class KeyGenerate:
250 """Generate positive and negative (invalid argument) test cases for key generation."""
251
252 def __init__(self, info: Information) -> None:
253 self.constructors = info.constructors
254
255 def test_cases_for_key_type_key_generation(
256 self,
257 kt: crypto_knowledge.KeyType,
258 param: Optional[int] = None,
259 param_descr: str = '',
260 ) -> Iterator[test_case.TestCase]:
261 """Return test cases exercising key generation.
262
263 All key types can be generated except for public keys. For public key
264 PSA_ERROR_INVALID_ARGUMENT status is expected.
265 """
266 result = 'PSA_SUCCESS'
267
268 import_dependencies = [psa_want_symbol(kt.name)]
269 if kt.params is not None:
270 import_dependencies += [psa_want_symbol(sym)
271 for i, sym in enumerate(kt.params)]
272 if kt.name.endswith('_PUBLIC_KEY'):
273 generate_dependencies = []
274 result = 'PSA_ERROR_INVALID_ARGUMENT'
275 else:
276 generate_dependencies = import_dependencies
277 for bits in kt.sizes_to_test():
278 yield test_case_for_key_generation(
279 kt.expression, bits,
280 finish_family_dependencies(generate_dependencies, bits),
281 str(bits),
282 result,
283 param_descr=param_descr
284 )
285
286 ECC_KEY_TYPES = ('PSA_KEY_TYPE_ECC_KEY_PAIR',
287 'PSA_KEY_TYPE_ECC_PUBLIC_KEY')
288
289 def test_cases_for_key_generation(self) -> Iterator[test_case.TestCase]:
290 """Generate test cases that exercise the generation of keys."""
291 for key_type in sorted(self.constructors.key_types):
292 if key_type in self.ECC_KEY_TYPES:
293 continue
294 kt = crypto_knowledge.KeyType(key_type)
295 yield from self.test_cases_for_key_type_key_generation(kt)
296 for curve_family in sorted(self.constructors.ecc_curves):
297 for constr in self.ECC_KEY_TYPES:
298 kt = crypto_knowledge.KeyType(constr, [curve_family])
299 yield from self.test_cases_for_key_type_key_generation(
300 kt, param_descr='type')
301 yield from self.test_cases_for_key_type_key_generation(
302 kt, 0, param_descr='curve')
303
304
Gilles Peskine897dff92021-03-10 15:03:44 +0100305class StorageKey(psa_storage.Key):
306 """Representation of a key for storage format testing."""
307
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200308 IMPLICIT_USAGE_FLAGS = {
309 'PSA_KEY_USAGE_SIGN_HASH': 'PSA_KEY_USAGE_SIGN_MESSAGE',
310 'PSA_KEY_USAGE_VERIFY_HASH': 'PSA_KEY_USAGE_VERIFY_MESSAGE'
311 } #type: Dict[str, str]
312 """Mapping of usage flags to the flags that they imply."""
313
314 def __init__(
315 self,
316 usage: str,
317 without_implicit_usage: Optional[bool] = False,
318 **kwargs
319 ) -> None:
320 """Prepare to generate a key.
321
322 * `usage` : The usage flags used for the key.
323 * `without_implicit_usage`: Flag to defide to apply the usage extension
324 """
gabor-mezei-arm3ea27322021-06-29 17:21:21 +0200325 super().__init__(usage=usage, **kwargs)
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200326
327 if not without_implicit_usage:
328 for flag, implicit in self.IMPLICIT_USAGE_FLAGS.items():
329 if self.usage.value() & psa_storage.Expr(flag).value() and \
330 self.usage.value() & psa_storage.Expr(implicit).value() == 0:
331 self.usage = psa_storage.Expr(self.usage.string + ' | ' + implicit)
332
333class StorageTestData(StorageKey):
334 """Representation of test case data for storage format testing."""
335
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200336 def __init__(
337 self,
338 description: str,
339 expected_usage: Optional[str] = None,
340 **kwargs
341 ) -> None:
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200342 """Prepare to generate test data
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200343
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200344 * `description` : used for the the test case names
345 * `expected_usage`: the usage flags generated as the expected usage flags
346 in the test cases. CAn differ from the usage flags
347 stored in the keys because of the usage flags extension.
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200348 """
Gilles Peskine897dff92021-03-10 15:03:44 +0100349 super().__init__(**kwargs)
350 self.description = description #type: str
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200351 self.expected_usage = expected_usage if expected_usage else self.usage.string #type: str
gabor-mezei-arm15c1f032021-06-24 10:04:38 +0200352
Gilles Peskine897dff92021-03-10 15:03:44 +0100353class StorageFormat:
354 """Storage format stability test cases."""
355
356 def __init__(self, info: Information, version: int, forward: bool) -> None:
357 """Prepare to generate test cases for storage format stability.
358
359 * `info`: information about the API. See the `Information` class.
360 * `version`: the storage format version to generate test cases for.
361 * `forward`: if true, generate forward compatibility test cases which
362 save a key and check that its representation is as intended. Otherwise
363 generate backward compatibility test cases which inject a key
364 representation and check that it can be read and used.
365 """
gabor-mezei-arm0bdb84e2021-06-23 17:01:44 +0200366 self.constructors = info.constructors #type: macro_collector.PSAMacroEnumerator
367 self.version = version #type: int
368 self.forward = forward #type: bool
Gilles Peskine897dff92021-03-10 15:03:44 +0100369
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200370 def make_test_case(self, key: StorageTestData) -> test_case.TestCase:
Gilles Peskine897dff92021-03-10 15:03:44 +0100371 """Construct a storage format test case for the given key.
372
373 If ``forward`` is true, generate a forward compatibility test case:
374 create a key and validate that it has the expected representation.
375 Otherwise generate a backward compatibility test case: inject the
376 key representation into storage and validate that it can be read
377 correctly.
378 """
379 verb = 'save' if self.forward else 'read'
380 tc = test_case.TestCase()
381 tc.set_description('PSA storage {}: {}'.format(verb, key.description))
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100382 dependencies = automatic_dependencies(
383 key.lifetime.string, key.type.string,
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200384 key.expected_usage, key.alg.string, key.alg2.string,
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100385 )
386 dependencies = finish_family_dependencies(dependencies, key.bits)
387 tc.set_dependencies(dependencies)
Gilles Peskine897dff92021-03-10 15:03:44 +0100388 tc.set_function('key_storage_' + verb)
389 if self.forward:
390 extra_arguments = []
391 else:
Gilles Peskine643eb832021-04-21 20:11:33 +0200392 flags = []
Gilles Peskine897dff92021-03-10 15:03:44 +0100393 # Some test keys have the RAW_DATA type and attributes that don't
394 # necessarily make sense. We do this to validate numerical
395 # encodings of the attributes.
396 # Raw data keys have no useful exercise anyway so there is no
397 # loss of test coverage.
Gilles Peskine643eb832021-04-21 20:11:33 +0200398 if key.type.string != 'PSA_KEY_TYPE_RAW_DATA':
399 flags.append('TEST_FLAG_EXERCISE')
400 if 'READ_ONLY' in key.lifetime.string:
401 flags.append('TEST_FLAG_READ_ONLY')
402 extra_arguments = [' | '.join(flags) if flags else '0']
Gilles Peskine897dff92021-03-10 15:03:44 +0100403 tc.set_arguments([key.lifetime.string,
404 key.type.string, str(key.bits),
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200405 key.expected_usage, key.alg.string, key.alg2.string,
Gilles Peskine897dff92021-03-10 15:03:44 +0100406 '"' + key.material.hex() + '"',
407 '"' + key.hex() + '"',
408 *extra_arguments])
409 return tc
410
Gilles Peskineefb584d2021-04-21 22:05:34 +0200411 def key_for_lifetime(
412 self,
413 lifetime: str,
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200414 ) -> StorageTestData:
Gilles Peskineefb584d2021-04-21 22:05:34 +0200415 """Construct a test key for the given lifetime."""
416 short = lifetime
417 short = re.sub(r'PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION',
418 r'', short)
419 short = re.sub(r'PSA_KEY_[A-Z]+_', r'', short)
420 description = 'lifetime: ' + short
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200421 key = StorageTestData(version=self.version,
422 id=1, lifetime=lifetime,
423 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
424 usage='PSA_KEY_USAGE_EXPORT', alg=0, alg2=0,
425 material=b'L',
426 description=description)
427 return key
Gilles Peskineefb584d2021-04-21 22:05:34 +0200428
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200429 def all_keys_for_lifetimes(self) -> Iterator[StorageTestData]:
Gilles Peskineefb584d2021-04-21 22:05:34 +0200430 """Generate test keys covering lifetimes."""
431 lifetimes = sorted(self.constructors.lifetimes)
432 expressions = self.constructors.generate_expressions(lifetimes)
433 for lifetime in expressions:
434 # Don't attempt to create or load a volatile key in storage
435 if 'VOLATILE' in lifetime:
436 continue
437 # Don't attempt to create a read-only key in storage,
438 # but do attempt to load one.
439 if 'READ_ONLY' in lifetime and self.forward:
440 continue
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200441 yield self.key_for_lifetime(lifetime)
Gilles Peskineefb584d2021-04-21 22:05:34 +0200442
gabor-mezei-arm912eca32021-06-29 15:39:56 +0200443 def keys_for_usage_flags(
Gilles Peskine897dff92021-03-10 15:03:44 +0100444 self,
445 usage_flags: List[str],
gabor-mezei-armd71659f2021-06-24 09:42:02 +0200446 short: Optional[str] = None,
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200447 test_implicit_usage: Optional[bool] = False
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200448 ) -> Iterator[StorageTestData]:
Gilles Peskine897dff92021-03-10 15:03:44 +0100449 """Construct a test key for the given key usage."""
450 usage = ' | '.join(usage_flags) if usage_flags else '0'
451 if short is None:
452 short = re.sub(r'\bPSA_KEY_USAGE_', r'', usage)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200453 extra_desc = ' with implication' if test_implicit_usage else ''
gabor-mezei-armd71659f2021-06-24 09:42:02 +0200454 description = 'usage' + extra_desc + ': ' + short
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200455 key1 = StorageTestData(version=self.version,
456 id=1, lifetime=0x00000001,
457 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
458 expected_usage=usage,
459 usage=usage, alg=0, alg2=0,
460 material=b'K',
461 description=description)
462 yield key1
463
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200464 if test_implicit_usage:
465 description = 'usage without implication' + ': ' + short
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200466 key2 = StorageTestData(version=self.version,
467 id=1, lifetime=0x00000001,
468 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
469 without_implicit_usage=True,
470 usage=usage, alg=0, alg2=0,
471 material=b'K',
472 description=description)
473 yield key2
Gilles Peskine897dff92021-03-10 15:03:44 +0100474
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200475 def generate_keys_for_usage_flags(self, **kwargs) -> Iterator[StorageTestData]:
Gilles Peskine897dff92021-03-10 15:03:44 +0100476 """Generate test keys covering usage flags."""
477 known_flags = sorted(self.constructors.key_usage_flags)
gabor-mezei-arm912eca32021-06-29 15:39:56 +0200478 yield from self.keys_for_usage_flags(['0'], **kwargs)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200479 for usage_flag in known_flags:
gabor-mezei-arm912eca32021-06-29 15:39:56 +0200480 yield from self.keys_for_usage_flags([usage_flag], **kwargs)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200481 for flag1, flag2 in zip(known_flags,
482 known_flags[1:] + [known_flags[0]]):
gabor-mezei-arm912eca32021-06-29 15:39:56 +0200483 yield from self.keys_for_usage_flags([flag1, flag2], **kwargs)
gabor-mezei-armbce85272021-06-24 14:38:51 +0200484
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200485 def generate_key_for_all_usage_flags(self) -> Iterator[StorageTestData]:
gabor-mezei-armbce85272021-06-24 14:38:51 +0200486 known_flags = sorted(self.constructors.key_usage_flags)
gabor-mezei-arm912eca32021-06-29 15:39:56 +0200487 yield from self.keys_for_usage_flags(known_flags, short='all known')
gabor-mezei-armbce85272021-06-24 14:38:51 +0200488
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200489 def all_keys_for_usage_flags(self) -> Iterator[StorageTestData]:
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200490 yield from self.generate_keys_for_usage_flags()
491 yield from self.generate_key_for_all_usage_flags()
Gilles Peskine897dff92021-03-10 15:03:44 +0100492
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100493 def keys_for_type(
494 self,
495 key_type: str,
496 params: Optional[Iterable[str]] = None
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200497 ) -> Iterator[StorageTestData]:
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100498 """Generate test keys for the given key type.
499
500 For key types that depend on a parameter (e.g. elliptic curve family),
501 `param` is the parameter to pass to the constructor. Only a single
502 parameter is supported.
503 """
504 kt = crypto_knowledge.KeyType(key_type, params)
505 for bits in kt.sizes_to_test():
506 usage_flags = 'PSA_KEY_USAGE_EXPORT'
507 alg = 0
508 alg2 = 0
509 key_material = kt.key_material(bits)
510 short_expression = re.sub(r'\bPSA_(?:KEY_TYPE|ECC_FAMILY)_',
511 r'',
512 kt.expression)
513 description = 'type: {} {}-bit'.format(short_expression, bits)
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200514 key = StorageTestData(version=self.version,
515 id=1, lifetime=0x00000001,
516 type=kt.expression, bits=bits,
517 usage=usage_flags, alg=alg, alg2=alg2,
518 material=key_material,
519 description=description)
520 yield key
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100521
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200522 def all_keys_for_types(self) -> Iterator[StorageTestData]:
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100523 """Generate test keys covering key types and their representations."""
Gilles Peskineb93f8542021-04-19 13:50:25 +0200524 key_types = sorted(self.constructors.key_types)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200525 for key_type in self.constructors.generate_expressions(key_types):
526 yield from self.keys_for_type(key_type)
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100527
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200528 def keys_for_algorithm(self, alg: str) -> Iterator[StorageTestData]:
Gilles Peskined86bc522021-03-10 15:08:57 +0100529 """Generate test keys for the specified algorithm."""
530 # For now, we don't have information on the compatibility of key
531 # types and algorithms. So we just test the encoding of algorithms,
532 # and not that operations can be performed with them.
Gilles Peskineff9629f2021-04-21 10:18:19 +0200533 descr = re.sub(r'PSA_ALG_', r'', alg)
534 descr = re.sub(r',', r', ', re.sub(r' +', r'', descr))
Gilles Peskined86bc522021-03-10 15:08:57 +0100535 usage = 'PSA_KEY_USAGE_EXPORT'
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200536 key1 = StorageTestData(version=self.version,
537 id=1, lifetime=0x00000001,
538 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
539 usage=usage, alg=alg, alg2=0,
540 material=b'K',
541 description='alg: ' + descr)
542 yield key1
543 key2 = StorageTestData(version=self.version,
544 id=1, lifetime=0x00000001,
545 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
546 usage=usage, alg=0, alg2=alg,
547 material=b'L',
548 description='alg2: ' + descr)
549 yield key2
Gilles Peskined86bc522021-03-10 15:08:57 +0100550
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200551 def all_keys_for_algorithms(self) -> Iterator[StorageTestData]:
Gilles Peskined86bc522021-03-10 15:08:57 +0100552 """Generate test keys covering algorithm encodings."""
Gilles Peskineb93f8542021-04-19 13:50:25 +0200553 algorithms = sorted(self.constructors.algorithms)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200554 for alg in self.constructors.generate_expressions(algorithms):
555 yield from self.keys_for_algorithm(alg)
Gilles Peskined86bc522021-03-10 15:08:57 +0100556
gabor-mezei-armea840de2021-06-29 15:42:57 +0200557 def generate_all_keys(self) -> Iterator[StorageTestData]:
gabor-mezei-arm8b0c91c2021-06-24 09:49:50 +0200558 """Generate all keys for the test cases."""
gabor-mezei-armea840de2021-06-29 15:42:57 +0200559 yield from self.all_keys_for_lifetimes()
560 yield from self.all_keys_for_usage_flags()
561 yield from self.all_keys_for_types()
562 yield from self.all_keys_for_algorithms()
gabor-mezei-arm8b0c91c2021-06-24 09:49:50 +0200563
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200564 def all_test_cases(self) -> Iterator[test_case.TestCase]:
Gilles Peskine897dff92021-03-10 15:03:44 +0100565 """Generate all storage format test cases."""
Gilles Peskineae9f14b2021-04-12 14:43:05 +0200566 # First build a list of all keys, then construct all the corresponding
567 # test cases. This allows all required information to be obtained in
568 # one go, which is a significant performance gain as the information
569 # includes numerical values obtained by compiling a C program.
Gilles Peskine3008c582021-07-06 21:05:52 +0200570 all_keys = list(self.generate_all_keys())
571 for key in all_keys:
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200572 if key.location_value() != 0:
573 # Skip keys with a non-default location, because they
574 # require a driver and we currently have no mechanism to
575 # determine whether a driver is available.
576 continue
577 yield self.make_test_case(key)
Gilles Peskine897dff92021-03-10 15:03:44 +0100578
gabor-mezei-arm4d9fb732021-06-24 09:53:26 +0200579class StorageFormatForward(StorageFormat):
580 """Storage format stability test cases for forward compatibility."""
581
582 def __init__(self, info: Information, version: int) -> None:
583 super().__init__(info, version, True)
584
585class StorageFormatV0(StorageFormat):
586 """Storage format stability test cases for version 0 compatibility."""
587
588 def __init__(self, info: Information) -> None:
589 super().__init__(info, 0, False)
Gilles Peskine897dff92021-03-10 15:03:44 +0100590
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200591 def all_keys_for_usage_flags(self) -> Iterator[StorageTestData]:
gabor-mezei-arm15c1f032021-06-24 10:04:38 +0200592 """Generate test keys covering usage flags."""
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200593 yield from self.generate_keys_for_usage_flags(test_implicit_usage=True)
594 yield from self.generate_key_for_all_usage_flags()
gabor-mezei-arm15c1f032021-06-24 10:04:38 +0200595
gabor-mezei-armacfcc182021-06-28 17:40:32 +0200596 def keys_for_implicit_usage(
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200597 self,
gabor-mezei-arme84d3212021-06-28 16:54:11 +0200598 implyer_usage: str,
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200599 alg: str,
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200600 key_type: crypto_knowledge.KeyType
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200601 ) -> StorageTestData:
gabor-mezei-armb92d61b2021-06-24 14:38:25 +0200602 # pylint: disable=too-many-locals
gabor-mezei-arm927742e2021-06-28 16:27:29 +0200603 """Generate test keys for the specified implicit usage flag,
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200604 algorithm and key type combination.
605 """
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200606 bits = key_type.sizes_to_test()[0]
gabor-mezei-arme84d3212021-06-28 16:54:11 +0200607 implicit_usage = StorageKey.IMPLICIT_USAGE_FLAGS[implyer_usage]
gabor-mezei-arm47812632021-06-28 16:35:48 +0200608 usage_flags = 'PSA_KEY_USAGE_EXPORT'
gabor-mezei-arme84d3212021-06-28 16:54:11 +0200609 material_usage_flags = usage_flags + ' | ' + implyer_usage
610 expected_usage_flags = material_usage_flags + ' | ' + implicit_usage
gabor-mezei-arm47812632021-06-28 16:35:48 +0200611 alg2 = 0
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200612 key_material = key_type.key_material(bits)
gabor-mezei-arme84d3212021-06-28 16:54:11 +0200613 usage_expression = re.sub(r'PSA_KEY_USAGE_', r'', implyer_usage)
gabor-mezei-arm47812632021-06-28 16:35:48 +0200614 alg_expression = re.sub(r'PSA_ALG_', r'', alg)
615 alg_expression = re.sub(r',', r', ', re.sub(r' +', r'', alg_expression))
616 key_type_expression = re.sub(r'\bPSA_(?:KEY_TYPE|ECC_FAMILY)_',
617 r'',
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200618 key_type.expression)
gabor-mezei-armacfcc182021-06-28 17:40:32 +0200619 description = 'implied by {}: {} {} {}-bit'.format(
gabor-mezei-arm47812632021-06-28 16:35:48 +0200620 usage_expression, alg_expression, key_type_expression, bits)
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200621 key = StorageTestData(version=self.version,
622 id=1, lifetime=0x00000001,
623 type=key_type.expression, bits=bits,
624 usage=material_usage_flags,
625 expected_usage=expected_usage_flags,
626 without_implicit_usage=True,
627 alg=alg, alg2=alg2,
628 material=key_material,
629 description=description)
630 return key
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200631
632 def gather_key_types_for_sign_alg(self) -> Dict[str, List[str]]:
gabor-mezei-armb92d61b2021-06-24 14:38:25 +0200633 # pylint: disable=too-many-locals
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200634 """Match possible key types for sign algorithms."""
635 # To create a valid combinaton both the algorithms and key types
636 # must be filtered. Pair them with keywords created from its names.
637 incompatible_alg_keyword = frozenset(['RAW', 'ANY', 'PURE'])
638 incompatible_key_type_keywords = frozenset(['MONTGOMERY'])
639 keyword_translation = {
640 'ECDSA': 'ECC',
641 'ED[0-9]*.*' : 'EDWARDS'
642 }
643 exclusive_keywords = {
644 'EDWARDS': 'ECC'
645 }
gabor-mezei-armb92d61b2021-06-24 14:38:25 +0200646 key_types = set(self.constructors.generate_expressions(self.constructors.key_types))
647 algorithms = set(self.constructors.generate_expressions(self.constructors.sign_algorithms))
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200648 alg_with_keys = {} #type: Dict[str, List[str]]
649 translation_table = str.maketrans('(', '_', ')')
650 for alg in algorithms:
651 # Generate keywords from the name of the algorithm
652 alg_keywords = set(alg.partition('(')[0].split(sep='_')[2:])
653 # Translate keywords for better matching with the key types
654 for keyword in alg_keywords.copy():
655 for pattern, replace in keyword_translation.items():
656 if re.match(pattern, keyword):
657 alg_keywords.remove(keyword)
658 alg_keywords.add(replace)
659 # Filter out incompatible algortihms
660 if not alg_keywords.isdisjoint(incompatible_alg_keyword):
661 continue
662
663 for key_type in key_types:
664 # Generate keywords from the of the key type
665 key_type_keywords = set(key_type.translate(translation_table).split(sep='_')[3:])
666
667 # Remove ambigious keywords
668 for keyword1, keyword2 in exclusive_keywords.items():
669 if keyword1 in key_type_keywords:
670 key_type_keywords.remove(keyword2)
671
672 if key_type_keywords.isdisjoint(incompatible_key_type_keywords) and\
673 not key_type_keywords.isdisjoint(alg_keywords):
674 if alg in alg_with_keys:
675 alg_with_keys[alg].append(key_type)
676 else:
677 alg_with_keys[alg] = [key_type]
678 return alg_with_keys
679
gabor-mezei-arme4b74992021-06-29 15:29:24 +0200680 def all_keys_for_implicit_usage(self) -> Iterator[StorageTestData]:
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200681 """Generate test keys for usage flag extensions."""
682 # Generate a key type and algorithm pair for each extendable usage
683 # flag to generate a valid key for exercising. The key is generated
684 # without usage extension to check the extension compatiblity.
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200685 alg_with_keys = self.gather_key_types_for_sign_alg()
gabor-mezei-arm7d2ec9a2021-06-24 16:35:01 +0200686
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200687 for usage in sorted(StorageKey.IMPLICIT_USAGE_FLAGS, key=str):
688 for alg in sorted(alg_with_keys):
689 for key_type in sorted(alg_with_keys[alg]):
690 # The key types must be filtered to fit the specific usage flag.
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200691 kt = crypto_knowledge.KeyType(key_type)
692 if kt.is_valid_for_signature(usage):
693 yield self.keys_for_implicit_usage(usage, alg, kt)
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200694
gabor-mezei-armea840de2021-06-29 15:42:57 +0200695 def generate_all_keys(self) -> Iterator[StorageTestData]:
696 yield from super().generate_all_keys()
697 yield from self.all_keys_for_implicit_usage()
gabor-mezei-arm15c1f032021-06-24 10:04:38 +0200698
Gilles Peskineb94ea512021-03-10 02:12:08 +0100699class TestGenerator:
700 """Generate test data."""
701
702 def __init__(self, options) -> None:
703 self.test_suite_directory = self.get_option(options, 'directory',
704 'tests/suites')
705 self.info = Information()
706
707 @staticmethod
708 def get_option(options, name: str, default: T) -> T:
709 value = getattr(options, name, None)
710 return default if value is None else value
711
Gilles Peskine0298bda2021-03-10 02:34:37 +0100712 def filename_for(self, basename: str) -> str:
713 """The location of the data file with the specified base name."""
714 return os.path.join(self.test_suite_directory, basename + '.data')
715
Gilles Peskineb94ea512021-03-10 02:12:08 +0100716 def write_test_data_file(self, basename: str,
717 test_cases: Iterable[test_case.TestCase]) -> None:
718 """Write the test cases to a .data file.
719
720 The output file is ``basename + '.data'`` in the test suite directory.
721 """
Gilles Peskine0298bda2021-03-10 02:34:37 +0100722 filename = self.filename_for(basename)
Gilles Peskineb94ea512021-03-10 02:12:08 +0100723 test_case.write_data_file(filename, test_cases)
724
Gilles Peskine0298bda2021-03-10 02:34:37 +0100725 TARGETS = {
Przemyslaw Stekiel997caf82021-10-15 15:21:51 +0200726 'test_suite_psa_crypto_generate_key.generated':
727 lambda info: KeyGenerate(info).test_cases_for_key_generation(),
Gilles Peskine0298bda2021-03-10 02:34:37 +0100728 'test_suite_psa_crypto_not_supported.generated':
Gilles Peskine3d778392021-02-17 15:11:05 +0100729 lambda info: NotSupported(info).test_cases_for_not_supported(),
Gilles Peskine897dff92021-03-10 15:03:44 +0100730 'test_suite_psa_crypto_storage_format.current':
gabor-mezei-arm4d9fb732021-06-24 09:53:26 +0200731 lambda info: StorageFormatForward(info, 0).all_test_cases(),
Gilles Peskine897dff92021-03-10 15:03:44 +0100732 'test_suite_psa_crypto_storage_format.v0':
gabor-mezei-arm4d9fb732021-06-24 09:53:26 +0200733 lambda info: StorageFormatV0(info).all_test_cases(),
Gilles Peskine0298bda2021-03-10 02:34:37 +0100734 } #type: Dict[str, Callable[[Information], Iterable[test_case.TestCase]]]
735
736 def generate_target(self, name: str) -> None:
737 test_cases = self.TARGETS[name](self.info)
738 self.write_test_data_file(name, test_cases)
Gilles Peskine14e428f2021-01-26 22:19:21 +0100739
Gilles Peskine09940492021-01-26 22:16:30 +0100740def main(args):
741 """Command line entry point."""
742 parser = argparse.ArgumentParser(description=__doc__)
Gilles Peskine0298bda2021-03-10 02:34:37 +0100743 parser.add_argument('--list', action='store_true',
744 help='List available targets and exit')
745 parser.add_argument('targets', nargs='*', metavar='TARGET',
746 help='Target file to generate (default: all; "-": none)')
Gilles Peskine09940492021-01-26 22:16:30 +0100747 options = parser.parse_args(args)
748 generator = TestGenerator(options)
Gilles Peskine0298bda2021-03-10 02:34:37 +0100749 if options.list:
750 for name in sorted(generator.TARGETS):
751 print(generator.filename_for(name))
752 return
753 if options.targets:
754 # Allow "-" as a special case so you can run
755 # ``generate_psa_tests.py - $targets`` and it works uniformly whether
756 # ``$targets`` is empty or not.
757 options.targets = [os.path.basename(re.sub(r'\.data\Z', r'', target))
758 for target in options.targets
759 if target != '-']
760 else:
761 options.targets = sorted(generator.TARGETS)
762 for target in options.targets:
763 generator.generate_target(target)
Gilles Peskine09940492021-01-26 22:16:30 +0100764
765if __name__ == '__main__':
766 main(sys.argv[1:])