Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous changes to scripts for PSA-Crypto enablement #7955

Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9a6c45b
Make all.sh PSA-crypto-friendly
davidhorstmann-arm Jul 14, 2023
795d8b5
Modify build_tree.py for the PSA Crypto repo
davidhorstmann-arm Jul 18, 2023
1d09184
Modify test_psa_compliance.py for psa-crypto repo
davidhorstmann-arm Jul 18, 2023
42f42f4
Support psa-crypto repo in psa_storage.py
davidhorstmann-arm Jul 18, 2023
e31014a
Tweak test_psa_compliance pylint annotations
davidhorstmann-arm Jul 19, 2023
76a7738
Invert logic for repo detection in all.sh
davidhorstmann-arm Aug 17, 2023
4dcddcf
Parameterize out of source build directory
davidhorstmann-arm Aug 17, 2023
7f93d22
Rename psa_crypto_lib_filename to just crypto_lib_filename
davidhorstmann-arm Aug 23, 2023
0ac57ca
Rename is_psa_crypto -> in_psa_crypto_repo
davidhorstmann-arm Aug 23, 2023
2fde999
Improve directory coverage in PSA repo detection
davidhorstmann-arm Aug 29, 2023
d02b5f8
Separate directory discernment into 2 functions
davidhorstmann-arm Aug 29, 2023
58cf7c6
Use repo detection functions at start of all.sh
davidhorstmann-arm Aug 29, 2023
98af198
Correctly detect presence of the built library
davidhorstmann-arm Aug 29, 2023
f757069
Rename 'mbedtls_dir' -> 'root_dir'
davidhorstmann-arm Aug 29, 2023
3b8984a
Remove or qualify references to Mbed TLS
davidhorstmann-arm Aug 29, 2023
2ba89be
Disable pylint error in CMake command
davidhorstmann-arm Aug 29, 2023
c69074d
Tidy up reference to Mbed TLS in help message
davidhorstmann-arm Aug 29, 2023
beaee26
Test PSA compliance: Build only the crypto target
davidhorstmann-arm Aug 29, 2023
b48822c
Appease pylint by renaming variables
davidhorstmann-arm Aug 29, 2023
41c316d
Move -B switch into a single argument
davidhorstmann-arm Aug 29, 2023
fd9264e
Fix pylint errors
davidhorstmann-arm Aug 29, 2023
9cc6b2f
Add missing import in test_psa_compliance.py
davidhorstmann-arm Aug 29, 2023
3ed1871
Disable pylint error for non-uppercase names
davidhorstmann-arm Aug 29, 2023
8f3ec8e
Use '--target' instead of shortened '-t'
davidhorstmann-arm Aug 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions scripts/mbedtls_dev/build_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@
import os
import inspect

def looks_like_psa_crypto_root(path: str) -> bool:
"""Whether the given directory looks like the root of the PSA Crypto source tree."""
return all(os.path.isdir(os.path.join(path, subdir))
for subdir in ['include', 'core', 'drivers', 'programs', 'tests'])

def looks_like_mbedtls_root(path: str) -> bool:
"""Whether the given directory looks like the root of the Mbed TLS source tree."""
return all(os.path.isdir(os.path.join(path, subdir))
for subdir in ['include', 'library', 'programs', 'tests'])

def looks_like_root(path: str) -> bool:
return looks_like_psa_crypto_root(path) or looks_like_mbedtls_root(path)

def check_repo_path():
"""
Check that the current working directory is the project root, and throw
Expand All @@ -42,7 +49,7 @@ def chdir_to_root() -> None:
for d in [os.path.curdir,
os.path.pardir,
os.path.join(os.path.pardir, os.path.pardir)]:
if looks_like_mbedtls_root(d):
if looks_like_root(d):
os.chdir(d)
return
raise Exception('Mbed TLS source tree not found')
Expand All @@ -62,6 +69,6 @@ def guess_mbedtls_root():
if d in dirs:
continue
dirs.add(d)
if looks_like_mbedtls_root(d):
if looks_like_root(d):
return d
raise Exception('Mbed TLS source tree not found')
6 changes: 5 additions & 1 deletion scripts/mbedtls_dev/psa_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import unittest

from . import c_build_helper
from . import build_tree


class Expr:
Expand All @@ -51,13 +52,16 @@ def __init__(self, content: Union[int, str]):
def update_cache(self) -> None:
"""Update `value_cache` for expressions registered in `unknown_values`."""
expressions = sorted(self.unknown_values)
includes = ['include']
if build_tree.looks_like_psa_crypto_root('.'):
includes.append('drivers/builtin/include')
values = c_build_helper.get_c_expression_values(
'unsigned long', '%lu',
expressions,
header="""
#include <psa/crypto.h>
""",
include_path=['include']) #type: List[str]
include_path=includes) #type: List[str]
for e, v in zip(expressions, values):
self.value_cache[e] = int(v, 0)
self.unknown_values.clear()
Expand Down
34 changes: 26 additions & 8 deletions tests/scripts/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,27 @@ set -e -o pipefail -u
# Enable ksh/bash extended file matching patterns
shopt -s extglob

in_mbedtls_repo () {
ronald-cron-arm marked this conversation as resolved.
Show resolved Hide resolved
test -d include -a -d library -a -d programs -a -d tests
}

in_psa_crypto_repo () {
test -d include -a -d core -a -d drivers -a -d programs -a -d tests
}

pre_check_environment () {
if [ -d library -a -d include -a -d tests ]; then :; else
echo "Must be run from mbed TLS root" >&2
if in_mbedtls_repo || in_psa_crypto_repo; then :; else
echo "Must be run from Mbed TLS root" >&2
ronald-cron-arm marked this conversation as resolved.
Show resolved Hide resolved
exit 1
fi
}

pre_initialize_variables () {
CONFIG_H='include/mbedtls/mbedtls_config.h'
if in_mbedtls_repo; then
CONFIG_H='include/mbedtls/mbedtls_config.h'
else
CONFIG_H='drivers/builtin/include/mbedtls/mbedtls_config.h'
fi
CRYPTO_CONFIG_H='include/psa/crypto_config.h'
CONFIG_TEST_DRIVER_H='tests/include/test/drivers/config_test_driver.h'

Expand All @@ -141,8 +153,10 @@ pre_initialize_variables () {
backup_suffix='.all.bak'
# Files clobbered by config.py
files_to_back_up="$CONFIG_H $CRYPTO_CONFIG_H $CONFIG_TEST_DRIVER_H"
# Files clobbered by in-tree cmake
files_to_back_up="$files_to_back_up Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile"
if in_mbedtls_repo; then
# Files clobbered by in-tree cmake
files_to_back_up="$files_to_back_up Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile"
fi

append_outcome=0
MEMORY=0
Expand Down Expand Up @@ -299,7 +313,9 @@ EOF
# Does not remove generated source files.
cleanup()
{
command make clean
if in_mbedtls_repo; then
command make clean
fi

# Remove CMake artefacts
find . -name .git -prune -o \
Expand Down Expand Up @@ -556,7 +572,7 @@ pre_check_git () {
fi

if ! git diff --quiet "$CONFIG_H"; then
err_msg "Warning - the configuration file 'include/mbedtls/mbedtls_config.h' has been edited. "
err_msg "Warning - the configuration file '$CONFIG_H' has been edited. "
echo "You can either delete or preserve your work, or force the test by rerunning the"
echo "script as: $0 --force"
exit 1
Expand Down Expand Up @@ -5264,7 +5280,9 @@ pre_prepare_outcome_file
pre_print_configuration
pre_check_tools
cleanup
pre_generate_files
if in_mbedtls_repo; then
pre_generate_files
fi

# Run the requested tests.
for ((error_test_i=1; error_test_i <= error_test; error_test_i++)); do
Expand Down
63 changes: 49 additions & 14 deletions tests/scripts/test_psa_compliance.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python3
"""Run the PSA Crypto API compliance test suite.
Clone the repo and check out the commit specified by PSA_ARCH_TEST_REPO and PSA_ARCH_TEST_REF,
then compile and run the test suite. The clone is stored at <Mbed TLS root>/psa-arch-tests.
Known defects in either the test suite or mbedtls - identified by their test number - are ignored,
while unexpected failures AND successes are reported as errors,
to help keep the list of known defects as up to date as possible.
then compile and run the test suite. The clone is stored at <repository root>/psa-arch-tests.
Known defects in either the test suite or mbedtls / psa-crypto - identified by their test
number - are ignored, while unexpected failures AND successes are reported as errors, to help
keep the list of known defects as up to date as possible.
"""

# Copyright The Mbed TLS Contributors
Expand All @@ -22,13 +22,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os
import re
import shutil
import subprocess
import sys

# PSA Compliance tests we expect to fail due to known defects in Mbed TLS (or the test suite)
#pylint: disable=unused-import
import scripts_path
from mbedtls_dev import build_tree

# PSA Compliance tests we expect to fail due to known defects in Mbed TLS / PSA Crypto
# (or the test suite).
# The test numbers correspond to the numbers used by the console output of the test suite.
# Test number 2xx corresponds to the files in the folder
# psa-arch-tests/api-tests/dev_apis/crypto/test_c0xx
Expand All @@ -49,12 +55,26 @@
PSA_ARCH_TESTS_REPO = 'https://github.com/bensze01/psa-arch-tests.git'
PSA_ARCH_TESTS_REF = 'fix-pr-5736'

#pylint: disable=too-many-branches,too-many-statements
def main():
mbedtls_dir = os.getcwd()
#pylint: disable=too-many-branches,too-many-statements,too-many-locals
def main(library_build_dir: str):
root_dir = os.getcwd()

in_psa_crypto_repo = build_tree.looks_like_psa_crypto_root(root_dir)

if not os.path.exists('library/libmbedcrypto.a'):
subprocess.check_call(['make', '-C', 'library', 'libmbedcrypto.a'])
if in_psa_crypto_repo:
crypto_lib_filename = \
library_build_dir + '/core/libpsacrypto.a'
else:
crypto_lib_filename = library_build_dir + '/library/libmbedcrypto.a'

if not os.path.exists(crypto_lib_filename):
#pylint: disable=bad-continuation
subprocess.check_call([
'cmake', '.',
'-GUnix Makefiles',
'-B', library_build_dir
])
subprocess.check_call(['cmake', '--build', library_build_dir])
ronald-cron-arm marked this conversation as resolved.
Show resolved Hide resolved

psa_arch_tests_dir = 'psa-arch-tests'
os.makedirs(psa_arch_tests_dir, exist_ok=True)
Expand All @@ -74,15 +94,19 @@ def main():
os.mkdir(build_dir)
os.chdir(build_dir)

extra_includes = (';{}/drivers/builtin/include'.format(root_dir)
if in_psa_crypto_repo else '')

#pylint: disable=bad-continuation
subprocess.check_call([
'cmake', '..',
'-GUnix Makefiles',
'-DTARGET=tgt_dev_apis_stdc',
'-DTOOLCHAIN=HOST_GCC',
'-DSUITE=CRYPTO',
'-DPSA_CRYPTO_LIB_FILENAME={}/library/libmbedcrypto.a'.format(mbedtls_dir),
'-DPSA_INCLUDE_PATHS={}/include'.format(mbedtls_dir)
'-DPSA_CRYPTO_LIB_FILENAME={}/{}'.format(root_dir,
crypto_lib_filename),
('-DPSA_INCLUDE_PATHS={}/include' + extra_includes).format(root_dir)
])
subprocess.check_call(['cmake', '--build', '.'])

Expand Down Expand Up @@ -136,7 +160,18 @@ def main():
print('SUCCESS')
return 0
finally:
os.chdir(mbedtls_dir)
os.chdir(root_dir)

if __name__ == '__main__':
sys.exit(main())
# Default build directory
library_build_dir = 'out_of_source_build'

parser = argparse.ArgumentParser()
parser.add_argument('--build-dir', nargs=1,
ronald-cron-arm marked this conversation as resolved.
Show resolved Hide resolved
help='path to Mbed TLS / PSA Crypto build directory')
args = parser.parse_args()

if args.build_dir is not None:
library_build_dir = args.build_dir[0]

sys.exit(main(library_build_dir))