Skip to content

Commit c893235

Browse files
Merge pull request #115 from gilles-peskine-arm/psa-error-compatibility_aliases
Improve how generate_psa_constants handles compatibility aliases
2 parents a291413 + 1983512 commit c893235

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

include/psa/crypto_extra.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,14 @@ extern "C" {
4545
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
4646
#define PSA_ERROR_UNKNOWN_ERROR \
4747
MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_GENERIC_ERROR )
48-
#endif
49-
50-
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
5148
#define PSA_ERROR_OCCUPIED_SLOT \
5249
MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_ALREADY_EXISTS )
53-
#endif
54-
55-
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
5650
#define PSA_ERROR_EMPTY_SLOT \
5751
MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_DOES_NOT_EXIST )
58-
#endif
59-
60-
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
6152
#define PSA_ERROR_INSUFFICIENT_CAPACITY \
6253
MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_INSUFFICIENT_DATA )
54+
#define PSA_ERROR_TAMPERING_DETECTED \
55+
MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_CORRUPTION_DETECTED )
6356
#endif
6457

6558
/** \addtogroup attributes

scripts/generate_psa_constants.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,34 +205,35 @@ def __init__(self):
205205
self.key_usages = set()
206206

207207
# "#define" followed by a macro name with either no parameters
208-
# or a single parameter. Grab the macro name in group 1, the
209-
# parameter name if any in group 2 and the definition in group 3.
210-
definition_re = re.compile(r'\s*#\s*define\s+(\w+)(?:\s+|\((\w+)\)\s*)(.+)(?:/[*/])?')
208+
# or a single parameter and a non-empty expansion.
209+
# Grab the macro name in group 1, the parameter name if any in group 2
210+
# and the expansion in group 3.
211+
_define_directive_re = re.compile(r'\s*#\s*define\s+(\w+)' +
212+
r'(?:\s+|\((\w+)\)\s*)' +
213+
r'(.+)')
214+
_deprecated_definition_re = re.compile(r'\s*MBEDTLS_DEPRECATED')
211215

212216
def read_line(self, line):
213217
"""Parse a C header line and record the PSA identifier it defines if any.
214218
This function analyzes lines that start with "#define PSA_"
215219
(up to non-significant whitespace) and skips all non-matching lines.
216220
"""
217221
# pylint: disable=too-many-branches
218-
m = re.match(self.definition_re, line)
222+
m = re.match(self._define_directive_re, line)
219223
if not m:
220224
return
221-
name, parameter, definition = m.groups()
225+
name, parameter, expansion = m.groups()
226+
expansion = re.sub(r'/\*.*?\*/|//.*', r' ', expansion)
227+
if re.match(self._deprecated_definition_re, expansion):
228+
# Skip deprecated values, which are assumed to be
229+
# backward compatibility aliases that share
230+
# numerical values with non-deprecated values.
231+
return
222232
if name.endswith('_FLAG') or name.endswith('MASK'):
223233
# Macro only to build actual values
224234
return
225235
elif (name.startswith('PSA_ERROR_') or name == 'PSA_SUCCESS') \
226236
and not parameter:
227-
if name in ['PSA_ERROR_UNKNOWN_ERROR',
228-
'PSA_ERROR_OCCUPIED_SLOT',
229-
'PSA_ERROR_EMPTY_SLOT',
230-
'PSA_ERROR_INSUFFICIENT_CAPACITY',
231-
]:
232-
# Ad hoc skipping of deprecated error codes, which share
233-
# numerical values with non-deprecated error codes
234-
return
235-
236237
self.statuses.add(name)
237238
elif name.startswith('PSA_KEY_TYPE_') and not parameter:
238239
self.key_types.add(name)
@@ -251,10 +252,10 @@ def read_line(self, line):
251252
return
252253
self.algorithms.add(name)
253254
# Ad hoc detection of hash algorithms
254-
if re.search(r'0x010000[0-9A-Fa-f]{2}', definition):
255+
if re.search(r'0x010000[0-9A-Fa-f]{2}', expansion):
255256
self.hash_algorithms.add(name)
256257
# Ad hoc detection of key agreement algorithms
257-
if re.search(r'0x30[0-9A-Fa-f]{2}0000', definition):
258+
if re.search(r'0x30[0-9A-Fa-f]{2}0000', expansion):
258259
self.ka_algorithms.add(name)
259260
elif name.startswith('PSA_ALG_') and parameter == 'hash_alg':
260261
if name in ['PSA_ALG_DSA', 'PSA_ALG_ECDSA']:
@@ -271,6 +272,9 @@ def read_line(self, line):
271272

272273
def read_file(self, header_file):
273274
for line in header_file:
275+
while line.endswith('\\\n'):
276+
cont = next(header_file)
277+
line = line[:-2] + cont
274278
self.read_line(line)
275279

276280
@staticmethod

tests/scripts/test_psa_constant_names.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,25 @@ def _argument_split(cls, arguments):
159159
# Regex of macro names to exclude.
160160
_excluded_name_re = re.compile(r'_(?:GET|IS|OF)_|_(?:BASE|FLAG|MASK)\Z')
161161
# Additional excluded macros.
162-
# PSA_ALG_ECDH and PSA_ALG_FFDH are excluded for now as the script
163-
# currently doesn't support them. Deprecated errors are also excluded.
164-
_excluded_names = set(['PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH',
165-
'PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE',
166-
'PSA_ALG_FULL_LENGTH_MAC',
167-
'PSA_ALG_ECDH',
168-
'PSA_ALG_FFDH',
169-
'PSA_ERROR_UNKNOWN_ERROR',
170-
'PSA_ERROR_OCCUPIED_SLOT',
171-
'PSA_ERROR_EMPTY_SLOT',
172-
'PSA_ERROR_INSUFFICIENT_CAPACITY',
173-
])
174-
162+
_excluded_names = set([
163+
# Macros that provide an alternative way to build the same
164+
# algorithm as another macro.
165+
'PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH',
166+
'PSA_ALG_FULL_LENGTH_MAC',
167+
# Auxiliary macro whose name doesn't fit the usual patterns for
168+
# auxiliary macros.
169+
'PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH_CASE',
170+
# PSA_ALG_ECDH and PSA_ALG_FFDH are excluded for now as the script
171+
# currently doesn't support them.
172+
'PSA_ALG_ECDH',
173+
'PSA_ALG_FFDH',
174+
# Deprecated aliases.
175+
'PSA_ERROR_UNKNOWN_ERROR',
176+
'PSA_ERROR_OCCUPIED_SLOT',
177+
'PSA_ERROR_EMPTY_SLOT',
178+
'PSA_ERROR_INSUFFICIENT_CAPACITY',
179+
'PSA_ERROR_TAMPERING_DETECTED',
180+
])
175181
def parse_header_line(self, line):
176182
"""Parse a C header line, looking for "#define PSA_xxx"."""
177183
m = re.match(self._header_line_re, line)

0 commit comments

Comments
 (0)