blob: b165042bf2794c8b313f2ac2aa8130fbe74e9992 [file] [log] [blame]
Manuel Pégourié-Gonnarde6df2e42024-10-01 13:19:04 +02001# all-helpers.sh
2#
3# Copyright The Mbed TLS Contributors
4# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5
6# This file contains helpers for test components that are executed by all.sh
7
8################################################################
9#### Helpers for components using libtestdriver1
10################################################################
11
12# How to use libtestdriver1
13# -------------------------
14#
15# 1. Define the list algorithms and key types to accelerate,
16# designated the same way as PSA_WANT_ macros but without PSA_WANT_.
17# Examples:
18# - loc_accel_list="ALG_JPAKE"
19# - loc_accel_list="ALG_FFDH KEY_TYPE_DH_KEY_PAIR KEY_TYPE_DH_PUBLIC_KEY"
20# 2. Make configurations changes for the driver and/or main libraries.
21# 2a. Call helper_libtestdriver1_adjust_config <base>, where the argument
22# can be either "default" to start with the default config, or a name
23# supported by scripts/config.py (for example, "full"). This selects
24# the base to use, and makes common adjustments.
25# 2b. If desired, adjust the PSA_WANT symbols in psa/crypto_config.h.
26# These changes affect both the driver and the main libraries.
27# (Note: they need to have the same set of PSA_WANT symbols, as that
28# determines the ABI between them.)
29# 2c. Adjust MBEDTLS_ symbols in mbedtls_config.h. This only affects the
30# main libraries. Typically, you want to disable the module(s) that are
31# being accelerated. You may need to also disable modules that depend
32# on them or options that are not supported with drivers.
33# 2d. On top of psa/crypto_config.h, the driver library uses its own config
34# file: tests/include/test/drivers/config_test_driver.h. You usually
35# don't need to edit it: using loc_extra_list (see below) is preferred.
36# However, when there's no PSA symbol for what you want to enable,
37# calling scripts/config.py on this file remains the only option.
38# 3. Build the driver library, then the main libraries, test, and programs.
39# 3a. Call helper_libtestdriver1_make_drivers "$loc_accel_list". You may
40# need to enable more algorithms here, typically hash algorithms when
41# accelerating some signature algorithms (ECDSA, RSAv2). This is done
42# by passing a 2nd argument listing the extra algorithms.
43# Example:
44# loc_extra_list="ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512"
45# helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
46# 3b. Call helper_libtestdriver1_make_main "$loc_accel_list". Any
47# additional arguments will be passed to make: this can be useful if
48# you don't want to build everything when iterating during development.
49# Example:
50# helper_libtestdriver1_make_main "$loc_accel_list" -C tests test_suite_foo
51# 4. Run the tests you want.
52
53# Adjust the configuration - for both libtestdriver1 and main library,
54# as they should have the same PSA_WANT macros.
55helper_libtestdriver1_adjust_config() {
56 base_config=$1
57 # Select the base configuration
58 if [ "$base_config" != "default" ]; then
59 scripts/config.py "$base_config"
60 fi
61
62 # Enable PSA-based config (necessary to use drivers)
63 scripts/config.py set MBEDTLS_PSA_CRYPTO_CONFIG
64
65 # Dynamic secure element support is a deprecated feature and needs to be disabled here.
66 # This is done to have the same form of psa_key_attributes_s for libdriver and library.
67 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
68
69 # If threading is enabled on the normal build, then we need to enable it in the drivers as well,
70 # otherwise we will end up running multithreaded tests without mutexes to protect them.
71 if scripts/config.py get MBEDTLS_THREADING_C; then
72 scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_THREADING_C
73 fi
74
75 if scripts/config.py get MBEDTLS_THREADING_PTHREAD; then
76 scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_THREADING_PTHREAD
77 fi
78}
79
80# Build the drivers library libtestdriver1.a (with ASan).
81#
82# Parameters:
83# 1. a space-separated list of things to accelerate;
84# 2. optional: a space-separate list of things to also support.
85# Here "things" are PSA_WANT_ symbols but with PSA_WANT_ removed.
86helper_libtestdriver1_make_drivers() {
87 loc_accel_flags=$( echo "$1 ${2-}" | sed 's/[^ ]* */-DLIBTESTDRIVER1_MBEDTLS_PSA_ACCEL_&/g' )
88 make CC=$ASAN_CC -C tests libtestdriver1.a CFLAGS=" $ASAN_CFLAGS $loc_accel_flags" LDFLAGS="$ASAN_CFLAGS"
89}
90
91# Build the main libraries, programs and tests,
92# linking to the drivers library (with ASan).
93#
94# Parameters:
95# 1. a space-separated list of things to accelerate;
96# *. remaining arguments if any are passed directly to make
97# (examples: lib, -C tests test_suite_xxx, etc.)
98# Here "things" are PSA_WANT_ symbols but with PSA_WANT_ removed.
99helper_libtestdriver1_make_main() {
100 loc_accel_list=$1
101 shift
102
103 # we need flags both with and without the LIBTESTDRIVER1_ prefix
104 loc_accel_flags=$( echo "$loc_accel_list" | sed 's/[^ ]* */-DLIBTESTDRIVER1_MBEDTLS_PSA_ACCEL_&/g' )
105 loc_accel_flags="$loc_accel_flags $( echo "$loc_accel_list" | sed 's/[^ ]* */-DMBEDTLS_PSA_ACCEL_&/g' )"
106 make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -I../tests/include -I../tests -I../../tests -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_TEST_LIBTESTDRIVER1 $loc_accel_flags" LDFLAGS="-ltestdriver1 $ASAN_CFLAGS" "$@"
107}
108
109################################################################
110#### Helpers for components using psasim
111################################################################
112
113# Set some default values $CONFIG_H in order to build server or client sides
114# in PSASIM. There is only 1 mandatory parameter:
115# - $1: target which can be "client" or "server"
116helper_psasim_config() {
117 TARGET=$1
118
119 if [ "$TARGET" == "client" ]; then
120 scripts/config.py full
121 scripts/config.py unset MBEDTLS_PSA_CRYPTO_C
122 scripts/config.py unset MBEDTLS_PSA_CRYPTO_STORAGE_C
123 # Dynamic secure element support is a deprecated feature and it is not
124 # available when CRYPTO_C and PSA_CRYPTO_STORAGE_C are disabled.
125 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
126 # Disable potentially problematic features
127 scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT
128 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
129 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
130 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
131 scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
132 scripts/config.py unset MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
133 else
134 scripts/config.py crypto_full
135 scripts/config.py unset MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS
136 # We need to match the client with MBEDTLS_PSA_CRYPTO_SE_C
137 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
138 # Also ensure MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER not set (to match client)
139 scripts/config.py unset MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
140 fi
141}
142
143# This is a helper function to be used in psasim builds. It is meant to clean
144# up the library's workspace after the server build and before the client
145# build. Built libraries (mbedcrypto, mbedx509 and mbedtls) are supposed to be
146# already copied to psasim folder at this point.
147helper_psasim_cleanup_before_client() {
148 # Clean up library files
149 make -C library clean
150
151 # Restore files that were backup before building library files. This
152 # includes $CONFIG_H and $CRYPTO_CONFIG_H.
153 restore_backed_up_files
154}
155
156# Helper to build the libraries for client/server in PSASIM. If the server is
157# being built, then it builds also the final executable.
158# There is only 1 mandatory parameter:
159# - $1: target which can be "client" or "server"
160helper_psasim_build() {
161 TARGET=$1
162 shift
163 TARGET_LIB=${TARGET}_libs
164
165 make -C $PSASIM_PATH CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" $TARGET_LIB "$@"
166
167 # Build also the server application after its libraries have been built.
168 if [ "$TARGET" == "server" ]; then
169 make -C $PSASIM_PATH CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" test/psa_server
170 fi
171}
172
173################################################################
174#### Configuration helpers
175################################################################
176
177# When called with no parameter this function disables all builtin curves.
178# The function optionally accepts 1 parameter: a space-separated list of the
179# curves that should be kept enabled.
180helper_disable_builtin_curves() {
181 allowed_list="${1:-}"
182 scripts/config.py unset-all "MBEDTLS_ECP_DP_[0-9A-Z_a-z]*_ENABLED"
183
184 for curve in $allowed_list; do
185 scripts/config.py set $curve
186 done
187}
188
189# Helper returning the list of supported elliptic curves from CRYPTO_CONFIG_H,
190# without the "PSA_WANT_" prefix. This becomes handy for accelerating curves
191# in the following helpers.
192helper_get_psa_curve_list () {
193 loc_list=""
194 for item in $(sed -n 's/^#define PSA_WANT_\(ECC_[0-9A-Z_a-z]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do
195 loc_list="$loc_list $item"
196 done
197
198 echo "$loc_list"
199}
200
201# Helper returning the list of supported DH groups from CRYPTO_CONFIG_H,
202# without the "PSA_WANT_" prefix. This becomes handy for accelerating DH groups
203# in the following helpers.
204helper_get_psa_dh_group_list () {
205 loc_list=""
206 for item in $(sed -n 's/^#define PSA_WANT_\(DH_RFC7919_[0-9]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do
207 loc_list="$loc_list $item"
208 done
209
210 echo "$loc_list"
211}
212
213# Get the list of uncommented PSA_WANT_KEY_TYPE_xxx_ from CRYPTO_CONFIG_H. This
214# is useful to easily get a list of key type symbols to accelerate.
215# The function accepts a single argument which is the key type: ECC, DH, RSA.
216helper_get_psa_key_type_list() {
217 key_type="$1"
218 loc_list=""
219 for item in $(sed -n "s/^#define PSA_WANT_\(KEY_TYPE_${key_type}_[0-9A-Z_a-z]*\).*/\1/p" <"$CRYPTO_CONFIG_H"); do
220 # Skip DERIVE for elliptic keys since there is no driver dispatch for
221 # it so it cannot be accelerated.
222 if [ "$item" != "KEY_TYPE_ECC_KEY_PAIR_DERIVE" ]; then
223 loc_list="$loc_list $item"
224 fi
225 done
226
227 echo "$loc_list"
228}
229
230################################################################
231#### Misc. helpers for components
232################################################################
233
234helper_armc6_build_test()
235{
236 FLAGS="$1"
237
238 msg "build: ARM Compiler 6 ($FLAGS)"
239 make clean
240 ARM_TOOL_VARIANT="ult" CC="$ARMC6_CC" AR="$ARMC6_AR" CFLAGS="$FLAGS" \
241 WARNING_CFLAGS='-Werror -xc -std=c99' make lib
242
243 msg "size: ARM Compiler 6 ($FLAGS)"
244 "$ARMC6_FROMELF" -z library/*.o
245 if [ -n ${PSA_CORE_PATH} ]; then
246 "$ARMC6_FROMELF" -z ${PSA_CORE_PATH}/*.o
247 fi
248 if [ -n ${BUILTIN_SRC_PATH} ]; then
249 "$ARMC6_FROMELF" -z ${BUILTIN_SRC_PATH}/*.o
250 fi
251}