blob: e7b37318114c6a18164c1b3e9db30ffc137c157a [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
Gilles Peskineb94ea512021-03-10 02:12:08 +0100130def test_case_for_key_type_not_supported(
131 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
145 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
152class NotSupported:
153 """Generate test cases for when something is not supported."""
154
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]:
Gilles Peskineaf172842021-01-27 18:24:48 +0100168 """Return test cases exercising key creation when the given type is unsupported.
169
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
172 conditioned on the base type not being supported.
173 """
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():
Gilles Peskine3d778392021-02-17 15:11:05 +0100189 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
Gilles Peskine3d778392021-02-17 15:11:05 +0100200 yield test_case_for_key_type_not_supported(
Gilles Peskine7f756872021-02-16 12:13:12 +0100201 'generate', kt.expression, bits,
202 finish_family_dependencies(generate_dependencies, bits),
Gilles Peskineaf172842021-01-27 18:24:48 +0100203 str(bits),
204 param_descr=param_descr,
Gilles Peskine3d778392021-02-17 15:11:05 +0100205 )
Gilles Peskine14e428f2021-01-26 22:19:21 +0100206 # To be added: derive
Gilles Peskine14e428f2021-01-26 22:19:21 +0100207
Gilles Peskineb93f8542021-04-19 13:50:25 +0200208 ECC_KEY_TYPES = ('PSA_KEY_TYPE_ECC_KEY_PAIR',
209 'PSA_KEY_TYPE_ECC_PUBLIC_KEY')
210
Gilles Peskine3d778392021-02-17 15:11:05 +0100211 def test_cases_for_not_supported(self) -> Iterator[test_case.TestCase]:
Gilles Peskine14e428f2021-01-26 22:19:21 +0100212 """Generate test cases that exercise the creation of keys of unsupported types."""
Gilles Peskine14e428f2021-01-26 22:19:21 +0100213 for key_type in sorted(self.constructors.key_types):
Gilles Peskineb93f8542021-04-19 13:50:25 +0200214 if key_type in self.ECC_KEY_TYPES:
215 continue
Gilles Peskine14e428f2021-01-26 22:19:21 +0100216 kt = crypto_knowledge.KeyType(key_type)
Gilles Peskine3d778392021-02-17 15:11:05 +0100217 yield from self.test_cases_for_key_type_not_supported(kt)
Gilles Peskineaf172842021-01-27 18:24:48 +0100218 for curve_family in sorted(self.constructors.ecc_curves):
Gilles Peskineb93f8542021-04-19 13:50:25 +0200219 for constr in self.ECC_KEY_TYPES:
Gilles Peskineaf172842021-01-27 18:24:48 +0100220 kt = crypto_knowledge.KeyType(constr, [curve_family])
Gilles Peskine3d778392021-02-17 15:11:05 +0100221 yield from self.test_cases_for_key_type_not_supported(
Gilles Peskineaf172842021-01-27 18:24:48 +0100222 kt, param_descr='type')
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, 0, param_descr='curve')
Gilles Peskineb94ea512021-03-10 02:12:08 +0100225
226
Gilles Peskine897dff92021-03-10 15:03:44 +0100227class StorageKey(psa_storage.Key):
228 """Representation of a key for storage format testing."""
229
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200230 def __init__(
231 self,
232 description: str,
233 expected_usage: Optional[str] = None,
234 **kwargs
235 ) -> None:
236 """Prepare to generate a key.
237
238 * `description`: used for the the test case names
gabor-mezei-armacfcc182021-06-28 17:40:32 +0200239 * `implicit_usage`: the usage flags generated as the expected usage
240 flags in the test cases. When testing implicit
241 usage flags, they can differ in the generated keys
242 and the expected usage flags in the test cases.
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200243 """
Gilles Peskine897dff92021-03-10 15:03:44 +0100244 super().__init__(**kwargs)
245 self.description = description #type: str
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200246 self.usage = psa_storage.as_expr(expected_usage) if expected_usage is not None else\
247 self.original_usage #type: psa_storage.Expr
gabor-mezei-arm15c1f032021-06-24 10:04:38 +0200248
Gilles Peskine897dff92021-03-10 15:03:44 +0100249class StorageFormat:
250 """Storage format stability test cases."""
251
252 def __init__(self, info: Information, version: int, forward: bool) -> None:
253 """Prepare to generate test cases for storage format stability.
254
255 * `info`: information about the API. See the `Information` class.
256 * `version`: the storage format version to generate test cases for.
257 * `forward`: if true, generate forward compatibility test cases which
258 save a key and check that its representation is as intended. Otherwise
259 generate backward compatibility test cases which inject a key
260 representation and check that it can be read and used.
261 """
gabor-mezei-arm0bdb84e2021-06-23 17:01:44 +0200262 self.constructors = info.constructors #type: macro_collector.PSAMacroEnumerator
263 self.version = version #type: int
264 self.forward = forward #type: bool
Gilles Peskine897dff92021-03-10 15:03:44 +0100265
266 def make_test_case(self, key: StorageKey) -> test_case.TestCase:
267 """Construct a storage format test case for the given key.
268
269 If ``forward`` is true, generate a forward compatibility test case:
270 create a key and validate that it has the expected representation.
271 Otherwise generate a backward compatibility test case: inject the
272 key representation into storage and validate that it can be read
273 correctly.
274 """
275 verb = 'save' if self.forward else 'read'
276 tc = test_case.TestCase()
277 tc.set_description('PSA storage {}: {}'.format(verb, key.description))
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100278 dependencies = automatic_dependencies(
279 key.lifetime.string, key.type.string,
280 key.usage.string, key.alg.string, key.alg2.string,
281 )
282 dependencies = finish_family_dependencies(dependencies, key.bits)
283 tc.set_dependencies(dependencies)
Gilles Peskine897dff92021-03-10 15:03:44 +0100284 tc.set_function('key_storage_' + verb)
285 if self.forward:
286 extra_arguments = []
287 else:
Gilles Peskine643eb832021-04-21 20:11:33 +0200288 flags = []
Gilles Peskine897dff92021-03-10 15:03:44 +0100289 # Some test keys have the RAW_DATA type and attributes that don't
290 # necessarily make sense. We do this to validate numerical
291 # encodings of the attributes.
292 # Raw data keys have no useful exercise anyway so there is no
293 # loss of test coverage.
Gilles Peskine643eb832021-04-21 20:11:33 +0200294 if key.type.string != 'PSA_KEY_TYPE_RAW_DATA':
295 flags.append('TEST_FLAG_EXERCISE')
296 if 'READ_ONLY' in key.lifetime.string:
297 flags.append('TEST_FLAG_READ_ONLY')
298 extra_arguments = [' | '.join(flags) if flags else '0']
Gilles Peskine897dff92021-03-10 15:03:44 +0100299 tc.set_arguments([key.lifetime.string,
300 key.type.string, str(key.bits),
301 key.usage.string, key.alg.string, key.alg2.string,
302 '"' + key.material.hex() + '"',
303 '"' + key.hex() + '"',
304 *extra_arguments])
305 return tc
306
Gilles Peskineefb584d2021-04-21 22:05:34 +0200307 def key_for_lifetime(
308 self,
309 lifetime: str,
310 ) -> StorageKey:
311 """Construct a test key for the given lifetime."""
312 short = lifetime
313 short = re.sub(r'PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION',
314 r'', short)
315 short = re.sub(r'PSA_KEY_[A-Z]+_', r'', short)
316 description = 'lifetime: ' + short
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200317 return StorageKey(version=self.version,
318 id=1, lifetime=lifetime,
319 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
320 usage='PSA_KEY_USAGE_EXPORT', alg=0, alg2=0,
321 material=b'L',
322 description=description)
Gilles Peskineefb584d2021-04-21 22:05:34 +0200323
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200324 def all_keys_for_lifetimes(self) -> Iterator[StorageKey]:
Gilles Peskineefb584d2021-04-21 22:05:34 +0200325 """Generate test keys covering lifetimes."""
326 lifetimes = sorted(self.constructors.lifetimes)
327 expressions = self.constructors.generate_expressions(lifetimes)
328 for lifetime in expressions:
329 # Don't attempt to create or load a volatile key in storage
330 if 'VOLATILE' in lifetime:
331 continue
332 # Don't attempt to create a read-only key in storage,
333 # but do attempt to load one.
334 if 'READ_ONLY' in lifetime and self.forward:
335 continue
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200336 yield self.key_for_lifetime(lifetime)
Gilles Peskineefb584d2021-04-21 22:05:34 +0200337
Gilles Peskine897dff92021-03-10 15:03:44 +0100338 def key_for_usage_flags(
339 self,
340 usage_flags: List[str],
gabor-mezei-armd71659f2021-06-24 09:42:02 +0200341 short: Optional[str] = None,
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200342 test_implicit_usage: Optional[bool] = False
343 ) -> Iterator[StorageKey]:
Gilles Peskine897dff92021-03-10 15:03:44 +0100344 """Construct a test key for the given key usage."""
345 usage = ' | '.join(usage_flags) if usage_flags else '0'
346 if short is None:
347 short = re.sub(r'\bPSA_KEY_USAGE_', r'', usage)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200348 extra_desc = ' with implication' if test_implicit_usage else ''
gabor-mezei-armd71659f2021-06-24 09:42:02 +0200349 description = 'usage' + extra_desc + ': ' + short
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200350 yield StorageKey(version=self.version,
351 id=1, lifetime=0x00000001,
352 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
353 usage=usage, alg=0, alg2=0,
354 material=b'K',
355 description=description,
356 implicit_usage=True)
357 if test_implicit_usage:
358 description = 'usage without implication' + ': ' + short
359 yield StorageKey(version=self.version,
360 id=1, lifetime=0x00000001,
361 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
362 usage=usage, alg=0, alg2=0,
363 material=b'K',
364 description=description,
365 implicit_usage=False)
Gilles Peskine897dff92021-03-10 15:03:44 +0100366
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200367
368 def generate_keys_for_usage_flags(self, **kwargs) -> Iterator[StorageKey]:
Gilles Peskine897dff92021-03-10 15:03:44 +0100369 """Generate test keys covering usage flags."""
370 known_flags = sorted(self.constructors.key_usage_flags)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200371 yield from self.key_for_usage_flags(['0'], **kwargs)
372 for usage_flag in known_flags:
373 yield from self.key_for_usage_flags([usage_flag], **kwargs)
374 for flag1, flag2 in zip(known_flags,
375 known_flags[1:] + [known_flags[0]]):
376 yield from self.key_for_usage_flags([flag1, flag2], **kwargs)
gabor-mezei-armbce85272021-06-24 14:38:51 +0200377
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200378 def generate_key_for_all_usage_flags(self) -> Iterator[StorageKey]:
gabor-mezei-armbce85272021-06-24 14:38:51 +0200379 known_flags = sorted(self.constructors.key_usage_flags)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200380 yield from self.key_for_usage_flags(known_flags, short='all known')
gabor-mezei-armbce85272021-06-24 14:38:51 +0200381
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200382 def all_keys_for_usage_flags(self) -> Iterator[StorageKey]:
383 yield from self.generate_keys_for_usage_flags()
384 yield from self.generate_key_for_all_usage_flags()
Gilles Peskine897dff92021-03-10 15:03:44 +0100385
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100386 def keys_for_type(
387 self,
388 key_type: str,
389 params: Optional[Iterable[str]] = None
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200390 ) -> Iterator[StorageKey]:
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100391 """Generate test keys for the given key type.
392
393 For key types that depend on a parameter (e.g. elliptic curve family),
394 `param` is the parameter to pass to the constructor. Only a single
395 parameter is supported.
396 """
397 kt = crypto_knowledge.KeyType(key_type, params)
398 for bits in kt.sizes_to_test():
399 usage_flags = 'PSA_KEY_USAGE_EXPORT'
400 alg = 0
401 alg2 = 0
402 key_material = kt.key_material(bits)
403 short_expression = re.sub(r'\bPSA_(?:KEY_TYPE|ECC_FAMILY)_',
404 r'',
405 kt.expression)
406 description = 'type: {} {}-bit'.format(short_expression, bits)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200407 yield StorageKey(version=self.version,
408 id=1, lifetime=0x00000001,
409 type=kt.expression, bits=bits,
410 usage=usage_flags, alg=alg, alg2=alg2,
411 material=key_material,
412 description=description)
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100413
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200414 def all_keys_for_types(self) -> Iterator[StorageKey]:
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100415 """Generate test keys covering key types and their representations."""
Gilles Peskineb93f8542021-04-19 13:50:25 +0200416 key_types = sorted(self.constructors.key_types)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200417 for key_type in self.constructors.generate_expressions(key_types):
418 yield from self.keys_for_type(key_type)
Gilles Peskinef8223ab2021-03-10 15:07:16 +0100419
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200420 def keys_for_algorithm(self, alg: str) -> Iterator[StorageKey]:
Gilles Peskined86bc522021-03-10 15:08:57 +0100421 """Generate test keys for the specified algorithm."""
422 # For now, we don't have information on the compatibility of key
423 # types and algorithms. So we just test the encoding of algorithms,
424 # and not that operations can be performed with them.
Gilles Peskineff9629f2021-04-21 10:18:19 +0200425 descr = re.sub(r'PSA_ALG_', r'', alg)
426 descr = re.sub(r',', r', ', re.sub(r' +', r'', descr))
Gilles Peskined86bc522021-03-10 15:08:57 +0100427 usage = 'PSA_KEY_USAGE_EXPORT'
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200428 yield StorageKey(version=self.version,
429 id=1, lifetime=0x00000001,
430 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
431 usage=usage, alg=alg, alg2=0,
432 material=b'K',
433 description='alg: ' + descr)
434 yield StorageKey(version=self.version,
435 id=1, lifetime=0x00000001,
436 type='PSA_KEY_TYPE_RAW_DATA', bits=8,
437 usage=usage, alg=0, alg2=alg,
438 material=b'L',
439 description='alg2: ' + descr)
Gilles Peskined86bc522021-03-10 15:08:57 +0100440
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200441 def all_keys_for_algorithms(self) -> Iterator[StorageKey]:
Gilles Peskined86bc522021-03-10 15:08:57 +0100442 """Generate test keys covering algorithm encodings."""
Gilles Peskineb93f8542021-04-19 13:50:25 +0200443 algorithms = sorted(self.constructors.algorithms)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200444 for alg in self.constructors.generate_expressions(algorithms):
445 yield from self.keys_for_algorithm(alg)
Gilles Peskined86bc522021-03-10 15:08:57 +0100446
gabor-mezei-arm8b0c91c2021-06-24 09:49:50 +0200447 def generate_all_keys(self) -> List[StorageKey]:
448 """Generate all keys for the test cases."""
449 keys = [] #type: List[StorageKey]
450 keys += self.all_keys_for_lifetimes()
451 keys += self.all_keys_for_usage_flags()
452 keys += self.all_keys_for_types()
453 keys += self.all_keys_for_algorithms()
454 return keys
455
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200456 def all_test_cases(self) -> Iterator[test_case.TestCase]:
Gilles Peskine897dff92021-03-10 15:03:44 +0100457 """Generate all storage format test cases."""
Gilles Peskineae9f14b2021-04-12 14:43:05 +0200458 # First build a list of all keys, then construct all the corresponding
459 # test cases. This allows all required information to be obtained in
460 # one go, which is a significant performance gain as the information
461 # includes numerical values obtained by compiling a C program.
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200462 for key in self.generate_all_keys():
463 if key.location_value() != 0:
464 # Skip keys with a non-default location, because they
465 # require a driver and we currently have no mechanism to
466 # determine whether a driver is available.
467 continue
468 yield self.make_test_case(key)
Gilles Peskine897dff92021-03-10 15:03:44 +0100469
gabor-mezei-arm4d9fb732021-06-24 09:53:26 +0200470class StorageFormatForward(StorageFormat):
471 """Storage format stability test cases for forward compatibility."""
472
473 def __init__(self, info: Information, version: int) -> None:
474 super().__init__(info, version, True)
475
476class StorageFormatV0(StorageFormat):
477 """Storage format stability test cases for version 0 compatibility."""
478
479 def __init__(self, info: Information) -> None:
480 super().__init__(info, 0, False)
Gilles Peskine897dff92021-03-10 15:03:44 +0100481
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200482 def all_keys_for_usage_flags(self) -> Iterator[StorageKey]:
gabor-mezei-arm15c1f032021-06-24 10:04:38 +0200483 """Generate test keys covering usage flags."""
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200484 yield from self.generate_keys_for_usage_flags(test_implicit_usage=True)
485 yield from self.generate_key_for_all_usage_flags()
gabor-mezei-arm15c1f032021-06-24 10:04:38 +0200486
gabor-mezei-armacfcc182021-06-28 17:40:32 +0200487 def keys_for_implicit_usage(
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200488 self,
gabor-mezei-arme84d3212021-06-28 16:54:11 +0200489 implyer_usage: str,
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200490 alg: str,
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200491 key_type: crypto_knowledge.KeyType
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200492 ) -> StorageKey:
gabor-mezei-armb92d61b2021-06-24 14:38:25 +0200493 # pylint: disable=too-many-locals
gabor-mezei-arm927742e2021-06-28 16:27:29 +0200494 """Generate test keys for the specified implicit usage flag,
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200495 algorithm and key type combination.
496 """
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200497 bits = key_type.sizes_to_test()[0]
gabor-mezei-arme84d3212021-06-28 16:54:11 +0200498 implicit_usage = StorageKey.IMPLICIT_USAGE_FLAGS[implyer_usage]
gabor-mezei-arm47812632021-06-28 16:35:48 +0200499 usage_flags = 'PSA_KEY_USAGE_EXPORT'
gabor-mezei-arme84d3212021-06-28 16:54:11 +0200500 material_usage_flags = usage_flags + ' | ' + implyer_usage
501 expected_usage_flags = material_usage_flags + ' | ' + implicit_usage
gabor-mezei-arm47812632021-06-28 16:35:48 +0200502 alg2 = 0
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200503 key_material = key_type.key_material(bits)
gabor-mezei-arme84d3212021-06-28 16:54:11 +0200504 usage_expression = re.sub(r'PSA_KEY_USAGE_', r'', implyer_usage)
gabor-mezei-arm47812632021-06-28 16:35:48 +0200505 alg_expression = re.sub(r'PSA_ALG_', r'', alg)
506 alg_expression = re.sub(r',', r', ', re.sub(r' +', r'', alg_expression))
507 key_type_expression = re.sub(r'\bPSA_(?:KEY_TYPE|ECC_FAMILY)_',
508 r'',
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200509 key_type.expression)
gabor-mezei-armacfcc182021-06-28 17:40:32 +0200510 description = 'implied by {}: {} {} {}-bit'.format(
gabor-mezei-arm47812632021-06-28 16:35:48 +0200511 usage_expression, alg_expression, key_type_expression, bits)
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200512 return StorageKey(version=self.version,
513 id=1, lifetime=0x00000001,
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200514 type=key_type.expression, bits=bits,
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200515 usage=material_usage_flags,
516 expected_usage=expected_usage_flags,
517 alg=alg, alg2=alg2,
518 material=key_material,
519 description=description,
520 implicit_usage=False)
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200521
522 def gather_key_types_for_sign_alg(self) -> Dict[str, List[str]]:
gabor-mezei-armb92d61b2021-06-24 14:38:25 +0200523 # pylint: disable=too-many-locals
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200524 """Match possible key types for sign algorithms."""
525 # To create a valid combinaton both the algorithms and key types
526 # must be filtered. Pair them with keywords created from its names.
527 incompatible_alg_keyword = frozenset(['RAW', 'ANY', 'PURE'])
528 incompatible_key_type_keywords = frozenset(['MONTGOMERY'])
529 keyword_translation = {
530 'ECDSA': 'ECC',
531 'ED[0-9]*.*' : 'EDWARDS'
532 }
533 exclusive_keywords = {
534 'EDWARDS': 'ECC'
535 }
gabor-mezei-armb92d61b2021-06-24 14:38:25 +0200536 key_types = set(self.constructors.generate_expressions(self.constructors.key_types))
537 algorithms = set(self.constructors.generate_expressions(self.constructors.sign_algorithms))
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200538 alg_with_keys = {} #type: Dict[str, List[str]]
539 translation_table = str.maketrans('(', '_', ')')
540 for alg in algorithms:
541 # Generate keywords from the name of the algorithm
542 alg_keywords = set(alg.partition('(')[0].split(sep='_')[2:])
543 # Translate keywords for better matching with the key types
544 for keyword in alg_keywords.copy():
545 for pattern, replace in keyword_translation.items():
546 if re.match(pattern, keyword):
547 alg_keywords.remove(keyword)
548 alg_keywords.add(replace)
549 # Filter out incompatible algortihms
550 if not alg_keywords.isdisjoint(incompatible_alg_keyword):
551 continue
552
553 for key_type in key_types:
554 # Generate keywords from the of the key type
555 key_type_keywords = set(key_type.translate(translation_table).split(sep='_')[3:])
556
557 # Remove ambigious keywords
558 for keyword1, keyword2 in exclusive_keywords.items():
559 if keyword1 in key_type_keywords:
560 key_type_keywords.remove(keyword2)
561
562 if key_type_keywords.isdisjoint(incompatible_key_type_keywords) and\
563 not key_type_keywords.isdisjoint(alg_keywords):
564 if alg in alg_with_keys:
565 alg_with_keys[alg].append(key_type)
566 else:
567 alg_with_keys[alg] = [key_type]
568 return alg_with_keys
569
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200570 def all_keys_for_implicit_usage(self) -> Iterator[StorageKey]:
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200571 """Generate test keys for usage flag extensions."""
572 # Generate a key type and algorithm pair for each extendable usage
573 # flag to generate a valid key for exercising. The key is generated
574 # without usage extension to check the extension compatiblity.
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200575 alg_with_keys = self.gather_key_types_for_sign_alg()
gabor-mezei-arm7d2ec9a2021-06-24 16:35:01 +0200576
gabor-mezei-arm5ea30372021-06-28 19:26:55 +0200577 for usage in sorted(StorageKey.IMPLICIT_USAGE_FLAGS, key=str):
578 for alg in sorted(alg_with_keys):
579 for key_type in sorted(alg_with_keys[alg]):
580 # The key types must be filtered to fit the specific usage flag.
gabor-mezei-arm805c7352021-06-28 20:02:11 +0200581 kt = crypto_knowledge.KeyType(key_type)
582 if kt.is_valid_for_signature(usage):
583 yield self.keys_for_implicit_usage(usage, alg, kt)
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200584
585 def generate_all_keys(self) -> List[StorageKey]:
586 keys = super().generate_all_keys()
gabor-mezei-armacfcc182021-06-28 17:40:32 +0200587 keys += self.all_keys_for_implicit_usage()
gabor-mezei-arm044fefc2021-06-24 10:16:44 +0200588 return keys
gabor-mezei-arm15c1f032021-06-24 10:04:38 +0200589
Gilles Peskineb94ea512021-03-10 02:12:08 +0100590class TestGenerator:
591 """Generate test data."""
592
593 def __init__(self, options) -> None:
594 self.test_suite_directory = self.get_option(options, 'directory',
595 'tests/suites')
596 self.info = Information()
597
598 @staticmethod
599 def get_option(options, name: str, default: T) -> T:
600 value = getattr(options, name, None)
601 return default if value is None else value
602
Gilles Peskine0298bda2021-03-10 02:34:37 +0100603 def filename_for(self, basename: str) -> str:
604 """The location of the data file with the specified base name."""
605 return os.path.join(self.test_suite_directory, basename + '.data')
606
Gilles Peskineb94ea512021-03-10 02:12:08 +0100607 def write_test_data_file(self, basename: str,
608 test_cases: Iterable[test_case.TestCase]) -> None:
609 """Write the test cases to a .data file.
610
611 The output file is ``basename + '.data'`` in the test suite directory.
612 """
Gilles Peskine0298bda2021-03-10 02:34:37 +0100613 filename = self.filename_for(basename)
Gilles Peskineb94ea512021-03-10 02:12:08 +0100614 test_case.write_data_file(filename, test_cases)
615
Gilles Peskine0298bda2021-03-10 02:34:37 +0100616 TARGETS = {
617 'test_suite_psa_crypto_not_supported.generated':
Gilles Peskine3d778392021-02-17 15:11:05 +0100618 lambda info: NotSupported(info).test_cases_for_not_supported(),
Gilles Peskine897dff92021-03-10 15:03:44 +0100619 'test_suite_psa_crypto_storage_format.current':
gabor-mezei-arm4d9fb732021-06-24 09:53:26 +0200620 lambda info: StorageFormatForward(info, 0).all_test_cases(),
Gilles Peskine897dff92021-03-10 15:03:44 +0100621 'test_suite_psa_crypto_storage_format.v0':
gabor-mezei-arm4d9fb732021-06-24 09:53:26 +0200622 lambda info: StorageFormatV0(info).all_test_cases(),
Gilles Peskine0298bda2021-03-10 02:34:37 +0100623 } #type: Dict[str, Callable[[Information], Iterable[test_case.TestCase]]]
624
625 def generate_target(self, name: str) -> None:
626 test_cases = self.TARGETS[name](self.info)
627 self.write_test_data_file(name, test_cases)
Gilles Peskine14e428f2021-01-26 22:19:21 +0100628
Gilles Peskine09940492021-01-26 22:16:30 +0100629def main(args):
630 """Command line entry point."""
631 parser = argparse.ArgumentParser(description=__doc__)
Gilles Peskine0298bda2021-03-10 02:34:37 +0100632 parser.add_argument('--list', action='store_true',
633 help='List available targets and exit')
634 parser.add_argument('targets', nargs='*', metavar='TARGET',
635 help='Target file to generate (default: all; "-": none)')
Gilles Peskine09940492021-01-26 22:16:30 +0100636 options = parser.parse_args(args)
637 generator = TestGenerator(options)
Gilles Peskine0298bda2021-03-10 02:34:37 +0100638 if options.list:
639 for name in sorted(generator.TARGETS):
640 print(generator.filename_for(name))
641 return
642 if options.targets:
643 # Allow "-" as a special case so you can run
644 # ``generate_psa_tests.py - $targets`` and it works uniformly whether
645 # ``$targets`` is empty or not.
646 options.targets = [os.path.basename(re.sub(r'\.data\Z', r'', target))
647 for target in options.targets
648 if target != '-']
649 else:
650 options.targets = sorted(generator.TARGETS)
651 for target in options.targets:
652 generator.generate_target(target)
Gilles Peskine09940492021-01-26 22:16:30 +0100653
654if __name__ == '__main__':
655 main(sys.argv[1:])