2019-05-27 16:24:31 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-05-27 16:25:46 +00:00
|
|
|
|
2020-05-29 09:29:49 +00:00
|
|
|
"""Generate psa_constant_names_generated.c
|
2019-05-27 16:25:46 +00:00
|
|
|
which is included by programs/psa/psa_constant_names.c.
|
|
|
|
The code generated by this module is only meant to be used in the context
|
|
|
|
of that program.
|
2020-05-29 09:29:49 +00:00
|
|
|
|
|
|
|
An argument passed to this script will modify the output directory where the
|
|
|
|
file is written:
|
|
|
|
* by default (no arguments passed): writes to programs/psa/
|
|
|
|
* OUTPUT_FILE_DIR passed: writes to OUTPUT_FILE_DIR/
|
2019-05-27 16:25:46 +00:00
|
|
|
"""
|
|
|
|
|
2020-08-07 11:07:28 +00:00
|
|
|
# Copyright The Mbed TLS Contributors
|
2023-11-02 19:47:20 +00:00
|
|
|
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2020-05-25 22:33:31 +00:00
|
|
|
|
2018-07-16 21:13:37 +00:00
|
|
|
import os
|
2020-05-28 07:42:01 +00:00
|
|
|
import sys
|
2018-07-16 21:13:37 +00:00
|
|
|
|
2024-05-10 15:58:31 +00:00
|
|
|
import framework_scripts_path # pylint: disable=unused-import
|
2024-05-03 13:36:12 +00:00
|
|
|
from mbedtls_framework import build_tree
|
|
|
|
from mbedtls_framework import macro_collector
|
2021-01-25 20:40:45 +00:00
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
OUTPUT_TEMPLATE = '''\
|
2018-07-16 21:13:37 +00:00
|
|
|
/* Automatically generated by generate_psa_constant.py. DO NOT EDIT. */
|
|
|
|
|
|
|
|
static const char *psa_strerror(psa_status_t status)
|
|
|
|
{
|
|
|
|
switch (status) {
|
|
|
|
%(status_cases)s
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 16:19:28 +00:00
|
|
|
static const char *psa_ecc_family_name(psa_ecc_family_t curve)
|
2018-07-16 21:13:37 +00:00
|
|
|
{
|
|
|
|
switch (curve) {
|
|
|
|
%(ecc_curve_cases)s
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 14:17:39 +00:00
|
|
|
static const char *psa_dh_family_name(psa_dh_family_t group)
|
2019-05-16 10:55:35 +00:00
|
|
|
{
|
|
|
|
switch (group) {
|
|
|
|
%(dh_group_cases)s
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:13:37 +00:00
|
|
|
static const char *psa_hash_algorithm_name(psa_algorithm_t hash_alg)
|
|
|
|
{
|
|
|
|
switch (hash_alg) {
|
|
|
|
%(hash_algorithm_cases)s
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 22:12:07 +00:00
|
|
|
static const char *psa_ka_algorithm_name(psa_algorithm_t ka_alg)
|
|
|
|
{
|
|
|
|
switch (ka_alg) {
|
|
|
|
%(ka_algorithm_cases)s
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:13:37 +00:00
|
|
|
static int psa_snprint_key_type(char *buffer, size_t buffer_size,
|
|
|
|
psa_key_type_t type)
|
|
|
|
{
|
|
|
|
size_t required_size = 0;
|
|
|
|
switch (type) {
|
|
|
|
%(key_type_cases)s
|
|
|
|
default:
|
|
|
|
%(key_type_code)s{
|
|
|
|
return snprintf(buffer, buffer_size,
|
2019-12-04 16:18:41 +00:00
|
|
|
"0x%%04x", (unsigned) type);
|
2018-07-16 21:13:37 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buffer[0] = 0;
|
2018-10-17 14:01:45 +00:00
|
|
|
return (int) required_size;
|
2018-07-16 21:13:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 22:12:07 +00:00
|
|
|
#define NO_LENGTH_MODIFIER 0xfffffffflu
|
2018-07-16 21:13:37 +00:00
|
|
|
static int psa_snprint_algorithm(char *buffer, size_t buffer_size,
|
|
|
|
psa_algorithm_t alg)
|
|
|
|
{
|
|
|
|
size_t required_size = 0;
|
2018-08-20 13:07:20 +00:00
|
|
|
psa_algorithm_t core_alg = alg;
|
2019-04-11 22:12:07 +00:00
|
|
|
unsigned long length_modifier = NO_LENGTH_MODIFIER;
|
2018-08-20 13:07:20 +00:00
|
|
|
if (PSA_ALG_IS_MAC(alg)) {
|
|
|
|
core_alg = PSA_ALG_TRUNCATED_MAC(alg, 0);
|
2021-02-22 18:59:35 +00:00
|
|
|
if (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) {
|
2018-08-20 13:07:20 +00:00
|
|
|
append(&buffer, buffer_size, &required_size,
|
2021-02-22 17:53:07 +00:00
|
|
|
"PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(", 33);
|
2018-08-20 13:07:20 +00:00
|
|
|
length_modifier = PSA_MAC_TRUNCATED_LENGTH(alg);
|
2021-02-25 09:33:30 +00:00
|
|
|
} else if (core_alg != alg) {
|
|
|
|
append(&buffer, buffer_size, &required_size,
|
|
|
|
"PSA_ALG_TRUNCATED_MAC(", 22);
|
|
|
|
length_modifier = PSA_MAC_TRUNCATED_LENGTH(alg);
|
2018-08-20 13:07:20 +00:00
|
|
|
}
|
|
|
|
} else if (PSA_ALG_IS_AEAD(alg)) {
|
2020-12-16 10:36:46 +00:00
|
|
|
core_alg = PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
|
2021-02-25 09:33:30 +00:00
|
|
|
if (core_alg == 0) {
|
|
|
|
/* For unknown AEAD algorithms, there is no "default tag length". */
|
|
|
|
core_alg = alg;
|
|
|
|
} else if (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) {
|
|
|
|
append(&buffer, buffer_size, &required_size,
|
|
|
|
"PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(", 43);
|
2021-03-19 17:46:15 +00:00
|
|
|
length_modifier = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
|
2021-02-25 09:33:30 +00:00
|
|
|
} else if (core_alg != alg) {
|
|
|
|
append(&buffer, buffer_size, &required_size,
|
|
|
|
"PSA_ALG_AEAD_WITH_SHORTENED_TAG(", 32);
|
2021-03-19 17:46:15 +00:00
|
|
|
length_modifier = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
|
2018-08-20 13:07:20 +00:00
|
|
|
}
|
2019-04-11 22:12:07 +00:00
|
|
|
} else if (PSA_ALG_IS_KEY_AGREEMENT(alg) &&
|
|
|
|
!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
|
|
|
|
core_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
|
|
|
|
append(&buffer, buffer_size, &required_size,
|
|
|
|
"PSA_ALG_KEY_AGREEMENT(", 22);
|
|
|
|
append_with_alg(&buffer, buffer_size, &required_size,
|
|
|
|
psa_ka_algorithm_name,
|
|
|
|
PSA_ALG_KEY_AGREEMENT_GET_BASE(alg));
|
|
|
|
append(&buffer, buffer_size, &required_size, ", ", 2);
|
2018-08-20 13:07:20 +00:00
|
|
|
}
|
|
|
|
switch (core_alg) {
|
2018-07-16 21:13:37 +00:00
|
|
|
%(algorithm_cases)s
|
|
|
|
default:
|
|
|
|
%(algorithm_code)s{
|
2018-08-20 13:06:39 +00:00
|
|
|
append_integer(&buffer, buffer_size, &required_size,
|
2018-10-19 09:33:51 +00:00
|
|
|
"0x%%08lx", (unsigned long) core_alg);
|
2018-07-16 21:13:37 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-08-20 13:07:20 +00:00
|
|
|
if (core_alg != alg) {
|
2019-04-11 22:12:07 +00:00
|
|
|
if (length_modifier != NO_LENGTH_MODIFIER) {
|
|
|
|
append(&buffer, buffer_size, &required_size, ", ", 2);
|
|
|
|
append_integer(&buffer, buffer_size, &required_size,
|
|
|
|
"%%lu", length_modifier);
|
|
|
|
}
|
2018-08-20 13:07:20 +00:00
|
|
|
append(&buffer, buffer_size, &required_size, ")", 1);
|
|
|
|
}
|
2018-07-16 21:13:37 +00:00
|
|
|
buffer[0] = 0;
|
2018-10-17 14:01:45 +00:00
|
|
|
return (int) required_size;
|
2018-07-16 21:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int psa_snprint_key_usage(char *buffer, size_t buffer_size,
|
|
|
|
psa_key_usage_t usage)
|
|
|
|
{
|
|
|
|
size_t required_size = 0;
|
|
|
|
if (usage == 0) {
|
|
|
|
if (buffer_size > 1) {
|
|
|
|
buffer[0] = '0';
|
|
|
|
buffer[1] = 0;
|
|
|
|
} else if (buffer_size == 1) {
|
|
|
|
buffer[0] = 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
%(key_usage_code)s
|
|
|
|
if (usage != 0) {
|
|
|
|
if (required_size != 0) {
|
|
|
|
append(&buffer, buffer_size, &required_size, " | ", 3);
|
|
|
|
}
|
2018-08-20 13:06:39 +00:00
|
|
|
append_integer(&buffer, buffer_size, &required_size,
|
|
|
|
"0x%%08lx", (unsigned long) usage);
|
2018-07-16 21:13:37 +00:00
|
|
|
} else {
|
|
|
|
buffer[0] = 0;
|
|
|
|
}
|
2018-10-17 14:01:45 +00:00
|
|
|
return (int) required_size;
|
2018-07-16 21:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End of automatically generated file. */
|
|
|
|
'''
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
KEY_TYPE_FROM_CURVE_TEMPLATE = '''if (%(tester)s(type)) {
|
2018-08-02 13:08:07 +00:00
|
|
|
append_with_curve(&buffer, buffer_size, &required_size,
|
|
|
|
"%(builder)s", %(builder_length)s,
|
2020-06-02 16:19:28 +00:00
|
|
|
PSA_KEY_TYPE_ECC_GET_FAMILY(type));
|
2018-08-02 13:08:07 +00:00
|
|
|
} else '''
|
2018-07-16 21:13:37 +00:00
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
KEY_TYPE_FROM_GROUP_TEMPLATE = '''if (%(tester)s(type)) {
|
2019-05-16 10:55:35 +00:00
|
|
|
append_with_group(&buffer, buffer_size, &required_size,
|
|
|
|
"%(builder)s", %(builder_length)s,
|
2020-06-03 14:17:39 +00:00
|
|
|
PSA_KEY_TYPE_DH_GET_FAMILY(type));
|
2019-05-16 10:55:35 +00:00
|
|
|
} else '''
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
ALGORITHM_FROM_HASH_TEMPLATE = '''if (%(tester)s(core_alg)) {
|
2019-04-11 22:12:07 +00:00
|
|
|
append(&buffer, buffer_size, &required_size,
|
|
|
|
"%(builder)s(", %(builder_length)s + 1);
|
|
|
|
append_with_alg(&buffer, buffer_size, &required_size,
|
|
|
|
psa_hash_algorithm_name,
|
|
|
|
PSA_ALG_GET_HASH(core_alg));
|
|
|
|
append(&buffer, buffer_size, &required_size, ")", 1);
|
2018-08-02 13:08:07 +00:00
|
|
|
} else '''
|
2018-07-16 21:13:37 +00:00
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
BIT_TEST_TEMPLATE = '''\
|
2018-07-16 21:13:37 +00:00
|
|
|
if (%(var)s & %(flag)s) {
|
|
|
|
if (required_size != 0) {
|
|
|
|
append(&buffer, buffer_size, &required_size, " | ", 3);
|
|
|
|
}
|
|
|
|
append(&buffer, buffer_size, &required_size, "%(flag)s", %(length)d);
|
|
|
|
%(var)s ^= %(flag)s;
|
|
|
|
}\
|
|
|
|
'''
|
|
|
|
|
2021-01-25 20:40:45 +00:00
|
|
|
class CaseBuilder(macro_collector.PSAMacroCollector):
|
2021-01-25 20:36:53 +00:00
|
|
|
"""Collect PSA crypto macro definitions and write value recognition functions.
|
|
|
|
|
|
|
|
1. Call `read_file` on the input header file(s).
|
|
|
|
2. Call `write_file` to write ``psa_constant_names_generated.c``.
|
|
|
|
"""
|
|
|
|
|
2021-01-25 21:42:14 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__(include_intermediate=True)
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def _make_return_case(name):
|
2018-07-16 21:13:37 +00:00
|
|
|
return 'case %(name)s: return "%(name)s";' % {'name': name}
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def _make_append_case(name):
|
2018-07-16 21:13:37 +00:00
|
|
|
template = ('case %(name)s: '
|
|
|
|
'append(&buffer, buffer_size, &required_size, "%(name)s", %(length)d); '
|
|
|
|
'break;')
|
|
|
|
return template % {'name': name, 'length': len(name)}
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def _make_bit_test(var, flag):
|
|
|
|
return BIT_TEST_TEMPLATE % {'var': var,
|
2018-07-16 21:13:37 +00:00
|
|
|
'flag': flag,
|
|
|
|
'length': len(flag)}
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
def _make_status_cases(self):
|
|
|
|
return '\n '.join(map(self._make_return_case,
|
2018-07-16 21:13:37 +00:00
|
|
|
sorted(self.statuses)))
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
def _make_ecc_curve_cases(self):
|
|
|
|
return '\n '.join(map(self._make_return_case,
|
2018-07-16 21:13:37 +00:00
|
|
|
sorted(self.ecc_curves)))
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
def _make_dh_group_cases(self):
|
|
|
|
return '\n '.join(map(self._make_return_case,
|
2019-05-16 10:55:35 +00:00
|
|
|
sorted(self.dh_groups)))
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
def _make_key_type_cases(self):
|
|
|
|
return '\n '.join(map(self._make_append_case,
|
2018-07-16 21:13:37 +00:00
|
|
|
sorted(self.key_types)))
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def _make_key_type_from_curve_code(builder, tester):
|
|
|
|
return KEY_TYPE_FROM_CURVE_TEMPLATE % {'builder': builder,
|
2018-07-16 21:13:37 +00:00
|
|
|
'builder_length': len(builder),
|
|
|
|
'tester': tester}
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def _make_key_type_from_group_code(builder, tester):
|
|
|
|
return KEY_TYPE_FROM_GROUP_TEMPLATE % {'builder': builder,
|
2019-05-16 10:55:35 +00:00
|
|
|
'builder_length': len(builder),
|
|
|
|
'tester': tester}
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
def _make_ecc_key_type_code(self):
|
2018-07-16 21:13:37 +00:00
|
|
|
d = self.key_types_from_curve
|
2019-05-27 16:29:47 +00:00
|
|
|
make = self._make_key_type_from_curve_code
|
2018-08-02 13:08:07 +00:00
|
|
|
return ''.join([make(k, d[k]) for k in sorted(d.keys())])
|
2018-07-16 21:13:37 +00:00
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
def _make_dh_key_type_code(self):
|
2019-05-16 10:55:35 +00:00
|
|
|
d = self.key_types_from_group
|
2019-05-27 16:29:47 +00:00
|
|
|
make = self._make_key_type_from_group_code
|
2019-05-16 10:55:35 +00:00
|
|
|
return ''.join([make(k, d[k]) for k in sorted(d.keys())])
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
def _make_hash_algorithm_cases(self):
|
|
|
|
return '\n '.join(map(self._make_return_case,
|
2018-07-16 21:13:37 +00:00
|
|
|
sorted(self.hash_algorithms)))
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
def _make_ka_algorithm_cases(self):
|
|
|
|
return '\n '.join(map(self._make_return_case,
|
2019-04-11 22:12:07 +00:00
|
|
|
sorted(self.ka_algorithms)))
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
def _make_algorithm_cases(self):
|
|
|
|
return '\n '.join(map(self._make_append_case,
|
2018-07-16 21:13:37 +00:00
|
|
|
sorted(self.algorithms)))
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def _make_algorithm_from_hash_code(builder, tester):
|
|
|
|
return ALGORITHM_FROM_HASH_TEMPLATE % {'builder': builder,
|
2018-07-16 21:13:37 +00:00
|
|
|
'builder_length': len(builder),
|
|
|
|
'tester': tester}
|
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
def _make_algorithm_code(self):
|
2018-07-16 21:13:37 +00:00
|
|
|
d = self.algorithms_from_hash
|
2019-05-27 16:29:47 +00:00
|
|
|
make = self._make_algorithm_from_hash_code
|
2018-08-02 13:08:07 +00:00
|
|
|
return ''.join([make(k, d[k]) for k in sorted(d.keys())])
|
2018-07-16 21:13:37 +00:00
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
def _make_key_usage_code(self):
|
|
|
|
return '\n'.join([self._make_bit_test('usage', bit)
|
2021-03-10 00:25:50 +00:00
|
|
|
for bit in sorted(self.key_usage_flags)])
|
2018-07-16 21:13:37 +00:00
|
|
|
|
|
|
|
def write_file(self, output_file):
|
2019-05-27 16:29:47 +00:00
|
|
|
"""Generate the pretty-printer function code from the gathered
|
2019-06-03 09:23:56 +00:00
|
|
|
constant definitions.
|
|
|
|
"""
|
2018-07-16 21:13:37 +00:00
|
|
|
data = {}
|
2019-05-27 16:29:47 +00:00
|
|
|
data['status_cases'] = self._make_status_cases()
|
|
|
|
data['ecc_curve_cases'] = self._make_ecc_curve_cases()
|
|
|
|
data['dh_group_cases'] = self._make_dh_group_cases()
|
|
|
|
data['key_type_cases'] = self._make_key_type_cases()
|
|
|
|
data['key_type_code'] = (self._make_ecc_key_type_code() +
|
|
|
|
self._make_dh_key_type_code())
|
|
|
|
data['hash_algorithm_cases'] = self._make_hash_algorithm_cases()
|
|
|
|
data['ka_algorithm_cases'] = self._make_ka_algorithm_cases()
|
|
|
|
data['algorithm_cases'] = self._make_algorithm_cases()
|
|
|
|
data['algorithm_code'] = self._make_algorithm_code()
|
|
|
|
data['key_usage_code'] = self._make_key_usage_code()
|
|
|
|
output_file.write(OUTPUT_TEMPLATE % data)
|
2018-07-16 21:13:37 +00:00
|
|
|
|
2019-01-04 18:44:59 +00:00
|
|
|
def generate_psa_constants(header_file_names, output_file_name):
|
2021-01-25 20:36:53 +00:00
|
|
|
collector = CaseBuilder()
|
2019-01-04 18:44:59 +00:00
|
|
|
for header_file_name in header_file_names:
|
2019-12-06 18:20:13 +00:00
|
|
|
with open(header_file_name, 'rb') as header_file:
|
2019-01-04 18:44:59 +00:00
|
|
|
collector.read_file(header_file)
|
2018-07-16 21:13:37 +00:00
|
|
|
temp_file_name = output_file_name + '.tmp'
|
|
|
|
with open(temp_file_name, 'w') as output_file:
|
|
|
|
collector.write_file(output_file)
|
2020-09-22 17:37:26 +00:00
|
|
|
os.replace(temp_file_name, output_file_name)
|
2018-07-16 21:13:37 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-04-21 22:20:47 +00:00
|
|
|
build_tree.chdir_to_root()
|
2020-05-28 07:42:01 +00:00
|
|
|
# Allow to change the directory where psa_constant_names_generated.c is written to.
|
|
|
|
OUTPUT_FILE_DIR = sys.argv[1] if len(sys.argv) == 2 else "programs/psa"
|
2024-06-10 07:41:49 +00:00
|
|
|
|
|
|
|
generate_psa_constants(['tf-psa-crypto/include/psa/crypto_values.h',
|
|
|
|
'tf-psa-crypto/include/psa/crypto_extra.h'],
|
2020-05-28 07:42:01 +00:00
|
|
|
OUTPUT_FILE_DIR + '/psa_constant_names_generated.c')
|