2018-09-25 18:49:23 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
'''Test the program psa_constant_names.
|
|
|
|
Gather constant names from header files and test cases. Compile a C program
|
|
|
|
to print out their numerical values, feed these numerical values to
|
|
|
|
psa_constant_names, and check that the output is the original name.
|
|
|
|
Return 0 if all test cases pass, 1 if the output was not always as expected,
|
|
|
|
or 1 (with a Python backtrace) if there was an operational error.'''
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import itertools
|
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
|
2018-10-19 11:27:10 +02:00
|
|
|
class ReadFileLineException(Exception):
|
|
|
|
def __init__(self, filename, line_number):
|
|
|
|
message = 'in {} at {}'.format(filename, line_number)
|
|
|
|
super(ReadFileLineException, self).__init__(message)
|
|
|
|
self.filename = filename
|
|
|
|
self.line_number = line_number
|
|
|
|
|
|
|
|
class read_file_lines:
|
|
|
|
'''Context manager to read a text file line by line.
|
|
|
|
with read_file_lines(filename) as lines:
|
|
|
|
for line in lines:
|
|
|
|
process(line)
|
|
|
|
is equivalent to
|
|
|
|
with open(filename, 'r') as input_file:
|
|
|
|
for line in input_file:
|
|
|
|
process(line)
|
|
|
|
except that if process(line) raises an exception, then the read_file_lines
|
|
|
|
snippet annotates the exception with the file name and line number.'''
|
|
|
|
def __init__(self, filename):
|
|
|
|
self.filename = filename
|
|
|
|
self.line_number = 'entry'
|
|
|
|
def __enter__(self):
|
|
|
|
self.generator = enumerate(open(self.filename, 'r'))
|
|
|
|
return self
|
|
|
|
def __iter__(self):
|
|
|
|
for line_number, content in self.generator:
|
|
|
|
self.line_number = line_number
|
|
|
|
yield content
|
|
|
|
self.line_number = 'exit'
|
2019-05-27 18:29:47 +02:00
|
|
|
def __exit__(self, exc_type, exc_value, exc_traceback):
|
|
|
|
if exc_type is not None:
|
2018-10-19 11:27:10 +02:00
|
|
|
raise ReadFileLineException(self.filename, self.line_number) \
|
2019-05-27 18:29:47 +02:00
|
|
|
from exc_value
|
2018-10-19 11:27:10 +02:00
|
|
|
|
2018-09-25 18:49:23 +02:00
|
|
|
class Inputs:
|
|
|
|
'''Accumulate information about macros to test.
|
|
|
|
This includes macro names as well as information about their arguments
|
|
|
|
when applicable.'''
|
|
|
|
def __init__(self):
|
|
|
|
# Sets of names per type
|
|
|
|
self.statuses = set(['PSA_SUCCESS'])
|
|
|
|
self.algorithms = set(['0xffffffff'])
|
|
|
|
self.ecc_curves = set(['0xffff'])
|
2019-05-16 12:55:35 +02:00
|
|
|
self.dh_groups = set(['0xffff'])
|
2018-09-25 18:49:23 +02:00
|
|
|
self.key_types = set(['0xffffffff'])
|
|
|
|
self.key_usage_flags = set(['0x80000000'])
|
2018-10-19 11:30:26 +02:00
|
|
|
# Hard-coded value for unknown algorithms
|
2019-02-04 17:00:21 +01:00
|
|
|
self.hash_algorithms = set(['0x010000fe'])
|
2018-10-19 11:30:26 +02:00
|
|
|
self.mac_algorithms = set(['0x02ff00ff'])
|
2019-04-12 00:12:07 +02:00
|
|
|
self.ka_algorithms = set(['0x30fc0000'])
|
|
|
|
self.kdf_algorithms = set(['0x200000ff'])
|
2018-10-19 11:30:26 +02:00
|
|
|
# For AEAD algorithms, the only variability is over the tag length,
|
|
|
|
# and this only applies to known algorithms, so don't test an
|
|
|
|
# unknown algorithm.
|
|
|
|
self.aead_algorithms = set()
|
2018-09-25 18:49:23 +02:00
|
|
|
# Identifier prefixes
|
|
|
|
self.table_by_prefix = {
|
|
|
|
'ERROR': self.statuses,
|
|
|
|
'ALG': self.algorithms,
|
|
|
|
'CURVE': self.ecc_curves,
|
2019-05-16 12:55:35 +02:00
|
|
|
'GROUP': self.dh_groups,
|
2018-09-25 18:49:23 +02:00
|
|
|
'KEY_TYPE': self.key_types,
|
|
|
|
'KEY_USAGE': self.key_usage_flags,
|
|
|
|
}
|
|
|
|
# macro name -> list of argument names
|
|
|
|
self.argspecs = {}
|
|
|
|
# argument name -> list of values
|
2018-10-19 11:30:26 +02:00
|
|
|
self.arguments_for = {
|
|
|
|
'mac_length': ['1', '63'],
|
|
|
|
'tag_length': ['1', '63'],
|
|
|
|
}
|
2018-09-25 18:49:23 +02:00
|
|
|
|
|
|
|
def gather_arguments(self):
|
|
|
|
'''Populate the list of values for macro arguments.
|
|
|
|
Call this after parsing all the inputs.'''
|
|
|
|
self.arguments_for['hash_alg'] = sorted(self.hash_algorithms)
|
2018-10-19 11:30:26 +02:00
|
|
|
self.arguments_for['mac_alg'] = sorted(self.mac_algorithms)
|
2019-04-12 00:12:07 +02:00
|
|
|
self.arguments_for['ka_alg'] = sorted(self.ka_algorithms)
|
2019-01-04 19:46:31 +01:00
|
|
|
self.arguments_for['kdf_alg'] = sorted(self.kdf_algorithms)
|
2018-10-19 11:30:26 +02:00
|
|
|
self.arguments_for['aead_alg'] = sorted(self.aead_algorithms)
|
2018-09-25 18:49:23 +02:00
|
|
|
self.arguments_for['curve'] = sorted(self.ecc_curves)
|
2019-05-16 12:55:35 +02:00
|
|
|
self.arguments_for['group'] = sorted(self.dh_groups)
|
2018-09-25 18:49:23 +02:00
|
|
|
|
2019-05-27 18:29:47 +02:00
|
|
|
@staticmethod
|
|
|
|
def _format_arguments(name, arguments):
|
2018-09-25 18:49:23 +02:00
|
|
|
'''Format a macro call with arguments..'''
|
|
|
|
return name + '(' + ', '.join(arguments) + ')'
|
|
|
|
|
|
|
|
def distribute_arguments(self, name):
|
|
|
|
'''Generate macro calls with each tested argument set.
|
|
|
|
If name is a macro without arguments, just yield "name".
|
|
|
|
If name is a macro with arguments, yield a series of "name(arg1,...,argN)"
|
|
|
|
where each argument takes each possible value at least once.'''
|
2018-10-19 11:27:10 +02:00
|
|
|
try:
|
|
|
|
if name not in self.argspecs:
|
|
|
|
yield name
|
|
|
|
return
|
|
|
|
argspec = self.argspecs[name]
|
|
|
|
if argspec == []:
|
|
|
|
yield name + '()'
|
|
|
|
return
|
|
|
|
argument_lists = [self.arguments_for[arg] for arg in argspec]
|
|
|
|
arguments = [values[0] for values in argument_lists]
|
2019-05-27 18:29:47 +02:00
|
|
|
yield self._format_arguments(name, arguments)
|
2018-10-19 11:27:10 +02:00
|
|
|
for i in range(len(arguments)):
|
|
|
|
for value in argument_lists[i][1:]:
|
|
|
|
arguments[i] = value
|
2019-05-27 18:29:47 +02:00
|
|
|
yield self._format_arguments(name, arguments)
|
2018-10-19 11:29:56 +02:00
|
|
|
arguments[i] = argument_lists[0][0]
|
2018-10-19 11:27:10 +02:00
|
|
|
except BaseException as e:
|
|
|
|
raise Exception('distribute_arguments({})'.format(name)) from e
|
2018-09-25 18:49:23 +02:00
|
|
|
|
2019-05-27 18:29:47 +02:00
|
|
|
_argument_split_re = re.compile(r' *, *')
|
|
|
|
@classmethod
|
|
|
|
def _argument_split(cls, arguments):
|
|
|
|
return re.split(cls._argument_split_re, arguments)
|
|
|
|
|
2018-09-25 18:49:23 +02:00
|
|
|
# Regex for interesting header lines.
|
|
|
|
# Groups: 1=macro name, 2=type, 3=argument list (optional).
|
2019-05-27 18:29:47 +02:00
|
|
|
_header_line_re = \
|
2018-09-25 18:49:23 +02:00
|
|
|
re.compile(r'#define +' +
|
|
|
|
r'(PSA_((?:KEY_)?[A-Z]+)_\w+)' +
|
|
|
|
r'(?:\(([^\n()]*)\))?')
|
|
|
|
# Regex of macro names to exclude.
|
2019-05-27 18:29:47 +02:00
|
|
|
_excluded_name_re = re.compile(r'_(?:GET|IS|OF)_|_(?:BASE|FLAG|MASK)\Z')
|
2018-10-19 11:31:52 +02:00
|
|
|
# Additional excluded macros.
|
2019-02-06 14:21:31 +01:00
|
|
|
# PSA_ALG_ECDH and PSA_ALG_FFDH are excluded for now as the script
|
2019-02-21 11:41:29 +01:00
|
|
|
# currently doesn't support them. Deprecated errors are also excluded.
|
2019-05-27 18:29:47 +02:00
|
|
|
_excluded_names = set(['PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH',
|
|
|
|
'PSA_ALG_FULL_LENGTH_MAC',
|
|
|
|
'PSA_ALG_ECDH',
|
|
|
|
'PSA_ALG_FFDH',
|
|
|
|
'PSA_ERROR_UNKNOWN_ERROR',
|
|
|
|
'PSA_ERROR_OCCUPIED_SLOT',
|
|
|
|
'PSA_ERROR_EMPTY_SLOT',
|
|
|
|
'PSA_ERROR_INSUFFICIENT_CAPACITY',
|
2019-02-21 11:41:29 +01:00
|
|
|
])
|
2019-05-27 18:29:47 +02:00
|
|
|
|
2018-09-25 18:49:23 +02:00
|
|
|
def parse_header_line(self, line):
|
|
|
|
'''Parse a C header line, looking for "#define PSA_xxx".'''
|
2019-05-27 18:29:47 +02:00
|
|
|
m = re.match(self._header_line_re, line)
|
2018-09-25 18:49:23 +02:00
|
|
|
if not m:
|
|
|
|
return
|
|
|
|
name = m.group(1)
|
2019-05-27 18:29:47 +02:00
|
|
|
if re.search(self._excluded_name_re, name) or \
|
|
|
|
name in self._excluded_names:
|
2018-09-25 18:49:23 +02:00
|
|
|
return
|
|
|
|
dest = self.table_by_prefix.get(m.group(2))
|
|
|
|
if dest is None:
|
|
|
|
return
|
|
|
|
dest.add(name)
|
|
|
|
if m.group(3):
|
2019-05-27 18:29:47 +02:00
|
|
|
self.argspecs[name] = self._argument_split(m.group(3))
|
2018-09-25 18:49:23 +02:00
|
|
|
|
|
|
|
def parse_header(self, filename):
|
|
|
|
'''Parse a C header file, looking for "#define PSA_xxx".'''
|
2018-10-19 11:27:10 +02:00
|
|
|
with read_file_lines(filename) as lines:
|
|
|
|
for line in lines:
|
2018-09-25 18:49:23 +02:00
|
|
|
self.parse_header_line(line)
|
|
|
|
|
|
|
|
def add_test_case_line(self, function, argument):
|
|
|
|
'''Parse a test case data line, looking for algorithm metadata tests.'''
|
|
|
|
if function.endswith('_algorithm'):
|
2019-02-06 14:21:31 +01:00
|
|
|
# As above, ECDH and FFDH algorithms are excluded for now.
|
|
|
|
# Support for them will be added in the future.
|
2019-01-29 16:48:00 +01:00
|
|
|
if 'ECDH' in argument or 'FFDH' in argument:
|
|
|
|
return
|
2018-09-25 18:49:23 +02:00
|
|
|
self.algorithms.add(argument)
|
|
|
|
if function == 'hash_algorithm':
|
|
|
|
self.hash_algorithms.add(argument)
|
2018-10-19 11:30:26 +02:00
|
|
|
elif function in ['mac_algorithm', 'hmac_algorithm']:
|
|
|
|
self.mac_algorithms.add(argument)
|
|
|
|
elif function == 'aead_algorithm':
|
|
|
|
self.aead_algorithms.add(argument)
|
2018-09-25 18:49:23 +02:00
|
|
|
elif function == 'key_type':
|
|
|
|
self.key_types.add(argument)
|
|
|
|
elif function == 'ecc_key_types':
|
|
|
|
self.ecc_curves.add(argument)
|
2019-05-16 12:55:35 +02:00
|
|
|
elif function == 'dh_key_types':
|
|
|
|
self.dh_groups.add(argument)
|
2018-09-25 18:49:23 +02:00
|
|
|
|
|
|
|
# Regex matching a *.data line containing a test function call and
|
|
|
|
# its arguments. The actual definition is partly positional, but this
|
|
|
|
# regex is good enough in practice.
|
2019-05-27 18:29:47 +02:00
|
|
|
_test_case_line_re = re.compile(r'(?!depends_on:)(\w+):([^\n :][^:\n]*)')
|
2018-09-25 18:49:23 +02:00
|
|
|
def parse_test_cases(self, filename):
|
|
|
|
'''Parse a test case file (*.data), looking for algorithm metadata tests.'''
|
2018-10-19 11:27:10 +02:00
|
|
|
with read_file_lines(filename) as lines:
|
|
|
|
for line in lines:
|
2019-05-27 18:29:47 +02:00
|
|
|
m = re.match(self._test_case_line_re, line)
|
2018-09-25 18:49:23 +02:00
|
|
|
if m:
|
|
|
|
self.add_test_case_line(m.group(1), m.group(2))
|
|
|
|
|
|
|
|
def gather_inputs(headers, test_suites):
|
|
|
|
'''Read the list of inputs to test psa_constant_names with.'''
|
|
|
|
inputs = Inputs()
|
|
|
|
for header in headers:
|
|
|
|
inputs.parse_header(header)
|
|
|
|
for test_cases in test_suites:
|
|
|
|
inputs.parse_test_cases(test_cases)
|
|
|
|
inputs.gather_arguments()
|
|
|
|
return inputs
|
|
|
|
|
|
|
|
def remove_file_if_exists(filename):
|
|
|
|
'''Remove the specified file, ignoring errors.'''
|
|
|
|
if not filename:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
os.remove(filename)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2019-05-27 18:29:47 +02:00
|
|
|
def run_c(options, type_word, names):
|
2018-09-25 18:49:23 +02:00
|
|
|
'''Generate and run a program to print out numerical values for names.'''
|
2019-05-27 18:29:47 +02:00
|
|
|
if type_word == 'status':
|
2019-02-13 18:42:53 +01:00
|
|
|
cast_to = 'long'
|
|
|
|
printf_format = '%ld'
|
|
|
|
else:
|
|
|
|
cast_to = 'unsigned long'
|
|
|
|
printf_format = '0x%08lx'
|
2018-09-25 18:49:23 +02:00
|
|
|
c_name = None
|
|
|
|
exe_name = None
|
|
|
|
try:
|
2019-05-27 18:29:47 +02:00
|
|
|
c_fd, c_name = tempfile.mkstemp(prefix='tmp-{}-'.format(type_word),
|
2019-01-04 19:46:59 +01:00
|
|
|
suffix='.c',
|
2018-09-25 18:49:23 +02:00
|
|
|
dir='programs/psa')
|
|
|
|
exe_suffix = '.exe' if platform.system() == 'Windows' else ''
|
|
|
|
exe_name = c_name[:-2] + exe_suffix
|
|
|
|
remove_file_if_exists(exe_name)
|
|
|
|
c_file = os.fdopen(c_fd, 'w', encoding='ascii')
|
2019-01-04 19:46:59 +01:00
|
|
|
c_file.write('/* Generated by test_psa_constant_names.py for {} values */'
|
2019-05-27 18:29:47 +02:00
|
|
|
.format(type_word))
|
2019-01-04 19:46:59 +01:00
|
|
|
c_file.write('''
|
2018-09-25 18:49:23 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <psa/crypto.h>
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
''')
|
|
|
|
for name in names:
|
2019-02-13 18:42:53 +01:00
|
|
|
c_file.write(' printf("{}\\n", ({}) {});\n'
|
|
|
|
.format(printf_format, cast_to, name))
|
2018-09-25 18:49:23 +02:00
|
|
|
c_file.write(''' return 0;
|
|
|
|
}
|
|
|
|
''')
|
|
|
|
c_file.close()
|
|
|
|
cc = os.getenv('CC', 'cc')
|
|
|
|
subprocess.check_call([cc] +
|
|
|
|
['-I' + dir for dir in options.include] +
|
|
|
|
['-o', exe_name, c_name])
|
2018-10-19 11:28:42 +02:00
|
|
|
if options.keep_c:
|
|
|
|
sys.stderr.write('List of {} tests kept at {}\n'
|
2019-05-27 18:29:47 +02:00
|
|
|
.format(type_word, c_name))
|
2018-10-19 11:28:42 +02:00
|
|
|
else:
|
|
|
|
os.remove(c_name)
|
2018-09-25 18:49:23 +02:00
|
|
|
output = subprocess.check_output([exe_name])
|
|
|
|
return output.decode('ascii').strip().split('\n')
|
|
|
|
finally:
|
|
|
|
remove_file_if_exists(exe_name)
|
|
|
|
|
2019-05-27 18:29:47 +02:00
|
|
|
NORMALIZE_STRIP_RE = re.compile(r'\s+')
|
2018-09-25 18:49:23 +02:00
|
|
|
def normalize(expr):
|
|
|
|
'''Normalize the C expression so as not to care about trivial differences.
|
|
|
|
Currently "trivial differences" means whitespace.'''
|
2019-05-27 18:29:47 +02:00
|
|
|
expr = re.sub(NORMALIZE_STRIP_RE, '', expr, len(expr))
|
2018-09-25 18:49:23 +02:00
|
|
|
return expr.strip().split('\n')
|
|
|
|
|
2019-05-27 18:29:47 +02:00
|
|
|
def do_test(options, inputs, type_word, names):
|
2018-09-25 18:49:23 +02:00
|
|
|
'''Test psa_constant_names for the specified type.
|
|
|
|
Run program on names.
|
|
|
|
Use inputs to figure out what arguments to pass to macros that take arguments.'''
|
|
|
|
names = sorted(itertools.chain(*map(inputs.distribute_arguments, names)))
|
2019-05-27 18:29:47 +02:00
|
|
|
values = run_c(options, type_word, names)
|
|
|
|
output = subprocess.check_output([options.program, type_word] + values)
|
2018-09-25 18:49:23 +02:00
|
|
|
outputs = output.decode('ascii').strip().split('\n')
|
2019-05-27 18:29:47 +02:00
|
|
|
errors = [(type_word, name, value, output)
|
2018-09-25 18:49:23 +02:00
|
|
|
for (name, value, output) in zip(names, values, outputs)
|
|
|
|
if normalize(name) != normalize(output)]
|
|
|
|
return len(names), errors
|
|
|
|
|
|
|
|
def report_errors(errors):
|
|
|
|
'''Describe each case where the output is not as expected.'''
|
2019-05-27 18:29:47 +02:00
|
|
|
for type_word, name, value, output in errors:
|
2018-09-25 18:49:23 +02:00
|
|
|
print('For {} "{}", got "{}" (value: {})'
|
2019-05-27 18:29:47 +02:00
|
|
|
.format(type_word, name, output, value))
|
2018-09-25 18:49:23 +02:00
|
|
|
|
|
|
|
def run_tests(options, inputs):
|
|
|
|
'''Run psa_constant_names on all the gathered inputs.
|
|
|
|
Return a tuple (count, errors) where count is the total number of inputs
|
|
|
|
that were tested and errors is the list of cases where the output was
|
|
|
|
not as expected.'''
|
|
|
|
count = 0
|
|
|
|
errors = []
|
2019-05-27 18:29:47 +02:00
|
|
|
for type_word, names in [('status', inputs.statuses),
|
|
|
|
('algorithm', inputs.algorithms),
|
|
|
|
('ecc_curve', inputs.ecc_curves),
|
|
|
|
('dh_group', inputs.dh_groups),
|
|
|
|
('key_type', inputs.key_types),
|
|
|
|
('key_usage', inputs.key_usage_flags)]:
|
|
|
|
c, e = do_test(options, inputs, type_word, names)
|
2018-09-25 18:49:23 +02:00
|
|
|
count += c
|
|
|
|
errors += e
|
|
|
|
return count, errors
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(description=globals()['__doc__'])
|
|
|
|
parser.add_argument('--include', '-I',
|
|
|
|
action='append', default=['include'],
|
|
|
|
help='Directory for header files')
|
|
|
|
parser.add_argument('--program',
|
|
|
|
default='programs/psa/psa_constant_names',
|
|
|
|
help='Program to test')
|
2018-10-19 11:28:42 +02:00
|
|
|
parser.add_argument('--keep-c',
|
|
|
|
action='store_true', dest='keep_c', default=False,
|
|
|
|
help='Keep the intermediate C file')
|
|
|
|
parser.add_argument('--no-keep-c',
|
|
|
|
action='store_false', dest='keep_c',
|
|
|
|
help='Don\'t keep the intermediate C file (default)')
|
2018-09-25 18:49:23 +02:00
|
|
|
options = parser.parse_args()
|
2019-01-04 19:44:59 +01:00
|
|
|
headers = [os.path.join(options.include[0], 'psa', h)
|
|
|
|
for h in ['crypto.h', 'crypto_extra.h', 'crypto_values.h']]
|
2018-09-25 18:49:23 +02:00
|
|
|
test_suites = ['tests/suites/test_suite_psa_crypto_metadata.data']
|
|
|
|
inputs = gather_inputs(headers, test_suites)
|
|
|
|
count, errors = run_tests(options, inputs)
|
|
|
|
report_errors(errors)
|
|
|
|
if errors == []:
|
|
|
|
print('{} test cases PASS'.format(count))
|
|
|
|
else:
|
|
|
|
print('{} test cases, {} FAIL'.format(count, len(errors)))
|
|
|
|
exit(1)
|