blob: e79557953c8af808afea5ad796370b3ecde928d0 [file] [log] [blame]
Gilles Peskine0156a152021-01-26 21:23:56 +01001"""Knowledge about cryptographic mechanisms implemented in Mbed TLS.
2
3This module is entirely based on the PSA API.
4"""
5
6# Copyright The Mbed TLS Contributors
7# SPDX-License-Identifier: Apache-2.0
8#
9# Licensed under the Apache License, Version 2.0 (the "License"); you may
10# not use this file except in compliance with the License.
11# You may obtain a copy of the License at
12#
13# http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18# See the License for the specific language governing permissions and
19# limitations under the License.
20
21import re
22from typing import List, Optional
23
24class KeyType:
25 """Knowledge about a PSA key type."""
26
27 def __init__(self, name: str, params: Optional[List[str]] = None):
28 """Analyze a key type.
29
30 The key type must be specified in PSA syntax. In its simplest form,
31 this is a string 'PSA_KEY_TYPE_xxx' which is the name of a PSA key
32 type macro. For key types that take arguments, the arguments can
33 be passed either through the optional argument `params` or by
34 passing an expression of the form 'PSA_KEY_TYPE_xxx(param1, param2)'
35 as the a string.
36 """
37 self.name = name.strip()
38 if params is None:
39 if '(' in self.name:
40 m = re.match(r'(\w+)\s*\((.*)\)\Z', self.name)
41 assert m is not None
42 self.name = m.group(1)
43 params = ','.split(m.group(2))
44 if params is None:
45 self.params = params
46 else:
47 self.params = [param.strip() for param in params]
48 self.expression = self.name
49 if self.params is not None:
50 self.expression += '(' + ', '.join(self.params) + ')'
51 self.private_type = re.sub(r'_PUBLIC_KEY\Z', r'_KEY_PAIR', self.name)