2021-01-26 22:16:30 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Generate test data for PSA cryptographic mechanisms.
|
2021-03-10 02:34:37 +01:00
|
|
|
|
|
|
|
With no arguments, generate all test data. With non-option arguments,
|
|
|
|
generate only the specified files.
|
2021-01-26 22:16:30 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Copyright The Mbed TLS Contributors
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import argparse
|
2022-03-15 17:26:33 +01:00
|
|
|
import enum
|
2021-01-26 22:19:21 +01:00
|
|
|
import os
|
2021-05-07 11:49:17 +02:00
|
|
|
import posixpath
|
2021-01-26 22:19:21 +01:00
|
|
|
import re
|
2021-01-26 22:16:30 +01:00
|
|
|
import sys
|
2021-02-17 15:11:05 +01:00
|
|
|
from typing import Callable, Dict, FrozenSet, Iterable, Iterator, List, Optional, TypeVar
|
2021-01-26 22:16:30 +01:00
|
|
|
|
|
|
|
import scripts_path # pylint: disable=unused-import
|
2021-04-22 00:20:47 +02:00
|
|
|
from mbedtls_dev import build_tree
|
2021-01-26 22:19:21 +01:00
|
|
|
from mbedtls_dev import crypto_knowledge
|
2021-01-26 22:16:30 +01:00
|
|
|
from mbedtls_dev import macro_collector
|
2021-03-10 15:03:44 +01:00
|
|
|
from mbedtls_dev import psa_storage
|
2021-01-26 22:19:21 +01:00
|
|
|
from mbedtls_dev import test_case
|
2021-01-26 22:16:30 +01:00
|
|
|
|
|
|
|
T = TypeVar('T') #pylint: disable=invalid-name
|
|
|
|
|
2021-01-26 22:19:21 +01:00
|
|
|
|
2021-02-16 12:13:12 +01:00
|
|
|
def psa_want_symbol(name: str) -> str:
|
2021-01-27 18:24:48 +01:00
|
|
|
"""Return the PSA_WANT_xxx symbol associated with a PSA crypto feature."""
|
|
|
|
if name.startswith('PSA_'):
|
|
|
|
return name[:4] + 'WANT_' + name[4:]
|
|
|
|
else:
|
|
|
|
raise ValueError('Unable to determine the PSA_WANT_ symbol for ' + name)
|
|
|
|
|
2021-02-16 12:13:12 +01:00
|
|
|
def finish_family_dependency(dep: str, bits: int) -> str:
|
|
|
|
"""Finish dep if it's a family dependency symbol prefix.
|
|
|
|
|
|
|
|
A family dependency symbol prefix is a PSA_WANT_ symbol that needs to be
|
|
|
|
qualified by the key size. If dep is such a symbol, finish it by adjusting
|
|
|
|
the prefix and appending the key size. Other symbols are left unchanged.
|
|
|
|
"""
|
|
|
|
return re.sub(r'_FAMILY_(.*)', r'_\1_' + str(bits), dep)
|
|
|
|
|
|
|
|
def finish_family_dependencies(dependencies: List[str], bits: int) -> List[str]:
|
|
|
|
"""Finish any family dependency symbol prefixes.
|
|
|
|
|
|
|
|
Apply `finish_family_dependency` to each element of `dependencies`.
|
|
|
|
"""
|
|
|
|
return [finish_family_dependency(dep, bits) for dep in dependencies]
|
2021-01-27 18:24:48 +01:00
|
|
|
|
2021-04-20 23:23:45 +02:00
|
|
|
SYMBOLS_WITHOUT_DEPENDENCY = frozenset([
|
|
|
|
'PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG', # modifier, only in policies
|
|
|
|
'PSA_ALG_AEAD_WITH_SHORTENED_TAG', # modifier
|
|
|
|
'PSA_ALG_ANY_HASH', # only in policies
|
|
|
|
'PSA_ALG_AT_LEAST_THIS_LENGTH_MAC', # modifier, only in policies
|
|
|
|
'PSA_ALG_KEY_AGREEMENT', # chaining
|
|
|
|
'PSA_ALG_TRUNCATED_MAC', # modifier
|
|
|
|
])
|
2021-03-10 15:07:16 +01:00
|
|
|
def automatic_dependencies(*expressions: str) -> List[str]:
|
|
|
|
"""Infer dependencies of a test case by looking for PSA_xxx symbols.
|
|
|
|
|
|
|
|
The arguments are strings which should be C expressions. Do not use
|
|
|
|
string literals or comments as this function is not smart enough to
|
|
|
|
skip them.
|
|
|
|
"""
|
|
|
|
used = set()
|
|
|
|
for expr in expressions:
|
|
|
|
used.update(re.findall(r'PSA_(?:ALG|ECC_FAMILY|KEY_TYPE)_\w+', expr))
|
2021-04-20 23:23:45 +02:00
|
|
|
used.difference_update(SYMBOLS_WITHOUT_DEPENDENCY)
|
2021-03-10 15:07:16 +01:00
|
|
|
return sorted(psa_want_symbol(name) for name in used)
|
|
|
|
|
2021-02-16 14:16:25 +01:00
|
|
|
# A temporary hack: at the time of writing, not all dependency symbols
|
|
|
|
# are implemented yet. Skip test cases for which the dependency symbols are
|
|
|
|
# not available. Once all dependency symbols are available, this hack must
|
|
|
|
# be removed so that a bug in the dependency symbols proprely leads to a test
|
|
|
|
# failure.
|
|
|
|
def read_implemented_dependencies(filename: str) -> FrozenSet[str]:
|
|
|
|
return frozenset(symbol
|
|
|
|
for line in open(filename)
|
|
|
|
for symbol in re.findall(r'\bPSA_WANT_\w+\b', line))
|
2021-04-22 00:20:47 +02:00
|
|
|
_implemented_dependencies = None #type: Optional[FrozenSet[str]] #pylint: disable=invalid-name
|
2021-02-16 14:16:25 +01:00
|
|
|
def hack_dependencies_not_implemented(dependencies: List[str]) -> None:
|
2021-04-22 00:20:47 +02:00
|
|
|
global _implemented_dependencies #pylint: disable=global-statement,invalid-name
|
|
|
|
if _implemented_dependencies is None:
|
|
|
|
_implemented_dependencies = \
|
|
|
|
read_implemented_dependencies('include/psa/crypto_config.h')
|
2021-10-22 10:39:56 +02:00
|
|
|
if not all((dep.lstrip('!') in _implemented_dependencies or 'PSA_WANT' not in dep)
|
2021-02-16 14:16:25 +01:00
|
|
|
for dep in dependencies):
|
|
|
|
dependencies.append('DEPENDENCY_NOT_IMPLEMENTED_YET')
|
|
|
|
|
2021-01-26 22:19:21 +01:00
|
|
|
|
2021-03-10 02:12:08 +01:00
|
|
|
class Information:
|
|
|
|
"""Gather information about PSA constructors."""
|
2021-01-26 22:16:30 +01:00
|
|
|
|
2021-03-10 02:12:08 +01:00
|
|
|
def __init__(self) -> None:
|
2021-01-26 22:16:30 +01:00
|
|
|
self.constructors = self.read_psa_interface()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def remove_unwanted_macros(
|
2021-04-19 13:50:25 +02:00
|
|
|
constructors: macro_collector.PSAMacroEnumerator
|
2021-01-26 22:16:30 +01:00
|
|
|
) -> None:
|
2021-04-19 13:50:25 +02:00
|
|
|
# Mbed TLS doesn't support finite-field DH yet and will not support
|
|
|
|
# finite-field DSA. Don't attempt to generate any related test case.
|
|
|
|
constructors.key_types.discard('PSA_KEY_TYPE_DH_KEY_PAIR')
|
|
|
|
constructors.key_types.discard('PSA_KEY_TYPE_DH_PUBLIC_KEY')
|
2021-01-26 22:16:30 +01:00
|
|
|
constructors.key_types.discard('PSA_KEY_TYPE_DSA_KEY_PAIR')
|
|
|
|
constructors.key_types.discard('PSA_KEY_TYPE_DSA_PUBLIC_KEY')
|
|
|
|
|
2021-04-19 13:50:25 +02:00
|
|
|
def read_psa_interface(self) -> macro_collector.PSAMacroEnumerator:
|
2021-01-26 22:16:30 +01:00
|
|
|
"""Return the list of known key types, algorithms, etc."""
|
2021-03-30 21:46:35 +02:00
|
|
|
constructors = macro_collector.InputsForTest()
|
2021-01-26 22:16:30 +01:00
|
|
|
header_file_names = ['include/psa/crypto_values.h',
|
|
|
|
'include/psa/crypto_extra.h']
|
2021-04-19 13:50:25 +02:00
|
|
|
test_suites = ['tests/suites/test_suite_psa_crypto_metadata.data']
|
2021-01-26 22:16:30 +01:00
|
|
|
for header_file_name in header_file_names:
|
2021-04-19 13:50:25 +02:00
|
|
|
constructors.parse_header(header_file_name)
|
|
|
|
for test_cases in test_suites:
|
|
|
|
constructors.parse_test_cases(test_cases)
|
2021-01-26 22:16:30 +01:00
|
|
|
self.remove_unwanted_macros(constructors)
|
2021-03-30 21:46:35 +02:00
|
|
|
constructors.gather_arguments()
|
2021-01-26 22:16:30 +01:00
|
|
|
return constructors
|
|
|
|
|
2021-01-26 22:19:21 +01:00
|
|
|
|
2021-10-11 10:15:25 +02:00
|
|
|
def test_case_for_key_type_not_supported(
|
2021-03-10 02:12:08 +01:00
|
|
|
verb: str, key_type: str, bits: int,
|
|
|
|
dependencies: List[str],
|
|
|
|
*args: str,
|
|
|
|
param_descr: str = ''
|
|
|
|
) -> test_case.TestCase:
|
|
|
|
"""Return one test case exercising a key creation method
|
|
|
|
for an unsupported key type or size.
|
|
|
|
"""
|
|
|
|
hack_dependencies_not_implemented(dependencies)
|
|
|
|
tc = test_case.TestCase()
|
2022-03-17 23:42:25 +01:00
|
|
|
short_key_type = crypto_knowledge.short_expression(key_type)
|
2021-03-10 02:12:08 +01:00
|
|
|
adverb = 'not' if dependencies else 'never'
|
|
|
|
if param_descr:
|
|
|
|
adverb = param_descr + ' ' + adverb
|
2021-10-11 10:15:25 +02:00
|
|
|
tc.set_description('PSA {} {} {}-bit {} supported'
|
|
|
|
.format(verb, short_key_type, bits, adverb))
|
|
|
|
tc.set_dependencies(dependencies)
|
|
|
|
tc.set_function(verb + '_not_supported')
|
|
|
|
tc.set_arguments([key_type] + list(args))
|
|
|
|
return tc
|
|
|
|
|
2021-03-10 02:12:08 +01:00
|
|
|
class NotSupported:
|
2021-10-18 14:58:20 +02:00
|
|
|
"""Generate test cases for when something is not supported."""
|
2021-03-10 02:12:08 +01:00
|
|
|
|
|
|
|
def __init__(self, info: Information) -> None:
|
|
|
|
self.constructors = info.constructors
|
2021-01-26 22:19:21 +01:00
|
|
|
|
2021-02-16 14:06:50 +01:00
|
|
|
ALWAYS_SUPPORTED = frozenset([
|
|
|
|
'PSA_KEY_TYPE_DERIVE',
|
|
|
|
'PSA_KEY_TYPE_RAW_DATA',
|
2022-05-05 11:52:30 +02:00
|
|
|
'PSA_KEY_TYPE_HMAC'
|
2021-02-16 14:06:50 +01:00
|
|
|
])
|
2021-01-26 22:19:21 +01:00
|
|
|
def test_cases_for_key_type_not_supported(
|
2021-02-16 14:06:50 +01:00
|
|
|
self,
|
2021-01-27 18:24:48 +01:00
|
|
|
kt: crypto_knowledge.KeyType,
|
|
|
|
param: Optional[int] = None,
|
|
|
|
param_descr: str = '',
|
2021-02-17 15:11:05 +01:00
|
|
|
) -> Iterator[test_case.TestCase]:
|
2021-10-18 14:58:20 +02:00
|
|
|
"""Return test cases exercising key creation when the given type is unsupported.
|
2021-01-27 18:24:48 +01:00
|
|
|
|
|
|
|
If param is present and not None, emit test cases conditioned on this
|
|
|
|
parameter not being supported. If it is absent or None, emit test cases
|
2021-10-18 14:58:20 +02:00
|
|
|
conditioned on the base type not being supported.
|
2021-01-27 18:24:48 +01:00
|
|
|
"""
|
2021-02-16 14:06:50 +01:00
|
|
|
if kt.name in self.ALWAYS_SUPPORTED:
|
|
|
|
# Don't generate test cases for key types that are always supported.
|
|
|
|
# They would be skipped in all configurations, which is noise.
|
2021-02-17 15:11:05 +01:00
|
|
|
return
|
2021-01-27 18:24:48 +01:00
|
|
|
import_dependencies = [('!' if param is None else '') +
|
|
|
|
psa_want_symbol(kt.name)]
|
|
|
|
if kt.params is not None:
|
|
|
|
import_dependencies += [('!' if param == i else '') +
|
|
|
|
psa_want_symbol(sym)
|
|
|
|
for i, sym in enumerate(kt.params)]
|
2021-01-26 22:19:21 +01:00
|
|
|
if kt.name.endswith('_PUBLIC_KEY'):
|
|
|
|
generate_dependencies = []
|
|
|
|
else:
|
|
|
|
generate_dependencies = import_dependencies
|
|
|
|
for bits in kt.sizes_to_test():
|
2021-10-11 10:15:25 +02:00
|
|
|
yield test_case_for_key_type_not_supported(
|
2021-02-16 12:13:12 +01:00
|
|
|
'import', kt.expression, bits,
|
|
|
|
finish_family_dependencies(import_dependencies, bits),
|
2021-01-27 18:24:48 +01:00
|
|
|
test_case.hex_string(kt.key_material(bits)),
|
|
|
|
param_descr=param_descr,
|
2021-02-17 15:11:05 +01:00
|
|
|
)
|
2021-01-27 18:24:48 +01:00
|
|
|
if not generate_dependencies and param is not None:
|
|
|
|
# If generation is impossible for this key type, rather than
|
|
|
|
# supported or not depending on implementation capabilities,
|
|
|
|
# only generate the test case once.
|
|
|
|
continue
|
2021-11-02 10:50:44 +01:00
|
|
|
# For public key we expect that key generation fails with
|
|
|
|
# INVALID_ARGUMENT. It is handled by KeyGenerate class.
|
2022-03-17 12:52:24 +01:00
|
|
|
if not kt.is_public():
|
2021-10-11 10:15:25 +02:00
|
|
|
yield test_case_for_key_type_not_supported(
|
|
|
|
'generate', kt.expression, bits,
|
|
|
|
finish_family_dependencies(generate_dependencies, bits),
|
|
|
|
str(bits),
|
|
|
|
param_descr=param_descr,
|
|
|
|
)
|
2021-01-26 22:19:21 +01:00
|
|
|
# To be added: derive
|
|
|
|
|
2021-04-19 13:50:25 +02:00
|
|
|
ECC_KEY_TYPES = ('PSA_KEY_TYPE_ECC_KEY_PAIR',
|
|
|
|
'PSA_KEY_TYPE_ECC_PUBLIC_KEY')
|
|
|
|
|
2021-02-17 15:11:05 +01:00
|
|
|
def test_cases_for_not_supported(self) -> Iterator[test_case.TestCase]:
|
2021-01-26 22:19:21 +01:00
|
|
|
"""Generate test cases that exercise the creation of keys of unsupported types."""
|
|
|
|
for key_type in sorted(self.constructors.key_types):
|
2021-04-19 13:50:25 +02:00
|
|
|
if key_type in self.ECC_KEY_TYPES:
|
|
|
|
continue
|
2021-01-26 22:19:21 +01:00
|
|
|
kt = crypto_knowledge.KeyType(key_type)
|
2021-02-17 15:11:05 +01:00
|
|
|
yield from self.test_cases_for_key_type_not_supported(kt)
|
2021-01-27 18:24:48 +01:00
|
|
|
for curve_family in sorted(self.constructors.ecc_curves):
|
2021-04-19 13:50:25 +02:00
|
|
|
for constr in self.ECC_KEY_TYPES:
|
2021-01-27 18:24:48 +01:00
|
|
|
kt = crypto_knowledge.KeyType(constr, [curve_family])
|
2021-02-17 15:11:05 +01:00
|
|
|
yield from self.test_cases_for_key_type_not_supported(
|
2021-01-27 18:24:48 +01:00
|
|
|
kt, param_descr='type')
|
2021-02-17 15:11:05 +01:00
|
|
|
yield from self.test_cases_for_key_type_not_supported(
|
2021-01-27 18:24:48 +01:00
|
|
|
kt, 0, param_descr='curve')
|
2021-03-10 02:12:08 +01:00
|
|
|
|
2021-10-15 15:21:51 +02:00
|
|
|
def test_case_for_key_generation(
|
|
|
|
key_type: str, bits: int,
|
|
|
|
dependencies: List[str],
|
|
|
|
*args: str,
|
2021-10-20 11:59:50 +02:00
|
|
|
result: str = ''
|
2021-10-15 15:21:51 +02:00
|
|
|
) -> test_case.TestCase:
|
|
|
|
"""Return one test case exercising a key generation.
|
|
|
|
"""
|
|
|
|
hack_dependencies_not_implemented(dependencies)
|
|
|
|
tc = test_case.TestCase()
|
2022-03-17 23:42:25 +01:00
|
|
|
short_key_type = crypto_knowledge.short_expression(key_type)
|
2021-10-15 15:21:51 +02:00
|
|
|
tc.set_description('PSA {} {}-bit'
|
2021-10-20 11:59:50 +02:00
|
|
|
.format(short_key_type, bits))
|
2021-10-15 15:21:51 +02:00
|
|
|
tc.set_dependencies(dependencies)
|
|
|
|
tc.set_function('generate_key')
|
2021-11-02 10:50:44 +01:00
|
|
|
tc.set_arguments([key_type] + list(args) + [result])
|
2021-10-15 15:21:51 +02:00
|
|
|
|
|
|
|
return tc
|
|
|
|
|
|
|
|
class KeyGenerate:
|
|
|
|
"""Generate positive and negative (invalid argument) test cases for key generation."""
|
|
|
|
|
|
|
|
def __init__(self, info: Information) -> None:
|
|
|
|
self.constructors = info.constructors
|
|
|
|
|
2021-10-20 11:59:50 +02:00
|
|
|
ECC_KEY_TYPES = ('PSA_KEY_TYPE_ECC_KEY_PAIR',
|
|
|
|
'PSA_KEY_TYPE_ECC_PUBLIC_KEY')
|
|
|
|
|
2021-11-02 10:50:44 +01:00
|
|
|
@staticmethod
|
2021-10-15 15:21:51 +02:00
|
|
|
def test_cases_for_key_type_key_generation(
|
2021-10-20 11:59:50 +02:00
|
|
|
kt: crypto_knowledge.KeyType
|
2021-10-15 15:21:51 +02:00
|
|
|
) -> Iterator[test_case.TestCase]:
|
|
|
|
"""Return test cases exercising key generation.
|
|
|
|
|
|
|
|
All key types can be generated except for public keys. For public key
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT status is expected.
|
|
|
|
"""
|
|
|
|
result = 'PSA_SUCCESS'
|
|
|
|
|
|
|
|
import_dependencies = [psa_want_symbol(kt.name)]
|
|
|
|
if kt.params is not None:
|
|
|
|
import_dependencies += [psa_want_symbol(sym)
|
|
|
|
for i, sym in enumerate(kt.params)]
|
|
|
|
if kt.name.endswith('_PUBLIC_KEY'):
|
2021-11-02 10:50:44 +01:00
|
|
|
# The library checks whether the key type is a public key generically,
|
|
|
|
# before it reaches a point where it needs support for the specific key
|
|
|
|
# type, so it returns INVALID_ARGUMENT for unsupported public key types.
|
2021-10-15 15:21:51 +02:00
|
|
|
generate_dependencies = []
|
|
|
|
result = 'PSA_ERROR_INVALID_ARGUMENT'
|
|
|
|
else:
|
|
|
|
generate_dependencies = import_dependencies
|
2021-11-02 10:50:44 +01:00
|
|
|
if kt.name == 'PSA_KEY_TYPE_RSA_KEY_PAIR':
|
2021-10-22 10:39:56 +02:00
|
|
|
generate_dependencies.append("MBEDTLS_GENPRIME")
|
2021-10-15 15:21:51 +02:00
|
|
|
for bits in kt.sizes_to_test():
|
|
|
|
yield test_case_for_key_generation(
|
|
|
|
kt.expression, bits,
|
|
|
|
finish_family_dependencies(generate_dependencies, bits),
|
|
|
|
str(bits),
|
2021-10-20 11:59:50 +02:00
|
|
|
result
|
2021-10-15 15:21:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_cases_for_key_generation(self) -> Iterator[test_case.TestCase]:
|
|
|
|
"""Generate test cases that exercise the generation of keys."""
|
|
|
|
for key_type in sorted(self.constructors.key_types):
|
|
|
|
if key_type in self.ECC_KEY_TYPES:
|
|
|
|
continue
|
|
|
|
kt = crypto_knowledge.KeyType(key_type)
|
|
|
|
yield from self.test_cases_for_key_type_key_generation(kt)
|
|
|
|
for curve_family in sorted(self.constructors.ecc_curves):
|
|
|
|
for constr in self.ECC_KEY_TYPES:
|
|
|
|
kt = crypto_knowledge.KeyType(constr, [curve_family])
|
2021-10-20 11:59:50 +02:00
|
|
|
yield from self.test_cases_for_key_type_key_generation(kt)
|
2021-10-15 15:21:51 +02:00
|
|
|
|
2021-04-27 20:40:10 +02:00
|
|
|
class OpFail:
|
|
|
|
"""Generate test cases for operations that must fail."""
|
|
|
|
#pylint: disable=too-few-public-methods
|
|
|
|
|
2022-03-15 17:26:33 +01:00
|
|
|
class Reason(enum.Enum):
|
|
|
|
NOT_SUPPORTED = 0
|
|
|
|
INVALID = 1
|
|
|
|
INCOMPATIBLE = 2
|
2021-04-29 21:56:59 +02:00
|
|
|
PUBLIC = 3
|
2022-03-15 17:26:33 +01:00
|
|
|
|
2021-04-27 20:40:10 +02:00
|
|
|
def __init__(self, info: Information) -> None:
|
|
|
|
self.constructors = info.constructors
|
2022-03-15 17:26:33 +01:00
|
|
|
key_type_expressions = self.constructors.generate_expressions(
|
|
|
|
sorted(self.constructors.key_types)
|
|
|
|
)
|
|
|
|
self.key_types = [crypto_knowledge.KeyType(kt_expr)
|
|
|
|
for kt_expr in key_type_expressions]
|
2021-04-27 20:40:10 +02:00
|
|
|
|
2022-03-15 17:26:33 +01:00
|
|
|
def make_test_case(
|
|
|
|
self,
|
|
|
|
alg: crypto_knowledge.Algorithm,
|
|
|
|
category: crypto_knowledge.AlgorithmCategory,
|
|
|
|
reason: 'Reason',
|
|
|
|
kt: Optional[crypto_knowledge.KeyType] = None,
|
|
|
|
not_deps: FrozenSet[str] = frozenset(),
|
|
|
|
) -> test_case.TestCase:
|
|
|
|
"""Construct a failure test case for a one-key or keyless operation."""
|
|
|
|
#pylint: disable=too-many-arguments,too-many-locals
|
2021-04-27 21:03:43 +02:00
|
|
|
tc = test_case.TestCase()
|
2022-03-17 23:42:25 +01:00
|
|
|
pretty_alg = alg.short_expression()
|
2021-04-29 21:35:03 +02:00
|
|
|
if reason == self.Reason.NOT_SUPPORTED:
|
|
|
|
short_deps = [re.sub(r'PSA_WANT_ALG_', r'', dep)
|
|
|
|
for dep in not_deps]
|
|
|
|
pretty_reason = '!' + '&'.join(sorted(short_deps))
|
|
|
|
else:
|
|
|
|
pretty_reason = reason.name.lower()
|
2022-03-15 17:26:33 +01:00
|
|
|
if kt:
|
|
|
|
key_type = kt.expression
|
2022-03-17 23:42:25 +01:00
|
|
|
pretty_type = kt.short_expression()
|
2021-04-27 21:03:43 +02:00
|
|
|
else:
|
2022-03-15 17:26:33 +01:00
|
|
|
key_type = ''
|
|
|
|
pretty_type = ''
|
|
|
|
tc.set_description('PSA {} {}: {}{}'
|
|
|
|
.format(category.name.lower(),
|
|
|
|
pretty_alg,
|
|
|
|
pretty_reason,
|
|
|
|
' with ' + pretty_type if pretty_type else ''))
|
|
|
|
dependencies = automatic_dependencies(alg.base_expression, key_type)
|
|
|
|
for i, dep in enumerate(dependencies):
|
|
|
|
if dep in not_deps:
|
|
|
|
dependencies[i] = '!' + dep
|
2021-04-27 21:03:43 +02:00
|
|
|
tc.set_dependencies(dependencies)
|
2022-03-15 17:26:33 +01:00
|
|
|
tc.set_function(category.name.lower() + '_fail')
|
|
|
|
arguments = []
|
|
|
|
if kt:
|
|
|
|
key_material = kt.key_material(kt.sizes_to_test()[0])
|
|
|
|
arguments += [key_type, test_case.hex_string(key_material)]
|
|
|
|
arguments.append(alg.expression)
|
2021-04-29 21:56:59 +02:00
|
|
|
if category.is_asymmetric():
|
|
|
|
arguments.append('1' if reason == self.Reason.PUBLIC else '0')
|
2022-03-15 17:26:33 +01:00
|
|
|
error = ('NOT_SUPPORTED' if reason == self.Reason.NOT_SUPPORTED else
|
|
|
|
'INVALID_ARGUMENT')
|
|
|
|
arguments.append('PSA_ERROR_' + error)
|
|
|
|
tc.set_arguments(arguments)
|
|
|
|
return tc
|
|
|
|
|
|
|
|
def no_key_test_cases(
|
|
|
|
self,
|
|
|
|
alg: crypto_knowledge.Algorithm,
|
|
|
|
category: crypto_knowledge.AlgorithmCategory,
|
|
|
|
) -> Iterator[test_case.TestCase]:
|
|
|
|
"""Generate failure test cases for keyless operations with the specified algorithm."""
|
2021-04-29 20:54:40 +02:00
|
|
|
if alg.can_do(category):
|
2022-03-15 17:26:33 +01:00
|
|
|
# Compatible operation, unsupported algorithm
|
|
|
|
for dep in automatic_dependencies(alg.base_expression):
|
|
|
|
yield self.make_test_case(alg, category,
|
|
|
|
self.Reason.NOT_SUPPORTED,
|
|
|
|
not_deps=frozenset([dep]))
|
|
|
|
else:
|
|
|
|
# Incompatible operation, supported algorithm
|
|
|
|
yield self.make_test_case(alg, category, self.Reason.INVALID)
|
2021-04-27 21:03:43 +02:00
|
|
|
|
2022-03-15 17:26:33 +01:00
|
|
|
def one_key_test_cases(
|
|
|
|
self,
|
|
|
|
alg: crypto_knowledge.Algorithm,
|
|
|
|
category: crypto_knowledge.AlgorithmCategory,
|
|
|
|
) -> Iterator[test_case.TestCase]:
|
|
|
|
"""Generate failure test cases for one-key operations with the specified algorithm."""
|
|
|
|
for kt in self.key_types:
|
|
|
|
key_is_compatible = kt.can_do(alg)
|
2021-04-29 20:54:40 +02:00
|
|
|
if key_is_compatible and alg.can_do(category):
|
2022-03-15 17:26:33 +01:00
|
|
|
# Compatible key and operation, unsupported algorithm
|
|
|
|
for dep in automatic_dependencies(alg.base_expression):
|
|
|
|
yield self.make_test_case(alg, category,
|
|
|
|
self.Reason.NOT_SUPPORTED,
|
|
|
|
kt=kt, not_deps=frozenset([dep]))
|
2021-04-29 21:56:59 +02:00
|
|
|
# Public key for a private-key operation
|
|
|
|
if category.is_asymmetric() and kt.is_public():
|
|
|
|
yield self.make_test_case(alg, category,
|
|
|
|
self.Reason.PUBLIC,
|
|
|
|
kt=kt)
|
2022-03-15 17:26:33 +01:00
|
|
|
elif key_is_compatible:
|
|
|
|
# Compatible key, incompatible operation, supported algorithm
|
|
|
|
yield self.make_test_case(alg, category,
|
|
|
|
self.Reason.INVALID,
|
|
|
|
kt=kt)
|
2021-04-29 20:54:40 +02:00
|
|
|
elif alg.can_do(category):
|
2022-03-15 17:26:33 +01:00
|
|
|
# Incompatible key, compatible operation, supported algorithm
|
|
|
|
yield self.make_test_case(alg, category,
|
|
|
|
self.Reason.INCOMPATIBLE,
|
|
|
|
kt=kt)
|
|
|
|
else:
|
|
|
|
# Incompatible key and operation. Don't test cases where
|
|
|
|
# multiple things are wrong, to keep the number of test
|
|
|
|
# cases reasonable.
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_cases_for_algorithm(
|
|
|
|
self,
|
|
|
|
alg: crypto_knowledge.Algorithm,
|
|
|
|
) -> Iterator[test_case.TestCase]:
|
2021-04-27 21:03:43 +02:00
|
|
|
"""Generate operation failure test cases for the specified algorithm."""
|
2022-03-15 17:26:33 +01:00
|
|
|
for category in crypto_knowledge.AlgorithmCategory:
|
|
|
|
if category == crypto_knowledge.AlgorithmCategory.PAKE:
|
|
|
|
# PAKE operations are not implemented yet
|
|
|
|
pass
|
|
|
|
elif category.requires_key():
|
|
|
|
yield from self.one_key_test_cases(alg, category)
|
|
|
|
else:
|
|
|
|
yield from self.no_key_test_cases(alg, category)
|
2021-04-27 21:03:43 +02:00
|
|
|
|
2021-04-27 20:40:10 +02:00
|
|
|
def all_test_cases(self) -> Iterator[test_case.TestCase]:
|
|
|
|
"""Generate all test cases for operations that must fail."""
|
2021-04-27 21:03:43 +02:00
|
|
|
algorithms = sorted(self.constructors.algorithms)
|
2022-03-15 17:26:33 +01:00
|
|
|
for expr in self.constructors.generate_expressions(algorithms):
|
|
|
|
alg = crypto_knowledge.Algorithm(expr)
|
2021-04-27 21:03:43 +02:00
|
|
|
yield from self.test_cases_for_algorithm(alg)
|
2021-04-27 20:40:10 +02:00
|
|
|
|
|
|
|
|
2021-03-10 15:03:44 +01:00
|
|
|
class StorageKey(psa_storage.Key):
|
|
|
|
"""Representation of a key for storage format testing."""
|
|
|
|
|
2021-06-29 15:29:24 +02:00
|
|
|
IMPLICIT_USAGE_FLAGS = {
|
|
|
|
'PSA_KEY_USAGE_SIGN_HASH': 'PSA_KEY_USAGE_SIGN_MESSAGE',
|
|
|
|
'PSA_KEY_USAGE_VERIFY_HASH': 'PSA_KEY_USAGE_VERIFY_MESSAGE'
|
|
|
|
} #type: Dict[str, str]
|
|
|
|
"""Mapping of usage flags to the flags that they imply."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2022-03-17 22:32:59 +01:00
|
|
|
usage: Iterable[str],
|
2021-06-29 15:29:24 +02:00
|
|
|
without_implicit_usage: Optional[bool] = False,
|
|
|
|
**kwargs
|
|
|
|
) -> None:
|
|
|
|
"""Prepare to generate a key.
|
|
|
|
|
|
|
|
* `usage` : The usage flags used for the key.
|
|
|
|
* `without_implicit_usage`: Flag to defide to apply the usage extension
|
|
|
|
"""
|
2022-03-17 22:32:59 +01:00
|
|
|
usage_flags = set(usage)
|
2021-06-29 15:29:24 +02:00
|
|
|
if not without_implicit_usage:
|
2022-03-17 22:32:59 +01:00
|
|
|
for flag in sorted(usage_flags):
|
|
|
|
if flag in self.IMPLICIT_USAGE_FLAGS:
|
|
|
|
usage_flags.add(self.IMPLICIT_USAGE_FLAGS[flag])
|
|
|
|
if usage_flags:
|
|
|
|
usage_expression = ' | '.join(sorted(usage_flags))
|
|
|
|
else:
|
|
|
|
usage_expression = '0'
|
|
|
|
super().__init__(usage=usage_expression, **kwargs)
|
2021-06-29 15:29:24 +02:00
|
|
|
|
|
|
|
class StorageTestData(StorageKey):
|
|
|
|
"""Representation of test case data for storage format testing."""
|
|
|
|
|
2021-06-24 10:16:44 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
description: str,
|
2022-03-17 22:32:59 +01:00
|
|
|
expected_usage: Optional[List[str]] = None,
|
2021-06-24 10:16:44 +02:00
|
|
|
**kwargs
|
|
|
|
) -> None:
|
2021-06-29 15:29:24 +02:00
|
|
|
"""Prepare to generate test data
|
2021-06-24 10:16:44 +02:00
|
|
|
|
2021-06-29 15:29:24 +02:00
|
|
|
* `description` : used for the the test case names
|
|
|
|
* `expected_usage`: the usage flags generated as the expected usage flags
|
|
|
|
in the test cases. CAn differ from the usage flags
|
|
|
|
stored in the keys because of the usage flags extension.
|
2021-06-24 10:16:44 +02:00
|
|
|
"""
|
2021-03-10 15:03:44 +01:00
|
|
|
super().__init__(**kwargs)
|
|
|
|
self.description = description #type: str
|
2022-03-17 22:32:59 +01:00
|
|
|
if expected_usage is None:
|
|
|
|
self.expected_usage = self.usage #type: psa_storage.Expr
|
|
|
|
elif expected_usage:
|
|
|
|
self.expected_usage = psa_storage.Expr(' | '.join(expected_usage))
|
|
|
|
else:
|
|
|
|
self.expected_usage = psa_storage.Expr(0)
|
2021-06-24 10:04:38 +02:00
|
|
|
|
2021-03-10 15:03:44 +01:00
|
|
|
class StorageFormat:
|
|
|
|
"""Storage format stability test cases."""
|
|
|
|
|
|
|
|
def __init__(self, info: Information, version: int, forward: bool) -> None:
|
|
|
|
"""Prepare to generate test cases for storage format stability.
|
|
|
|
|
|
|
|
* `info`: information about the API. See the `Information` class.
|
|
|
|
* `version`: the storage format version to generate test cases for.
|
|
|
|
* `forward`: if true, generate forward compatibility test cases which
|
|
|
|
save a key and check that its representation is as intended. Otherwise
|
|
|
|
generate backward compatibility test cases which inject a key
|
|
|
|
representation and check that it can be read and used.
|
|
|
|
"""
|
2021-06-23 17:01:44 +02:00
|
|
|
self.constructors = info.constructors #type: macro_collector.PSAMacroEnumerator
|
|
|
|
self.version = version #type: int
|
|
|
|
self.forward = forward #type: bool
|
2021-03-10 15:03:44 +01:00
|
|
|
|
2022-03-19 12:09:13 +01:00
|
|
|
RSA_OAEP_RE = re.compile(r'PSA_ALG_RSA_OAEP\((.*)\)\Z')
|
2022-03-19 15:36:09 +01:00
|
|
|
BRAINPOOL_RE = re.compile(r'PSA_KEY_TYPE_\w+\(PSA_ECC_FAMILY_BRAINPOOL_\w+\)\Z')
|
2022-03-19 12:09:13 +01:00
|
|
|
@classmethod
|
2022-03-19 15:36:09 +01:00
|
|
|
def exercise_key_with_algorithm(
|
2022-03-19 12:09:13 +01:00
|
|
|
cls,
|
|
|
|
key_type: psa_storage.Expr, bits: int,
|
|
|
|
alg: psa_storage.Expr
|
|
|
|
) -> bool:
|
2022-03-19 15:36:09 +01:00
|
|
|
"""Whether to the given key with the given algorithm.
|
2022-03-19 12:09:13 +01:00
|
|
|
|
|
|
|
Normally only the type and algorithm matter for compatibility, and
|
|
|
|
this is handled in crypto_knowledge.KeyType.can_do(). This function
|
|
|
|
exists to detect exceptional cases. Exceptional cases detected here
|
|
|
|
are not tested in OpFail and should therefore have manually written
|
|
|
|
test cases.
|
|
|
|
"""
|
2022-03-19 15:36:09 +01:00
|
|
|
# Some test keys have the RAW_DATA type and attributes that don't
|
|
|
|
# necessarily make sense. We do this to validate numerical
|
|
|
|
# encodings of the attributes.
|
|
|
|
# Raw data keys have no useful exercise anyway so there is no
|
|
|
|
# loss of test coverage.
|
|
|
|
if key_type.string == 'PSA_KEY_TYPE_RAW_DATA':
|
|
|
|
return False
|
2022-03-19 12:09:13 +01:00
|
|
|
# OAEP requires room for two hashes plus wrapping
|
|
|
|
m = cls.RSA_OAEP_RE.match(alg.string)
|
|
|
|
if m:
|
|
|
|
hash_alg = m.group(1)
|
|
|
|
hash_length = crypto_knowledge.Algorithm.hash_length(hash_alg)
|
|
|
|
key_length = (bits + 7) // 8
|
|
|
|
# Leave enough room for at least one byte of plaintext
|
|
|
|
return key_length > 2 * hash_length + 2
|
2022-03-19 15:36:09 +01:00
|
|
|
# There's nothing wrong with ECC keys on Brainpool curves,
|
|
|
|
# but operations with them are very slow. So we only exercise them
|
|
|
|
# with a single algorithm, not with all possible hashes. We do
|
|
|
|
# exercise other curves with all algorithms so test coverage is
|
|
|
|
# perfectly adequate like this.
|
|
|
|
m = cls.BRAINPOOL_RE.match(key_type.string)
|
|
|
|
if m and alg.string != 'PSA_ALG_ECDSA_ANY':
|
|
|
|
return False
|
2022-03-19 12:09:13 +01:00
|
|
|
return True
|
|
|
|
|
2021-06-29 15:29:24 +02:00
|
|
|
def make_test_case(self, key: StorageTestData) -> test_case.TestCase:
|
2021-03-10 15:03:44 +01:00
|
|
|
"""Construct a storage format test case for the given key.
|
|
|
|
|
|
|
|
If ``forward`` is true, generate a forward compatibility test case:
|
|
|
|
create a key and validate that it has the expected representation.
|
|
|
|
Otherwise generate a backward compatibility test case: inject the
|
|
|
|
key representation into storage and validate that it can be read
|
|
|
|
correctly.
|
|
|
|
"""
|
|
|
|
verb = 'save' if self.forward else 'read'
|
|
|
|
tc = test_case.TestCase()
|
2022-03-18 00:02:15 +01:00
|
|
|
tc.set_description(verb + ' ' + key.description)
|
2021-03-10 15:07:16 +01:00
|
|
|
dependencies = automatic_dependencies(
|
|
|
|
key.lifetime.string, key.type.string,
|
2022-03-17 22:32:59 +01:00
|
|
|
key.alg.string, key.alg2.string,
|
2021-03-10 15:07:16 +01:00
|
|
|
)
|
|
|
|
dependencies = finish_family_dependencies(dependencies, key.bits)
|
|
|
|
tc.set_dependencies(dependencies)
|
2021-03-10 15:03:44 +01:00
|
|
|
tc.set_function('key_storage_' + verb)
|
|
|
|
if self.forward:
|
|
|
|
extra_arguments = []
|
|
|
|
else:
|
2021-04-21 20:11:33 +02:00
|
|
|
flags = []
|
2022-03-19 15:36:09 +01:00
|
|
|
if self.exercise_key_with_algorithm(key.type, key.bits, key.alg):
|
2021-04-21 20:11:33 +02:00
|
|
|
flags.append('TEST_FLAG_EXERCISE')
|
|
|
|
if 'READ_ONLY' in key.lifetime.string:
|
|
|
|
flags.append('TEST_FLAG_READ_ONLY')
|
|
|
|
extra_arguments = [' | '.join(flags) if flags else '0']
|
2021-03-10 15:03:44 +01:00
|
|
|
tc.set_arguments([key.lifetime.string,
|
|
|
|
key.type.string, str(key.bits),
|
2022-03-17 22:32:59 +01:00
|
|
|
key.expected_usage.string,
|
|
|
|
key.alg.string, key.alg2.string,
|
2021-03-10 15:03:44 +01:00
|
|
|
'"' + key.material.hex() + '"',
|
|
|
|
'"' + key.hex() + '"',
|
|
|
|
*extra_arguments])
|
|
|
|
return tc
|
|
|
|
|
2021-04-21 22:05:34 +02:00
|
|
|
def key_for_lifetime(
|
|
|
|
self,
|
|
|
|
lifetime: str,
|
2021-06-29 15:29:24 +02:00
|
|
|
) -> StorageTestData:
|
2021-04-21 22:05:34 +02:00
|
|
|
"""Construct a test key for the given lifetime."""
|
|
|
|
short = lifetime
|
|
|
|
short = re.sub(r'PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION',
|
|
|
|
r'', short)
|
2022-03-17 23:42:25 +01:00
|
|
|
short = crypto_knowledge.short_expression(short)
|
2021-04-21 22:05:34 +02:00
|
|
|
description = 'lifetime: ' + short
|
2021-06-29 15:29:24 +02:00
|
|
|
key = StorageTestData(version=self.version,
|
|
|
|
id=1, lifetime=lifetime,
|
|
|
|
type='PSA_KEY_TYPE_RAW_DATA', bits=8,
|
2022-03-17 22:32:59 +01:00
|
|
|
usage=['PSA_KEY_USAGE_EXPORT'], alg=0, alg2=0,
|
2021-06-29 15:29:24 +02:00
|
|
|
material=b'L',
|
|
|
|
description=description)
|
|
|
|
return key
|
|
|
|
|
|
|
|
def all_keys_for_lifetimes(self) -> Iterator[StorageTestData]:
|
2021-04-21 22:05:34 +02:00
|
|
|
"""Generate test keys covering lifetimes."""
|
|
|
|
lifetimes = sorted(self.constructors.lifetimes)
|
|
|
|
expressions = self.constructors.generate_expressions(lifetimes)
|
|
|
|
for lifetime in expressions:
|
|
|
|
# Don't attempt to create or load a volatile key in storage
|
|
|
|
if 'VOLATILE' in lifetime:
|
|
|
|
continue
|
|
|
|
# Don't attempt to create a read-only key in storage,
|
|
|
|
# but do attempt to load one.
|
|
|
|
if 'READ_ONLY' in lifetime and self.forward:
|
|
|
|
continue
|
2021-06-28 19:26:55 +02:00
|
|
|
yield self.key_for_lifetime(lifetime)
|
2021-04-21 22:05:34 +02:00
|
|
|
|
2022-02-24 18:58:08 +01:00
|
|
|
def key_for_usage_flags(
|
2021-03-10 15:03:44 +01:00
|
|
|
self,
|
|
|
|
usage_flags: List[str],
|
2021-06-24 09:42:02 +02:00
|
|
|
short: Optional[str] = None,
|
2022-02-24 18:58:08 +01:00
|
|
|
test_implicit_usage: Optional[bool] = True
|
|
|
|
) -> StorageTestData:
|
2021-03-10 15:03:44 +01:00
|
|
|
"""Construct a test key for the given key usage."""
|
2022-02-24 18:58:08 +01:00
|
|
|
extra_desc = ' without implication' if test_implicit_usage else ''
|
2022-03-17 22:32:59 +01:00
|
|
|
description = 'usage' + extra_desc + ': '
|
2021-06-29 15:29:24 +02:00
|
|
|
key1 = StorageTestData(version=self.version,
|
|
|
|
id=1, lifetime=0x00000001,
|
|
|
|
type='PSA_KEY_TYPE_RAW_DATA', bits=8,
|
2022-03-17 22:32:59 +01:00
|
|
|
expected_usage=usage_flags,
|
2022-02-24 18:58:08 +01:00
|
|
|
without_implicit_usage=not test_implicit_usage,
|
2022-03-17 22:32:59 +01:00
|
|
|
usage=usage_flags, alg=0, alg2=0,
|
2021-06-29 15:29:24 +02:00
|
|
|
material=b'K',
|
|
|
|
description=description)
|
2022-03-17 22:32:59 +01:00
|
|
|
if short is None:
|
2022-03-17 23:42:25 +01:00
|
|
|
usage_expr = key1.expected_usage.string
|
|
|
|
key1.description += crypto_knowledge.short_expression(usage_expr)
|
2022-03-17 22:32:59 +01:00
|
|
|
else:
|
|
|
|
key1.description += short
|
2022-02-24 18:58:08 +01:00
|
|
|
return key1
|
2021-06-29 15:29:24 +02:00
|
|
|
|
|
|
|
def generate_keys_for_usage_flags(self, **kwargs) -> Iterator[StorageTestData]:
|
2021-03-10 15:03:44 +01:00
|
|
|
"""Generate test keys covering usage flags."""
|
|
|
|
known_flags = sorted(self.constructors.key_usage_flags)
|
2022-02-24 18:58:08 +01:00
|
|
|
yield self.key_for_usage_flags(['0'], **kwargs)
|
2021-06-28 19:26:55 +02:00
|
|
|
for usage_flag in known_flags:
|
2022-02-24 18:58:08 +01:00
|
|
|
yield self.key_for_usage_flags([usage_flag], **kwargs)
|
2021-06-28 19:26:55 +02:00
|
|
|
for flag1, flag2 in zip(known_flags,
|
|
|
|
known_flags[1:] + [known_flags[0]]):
|
2022-02-24 18:58:08 +01:00
|
|
|
yield self.key_for_usage_flags([flag1, flag2], **kwargs)
|
2021-06-28 19:26:55 +02:00
|
|
|
|
2021-06-29 15:29:24 +02:00
|
|
|
def generate_key_for_all_usage_flags(self) -> Iterator[StorageTestData]:
|
2021-06-24 14:38:51 +02:00
|
|
|
known_flags = sorted(self.constructors.key_usage_flags)
|
2022-02-24 18:58:08 +01:00
|
|
|
yield self.key_for_usage_flags(known_flags, short='all known')
|
2021-06-24 14:38:51 +02:00
|
|
|
|
2021-06-29 15:29:24 +02:00
|
|
|
def all_keys_for_usage_flags(self) -> Iterator[StorageTestData]:
|
2021-06-28 19:26:55 +02:00
|
|
|
yield from self.generate_keys_for_usage_flags()
|
|
|
|
yield from self.generate_key_for_all_usage_flags()
|
2021-03-10 15:03:44 +01:00
|
|
|
|
2021-04-29 22:28:07 +02:00
|
|
|
def key_for_type_and_alg(
|
|
|
|
self,
|
|
|
|
kt: crypto_knowledge.KeyType,
|
|
|
|
bits: int,
|
|
|
|
alg: Optional[crypto_knowledge.Algorithm] = None,
|
|
|
|
) -> StorageTestData:
|
|
|
|
"""Construct a test key of the given type.
|
|
|
|
|
|
|
|
If alg is not None, this key allows it.
|
|
|
|
"""
|
2022-03-17 22:32:59 +01:00
|
|
|
usage_flags = ['PSA_KEY_USAGE_EXPORT']
|
2022-03-18 09:58:09 +01:00
|
|
|
alg1 = 0 #type: psa_storage.Exprable
|
2021-04-29 22:28:07 +02:00
|
|
|
alg2 = 0
|
2022-03-18 09:58:09 +01:00
|
|
|
if alg is not None:
|
|
|
|
alg1 = alg.expression
|
|
|
|
usage_flags += alg.usage_flags(public=kt.is_public())
|
2021-04-29 22:28:07 +02:00
|
|
|
key_material = kt.key_material(bits)
|
2022-03-18 00:02:15 +01:00
|
|
|
description = 'type: {} {}-bit'.format(kt.short_expression(1), bits)
|
2021-04-29 22:28:07 +02:00
|
|
|
if alg is not None:
|
2022-03-18 00:02:15 +01:00
|
|
|
description += ', ' + alg.short_expression(1)
|
2021-04-29 22:28:07 +02:00
|
|
|
key = StorageTestData(version=self.version,
|
|
|
|
id=1, lifetime=0x00000001,
|
|
|
|
type=kt.expression, bits=bits,
|
|
|
|
usage=usage_flags, alg=alg1, alg2=alg2,
|
|
|
|
material=key_material,
|
|
|
|
description=description)
|
|
|
|
return key
|
|
|
|
|
2021-03-10 15:07:16 +01:00
|
|
|
def keys_for_type(
|
|
|
|
self,
|
|
|
|
key_type: str,
|
2021-04-29 22:28:07 +02:00
|
|
|
all_algorithms: List[crypto_knowledge.Algorithm],
|
2021-06-29 15:29:24 +02:00
|
|
|
) -> Iterator[StorageTestData]:
|
2021-04-29 22:28:07 +02:00
|
|
|
"""Generate test keys for the given key type."""
|
|
|
|
kt = crypto_knowledge.KeyType(key_type)
|
2021-03-10 15:07:16 +01:00
|
|
|
for bits in kt.sizes_to_test():
|
2021-04-29 22:28:07 +02:00
|
|
|
# Test a non-exercisable key, as well as exercisable keys for
|
|
|
|
# each compatible algorithm.
|
|
|
|
# To do: test reading a key from storage with an incompatible
|
|
|
|
# or unsupported algorithm.
|
|
|
|
yield self.key_for_type_and_alg(kt, bits)
|
|
|
|
compatible_algorithms = [alg for alg in all_algorithms
|
|
|
|
if kt.can_do(alg)]
|
|
|
|
for alg in compatible_algorithms:
|
|
|
|
yield self.key_for_type_and_alg(kt, bits, alg)
|
2021-06-29 15:29:24 +02:00
|
|
|
|
|
|
|
def all_keys_for_types(self) -> Iterator[StorageTestData]:
|
2021-03-10 15:07:16 +01:00
|
|
|
"""Generate test keys covering key types and their representations."""
|
2021-04-19 13:50:25 +02:00
|
|
|
key_types = sorted(self.constructors.key_types)
|
2021-04-29 22:28:07 +02:00
|
|
|
all_algorithms = [crypto_knowledge.Algorithm(alg)
|
|
|
|
for alg in self.constructors.generate_expressions(
|
|
|
|
sorted(self.constructors.algorithms)
|
|
|
|
)]
|
2021-06-28 19:26:55 +02:00
|
|
|
for key_type in self.constructors.generate_expressions(key_types):
|
2021-04-29 22:28:07 +02:00
|
|
|
yield from self.keys_for_type(key_type, all_algorithms)
|
2021-03-10 15:07:16 +01:00
|
|
|
|
2021-06-29 15:29:24 +02:00
|
|
|
def keys_for_algorithm(self, alg: str) -> Iterator[StorageTestData]:
|
2021-04-29 22:28:07 +02:00
|
|
|
"""Generate test keys for the encoding of the specified algorithm."""
|
|
|
|
# These test cases only validate the encoding of algorithms, not
|
|
|
|
# whether the key read from storage is suitable for an operation.
|
|
|
|
# `keys_for_types` generate read tests with an algorithm and a
|
|
|
|
# compatible key.
|
2022-03-18 00:02:15 +01:00
|
|
|
descr = crypto_knowledge.short_expression(alg, 1)
|
2022-03-17 22:32:59 +01:00
|
|
|
usage = ['PSA_KEY_USAGE_EXPORT']
|
2021-06-29 15:29:24 +02:00
|
|
|
key1 = StorageTestData(version=self.version,
|
|
|
|
id=1, lifetime=0x00000001,
|
|
|
|
type='PSA_KEY_TYPE_RAW_DATA', bits=8,
|
|
|
|
usage=usage, alg=alg, alg2=0,
|
|
|
|
material=b'K',
|
|
|
|
description='alg: ' + descr)
|
|
|
|
yield key1
|
|
|
|
key2 = StorageTestData(version=self.version,
|
|
|
|
id=1, lifetime=0x00000001,
|
|
|
|
type='PSA_KEY_TYPE_RAW_DATA', bits=8,
|
|
|
|
usage=usage, alg=0, alg2=alg,
|
|
|
|
material=b'L',
|
|
|
|
description='alg2: ' + descr)
|
|
|
|
yield key2
|
|
|
|
|
|
|
|
def all_keys_for_algorithms(self) -> Iterator[StorageTestData]:
|
2021-03-10 15:08:57 +01:00
|
|
|
"""Generate test keys covering algorithm encodings."""
|
2021-04-19 13:50:25 +02:00
|
|
|
algorithms = sorted(self.constructors.algorithms)
|
2021-06-28 19:26:55 +02:00
|
|
|
for alg in self.constructors.generate_expressions(algorithms):
|
|
|
|
yield from self.keys_for_algorithm(alg)
|
2021-03-10 15:08:57 +01:00
|
|
|
|
2021-06-29 15:42:57 +02:00
|
|
|
def generate_all_keys(self) -> Iterator[StorageTestData]:
|
2021-06-24 09:49:50 +02:00
|
|
|
"""Generate all keys for the test cases."""
|
2021-06-29 15:42:57 +02:00
|
|
|
yield from self.all_keys_for_lifetimes()
|
|
|
|
yield from self.all_keys_for_usage_flags()
|
|
|
|
yield from self.all_keys_for_types()
|
|
|
|
yield from self.all_keys_for_algorithms()
|
2021-06-24 09:49:50 +02:00
|
|
|
|
2021-06-28 19:26:55 +02:00
|
|
|
def all_test_cases(self) -> Iterator[test_case.TestCase]:
|
2021-03-10 15:03:44 +01:00
|
|
|
"""Generate all storage format test cases."""
|
2021-04-12 14:43:05 +02:00
|
|
|
# First build a list of all keys, then construct all the corresponding
|
|
|
|
# test cases. This allows all required information to be obtained in
|
|
|
|
# one go, which is a significant performance gain as the information
|
|
|
|
# includes numerical values obtained by compiling a C program.
|
2021-07-06 21:05:52 +02:00
|
|
|
all_keys = list(self.generate_all_keys())
|
|
|
|
for key in all_keys:
|
2021-06-28 19:26:55 +02:00
|
|
|
if key.location_value() != 0:
|
|
|
|
# Skip keys with a non-default location, because they
|
|
|
|
# require a driver and we currently have no mechanism to
|
|
|
|
# determine whether a driver is available.
|
|
|
|
continue
|
|
|
|
yield self.make_test_case(key)
|
2021-03-10 15:03:44 +01:00
|
|
|
|
2021-06-24 09:53:26 +02:00
|
|
|
class StorageFormatForward(StorageFormat):
|
|
|
|
"""Storage format stability test cases for forward compatibility."""
|
|
|
|
|
|
|
|
def __init__(self, info: Information, version: int) -> None:
|
|
|
|
super().__init__(info, version, True)
|
|
|
|
|
|
|
|
class StorageFormatV0(StorageFormat):
|
|
|
|
"""Storage format stability test cases for version 0 compatibility."""
|
|
|
|
|
|
|
|
def __init__(self, info: Information) -> None:
|
|
|
|
super().__init__(info, 0, False)
|
2021-03-10 15:03:44 +01:00
|
|
|
|
2021-06-29 15:29:24 +02:00
|
|
|
def all_keys_for_usage_flags(self) -> Iterator[StorageTestData]:
|
2021-06-24 10:04:38 +02:00
|
|
|
"""Generate test keys covering usage flags."""
|
2022-02-24 18:58:08 +01:00
|
|
|
yield from super().all_keys_for_usage_flags()
|
|
|
|
yield from self.generate_keys_for_usage_flags(test_implicit_usage=False)
|
2021-06-24 10:04:38 +02:00
|
|
|
|
2021-06-28 17:40:32 +02:00
|
|
|
def keys_for_implicit_usage(
|
2021-06-24 10:16:44 +02:00
|
|
|
self,
|
2021-06-28 16:54:11 +02:00
|
|
|
implyer_usage: str,
|
2021-06-24 10:16:44 +02:00
|
|
|
alg: str,
|
2021-06-28 20:02:11 +02:00
|
|
|
key_type: crypto_knowledge.KeyType
|
2021-06-29 15:29:24 +02:00
|
|
|
) -> StorageTestData:
|
2021-06-24 14:38:25 +02:00
|
|
|
# pylint: disable=too-many-locals
|
2021-06-28 16:27:29 +02:00
|
|
|
"""Generate test keys for the specified implicit usage flag,
|
2021-06-24 10:16:44 +02:00
|
|
|
algorithm and key type combination.
|
|
|
|
"""
|
2021-06-28 20:02:11 +02:00
|
|
|
bits = key_type.sizes_to_test()[0]
|
2021-06-28 16:54:11 +02:00
|
|
|
implicit_usage = StorageKey.IMPLICIT_USAGE_FLAGS[implyer_usage]
|
2022-03-17 22:32:59 +01:00
|
|
|
usage_flags = ['PSA_KEY_USAGE_EXPORT']
|
|
|
|
material_usage_flags = usage_flags + [implyer_usage]
|
|
|
|
expected_usage_flags = material_usage_flags + [implicit_usage]
|
2021-06-28 16:35:48 +02:00
|
|
|
alg2 = 0
|
2021-06-28 20:02:11 +02:00
|
|
|
key_material = key_type.key_material(bits)
|
2022-03-18 00:02:15 +01:00
|
|
|
usage_expression = crypto_knowledge.short_expression(implyer_usage, 1)
|
|
|
|
alg_expression = crypto_knowledge.short_expression(alg, 1)
|
|
|
|
key_type_expression = key_type.short_expression(1)
|
2021-06-28 17:40:32 +02:00
|
|
|
description = 'implied by {}: {} {} {}-bit'.format(
|
2021-06-28 16:35:48 +02:00
|
|
|
usage_expression, alg_expression, key_type_expression, bits)
|
2021-06-29 15:29:24 +02:00
|
|
|
key = StorageTestData(version=self.version,
|
|
|
|
id=1, lifetime=0x00000001,
|
|
|
|
type=key_type.expression, bits=bits,
|
|
|
|
usage=material_usage_flags,
|
|
|
|
expected_usage=expected_usage_flags,
|
|
|
|
without_implicit_usage=True,
|
|
|
|
alg=alg, alg2=alg2,
|
|
|
|
material=key_material,
|
|
|
|
description=description)
|
|
|
|
return key
|
2021-06-24 10:16:44 +02:00
|
|
|
|
|
|
|
def gather_key_types_for_sign_alg(self) -> Dict[str, List[str]]:
|
2021-06-24 14:38:25 +02:00
|
|
|
# pylint: disable=too-many-locals
|
2021-06-24 10:16:44 +02:00
|
|
|
"""Match possible key types for sign algorithms."""
|
2021-12-21 06:14:10 +01:00
|
|
|
# To create a valid combination both the algorithms and key types
|
2021-06-24 10:16:44 +02:00
|
|
|
# must be filtered. Pair them with keywords created from its names.
|
|
|
|
incompatible_alg_keyword = frozenset(['RAW', 'ANY', 'PURE'])
|
|
|
|
incompatible_key_type_keywords = frozenset(['MONTGOMERY'])
|
|
|
|
keyword_translation = {
|
|
|
|
'ECDSA': 'ECC',
|
|
|
|
'ED[0-9]*.*' : 'EDWARDS'
|
|
|
|
}
|
|
|
|
exclusive_keywords = {
|
|
|
|
'EDWARDS': 'ECC'
|
|
|
|
}
|
2021-06-24 14:38:25 +02:00
|
|
|
key_types = set(self.constructors.generate_expressions(self.constructors.key_types))
|
|
|
|
algorithms = set(self.constructors.generate_expressions(self.constructors.sign_algorithms))
|
2021-06-24 10:16:44 +02:00
|
|
|
alg_with_keys = {} #type: Dict[str, List[str]]
|
|
|
|
translation_table = str.maketrans('(', '_', ')')
|
|
|
|
for alg in algorithms:
|
|
|
|
# Generate keywords from the name of the algorithm
|
|
|
|
alg_keywords = set(alg.partition('(')[0].split(sep='_')[2:])
|
|
|
|
# Translate keywords for better matching with the key types
|
|
|
|
for keyword in alg_keywords.copy():
|
|
|
|
for pattern, replace in keyword_translation.items():
|
|
|
|
if re.match(pattern, keyword):
|
|
|
|
alg_keywords.remove(keyword)
|
|
|
|
alg_keywords.add(replace)
|
2021-12-21 06:14:10 +01:00
|
|
|
# Filter out incompatible algorithms
|
2021-06-24 10:16:44 +02:00
|
|
|
if not alg_keywords.isdisjoint(incompatible_alg_keyword):
|
|
|
|
continue
|
|
|
|
|
|
|
|
for key_type in key_types:
|
|
|
|
# Generate keywords from the of the key type
|
|
|
|
key_type_keywords = set(key_type.translate(translation_table).split(sep='_')[3:])
|
|
|
|
|
2021-12-21 06:14:10 +01:00
|
|
|
# Remove ambiguous keywords
|
2021-06-24 10:16:44 +02:00
|
|
|
for keyword1, keyword2 in exclusive_keywords.items():
|
|
|
|
if keyword1 in key_type_keywords:
|
|
|
|
key_type_keywords.remove(keyword2)
|
|
|
|
|
|
|
|
if key_type_keywords.isdisjoint(incompatible_key_type_keywords) and\
|
|
|
|
not key_type_keywords.isdisjoint(alg_keywords):
|
|
|
|
if alg in alg_with_keys:
|
|
|
|
alg_with_keys[alg].append(key_type)
|
|
|
|
else:
|
|
|
|
alg_with_keys[alg] = [key_type]
|
|
|
|
return alg_with_keys
|
|
|
|
|
2021-06-29 15:29:24 +02:00
|
|
|
def all_keys_for_implicit_usage(self) -> Iterator[StorageTestData]:
|
2021-06-24 10:16:44 +02:00
|
|
|
"""Generate test keys for usage flag extensions."""
|
|
|
|
# Generate a key type and algorithm pair for each extendable usage
|
|
|
|
# flag to generate a valid key for exercising. The key is generated
|
2021-12-21 06:14:10 +01:00
|
|
|
# without usage extension to check the extension compatibility.
|
2021-06-24 10:16:44 +02:00
|
|
|
alg_with_keys = self.gather_key_types_for_sign_alg()
|
2021-06-24 16:35:01 +02:00
|
|
|
|
2021-06-28 19:26:55 +02:00
|
|
|
for usage in sorted(StorageKey.IMPLICIT_USAGE_FLAGS, key=str):
|
|
|
|
for alg in sorted(alg_with_keys):
|
|
|
|
for key_type in sorted(alg_with_keys[alg]):
|
|
|
|
# The key types must be filtered to fit the specific usage flag.
|
2021-06-28 20:02:11 +02:00
|
|
|
kt = crypto_knowledge.KeyType(key_type)
|
2022-03-17 12:52:24 +01:00
|
|
|
if kt.is_public() and '_SIGN_' in usage:
|
|
|
|
# Can't sign with a public key
|
|
|
|
continue
|
|
|
|
yield self.keys_for_implicit_usage(usage, alg, kt)
|
2021-06-24 10:16:44 +02:00
|
|
|
|
2021-06-29 15:42:57 +02:00
|
|
|
def generate_all_keys(self) -> Iterator[StorageTestData]:
|
|
|
|
yield from super().generate_all_keys()
|
|
|
|
yield from self.all_keys_for_implicit_usage()
|
2021-06-24 10:04:38 +02:00
|
|
|
|
2021-03-10 02:12:08 +01:00
|
|
|
class TestGenerator:
|
|
|
|
"""Generate test data."""
|
|
|
|
|
|
|
|
def __init__(self, options) -> None:
|
|
|
|
self.test_suite_directory = self.get_option(options, 'directory',
|
|
|
|
'tests/suites')
|
|
|
|
self.info = Information()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_option(options, name: str, default: T) -> T:
|
|
|
|
value = getattr(options, name, None)
|
|
|
|
return default if value is None else value
|
|
|
|
|
2021-03-10 02:34:37 +01:00
|
|
|
def filename_for(self, basename: str) -> str:
|
|
|
|
"""The location of the data file with the specified base name."""
|
2021-05-07 11:49:17 +02:00
|
|
|
return posixpath.join(self.test_suite_directory, basename + '.data')
|
2021-03-10 02:34:37 +01:00
|
|
|
|
2021-03-10 02:12:08 +01:00
|
|
|
def write_test_data_file(self, basename: str,
|
|
|
|
test_cases: Iterable[test_case.TestCase]) -> None:
|
|
|
|
"""Write the test cases to a .data file.
|
|
|
|
|
|
|
|
The output file is ``basename + '.data'`` in the test suite directory.
|
|
|
|
"""
|
2021-03-10 02:34:37 +01:00
|
|
|
filename = self.filename_for(basename)
|
2021-03-10 02:12:08 +01:00
|
|
|
test_case.write_data_file(filename, test_cases)
|
|
|
|
|
2022-04-22 15:52:41 +02:00
|
|
|
# Note that targets whose names contain 'test_format' have their content
|
2021-04-23 16:37:12 +02:00
|
|
|
# validated by `abi_check.py`.
|
2021-03-10 02:34:37 +01:00
|
|
|
TARGETS = {
|
2021-10-15 15:21:51 +02:00
|
|
|
'test_suite_psa_crypto_generate_key.generated':
|
|
|
|
lambda info: KeyGenerate(info).test_cases_for_key_generation(),
|
2021-03-10 02:34:37 +01:00
|
|
|
'test_suite_psa_crypto_not_supported.generated':
|
2021-02-17 15:11:05 +01:00
|
|
|
lambda info: NotSupported(info).test_cases_for_not_supported(),
|
2021-04-27 20:40:10 +02:00
|
|
|
'test_suite_psa_crypto_op_fail.generated':
|
|
|
|
lambda info: OpFail(info).all_test_cases(),
|
2021-03-10 15:03:44 +01:00
|
|
|
'test_suite_psa_crypto_storage_format.current':
|
2021-06-24 09:53:26 +02:00
|
|
|
lambda info: StorageFormatForward(info, 0).all_test_cases(),
|
2021-03-10 15:03:44 +01:00
|
|
|
'test_suite_psa_crypto_storage_format.v0':
|
2021-06-24 09:53:26 +02:00
|
|
|
lambda info: StorageFormatV0(info).all_test_cases(),
|
2021-03-10 02:34:37 +01:00
|
|
|
} #type: Dict[str, Callable[[Information], Iterable[test_case.TestCase]]]
|
|
|
|
|
|
|
|
def generate_target(self, name: str) -> None:
|
|
|
|
test_cases = self.TARGETS[name](self.info)
|
|
|
|
self.write_test_data_file(name, test_cases)
|
2021-01-26 22:19:21 +01:00
|
|
|
|
2021-01-26 22:16:30 +01:00
|
|
|
def main(args):
|
|
|
|
"""Command line entry point."""
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
2021-03-10 02:34:37 +01:00
|
|
|
parser.add_argument('--list', action='store_true',
|
|
|
|
help='List available targets and exit')
|
2021-10-15 20:10:15 +02:00
|
|
|
parser.add_argument('--list-for-cmake', action='store_true',
|
|
|
|
help='Print \';\'-separated list of available targets and exit')
|
2021-05-14 11:37:09 +02:00
|
|
|
parser.add_argument('--directory', metavar='DIR',
|
|
|
|
help='Output directory (default: tests/suites)')
|
2021-03-10 02:34:37 +01:00
|
|
|
parser.add_argument('targets', nargs='*', metavar='TARGET',
|
|
|
|
help='Target file to generate (default: all; "-": none)')
|
2021-01-26 22:16:30 +01:00
|
|
|
options = parser.parse_args(args)
|
2021-04-22 00:20:47 +02:00
|
|
|
build_tree.chdir_to_root()
|
2021-01-26 22:16:30 +01:00
|
|
|
generator = TestGenerator(options)
|
2021-03-10 02:34:37 +01:00
|
|
|
if options.list:
|
|
|
|
for name in sorted(generator.TARGETS):
|
|
|
|
print(generator.filename_for(name))
|
|
|
|
return
|
2021-10-15 20:10:15 +02:00
|
|
|
# List in a cmake list format (i.e. ';'-separated)
|
|
|
|
if options.list_for_cmake:
|
2021-10-21 17:09:51 +02:00
|
|
|
print(';'.join(generator.filename_for(name)
|
|
|
|
for name in sorted(generator.TARGETS)), end='')
|
2021-10-15 20:10:15 +02:00
|
|
|
return
|
2021-03-10 02:34:37 +01:00
|
|
|
if options.targets:
|
|
|
|
# Allow "-" as a special case so you can run
|
|
|
|
# ``generate_psa_tests.py - $targets`` and it works uniformly whether
|
|
|
|
# ``$targets`` is empty or not.
|
|
|
|
options.targets = [os.path.basename(re.sub(r'\.data\Z', r'', target))
|
|
|
|
for target in options.targets
|
|
|
|
if target != '-']
|
|
|
|
else:
|
|
|
|
options.targets = sorted(generator.TARGETS)
|
|
|
|
for target in options.targets:
|
|
|
|
generator.generate_target(target)
|
2021-01-26 22:16:30 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv[1:])
|