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])
             yield from self.test_cases_for_key_type_key_generation(kt)
Exemple #2
0
 def test_cases_for_not_supported(self) -> Iterator[test_case.TestCase]:
     """Generate test cases that exercise the creation of keys of unsupported types."""
     for key_type in sorted(self.constructors.key_types):
         kt = crypto_knowledge.KeyType(key_type)
         yield from self.test_cases_for_key_type_not_supported(kt)
     for curve_family in sorted(self.constructors.ecc_curves):
         for constr in ('PSA_KEY_TYPE_ECC_KEY_PAIR',
                        'PSA_KEY_TYPE_ECC_PUBLIC_KEY'):
             kt = crypto_knowledge.KeyType(constr, [curve_family])
             yield from self.test_cases_for_key_type_not_supported(
                 kt, param_descr='type')
             yield from self.test_cases_for_key_type_not_supported(
                 kt, 0, param_descr='curve')
Exemple #3
0
    def keys_for_type(self,
                      key_type: str,
                      params: Optional[Iterable[str]] = None
                      ) -> Iterator[StorageTestData]:
        """Generate test keys for the given key type.

        For key types that depend on a parameter (e.g. elliptic curve family),
        `param` is the parameter to pass to the constructor. Only a single
        parameter is supported.
        """
        kt = crypto_knowledge.KeyType(key_type, params)
        for bits in kt.sizes_to_test():
            usage_flags = 'PSA_KEY_USAGE_EXPORT'
            alg = 0
            alg2 = 0
            key_material = kt.key_material(bits)
            short_expression = re.sub(r'\bPSA_(?:KEY_TYPE|ECC_FAMILY)_', r'',
                                      kt.expression)
            description = 'type: {} {}-bit'.format(short_expression, bits)
            key = StorageTestData(version=self.version,
                                  id=1,
                                  lifetime=0x00000001,
                                  type=kt.expression,
                                  bits=bits,
                                  usage=usage_flags,
                                  alg=alg,
                                  alg2=alg2,
                                  material=key_material,
                                  description=description)
            yield key
Exemple #4
0
    def all_keys_for_implicit_usage(self) -> Iterator[StorageTestData]:
        """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
        # without usage extension to check the extension compatiblity.
        alg_with_keys = self.gather_key_types_for_sign_alg()

        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.
                    kt = crypto_knowledge.KeyType(key_type)
                    if kt.is_valid_for_signature(usage):
                        yield self.keys_for_implicit_usage(usage, alg, kt)