all-helpers.sh (14295B)
1 # 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. 63 helper_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 if in_mbedtls_repo && in_3_6_branch; then 71 # Enable PSA-based config (necessary to use drivers) 72 # MBEDTLS_PSA_CRYPTO_CONFIG is a legacy setting which should only be set on 3.6 LTS branches. 73 scripts/config.py set MBEDTLS_PSA_CRYPTO_CONFIG 74 75 # Dynamic secure element support is a deprecated feature and needs to be disabled here. 76 # This is done to have the same form of psa_key_attributes_s for libdriver and library. 77 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C 78 fi 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. 105 helper_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. 118 helper_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" 135 helper_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 if in_mbedtls_repo && in_3_6_branch; then 143 # Dynamic secure element support is a deprecated feature and it is not 144 # available when CRYPTO_C and PSA_CRYPTO_STORAGE_C are disabled. 145 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C 146 fi 147 # Disable potentially problematic features 148 scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT 149 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED 150 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED 151 scripts/config.py unset MBEDTLS_ECP_RESTARTABLE 152 scripts/config.py unset MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER 153 else 154 scripts/config.py crypto_full 155 scripts/config.py unset MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS 156 if in_mbedtls_repo && in_3_6_branch; then 157 # We need to match the client with MBEDTLS_PSA_CRYPTO_SE_C 158 scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C 159 fi 160 # Also ensure MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER not set (to match client) 161 scripts/config.py unset MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER 162 fi 163 } 164 165 # This is a helper function to be used in psasim builds. It is meant to clean 166 # up the library's workspace after the server build and before the client 167 # build. Built libraries (mbedcrypto, mbedx509 and mbedtls) are supposed to be 168 # already copied to psasim folder at this point. 169 helper_psasim_cleanup_before_client() { 170 # Clean up library files 171 make -C library clean 172 173 # Restore files that were backup before building library files. This 174 # includes $CONFIG_H and $CRYPTO_CONFIG_H. 175 restore_backed_up_files 176 } 177 178 # Helper to build the libraries for client/server in PSASIM. If the server is 179 # being built, then it builds also the final executable. 180 # There is only 1 mandatory parameter: 181 # - $1: target which can be "client" or "server" 182 helper_psasim_build() { 183 TARGET=$1 184 shift 185 TARGET_LIB=${TARGET}_libs 186 187 make -C $PSASIM_PATH CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" $TARGET_LIB "$@" 188 189 # Build also the server application after its libraries have been built. 190 if [ "$TARGET" == "server" ]; then 191 make -C $PSASIM_PATH CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" test/psa_server 192 fi 193 } 194 195 ################################################################ 196 #### Configuration helpers 197 ################################################################ 198 199 # When called with no parameter this function disables all builtin curves. 200 # The function optionally accepts 1 parameter: a space-separated list of the 201 # curves that should be kept enabled. 202 helper_disable_builtin_curves() { 203 allowed_list="${1:-}" 204 scripts/config.py unset-all "MBEDTLS_ECP_DP_[0-9A-Z_a-z]*_ENABLED" 205 206 for curve in $allowed_list; do 207 scripts/config.py set $curve 208 done 209 } 210 211 # Helper returning the list of supported elliptic curves from CRYPTO_CONFIG_H, 212 # without the "PSA_WANT_" prefix. This becomes handy for accelerating curves 213 # in the following helpers. 214 helper_get_psa_curve_list () { 215 loc_list="" 216 for item in $(sed -n 's/^#define PSA_WANT_\(ECC_[0-9A-Z_a-z]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do 217 loc_list="$loc_list $item" 218 done 219 220 echo "$loc_list" 221 } 222 223 # Helper returning the list of supported DH groups from CRYPTO_CONFIG_H, 224 # without the "PSA_WANT_" prefix. This becomes handy for accelerating DH groups 225 # in the following helpers. 226 helper_get_psa_dh_group_list () { 227 loc_list="" 228 for item in $(sed -n 's/^#define PSA_WANT_\(DH_RFC7919_[0-9]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do 229 loc_list="$loc_list $item" 230 done 231 232 echo "$loc_list" 233 } 234 235 # Get the list of uncommented PSA_WANT_KEY_TYPE_xxx_ from CRYPTO_CONFIG_H. This 236 # is useful to easily get a list of key type symbols to accelerate. 237 # The function accepts a single argument which is the key type: ECC, DH, RSA. 238 helper_get_psa_key_type_list() { 239 key_type="$1" 240 loc_list="" 241 for item in $(sed -n "s/^#define PSA_WANT_\(KEY_TYPE_${key_type}_[0-9A-Z_a-z]*\).*/\1/p" <"$CRYPTO_CONFIG_H"); do 242 # Skip DERIVE for elliptic keys since there is no driver dispatch for 243 # it so it cannot be accelerated. 244 if [ "$item" != "KEY_TYPE_ECC_KEY_PAIR_DERIVE" ]; then 245 loc_list="$loc_list $item" 246 fi 247 done 248 249 echo "$loc_list" 250 } 251 252 ################################################################ 253 #### Misc. helpers for components 254 ################################################################ 255 256 helper_armc6_build_test() 257 { 258 FLAGS="$1" 259 260 msg "build: ARM Compiler 6 ($FLAGS)" 261 262 make clean 263 ARM_TOOL_VARIANT="ult" CC="$ARMC6_CC" AR="$ARMC6_AR" CFLAGS="$FLAGS" \ 264 WARNING_CFLAGS='-Werror -xc -std=c99' make lib 265 266 msg "size: ARM Compiler 6 ($FLAGS)" 267 "$ARMC6_FROMELF" -z library/*.o 268 if [ -n "${PSA_CORE_PATH}" ]; then 269 "$ARMC6_FROMELF" -z ${PSA_CORE_PATH}/*.o 270 fi 271 if [ -n "${BUILTIN_SRC_PATH}" ]; then 272 "$ARMC6_FROMELF" -z ${BUILTIN_SRC_PATH}/*.o 273 fi 274 } 275 276 helper_armc6_cmake_build_test() 277 { 278 FLAGS="$1" 279 280 msg "build: CMake + ARM Compiler 6 ($FLAGS)" 281 282 cmake -DCMAKE_SYSTEM_NAME="Generic" -DCMAKE_SYSTEM_PROCESSOR="cortex-m0" \ 283 -DCMAKE_C_COMPILER="$ARMC6_CC" -DCMAKE_C_LINKER="$ARMC6_LINK" \ 284 -DCMAKE_AR="$ARMC6_AR" -DCMAKE_C_FLAGS="$FLAGS" \ 285 -DCMAKE_C_COMPILER_WORKS=TRUE -DENABLE_TESTING=OFF \ 286 -DENABLE_PROGRAMS=OFF "$TF_PSA_CRYPTO_ROOT_DIR" 287 make 288 289 msg "size: ARM Compiler 6 ($FLAGS)" 290 "$ARMC6_FROMELF" -z ${PSA_CORE_PATH}/CMakeFiles/tfpsacrypto.dir/*.o 291 "$ARMC6_FROMELF" -z ${BUILTIN_SRC_PATH}/../CMakeFiles/builtin.dir/src/*.o 292 } 293 294 clang_version() { 295 if command -v clang > /dev/null ; then 296 clang --version|grep version|sed -E 's#.*version ([0-9]+).*#\1#' 297 else 298 echo 0 # report version 0 for "no clang" 299 fi 300 } 301 302 gcc_version() { 303 gcc="$1" 304 if command -v "$gcc" > /dev/null ; then 305 "$gcc" --version | sed -En '1s/^[^ ]* \([^)]*\) ([0-9]+).*/\1/p' 306 else 307 echo 0 # report version 0 for "no gcc" 308 fi 309 } 310 311 can_run_cc_output() { 312 cc="$1" 313 result=false 314 if type "$cc" >/dev/null 2>&1; then 315 testbin=$(mktemp) 316 if echo 'int main(void){return 0;}' | "$cc" -o "$testbin" -x c -; then 317 if "$testbin" 2>/dev/null; then 318 result=true 319 fi 320 fi 321 rm -f "$testbin" 322 fi 323 $result 324 } 325 326 can_run_arm_linux_gnueabi= 327 can_run_arm_linux_gnueabi () { 328 if [ -z "$can_run_arm_linux_gnueabi" ]; then 329 if can_run_cc_output "${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc"; then 330 can_run_arm_linux_gnueabi=true 331 else 332 can_run_arm_linux_gnueabi=false 333 fi 334 fi 335 $can_run_arm_linux_gnueabi 336 } 337 338 can_run_arm_linux_gnueabihf= 339 can_run_arm_linux_gnueabihf () { 340 if [ -z "$can_run_arm_linux_gnueabihf" ]; then 341 if can_run_cc_output "${ARM_LINUX_GNUEABIHF_GCC_PREFIX}gcc"; then 342 can_run_arm_linux_gnueabihf=true 343 else 344 can_run_arm_linux_gnueabihf=false 345 fi 346 fi 347 $can_run_arm_linux_gnueabihf 348 } 349 350 can_run_aarch64_linux_gnu= 351 can_run_aarch64_linux_gnu () { 352 if [ -z "$can_run_aarch64_linux_gnu" ]; then 353 if can_run_cc_output "${AARCH64_LINUX_GNU_GCC_PREFIX}gcc"; then 354 can_run_aarch64_linux_gnu=true 355 else 356 can_run_aarch64_linux_gnu=false 357 fi 358 fi 359 $can_run_aarch64_linux_gnu 360 }