blob: 7ebd8f7c3f417fee3de658cae39af3520054ee1b [file] [log] [blame]
Gilles Peskinee7c44552021-01-25 21:40:45 +01001"""Collect macro definitions from header files.
2"""
3
4# Copyright The Mbed TLS Contributors
5# SPDX-License-Identifier: Apache-2.0
6#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18
19import re
Gilles Peskine10ab2672021-03-10 00:59:53 +010020from typing import Dict, Set
Gilles Peskinee7c44552021-01-25 21:40:45 +010021
22class PSAMacroCollector:
23 """Collect PSA crypto macro definitions from C header files.
24 """
25
Gilles Peskine10ab2672021-03-10 00:59:53 +010026 def __init__(self, include_intermediate: bool = False) -> None:
Gilles Peskine13d60eb2021-01-25 22:42:14 +010027 """Set up an object to collect PSA macro definitions.
28
29 Call the read_file method of the constructed object on each header file.
30
31 * include_intermediate: if true, include intermediate macros such as
32 PSA_XXX_BASE that do not designate semantic values.
33 """
34 self.include_intermediate = include_intermediate
Gilles Peskine10ab2672021-03-10 00:59:53 +010035 self.statuses = set() #type: Set[str]
36 self.key_types = set() #type: Set[str]
37 self.key_types_from_curve = {} #type: Dict[str, str]
38 self.key_types_from_group = {} #type: Dict[str, str]
39 self.ecc_curves = set() #type: Set[str]
40 self.dh_groups = set() #type: Set[str]
41 self.algorithms = set() #type: Set[str]
42 self.hash_algorithms = set() #type: Set[str]
43 self.ka_algorithms = set() #type: Set[str]
44 self.algorithms_from_hash = {} #type: Dict[str, str]
45 self.key_usages = set() #type: Set[str]
Gilles Peskinee7c44552021-01-25 21:40:45 +010046
Gilles Peskine10ab2672021-03-10 00:59:53 +010047 def is_internal_name(self, name: str) -> bool:
Gilles Peskinef8deb752021-01-25 22:41:45 +010048 """Whether this is an internal macro. Internal macros will be skipped."""
Gilles Peskine13d60eb2021-01-25 22:42:14 +010049 if not self.include_intermediate:
50 if name.endswith('_BASE') or name.endswith('_NONE'):
51 return True
52 if '_CATEGORY_' in name:
53 return True
Gilles Peskine0655b4f2021-01-25 22:44:36 +010054 return name.endswith('_FLAG') or name.endswith('_MASK')
Gilles Peskinef8deb752021-01-25 22:41:45 +010055
Gilles Peskinee7c44552021-01-25 21:40:45 +010056 # "#define" followed by a macro name with either no parameters
57 # or a single parameter and a non-empty expansion.
58 # Grab the macro name in group 1, the parameter name if any in group 2
59 # and the expansion in group 3.
60 _define_directive_re = re.compile(r'\s*#\s*define\s+(\w+)' +
61 r'(?:\s+|\((\w+)\)\s*)' +
62 r'(.+)')
63 _deprecated_definition_re = re.compile(r'\s*MBEDTLS_DEPRECATED')
64
65 def read_line(self, line):
66 """Parse a C header line and record the PSA identifier it defines if any.
67 This function analyzes lines that start with "#define PSA_"
68 (up to non-significant whitespace) and skips all non-matching lines.
69 """
70 # pylint: disable=too-many-branches
71 m = re.match(self._define_directive_re, line)
72 if not m:
73 return
74 name, parameter, expansion = m.groups()
75 expansion = re.sub(r'/\*.*?\*/|//.*', r' ', expansion)
76 if re.match(self._deprecated_definition_re, expansion):
77 # Skip deprecated values, which are assumed to be
78 # backward compatibility aliases that share
79 # numerical values with non-deprecated values.
80 return
Gilles Peskinef8deb752021-01-25 22:41:45 +010081 if self.is_internal_name(name):
Gilles Peskinee7c44552021-01-25 21:40:45 +010082 # Macro only to build actual values
83 return
84 elif (name.startswith('PSA_ERROR_') or name == 'PSA_SUCCESS') \
85 and not parameter:
86 self.statuses.add(name)
87 elif name.startswith('PSA_KEY_TYPE_') and not parameter:
88 self.key_types.add(name)
89 elif name.startswith('PSA_KEY_TYPE_') and parameter == 'curve':
90 self.key_types_from_curve[name] = name[:13] + 'IS_' + name[13:]
91 elif name.startswith('PSA_KEY_TYPE_') and parameter == 'group':
92 self.key_types_from_group[name] = name[:13] + 'IS_' + name[13:]
93 elif name.startswith('PSA_ECC_FAMILY_') and not parameter:
94 self.ecc_curves.add(name)
95 elif name.startswith('PSA_DH_FAMILY_') and not parameter:
96 self.dh_groups.add(name)
97 elif name.startswith('PSA_ALG_') and not parameter:
98 if name in ['PSA_ALG_ECDSA_BASE',
99 'PSA_ALG_RSA_PKCS1V15_SIGN_BASE']:
100 # Ad hoc skipping of duplicate names for some numerical values
101 return
102 self.algorithms.add(name)
103 # Ad hoc detection of hash algorithms
104 if re.search(r'0x020000[0-9A-Fa-f]{2}', expansion):
105 self.hash_algorithms.add(name)
106 # Ad hoc detection of key agreement algorithms
107 if re.search(r'0x09[0-9A-Fa-f]{2}0000', expansion):
108 self.ka_algorithms.add(name)
109 elif name.startswith('PSA_ALG_') and parameter == 'hash_alg':
110 if name in ['PSA_ALG_DSA', 'PSA_ALG_ECDSA']:
111 # A naming irregularity
112 tester = name[:8] + 'IS_RANDOMIZED_' + name[8:]
113 else:
114 tester = name[:8] + 'IS_' + name[8:]
115 self.algorithms_from_hash[name] = tester
116 elif name.startswith('PSA_KEY_USAGE_') and not parameter:
117 self.key_usages.add(name)
118 else:
119 # Other macro without parameter
120 return
121
122 _nonascii_re = re.compile(rb'[^\x00-\x7f]+')
123 _continued_line_re = re.compile(rb'\\\r?\n\Z')
124 def read_file(self, header_file):
125 for line in header_file:
126 m = re.search(self._continued_line_re, line)
127 while m:
128 cont = next(header_file)
129 line = line[:m.start(0)] + cont
130 m = re.search(self._continued_line_re, line)
131 line = re.sub(self._nonascii_re, rb'', line).decode('ascii')
132 self.read_line(line)