blob: 0f7be604d7c03190d03d1950a49e0b8e1c80b48e [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
20
21class PSAMacroCollector:
22 """Collect PSA crypto macro definitions from C header files.
23 """
24
25 def __init__(self):
26 self.statuses = set()
27 self.key_types = set()
28 self.key_types_from_curve = {}
29 self.key_types_from_group = {}
30 self.ecc_curves = set()
31 self.dh_groups = set()
32 self.algorithms = set()
33 self.hash_algorithms = set()
34 self.ka_algorithms = set()
35 self.algorithms_from_hash = {}
36 self.key_usages = set()
37
Gilles Peskinef8deb752021-01-25 22:41:45 +010038 def is_internal_name(self, name):
39 """Whether this is an internal macro. Internal macros will be skipped."""
40 return name.endswith('_FLAG') or name.endswith('MASK')
41
Gilles Peskinee7c44552021-01-25 21:40:45 +010042 # "#define" followed by a macro name with either no parameters
43 # or a single parameter and a non-empty expansion.
44 # Grab the macro name in group 1, the parameter name if any in group 2
45 # and the expansion in group 3.
46 _define_directive_re = re.compile(r'\s*#\s*define\s+(\w+)' +
47 r'(?:\s+|\((\w+)\)\s*)' +
48 r'(.+)')
49 _deprecated_definition_re = re.compile(r'\s*MBEDTLS_DEPRECATED')
50
51 def read_line(self, line):
52 """Parse a C header line and record the PSA identifier it defines if any.
53 This function analyzes lines that start with "#define PSA_"
54 (up to non-significant whitespace) and skips all non-matching lines.
55 """
56 # pylint: disable=too-many-branches
57 m = re.match(self._define_directive_re, line)
58 if not m:
59 return
60 name, parameter, expansion = m.groups()
61 expansion = re.sub(r'/\*.*?\*/|//.*', r' ', expansion)
62 if re.match(self._deprecated_definition_re, expansion):
63 # Skip deprecated values, which are assumed to be
64 # backward compatibility aliases that share
65 # numerical values with non-deprecated values.
66 return
Gilles Peskinef8deb752021-01-25 22:41:45 +010067 if self.is_internal_name(name):
Gilles Peskinee7c44552021-01-25 21:40:45 +010068 # Macro only to build actual values
69 return
70 elif (name.startswith('PSA_ERROR_') or name == 'PSA_SUCCESS') \
71 and not parameter:
72 self.statuses.add(name)
73 elif name.startswith('PSA_KEY_TYPE_') and not parameter:
74 self.key_types.add(name)
75 elif name.startswith('PSA_KEY_TYPE_') and parameter == 'curve':
76 self.key_types_from_curve[name] = name[:13] + 'IS_' + name[13:]
77 elif name.startswith('PSA_KEY_TYPE_') and parameter == 'group':
78 self.key_types_from_group[name] = name[:13] + 'IS_' + name[13:]
79 elif name.startswith('PSA_ECC_FAMILY_') and not parameter:
80 self.ecc_curves.add(name)
81 elif name.startswith('PSA_DH_FAMILY_') and not parameter:
82 self.dh_groups.add(name)
83 elif name.startswith('PSA_ALG_') and not parameter:
84 if name in ['PSA_ALG_ECDSA_BASE',
85 'PSA_ALG_RSA_PKCS1V15_SIGN_BASE']:
86 # Ad hoc skipping of duplicate names for some numerical values
87 return
88 self.algorithms.add(name)
89 # Ad hoc detection of hash algorithms
90 if re.search(r'0x020000[0-9A-Fa-f]{2}', expansion):
91 self.hash_algorithms.add(name)
92 # Ad hoc detection of key agreement algorithms
93 if re.search(r'0x09[0-9A-Fa-f]{2}0000', expansion):
94 self.ka_algorithms.add(name)
95 elif name.startswith('PSA_ALG_') and parameter == 'hash_alg':
96 if name in ['PSA_ALG_DSA', 'PSA_ALG_ECDSA']:
97 # A naming irregularity
98 tester = name[:8] + 'IS_RANDOMIZED_' + name[8:]
99 else:
100 tester = name[:8] + 'IS_' + name[8:]
101 self.algorithms_from_hash[name] = tester
102 elif name.startswith('PSA_KEY_USAGE_') and not parameter:
103 self.key_usages.add(name)
104 else:
105 # Other macro without parameter
106 return
107
108 _nonascii_re = re.compile(rb'[^\x00-\x7f]+')
109 _continued_line_re = re.compile(rb'\\\r?\n\Z')
110 def read_file(self, header_file):
111 for line in header_file:
112 m = re.search(self._continued_line_re, line)
113 while m:
114 cont = next(header_file)
115 line = line[:m.start(0)] + cont
116 m = re.search(self._continued_line_re, line)
117 line = re.sub(self._nonascii_re, rb'', line).decode('ascii')
118 self.read_line(line)