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