blob: 205c83fcc03251bcd316b444ec664c499c1dd3bf [file] [log] [blame]
Minos Galanakis2c824b42025-03-20 09:28:45 +00001# 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# See "Files structure" in all-core.sh for other files used by all.sh.
8#
9# This file is the right place for helpers:
10# - that are used by more than one component living in more than one file;
11# - or (inclusive) that we want to share accross repos or branches.
12#
13# Helpers that are used in a single component file that is
14# repo&branch-specific can be defined in the file where they are used.
15
16################################################################
17#### Helpers for components using libtestdriver1
18################################################################
19
20# How to use libtestdriver1
21# -------------------------
22#
23# 1. Define the list algorithms and key types to accelerate,
24# designated the same way as PSA_WANT_ macros but without PSA_WANT_.
25# Examples:
26# - loc_accel_list="ALG_JPAKE"
27# - loc_accel_list="ALG_FFDH KEY_TYPE_DH_KEY_PAIR KEY_TYPE_DH_PUBLIC_KEY"
28# 2. Make configurations changes for the driver and/or main libraries.
29# 2a. Call helper_libtestdriver1_adjust_config <base>, where the argument
30# can be either "default" to start with the default config, or a name
31# supported by scripts/config.py (for example, "full"). This selects
32# the base to use, and makes common adjustments.
33# 2b. If desired, adjust the PSA_WANT symbols in psa/crypto_config.h.
34# These changes affect both the driver and the main libraries.
35# (Note: they need to have the same set of PSA_WANT symbols, as that
36# determines the ABI between them.)
37# 2c. Adjust MBEDTLS_ symbols in mbedtls_config.h. This only affects the
38# main libraries. Typically, you want to disable the module(s) that are
39# being accelerated. You may need to also disable modules that depend
40# on them or options that are not supported with drivers.
41# 2d. On top of psa/crypto_config.h, the driver library uses its own config
42# file: tests/configs/config_test_driver.h. You usually don't need to
43# edit it: using loc_extra_list (see below) is preferred. However, when
44# there's no PSA symbol for what you want to enable, calling
45# scripts/config.py on this file remains the only option.
46# 3. Build the driver library, then the main libraries, test, and programs.
47# 3a. Call helper_libtestdriver1_make_drivers "$loc_accel_list". You may
48# need to enable more algorithms here, typically hash algorithms when
49# accelerating some signature algorithms (ECDSA, RSAv2). This is done
50# by passing a 2nd argument listing the extra algorithms.
51# Example:
52# loc_extra_list="ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512"
53# helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
54# 3b. Call helper_libtestdriver1_make_main "$loc_accel_list". Any
55# additional arguments will be passed to make: this can be useful if
56# you don't want to build everything when iterating during development.
57# Example:
58# helper_libtestdriver1_make_main "$loc_accel_list" -C tests test_suite_foo
59# 4. Run the tests you want.
60
61# Adjust the configuration - for both libtestdriver1 and main library,
62# as they should have the same PSA_WANT macros.
63helper_libtestdriver1_adjust_config() {
64 base_config=$1
65 # Select the base configuration
66 if [ "$base_config" != "default" ]; then
67 scripts/config.py "$base_config"
68 fi
69
70 # Enable PSA-based config (necessary to use drivers)
71 # MBEDTLS_PSA_CRYPTO_CONFIG is a legacy setting which should only be set on 3.6 LTS branches.
72 if in_mbedtls_repo && in_3_6_branch; then
73 scripts/config.py set MBEDTLS_PSA_CRYPTO_CONFIG
74 fi
75
76 # Dynamic secure element support is a deprecated feature and needs to be disabled here.
77 # This is done to have the same form of psa_key_attributes_s for libdriver and library.
78 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
79
80 # If threading is enabled on the normal build, then we need to enable it in the drivers as well,
81 # otherwise we will end up running multithreaded tests without mutexes to protect them.
82 if scripts/config.py get MBEDTLS_THREADING_C; then
83 if in_3_6_branch; then
84 scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_THREADING_C
85 else
86 scripts/config.py -c "$CONFIG_TEST_DRIVER_H" set MBEDTLS_THREADING_C
87 fi
88 fi
89
90 if scripts/config.py get MBEDTLS_THREADING_PTHREAD; then
91 if in_3_6_branch; then
92 scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_THREADING_PTHREAD
93 else
94 scripts/config.py -c "$CONFIG_TEST_DRIVER_H" set MBEDTLS_THREADING_PTHREAD
95 fi
96 fi
97}
98
99# Build the drivers library libtestdriver1.a (with ASan).
100#
101# Parameters:
102# 1. a space-separated list of things to accelerate;
103# 2. optional: a space-separate list of things to also support.
104# Here "things" are PSA_WANT_ symbols but with PSA_WANT_ removed.
105helper_libtestdriver1_make_drivers() {
106 loc_accel_flags=$( echo "$1 ${2-}" | sed 's/[^ ]* */-DLIBTESTDRIVER1_MBEDTLS_PSA_ACCEL_&/g' )
107 make CC=$ASAN_CC -C tests libtestdriver1.a CFLAGS=" $ASAN_CFLAGS $loc_accel_flags" LDFLAGS="$ASAN_CFLAGS"
108}
109
110# Build the main libraries, programs and tests,
111# linking to the drivers library (with ASan).
112#
113# Parameters:
114# 1. a space-separated list of things to accelerate;
115# *. remaining arguments if any are passed directly to make
116# (examples: lib, -C tests test_suite_xxx, etc.)
117# Here "things" are PSA_WANT_ symbols but with PSA_WANT_ removed.
118helper_libtestdriver1_make_main() {
119 loc_accel_list=$1
120 shift
121
122 # we need flags both with and without the LIBTESTDRIVER1_ prefix
123 loc_accel_flags=$( echo "$loc_accel_list" | sed 's/[^ ]* */-DLIBTESTDRIVER1_MBEDTLS_PSA_ACCEL_&/g' )
124 loc_accel_flags="$loc_accel_flags $( echo "$loc_accel_list" | sed 's/[^ ]* */-DMBEDTLS_PSA_ACCEL_&/g' )"
125 make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -I../tests/include -I../framework/tests/include -I../tests -I../../tests -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_TEST_LIBTESTDRIVER1 $loc_accel_flags" LDFLAGS="-ltestdriver1 $ASAN_CFLAGS" "$@"
126}
127
128################################################################
129#### Helpers for components using psasim
130################################################################
131
132# Set some default values $CONFIG_H in order to build server or client sides
133# in PSASIM. There is only 1 mandatory parameter:
134# - $1: target which can be "client" or "server"
135helper_psasim_config() {
136 TARGET=$1
137
138 if [ "$TARGET" == "client" ]; then
139 scripts/config.py full
140 scripts/config.py unset MBEDTLS_PSA_CRYPTO_C
141 scripts/config.py unset MBEDTLS_PSA_CRYPTO_STORAGE_C
142 # Dynamic secure element support is a deprecated feature and it is not
143 # available when CRYPTO_C and PSA_CRYPTO_STORAGE_C are disabled.
144 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
145 # Disable potentially problematic features
146 scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT
147 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
148 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
149 scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
150 scripts/config.py unset MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
151 else
152 scripts/config.py crypto_full
153 scripts/config.py unset MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS
154 # We need to match the client with MBEDTLS_PSA_CRYPTO_SE_C
155 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
156 # Also ensure MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER not set (to match client)
157 scripts/config.py unset MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
158 fi
159}
160
161# This is a helper function to be used in psasim builds. It is meant to clean
162# up the library's workspace after the server build and before the client
163# build. Built libraries (mbedcrypto, mbedx509 and mbedtls) are supposed to be
164# already copied to psasim folder at this point.
165helper_psasim_cleanup_before_client() {
166 # Clean up library files
167 make -C library clean
168
169 # Restore files that were backup before building library files. This
170 # includes $CONFIG_H and $CRYPTO_CONFIG_H.
171 restore_backed_up_files
172}
173
174# Helper to build the libraries for client/server in PSASIM. If the server is
175# being built, then it builds also the final executable.
176# There is only 1 mandatory parameter:
177# - $1: target which can be "client" or "server"
178helper_psasim_build() {
179 TARGET=$1
180 shift
181 TARGET_LIB=${TARGET}_libs
182
183 make -C $PSASIM_PATH CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" $TARGET_LIB "$@"
184
185 # Build also the server application after its libraries have been built.
186 if [ "$TARGET" == "server" ]; then
187 make -C $PSASIM_PATH CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" test/psa_server
188 fi
189}
190
191################################################################
192#### Configuration helpers
193################################################################
194
195# When called with no parameter this function disables all builtin curves.
196# The function optionally accepts 1 parameter: a space-separated list of the
197# curves that should be kept enabled.
198helper_disable_builtin_curves() {
199 allowed_list="${1:-}"
200 scripts/config.py unset-all "MBEDTLS_ECP_DP_[0-9A-Z_a-z]*_ENABLED"
201
202 for curve in $allowed_list; do
203 scripts/config.py set $curve
204 done
205}
206
207# Helper returning the list of supported elliptic curves from CRYPTO_CONFIG_H,
208# without the "PSA_WANT_" prefix. This becomes handy for accelerating curves
209# in the following helpers.
210helper_get_psa_curve_list () {
211 loc_list=""
212 for item in $(sed -n 's/^#define PSA_WANT_\(ECC_[0-9A-Z_a-z]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do
213 loc_list="$loc_list $item"
214 done
215
216 echo "$loc_list"
217}
218
219# Helper returning the list of supported DH groups from CRYPTO_CONFIG_H,
220# without the "PSA_WANT_" prefix. This becomes handy for accelerating DH groups
221# in the following helpers.
222helper_get_psa_dh_group_list () {
223 loc_list=""
224 for item in $(sed -n 's/^#define PSA_WANT_\(DH_RFC7919_[0-9]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do
225 loc_list="$loc_list $item"
226 done
227
228 echo "$loc_list"
229}
230
231# Get the list of uncommented PSA_WANT_KEY_TYPE_xxx_ from CRYPTO_CONFIG_H. This
232# is useful to easily get a list of key type symbols to accelerate.
233# The function accepts a single argument which is the key type: ECC, DH, RSA.
234helper_get_psa_key_type_list() {
235 key_type="$1"
236 loc_list=""
237 for item in $(sed -n "s/^#define PSA_WANT_\(KEY_TYPE_${key_type}_[0-9A-Z_a-z]*\).*/\1/p" <"$CRYPTO_CONFIG_H"); do
238 # Skip DERIVE for elliptic keys since there is no driver dispatch for
239 # it so it cannot be accelerated.
240 if [ "$item" != "KEY_TYPE_ECC_KEY_PAIR_DERIVE" ]; then
241 loc_list="$loc_list $item"
242 fi
243 done
244
245 echo "$loc_list"
246}
247
248################################################################
249#### Misc. helpers for components
250################################################################
251
252helper_armc6_build_test()
253{
254 FLAGS="$1"
255
256 msg "build: ARM Compiler 6 ($FLAGS)"
257 make clean
258 ARM_TOOL_VARIANT="ult" CC="$ARMC6_CC" AR="$ARMC6_AR" CFLAGS="$FLAGS" \
259 WARNING_CFLAGS='-Werror -xc -std=c99' make lib
260
261 msg "size: ARM Compiler 6 ($FLAGS)"
262 "$ARMC6_FROMELF" -z library/*.o
263 if [ -n "${PSA_CORE_PATH}" ]; then
264 "$ARMC6_FROMELF" -z ${PSA_CORE_PATH}/*.o
265 fi
266 if [ -n "${BUILTIN_SRC_PATH}" ]; then
267 "$ARMC6_FROMELF" -z ${BUILTIN_SRC_PATH}/*.o
268 fi
269}
270
271clang_version() {
272 if command -v clang > /dev/null ; then
273 clang --version|grep version|sed -E 's#.*version ([0-9]+).*#\1#'
274 else
275 echo 0 # report version 0 for "no clang"
276 fi
277}
278
279gcc_version() {
280 gcc="$1"
281 if command -v "$gcc" > /dev/null ; then
282 "$gcc" --version | sed -En '1s/^[^ ]* \([^)]*\) ([0-9]+).*/\1/p'
283 else
284 echo 0 # report version 0 for "no gcc"
285 fi
286}
287
288can_run_cc_output() {
289 cc="$1"
290 result=false
291 if type "$cc" >/dev/null 2>&1; then
292 testbin=$(mktemp)
293 if echo 'int main(void){return 0;}' | "$cc" -o "$testbin" -x c -; then
294 if "$testbin" 2>/dev/null; then
295 result=true
296 fi
297 fi
298 rm -f "$testbin"
299 fi
300 $result
301}
302
303can_run_arm_linux_gnueabi=
304can_run_arm_linux_gnueabi () {
305 if [ -z "$can_run_arm_linux_gnueabi" ]; then
306 if can_run_cc_output "${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc"; then
307 can_run_arm_linux_gnueabi=true
308 else
309 can_run_arm_linux_gnueabi=false
310 fi
311 fi
312 $can_run_arm_linux_gnueabi
313}
314
315can_run_arm_linux_gnueabihf=
316can_run_arm_linux_gnueabihf () {
317 if [ -z "$can_run_arm_linux_gnueabihf" ]; then
318 if can_run_cc_output "${ARM_LINUX_GNUEABIHF_GCC_PREFIX}gcc"; then
319 can_run_arm_linux_gnueabihf=true
320 else
321 can_run_arm_linux_gnueabihf=false
322 fi
323 fi
324 $can_run_arm_linux_gnueabihf
325}
326
327can_run_aarch64_linux_gnu=
328can_run_aarch64_linux_gnu () {
329 if [ -z "$can_run_aarch64_linux_gnu" ]; then
330 if can_run_cc_output "${AARCH64_LINUX_GNU_GCC_PREFIX}gcc"; then
331 can_run_aarch64_linux_gnu=true
332 else
333 can_run_aarch64_linux_gnu=false
334 fi
335 fi
336 $can_run_aarch64_linux_gnu
337}