Merge branch 'Mbed-TLS:development' into codegen_1.1
This commit is contained in:
commit
b549776a23
389 changed files with 50509 additions and 19601 deletions
|
@ -1,9 +1,10 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
This script compares the interfaces of two versions of Mbed TLS, looking
|
||||
"""This script compares the interfaces of two versions of Mbed TLS, looking
|
||||
for backward incompatibilities between two different Git revisions within
|
||||
an Mbed TLS repository. It must be run from the root of a Git working tree.
|
||||
|
||||
### How the script works ###
|
||||
|
||||
For the source (API) and runtime (ABI) interface compatibility, this script
|
||||
is a small wrapper around the abi-compliance-checker and abi-dumper tools,
|
||||
applying them to compare the header and library files.
|
||||
|
@ -20,7 +21,66 @@ at a configurable location, or are given as a brief list of problems.
|
|||
Returns 0 on success, 1 on non-compliance, and 2 if there is an error
|
||||
while running the script.
|
||||
|
||||
You must run this test from an Mbed TLS root.
|
||||
### How to interpret non-compliance ###
|
||||
|
||||
This script has relatively common false positives. In many scenarios, it only
|
||||
reports a pass if there is a strict textual match between the old version and
|
||||
the new version, and it reports problems where there is a sufficient semantic
|
||||
match but not a textual match. This section lists some common false positives.
|
||||
This is not an exhaustive list: in the end what matters is whether we are
|
||||
breaking a backward compatibility goal.
|
||||
|
||||
**API**: the goal is that if an application works with the old version of the
|
||||
library, it can be recompiled against the new version and will still work.
|
||||
This is normally validated by comparing the declarations in `include/*/*.h`.
|
||||
A failure is a declaration that has disappeared or that now has a different
|
||||
type.
|
||||
|
||||
* It's ok to change or remove macros and functions that are documented as
|
||||
for internal use only or as experimental.
|
||||
* It's ok to rename function or macro parameters as long as the semantics
|
||||
has not changed.
|
||||
* It's ok to change or remove structure fields that are documented as
|
||||
private.
|
||||
* It's ok to add fields to a structure that already had private fields
|
||||
or was documented as extensible.
|
||||
|
||||
**ABI**: the goal is that if an application was built against the old version
|
||||
of the library, the same binary will work when linked against the new version.
|
||||
This is normally validated by comparing the symbols exported by `libmbed*.so`.
|
||||
A failure is a symbol that is no longer exported by the same library or that
|
||||
now has a different type.
|
||||
|
||||
* All ABI changes are acceptable if the library version is bumped
|
||||
(see `scripts/bump_version.sh`).
|
||||
* ABI changes that concern functions which are declared only inside the
|
||||
library directory, and not in `include/*/*.h`, are acceptable only if
|
||||
the function was only ever used inside the same library (libmbedcrypto,
|
||||
libmbedx509, libmbedtls). As a counter example, if the old version
|
||||
of libmbedtls calls mbedtls_foo() from libmbedcrypto, and the new version
|
||||
of libmbedcrypto no longer has a compatible mbedtls_foo(), this does
|
||||
require a version bump for libmbedcrypto.
|
||||
|
||||
**Storage format**: the goal is to check that persistent keys stored by the
|
||||
old version can be read by the new version. This is normally validated by
|
||||
comparing the `*read*` test cases in `test_suite*storage_format*.data`.
|
||||
A failure is a storage read test case that is no longer present with the same
|
||||
function name and parameter list.
|
||||
|
||||
* It's ok if the same test data is present, but its presentation has changed,
|
||||
for example if a test function is renamed or has different parameters.
|
||||
* It's ok if redundant tests are removed.
|
||||
|
||||
**Generated test coverage**: the goal is to check that automatically
|
||||
generated tests have as much coverage as before. This is normally validated
|
||||
by comparing the test cases that are automatically generated by a script.
|
||||
A failure is a generated test case that is no longer present with the same
|
||||
function name and parameter list.
|
||||
|
||||
* It's ok if the same test data is present, but its presentation has changed,
|
||||
for example if a test function is renamed or has different parameters.
|
||||
* It's ok if redundant tests are removed.
|
||||
|
||||
"""
|
||||
|
||||
# Copyright The Mbed TLS Contributors
|
||||
|
|
|
@ -122,7 +122,7 @@ class ChangelogFormat:
|
|||
class TextChangelogFormat(ChangelogFormat):
|
||||
"""The traditional Mbed TLS changelog format."""
|
||||
|
||||
_unreleased_version_text = '= mbed TLS x.x.x branch released xxxx-xx-xx'
|
||||
_unreleased_version_text = '= Mbed TLS x.x.x branch released xxxx-xx-xx'
|
||||
@classmethod
|
||||
def is_released_version(cls, title):
|
||||
# Look for an incomplete release date
|
||||
|
|
|
@ -96,7 +96,7 @@ then
|
|||
mv tmp library/CMakeLists.txt
|
||||
|
||||
[ $VERBOSE ] && echo "Bumping SOVERSION for libmbedcrypto in library/Makefile"
|
||||
sed -e "s/SOEXT_CRYPTO=so.[0-9]\{1,\}/SOEXT_CRYPTO=so.$SO_CRYPTO/g" < library/Makefile > tmp
|
||||
sed -e "s/SOEXT_CRYPTO?=so.[0-9]\{1,\}/SOEXT_CRYPTO?=so.$SO_CRYPTO/g" < library/Makefile > tmp
|
||||
mv tmp library/Makefile
|
||||
fi
|
||||
|
||||
|
@ -107,7 +107,7 @@ then
|
|||
mv tmp library/CMakeLists.txt
|
||||
|
||||
[ $VERBOSE ] && echo "Bumping SOVERSION for libmbedx509 in library/Makefile"
|
||||
sed -e "s/SOEXT_X509=so.[0-9]\{1,\}/SOEXT_X509=so.$SO_X509/g" < library/Makefile > tmp
|
||||
sed -e "s/SOEXT_X509?=so.[0-9]\{1,\}/SOEXT_X509?=so.$SO_X509/g" < library/Makefile > tmp
|
||||
mv tmp library/Makefile
|
||||
fi
|
||||
|
||||
|
@ -118,7 +118,7 @@ then
|
|||
mv tmp library/CMakeLists.txt
|
||||
|
||||
[ $VERBOSE ] && echo "Bumping SOVERSION for libmbedtls in library/Makefile"
|
||||
sed -e "s/SOEXT_TLS=so.[0-9]\{1,\}/SOEXT_TLS=so.$SO_TLS/g" < library/Makefile > tmp
|
||||
sed -e "s/SOEXT_TLS?=so.[0-9]\{1,\}/SOEXT_TLS?=so.$SO_TLS/g" < library/Makefile > tmp
|
||||
mv tmp library/Makefile
|
||||
fi
|
||||
|
||||
|
|
|
@ -8,5 +8,5 @@
|
|||
pylint == 2.4.4
|
||||
|
||||
# Use the earliest version of mypy that works with our code base.
|
||||
# See https://github.com/ARMmbed/mbedtls/pull/3953 .
|
||||
# See https://github.com/Mbed-TLS/mbedtls/pull/3953 .
|
||||
mypy >= 0.780
|
||||
|
|
|
@ -37,7 +37,7 @@ class CodeSizeComparison:
|
|||
"""
|
||||
old_revision: revision to compare against
|
||||
new_revision:
|
||||
result_dir: directory for comparision result
|
||||
result_dir: directory for comparison result
|
||||
"""
|
||||
self.repo_path = "."
|
||||
self.result_dir = os.path.abspath(result_dir)
|
||||
|
@ -140,7 +140,7 @@ class CodeSizeComparison:
|
|||
+ "-" + self.new_rev + ".csv"), "w")
|
||||
|
||||
res_file.write("file_name, this_size, old_size, change, change %\n")
|
||||
print("Generating comparision results.")
|
||||
print("Generating comparison results.")
|
||||
|
||||
old_ds = {}
|
||||
for line in old_file.readlines()[1:]:
|
||||
|
@ -199,7 +199,7 @@ def main():
|
|||
parser.add_argument(
|
||||
"-n", "--new-rev", type=str, default=None,
|
||||
help="new revision for comparison, default is the current work \
|
||||
directory, including uncommited changes."
|
||||
directory, including uncommitted changes."
|
||||
)
|
||||
comp_args = parser.parse_args()
|
||||
|
||||
|
|
|
@ -161,8 +161,16 @@ def is_full_section(section):
|
|||
return section.endswith('support') or section.endswith('modules')
|
||||
|
||||
def realfull_adapter(_name, active, section):
|
||||
"""Activate all symbols found in the system and feature sections."""
|
||||
if not is_full_section(section):
|
||||
"""Activate all symbols found in the global and boolean feature sections.
|
||||
|
||||
This is intended for building the documentation, including the
|
||||
documentation of settings that are activated by defining an optional
|
||||
preprocessor macro.
|
||||
|
||||
Do not activate definitions in the section containing symbols that are
|
||||
supposed to be defined and documented in their own module.
|
||||
"""
|
||||
if section == 'Module configuration options':
|
||||
return active
|
||||
return True
|
||||
|
||||
|
@ -198,6 +206,8 @@ EXCLUDE_FROM_FULL = frozenset([
|
|||
'MBEDTLS_PSA_CRYPTO_SPM', # platform dependency (PSA SPM)
|
||||
'MBEDTLS_PSA_INJECT_ENTROPY', # build dependency (hook functions)
|
||||
'MBEDTLS_RSA_NO_CRT', # influences the use of RSA in X.509 and TLS
|
||||
'MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT
|
||||
'MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT
|
||||
'MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN', # build dependency (clang+memsan)
|
||||
'MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND', # build dependency (valgrind headers)
|
||||
'MBEDTLS_X509_REMOVE_INFO', # removes a feature
|
||||
|
@ -272,6 +282,21 @@ def baremetal_adapter(name, active, section):
|
|||
return True
|
||||
return include_in_full(name) and keep_in_baremetal(name)
|
||||
|
||||
# This set contains options that are mostly for debugging or test purposes,
|
||||
# and therefore should be excluded when doing code size measurements.
|
||||
# Options that are their own module (such as MBEDTLS_ERROR_C) are not listed
|
||||
# and therefore will be included when doing code size measurements.
|
||||
EXCLUDE_FOR_SIZE = frozenset([
|
||||
'MBEDTLS_DEBUG_C', # large code size increase in TLS
|
||||
'MBEDTLS_SELF_TEST', # increases the size of many modules
|
||||
'MBEDTLS_TEST_HOOKS', # only useful with the hosted test framework, increases code size
|
||||
])
|
||||
|
||||
def baremetal_size_adapter(name, active, section):
|
||||
if name in EXCLUDE_FOR_SIZE:
|
||||
return False
|
||||
return baremetal_adapter(name, active, section)
|
||||
|
||||
def include_in_crypto(name):
|
||||
"""Rules for symbols in a crypto configuration."""
|
||||
if name.startswith('MBEDTLS_X509_') or \
|
||||
|
@ -299,6 +324,9 @@ def crypto_adapter(adapter):
|
|||
return adapter(name, active, section)
|
||||
return continuation
|
||||
|
||||
DEPRECATED = frozenset([
|
||||
'MBEDTLS_PSA_CRYPTO_SE_C',
|
||||
])
|
||||
def no_deprecated_adapter(adapter):
|
||||
"""Modify an adapter to disable deprecated symbols.
|
||||
|
||||
|
@ -309,6 +337,8 @@ def no_deprecated_adapter(adapter):
|
|||
def continuation(name, active, section):
|
||||
if name == 'MBEDTLS_DEPRECATED_REMOVED':
|
||||
return True
|
||||
if name in DEPRECATED:
|
||||
return False
|
||||
if adapter is None:
|
||||
return active
|
||||
return adapter(name, active, section)
|
||||
|
@ -393,7 +423,7 @@ class ConfigFile(Config):
|
|||
value = setting.value
|
||||
if value is None:
|
||||
value = ''
|
||||
# Normally the whitespace to separte the symbol name from the
|
||||
# Normally the whitespace to separate the symbol name from the
|
||||
# value is part of middle, and there's no whitespace for a symbol
|
||||
# with no value. But if a symbol has been changed from having a
|
||||
# value to not having one, the whitespace is wrong, so fix it.
|
||||
|
@ -484,6 +514,9 @@ if __name__ == '__main__':
|
|||
add_adapter('baremetal', baremetal_adapter,
|
||||
"""Like full, but exclude features that require platform
|
||||
features such as file input-output.""")
|
||||
add_adapter('baremetal_size', baremetal_size_adapter,
|
||||
"""Like baremetal, but exclude debugging features.
|
||||
Useful for code size measurements.""")
|
||||
add_adapter('full', full_adapter,
|
||||
"""Uncomment most features.
|
||||
Exclude alternative implementations and platform support
|
||||
|
|
|
@ -150,7 +150,7 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
|
|||
#else /* MBEDTLS_ERROR_C */
|
||||
|
||||
/*
|
||||
* Provide an non-function in case MBEDTLS_ERROR_C is not defined
|
||||
* Provide a dummy implementation when MBEDTLS_ERROR_C is not defined
|
||||
*/
|
||||
void mbedtls_strerror( int ret, char *buf, size_t buflen )
|
||||
{
|
||||
|
|
|
@ -30,8 +30,12 @@
|
|||
|
||||
/*
|
||||
* Include all the headers with public APIs in case they define a macro to its
|
||||
* default value when that configuration is not set in the mbedtls_config.h.
|
||||
* default value when that configuration is not set in mbedtls_config.h, or
|
||||
* for PSA_WANT macros, in case they're auto-defined based on mbedtls_config.h
|
||||
* rather than defined directly in crypto_config.h.
|
||||
*/
|
||||
#include "psa/crypto.h"
|
||||
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/aria.h"
|
||||
#include "mbedtls/asn1.h"
|
||||
|
@ -67,7 +71,9 @@
|
|||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/pkcs12.h"
|
||||
#include "mbedtls/pkcs5.h"
|
||||
#if defined(MBEDTLS_HAVE_TIME)
|
||||
#include "mbedtls/platform_time.h"
|
||||
#endif
|
||||
#include "mbedtls/platform_util.h"
|
||||
#include "mbedtls/poly1305.h"
|
||||
#include "mbedtls/ripemd160.h"
|
||||
|
|
|
@ -7,10 +7,12 @@
|
|||
markupsafe < 2.1
|
||||
|
||||
# Use the version of Jinja that's in Ubuntu 20.04.
|
||||
# See https://github.com/ARMmbed/mbedtls/pull/5067#discussion_r738794607 .
|
||||
# See https://github.com/Mbed-TLS/mbedtls/pull/5067#discussion_r738794607 .
|
||||
# Note that Jinja 3.0 drops support for Python 3.5, so we need to support
|
||||
# Jinja 2.x as long as we're still using Python 3.5 anywhere.
|
||||
Jinja2 >= 2.10.1
|
||||
# Jinja 2.10.1 doesn't support Python 3.10+
|
||||
Jinja2 >= 2.10.1; python_version < '3.10'
|
||||
Jinja2 >= 2.10.3; python_version >= '3.10'
|
||||
# Jinja2 >=2.10, <3.0 needs a separate package for type annotations
|
||||
types-Jinja2
|
||||
jsonschema >= 3.2.0
|
||||
|
|
|
@ -57,27 +57,39 @@ cat << EOF >$CONFIG_H
|
|||
#define MBEDTLS_ASN1_PARSE_C
|
||||
#define MBEDTLS_ASN1_WRITE_C
|
||||
#define MBEDTLS_ECDSA_C
|
||||
#define MBEDTLS_SHA256_C // ECDSA benchmark needs it
|
||||
#define MBEDTLS_SHA224_C // SHA256 requires this for now
|
||||
#define MBEDTLS_ECDH_C
|
||||
|
||||
#define MBEDTLS_ECP_DP_SECP192R1_ENABLED
|
||||
#define MBEDTLS_ECP_DP_SECP224R1_ENABLED
|
||||
// NIST curves >= 256 bits
|
||||
#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
|
||||
#define MBEDTLS_ECP_DP_SECP384R1_ENABLED
|
||||
#define MBEDTLS_ECP_DP_SECP521R1_ENABLED
|
||||
// SECP "koblitz-like" curve >= 256 bits
|
||||
#define MBEDTLS_ECP_DP_SECP256K1_ENABLED
|
||||
// Brainpool curves (no specialised "mod p" routine)
|
||||
#define MBEDTLS_ECP_DP_BP256R1_ENABLED
|
||||
#define MBEDTLS_ECP_DP_BP384R1_ENABLED
|
||||
#define MBEDTLS_ECP_DP_BP512R1_ENABLED
|
||||
// Montgomery curves
|
||||
#define MBEDTLS_ECP_DP_CURVE25519_ENABLED
|
||||
#define MBEDTLS_ECP_DP_CURVE448_ENABLED
|
||||
|
||||
//#define MBEDTLS_ECP_WINDOW_SIZE 6
|
||||
#define MBEDTLS_HAVE_ASM // just make things a bit faster
|
||||
#define MBEDTLS_ECP_NIST_OPTIM // faster and less allocations
|
||||
|
||||
//#define MBEDTLS_ECP_WINDOW_SIZE 4
|
||||
//#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1
|
||||
EOF
|
||||
|
||||
for F in 0 1; do
|
||||
for W in 2 3 4 5 6; do
|
||||
for W in 2 3 4; do
|
||||
scripts/config.py set MBEDTLS_ECP_WINDOW_SIZE $W
|
||||
scripts/config.py set MBEDTLS_ECP_FIXED_POINT_OPTIM $F
|
||||
make benchmark >/dev/null 2>&1
|
||||
echo "fixed point optim = $F, max window size = $W"
|
||||
echo "--------------------------------------------"
|
||||
programs/test/benchmark
|
||||
programs/test/benchmark ecdh ecdsa
|
||||
done
|
||||
done
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Generate library/psa_crypto_driver_wrappers.c
|
||||
|
||||
This module is invoked by the build sripts to auto generate the
|
||||
This module is invoked by the build scripts to auto generate the
|
||||
psa_crypto_driver_wrappers.c based on template files in
|
||||
script/data_files/driver_templates/.
|
||||
"""
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
# function by using the template in scripts/data_files/query_config.fmt.
|
||||
#
|
||||
# Usage: scripts/generate_query_config.pl without arguments, or
|
||||
# generate_query_config.pl config_file template_file output_file
|
||||
# generate_query_config.pl mbedtls_config_file template_file output_file [psa_crypto_config_file]
|
||||
#
|
||||
# Copyright The Mbed TLS Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
@ -34,22 +34,33 @@
|
|||
|
||||
use strict;
|
||||
|
||||
my ($config_file, $query_config_format_file, $query_config_file);
|
||||
my ($mbedtls_config_file, $query_config_format_file, $query_config_file, $psa_crypto_config_file);
|
||||
|
||||
my $default_mbedtls_config_file = "./include/mbedtls/mbedtls_config.h";
|
||||
my $default_query_config_format_file = "./scripts/data_files/query_config.fmt";
|
||||
my $default_query_config_file = "./programs/test/query_config.c";
|
||||
my $default_psa_crypto_config_file = "./include/psa/crypto_config.h";
|
||||
|
||||
if( @ARGV ) {
|
||||
die "Invalid number of arguments - usage: $0 [CONFIG_FILE TEMPLATE_FILE OUTPUT_FILE]" if scalar @ARGV != 3;
|
||||
($config_file, $query_config_format_file, $query_config_file) = @ARGV;
|
||||
($mbedtls_config_file, $query_config_format_file, $query_config_file) = @ARGV;
|
||||
|
||||
-f $config_file or die "No such file: $config_file";
|
||||
-f $mbedtls_config_file or die "No such file: $mbedtls_config_file";
|
||||
-f $query_config_format_file or die "No such file: $query_config_format_file";
|
||||
if (defined($psa_crypto_config_file) && length($psa_crypto_config_file)) {
|
||||
-f $psa_crypto_config_file or die "No such file: $psa_crypto_config_file";
|
||||
} else {
|
||||
$psa_crypto_config_file = (-f $default_psa_crypto_config_file) ? $default_psa_crypto_config_file : undef;
|
||||
}
|
||||
} else {
|
||||
$config_file = "./include/mbedtls/mbedtls_config.h";
|
||||
$query_config_format_file = "./scripts/data_files/query_config.fmt";
|
||||
$query_config_file = "./programs/test/query_config.c";
|
||||
$mbedtls_config_file = $default_mbedtls_config_file;
|
||||
$query_config_format_file = $default_query_config_format_file;
|
||||
$query_config_file = $default_query_config_file;
|
||||
$psa_crypto_config_file = $default_psa_crypto_config_file;
|
||||
|
||||
unless( -f $config_file && -f $query_config_format_file ) {
|
||||
unless(-f $mbedtls_config_file && -f $query_config_format_file && -f $psa_crypto_config_file) {
|
||||
chdir '..' or die;
|
||||
-f $config_file && -f $query_config_format_file
|
||||
-f $mbedtls_config_file && -f $query_config_format_file && -f $psa_crypto_config_file
|
||||
or die "No arguments supplied, must be run from project root or a first-level subdirectory\n";
|
||||
}
|
||||
}
|
||||
|
@ -63,39 +74,50 @@ MBEDTLS_SSL_CIPHERSUITES
|
|||
);
|
||||
my $excluded_re = join '|', @excluded;
|
||||
|
||||
open(CONFIG_FILE, "$config_file") or die "Opening config file '$config_file': $!";
|
||||
|
||||
# This variable will contain the string to replace in the CHECK_CONFIG of the
|
||||
# format file
|
||||
my $config_check = "";
|
||||
my $list_config = "";
|
||||
|
||||
while (my $line = <CONFIG_FILE>) {
|
||||
if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+).*/) {
|
||||
my $name = $2;
|
||||
for my $config_file ($mbedtls_config_file, $psa_crypto_config_file) {
|
||||
|
||||
# Skip over the macro if it is in the ecluded list
|
||||
next if $name =~ /$excluded_re/;
|
||||
next unless defined($config_file); # we might not have been given a PSA crypto config file
|
||||
|
||||
$config_check .= "#if defined($name)\n";
|
||||
$config_check .= " if( strcmp( \"$name\", config ) == 0 )\n";
|
||||
$config_check .= " {\n";
|
||||
$config_check .= " MACRO_EXPANSION_TO_STR( $name );\n";
|
||||
$config_check .= " return( 0 );\n";
|
||||
$config_check .= " }\n";
|
||||
$config_check .= "#endif /* $name */\n";
|
||||
$config_check .= "\n";
|
||||
open(CONFIG_FILE, "<", $config_file) or die "Opening config file '$config_file': $!";
|
||||
|
||||
$list_config .= "#if defined($name)\n";
|
||||
$list_config .= " OUTPUT_MACRO_NAME_VALUE($name);\n";
|
||||
$list_config .= "#endif /* $name */\n";
|
||||
$list_config .= "\n";
|
||||
while (my $line = <CONFIG_FILE>) {
|
||||
if ($line =~ /^(\/\/)?\s*#\s*define\s+(MBEDTLS_\w+|PSA_WANT_\w+).*/) {
|
||||
my $name = $2;
|
||||
|
||||
# Skip over the macro if it is in the excluded list
|
||||
next if $name =~ /$excluded_re/;
|
||||
|
||||
$config_check .= <<EOT;
|
||||
#if defined($name)
|
||||
if( strcmp( "$name", config ) == 0 )
|
||||
{
|
||||
MACRO_EXPANSION_TO_STR( $name );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* $name */
|
||||
|
||||
EOT
|
||||
|
||||
$list_config .= <<EOT;
|
||||
#if defined($name)
|
||||
OUTPUT_MACRO_NAME_VALUE($name);
|
||||
#endif /* $name */
|
||||
|
||||
EOT
|
||||
}
|
||||
}
|
||||
|
||||
close(CONFIG_FILE);
|
||||
}
|
||||
|
||||
# Read the full format file into a string
|
||||
local $/;
|
||||
open(FORMAT_FILE, "$query_config_format_file") or die "Opening query config format file '$query_config_format_file': $!";
|
||||
open(FORMAT_FILE, "<", $query_config_format_file) or die "Opening query config format file '$query_config_format_file': $!";
|
||||
my $query_config_format = <FORMAT_FILE>;
|
||||
close(FORMAT_FILE);
|
||||
|
||||
|
@ -104,6 +126,6 @@ $query_config_format =~ s/CHECK_CONFIG/$config_check/g;
|
|||
$query_config_format =~ s/LIST_CONFIG/$list_config/g;
|
||||
|
||||
# Rewrite the query_config.c file
|
||||
open(QUERY_CONFIG_FILE, ">$query_config_file") or die "Opening destination file '$query_config_file': $!";
|
||||
open(QUERY_CONFIG_FILE, ">", $query_config_file) or die "Opening destination file '$query_config_file': $!";
|
||||
print QUERY_CONFIG_FILE $query_config_format;
|
||||
close(QUERY_CONFIG_FILE);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""Generate library/ssl_debug_helps_generated.c
|
||||
"""Generate library/ssl_debug_helpers_generated.c
|
||||
|
||||
The code generated by this module includes debug helper functions that can not be
|
||||
implemented by fixed codes.
|
||||
|
@ -53,7 +53,7 @@ def preprocess_c_source_code(source, *classes):
|
|||
"""
|
||||
Simple preprocessor for C source code.
|
||||
|
||||
Only processses condition directives without expanding them.
|
||||
Only processes condition directives without expanding them.
|
||||
Yield object according to the classes input. Most match firstly
|
||||
|
||||
If the directive pair does not match , raise CondDirectiveNotMatch.
|
||||
|
@ -224,7 +224,7 @@ class EnumDefinition:
|
|||
if( in > ( sizeof( in_to_str )/sizeof( in_to_str[0]) - 1 ) ||
|
||||
in_to_str[ in ] == NULL )
|
||||
{{
|
||||
return "UNKOWN_VAULE";
|
||||
return "UNKNOWN_VALUE";
|
||||
}}
|
||||
return in_to_str[ in ];
|
||||
}}
|
||||
|
@ -235,13 +235,130 @@ class EnumDefinition:
|
|||
return body
|
||||
|
||||
|
||||
class SignatureAlgorithmDefinition:
|
||||
"""
|
||||
Generate helper functions for signature algorithms.
|
||||
|
||||
It generates translation function from signature algorithm define to string.
|
||||
Signature algorithm definition looks like:
|
||||
#define MBEDTLS_TLS1_3_SIG_[ upper case signature algorithm ] [ value(hex) ]
|
||||
|
||||
Known limitation:
|
||||
- the definitions SHOULD exist in same macro blocks.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def extract(cls, source_code, start=0, end=-1):
|
||||
sig_alg_pattern = re.compile(r'#define\s+(?P<name>MBEDTLS_TLS1_3_SIG_\w+)\s+' +
|
||||
r'(?P<value>0[xX][0-9a-fA-F]+)$',
|
||||
re.MULTILINE | re.DOTALL)
|
||||
matches = list(sig_alg_pattern.finditer(source_code, start, end))
|
||||
if matches:
|
||||
yield SignatureAlgorithmDefinition(source_code, definitions=matches)
|
||||
|
||||
def __init__(self, source_code, definitions=None):
|
||||
if definitions is None:
|
||||
definitions = []
|
||||
assert isinstance(definitions, list) and definitions
|
||||
self._definitions = definitions
|
||||
self._source = source_code
|
||||
|
||||
def __repr__(self):
|
||||
return 'SigAlgs({})'.format(self._definitions[0].span())
|
||||
|
||||
def span(self):
|
||||
return self._definitions[0].span()
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
Generate function for translating value to string
|
||||
"""
|
||||
translation_table = []
|
||||
for m in self._definitions:
|
||||
name = m.groupdict()['name']
|
||||
return_val = name[len('MBEDTLS_TLS1_3_SIG_'):].lower()
|
||||
translation_table.append(
|
||||
' case {}:\n return "{}";'.format(name, return_val))
|
||||
|
||||
body = textwrap.dedent('''\
|
||||
const char *mbedtls_ssl_sig_alg_to_str( uint16_t in )
|
||||
{{
|
||||
switch( in )
|
||||
{{
|
||||
{translation_table}
|
||||
}};
|
||||
|
||||
return "UNKNOWN";
|
||||
}}''')
|
||||
body = body.format(translation_table='\n'.join(translation_table))
|
||||
return body
|
||||
|
||||
|
||||
class NamedGroupDefinition:
|
||||
"""
|
||||
Generate helper functions for named group
|
||||
|
||||
It generates translation function from named group define to string.
|
||||
Named group definition looks like:
|
||||
#define MBEDTLS_SSL_IANA_TLS_GROUP_[ upper case named group ] [ value(hex) ]
|
||||
|
||||
Known limitation:
|
||||
- the definitions SHOULD exist in same macro blocks.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def extract(cls, source_code, start=0, end=-1):
|
||||
named_group_pattern = re.compile(r'#define\s+(?P<name>MBEDTLS_SSL_IANA_TLS_GROUP_\w+)\s+' +
|
||||
r'(?P<value>0[xX][0-9a-fA-F]+)$',
|
||||
re.MULTILINE | re.DOTALL)
|
||||
matches = list(named_group_pattern.finditer(source_code, start, end))
|
||||
if matches:
|
||||
yield NamedGroupDefinition(source_code, definitions=matches)
|
||||
|
||||
def __init__(self, source_code, definitions=None):
|
||||
if definitions is None:
|
||||
definitions = []
|
||||
assert isinstance(definitions, list) and definitions
|
||||
self._definitions = definitions
|
||||
self._source = source_code
|
||||
|
||||
def __repr__(self):
|
||||
return 'NamedGroup({})'.format(self._definitions[0].span())
|
||||
|
||||
def span(self):
|
||||
return self._definitions[0].span()
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
Generate function for translating value to string
|
||||
"""
|
||||
translation_table = []
|
||||
for m in self._definitions:
|
||||
name = m.groupdict()['name']
|
||||
iana_name = name[len('MBEDTLS_SSL_IANA_TLS_GROUP_'):].lower()
|
||||
translation_table.append(' case {}:\n return "{}";'.format(name, iana_name))
|
||||
|
||||
body = textwrap.dedent('''\
|
||||
const char *mbedtls_ssl_named_group_to_str( uint16_t in )
|
||||
{{
|
||||
switch( in )
|
||||
{{
|
||||
{translation_table}
|
||||
}};
|
||||
|
||||
return "UNKOWN";
|
||||
}}''')
|
||||
body = body.format(translation_table='\n'.join(translation_table))
|
||||
return body
|
||||
|
||||
|
||||
OUTPUT_C_TEMPLATE = '''\
|
||||
/* Automatically generated by generate_ssl_debug_helpers.py. DO NOT EDIT. */
|
||||
|
||||
/**
|
||||
* \file ssl_debug_helpers_generated.c
|
||||
* \\file ssl_debug_helpers_generated.c
|
||||
*
|
||||
* \brief Automatically generated helper functions for debugging
|
||||
* \\brief Automatically generated helper functions for debugging
|
||||
*/
|
||||
/*
|
||||
* Copyright The Mbed TLS Contributors
|
||||
|
@ -278,12 +395,16 @@ def generate_ssl_debug_helpers(output_directory, mbedtls_root):
|
|||
"""
|
||||
Generate functions of debug helps
|
||||
"""
|
||||
mbedtls_root = os.path.abspath(mbedtls_root or build_tree.guess_mbedtls_root())
|
||||
mbedtls_root = os.path.abspath(
|
||||
mbedtls_root or build_tree.guess_mbedtls_root())
|
||||
with open(os.path.join(mbedtls_root, 'include/mbedtls/ssl.h')) as f:
|
||||
source_code = remove_c_comments(f.read())
|
||||
|
||||
definitions = dict()
|
||||
for start, instance in preprocess_c_source_code(source_code, EnumDefinition):
|
||||
for start, instance in preprocess_c_source_code(source_code,
|
||||
EnumDefinition,
|
||||
SignatureAlgorithmDefinition,
|
||||
NamedGroupDefinition):
|
||||
if start in definitions:
|
||||
continue
|
||||
if isinstance(instance, EnumDefinition):
|
||||
|
|
|
@ -10,4 +10,5 @@ perl scripts\generate_features.pl || exit /b 1
|
|||
python scripts\generate_ssl_debug_helpers.py || exit /b 1
|
||||
perl scripts\generate_visualc_files.pl || exit /b 1
|
||||
python scripts\generate_psa_constants.py || exit /b 1
|
||||
python tests\scripts\generate_bignum_tests.py || exit /b 1
|
||||
python tests\scripts\generate_psa_tests.py || exit /b 1
|
||||
|
|
|
@ -18,15 +18,68 @@ This module is entirely based on the PSA API.
|
|||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import enum
|
||||
import re
|
||||
from typing import Dict, Iterable, Optional, Pattern, Tuple
|
||||
from typing import FrozenSet, Iterable, List, Optional, Tuple
|
||||
|
||||
from mbedtls_dev.asymmetric_key_data import ASYMMETRIC_KEY_DATA
|
||||
|
||||
|
||||
def short_expression(original: str, level: int = 0) -> str:
|
||||
"""Abbreviate the expression, keeping it human-readable.
|
||||
|
||||
If `level` is 0, just remove parts that are implicit from context,
|
||||
such as a leading ``PSA_KEY_TYPE_``.
|
||||
For larger values of `level`, also abbreviate some names in an
|
||||
unambiguous, but ad hoc way.
|
||||
"""
|
||||
short = original
|
||||
short = re.sub(r'\bPSA_(?:ALG|ECC_FAMILY|KEY_[A-Z]+)_', r'', short)
|
||||
short = re.sub(r' +', r'', short)
|
||||
if level >= 1:
|
||||
short = re.sub(r'PUBLIC_KEY\b', r'PUB', short)
|
||||
short = re.sub(r'KEY_PAIR\b', r'PAIR', short)
|
||||
short = re.sub(r'\bBRAINPOOL_P', r'BP', short)
|
||||
short = re.sub(r'\bMONTGOMERY\b', r'MGM', short)
|
||||
short = re.sub(r'AEAD_WITH_SHORTENED_TAG\b', r'AEAD_SHORT', short)
|
||||
short = re.sub(r'\bDETERMINISTIC_', r'DET_', short)
|
||||
short = re.sub(r'\bKEY_AGREEMENT\b', r'KA', short)
|
||||
short = re.sub(r'_PSK_TO_MS\b', r'_PSK2MS', short)
|
||||
return short
|
||||
|
||||
|
||||
BLOCK_CIPHERS = frozenset(['AES', 'ARIA', 'CAMELLIA', 'DES'])
|
||||
BLOCK_MAC_MODES = frozenset(['CBC_MAC', 'CMAC'])
|
||||
BLOCK_CIPHER_MODES = frozenset([
|
||||
'CTR', 'CFB', 'OFB', 'XTS', 'CCM_STAR_NO_TAG',
|
||||
'ECB_NO_PADDING', 'CBC_NO_PADDING', 'CBC_PKCS7',
|
||||
])
|
||||
BLOCK_AEAD_MODES = frozenset(['CCM', 'GCM'])
|
||||
|
||||
class EllipticCurveCategory(enum.Enum):
|
||||
"""Categorization of elliptic curve families.
|
||||
|
||||
The category of a curve determines what algorithms are defined over it.
|
||||
"""
|
||||
|
||||
SHORT_WEIERSTRASS = 0
|
||||
MONTGOMERY = 1
|
||||
TWISTED_EDWARDS = 2
|
||||
|
||||
@staticmethod
|
||||
def from_family(family: str) -> 'EllipticCurveCategory':
|
||||
if family == 'PSA_ECC_FAMILY_MONTGOMERY':
|
||||
return EllipticCurveCategory.MONTGOMERY
|
||||
if family == 'PSA_ECC_FAMILY_TWISTED_EDWARDS':
|
||||
return EllipticCurveCategory.TWISTED_EDWARDS
|
||||
# Default to SW, which most curves belong to.
|
||||
return EllipticCurveCategory.SHORT_WEIERSTRASS
|
||||
|
||||
|
||||
class KeyType:
|
||||
"""Knowledge about a PSA key type."""
|
||||
|
||||
def __init__(self, name: str, params: Optional[Iterable[str]] = None):
|
||||
def __init__(self, name: str, params: Optional[Iterable[str]] = None) -> None:
|
||||
"""Analyze a key type.
|
||||
|
||||
The key type must be specified in PSA syntax. In its simplest form,
|
||||
|
@ -62,6 +115,11 @@ class KeyType:
|
|||
if self.params is not None:
|
||||
self.expression += '(' + ', '.join(self.params) + ')'
|
||||
|
||||
m = re.match(r'PSA_KEY_TYPE_(\w+)', self.name)
|
||||
assert m
|
||||
self.head = re.sub(r'_(?:PUBLIC_KEY|KEY_PAIR)\Z', r'', m.group(1))
|
||||
"""The key type macro name, with common prefixes and suffixes stripped."""
|
||||
|
||||
self.private_type = re.sub(r'_PUBLIC_KEY\Z', r'_KEY_PAIR', self.name)
|
||||
"""The key type macro name for the corresponding key pair type.
|
||||
|
||||
|
@ -69,6 +127,17 @@ class KeyType:
|
|||
`self.name`.
|
||||
"""
|
||||
|
||||
def short_expression(self, level: int = 0) -> str:
|
||||
"""Abbreviate the expression, keeping it human-readable.
|
||||
|
||||
See `crypto_knowledge.short_expression`.
|
||||
"""
|
||||
return short_expression(self.expression, level=level)
|
||||
|
||||
def is_public(self) -> bool:
|
||||
"""Whether the key type is for public keys."""
|
||||
return self.name.endswith('_PUBLIC_KEY')
|
||||
|
||||
ECC_KEY_SIZES = {
|
||||
'PSA_ECC_FAMILY_SECP_K1': (192, 224, 256),
|
||||
'PSA_ECC_FAMILY_SECP_R1': (225, 256, 384, 521),
|
||||
|
@ -139,17 +208,329 @@ class KeyType:
|
|||
return b''.join([self.DATA_BLOCK] * (length // len(self.DATA_BLOCK)) +
|
||||
[self.DATA_BLOCK[:length % len(self.DATA_BLOCK)]])
|
||||
|
||||
KEY_TYPE_FOR_SIGNATURE = {
|
||||
'PSA_KEY_USAGE_SIGN_HASH': re.compile('.*KEY_PAIR'),
|
||||
'PSA_KEY_USAGE_VERIFY_HASH': re.compile('.*KEY.*')
|
||||
} #type: Dict[str, Pattern]
|
||||
"""Use a regexp to determine key types for which signature is possible
|
||||
when using the actual usage flag.
|
||||
"""
|
||||
def is_valid_for_signature(self, usage: str) -> bool:
|
||||
"""Determine if the key type is compatible with the specified
|
||||
signitute type.
|
||||
def can_do(self, alg: 'Algorithm') -> bool:
|
||||
"""Whether this key type can be used for operations with the given algorithm.
|
||||
|
||||
This function does not currently handle key derivation or PAKE.
|
||||
"""
|
||||
# This is just temporaly solution for the implicit usage flags.
|
||||
return re.match(self.KEY_TYPE_FOR_SIGNATURE[usage], self.name) is not None
|
||||
#pylint: disable=too-many-branches,too-many-return-statements
|
||||
if alg.is_wildcard:
|
||||
return False
|
||||
if alg.is_invalid_truncation():
|
||||
return False
|
||||
if self.head == 'HMAC' and alg.head == 'HMAC':
|
||||
return True
|
||||
if self.head == 'DES':
|
||||
# 64-bit block ciphers only allow a reduced set of modes.
|
||||
return alg.head in [
|
||||
'CBC_NO_PADDING', 'CBC_PKCS7',
|
||||
'ECB_NO_PADDING',
|
||||
]
|
||||
if self.head in BLOCK_CIPHERS and \
|
||||
alg.head in frozenset.union(BLOCK_MAC_MODES,
|
||||
BLOCK_CIPHER_MODES,
|
||||
BLOCK_AEAD_MODES):
|
||||
if alg.head in ['CMAC', 'OFB'] and \
|
||||
self.head in ['ARIA', 'CAMELLIA']:
|
||||
return False # not implemented in Mbed TLS
|
||||
return True
|
||||
if self.head == 'CHACHA20' and alg.head == 'CHACHA20_POLY1305':
|
||||
return True
|
||||
if self.head in {'ARC4', 'CHACHA20'} and \
|
||||
alg.head == 'STREAM_CIPHER':
|
||||
return True
|
||||
if self.head == 'RSA' and alg.head.startswith('RSA_'):
|
||||
return True
|
||||
if alg.category == AlgorithmCategory.KEY_AGREEMENT and \
|
||||
self.is_public():
|
||||
# The PSA API does not use public key objects in key agreement
|
||||
# operations: it imports the public key as a formatted byte string.
|
||||
# So a public key object with a key agreement algorithm is not
|
||||
# a valid combination.
|
||||
return False
|
||||
if self.head == 'ECC':
|
||||
assert self.params is not None
|
||||
eccc = EllipticCurveCategory.from_family(self.params[0])
|
||||
if alg.head == 'ECDH' and \
|
||||
eccc in {EllipticCurveCategory.SHORT_WEIERSTRASS,
|
||||
EllipticCurveCategory.MONTGOMERY}:
|
||||
return True
|
||||
if alg.head == 'ECDSA' and \
|
||||
eccc == EllipticCurveCategory.SHORT_WEIERSTRASS:
|
||||
return True
|
||||
if alg.head in {'PURE_EDDSA', 'EDDSA_PREHASH'} and \
|
||||
eccc == EllipticCurveCategory.TWISTED_EDWARDS:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class AlgorithmCategory(enum.Enum):
|
||||
"""PSA algorithm categories."""
|
||||
# The numbers are aligned with the category bits in numerical values of
|
||||
# algorithms.
|
||||
HASH = 2
|
||||
MAC = 3
|
||||
CIPHER = 4
|
||||
AEAD = 5
|
||||
SIGN = 6
|
||||
ASYMMETRIC_ENCRYPTION = 7
|
||||
KEY_DERIVATION = 8
|
||||
KEY_AGREEMENT = 9
|
||||
PAKE = 10
|
||||
|
||||
def requires_key(self) -> bool:
|
||||
"""Whether operations in this category are set up with a key."""
|
||||
return self not in {self.HASH, self.KEY_DERIVATION}
|
||||
|
||||
def is_asymmetric(self) -> bool:
|
||||
"""Whether operations in this category involve asymmetric keys."""
|
||||
return self in {
|
||||
self.SIGN,
|
||||
self.ASYMMETRIC_ENCRYPTION,
|
||||
self.KEY_AGREEMENT
|
||||
}
|
||||
|
||||
|
||||
class AlgorithmNotRecognized(Exception):
|
||||
def __init__(self, expr: str) -> None:
|
||||
super().__init__('Algorithm not recognized: ' + expr)
|
||||
self.expr = expr
|
||||
|
||||
|
||||
class Algorithm:
|
||||
"""Knowledge about a PSA algorithm."""
|
||||
|
||||
@staticmethod
|
||||
def determine_base(expr: str) -> str:
|
||||
"""Return an expression for the "base" of the algorithm.
|
||||
|
||||
This strips off variants of algorithms such as MAC truncation.
|
||||
|
||||
This function does not attempt to detect invalid inputs.
|
||||
"""
|
||||
m = re.match(r'PSA_ALG_(?:'
|
||||
r'(?:TRUNCATED|AT_LEAST_THIS_LENGTH)_MAC|'
|
||||
r'AEAD_WITH_(?:SHORTENED|AT_LEAST_THIS_LENGTH)_TAG'
|
||||
r')\((.*),[^,]+\)\Z', expr)
|
||||
if m:
|
||||
expr = m.group(1)
|
||||
return expr
|
||||
|
||||
@staticmethod
|
||||
def determine_head(expr: str) -> str:
|
||||
"""Return the head of an algorithm expression.
|
||||
|
||||
The head is the first (outermost) constructor, without its PSA_ALG_
|
||||
prefix, and with some normalization of similar algorithms.
|
||||
"""
|
||||
m = re.match(r'PSA_ALG_(?:DETERMINISTIC_)?(\w+)', expr)
|
||||
if not m:
|
||||
raise AlgorithmNotRecognized(expr)
|
||||
head = m.group(1)
|
||||
if head == 'KEY_AGREEMENT':
|
||||
m = re.match(r'PSA_ALG_KEY_AGREEMENT\s*\(\s*PSA_ALG_(\w+)', expr)
|
||||
if not m:
|
||||
raise AlgorithmNotRecognized(expr)
|
||||
head = m.group(1)
|
||||
head = re.sub(r'_ANY\Z', r'', head)
|
||||
if re.match(r'ED[0-9]+PH\Z', head):
|
||||
head = 'EDDSA_PREHASH'
|
||||
return head
|
||||
|
||||
CATEGORY_FROM_HEAD = {
|
||||
'SHA': AlgorithmCategory.HASH,
|
||||
'SHAKE256_512': AlgorithmCategory.HASH,
|
||||
'MD': AlgorithmCategory.HASH,
|
||||
'RIPEMD': AlgorithmCategory.HASH,
|
||||
'ANY_HASH': AlgorithmCategory.HASH,
|
||||
'HMAC': AlgorithmCategory.MAC,
|
||||
'STREAM_CIPHER': AlgorithmCategory.CIPHER,
|
||||
'CHACHA20_POLY1305': AlgorithmCategory.AEAD,
|
||||
'DSA': AlgorithmCategory.SIGN,
|
||||
'ECDSA': AlgorithmCategory.SIGN,
|
||||
'EDDSA': AlgorithmCategory.SIGN,
|
||||
'PURE_EDDSA': AlgorithmCategory.SIGN,
|
||||
'RSA_PSS': AlgorithmCategory.SIGN,
|
||||
'RSA_PKCS1V15_SIGN': AlgorithmCategory.SIGN,
|
||||
'RSA_PKCS1V15_CRYPT': AlgorithmCategory.ASYMMETRIC_ENCRYPTION,
|
||||
'RSA_OAEP': AlgorithmCategory.ASYMMETRIC_ENCRYPTION,
|
||||
'HKDF': AlgorithmCategory.KEY_DERIVATION,
|
||||
'TLS12_PRF': AlgorithmCategory.KEY_DERIVATION,
|
||||
'TLS12_PSK_TO_MS': AlgorithmCategory.KEY_DERIVATION,
|
||||
'PBKDF': AlgorithmCategory.KEY_DERIVATION,
|
||||
'ECDH': AlgorithmCategory.KEY_AGREEMENT,
|
||||
'FFDH': AlgorithmCategory.KEY_AGREEMENT,
|
||||
# KEY_AGREEMENT(...) is a key derivation with a key agreement component
|
||||
'KEY_AGREEMENT': AlgorithmCategory.KEY_DERIVATION,
|
||||
'JPAKE': AlgorithmCategory.PAKE,
|
||||
}
|
||||
for x in BLOCK_MAC_MODES:
|
||||
CATEGORY_FROM_HEAD[x] = AlgorithmCategory.MAC
|
||||
for x in BLOCK_CIPHER_MODES:
|
||||
CATEGORY_FROM_HEAD[x] = AlgorithmCategory.CIPHER
|
||||
for x in BLOCK_AEAD_MODES:
|
||||
CATEGORY_FROM_HEAD[x] = AlgorithmCategory.AEAD
|
||||
|
||||
def determine_category(self, expr: str, head: str) -> AlgorithmCategory:
|
||||
"""Return the category of the given algorithm expression.
|
||||
|
||||
This function does not attempt to detect invalid inputs.
|
||||
"""
|
||||
prefix = head
|
||||
while prefix:
|
||||
if prefix in self.CATEGORY_FROM_HEAD:
|
||||
return self.CATEGORY_FROM_HEAD[prefix]
|
||||
if re.match(r'.*[0-9]\Z', prefix):
|
||||
prefix = re.sub(r'_*[0-9]+\Z', r'', prefix)
|
||||
else:
|
||||
prefix = re.sub(r'_*[^_]*\Z', r'', prefix)
|
||||
raise AlgorithmNotRecognized(expr)
|
||||
|
||||
@staticmethod
|
||||
def determine_wildcard(expr) -> bool:
|
||||
"""Whether the given algorithm expression is a wildcard.
|
||||
|
||||
This function does not attempt to detect invalid inputs.
|
||||
"""
|
||||
if re.search(r'\bPSA_ALG_ANY_HASH\b', expr):
|
||||
return True
|
||||
if re.search(r'_AT_LEAST_', expr):
|
||||
return True
|
||||
return False
|
||||
|
||||
def __init__(self, expr: str) -> None:
|
||||
"""Analyze an algorithm value.
|
||||
|
||||
The algorithm must be expressed as a C expression containing only
|
||||
calls to PSA algorithm constructor macros and numeric literals.
|
||||
|
||||
This class is only programmed to handle valid expressions. Invalid
|
||||
expressions may result in exceptions or in nonsensical results.
|
||||
"""
|
||||
self.expression = re.sub(r'\s+', r'', expr)
|
||||
self.base_expression = self.determine_base(self.expression)
|
||||
self.head = self.determine_head(self.base_expression)
|
||||
self.category = self.determine_category(self.base_expression, self.head)
|
||||
self.is_wildcard = self.determine_wildcard(self.expression)
|
||||
|
||||
def is_key_agreement_with_derivation(self) -> bool:
|
||||
"""Whether this is a combined key agreement and key derivation algorithm."""
|
||||
if self.category != AlgorithmCategory.KEY_AGREEMENT:
|
||||
return False
|
||||
m = re.match(r'PSA_ALG_KEY_AGREEMENT\(\w+,\s*(.*)\)\Z', self.expression)
|
||||
if not m:
|
||||
return False
|
||||
kdf_alg = m.group(1)
|
||||
# Assume kdf_alg is either a valid KDF or 0.
|
||||
return not re.match(r'(?:0[Xx])?0+\s*\Z', kdf_alg)
|
||||
|
||||
|
||||
def short_expression(self, level: int = 0) -> str:
|
||||
"""Abbreviate the expression, keeping it human-readable.
|
||||
|
||||
See `crypto_knowledge.short_expression`.
|
||||
"""
|
||||
return short_expression(self.expression, level=level)
|
||||
|
||||
HASH_LENGTH = {
|
||||
'PSA_ALG_MD5': 16,
|
||||
'PSA_ALG_SHA_1': 20,
|
||||
}
|
||||
HASH_LENGTH_BITS_RE = re.compile(r'([0-9]+)\Z')
|
||||
@classmethod
|
||||
def hash_length(cls, alg: str) -> int:
|
||||
"""The length of the given hash algorithm, in bytes."""
|
||||
if alg in cls.HASH_LENGTH:
|
||||
return cls.HASH_LENGTH[alg]
|
||||
m = cls.HASH_LENGTH_BITS_RE.search(alg)
|
||||
if m:
|
||||
return int(m.group(1)) // 8
|
||||
raise ValueError('Unknown hash length for ' + alg)
|
||||
|
||||
PERMITTED_TAG_LENGTHS = {
|
||||
'PSA_ALG_CCM': frozenset([4, 6, 8, 10, 12, 14, 16]),
|
||||
'PSA_ALG_CHACHA20_POLY1305': frozenset([16]),
|
||||
'PSA_ALG_GCM': frozenset([4, 8, 12, 13, 14, 15, 16]),
|
||||
}
|
||||
MAC_LENGTH = {
|
||||
'PSA_ALG_CBC_MAC': 16, # actually the block cipher length
|
||||
'PSA_ALG_CMAC': 16, # actually the block cipher length
|
||||
}
|
||||
HMAC_RE = re.compile(r'PSA_ALG_HMAC\((.*)\)\Z')
|
||||
@classmethod
|
||||
def permitted_truncations(cls, base: str) -> FrozenSet[int]:
|
||||
"""Permitted output lengths for the given MAC or AEAD base algorithm.
|
||||
|
||||
For a MAC algorithm, this is the set of truncation lengths that
|
||||
Mbed TLS supports.
|
||||
For an AEAD algorithm, this is the set of truncation lengths that
|
||||
are permitted by the algorithm specification.
|
||||
"""
|
||||
if base in cls.PERMITTED_TAG_LENGTHS:
|
||||
return cls.PERMITTED_TAG_LENGTHS[base]
|
||||
max_length = cls.MAC_LENGTH.get(base, None)
|
||||
if max_length is None:
|
||||
m = cls.HMAC_RE.match(base)
|
||||
if m:
|
||||
max_length = cls.hash_length(m.group(1))
|
||||
if max_length is None:
|
||||
raise ValueError('Unknown permitted lengths for ' + base)
|
||||
return frozenset(range(4, max_length + 1))
|
||||
|
||||
TRUNCATED_ALG_RE = re.compile(
|
||||
r'(?P<face>PSA_ALG_(?:AEAD_WITH_SHORTENED_TAG|TRUNCATED_MAC))'
|
||||
r'\((?P<base>.*),'
|
||||
r'(?P<length>0[Xx][0-9A-Fa-f]+|[1-9][0-9]*|0[0-7]*)[LUlu]*\)\Z')
|
||||
def is_invalid_truncation(self) -> bool:
|
||||
"""False for a MAC or AEAD algorithm truncated to an invalid length.
|
||||
|
||||
True for a MAC or AEAD algorithm truncated to a valid length or to
|
||||
a length that cannot be determined. True for anything other than
|
||||
a truncated MAC or AEAD.
|
||||
"""
|
||||
m = self.TRUNCATED_ALG_RE.match(self.expression)
|
||||
if m:
|
||||
base = m.group('base')
|
||||
to_length = int(m.group('length'), 0)
|
||||
permitted_lengths = self.permitted_truncations(base)
|
||||
if to_length not in permitted_lengths:
|
||||
return True
|
||||
return False
|
||||
|
||||
def can_do(self, category: AlgorithmCategory) -> bool:
|
||||
"""Whether this algorithm can perform operations in the given category.
|
||||
"""
|
||||
if category == self.category:
|
||||
return True
|
||||
if category == AlgorithmCategory.KEY_DERIVATION and \
|
||||
self.is_key_agreement_with_derivation():
|
||||
return True
|
||||
return False
|
||||
|
||||
def usage_flags(self, public: bool = False) -> List[str]:
|
||||
"""The list of usage flags describing operations that can perform this algorithm.
|
||||
|
||||
If public is true, only return public-key operations, not private-key operations.
|
||||
"""
|
||||
if self.category == AlgorithmCategory.HASH:
|
||||
flags = []
|
||||
elif self.category == AlgorithmCategory.MAC:
|
||||
flags = ['SIGN_HASH', 'SIGN_MESSAGE',
|
||||
'VERIFY_HASH', 'VERIFY_MESSAGE']
|
||||
elif self.category == AlgorithmCategory.CIPHER or \
|
||||
self.category == AlgorithmCategory.AEAD:
|
||||
flags = ['DECRYPT', 'ENCRYPT']
|
||||
elif self.category == AlgorithmCategory.SIGN:
|
||||
flags = ['VERIFY_HASH', 'VERIFY_MESSAGE']
|
||||
if not public:
|
||||
flags += ['SIGN_HASH', 'SIGN_MESSAGE']
|
||||
elif self.category == AlgorithmCategory.ASYMMETRIC_ENCRYPTION:
|
||||
flags = ['ENCRYPT']
|
||||
if not public:
|
||||
flags += ['DECRYPT']
|
||||
elif self.category == AlgorithmCategory.KEY_DERIVATION or \
|
||||
self.category == AlgorithmCategory.KEY_AGREEMENT:
|
||||
flags = ['DERIVE']
|
||||
else:
|
||||
raise AlgorithmNotRecognized(self.expression)
|
||||
return ['PSA_KEY_USAGE_' + flag for flag in flags]
|
||||
|
|
|
@ -186,7 +186,7 @@ class PSAMacroEnumerator:
|
|||
for value in argument_lists[i][1:]:
|
||||
arguments[i] = value
|
||||
yield self._format_arguments(name, arguments)
|
||||
arguments[i] = argument_lists[0][0]
|
||||
arguments[i] = argument_lists[i][0]
|
||||
except BaseException as e:
|
||||
raise Exception('distribute_arguments({})'.format(name)) from e
|
||||
|
||||
|
@ -400,10 +400,26 @@ enumerate
|
|||
'other_algorithm': [],
|
||||
'lifetime': [self.lifetimes],
|
||||
} #type: Dict[str, List[Set[str]]]
|
||||
self.arguments_for['mac_length'] += ['1', '63']
|
||||
self.arguments_for['min_mac_length'] += ['1', '63']
|
||||
self.arguments_for['tag_length'] += ['1', '63']
|
||||
self.arguments_for['min_tag_length'] += ['1', '63']
|
||||
mac_lengths = [str(n) for n in [
|
||||
1, # minimum expressible
|
||||
4, # minimum allowed by policy
|
||||
13, # an odd size in a plausible range
|
||||
14, # an even non-power-of-two size in a plausible range
|
||||
16, # same as full size for at least one algorithm
|
||||
63, # maximum expressible
|
||||
]]
|
||||
self.arguments_for['mac_length'] += mac_lengths
|
||||
self.arguments_for['min_mac_length'] += mac_lengths
|
||||
aead_lengths = [str(n) for n in [
|
||||
1, # minimum expressible
|
||||
4, # minimum allowed by policy
|
||||
13, # an odd size in a plausible range
|
||||
14, # an even non-power-of-two size in a plausible range
|
||||
16, # same as full size for at least one algorithm
|
||||
63, # maximum expressible
|
||||
]]
|
||||
self.arguments_for['tag_length'] += aead_lengths
|
||||
self.arguments_for['min_tag_length'] += aead_lengths
|
||||
|
||||
def add_numerical_values(self) -> None:
|
||||
"""Add numerical values that are not supported to the known identifiers."""
|
||||
|
|
|
@ -1,4 +1,9 @@
|
|||
"""Knowledge about the PSA key store as implemented in Mbed TLS.
|
||||
|
||||
Note that if you need to make a change that affects how keys are
|
||||
stored, this may indicate that the key store is changing in a
|
||||
backward-incompatible way! Think carefully about backward compatibility
|
||||
before changing how test data is constructed or validated.
|
||||
"""
|
||||
|
||||
# Copyright The Mbed TLS Contributors
|
||||
|
@ -146,6 +151,11 @@ class Key:
|
|||
This is the content of the PSA storage file. When PSA storage is
|
||||
implemented over stdio files, this does not include any wrapping made
|
||||
by the PSA-storage-over-stdio-file implementation.
|
||||
|
||||
Note that if you need to make a change in this function,
|
||||
this may indicate that the key store is changing in a
|
||||
backward-incompatible way! Think carefully about backward
|
||||
compatibility before making any change here.
|
||||
"""
|
||||
header = self.MAGIC + self.pack('L', self.version)
|
||||
if self.version == 0:
|
||||
|
|
219
scripts/mbedtls_dev/test_generation.py
Normal file
219
scripts/mbedtls_dev/test_generation.py
Normal file
|
@ -0,0 +1,219 @@
|
|||
"""Common test generation classes and main function.
|
||||
|
||||
These are used both by generate_psa_tests.py and generate_bignum_tests.py.
|
||||
"""
|
||||
|
||||
# Copyright The Mbed TLS Contributors
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import posixpath
|
||||
import re
|
||||
|
||||
from abc import ABCMeta, abstractmethod
|
||||
from typing import Callable, Dict, Iterable, Iterator, List, Type, TypeVar
|
||||
|
||||
from mbedtls_dev import build_tree
|
||||
from mbedtls_dev import test_case
|
||||
|
||||
T = TypeVar('T') #pylint: disable=invalid-name
|
||||
|
||||
|
||||
class BaseTarget(metaclass=ABCMeta):
|
||||
"""Base target for test case generation.
|
||||
|
||||
Child classes of this class represent an output file, and can be referred
|
||||
to as file targets. These indicate where test cases will be written to for
|
||||
all subclasses of the file target, which is set by `target_basename`.
|
||||
|
||||
Attributes:
|
||||
count: Counter for test cases from this class.
|
||||
case_description: Short description of the test case. This may be
|
||||
automatically generated using the class, or manually set.
|
||||
dependencies: A list of dependencies required for the test case.
|
||||
show_test_count: Toggle for inclusion of `count` in the test description.
|
||||
target_basename: Basename of file to write generated tests to. This
|
||||
should be specified in a child class of BaseTarget.
|
||||
test_function: Test function which the class generates cases for.
|
||||
test_name: A common name or description of the test function. This can
|
||||
be `test_function`, a clearer equivalent, or a short summary of the
|
||||
test function's purpose.
|
||||
"""
|
||||
count = 0
|
||||
case_description = ""
|
||||
dependencies = [] # type: List[str]
|
||||
show_test_count = True
|
||||
target_basename = ""
|
||||
test_function = ""
|
||||
test_name = ""
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
# pylint: disable=unused-argument
|
||||
cls.count += 1
|
||||
return super().__new__(cls)
|
||||
|
||||
@abstractmethod
|
||||
def arguments(self) -> List[str]:
|
||||
"""Get the list of arguments for the test case.
|
||||
|
||||
Override this method to provide the list of arguments required for
|
||||
the `test_function`.
|
||||
|
||||
Returns:
|
||||
List of arguments required for the test function.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def description(self) -> str:
|
||||
"""Create a test case description.
|
||||
|
||||
Creates a description of the test case, including a name for the test
|
||||
function, an optional case count, and a description of the specific
|
||||
test case. This should inform a reader what is being tested, and
|
||||
provide context for the test case.
|
||||
|
||||
Returns:
|
||||
Description for the test case.
|
||||
"""
|
||||
if self.show_test_count:
|
||||
return "{} #{} {}".format(
|
||||
self.test_name, self.count, self.case_description
|
||||
).strip()
|
||||
else:
|
||||
return "{} {}".format(self.test_name, self.case_description).strip()
|
||||
|
||||
|
||||
def create_test_case(self) -> test_case.TestCase:
|
||||
"""Generate TestCase from the instance."""
|
||||
tc = test_case.TestCase()
|
||||
tc.set_description(self.description())
|
||||
tc.set_function(self.test_function)
|
||||
tc.set_arguments(self.arguments())
|
||||
tc.set_dependencies(self.dependencies)
|
||||
|
||||
return tc
|
||||
|
||||
@classmethod
|
||||
@abstractmethod
|
||||
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
||||
"""Generate test cases for the class test function.
|
||||
|
||||
This will be called in classes where `test_function` is set.
|
||||
Implementations should yield TestCase objects, by creating instances
|
||||
of the class with appropriate input data, and then calling
|
||||
`create_test_case()` on each.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def generate_tests(cls) -> Iterator[test_case.TestCase]:
|
||||
"""Generate test cases for the class and its subclasses.
|
||||
|
||||
In classes with `test_function` set, `generate_function_tests()` is
|
||||
called to generate test cases first.
|
||||
|
||||
In all classes, this method will iterate over its subclasses, and
|
||||
yield from `generate_tests()` in each. Calling this method on a class X
|
||||
will yield test cases from all classes derived from X.
|
||||
"""
|
||||
if cls.test_function:
|
||||
yield from cls.generate_function_tests()
|
||||
for subclass in sorted(cls.__subclasses__(), key=lambda c: c.__name__):
|
||||
yield from subclass.generate_tests()
|
||||
|
||||
|
||||
class TestGenerator:
|
||||
"""Generate test cases and write to data files."""
|
||||
def __init__(self, options) -> None:
|
||||
self.test_suite_directory = self.get_option(options, 'directory',
|
||||
'tests/suites')
|
||||
# Update `targets` with an entry for each child class of BaseTarget.
|
||||
# Each entry represents a file generated by the BaseTarget framework,
|
||||
# and enables generating the .data files using the CLI.
|
||||
self.targets.update({
|
||||
subclass.target_basename: subclass.generate_tests
|
||||
for subclass in BaseTarget.__subclasses__()
|
||||
})
|
||||
|
||||
@staticmethod
|
||||
def get_option(options, name: str, default: T) -> T:
|
||||
value = getattr(options, name, None)
|
||||
return default if value is None else value
|
||||
|
||||
def filename_for(self, basename: str) -> str:
|
||||
"""The location of the data file with the specified base name."""
|
||||
return posixpath.join(self.test_suite_directory, basename + '.data')
|
||||
|
||||
def write_test_data_file(self, basename: str,
|
||||
test_cases: Iterable[test_case.TestCase]) -> None:
|
||||
"""Write the test cases to a .data file.
|
||||
|
||||
The output file is ``basename + '.data'`` in the test suite directory.
|
||||
"""
|
||||
filename = self.filename_for(basename)
|
||||
test_case.write_data_file(filename, test_cases)
|
||||
|
||||
# Note that targets whose names contain 'test_format' have their content
|
||||
# validated by `abi_check.py`.
|
||||
targets = {} # type: Dict[str, Callable[..., Iterable[test_case.TestCase]]]
|
||||
|
||||
def generate_target(self, name: str, *target_args) -> None:
|
||||
"""Generate cases and write to data file for a target.
|
||||
|
||||
For target callables which require arguments, override this function
|
||||
and pass these arguments using super() (see PSATestGenerator).
|
||||
"""
|
||||
test_cases = self.targets[name](*target_args)
|
||||
self.write_test_data_file(name, test_cases)
|
||||
|
||||
def main(args, description: str, generator_class: Type[TestGenerator] = TestGenerator):
|
||||
"""Command line entry point."""
|
||||
parser = argparse.ArgumentParser(description=description)
|
||||
parser.add_argument('--list', action='store_true',
|
||||
help='List available targets and exit')
|
||||
parser.add_argument('--list-for-cmake', action='store_true',
|
||||
help='Print \';\'-separated list of available targets and exit')
|
||||
parser.add_argument('--directory', metavar='DIR',
|
||||
help='Output directory (default: tests/suites)')
|
||||
# The `--directory` option is interpreted relative to the directory from
|
||||
# which the script is invoked, but the default is relative to the root of
|
||||
# the mbedtls tree. The default should not be set above, but instead after
|
||||
# `build_tree.chdir_to_root()` is called.
|
||||
parser.add_argument('targets', nargs='*', metavar='TARGET',
|
||||
help='Target file to generate (default: all; "-": none)')
|
||||
options = parser.parse_args(args)
|
||||
build_tree.chdir_to_root()
|
||||
generator = generator_class(options)
|
||||
if options.list:
|
||||
for name in sorted(generator.targets):
|
||||
print(generator.filename_for(name))
|
||||
return
|
||||
# List in a cmake list format (i.e. ';'-separated)
|
||||
if options.list_for_cmake:
|
||||
print(';'.join(generator.filename_for(name)
|
||||
for name in sorted(generator.targets)), end='')
|
||||
return
|
||||
if options.targets:
|
||||
# Allow "-" as a special case so you can run
|
||||
# ``generate_xxx_tests.py - $targets`` and it works uniformly whether
|
||||
# ``$targets`` is empty or not.
|
||||
options.targets = [os.path.basename(re.sub(r'\.data\Z', r'', target))
|
||||
for target in options.targets
|
||||
if target != '-']
|
||||
else:
|
||||
options.targets = sorted(generator.targets)
|
||||
for target in options.targets:
|
||||
generator.generate_target(target)
|
|
@ -44,8 +44,9 @@ class Requirements:
|
|||
"""Adjust a requirement to the minimum specified version."""
|
||||
# allow inheritance #pylint: disable=no-self-use
|
||||
# If a requirement specifies a minimum version, impose that version.
|
||||
req = re.sub(r'>=|~=', r'==', req)
|
||||
return req
|
||||
split_req = req.split(';', 1)
|
||||
split_req[0] = re.sub(r'>=|~=', r'==', split_req[0])
|
||||
return ';'.join(split_req)
|
||||
|
||||
def add_file(self, filename: str) -> None:
|
||||
"""Add requirements from the specified file.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue