mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-08-07 06:42:56 +03:00
Merge branch 'development' into sha3
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -113,6 +113,8 @@ from types import SimpleNamespace
|
||||
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
from mbedtls_dev import build_tree
|
||||
|
||||
|
||||
class AbiChecker:
|
||||
"""API and ABI checker."""
|
||||
@@ -150,11 +152,6 @@ class AbiChecker:
|
||||
self.git_command = "git"
|
||||
self.make_command = "make"
|
||||
|
||||
@staticmethod
|
||||
def check_repo_path():
|
||||
if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
|
||||
raise Exception("Must be run from Mbed TLS root")
|
||||
|
||||
def _setup_logger(self):
|
||||
self.log = logging.getLogger()
|
||||
if self.verbose:
|
||||
@@ -540,7 +537,7 @@ class AbiChecker:
|
||||
def check_for_abi_changes(self):
|
||||
"""Generate a report of ABI differences
|
||||
between self.old_rev and self.new_rev."""
|
||||
self.check_repo_path()
|
||||
build_tree.check_repo_path()
|
||||
if self.check_api or self.check_abi:
|
||||
self.check_abi_tools_are_installed()
|
||||
self._get_abi_dump_for_ref(self.old_version)
|
||||
|
@@ -30,6 +30,9 @@ import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from mbedtls_dev import build_tree
|
||||
|
||||
|
||||
class CodeSizeComparison:
|
||||
"""Compare code size between two Git revisions."""
|
||||
|
||||
@@ -51,11 +54,6 @@ class CodeSizeComparison:
|
||||
self.git_command = "git"
|
||||
self.make_command = "make"
|
||||
|
||||
@staticmethod
|
||||
def check_repo_path():
|
||||
if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
|
||||
raise Exception("Must be run from Mbed TLS root")
|
||||
|
||||
@staticmethod
|
||||
def validate_revision(revision):
|
||||
result = subprocess.check_output(["git", "rev-parse", "--verify",
|
||||
@@ -172,7 +170,7 @@ class CodeSizeComparison:
|
||||
def get_comparision_results(self):
|
||||
"""Compare size of library/*.o between self.old_rev and self.new_rev,
|
||||
and generate the result file."""
|
||||
self.check_repo_path()
|
||||
build_tree.check_repo_path()
|
||||
self._get_code_size_for_rev(self.old_rev)
|
||||
self._get_code_size_for_rev(self.new_rev)
|
||||
return self.compare_code_size()
|
||||
|
206
scripts/code_style.py
Executable file
206
scripts/code_style.py
Executable file
@@ -0,0 +1,206 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Check or fix the code style by running Uncrustify.
|
||||
|
||||
This script must be run from the root of a Git work tree containing Mbed TLS.
|
||||
"""
|
||||
# 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 re
|
||||
import subprocess
|
||||
import sys
|
||||
from typing import FrozenSet, List
|
||||
|
||||
UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1"
|
||||
CONFIG_FILE = ".uncrustify.cfg"
|
||||
UNCRUSTIFY_EXE = "uncrustify"
|
||||
UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE]
|
||||
CHECK_GENERATED_FILES = "tests/scripts/check-generated-files.sh"
|
||||
|
||||
def print_err(*args):
|
||||
print("Error: ", *args, file=sys.stderr)
|
||||
|
||||
# Match FILENAME(s) in "check SCRIPT (FILENAME...)"
|
||||
CHECK_CALL_RE = re.compile(r"\n\s*check\s+[^\s#$&*?;|]+([^\n#$&*?;|]+)",
|
||||
re.ASCII)
|
||||
def list_generated_files() -> FrozenSet[str]:
|
||||
"""Return the names of generated files.
|
||||
|
||||
We don't reformat generated files, since the result might be different
|
||||
from the output of the generator. Ideally the result of the generator
|
||||
would conform to the code style, but this would be difficult, especially
|
||||
with respect to the placement of line breaks in long logical lines.
|
||||
"""
|
||||
# Parse check-generated-files.sh to get an up-to-date list of
|
||||
# generated files. Read the file rather than calling it so that
|
||||
# this script only depends on Git, Python and uncrustify, and not other
|
||||
# tools such as sh or grep which might not be available on Windows.
|
||||
# This introduces a limitation: check-generated-files.sh must have
|
||||
# the expected format and must list the files explicitly, not through
|
||||
# wildcards or command substitution.
|
||||
content = open(CHECK_GENERATED_FILES, encoding="utf-8").read()
|
||||
checks = re.findall(CHECK_CALL_RE, content)
|
||||
return frozenset(word for s in checks for word in s.split())
|
||||
|
||||
def get_src_files() -> List[str]:
|
||||
"""
|
||||
Use git ls-files to get a list of the source files
|
||||
"""
|
||||
git_ls_files_cmd = ["git", "ls-files",
|
||||
"*.[hc]",
|
||||
"tests/suites/*.function",
|
||||
"scripts/data_files/*.fmt"]
|
||||
|
||||
result = subprocess.run(git_ls_files_cmd, stdout=subprocess.PIPE,
|
||||
check=False)
|
||||
|
||||
if result.returncode != 0:
|
||||
print_err("git ls-files returned: " + str(result.returncode))
|
||||
return []
|
||||
else:
|
||||
generated_files = list_generated_files()
|
||||
src_files = str(result.stdout, "utf-8").split()
|
||||
# Don't correct style for third-party files (and, for simplicity,
|
||||
# companion files in the same subtree), or for automatically
|
||||
# generated files (we're correcting the templates instead).
|
||||
src_files = [filename for filename in src_files
|
||||
if not (filename.startswith("3rdparty/") or
|
||||
filename in generated_files)]
|
||||
return src_files
|
||||
|
||||
def get_uncrustify_version() -> str:
|
||||
"""
|
||||
Get the version string from Uncrustify
|
||||
"""
|
||||
result = subprocess.run([UNCRUSTIFY_EXE, "--version"],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
check=False)
|
||||
if result.returncode != 0:
|
||||
print_err("Could not get Uncrustify version:", str(result.stderr, "utf-8"))
|
||||
return ""
|
||||
else:
|
||||
return str(result.stdout, "utf-8")
|
||||
|
||||
def check_style_is_correct(src_file_list: List[str]) -> bool:
|
||||
"""
|
||||
Check the code style and output a diff for each file whose style is
|
||||
incorrect.
|
||||
"""
|
||||
style_correct = True
|
||||
for src_file in src_file_list:
|
||||
uncrustify_cmd = [UNCRUSTIFY_EXE] + UNCRUSTIFY_ARGS + [src_file]
|
||||
result = subprocess.run(uncrustify_cmd, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE, check=False)
|
||||
if result.returncode != 0:
|
||||
print_err("Uncrustify returned " + str(result.returncode) +
|
||||
" correcting file " + src_file)
|
||||
return False
|
||||
|
||||
# Uncrustify makes changes to the code and places the result in a new
|
||||
# file with the extension ".uncrustify". To get the changes (if any)
|
||||
# simply diff the 2 files.
|
||||
diff_cmd = ["diff", "-u", src_file, src_file + ".uncrustify"]
|
||||
cp = subprocess.run(diff_cmd, check=False)
|
||||
|
||||
if cp.returncode == 1:
|
||||
print(src_file + " changed - code style is incorrect.")
|
||||
style_correct = False
|
||||
elif cp.returncode != 0:
|
||||
raise subprocess.CalledProcessError(cp.returncode, cp.args,
|
||||
cp.stdout, cp.stderr)
|
||||
|
||||
# Tidy up artifact
|
||||
os.remove(src_file + ".uncrustify")
|
||||
|
||||
return style_correct
|
||||
|
||||
def fix_style_single_pass(src_file_list: List[str]) -> bool:
|
||||
"""
|
||||
Run Uncrustify once over the source files.
|
||||
"""
|
||||
code_change_args = UNCRUSTIFY_ARGS + ["--no-backup"]
|
||||
for src_file in src_file_list:
|
||||
uncrustify_cmd = [UNCRUSTIFY_EXE] + code_change_args + [src_file]
|
||||
result = subprocess.run(uncrustify_cmd, check=False)
|
||||
if result.returncode != 0:
|
||||
print_err("Uncrustify with file returned: " +
|
||||
str(result.returncode) + " correcting file " +
|
||||
src_file)
|
||||
return False
|
||||
return True
|
||||
|
||||
def fix_style(src_file_list: List[str]) -> int:
|
||||
"""
|
||||
Fix the code style. This takes 2 passes of Uncrustify.
|
||||
"""
|
||||
if not fix_style_single_pass(src_file_list):
|
||||
return 1
|
||||
if not fix_style_single_pass(src_file_list):
|
||||
return 1
|
||||
|
||||
# Guard against future changes that cause the codebase to require
|
||||
# more passes.
|
||||
if not check_style_is_correct(src_file_list):
|
||||
print_err("Code style still incorrect after second run of Uncrustify.")
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
def main() -> int:
|
||||
"""
|
||||
Main with command line arguments.
|
||||
"""
|
||||
uncrustify_version = get_uncrustify_version().strip()
|
||||
if UNCRUSTIFY_SUPPORTED_VERSION not in uncrustify_version:
|
||||
print("Warning: Using unsupported Uncrustify version '" +
|
||||
uncrustify_version + "'")
|
||||
print("Note: The only supported version is " +
|
||||
UNCRUSTIFY_SUPPORTED_VERSION)
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-f', '--fix', action='store_true',
|
||||
help=('modify source files to fix the code style '
|
||||
'(default: print diff, do not modify files)'))
|
||||
# --files is almost useless: it only matters if there are no files
|
||||
# ('code_style.py' without arguments checks all files known to Git,
|
||||
# 'code_style.py --files' does nothing). In particular,
|
||||
# 'code_style.py --fix --files ...' is intended as a stable ("porcelain")
|
||||
# way to restyle a possibly empty set of files.
|
||||
parser.add_argument('--files', action='store_true',
|
||||
help='only check the specified files (default with non-option arguments)')
|
||||
parser.add_argument('operands', nargs='*', metavar='FILE',
|
||||
help='files to check (if none: check files that are known to git)')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.files or args.operands:
|
||||
src_files = args.operands
|
||||
else:
|
||||
src_files = get_src_files()
|
||||
|
||||
if args.fix:
|
||||
# Fix mode
|
||||
return fix_style(src_files)
|
||||
else:
|
||||
# Check mode
|
||||
if check_style_is_correct(src_files):
|
||||
print("Checked {} files, style ok.".format(len(src_files)))
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
@@ -194,7 +194,6 @@ EXCLUDE_FROM_FULL = frozenset([
|
||||
'MBEDTLS_DEPRECATED_WARNING', # conflicts with deprecated options
|
||||
'MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED', # influences the use of ECDH in TLS
|
||||
'MBEDTLS_ECP_NO_FALLBACK', # removes internal ECP implementation
|
||||
'MBEDTLS_ECP_RESTARTABLE', # incompatible with USE_PSA_CRYPTO
|
||||
'MBEDTLS_ENTROPY_FORCE_SHA256', # interacts with CTR_DRBG_128_BIT_KEY
|
||||
'MBEDTLS_HAVE_SSE2', # hardware dependency
|
||||
'MBEDTLS_MEMORY_BACKTRACE', # depends on MEMORY_BUFFER_ALLOC_C
|
||||
@@ -311,6 +310,7 @@ def include_in_crypto(name):
|
||||
if name in [
|
||||
'MBEDTLS_DEBUG_C', # part of libmbedtls
|
||||
'MBEDTLS_NET_C', # part of libmbedtls
|
||||
'MBEDTLS_PKCS7_C', # part of libmbedx509
|
||||
]:
|
||||
return False
|
||||
return True
|
||||
|
71
scripts/data_files/driver_jsons/driver_opaque_schema.json
Normal file
71
scripts/data_files/driver_jsons/driver_opaque_schema.json
Normal file
@@ -0,0 +1,71 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"_comment": {
|
||||
"type": "string"
|
||||
},
|
||||
"prefix": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Z_a-z][0-9A-Z_a-z]*$"
|
||||
},
|
||||
"type": {
|
||||
"type": "string",
|
||||
"const": ["opaque"]
|
||||
},
|
||||
"location": {
|
||||
"type": ["integer","string"],
|
||||
"pattern": "^(0x|0X)?[a-fA-F0-9]+$"
|
||||
},
|
||||
"mbedtls/h_condition": {
|
||||
"type": "string"
|
||||
},
|
||||
"headers": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"default": []
|
||||
},
|
||||
"capabilities": {
|
||||
"type": "array",
|
||||
"items": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"_comment": {
|
||||
"type": "string"
|
||||
},
|
||||
"mbedtls/c_condition": {
|
||||
"type": "string"
|
||||
},
|
||||
"entry_points": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"names": {
|
||||
"type": "object",
|
||||
"patternProperties": {
|
||||
"^[A-Z_a-z][0-9A-Z_a-z]*$": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Z_a-z][0-9A-Z_a-z]*$"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"entry_points"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"prefix",
|
||||
"type",
|
||||
"location",
|
||||
"capabilities"
|
||||
]
|
||||
}
|
@@ -0,0 +1,70 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"_comment": {
|
||||
"type": "string"
|
||||
},
|
||||
"prefix": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Z_a-z][0-9A-Z_a-z]*$"
|
||||
},
|
||||
"type": {
|
||||
"type": "string",
|
||||
"const": ["transparent"]
|
||||
},
|
||||
"mbedtls/h_condition": {
|
||||
"type": "string"
|
||||
},
|
||||
"headers": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"default": []
|
||||
},
|
||||
"capabilities": {
|
||||
"type": "array",
|
||||
"items": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"_comment": {
|
||||
"type": "string"
|
||||
},
|
||||
"mbedtls/c_condition": {
|
||||
"type": "string"
|
||||
},
|
||||
"entry_points": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"names": {
|
||||
"type": "object",
|
||||
"patternProperties": {
|
||||
"^[A-Z_a-z][0-9A-Z_a-z]*$": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Z_a-z][0-9A-Z_a-z]*$"
|
||||
}
|
||||
}
|
||||
},
|
||||
"fallback": {
|
||||
"type": "boolean",
|
||||
"default": "false"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"entry_points"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"prefix",
|
||||
"type",
|
||||
"capabilities"
|
||||
]
|
||||
}
|
1
scripts/data_files/driver_jsons/driverlist.json
Normal file
1
scripts/data_files/driver_jsons/driverlist.json
Normal file
@@ -0,0 +1 @@
|
||||
["mbedtls_test_opaque_driver.json","mbedtls_test_transparent_driver.json"]
|
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"prefix": "mbedtls_test",
|
||||
"type": "opaque",
|
||||
"location": "0x7fffff",
|
||||
"mbedtls/h_condition": "defined(PSA_CRYPTO_DRIVER_TEST)",
|
||||
"headers": ["test/drivers/test_driver.h"],
|
||||
"capabilities": [
|
||||
{
|
||||
"_comment": "The Mbed TLS opaque driver supports import key/export key/export_public key",
|
||||
"mbedtls/c_condition": "defined(PSA_CRYPTO_DRIVER_TEST)",
|
||||
"entry_points": ["import_key", "export_key", "export_public_key"]
|
||||
},
|
||||
{
|
||||
"_comment": "The Mbed TLS opaque driver supports copy key/ get builtin key",
|
||||
"mbedtls/c_condition": "defined(PSA_CRYPTO_DRIVER_TEST)",
|
||||
"entry_points": ["copy_key", "get_builtin_key"],
|
||||
"names": {"copy_key":"mbedtls_test_opaque_copy_key", "get_builtin_key":"mbedtls_test_opaque_get_builtin_key"}
|
||||
}
|
||||
]
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"prefix": "mbedtls_test",
|
||||
"type": "transparent",
|
||||
"mbedtls/h_condition": "defined(PSA_CRYPTO_DRIVER_TEST)",
|
||||
"headers": ["test/drivers/test_driver.h"],
|
||||
"capabilities": [
|
||||
{
|
||||
"_comment": "The Mbed TLS transparent driver supports import key/export key",
|
||||
"mbedtls/c_condition": "defined(PSA_CRYPTO_DRIVER_TEST)",
|
||||
"entry_points": ["import_key", "export_key"],
|
||||
"fallback": true
|
||||
},
|
||||
{
|
||||
"_comment": "The Mbed TLS transparent driver supports export_public key",
|
||||
"mbedtls/c_condition": "defined(PSA_CRYPTO_DRIVER_TEST)",
|
||||
"entry_points": ["export_public_key"],
|
||||
"fallback": true,
|
||||
"names": {"export_public_key":"mbedtls_test_transparent_export_public_key"}
|
||||
}
|
||||
|
||||
]
|
||||
}
|
17
scripts/data_files/driver_templates/OS-template-opaque.jinja
Normal file
17
scripts/data_files/driver_templates/OS-template-opaque.jinja
Normal file
@@ -0,0 +1,17 @@
|
||||
{# One Shot function's dispatch code for opaque drivers.
|
||||
Expected inputs:
|
||||
* drivers: the list of driver descriptions.
|
||||
* entry_point: the name of the entry point that this function dispatches to.
|
||||
* entry_point_param(driver): the parameters to pass to the entry point.
|
||||
* nest_indent: number of extra spaces to indent the code to.
|
||||
-#}
|
||||
{% for driver in drivers if driver.type == "opaque" -%}
|
||||
{% for capability in driver.capabilities if entry_point in capability.entry_points -%}
|
||||
#if ({% if capability['mbedtls/c_condition'] is defined -%}{{ capability['mbedtls/c_condition'] }} {% else -%} {{ 1 }} {% endif %})
|
||||
{%- filter indent(width = nest_indent) %}
|
||||
case {{ driver.location }}:
|
||||
return( {{ entry_point_name(capability, entry_point, driver) }}({{entry_point_param(driver) | indent(20)}}));
|
||||
{% endfilter -%}
|
||||
#endif
|
||||
{% endfor %}
|
||||
{% endfor %}
|
@@ -0,0 +1,19 @@
|
||||
{# One Shot function's dispatch code for transparent drivers.
|
||||
Expected inputs:
|
||||
* drivers: the list of driver descriptions.
|
||||
* entry_point: the name of the entry point that this function dispatches to.
|
||||
* entry_point_param(driver): the parameters to pass to the entry point.
|
||||
* nest_indent: number of extra spaces to indent the code to.
|
||||
-#}
|
||||
{% for driver in drivers if driver.type == "transparent" -%}
|
||||
{% for capability in driver.capabilities if entry_point in capability.entry_points -%}
|
||||
#if ({% if capability['mbedtls/c_condition'] is defined -%}{{ capability['mbedtls/c_condition'] }} {% else -%} {{ 1 }} {% endif %})
|
||||
{%- filter indent(width = nest_indent) %}
|
||||
status = {{ entry_point_name(capability, entry_point, driver) }}({{entry_point_param(driver) | indent(20)}});
|
||||
|
||||
if( status != PSA_ERROR_NOT_SUPPORTED )
|
||||
return( status );
|
||||
{% endfilter -%}
|
||||
#endif
|
||||
{% endfor %}
|
||||
{% endfor %}
|
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Functions to delegate cryptographic operations to an available
|
||||
* and appropriate accelerator.
|
||||
* Warning: This file will be auto-generated in the future.
|
||||
* Warning: This file is now auto-generated.
|
||||
*/
|
||||
/* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
@@ -19,6 +19,8 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
/* BEGIN-common headers */
|
||||
#include "common.h"
|
||||
#include "psa_crypto_aead.h"
|
||||
#include "psa_crypto_cipher.h"
|
||||
@@ -29,34 +31,46 @@
|
||||
#include "psa_crypto_rsa.h"
|
||||
|
||||
#include "mbedtls/platform.h"
|
||||
/* END-common headers */
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
|
||||
/* BEGIN-driver headers */
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
|
||||
|
||||
/* Include test driver definition when running tests */
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
#ifndef PSA_CRYPTO_DRIVER_PRESENT
|
||||
#define PSA_CRYPTO_DRIVER_PRESENT
|
||||
{% for driver in drivers -%}
|
||||
/* Headers for {{driver.prefix}} {{driver.type}} driver */
|
||||
{% if driver['mbedtls/h_condition'] is defined -%}
|
||||
#if {{ driver['mbedtls/h_condition'] }}
|
||||
{% endif -%}
|
||||
{% for header in driver.headers -%}
|
||||
#include "{{ header }}"
|
||||
{% endfor %}
|
||||
{% if driver['mbedtls/h_condition'] is defined -%}
|
||||
#endif
|
||||
#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
|
||||
#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
|
||||
#endif
|
||||
#include "test/drivers/test_driver.h"
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
|
||||
/* Repeat above block for each JSON-declared driver during autogeneration */
|
||||
{% endif -%}
|
||||
{% endfor %}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
|
||||
/* END-driver headers */
|
||||
|
||||
/* Auto-generated values depending on which drivers are registered.
|
||||
* ID 0 is reserved for unallocated operations.
|
||||
* ID 1 is reserved for the Mbed TLS software driver. */
|
||||
/* BEGIN-driver id definition */
|
||||
#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
|
||||
{% for driver in drivers -%}
|
||||
#define {{(driver.prefix + "_" + driver.type + "_driver_id").upper()}} ({{ loop.index + 1 }})
|
||||
{% endfor %}
|
||||
/* END-driver id */
|
||||
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
|
||||
#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
/* BEGIN-Common Macro definitions */
|
||||
{% macro entry_point_name(capability, entry_point, driver) -%}
|
||||
{% if capability.name is defined and entry_point in capability.names.keys() -%}
|
||||
{{ capability.names[entry_point]}}
|
||||
{% else -%}
|
||||
{{driver.prefix}}_{{driver.type}}_{{entry_point}}
|
||||
{% endif -%}
|
||||
{% endmacro %}
|
||||
/* END-Common Macro definitions */
|
||||
|
||||
/* Support the 'old' SE interface when asked to */
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
|
||||
@@ -277,7 +291,7 @@ psa_status_t psa_driver_wrapper_sign_hash(
|
||||
alg, hash, hash_length,
|
||||
signature, signature_size, signature_length ) );
|
||||
}
|
||||
#endif /* PSA_CRYPTO_SE_C */
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
||||
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_key_location_t location =
|
||||
@@ -361,7 +375,7 @@ psa_status_t psa_driver_wrapper_verify_hash(
|
||||
alg, hash, hash_length,
|
||||
signature, signature_length ) );
|
||||
}
|
||||
#endif /* PSA_CRYPTO_SE_C */
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
||||
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_key_location_t location =
|
||||
@@ -592,6 +606,16 @@ psa_status_t psa_driver_wrapper_import_key(
|
||||
size_t *key_buffer_length,
|
||||
size_t *bits )
|
||||
{
|
||||
{% with entry_point = "import_key" -%}
|
||||
{% macro entry_point_param(driver) -%}
|
||||
attributes,
|
||||
data,
|
||||
data_length,
|
||||
key_buffer,
|
||||
key_buffer_size,
|
||||
key_buffer_length,
|
||||
bits
|
||||
{% endmacro %}
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
|
||||
psa_get_key_lifetime( attributes ) );
|
||||
@@ -623,7 +647,7 @@ psa_status_t psa_driver_wrapper_import_key(
|
||||
|
||||
return( PSA_SUCCESS );
|
||||
}
|
||||
#endif /* PSA_CRYPTO_SE_C */
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
||||
|
||||
switch( location )
|
||||
{
|
||||
@@ -631,17 +655,11 @@ psa_status_t psa_driver_wrapper_import_key(
|
||||
/* Key is stored in the slot in export representation, so
|
||||
* cycle through all known transparent accelerators */
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
status = mbedtls_test_transparent_import_key(
|
||||
attributes,
|
||||
data, data_length,
|
||||
key_buffer, key_buffer_size,
|
||||
key_buffer_length, bits );
|
||||
/* Declared with fallback == true */
|
||||
if( status != PSA_ERROR_NOT_SUPPORTED )
|
||||
return( status );
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
{% with nest_indent=12 %}
|
||||
{% include "OS-template-transparent.jinja" -%}
|
||||
{% endwith -%}
|
||||
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
|
||||
|
||||
/* Fell through, meaning no accelerator supports this operation */
|
||||
return( psa_import_key_into_slot( attributes,
|
||||
data, data_length,
|
||||
@@ -649,20 +667,15 @@ psa_status_t psa_driver_wrapper_import_key(
|
||||
key_buffer_length, bits ) );
|
||||
/* Add cases for opaque driver here */
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TEST_DRIVER_LOCATION:
|
||||
return( mbedtls_test_opaque_import_key(
|
||||
attributes,
|
||||
data, data_length,
|
||||
key_buffer, key_buffer_size,
|
||||
key_buffer_length, bits ) );
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
{% with nest_indent=8 %}
|
||||
{% include "OS-template-opaque.jinja" -%}
|
||||
{% endwith -%}
|
||||
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
|
||||
default:
|
||||
(void)status;
|
||||
return( PSA_ERROR_INVALID_ARGUMENT );
|
||||
}
|
||||
|
||||
{% endwith %}
|
||||
}
|
||||
|
||||
psa_status_t psa_driver_wrapper_export_key(
|
||||
@@ -671,6 +684,15 @@ psa_status_t psa_driver_wrapper_export_key(
|
||||
uint8_t *data, size_t data_size, size_t *data_length )
|
||||
|
||||
{
|
||||
{% with entry_point = "export_key" -%}
|
||||
{% macro entry_point_param(driver) -%}
|
||||
attributes,
|
||||
key_buffer,
|
||||
key_buffer_size,
|
||||
data,
|
||||
data_size,
|
||||
data_length
|
||||
{% endmacro %}
|
||||
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
|
||||
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
|
||||
psa_get_key_lifetime( attributes ) );
|
||||
@@ -693,7 +715,7 @@ psa_status_t psa_driver_wrapper_export_key(
|
||||
*( (psa_key_slot_number_t *)key_buffer ),
|
||||
data, data_size, data_length ) );
|
||||
}
|
||||
#endif /* PSA_CRYPTO_SE_C */
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
|
||||
|
||||
switch( location )
|
||||
{
|
||||
@@ -707,20 +729,15 @@ psa_status_t psa_driver_wrapper_export_key(
|
||||
|
||||
/* Add cases for opaque driver here */
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TEST_DRIVER_LOCATION:
|
||||
return( mbedtls_test_opaque_export_key( attributes,
|
||||
key_buffer,
|
||||
key_buffer_size,
|
||||
data,
|
||||
data_size,
|
||||
data_length ) );
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
{% with nest_indent=8 %}
|
||||
{% include "OS-template-opaque.jinja" -%}
|
||||
{% endwith -%}
|
||||
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
|
||||
default:
|
||||
/* Key is declared with a lifetime not known to us */
|
||||
return( status );
|
||||
}
|
||||
{% endwith %}
|
||||
}
|
||||
|
||||
psa_status_t psa_driver_wrapper_export_public_key(
|
||||
@@ -729,6 +746,15 @@ psa_status_t psa_driver_wrapper_export_public_key(
|
||||
uint8_t *data, size_t data_size, size_t *data_length )
|
||||
|
||||
{
|
||||
{% with entry_point = "export_public_key" -%}
|
||||
{% macro entry_point_param(driver) -%}
|
||||
attributes,
|
||||
key_buffer,
|
||||
key_buffer_size,
|
||||
data,
|
||||
data_size,
|
||||
data_length
|
||||
{% endmacro %}
|
||||
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
|
||||
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
|
||||
psa_get_key_lifetime( attributes ) );
|
||||
@@ -759,18 +785,9 @@ psa_status_t psa_driver_wrapper_export_public_key(
|
||||
/* Key is stored in the slot in export representation, so
|
||||
* cycle through all known transparent accelerators */
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
status = mbedtls_test_transparent_export_public_key(
|
||||
attributes,
|
||||
key_buffer,
|
||||
key_buffer_size,
|
||||
data,
|
||||
data_size,
|
||||
data_length );
|
||||
/* Declared with fallback == true */
|
||||
if( status != PSA_ERROR_NOT_SUPPORTED )
|
||||
return( status );
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
{% with nest_indent=12 %}
|
||||
{% include "OS-template-transparent.jinja" -%}
|
||||
{% endwith -%}
|
||||
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
|
||||
/* Fell through, meaning no accelerator supports this operation */
|
||||
return( psa_export_public_key_internal( attributes,
|
||||
@@ -782,20 +799,15 @@ psa_status_t psa_driver_wrapper_export_public_key(
|
||||
|
||||
/* Add cases for opaque driver here */
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TEST_DRIVER_LOCATION:
|
||||
return( mbedtls_test_opaque_export_public_key( attributes,
|
||||
key_buffer,
|
||||
key_buffer_size,
|
||||
data,
|
||||
data_size,
|
||||
data_length ) );
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
{% with nest_indent=8 %}
|
||||
{% include "OS-template-opaque.jinja" -%}
|
||||
{% endwith -%}
|
||||
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
|
||||
default:
|
||||
/* Key is declared with a lifetime not known to us */
|
||||
return( status );
|
||||
}
|
||||
{% endwith %}
|
||||
}
|
||||
|
||||
psa_status_t psa_driver_wrapper_get_builtin_key(
|
||||
@@ -803,15 +815,21 @@ psa_status_t psa_driver_wrapper_get_builtin_key(
|
||||
psa_key_attributes_t *attributes,
|
||||
uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
|
||||
{
|
||||
{% with entry_point = "get_builtin_key" -%}
|
||||
{% macro entry_point_param(driver) -%}
|
||||
slot_number,
|
||||
attributes,
|
||||
key_buffer,
|
||||
key_buffer_size,
|
||||
key_buffer_length
|
||||
{% endmacro %}
|
||||
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
|
||||
switch( location )
|
||||
{
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TEST_DRIVER_LOCATION:
|
||||
return( mbedtls_test_opaque_get_builtin_key(
|
||||
slot_number,
|
||||
attributes,
|
||||
key_buffer, key_buffer_size, key_buffer_length ) );
|
||||
{% with nest_indent=8 %}
|
||||
{% include "OS-template-opaque.jinja" -%}
|
||||
{% endwith -%}
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
default:
|
||||
(void) slot_number;
|
||||
@@ -820,6 +838,7 @@ psa_status_t psa_driver_wrapper_get_builtin_key(
|
||||
(void) key_buffer_length;
|
||||
return( PSA_ERROR_DOES_NOT_EXIST );
|
||||
}
|
||||
{% endwith %}
|
||||
}
|
||||
|
||||
psa_status_t psa_driver_wrapper_copy_key(
|
||||
@@ -828,6 +847,15 @@ psa_status_t psa_driver_wrapper_copy_key(
|
||||
uint8_t *target_key_buffer, size_t target_key_buffer_size,
|
||||
size_t *target_key_buffer_length )
|
||||
{
|
||||
{% with entry_point = "copy_key" -%}
|
||||
{% macro entry_point_param(driver) -%}
|
||||
attributes,
|
||||
source_key,
|
||||
source_key_length,
|
||||
target_key_buffer,
|
||||
target_key_buffer_size,
|
||||
target_key_buffer_length
|
||||
{% endmacro %}
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_key_location_t location =
|
||||
PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
|
||||
@@ -846,14 +874,9 @@ psa_status_t psa_driver_wrapper_copy_key(
|
||||
switch( location )
|
||||
{
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TEST_DRIVER_LOCATION:
|
||||
return( mbedtls_test_opaque_copy_key( attributes, source_key,
|
||||
source_key_length,
|
||||
target_key_buffer,
|
||||
target_key_buffer_size,
|
||||
target_key_buffer_length) );
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
{% with nest_indent=8 %}
|
||||
{% include "OS-template-opaque.jinja" -%}
|
||||
{% endwith -%}
|
||||
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
|
||||
default:
|
||||
(void)source_key;
|
||||
@@ -864,6 +887,7 @@ psa_status_t psa_driver_wrapper_copy_key(
|
||||
status = PSA_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
return( status );
|
||||
{% endwith %}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1068,7 +1092,7 @@ psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
|
||||
alg );
|
||||
/* Declared with fallback == true */
|
||||
if( status == PSA_SUCCESS )
|
||||
operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
|
||||
operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
|
||||
|
||||
if( status != PSA_ERROR_NOT_SUPPORTED )
|
||||
return( status );
|
||||
@@ -1100,7 +1124,7 @@ psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
|
||||
alg );
|
||||
|
||||
if( status == PSA_SUCCESS )
|
||||
operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
|
||||
operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID;
|
||||
|
||||
return( status );
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
@@ -1141,7 +1165,7 @@ psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
|
||||
alg );
|
||||
/* Declared with fallback == true */
|
||||
if( status == PSA_SUCCESS )
|
||||
operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
|
||||
operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
|
||||
|
||||
if( status != PSA_ERROR_NOT_SUPPORTED )
|
||||
return( status );
|
||||
@@ -1172,7 +1196,7 @@ psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
|
||||
alg );
|
||||
|
||||
if( status == PSA_SUCCESS )
|
||||
operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
|
||||
operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID;
|
||||
|
||||
return( status );
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
@@ -1204,12 +1228,12 @@ psa_status_t psa_driver_wrapper_cipher_set_iv(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_cipher_set_iv(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
iv, iv_length ) );
|
||||
|
||||
case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
|
||||
return( mbedtls_test_opaque_cipher_set_iv(
|
||||
&operation->ctx.opaque_test_driver_ctx,
|
||||
iv, iv_length ) );
|
||||
@@ -1245,13 +1269,13 @@ psa_status_t psa_driver_wrapper_cipher_update(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_cipher_update(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
input, input_length,
|
||||
output, output_size, output_length ) );
|
||||
|
||||
case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
|
||||
return( mbedtls_test_opaque_cipher_update(
|
||||
&operation->ctx.opaque_test_driver_ctx,
|
||||
input, input_length,
|
||||
@@ -1287,12 +1311,12 @@ psa_status_t psa_driver_wrapper_cipher_finish(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_cipher_finish(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
output, output_size, output_length ) );
|
||||
|
||||
case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
|
||||
return( mbedtls_test_opaque_cipher_finish(
|
||||
&operation->ctx.opaque_test_driver_ctx,
|
||||
output, output_size, output_length ) );
|
||||
@@ -1321,7 +1345,7 @@ psa_status_t psa_driver_wrapper_cipher_abort(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
status = mbedtls_test_transparent_cipher_abort(
|
||||
&operation->ctx.transparent_test_driver_ctx );
|
||||
mbedtls_platform_zeroize(
|
||||
@@ -1329,7 +1353,7 @@ psa_status_t psa_driver_wrapper_cipher_abort(
|
||||
sizeof( operation->ctx.transparent_test_driver_ctx ) );
|
||||
return( status );
|
||||
|
||||
case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
|
||||
status = mbedtls_test_opaque_cipher_abort(
|
||||
&operation->ctx.opaque_test_driver_ctx );
|
||||
mbedtls_platform_zeroize(
|
||||
@@ -1394,7 +1418,7 @@ psa_status_t psa_driver_wrapper_hash_setup(
|
||||
status = mbedtls_test_transparent_hash_setup(
|
||||
&operation->ctx.test_driver_ctx, alg );
|
||||
if( status == PSA_SUCCESS )
|
||||
operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
|
||||
operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
|
||||
|
||||
if( status != PSA_ERROR_NOT_SUPPORTED )
|
||||
return( status );
|
||||
@@ -1429,8 +1453,8 @@ psa_status_t psa_driver_wrapper_hash_clone(
|
||||
&target_operation->ctx.mbedtls_ctx ) );
|
||||
#endif
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
target_operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
|
||||
return( mbedtls_test_transparent_hash_clone(
|
||||
&source_operation->ctx.test_driver_ctx,
|
||||
&target_operation->ctx.test_driver_ctx ) );
|
||||
@@ -1454,7 +1478,7 @@ psa_status_t psa_driver_wrapper_hash_update(
|
||||
input, input_length ) );
|
||||
#endif
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_hash_update(
|
||||
&operation->ctx.test_driver_ctx,
|
||||
input, input_length ) );
|
||||
@@ -1480,7 +1504,7 @@ psa_status_t psa_driver_wrapper_hash_finish(
|
||||
hash, hash_size, hash_length ) );
|
||||
#endif
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_hash_finish(
|
||||
&operation->ctx.test_driver_ctx,
|
||||
hash, hash_size, hash_length ) );
|
||||
@@ -1503,7 +1527,7 @@ psa_status_t psa_driver_wrapper_hash_abort(
|
||||
return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
|
||||
#endif
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_hash_abort(
|
||||
&operation->ctx.test_driver_ctx ) );
|
||||
#endif
|
||||
@@ -1616,22 +1640,6 @@ psa_status_t psa_driver_wrapper_aead_decrypt(
|
||||
}
|
||||
}
|
||||
|
||||
psa_status_t psa_driver_get_tag_len( psa_aead_operation_t *operation,
|
||||
uint8_t *tag_len )
|
||||
{
|
||||
if( operation == NULL || tag_len == NULL )
|
||||
return( PSA_ERROR_INVALID_ARGUMENT );
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
*tag_len = operation->ctx.transparent_test_driver_ctx.tag_length;
|
||||
return ( PSA_SUCCESS );
|
||||
#endif
|
||||
#endif
|
||||
*tag_len = operation->ctx.mbedtls_ctx.tag_length;
|
||||
return ( PSA_SUCCESS );
|
||||
}
|
||||
|
||||
psa_status_t psa_driver_wrapper_aead_encrypt_setup(
|
||||
psa_aead_operation_t *operation,
|
||||
const psa_key_attributes_t *attributes,
|
||||
@@ -1650,7 +1658,7 @@ psa_status_t psa_driver_wrapper_aead_encrypt_setup(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
|
||||
operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
|
||||
status = mbedtls_test_transparent_aead_encrypt_setup(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
attributes, key_buffer, key_buffer_size,
|
||||
@@ -1698,7 +1706,7 @@ psa_status_t psa_driver_wrapper_aead_decrypt_setup(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
|
||||
operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
|
||||
status = mbedtls_test_transparent_aead_decrypt_setup(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
attributes,
|
||||
@@ -1747,7 +1755,7 @@ psa_status_t psa_driver_wrapper_aead_set_nonce(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_aead_set_nonce(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
nonce, nonce_length ) );
|
||||
@@ -1781,7 +1789,7 @@ psa_status_t psa_driver_wrapper_aead_set_lengths(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_aead_set_lengths(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
ad_length, plaintext_length ) );
|
||||
@@ -1815,7 +1823,7 @@ psa_status_t psa_driver_wrapper_aead_update_ad(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_aead_update_ad(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
input, input_length ) );
|
||||
@@ -1853,7 +1861,7 @@ psa_status_t psa_driver_wrapper_aead_update(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_aead_update(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
input, input_length, output, output_size,
|
||||
@@ -1897,7 +1905,7 @@ psa_status_t psa_driver_wrapper_aead_finish(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_aead_finish(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
ciphertext, ciphertext_size,
|
||||
@@ -1961,7 +1969,7 @@ psa_status_t psa_driver_wrapper_aead_verify(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_aead_verify(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
plaintext, plaintext_size,
|
||||
@@ -1995,7 +2003,7 @@ psa_status_t psa_driver_wrapper_aead_abort(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_aead_abort(
|
||||
&operation->ctx.transparent_test_driver_ctx ) );
|
||||
|
||||
@@ -2104,7 +2112,7 @@ psa_status_t psa_driver_wrapper_mac_sign_setup(
|
||||
alg );
|
||||
/* Declared with fallback == true */
|
||||
if( status == PSA_SUCCESS )
|
||||
operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
|
||||
operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
|
||||
|
||||
if( status != PSA_ERROR_NOT_SUPPORTED )
|
||||
return( status );
|
||||
@@ -2135,7 +2143,7 @@ psa_status_t psa_driver_wrapper_mac_sign_setup(
|
||||
alg );
|
||||
|
||||
if( status == PSA_SUCCESS )
|
||||
operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
|
||||
operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID;
|
||||
|
||||
return( status );
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
@@ -2176,7 +2184,7 @@ psa_status_t psa_driver_wrapper_mac_verify_setup(
|
||||
alg );
|
||||
/* Declared with fallback == true */
|
||||
if( status == PSA_SUCCESS )
|
||||
operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
|
||||
operation->id = MBEDTLS_TEST_TRANSPARENT_DRIVER_ID;
|
||||
|
||||
if( status != PSA_ERROR_NOT_SUPPORTED )
|
||||
return( status );
|
||||
@@ -2207,7 +2215,7 @@ psa_status_t psa_driver_wrapper_mac_verify_setup(
|
||||
alg );
|
||||
|
||||
if( status == PSA_SUCCESS )
|
||||
operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
|
||||
operation->id = MBEDTLS_TEST_OPAQUE_DRIVER_ID;
|
||||
|
||||
return( status );
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
@@ -2238,12 +2246,12 @@ psa_status_t psa_driver_wrapper_mac_update(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_mac_update(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
input, input_length ) );
|
||||
|
||||
case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
|
||||
return( mbedtls_test_opaque_mac_update(
|
||||
&operation->ctx.opaque_test_driver_ctx,
|
||||
input, input_length ) );
|
||||
@@ -2272,12 +2280,12 @@ psa_status_t psa_driver_wrapper_mac_sign_finish(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_mac_sign_finish(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
mac, mac_size, mac_length ) );
|
||||
|
||||
case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
|
||||
return( mbedtls_test_opaque_mac_sign_finish(
|
||||
&operation->ctx.opaque_test_driver_ctx,
|
||||
mac, mac_size, mac_length ) );
|
||||
@@ -2306,12 +2314,12 @@ psa_status_t psa_driver_wrapper_mac_verify_finish(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_mac_verify_finish(
|
||||
&operation->ctx.transparent_test_driver_ctx,
|
||||
mac, mac_length ) );
|
||||
|
||||
case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
|
||||
return( mbedtls_test_opaque_mac_verify_finish(
|
||||
&operation->ctx.opaque_test_driver_ctx,
|
||||
mac, mac_length ) );
|
||||
@@ -2336,10 +2344,10 @@ psa_status_t psa_driver_wrapper_mac_abort(
|
||||
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_TRANSPARENT_DRIVER_ID:
|
||||
return( mbedtls_test_transparent_mac_abort(
|
||||
&operation->ctx.transparent_test_driver_ctx ) );
|
||||
case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
|
||||
case MBEDTLS_TEST_OPAQUE_DRIVER_ID:
|
||||
return( mbedtls_test_opaque_mac_abort(
|
||||
&operation->ctx.opaque_test_driver_ctx ) );
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
@@ -2468,4 +2476,72 @@ psa_status_t psa_driver_wrapper_asymmetric_decrypt(
|
||||
}
|
||||
}
|
||||
|
||||
psa_status_t psa_driver_wrapper_key_agreement(
|
||||
const psa_key_attributes_t *attributes,
|
||||
const uint8_t *key_buffer,
|
||||
size_t key_buffer_size,
|
||||
psa_algorithm_t alg,
|
||||
const uint8_t *peer_key,
|
||||
size_t peer_key_length,
|
||||
uint8_t *shared_secret,
|
||||
size_t shared_secret_size,
|
||||
size_t *shared_secret_length
|
||||
)
|
||||
{
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_key_location_t location =
|
||||
PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
|
||||
|
||||
switch( location )
|
||||
{
|
||||
case PSA_KEY_LOCATION_LOCAL_STORAGE:
|
||||
/* Key is stored in the slot in export representation, so
|
||||
* cycle through all known transparent accelerators */
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
status =
|
||||
mbedtls_test_transparent_key_agreement( attributes,
|
||||
key_buffer, key_buffer_size, alg, peer_key,
|
||||
peer_key_length, shared_secret, shared_secret_size,
|
||||
shared_secret_length );
|
||||
if( status != PSA_ERROR_NOT_SUPPORTED )
|
||||
return( status );
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
|
||||
|
||||
/* Software Fallback */
|
||||
status = psa_key_agreement_raw_builtin( attributes,
|
||||
key_buffer,
|
||||
key_buffer_size,
|
||||
alg,
|
||||
peer_key,
|
||||
peer_key_length,
|
||||
shared_secret,
|
||||
shared_secret_size,
|
||||
shared_secret_length );
|
||||
return( status );
|
||||
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
|
||||
#if defined(PSA_CRYPTO_DRIVER_TEST)
|
||||
case PSA_CRYPTO_TEST_DRIVER_LOCATION:
|
||||
return( mbedtls_test_opaque_key_agreement( attributes,
|
||||
key_buffer, key_buffer_size, alg, peer_key,
|
||||
peer_key_length, shared_secret, shared_secret_size,
|
||||
shared_secret_length ) );
|
||||
#endif /* PSA_CRYPTO_DRIVER_TEST */
|
||||
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
|
||||
|
||||
default:
|
||||
(void) attributes;
|
||||
(void) key_buffer;
|
||||
(void) key_buffer_size;
|
||||
(void) peer_key;
|
||||
(void) peer_key_length;
|
||||
(void) shared_secret;
|
||||
(void) shared_secret_size;
|
||||
(void) shared_secret_length;
|
||||
return( PSA_ERROR_NOT_SUPPORTED );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
@@ -25,126 +25,127 @@
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#define mbedtls_snprintf snprintf
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
HEADER_INCLUDED
|
||||
|
||||
const char * mbedtls_high_level_strerr( int error_code )
|
||||
const char *mbedtls_high_level_strerr(int error_code)
|
||||
{
|
||||
int high_level_error_code;
|
||||
|
||||
if( error_code < 0 )
|
||||
if (error_code < 0) {
|
||||
error_code = -error_code;
|
||||
}
|
||||
|
||||
/* Extract the high-level part from the error code. */
|
||||
high_level_error_code = error_code & 0xFF80;
|
||||
|
||||
switch( high_level_error_code )
|
||||
{
|
||||
/* Begin Auto-Generated Code. */
|
||||
HIGH_LEVEL_CODE_CHECKS
|
||||
switch (high_level_error_code) {
|
||||
/* Begin Auto-Generated Code. */
|
||||
HIGH_LEVEL_CODE_CHECKS
|
||||
/* End Auto-Generated Code. */
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return( NULL );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char * mbedtls_low_level_strerr( int error_code )
|
||||
const char *mbedtls_low_level_strerr(int error_code)
|
||||
{
|
||||
int low_level_error_code;
|
||||
|
||||
if( error_code < 0 )
|
||||
if (error_code < 0) {
|
||||
error_code = -error_code;
|
||||
}
|
||||
|
||||
/* Extract the low-level part from the error code. */
|
||||
low_level_error_code = error_code & ~0xFF80;
|
||||
|
||||
switch( low_level_error_code )
|
||||
{
|
||||
/* Begin Auto-Generated Code. */
|
||||
LOW_LEVEL_CODE_CHECKS
|
||||
switch (low_level_error_code) {
|
||||
/* Begin Auto-Generated Code. */
|
||||
LOW_LEVEL_CODE_CHECKS
|
||||
/* End Auto-Generated Code. */
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return( NULL );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void mbedtls_strerror( int ret, char *buf, size_t buflen )
|
||||
void mbedtls_strerror(int ret, char *buf, size_t buflen)
|
||||
{
|
||||
size_t len;
|
||||
int use_ret;
|
||||
const char * high_level_error_description = NULL;
|
||||
const char * low_level_error_description = NULL;
|
||||
const char *high_level_error_description = NULL;
|
||||
const char *low_level_error_description = NULL;
|
||||
|
||||
if( buflen == 0 )
|
||||
if (buflen == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
memset( buf, 0x00, buflen );
|
||||
memset(buf, 0x00, buflen);
|
||||
|
||||
if( ret < 0 )
|
||||
if (ret < 0) {
|
||||
ret = -ret;
|
||||
}
|
||||
|
||||
if( ret & 0xFF80 )
|
||||
{
|
||||
if (ret & 0xFF80) {
|
||||
use_ret = ret & 0xFF80;
|
||||
|
||||
// Translate high level error code.
|
||||
high_level_error_description = mbedtls_high_level_strerr( ret );
|
||||
high_level_error_description = mbedtls_high_level_strerr(ret);
|
||||
|
||||
if( high_level_error_description == NULL )
|
||||
mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret );
|
||||
else
|
||||
mbedtls_snprintf( buf, buflen, "%s", high_level_error_description );
|
||||
if (high_level_error_description == NULL) {
|
||||
mbedtls_snprintf(buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret);
|
||||
} else {
|
||||
mbedtls_snprintf(buf, buflen, "%s", high_level_error_description);
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_SSL_TLS_C)
|
||||
// Early return in case of a fatal error - do not try to translate low
|
||||
// level code.
|
||||
if(use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE))
|
||||
if (use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE)) {
|
||||
return;
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_TLS_C */
|
||||
}
|
||||
|
||||
use_ret = ret & ~0xFF80;
|
||||
|
||||
if( use_ret == 0 )
|
||||
if (use_ret == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If high level code is present, make a concatenation between both
|
||||
// error strings.
|
||||
//
|
||||
len = strlen( buf );
|
||||
len = strlen(buf);
|
||||
|
||||
if( len > 0 )
|
||||
{
|
||||
if( buflen - len < 5 )
|
||||
if (len > 0) {
|
||||
if (buflen - len < 5) {
|
||||
return;
|
||||
}
|
||||
|
||||
mbedtls_snprintf( buf + len, buflen - len, " : " );
|
||||
mbedtls_snprintf(buf + len, buflen - len, " : ");
|
||||
|
||||
buf += len + 3;
|
||||
buflen -= len + 3;
|
||||
}
|
||||
|
||||
// Translate low level error code.
|
||||
low_level_error_description = mbedtls_low_level_strerr( ret );
|
||||
low_level_error_description = mbedtls_low_level_strerr(ret);
|
||||
|
||||
if( low_level_error_description == NULL )
|
||||
mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret );
|
||||
else
|
||||
mbedtls_snprintf( buf, buflen, "%s", low_level_error_description );
|
||||
if (low_level_error_description == NULL) {
|
||||
mbedtls_snprintf(buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret);
|
||||
} else {
|
||||
mbedtls_snprintf(buf, buflen, "%s", low_level_error_description);
|
||||
}
|
||||
}
|
||||
|
||||
#else /* MBEDTLS_ERROR_C */
|
||||
@@ -152,18 +153,19 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
|
||||
/*
|
||||
* Provide a dummy implementation when MBEDTLS_ERROR_C is not defined
|
||||
*/
|
||||
void mbedtls_strerror( int ret, char *buf, size_t buflen )
|
||||
void mbedtls_strerror(int ret, char *buf, size_t buflen)
|
||||
{
|
||||
((void) ret);
|
||||
|
||||
if( buflen > 0 )
|
||||
if (buflen > 0) {
|
||||
buf[0] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_ERROR_C */
|
||||
|
||||
#if defined(MBEDTLS_TEST_HOOKS)
|
||||
void (*mbedtls_test_hook_error_add)( int, int, const char *, int );
|
||||
void (*mbedtls_test_hook_error_add)(int, int, const char *, int);
|
||||
#endif
|
||||
|
||||
#endif /* MBEDTLS_ERROR_C || MBEDTLS_ERROR_STRERROR_DUMMY */
|
||||
|
@@ -21,12 +21,7 @@
|
||||
|
||||
#include "query_config.h"
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
/*
|
||||
* Include all the headers with public APIs in case they define a macro to its
|
||||
@@ -103,11 +98,12 @@
|
||||
*/
|
||||
#define MACRO_EXPANSION_TO_STR(macro) MACRO_NAME_TO_STR(macro)
|
||||
#define MACRO_NAME_TO_STR(macro) \
|
||||
mbedtls_printf( "%s", strlen( #macro "" ) > 0 ? #macro "\n" : "" )
|
||||
mbedtls_printf("%s", strlen( #macro "") > 0 ? #macro "\n" : "")
|
||||
|
||||
#define STRINGIFY(macro) #macro
|
||||
#define OUTPUT_MACRO_NAME_VALUE(macro) mbedtls_printf( #macro "%s\n", \
|
||||
( STRINGIFY(macro) "" )[0] != 0 ? "=" STRINGIFY(macro) : "" )
|
||||
(STRINGIFY(macro) "")[0] != 0 ? "=" STRINGIFY( \
|
||||
macro) : "")
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
/*
|
||||
@@ -122,13 +118,13 @@
|
||||
#pragma warning(disable:4003)
|
||||
#endif /* _MSC_VER */
|
||||
|
||||
int query_config( const char *config )
|
||||
int query_config(const char *config)
|
||||
{
|
||||
CHECK_CONFIG /* If the symbol is not found, return an error */
|
||||
return( 1 );
|
||||
CHECK_CONFIG /* If the symbol is not found, return an error */
|
||||
return 1;
|
||||
}
|
||||
|
||||
void list_config( void )
|
||||
void list_config(void)
|
||||
{
|
||||
LIST_CONFIG
|
||||
}
|
||||
|
@@ -27,28 +27,30 @@
|
||||
|
||||
static const char * const features[] = {
|
||||
#if defined(MBEDTLS_VERSION_FEATURES)
|
||||
FEATURE_DEFINES
|
||||
FEATURE_DEFINES
|
||||
#endif /* MBEDTLS_VERSION_FEATURES */
|
||||
NULL
|
||||
};
|
||||
|
||||
int mbedtls_version_check_feature( const char *feature )
|
||||
int mbedtls_version_check_feature(const char *feature)
|
||||
{
|
||||
const char * const *idx = features;
|
||||
|
||||
if( *idx == NULL )
|
||||
return( -2 );
|
||||
if (*idx == NULL) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
if( feature == NULL )
|
||||
return( -1 );
|
||||
if (feature == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
while( *idx != NULL )
|
||||
{
|
||||
if( !strcmp( *idx, feature ) )
|
||||
return( 0 );
|
||||
while (*idx != NULL) {
|
||||
if (!strcmp(*idx, feature)) {
|
||||
return 0;
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
return( -1 );
|
||||
return -1;
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_VERSION_C */
|
||||
|
@@ -1,6 +1,8 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 11.00
|
||||
# Visual C++ Express 2010
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2013
|
||||
VisualStudioVersion = 12.0.31101.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mbedTLS", "mbedTLS.vcxproj", "{46CF2D25-6A36-4189-B59C-E4815388E554}"
|
||||
EndProject
|
||||
APP_ENTRIES
|
@@ -15,4 +15,5 @@ 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
|
||||
types-jsonschema
|
||||
|
@@ -22,54 +22,194 @@
|
||||
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
from typing import NewType, Dict, Any
|
||||
from traceback import format_tb
|
||||
import argparse
|
||||
import jsonschema
|
||||
import jinja2
|
||||
from mbedtls_dev import build_tree
|
||||
|
||||
def render(template_path: str) -> str:
|
||||
JSONSchema = NewType('JSONSchema', object)
|
||||
# The Driver is an Object, but practically it's indexable and can called a dictionary to
|
||||
# keep MyPy happy till MyPy comes with a more composite type for JsonObjects.
|
||||
Driver = NewType('Driver', dict)
|
||||
|
||||
|
||||
class JsonValidationException(Exception):
|
||||
def __init__(self, message="Json Validation Failed"):
|
||||
self.message = message
|
||||
super().__init__(self.message)
|
||||
|
||||
|
||||
class DriverReaderException(Exception):
|
||||
def __init__(self, message="Driver Reader Failed"):
|
||||
self.message = message
|
||||
super().__init__(self.message)
|
||||
|
||||
|
||||
def render(template_path: str, driver_jsoncontext: list) -> str:
|
||||
"""
|
||||
Render template from the input file.
|
||||
Render template from the input file and driver JSON.
|
||||
"""
|
||||
environment = jinja2.Environment(
|
||||
loader=jinja2.FileSystemLoader(os.path.dirname(template_path)),
|
||||
keep_trailing_newline=True)
|
||||
template = environment.get_template(os.path.basename(template_path))
|
||||
|
||||
return template.render()
|
||||
return template.render(drivers=driver_jsoncontext)
|
||||
|
||||
def generate_driver_wrapper_file(mbedtls_root: str, output_dir: str) -> None:
|
||||
|
||||
def generate_driver_wrapper_file(template_dir: str,
|
||||
output_dir: str,
|
||||
driver_jsoncontext: list) -> None:
|
||||
"""
|
||||
Generate the file psa_crypto_driver_wrapper.c.
|
||||
"""
|
||||
driver_wrapper_template_filename = \
|
||||
os.path.join(mbedtls_root, \
|
||||
"scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja")
|
||||
os.path.join(template_dir, "psa_crypto_driver_wrappers.c.jinja")
|
||||
|
||||
result = render(driver_wrapper_template_filename)
|
||||
result = render(driver_wrapper_template_filename, driver_jsoncontext)
|
||||
|
||||
with open(os.path.join(output_dir, "psa_crypto_driver_wrappers.c"), 'w') as out_file:
|
||||
with open(file=os.path.join(output_dir, "psa_crypto_driver_wrappers.c"),
|
||||
mode='w',
|
||||
encoding='UTF-8') as out_file:
|
||||
out_file.write(result)
|
||||
|
||||
|
||||
def validate_json(driverjson_data: Driver, driverschema_list: dict) -> None:
|
||||
"""
|
||||
Validate the Driver JSON against an appropriate schema
|
||||
the schema passed could be that matching an opaque/ transparent driver.
|
||||
"""
|
||||
driver_type = driverjson_data["type"]
|
||||
driver_prefix = driverjson_data["prefix"]
|
||||
try:
|
||||
_schema = driverschema_list[driver_type]
|
||||
jsonschema.validate(instance=driverjson_data, schema=_schema)
|
||||
except KeyError as err:
|
||||
# This could happen if the driverjson_data.type does not exist in the provided schema list
|
||||
# schemas = {'transparent': transparent_driver_schema, 'opaque': opaque_driver_schema}
|
||||
# Print onto stdout and stderr.
|
||||
print("Unknown Driver type " + driver_type +
|
||||
" for driver " + driver_prefix, str(err))
|
||||
print("Unknown Driver type " + driver_type +
|
||||
" for driver " + driver_prefix, str(err), file=sys.stderr)
|
||||
raise JsonValidationException() from err
|
||||
|
||||
except jsonschema.exceptions.ValidationError as err:
|
||||
# Print onto stdout and stderr.
|
||||
print("Error: Failed to validate data file: {} using schema: {}."
|
||||
"\n Exception Message: \"{}\""
|
||||
" ".format(driverjson_data, _schema, str(err)))
|
||||
print("Error: Failed to validate data file: {} using schema: {}."
|
||||
"\n Exception Message: \"{}\""
|
||||
" ".format(driverjson_data, _schema, str(err)), file=sys.stderr)
|
||||
raise JsonValidationException() from err
|
||||
|
||||
|
||||
def load_driver(schemas: Dict[str, Any], driver_file: str) -> Any:
|
||||
"""loads validated json driver"""
|
||||
with open(file=driver_file, mode='r', encoding='UTF-8') as f:
|
||||
json_data = json.load(f)
|
||||
try:
|
||||
validate_json(json_data, schemas)
|
||||
except JsonValidationException as e:
|
||||
raise DriverReaderException from e
|
||||
return json_data
|
||||
|
||||
|
||||
def load_schemas(mbedtls_root: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Load schemas map
|
||||
"""
|
||||
schema_file_paths = {
|
||||
'transparent': os.path.join(mbedtls_root,
|
||||
'scripts',
|
||||
'data_files',
|
||||
'driver_jsons',
|
||||
'driver_transparent_schema.json'),
|
||||
'opaque': os.path.join(mbedtls_root,
|
||||
'scripts',
|
||||
'data_files',
|
||||
'driver_jsons',
|
||||
'driver_opaque_schema.json')
|
||||
}
|
||||
driver_schema = {}
|
||||
for key, file_path in schema_file_paths.items():
|
||||
with open(file=file_path, mode='r', encoding='UTF-8') as file:
|
||||
driver_schema[key] = json.load(file)
|
||||
return driver_schema
|
||||
|
||||
|
||||
def read_driver_descriptions(mbedtls_root: str,
|
||||
json_directory: str,
|
||||
jsondriver_list: str) -> list:
|
||||
"""
|
||||
Merge driver JSON files into a single ordered JSON after validation.
|
||||
"""
|
||||
driver_schema = load_schemas(mbedtls_root)
|
||||
|
||||
with open(file=os.path.join(json_directory, jsondriver_list),
|
||||
mode='r',
|
||||
encoding='UTF-8') as driver_list_file:
|
||||
driver_list = json.load(driver_list_file)
|
||||
|
||||
return [load_driver(schemas=driver_schema,
|
||||
driver_file=os.path.join(json_directory, driver_file_name))
|
||||
for driver_file_name in driver_list]
|
||||
|
||||
|
||||
def trace_exception(e: Exception, file=sys.stderr) -> None:
|
||||
"""Prints exception trace to the given TextIO handle"""
|
||||
print("Exception: type: %s, message: %s, trace: %s" % (
|
||||
e.__class__, str(e), format_tb(e.__traceback__)
|
||||
), file)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
"""
|
||||
Main with command line arguments.
|
||||
"""
|
||||
def_arg_mbedtls_root = build_tree.guess_mbedtls_root()
|
||||
def_arg_output_dir = os.path.join(def_arg_mbedtls_root, 'library')
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--mbedtls-root', nargs='?', default=def_arg_mbedtls_root,
|
||||
parser.add_argument('--mbedtls-root', default=def_arg_mbedtls_root,
|
||||
help='root directory of mbedtls source code')
|
||||
parser.add_argument('--template-dir',
|
||||
help='directory holding the driver templates')
|
||||
parser.add_argument('--json-dir',
|
||||
help='directory holding the driver JSONs')
|
||||
parser.add_argument('output_directory', nargs='?',
|
||||
default=def_arg_output_dir, help='output file\'s location')
|
||||
help='output file\'s location')
|
||||
args = parser.parse_args()
|
||||
|
||||
mbedtls_root = os.path.abspath(args.mbedtls_root)
|
||||
output_directory = args.output_directory
|
||||
|
||||
generate_driver_wrapper_file(mbedtls_root, output_directory)
|
||||
output_directory = args.output_directory if args.output_directory is not None else \
|
||||
os.path.join(mbedtls_root, 'library')
|
||||
template_directory = args.template_dir if args.template_dir is not None else \
|
||||
os.path.join(mbedtls_root,
|
||||
'scripts',
|
||||
'data_files',
|
||||
'driver_templates')
|
||||
json_directory = args.json_dir if args.json_dir is not None else \
|
||||
os.path.join(mbedtls_root,
|
||||
'scripts',
|
||||
'data_files',
|
||||
'driver_jsons')
|
||||
|
||||
try:
|
||||
# Read and validate list of driver jsons from driverlist.json
|
||||
merged_driver_json = read_driver_descriptions(mbedtls_root,
|
||||
json_directory,
|
||||
'driverlist.json')
|
||||
except DriverReaderException as e:
|
||||
trace_exception(e)
|
||||
return 1
|
||||
generate_driver_wrapper_file(template_directory, output_directory, merged_driver_json)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
@@ -47,12 +47,12 @@ my $error_format_file = $data_dir.'/error.fmt';
|
||||
|
||||
my @low_level_modules = qw( AES ARIA ASN1 BASE64 BIGNUM
|
||||
CAMELLIA CCM CHACHA20 CHACHAPOLY CMAC CTR_DRBG DES
|
||||
ENTROPY ERROR GCM HKDF HMAC_DRBG MD5
|
||||
ENTROPY ERROR GCM HKDF HMAC_DRBG LMS MD5
|
||||
NET OID PADLOCK PBKDF2 PLATFORM POLY1305 RIPEMD160
|
||||
SHA1 SHA256 SHA512 SHA3 THREADING );
|
||||
my @high_level_modules = qw( CIPHER DHM ECP MD
|
||||
PEM PK PKCS12 PKCS5
|
||||
RSA SSL X509 );
|
||||
RSA SSL X509 PKCS7 );
|
||||
|
||||
undef $/;
|
||||
|
||||
@@ -60,11 +60,11 @@ open(FORMAT_FILE, '<:crlf', "$error_format_file") or die "Opening error format f
|
||||
my $error_format = <FORMAT_FILE>;
|
||||
close(FORMAT_FILE);
|
||||
|
||||
my @files = <$include_dir/*.h>;
|
||||
my @files = glob qq("$include_dir/*.h");
|
||||
my @necessary_include_files;
|
||||
my @matches;
|
||||
foreach my $file (@files) {
|
||||
open(FILE, '<:crlf', "$file");
|
||||
open(FILE, '<:crlf', $file) or die("$0: $file: $!");
|
||||
my $content = <FILE>;
|
||||
close FILE;
|
||||
my $found = 0;
|
||||
@@ -136,6 +136,7 @@ foreach my $match (@matches)
|
||||
$define_name = "ASN1_PARSE" if ($define_name eq "ASN1");
|
||||
$define_name = "SSL_TLS" if ($define_name eq "SSL");
|
||||
$define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM");
|
||||
$define_name = "PKCS7" if ($define_name eq "PKCS7");
|
||||
|
||||
my $include_name = $module_name;
|
||||
$include_name =~ tr/A-Z/a-z/;
|
||||
|
@@ -1,9 +1,9 @@
|
||||
#!/usr/bin/env perl
|
||||
|
||||
# Generate main file, individual apps and solution files for MS Visual Studio
|
||||
# 2010
|
||||
# Generate main file, individual apps and solution files for
|
||||
# MS Visual Studio 2013
|
||||
#
|
||||
# Must be run from mbedTLS root or scripts directory.
|
||||
# Must be run from Mbed TLS root or scripts directory.
|
||||
# Takes no argument.
|
||||
#
|
||||
# Copyright The Mbed TLS Contributors
|
||||
@@ -25,12 +25,12 @@ use warnings;
|
||||
use strict;
|
||||
use Digest::MD5 'md5_hex';
|
||||
|
||||
my $vsx_dir = "visualc/VS2010";
|
||||
my $vsx_dir = "visualc/VS2013";
|
||||
my $vsx_ext = "vcxproj";
|
||||
my $vsx_app_tpl_file = "scripts/data_files/vs2010-app-template.$vsx_ext";
|
||||
my $vsx_main_tpl_file = "scripts/data_files/vs2010-main-template.$vsx_ext";
|
||||
my $vsx_app_tpl_file = "scripts/data_files/vs2013-app-template.$vsx_ext";
|
||||
my $vsx_main_tpl_file = "scripts/data_files/vs2013-main-template.$vsx_ext";
|
||||
my $vsx_main_file = "$vsx_dir/mbedTLS.$vsx_ext";
|
||||
my $vsx_sln_tpl_file = "scripts/data_files/vs2010-sln-template.sln";
|
||||
my $vsx_sln_tpl_file = "scripts/data_files/vs2013-sln-template.sln";
|
||||
my $vsx_sln_file = "$vsx_dir/mbedTLS.sln";
|
||||
|
||||
my $programs_dir = 'programs';
|
||||
@@ -58,7 +58,7 @@ my @include_directories = qw(
|
||||
include
|
||||
3rdparty/everest/include/
|
||||
3rdparty/everest/include/everest
|
||||
3rdparty/everest/include/everest/vs2010
|
||||
3rdparty/everest/include/everest/vs2013
|
||||
3rdparty/everest/include/everest/kremlib
|
||||
tests/include
|
||||
);
|
||||
@@ -256,7 +256,7 @@ sub del_vsx_files {
|
||||
sub main {
|
||||
if( ! check_dirs() ) {
|
||||
chdir '..' or die;
|
||||
check_dirs or die "Must but run from mbedTLS root or scripts dir\n";
|
||||
check_dirs or die "Must be run from Mbed TLS root or scripts dir\n";
|
||||
}
|
||||
|
||||
# Remove old files to ensure that, for example, project files from deleted
|
||||
|
81
scripts/lcov.sh
Executable file
81
scripts/lcov.sh
Executable file
@@ -0,0 +1,81 @@
|
||||
#!/bin/sh
|
||||
|
||||
help () {
|
||||
cat <<EOF
|
||||
Usage: $0 [-r]
|
||||
Collect coverage statistics of library code into an HTML report.
|
||||
|
||||
General instructions:
|
||||
1. Build the library with CFLAGS="--coverage -O0 -g3" and link the test
|
||||
programs with LDFLAGS="--coverage".
|
||||
This can be an out-of-tree build.
|
||||
For example (in-tree):
|
||||
make CFLAGS="--coverage -O0 -g3" LDFLAGS="--coverage"
|
||||
Or (out-of-tree):
|
||||
mkdir build-coverage && cd build-coverage &&
|
||||
cmake -D CMAKE_BUILD_TYPE=Coverage .. && make
|
||||
2. Run whatever tests you want.
|
||||
3. Run this script from the parent of the directory containing the library
|
||||
object files and coverage statistics files.
|
||||
4. Browse the coverage report in Coverage/index.html.
|
||||
5. After rework, run "$0 -r", then re-test and run "$0" to get a fresh report.
|
||||
|
||||
Options
|
||||
-r Reset traces. Run this before re-testing to get fresh measurements.
|
||||
EOF
|
||||
}
|
||||
|
||||
# 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.
|
||||
|
||||
set -eu
|
||||
|
||||
# Collect stats and build a HTML report.
|
||||
lcov_library_report () {
|
||||
rm -rf Coverage
|
||||
mkdir Coverage Coverage/tmp
|
||||
lcov --capture --initial --directory library -o Coverage/tmp/files.info
|
||||
lcov --rc lcov_branch_coverage=1 --capture --directory library -o Coverage/tmp/tests.info
|
||||
lcov --rc lcov_branch_coverage=1 --add-tracefile Coverage/tmp/files.info --add-tracefile Coverage/tmp/tests.info -o Coverage/tmp/all.info
|
||||
lcov --rc lcov_branch_coverage=1 --remove Coverage/tmp/all.info -o Coverage/tmp/final.info '*.h'
|
||||
gendesc tests/Descriptions.txt -o Coverage/tmp/descriptions
|
||||
genhtml --title "mbed TLS" --description-file Coverage/tmp/descriptions --keep-descriptions --legend --branch-coverage -o Coverage Coverage/tmp/final.info
|
||||
rm -f Coverage/tmp/*.info Coverage/tmp/descriptions
|
||||
echo "Coverage report in: Coverage/index.html"
|
||||
}
|
||||
|
||||
# Reset the traces to 0.
|
||||
lcov_reset_traces () {
|
||||
# Location with plain make
|
||||
rm -f library/*.gcda
|
||||
# Location with CMake
|
||||
rm -f library/CMakeFiles/*.dir/*.gcda
|
||||
}
|
||||
|
||||
if [ $# -gt 0 ] && [ "$1" = "--help" ]; then
|
||||
help
|
||||
exit
|
||||
fi
|
||||
|
||||
main=lcov_library_report
|
||||
while getopts r OPTLET; do
|
||||
case $OPTLET in
|
||||
r) main=lcov_reset_traces;;
|
||||
*) help 2>&1; exit 120;;
|
||||
esac
|
||||
done
|
||||
shift $((OPTIND - 1))
|
||||
|
||||
"$main" "$@"
|
3
scripts/mbedtls_dev/__init__.py
Normal file
3
scripts/mbedtls_dev/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# This file needs to exist to make mbedtls_dev a package.
|
||||
# Among other things, this allows modules in this directory to make
|
||||
# relative imports.
|
426
scripts/mbedtls_dev/bignum_common.py
Normal file
426
scripts/mbedtls_dev/bignum_common.py
Normal file
@@ -0,0 +1,426 @@
|
||||
"""Common features for bignum in test generation framework."""
|
||||
# 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.
|
||||
|
||||
from abc import abstractmethod
|
||||
import enum
|
||||
from typing import Iterator, List, Tuple, TypeVar, Any
|
||||
from itertools import chain
|
||||
|
||||
from . import test_case
|
||||
from . import test_data_generation
|
||||
from .bignum_data import INPUTS_DEFAULT, MODULI_DEFAULT
|
||||
|
||||
T = TypeVar('T') #pylint: disable=invalid-name
|
||||
|
||||
def invmod(a: int, n: int) -> int:
|
||||
"""Return inverse of a to modulo n.
|
||||
|
||||
Equivalent to pow(a, -1, n) in Python 3.8+. Implementation is equivalent
|
||||
to long_invmod() in CPython.
|
||||
"""
|
||||
b, c = 1, 0
|
||||
while n:
|
||||
q, r = divmod(a, n)
|
||||
a, b, c, n = n, c, b - q*c, r
|
||||
# at this point a is the gcd of the original inputs
|
||||
if a == 1:
|
||||
return b
|
||||
raise ValueError("Not invertible")
|
||||
|
||||
def invmod_positive(a: int, n: int) -> int:
|
||||
"""Return a non-negative inverse of a to modulo n."""
|
||||
inv = invmod(a, n)
|
||||
return inv if inv >= 0 else inv + n
|
||||
|
||||
def hex_to_int(val: str) -> int:
|
||||
"""Implement the syntax accepted by mbedtls_test_read_mpi().
|
||||
|
||||
This is a superset of what is accepted by mbedtls_test_read_mpi_core().
|
||||
"""
|
||||
if val in ['', '-']:
|
||||
return 0
|
||||
return int(val, 16)
|
||||
|
||||
def quote_str(val: str) -> str:
|
||||
return "\"{}\"".format(val)
|
||||
|
||||
def bound_mpi(val: int, bits_in_limb: int) -> int:
|
||||
"""First number exceeding number of limbs needed for given input value."""
|
||||
return bound_mpi_limbs(limbs_mpi(val, bits_in_limb), bits_in_limb)
|
||||
|
||||
def bound_mpi_limbs(limbs: int, bits_in_limb: int) -> int:
|
||||
"""First number exceeding maximum of given number of limbs."""
|
||||
bits = bits_in_limb * limbs
|
||||
return 1 << bits
|
||||
|
||||
def limbs_mpi(val: int, bits_in_limb: int) -> int:
|
||||
"""Return the number of limbs required to store value."""
|
||||
return (val.bit_length() + bits_in_limb - 1) // bits_in_limb
|
||||
|
||||
def combination_pairs(values: List[T]) -> List[Tuple[T, T]]:
|
||||
"""Return all pair combinations from input values."""
|
||||
return [(x, y) for x in values for y in values]
|
||||
|
||||
class OperationCommon(test_data_generation.BaseTest):
|
||||
"""Common features for bignum binary operations.
|
||||
|
||||
This adds functionality common in binary operation tests.
|
||||
|
||||
Attributes:
|
||||
symbol: Symbol to use for the operation in case description.
|
||||
input_values: List of values to use as test case inputs. These are
|
||||
combined to produce pairs of values.
|
||||
input_cases: List of tuples containing pairs of test case inputs. This
|
||||
can be used to implement specific pairs of inputs.
|
||||
unique_combinations_only: Boolean to select if test case combinations
|
||||
must be unique. If True, only A,B or B,A would be included as a test
|
||||
case. If False, both A,B and B,A would be included.
|
||||
input_style: Controls the way how test data is passed to the functions
|
||||
in the generated test cases. "variable" passes them as they are
|
||||
defined in the python source. "arch_split" pads the values with
|
||||
zeroes depending on the architecture/limb size. If this is set,
|
||||
test cases are generated for all architectures.
|
||||
arity: the number of operands for the operation. Currently supported
|
||||
values are 1 and 2.
|
||||
"""
|
||||
symbol = ""
|
||||
input_values = INPUTS_DEFAULT # type: List[str]
|
||||
input_cases = [] # type: List[Any]
|
||||
unique_combinations_only = False
|
||||
input_styles = ["variable", "fixed", "arch_split"] # type: List[str]
|
||||
input_style = "variable" # type: str
|
||||
limb_sizes = [32, 64] # type: List[int]
|
||||
arities = [1, 2]
|
||||
arity = 2
|
||||
suffix = False # for arity = 1, symbol can be prefix (default) or suffix
|
||||
|
||||
def __init__(self, val_a: str, val_b: str = "0", bits_in_limb: int = 32) -> None:
|
||||
self.val_a = val_a
|
||||
self.val_b = val_b
|
||||
# Setting the int versions here as opposed to making them @properties
|
||||
# provides earlier/more robust input validation.
|
||||
self.int_a = hex_to_int(val_a)
|
||||
self.int_b = hex_to_int(val_b)
|
||||
if bits_in_limb not in self.limb_sizes:
|
||||
raise ValueError("Invalid number of bits in limb!")
|
||||
if self.input_style == "arch_split":
|
||||
self.dependencies = ["MBEDTLS_HAVE_INT{:d}".format(bits_in_limb)]
|
||||
self.bits_in_limb = bits_in_limb
|
||||
|
||||
@property
|
||||
def boundary(self) -> int:
|
||||
if self.arity == 1:
|
||||
return self.int_a
|
||||
elif self.arity == 2:
|
||||
return max(self.int_a, self.int_b)
|
||||
raise ValueError("Unsupported number of operands!")
|
||||
|
||||
@property
|
||||
def limb_boundary(self) -> int:
|
||||
return bound_mpi(self.boundary, self.bits_in_limb)
|
||||
|
||||
@property
|
||||
def limbs(self) -> int:
|
||||
return limbs_mpi(self.boundary, self.bits_in_limb)
|
||||
|
||||
@property
|
||||
def hex_digits(self) -> int:
|
||||
return 2 * (self.limbs * self.bits_in_limb // 8)
|
||||
|
||||
def format_arg(self, val: str) -> str:
|
||||
if self.input_style not in self.input_styles:
|
||||
raise ValueError("Unknown input style!")
|
||||
if self.input_style == "variable":
|
||||
return val
|
||||
else:
|
||||
return val.zfill(self.hex_digits)
|
||||
|
||||
def format_result(self, res: int) -> str:
|
||||
res_str = '{:x}'.format(res)
|
||||
return quote_str(self.format_arg(res_str))
|
||||
|
||||
@property
|
||||
def arg_a(self) -> str:
|
||||
return self.format_arg(self.val_a)
|
||||
|
||||
@property
|
||||
def arg_b(self) -> str:
|
||||
if self.arity == 1:
|
||||
raise AttributeError("Operation is unary and doesn't have arg_b!")
|
||||
return self.format_arg(self.val_b)
|
||||
|
||||
def arguments(self) -> List[str]:
|
||||
args = [quote_str(self.arg_a)]
|
||||
if self.arity == 2:
|
||||
args.append(quote_str(self.arg_b))
|
||||
return args + self.result()
|
||||
|
||||
def description(self) -> str:
|
||||
"""Generate a description for the test case.
|
||||
|
||||
If not set, case_description uses the form A `symbol` B, where symbol
|
||||
is used to represent the operation. Descriptions of each value are
|
||||
generated to provide some context to the test case.
|
||||
"""
|
||||
if not self.case_description:
|
||||
if self.arity == 1:
|
||||
format_string = "{1:x} {0}" if self.suffix else "{0} {1:x}"
|
||||
self.case_description = format_string.format(
|
||||
self.symbol, self.int_a
|
||||
)
|
||||
elif self.arity == 2:
|
||||
self.case_description = "{:x} {} {:x}".format(
|
||||
self.int_a, self.symbol, self.int_b
|
||||
)
|
||||
return super().description()
|
||||
|
||||
@property
|
||||
def is_valid(self) -> bool:
|
||||
return True
|
||||
|
||||
@abstractmethod
|
||||
def result(self) -> List[str]:
|
||||
"""Get the result of the operation.
|
||||
|
||||
This could be calculated during initialization and stored as `_result`
|
||||
and then returned, or calculated when the method is called.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def get_value_pairs(cls) -> Iterator[Tuple[str, str]]:
|
||||
"""Generator to yield pairs of inputs.
|
||||
|
||||
Combinations are first generated from all input values, and then
|
||||
specific cases provided.
|
||||
"""
|
||||
if cls.arity == 1:
|
||||
yield from ((a, "0") for a in cls.input_values)
|
||||
elif cls.arity == 2:
|
||||
if cls.unique_combinations_only:
|
||||
yield from combination_pairs(cls.input_values)
|
||||
else:
|
||||
yield from (
|
||||
(a, b)
|
||||
for a in cls.input_values
|
||||
for b in cls.input_values
|
||||
)
|
||||
else:
|
||||
raise ValueError("Unsupported number of operands!")
|
||||
|
||||
@classmethod
|
||||
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
||||
if cls.input_style not in cls.input_styles:
|
||||
raise ValueError("Unknown input style!")
|
||||
if cls.arity not in cls.arities:
|
||||
raise ValueError("Unsupported number of operands!")
|
||||
if cls.input_style == "arch_split":
|
||||
test_objects = (cls(a, b, bits_in_limb=bil)
|
||||
for a, b in cls.get_value_pairs()
|
||||
for bil in cls.limb_sizes)
|
||||
special_cases = (cls(*args, bits_in_limb=bil) # type: ignore
|
||||
for args in cls.input_cases
|
||||
for bil in cls.limb_sizes)
|
||||
else:
|
||||
test_objects = (cls(a, b)
|
||||
for a, b in cls.get_value_pairs())
|
||||
special_cases = (cls(*args) for args in cls.input_cases)
|
||||
yield from (valid_test_object.create_test_case()
|
||||
for valid_test_object in filter(
|
||||
lambda test_object: test_object.is_valid,
|
||||
chain(test_objects, special_cases)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class ModulusRepresentation(enum.Enum):
|
||||
"""Representation selector of a modulus."""
|
||||
# Numerical values aligned with the type mbedtls_mpi_mod_rep_selector
|
||||
INVALID = 0
|
||||
MONTGOMERY = 2
|
||||
OPT_RED = 3
|
||||
|
||||
def symbol(self) -> str:
|
||||
"""The C symbol for this representation selector."""
|
||||
return 'MBEDTLS_MPI_MOD_REP_' + self.name
|
||||
|
||||
@classmethod
|
||||
def supported_representations(cls) -> List['ModulusRepresentation']:
|
||||
"""Return all representations that are supported in positive test cases."""
|
||||
return [cls.MONTGOMERY, cls.OPT_RED]
|
||||
|
||||
|
||||
class ModOperationCommon(OperationCommon):
|
||||
#pylint: disable=abstract-method
|
||||
"""Target for bignum mod_raw test case generation."""
|
||||
moduli = MODULI_DEFAULT # type: List[str]
|
||||
montgomery_form_a = False
|
||||
disallow_zero_a = False
|
||||
|
||||
def __init__(self, val_n: str, val_a: str, val_b: str = "0",
|
||||
bits_in_limb: int = 64) -> None:
|
||||
super().__init__(val_a=val_a, val_b=val_b, bits_in_limb=bits_in_limb)
|
||||
self.val_n = val_n
|
||||
# Setting the int versions here as opposed to making them @properties
|
||||
# provides earlier/more robust input validation.
|
||||
self.int_n = hex_to_int(val_n)
|
||||
|
||||
def to_montgomery(self, val: int) -> int:
|
||||
return (val * self.r) % self.int_n
|
||||
|
||||
def from_montgomery(self, val: int) -> int:
|
||||
return (val * self.r_inv) % self.int_n
|
||||
|
||||
def convert_from_canonical(self, canonical: int,
|
||||
rep: ModulusRepresentation) -> int:
|
||||
"""Convert values from canonical representation to the given representation."""
|
||||
if rep is ModulusRepresentation.MONTGOMERY:
|
||||
return self.to_montgomery(canonical)
|
||||
elif rep is ModulusRepresentation.OPT_RED:
|
||||
return canonical
|
||||
else:
|
||||
raise ValueError('Modulus representation not supported: {}'
|
||||
.format(rep.name))
|
||||
|
||||
@property
|
||||
def boundary(self) -> int:
|
||||
return self.int_n
|
||||
|
||||
@property
|
||||
def arg_a(self) -> str:
|
||||
if self.montgomery_form_a:
|
||||
value_a = self.to_montgomery(self.int_a)
|
||||
else:
|
||||
value_a = self.int_a
|
||||
return self.format_arg('{:x}'.format(value_a))
|
||||
|
||||
@property
|
||||
def arg_n(self) -> str:
|
||||
return self.format_arg(self.val_n)
|
||||
|
||||
def format_arg(self, val: str) -> str:
|
||||
return super().format_arg(val).zfill(self.hex_digits)
|
||||
|
||||
def arguments(self) -> List[str]:
|
||||
return [quote_str(self.arg_n)] + super().arguments()
|
||||
|
||||
@property
|
||||
def r(self) -> int: # pylint: disable=invalid-name
|
||||
l = limbs_mpi(self.int_n, self.bits_in_limb)
|
||||
return bound_mpi_limbs(l, self.bits_in_limb)
|
||||
|
||||
@property
|
||||
def r_inv(self) -> int:
|
||||
return invmod(self.r, self.int_n)
|
||||
|
||||
@property
|
||||
def r2(self) -> int: # pylint: disable=invalid-name
|
||||
return pow(self.r, 2)
|
||||
|
||||
@property
|
||||
def is_valid(self) -> bool:
|
||||
if self.int_a >= self.int_n:
|
||||
return False
|
||||
if self.disallow_zero_a and self.int_a == 0:
|
||||
return False
|
||||
if self.arity == 2 and self.int_b >= self.int_n:
|
||||
return False
|
||||
return True
|
||||
|
||||
def description(self) -> str:
|
||||
"""Generate a description for the test case.
|
||||
|
||||
It uses the form A `symbol` B mod N, where symbol is used to represent
|
||||
the operation.
|
||||
"""
|
||||
|
||||
if not self.case_description:
|
||||
return super().description() + " mod {:x}".format(self.int_n)
|
||||
return super().description()
|
||||
|
||||
@classmethod
|
||||
def input_cases_args(cls) -> Iterator[Tuple[Any, Any, Any]]:
|
||||
if cls.arity == 1:
|
||||
yield from ((n, a, "0") for a, n in cls.input_cases)
|
||||
elif cls.arity == 2:
|
||||
yield from ((n, a, b) for a, b, n in cls.input_cases)
|
||||
else:
|
||||
raise ValueError("Unsupported number of operands!")
|
||||
|
||||
@classmethod
|
||||
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
||||
if cls.input_style not in cls.input_styles:
|
||||
raise ValueError("Unknown input style!")
|
||||
if cls.arity not in cls.arities:
|
||||
raise ValueError("Unsupported number of operands!")
|
||||
if cls.input_style == "arch_split":
|
||||
test_objects = (cls(n, a, b, bits_in_limb=bil)
|
||||
for n in cls.moduli
|
||||
for a, b in cls.get_value_pairs()
|
||||
for bil in cls.limb_sizes)
|
||||
special_cases = (cls(*args, bits_in_limb=bil)
|
||||
for args in cls.input_cases_args()
|
||||
for bil in cls.limb_sizes)
|
||||
else:
|
||||
test_objects = (cls(n, a, b)
|
||||
for n in cls.moduli
|
||||
for a, b in cls.get_value_pairs())
|
||||
special_cases = (cls(*args) for args in cls.input_cases_args())
|
||||
yield from (valid_test_object.create_test_case()
|
||||
for valid_test_object in filter(
|
||||
lambda test_object: test_object.is_valid,
|
||||
chain(test_objects, special_cases)
|
||||
))
|
||||
|
||||
# BEGIN MERGE SLOT 1
|
||||
|
||||
# END MERGE SLOT 1
|
||||
|
||||
# BEGIN MERGE SLOT 2
|
||||
|
||||
# END MERGE SLOT 2
|
||||
|
||||
# BEGIN MERGE SLOT 3
|
||||
|
||||
# END MERGE SLOT 3
|
||||
|
||||
# BEGIN MERGE SLOT 4
|
||||
|
||||
# END MERGE SLOT 4
|
||||
|
||||
# BEGIN MERGE SLOT 5
|
||||
|
||||
# END MERGE SLOT 5
|
||||
|
||||
# BEGIN MERGE SLOT 6
|
||||
|
||||
# END MERGE SLOT 6
|
||||
|
||||
# BEGIN MERGE SLOT 7
|
||||
|
||||
# END MERGE SLOT 7
|
||||
|
||||
# BEGIN MERGE SLOT 8
|
||||
|
||||
# END MERGE SLOT 8
|
||||
|
||||
# BEGIN MERGE SLOT 9
|
||||
|
||||
# END MERGE SLOT 9
|
||||
|
||||
# BEGIN MERGE SLOT 10
|
||||
|
||||
# END MERGE SLOT 10
|
855
scripts/mbedtls_dev/bignum_core.py
Normal file
855
scripts/mbedtls_dev/bignum_core.py
Normal file
@@ -0,0 +1,855 @@
|
||||
"""Framework classes for generation of bignum core test cases."""
|
||||
# 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 random
|
||||
|
||||
from typing import Dict, Iterator, List, Tuple
|
||||
|
||||
from . import test_case
|
||||
from . import test_data_generation
|
||||
from . import bignum_common
|
||||
|
||||
class BignumCoreTarget(test_data_generation.BaseTarget):
|
||||
#pylint: disable=abstract-method, too-few-public-methods
|
||||
"""Target for bignum core test case generation."""
|
||||
target_basename = 'test_suite_bignum_core.generated'
|
||||
|
||||
|
||||
class BignumCoreShiftR(BignumCoreTarget, test_data_generation.BaseTest):
|
||||
"""Test cases for mbedtls_bignum_core_shift_r()."""
|
||||
count = 0
|
||||
test_function = "mpi_core_shift_r"
|
||||
test_name = "Core shift right"
|
||||
|
||||
DATA = [
|
||||
('00', '0', [0, 1, 8]),
|
||||
('01', '1', [0, 1, 2, 8, 64]),
|
||||
('dee5ca1a7ef10a75', '64-bit',
|
||||
list(range(11)) + [31, 32, 33, 63, 64, 65, 71, 72]),
|
||||
('002e7ab0070ad57001', '[leading 0 limb]',
|
||||
[0, 1, 8, 63, 64]),
|
||||
('a1055eb0bb1efa1150ff', '80-bit',
|
||||
[0, 1, 8, 63, 64, 65, 72, 79, 80, 81, 88, 128, 129, 136]),
|
||||
('020100000000000000001011121314151617', '138-bit',
|
||||
[0, 1, 8, 9, 16, 72, 73, 136, 137, 138, 144]),
|
||||
]
|
||||
|
||||
def __init__(self, input_hex: str, descr: str, count: int) -> None:
|
||||
self.input_hex = input_hex
|
||||
self.number_description = descr
|
||||
self.shift_count = count
|
||||
self.result = bignum_common.hex_to_int(input_hex) >> count
|
||||
|
||||
def arguments(self) -> List[str]:
|
||||
return ['"{}"'.format(self.input_hex),
|
||||
str(self.shift_count),
|
||||
'"{:0{}x}"'.format(self.result, len(self.input_hex))]
|
||||
|
||||
def description(self) -> str:
|
||||
return 'Core shift {} >> {}'.format(self.number_description,
|
||||
self.shift_count)
|
||||
|
||||
@classmethod
|
||||
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
||||
for input_hex, descr, counts in cls.DATA:
|
||||
for count in counts:
|
||||
yield cls(input_hex, descr, count).create_test_case()
|
||||
|
||||
class BignumCoreCTLookup(BignumCoreTarget, test_data_generation.BaseTest):
|
||||
"""Test cases for mbedtls_mpi_core_ct_uint_table_lookup()."""
|
||||
test_function = "mpi_core_ct_uint_table_lookup"
|
||||
test_name = "Constant time MPI table lookup"
|
||||
|
||||
bitsizes = [
|
||||
(32, "One limb"),
|
||||
(192, "Smallest curve sized"),
|
||||
(512, "Largest curve sized"),
|
||||
(2048, "Small FF/RSA sized"),
|
||||
(4096, "Large FF/RSA sized"),
|
||||
]
|
||||
|
||||
window_sizes = [0, 1, 2, 3, 4, 5, 6]
|
||||
|
||||
def __init__(self,
|
||||
bitsize: int, descr: str, window_size: int) -> None:
|
||||
self.bitsize = bitsize
|
||||
self.bitsize_description = descr
|
||||
self.window_size = window_size
|
||||
|
||||
def arguments(self) -> List[str]:
|
||||
return [str(self.bitsize), str(self.window_size)]
|
||||
|
||||
def description(self) -> str:
|
||||
return '{} - {} MPI with {} bit window'.format(
|
||||
BignumCoreCTLookup.test_name,
|
||||
self.bitsize_description,
|
||||
self.window_size
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
||||
for bitsize, bitsize_description in cls.bitsizes:
|
||||
for window_size in cls.window_sizes:
|
||||
yield (cls(bitsize, bitsize_description, window_size)
|
||||
.create_test_case())
|
||||
|
||||
|
||||
class BignumCoreAddAndAddIf(BignumCoreTarget, bignum_common.OperationCommon):
|
||||
"""Test cases for bignum core add and add-if."""
|
||||
count = 0
|
||||
symbol = "+"
|
||||
test_function = "mpi_core_add_and_add_if"
|
||||
test_name = "mpi_core_add_and_add_if"
|
||||
input_style = "arch_split"
|
||||
unique_combinations_only = True
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = self.int_a + self.int_b
|
||||
|
||||
carry, result = divmod(result, self.limb_boundary)
|
||||
|
||||
return [
|
||||
self.format_result(result),
|
||||
str(carry)
|
||||
]
|
||||
|
||||
|
||||
class BignumCoreSub(BignumCoreTarget, bignum_common.OperationCommon):
|
||||
"""Test cases for bignum core sub."""
|
||||
count = 0
|
||||
input_style = "arch_split"
|
||||
symbol = "-"
|
||||
test_function = "mpi_core_sub"
|
||||
test_name = "mbedtls_mpi_core_sub"
|
||||
|
||||
def result(self) -> List[str]:
|
||||
if self.int_a >= self.int_b:
|
||||
result = self.int_a - self.int_b
|
||||
carry = 0
|
||||
else:
|
||||
result = self.limb_boundary + self.int_a - self.int_b
|
||||
carry = 1
|
||||
return [
|
||||
self.format_result(result),
|
||||
str(carry)
|
||||
]
|
||||
|
||||
|
||||
class BignumCoreMLA(BignumCoreTarget, bignum_common.OperationCommon):
|
||||
"""Test cases for fixed-size multiply accumulate."""
|
||||
count = 0
|
||||
test_function = "mpi_core_mla"
|
||||
test_name = "mbedtls_mpi_core_mla"
|
||||
|
||||
input_values = [
|
||||
"0", "1", "fffe", "ffffffff", "100000000", "20000000000000",
|
||||
"ffffffffffffffff", "10000000000000000", "1234567890abcdef0",
|
||||
"fffffffffffffffffefefefefefefefe",
|
||||
"100000000000000000000000000000000",
|
||||
"1234567890abcdef01234567890abcdef0",
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||
"1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0",
|
||||
(
|
||||
"4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f"
|
||||
"34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf17"
|
||||
"9298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38"
|
||||
"edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec02507"
|
||||
"6b12b"
|
||||
)
|
||||
] # type: List[str]
|
||||
input_scalars = [
|
||||
"0", "3", "fe", "ff", "ffff", "10000", "ffffffff", "100000000",
|
||||
"7f7f7f7f7f7f7f7f", "8000000000000000", "fffffffffffffffe"
|
||||
] # type: List[str]
|
||||
|
||||
def __init__(self, val_a: str, val_b: str, val_s: str) -> None:
|
||||
super().__init__(val_a, val_b)
|
||||
self.arg_scalar = val_s
|
||||
self.int_scalar = bignum_common.hex_to_int(val_s)
|
||||
if bignum_common.limbs_mpi(self.int_scalar, 32) > 1:
|
||||
self.dependencies = ["MBEDTLS_HAVE_INT64"]
|
||||
|
||||
def arguments(self) -> List[str]:
|
||||
return [
|
||||
bignum_common.quote_str(self.arg_a),
|
||||
bignum_common.quote_str(self.arg_b),
|
||||
bignum_common.quote_str(self.arg_scalar)
|
||||
] + self.result()
|
||||
|
||||
def description(self) -> str:
|
||||
"""Override and add the additional scalar."""
|
||||
if not self.case_description:
|
||||
self.case_description = "0x{} + 0x{} * 0x{}".format(
|
||||
self.arg_a, self.arg_b, self.arg_scalar
|
||||
)
|
||||
return super().description()
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = self.int_a + (self.int_b * self.int_scalar)
|
||||
bound_val = max(self.int_a, self.int_b)
|
||||
bound_4 = bignum_common.bound_mpi(bound_val, 32)
|
||||
bound_8 = bignum_common.bound_mpi(bound_val, 64)
|
||||
carry_4, remainder_4 = divmod(result, bound_4)
|
||||
carry_8, remainder_8 = divmod(result, bound_8)
|
||||
return [
|
||||
"\"{:x}\"".format(remainder_4),
|
||||
"\"{:x}\"".format(carry_4),
|
||||
"\"{:x}\"".format(remainder_8),
|
||||
"\"{:x}\"".format(carry_8)
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def get_value_pairs(cls) -> Iterator[Tuple[str, str]]:
|
||||
"""Generator to yield pairs of inputs.
|
||||
|
||||
Combinations are first generated from all input values, and then
|
||||
specific cases provided.
|
||||
"""
|
||||
yield from super().get_value_pairs()
|
||||
yield from cls.input_cases
|
||||
|
||||
@classmethod
|
||||
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
||||
"""Override for additional scalar input."""
|
||||
for a_value, b_value in cls.get_value_pairs():
|
||||
for s_value in cls.input_scalars:
|
||||
cur_op = cls(a_value, b_value, s_value)
|
||||
yield cur_op.create_test_case()
|
||||
|
||||
|
||||
class BignumCoreMontmul(BignumCoreTarget, test_data_generation.BaseTest):
|
||||
"""Test cases for Montgomery multiplication."""
|
||||
count = 0
|
||||
test_function = "mpi_core_montmul"
|
||||
test_name = "mbedtls_mpi_core_montmul"
|
||||
|
||||
start_2_mpi4 = False
|
||||
start_2_mpi8 = False
|
||||
|
||||
replay_test_cases = [
|
||||
(2, 1, 1, 1, "19", "1", "1D"), (2, 1, 1, 1, "7", "1", "9"),
|
||||
(2, 1, 1, 1, "4", "1", "9"),
|
||||
(
|
||||
12, 1, 6, 1, (
|
||||
"3C246D0E059A93A266288A7718419EC741661B474C58C032C5EDAF92709402"
|
||||
"B07CC8C7CE0B781C641A1EA8DB2F4343"
|
||||
), "1", (
|
||||
"66A198186C18C10B2F5ED9B522752A9830B69916E535C8F047518A889A43A5"
|
||||
"94B6BED27A168D31D4A52F88925AA8F5"
|
||||
)
|
||||
), (
|
||||
8, 1, 4, 1,
|
||||
"1E442976B0E63D64FCCE74B999E470CA9888165CB75BFA1F340E918CE03C6211",
|
||||
"1", "B3A119602EE213CDE28581ECD892E0F592A338655DCE4CA88054B3D124D0E561"
|
||||
), (
|
||||
22, 1, 11, 1, (
|
||||
"7CF5AC97304E0B63C65413F57249F59994B0FED1D2A8D3D83ED5FA38560FFB"
|
||||
"82392870D6D08F87D711917FD7537E13B7E125BE407E74157776839B0AC9DB"
|
||||
"23CBDFC696104353E4D2780B2B4968F8D8542306BCA7A2366E"
|
||||
), "1", (
|
||||
"284139EA19C139EBE09A8111926AAA39A2C2BE12ED487A809D3CB5BC558547"
|
||||
"25B4CDCB5734C58F90B2F60D99CC1950CDBC8D651793E93C9C6F0EAD752500"
|
||||
"A32C56C62082912B66132B2A6AA42ADA923E1AD22CEB7BA0123"
|
||||
)
|
||||
)
|
||||
] # type: List[Tuple[int, int, int, int, str, str, str]]
|
||||
|
||||
random_test_cases = [
|
||||
("2", "2", "3", ""), ("1", "2", "3", ""), ("2", "1", "3", ""),
|
||||
("6", "5", "7", ""), ("3", "4", "7", ""), ("1", "6", "7", ""), ("5", "6", "7", ""),
|
||||
("3", "4", "B", ""), ("7", "4", "B", ""), ("9", "7", "B", ""), ("2", "a", "B", ""),
|
||||
("25", "16", "29", "(0x29 is prime)"), ("8", "28", "29", ""),
|
||||
("18", "21", "29", ""), ("15", "f", "29", ""),
|
||||
("e2", "ea", "FF", ""), ("43", "72", "FF", ""),
|
||||
("d8", "70", "FF", ""), ("3c", "7c", "FF", ""),
|
||||
("99", "b9", "101", "(0x101 is prime)"), ("65", "b2", "101", ""),
|
||||
("81", "32", "101", ""), ("51", "dd", "101", ""),
|
||||
("d5", "143", "38B", "(0x38B is prime)"), ("3d", "387", "38B", ""),
|
||||
("160", "2e5", "38B", ""), ("10f", "137", "38B", ""),
|
||||
("7dac", "25a", "8003", "(0x8003 is prime)"), ("6f1c", "3286", "8003", ""),
|
||||
("59ed", "2f3f", "8003", ""), ("6893", "736d", "8003", ""),
|
||||
("d199", "2832", "10001", "(0x10001 is prime)"), ("c3b2", "3e5b", "10001", ""),
|
||||
("abe4", "214e", "10001", ""), ("4360", "a05d", "10001", ""),
|
||||
("3f5a1", "165b2", "7F7F7", ""), ("3bd29", "37863", "7F7F7", ""),
|
||||
("60c47", "64819", "7F7F7", ""), ("16584", "12c49", "7F7F7", ""),
|
||||
("1ff03f", "610347", "800009", "(0x800009 is prime)"), ("340fd5", "19812e", "800009", ""),
|
||||
("3fe2e8", "4d0dc7", "800009", ""), ("40356", "e6392", "800009", ""),
|
||||
("dd8a1d", "266c0e", "100002B", "(0x100002B is prime)"),
|
||||
("3fa1cb", "847fd6", "100002B", ""), ("5f439d", "5c3196", "100002B", ""),
|
||||
("18d645", "f72dc6", "100002B", ""),
|
||||
("20051ad", "37def6e", "37EEE9D", "(0x37EEE9D is prime)"),
|
||||
("2ec140b", "3580dbf", "37EEE9D", ""), ("1d91b46", "190d4fc", "37EEE9D", ""),
|
||||
("34e488d", "1224d24", "37EEE9D", ""),
|
||||
("2a4fe2cb", "263466a9", "8000000B", "(0x8000000B is prime)"),
|
||||
("5643fe94", "29a1aefa", "8000000B", ""), ("29633513", "7b007ac4", "8000000B", ""),
|
||||
("2439cef5", "5c9d5a47", "8000000B", ""),
|
||||
("4de3cfaa", "50dea178", "8CD626B9", "(0x8CD626B9 is prime)"),
|
||||
("b8b8563", "10dbbbac", "8CD626B9", ""), ("4e8a6151", "5574ec19", "8CD626B9", ""),
|
||||
("69224878", "309cfc23", "8CD626B9", ""),
|
||||
("fb6f7fb6", "afb05423", "10000000F", "(0x10000000F is prime)"),
|
||||
("8391a243", "26034dcd", "10000000F", ""), ("d26b98c", "14b2d6aa", "10000000F", ""),
|
||||
("6b9f1371", "a21daf1d", "10000000F", ""),
|
||||
(
|
||||
"9f49435ad", "c8264ade8", "174876E7E9",
|
||||
"0x174876E7E9 is prime (dec) 99999999977"
|
||||
),
|
||||
("c402da434", "1fb427acf", "174876E7E9", ""),
|
||||
("f6ebc2bb1", "1096d39f2a", "174876E7E9", ""),
|
||||
("153b7f7b6b", "878fda8ff", "174876E7E9", ""),
|
||||
("2c1adbb8d6", "4384d2d3c6", "8000000017", "(0x8000000017 is prime)"),
|
||||
("2e4f9cf5fb", "794f3443d9", "8000000017", ""),
|
||||
("149e495582", "3802b8f7b7", "8000000017", ""),
|
||||
("7b9d49df82", "69c68a442a", "8000000017", ""),
|
||||
("683a134600", "6dd80ea9f6", "864CB9076D", "(0x864CB9076D is prime)"),
|
||||
("13a870ff0d", "59b099694a", "864CB9076D", ""),
|
||||
("37d06b0e63", "4d2147e46f", "864CB9076D", ""),
|
||||
("661714f8f4", "22e55df507", "864CB9076D", ""),
|
||||
("2f0a96363", "52693307b4", "F7F7F7F7F7", ""),
|
||||
("3c85078e64", "f2275ecb6d", "F7F7F7F7F7", ""),
|
||||
("352dae68d1", "707775b4c6", "F7F7F7F7F7", ""),
|
||||
("37ae0f3e0b", "912113040f", "F7F7F7F7F7", ""),
|
||||
("6dada15e31", "f58ed9eff7", "1000000000F", "(0x1000000000F is prime)"),
|
||||
("69627a7c89", "cfb5ebd13d", "1000000000F", ""),
|
||||
("a5e1ad239b", "afc030c731", "1000000000F", ""),
|
||||
("f1cc45f4c5", "c64ad607c8", "1000000000F", ""),
|
||||
("2ebad87d2e31", "4c72d90bca78", "800000000005", "(0x800000000005 is prime)"),
|
||||
("a30b3cc50d", "29ac4fe59490", "800000000005", ""),
|
||||
("33674e9647b4", "5ec7ee7e72d3", "800000000005", ""),
|
||||
("3d956f474f61", "74070040257d", "800000000005", ""),
|
||||
("48348e3717d6", "43fcb4399571", "800795D9BA47", "(0x800795D9BA47 is prime)"),
|
||||
("5234c03cc99b", "2f3cccb87803", "800795D9BA47", ""),
|
||||
("3ed13db194ab", "44b8f4ba7030", "800795D9BA47", ""),
|
||||
("1c11e843bfdb", "95bd1b47b08", "800795D9BA47", ""),
|
||||
("a81d11cb81fd", "1e5753a3f33d", "1000000000015", "(0x1000000000015 is prime)"),
|
||||
("688c4db99232", "36fc0cf7ed", "1000000000015", ""),
|
||||
("f0720cc07e07", "fc76140ed903", "1000000000015", ""),
|
||||
("2ec61f8d17d1", "d270c85e36d2", "1000000000015", ""),
|
||||
(
|
||||
"6a24cd3ab63820", "ed4aad55e5e348", "100000000000051",
|
||||
"(0x100000000000051 is prime)"
|
||||
),
|
||||
("e680c160d3b248", "31e0d8840ed510", "100000000000051", ""),
|
||||
("a80637e9aebc38", "bb81decc4e1738", "100000000000051", ""),
|
||||
("9afa5a59e9d630", "be9e65a6d42938", "100000000000051", ""),
|
||||
("ab5e104eeb71c000", "2cffbd639e9fea00", "ABCDEF0123456789", ""),
|
||||
("197b867547f68a00", "44b796cf94654800", "ABCDEF0123456789", ""),
|
||||
("329f9483a04f2c00", "9892f76961d0f000", "ABCDEF0123456789", ""),
|
||||
("4a2e12dfb4545000", "1aa3e89a69794500", "ABCDEF0123456789", ""),
|
||||
(
|
||||
"8b9acdf013d140f000", "12e4ceaefabdf2b2f00", "25A55A46E5DA99C71C7",
|
||||
"0x25A55A46E5DA99C71C7 is the 3rd repunit prime(dec) 11111111111111111111111"
|
||||
),
|
||||
("1b8d960ea277e3f5500", "14418aa980e37dd000", "25A55A46E5DA99C71C7", ""),
|
||||
("7314524977e8075980", "8172fa45618ccd0d80", "25A55A46E5DA99C71C7", ""),
|
||||
("ca14f031769be63580", "147a2f3cf2964ca9400", "25A55A46E5DA99C71C7", ""),
|
||||
(
|
||||
"18532ba119d5cd0cf39735c0000", "25f9838e31634844924733000000",
|
||||
"314DC643FB763F2B8C0E2DE00879",
|
||||
"0x314DC643FB763F2B8C0E2DE00879 is (dec)99999999977^3"
|
||||
),
|
||||
(
|
||||
"a56e2d2517519e3970e70c40000", "ec27428d4bb380458588fa80000",
|
||||
"314DC643FB763F2B8C0E2DE00879", ""
|
||||
),
|
||||
(
|
||||
"1cb5e8257710e8653fff33a00000", "15fdd42fe440fd3a1d121380000",
|
||||
"314DC643FB763F2B8C0E2DE00879", ""
|
||||
),
|
||||
(
|
||||
"e50d07a65fc6f93e538ce040000", "1f4b059ca609f3ce597f61240000",
|
||||
"314DC643FB763F2B8C0E2DE00879", ""
|
||||
),
|
||||
(
|
||||
"1ea3ade786a095d978d387f30df9f20000000",
|
||||
"127c448575f04af5a367a7be06c7da0000000",
|
||||
"47BF19662275FA2F6845C74942ED1D852E521",
|
||||
"0x47BF19662275FA2F6845C74942ED1D852E521 is (dec) 99999999977^4"
|
||||
),
|
||||
(
|
||||
"16e15b0ca82764e72e38357b1f10a20000000",
|
||||
"43e2355d8514bbe22b0838fdc3983a0000000",
|
||||
"47BF19662275FA2F6845C74942ED1D852E521", ""
|
||||
),
|
||||
(
|
||||
"be39332529d93f25c3d116c004c620000000",
|
||||
"5cccec42370a0a2c89c6772da801a0000000",
|
||||
"47BF19662275FA2F6845C74942ED1D852E521", ""
|
||||
),
|
||||
(
|
||||
"ecaa468d90de0eeda474d39b3e1fc0000000",
|
||||
"1e714554018de6dc0fe576bfd3b5660000000",
|
||||
"47BF19662275FA2F6845C74942ED1D852E521", ""
|
||||
),
|
||||
(
|
||||
"32298816711c5dce46f9ba06e775c4bedfc770e6700000000000000",
|
||||
"8ee751fd5fb24f0b4a653cb3a0c8b7d9e724574d168000000000000",
|
||||
"97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931",
|
||||
(
|
||||
"0x97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931"
|
||||
" is (dec) 99999999977^6"
|
||||
)
|
||||
),
|
||||
(
|
||||
"29213b9df3cfd15f4b428645b67b677c29d1378d810000000000000",
|
||||
"6cbb732c65e10a28872394dfdd1936d5171c3c3aac0000000000000",
|
||||
"97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", ""
|
||||
),
|
||||
(
|
||||
"6f18db06ad4abc52c0c50643dd13098abccd4a232f0000000000000",
|
||||
"7e6bf41f2a86098ad51f98dfc10490ba3e8081bc830000000000000",
|
||||
"97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", ""
|
||||
),
|
||||
(
|
||||
"62d3286cd706ad9d73caff63f1722775d7e8c731208000000000000",
|
||||
"530f7ba02ae2b04c2fe3e3d27ec095925631a6c2528000000000000",
|
||||
"97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931", ""
|
||||
),
|
||||
(
|
||||
"a6c6503e3c031fdbf6009a89ed60582b7233c5a85de28b16000000000000000",
|
||||
"75c8ed18270b583f16d442a467d32bf95c5e491e9b8523798000000000000000",
|
||||
"DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499",
|
||||
(
|
||||
"0xDD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499"
|
||||
" is (dec) 99999999977^7"
|
||||
)
|
||||
),
|
||||
(
|
||||
"bf84d1f85cf6b51e04d2c8f4ffd03532d852053cf99b387d4000000000000000",
|
||||
"397ba5a743c349f4f28bc583ecd5f06e0a25f9c6d98f09134000000000000000",
|
||||
"DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", ""
|
||||
),
|
||||
(
|
||||
"6db11c3a4152ed1a2aa6fa34b0903ec82ea1b88908dcb482000000000000000",
|
||||
"ac8ac576a74ad6ca48f201bf89f77350ce86e821358d85920000000000000000",
|
||||
"DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", ""
|
||||
),
|
||||
(
|
||||
"3001d96d7fe8b733f33687646fc3017e3ac417eb32e0ec708000000000000000",
|
||||
"925ddbdac4174e8321a48a32f79640e8cf7ec6f46ea235a80000000000000000",
|
||||
"DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499", ""
|
||||
),
|
||||
(
|
||||
"1029048755f2e60dd98c8de6d9989226b6bb4f0db8e46bd1939de560000000000000000000",
|
||||
"51bb7270b2e25cec0301a03e8275213bb6c2f6e6ec93d4d46d36ca0000000000000000000",
|
||||
"141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41",
|
||||
(
|
||||
"0x141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146"
|
||||
"380E41 is 99999999977^8"
|
||||
)
|
||||
),
|
||||
(
|
||||
"1c5337ff982b3ad6611257dbff5bbd7a9920ba2d4f5838a0cc681ce000000000000000000",
|
||||
"520c5d049ca4702031ba728591b665c4d4ccd3b2b86864d4c160fd2000000000000000000",
|
||||
"141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41",
|
||||
""
|
||||
),
|
||||
(
|
||||
"57074dfa00e42f6555bae624b7f0209f218adf57f73ed34ab0ff90c000000000000000000",
|
||||
"41eb14b6c07bfd3d1fe4f4a610c17cc44fcfcda695db040e011065000000000000000000",
|
||||
"141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41",
|
||||
""
|
||||
),
|
||||
(
|
||||
"d8ed7feed2fe855e6997ad6397f776158573d425031bf085a615784000000000000000000",
|
||||
"6f121dcd18c578ab5e229881006007bb6d319b179f11015fe958b9c000000000000000000",
|
||||
"141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41",
|
||||
""
|
||||
),
|
||||
(
|
||||
(
|
||||
"2a462b156180ea5fe550d3758c764e06fae54e626b5f503265a09df76edbdfbf"
|
||||
"a1e6000000000000000000000000"
|
||||
), (
|
||||
"1136f41d1879fd4fb9e49e0943a46b6704d77c068ee237c3121f9071cfd3e6a0"
|
||||
"0315800000000000000000000000"
|
||||
), (
|
||||
"2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC90"
|
||||
"2713E40F51E3B3C214EDFABC451"
|
||||
), (
|
||||
"0x2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC"
|
||||
"902713E40F51E3B3C214EDFABC451 is (dec) 99999999977^10"
|
||||
)
|
||||
),
|
||||
(
|
||||
(
|
||||
"c1ac3800dfb3c6954dea391d206200cf3c47f795bf4a5603b4cb88ae7e574de47"
|
||||
"40800000000000000000000000"
|
||||
), (
|
||||
"c0d16eda0549ede42fa0deb4635f7b7ce061fadea02ee4d85cba4c4f709603419"
|
||||
"3c800000000000000000000000"
|
||||
), (
|
||||
"2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC90"
|
||||
"2713E40F51E3B3C214EDFABC451"
|
||||
), ""
|
||||
),
|
||||
(
|
||||
(
|
||||
"19e45bb7633094d272588ad2e43bcb3ee341991c6731b6fa9d47c4018d7ce7bba"
|
||||
"5ee800000000000000000000000"
|
||||
), (
|
||||
"1e4f83166ae59f6b9cc8fd3e7677ed8bfc01bb99c98bd3eb084246b64c1e18c33"
|
||||
"65b800000000000000000000000"
|
||||
), (
|
||||
"2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC90"
|
||||
"2713E40F51E3B3C214EDFABC451"
|
||||
), ""
|
||||
),
|
||||
(
|
||||
(
|
||||
"1aa93395fad5f9b7f20b8f9028a054c0bb7c11bb8520e6a95e5a34f06cb70bcdd"
|
||||
"01a800000000000000000000000"
|
||||
), (
|
||||
"54b45afa5d4310192f8d224634242dd7dcfb342318df3d9bd37b4c614788ba13b"
|
||||
"8b000000000000000000000000"
|
||||
), (
|
||||
"2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E2833EC90"
|
||||
"2713E40F51E3B3C214EDFABC451"
|
||||
), ""
|
||||
),
|
||||
(
|
||||
(
|
||||
"544f2628a28cfb5ce0a1b7180ee66b49716f1d9476c466c57f0c4b23089917843"
|
||||
"06d48f78686115ee19e25400000000000000000000000000000000"
|
||||
), (
|
||||
"677eb31ef8d66c120fa872a60cd47f6e10cbfdf94f90501bd7883cba03d185be0"
|
||||
"a0148d1625745e9c4c827300000000000000000000000000000000"
|
||||
), (
|
||||
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA1"
|
||||
"1DABD6E6144BEF37C6800000000000000000000000000000000051"
|
||||
), (
|
||||
"0x8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBF"
|
||||
"A11DABD6E6144BEF37C6800000000000000000000000000000000051 is prime,"
|
||||
" (dec) 10^143 + 3^4"
|
||||
)
|
||||
),
|
||||
(
|
||||
(
|
||||
"76bb3470985174915e9993522aec989666908f9e8cf5cb9f037bf4aee33d8865c"
|
||||
"b6464174795d07e30015b80000000000000000000000000000000"
|
||||
), (
|
||||
"6aaaf60d5784dcef612d133613b179a317532ecca0eed40b8ad0c01e6d4a6d8c7"
|
||||
"9a52af190abd51739009a900000000000000000000000000000000"
|
||||
), (
|
||||
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA1"
|
||||
"1DABD6E6144BEF37C6800000000000000000000000000000000051"
|
||||
), ""
|
||||
),
|
||||
(
|
||||
(
|
||||
"6cfdd6e60912e441d2d1fc88f421b533f0103a5322ccd3f4db84861643ad63fd6"
|
||||
"3d1d8cfbc1d498162786ba00000000000000000000000000000000"
|
||||
), (
|
||||
"1177246ec5e93814816465e7f8f248b350d954439d35b2b5d75d917218e7fd5fb"
|
||||
"4c2f6d0667f9467fdcf33400000000000000000000000000000000"
|
||||
), (
|
||||
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA1"
|
||||
"1DABD6E6144BEF37C6800000000000000000000000000000000051"
|
||||
), ""
|
||||
),
|
||||
(
|
||||
(
|
||||
"7a09a0b0f8bbf8057116fb0277a9bdf3a91b5eaa8830d448081510d8973888be5"
|
||||
"a9f0ad04facb69aa3715f00000000000000000000000000000000"
|
||||
), (
|
||||
"764dec6c05a1c0d87b649efa5fd94c91ea28bffb4725d4ab4b33f1a3e8e3b314d"
|
||||
"799020e244a835a145ec9800000000000000000000000000000000"
|
||||
), (
|
||||
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA1"
|
||||
"1DABD6E6144BEF37C6800000000000000000000000000000000051"
|
||||
), ""
|
||||
)
|
||||
] # type: List[Tuple[str, str, str, str]]
|
||||
|
||||
def __init__(
|
||||
self, val_a: str, val_b: str, val_n: str, case_description: str = ""
|
||||
):
|
||||
self.case_description = case_description
|
||||
self.arg_a = val_a
|
||||
self.int_a = bignum_common.hex_to_int(val_a)
|
||||
self.arg_b = val_b
|
||||
self.int_b = bignum_common.hex_to_int(val_b)
|
||||
self.arg_n = val_n
|
||||
self.int_n = bignum_common.hex_to_int(val_n)
|
||||
|
||||
limbs_a4 = bignum_common.limbs_mpi(self.int_a, 32)
|
||||
limbs_a8 = bignum_common.limbs_mpi(self.int_a, 64)
|
||||
self.limbs_b4 = bignum_common.limbs_mpi(self.int_b, 32)
|
||||
self.limbs_b8 = bignum_common.limbs_mpi(self.int_b, 64)
|
||||
self.limbs_an4 = bignum_common.limbs_mpi(self.int_n, 32)
|
||||
self.limbs_an8 = bignum_common.limbs_mpi(self.int_n, 64)
|
||||
|
||||
if limbs_a4 > self.limbs_an4 or limbs_a8 > self.limbs_an8:
|
||||
raise Exception("Limbs of input A ({}) exceeds N ({})".format(
|
||||
self.arg_a, self.arg_n
|
||||
))
|
||||
|
||||
def arguments(self) -> List[str]:
|
||||
return [
|
||||
str(self.limbs_an4), str(self.limbs_b4),
|
||||
str(self.limbs_an8), str(self.limbs_b8),
|
||||
bignum_common.quote_str(self.arg_a),
|
||||
bignum_common.quote_str(self.arg_b),
|
||||
bignum_common.quote_str(self.arg_n)
|
||||
] + self.result()
|
||||
|
||||
def description(self) -> str:
|
||||
if self.case_description != "replay":
|
||||
if not self.start_2_mpi4 and self.limbs_an4 > 1:
|
||||
tmp = "(start of 2-MPI 4-byte bignums) "
|
||||
self.__class__.start_2_mpi4 = True
|
||||
elif not self.start_2_mpi8 and self.limbs_an8 > 1:
|
||||
tmp = "(start of 2-MPI 8-byte bignums) "
|
||||
self.__class__.start_2_mpi8 = True
|
||||
else:
|
||||
tmp = "(gen) "
|
||||
self.case_description = tmp + self.case_description
|
||||
return super().description()
|
||||
|
||||
def result(self) -> List[str]:
|
||||
"""Get the result of the operation."""
|
||||
r4 = bignum_common.bound_mpi_limbs(self.limbs_an4, 32)
|
||||
i4 = bignum_common.invmod(r4, self.int_n)
|
||||
x4 = self.int_a * self.int_b * i4
|
||||
x4 = x4 % self.int_n
|
||||
|
||||
r8 = bignum_common.bound_mpi_limbs(self.limbs_an8, 64)
|
||||
i8 = bignum_common.invmod(r8, self.int_n)
|
||||
x8 = self.int_a * self.int_b * i8
|
||||
x8 = x8 % self.int_n
|
||||
return [
|
||||
"\"{:x}\"".format(x4),
|
||||
"\"{:x}\"".format(x8)
|
||||
]
|
||||
|
||||
def set_limbs(
|
||||
self, limbs_an4: int, limbs_b4: int, limbs_an8: int, limbs_b8: int
|
||||
) -> None:
|
||||
"""Set number of limbs for each input.
|
||||
|
||||
Replaces default values set during initialization.
|
||||
"""
|
||||
self.limbs_an4 = limbs_an4
|
||||
self.limbs_b4 = limbs_b4
|
||||
self.limbs_an8 = limbs_an8
|
||||
self.limbs_b8 = limbs_b8
|
||||
|
||||
@classmethod
|
||||
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
||||
"""Generate replay and randomly generated test cases."""
|
||||
# Test cases which replay captured invocations during unit test runs.
|
||||
for limbs_an4, limbs_b4, limbs_an8, limbs_b8, a, b, n in cls.replay_test_cases:
|
||||
cur_op = cls(a, b, n, case_description="replay")
|
||||
cur_op.set_limbs(limbs_an4, limbs_b4, limbs_an8, limbs_b8)
|
||||
yield cur_op.create_test_case()
|
||||
# Random test cases can be generated using mpi_modmul_case_generate()
|
||||
# Uses a mixture of primes and odd numbers as N, with four randomly
|
||||
# generated cases for each N.
|
||||
for a, b, n, description in cls.random_test_cases:
|
||||
cur_op = cls(a, b, n, case_description=description)
|
||||
yield cur_op.create_test_case()
|
||||
|
||||
|
||||
def mpi_modmul_case_generate() -> None:
|
||||
"""Generate valid inputs for montmul tests using moduli.
|
||||
|
||||
For each modulus, generates random values for A and B and simple descriptions
|
||||
for the test case.
|
||||
"""
|
||||
moduli = [
|
||||
("3", ""), ("7", ""), ("B", ""), ("29", ""), ("FF", ""),
|
||||
("101", ""), ("38B", ""), ("8003", ""), ("10001", ""),
|
||||
("7F7F7", ""), ("800009", ""), ("100002B", ""), ("37EEE9D", ""),
|
||||
("8000000B", ""), ("8CD626B9", ""), ("10000000F", ""),
|
||||
("174876E7E9", "is prime (dec) 99999999977"),
|
||||
("8000000017", ""), ("864CB9076D", ""), ("F7F7F7F7F7", ""),
|
||||
("1000000000F", ""), ("800000000005", ""), ("800795D9BA47", ""),
|
||||
("1000000000015", ""), ("100000000000051", ""), ("ABCDEF0123456789", ""),
|
||||
(
|
||||
"25A55A46E5DA99C71C7",
|
||||
"is the 3rd repunit prime (dec) 11111111111111111111111"
|
||||
),
|
||||
("314DC643FB763F2B8C0E2DE00879", "is (dec)99999999977^3"),
|
||||
("47BF19662275FA2F6845C74942ED1D852E521", "is (dec) 99999999977^4"),
|
||||
(
|
||||
"97EDD86E4B5C4592C6D32064AC55C888A7245F07CA3CC455E07C931",
|
||||
"is (dec) 99999999977^6"
|
||||
),
|
||||
(
|
||||
"DD15FE80B731872AC104DB37832F7E75A244AA2631BC87885B861E8F20375499",
|
||||
"is (dec) 99999999977^7"
|
||||
),
|
||||
(
|
||||
"141B8EBD9009F84C241879A1F680FACCED355DA36C498F73E96E880CF78EA5F96146380E41",
|
||||
"is (dec) 99999999977^8"
|
||||
),
|
||||
(
|
||||
(
|
||||
"2A94608DE88B6D5E9F8920F5ABB06B24CC35AE1FBACC87D075C621C3E283"
|
||||
"3EC902713E40F51E3B3C214EDFABC451"
|
||||
),
|
||||
"is (dec) 99999999977^10"
|
||||
),
|
||||
(
|
||||
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11"
|
||||
"DABD6E6144BEF37C6800000000000000000000000000000000051",
|
||||
"is prime, (dec) 10^143 + 3^4"
|
||||
)
|
||||
] # type: List[Tuple[str, str]]
|
||||
primes = [
|
||||
"3", "7", "B", "29", "101", "38B", "8003", "10001", "800009",
|
||||
"100002B", "37EEE9D", "8000000B", "8CD626B9",
|
||||
# From here they require > 1 4-byte MPI
|
||||
"10000000F", "174876E7E9", "8000000017", "864CB9076D", "1000000000F",
|
||||
"800000000005", "800795D9BA47", "1000000000015", "100000000000051",
|
||||
# From here they require > 1 8-byte MPI
|
||||
"25A55A46E5DA99C71C7", # this is 11111111111111111111111 decimal
|
||||
# 10^143 + 3^4: (which is prime)
|
||||
# 100000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
# 000000000000000000000000000000000000000000000000000000000000000081
|
||||
(
|
||||
"8335616AED761F1F7F44E6BD49E807B82E3BF2BF11BFA6AF813C808DBF33DBFA11"
|
||||
"DABD6E6144BEF37C6800000000000000000000000000000000051"
|
||||
)
|
||||
] # type: List[str]
|
||||
generated_inputs = []
|
||||
for mod, description in moduli:
|
||||
n = bignum_common.hex_to_int(mod)
|
||||
mod_read = "{:x}".format(n)
|
||||
case_count = 3 if n < 5 else 4
|
||||
cases = {} # type: Dict[int, int]
|
||||
i = 0
|
||||
while i < case_count:
|
||||
a = random.randint(1, n)
|
||||
b = random.randint(1, n)
|
||||
if cases.get(a) == b:
|
||||
continue
|
||||
cases[a] = b
|
||||
if description:
|
||||
out_description = "0x{} {}".format(mod_read, description)
|
||||
elif i == 0 and len(mod) > 1 and mod in primes:
|
||||
out_description = "(0x{} is prime)"
|
||||
else:
|
||||
out_description = ""
|
||||
generated_inputs.append(
|
||||
("{:x}".format(a), "{:x}".format(b), mod, out_description)
|
||||
)
|
||||
i += 1
|
||||
print(generated_inputs)
|
||||
|
||||
# BEGIN MERGE SLOT 1
|
||||
|
||||
class BignumCoreExpMod(BignumCoreTarget, bignum_common.ModOperationCommon):
|
||||
"""Test cases for bignum core exponentiation."""
|
||||
symbol = "^"
|
||||
test_function = "mpi_core_exp_mod"
|
||||
test_name = "Core modular exponentiation (Mongtomery form only)"
|
||||
input_style = "fixed"
|
||||
montgomery_form_a = True
|
||||
|
||||
def result(self) -> List[str]:
|
||||
# Result has to be given in Montgomery form too
|
||||
result = pow(self.int_a, self.int_b, self.int_n)
|
||||
mont_result = self.to_montgomery(result)
|
||||
return [self.format_result(mont_result)]
|
||||
|
||||
@property
|
||||
def is_valid(self) -> bool:
|
||||
# The base needs to be canonical, but the exponent can be larger than
|
||||
# the modulus (see for example exponent blinding)
|
||||
return bool(self.int_a < self.int_n)
|
||||
|
||||
# END MERGE SLOT 1
|
||||
|
||||
# BEGIN MERGE SLOT 2
|
||||
|
||||
# END MERGE SLOT 2
|
||||
|
||||
# BEGIN MERGE SLOT 3
|
||||
|
||||
class BignumCoreSubInt(BignumCoreTarget, bignum_common.OperationCommon):
|
||||
"""Test cases for bignum core sub int."""
|
||||
count = 0
|
||||
symbol = "-"
|
||||
test_function = "mpi_core_sub_int"
|
||||
test_name = "mpi_core_sub_int"
|
||||
input_style = "arch_split"
|
||||
|
||||
@property
|
||||
def is_valid(self) -> bool:
|
||||
# This is "sub int", so b is only one limb
|
||||
if bignum_common.limbs_mpi(self.int_b, self.bits_in_limb) > 1:
|
||||
return False
|
||||
return True
|
||||
|
||||
# Overriding because we don't want leading zeros on b
|
||||
@property
|
||||
def arg_b(self) -> str:
|
||||
return self.val_b
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = self.int_a - self.int_b
|
||||
|
||||
borrow, result = divmod(result, self.limb_boundary)
|
||||
|
||||
# Borrow will be -1 if non-zero, but we want it to be 1 in the test data
|
||||
return [
|
||||
self.format_result(result),
|
||||
str(-borrow)
|
||||
]
|
||||
|
||||
class BignumCoreZeroCheckCT(BignumCoreTarget, bignum_common.OperationCommon):
|
||||
"""Test cases for bignum core zero check (constant flow)."""
|
||||
count = 0
|
||||
symbol = "== 0"
|
||||
test_function = "mpi_core_check_zero_ct"
|
||||
test_name = "mpi_core_check_zero_ct"
|
||||
input_style = "variable"
|
||||
arity = 1
|
||||
suffix = True
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = 1 if self.int_a == 0 else 0
|
||||
return [str(result)]
|
||||
|
||||
# END MERGE SLOT 3
|
||||
|
||||
# BEGIN MERGE SLOT 4
|
||||
|
||||
# END MERGE SLOT 4
|
||||
|
||||
# BEGIN MERGE SLOT 5
|
||||
|
||||
# END MERGE SLOT 5
|
||||
|
||||
# BEGIN MERGE SLOT 6
|
||||
|
||||
# END MERGE SLOT 6
|
||||
|
||||
# BEGIN MERGE SLOT 7
|
||||
|
||||
# END MERGE SLOT 7
|
||||
|
||||
# BEGIN MERGE SLOT 8
|
||||
|
||||
# END MERGE SLOT 8
|
||||
|
||||
# BEGIN MERGE SLOT 9
|
||||
|
||||
# END MERGE SLOT 9
|
||||
|
||||
# BEGIN MERGE SLOT 10
|
||||
|
||||
# END MERGE SLOT 10
|
147
scripts/mbedtls_dev/bignum_data.py
Normal file
147
scripts/mbedtls_dev/bignum_data.py
Normal file
@@ -0,0 +1,147 @@
|
||||
"""Base values and datasets for bignum generated tests and helper functions that
|
||||
produced them."""
|
||||
# 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 random
|
||||
|
||||
# Functions calling these were used to produce test data and are here only for
|
||||
# reproducibility, they are not used by the test generation framework/classes
|
||||
try:
|
||||
from Cryptodome.Util.number import isPrime, getPrime #type: ignore #pylint: disable=import-error
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
# Generated by bignum_common.gen_safe_prime(192,1)
|
||||
SAFE_PRIME_192_BIT_SEED_1 = "d1c127a667786703830500038ebaef20e5a3e2dc378fb75b"
|
||||
|
||||
# First number generated by random.getrandbits(192) - seed(2,2), not a prime
|
||||
RANDOM_192_BIT_SEED_2_NO1 = "177219d30e7a269fd95bafc8f2a4d27bdcf4bb99f4bea973"
|
||||
|
||||
# Second number generated by random.getrandbits(192) - seed(2,2), not a prime
|
||||
RANDOM_192_BIT_SEED_2_NO2 = "cf1822ffbc6887782b491044d5e341245c6e433715ba2bdd"
|
||||
|
||||
# Third number generated by random.getrandbits(192) - seed(2,2), not a prime
|
||||
RANDOM_192_BIT_SEED_2_NO3 = "3653f8dd9b1f282e4067c3584ee207f8da94e3e8ab73738f"
|
||||
|
||||
# Fourth number generated by random.getrandbits(192) - seed(2,2), not a prime
|
||||
RANDOM_192_BIT_SEED_2_NO4 = "ffed9235288bc781ae66267594c9c9500925e4749b575bd1"
|
||||
|
||||
# Ninth number generated by random.getrandbits(192) - seed(2,2), not a prime
|
||||
RANDOM_192_BIT_SEED_2_NO9 = "2a1be9cd8697bbd0e2520e33e44c50556c71c4a66148a86f"
|
||||
|
||||
# Generated by bignum_common.gen_safe_prime(1024,3)
|
||||
SAFE_PRIME_1024_BIT_SEED_3 = ("c93ba7ec74d96f411ba008bdb78e63ff11bb5df46a51e16b"
|
||||
"2c9d156f8e4e18abf5e052cb01f47d0d1925a77f60991577"
|
||||
"e128fb6f52f34a27950a594baadd3d8057abeb222cf3cca9"
|
||||
"62db16abf79f2ada5bd29ab2f51244bf295eff9f6aaba130"
|
||||
"2efc449b128be75eeaca04bc3c1a155d11d14e8be32a2c82"
|
||||
"87b3996cf6ad5223")
|
||||
|
||||
# First number generated by random.getrandbits(1024) - seed(4,2), not a prime
|
||||
RANDOM_1024_BIT_SEED_4_NO1 = ("6905269ed6f0b09f165c8ce36e2f24b43000de01b2ed40ed"
|
||||
"3addccb2c33be0ac79d679346d4ac7a5c3902b38963dc6e8"
|
||||
"534f45738d048ec0f1099c6c3e1b258fd724452ccea71ff4"
|
||||
"a14876aeaff1a098ca5996666ceab360512bd13110722311"
|
||||
"710cf5327ac435a7a97c643656412a9b8a1abcd1a6916c74"
|
||||
"da4f9fc3c6da5d7")
|
||||
|
||||
# Second number generated by random.getrandbits(1024) - seed(4,2), not a prime
|
||||
RANDOM_1024_BIT_SEED_4_NO2 = ("f1cfd99216df648647adec26793d0e453f5082492d83a823"
|
||||
"3fb62d2c81862fc9634f806fabf4a07c566002249b191bf4"
|
||||
"d8441b5616332aca5f552773e14b0190d93936e1daca3c06"
|
||||
"f5ff0c03bb5d7385de08caa1a08179104a25e4664f5253a0"
|
||||
"2a3187853184ff27459142deccea264542a00403ce80c4b0"
|
||||
"a4042bb3d4341aad")
|
||||
|
||||
# Third number generated by random.getrandbits(1024) - seed(4,2), not a prime
|
||||
RANDOM_1024_BIT_SEED_4_NO3 = ("14c15c910b11ad28cc21ce88d0060cc54278c2614e1bcb38"
|
||||
"3bb4a570294c4ea3738d243a6e58d5ca49c7b59b995253fd"
|
||||
"6c79a3de69f85e3131f3b9238224b122c3e4a892d9196ada"
|
||||
"4fcfa583e1df8af9b474c7e89286a1754abcb06ae8abb93f"
|
||||
"01d89a024cdce7a6d7288ff68c320f89f1347e0cdd905ecf"
|
||||
"d160c5d0ef412ed6")
|
||||
|
||||
# Fourth number generated by random.getrandbits(1024) - seed(4,2), not a prime
|
||||
RANDOM_1024_BIT_SEED_4_NO4 = ("32decd6b8efbc170a26a25c852175b7a96b98b5fbf37a2be"
|
||||
"6f98bca35b17b9662f0733c846bbe9e870ef55b1a1f65507"
|
||||
"a2909cb633e238b4e9dd38b869ace91311021c9e32111ac1"
|
||||
"ac7cc4a4ff4dab102522d53857c49391b36cc9aa78a330a1"
|
||||
"a5e333cb88dcf94384d4cd1f47ca7883ff5a52f1a05885ac"
|
||||
"7671863c0bdbc23a")
|
||||
|
||||
# Fifth number generated by random.getrandbits(1024) - seed(4,2), not a prime
|
||||
RANDOM_1024_BIT_SEED_4_NO5 = ("53be4721f5b9e1f5acdac615bc20f6264922b9ccf469aef8"
|
||||
"f6e7d078e55b85dd1525f363b281b8885b69dc230af5ac87"
|
||||
"0692b534758240df4a7a03052d733dcdef40af2e54c0ce68"
|
||||
"1f44ebd13cc75f3edcb285f89d8cf4d4950b16ffc3e1ac3b"
|
||||
"4708d9893a973000b54a23020fc5b043d6e4a51519d9c9cc"
|
||||
"52d32377e78131c1")
|
||||
|
||||
# Adding 192 bit and 1024 bit numbers because these are the shortest required
|
||||
# for ECC and RSA respectively.
|
||||
INPUTS_DEFAULT = [
|
||||
"0", "1", # corner cases
|
||||
"2", "3", # small primes
|
||||
"4", # non-prime even
|
||||
"38", # small random
|
||||
SAFE_PRIME_192_BIT_SEED_1, # prime
|
||||
RANDOM_192_BIT_SEED_2_NO1, # not a prime
|
||||
RANDOM_192_BIT_SEED_2_NO2, # not a prime
|
||||
SAFE_PRIME_1024_BIT_SEED_3, # prime
|
||||
RANDOM_1024_BIT_SEED_4_NO1, # not a prime
|
||||
RANDOM_1024_BIT_SEED_4_NO3, # not a prime
|
||||
RANDOM_1024_BIT_SEED_4_NO2, # largest (not a prime)
|
||||
]
|
||||
|
||||
# Only odd moduli are present as in the new bignum code only odd moduli are
|
||||
# supported for now.
|
||||
MODULI_DEFAULT = [
|
||||
"53", # safe prime
|
||||
"45", # non-prime
|
||||
SAFE_PRIME_192_BIT_SEED_1, # safe prime
|
||||
RANDOM_192_BIT_SEED_2_NO4, # not a prime
|
||||
SAFE_PRIME_1024_BIT_SEED_3, # safe prime
|
||||
RANDOM_1024_BIT_SEED_4_NO5, # not a prime
|
||||
]
|
||||
|
||||
# Some functions, e.g. mbedtls_mpi_mod_raw_inv_prime(), only support prime moduli.
|
||||
ONLY_PRIME_MODULI = [
|
||||
"53", # safe prime
|
||||
"8ac72304057392b5", # 9999999997777777333 (longer, not safe, prime)
|
||||
# The next prime has a different R in Montgomery form depending on
|
||||
# whether 32- or 64-bit MPIs are used.
|
||||
"152d02c7e14af67fe0bf", # 99999999999999999991999
|
||||
SAFE_PRIME_192_BIT_SEED_1, # safe prime
|
||||
SAFE_PRIME_1024_BIT_SEED_3, # safe prime
|
||||
]
|
||||
|
||||
def __gen_safe_prime(bits, seed):
|
||||
'''
|
||||
Generate a safe prime.
|
||||
|
||||
This function is intended for generating constants offline and shouldn't be
|
||||
used in test generation classes.
|
||||
|
||||
Requires pycryptodomex for getPrime and isPrime and python 3.9 or later for
|
||||
randbytes.
|
||||
'''
|
||||
rng = random.Random()
|
||||
# We want reproducibility across python versions
|
||||
rng.seed(seed, version=2)
|
||||
while True:
|
||||
prime = 2*getPrime(bits-1, rng.randbytes)+1 #pylint: disable=no-member
|
||||
if isPrime(prime, 1e-30):
|
||||
return prime
|
150
scripts/mbedtls_dev/bignum_mod.py
Normal file
150
scripts/mbedtls_dev/bignum_mod.py
Normal file
@@ -0,0 +1,150 @@
|
||||
"""Framework classes for generation of bignum mod test cases."""
|
||||
# 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.
|
||||
|
||||
from typing import Dict, List
|
||||
|
||||
from . import test_data_generation
|
||||
from . import bignum_common
|
||||
from .bignum_data import ONLY_PRIME_MODULI
|
||||
|
||||
class BignumModTarget(test_data_generation.BaseTarget):
|
||||
#pylint: disable=abstract-method, too-few-public-methods
|
||||
"""Target for bignum mod test case generation."""
|
||||
target_basename = 'test_suite_bignum_mod.generated'
|
||||
|
||||
# BEGIN MERGE SLOT 1
|
||||
|
||||
# END MERGE SLOT 1
|
||||
|
||||
# BEGIN MERGE SLOT 2
|
||||
|
||||
class BignumModMul(bignum_common.ModOperationCommon,
|
||||
BignumModTarget):
|
||||
# pylint:disable=duplicate-code
|
||||
"""Test cases for bignum mpi_mod_mul()."""
|
||||
symbol = "*"
|
||||
test_function = "mpi_mod_mul"
|
||||
test_name = "mbedtls_mpi_mod_mul"
|
||||
input_style = "arch_split"
|
||||
arity = 2
|
||||
|
||||
def arguments(self) -> List[str]:
|
||||
return [self.format_result(self.to_montgomery(self.int_a)),
|
||||
self.format_result(self.to_montgomery(self.int_b)),
|
||||
bignum_common.quote_str(self.arg_n)
|
||||
] + self.result()
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = (self.int_a * self.int_b) % self.int_n
|
||||
return [self.format_result(self.to_montgomery(result))]
|
||||
|
||||
# END MERGE SLOT 2
|
||||
|
||||
# BEGIN MERGE SLOT 3
|
||||
|
||||
class BignumModSub(bignum_common.ModOperationCommon, BignumModTarget):
|
||||
"""Test cases for bignum mpi_mod_sub()."""
|
||||
symbol = "-"
|
||||
test_function = "mpi_mod_sub"
|
||||
test_name = "mbedtls_mpi_mod_sub"
|
||||
input_style = "fixed"
|
||||
arity = 2
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = (self.int_a - self.int_b) % self.int_n
|
||||
# To make negative tests easier, append 0 for success to the
|
||||
# generated cases
|
||||
return [self.format_result(result), "0"]
|
||||
|
||||
class BignumModInvNonMont(bignum_common.ModOperationCommon, BignumModTarget):
|
||||
"""Test cases for bignum mpi_mod_inv() - not in Montgomery form."""
|
||||
moduli = ONLY_PRIME_MODULI # for now only prime moduli supported
|
||||
symbol = "^ -1"
|
||||
test_function = "mpi_mod_inv_non_mont"
|
||||
test_name = "mbedtls_mpi_mod_inv non-Mont. form"
|
||||
input_style = "fixed"
|
||||
arity = 1
|
||||
suffix = True
|
||||
disallow_zero_a = True
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = bignum_common.invmod_positive(self.int_a, self.int_n)
|
||||
# To make negative tests easier, append 0 for success to the
|
||||
# generated cases
|
||||
return [self.format_result(result), "0"]
|
||||
|
||||
class BignumModInvMont(bignum_common.ModOperationCommon, BignumModTarget):
|
||||
"""Test cases for bignum mpi_mod_inv() - Montgomery form."""
|
||||
moduli = ONLY_PRIME_MODULI # for now only prime moduli supported
|
||||
symbol = "^ -1"
|
||||
test_function = "mpi_mod_inv_mont"
|
||||
test_name = "mbedtls_mpi_mod_inv Mont. form"
|
||||
input_style = "arch_split" # Mont. form requires arch_split
|
||||
arity = 1
|
||||
suffix = True
|
||||
disallow_zero_a = True
|
||||
montgomery_form_a = True
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = bignum_common.invmod_positive(self.int_a, self.int_n)
|
||||
mont_result = self.to_montgomery(result)
|
||||
# To make negative tests easier, append 0 for success to the
|
||||
# generated cases
|
||||
return [self.format_result(mont_result), "0"]
|
||||
|
||||
# END MERGE SLOT 3
|
||||
|
||||
# BEGIN MERGE SLOT 4
|
||||
|
||||
# END MERGE SLOT 4
|
||||
|
||||
# BEGIN MERGE SLOT 5
|
||||
class BignumModAdd(bignum_common.ModOperationCommon, BignumModTarget):
|
||||
"""Test cases for bignum mpi_mod_add()."""
|
||||
count = 0
|
||||
symbol = "+"
|
||||
test_function = "mpi_mod_add"
|
||||
test_name = "mbedtls_mpi_mod_add"
|
||||
input_style = "fixed"
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = (self.int_a + self.int_b) % self.int_n
|
||||
# To make negative tests easier, append "0" for success to the
|
||||
# generated cases
|
||||
return [self.format_result(result), "0"]
|
||||
|
||||
|
||||
# END MERGE SLOT 5
|
||||
|
||||
# BEGIN MERGE SLOT 6
|
||||
|
||||
# END MERGE SLOT 6
|
||||
|
||||
# BEGIN MERGE SLOT 7
|
||||
|
||||
# END MERGE SLOT 7
|
||||
|
||||
# BEGIN MERGE SLOT 8
|
||||
|
||||
# END MERGE SLOT 8
|
||||
|
||||
# BEGIN MERGE SLOT 9
|
||||
|
||||
# END MERGE SLOT 9
|
||||
|
||||
# BEGIN MERGE SLOT 10
|
||||
|
||||
# END MERGE SLOT 10
|
287
scripts/mbedtls_dev/bignum_mod_raw.py
Normal file
287
scripts/mbedtls_dev/bignum_mod_raw.py
Normal file
@@ -0,0 +1,287 @@
|
||||
"""Framework classes for generation of bignum mod_raw test cases."""
|
||||
# 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.
|
||||
|
||||
from typing import Iterator, List
|
||||
|
||||
from . import test_case
|
||||
from . import test_data_generation
|
||||
from . import bignum_common
|
||||
from .bignum_data import ONLY_PRIME_MODULI
|
||||
|
||||
class BignumModRawTarget(test_data_generation.BaseTarget):
|
||||
#pylint: disable=abstract-method, too-few-public-methods
|
||||
"""Target for bignum mod_raw test case generation."""
|
||||
target_basename = 'test_suite_bignum_mod_raw.generated'
|
||||
|
||||
# BEGIN MERGE SLOT 1
|
||||
|
||||
# END MERGE SLOT 1
|
||||
|
||||
# BEGIN MERGE SLOT 2
|
||||
|
||||
class BignumModRawSub(bignum_common.ModOperationCommon,
|
||||
BignumModRawTarget):
|
||||
"""Test cases for bignum mpi_mod_raw_sub()."""
|
||||
symbol = "-"
|
||||
test_function = "mpi_mod_raw_sub"
|
||||
test_name = "mbedtls_mpi_mod_raw_sub"
|
||||
input_style = "fixed"
|
||||
arity = 2
|
||||
|
||||
def arguments(self) -> List[str]:
|
||||
return [bignum_common.quote_str(n) for n in [self.arg_a,
|
||||
self.arg_b,
|
||||
self.arg_n]
|
||||
] + self.result()
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = (self.int_a - self.int_b) % self.int_n
|
||||
return [self.format_result(result)]
|
||||
|
||||
class BignumModRawFixQuasiReduction(bignum_common.ModOperationCommon,
|
||||
BignumModRawTarget):
|
||||
"""Test cases for ecp quasi_reduction()."""
|
||||
symbol = "-"
|
||||
test_function = "mpi_mod_raw_fix_quasi_reduction"
|
||||
test_name = "fix_quasi_reduction"
|
||||
input_style = "fixed"
|
||||
arity = 1
|
||||
|
||||
# Extend the default values with n < x < 2n
|
||||
input_values = bignum_common.ModOperationCommon.input_values + [
|
||||
"73",
|
||||
|
||||
# First number generated by random.getrandbits(1024) - seed(3,2)
|
||||
"ea7b5bf55eb561a4216363698b529b4a97b750923ceb3ffd",
|
||||
|
||||
# First number generated by random.getrandbits(1024) - seed(1,2)
|
||||
("cd447e35b8b6d8fe442e3d437204e52db2221a58008a05a6c4647159c324c985"
|
||||
"9b810e766ec9d28663ca828dd5f4b3b2e4b06ce60741c7a87ce42c8218072e8c"
|
||||
"35bf992dc9e9c616612e7696a6cecc1b78e510617311d8a3c2ce6f447ed4d57b"
|
||||
"1e2feb89414c343c1027c4d1c386bbc4cd613e30d8f16adf91b7584a2265b1f5")
|
||||
] # type: List[str]
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = self.int_a % self.int_n
|
||||
return [self.format_result(result)]
|
||||
|
||||
@property
|
||||
def is_valid(self) -> bool:
|
||||
return bool(self.int_a < 2 * self.int_n)
|
||||
|
||||
class BignumModRawMul(bignum_common.ModOperationCommon,
|
||||
BignumModRawTarget):
|
||||
"""Test cases for bignum mpi_mod_raw_mul()."""
|
||||
symbol = "*"
|
||||
test_function = "mpi_mod_raw_mul"
|
||||
test_name = "mbedtls_mpi_mod_raw_mul"
|
||||
input_style = "arch_split"
|
||||
arity = 2
|
||||
|
||||
def arguments(self) -> List[str]:
|
||||
return [self.format_result(self.to_montgomery(self.int_a)),
|
||||
self.format_result(self.to_montgomery(self.int_b)),
|
||||
bignum_common.quote_str(self.arg_n)
|
||||
] + self.result()
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = (self.int_a * self.int_b) % self.int_n
|
||||
return [self.format_result(self.to_montgomery(result))]
|
||||
|
||||
# END MERGE SLOT 2
|
||||
|
||||
# BEGIN MERGE SLOT 3
|
||||
|
||||
class BignumModRawInvPrime(bignum_common.ModOperationCommon,
|
||||
BignumModRawTarget):
|
||||
"""Test cases for bignum mpi_mod_raw_inv_prime()."""
|
||||
moduli = ONLY_PRIME_MODULI
|
||||
symbol = "^ -1"
|
||||
test_function = "mpi_mod_raw_inv_prime"
|
||||
test_name = "mbedtls_mpi_mod_raw_inv_prime (Montgomery form only)"
|
||||
input_style = "arch_split"
|
||||
arity = 1
|
||||
suffix = True
|
||||
montgomery_form_a = True
|
||||
disallow_zero_a = True
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = bignum_common.invmod_positive(self.int_a, self.int_n)
|
||||
mont_result = self.to_montgomery(result)
|
||||
return [self.format_result(mont_result)]
|
||||
|
||||
# END MERGE SLOT 3
|
||||
|
||||
# BEGIN MERGE SLOT 4
|
||||
|
||||
# END MERGE SLOT 4
|
||||
|
||||
# BEGIN MERGE SLOT 5
|
||||
|
||||
class BignumModRawAdd(bignum_common.ModOperationCommon,
|
||||
BignumModRawTarget):
|
||||
"""Test cases for bignum mpi_mod_raw_add()."""
|
||||
symbol = "+"
|
||||
test_function = "mpi_mod_raw_add"
|
||||
test_name = "mbedtls_mpi_mod_raw_add"
|
||||
input_style = "fixed"
|
||||
arity = 2
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = (self.int_a + self.int_b) % self.int_n
|
||||
return [self.format_result(result)]
|
||||
|
||||
# END MERGE SLOT 5
|
||||
|
||||
# BEGIN MERGE SLOT 6
|
||||
|
||||
class BignumModRawConvertRep(bignum_common.ModOperationCommon,
|
||||
BignumModRawTarget):
|
||||
# This is an abstract class, it's ok to have unimplemented methods.
|
||||
#pylint: disable=abstract-method
|
||||
"""Test cases for representation conversion."""
|
||||
symbol = ""
|
||||
input_style = "arch_split"
|
||||
arity = 1
|
||||
rep = bignum_common.ModulusRepresentation.INVALID
|
||||
|
||||
def set_representation(self, r: bignum_common.ModulusRepresentation) -> None:
|
||||
self.rep = r
|
||||
|
||||
def arguments(self) -> List[str]:
|
||||
return ([bignum_common.quote_str(self.arg_n), self.rep.symbol(),
|
||||
bignum_common.quote_str(self.arg_a)] +
|
||||
self.result())
|
||||
|
||||
def description(self) -> str:
|
||||
base = super().description()
|
||||
mod_with_rep = 'mod({})'.format(self.rep.name)
|
||||
return base.replace('mod', mod_with_rep, 1)
|
||||
|
||||
@classmethod
|
||||
def test_cases_for_values(cls, rep: bignum_common.ModulusRepresentation,
|
||||
n: str, a: str) -> Iterator[test_case.TestCase]:
|
||||
"""Emit test cases for the given values (if any).
|
||||
|
||||
This may emit no test cases if a isn't valid for the modulus n,
|
||||
or multiple test cases if rep requires different data depending
|
||||
on the limb size.
|
||||
"""
|
||||
for bil in cls.limb_sizes:
|
||||
test_object = cls(n, a, bits_in_limb=bil)
|
||||
test_object.set_representation(rep)
|
||||
# The class is set to having separate test cases for each limb
|
||||
# size, because the Montgomery representation requires it.
|
||||
# But other representations don't require it. So for other
|
||||
# representations, emit a single test case with no dependency
|
||||
# on the limb size.
|
||||
if rep is not bignum_common.ModulusRepresentation.MONTGOMERY:
|
||||
test_object.dependencies = \
|
||||
[dep for dep in test_object.dependencies
|
||||
if not dep.startswith('MBEDTLS_HAVE_INT')]
|
||||
if test_object.is_valid:
|
||||
yield test_object.create_test_case()
|
||||
if rep is not bignum_common.ModulusRepresentation.MONTGOMERY:
|
||||
# A single test case (emitted, or skipped due to invalidity)
|
||||
# is enough, since this test case doesn't depend on the
|
||||
# limb size.
|
||||
break
|
||||
|
||||
# The parent class doesn't support non-bignum parameters. So we override
|
||||
# test generation, in order to have the representation as a parameter.
|
||||
@classmethod
|
||||
def generate_function_tests(cls) -> Iterator[test_case.TestCase]:
|
||||
|
||||
for rep in bignum_common.ModulusRepresentation.supported_representations():
|
||||
for n in cls.moduli:
|
||||
for a in cls.input_values:
|
||||
yield from cls.test_cases_for_values(rep, n, a)
|
||||
|
||||
class BignumModRawCanonicalToModulusRep(BignumModRawConvertRep):
|
||||
"""Test cases for mpi_mod_raw_canonical_to_modulus_rep."""
|
||||
test_function = "mpi_mod_raw_canonical_to_modulus_rep"
|
||||
test_name = "Rep canon->mod"
|
||||
|
||||
def result(self) -> List[str]:
|
||||
return [self.format_result(self.convert_from_canonical(self.int_a, self.rep))]
|
||||
|
||||
class BignumModRawModulusToCanonicalRep(BignumModRawConvertRep):
|
||||
"""Test cases for mpi_mod_raw_modulus_to_canonical_rep."""
|
||||
test_function = "mpi_mod_raw_modulus_to_canonical_rep"
|
||||
test_name = "Rep mod->canon"
|
||||
|
||||
@property
|
||||
def arg_a(self) -> str:
|
||||
return self.format_arg("{:x}".format(self.convert_from_canonical(self.int_a, self.rep)))
|
||||
|
||||
def result(self) -> List[str]:
|
||||
return [self.format_result(self.int_a)]
|
||||
|
||||
# END MERGE SLOT 6
|
||||
|
||||
# BEGIN MERGE SLOT 7
|
||||
|
||||
class BignumModRawConvertToMont(bignum_common.ModOperationCommon,
|
||||
BignumModRawTarget):
|
||||
""" Test cases for mpi_mod_raw_to_mont_rep(). """
|
||||
test_function = "mpi_mod_raw_to_mont_rep"
|
||||
test_name = "Convert into Mont: "
|
||||
symbol = "R *"
|
||||
input_style = "arch_split"
|
||||
arity = 1
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = self.to_montgomery(self.int_a)
|
||||
return [self.format_result(result)]
|
||||
|
||||
class BignumModRawConvertFromMont(bignum_common.ModOperationCommon,
|
||||
BignumModRawTarget):
|
||||
""" Test cases for mpi_mod_raw_from_mont_rep(). """
|
||||
test_function = "mpi_mod_raw_from_mont_rep"
|
||||
test_name = "Convert from Mont: "
|
||||
symbol = "1/R *"
|
||||
input_style = "arch_split"
|
||||
arity = 1
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = self.from_montgomery(self.int_a)
|
||||
return [self.format_result(result)]
|
||||
|
||||
class BignumModRawModNegate(bignum_common.ModOperationCommon,
|
||||
BignumModRawTarget):
|
||||
""" Test cases for mpi_mod_raw_neg(). """
|
||||
test_function = "mpi_mod_raw_neg"
|
||||
test_name = "Modular negation: "
|
||||
symbol = "-"
|
||||
input_style = "arch_split"
|
||||
arity = 1
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = (self.int_n - self.int_a) % self.int_n
|
||||
return [self.format_result(result)]
|
||||
# END MERGE SLOT 7
|
||||
|
||||
# BEGIN MERGE SLOT 8
|
||||
|
||||
# END MERGE SLOT 8
|
||||
|
||||
# BEGIN MERGE SLOT 9
|
||||
|
||||
# END MERGE SLOT 9
|
||||
|
||||
# BEGIN MERGE SLOT 10
|
||||
|
||||
# END MERGE SLOT 10
|
@@ -25,6 +25,13 @@ def looks_like_mbedtls_root(path: str) -> bool:
|
||||
return all(os.path.isdir(os.path.join(path, subdir))
|
||||
for subdir in ['include', 'library', 'programs', 'tests'])
|
||||
|
||||
def check_repo_path():
|
||||
"""
|
||||
Check that the current working directory is the project root, and throw
|
||||
an exception if not.
|
||||
"""
|
||||
if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
|
||||
raise Exception("This script must be run from Mbed TLS root")
|
||||
|
||||
def chdir_to_root() -> None:
|
||||
"""Detect the root of the Mbed TLS source tree and change to it.
|
||||
|
@@ -89,6 +89,37 @@ int main(void)
|
||||
}
|
||||
''')
|
||||
|
||||
def compile_c_file(c_filename, exe_filename, include_dirs):
|
||||
"""Compile a C source file with the host compiler.
|
||||
|
||||
* ``c_filename``: the name of the source file to compile.
|
||||
* ``exe_filename``: the name for the executable to be created.
|
||||
* ``include_dirs``: a list of paths to include directories to be passed
|
||||
with the -I switch.
|
||||
"""
|
||||
# Respect $HOSTCC if it is set
|
||||
cc = os.getenv('HOSTCC', None)
|
||||
if cc is None:
|
||||
cc = os.getenv('CC', 'cc')
|
||||
cmd = [cc]
|
||||
|
||||
proc = subprocess.Popen(cmd,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.PIPE,
|
||||
universal_newlines=True)
|
||||
cc_is_msvc = 'Microsoft (R) C/C++' in proc.communicate()[1]
|
||||
|
||||
cmd += ['-I' + dir for dir in include_dirs]
|
||||
if cc_is_msvc:
|
||||
# MSVC has deprecated using -o to specify the output file,
|
||||
# and produces an object file in the working directory by default.
|
||||
obj_filename = exe_filename[:-4] + '.obj'
|
||||
cmd += ['-Fe' + exe_filename, '-Fo' + obj_filename]
|
||||
else:
|
||||
cmd += ['-o' + exe_filename]
|
||||
|
||||
subprocess.check_call(cmd + [c_filename])
|
||||
|
||||
def get_c_expression_values(
|
||||
cast_to, printf_format,
|
||||
expressions,
|
||||
@@ -128,24 +159,8 @@ def get_c_expression_values(
|
||||
expressions)
|
||||
)
|
||||
c_file.close()
|
||||
cc = os.getenv('CC', 'cc')
|
||||
cmd = [cc]
|
||||
|
||||
proc = subprocess.Popen(cmd,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.PIPE,
|
||||
universal_newlines=True)
|
||||
cc_is_msvc = 'Microsoft (R) C/C++' in proc.communicate()[1]
|
||||
|
||||
cmd += ['-I' + dir for dir in include_path]
|
||||
if cc_is_msvc:
|
||||
# MSVC has deprecated using -o to specify the output file,
|
||||
# and produces an object file in the working directory by default.
|
||||
obj_name = exe_name[:-4] + '.obj'
|
||||
cmd += ['-Fe' + exe_name, '-Fo' + obj_name]
|
||||
else:
|
||||
cmd += ['-o' + exe_name]
|
||||
subprocess.check_call(cmd + [c_name])
|
||||
compile_c_file(c_name, exe_name, include_path)
|
||||
if keep_c:
|
||||
sys.stderr.write('List of {} tests kept at {}\n'
|
||||
.format(caller, c_name))
|
||||
|
@@ -20,9 +20,9 @@ This module is entirely based on the PSA API.
|
||||
|
||||
import enum
|
||||
import re
|
||||
from typing import FrozenSet, Iterable, List, Optional, Tuple
|
||||
from typing import FrozenSet, Iterable, List, Optional, Tuple, Dict
|
||||
|
||||
from mbedtls_dev.asymmetric_key_data import ASYMMETRIC_KEY_DATA
|
||||
from .asymmetric_key_data import ASYMMETRIC_KEY_DATA
|
||||
|
||||
|
||||
def short_expression(original: str, level: int = 0) -> str:
|
||||
@@ -148,7 +148,7 @@ class KeyType:
|
||||
'PSA_ECC_FAMILY_BRAINPOOL_P_R1': (160, 192, 224, 256, 320, 384, 512),
|
||||
'PSA_ECC_FAMILY_MONTGOMERY': (255, 448),
|
||||
'PSA_ECC_FAMILY_TWISTED_EDWARDS': (255, 448),
|
||||
}
|
||||
} # type: Dict[str, Tuple[int, ...]]
|
||||
KEY_TYPE_SIZES = {
|
||||
'PSA_KEY_TYPE_AES': (128, 192, 256), # exhaustive
|
||||
'PSA_KEY_TYPE_ARIA': (128, 192, 256), # exhaustive
|
||||
@@ -162,7 +162,7 @@ class KeyType:
|
||||
'PSA_KEY_TYPE_PEPPER': (128, 256), # sample
|
||||
'PSA_KEY_TYPE_RAW_DATA': (8, 40, 128), # sample
|
||||
'PSA_KEY_TYPE_RSA_KEY_PAIR': (1024, 1536), # small sample
|
||||
}
|
||||
} # type: Dict[str, Tuple[int, ...]]
|
||||
def sizes_to_test(self) -> Tuple[int, ...]:
|
||||
"""Return a tuple of key sizes to test.
|
||||
|
||||
@@ -214,9 +214,7 @@ class KeyType:
|
||||
This function does not currently handle key derivation or PAKE.
|
||||
"""
|
||||
#pylint: disable=too-many-branches,too-many-return-statements
|
||||
if alg.is_wildcard:
|
||||
return False
|
||||
if alg.is_invalid_truncation():
|
||||
if not alg.is_valid_for_operation():
|
||||
return False
|
||||
if self.head == 'HMAC' and alg.head == 'HMAC':
|
||||
return True
|
||||
@@ -248,6 +246,8 @@ class KeyType:
|
||||
# So a public key object with a key agreement algorithm is not
|
||||
# a valid combination.
|
||||
return False
|
||||
if alg.is_invalid_key_agreement_with_derivation():
|
||||
return False
|
||||
if self.head == 'ECC':
|
||||
assert self.params is not None
|
||||
eccc = EllipticCurveCategory.from_family(self.params[0])
|
||||
@@ -414,17 +414,38 @@ class Algorithm:
|
||||
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."""
|
||||
def get_key_agreement_derivation(self) -> Optional[str]:
|
||||
"""For a combined key agreement and key derivation algorithm, get the derivation part.
|
||||
|
||||
For anything else, return None.
|
||||
"""
|
||||
if self.category != AlgorithmCategory.KEY_AGREEMENT:
|
||||
return False
|
||||
return None
|
||||
m = re.match(r'PSA_ALG_KEY_AGREEMENT\(\w+,\s*(.*)\)\Z', self.expression)
|
||||
if not m:
|
||||
return False
|
||||
return None
|
||||
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)
|
||||
if re.match(r'(?:0[Xx])?0+\s*\Z', kdf_alg):
|
||||
return None
|
||||
return kdf_alg
|
||||
|
||||
KEY_DERIVATIONS_INCOMPATIBLE_WITH_AGREEMENT = frozenset([
|
||||
'PSA_ALG_TLS12_ECJPAKE_TO_PMS', # secret input in specific format
|
||||
])
|
||||
def is_valid_key_agreement_with_derivation(self) -> bool:
|
||||
"""Whether this is a valid combined key agreement and key derivation algorithm."""
|
||||
kdf_alg = self.get_key_agreement_derivation()
|
||||
if kdf_alg is None:
|
||||
return False
|
||||
return kdf_alg not in self.KEY_DERIVATIONS_INCOMPATIBLE_WITH_AGREEMENT
|
||||
|
||||
def is_invalid_key_agreement_with_derivation(self) -> bool:
|
||||
"""Whether this is an invalid combined key agreement and key derivation algorithm."""
|
||||
kdf_alg = self.get_key_agreement_derivation()
|
||||
if kdf_alg is None:
|
||||
return False
|
||||
return kdf_alg in self.KEY_DERIVATIONS_INCOMPATIBLE_WITH_AGREEMENT
|
||||
|
||||
def short_expression(self, level: int = 0) -> str:
|
||||
"""Abbreviate the expression, keeping it human-readable.
|
||||
@@ -498,13 +519,26 @@ class Algorithm:
|
||||
return True
|
||||
return False
|
||||
|
||||
def is_valid_for_operation(self) -> bool:
|
||||
"""Whether this algorithm construction is valid for an operation.
|
||||
|
||||
This function assumes that the algorithm is constructed in a
|
||||
"grammatically" correct way, and only rejects semantically invalid
|
||||
combinations.
|
||||
"""
|
||||
if self.is_wildcard:
|
||||
return False
|
||||
if self.is_invalid_truncation():
|
||||
return False
|
||||
return True
|
||||
|
||||
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():
|
||||
self.is_valid_key_agreement_with_derivation():
|
||||
return True
|
||||
return False
|
||||
|
||||
|
@@ -26,7 +26,7 @@ import struct
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
import unittest
|
||||
|
||||
from mbedtls_dev import c_build_helper
|
||||
from . import c_build_helper
|
||||
|
||||
|
||||
class Expr:
|
||||
|
@@ -1,4 +1,4 @@
|
||||
"""Library for generating Mbed TLS test data.
|
||||
"""Library for constructing an Mbed TLS test case.
|
||||
"""
|
||||
|
||||
# Copyright The Mbed TLS Contributors
|
||||
@@ -21,7 +21,7 @@ import os
|
||||
import sys
|
||||
from typing import Iterable, List, Optional
|
||||
|
||||
from mbedtls_dev import typing_util
|
||||
from . import typing_util
|
||||
|
||||
def hex_string(data: bytes) -> str:
|
||||
return '"' + binascii.hexlify(data).decode('ascii') + '"'
|
||||
|
@@ -1,4 +1,7 @@
|
||||
"""Common test generation classes and main function.
|
||||
"""Common code for test data generation.
|
||||
|
||||
This module defines classes that are of general use to automatically
|
||||
generate .data files for unit tests, as well as a main function.
|
||||
|
||||
These are used both by generate_psa_tests.py and generate_bignum_tests.py.
|
||||
"""
|
||||
@@ -22,22 +25,19 @@ import argparse
|
||||
import os
|
||||
import posixpath
|
||||
import re
|
||||
import inspect
|
||||
|
||||
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
|
||||
from . import build_tree
|
||||
from . 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`.
|
||||
class BaseTest(metaclass=ABCMeta):
|
||||
"""Base class for test case generation.
|
||||
|
||||
Attributes:
|
||||
count: Counter for test cases from this class.
|
||||
@@ -45,8 +45,6 @@ class BaseTarget(metaclass=ABCMeta):
|
||||
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
|
||||
@@ -56,7 +54,6 @@ class BaseTarget(metaclass=ABCMeta):
|
||||
case_description = ""
|
||||
dependencies = [] # type: List[str]
|
||||
show_test_count = True
|
||||
target_basename = ""
|
||||
test_function = ""
|
||||
test_name = ""
|
||||
|
||||
@@ -118,6 +115,21 @@ class BaseTarget(metaclass=ABCMeta):
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class BaseTarget:
|
||||
#pylint: disable=too-few-public-methods
|
||||
"""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:
|
||||
target_basename: Basename of file to write generated tests to. This
|
||||
should be specified in a child class of BaseTarget.
|
||||
"""
|
||||
target_basename = ""
|
||||
|
||||
@classmethod
|
||||
def generate_tests(cls) -> Iterator[test_case.TestCase]:
|
||||
"""Generate test cases for the class and its subclasses.
|
||||
@@ -129,7 +141,8 @@ class BaseTarget(metaclass=ABCMeta):
|
||||
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:
|
||||
if issubclass(cls, BaseTest) and not inspect.isabstract(cls):
|
||||
#pylint: disable=no-member
|
||||
yield from cls.generate_function_tests()
|
||||
for subclass in sorted(cls.__subclasses__(), key=lambda c: c.__name__):
|
||||
yield from subclass.generate_tests()
|
||||
@@ -138,21 +151,16 @@ class BaseTarget(metaclass=ABCMeta):
|
||||
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')
|
||||
self.test_suite_directory = options.directory
|
||||
# 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__()
|
||||
if subclass.target_basename
|
||||
})
|
||||
|
||||
@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')
|
||||
@@ -186,16 +194,24 @@ def main(args, description: str, generator_class: Type[TestGenerator] = TestGene
|
||||
help='List available targets and exit')
|
||||
parser.add_argument('--list-for-cmake', action='store_true',
|
||||
help='Print \';\'-separated list of available targets and exit')
|
||||
# If specified explicitly, this option may be a path relative to the
|
||||
# current directory when the script is invoked. The default value
|
||||
# is relative to the mbedtls root, which we don't know yet. So we
|
||||
# can't set a string as the default value here.
|
||||
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)
|
||||
|
||||
# Change to the mbedtls root, to keep things simple. But first, adjust
|
||||
# command line options that might be relative paths.
|
||||
if options.directory is None:
|
||||
options.directory = 'tests/suites'
|
||||
else:
|
||||
options.directory = os.path.abspath(options.directory)
|
||||
build_tree.chdir_to_root()
|
||||
|
||||
generator = generator_class(options)
|
||||
if options.list:
|
||||
for name in sorted(generator.targets):
|
@@ -14,7 +14,7 @@ if not "%~1"=="" set "retarget=,PlatformToolset=%1"
|
||||
@rem vcvarsall.bat will silently change the directory to that directory.
|
||||
@rem Setting the VSCMD_START_DIR environment variable causes it to change
|
||||
@rem to that directory instead.
|
||||
set "VSCMD_START_DIR=%~dp0\..\visualc\VS2010"
|
||||
set "VSCMD_START_DIR=%~dp0\..\visualc\VS2013"
|
||||
|
||||
"%vcvarsall%" x64 && ^
|
||||
msbuild /t:Rebuild /p:Configuration=%cfg%%retarget% /m mbedTLS.sln
|
||||
|
Reference in New Issue
Block a user