1#! /usr/bin/env bash
2
3# all.sh
4#
5# Copyright The Mbed TLS Contributors
6# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7
8
9
10################################################################
11#### Documentation
12################################################################
13
14# Purpose
15# -------
16#
17# To run all tests possible or available on the platform.
18#
19# Notes for users
20# ---------------
21#
22# Warning: the test is destructive. It includes various build modes and
23# configurations, and can and will arbitrarily change the current CMake
24# configuration. The following files must be committed into git:
25#    * include/mbedtls/mbedtls_config.h
26#    * Makefile, library/Makefile, programs/Makefile, tests/Makefile,
27#      programs/fuzz/Makefile
28# After running this script, the CMake cache will be lost and CMake
29# will no longer be initialised.
30#
31# The script assumes the presence of a number of tools:
32#   * Basic Unix tools (Windows users note: a Unix-style find must be before
33#     the Windows find in the PATH)
34#   * Perl
35#   * GNU Make
36#   * CMake
37#   * GCC and Clang (recent enough for using ASan with gcc and MemSan with clang, or valgrind)
38#   * G++
39#   * arm-gcc and mingw-gcc
40#   * ArmCC 5 and ArmCC 6, unless invoked with --no-armcc
41#   * OpenSSL and GnuTLS command line tools, in suitable versions for the
42#     interoperability tests. The following are the official versions at the
43#     time of writing:
44#     * GNUTLS_{CLI,SERV} = 3.4.10
45#     * GNUTLS_NEXT_{CLI,SERV} = 3.7.2
46#     * OPENSSL = 1.0.2g (without Debian/Ubuntu patches)
47#     * OPENSSL_NEXT = 1.1.1a
48# See the invocation of check_tools below for details.
49#
50# This script must be invoked from the toplevel directory of a git
51# working copy of Mbed TLS.
52#
53# The behavior on an error depends on whether --keep-going (alias -k)
54# is in effect.
55#  * Without --keep-going: the script stops on the first error without
56#    cleaning up. This lets you work in the configuration of the failing
57#    component.
58#  * With --keep-going: the script runs all requested components and
59#    reports failures at the end. In particular the script always cleans
60#    up on exit.
61#
62# Note that the output is not saved. You may want to run
63#   script -c tests/scripts/all.sh
64# or
65#   tests/scripts/all.sh >all.log 2>&1
66#
67# Notes for maintainers
68# ---------------------
69#
70# The bulk of the code is organized into functions that follow one of the
71# following naming conventions:
72#  * pre_XXX: things to do before running the tests, in order.
73#  * component_XXX: independent components. They can be run in any order.
74#      * component_check_XXX: quick tests that aren't worth parallelizing.
75#      * component_build_XXX: build things but don't run them.
76#      * component_test_XXX: build and test.
77#      * component_release_XXX: tests that the CI should skip during PR testing.
78#  * support_XXX: if support_XXX exists and returns false then
79#    component_XXX is not run by default.
80#  * post_XXX: things to do after running the tests.
81#  * other: miscellaneous support functions.
82#
83# Each component must start by invoking `msg` with a short informative message.
84#
85# Warning: due to the way bash detects errors, the failure of a command
86# inside 'if' or '!' is not detected. Use the 'not' function instead of '!'.
87#
88# Each component is executed in a separate shell process. The component
89# fails if any command in it returns a non-zero status.
90#
91# The framework performs some cleanup tasks after each component. This
92# means that components can assume that the working directory is in a
93# cleaned-up state, and don't need to perform the cleanup themselves.
94# * Run `make clean`.
95# * Restore `include/mbedtls/mbedtls_config.h` from a backup made before running
96#   the component.
97# * Check out `Makefile`, `library/Makefile`, `programs/Makefile`,
98#   `tests/Makefile` and `programs/fuzz/Makefile` from git.
99#   This cleans up after an in-tree use of CMake.
100#
101# The tests are roughly in order from fastest to slowest. This doesn't
102# have to be exact, but in general you should add slower tests towards
103# the end and fast checks near the beginning.
104
105
106
107################################################################
108#### Initialization and command line parsing
109################################################################
110
111# Abort on errors (even on the left-hand side of a pipe).
112# Treat uninitialised variables as errors.
113set -e -o pipefail -u
114
115# Enable ksh/bash extended file matching patterns
116shopt -s extglob
117
118in_mbedtls_repo () {
119    test -d include -a -d library -a -d programs -a -d tests
120}
121
122in_tf_psa_crypto_repo () {
123    test -d include -a -d core -a -d drivers -a -d programs -a -d tests
124}
125
126pre_check_environment () {
127    if in_mbedtls_repo || in_tf_psa_crypto_repo; then :; else
128        echo "Must be run from Mbed TLS / TF-PSA-Crypto root" >&2
129        exit 1
130    fi
131}
132
133pre_initialize_variables () {
134    if in_mbedtls_repo; then
135        CONFIG_H='include/mbedtls/mbedtls_config.h'
136    else
137        CONFIG_H='drivers/builtin/include/mbedtls/mbedtls_config.h'
138    fi
139    CRYPTO_CONFIG_H='include/psa/crypto_config.h'
140    CONFIG_TEST_DRIVER_H='tests/include/test/drivers/config_test_driver.h'
141
142    # Files that are clobbered by some jobs will be backed up. Use a different
143    # suffix from auxiliary scripts so that all.sh and auxiliary scripts can
144    # independently decide when to remove the backup file.
145    backup_suffix='.all.bak'
146    # Files clobbered by config.py
147    files_to_back_up="$CONFIG_H $CRYPTO_CONFIG_H $CONFIG_TEST_DRIVER_H"
148    if in_mbedtls_repo; then
149        # Files clobbered by in-tree cmake
150        files_to_back_up="$files_to_back_up Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile"
151    fi
152
153    append_outcome=0
154    MEMORY=0
155    FORCE=0
156    QUIET=0
157    KEEP_GOING=0
158
159    # Seed value used with the --release-test option.
160    #
161    # See also RELEASE_SEED in basic-build-test.sh. Debugging is easier if
162    # both values are kept in sync. If you change the value here because it
163    # breaks some tests, you'll definitely want to change it in
164    # basic-build-test.sh as well.
165    RELEASE_SEED=1
166
167    # Specify character collation for regular expressions and sorting with C locale
168    export LC_COLLATE=C
169
170    : ${MBEDTLS_TEST_OUTCOME_FILE=}
171    : ${MBEDTLS_TEST_PLATFORM="$(uname -s | tr -c \\n0-9A-Za-z _)-$(uname -m | tr -c \\n0-9A-Za-z _)"}
172    export MBEDTLS_TEST_OUTCOME_FILE
173    export MBEDTLS_TEST_PLATFORM
174
175    # Default commands, can be overridden by the environment
176    : ${OPENSSL:="openssl"}
177    : ${OPENSSL_NEXT:="$OPENSSL"}
178    : ${GNUTLS_CLI:="gnutls-cli"}
179    : ${GNUTLS_SERV:="gnutls-serv"}
180    : ${OUT_OF_SOURCE_DIR:=./mbedtls_out_of_source_build}
181    : ${ARMC5_BIN_DIR:=/usr/bin}
182    : ${ARMC6_BIN_DIR:=/usr/bin}
183    : ${ARM_NONE_EABI_GCC_PREFIX:=arm-none-eabi-}
184    : ${ARM_LINUX_GNUEABI_GCC_PREFIX:=arm-linux-gnueabi-}
185    : ${CLANG_LATEST:="clang-latest"}
186    : ${CLANG_EARLIEST:="clang-earliest"}
187    : ${GCC_LATEST:="gcc-latest"}
188    : ${GCC_EARLIEST:="gcc-earliest"}
189    # if MAKEFLAGS is not set add the -j option to speed up invocations of make
190    if [ -z "${MAKEFLAGS+set}" ]; then
191        export MAKEFLAGS="-j$(all_sh_nproc)"
192    fi
193    # if CC is not set, use clang by default (if present) to improve build times
194    if [ -z "${CC+set}" ] && (type clang > /dev/null 2>&1); then
195        export CC="clang"
196    fi
197
198    # Include more verbose output for failing tests run by CMake or make
199    export CTEST_OUTPUT_ON_FAILURE=1
200
201    # CFLAGS and LDFLAGS for Asan builds that don't use CMake
202    # default to -O2, use -Ox _after_ this if you want another level
203    ASAN_CFLAGS='-O2 -Werror -fsanitize=address,undefined -fno-sanitize-recover=all'
204    # Normally, tests should use this compiler for ASAN testing
205    ASAN_CC=clang
206
207    # Platform tests have an allocation that returns null
208    export ASAN_OPTIONS="allocator_may_return_null=1"
209    export MSAN_OPTIONS="allocator_may_return_null=1"
210
211    # Gather the list of available components. These are the functions
212    # defined in this script whose name starts with "component_".
213    ALL_COMPONENTS=$(compgen -A function component_ | sed 's/component_//')
214
215    # Delay determining SUPPORTED_COMPONENTS until the command line options have a chance to override
216    # the commands set by the environment
217}
218
219setup_quiet_wrappers()
220{
221    # Pick up "quiet" wrappers for make and cmake, which don't output very much
222    # unless there is an error. This reduces logging overhead in the CI.
223    #
224    # Note that the cmake wrapper breaks unless we use an absolute path here.
225    if [[ -e ${PWD}/tests/scripts/quiet ]]; then
226        export PATH=${PWD}/tests/scripts/quiet:$PATH
227    fi
228}
229
230# Test whether the component $1 is included in the command line patterns.
231is_component_included()
232{
233    # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS
234    # only does word splitting.
235    set -f
236    for pattern in $COMMAND_LINE_COMPONENTS; do
237        set +f
238        case ${1#component_} in $pattern) return 0;; esac
239    done
240    set +f
241    return 1
242}
243
244usage()
245{
246    cat <<EOF
247Usage: $0 [OPTION]... [COMPONENT]...
248Run mbedtls release validation tests.
249By default, run all tests. With one or more COMPONENT, run only those.
250COMPONENT can be the name of a component or a shell wildcard pattern.
251
252Examples:
253  $0 "check_*"
254    Run all sanity checks.
255  $0 --no-armcc --except test_memsan
256    Run everything except builds that require armcc and MemSan.
257
258Special options:
259  -h|--help             Print this help and exit.
260  --list-all-components List all available test components and exit.
261  --list-components     List components supported on this platform and exit.
262
263General options:
264  -q|--quiet            Only output component names, and errors if any.
265  -f|--force            Force the tests to overwrite any modified files.
266  -k|--keep-going       Run all tests and report errors at the end.
267  -m|--memory           Additional optional memory tests.
268     --append-outcome   Append to the outcome file (if used).
269     --arm-none-eabi-gcc-prefix=<string>
270                        Prefix for a cross-compiler for arm-none-eabi
271                        (default: "${ARM_NONE_EABI_GCC_PREFIX}")
272     --arm-linux-gnueabi-gcc-prefix=<string>
273                        Prefix for a cross-compiler for arm-linux-gnueabi
274                        (default: "${ARM_LINUX_GNUEABI_GCC_PREFIX}")
275     --armcc            Run ARM Compiler builds (on by default).
276     --restore          First clean up the build tree, restoring backed up
277                        files. Do not run any components unless they are
278                        explicitly specified.
279     --error-test       Error test mode: run a failing function in addition
280                        to any specified component. May be repeated.
281     --except           Exclude the COMPONENTs listed on the command line,
282                        instead of running only those.
283     --no-append-outcome    Write a new outcome file and analyze it (default).
284     --no-armcc         Skip ARM Compiler builds.
285     --no-force         Refuse to overwrite modified files (default).
286     --no-keep-going    Stop at the first error (default).
287     --no-memory        No additional memory tests (default).
288     --no-quiet         Print full output from components.
289     --out-of-source-dir=<path>  Directory used for CMake out-of-source build tests.
290     --outcome-file=<path>  File where test outcomes are written (not done if
291                            empty; default: \$MBEDTLS_TEST_OUTCOME_FILE).
292     --random-seed      Use a random seed value for randomized tests (default).
293  -r|--release-test     Run this script in release mode. This fixes the seed value to ${RELEASE_SEED}.
294  -s|--seed             Integer seed value to use for this test run.
295
296Tool path options:
297     --armc5-bin-dir=<ARMC5_bin_dir_path>       ARM Compiler 5 bin directory.
298     --armc6-bin-dir=<ARMC6_bin_dir_path>       ARM Compiler 6 bin directory.
299     --clang-earliest=<Clang_earliest_path>     Earliest version of clang available
300     --clang-latest=<Clang_latest_path>         Latest version of clang available
301     --gcc-earliest=<GCC_earliest_path>         Earliest version of GCC available
302     --gcc-latest=<GCC_latest_path>             Latest version of GCC available
303     --gnutls-cli=<GnuTLS_cli_path>             GnuTLS client executable to use for most tests.
304     --gnutls-serv=<GnuTLS_serv_path>           GnuTLS server executable to use for most tests.
305     --openssl=<OpenSSL_path>                   OpenSSL executable to use for most tests.
306     --openssl-next=<OpenSSL_path>              OpenSSL executable to use for recent things like ARIA
307EOF
308}
309
310# Cleanup before/after running a component.
311# Remove built files as well as the cmake cache/config.
312# Does not remove generated source files.
313cleanup()
314{
315    if in_mbedtls_repo; then
316        command make clean
317    fi
318
319    # Remove CMake artefacts
320    find . -name .git -prune -o \
321           -iname CMakeFiles -exec rm -rf {} \+ -o \
322           \( -iname cmake_install.cmake -o \
323              -iname CTestTestfile.cmake -o \
324              -iname CMakeCache.txt -o \
325              -path './cmake/*.cmake' \) -exec rm -f {} \+
326    # Recover files overwritten by in-tree CMake builds
327    rm -f include/Makefile include/mbedtls/Makefile programs/!(fuzz)/Makefile
328
329    # Remove any artifacts from the component_test_cmake_as_subdirectory test.
330    rm -rf programs/test/cmake_subproject/build
331    rm -f programs/test/cmake_subproject/Makefile
332    rm -f programs/test/cmake_subproject/cmake_subproject
333
334    # Remove any artifacts from the component_test_cmake_as_package test.
335    rm -rf programs/test/cmake_package/build
336    rm -f programs/test/cmake_package/Makefile
337    rm -f programs/test/cmake_package/cmake_package
338
339    # Remove any artifacts from the component_test_cmake_as_installed_package test.
340    rm -rf programs/test/cmake_package_install/build
341    rm -f programs/test/cmake_package_install/Makefile
342    rm -f programs/test/cmake_package_install/cmake_package_install
343
344    # Restore files that may have been clobbered by the job
345    for x in $files_to_back_up; do
346        if [[ -e "$x$backup_suffix" ]]; then
347            cp -p "$x$backup_suffix" "$x"
348        fi
349    done
350}
351
352# Final cleanup when this script exits (except when exiting on a failure
353# in non-keep-going mode).
354final_cleanup () {
355    cleanup
356
357    for x in $files_to_back_up; do
358        rm -f "$x$backup_suffix"
359    done
360}
361
362# Executed on exit. May be redefined depending on command line options.
363final_report () {
364    :
365}
366
367fatal_signal () {
368    final_cleanup
369    final_report $1
370    trap - $1
371    kill -$1 $$
372}
373
374trap 'fatal_signal HUP' HUP
375trap 'fatal_signal INT' INT
376trap 'fatal_signal TERM' TERM
377
378# Number of processors on this machine. Used as the default setting
379# for parallel make.
380all_sh_nproc ()
381{
382    {
383        nproc || # Linux
384        sysctl -n hw.ncpuonline || # NetBSD, OpenBSD
385        sysctl -n hw.ncpu || # FreeBSD
386        echo 1
387    } 2>/dev/null
388}
389
390msg()
391{
392    if [ -n "${current_component:-}" ]; then
393        current_section="${current_component#component_}: $1"
394    else
395        current_section="$1"
396    fi
397
398    if [ $QUIET -eq 1 ]; then
399        return
400    fi
401
402    echo ""
403    echo "******************************************************************"
404    echo "* $current_section "
405    printf "* "; date
406    echo "******************************************************************"
407}
408
409armc6_build_test()
410{
411    FLAGS="$1"
412
413    msg "build: ARM Compiler 6 ($FLAGS)"
414    make clean
415    ARM_TOOL_VARIANT="ult" CC="$ARMC6_CC" AR="$ARMC6_AR" CFLAGS="$FLAGS" \
416                    WARNING_CFLAGS='-Werror -xc -std=c99' make lib
417
418    msg "size: ARM Compiler 6 ($FLAGS)"
419    "$ARMC6_FROMELF" -z library/*.o
420}
421
422err_msg()
423{
424    echo "$1" >&2
425}
426
427check_tools()
428{
429    for tool in "$@"; do
430        if ! `type "$tool" >/dev/null 2>&1`; then
431            err_msg "$tool not found!"
432            exit 1
433        fi
434    done
435}
436
437pre_parse_command_line () {
438    COMMAND_LINE_COMPONENTS=
439    all_except=0
440    error_test=0
441    list_components=0
442    restore_first=0
443    no_armcc=
444
445    # Note that legacy options are ignored instead of being omitted from this
446    # list of options, so invocations that worked with previous version of
447    # all.sh will still run and work properly.
448    while [ $# -gt 0 ]; do
449        case "$1" in
450            --append-outcome) append_outcome=1;;
451            --arm-none-eabi-gcc-prefix) shift; ARM_NONE_EABI_GCC_PREFIX="$1";;
452            --arm-linux-gnueabi-gcc-prefix) shift; ARM_LINUX_GNUEABI_GCC_PREFIX="$1";;
453            --armcc) no_armcc=;;
454            --armc5-bin-dir) shift; ARMC5_BIN_DIR="$1";;
455            --armc6-bin-dir) shift; ARMC6_BIN_DIR="$1";;
456            --clang-earliest) shift; CLANG_EARLIEST="$1";;
457            --clang-latest) shift; CLANG_LATEST="$1";;
458            --error-test) error_test=$((error_test + 1));;
459            --except) all_except=1;;
460            --force|-f) FORCE=1;;
461            --gcc-earliest) shift; GCC_EARLIEST="$1";;
462            --gcc-latest) shift; GCC_LATEST="$1";;
463            --gnutls-cli) shift; GNUTLS_CLI="$1";;
464            --gnutls-legacy-cli) shift;; # ignored for backward compatibility
465            --gnutls-legacy-serv) shift;; # ignored for backward compatibility
466            --gnutls-serv) shift; GNUTLS_SERV="$1";;
467            --help|-h) usage; exit;;
468            --keep-going|-k) KEEP_GOING=1;;
469            --list-all-components) printf '%s\n' $ALL_COMPONENTS; exit;;
470            --list-components) list_components=1;;
471            --memory|-m) MEMORY=1;;
472            --no-append-outcome) append_outcome=0;;
473            --no-armcc) no_armcc=1;;
474            --no-force) FORCE=0;;
475            --no-keep-going) KEEP_GOING=0;;
476            --no-memory) MEMORY=0;;
477            --no-quiet) QUIET=0;;
478            --openssl) shift; OPENSSL="$1";;
479            --openssl-next) shift; OPENSSL_NEXT="$1";;
480            --outcome-file) shift; MBEDTLS_TEST_OUTCOME_FILE="$1";;
481            --out-of-source-dir) shift; OUT_OF_SOURCE_DIR="$1";;
482            --quiet|-q) QUIET=1;;
483            --random-seed) unset SEED;;
484            --release-test|-r) SEED=$RELEASE_SEED;;
485            --restore) restore_first=1;;
486            --seed|-s) shift; SEED="$1";;
487            -*)
488                echo >&2 "Unknown option: $1"
489                echo >&2 "Run $0 --help for usage."
490                exit 120
491                ;;
492            *) COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS $1";;
493        esac
494        shift
495    done
496
497    # Exclude components that are not supported on this platform.
498    SUPPORTED_COMPONENTS=
499    for component in $ALL_COMPONENTS; do
500        case $(type "support_$component" 2>&1) in
501            *' function'*)
502                if ! support_$component; then continue; fi;;
503        esac
504        SUPPORTED_COMPONENTS="$SUPPORTED_COMPONENTS $component"
505    done
506
507    if [ $list_components -eq 1 ]; then
508        printf '%s\n' $SUPPORTED_COMPONENTS
509        exit
510    fi
511
512    # With no list of components, run everything.
513    if [ -z "$COMMAND_LINE_COMPONENTS" ] && [ $restore_first -eq 0 ]; then
514        all_except=1
515    fi
516
517    # --no-armcc is a legacy option. The modern way is --except '*_armcc*'.
518    # Ignore it if components are listed explicitly on the command line.
519    if [ -n "$no_armcc" ] && [ $all_except -eq 1 ]; then
520        COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS *_armcc*"
521    fi
522
523    # Error out if an explicitly requested component doesn't exist.
524    if [ $all_except -eq 0 ]; then
525        unsupported=0
526        # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS
527        # only does word splitting.
528        set -f
529        for component in $COMMAND_LINE_COMPONENTS; do
530            set +f
531            # If the requested name includes a wildcard character, don't
532            # check it. Accept wildcard patterns that don't match anything.
533            case $component in
534                *[*?\[]*) continue;;
535            esac
536            case " $SUPPORTED_COMPONENTS " in
537                *" $component "*) :;;
538                *)
539                    echo >&2 "Component $component was explicitly requested, but is not known or not supported."
540                    unsupported=$((unsupported + 1));;
541            esac
542        done
543        set +f
544        if [ $unsupported -ne 0 ]; then
545            exit 2
546        fi
547    fi
548
549    # Build the list of components to run.
550    RUN_COMPONENTS=
551    for component in $SUPPORTED_COMPONENTS; do
552        if is_component_included "$component"; [ $? -eq $all_except ]; then
553            RUN_COMPONENTS="$RUN_COMPONENTS $component"
554        fi
555    done
556
557    unset all_except
558    unset no_armcc
559}
560
561pre_check_git () {
562    if [ $FORCE -eq 1 ]; then
563        rm -rf "$OUT_OF_SOURCE_DIR"
564        git checkout-index -f -q $CONFIG_H
565        cleanup
566    else
567
568        if [ -d "$OUT_OF_SOURCE_DIR" ]; then
569            echo "Warning - there is an existing directory at '$OUT_OF_SOURCE_DIR'" >&2
570            echo "You can either delete this directory manually, or force the test by rerunning"
571            echo "the script as: $0 --force --out-of-source-dir $OUT_OF_SOURCE_DIR"
572            exit 1
573        fi
574
575        if ! git diff --quiet "$CONFIG_H"; then
576            err_msg "Warning - the configuration file '$CONFIG_H' has been edited. "
577            echo "You can either delete or preserve your work, or force the test by rerunning the"
578            echo "script as: $0 --force"
579            exit 1
580        fi
581    fi
582}
583
584pre_restore_files () {
585    # If the makefiles have been generated by a framework such as cmake,
586    # restore them from git. If the makefiles look like modifications from
587    # the ones checked into git, take care not to modify them. Whatever
588    # this function leaves behind is what the script will restore before
589    # each component.
590    case "$(head -n1 Makefile)" in
591        *[Gg]enerated*)
592            git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile
593            git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile
594            ;;
595    esac
596}
597
598pre_back_up () {
599    for x in $files_to_back_up; do
600        cp -p "$x" "$x$backup_suffix"
601    done
602}
603
604pre_setup_keep_going () {
605    failure_count=0 # Number of failed components
606    last_failure_status=0 # Last failure status in this component
607
608    # See err_trap
609    previous_failure_status=0
610    previous_failed_command=
611    previous_failure_funcall_depth=0
612    unset report_failed_command
613
614    start_red=
615    end_color=
616    if [ -t 1 ]; then
617        case "${TERM:-}" in
618            *color*|cygwin|linux|rxvt*|screen|[Eex]term*)
619                start_red=$(printf '\033[31m')
620                end_color=$(printf '\033[0m')
621                ;;
622        esac
623    fi
624
625    # Keep a summary of failures in a file. We'll print it out at the end.
626    failure_summary_file=$PWD/all-sh-failures-$$.log
627    : >"$failure_summary_file"
628
629    # Whether it makes sense to keep a component going after the specified
630    # command fails (test command) or not (configure or build).
631    # This function normally receives the failing simple command
632    # ($BASH_COMMAND) as an argument, but if $report_failed_command is set,
633    # this is passed instead.
634    # This doesn't have to be 100% accurate: all failures are recorded anyway.
635    # False positives result in running things that can't be expected to
636    # work. False negatives result in things not running after something else
637    # failed even though they might have given useful feedback.
638    can_keep_going_after_failure () {
639        case "$1" in
640            "msg "*) false;;
641            "cd "*) false;;
642            "diff "*) true;;
643            *make*[\ /]tests*) false;; # make tests, make CFLAGS=-I../tests, ...
644            *test*) true;; # make test, tests/stuff, env V=v tests/stuff, ...
645            *make*check*) true;;
646            "grep "*) true;;
647            "[ "*) true;;
648            "! "*) true;;
649            *) false;;
650        esac
651    }
652
653    # This function runs if there is any error in a component.
654    # It must either exit with a nonzero status, or set
655    # last_failure_status to a nonzero value.
656    err_trap () {
657        # Save $? (status of the failing command). This must be the very
658        # first thing, before $? is overridden.
659        last_failure_status=$?
660        failed_command=${report_failed_command-$BASH_COMMAND}
661
662        if [[ $last_failure_status -eq $previous_failure_status &&
663              "$failed_command" == "$previous_failed_command" &&
664              ${#FUNCNAME[@]} == $((previous_failure_funcall_depth - 1)) ]]
665        then
666            # The same command failed twice in a row, but this time one level
667            # less deep in the function call stack. This happens when the last
668            # command of a function returns a nonzero status, and the function
669            # returns that same status. Ignore the second failure.
670            previous_failure_funcall_depth=${#FUNCNAME[@]}
671            return
672        fi
673        previous_failure_status=$last_failure_status
674        previous_failed_command=$failed_command
675        previous_failure_funcall_depth=${#FUNCNAME[@]}
676
677        text="$current_section: $failed_command -> $last_failure_status"
678        echo "${start_red}^^^^$text^^^^${end_color}" >&2
679        echo "$text" >>"$failure_summary_file"
680
681        # If the command is fatal (configure or build command), stop this
682        # component. Otherwise (test command) keep the component running
683        # (run more tests from the same build).
684        if ! can_keep_going_after_failure "$failed_command"; then
685            exit $last_failure_status
686        fi
687    }
688
689    final_report () {
690        if [ $failure_count -gt 0 ]; then
691            echo
692            echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
693            echo "${start_red}FAILED: $failure_count components${end_color}"
694            cat "$failure_summary_file"
695            echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
696        elif [ -z "${1-}" ]; then
697            echo "SUCCESS :)"
698        fi
699        if [ -n "${1-}" ]; then
700            echo "Killed by SIG$1."
701        fi
702        rm -f "$failure_summary_file"
703        if [ $failure_count -gt 0 ]; then
704            exit 1
705        fi
706    }
707}
708
709# record_status() and if_build_succeeded() are kept temporarily for backward
710# compatibility. Don't use them in new components.
711record_status () {
712    "$@"
713}
714if_build_succeeded () {
715    "$@"
716}
717
718# '! true' does not trigger the ERR trap. Arrange to trigger it, with
719# a reasonably informative error message (not just "$@").
720not () {
721    if "$@"; then
722        report_failed_command="! $*"
723        false
724        unset report_failed_command
725    fi
726}
727
728pre_prepare_outcome_file () {
729    case "$MBEDTLS_TEST_OUTCOME_FILE" in
730      [!/]*) MBEDTLS_TEST_OUTCOME_FILE="$PWD/$MBEDTLS_TEST_OUTCOME_FILE";;
731    esac
732    if [ -n "$MBEDTLS_TEST_OUTCOME_FILE" ] && [ "$append_outcome" -eq 0 ]; then
733        rm -f "$MBEDTLS_TEST_OUTCOME_FILE"
734    fi
735}
736
737pre_print_configuration () {
738    if [ $QUIET -eq 1 ]; then
739        return
740    fi
741
742    msg "info: $0 configuration"
743    echo "MEMORY: $MEMORY"
744    echo "FORCE: $FORCE"
745    echo "MBEDTLS_TEST_OUTCOME_FILE: ${MBEDTLS_TEST_OUTCOME_FILE:-(none)}"
746    echo "SEED: ${SEED-"UNSET"}"
747    echo
748    echo "OPENSSL: $OPENSSL"
749    echo "OPENSSL_NEXT: $OPENSSL_NEXT"
750    echo "GNUTLS_CLI: $GNUTLS_CLI"
751    echo "GNUTLS_SERV: $GNUTLS_SERV"
752    echo "ARMC5_BIN_DIR: $ARMC5_BIN_DIR"
753    echo "ARMC6_BIN_DIR: $ARMC6_BIN_DIR"
754}
755
756# Make sure the tools we need are available.
757pre_check_tools () {
758    # Build the list of variables to pass to output_env.sh.
759    set env
760
761    case " $RUN_COMPONENTS " in
762        # Require OpenSSL and GnuTLS if running any tests (as opposed to
763        # only doing builds). Not all tests run OpenSSL and GnuTLS, but this
764        # is a good enough approximation in practice.
765        *" test_"* | *" release_test_"*)
766            # To avoid setting OpenSSL and GnuTLS for each call to compat.sh
767            # and ssl-opt.sh, we just export the variables they require.
768            export OPENSSL="$OPENSSL"
769            export GNUTLS_CLI="$GNUTLS_CLI"
770            export GNUTLS_SERV="$GNUTLS_SERV"
771            # Avoid passing --seed flag in every call to ssl-opt.sh
772            if [ -n "${SEED-}" ]; then
773                export SEED
774            fi
775            set "$@" OPENSSL="$OPENSSL"
776            set "$@" GNUTLS_CLI="$GNUTLS_CLI" GNUTLS_SERV="$GNUTLS_SERV"
777            check_tools "$OPENSSL" "$OPENSSL_NEXT" \
778                        "$GNUTLS_CLI" "$GNUTLS_SERV"
779            ;;
780    esac
781
782    case " $RUN_COMPONENTS " in
783        *_doxygen[_\ ]*) check_tools "doxygen" "dot";;
784    esac
785
786    case " $RUN_COMPONENTS " in
787        *_arm_none_eabi_gcc[_\ ]*) check_tools "${ARM_NONE_EABI_GCC_PREFIX}gcc";;
788    esac
789
790    case " $RUN_COMPONENTS " in
791        *_mingw[_\ ]*) check_tools "i686-w64-mingw32-gcc";;
792    esac
793
794    case " $RUN_COMPONENTS " in
795        *" test_zeroize "*) check_tools "gdb";;
796    esac
797
798    case " $RUN_COMPONENTS " in
799        *_armcc*)
800            ARMC5_CC="$ARMC5_BIN_DIR/armcc"
801            ARMC5_AR="$ARMC5_BIN_DIR/armar"
802            ARMC5_FROMELF="$ARMC5_BIN_DIR/fromelf"
803            ARMC6_CC="$ARMC6_BIN_DIR/armclang"
804            ARMC6_AR="$ARMC6_BIN_DIR/armar"
805            ARMC6_FROMELF="$ARMC6_BIN_DIR/fromelf"
806            check_tools "$ARMC5_CC" "$ARMC5_AR" "$ARMC5_FROMELF" \
807                        "$ARMC6_CC" "$ARMC6_AR" "$ARMC6_FROMELF";;
808    esac
809
810    # past this point, no call to check_tool, only printing output
811    if [ $QUIET -eq 1 ]; then
812        return
813    fi
814
815    msg "info: output_env.sh"
816    case $RUN_COMPONENTS in
817        *_armcc*)
818            set "$@" ARMC5_CC="$ARMC5_CC" ARMC6_CC="$ARMC6_CC" RUN_ARMCC=1;;
819        *) set "$@" RUN_ARMCC=0;;
820    esac
821    "$@" scripts/output_env.sh
822}
823
824pre_generate_files() {
825    # since make doesn't have proper dependencies, remove any possibly outdate
826    # file that might be around before generating fresh ones
827    make neat
828    if [ $QUIET -eq 1 ]; then
829        make generated_files >/dev/null
830    else
831        make generated_files
832    fi
833}
834
835clang_version() {
836    if command -v clang > /dev/null ; then
837        clang --version|grep version|sed -E 's#.*version ([0-9]+).*#\1#'
838    else
839        echo 0  # report version 0 for "no clang"
840    fi
841}
842
843################################################################
844#### Helpers for components using libtestdriver1
845################################################################
846
847# How to use libtestdriver1
848# -------------------------
849#
850# 1. Define the list algorithms and key types to accelerate,
851#    designated the same way as PSA_WANT_ macros but without PSA_WANT_.
852#    Examples:
853#      - loc_accel_list="ALG_JPAKE"
854#      - loc_accel_list="ALG_FFDH KEY_TYPE_DH_KEY_PAIR KEY_TYPE_DH_PUBLIC_KEY"
855# 2. Make configurations changes for the driver and/or main libraries.
856#    2a. Call helper_libtestdriver1_adjust_config <base>, where the argument
857#        can be either "default" to start with the default config, or a name
858#        supported by scripts/config.py (for example, "full"). This selects
859#        the base to use, and makes common adjustments.
860#    2b. If desired, adjust the PSA_WANT symbols in psa/crypto_config.h.
861#        These changes affect both the driver and the main libraries.
862#        (Note: they need to have the same set of PSA_WANT symbols, as that
863#        determines the ABI between them.)
864#    2c. Adjust MBEDTLS_ symbols in mbedtls_config.h. This only affects the
865#        main libraries. Typically, you want to disable the module(s) that are
866#        being accelerated. You may need to also disable modules that depend
867#        on them or options that are not supported with drivers.
868#    2d. On top of psa/crypto_config.h, the driver library uses its own config
869#        file: tests/include/test/drivers/config_test_driver.h. You usually
870#        don't need to edit it: using loc_extra_list (see below) is preferred.
871#        However, when there's no PSA symbol for what you want to enable,
872#        calling scripts/config.py on this file remains the only option.
873# 3. Build the driver library, then the main libraries, test, and programs.
874#    3a. Call helper_libtestdriver1_make_drivers "$loc_accel_list". You may
875#        need to enable more algorithms here, typically hash algorithms when
876#        accelerating some signature algorithms (ECDSA, RSAv2). This is done
877#        by passing a 2nd argument listing the extra algorithms.
878#        Example:
879#          loc_extra_list="ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512"
880#          helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
881#    3b. Call helper_libtestdriver1_make_main "$loc_accel_list". Any
882#        additional arguments will be passed to make: this can be useful if
883#        you don't want to build everything when iterating during development.
884#        Example:
885#          helper_libtestdriver1_make_main "$loc_accel_list" -C tests test_suite_foo
886# 4. Run the tests you want.
887
888# Adjust the configuration - for both libtestdriver1 and main library,
889# as they should have the same PSA_WANT macros.
890helper_libtestdriver1_adjust_config() {
891    base_config=$1
892    # Select the base configuration
893    if [ "$base_config" != "default" ]; then
894        scripts/config.py "$base_config"
895    fi
896
897    # Enable PSA-based config (necessary to use drivers)
898    scripts/config.py set MBEDTLS_PSA_CRYPTO_CONFIG
899
900    # Dynamic secure element support is a deprecated feature and needs to be disabled here.
901    # This is done to have the same form of psa_key_attributes_s for libdriver and library.
902    scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
903
904    # If threading is enabled on the normal build, then we need to enable it in the drivers as well,
905    # otherwise we will end up running multithreaded tests without mutexes to protect them.
906    if scripts/config.py get MBEDTLS_THREADING_C; then
907        scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_THREADING_C
908    fi
909
910    if scripts/config.py get MBEDTLS_THREADING_PTHREAD; then
911        scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_THREADING_PTHREAD
912    fi
913}
914
915# When called with no parameter this function disables all builtin curves.
916# The function optionally accepts 1 parameter: a space-separated list of the
917# curves that should be kept enabled.
918helper_disable_builtin_curves() {
919    allowed_list="${1:-}"
920    scripts/config.py unset-all "MBEDTLS_ECP_DP_[0-9A-Z_a-z]*_ENABLED"
921
922    for curve in $allowed_list; do
923        scripts/config.py set $curve
924    done
925}
926
927# Helper returning the list of supported elliptic curves from CRYPTO_CONFIG_H,
928# without the "PSA_WANT_" prefix. This becomes handy for accelerating curves
929# in the following helpers.
930helper_get_psa_curve_list () {
931    loc_list=""
932    for item in $(sed -n 's/^#define PSA_WANT_\(ECC_[0-9A-Z_a-z]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do
933        loc_list="$loc_list $item"
934    done
935
936    echo "$loc_list"
937}
938
939# Helper returning the list of supported DH groups from CRYPTO_CONFIG_H,
940# without the "PSA_WANT_" prefix. This becomes handy for accelerating DH groups
941# in the following helpers.
942helper_get_psa_dh_group_list () {
943    loc_list=""
944    for item in $(sed -n 's/^#define PSA_WANT_\(DH_RFC7919_[0-9]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do
945        loc_list="$loc_list $item"
946    done
947
948    echo "$loc_list"
949}
950
951# Get the list of uncommented PSA_WANT_KEY_TYPE_xxx_ from CRYPTO_CONFIG_H. This
952# is useful to easily get a list of key type symbols to accelerate.
953# The function accepts a single argument which is the key type: ECC, DH, RSA.
954helper_get_psa_key_type_list() {
955    key_type="$1"
956    loc_list=""
957    for item in $(sed -n "s/^#define PSA_WANT_\(KEY_TYPE_${key_type}_[0-9A-Z_a-z]*\).*/\1/p" <"$CRYPTO_CONFIG_H"); do
958        # Skip DERIVE for elliptic keys since there is no driver dispatch for
959        # it so it cannot be accelerated.
960        if [ "$item" != "KEY_TYPE_ECC_KEY_PAIR_DERIVE" ]; then
961            loc_list="$loc_list $item"
962        fi
963    done
964
965    echo "$loc_list"
966}
967
968# Build the drivers library libtestdriver1.a (with ASan).
969#
970# Parameters:
971# 1. a space-separated list of things to accelerate;
972# 2. optional: a space-separate list of things to also support.
973# Here "things" are PSA_WANT_ symbols but with PSA_WANT_ removed.
974helper_libtestdriver1_make_drivers() {
975    loc_accel_flags=$( echo "$1 ${2-}" | sed 's/[^ ]* */-DLIBTESTDRIVER1_MBEDTLS_PSA_ACCEL_&/g' )
976    make CC=$ASAN_CC -C tests libtestdriver1.a CFLAGS=" $ASAN_CFLAGS $loc_accel_flags" LDFLAGS="$ASAN_CFLAGS"
977}
978
979# Build the main libraries, programs and tests,
980# linking to the drivers library (with ASan).
981#
982# Parameters:
983# 1. a space-separated list of things to accelerate;
984# *. remaining arguments if any are passed directly to make
985#    (examples: lib, -C tests test_suite_xxx, etc.)
986# Here "things" are PSA_WANT_ symbols but with PSA_WANT_ removed.
987helper_libtestdriver1_make_main() {
988    loc_accel_list=$1
989    shift
990
991    # we need flags both with and without the LIBTESTDRIVER1_ prefix
992    loc_accel_flags=$( echo "$loc_accel_list" | sed 's/[^ ]* */-DLIBTESTDRIVER1_MBEDTLS_PSA_ACCEL_&/g' )
993    loc_accel_flags="$loc_accel_flags $( echo "$loc_accel_list" | sed 's/[^ ]* */-DMBEDTLS_PSA_ACCEL_&/g' )"
994    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" "$@"
995}
996
997################################################################
998#### Basic checks
999################################################################
1000
1001#
1002# Test Suites to be executed
1003#
1004# The test ordering tries to optimize for the following criteria:
1005# 1. Catch possible problems early, by running first tests that run quickly
1006#    and/or are more likely to fail than others (eg I use Clang most of the
1007#    time, so start with a GCC build).
1008# 2. Minimize total running time, by avoiding useless rebuilds
1009#
1010# Indicative running times are given for reference.
1011
1012component_check_recursion () {
1013    msg "Check: recursion.pl" # < 1s
1014    tests/scripts/recursion.pl library/*.c
1015}
1016
1017component_check_generated_files () {
1018    msg "Check: check-generated-files, files generated with make" # 2s
1019    make generated_files
1020    tests/scripts/check-generated-files.sh
1021
1022    msg "Check: check-generated-files -u, files present" # 2s
1023    tests/scripts/check-generated-files.sh -u
1024    # Check that the generated files are considered up to date.
1025    tests/scripts/check-generated-files.sh
1026
1027    msg "Check: check-generated-files -u, files absent" # 2s
1028    command make neat
1029    tests/scripts/check-generated-files.sh -u
1030    # Check that the generated files are considered up to date.
1031    tests/scripts/check-generated-files.sh
1032
1033    # This component ends with the generated files present in the source tree.
1034    # This is necessary for subsequent components!
1035}
1036
1037component_check_doxy_blocks () {
1038    msg "Check: doxygen markup outside doxygen blocks" # < 1s
1039    tests/scripts/check-doxy-blocks.pl
1040}
1041
1042component_check_files () {
1043    msg "Check: file sanity checks (permissions, encodings)" # < 1s
1044    tests/scripts/check_files.py
1045}
1046
1047component_check_changelog () {
1048    msg "Check: changelog entries" # < 1s
1049    rm -f ChangeLog.new
1050    scripts/assemble_changelog.py -o ChangeLog.new
1051    if [ -e ChangeLog.new ]; then
1052        # Show the diff for information. It isn't an error if the diff is
1053        # non-empty.
1054        diff -u ChangeLog ChangeLog.new || true
1055        rm ChangeLog.new
1056    fi
1057}
1058
1059component_check_names () {
1060    msg "Check: declared and exported names (builds the library)" # < 3s
1061    tests/scripts/check_names.py -v
1062}
1063
1064component_check_test_cases () {
1065    msg "Check: test case descriptions" # < 1s
1066    if [ $QUIET -eq 1 ]; then
1067        opt='--quiet'
1068    else
1069        opt=''
1070    fi
1071    tests/scripts/check_test_cases.py -q $opt
1072    unset opt
1073}
1074
1075component_check_test_dependencies () {
1076    msg "Check: test case dependencies: legacy vs PSA" # < 1s
1077    # The purpose of this component is to catch unjustified dependencies on
1078    # legacy feature macros (MBEDTLS_xxx) in PSA tests. Generally speaking,
1079    # PSA test should use PSA feature macros (PSA_WANT_xxx, more rarely
1080    # MBEDTLS_PSA_xxx).
1081    #
1082    # Most of the time, use of legacy MBEDTLS_xxx macros are mistakes, which
1083    # this component is meant to catch. However a few of them are justified,
1084    # mostly by the absence of a PSA equivalent, so this component includes a
1085    # list of expected exceptions.
1086
1087    found="check-test-deps-found-$$"
1088    expected="check-test-deps-expected-$$"
1089
1090    # Find legacy dependencies in PSA tests
1091    grep 'depends_on' \
1092        tests/suites/test_suite_psa*.data tests/suites/test_suite_psa*.function |
1093        grep -Eo '!?MBEDTLS_[^: ]*' |
1094        grep -v -e MBEDTLS_PSA_ -e MBEDTLS_TEST_ |
1095        sort -u > $found
1096
1097    # Expected ones with justification - keep in sorted order by ASCII table!
1098    rm -f $expected
1099    # No PSA equivalent - WANT_KEY_TYPE_AES means all sizes
1100    echo "!MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH" >> $expected
1101    # No PSA equivalent - used to skip decryption tests in PSA-ECB, CBC/XTS/NIST_KW/DES
1102    echo "!MBEDTLS_BLOCK_CIPHER_NO_DECRYPT" >> $expected
1103    # MBEDTLS_ASN1_WRITE_C is used by import_rsa_made_up() in test_suite_psa_crypto
1104    # in order to build a fake RSA key of the wanted size based on
1105    # PSA_VENDOR_RSA_MAX_KEY_BITS. The legacy module is only used by
1106    # the test code and that's probably the most convenient way of achieving
1107    # the test's goal.
1108    echo "MBEDTLS_ASN1_WRITE_C" >> $expected
1109    # No PSA equivalent - we should probably have one in the future.
1110    echo "MBEDTLS_ECP_RESTARTABLE" >> $expected
1111    # No PSA equivalent - needed by some init tests
1112    echo "MBEDTLS_ENTROPY_NV_SEED" >> $expected
1113    # No PSA equivalent - required to run threaded tests.
1114    echo "MBEDTLS_THREADING_PTHREAD" >> $expected
1115
1116    # Compare reality with expectation.
1117    # We want an exact match, to ensure the above list remains up-to-date.
1118    #
1119    # The output should be empty. When it's not:
1120    # - Each '+' line is a macro that was found but not expected. You want to
1121    # find where that macro occurs, and either replace it with PSA macros, or
1122    # add it to the exceptions list above with a justification.
1123    # - Each '-' line is a macro that was expected but not found; it means the
1124    # exceptions list above should be updated by removing that macro.
1125    diff -U0 $expected $found
1126
1127    rm $found $expected
1128}
1129
1130component_check_doxygen_warnings () {
1131    msg "Check: doxygen warnings (builds the documentation)" # ~ 3s
1132    tests/scripts/doxygen.sh
1133}
1134
1135
1136
1137################################################################
1138#### Build and test many configurations and targets
1139################################################################
1140
1141component_test_default_out_of_box () {
1142    msg "build: make, default config (out-of-box)" # ~1min
1143    make
1144    # Disable fancy stuff
1145    unset MBEDTLS_TEST_OUTCOME_FILE
1146
1147    msg "test: main suites make, default config (out-of-box)" # ~10s
1148    make test
1149
1150    msg "selftest: make, default config (out-of-box)" # ~10s
1151    programs/test/selftest
1152
1153    msg "program demos: make, default config (out-of-box)" # ~10s
1154    tests/scripts/run_demos.py
1155}
1156
1157component_test_default_cmake_gcc_asan () {
1158    msg "build: cmake, gcc, ASan" # ~ 1 min 50s
1159    CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
1160    make
1161
1162    msg "test: main suites (inc. selftests) (ASan build)" # ~ 50s
1163    make test
1164
1165    msg "program demos (ASan build)" # ~10s
1166    tests/scripts/run_demos.py
1167
1168    msg "test: selftest (ASan build)" # ~ 10s
1169    programs/test/selftest
1170
1171    msg "test: metatests (GCC, ASan build)"
1172    tests/scripts/run-metatests.sh any asan poison
1173
1174    msg "test: ssl-opt.sh (ASan build)" # ~ 1 min
1175    tests/ssl-opt.sh
1176
1177    msg "test: compat.sh (ASan build)" # ~ 6 min
1178    tests/compat.sh
1179
1180    msg "test: context-info.sh (ASan build)" # ~ 15 sec
1181    tests/context-info.sh
1182}
1183
1184component_test_default_cmake_gcc_asan_new_bignum () {
1185    msg "build: cmake, gcc, ASan" # ~ 1 min 50s
1186    scripts/config.py set MBEDTLS_ECP_WITH_MPI_UINT
1187    CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
1188    make
1189
1190    msg "test: main suites (inc. selftests) (ASan build)" # ~ 50s
1191    make test
1192
1193    msg "test: selftest (ASan build)" # ~ 10s
1194    programs/test/selftest
1195
1196    msg "test: ssl-opt.sh (ASan build)" # ~ 1 min
1197    tests/ssl-opt.sh
1198
1199    msg "test: compat.sh (ASan build)" # ~ 6 min
1200    tests/compat.sh
1201
1202    msg "test: context-info.sh (ASan build)" # ~ 15 sec
1203    tests/context-info.sh
1204}
1205
1206component_test_full_cmake_gcc_asan () {
1207    msg "build: full config, cmake, gcc, ASan"
1208    scripts/config.py full
1209    CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
1210    make
1211
1212    msg "test: main suites (inc. selftests) (full config, ASan build)"
1213    make test
1214
1215    msg "test: selftest (ASan build)" # ~ 10s
1216    programs/test/selftest
1217
1218    msg "test: ssl-opt.sh (full config, ASan build)"
1219    tests/ssl-opt.sh
1220
1221    msg "test: compat.sh (full config, ASan build)"
1222    tests/compat.sh
1223
1224    msg "test: context-info.sh (full config, ASan build)" # ~ 15 sec
1225    tests/context-info.sh
1226}
1227
1228
1229component_test_full_cmake_gcc_asan_new_bignum () {
1230    msg "build: full config, cmake, gcc, ASan"
1231    scripts/config.py full
1232    scripts/config.py set MBEDTLS_ECP_WITH_MPI_UINT
1233    CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
1234    make
1235
1236    msg "test: main suites (inc. selftests) (full config, ASan build)"
1237    make test
1238
1239    msg "test: selftest (ASan build)" # ~ 10s
1240    programs/test/selftest
1241
1242    msg "test: ssl-opt.sh (full config, ASan build)"
1243    tests/ssl-opt.sh
1244
1245    msg "test: compat.sh (full config, ASan build)"
1246    tests/compat.sh
1247
1248    msg "test: context-info.sh (full config, ASan build)" # ~ 15 sec
1249    tests/context-info.sh
1250}
1251
1252component_test_psa_crypto_key_id_encodes_owner () {
1253    msg "build: full config + PSA_CRYPTO_KEY_ID_ENCODES_OWNER, cmake, gcc, ASan"
1254    scripts/config.py full
1255    scripts/config.py set MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
1256    CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
1257    make
1258
1259    msg "test: full config - USE_PSA_CRYPTO + PSA_CRYPTO_KEY_ID_ENCODES_OWNER, cmake, gcc, ASan"
1260    make test
1261}
1262
1263component_test_psa_assume_exclusive_buffers () {
1264    msg "build: full config + MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS, cmake, gcc, ASan"
1265    scripts/config.py full
1266    scripts/config.py set MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS
1267    CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
1268    make
1269
1270    msg "test: full config + MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS, cmake, gcc, ASan"
1271    make test
1272}
1273
1274# check_renamed_symbols HEADER LIB
1275# Check that if HEADER contains '#define MACRO ...' then MACRO is not a symbol
1276# name is LIB.
1277check_renamed_symbols () {
1278    ! nm "$2" | sed 's/.* //' |
1279      grep -x -F "$(sed -n 's/^ *# *define  *\([A-Z_a-z][0-9A-Z_a-z]*\)..*/\1/p' "$1")"
1280}
1281
1282component_build_psa_crypto_spm () {
1283    msg "build: full config + PSA_CRYPTO_KEY_ID_ENCODES_OWNER + PSA_CRYPTO_SPM, make, gcc"
1284    scripts/config.py full
1285    scripts/config.py unset MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS
1286    scripts/config.py set MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
1287    scripts/config.py set MBEDTLS_PSA_CRYPTO_SPM
1288    # We can only compile, not link, since our test and sample programs
1289    # aren't equipped for the modified names used when MBEDTLS_PSA_CRYPTO_SPM
1290    # is active.
1291    make CC=gcc CFLAGS='-Werror -Wall -Wextra -I../tests/include/spe' lib
1292
1293    # Check that if a symbol is renamed by crypto_spe.h, the non-renamed
1294    # version is not present.
1295    echo "Checking for renamed symbols in the library"
1296    check_renamed_symbols tests/include/spe/crypto_spe.h library/libmbedcrypto.a
1297}
1298
1299# Get a list of library-wise undefined symbols and ensure that they only
1300# belong to psa_xxx() functions and not to mbedtls_yyy() ones.
1301# This function is a common helper used by both:
1302# - component_test_default_psa_crypto_client_without_crypto_provider
1303# - component_build_full_psa_crypto_client_without_crypto_provider.
1304common_check_mbedtls_missing_symbols() {
1305    nm library/libmbedcrypto.a | grep ' [TRrDC] ' | grep -Eo '(mbedtls_|psa_).*' | sort -u > sym_def.txt
1306    nm library/libmbedcrypto.a | grep ' U ' | grep -Eo '(mbedtls_|psa_).*' | sort -u > sym_undef.txt
1307    comm sym_def.txt sym_undef.txt -13 > linking_errors.txt
1308    not grep mbedtls_ linking_errors.txt
1309
1310    rm sym_def.txt sym_undef.txt linking_errors.txt
1311}
1312
1313component_test_default_psa_crypto_client_without_crypto_provider () {
1314    msg "build: default config - PSA_CRYPTO_C + PSA_CRYPTO_CLIENT"
1315
1316    scripts/config.py unset MBEDTLS_PSA_CRYPTO_C
1317    scripts/config.py unset MBEDTLS_PSA_CRYPTO_STORAGE_C
1318    scripts/config.py unset MBEDTLS_PSA_ITS_FILE_C
1319    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
1320    scripts/config.py set MBEDTLS_PSA_CRYPTO_CLIENT
1321    scripts/config.py unset MBEDTLS_LMS_C
1322
1323    make
1324
1325    msg "check missing symbols: default config - PSA_CRYPTO_C + PSA_CRYPTO_CLIENT"
1326    common_check_mbedtls_missing_symbols
1327
1328    msg "test: default config - PSA_CRYPTO_C + PSA_CRYPTO_CLIENT"
1329    make test
1330}
1331
1332component_build_full_psa_crypto_client_without_crypto_provider () {
1333    msg "build: full config - PSA_CRYPTO_C"
1334
1335    # Use full config which includes USE_PSA and CRYPTO_CLIENT.
1336    scripts/config.py full
1337
1338    scripts/config.py unset MBEDTLS_PSA_CRYPTO_C
1339    scripts/config.py unset MBEDTLS_PSA_CRYPTO_STORAGE_C
1340    # Dynamic secure element support is a deprecated feature and it is not
1341    # available when CRYPTO_C and PSA_CRYPTO_STORAGE_C are disabled.
1342    scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
1343
1344    # Since there is no crypto provider in this build it is not possible to
1345    # build all the test executables and progrems due to missing PSA functions
1346    # at link time. Therefore we will just build libraries and we'll check
1347    # that symbols of interest are there.
1348    make lib
1349
1350    msg "check missing symbols: full config - PSA_CRYPTO_C"
1351
1352    common_check_mbedtls_missing_symbols
1353
1354    # Ensure that desired functions are included into the build (extend the
1355    # following list as required).
1356    grep mbedtls_pk_get_psa_attributes library/libmbedcrypto.a
1357    grep mbedtls_pk_import_into_psa library/libmbedcrypto.a
1358    grep mbedtls_pk_copy_from_psa library/libmbedcrypto.a
1359}
1360
1361component_test_psa_crypto_rsa_no_genprime() {
1362    msg "build: default config minus MBEDTLS_GENPRIME"
1363    scripts/config.py unset MBEDTLS_GENPRIME
1364    make
1365
1366    msg "test: default config minus MBEDTLS_GENPRIME"
1367    make test
1368}
1369
1370component_test_ref_configs () {
1371    msg "test/build: ref-configs (ASan build)" # ~ 6 min 20s
1372    # test-ref-configs works by overwriting mbedtls_config.h; this makes cmake
1373    # want to re-generate generated files that depend on it, quite correctly.
1374    # However this doesn't work as the generation script expects a specific
1375    # format for mbedtls_config.h, which the other files don't follow. Also,
1376    # cmake can't know this, but re-generation is actually not necessary as
1377    # the generated files only depend on the list of available options, not
1378    # whether they're on or off. So, disable cmake's (over-sensitive here)
1379    # dependency resolution for generated files and just rely on them being
1380    # present (thanks to pre_generate_files) by turning GEN_FILES off.
1381    CC=$ASAN_CC cmake -D GEN_FILES=Off -D CMAKE_BUILD_TYPE:String=Asan .
1382    tests/scripts/test-ref-configs.pl
1383}
1384
1385component_test_no_renegotiation () {
1386    msg "build: Default + !MBEDTLS_SSL_RENEGOTIATION (ASan build)" # ~ 6 min
1387    scripts/config.py unset MBEDTLS_SSL_RENEGOTIATION
1388    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
1389    make
1390
1391    msg "test: !MBEDTLS_SSL_RENEGOTIATION - main suites (inc. selftests) (ASan build)" # ~ 50s
1392    make test
1393
1394    msg "test: !MBEDTLS_SSL_RENEGOTIATION - ssl-opt.sh (ASan build)" # ~ 6 min
1395    tests/ssl-opt.sh
1396}
1397
1398component_test_no_pem_no_fs () {
1399    msg "build: Default + !MBEDTLS_PEM_PARSE_C + !MBEDTLS_FS_IO (ASan build)"
1400    scripts/config.py unset MBEDTLS_PEM_PARSE_C
1401    scripts/config.py unset MBEDTLS_FS_IO
1402    scripts/config.py unset MBEDTLS_PSA_ITS_FILE_C # requires a filesystem
1403    scripts/config.py unset MBEDTLS_PSA_CRYPTO_STORAGE_C # requires PSA ITS
1404    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
1405    make
1406
1407    msg "test: !MBEDTLS_PEM_PARSE_C !MBEDTLS_FS_IO - main suites (inc. selftests) (ASan build)" # ~ 50s
1408    make test
1409
1410    msg "test: !MBEDTLS_PEM_PARSE_C !MBEDTLS_FS_IO - ssl-opt.sh (ASan build)" # ~ 6 min
1411    tests/ssl-opt.sh
1412}
1413
1414component_test_rsa_no_crt () {
1415    msg "build: Default + RSA_NO_CRT (ASan build)" # ~ 6 min
1416    scripts/config.py set MBEDTLS_RSA_NO_CRT
1417    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
1418    make
1419
1420    msg "test: RSA_NO_CRT - main suites (inc. selftests) (ASan build)" # ~ 50s
1421    make test
1422
1423    msg "test: RSA_NO_CRT - RSA-related part of ssl-opt.sh (ASan build)" # ~ 5s
1424    tests/ssl-opt.sh -f RSA
1425
1426    msg "test: RSA_NO_CRT - RSA-related part of compat.sh (ASan build)" # ~ 3 min
1427    tests/compat.sh -t RSA
1428
1429    msg "test: RSA_NO_CRT - RSA-related part of context-info.sh (ASan build)" # ~ 15 sec
1430    tests/context-info.sh
1431}
1432
1433component_test_no_ctr_drbg_classic () {
1434    msg "build: Full minus CTR_DRBG, classic crypto in TLS"
1435    scripts/config.py full
1436    scripts/config.py unset MBEDTLS_CTR_DRBG_C
1437    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
1438    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
1439
1440    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
1441    make
1442
1443    msg "test: Full minus CTR_DRBG, classic crypto - main suites"
1444    make test
1445
1446    # In this configuration, the TLS test programs use HMAC_DRBG.
1447    # The SSL tests are slow, so run a small subset, just enough to get
1448    # confidence that the SSL code copes with HMAC_DRBG.
1449    msg "test: Full minus CTR_DRBG, classic crypto - ssl-opt.sh (subset)"
1450    tests/ssl-opt.sh -f 'Default\|SSL async private.*delay=\|tickets enabled on server'
1451
1452    msg "test: Full minus CTR_DRBG, classic crypto - compat.sh (subset)"
1453    tests/compat.sh -m tls12 -t 'ECDSA PSK' -V NO -p OpenSSL
1454}
1455
1456component_test_no_ctr_drbg_use_psa () {
1457    msg "build: Full minus CTR_DRBG, PSA crypto in TLS"
1458    scripts/config.py full
1459    scripts/config.py unset MBEDTLS_CTR_DRBG_C
1460    scripts/config.py set MBEDTLS_USE_PSA_CRYPTO
1461
1462    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
1463    make
1464
1465    msg "test: Full minus CTR_DRBG, USE_PSA_CRYPTO - main suites"
1466    make test
1467
1468    # In this configuration, the TLS test programs use HMAC_DRBG.
1469    # The SSL tests are slow, so run a small subset, just enough to get
1470    # confidence that the SSL code copes with HMAC_DRBG.
1471    msg "test: Full minus CTR_DRBG, USE_PSA_CRYPTO - ssl-opt.sh (subset)"
1472    tests/ssl-opt.sh -f 'Default\|SSL async private.*delay=\|tickets enabled on server'
1473
1474    msg "test: Full minus CTR_DRBG, USE_PSA_CRYPTO - compat.sh (subset)"
1475    tests/compat.sh -m tls12 -t 'ECDSA PSK' -V NO -p OpenSSL
1476}
1477
1478component_test_no_hmac_drbg_classic () {
1479    msg "build: Full minus HMAC_DRBG, classic crypto in TLS"
1480    scripts/config.py full
1481    scripts/config.py unset MBEDTLS_HMAC_DRBG_C
1482    scripts/config.py unset MBEDTLS_ECDSA_DETERMINISTIC # requires HMAC_DRBG
1483    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
1484    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
1485
1486    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
1487    make
1488
1489    msg "test: Full minus HMAC_DRBG, classic crypto - main suites"
1490    make test
1491
1492    # Normally our ECDSA implementation uses deterministic ECDSA. But since
1493    # HMAC_DRBG is disabled in this configuration, randomized ECDSA is used
1494    # instead.
1495    # Test SSL with non-deterministic ECDSA. Only test features that
1496    # might be affected by how ECDSA signature is performed.
1497    msg "test: Full minus HMAC_DRBG, classic crypto - ssl-opt.sh (subset)"
1498    tests/ssl-opt.sh -f 'Default\|SSL async private: sign'
1499
1500    # To save time, only test one protocol version, since this part of
1501    # the protocol is identical in (D)TLS up to 1.2.
1502    msg "test: Full minus HMAC_DRBG, classic crypto - compat.sh (ECDSA)"
1503    tests/compat.sh -m tls12 -t 'ECDSA'
1504}
1505
1506component_test_no_hmac_drbg_use_psa () {
1507    msg "build: Full minus HMAC_DRBG, PSA crypto in TLS"
1508    scripts/config.py full
1509    scripts/config.py unset MBEDTLS_HMAC_DRBG_C
1510    scripts/config.py unset MBEDTLS_ECDSA_DETERMINISTIC # requires HMAC_DRBG
1511    scripts/config.py set MBEDTLS_USE_PSA_CRYPTO
1512
1513    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
1514    make
1515
1516    msg "test: Full minus HMAC_DRBG, USE_PSA_CRYPTO - main suites"
1517    make test
1518
1519    # Normally our ECDSA implementation uses deterministic ECDSA. But since
1520    # HMAC_DRBG is disabled in this configuration, randomized ECDSA is used
1521    # instead.
1522    # Test SSL with non-deterministic ECDSA. Only test features that
1523    # might be affected by how ECDSA signature is performed.
1524    msg "test: Full minus HMAC_DRBG, USE_PSA_CRYPTO - ssl-opt.sh (subset)"
1525    tests/ssl-opt.sh -f 'Default\|SSL async private: sign'
1526
1527    # To save time, only test one protocol version, since this part of
1528    # the protocol is identical in (D)TLS up to 1.2.
1529    msg "test: Full minus HMAC_DRBG, USE_PSA_CRYPTO - compat.sh (ECDSA)"
1530    tests/compat.sh -m tls12 -t 'ECDSA'
1531}
1532
1533component_test_psa_external_rng_no_drbg_classic () {
1534    msg "build: PSA_CRYPTO_EXTERNAL_RNG minus *_DRBG, classic crypto in TLS"
1535    scripts/config.py full
1536    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
1537    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
1538    scripts/config.py set MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
1539    scripts/config.py unset MBEDTLS_ENTROPY_C
1540    scripts/config.py unset MBEDTLS_ENTROPY_NV_SEED
1541    scripts/config.py unset MBEDTLS_PLATFORM_NV_SEED_ALT
1542    scripts/config.py unset MBEDTLS_CTR_DRBG_C
1543    scripts/config.py unset MBEDTLS_HMAC_DRBG_C
1544    scripts/config.py unset MBEDTLS_ECDSA_DETERMINISTIC # requires HMAC_DRBG
1545    # When MBEDTLS_USE_PSA_CRYPTO is disabled and there is no DRBG,
1546    # the SSL test programs don't have an RNG and can't work. Explicitly
1547    # make them use the PSA RNG with -DMBEDTLS_TEST_USE_PSA_CRYPTO_RNG.
1548    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DMBEDTLS_TEST_USE_PSA_CRYPTO_RNG" LDFLAGS="$ASAN_CFLAGS"
1549
1550    msg "test: PSA_CRYPTO_EXTERNAL_RNG minus *_DRBG, classic crypto - main suites"
1551    make test
1552
1553    msg "test: PSA_CRYPTO_EXTERNAL_RNG minus *_DRBG, classic crypto - ssl-opt.sh (subset)"
1554    tests/ssl-opt.sh -f 'Default'
1555}
1556
1557component_test_psa_external_rng_no_drbg_use_psa () {
1558    msg "build: PSA_CRYPTO_EXTERNAL_RNG minus *_DRBG, PSA crypto in TLS"
1559    scripts/config.py full
1560    scripts/config.py set MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
1561    scripts/config.py unset MBEDTLS_ENTROPY_C
1562    scripts/config.py unset MBEDTLS_ENTROPY_NV_SEED
1563    scripts/config.py unset MBEDTLS_PLATFORM_NV_SEED_ALT
1564    scripts/config.py unset MBEDTLS_CTR_DRBG_C
1565    scripts/config.py unset MBEDTLS_HMAC_DRBG_C
1566    scripts/config.py unset MBEDTLS_ECDSA_DETERMINISTIC # requires HMAC_DRBG
1567    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
1568
1569    msg "test: PSA_CRYPTO_EXTERNAL_RNG minus *_DRBG, PSA crypto - main suites"
1570    make test
1571
1572    msg "test: PSA_CRYPTO_EXTERNAL_RNG minus *_DRBG, PSA crypto - ssl-opt.sh (subset)"
1573    tests/ssl-opt.sh -f 'Default\|opaque'
1574}
1575
1576component_test_psa_external_rng_use_psa_crypto () {
1577    msg "build: full + PSA_CRYPTO_EXTERNAL_RNG + USE_PSA_CRYPTO minus CTR_DRBG"
1578    scripts/config.py full
1579    scripts/config.py set MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
1580    scripts/config.py set MBEDTLS_USE_PSA_CRYPTO
1581    scripts/config.py unset MBEDTLS_CTR_DRBG_C
1582    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
1583
1584    msg "test: full + PSA_CRYPTO_EXTERNAL_RNG + USE_PSA_CRYPTO minus CTR_DRBG"
1585    make test
1586
1587    msg "test: full + PSA_CRYPTO_EXTERNAL_RNG + USE_PSA_CRYPTO minus CTR_DRBG"
1588    tests/ssl-opt.sh -f 'Default\|opaque'
1589}
1590
1591component_test_psa_inject_entropy () {
1592    msg "build: full + MBEDTLS_PSA_INJECT_ENTROPY"
1593    scripts/config.py full
1594    scripts/config.py set MBEDTLS_PSA_INJECT_ENTROPY
1595    scripts/config.py set MBEDTLS_ENTROPY_NV_SEED
1596    scripts/config.py set MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
1597    scripts/config.py unset MBEDTLS_PLATFORM_NV_SEED_ALT
1598    scripts/config.py unset MBEDTLS_PLATFORM_STD_NV_SEED_READ
1599    scripts/config.py unset MBEDTLS_PLATFORM_STD_NV_SEED_WRITE
1600    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS '-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/user-config-for-test.h\"'" LDFLAGS="$ASAN_CFLAGS"
1601
1602    msg "test: full + MBEDTLS_PSA_INJECT_ENTROPY"
1603    make test
1604}
1605
1606component_test_sw_inet_pton () {
1607    msg "build: default plus MBEDTLS_TEST_SW_INET_PTON"
1608
1609    # MBEDTLS_TEST_HOOKS required for x509_crt_parse_cn_inet_pton
1610    scripts/config.py set MBEDTLS_TEST_HOOKS
1611    make CFLAGS="-DMBEDTLS_TEST_SW_INET_PTON"
1612
1613    msg "test: default plus MBEDTLS_TEST_SW_INET_PTON"
1614    make test
1615}
1616
1617component_full_no_pkparse_pkwrite() {
1618    msg "build: full without pkparse and pkwrite"
1619
1620    scripts/config.py crypto_full
1621    scripts/config.py unset MBEDTLS_PK_PARSE_C
1622    scripts/config.py unset MBEDTLS_PK_WRITE_C
1623
1624    make CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
1625
1626    # Ensure that PK_[PARSE|WRITE]_C were not re-enabled accidentally (additive config).
1627    not grep mbedtls_pk_parse_key library/pkparse.o
1628    not grep mbedtls_pk_write_key_der library/pkwrite.o
1629
1630    msg "test: full without pkparse and pkwrite"
1631    make test
1632}
1633
1634component_test_crypto_full_md_light_only () {
1635    msg "build: crypto_full with only the light subset of MD"
1636    scripts/config.py crypto_full
1637    scripts/config.py unset MBEDTLS_PSA_CRYPTO_CONFIG
1638    # Disable MD
1639    scripts/config.py unset MBEDTLS_MD_C
1640    # Disable direct dependencies of MD_C
1641    scripts/config.py unset MBEDTLS_HKDF_C
1642    scripts/config.py unset MBEDTLS_HMAC_DRBG_C
1643    scripts/config.py unset MBEDTLS_PKCS7_C
1644    # Disable indirect dependencies of MD_C
1645    scripts/config.py unset MBEDTLS_ECDSA_DETERMINISTIC # needs HMAC_DRBG
1646    # Disable things that would auto-enable MD_C
1647    scripts/config.py unset MBEDTLS_PKCS5_C
1648
1649    # Note: MD-light is auto-enabled in build_info.h by modules that need it,
1650    # which we haven't disabled, so no need to explicitly enable it.
1651    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
1652
1653    # Make sure we don't have the HMAC functions, but the hashing functions
1654    not grep mbedtls_md_hmac library/md.o
1655    grep mbedtls_md library/md.o
1656
1657    msg "test: crypto_full with only the light subset of MD"
1658    make test
1659}
1660
1661component_test_full_no_cipher_no_psa_crypto () {
1662    msg "build: full no CIPHER no PSA_CRYPTO_C"
1663    scripts/config.py full
1664    scripts/config.py unset MBEDTLS_CIPHER_C
1665    # Don't pull in cipher via PSA mechanisms
1666    # (currently ignored anyway because we completely disable PSA)
1667    scripts/config.py unset MBEDTLS_PSA_CRYPTO_CONFIG
1668    # Disable features that depend on CIPHER_C
1669    scripts/config.py unset MBEDTLS_CMAC_C
1670    scripts/config.py unset MBEDTLS_NIST_KW_C
1671    scripts/config.py unset MBEDTLS_PSA_CRYPTO_C
1672    scripts/config.py unset MBEDTLS_PSA_CRYPTO_CLIENT
1673    scripts/config.py unset MBEDTLS_SSL_TLS_C
1674    scripts/config.py unset MBEDTLS_SSL_TICKET_C
1675    # Disable features that depend on PSA_CRYPTO_C
1676    scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
1677    scripts/config.py unset MBEDTLS_PSA_CRYPTO_STORAGE_C
1678    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
1679    scripts/config.py unset MBEDTLS_LMS_C
1680    scripts/config.py unset MBEDTLS_LMS_PRIVATE
1681
1682    msg "test: full no CIPHER no PSA_CRYPTO_C"
1683    make test
1684}
1685
1686# This is a common configurator and test function that is used in:
1687# - component_test_full_no_cipher_with_psa_crypto
1688# - component_test_full_no_cipher_with_psa_crypto_config
1689# It accepts 2 input parameters:
1690# - $1: boolean value which basically reflects status of MBEDTLS_PSA_CRYPTO_CONFIG
1691# - $2: a text string which describes the test component
1692common_test_full_no_cipher_with_psa_crypto () {
1693    USE_CRYPTO_CONFIG="$1"
1694    COMPONENT_DESCRIPTION="$2"
1695
1696    msg "build: $COMPONENT_DESCRIPTION"
1697
1698    scripts/config.py full
1699    scripts/config.py unset MBEDTLS_CIPHER_C
1700
1701    if [ "$USE_CRYPTO_CONFIG" -eq 1 ]; then
1702        # The built-in implementation of the following algs/key-types depends
1703        # on CIPHER_C so we disable them.
1704        # This does not hold for KEY_TYPE_CHACHA20 and ALG_CHACHA20_POLY1305
1705        # so we keep them enabled.
1706        scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_CCM_STAR_NO_TAG
1707        scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_CMAC
1708        scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_CBC_NO_PADDING
1709        scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_CBC_PKCS7
1710        scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_CFB
1711        scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_CTR
1712        scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_ECB_NO_PADDING
1713        scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_OFB
1714        scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_STREAM_CIPHER
1715        scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_KEY_TYPE_DES
1716    else
1717        # Don't pull in cipher via PSA mechanisms
1718        scripts/config.py unset MBEDTLS_PSA_CRYPTO_CONFIG
1719        # Disable cipher modes/keys that make PSA depend on CIPHER_C.
1720        # Keep CHACHA20 and CHACHAPOLY enabled since they do not depend on CIPHER_C.
1721        scripts/config.py unset-all MBEDTLS_CIPHER_MODE
1722    fi
1723    # The following modules directly depends on CIPHER_C
1724    scripts/config.py unset MBEDTLS_CMAC_C
1725    scripts/config.py unset MBEDTLS_NIST_KW_C
1726
1727    make
1728
1729    # Ensure that CIPHER_C was not re-enabled
1730    not grep mbedtls_cipher_init library/cipher.o
1731
1732    msg "test: $COMPONENT_DESCRIPTION"
1733    make test
1734}
1735
1736component_test_full_no_cipher_with_psa_crypto() {
1737    common_test_full_no_cipher_with_psa_crypto 0 "full no CIPHER no CRYPTO_CONFIG"
1738}
1739
1740component_test_full_no_cipher_with_psa_crypto_config() {
1741    common_test_full_no_cipher_with_psa_crypto 1 "full no CIPHER"
1742}
1743
1744component_test_full_no_ccm() {
1745    msg "build: full no PSA_WANT_ALG_CCM"
1746
1747    # Full config enables:
1748    # - USE_PSA_CRYPTO so that TLS code dispatches cipher/AEAD to PSA
1749    # - CRYPTO_CONFIG so that PSA_WANT config symbols are evaluated
1750    scripts/config.py full
1751
1752    # Disable PSA_WANT_ALG_CCM so that CCM is not supported in PSA. CCM_C is still
1753    # enabled, but not used from TLS since USE_PSA is set.
1754    # This is helpful to ensure that TLS tests below have proper dependencies.
1755    #
1756    # Note: also PSA_WANT_ALG_CCM_STAR_NO_TAG is enabled, but it does not cause
1757    # PSA_WANT_ALG_CCM to be re-enabled.
1758    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CCM
1759
1760    make
1761
1762    msg "test: full no PSA_WANT_ALG_CCM"
1763    make test
1764}
1765
1766component_test_full_no_ccm_star_no_tag() {
1767    msg "build: full no PSA_WANT_ALG_CCM_STAR_NO_TAG"
1768
1769    # Full config enables CRYPTO_CONFIG so that PSA_WANT config symbols are evaluated
1770    scripts/config.py full
1771
1772    # Disable CCM_STAR_NO_TAG, which is the target of this test, as well as all
1773    # other components that enable MBEDTLS_PSA_BUILTIN_CIPHER internal symbol.
1774    # This basically disables all unauthenticated ciphers on the PSA side, while
1775    # keeping AEADs enabled.
1776    #
1777    # Note: PSA_WANT_ALG_CCM is enabled, but it does not cause
1778    # PSA_WANT_ALG_CCM_STAR_NO_TAG to be re-enabled.
1779    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CCM_STAR_NO_TAG
1780    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_STREAM_CIPHER
1781    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CTR
1782    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CFB
1783    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_OFB
1784    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_ECB_NO_PADDING
1785    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CBC_NO_PADDING
1786    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CBC_PKCS7
1787
1788    make
1789
1790    # Ensure MBEDTLS_PSA_BUILTIN_CIPHER was not enabled
1791    not grep mbedtls_psa_cipher library/psa_crypto_cipher.o
1792
1793    msg "test: full no PSA_WANT_ALG_CCM_STAR_NO_TAG"
1794    make test
1795}
1796
1797component_test_full_no_bignum () {
1798    msg "build: full minus bignum"
1799    scripts/config.py full
1800    scripts/config.py unset MBEDTLS_BIGNUM_C
1801    # Direct dependencies of bignum
1802    scripts/config.py unset MBEDTLS_ECP_C
1803    scripts/config.py unset MBEDTLS_RSA_C
1804    scripts/config.py unset MBEDTLS_DHM_C
1805    # Direct dependencies of ECP
1806    scripts/config.py unset MBEDTLS_ECDH_C
1807    scripts/config.py unset MBEDTLS_ECDSA_C
1808    scripts/config.py unset MBEDTLS_ECJPAKE_C
1809    scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
1810    # Disable what auto-enables ECP_LIGHT
1811    scripts/config.py unset MBEDTLS_PK_PARSE_EC_EXTENDED
1812    scripts/config.py unset MBEDTLS_PK_PARSE_EC_COMPRESSED
1813    # Indirect dependencies of ECP
1814    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
1815    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
1816    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED
1817    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
1818    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
1819    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
1820    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED
1821    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED
1822    # Direct dependencies of DHM
1823    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
1824    # Direct dependencies of RSA
1825    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
1826    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
1827    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
1828    scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT
1829    # PK and its dependencies
1830    scripts/config.py unset MBEDTLS_PK_C
1831    scripts/config.py unset MBEDTLS_PK_PARSE_C
1832    scripts/config.py unset MBEDTLS_PK_WRITE_C
1833    scripts/config.py unset MBEDTLS_X509_USE_C
1834    scripts/config.py unset MBEDTLS_X509_CRT_PARSE_C
1835    scripts/config.py unset MBEDTLS_X509_CRL_PARSE_C
1836    scripts/config.py unset MBEDTLS_X509_CSR_PARSE_C
1837    scripts/config.py unset MBEDTLS_X509_CREATE_C
1838    scripts/config.py unset MBEDTLS_X509_CRT_WRITE_C
1839    scripts/config.py unset MBEDTLS_X509_CSR_WRITE_C
1840    scripts/config.py unset MBEDTLS_PKCS7_C
1841    scripts/config.py unset MBEDTLS_SSL_SERVER_NAME_INDICATION
1842    scripts/config.py unset MBEDTLS_SSL_ASYNC_PRIVATE
1843    scripts/config.py unset MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK
1844
1845    make
1846
1847    msg "test: full minus bignum"
1848    make test
1849}
1850
1851component_test_tls1_2_default_stream_cipher_only () {
1852    msg "build: default with only stream cipher"
1853
1854    # Disable AEAD (controlled by the presence of one of GCM_C, CCM_C, CHACHAPOLY_C
1855    scripts/config.py unset MBEDTLS_GCM_C
1856    scripts/config.py unset MBEDTLS_CCM_C
1857    scripts/config.py unset MBEDTLS_CHACHAPOLY_C
1858    #Disable TLS 1.3 (as no AEAD)
1859    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
1860    # Disable CBC-legacy (controlled by MBEDTLS_CIPHER_MODE_CBC plus at least one block cipher (AES, ARIA, Camellia, DES))
1861    scripts/config.py unset MBEDTLS_CIPHER_MODE_CBC
1862    # Disable CBC-EtM (controlled by the same as CBC-legacy plus MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1863    scripts/config.py unset MBEDTLS_SSL_ENCRYPT_THEN_MAC
1864    # Enable stream (currently that's just the NULL pseudo-cipher (controlled by MBEDTLS_CIPHER_NULL_CIPHER))
1865    scripts/config.py set MBEDTLS_CIPHER_NULL_CIPHER
1866    # Modules that depend on AEAD
1867    scripts/config.py unset MBEDTLS_SSL_CONTEXT_SERIALIZATION
1868    scripts/config.py unset MBEDTLS_SSL_TICKET_C
1869
1870    make
1871
1872    msg "test: default with only stream cipher"
1873    make test
1874
1875    # Not running ssl-opt.sh because most tests require a non-NULL ciphersuite.
1876}
1877
1878component_test_tls1_2_default_stream_cipher_only_use_psa () {
1879    msg "build: default with only stream cipher use psa"
1880
1881    scripts/config.py set MBEDTLS_USE_PSA_CRYPTO
1882    # Disable AEAD (controlled by the presence of one of GCM_C, CCM_C, CHACHAPOLY_C)
1883    scripts/config.py unset MBEDTLS_GCM_C
1884    scripts/config.py unset MBEDTLS_CCM_C
1885    scripts/config.py unset MBEDTLS_CHACHAPOLY_C
1886    #Disable TLS 1.3 (as no AEAD)
1887    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
1888    # Disable CBC-legacy (controlled by MBEDTLS_CIPHER_MODE_CBC plus at least one block cipher (AES, ARIA, Camellia, DES))
1889    scripts/config.py unset MBEDTLS_CIPHER_MODE_CBC
1890    # Disable CBC-EtM (controlled by the same as CBC-legacy plus MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1891    scripts/config.py unset MBEDTLS_SSL_ENCRYPT_THEN_MAC
1892    # Enable stream (currently that's just the NULL pseudo-cipher (controlled by MBEDTLS_CIPHER_NULL_CIPHER))
1893    scripts/config.py set MBEDTLS_CIPHER_NULL_CIPHER
1894    # Modules that depend on AEAD
1895    scripts/config.py unset MBEDTLS_SSL_CONTEXT_SERIALIZATION
1896    scripts/config.py unset MBEDTLS_SSL_TICKET_C
1897
1898    make
1899
1900    msg "test: default with only stream cipher use psa"
1901    make test
1902
1903    # Not running ssl-opt.sh because most tests require a non-NULL ciphersuite.
1904}
1905
1906component_test_tls1_2_default_cbc_legacy_cipher_only () {
1907    msg "build: default with only CBC-legacy cipher"
1908
1909    # Disable AEAD (controlled by the presence of one of GCM_C, CCM_C, CHACHAPOLY_C)
1910    scripts/config.py unset MBEDTLS_GCM_C
1911    scripts/config.py unset MBEDTLS_CCM_C
1912    scripts/config.py unset MBEDTLS_CHACHAPOLY_C
1913    #Disable TLS 1.3 (as no AEAD)
1914    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
1915    # Enable CBC-legacy (controlled by MBEDTLS_CIPHER_MODE_CBC plus at least one block cipher (AES, ARIA, Camellia, DES))
1916    scripts/config.py set MBEDTLS_CIPHER_MODE_CBC
1917    # Disable CBC-EtM (controlled by the same as CBC-legacy plus MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1918    scripts/config.py unset MBEDTLS_SSL_ENCRYPT_THEN_MAC
1919    # Disable stream (currently that's just the NULL pseudo-cipher (controlled by MBEDTLS_CIPHER_NULL_CIPHER))
1920    scripts/config.py unset MBEDTLS_CIPHER_NULL_CIPHER
1921    # Modules that depend on AEAD
1922    scripts/config.py unset MBEDTLS_SSL_CONTEXT_SERIALIZATION
1923    scripts/config.py unset MBEDTLS_SSL_TICKET_C
1924
1925    make
1926
1927    msg "test: default with only CBC-legacy cipher"
1928    make test
1929
1930    msg "test: default with only CBC-legacy cipher - ssl-opt.sh (subset)"
1931    tests/ssl-opt.sh -f "TLS 1.2"
1932}
1933
1934component_test_tls1_2_deafult_cbc_legacy_cipher_only_use_psa () {
1935    msg "build: default with only CBC-legacy cipher use psa"
1936
1937    scripts/config.py set MBEDTLS_USE_PSA_CRYPTO
1938    # Disable AEAD (controlled by the presence of one of GCM_C, CCM_C, CHACHAPOLY_C)
1939    scripts/config.py unset MBEDTLS_GCM_C
1940    scripts/config.py unset MBEDTLS_CCM_C
1941    scripts/config.py unset MBEDTLS_CHACHAPOLY_C
1942    #Disable TLS 1.3 (as no AEAD)
1943    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
1944    # Enable CBC-legacy (controlled by MBEDTLS_CIPHER_MODE_CBC plus at least one block cipher (AES, ARIA, Camellia, DES))
1945    scripts/config.py set MBEDTLS_CIPHER_MODE_CBC
1946    # Disable CBC-EtM (controlled by the same as CBC-legacy plus MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1947    scripts/config.py unset MBEDTLS_SSL_ENCRYPT_THEN_MAC
1948    # Disable stream (currently that's just the NULL pseudo-cipher (controlled by MBEDTLS_CIPHER_NULL_CIPHER))
1949    scripts/config.py unset MBEDTLS_CIPHER_NULL_CIPHER
1950    # Modules that depend on AEAD
1951    scripts/config.py unset MBEDTLS_SSL_CONTEXT_SERIALIZATION
1952    scripts/config.py unset MBEDTLS_SSL_TICKET_C
1953
1954    make
1955
1956    msg "test: default with only CBC-legacy cipher use psa"
1957    make test
1958
1959    msg "test: default with only CBC-legacy cipher use psa - ssl-opt.sh (subset)"
1960    tests/ssl-opt.sh -f "TLS 1.2"
1961}
1962
1963component_test_tls1_2_default_cbc_legacy_cbc_etm_cipher_only () {
1964    msg "build: default with only CBC-legacy and CBC-EtM ciphers"
1965
1966    # Disable AEAD (controlled by the presence of one of GCM_C, CCM_C, CHACHAPOLY_C)
1967    scripts/config.py unset MBEDTLS_GCM_C
1968    scripts/config.py unset MBEDTLS_CCM_C
1969    scripts/config.py unset MBEDTLS_CHACHAPOLY_C
1970    #Disable TLS 1.3 (as no AEAD)
1971    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
1972    # Enable CBC-legacy (controlled by MBEDTLS_CIPHER_MODE_CBC plus at least one block cipher (AES, ARIA, Camellia, DES))
1973    scripts/config.py set MBEDTLS_CIPHER_MODE_CBC
1974    # Enable CBC-EtM (controlled by the same as CBC-legacy plus MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1975    scripts/config.py set MBEDTLS_SSL_ENCRYPT_THEN_MAC
1976    # Disable stream (currently that's just the NULL pseudo-cipher (controlled by MBEDTLS_CIPHER_NULL_CIPHER))
1977    scripts/config.py unset MBEDTLS_CIPHER_NULL_CIPHER
1978    # Modules that depend on AEAD
1979    scripts/config.py unset MBEDTLS_SSL_CONTEXT_SERIALIZATION
1980    scripts/config.py unset MBEDTLS_SSL_TICKET_C
1981
1982    make
1983
1984    msg "test: default with only CBC-legacy and CBC-EtM ciphers"
1985    make test
1986
1987    msg "test: default with only CBC-legacy and CBC-EtM ciphers - ssl-opt.sh (subset)"
1988    tests/ssl-opt.sh -f "TLS 1.2"
1989}
1990
1991component_test_tls1_2_default_cbc_legacy_cbc_etm_cipher_only_use_psa () {
1992    msg "build: default with only CBC-legacy and CBC-EtM ciphers use psa"
1993
1994    scripts/config.py set MBEDTLS_USE_PSA_CRYPTO
1995    # Disable AEAD (controlled by the presence of one of GCM_C, CCM_C, CHACHAPOLY_C)
1996    scripts/config.py unset MBEDTLS_GCM_C
1997    scripts/config.py unset MBEDTLS_CCM_C
1998    scripts/config.py unset MBEDTLS_CHACHAPOLY_C
1999    #Disable TLS 1.3 (as no AEAD)
2000    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
2001    # Enable CBC-legacy (controlled by MBEDTLS_CIPHER_MODE_CBC plus at least one block cipher (AES, ARIA, Camellia, DES))
2002    scripts/config.py set MBEDTLS_CIPHER_MODE_CBC
2003    # Enable CBC-EtM (controlled by the same as CBC-legacy plus MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2004    scripts/config.py set MBEDTLS_SSL_ENCRYPT_THEN_MAC
2005    # Disable stream (currently that's just the NULL pseudo-cipher (controlled by MBEDTLS_CIPHER_NULL_CIPHER))
2006    scripts/config.py unset MBEDTLS_CIPHER_NULL_CIPHER
2007    # Modules that depend on AEAD
2008    scripts/config.py unset MBEDTLS_SSL_CONTEXT_SERIALIZATION
2009    scripts/config.py unset MBEDTLS_SSL_TICKET_C
2010
2011    make
2012
2013    msg "test: default with only CBC-legacy and CBC-EtM ciphers use psa"
2014    make test
2015
2016    msg "test: default with only CBC-legacy and CBC-EtM ciphers use psa - ssl-opt.sh (subset)"
2017    tests/ssl-opt.sh -f "TLS 1.2"
2018}
2019
2020# We're not aware of any other (open source) implementation of EC J-PAKE in TLS
2021# that we could use for interop testing. However, we now have sort of two
2022# implementations ourselves: one using PSA, the other not. At least test that
2023# these two interoperate with each other.
2024component_test_tls1_2_ecjpake_compatibility() {
2025    msg "build: TLS1.2 server+client w/ EC-JPAKE w/o USE_PSA"
2026    scripts/config.py set MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2027    # Explicitly make lib first to avoid a race condition:
2028    # https://github.com/Mbed-TLS/mbedtls/issues/8229
2029    make lib
2030    make -C programs ssl/ssl_server2 ssl/ssl_client2
2031    cp programs/ssl/ssl_server2 s2_no_use_psa
2032    cp programs/ssl/ssl_client2 c2_no_use_psa
2033
2034    msg "build: TLS1.2 server+client w/ EC-JPAKE w/ USE_PSA"
2035    scripts/config.py set MBEDTLS_USE_PSA_CRYPTO
2036    make clean
2037    make lib
2038    make -C programs ssl/ssl_server2 ssl/ssl_client2
2039    make -C programs test/udp_proxy test/query_compile_time_config
2040
2041    msg "test: server w/o USE_PSA - client w/ USE_PSA, text password"
2042    P_SRV=../s2_no_use_psa tests/ssl-opt.sh -f "ECJPAKE: working, TLS"
2043    msg "test: server w/o USE_PSA - client w/ USE_PSA, opaque password"
2044    P_SRV=../s2_no_use_psa tests/ssl-opt.sh -f "ECJPAKE: opaque password client only, working, TLS"
2045    msg "test: client w/o USE_PSA - server w/ USE_PSA, text password"
2046    P_CLI=../c2_no_use_psa tests/ssl-opt.sh -f "ECJPAKE: working, TLS"
2047    msg "test: client w/o USE_PSA - server w/ USE_PSA, opaque password"
2048    P_CLI=../c2_no_use_psa tests/ssl-opt.sh -f "ECJPAKE: opaque password server only, working, TLS"
2049
2050    rm s2_no_use_psa c2_no_use_psa
2051}
2052
2053component_test_everest () {
2054    msg "build: Everest ECDH context (ASan build)" # ~ 6 min
2055    scripts/config.py set MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED
2056    CC=clang cmake -D CMAKE_BUILD_TYPE:String=Asan .
2057    make
2058
2059    msg "test: Everest ECDH context - main suites (inc. selftests) (ASan build)" # ~ 50s
2060    make test
2061
2062    msg "test: metatests (clang, ASan)"
2063    tests/scripts/run-metatests.sh any asan poison
2064
2065    msg "test: Everest ECDH context - ECDH-related part of ssl-opt.sh (ASan build)" # ~ 5s
2066    tests/ssl-opt.sh -f ECDH
2067
2068    msg "test: Everest ECDH context - compat.sh with some ECDH ciphersuites (ASan build)" # ~ 3 min
2069    # Exclude some symmetric ciphers that are redundant here to gain time.
2070    tests/compat.sh -f ECDH -V NO -e 'ARIA\|CAMELLIA\|CHACHA'
2071}
2072
2073component_test_everest_curve25519_only () {
2074    msg "build: Everest ECDH context, only Curve25519" # ~ 6 min
2075    scripts/config.py set MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED
2076    scripts/config.py unset MBEDTLS_ECDSA_C
2077    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
2078    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2079    scripts/config.py unset MBEDTLS_ECJPAKE_C
2080    # Disable all curves
2081    scripts/config.py unset-all "MBEDTLS_ECP_DP_[0-9A-Z_a-z]*_ENABLED"
2082    scripts/config.py set MBEDTLS_ECP_DP_CURVE25519_ENABLED
2083
2084    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
2085
2086    msg "test: Everest ECDH context, only Curve25519" # ~ 50s
2087    make test
2088}
2089
2090component_test_small_ssl_out_content_len () {
2091    msg "build: small SSL_OUT_CONTENT_LEN (ASan build)"
2092    scripts/config.py set MBEDTLS_SSL_IN_CONTENT_LEN 16384
2093    scripts/config.py set MBEDTLS_SSL_OUT_CONTENT_LEN 4096
2094    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
2095    make
2096
2097    msg "test: small SSL_OUT_CONTENT_LEN - ssl-opt.sh MFL and large packet tests"
2098    tests/ssl-opt.sh -f "Max fragment\|Large packet"
2099}
2100
2101component_test_small_ssl_in_content_len () {
2102    msg "build: small SSL_IN_CONTENT_LEN (ASan build)"
2103    scripts/config.py set MBEDTLS_SSL_IN_CONTENT_LEN 4096
2104    scripts/config.py set MBEDTLS_SSL_OUT_CONTENT_LEN 16384
2105    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
2106    make
2107
2108    msg "test: small SSL_IN_CONTENT_LEN - ssl-opt.sh MFL tests"
2109    tests/ssl-opt.sh -f "Max fragment"
2110}
2111
2112component_test_small_ssl_dtls_max_buffering () {
2113    msg "build: small MBEDTLS_SSL_DTLS_MAX_BUFFERING #0"
2114    scripts/config.py set MBEDTLS_SSL_DTLS_MAX_BUFFERING 1000
2115    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
2116    make
2117
2118    msg "test: small MBEDTLS_SSL_DTLS_MAX_BUFFERING #0 - ssl-opt.sh specific reordering test"
2119    tests/ssl-opt.sh -f "DTLS reordering: Buffer out-of-order hs msg before reassembling next, free buffered msg"
2120}
2121
2122component_test_small_mbedtls_ssl_dtls_max_buffering () {
2123    msg "build: small MBEDTLS_SSL_DTLS_MAX_BUFFERING #1"
2124    scripts/config.py set MBEDTLS_SSL_DTLS_MAX_BUFFERING 190
2125    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
2126    make
2127
2128    msg "test: small MBEDTLS_SSL_DTLS_MAX_BUFFERING #1 - ssl-opt.sh specific reordering test"
2129    tests/ssl-opt.sh -f "DTLS reordering: Buffer encrypted Finished message, drop for fragmented NewSessionTicket"
2130}
2131
2132component_test_psa_collect_statuses () {
2133  msg "build+test: psa_collect_statuses" # ~30s
2134  scripts/config.py full
2135  tests/scripts/psa_collect_statuses.py
2136  # Check that psa_crypto_init() succeeded at least once
2137  grep -q '^0:psa_crypto_init:' tests/statuses.log
2138  rm -f tests/statuses.log
2139}
2140
2141component_test_full_cmake_clang () {
2142    msg "build: cmake, full config, clang" # ~ 50s
2143    scripts/config.py full
2144    CC=clang CXX=clang cmake -D CMAKE_BUILD_TYPE:String=Release -D ENABLE_TESTING=On -D TEST_CPP=1 .
2145    make
2146
2147    msg "test: main suites (full config, clang)" # ~ 5s
2148    make test
2149
2150    msg "test: cpp_dummy_build (full config, clang)" # ~ 1s
2151    programs/test/cpp_dummy_build
2152
2153    msg "test: metatests (clang)"
2154    tests/scripts/run-metatests.sh any pthread
2155
2156    msg "program demos (full config, clang)" # ~10s
2157    tests/scripts/run_demos.py
2158
2159    msg "test: psa_constant_names (full config, clang)" # ~ 1s
2160    tests/scripts/test_psa_constant_names.py
2161
2162    msg "test: ssl-opt.sh default, ECJPAKE, SSL async (full config)" # ~ 1s
2163    tests/ssl-opt.sh -f 'Default\|ECJPAKE\|SSL async private'
2164
2165    msg "test: compat.sh NULL (full config)" # ~ 2 min
2166    tests/compat.sh -e '^$' -f 'NULL'
2167
2168    msg "test: compat.sh ARIA + ChachaPoly"
2169    env OPENSSL="$OPENSSL_NEXT" tests/compat.sh -e '^$' -f 'ARIA\|CHACHA'
2170}
2171
2172skip_suites_without_constant_flow () {
2173    # Skip the test suites that don't have any constant-flow annotations.
2174    # This will need to be adjusted if we ever start declaring things as
2175    # secret from macros or functions inside tests/include or tests/src.
2176    SKIP_TEST_SUITES=$(
2177        git -C tests/suites grep -L TEST_CF_ 'test_suite_*.function' |
2178            sed 's/test_suite_//; s/\.function$//' |
2179            tr '\n' ,)
2180    export SKIP_TEST_SUITES
2181}
2182
2183skip_all_except_given_suite () {
2184    # Skip all but the given test suite
2185    SKIP_TEST_SUITES=$(
2186        ls -1 tests/suites/test_suite_*.function |
2187        grep -v $1.function |
2188         sed 's/tests.suites.test_suite_//; s/\.function$//' |
2189        tr '\n' ,)
2190    export SKIP_TEST_SUITES
2191}
2192
2193component_test_memsan_constant_flow () {
2194    # This tests both (1) accesses to undefined memory, and (2) branches or
2195    # memory access depending on secret values. To distinguish between those:
2196    # - unset MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN - does the failure persist?
2197    # - or alternatively, change the build type to MemSanDbg, which enables
2198    # origin tracking and nicer stack traces (which are useful for debugging
2199    # anyway), and check if the origin was TEST_CF_SECRET() or something else.
2200    msg "build: cmake MSan (clang), full config minus MBEDTLS_USE_PSA_CRYPTO with constant flow testing"
2201    scripts/config.py full
2202    scripts/config.py set MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN
2203    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
2204    scripts/config.py unset MBEDTLS_AESNI_C # memsan doesn't grok asm
2205    CC=clang cmake -D CMAKE_BUILD_TYPE:String=MemSan .
2206    make
2207
2208    msg "test: main suites (full minus MBEDTLS_USE_PSA_CRYPTO, Msan + constant flow)"
2209    make test
2210}
2211
2212component_test_memsan_constant_flow_psa () {
2213    # This tests both (1) accesses to undefined memory, and (2) branches or
2214    # memory access depending on secret values. To distinguish between those:
2215    # - unset MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN - does the failure persist?
2216    # - or alternatively, change the build type to MemSanDbg, which enables
2217    # origin tracking and nicer stack traces (which are useful for debugging
2218    # anyway), and check if the origin was TEST_CF_SECRET() or something else.
2219    msg "build: cmake MSan (clang), full config with constant flow testing"
2220    scripts/config.py full
2221    scripts/config.py set MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN
2222    scripts/config.py unset MBEDTLS_AESNI_C # memsan doesn't grok asm
2223    CC=clang cmake -D CMAKE_BUILD_TYPE:String=MemSan .
2224    make
2225
2226    msg "test: main suites (Msan + constant flow)"
2227    make test
2228}
2229
2230component_release_test_valgrind_constant_flow () {
2231    # This tests both (1) everything that valgrind's memcheck usually checks
2232    # (heap buffer overflows, use of uninitialized memory, use-after-free,
2233    # etc.) and (2) branches or memory access depending on secret values,
2234    # which will be reported as uninitialized memory. To distinguish between
2235    # secret and actually uninitialized:
2236    # - unset MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND - does the failure persist?
2237    # - or alternatively, build with debug info and manually run the offending
2238    # test suite with valgrind --track-origins=yes, then check if the origin
2239    # was TEST_CF_SECRET() or something else.
2240    msg "build: cmake release GCC, full config minus MBEDTLS_USE_PSA_CRYPTO with constant flow testing"
2241    scripts/config.py full
2242    scripts/config.py set MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND
2243    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
2244    skip_suites_without_constant_flow
2245    cmake -D CMAKE_BUILD_TYPE:String=Release .
2246    make
2247
2248    # this only shows a summary of the results (how many of each type)
2249    # details are left in Testing/<date>/DynamicAnalysis.xml
2250    msg "test: some suites (full minus MBEDTLS_USE_PSA_CRYPTO, valgrind + constant flow)"
2251    make memcheck
2252
2253    # Test asm path in constant time module - by default, it will test the plain C
2254    # path under Valgrind or Memsan. Running only the constant_time tests is fast (<1s)
2255    msg "test: valgrind asm constant_time"
2256    scripts/config.py --force set MBEDTLS_TEST_CONSTANT_FLOW_ASM
2257    skip_all_except_given_suite test_suite_constant_time
2258    cmake -D CMAKE_BUILD_TYPE:String=Release .
2259    make clean
2260    make
2261    make memcheck
2262}
2263
2264component_release_test_valgrind_constant_flow_psa () {
2265    # This tests both (1) everything that valgrind's memcheck usually checks
2266    # (heap buffer overflows, use of uninitialized memory, use-after-free,
2267    # etc.) and (2) branches or memory access depending on secret values,
2268    # which will be reported as uninitialized memory. To distinguish between
2269    # secret and actually uninitialized:
2270    # - unset MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND - does the failure persist?
2271    # - or alternatively, build with debug info and manually run the offending
2272    # test suite with valgrind --track-origins=yes, then check if the origin
2273    # was TEST_CF_SECRET() or something else.
2274    msg "build: cmake release GCC, full config with constant flow testing"
2275    scripts/config.py full
2276    scripts/config.py set MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND
2277    skip_suites_without_constant_flow
2278    cmake -D CMAKE_BUILD_TYPE:String=Release .
2279    make
2280
2281    # this only shows a summary of the results (how many of each type)
2282    # details are left in Testing/<date>/DynamicAnalysis.xml
2283    msg "test: some suites (valgrind + constant flow)"
2284    make memcheck
2285}
2286
2287component_test_tsan () {
2288    msg "build: TSan (clang)"
2289    scripts/config.py full
2290    scripts/config.py set MBEDTLS_THREADING_C
2291    scripts/config.py set MBEDTLS_THREADING_PTHREAD
2292    # Self-tests do not currently use multiple threads.
2293    scripts/config.py unset MBEDTLS_SELF_TEST
2294
2295    # The deprecated MBEDTLS_PSA_CRYPTO_SE_C interface is not thread safe.
2296    scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
2297
2298    CC=clang cmake -D CMAKE_BUILD_TYPE:String=TSan .
2299    make
2300
2301    msg "test: main suites (TSan)"
2302    make test
2303}
2304
2305component_test_default_no_deprecated () {
2306    # Test that removing the deprecated features from the default
2307    # configuration leaves something consistent.
2308    msg "build: make, default + MBEDTLS_DEPRECATED_REMOVED" # ~ 30s
2309    scripts/config.py set MBEDTLS_DEPRECATED_REMOVED
2310    make CFLAGS='-O -Werror -Wall -Wextra'
2311
2312    msg "test: make, default + MBEDTLS_DEPRECATED_REMOVED" # ~ 5s
2313    make test
2314}
2315
2316component_test_full_no_deprecated () {
2317    msg "build: make, full_no_deprecated config" # ~ 30s
2318    scripts/config.py full_no_deprecated
2319    make CFLAGS='-O -Werror -Wall -Wextra'
2320
2321    msg "test: make, full_no_deprecated config" # ~ 5s
2322    make test
2323
2324    msg "test: ensure that X509 has no direct dependency on BIGNUM_C"
2325    not grep mbedtls_mpi library/libmbedx509.a
2326}
2327
2328component_test_full_no_deprecated_deprecated_warning () {
2329    # Test that there is nothing deprecated in "full_no_deprecated".
2330    # A deprecated feature would trigger a warning (made fatal) from
2331    # MBEDTLS_DEPRECATED_WARNING.
2332    msg "build: make, full_no_deprecated config, MBEDTLS_DEPRECATED_WARNING" # ~ 30s
2333    scripts/config.py full_no_deprecated
2334    scripts/config.py unset MBEDTLS_DEPRECATED_REMOVED
2335    scripts/config.py set MBEDTLS_DEPRECATED_WARNING
2336    make CFLAGS='-O -Werror -Wall -Wextra'
2337
2338    msg "test: make, full_no_deprecated config, MBEDTLS_DEPRECATED_WARNING" # ~ 5s
2339    make test
2340}
2341
2342component_test_full_deprecated_warning () {
2343    # Test that when MBEDTLS_DEPRECATED_WARNING is enabled, the build passes
2344    # with only certain whitelisted types of warnings.
2345    msg "build: make, full config + MBEDTLS_DEPRECATED_WARNING, expect warnings" # ~ 30s
2346    scripts/config.py full
2347    scripts/config.py set MBEDTLS_DEPRECATED_WARNING
2348    # Expect warnings from '#warning' directives in check_config.h.
2349    # Note that gcc is required to allow the use of -Wno-error=cpp, which allows us to
2350    # display #warning messages without them being treated as errors.
2351    make CC=gcc CFLAGS='-O -Werror -Wall -Wextra -Wno-error=cpp' lib programs
2352
2353    msg "build: make tests, full config + MBEDTLS_DEPRECATED_WARNING, expect warnings" # ~ 30s
2354    # Set MBEDTLS_TEST_DEPRECATED to enable tests for deprecated features.
2355    # By default those are disabled when MBEDTLS_DEPRECATED_WARNING is set.
2356    # Expect warnings from '#warning' directives in check_config.h and
2357    # from the use of deprecated functions in test suites.
2358    make CC=gcc CFLAGS='-O -Werror -Wall -Wextra -Wno-error=deprecated-declarations -Wno-error=cpp -DMBEDTLS_TEST_DEPRECATED' tests
2359
2360    msg "test: full config + MBEDTLS_TEST_DEPRECATED" # ~ 30s
2361    make test
2362
2363    msg "program demos: full config + MBEDTLS_TEST_DEPRECATED" # ~10s
2364    tests/scripts/run_demos.py
2365}
2366
2367# Check that the specified libraries exist and are empty.
2368are_empty_libraries () {
2369  nm "$@" >/dev/null 2>/dev/null
2370  ! nm "$@" 2>/dev/null | grep -v ':$' | grep .
2371}
2372
2373component_build_crypto_default () {
2374  msg "build: make, crypto only"
2375  scripts/config.py crypto
2376  make CFLAGS='-O1 -Werror'
2377  are_empty_libraries library/libmbedx509.* library/libmbedtls.*
2378}
2379
2380component_build_crypto_full () {
2381  msg "build: make, crypto only, full config"
2382  scripts/config.py crypto_full
2383  make CFLAGS='-O1 -Werror'
2384  are_empty_libraries library/libmbedx509.* library/libmbedtls.*
2385}
2386
2387component_test_crypto_for_psa_service () {
2388  msg "build: make, config for PSA crypto service"
2389  scripts/config.py crypto
2390  scripts/config.py set MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
2391  # Disable things that are not needed for just cryptography, to
2392  # reach a configuration that would be typical for a PSA cryptography
2393  # service providing all implemented PSA algorithms.
2394  # System stuff
2395  scripts/config.py unset MBEDTLS_ERROR_C
2396  scripts/config.py unset MBEDTLS_TIMING_C
2397  scripts/config.py unset MBEDTLS_VERSION_FEATURES
2398  # Crypto stuff with no PSA interface
2399  scripts/config.py unset MBEDTLS_BASE64_C
2400  # Keep MBEDTLS_CIPHER_C because psa_crypto_cipher, CCM and GCM need it.
2401  scripts/config.py unset MBEDTLS_HKDF_C # PSA's HKDF is independent
2402  # Keep MBEDTLS_MD_C because deterministic ECDSA needs it for HMAC_DRBG.
2403  scripts/config.py unset MBEDTLS_NIST_KW_C
2404  scripts/config.py unset MBEDTLS_PEM_PARSE_C
2405  scripts/config.py unset MBEDTLS_PEM_WRITE_C
2406  scripts/config.py unset MBEDTLS_PKCS12_C
2407  scripts/config.py unset MBEDTLS_PKCS5_C
2408  # MBEDTLS_PK_PARSE_C and MBEDTLS_PK_WRITE_C are actually currently needed
2409  # in PSA code to work with RSA keys. We don't require users to set those:
2410  # they will be reenabled in build_info.h.
2411  scripts/config.py unset MBEDTLS_PK_C
2412  scripts/config.py unset MBEDTLS_PK_PARSE_C
2413  scripts/config.py unset MBEDTLS_PK_WRITE_C
2414  make CFLAGS='-O1 -Werror' all test
2415  are_empty_libraries library/libmbedx509.* library/libmbedtls.*
2416}
2417
2418component_build_crypto_baremetal () {
2419  msg "build: make, crypto only, baremetal config"
2420  scripts/config.py crypto_baremetal
2421  make CFLAGS="-O1 -Werror -I$PWD/tests/include/baremetal-override/"
2422  are_empty_libraries library/libmbedx509.* library/libmbedtls.*
2423}
2424support_build_crypto_baremetal () {
2425    support_build_baremetal "$@"
2426}
2427
2428component_build_baremetal () {
2429  msg "build: make, baremetal config"
2430  scripts/config.py baremetal
2431  make CFLAGS="-O1 -Werror -I$PWD/tests/include/baremetal-override/"
2432}
2433support_build_baremetal () {
2434    # Older Glibc versions include time.h from other headers such as stdlib.h,
2435    # which makes the no-time.h-in-baremetal check fail. Ubuntu 16.04 has this
2436    # problem, Ubuntu 18.04 is ok.
2437    ! grep -q -F time.h /usr/include/x86_64-linux-gnu/sys/types.h
2438}
2439
2440# depends.py family of tests
2441component_test_depends_py_cipher_id () {
2442    msg "test/build: depends.py cipher_id (gcc)"
2443    tests/scripts/depends.py cipher_id --unset-use-psa
2444}
2445
2446component_test_depends_py_cipher_chaining () {
2447    msg "test/build: depends.py cipher_chaining (gcc)"
2448    tests/scripts/depends.py cipher_chaining --unset-use-psa
2449}
2450
2451component_test_depends_py_cipher_padding () {
2452    msg "test/build: depends.py cipher_padding (gcc)"
2453    tests/scripts/depends.py cipher_padding --unset-use-psa
2454}
2455
2456component_test_depends_py_curves () {
2457    msg "test/build: depends.py curves (gcc)"
2458    tests/scripts/depends.py curves --unset-use-psa
2459}
2460
2461component_test_depends_py_hashes () {
2462    msg "test/build: depends.py hashes (gcc)"
2463    tests/scripts/depends.py hashes --unset-use-psa
2464}
2465
2466component_test_depends_py_kex () {
2467    msg "test/build: depends.py kex (gcc)"
2468    tests/scripts/depends.py kex --unset-use-psa
2469}
2470
2471component_test_depends_py_pkalgs () {
2472    msg "test/build: depends.py pkalgs (gcc)"
2473    tests/scripts/depends.py pkalgs --unset-use-psa
2474}
2475
2476# PSA equivalents of the depends.py tests
2477component_test_depends_py_cipher_id_psa () {
2478    msg "test/build: depends.py cipher_id (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
2479    tests/scripts/depends.py cipher_id
2480}
2481
2482component_test_depends_py_cipher_chaining_psa () {
2483    msg "test/build: depends.py cipher_chaining (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
2484    tests/scripts/depends.py cipher_chaining
2485}
2486
2487component_test_depends_py_cipher_padding_psa () {
2488    msg "test/build: depends.py cipher_padding (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
2489    tests/scripts/depends.py cipher_padding
2490}
2491
2492component_test_depends_py_curves_psa () {
2493    msg "test/build: depends.py curves (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
2494    tests/scripts/depends.py curves
2495}
2496
2497component_test_depends_py_hashes_psa () {
2498    msg "test/build: depends.py hashes (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
2499    tests/scripts/depends.py hashes
2500}
2501
2502component_test_depends_py_kex_psa () {
2503    msg "test/build: depends.py kex (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
2504    tests/scripts/depends.py kex
2505}
2506
2507component_test_depends_py_pkalgs_psa () {
2508    msg "test/build: depends.py pkalgs (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
2509    tests/scripts/depends.py pkalgs
2510}
2511
2512component_test_psa_crypto_config_ffdh_2048_only () {
2513    msg "build: full config - only DH 2048"
2514
2515    scripts/config.py full
2516
2517    # Disable all DH groups other than 2048.
2518    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_DH_RFC7919_3072
2519    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_DH_RFC7919_4096
2520    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_DH_RFC7919_6144
2521    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_DH_RFC7919_8192
2522
2523    make CFLAGS="$ASAN_CFLAGS -Werror" LDFLAGS="$ASAN_CFLAGS"
2524
2525    msg "test: full config - only DH 2048"
2526    make test
2527
2528    msg "ssl-opt: full config - only DH 2048"
2529    tests/ssl-opt.sh -f "ffdh"
2530}
2531
2532component_build_no_pk_rsa_alt_support () {
2533    msg "build: !MBEDTLS_PK_RSA_ALT_SUPPORT" # ~30s
2534
2535    scripts/config.py full
2536    scripts/config.py unset MBEDTLS_PK_RSA_ALT_SUPPORT
2537    scripts/config.py set MBEDTLS_RSA_C
2538    scripts/config.py set MBEDTLS_X509_CRT_WRITE_C
2539
2540    # Only compile - this is primarily to test for compile issues
2541    make CFLAGS='-Werror -Wall -Wextra -I../tests/include/alt-dummy'
2542}
2543
2544component_build_module_alt () {
2545    msg "build: MBEDTLS_XXX_ALT" # ~30s
2546    scripts/config.py full
2547
2548    # Disable options that are incompatible with some ALT implementations:
2549    # aesni.c and padlock.c reference mbedtls_aes_context fields directly.
2550    scripts/config.py unset MBEDTLS_AESNI_C
2551    scripts/config.py unset MBEDTLS_PADLOCK_C
2552    scripts/config.py unset MBEDTLS_AESCE_C
2553    # MBEDTLS_ECP_RESTARTABLE is documented as incompatible.
2554    scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
2555    # You can only have one threading implementation: alt or pthread, not both.
2556    scripts/config.py unset MBEDTLS_THREADING_PTHREAD
2557    # The SpecifiedECDomain parsing code accesses mbedtls_ecp_group fields
2558    # directly and assumes the implementation works with partial groups.
2559    scripts/config.py unset MBEDTLS_PK_PARSE_EC_EXTENDED
2560    # MBEDTLS_SHA256_*ALT can't be used with MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_*
2561    scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
2562    scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY
2563    # MBEDTLS_SHA512_*ALT can't be used with MBEDTLS_SHA512_USE_A64_CRYPTO_*
2564    scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
2565    scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY
2566
2567    # Enable all MBEDTLS_XXX_ALT for whole modules. Do not enable
2568    # MBEDTLS_XXX_YYY_ALT which are for single functions.
2569    scripts/config.py set-all 'MBEDTLS_([A-Z0-9]*|NIST_KW)_ALT'
2570    scripts/config.py unset MBEDTLS_DHM_ALT #incompatible with MBEDTLS_DEBUG_C
2571
2572    # We can only compile, not link, since we don't have any implementations
2573    # suitable for testing with the dummy alt headers.
2574    make CFLAGS='-Werror -Wall -Wextra -I../tests/include/alt-dummy' lib
2575}
2576
2577component_build_dhm_alt () {
2578    msg "build: MBEDTLS_DHM_ALT" # ~30s
2579    scripts/config.py full
2580    scripts/config.py set MBEDTLS_DHM_ALT
2581    # debug.c currently references mbedtls_dhm_context fields directly.
2582    scripts/config.py unset MBEDTLS_DEBUG_C
2583    # We can only compile, not link, since we don't have any implementations
2584    # suitable for testing with the dummy alt headers.
2585    make CFLAGS='-Werror -Wall -Wextra -I../tests/include/alt-dummy' lib
2586}
2587
2588component_test_no_psa_crypto_full_cmake_asan() {
2589    # full minus MBEDTLS_PSA_CRYPTO_C: run the same set of tests as basic-build-test.sh
2590    msg "build: cmake, full config minus PSA crypto, ASan"
2591    scripts/config.py full
2592    scripts/config.py unset MBEDTLS_PSA_CRYPTO_C
2593    scripts/config.py unset MBEDTLS_PSA_CRYPTO_CLIENT
2594    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
2595    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
2596    scripts/config.py unset MBEDTLS_PSA_ITS_FILE_C
2597    scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
2598    scripts/config.py unset MBEDTLS_PSA_CRYPTO_STORAGE_C
2599    scripts/config.py unset MBEDTLS_LMS_C
2600    scripts/config.py unset MBEDTLS_LMS_PRIVATE
2601    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
2602    make
2603
2604    msg "test: main suites (full minus PSA crypto)"
2605    make test
2606
2607    # Note: ssl-opt.sh has some test cases that depend on
2608    # MBEDTLS_ECP_RESTARTABLE && !MBEDTLS_USE_PSA_CRYPTO
2609    # This is the only component where those tests are not skipped.
2610    msg "test: ssl-opt.sh (full minus PSA crypto)"
2611    tests/ssl-opt.sh
2612
2613    msg "test: compat.sh default (full minus PSA crypto)"
2614    tests/compat.sh
2615
2616    msg "test: compat.sh NULL (full minus PSA crypto)"
2617    tests/compat.sh -f 'NULL'
2618
2619    msg "test: compat.sh ARIA + ChachaPoly (full minus PSA crypto)"
2620    env OPENSSL="$OPENSSL_NEXT" tests/compat.sh -e '^$' -f 'ARIA\|CHACHA'
2621}
2622
2623component_test_psa_crypto_config_accel_ecdsa () {
2624    msg "build: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated ECDSA"
2625
2626    # Algorithms and key types to accelerate
2627    loc_accel_list="ALG_ECDSA ALG_DETERMINISTIC_ECDSA \
2628                    $(helper_get_psa_key_type_list "ECC") \
2629                    $(helper_get_psa_curve_list)"
2630
2631    # Configure
2632    # ---------
2633
2634    # Start from default config (no USE_PSA) + TLS 1.3
2635    helper_libtestdriver1_adjust_config "default"
2636
2637    # Disable the module that's accelerated
2638    scripts/config.py unset MBEDTLS_ECDSA_C
2639
2640    # Disable things that depend on it
2641    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2642    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
2643
2644    # Build
2645    # -----
2646
2647    # These hashes are needed for some ECDSA signature tests.
2648    loc_extra_list="ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
2649                    ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
2650
2651    helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
2652
2653    helper_libtestdriver1_make_main "$loc_accel_list"
2654
2655    # Make sure this was not re-enabled by accident (additive config)
2656    not grep mbedtls_ecdsa_ library/ecdsa.o
2657
2658    # Run the tests
2659    # -------------
2660
2661    msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated ECDSA"
2662    make test
2663}
2664
2665component_test_psa_crypto_config_accel_ecdh () {
2666    msg "build: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated ECDH"
2667
2668    # Algorithms and key types to accelerate
2669    loc_accel_list="ALG_ECDH \
2670                    $(helper_get_psa_key_type_list "ECC") \
2671                    $(helper_get_psa_curve_list)"
2672
2673    # Configure
2674    # ---------
2675
2676    # Start from default config (no USE_PSA)
2677    helper_libtestdriver1_adjust_config "default"
2678
2679    # Disable the module that's accelerated
2680    scripts/config.py unset MBEDTLS_ECDH_C
2681
2682    # Disable things that depend on it
2683    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
2684    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
2685    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2686    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2687    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED
2688
2689    # Build
2690    # -----
2691
2692    helper_libtestdriver1_make_drivers "$loc_accel_list"
2693
2694    helper_libtestdriver1_make_main "$loc_accel_list"
2695
2696    # Make sure this was not re-enabled by accident (additive config)
2697    not grep mbedtls_ecdh_ library/ecdh.o
2698
2699    # Run the tests
2700    # -------------
2701
2702    msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated ECDH"
2703    make test
2704}
2705
2706component_test_psa_crypto_config_accel_ffdh () {
2707    msg "build: full with accelerated FFDH"
2708
2709    # Algorithms and key types to accelerate
2710    loc_accel_list="ALG_FFDH \
2711                    $(helper_get_psa_key_type_list "DH") \
2712                    $(helper_get_psa_dh_group_list)"
2713
2714    # Configure
2715    # ---------
2716
2717    # start with full (USE_PSA and TLS 1.3)
2718    helper_libtestdriver1_adjust_config "full"
2719
2720    # Disable the module that's accelerated
2721    scripts/config.py unset MBEDTLS_DHM_C
2722
2723    # Disable things that depend on it
2724    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
2725    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
2726
2727    # Build
2728    # -----
2729
2730    helper_libtestdriver1_make_drivers "$loc_accel_list"
2731
2732    helper_libtestdriver1_make_main "$loc_accel_list"
2733
2734    # Make sure this was not re-enabled by accident (additive config)
2735    not grep mbedtls_dhm_ library/dhm.o
2736
2737    # Run the tests
2738    # -------------
2739
2740    msg "test: full with accelerated FFDH"
2741    make test
2742
2743    msg "ssl-opt: full with accelerated FFDH alg"
2744    tests/ssl-opt.sh -f "ffdh"
2745}
2746
2747component_test_psa_crypto_config_reference_ffdh () {
2748    msg "build: full with non-accelerated FFDH"
2749
2750    # Start with full (USE_PSA and TLS 1.3)
2751    helper_libtestdriver1_adjust_config "full"
2752
2753    # Disable things that are not supported
2754    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
2755    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
2756    make
2757
2758    msg "test suites: full with non-accelerated FFDH alg"
2759    make test
2760
2761    msg "ssl-opt: full with non-accelerated FFDH alg"
2762    tests/ssl-opt.sh -f "ffdh"
2763}
2764
2765component_test_psa_crypto_config_accel_pake() {
2766    msg "build: full with accelerated PAKE"
2767
2768    loc_accel_list="ALG_JPAKE \
2769                    $(helper_get_psa_key_type_list "ECC") \
2770                    $(helper_get_psa_curve_list)"
2771
2772    # Configure
2773    # ---------
2774
2775    helper_libtestdriver1_adjust_config "full"
2776
2777    # Make built-in fallback not available
2778    scripts/config.py unset MBEDTLS_ECJPAKE_C
2779    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
2780
2781    # Build
2782    # -----
2783
2784    helper_libtestdriver1_make_drivers "$loc_accel_list"
2785
2786    helper_libtestdriver1_make_main "$loc_accel_list"
2787
2788    # Make sure this was not re-enabled by accident (additive config)
2789    not grep mbedtls_ecjpake_init library/ecjpake.o
2790
2791    # Run the tests
2792    # -------------
2793
2794    msg "test: full with accelerated PAKE"
2795    make test
2796}
2797
2798component_test_psa_crypto_config_accel_ecc_some_key_types () {
2799    msg "build: full with accelerated EC algs and some key types"
2800
2801    # Algorithms and key types to accelerate
2802    # For key types, use an explicitly list to omit GENERATE (and DERIVE)
2803    loc_accel_list="ALG_ECDSA ALG_DETERMINISTIC_ECDSA \
2804                    ALG_ECDH \
2805                    ALG_JPAKE \
2806                    KEY_TYPE_ECC_PUBLIC_KEY \
2807                    KEY_TYPE_ECC_KEY_PAIR_BASIC \
2808                    KEY_TYPE_ECC_KEY_PAIR_IMPORT \
2809                    KEY_TYPE_ECC_KEY_PAIR_EXPORT \
2810                    $(helper_get_psa_curve_list)"
2811
2812    # Configure
2813    # ---------
2814
2815    # start with config full for maximum coverage (also enables USE_PSA)
2816    helper_libtestdriver1_adjust_config "full"
2817
2818    # Disable modules that are accelerated - some will be re-enabled
2819    scripts/config.py unset MBEDTLS_ECDSA_C
2820    scripts/config.py unset MBEDTLS_ECDH_C
2821    scripts/config.py unset MBEDTLS_ECJPAKE_C
2822    scripts/config.py unset MBEDTLS_ECP_C
2823
2824    # Disable all curves - those that aren't accelerated should be re-enabled
2825    helper_disable_builtin_curves
2826
2827    # Restartable feature is not yet supported by PSA. Once it will in
2828    # the future, the following line could be removed (see issues
2829    # 6061, 6332 and following ones)
2830    scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
2831
2832    # this is not supported by the driver API yet
2833    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE
2834
2835    # Build
2836    # -----
2837
2838    # These hashes are needed for some ECDSA signature tests.
2839    loc_extra_list="ALG_SHA_1 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
2840                    ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
2841    helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
2842
2843    helper_libtestdriver1_make_main "$loc_accel_list"
2844
2845    # ECP should be re-enabled but not the others
2846    not grep mbedtls_ecdh_ library/ecdh.o
2847    not grep mbedtls_ecdsa library/ecdsa.o
2848    not grep mbedtls_ecjpake  library/ecjpake.o
2849    grep mbedtls_ecp library/ecp.o
2850
2851    # Run the tests
2852    # -------------
2853
2854    msg "test suites: full with accelerated EC algs and some key types"
2855    make test
2856}
2857
2858# Run tests with only (non-)Weierstrass accelerated
2859# Common code used in:
2860# - component_test_psa_crypto_config_accel_ecc_weierstrass_curves
2861# - component_test_psa_crypto_config_accel_ecc_non_weierstrass_curves
2862common_test_psa_crypto_config_accel_ecc_some_curves () {
2863    weierstrass=$1
2864    if [ $weierstrass -eq 1 ]; then
2865        desc="Weierstrass"
2866    else
2867        desc="non-Weierstrass"
2868    fi
2869
2870    msg "build: crypto_full minus PK with accelerated EC algs and $desc curves"
2871
2872    # Note: Curves are handled in a special way by the libtestdriver machinery,
2873    # so we only want to include them in the accel list when building the main
2874    # libraries, hence the use of a separate variable.
2875    # Note: the following loop is a modified version of
2876    # helper_get_psa_curve_list that only keeps Weierstrass families.
2877    loc_weierstrass_list=""
2878    loc_non_weierstrass_list=""
2879    for item in $(sed -n 's/^#define PSA_WANT_\(ECC_[0-9A-Z_a-z]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do
2880        case $item in
2881            ECC_BRAINPOOL*|ECC_SECP*)
2882                loc_weierstrass_list="$loc_weierstrass_list $item"
2883                ;;
2884            *)
2885                loc_non_weierstrass_list="$loc_non_weierstrass_list $item"
2886                ;;
2887        esac
2888    done
2889    if [ $weierstrass -eq 1 ]; then
2890        loc_curve_list=$loc_weierstrass_list
2891    else
2892        loc_curve_list=$loc_non_weierstrass_list
2893    fi
2894
2895    # Algorithms and key types to accelerate
2896    loc_accel_list="ALG_ECDSA ALG_DETERMINISTIC_ECDSA \
2897                    ALG_ECDH \
2898                    ALG_JPAKE \
2899                    $(helper_get_psa_key_type_list "ECC") \
2900                    $loc_curve_list"
2901
2902    # Configure
2903    # ---------
2904
2905    # Start with config crypto_full and remove PK_C:
2906    # that's what's supported now, see docs/driver-only-builds.md.
2907    helper_libtestdriver1_adjust_config "crypto_full"
2908    scripts/config.py unset MBEDTLS_PK_C
2909    scripts/config.py unset MBEDTLS_PK_PARSE_C
2910    scripts/config.py unset MBEDTLS_PK_WRITE_C
2911
2912    # Disable modules that are accelerated - some will be re-enabled
2913    scripts/config.py unset MBEDTLS_ECDSA_C
2914    scripts/config.py unset MBEDTLS_ECDH_C
2915    scripts/config.py unset MBEDTLS_ECJPAKE_C
2916    scripts/config.py unset MBEDTLS_ECP_C
2917
2918    # Disable all curves - those that aren't accelerated should be re-enabled
2919    helper_disable_builtin_curves
2920
2921    # Restartable feature is not yet supported by PSA. Once it will in
2922    # the future, the following line could be removed (see issues
2923    # 6061, 6332 and following ones)
2924    scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
2925
2926    # this is not supported by the driver API yet
2927    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE
2928
2929    # Build
2930    # -----
2931
2932    # These hashes are needed for some ECDSA signature tests.
2933    loc_extra_list="ALG_SHA_1 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
2934                    ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
2935    helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
2936
2937    helper_libtestdriver1_make_main "$loc_accel_list"
2938
2939    # We expect ECDH to be re-enabled for the missing curves
2940    grep mbedtls_ecdh_ library/ecdh.o
2941    # We expect ECP to be re-enabled, however the parts specific to the
2942    # families of curves that are accelerated should be ommited.
2943    # - functions with mxz in the name are specific to Montgomery curves
2944    # - ecp_muladd is specific to Weierstrass curves
2945    ##nm library/ecp.o | tee ecp.syms
2946    if [ $weierstrass -eq 1 ]; then
2947        not grep mbedtls_ecp_muladd library/ecp.o
2948        grep mxz library/ecp.o
2949    else
2950        grep mbedtls_ecp_muladd library/ecp.o
2951        not grep mxz library/ecp.o
2952    fi
2953    # We expect ECDSA and ECJPAKE to be re-enabled only when
2954    # Weierstrass curves are not accelerated
2955    if [ $weierstrass -eq 1 ]; then
2956        not grep mbedtls_ecdsa library/ecdsa.o
2957        not grep mbedtls_ecjpake  library/ecjpake.o
2958    else
2959        grep mbedtls_ecdsa library/ecdsa.o
2960        grep mbedtls_ecjpake  library/ecjpake.o
2961    fi
2962
2963    # Run the tests
2964    # -------------
2965
2966    msg "test suites: crypto_full minus PK with accelerated EC algs and $desc curves"
2967    make test
2968}
2969
2970component_test_psa_crypto_config_accel_ecc_weierstrass_curves () {
2971    common_test_psa_crypto_config_accel_ecc_some_curves 1
2972}
2973
2974component_test_psa_crypto_config_accel_ecc_non_weierstrass_curves () {
2975    common_test_psa_crypto_config_accel_ecc_some_curves 0
2976}
2977
2978# Auxiliary function to build config for all EC based algorithms (EC-JPAKE,
2979# ECDH, ECDSA) with and without drivers.
2980# The input parameter is a boolean value which indicates:
2981# - 0 keep built-in EC algs,
2982# - 1 exclude built-in EC algs (driver only).
2983#
2984# This is used by the two following components to ensure they always use the
2985# same config, except for the use of driver or built-in EC algorithms:
2986# - component_test_psa_crypto_config_accel_ecc_ecp_light_only;
2987# - component_test_psa_crypto_config_reference_ecc_ecp_light_only.
2988# This supports comparing their test coverage with analyze_outcomes.py.
2989config_psa_crypto_config_ecp_light_only () {
2990    driver_only="$1"
2991    # start with config full for maximum coverage (also enables USE_PSA)
2992    helper_libtestdriver1_adjust_config "full"
2993    if [ "$driver_only" -eq 1 ]; then
2994        # Disable modules that are accelerated
2995        scripts/config.py unset MBEDTLS_ECDSA_C
2996        scripts/config.py unset MBEDTLS_ECDH_C
2997        scripts/config.py unset MBEDTLS_ECJPAKE_C
2998        scripts/config.py unset MBEDTLS_ECP_C
2999    fi
3000
3001    # Restartable feature is not yet supported by PSA. Once it will in
3002    # the future, the following line could be removed (see issues
3003    # 6061, 6332 and following ones)
3004    scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
3005}
3006
3007# Keep in sync with component_test_psa_crypto_config_reference_ecc_ecp_light_only
3008component_test_psa_crypto_config_accel_ecc_ecp_light_only () {
3009    msg "build: full with accelerated EC algs"
3010
3011    # Algorithms and key types to accelerate
3012    loc_accel_list="ALG_ECDSA ALG_DETERMINISTIC_ECDSA \
3013                    ALG_ECDH \
3014                    ALG_JPAKE \
3015                    $(helper_get_psa_key_type_list "ECC") \
3016                    $(helper_get_psa_curve_list)"
3017
3018    # Configure
3019    # ---------
3020
3021    # Use the same config as reference, only without built-in EC algs
3022    config_psa_crypto_config_ecp_light_only 1
3023
3024    # Do not disable builtin curves because that support is required for:
3025    # - MBEDTLS_PK_PARSE_EC_EXTENDED
3026    # - MBEDTLS_PK_PARSE_EC_COMPRESSED
3027
3028    # Build
3029    # -----
3030
3031    # These hashes are needed for some ECDSA signature tests.
3032    loc_extra_list="ALG_SHA_1 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
3033                    ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
3034    helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
3035
3036    helper_libtestdriver1_make_main "$loc_accel_list"
3037
3038    # Make sure any built-in EC alg was not re-enabled by accident (additive config)
3039    not grep mbedtls_ecdsa_ library/ecdsa.o
3040    not grep mbedtls_ecdh_ library/ecdh.o
3041    not grep mbedtls_ecjpake_ library/ecjpake.o
3042    not grep mbedtls_ecp_mul library/ecp.o
3043
3044    # Run the tests
3045    # -------------
3046
3047    msg "test suites: full with accelerated EC algs"
3048    make test
3049
3050    msg "ssl-opt: full with accelerated EC algs"
3051    tests/ssl-opt.sh
3052}
3053
3054# Keep in sync with component_test_psa_crypto_config_accel_ecc_ecp_light_only
3055component_test_psa_crypto_config_reference_ecc_ecp_light_only () {
3056    msg "build: MBEDTLS_PSA_CRYPTO_CONFIG with non-accelerated EC algs"
3057
3058    config_psa_crypto_config_ecp_light_only 0
3059
3060    make
3061
3062    msg "test suites: full with non-accelerated EC algs"
3063    make test
3064
3065    msg "ssl-opt: full with non-accelerated EC algs"
3066    tests/ssl-opt.sh
3067}
3068
3069# This helper function is used by:
3070# - component_test_psa_crypto_config_accel_ecc_no_ecp_at_all()
3071# - component_test_psa_crypto_config_reference_ecc_no_ecp_at_all()
3072# to ensure that both tests use the same underlying configuration when testing
3073# driver's coverage with analyze_outcomes.py.
3074#
3075# This functions accepts 1 boolean parameter as follows:
3076# - 1: building with accelerated EC algorithms (ECDSA, ECDH, ECJPAKE), therefore
3077#      excluding their built-in implementation as well as ECP_C & ECP_LIGHT
3078# - 0: include built-in implementation of EC algorithms.
3079#
3080# PK_C and RSA_C are always disabled to ensure there is no remaining dependency
3081# on the ECP module.
3082config_psa_crypto_no_ecp_at_all () {
3083    driver_only="$1"
3084    # start with full config for maximum coverage (also enables USE_PSA)
3085    helper_libtestdriver1_adjust_config "full"
3086
3087    if [ "$driver_only" -eq 1 ]; then
3088        # Disable modules that are accelerated
3089        scripts/config.py unset MBEDTLS_ECDSA_C
3090        scripts/config.py unset MBEDTLS_ECDH_C
3091        scripts/config.py unset MBEDTLS_ECJPAKE_C
3092        # Disable ECP module (entirely)
3093        scripts/config.py unset MBEDTLS_ECP_C
3094    fi
3095
3096    # Disable all the features that auto-enable ECP_LIGHT (see build_info.h)
3097    scripts/config.py unset MBEDTLS_PK_PARSE_EC_EXTENDED
3098    scripts/config.py unset MBEDTLS_PK_PARSE_EC_COMPRESSED
3099    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE
3100
3101    # Restartable feature is not yet supported by PSA. Once it will in
3102    # the future, the following line could be removed (see issues
3103    # 6061, 6332 and following ones)
3104    scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
3105}
3106
3107# Build and test a configuration where driver accelerates all EC algs while
3108# all support and dependencies from ECP and ECP_LIGHT are removed on the library
3109# side.
3110#
3111# Keep in sync with component_test_psa_crypto_config_reference_ecc_no_ecp_at_all()
3112component_test_psa_crypto_config_accel_ecc_no_ecp_at_all () {
3113    msg "build: full + accelerated EC algs - ECP"
3114
3115    # Algorithms and key types to accelerate
3116    loc_accel_list="ALG_ECDSA ALG_DETERMINISTIC_ECDSA \
3117                    ALG_ECDH \
3118                    ALG_JPAKE \
3119                    $(helper_get_psa_key_type_list "ECC") \
3120                    $(helper_get_psa_curve_list)"
3121
3122    # Configure
3123    # ---------
3124
3125    # Set common configurations between library's and driver's builds
3126    config_psa_crypto_no_ecp_at_all 1
3127    # Disable all the builtin curves. All the required algs are accelerated.
3128    helper_disable_builtin_curves
3129
3130    # Build
3131    # -----
3132
3133    # Things we wanted supported in libtestdriver1, but not accelerated in the main library:
3134    # SHA-1 and all SHA-2/3 variants, as they are used by ECDSA deterministic.
3135    loc_extra_list="ALG_SHA_1 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
3136                    ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
3137
3138    helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
3139
3140    helper_libtestdriver1_make_main "$loc_accel_list"
3141
3142    # Make sure any built-in EC alg was not re-enabled by accident (additive config)
3143    not grep mbedtls_ecdsa_ library/ecdsa.o
3144    not grep mbedtls_ecdh_ library/ecdh.o
3145    not grep mbedtls_ecjpake_ library/ecjpake.o
3146    # Also ensure that ECP module was not re-enabled
3147    not grep mbedtls_ecp_ library/ecp.o
3148
3149    # Run the tests
3150    # -------------
3151
3152    msg "test: full + accelerated EC algs - ECP"
3153    make test
3154
3155    msg "ssl-opt: full + accelerated EC algs - ECP"
3156    tests/ssl-opt.sh
3157}
3158
3159# Reference function used for driver's coverage analysis in analyze_outcomes.py
3160# in conjunction with component_test_psa_crypto_config_accel_ecc_no_ecp_at_all().
3161# Keep in sync with its accelerated counterpart.
3162component_test_psa_crypto_config_reference_ecc_no_ecp_at_all () {
3163    msg "build: full + non accelerated EC algs"
3164
3165    config_psa_crypto_no_ecp_at_all 0
3166
3167    make
3168
3169    msg "test: full + non accelerated EC algs"
3170    make test
3171
3172    msg "ssl-opt: full + non accelerated EC algs"
3173    tests/ssl-opt.sh
3174}
3175
3176# This is a common configuration helper used directly from:
3177# - common_test_psa_crypto_config_accel_ecc_ffdh_no_bignum
3178# - common_test_psa_crypto_config_reference_ecc_ffdh_no_bignum
3179# and indirectly from:
3180# - component_test_psa_crypto_config_accel_ecc_no_bignum
3181#       - accelerate all EC algs, disable RSA and FFDH
3182# - component_test_psa_crypto_config_reference_ecc_no_bignum
3183#       - this is the reference component of the above
3184#       - it still disables RSA and FFDH, but it uses builtin EC algs
3185# - component_test_psa_crypto_config_accel_ecc_ffdh_no_bignum
3186#       - accelerate all EC and FFDH algs, disable only RSA
3187# - component_test_psa_crypto_config_reference_ecc_ffdh_no_bignum
3188#       - this is the reference component of the above
3189#       - it still disables RSA, but it uses builtin EC and FFDH algs
3190#
3191# This function accepts 2 parameters:
3192# $1: a boolean value which states if we are testing an accelerated scenario
3193#     or not.
3194# $2: a string value which states which components are tested. Allowed values
3195#     are "ECC" or "ECC_DH".
3196config_psa_crypto_config_accel_ecc_ffdh_no_bignum() {
3197    driver_only="$1"
3198    test_target="$2"
3199    # start with full config for maximum coverage (also enables USE_PSA)
3200    helper_libtestdriver1_adjust_config "full"
3201
3202    if [ "$driver_only" -eq 1 ]; then
3203        # Disable modules that are accelerated
3204        scripts/config.py unset MBEDTLS_ECDSA_C
3205        scripts/config.py unset MBEDTLS_ECDH_C
3206        scripts/config.py unset MBEDTLS_ECJPAKE_C
3207        # Disable ECP module (entirely)
3208        scripts/config.py unset MBEDTLS_ECP_C
3209        # Also disable bignum
3210        scripts/config.py unset MBEDTLS_BIGNUM_C
3211    fi
3212
3213    # Disable all the features that auto-enable ECP_LIGHT (see build_info.h)
3214    scripts/config.py unset MBEDTLS_PK_PARSE_EC_EXTENDED
3215    scripts/config.py unset MBEDTLS_PK_PARSE_EC_COMPRESSED
3216    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE
3217
3218    # RSA support is intentionally disabled on this test because RSA_C depends
3219    # on BIGNUM_C.
3220    scripts/config.py -f "$CRYPTO_CONFIG_H" unset-all "PSA_WANT_KEY_TYPE_RSA_[0-9A-Z_a-z]*"
3221    scripts/config.py -f "$CRYPTO_CONFIG_H" unset-all "PSA_WANT_ALG_RSA_[0-9A-Z_a-z]*"
3222    scripts/config.py unset MBEDTLS_RSA_C
3223    scripts/config.py unset MBEDTLS_PKCS1_V15
3224    scripts/config.py unset MBEDTLS_PKCS1_V21
3225    scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT
3226    # Also disable key exchanges that depend on RSA
3227    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
3228    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
3229    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
3230    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3231    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
3232
3233    if [ "$test_target" = "ECC" ]; then
3234        # When testing ECC only, we disable FFDH support, both from builtin and
3235        # PSA sides, and also disable the key exchanges that depend on DHM.
3236        scripts/config.py -f include/psa/crypto_config.h unset PSA_WANT_ALG_FFDH
3237        scripts/config.py -f "$CRYPTO_CONFIG_H" unset-all "PSA_WANT_KEY_TYPE_DH_[0-9A-Z_a-z]*"
3238        scripts/config.py -f "$CRYPTO_CONFIG_H" unset-all "PSA_WANT_DH_RFC7919_[0-9]*"
3239        scripts/config.py unset MBEDTLS_DHM_C
3240        scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
3241        scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
3242    else
3243        # When testing ECC and DH instead, we disable DHM and depending key
3244        # exchanges only in the accelerated build
3245        if [ "$driver_only" -eq 1 ]; then
3246            scripts/config.py unset MBEDTLS_DHM_C
3247            scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
3248            scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
3249        fi
3250    fi
3251
3252    # Restartable feature is not yet supported by PSA. Once it will in
3253    # the future, the following line could be removed (see issues
3254    # 6061, 6332 and following ones)
3255    scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
3256}
3257
3258# Common helper used by:
3259# - component_test_psa_crypto_config_accel_ecc_no_bignum
3260# - component_test_psa_crypto_config_accel_ecc_ffdh_no_bignum
3261#
3262# The goal is to build and test accelerating either:
3263# - ECC only or
3264# - both ECC and FFDH
3265#
3266# It is meant to be used in conjunction with
3267# common_test_psa_crypto_config_reference_ecc_ffdh_no_bignum() for drivers
3268# coverage analysis in the "analyze_outcomes.py" script.
3269common_test_psa_crypto_config_accel_ecc_ffdh_no_bignum () {
3270    test_target="$1"
3271
3272    # This is an internal helper to simplify text message handling
3273    if [ "$test_target" = "ECC_DH" ]; then
3274        accel_text="ECC/FFDH"
3275        removed_text="ECP - DH"
3276    else
3277        accel_text="ECC"
3278        removed_text="ECP"
3279    fi
3280
3281    msg "build: full + accelerated $accel_text algs + USE_PSA - $removed_text - BIGNUM"
3282
3283    # By default we accelerate all EC keys/algs
3284    loc_accel_list="ALG_ECDSA ALG_DETERMINISTIC_ECDSA \
3285                    ALG_ECDH \
3286                    ALG_JPAKE \
3287                    $(helper_get_psa_key_type_list "ECC") \
3288                    $(helper_get_psa_curve_list)"
3289    # Optionally we can also add DH to the list of accelerated items
3290    if [ "$test_target" = "ECC_DH" ]; then
3291        loc_accel_list="$loc_accel_list \
3292                        ALG_FFDH \
3293                        $(helper_get_psa_key_type_list "DH") \
3294                        $(helper_get_psa_dh_group_list)"
3295    fi
3296
3297    # Configure
3298    # ---------
3299
3300    # Set common configurations between library's and driver's builds
3301    config_psa_crypto_config_accel_ecc_ffdh_no_bignum 1 "$test_target"
3302    # Disable all the builtin curves. All the required algs are accelerated.
3303    helper_disable_builtin_curves
3304
3305    # Build
3306    # -----
3307
3308    # Things we wanted supported in libtestdriver1, but not accelerated in the main library:
3309    # SHA-1 and all SHA-2/3 variants, as they are used by ECDSA deterministic.
3310    loc_extra_list="ALG_SHA_1 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
3311                    ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
3312
3313    helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
3314
3315    helper_libtestdriver1_make_main "$loc_accel_list"
3316
3317    # Make sure any built-in EC alg was not re-enabled by accident (additive config)
3318    not grep mbedtls_ecdsa_ library/ecdsa.o
3319    not grep mbedtls_ecdh_ library/ecdh.o
3320    not grep mbedtls_ecjpake_ library/ecjpake.o
3321    # Also ensure that ECP, RSA, [DHM] or BIGNUM modules were not re-enabled
3322    not grep mbedtls_ecp_ library/ecp.o
3323    not grep mbedtls_rsa_ library/rsa.o
3324    not grep mbedtls_mpi_ library/bignum.o
3325    not grep mbedtls_dhm_ library/dhm.o
3326
3327    # Run the tests
3328    # -------------
3329
3330    msg "test suites: full + accelerated $accel_text algs + USE_PSA - $removed_text - DHM - BIGNUM"
3331
3332    make test
3333
3334    msg "ssl-opt: full + accelerated $accel_text algs + USE_PSA - $removed_text - BIGNUM"
3335    tests/ssl-opt.sh
3336}
3337
3338# Common helper used by:
3339# - component_test_psa_crypto_config_reference_ecc_no_bignum
3340# - component_test_psa_crypto_config_reference_ecc_ffdh_no_bignum
3341#
3342# The goal is to build and test a reference scenario (i.e. with builtin
3343# components) compared to the ones used in
3344# common_test_psa_crypto_config_accel_ecc_ffdh_no_bignum() above.
3345#
3346# It is meant to be used in conjunction with
3347# common_test_psa_crypto_config_accel_ecc_ffdh_no_bignum() for drivers'
3348# coverage analysis in "analyze_outcomes.py" script.
3349common_test_psa_crypto_config_reference_ecc_ffdh_no_bignum () {
3350    test_target="$1"
3351
3352    # This is an internal helper to simplify text message handling
3353    if [ "$test_target" = "ECC_DH" ]; then
3354        accel_text="ECC/FFDH"
3355    else
3356        accel_text="ECC"
3357    fi
3358
3359    msg "build: full + non accelerated $accel_text algs + USE_PSA"
3360
3361    config_psa_crypto_config_accel_ecc_ffdh_no_bignum 0 "$test_target"
3362
3363    make
3364
3365    msg "test suites: full + non accelerated EC algs + USE_PSA"
3366    make test
3367
3368    msg "ssl-opt: full + non accelerated $accel_text algs + USE_PSA"
3369    tests/ssl-opt.sh
3370}
3371
3372component_test_psa_crypto_config_accel_ecc_no_bignum () {
3373    common_test_psa_crypto_config_accel_ecc_ffdh_no_bignum "ECC"
3374}
3375
3376component_test_psa_crypto_config_reference_ecc_no_bignum () {
3377    common_test_psa_crypto_config_reference_ecc_ffdh_no_bignum "ECC"
3378}
3379
3380component_test_psa_crypto_config_accel_ecc_ffdh_no_bignum () {
3381    common_test_psa_crypto_config_accel_ecc_ffdh_no_bignum "ECC_DH"
3382}
3383
3384component_test_psa_crypto_config_reference_ecc_ffdh_no_bignum () {
3385    common_test_psa_crypto_config_reference_ecc_ffdh_no_bignum "ECC_DH"
3386}
3387
3388# Helper for setting common configurations between:
3389# - component_test_tfm_config_p256m_driver_accel_ec()
3390# - component_test_tfm_config()
3391common_tfm_config () {
3392    # Enable TF-M config
3393    cp configs/config-tfm.h "$CONFIG_H"
3394    echo "#undef MBEDTLS_PSA_CRYPTO_CONFIG_FILE" >> "$CONFIG_H"
3395    cp configs/ext/crypto_config_profile_medium.h "$CRYPTO_CONFIG_H"
3396
3397    # Other config adjustment to make the tests pass.
3398    # This should probably be adopted upstream.
3399    #
3400    # - USE_PSA_CRYPTO for PK_HAVE_ECC_KEYS
3401    echo "#define MBEDTLS_USE_PSA_CRYPTO" >> "$CONFIG_H"
3402
3403    # Config adjustment for better test coverage in our environment.
3404    # This is not needed just to build and pass tests.
3405    #
3406    # Enable filesystem I/O for the benefit of PK parse/write tests.
3407    echo "#define MBEDTLS_FS_IO" >> "$CONFIG_H"
3408}
3409
3410# Keep this in sync with component_test_tfm_config() as they are both meant
3411# to be used in analyze_outcomes.py for driver's coverage analysis.
3412component_test_tfm_config_p256m_driver_accel_ec () {
3413    msg "build: TF-M config + p256m driver + accel ECDH(E)/ECDSA"
3414
3415    common_tfm_config
3416
3417    # Build crypto library
3418    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -I../tests/include/spe" LDFLAGS="$ASAN_CFLAGS"
3419
3420    # Make sure any built-in EC alg was not re-enabled by accident (additive config)
3421    not grep mbedtls_ecdsa_ library/ecdsa.o
3422    not grep mbedtls_ecdh_ library/ecdh.o
3423    not grep mbedtls_ecjpake_ library/ecjpake.o
3424    # Also ensure that ECP, RSA, DHM or BIGNUM modules were not re-enabled
3425    not grep mbedtls_ecp_ library/ecp.o
3426    not grep mbedtls_rsa_ library/rsa.o
3427    not grep mbedtls_dhm_ library/dhm.o
3428    not grep mbedtls_mpi_ library/bignum.o
3429    # Check that p256m was built
3430    grep -q p256_ecdsa_ library/libmbedcrypto.a
3431
3432    # In "config-tfm.h" we disabled CIPHER_C tweaking TF-M's configuration
3433    # files, so we want to ensure that it has not be re-enabled accidentally.
3434    not grep mbedtls_cipher library/cipher.o
3435
3436    # Run the tests
3437    msg "test: TF-M config + p256m driver + accel ECDH(E)/ECDSA"
3438    make test
3439}
3440
3441# Keep this in sync with component_test_tfm_config_p256m_driver_accel_ec() as
3442# they are both meant to be used in analyze_outcomes.py for driver's coverage
3443# analysis.
3444component_test_tfm_config() {
3445    common_tfm_config
3446
3447    # Disable P256M driver, which is on by default, so that analyze_outcomes
3448    # can compare this test with test_tfm_config_p256m_driver_accel_ec
3449    echo "#undef MBEDTLS_PSA_P256M_DRIVER_ENABLED" >> "$CONFIG_H"
3450
3451    msg "build: TF-M config"
3452    make CFLAGS='-Werror -Wall -Wextra -I../tests/include/spe' tests
3453
3454    # Check that p256m was not built
3455    not grep p256_ecdsa_ library/libmbedcrypto.a
3456
3457    # In "config-tfm.h" we disabled CIPHER_C tweaking TF-M's configuration
3458    # files, so we want to ensure that it has not be re-enabled accidentally.
3459    not grep mbedtls_cipher library/cipher.o
3460
3461    msg "test: TF-M config"
3462    make test
3463}
3464
3465# Common helper for component_full_without_ecdhe_ecdsa() and
3466# component_full_without_ecdhe_ecdsa_and_tls13() which:
3467# - starts from the "full" configuration minus the list of symbols passed in
3468#   as 1st parameter
3469# - build
3470# - test only TLS (i.e. test_suite_tls and ssl-opt)
3471build_full_minus_something_and_test_tls () {
3472    symbols_to_disable="$1"
3473
3474    msg "build: full minus something, test TLS"
3475
3476    scripts/config.py full
3477    for sym in $symbols_to_disable; do
3478        echo "Disabling $sym"
3479        scripts/config.py unset $sym
3480    done
3481
3482    make
3483
3484    msg "test: full minus something, test TLS"
3485    ( cd tests; ./test_suite_ssl )
3486
3487    msg "ssl-opt: full minus something, test TLS"
3488    tests/ssl-opt.sh
3489}
3490
3491component_full_without_ecdhe_ecdsa () {
3492    build_full_minus_something_and_test_tls "MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED"
3493}
3494
3495component_full_without_ecdhe_ecdsa_and_tls13 () {
3496    build_full_minus_something_and_test_tls "MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3497                                             MBEDTLS_SSL_PROTO_TLS1_3"
3498}
3499
3500# This is an helper used by:
3501# - component_test_psa_ecc_key_pair_no_derive
3502# - component_test_psa_ecc_key_pair_no_generate
3503# The goal is to test with all PSA_WANT_KEY_TYPE_xxx_KEY_PAIR_yyy symbols
3504# enabled, but one. Input arguments are as follows:
3505# - $1 is the key type under test, i.e. ECC/RSA/DH
3506# - $2 is the key option to be unset (i.e. generate, derive, etc)
3507build_and_test_psa_want_key_pair_partial() {
3508    key_type=$1
3509    unset_option=$2
3510    disabled_psa_want="PSA_WANT_KEY_TYPE_${key_type}_KEY_PAIR_${unset_option}"
3511
3512    msg "build: full - MBEDTLS_USE_PSA_CRYPTO - ${disabled_psa_want}"
3513    scripts/config.py full
3514    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
3515    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
3516
3517    # All the PSA_WANT_KEY_TYPE_xxx_KEY_PAIR_yyy are enabled by default in
3518    # crypto_config.h so we just disable the one we don't want.
3519    scripts/config.py -f "$CRYPTO_CONFIG_H" unset "$disabled_psa_want"
3520
3521    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
3522
3523    msg "test: full - MBEDTLS_USE_PSA_CRYPTO - ${disabled_psa_want}"
3524    make test
3525}
3526
3527component_test_psa_ecc_key_pair_no_derive() {
3528    build_and_test_psa_want_key_pair_partial "ECC" "DERIVE"
3529}
3530
3531component_test_psa_ecc_key_pair_no_generate() {
3532    build_and_test_psa_want_key_pair_partial "ECC" "GENERATE"
3533}
3534
3535config_psa_crypto_accel_rsa () {
3536    driver_only=$1
3537
3538    # Start from crypto_full config (no X.509, no TLS)
3539    helper_libtestdriver1_adjust_config "crypto_full"
3540
3541    if [ "$driver_only" -eq 1 ]; then
3542        # Remove RSA support and its dependencies
3543        scripts/config.py unset MBEDTLS_RSA_C
3544        scripts/config.py unset MBEDTLS_PKCS1_V15
3545        scripts/config.py unset MBEDTLS_PKCS1_V21
3546
3547        # We need PEM parsing in the test library as well to support the import
3548        # of PEM encoded RSA keys.
3549        scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_PEM_PARSE_C
3550        scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_BASE64_C
3551    fi
3552}
3553
3554component_test_psa_crypto_config_accel_rsa_crypto () {
3555    msg "build: crypto_full with accelerated RSA"
3556
3557    loc_accel_list="ALG_RSA_OAEP ALG_RSA_PSS \
3558                    ALG_RSA_PKCS1V15_CRYPT ALG_RSA_PKCS1V15_SIGN \
3559                    KEY_TYPE_RSA_PUBLIC_KEY \
3560                    KEY_TYPE_RSA_KEY_PAIR_BASIC \
3561                    KEY_TYPE_RSA_KEY_PAIR_GENERATE \
3562                    KEY_TYPE_RSA_KEY_PAIR_IMPORT \
3563                    KEY_TYPE_RSA_KEY_PAIR_EXPORT"
3564
3565    # Configure
3566    # ---------
3567
3568    config_psa_crypto_accel_rsa 1
3569
3570    # Build
3571    # -----
3572
3573    # These hashes are needed for unit tests.
3574    loc_extra_list="ALG_SHA_1 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
3575                    ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512 ALG_MD5"
3576    helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
3577
3578    helper_libtestdriver1_make_main "$loc_accel_list"
3579
3580    # Make sure this was not re-enabled by accident (additive config)
3581    not grep mbedtls_rsa library/rsa.o
3582
3583    # Run the tests
3584    # -------------
3585
3586    msg "test: crypto_full with accelerated RSA"
3587    make test
3588}
3589
3590component_test_psa_crypto_config_reference_rsa_crypto () {
3591    msg "build: crypto_full with non-accelerated RSA"
3592
3593    # Configure
3594    # ---------
3595    config_psa_crypto_accel_rsa 0
3596
3597    # Build
3598    # -----
3599    make
3600
3601    # Run the tests
3602    # -------------
3603    msg "test: crypto_full with non-accelerated RSA"
3604    make test
3605}
3606
3607# This is a temporary test to verify that full RSA support is present even when
3608# only one single new symbols (PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) is defined.
3609component_test_new_psa_want_key_pair_symbol() {
3610    msg "Build: crypto config - MBEDTLS_RSA_C + PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC"
3611
3612    # Create a temporary output file unless there is already one set
3613    if [ "$MBEDTLS_TEST_OUTCOME_FILE" ]; then
3614        REMOVE_OUTCOME_ON_EXIT="no"
3615    else
3616        REMOVE_OUTCOME_ON_EXIT="yes"
3617        MBEDTLS_TEST_OUTCOME_FILE="$PWD/out.csv"
3618        export MBEDTLS_TEST_OUTCOME_FILE
3619    fi
3620
3621    # Start from crypto configuration
3622    scripts/config.py crypto
3623
3624    # Remove RSA support and its dependencies
3625    scripts/config.py unset MBEDTLS_PKCS1_V15
3626    scripts/config.py unset MBEDTLS_PKCS1_V21
3627    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
3628    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
3629    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3630    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
3631    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
3632    scripts/config.py unset MBEDTLS_RSA_C
3633    scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT
3634
3635    # Enable PSA support
3636    scripts/config.py set MBEDTLS_PSA_CRYPTO_CONFIG
3637
3638    # Keep only PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC enabled in order to ensure
3639    # that proper translations is done in crypto_legacy.h.
3640    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_IMPORT
3641    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_EXPORT
3642    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE
3643
3644    make
3645
3646    msg "Test: crypto config - MBEDTLS_RSA_C + PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC"
3647    make test
3648
3649    # Parse only 1 relevant line from the outcome file, i.e. a test which is
3650    # performing RSA signature.
3651    msg "Verify that 'RSA PKCS1 Sign #1 (SHA512, 1536 bits RSA)' is PASS"
3652    cat $MBEDTLS_TEST_OUTCOME_FILE | grep 'RSA PKCS1 Sign #1 (SHA512, 1536 bits RSA)' | grep -q "PASS"
3653
3654    if [ "$REMOVE_OUTCOME_ON_EXIT" == "yes" ]; then
3655        rm $MBEDTLS_TEST_OUTCOME_FILE
3656    fi
3657}
3658
3659component_test_psa_crypto_config_accel_hash () {
3660    msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated hash"
3661
3662    loc_accel_list="ALG_MD5 ALG_RIPEMD160 ALG_SHA_1 \
3663                    ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
3664                    ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
3665
3666    # Configure
3667    # ---------
3668
3669    # Start from default config (no USE_PSA)
3670    helper_libtestdriver1_adjust_config "default"
3671
3672    # Disable the things that are being accelerated
3673    scripts/config.py unset MBEDTLS_MD5_C
3674    scripts/config.py unset MBEDTLS_RIPEMD160_C
3675    scripts/config.py unset MBEDTLS_SHA1_C
3676    scripts/config.py unset MBEDTLS_SHA224_C
3677    scripts/config.py unset MBEDTLS_SHA256_C
3678    scripts/config.py unset MBEDTLS_SHA384_C
3679    scripts/config.py unset MBEDTLS_SHA512_C
3680    scripts/config.py unset MBEDTLS_SHA3_C
3681
3682    # Build
3683    # -----
3684
3685    helper_libtestdriver1_make_drivers "$loc_accel_list"
3686
3687    helper_libtestdriver1_make_main "$loc_accel_list"
3688
3689    # There's a risk of something getting re-enabled via config_psa.h;
3690    # make sure it did not happen. Note: it's OK for MD_C to be enabled.
3691    not grep mbedtls_md5 library/md5.o
3692    not grep mbedtls_sha1 library/sha1.o
3693    not grep mbedtls_sha256 library/sha256.o
3694    not grep mbedtls_sha512 library/sha512.o
3695    not grep mbedtls_ripemd160 library/ripemd160.o
3696
3697    # Run the tests
3698    # -------------
3699
3700    msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated hash"
3701    make test
3702}
3703
3704component_test_psa_crypto_config_accel_hash_keep_builtins () {
3705    msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated+builtin hash"
3706    # This component ensures that all the test cases for
3707    # md_psa_dynamic_dispatch with legacy+driver in test_suite_md are run.
3708
3709    loc_accel_list="ALG_MD5 ALG_RIPEMD160 ALG_SHA_1 \
3710                    ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
3711                    ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
3712
3713    # Start from default config (no USE_PSA)
3714    helper_libtestdriver1_adjust_config "default"
3715
3716    helper_libtestdriver1_make_drivers "$loc_accel_list"
3717
3718    helper_libtestdriver1_make_main "$loc_accel_list"
3719
3720    msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated+builtin hash"
3721    make test
3722}
3723
3724# Auxiliary function to build config for hashes with and without drivers
3725config_psa_crypto_hash_use_psa () {
3726    driver_only="$1"
3727    # start with config full for maximum coverage (also enables USE_PSA)
3728    helper_libtestdriver1_adjust_config "full"
3729    if [ "$driver_only" -eq 1 ]; then
3730        # disable the built-in implementation of hashes
3731        scripts/config.py unset MBEDTLS_MD5_C
3732        scripts/config.py unset MBEDTLS_RIPEMD160_C
3733        scripts/config.py unset MBEDTLS_SHA1_C
3734        scripts/config.py unset MBEDTLS_SHA224_C
3735        scripts/config.py unset MBEDTLS_SHA256_C # see external RNG below
3736        scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
3737        scripts/config.py unset MBEDTLS_SHA384_C
3738        scripts/config.py unset MBEDTLS_SHA512_C
3739        scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
3740        scripts/config.py unset MBEDTLS_SHA3_C
3741    fi
3742}
3743
3744# Note that component_test_psa_crypto_config_reference_hash_use_psa
3745# is related to this component and both components need to be kept in sync.
3746# For details please see comments for component_test_psa_crypto_config_reference_hash_use_psa.
3747component_test_psa_crypto_config_accel_hash_use_psa () {
3748    msg "test: full with accelerated hashes"
3749
3750    loc_accel_list="ALG_MD5 ALG_RIPEMD160 ALG_SHA_1 \
3751                    ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
3752                    ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
3753
3754    # Configure
3755    # ---------
3756
3757    config_psa_crypto_hash_use_psa 1
3758
3759    # Build
3760    # -----
3761
3762    helper_libtestdriver1_make_drivers "$loc_accel_list"
3763
3764    helper_libtestdriver1_make_main "$loc_accel_list"
3765
3766    # There's a risk of something getting re-enabled via config_psa.h;
3767    # make sure it did not happen. Note: it's OK for MD_C to be enabled.
3768    not grep mbedtls_md5 library/md5.o
3769    not grep mbedtls_sha1 library/sha1.o
3770    not grep mbedtls_sha256 library/sha256.o
3771    not grep mbedtls_sha512 library/sha512.o
3772    not grep mbedtls_ripemd160 library/ripemd160.o
3773
3774    # Run the tests
3775    # -------------
3776
3777    msg "test: full with accelerated hashes"
3778    make test
3779
3780    # This is mostly useful so that we can later compare outcome files with
3781    # the reference config in analyze_outcomes.py, to check that the
3782    # dependency declarations in ssl-opt.sh and in TLS code are correct.
3783    msg "test: ssl-opt.sh, full with accelerated hashes"
3784    tests/ssl-opt.sh
3785
3786    # This is to make sure all ciphersuites are exercised, but we don't need
3787    # interop testing (besides, we already got some from ssl-opt.sh).
3788    msg "test: compat.sh, full with accelerated hashes"
3789    tests/compat.sh -p mbedTLS -V YES
3790}
3791
3792# This component provides reference configuration for test_psa_crypto_config_accel_hash_use_psa
3793# without accelerated hash. The outcome from both components are used by the analyze_outcomes.py
3794# script to find regression in test coverage when accelerated hash is used (tests and ssl-opt).
3795# Both components need to be kept in sync.
3796component_test_psa_crypto_config_reference_hash_use_psa() {
3797    msg "test: full without accelerated hashes"
3798
3799    config_psa_crypto_hash_use_psa 0
3800
3801    make
3802
3803    msg "test: full without accelerated hashes"
3804    make test
3805
3806    msg "test: ssl-opt.sh, full without accelerated hashes"
3807    tests/ssl-opt.sh
3808}
3809
3810# Auxiliary function to build config for hashes with and without drivers
3811config_psa_crypto_hmac_use_psa () {
3812    driver_only="$1"
3813    # start with config full for maximum coverage (also enables USE_PSA)
3814    helper_libtestdriver1_adjust_config "full"
3815
3816    if [ "$driver_only" -eq 1 ]; then
3817        # Disable MD_C in order to disable the builtin support for HMAC. MD_LIGHT
3818        # is still enabled though (for ENTROPY_C among others).
3819        scripts/config.py unset MBEDTLS_MD_C
3820        # Disable also the builtin hashes since they are supported by the driver
3821        # and MD module is able to perform PSA dispathing.
3822        scripts/config.py unset-all MBEDTLS_SHA
3823        scripts/config.py unset MBEDTLS_MD5_C
3824        scripts/config.py unset MBEDTLS_RIPEMD160_C
3825    fi
3826
3827    # Direct dependencies of MD_C. We disable them also in the reference
3828    # component to work with the same set of features.
3829    scripts/config.py unset MBEDTLS_PKCS7_C
3830    scripts/config.py unset MBEDTLS_PKCS5_C
3831    scripts/config.py unset MBEDTLS_HMAC_DRBG_C
3832    scripts/config.py unset MBEDTLS_HKDF_C
3833    # Dependencies of HMAC_DRBG
3834    scripts/config.py unset MBEDTLS_ECDSA_DETERMINISTIC
3835    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_DETERMINISTIC_ECDSA
3836}
3837
3838component_test_psa_crypto_config_accel_hmac() {
3839    msg "test: full with accelerated hmac"
3840
3841    loc_accel_list="ALG_HMAC KEY_TYPE_HMAC \
3842                    ALG_MD5 ALG_RIPEMD160 ALG_SHA_1 \
3843                    ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
3844                    ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
3845
3846    # Configure
3847    # ---------
3848
3849    config_psa_crypto_hmac_use_psa 1
3850
3851    # Build
3852    # -----
3853
3854    helper_libtestdriver1_make_drivers "$loc_accel_list"
3855
3856    helper_libtestdriver1_make_main "$loc_accel_list"
3857
3858    # Ensure that built-in support for HMAC is disabled.
3859    not grep mbedtls_md_hmac library/md.o
3860
3861    # Run the tests
3862    # -------------
3863
3864    msg "test: full with accelerated hmac"
3865    make test
3866}
3867
3868component_test_psa_crypto_config_reference_hmac() {
3869    msg "test: full without accelerated hmac"
3870
3871    config_psa_crypto_hmac_use_psa 0
3872
3873    make
3874
3875    msg "test: full without accelerated hmac"
3876    make test
3877}
3878
3879component_test_psa_crypto_config_accel_des () {
3880    msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated DES"
3881
3882    # Albeit this components aims at accelerating DES which should only support
3883    # CBC and ECB modes, we need to accelerate more than that otherwise DES_C
3884    # would automatically be re-enabled by "config_adjust_legacy_from_psa.c"
3885    loc_accel_list="ALG_ECB_NO_PADDING ALG_CBC_NO_PADDING ALG_CBC_PKCS7 \
3886                    ALG_CTR ALG_CFB ALG_OFB ALG_XTS ALG_CMAC \
3887                    KEY_TYPE_DES"
3888
3889    # Note: we cannot accelerate all ciphers' key types otherwise we would also
3890    # have to either disable CCM/GCM or accelerate them, but that's out of scope
3891    # of this component. This limitation will be addressed by #8598.
3892
3893    # Configure
3894    # ---------
3895
3896    # Start from the full config
3897    helper_libtestdriver1_adjust_config "full"
3898
3899    # Disable the things that are being accelerated
3900    scripts/config.py unset MBEDTLS_CIPHER_MODE_CBC
3901    scripts/config.py unset MBEDTLS_CIPHER_PADDING_PKCS7
3902    scripts/config.py unset MBEDTLS_CIPHER_MODE_CTR
3903    scripts/config.py unset MBEDTLS_CIPHER_MODE_CFB
3904    scripts/config.py unset MBEDTLS_CIPHER_MODE_OFB
3905    scripts/config.py unset MBEDTLS_CIPHER_MODE_XTS
3906    scripts/config.py unset MBEDTLS_DES_C
3907    scripts/config.py unset MBEDTLS_CMAC_C
3908
3909    # Build
3910    # -----
3911
3912    helper_libtestdriver1_make_drivers "$loc_accel_list"
3913
3914    helper_libtestdriver1_make_main "$loc_accel_list"
3915
3916    # Make sure this was not re-enabled by accident (additive config)
3917    not grep mbedtls_des* library/des.o
3918
3919    # Run the tests
3920    # -------------
3921
3922    msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated DES"
3923    make test
3924}
3925
3926component_test_psa_crypto_config_accel_aead () {
3927    msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated AEAD"
3928
3929    loc_accel_list="ALG_GCM ALG_CCM ALG_CHACHA20_POLY1305 \
3930                    KEY_TYPE_AES KEY_TYPE_CHACHA20 KEY_TYPE_ARIA KEY_TYPE_CAMELLIA"
3931
3932    # Configure
3933    # ---------
3934
3935    # Start from full config
3936    helper_libtestdriver1_adjust_config "full"
3937
3938    # Disable things that are being accelerated
3939    scripts/config.py unset MBEDTLS_GCM_C
3940    scripts/config.py unset MBEDTLS_CCM_C
3941    scripts/config.py unset MBEDTLS_CHACHAPOLY_C
3942
3943    # Disable CCM_STAR_NO_TAG because this re-enables CCM_C.
3944    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CCM_STAR_NO_TAG
3945
3946    # Build
3947    # -----
3948
3949    helper_libtestdriver1_make_drivers "$loc_accel_list"
3950
3951    helper_libtestdriver1_make_main "$loc_accel_list"
3952
3953    # Make sure this was not re-enabled by accident (additive config)
3954    not grep mbedtls_ccm library/ccm.o
3955    not grep mbedtls_gcm library/gcm.o
3956    not grep mbedtls_chachapoly library/chachapoly.o
3957
3958    # Run the tests
3959    # -------------
3960
3961    msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated AEAD"
3962    make test
3963}
3964
3965# This is a common configuration function used in:
3966# - component_test_psa_crypto_config_accel_cipher_aead_cmac
3967# - component_test_psa_crypto_config_reference_cipher_aead_cmac
3968common_psa_crypto_config_accel_cipher_aead_cmac() {
3969    # Start from the full config
3970    helper_libtestdriver1_adjust_config "full"
3971
3972    scripts/config.py unset MBEDTLS_NIST_KW_C
3973}
3974
3975# The 2 following test components, i.e.
3976# - component_test_psa_crypto_config_accel_cipher_aead_cmac
3977# - component_test_psa_crypto_config_reference_cipher_aead_cmac
3978# are meant to be used together in analyze_outcomes.py script in order to test
3979# driver's coverage for ciphers and AEADs.
3980component_test_psa_crypto_config_accel_cipher_aead_cmac () {
3981    msg "build: full config with accelerated cipher inc. AEAD and CMAC"
3982
3983    loc_accel_list="ALG_ECB_NO_PADDING ALG_CBC_NO_PADDING ALG_CBC_PKCS7 ALG_CTR ALG_CFB \
3984                    ALG_OFB ALG_XTS ALG_STREAM_CIPHER ALG_CCM_STAR_NO_TAG \
3985                    ALG_GCM ALG_CCM ALG_CHACHA20_POLY1305 ALG_CMAC \
3986                    KEY_TYPE_DES KEY_TYPE_AES KEY_TYPE_ARIA KEY_TYPE_CHACHA20 KEY_TYPE_CAMELLIA"
3987
3988    # Configure
3989    # ---------
3990
3991    common_psa_crypto_config_accel_cipher_aead_cmac
3992
3993    # Disable the things that are being accelerated
3994    scripts/config.py unset MBEDTLS_CIPHER_MODE_CBC
3995    scripts/config.py unset MBEDTLS_CIPHER_PADDING_PKCS7
3996    scripts/config.py unset MBEDTLS_CIPHER_MODE_CTR
3997    scripts/config.py unset MBEDTLS_CIPHER_MODE_CFB
3998    scripts/config.py unset MBEDTLS_CIPHER_MODE_OFB
3999    scripts/config.py unset MBEDTLS_CIPHER_MODE_XTS
4000    scripts/config.py unset MBEDTLS_GCM_C
4001    scripts/config.py unset MBEDTLS_CCM_C
4002    scripts/config.py unset MBEDTLS_CHACHAPOLY_C
4003    scripts/config.py unset MBEDTLS_CMAC_C
4004    scripts/config.py unset MBEDTLS_DES_C
4005    scripts/config.py unset MBEDTLS_AES_C
4006    scripts/config.py unset MBEDTLS_ARIA_C
4007    scripts/config.py unset MBEDTLS_CHACHA20_C
4008    scripts/config.py unset MBEDTLS_CAMELLIA_C
4009
4010    # Disable CIPHER_C entirely as all ciphers/AEADs are accelerated and PSA
4011    # does not depend on it.
4012    scripts/config.py unset MBEDTLS_CIPHER_C
4013
4014    # Build
4015    # -----
4016
4017    helper_libtestdriver1_make_drivers "$loc_accel_list"
4018
4019    helper_libtestdriver1_make_main "$loc_accel_list"
4020
4021    # Make sure this was not re-enabled by accident (additive config)
4022    not grep mbedtls_cipher library/cipher.o
4023    not grep mbedtls_des library/des.o
4024    not grep mbedtls_aes library/aes.o
4025    not grep mbedtls_aria library/aria.o
4026    not grep mbedtls_camellia library/camellia.o
4027    not grep mbedtls_ccm library/ccm.o
4028    not grep mbedtls_gcm library/gcm.o
4029    not grep mbedtls_chachapoly library/chachapoly.o
4030    not grep mbedtls_cmac library/cmac.o
4031
4032    # Run the tests
4033    # -------------
4034
4035    msg "test: full config with accelerated cipher inc. AEAD and CMAC"
4036    make test
4037
4038    msg "ssl-opt: full config with accelerated cipher inc. AEAD and CMAC"
4039    tests/ssl-opt.sh
4040
4041    msg "compat.sh: full config with accelerated cipher inc. AEAD and CMAC"
4042    tests/compat.sh -V NO -p mbedTLS
4043}
4044
4045component_test_psa_crypto_config_reference_cipher_aead_cmac () {
4046    msg "build: full config with non-accelerated cipher inc. AEAD and CMAC"
4047    common_psa_crypto_config_accel_cipher_aead_cmac
4048
4049    make
4050
4051    msg "test: full config with non-accelerated cipher inc. AEAD and CMAC"
4052    make test
4053
4054    msg "ssl-opt: full config with non-accelerated cipher inc. AEAD and CMAC"
4055    tests/ssl-opt.sh
4056
4057    msg "compat.sh: full config with non-accelerated cipher inc. AEAD and CMAC"
4058    tests/compat.sh -V NO -p mbedTLS
4059}
4060
4061common_block_cipher_dispatch() {
4062    TEST_WITH_DRIVER="$1"
4063
4064    # Start from the full config
4065    helper_libtestdriver1_adjust_config "full"
4066
4067    if [ "$TEST_WITH_DRIVER" -eq 1 ]; then
4068        # Disable key types that are accelerated (there is no legacy equivalent
4069        # symbol for ECB)
4070        scripts/config.py unset MBEDTLS_AES_C
4071        scripts/config.py unset MBEDTLS_ARIA_C
4072        scripts/config.py unset MBEDTLS_CAMELLIA_C
4073    fi
4074
4075    # Disable cipher's modes that, when not accelerated, cause
4076    # legacy key types to be re-enabled in "config_adjust_legacy_from_psa.h".
4077    # Keep this also in the reference component in order to skip the same tests
4078    # that were skipped in the accelerated one.
4079    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CTR
4080    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CFB
4081    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_OFB
4082    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CBC_NO_PADDING
4083    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CBC_PKCS7
4084    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CMAC
4085    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CCM_STAR_NO_TAG
4086
4087    # Disable direct dependency on AES_C
4088    scripts/config.py unset MBEDTLS_NIST_KW_C
4089
4090    # Prevent the cipher module from using deprecated PSA path. The reason is
4091    # that otherwise there will be tests relying on "aes_info" (defined in
4092    # "cipher_wrap.c") whose functions are not available when AES_C is
4093    # not defined. ARIA and Camellia are not a problem in this case because
4094    # the PSA path is not tested for these key types.
4095    scripts/config.py set MBEDTLS_DEPRECATED_REMOVED
4096}
4097
4098component_test_full_block_cipher_psa_dispatch () {
4099    msg "build: full + PSA dispatch in block_cipher"
4100
4101    loc_accel_list="ALG_ECB_NO_PADDING \
4102                    KEY_TYPE_AES KEY_TYPE_ARIA KEY_TYPE_CAMELLIA"
4103
4104    # Configure
4105    # ---------
4106
4107    common_block_cipher_dispatch 1
4108
4109    # Build
4110    # -----
4111
4112    helper_libtestdriver1_make_drivers "$loc_accel_list"
4113
4114    helper_libtestdriver1_make_main "$loc_accel_list"
4115
4116    # Make sure disabled components were not re-enabled by accident (additive
4117    # config)
4118    not grep mbedtls_aes_ library/aes.o
4119    not grep mbedtls_aria_ library/aria.o
4120    not grep mbedtls_camellia_ library/camellia.o
4121
4122    # Run the tests
4123    # -------------
4124
4125    msg "test: full + PSA dispatch in block_cipher"
4126    make test
4127}
4128
4129# This is the reference component of component_test_full_block_cipher_psa_dispatch
4130component_test_full_block_cipher_legacy_dispatch () {
4131    msg "build: full + legacy dispatch in block_cipher"
4132
4133    common_block_cipher_dispatch 0
4134
4135    make
4136
4137    msg "test: full + legacy dispatch in block_cipher"
4138    make test
4139}
4140
4141component_test_aead_chachapoly_disabled() {
4142    msg "build: full minus CHACHAPOLY"
4143    scripts/config.py full
4144    scripts/config.py unset MBEDTLS_CHACHAPOLY_C
4145    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CHACHA20_POLY1305
4146    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
4147
4148    msg "test: full minus CHACHAPOLY"
4149    make test
4150}
4151
4152component_test_aead_only_ccm() {
4153    msg "build: full minus CHACHAPOLY and GCM"
4154    scripts/config.py full
4155    scripts/config.py unset MBEDTLS_CHACHAPOLY_C
4156    scripts/config.py unset MBEDTLS_GCM_C
4157    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CHACHA20_POLY1305
4158    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_GCM
4159    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
4160
4161    msg "test: full minus CHACHAPOLY and GCM"
4162    make test
4163}
4164
4165component_test_ccm_aes_sha256() {
4166    msg "build: CCM + AES + SHA256 configuration"
4167
4168    cp "$CONFIG_TEST_DRIVER_H" "$CONFIG_H"
4169    cp configs/crypto-config-ccm-aes-sha256.h "$CRYPTO_CONFIG_H"
4170
4171    make
4172
4173    msg "test: CCM + AES + SHA256 configuration"
4174    make test
4175}
4176
4177# This should be renamed to test and updated once the accelerator ECDH code is in place and ready to test.
4178component_build_psa_accel_alg_ecdh() {
4179    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_ECDH without MBEDTLS_ECDH_C"
4180    scripts/config.py full
4181    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4182    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4183    scripts/config.py unset MBEDTLS_ECDH_C
4184    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
4185    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
4186    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4187    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4188    scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED
4189    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4190    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_ECDH -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4191}
4192
4193# This should be renamed to test and updated once the accelerator HMAC code is in place and ready to test.
4194component_build_psa_accel_alg_hmac() {
4195    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_HMAC"
4196    scripts/config.py full
4197    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4198    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4199    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4200    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_HMAC -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4201}
4202
4203# This should be renamed to test and updated once the accelerator HKDF code is in place and ready to test.
4204component_build_psa_accel_alg_hkdf() {
4205    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_HKDF without MBEDTLS_HKDF_C"
4206    scripts/config.py full
4207    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4208    scripts/config.py unset MBEDTLS_HKDF_C
4209    # Make sure to unset TLS1_3 since it requires HKDF_C and will not build properly without it.
4210    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4211    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4212    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_HKDF -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4213}
4214
4215# This should be renamed to test and updated once the accelerator MD5 code is in place and ready to test.
4216component_build_psa_accel_alg_md5() {
4217    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_MD5 - other hashes"
4218    scripts/config.py full
4219    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4220    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4221    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RIPEMD160
4222    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_1
4223    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_224
4224    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_256
4225    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_384
4226    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_512
4227    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS
4228    scripts/config.py unset MBEDTLS_LMS_C
4229    scripts/config.py unset MBEDTLS_LMS_PRIVATE
4230    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4231    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_MD5 -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4232}
4233
4234# This should be renamed to test and updated once the accelerator RIPEMD160 code is in place and ready to test.
4235component_build_psa_accel_alg_ripemd160() {
4236    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_RIPEMD160 - other hashes"
4237    scripts/config.py full
4238    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4239    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4240    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_MD5
4241    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_1
4242    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_224
4243    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_256
4244    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_384
4245    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_512
4246    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS
4247    scripts/config.py unset MBEDTLS_LMS_C
4248    scripts/config.py unset MBEDTLS_LMS_PRIVATE
4249    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4250    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_RIPEMD160 -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4251}
4252
4253# This should be renamed to test and updated once the accelerator SHA1 code is in place and ready to test.
4254component_build_psa_accel_alg_sha1() {
4255    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_SHA_1 - other hashes"
4256    scripts/config.py full
4257    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4258    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4259    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_MD5
4260    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RIPEMD160
4261    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_224
4262    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_256
4263    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_384
4264    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_512
4265    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS
4266    scripts/config.py unset MBEDTLS_LMS_C
4267    scripts/config.py unset MBEDTLS_LMS_PRIVATE
4268    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4269    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_SHA_1 -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4270}
4271
4272# This should be renamed to test and updated once the accelerator SHA224 code is in place and ready to test.
4273component_build_psa_accel_alg_sha224() {
4274    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_SHA_224 - other hashes"
4275    scripts/config.py full
4276    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4277    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4278    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_MD5
4279    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RIPEMD160
4280    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_1
4281    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_384
4282    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_512
4283    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS
4284    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4285    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_SHA_224 -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4286}
4287
4288# This should be renamed to test and updated once the accelerator SHA256 code is in place and ready to test.
4289component_build_psa_accel_alg_sha256() {
4290    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_SHA_256 - other hashes"
4291    scripts/config.py full
4292    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4293    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4294    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_MD5
4295    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RIPEMD160
4296    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_1
4297    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_224
4298    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_384
4299    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_512
4300    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4301    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_SHA_256 -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4302}
4303
4304# This should be renamed to test and updated once the accelerator SHA384 code is in place and ready to test.
4305component_build_psa_accel_alg_sha384() {
4306    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_SHA_384 - other hashes"
4307    scripts/config.py full
4308    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4309    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4310    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_MD5
4311    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RIPEMD160
4312    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_1
4313    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_224
4314    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_256
4315    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS
4316    scripts/config.py unset MBEDTLS_LMS_C
4317    scripts/config.py unset MBEDTLS_LMS_PRIVATE
4318    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4319    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_SHA_384 -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4320}
4321
4322# This should be renamed to test and updated once the accelerator SHA512 code is in place and ready to test.
4323component_build_psa_accel_alg_sha512() {
4324    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_SHA_512 - other hashes"
4325    scripts/config.py full
4326    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4327    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4328    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_MD5
4329    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RIPEMD160
4330    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_1
4331    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_224
4332    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_256
4333    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_SHA_384
4334    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS
4335    scripts/config.py unset MBEDTLS_LMS_C
4336    scripts/config.py unset MBEDTLS_LMS_PRIVATE
4337    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4338    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_SHA_512 -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4339}
4340
4341# This should be renamed to test and updated once the accelerator RSA code is in place and ready to test.
4342component_build_psa_accel_alg_rsa_pkcs1v15_crypt() {
4343    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_RSA_PKCS1V15_CRYPT + PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY"
4344    scripts/config.py full
4345    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4346    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4347    scripts/config.py -f "$CRYPTO_CONFIG_H" set PSA_WANT_ALG_RSA_PKCS1V15_CRYPT 1
4348    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RSA_PKCS1V15_SIGN
4349    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RSA_OAEP
4350    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RSA_PSS
4351    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4352    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_CRYPT -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4353}
4354
4355# This should be renamed to test and updated once the accelerator RSA code is in place and ready to test.
4356component_build_psa_accel_alg_rsa_pkcs1v15_sign() {
4357    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_RSA_PKCS1V15_SIGN + PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY"
4358    scripts/config.py full
4359    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4360    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4361    scripts/config.py -f "$CRYPTO_CONFIG_H" set PSA_WANT_ALG_RSA_PKCS1V15_SIGN 1
4362    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RSA_PKCS1V15_CRYPT
4363    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RSA_OAEP
4364    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RSA_PSS
4365    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4366    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4367}
4368
4369# This should be renamed to test and updated once the accelerator RSA code is in place and ready to test.
4370component_build_psa_accel_alg_rsa_oaep() {
4371    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_RSA_OAEP + PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY"
4372    scripts/config.py full
4373    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4374    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4375    scripts/config.py -f "$CRYPTO_CONFIG_H" set PSA_WANT_ALG_RSA_OAEP 1
4376    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RSA_PKCS1V15_CRYPT
4377    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RSA_PKCS1V15_SIGN
4378    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RSA_PSS
4379    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4380    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_RSA_OAEP -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4381}
4382
4383# This should be renamed to test and updated once the accelerator RSA code is in place and ready to test.
4384component_build_psa_accel_alg_rsa_pss() {
4385    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_ALG_RSA_PSS + PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY"
4386    scripts/config.py full
4387    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4388    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4389    scripts/config.py -f "$CRYPTO_CONFIG_H" set PSA_WANT_ALG_RSA_PSS 1
4390    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RSA_PKCS1V15_CRYPT
4391    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RSA_PKCS1V15_SIGN
4392    scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_RSA_OAEP
4393    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4394    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_ALG_RSA_PSS -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4395}
4396
4397# This should be renamed to test and updated once the accelerator RSA code is in place and ready to test.
4398component_build_psa_accel_key_type_rsa_key_pair() {
4399    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_xxx + PSA_WANT_ALG_RSA_PSS"
4400    scripts/config.py full
4401    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4402    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4403    scripts/config.py -f "$CRYPTO_CONFIG_H" set PSA_WANT_ALG_RSA_PSS 1
4404    scripts/config.py -f "$CRYPTO_CONFIG_H" set PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC 1
4405    scripts/config.py -f "$CRYPTO_CONFIG_H" set PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_IMPORT 1
4406    scripts/config.py -f "$CRYPTO_CONFIG_H" set PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_EXPORT 1
4407    scripts/config.py -f "$CRYPTO_CONFIG_H" set PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE 1
4408    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4409    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4410}
4411
4412# This should be renamed to test and updated once the accelerator RSA code is in place and ready to test.
4413component_build_psa_accel_key_type_rsa_public_key() {
4414    msg "build: full - MBEDTLS_USE_PSA_CRYPTO + PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY + PSA_WANT_ALG_RSA_PSS"
4415    scripts/config.py full
4416    scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
4417    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4418    scripts/config.py -f "$CRYPTO_CONFIG_H" set PSA_WANT_ALG_RSA_PSS 1
4419    scripts/config.py -f "$CRYPTO_CONFIG_H" set PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY 1
4420    # Need to define the correct symbol and include the test driver header path in order to build with the test driver
4421    make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST -DMBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY -I../tests/include" LDFLAGS="$ASAN_CFLAGS"
4422}
4423
4424
4425support_build_tfm_armcc () {
4426    support_build_armcc
4427}
4428
4429component_build_tfm_armcc() {
4430    # test the TF-M configuration can build cleanly with various warning flags enabled
4431    cp configs/config-tfm.h "$CONFIG_H"
4432
4433    msg "build: TF-M config, armclang armv7-m thumb2"
4434    armc6_build_test "--target=arm-arm-none-eabi -march=armv7-m -mthumb -Os -std=c99 -Werror -Wall -Wextra -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral -Wshadow -Wasm-operand-widths -Wunused -I../tests/include/spe"
4435}
4436
4437component_build_tfm() {
4438    # Check that the TF-M configuration can build cleanly with various
4439    # warning flags enabled. We don't build or run tests, since the
4440    # TF-M configuration needs a TF-M platform. A tweaked version of
4441    # the configuration that works on mainstream platforms is in
4442    # configs/config-tfm.h, tested via test-ref-configs.pl.
4443    cp configs/config-tfm.h "$CONFIG_H"
4444
4445    msg "build: TF-M config, clang, armv7-m thumb2"
4446    make lib CC="clang" CFLAGS="--target=arm-linux-gnueabihf -march=armv7-m -mthumb -Os -std=c99 -Werror -Wall -Wextra -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral -Wshadow -Wasm-operand-widths -Wunused -I../tests/include/spe"
4447
4448    msg "build: TF-M config, gcc native build"
4449    make clean
4450    make lib CC="gcc" CFLAGS="-Os -std=c99 -Werror -Wall -Wextra -Wwrite-strings -Wpointer-arith -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral -Wshadow -Wformat-signedness -Wlogical-op -I../tests/include/spe"
4451}
4452
4453# Test that the given .o file builds with all (valid) combinations of the given options.
4454#
4455# Syntax: build_test_config_combos FILE VALIDATOR_FUNCTION OPT1 OPT2 ...
4456#
4457# The validator function is the name of a function to validate the combination of options.
4458# It may be "" if all combinations are valid.
4459# It receives a string containing a combination of options, as passed to the compiler,
4460# e.g. "-DOPT1 -DOPT2 ...". It must return 0 iff the combination is valid, non-zero if invalid.
4461build_test_config_combos() {
4462    file=$1
4463    shift
4464    validate_options=$1
4465    shift
4466    options=("$@")
4467
4468    # clear all of the options so that they can be overridden on the clang commandline
4469    for opt in "${options[@]}"; do
4470        ./scripts/config.py unset ${opt}
4471    done
4472
4473    # enter the directory containing the target file & strip the dir from the filename
4474    cd $(dirname ${file})
4475    file=$(basename ${file})
4476
4477    # The most common issue is unused variables/functions, so ensure -Wunused is set.
4478    warning_flags="-Werror -Wall -Wextra -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral -Wshadow -Wasm-operand-widths -Wunused"
4479
4480    # Extract the command generated by the Makefile to build the target file.
4481    # This ensures that we have any include paths, macro definitions, etc
4482    # that may be applied by make.
4483    # Add -fsyntax-only as we only want a syntax check and don't need to generate a file.
4484    compile_cmd="clang \$(LOCAL_CFLAGS) ${warning_flags} -fsyntax-only -c"
4485
4486    makefile=$(TMPDIR=. mktemp)
4487    deps=""
4488
4489    len=${#options[@]}
4490    source_file=${file%.o}.c
4491
4492    targets=0
4493    echo 'include Makefile' >${makefile}
4494
4495    for ((i = 0; i < $((2**${len})); i++)); do
4496        # generate each of 2^n combinations of options
4497        # each bit of $i is used to determine if options[i] will be set or not
4498        target="t"
4499        clang_args=""
4500        for ((j = 0; j < ${len}; j++)); do
4501            if (((i >> j) & 1)); then
4502                opt=-D${options[$j]}
4503                clang_args="${clang_args} ${opt}"
4504                target="${target}${opt}"
4505            fi
4506        done
4507
4508        # if combination is not known to be invalid, add it to the makefile
4509        if [[ -z $validate_options ]] || $validate_options "${clang_args}"; then
4510            cmd="${compile_cmd} ${clang_args}"
4511            echo "${target}: ${source_file}; $cmd ${source_file}" >> ${makefile}
4512
4513            deps="${deps} ${target}"
4514            ((++targets))
4515        fi
4516    done
4517
4518    echo "build_test_config_combos: ${deps}" >> ${makefile}
4519
4520    # execute all of the commands via Make (probably in parallel)
4521    make -s -f ${makefile} build_test_config_combos
4522    echo "$targets targets checked"
4523
4524    # clean up the temporary makefile
4525    rm ${makefile}
4526}
4527
4528validate_aes_config_variations() {
4529    if [[ "$1" == *"MBEDTLS_AES_USE_HARDWARE_ONLY"* ]]; then
4530        if [[ "$1" == *"MBEDTLS_PADLOCK_C"* ]]; then
4531            return 1
4532        fi
4533        if [[ !(("$HOSTTYPE" == "aarch64" && "$1" != *"MBEDTLS_AESCE_C"*) || \
4534                ("$HOSTTYPE" == "x86_64"  && "$1" != *"MBEDTLS_AESNI_C"*)) ]]; then
4535            return 1
4536        fi
4537    fi
4538    return 0
4539}
4540
4541component_build_aes_variations() {
4542    # 18s - around 90ms per clang invocation on M1 Pro
4543    #
4544    # aes.o has many #if defined(...) guards that intersect in complex ways.
4545    # Test that all the combinations build cleanly.
4546
4547    MBEDTLS_ROOT_DIR="$PWD"
4548    msg "build: aes.o for all combinations of relevant config options"
4549
4550    build_test_config_combos library/aes.o validate_aes_config_variations \
4551        "MBEDTLS_AES_SETKEY_ENC_ALT" "MBEDTLS_AES_DECRYPT_ALT" \
4552        "MBEDTLS_AES_ROM_TABLES" "MBEDTLS_AES_ENCRYPT_ALT" "MBEDTLS_AES_SETKEY_DEC_ALT" \
4553        "MBEDTLS_AES_FEWER_TABLES" "MBEDTLS_PADLOCK_C" "MBEDTLS_AES_USE_HARDWARE_ONLY" \
4554        "MBEDTLS_AESNI_C" "MBEDTLS_AESCE_C" "MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH"
4555
4556    cd "$MBEDTLS_ROOT_DIR"
4557    msg "build: aes.o for all combinations of relevant config options + BLOCK_CIPHER_NO_DECRYPT"
4558
4559    # MBEDTLS_BLOCK_CIPHER_NO_DECRYPT is incompatible with ECB in PSA, CBC/XTS/NIST_KW/DES,
4560    # manually set or unset those configurations to check
4561    # MBEDTLS_BLOCK_CIPHER_NO_DECRYPT with various combinations in aes.o.
4562    scripts/config.py set MBEDTLS_BLOCK_CIPHER_NO_DECRYPT
4563    scripts/config.py unset MBEDTLS_CIPHER_MODE_CBC
4564    scripts/config.py unset MBEDTLS_CIPHER_MODE_XTS
4565    scripts/config.py unset MBEDTLS_DES_C
4566    scripts/config.py unset MBEDTLS_NIST_KW_C
4567    build_test_config_combos library/aes.o validate_aes_config_variations \
4568        "MBEDTLS_AES_SETKEY_ENC_ALT" "MBEDTLS_AES_DECRYPT_ALT" \
4569        "MBEDTLS_AES_ROM_TABLES" "MBEDTLS_AES_ENCRYPT_ALT" "MBEDTLS_AES_SETKEY_DEC_ALT" \
4570        "MBEDTLS_AES_FEWER_TABLES" "MBEDTLS_PADLOCK_C" "MBEDTLS_AES_USE_HARDWARE_ONLY" \
4571        "MBEDTLS_AESNI_C" "MBEDTLS_AESCE_C" "MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH"
4572}
4573
4574component_test_no_platform () {
4575    # Full configuration build, without platform support, file IO and net sockets.
4576    # This should catch missing mbedtls_printf definitions, and by disabling file
4577    # IO, it should catch missing '#include <stdio.h>'
4578    msg "build: full config except platform/fsio/net, make, gcc, C99" # ~ 30s
4579    scripts/config.py full_no_platform
4580    scripts/config.py unset MBEDTLS_PLATFORM_C
4581    scripts/config.py unset MBEDTLS_NET_C
4582    scripts/config.py unset MBEDTLS_FS_IO
4583    scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
4584    scripts/config.py unset MBEDTLS_PSA_CRYPTO_STORAGE_C
4585    scripts/config.py unset MBEDTLS_PSA_ITS_FILE_C
4586    scripts/config.py unset MBEDTLS_ENTROPY_NV_SEED
4587    # Note, _DEFAULT_SOURCE needs to be defined for platforms using glibc version >2.19,
4588    # to re-enable platform integration features otherwise disabled in C99 builds
4589    make CC=gcc CFLAGS='-Werror -Wall -Wextra -std=c99 -pedantic -Os -D_DEFAULT_SOURCE' lib programs
4590    make CC=gcc CFLAGS='-Werror -Wall -Wextra -Os' test
4591}
4592
4593component_build_no_std_function () {
4594    # catch compile bugs in _uninit functions
4595    msg "build: full config with NO_STD_FUNCTION, make, gcc" # ~ 30s
4596    scripts/config.py full
4597    scripts/config.py set MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
4598    scripts/config.py unset MBEDTLS_ENTROPY_NV_SEED
4599    scripts/config.py unset MBEDTLS_PLATFORM_NV_SEED_ALT
4600    CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Check .
4601    make
4602}
4603
4604component_build_no_ssl_srv () {
4605    msg "build: full config except SSL server, make, gcc" # ~ 30s
4606    scripts/config.py full
4607    scripts/config.py unset MBEDTLS_SSL_SRV_C
4608    make CC=gcc CFLAGS='-Werror -Wall -Wextra -O1'
4609}
4610
4611component_build_no_ssl_cli () {
4612    msg "build: full config except SSL client, make, gcc" # ~ 30s
4613    scripts/config.py full
4614    scripts/config.py unset MBEDTLS_SSL_CLI_C
4615    make CC=gcc CFLAGS='-Werror -Wall -Wextra -O1'
4616}
4617
4618component_build_no_sockets () {
4619    # Note, C99 compliance can also be tested with the sockets support disabled,
4620    # as that requires a POSIX platform (which isn't the same as C99).
4621    msg "build: full config except net_sockets.c, make, gcc -std=c99 -pedantic" # ~ 30s
4622    scripts/config.py full
4623    scripts/config.py unset MBEDTLS_NET_C # getaddrinfo() undeclared, etc.
4624    scripts/config.py set MBEDTLS_NO_PLATFORM_ENTROPY # uses syscall() on GNU/Linux
4625    make CC=gcc CFLAGS='-Werror -Wall -Wextra -O1 -std=c99 -pedantic' lib
4626}
4627
4628component_test_memory_buffer_allocator_backtrace () {
4629    msg "build: default config with memory buffer allocator and backtrace enabled"
4630    scripts/config.py set MBEDTLS_MEMORY_BUFFER_ALLOC_C
4631    scripts/config.py set MBEDTLS_PLATFORM_MEMORY
4632    scripts/config.py set MBEDTLS_MEMORY_BACKTRACE
4633    scripts/config.py set MBEDTLS_MEMORY_DEBUG
4634    cmake -DCMAKE_BUILD_TYPE:String=Release .
4635    make
4636
4637    msg "test: MBEDTLS_MEMORY_BUFFER_ALLOC_C and MBEDTLS_MEMORY_BACKTRACE"
4638    make test
4639}
4640
4641component_test_memory_buffer_allocator () {
4642    msg "build: default config with memory buffer allocator"
4643    scripts/config.py set MBEDTLS_MEMORY_BUFFER_ALLOC_C
4644    scripts/config.py set MBEDTLS_PLATFORM_MEMORY
4645    cmake -DCMAKE_BUILD_TYPE:String=Release .
4646    make
4647
4648    msg "test: MBEDTLS_MEMORY_BUFFER_ALLOC_C"
4649    make test
4650
4651    msg "test: ssl-opt.sh, MBEDTLS_MEMORY_BUFFER_ALLOC_C"
4652    # MBEDTLS_MEMORY_BUFFER_ALLOC is slow. Skip tests that tend to time out.
4653    tests/ssl-opt.sh -e '^DTLS proxy'
4654}
4655
4656component_test_no_max_fragment_length () {
4657    # Run max fragment length tests with MFL disabled
4658    msg "build: default config except MFL extension (ASan build)" # ~ 30s
4659    scripts/config.py unset MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
4660    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
4661    make
4662
4663    msg "test: ssl-opt.sh, MFL-related tests"
4664    tests/ssl-opt.sh -f "Max fragment length"
4665}
4666
4667component_test_asan_remove_peer_certificate () {
4668    msg "build: default config with MBEDTLS_SSL_KEEP_PEER_CERTIFICATE disabled (ASan build)"
4669    scripts/config.py unset MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
4670    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
4671    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
4672    make
4673
4674    msg "test: !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE"
4675    make test
4676
4677    msg "test: ssl-opt.sh, !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE"
4678    tests/ssl-opt.sh
4679
4680    msg "test: compat.sh, !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE"
4681    tests/compat.sh
4682
4683    msg "test: context-info.sh, !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE"
4684    tests/context-info.sh
4685}
4686
4687component_test_no_max_fragment_length_small_ssl_out_content_len () {
4688    msg "build: no MFL extension, small SSL_OUT_CONTENT_LEN (ASan build)"
4689    scripts/config.py unset MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
4690    scripts/config.py set MBEDTLS_SSL_IN_CONTENT_LEN 16384
4691    scripts/config.py set MBEDTLS_SSL_OUT_CONTENT_LEN 4096
4692    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
4693    make
4694
4695    msg "test: MFL tests (disabled MFL extension case) & large packet tests"
4696    tests/ssl-opt.sh -f "Max fragment length\|Large buffer"
4697
4698    msg "test: context-info.sh (disabled MFL extension case)"
4699    tests/context-info.sh
4700}
4701
4702component_test_variable_ssl_in_out_buffer_len () {
4703    msg "build: MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH enabled (ASan build)"
4704    scripts/config.py set MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH
4705    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
4706    make
4707
4708    msg "test: MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH enabled"
4709    make test
4710
4711    msg "test: ssl-opt.sh, MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH enabled"
4712    tests/ssl-opt.sh
4713
4714    msg "test: compat.sh, MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH enabled"
4715    tests/compat.sh
4716}
4717
4718component_test_dtls_cid_legacy () {
4719    msg "build: MBEDTLS_SSL_DTLS_CONNECTION_ID (legacy) enabled (ASan build)"
4720    scripts/config.py set MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT 1
4721
4722    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
4723    make
4724
4725    msg "test: MBEDTLS_SSL_DTLS_CONNECTION_ID (legacy)"
4726    make test
4727
4728    msg "test: ssl-opt.sh, MBEDTLS_SSL_DTLS_CONNECTION_ID (legacy) enabled"
4729    tests/ssl-opt.sh
4730
4731    msg "test: compat.sh, MBEDTLS_SSL_DTLS_CONNECTION_ID (legacy) enabled"
4732    tests/compat.sh
4733}
4734
4735component_test_ssl_alloc_buffer_and_mfl () {
4736    msg "build: default config with memory buffer allocator and MFL extension"
4737    scripts/config.py set MBEDTLS_MEMORY_BUFFER_ALLOC_C
4738    scripts/config.py set MBEDTLS_PLATFORM_MEMORY
4739    scripts/config.py set MBEDTLS_MEMORY_DEBUG
4740    scripts/config.py set MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
4741    scripts/config.py set MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH
4742    cmake -DCMAKE_BUILD_TYPE:String=Release .
4743    make
4744
4745    msg "test: MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH, MBEDTLS_MEMORY_BUFFER_ALLOC_C, MBEDTLS_MEMORY_DEBUG and MBEDTLS_SSL_MAX_FRAGMENT_LENGTH"
4746    make test
4747
4748    msg "test: MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH, MBEDTLS_MEMORY_BUFFER_ALLOC_C, MBEDTLS_MEMORY_DEBUG and MBEDTLS_SSL_MAX_FRAGMENT_LENGTH"
4749    tests/ssl-opt.sh -f "Handshake memory usage"
4750}
4751
4752component_test_when_no_ciphersuites_have_mac () {
4753    msg "build: when no ciphersuites have MAC"
4754    scripts/config.py unset MBEDTLS_CIPHER_NULL_CIPHER
4755    scripts/config.py unset MBEDTLS_CIPHER_MODE_CBC
4756    scripts/config.py unset MBEDTLS_CMAC_C
4757    make
4758
4759    msg "test: !MBEDTLS_SSL_SOME_MODES_USE_MAC"
4760    make test
4761
4762    msg "test ssl-opt.sh: !MBEDTLS_SSL_SOME_MODES_USE_MAC"
4763    tests/ssl-opt.sh -f 'Default\|EtM' -e 'without EtM'
4764}
4765
4766component_test_no_date_time () {
4767    msg "build: default config without MBEDTLS_HAVE_TIME_DATE"
4768    scripts/config.py unset MBEDTLS_HAVE_TIME_DATE
4769    cmake -D CMAKE_BUILD_TYPE:String=Check .
4770    make
4771
4772    msg "test: !MBEDTLS_HAVE_TIME_DATE - main suites"
4773    make test
4774}
4775
4776component_test_platform_calloc_macro () {
4777    msg "build: MBEDTLS_PLATFORM_{CALLOC/FREE}_MACRO enabled (ASan build)"
4778    scripts/config.py set MBEDTLS_PLATFORM_MEMORY
4779    scripts/config.py set MBEDTLS_PLATFORM_CALLOC_MACRO calloc
4780    scripts/config.py set MBEDTLS_PLATFORM_FREE_MACRO   free
4781    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
4782    make
4783
4784    msg "test: MBEDTLS_PLATFORM_{CALLOC/FREE}_MACRO enabled (ASan build)"
4785    make test
4786}
4787
4788component_test_malloc_0_null () {
4789    msg "build: malloc(0) returns NULL (ASan+UBSan build)"
4790    scripts/config.py full
4791    make CC=$ASAN_CC CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"$PWD/tests/configs/user-config-malloc-0-null.h\"' $ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
4792
4793    msg "test: malloc(0) returns NULL (ASan+UBSan build)"
4794    make test
4795
4796    msg "selftest: malloc(0) returns NULL (ASan+UBSan build)"
4797    # Just the calloc selftest. "make test" ran the others as part of the
4798    # test suites.
4799    programs/test/selftest calloc
4800
4801    msg "test ssl-opt.sh: malloc(0) returns NULL (ASan+UBSan build)"
4802    # Run a subset of the tests. The choice is a balance between coverage
4803    # and time (including time indirectly wasted due to flaky tests).
4804    # The current choice is to skip tests whose description includes
4805    # "proxy", which is an approximation of skipping tests that use the
4806    # UDP proxy, which tend to be slower and flakier.
4807    tests/ssl-opt.sh -e 'proxy'
4808}
4809
4810support_test_aesni() {
4811    # Check that gcc targets x86_64 (we can build AESNI), and check for
4812    # AESNI support on the host (we can run AESNI).
4813    #
4814    # The name of this function is possibly slightly misleading, but needs to align
4815    # with the name of the corresponding test, component_test_aesni.
4816    #
4817    # In principle 32-bit x86 can support AESNI, but our implementation does not
4818    # support 32-bit x86, so we check for x86-64.
4819    # We can only grep /proc/cpuinfo on Linux, so this also checks for Linux
4820    (gcc -v 2>&1 | grep Target | grep -q x86_64) &&
4821        [[ "$HOSTTYPE" == "x86_64" && "$OSTYPE" == "linux-gnu" ]] &&
4822        (lscpu | grep -qw aes)
4823}
4824
4825component_test_aesni () { # ~ 60s
4826    # This tests the two AESNI implementations (intrinsics and assembly), and also the plain C
4827    # fallback. It also tests the logic that is used to select which implementation(s) to build.
4828    #
4829    # This test does not require the host to have support for AESNI (if it doesn't, the run-time
4830    # AESNI detection will fallback to the plain C implementation, so the tests will instead
4831    # exercise the plain C impl).
4832
4833    msg "build: default config with different AES implementations"
4834    scripts/config.py set MBEDTLS_AESNI_C
4835    scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
4836    scripts/config.py set MBEDTLS_HAVE_ASM
4837
4838    # test the intrinsics implementation
4839    msg "AES tests, test intrinsics"
4840    make clean
4841    make CC=gcc CFLAGS='-Werror -Wall -Wextra -mpclmul -msse2 -maes'
4842    # check that we built intrinsics - this should be used by default when supported by the compiler
4843    ./programs/test/selftest aes | grep "AESNI code" | grep -q "intrinsics"
4844
4845    # test the asm implementation
4846    msg "AES tests, test assembly"
4847    make clean
4848    make CC=gcc CFLAGS='-Werror -Wall -Wextra -mno-pclmul -mno-sse2 -mno-aes'
4849    # check that we built assembly - this should be built if the compiler does not support intrinsics
4850    ./programs/test/selftest aes | grep "AESNI code" | grep -q "assembly"
4851
4852    # test the plain C implementation
4853    scripts/config.py unset MBEDTLS_AESNI_C
4854    scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
4855    msg "AES tests, plain C"
4856    make clean
4857    make CC=gcc CFLAGS='-O2 -Werror'
4858    # check that there is no AESNI code present
4859    ./programs/test/selftest aes | not grep -q "AESNI code"
4860    not grep -q "AES note: using AESNI" ./programs/test/selftest
4861    grep -q "AES note: built-in implementation." ./programs/test/selftest
4862
4863    # test the intrinsics implementation
4864    scripts/config.py set MBEDTLS_AESNI_C
4865    scripts/config.py set MBEDTLS_AES_USE_HARDWARE_ONLY
4866    msg "AES tests, test AESNI only"
4867    make clean
4868    make CC=gcc CFLAGS='-Werror -Wall -Wextra -mpclmul -msse2 -maes'
4869    ./programs/test/selftest aes | grep -q "AES note: using AESNI"
4870    ./programs/test/selftest aes | not grep -q "AES note: built-in implementation."
4871    grep -q "AES note: using AESNI" ./programs/test/selftest
4872    not grep -q "AES note: built-in implementation." ./programs/test/selftest
4873}
4874
4875component_test_sha3_variations() {
4876    msg "sha3 loop unroll variations"
4877
4878    # define minimal config sufficient to test SHA3
4879    cat > include/mbedtls/mbedtls_config.h << END
4880        #define MBEDTLS_SELF_TEST
4881        #define MBEDTLS_SHA3_C
4882END
4883
4884    msg "all loops unrolled"
4885    make clean
4886    make -C tests test_suite_shax CFLAGS="-DMBEDTLS_SHA3_THETA_UNROLL=1 -DMBEDTLS_SHA3_PI_UNROLL=1 -DMBEDTLS_SHA3_CHI_UNROLL=1 -DMBEDTLS_SHA3_RHO_UNROLL=1"
4887    ./tests/test_suite_shax
4888
4889    msg "all loops rolled up"
4890    make clean
4891    make -C tests test_suite_shax CFLAGS="-DMBEDTLS_SHA3_THETA_UNROLL=0 -DMBEDTLS_SHA3_PI_UNROLL=0 -DMBEDTLS_SHA3_CHI_UNROLL=0 -DMBEDTLS_SHA3_RHO_UNROLL=0"
4892    ./tests/test_suite_shax
4893}
4894
4895support_test_aesni_m32() {
4896    support_test_m32_no_asm && (lscpu | grep -qw aes)
4897}
4898
4899component_test_aesni_m32 () { # ~ 60s
4900    # This tests are duplicated from component_test_aesni for i386 target
4901    #
4902    # AESNI intrinsic code supports i386 and assembly code does not support it.
4903
4904    msg "build: default config with different AES implementations"
4905    scripts/config.py set MBEDTLS_AESNI_C
4906    scripts/config.py set MBEDTLS_PADLOCK_C
4907    scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
4908    scripts/config.py set MBEDTLS_HAVE_ASM
4909
4910    # test the intrinsics implementation with gcc
4911    msg "AES tests, test intrinsics (gcc)"
4912    make clean
4913    make CC=gcc CFLAGS='-m32 -Werror -Wall -Wextra' LDFLAGS='-m32'
4914    # check that we built intrinsics - this should be used by default when supported by the compiler
4915    ./programs/test/selftest aes | grep "AESNI code" | grep -q "intrinsics"
4916    grep -q "AES note: using AESNI" ./programs/test/selftest
4917    grep -q "AES note: built-in implementation." ./programs/test/selftest
4918    grep -q "AES note: using VIA Padlock" ./programs/test/selftest
4919    grep -q mbedtls_aesni_has_support ./programs/test/selftest
4920
4921    scripts/config.py set MBEDTLS_AESNI_C
4922    scripts/config.py unset MBEDTLS_PADLOCK_C
4923    scripts/config.py set MBEDTLS_AES_USE_HARDWARE_ONLY
4924    msg "AES tests, test AESNI only"
4925    make clean
4926    make CC=gcc CFLAGS='-m32 -Werror -Wall -Wextra -mpclmul -msse2 -maes' LDFLAGS='-m32'
4927    ./programs/test/selftest aes | grep -q "AES note: using AESNI"
4928    ./programs/test/selftest aes | not grep -q "AES note: built-in implementation."
4929    grep -q "AES note: using AESNI" ./programs/test/selftest
4930    not grep -q "AES note: built-in implementation." ./programs/test/selftest
4931    not grep -q "AES note: using VIA Padlock" ./programs/test/selftest
4932    not grep -q mbedtls_aesni_has_support ./programs/test/selftest
4933}
4934
4935support_test_aesni_m32_clang() {
4936    # clang >= 4 is required to build with target attributes
4937    support_test_aesni_m32 && [[ $(clang_version) -ge 4 ]]
4938}
4939
4940component_test_aesni_m32_clang() {
4941
4942    scripts/config.py set MBEDTLS_AESNI_C
4943    scripts/config.py set MBEDTLS_PADLOCK_C
4944    scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
4945    scripts/config.py set MBEDTLS_HAVE_ASM
4946
4947    # test the intrinsics implementation with clang
4948    msg "AES tests, test intrinsics (clang)"
4949    make clean
4950    make CC=clang CFLAGS='-m32 -Werror -Wall -Wextra' LDFLAGS='-m32'
4951    # check that we built intrinsics - this should be used by default when supported by the compiler
4952    ./programs/test/selftest aes | grep "AESNI code" | grep -q "intrinsics"
4953    grep -q "AES note: using AESNI" ./programs/test/selftest
4954    grep -q "AES note: built-in implementation." ./programs/test/selftest
4955    grep -q "AES note: using VIA Padlock" ./programs/test/selftest
4956    grep -q mbedtls_aesni_has_support ./programs/test/selftest
4957}
4958
4959# For timebeing, no aarch64 gcc available in CI and no arm64 CI node.
4960component_build_aes_aesce_armcc () {
4961    msg "Build: AESCE test on arm64 platform without plain C."
4962    scripts/config.py baremetal
4963
4964    # armc[56] don't support SHA-512 intrinsics
4965    scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
4966
4967    # Stop armclang warning about feature detection for A64_CRYPTO.
4968    # With this enabled, the library does build correctly under armclang,
4969    # but in baremetal builds (as tested here), feature detection is
4970    # unavailable, and the user is notified via a #warning. So enabling
4971    # this feature would prevent us from building with -Werror on
4972    # armclang. Tracked in #7198.
4973    scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
4974    scripts/config.py set MBEDTLS_HAVE_ASM
4975
4976    msg "AESCE, build with default configuration."
4977    scripts/config.py set MBEDTLS_AESCE_C
4978    scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
4979    armc6_build_test "-O1 --target=aarch64-arm-none-eabi -march=armv8-a+crypto"
4980
4981    msg "AESCE, build AESCE only"
4982    scripts/config.py set MBEDTLS_AESCE_C
4983    scripts/config.py set MBEDTLS_AES_USE_HARDWARE_ONLY
4984    armc6_build_test "-O1 --target=aarch64-arm-none-eabi -march=armv8-a+crypto"
4985}
4986
4987support_build_aes_armce() {
4988    # clang >= 11 is required to build with AES extensions
4989    [[ $(clang_version) -ge 11 ]]
4990}
4991
4992component_build_aes_armce () {
4993    # Test variations of AES with Armv8 crypto extensions
4994    scripts/config.py set MBEDTLS_AESCE_C
4995    scripts/config.py set MBEDTLS_AES_USE_HARDWARE_ONLY
4996
4997    msg "MBEDTLS_AES_USE_HARDWARE_ONLY, clang, aarch64"
4998    make -B library/aesce.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a+crypto"
4999
5000    msg "MBEDTLS_AES_USE_HARDWARE_ONLY, clang, arm"
5001    make -B library/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm"
5002
5003    msg "MBEDTLS_AES_USE_HARDWARE_ONLY, clang, thumb"
5004    make -B library/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb"
5005
5006    scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
5007
5008    msg "no MBEDTLS_AES_USE_HARDWARE_ONLY, clang, aarch64"
5009    make -B library/aesce.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a+crypto"
5010
5011    msg "no MBEDTLS_AES_USE_HARDWARE_ONLY, clang, arm"
5012    make -B library/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm"
5013
5014    msg "no MBEDTLS_AES_USE_HARDWARE_ONLY, clang, thumb"
5015    make -B library/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb"
5016
5017    # test for presence of AES instructions
5018    scripts/config.py set MBEDTLS_AES_USE_HARDWARE_ONLY
5019    msg "clang, test A32 crypto instructions built"
5020    make -B library/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm -S"
5021    grep -E 'aes[0-9a-z]+.[0-9]\s*[qv]' library/aesce.o
5022    msg "clang, test T32 crypto instructions built"
5023    make -B library/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb -S"
5024    grep -E 'aes[0-9a-z]+.[0-9]\s*[qv]' library/aesce.o
5025    msg "clang, test aarch64 crypto instructions built"
5026    make -B library/aesce.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a -S"
5027    grep -E 'aes[a-z]+\s*[qv]' library/aesce.o
5028
5029    # test for absence of AES instructions
5030    scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
5031    scripts/config.py unset MBEDTLS_AESCE_C
5032    msg "clang, test A32 crypto instructions not built"
5033    make -B library/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm -S"
5034    not grep -E 'aes[0-9a-z]+.[0-9]\s*[qv]' library/aesce.o
5035    msg "clang, test T32 crypto instructions not built"
5036    make -B library/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb -S"
5037    not grep -E 'aes[0-9a-z]+.[0-9]\s*[qv]' library/aesce.o
5038    msg "clang, test aarch64 crypto instructions not built"
5039    make -B library/aesce.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a -S"
5040    not grep -E 'aes[a-z]+\s*[qv]' library/aesce.o
5041}
5042
5043support_build_sha_armce() {
5044    # clang >= 4 is required to build with SHA extensions
5045    [[ $(clang_version) -ge 4 ]]
5046}
5047
5048component_build_sha_armce () {
5049    scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
5050
5051
5052    # Test variations of SHA256 Armv8 crypto extensions
5053    scripts/config.py set MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY
5054        msg "MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY clang, aarch64"
5055        make -B library/sha256.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a"
5056        msg "MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY clang, arm"
5057        make -B library/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm"
5058    scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY
5059
5060
5061    # test the deprecated form of the config option
5062    scripts/config.py set MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY
5063        msg "MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY clang, thumb"
5064        make -B library/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb"
5065    scripts/config.py unset MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY
5066
5067    scripts/config.py set MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
5068        msg "MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT clang, aarch64"
5069        make -B library/sha256.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a"
5070    scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
5071
5072
5073    # test the deprecated form of the config option
5074    scripts/config.py set MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
5075        msg "MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT clang, arm"
5076        make -B library/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm -std=c99"
5077        msg "MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT clang, thumb"
5078        make -B library/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb"
5079    scripts/config.py unset MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
5080
5081
5082    # examine the disassembly for presence of SHA instructions
5083    for opt in MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT; do
5084        scripts/config.py set ${opt}
5085            msg "${opt} clang, test A32 crypto instructions built"
5086            make -B library/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm -S"
5087            grep -E 'sha256[a-z0-9]+.32\s+[qv]' library/sha256.o
5088
5089            msg "${opt} clang, test T32 crypto instructions built"
5090            make -B library/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb -S"
5091            grep -E 'sha256[a-z0-9]+.32\s+[qv]' library/sha256.o
5092
5093            msg "${opt} clang, test aarch64 crypto instructions built"
5094            make -B library/sha256.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a -S"
5095            grep -E 'sha256[a-z0-9]+\s+[qv]' library/sha256.o
5096        scripts/config.py unset ${opt}
5097    done
5098
5099
5100    # examine the disassembly for absence of SHA instructions
5101    msg "clang, test A32 crypto instructions not built"
5102    make -B library/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm -S"
5103    not grep -E 'sha256[a-z0-9]+.32\s+[qv]' library/sha256.o
5104
5105    msg "clang, test T32 crypto instructions not built"
5106    make -B library/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb -S"
5107    not grep -E 'sha256[a-z0-9]+.32\s+[qv]' library/sha256.o
5108
5109    msg "clang, test aarch64 crypto instructions not built"
5110    make -B library/sha256.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a -S"
5111    not grep -E 'sha256[a-z0-9]+\s+[qv]' library/sha256.o
5112}
5113
5114# For timebeing, no VIA Padlock platform available.
5115component_build_aes_via_padlock () {
5116
5117    msg "AES:VIA PadLock, build with default configuration."
5118    scripts/config.py unset MBEDTLS_AESNI_C
5119    scripts/config.py set MBEDTLS_PADLOCK_C
5120    scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
5121    make CC=gcc CFLAGS="$ASAN_CFLAGS -m32" LDFLAGS="-m32 $ASAN_CFLAGS"
5122    grep -q mbedtls_padlock_has_support ./programs/test/selftest
5123
5124}
5125
5126support_build_aes_via_padlock_only () {
5127    ( [ "$MBEDTLS_TEST_PLATFORM" == "Linux-x86_64" ] || \
5128        [ "$MBEDTLS_TEST_PLATFORM" == "Linux-amd64" ] ) && \
5129    [ "`dpkg --print-foreign-architectures`" == "i386" ]
5130}
5131
5132support_build_aes_aesce_armcc () {
5133    support_build_armcc
5134}
5135
5136component_test_aes_only_128_bit_keys () {
5137    msg "build: default config + AES_ONLY_128_BIT_KEY_LENGTH"
5138    scripts/config.py set MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
5139    scripts/config.py unset MBEDTLS_PADLOCK_C
5140
5141    make CFLAGS='-O2 -Werror -Wall -Wextra'
5142
5143    msg "test: default config + AES_ONLY_128_BIT_KEY_LENGTH"
5144    make test
5145}
5146
5147component_test_no_ctr_drbg_aes_only_128_bit_keys () {
5148    msg "build: default config + AES_ONLY_128_BIT_KEY_LENGTH - CTR_DRBG_C"
5149    scripts/config.py set MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
5150    scripts/config.py unset MBEDTLS_CTR_DRBG_C
5151    scripts/config.py unset MBEDTLS_PADLOCK_C
5152
5153    make CC=clang CFLAGS='-Werror -Wall -Wextra'
5154
5155    msg "test: default config + AES_ONLY_128_BIT_KEY_LENGTH - CTR_DRBG_C"
5156    make test
5157}
5158
5159component_test_aes_only_128_bit_keys_have_builtins () {
5160    msg "build: default config + AES_ONLY_128_BIT_KEY_LENGTH - AESNI_C - AESCE_C"
5161    scripts/config.py set MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
5162    scripts/config.py unset MBEDTLS_PADLOCK_C
5163    scripts/config.py unset MBEDTLS_AESNI_C
5164    scripts/config.py unset MBEDTLS_AESCE_C
5165
5166    make CFLAGS='-O2 -Werror -Wall -Wextra'
5167
5168    msg "test: default config + AES_ONLY_128_BIT_KEY_LENGTH - AESNI_C - AESCE_C"
5169    make test
5170
5171    msg "selftest: default config + AES_ONLY_128_BIT_KEY_LENGTH - AESNI_C - AESCE_C"
5172    programs/test/selftest
5173}
5174
5175component_test_gcm_largetable () {
5176    msg "build: default config + GCM_LARGE_TABLE - AESNI_C - AESCE_C"
5177    scripts/config.py set MBEDTLS_GCM_LARGE_TABLE
5178    scripts/config.py unset MBEDTLS_PADLOCK_C
5179    scripts/config.py unset MBEDTLS_AESNI_C
5180    scripts/config.py unset MBEDTLS_AESCE_C
5181
5182    make CFLAGS='-O2 -Werror -Wall -Wextra'
5183
5184    msg "test: default config - GCM_LARGE_TABLE - AESNI_C - AESCE_C"
5185    make test
5186}
5187
5188component_test_aes_fewer_tables () {
5189    msg "build: default config with AES_FEWER_TABLES enabled"
5190    scripts/config.py set MBEDTLS_AES_FEWER_TABLES
5191    make CFLAGS='-O2 -Werror -Wall -Wextra'
5192
5193    msg "test: AES_FEWER_TABLES"
5194    make test
5195}
5196
5197component_test_aes_rom_tables () {
5198    msg "build: default config with AES_ROM_TABLES enabled"
5199    scripts/config.py set MBEDTLS_AES_ROM_TABLES
5200    make CFLAGS='-O2 -Werror -Wall -Wextra'
5201
5202    msg "test: AES_ROM_TABLES"
5203    make test
5204}
5205
5206component_test_aes_fewer_tables_and_rom_tables () {
5207    msg "build: default config with AES_ROM_TABLES and AES_FEWER_TABLES enabled"
5208    scripts/config.py set MBEDTLS_AES_FEWER_TABLES
5209    scripts/config.py set MBEDTLS_AES_ROM_TABLES
5210    make CFLAGS='-O2 -Werror -Wall -Wextra'
5211
5212    msg "test: AES_FEWER_TABLES + AES_ROM_TABLES"
5213    make test
5214}
5215
5216# helper for common_block_cipher_no_decrypt() which:
5217# - enable/disable the list of config options passed from -s/-u respectively.
5218# - build
5219# - test for tests_suite_xxx
5220# - selftest
5221#
5222# Usage: helper_block_cipher_no_decrypt_build_test
5223#        [-s set_opts] [-u unset_opts] [-c cflags] [-l ldflags] [option [...]]
5224# Options:  -s set_opts     the list of config options to enable
5225#           -u unset_opts   the list of config options to disable
5226#           -c cflags       the list of options passed to CFLAGS
5227#           -l ldflags      the list of options passed to LDFLAGS
5228helper_block_cipher_no_decrypt_build_test () {
5229    while [ $# -gt 0 ]; do
5230        case "$1" in
5231            -s)
5232                shift; local set_opts="$1";;
5233            -u)
5234                shift; local unset_opts="$1";;
5235            -c)
5236                shift; local cflags="-Werror -Wall -Wextra $1";;
5237            -l)
5238                shift; local ldflags="$1";;
5239        esac
5240        shift
5241    done
5242    set_opts="${set_opts:-}"
5243    unset_opts="${unset_opts:-}"
5244    cflags="${cflags:-}"
5245    ldflags="${ldflags:-}"
5246
5247    [ -n "$set_opts" ] && echo "Enabling: $set_opts" && scripts/config.py set-all $set_opts
5248    [ -n "$unset_opts" ] && echo "Disabling: $unset_opts" && scripts/config.py unset-all $unset_opts
5249
5250    msg "build: default config + BLOCK_CIPHER_NO_DECRYPT${set_opts:+ + $set_opts}${unset_opts:+ - $unset_opts} with $cflags${ldflags:+, $ldflags}"
5251    make clean
5252    make CFLAGS="-O2 $cflags" LDFLAGS="$ldflags"
5253
5254    # Make sure we don't have mbedtls_xxx_setkey_dec in AES/ARIA/CAMELLIA
5255    not grep mbedtls_aes_setkey_dec library/aes.o
5256    not grep mbedtls_aria_setkey_dec library/aria.o
5257    not grep mbedtls_camellia_setkey_dec library/camellia.o
5258    # Make sure we don't have mbedtls_internal_aes_decrypt in AES
5259    not grep mbedtls_internal_aes_decrypt library/aes.o
5260    # Make sure we don't have mbedtls_aesni_inverse_key in AESNI
5261    not grep mbedtls_aesni_inverse_key library/aesni.o
5262
5263    msg "test: default config + BLOCK_CIPHER_NO_DECRYPT${set_opts:+ + $set_opts}${unset_opts:+ - $unset_opts} with $cflags${ldflags:+, $ldflags}"
5264    make test
5265
5266    msg "selftest: default config + BLOCK_CIPHER_NO_DECRYPT${set_opts:+ + $set_opts}${unset_opts:+ - $unset_opts} with $cflags${ldflags:+, $ldflags}"
5267    programs/test/selftest
5268}
5269
5270# This is a common configuration function used in:
5271# - component_test_block_cipher_no_decrypt_aesni_legacy()
5272# - component_test_block_cipher_no_decrypt_aesni_use_psa()
5273# in order to test BLOCK_CIPHER_NO_DECRYPT with AESNI intrinsics,
5274# AESNI assembly and AES C implementation on x86_64 and with AESNI intrinsics
5275# on x86.
5276common_block_cipher_no_decrypt () {
5277    # test AESNI intrinsics
5278    helper_block_cipher_no_decrypt_build_test \
5279        -s "MBEDTLS_AESNI_C" \
5280        -c "-mpclmul -msse2 -maes"
5281
5282    # test AESNI assembly
5283    helper_block_cipher_no_decrypt_build_test \
5284        -s "MBEDTLS_AESNI_C" \
5285        -c "-mno-pclmul -mno-sse2 -mno-aes"
5286
5287    # test AES C implementation
5288    helper_block_cipher_no_decrypt_build_test \
5289        -u "MBEDTLS_AESNI_C"
5290
5291    # test AESNI intrinsics for i386 target
5292    helper_block_cipher_no_decrypt_build_test \
5293        -s "MBEDTLS_AESNI_C" \
5294        -c "-m32 -mpclmul -msse2 -maes" \
5295        -l "-m32"
5296}
5297
5298# This is a configuration function used in component_test_block_cipher_no_decrypt_xxx:
5299# usage: 0: no PSA crypto configuration
5300#        1: use PSA crypto configuration
5301config_block_cipher_no_decrypt () {
5302    use_psa=$1
5303
5304    scripts/config.py set MBEDTLS_BLOCK_CIPHER_NO_DECRYPT
5305    scripts/config.py unset MBEDTLS_CIPHER_MODE_CBC
5306    scripts/config.py unset MBEDTLS_CIPHER_MODE_XTS
5307    scripts/config.py unset MBEDTLS_DES_C
5308    scripts/config.py unset MBEDTLS_NIST_KW_C
5309
5310    if [ "$use_psa" -eq 1 ]; then
5311        # Enable support for cryptographic mechanisms through the PSA API.
5312        # Note: XTS, KW are not yet supported via the PSA API in Mbed TLS.
5313        scripts/config.py set MBEDTLS_PSA_CRYPTO_CONFIG
5314        scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CBC_NO_PADDING
5315        scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CBC_PKCS7
5316        scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_ECB_NO_PADDING
5317        scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_DES
5318    fi
5319}
5320
5321component_test_block_cipher_no_decrypt_aesni () {
5322    # This consistently causes an llvm crash on clang 3.8, so use gcc
5323    export CC=gcc
5324    config_block_cipher_no_decrypt 0
5325    common_block_cipher_no_decrypt
5326}
5327
5328component_test_block_cipher_no_decrypt_aesni_use_psa () {
5329    # This consistently causes an llvm crash on clang 3.8, so use gcc
5330    export CC=gcc
5331    config_block_cipher_no_decrypt 1
5332    common_block_cipher_no_decrypt
5333}
5334
5335support_test_block_cipher_no_decrypt_aesce_armcc () {
5336    support_build_armcc
5337}
5338
5339component_test_block_cipher_no_decrypt_aesce_armcc () {
5340    scripts/config.py baremetal
5341
5342    # armc[56] don't support SHA-512 intrinsics
5343    scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
5344
5345    # Stop armclang warning about feature detection for A64_CRYPTO.
5346    # With this enabled, the library does build correctly under armclang,
5347    # but in baremetal builds (as tested here), feature detection is
5348    # unavailable, and the user is notified via a #warning. So enabling
5349    # this feature would prevent us from building with -Werror on
5350    # armclang. Tracked in #7198.
5351    scripts/config.py unset MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
5352    scripts/config.py set MBEDTLS_HAVE_ASM
5353
5354    config_block_cipher_no_decrypt 1
5355
5356    # test AESCE baremetal build
5357    scripts/config.py set MBEDTLS_AESCE_C
5358    msg "build: default config + BLOCK_CIPHER_NO_DECRYPT with AESCE"
5359    armc6_build_test "-O1 --target=aarch64-arm-none-eabi -march=armv8-a+crypto -Werror -Wall -Wextra"
5360
5361    # Make sure we don't have mbedtls_xxx_setkey_dec in AES/ARIA/CAMELLIA
5362    not grep mbedtls_aes_setkey_dec library/aes.o
5363    not grep mbedtls_aria_setkey_dec library/aria.o
5364    not grep mbedtls_camellia_setkey_dec library/camellia.o
5365    # Make sure we don't have mbedtls_internal_aes_decrypt in AES
5366    not grep mbedtls_internal_aes_decrypt library/aes.o
5367    # Make sure we don't have mbedtls_aesce_inverse_key and aesce_decrypt_block in AESCE
5368    not grep mbedtls_aesce_inverse_key library/aesce.o
5369    not grep aesce_decrypt_block library/aesce.o
5370}
5371
5372component_test_ctr_drbg_aes_256_sha_256 () {
5373    msg "build: full + MBEDTLS_ENTROPY_FORCE_SHA256 (ASan build)"
5374    scripts/config.py full
5375    scripts/config.py unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
5376    scripts/config.py set MBEDTLS_ENTROPY_FORCE_SHA256
5377    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
5378    make
5379
5380    msg "test: full + MBEDTLS_ENTROPY_FORCE_SHA256 (ASan build)"
5381    make test
5382}
5383
5384component_test_ctr_drbg_aes_128_sha_512 () {
5385    msg "build: full + MBEDTLS_CTR_DRBG_USE_128_BIT_KEY (ASan build)"
5386    scripts/config.py full
5387    scripts/config.py unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
5388    scripts/config.py set MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
5389    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
5390    make
5391
5392    msg "test: full + MBEDTLS_CTR_DRBG_USE_128_BIT_KEY (ASan build)"
5393    make test
5394}
5395
5396component_test_ctr_drbg_aes_128_sha_256 () {
5397    msg "build: full + MBEDTLS_CTR_DRBG_USE_128_BIT_KEY + MBEDTLS_ENTROPY_FORCE_SHA256 (ASan build)"
5398    scripts/config.py full
5399    scripts/config.py unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
5400    scripts/config.py set MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
5401    scripts/config.py set MBEDTLS_ENTROPY_FORCE_SHA256
5402    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
5403    make
5404
5405    msg "test: full + MBEDTLS_CTR_DRBG_USE_128_BIT_KEY + MBEDTLS_ENTROPY_FORCE_SHA256 (ASan build)"
5406    make test
5407}
5408
5409component_test_se_default () {
5410    msg "build: default config + MBEDTLS_PSA_CRYPTO_SE_C"
5411    scripts/config.py set MBEDTLS_PSA_CRYPTO_SE_C
5412    make CC=clang CFLAGS="$ASAN_CFLAGS -Os" LDFLAGS="$ASAN_CFLAGS"
5413
5414    msg "test: default config + MBEDTLS_PSA_CRYPTO_SE_C"
5415    make test
5416}
5417
5418component_test_psa_crypto_drivers () {
5419    msg "build: full + test drivers dispatching to builtins"
5420    scripts/config.py full
5421    scripts/config.py unset MBEDTLS_PSA_CRYPTO_CONFIG
5422    loc_cflags="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST_ALL"
5423    loc_cflags="${loc_cflags} '-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/user-config-for-test.h\"'"
5424    loc_cflags="${loc_cflags} -I../tests/include -O2"
5425
5426    make CC=$ASAN_CC CFLAGS="${loc_cflags}" LDFLAGS="$ASAN_CFLAGS"
5427
5428    msg "test: full + test drivers dispatching to builtins"
5429    make test
5430}
5431
5432component_test_make_shared () {
5433    msg "build/test: make shared" # ~ 40s
5434    make SHARED=1 all check
5435    ldd programs/util/strerror | grep libmbedcrypto
5436    programs/test/dlopen_demo.sh
5437}
5438
5439component_test_cmake_shared () {
5440    msg "build/test: cmake shared" # ~ 2min
5441    cmake -DUSE_SHARED_MBEDTLS_LIBRARY=On .
5442    make
5443    ldd programs/util/strerror | grep libmbedcrypto
5444    make test
5445    programs/test/dlopen_demo.sh
5446}
5447
5448test_build_opt () {
5449    info=$1 cc=$2; shift 2
5450    $cc --version
5451    for opt in "$@"; do
5452          msg "build/test: $cc $opt, $info" # ~ 30s
5453          make CC="$cc" CFLAGS="$opt -std=c99 -pedantic -Wall -Wextra -Werror"
5454          # We're confident enough in compilers to not run _all_ the tests,
5455          # but at least run the unit tests. In particular, runs with
5456          # optimizations use inline assembly whereas runs with -O0
5457          # skip inline assembly.
5458          make test # ~30s
5459          make clean
5460    done
5461}
5462
5463# For FreeBSD we invoke the function by name so this condition is added
5464# to disable the existing test_clang_opt function for linux.
5465if [[ $(uname) != "Linux" ]]; then
5466    component_test_clang_opt () {
5467        scripts/config.py full
5468        test_build_opt 'full config' clang -O0 -Os -O2
5469    }
5470fi
5471
5472component_test_clang_latest_opt () {
5473    scripts/config.py full
5474    test_build_opt 'full config' "$CLANG_LATEST" -O0 -Os -O2
5475}
5476support_test_clang_latest_opt () {
5477    type "$CLANG_LATEST" >/dev/null 2>/dev/null
5478}
5479
5480component_test_clang_earliest_opt () {
5481    scripts/config.py full
5482    test_build_opt 'full config' "$CLANG_EARLIEST" -O0
5483}
5484support_test_clang_earliest_opt () {
5485    type "$CLANG_EARLIEST" >/dev/null 2>/dev/null
5486}
5487
5488component_test_gcc_latest_opt () {
5489    scripts/config.py full
5490    test_build_opt 'full config' "$GCC_LATEST" -O0 -Os -O2
5491}
5492support_test_gcc_latest_opt () {
5493    type "$GCC_LATEST" >/dev/null 2>/dev/null
5494}
5495
5496component_test_gcc_earliest_opt () {
5497    scripts/config.py full
5498    test_build_opt 'full config' "$GCC_EARLIEST" -O0
5499}
5500support_test_gcc_earliest_opt () {
5501    type "$GCC_EARLIEST" >/dev/null 2>/dev/null
5502}
5503
5504component_build_mbedtls_config_file () {
5505    msg "build: make with MBEDTLS_CONFIG_FILE" # ~40s
5506    scripts/config.py -w full_config.h full
5507    echo '#error "MBEDTLS_CONFIG_FILE is not working"' >"$CONFIG_H"
5508    make CFLAGS="-I '$PWD' -DMBEDTLS_CONFIG_FILE='\"full_config.h\"'"
5509    # Make sure this feature is enabled. We'll disable it in the next phase.
5510    programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
5511    make clean
5512
5513    msg "build: make with MBEDTLS_CONFIG_FILE + MBEDTLS_USER_CONFIG_FILE"
5514    # In the user config, disable one feature (for simplicity, pick a feature
5515    # that nothing else depends on).
5516    echo '#undef MBEDTLS_NIST_KW_C' >user_config.h
5517    make CFLAGS="-I '$PWD' -DMBEDTLS_CONFIG_FILE='\"full_config.h\"' -DMBEDTLS_USER_CONFIG_FILE='\"user_config.h\"'"
5518    not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
5519
5520    rm -f user_config.h full_config.h
5521}
5522
5523component_build_psa_config_file () {
5524    msg "build: make with MBEDTLS_PSA_CRYPTO_CONFIG_FILE" # ~40s
5525    scripts/config.py set MBEDTLS_PSA_CRYPTO_CONFIG
5526    cp "$CRYPTO_CONFIG_H" psa_test_config.h
5527    echo '#error "MBEDTLS_PSA_CRYPTO_CONFIG_FILE is not working"' >"$CRYPTO_CONFIG_H"
5528    make CFLAGS="-I '$PWD' -DMBEDTLS_PSA_CRYPTO_CONFIG_FILE='\"psa_test_config.h\"'"
5529    # Make sure this feature is enabled. We'll disable it in the next phase.
5530    programs/test/query_compile_time_config MBEDTLS_CMAC_C
5531    make clean
5532
5533    msg "build: make with MBEDTLS_PSA_CRYPTO_CONFIG_FILE + MBEDTLS_PSA_CRYPTO_USER_CONFIG_FILE" # ~40s
5534    # In the user config, disable one feature, which will reflect on the
5535    # mbedtls configuration so we can query it with query_compile_time_config.
5536    echo '#undef PSA_WANT_ALG_CMAC' >psa_user_config.h
5537    scripts/config.py unset MBEDTLS_CMAC_C
5538    make CFLAGS="-I '$PWD' -DMBEDTLS_PSA_CRYPTO_CONFIG_FILE='\"psa_test_config.h\"' -DMBEDTLS_PSA_CRYPTO_USER_CONFIG_FILE='\"psa_user_config.h\"'"
5539    not programs/test/query_compile_time_config MBEDTLS_CMAC_C
5540
5541    rm -f psa_test_config.h psa_user_config.h
5542}
5543
5544component_build_psa_alt_headers () {
5545    msg "build: make with PSA alt headers" # ~20s
5546
5547    # Generate alternative versions of the substitutable headers with the
5548    # same content except different include guards.
5549    make -C tests include/alt-extra/psa/crypto_platform_alt.h include/alt-extra/psa/crypto_struct_alt.h
5550
5551    # Build the library and some programs.
5552    # Don't build the fuzzers to avoid having to go through hoops to set
5553    # a correct include path for programs/fuzz/Makefile.
5554    make CFLAGS="-I ../tests/include/alt-extra -DMBEDTLS_PSA_CRYPTO_PLATFORM_FILE='\"psa/crypto_platform_alt.h\"' -DMBEDTLS_PSA_CRYPTO_STRUCT_FILE='\"psa/crypto_struct_alt.h\"'" lib
5555    make -C programs -o fuzz CFLAGS="-I ../tests/include/alt-extra -DMBEDTLS_PSA_CRYPTO_PLATFORM_FILE='\"psa/crypto_platform_alt.h\"' -DMBEDTLS_PSA_CRYPTO_STRUCT_FILE='\"psa/crypto_struct_alt.h\"'"
5556
5557    # Check that we're getting the alternative include guards and not the
5558    # original include guards.
5559    programs/test/query_included_headers | grep -x PSA_CRYPTO_PLATFORM_ALT_H
5560    programs/test/query_included_headers | grep -x PSA_CRYPTO_STRUCT_ALT_H
5561    programs/test/query_included_headers | not grep -x PSA_CRYPTO_PLATFORM_H
5562    programs/test/query_included_headers | not grep -x PSA_CRYPTO_STRUCT_H
5563}
5564
5565component_test_m32_no_asm () {
5566    # Build without assembly, so as to use portable C code (in a 32-bit
5567    # build) and not the i386-specific inline assembly.
5568    #
5569    # Note that we require gcc, because clang Asan builds fail to link for
5570    # this target (cannot find libclang_rt.lsan-i386.a - this is a known clang issue).
5571    msg "build: i386, make, gcc, no asm (ASan build)" # ~ 30s
5572    scripts/config.py full
5573    scripts/config.py unset MBEDTLS_HAVE_ASM
5574    scripts/config.py unset MBEDTLS_PADLOCK_C
5575    scripts/config.py unset MBEDTLS_AESNI_C # AESNI for 32-bit is tested in test_aesni_m32
5576    make CC=gcc CFLAGS="$ASAN_CFLAGS -m32" LDFLAGS="-m32 $ASAN_CFLAGS"
5577
5578    msg "test: i386, make, gcc, no asm (ASan build)"
5579    make test
5580}
5581support_test_m32_no_asm () {
5582    case $(uname -m) in
5583        amd64|x86_64) true;;
5584        *) false;;
5585    esac
5586}
5587
5588component_test_m32_o2 () {
5589    # Build with optimization, to use the i386 specific inline assembly
5590    # and go faster for tests.
5591    msg "build: i386, make, gcc -O2 (ASan build)" # ~ 30s
5592    scripts/config.py full
5593    scripts/config.py unset MBEDTLS_AESNI_C # AESNI for 32-bit is tested in test_aesni_m32
5594    make CC=gcc CFLAGS="$ASAN_CFLAGS -m32" LDFLAGS="-m32 $ASAN_CFLAGS"
5595
5596    msg "test: i386, make, gcc -O2 (ASan build)"
5597    make test
5598
5599    msg "test ssl-opt.sh, i386, make, gcc-O2"
5600    tests/ssl-opt.sh
5601}
5602support_test_m32_o2 () {
5603    support_test_m32_no_asm "$@"
5604}
5605
5606component_test_m32_everest () {
5607    msg "build: i386, Everest ECDH context (ASan build)" # ~ 6 min
5608    scripts/config.py set MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED
5609    scripts/config.py unset MBEDTLS_AESNI_C # AESNI for 32-bit is tested in test_aesni_m32
5610    make CC=gcc CFLAGS="$ASAN_CFLAGS -m32" LDFLAGS="-m32 $ASAN_CFLAGS"
5611
5612    msg "test: i386, Everest ECDH context - main suites (inc. selftests) (ASan build)" # ~ 50s
5613    make test
5614
5615    msg "test: i386, Everest ECDH context - ECDH-related part of ssl-opt.sh (ASan build)" # ~ 5s
5616    tests/ssl-opt.sh -f ECDH
5617
5618    msg "test: i386, Everest ECDH context - compat.sh with some ECDH ciphersuites (ASan build)" # ~ 3 min
5619    # Exclude some symmetric ciphers that are redundant here to gain time.
5620    tests/compat.sh -f ECDH -V NO -e 'ARIA\|CAMELLIA\|CHACHA'
5621}
5622support_test_m32_everest () {
5623    support_test_m32_no_asm "$@"
5624}
5625
5626component_test_mx32 () {
5627    msg "build: 64-bit ILP32, make, gcc" # ~ 30s
5628    scripts/config.py full
5629    make CC=gcc CFLAGS='-O2 -Werror -Wall -Wextra -mx32' LDFLAGS='-mx32'
5630
5631    msg "test: 64-bit ILP32, make, gcc"
5632    make test
5633}
5634support_test_mx32 () {
5635    case $(uname -m) in
5636        amd64|x86_64) true;;
5637        *) false;;
5638    esac
5639}
5640
5641component_test_min_mpi_window_size () {
5642    msg "build: Default + MBEDTLS_MPI_WINDOW_SIZE=1 (ASan build)" # ~ 10s
5643    scripts/config.py set MBEDTLS_MPI_WINDOW_SIZE 1
5644    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
5645    make
5646
5647    msg "test: MBEDTLS_MPI_WINDOW_SIZE=1 - main suites (inc. selftests) (ASan build)" # ~ 10s
5648    make test
5649}
5650
5651component_test_have_int32 () {
5652    msg "build: gcc, force 32-bit bignum limbs"
5653    scripts/config.py unset MBEDTLS_HAVE_ASM
5654    scripts/config.py unset MBEDTLS_AESNI_C
5655    scripts/config.py unset MBEDTLS_PADLOCK_C
5656    scripts/config.py unset MBEDTLS_AESCE_C
5657    make CC=gcc CFLAGS='-O2 -Werror -Wall -Wextra -DMBEDTLS_HAVE_INT32'
5658
5659    msg "test: gcc, force 32-bit bignum limbs"
5660    make test
5661}
5662
5663component_test_have_int64 () {
5664    msg "build: gcc, force 64-bit bignum limbs"
5665    scripts/config.py unset MBEDTLS_HAVE_ASM
5666    scripts/config.py unset MBEDTLS_AESNI_C
5667    scripts/config.py unset MBEDTLS_PADLOCK_C
5668    scripts/config.py unset MBEDTLS_AESCE_C
5669    make CC=gcc CFLAGS='-O2 -Werror -Wall -Wextra -DMBEDTLS_HAVE_INT64'
5670
5671    msg "test: gcc, force 64-bit bignum limbs"
5672    make test
5673}
5674
5675component_test_have_int32_cmake_new_bignum () {
5676    msg "build: gcc, force 32-bit bignum limbs, new bignum interface, test hooks (ASan build)"
5677    scripts/config.py unset MBEDTLS_HAVE_ASM
5678    scripts/config.py unset MBEDTLS_AESNI_C
5679    scripts/config.py unset MBEDTLS_PADLOCK_C
5680    scripts/config.py unset MBEDTLS_AESCE_C
5681    scripts/config.py set MBEDTLS_TEST_HOOKS
5682    scripts/config.py set MBEDTLS_ECP_WITH_MPI_UINT
5683    make CC=gcc CFLAGS="$ASAN_CFLAGS -Werror -Wall -Wextra -DMBEDTLS_HAVE_INT32" LDFLAGS="$ASAN_CFLAGS"
5684
5685    msg "test: gcc, force 32-bit bignum limbs, new bignum interface, test hooks (ASan build)"
5686    make test
5687}
5688
5689component_test_no_udbl_division () {
5690    msg "build: MBEDTLS_NO_UDBL_DIVISION native" # ~ 10s
5691    scripts/config.py full
5692    scripts/config.py set MBEDTLS_NO_UDBL_DIVISION
5693    make CFLAGS='-Werror -O1'
5694
5695    msg "test: MBEDTLS_NO_UDBL_DIVISION native" # ~ 10s
5696    make test
5697}
5698
5699component_test_no_64bit_multiplication () {
5700    msg "build: MBEDTLS_NO_64BIT_MULTIPLICATION native" # ~ 10s
5701    scripts/config.py full
5702    scripts/config.py set MBEDTLS_NO_64BIT_MULTIPLICATION
5703    make CFLAGS='-Werror -O1'
5704
5705    msg "test: MBEDTLS_NO_64BIT_MULTIPLICATION native" # ~ 10s
5706    make test
5707}
5708
5709component_test_no_strings () {
5710    msg "build: no strings" # ~10s
5711    scripts/config.py full
5712    # Disable options that activate a large amount of string constants.
5713    scripts/config.py unset MBEDTLS_DEBUG_C
5714    scripts/config.py unset MBEDTLS_ERROR_C
5715    scripts/config.py set MBEDTLS_ERROR_STRERROR_DUMMY
5716    scripts/config.py unset MBEDTLS_VERSION_FEATURES
5717    make CFLAGS='-Werror -Os'
5718
5719    msg "test: no strings" # ~ 10s
5720    make test
5721}
5722
5723component_test_no_x509_info () {
5724    msg "build: full + MBEDTLS_X509_REMOVE_INFO" # ~ 10s
5725    scripts/config.pl full
5726    scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # too slow for tests
5727    scripts/config.pl set MBEDTLS_X509_REMOVE_INFO
5728    make CFLAGS='-Werror -O2'
5729
5730    msg "test: full + MBEDTLS_X509_REMOVE_INFO" # ~ 10s
5731    make test
5732
5733    msg "test: ssl-opt.sh, full + MBEDTLS_X509_REMOVE_INFO" # ~ 1 min
5734    tests/ssl-opt.sh
5735}
5736
5737component_build_arm_none_eabi_gcc () {
5738    msg "build: ${ARM_NONE_EABI_GCC_PREFIX}gcc -O1, baremetal+debug" # ~ 10s
5739    scripts/config.py baremetal
5740    make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" LD="${ARM_NONE_EABI_GCC_PREFIX}ld" CFLAGS='-std=c99 -Werror -Wall -Wextra -O1' lib
5741
5742    msg "size: ${ARM_NONE_EABI_GCC_PREFIX}gcc -O1, baremetal+debug"
5743    ${ARM_NONE_EABI_GCC_PREFIX}size -t library/*.o
5744}
5745
5746component_build_arm_linux_gnueabi_gcc_arm5vte () {
5747    msg "build: ${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc -march=arm5vte, baremetal+debug" # ~ 10s
5748    scripts/config.py baremetal
5749    # Build for a target platform that's close to what Debian uses
5750    # for its "armel" distribution (https://wiki.debian.org/ArmEabiPort).
5751    # See https://github.com/Mbed-TLS/mbedtls/pull/2169 and comments.
5752    # Build everything including programs, see for example
5753    # https://github.com/Mbed-TLS/mbedtls/pull/3449#issuecomment-675313720
5754    make CC="${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc" AR="${ARM_LINUX_GNUEABI_GCC_PREFIX}ar" CFLAGS='-Werror -Wall -Wextra -march=armv5te -O1' LDFLAGS='-march=armv5te'
5755
5756    msg "size: ${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc -march=armv5te -O1, baremetal+debug"
5757    ${ARM_LINUX_GNUEABI_GCC_PREFIX}size -t library/*.o
5758}
5759support_build_arm_linux_gnueabi_gcc_arm5vte () {
5760    type ${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc >/dev/null 2>&1
5761}
5762
5763component_build_arm_none_eabi_gcc_arm5vte () {
5764    msg "build: ${ARM_NONE_EABI_GCC_PREFIX}gcc -march=arm5vte, baremetal+debug" # ~ 10s
5765    scripts/config.py baremetal
5766    # This is an imperfect substitute for
5767    # component_build_arm_linux_gnueabi_gcc_arm5vte
5768    # in case the gcc-arm-linux-gnueabi toolchain is not available
5769    make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" CFLAGS='-std=c99 -Werror -Wall -Wextra -march=armv5te -O1' LDFLAGS='-march=armv5te' SHELL='sh -x' lib
5770
5771    msg "size: ${ARM_NONE_EABI_GCC_PREFIX}gcc -march=armv5te -O1, baremetal+debug"
5772    ${ARM_NONE_EABI_GCC_PREFIX}size -t library/*.o
5773}
5774
5775component_build_arm_none_eabi_gcc_m0plus () {
5776    msg "build: ${ARM_NONE_EABI_GCC_PREFIX}gcc -mthumb -mcpu=cortex-m0plus, baremetal_size" # ~ 10s
5777    scripts/config.py baremetal_size
5778    make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" LD="${ARM_NONE_EABI_GCC_PREFIX}ld" CFLAGS='-std=c99 -Werror -Wall -Wextra -mthumb -mcpu=cortex-m0plus -Os' lib
5779
5780    msg "size: ${ARM_NONE_EABI_GCC_PREFIX}gcc -mthumb -mcpu=cortex-m0plus -Os, baremetal_size"
5781    ${ARM_NONE_EABI_GCC_PREFIX}size -t library/*.o
5782    for lib in library/*.a; do
5783        echo "$lib:"
5784        ${ARM_NONE_EABI_GCC_PREFIX}size -t $lib | grep TOTALS
5785    done
5786}
5787
5788component_build_arm_none_eabi_gcc_no_udbl_division () {
5789    msg "build: ${ARM_NONE_EABI_GCC_PREFIX}gcc -DMBEDTLS_NO_UDBL_DIVISION, make" # ~ 10s
5790    scripts/config.py baremetal
5791    scripts/config.py set MBEDTLS_NO_UDBL_DIVISION
5792    make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" LD="${ARM_NONE_EABI_GCC_PREFIX}ld" CFLAGS='-std=c99 -Werror -Wall -Wextra' lib
5793    echo "Checking that software 64-bit division is not required"
5794    not grep __aeabi_uldiv library/*.o
5795}
5796
5797component_build_arm_none_eabi_gcc_no_64bit_multiplication () {
5798    msg "build: ${ARM_NONE_EABI_GCC_PREFIX}gcc MBEDTLS_NO_64BIT_MULTIPLICATION, make" # ~ 10s
5799    scripts/config.py baremetal
5800    scripts/config.py set MBEDTLS_NO_64BIT_MULTIPLICATION
5801    make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" LD="${ARM_NONE_EABI_GCC_PREFIX}ld" CFLAGS='-std=c99 -Werror -O1 -march=armv6-m -mthumb' lib
5802    echo "Checking that software 64-bit multiplication is not required"
5803    not grep __aeabi_lmul library/*.o
5804}
5805
5806component_build_arm_clang_thumb () {
5807    # ~ 30s
5808
5809    scripts/config.py baremetal
5810
5811    msg "build: clang thumb 2, make"
5812    make clean
5813    make CC="clang" CFLAGS='-std=c99 -Werror -Os --target=arm-linux-gnueabihf -march=armv7-m -mthumb' lib
5814
5815    # Some Thumb 1 asm is sensitive to optimisation level, so test both -O0 and -Os
5816    msg "build: clang thumb 1 -O0, make"
5817    make clean
5818    make CC="clang" CFLAGS='-std=c99 -Werror -O0 --target=arm-linux-gnueabihf -mcpu=arm1136j-s -mthumb' lib
5819
5820    msg "build: clang thumb 1 -Os, make"
5821    make clean
5822    make CC="clang" CFLAGS='-std=c99 -Werror -Os --target=arm-linux-gnueabihf -mcpu=arm1136j-s -mthumb' lib
5823}
5824
5825component_build_armcc () {
5826    msg "build: ARM Compiler 5"
5827    scripts/config.py baremetal
5828    # armc[56] don't support SHA-512 intrinsics
5829    scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
5830
5831    # older versions of armcc/armclang don't support AESCE_C on 32-bit Arm
5832    scripts/config.py unset MBEDTLS_AESCE_C
5833
5834    # Stop armclang warning about feature detection for A64_CRYPTO.
5835    # With this enabled, the library does build correctly under armclang,
5836    # but in baremetal builds (as tested here), feature detection is
5837    # unavailable, and the user is notified via a #warning. So enabling
5838    # this feature would prevent us from building with -Werror on
5839    # armclang. Tracked in #7198.
5840    scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
5841
5842    scripts/config.py set MBEDTLS_HAVE_ASM
5843
5844    make CC="$ARMC5_CC" AR="$ARMC5_AR" WARNING_CFLAGS='--strict --c99' lib
5845
5846    msg "size: ARM Compiler 5"
5847    "$ARMC5_FROMELF" -z library/*.o
5848
5849    # Compile mostly with -O1 since some Arm inline assembly is disabled for -O0.
5850
5851    # ARM Compiler 6 - Target ARMv7-A
5852    armc6_build_test "-O1 --target=arm-arm-none-eabi -march=armv7-a"
5853
5854    # ARM Compiler 6 - Target ARMv7-M
5855    armc6_build_test "-O1 --target=arm-arm-none-eabi -march=armv7-m"
5856
5857    # ARM Compiler 6 - Target ARMv7-M+DSP
5858    armc6_build_test "-O1 --target=arm-arm-none-eabi -march=armv7-m+dsp"
5859
5860    # ARM Compiler 6 - Target ARMv8-A - AArch32
5861    armc6_build_test "-O1 --target=arm-arm-none-eabi -march=armv8.2-a"
5862
5863    # ARM Compiler 6 - Target ARMv8-M
5864    armc6_build_test "-O1 --target=arm-arm-none-eabi -march=armv8-m.main"
5865
5866    # ARM Compiler 6 - Target Cortex-M0 - no optimisation
5867    armc6_build_test "-O0 --target=arm-arm-none-eabi -mcpu=cortex-m0"
5868
5869    # ARM Compiler 6 - Target Cortex-M0
5870    armc6_build_test "-Os --target=arm-arm-none-eabi -mcpu=cortex-m0"
5871
5872    # ARM Compiler 6 - Target ARMv8.2-A - AArch64
5873    #
5874    # Re-enable MBEDTLS_AESCE_C as this should be supported by the version of armclang
5875    # that we have in our CI
5876    scripts/config.py set MBEDTLS_AESCE_C
5877    armc6_build_test "-O1 --target=aarch64-arm-none-eabi -march=armv8.2-a+crypto"
5878}
5879
5880support_build_armcc () {
5881    armc5_cc="$ARMC5_BIN_DIR/armcc"
5882    armc6_cc="$ARMC6_BIN_DIR/armclang"
5883    (check_tools "$armc5_cc" "$armc6_cc" > /dev/null 2>&1)
5884}
5885
5886component_test_tls12_only () {
5887    msg "build: default config without MBEDTLS_SSL_PROTO_TLS1_3, cmake, gcc, ASan"
5888    scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
5889    CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
5890    make
5891
5892    msg "test: main suites (inc. selftests) (ASan build)"
5893    make test
5894
5895    msg "test: ssl-opt.sh (ASan build)"
5896    tests/ssl-opt.sh
5897
5898    msg "test: compat.sh (ASan build)"
5899    tests/compat.sh
5900}
5901
5902component_test_tls13_only () {
5903    msg "build: default config without MBEDTLS_SSL_PROTO_TLS1_2"
5904    scripts/config.py set MBEDTLS_SSL_EARLY_DATA
5905    scripts/config.py set MBEDTLS_SSL_RECORD_SIZE_LIMIT
5906    make CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/tls13-only.h\"'"
5907
5908    msg "test: TLS 1.3 only, all key exchange modes enabled"
5909    make test
5910
5911    msg "ssl-opt.sh: TLS 1.3 only, all key exchange modes enabled"
5912    tests/ssl-opt.sh
5913}
5914
5915component_test_tls13_only_psk () {
5916    msg "build: TLS 1.3 only from default, only PSK key exchange mode"
5917    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED
5918    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED
5919    scripts/config.py unset MBEDTLS_ECDH_C
5920    scripts/config.py unset MBEDTLS_DHM_C
5921    scripts/config.py unset MBEDTLS_X509_CRT_PARSE_C
5922    scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT
5923    scripts/config.py unset MBEDTLS_SSL_SERVER_NAME_INDICATION
5924    scripts/config.py unset MBEDTLS_ECDSA_C
5925    scripts/config.py unset MBEDTLS_PKCS1_V21
5926    scripts/config.py unset MBEDTLS_PKCS7_C
5927    scripts/config.py set   MBEDTLS_SSL_EARLY_DATA
5928    make CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/tls13-only.h\"'"
5929
5930    msg "test_suite_ssl: TLS 1.3 only, only PSK key exchange mode enabled"
5931    cd tests; ./test_suite_ssl; cd ..
5932
5933    msg "ssl-opt.sh: TLS 1.3 only, only PSK key exchange mode enabled"
5934    tests/ssl-opt.sh
5935}
5936
5937component_test_tls13_only_ephemeral () {
5938    msg "build: TLS 1.3 only from default, only ephemeral key exchange mode"
5939    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED
5940    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED
5941    scripts/config.py unset MBEDTLS_SSL_EARLY_DATA
5942    make CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/tls13-only.h\"'"
5943
5944    msg "test_suite_ssl: TLS 1.3 only, only ephemeral key exchange mode"
5945    cd tests; ./test_suite_ssl; cd ..
5946
5947    msg "ssl-opt.sh: TLS 1.3 only, only ephemeral key exchange mode"
5948    tests/ssl-opt.sh
5949}
5950
5951component_test_tls13_only_ephemeral_ffdh () {
5952    msg "build: TLS 1.3 only from default, only ephemeral ffdh key exchange mode"
5953    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED
5954    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED
5955    scripts/config.py unset MBEDTLS_SSL_EARLY_DATA
5956    scripts/config.py unset MBEDTLS_ECDH_C
5957
5958    make CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/tls13-only.h\"'"
5959
5960    msg "test_suite_ssl: TLS 1.3 only, only ephemeral ffdh key exchange mode"
5961    cd tests; ./test_suite_ssl; cd ..
5962
5963    msg "ssl-opt.sh: TLS 1.3 only, only ephemeral ffdh key exchange mode"
5964    tests/ssl-opt.sh
5965}
5966
5967component_test_tls13_only_psk_ephemeral () {
5968    msg "build: TLS 1.3 only from default, only PSK ephemeral key exchange mode"
5969    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED
5970    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED
5971    scripts/config.py unset MBEDTLS_X509_CRT_PARSE_C
5972    scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT
5973    scripts/config.py unset MBEDTLS_SSL_SERVER_NAME_INDICATION
5974    scripts/config.py unset MBEDTLS_ECDSA_C
5975    scripts/config.py unset MBEDTLS_PKCS1_V21
5976    scripts/config.py unset MBEDTLS_PKCS7_C
5977    scripts/config.py set   MBEDTLS_SSL_EARLY_DATA
5978    make CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/tls13-only.h\"'"
5979
5980    msg "test_suite_ssl: TLS 1.3 only, only PSK ephemeral key exchange mode"
5981    cd tests; ./test_suite_ssl; cd ..
5982
5983    msg "ssl-opt.sh: TLS 1.3 only, only PSK ephemeral key exchange mode"
5984    tests/ssl-opt.sh
5985}
5986
5987component_test_tls13_only_psk_ephemeral_ffdh () {
5988    msg "build: TLS 1.3 only from default, only PSK ephemeral ffdh key exchange mode"
5989    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED
5990    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED
5991    scripts/config.py unset MBEDTLS_X509_CRT_PARSE_C
5992    scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT
5993    scripts/config.py unset MBEDTLS_SSL_SERVER_NAME_INDICATION
5994    scripts/config.py unset MBEDTLS_ECDSA_C
5995    scripts/config.py unset MBEDTLS_PKCS1_V21
5996    scripts/config.py unset MBEDTLS_PKCS7_C
5997    scripts/config.py set   MBEDTLS_SSL_EARLY_DATA
5998    scripts/config.py unset MBEDTLS_ECDH_C
5999    make CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/tls13-only.h\"'"
6000
6001    msg "test_suite_ssl: TLS 1.3 only, only PSK ephemeral ffdh key exchange mode"
6002    cd tests; ./test_suite_ssl; cd ..
6003
6004    msg "ssl-opt.sh: TLS 1.3 only, only PSK ephemeral ffdh key exchange mode"
6005    tests/ssl-opt.sh
6006}
6007
6008component_test_tls13_only_psk_all () {
6009    msg "build: TLS 1.3 only from default, without ephemeral key exchange mode"
6010    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED
6011    scripts/config.py unset MBEDTLS_X509_CRT_PARSE_C
6012    scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT
6013    scripts/config.py unset MBEDTLS_SSL_SERVER_NAME_INDICATION
6014    scripts/config.py unset MBEDTLS_ECDSA_C
6015    scripts/config.py unset MBEDTLS_PKCS1_V21
6016    scripts/config.py unset MBEDTLS_PKCS7_C
6017    scripts/config.py set   MBEDTLS_SSL_EARLY_DATA
6018    make CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/tls13-only.h\"'"
6019
6020    msg "test_suite_ssl: TLS 1.3 only, PSK and PSK ephemeral key exchange modes"
6021    cd tests; ./test_suite_ssl; cd ..
6022
6023    msg "ssl-opt.sh: TLS 1.3 only, PSK and PSK ephemeral key exchange modes"
6024    tests/ssl-opt.sh
6025}
6026
6027component_test_tls13_only_ephemeral_all () {
6028    msg "build: TLS 1.3 only from default, without PSK key exchange mode"
6029    scripts/config.py unset MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED
6030    scripts/config.py set   MBEDTLS_SSL_EARLY_DATA
6031    make CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/tls13-only.h\"'"
6032
6033    msg "test_suite_ssl: TLS 1.3 only, ephemeral and PSK ephemeral key exchange modes"
6034    cd tests; ./test_suite_ssl; cd ..
6035
6036    msg "ssl-opt.sh: TLS 1.3 only, ephemeral and PSK ephemeral key exchange modes"
6037    tests/ssl-opt.sh
6038}
6039
6040component_test_tls13_no_padding () {
6041    msg "build: default config plus early data minus padding"
6042    scripts/config.py set MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY 1
6043    scripts/config.py set MBEDTLS_SSL_EARLY_DATA
6044    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
6045    make
6046    msg "test: default config plus early data minus padding"
6047    make test
6048    msg "ssl-opt.sh (TLS 1.3 no padding)"
6049    tests/ssl-opt.sh
6050}
6051
6052component_test_tls13_no_compatibility_mode () {
6053    msg "build: default config plus early data minus middlebox compatibility mode"
6054    scripts/config.py unset MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE
6055    scripts/config.py set   MBEDTLS_SSL_EARLY_DATA
6056    CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
6057    make
6058    msg "test: default config plus early data minus middlebox compatibility mode"
6059    make test
6060    msg "ssl-opt.sh (TLS 1.3 no compatibility mode)"
6061    tests/ssl-opt.sh
6062}
6063
6064component_build_mingw () {
6065    msg "build: Windows cross build - mingw64, make (Link Library)" # ~ 30s
6066    make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra -maes -msse2 -mpclmul' WINDOWS_BUILD=1 lib programs
6067
6068    # note Make tests only builds the tests, but doesn't run them
6069    make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -maes -msse2 -mpclmul' WINDOWS_BUILD=1 tests
6070    make WINDOWS_BUILD=1 clean
6071
6072    msg "build: Windows cross build - mingw64, make (DLL)" # ~ 30s
6073    make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra -maes -msse2 -mpclmul' WINDOWS_BUILD=1 SHARED=1 lib programs
6074    make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra -maes -msse2 -mpclmul' WINDOWS_BUILD=1 SHARED=1 tests
6075    make WINDOWS_BUILD=1 clean
6076
6077    msg "build: Windows cross build - mingw64, make (Library only, default config without MBEDTLS_AESNI_C)" # ~ 30s
6078    ./scripts/config.py unset MBEDTLS_AESNI_C #
6079    make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra' WINDOWS_BUILD=1 lib
6080    make WINDOWS_BUILD=1 clean
6081}
6082support_build_mingw() {
6083    case $(i686-w64-mingw32-gcc -dumpversion 2>/dev/null) in
6084        [0-5]*|"") false;;
6085        *) true;;
6086    esac
6087}
6088
6089component_test_memsan () {
6090    msg "build: MSan (clang)" # ~ 1 min 20s
6091    scripts/config.py unset MBEDTLS_AESNI_C # memsan doesn't grok asm
6092    CC=clang cmake -D CMAKE_BUILD_TYPE:String=MemSan .
6093    make
6094
6095    msg "test: main suites (MSan)" # ~ 10s
6096    make test
6097
6098    msg "test: metatests (MSan)"
6099    tests/scripts/run-metatests.sh any msan
6100
6101    msg "program demos (MSan)" # ~20s
6102    tests/scripts/run_demos.py
6103
6104    msg "test: ssl-opt.sh (MSan)" # ~ 1 min
6105    tests/ssl-opt.sh
6106
6107    # Optional part(s)
6108
6109    if [ "$MEMORY" -gt 0 ]; then
6110        msg "test: compat.sh (MSan)" # ~ 6 min 20s
6111        tests/compat.sh
6112    fi
6113}
6114
6115component_release_test_valgrind () {
6116    msg "build: Release (clang)"
6117    # default config, in particular without MBEDTLS_USE_PSA_CRYPTO
6118    CC=clang cmake -D CMAKE_BUILD_TYPE:String=Release .
6119    make
6120
6121    msg "test: main suites, Valgrind (default config)"
6122    make memcheck
6123
6124    # Optional parts (slow; currently broken on OS X because programs don't
6125    # seem to receive signals under valgrind on OS X).
6126    # These optional parts don't run on the CI.
6127    if [ "$MEMORY" -gt 0 ]; then
6128        msg "test: ssl-opt.sh --memcheck (default config)"
6129        tests/ssl-opt.sh --memcheck
6130    fi
6131
6132    if [ "$MEMORY" -gt 1 ]; then
6133        msg "test: compat.sh --memcheck (default config)"
6134        tests/compat.sh --memcheck
6135    fi
6136
6137    if [ "$MEMORY" -gt 0 ]; then
6138        msg "test: context-info.sh --memcheck (default config)"
6139        tests/context-info.sh --memcheck
6140    fi
6141}
6142
6143component_release_test_valgrind_psa () {
6144    msg "build: Release, full (clang)"
6145    # full config, in particular with MBEDTLS_USE_PSA_CRYPTO
6146    scripts/config.py full
6147    CC=clang cmake -D CMAKE_BUILD_TYPE:String=Release .
6148    make
6149
6150    msg "test: main suites, Valgrind (full config)"
6151    make memcheck
6152}
6153
6154support_test_cmake_out_of_source () {
6155    distrib_id=""
6156    distrib_ver=""
6157    distrib_ver_minor=""
6158    distrib_ver_major=""
6159
6160    # Attempt to parse lsb-release to find out distribution and version. If not
6161    # found this should fail safe (test is supported).
6162    if [[ -f /etc/lsb-release ]]; then
6163
6164        while read -r lsb_line; do
6165            case "$lsb_line" in
6166                "DISTRIB_ID"*) distrib_id=${lsb_line/#DISTRIB_ID=};;
6167                "DISTRIB_RELEASE"*) distrib_ver=${lsb_line/#DISTRIB_RELEASE=};;
6168            esac
6169        done < /etc/lsb-release
6170
6171        distrib_ver_major="${distrib_ver%%.*}"
6172        distrib_ver="${distrib_ver#*.}"
6173        distrib_ver_minor="${distrib_ver%%.*}"
6174    fi
6175
6176    # Running the out of source CMake test on Ubuntu 16.04 using more than one
6177    # processor (as the CI does) can create a race condition whereby the build
6178    # fails to see a generated file, despite that file actually having been
6179    # generated. This problem appears to go away with 18.04 or newer, so make
6180    # the out of source tests unsupported on Ubuntu 16.04.
6181    [ "$distrib_id" != "Ubuntu" ] || [ "$distrib_ver_major" -gt 16 ]
6182}
6183
6184component_test_cmake_out_of_source () {
6185    # Remove existing generated files so that we use the ones cmake
6186    # generates
6187    make neat
6188
6189    msg "build: cmake 'out-of-source' build"
6190    MBEDTLS_ROOT_DIR="$PWD"
6191    mkdir "$OUT_OF_SOURCE_DIR"
6192    cd "$OUT_OF_SOURCE_DIR"
6193    # Note: Explicitly generate files as these are turned off in releases
6194    cmake -D CMAKE_BUILD_TYPE:String=Check -D GEN_FILES=ON "$MBEDTLS_ROOT_DIR"
6195    make
6196
6197    msg "test: cmake 'out-of-source' build"
6198    make test
6199    # Check that ssl-opt.sh can find the test programs.
6200    # Also ensure that there are no error messages such as
6201    # "No such file or directory", which would indicate that some required
6202    # file is missing (ssl-opt.sh tolerates the absence of some files so
6203    # may exit with status 0 but emit errors).
6204    ./tests/ssl-opt.sh -f 'Default' >ssl-opt.out 2>ssl-opt.err
6205    grep PASS ssl-opt.out
6206    cat ssl-opt.err >&2
6207    # If ssl-opt.err is non-empty, record an error and keep going.
6208    [ ! -s ssl-opt.err ]
6209    rm ssl-opt.out ssl-opt.err
6210    cd "$MBEDTLS_ROOT_DIR"
6211    rm -rf "$OUT_OF_SOURCE_DIR"
6212}
6213
6214component_test_cmake_as_subdirectory () {
6215    # Remove existing generated files so that we use the ones CMake
6216    # generates
6217    make neat
6218
6219    msg "build: cmake 'as-subdirectory' build"
6220    cd programs/test/cmake_subproject
6221    # Note: Explicitly generate files as these are turned off in releases
6222    cmake -D GEN_FILES=ON .
6223    make
6224    ./cmake_subproject
6225}
6226support_test_cmake_as_subdirectory () {
6227    support_test_cmake_out_of_source
6228}
6229
6230component_test_cmake_as_package () {
6231    # Remove existing generated files so that we use the ones CMake
6232    # generates
6233    make neat
6234
6235    msg "build: cmake 'as-package' build"
6236    cd programs/test/cmake_package
6237    cmake .
6238    make
6239    ./cmake_package
6240}
6241support_test_cmake_as_package () {
6242    support_test_cmake_out_of_source
6243}
6244
6245component_test_cmake_as_package_install () {
6246    # Remove existing generated files so that we use the ones CMake
6247    # generates
6248    make neat
6249
6250    msg "build: cmake 'as-installed-package' build"
6251    cd programs/test/cmake_package_install
6252    cmake .
6253    make
6254    ./cmake_package_install
6255}
6256support_test_cmake_as_package_install () {
6257    support_test_cmake_out_of_source
6258}
6259
6260component_build_cmake_custom_config_file () {
6261    # Make a copy of config file to use for the in-tree test
6262    cp "$CONFIG_H" include/mbedtls_config_in_tree_copy.h
6263
6264    MBEDTLS_ROOT_DIR="$PWD"
6265    mkdir "$OUT_OF_SOURCE_DIR"
6266    cd "$OUT_OF_SOURCE_DIR"
6267
6268    # Build once to get the generated files (which need an intact config file)
6269    cmake "$MBEDTLS_ROOT_DIR"
6270    make
6271
6272    msg "build: cmake with -DMBEDTLS_CONFIG_FILE"
6273    scripts/config.py -w full_config.h full
6274    echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
6275    cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h "$MBEDTLS_ROOT_DIR"
6276    make
6277
6278    msg "build: cmake with -DMBEDTLS_CONFIG_FILE + -DMBEDTLS_USER_CONFIG_FILE"
6279    # In the user config, disable one feature (for simplicity, pick a feature
6280    # that nothing else depends on).
6281    echo '#undef MBEDTLS_NIST_KW_C' >user_config.h
6282
6283    cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h -DMBEDTLS_USER_CONFIG_FILE=user_config.h "$MBEDTLS_ROOT_DIR"
6284    make
6285    not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
6286
6287    rm -f user_config.h full_config.h
6288
6289    cd "$MBEDTLS_ROOT_DIR"
6290    rm -rf "$OUT_OF_SOURCE_DIR"
6291
6292    # Now repeat the test for an in-tree build:
6293
6294    # Restore config for the in-tree test
6295    mv include/mbedtls_config_in_tree_copy.h "$CONFIG_H"
6296
6297    # Build once to get the generated files (which need an intact config)
6298    cmake .
6299    make
6300
6301    msg "build: cmake (in-tree) with -DMBEDTLS_CONFIG_FILE"
6302    scripts/config.py -w full_config.h full
6303    echo '#error "cmake -DMBEDTLS_CONFIG_FILE is not working."' > "$MBEDTLS_ROOT_DIR/$CONFIG_H"
6304    cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h .
6305    make
6306
6307    msg "build: cmake (in-tree) with -DMBEDTLS_CONFIG_FILE + -DMBEDTLS_USER_CONFIG_FILE"
6308    # In the user config, disable one feature (for simplicity, pick a feature
6309    # that nothing else depends on).
6310    echo '#undef MBEDTLS_NIST_KW_C' >user_config.h
6311
6312    cmake -DGEN_FILES=OFF -DMBEDTLS_CONFIG_FILE=full_config.h -DMBEDTLS_USER_CONFIG_FILE=user_config.h .
6313    make
6314    not programs/test/query_compile_time_config MBEDTLS_NIST_KW_C
6315
6316    rm -f user_config.h full_config.h
6317}
6318support_build_cmake_custom_config_file () {
6319    support_test_cmake_out_of_source
6320}
6321
6322
6323component_build_zeroize_checks () {
6324    msg "build: check for obviously wrong calls to mbedtls_platform_zeroize()"
6325
6326    scripts/config.py full
6327
6328    # Only compile - we're looking for sizeof-pointer-memaccess warnings
6329    make CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/user-config-zeroize-memset.h\"' -DMBEDTLS_TEST_DEFINES_ZEROIZE -Werror -Wsizeof-pointer-memaccess"
6330}
6331
6332
6333component_test_zeroize () {
6334    # Test that the function mbedtls_platform_zeroize() is not optimized away by
6335    # different combinations of compilers and optimization flags by using an
6336    # auxiliary GDB script. Unfortunately, GDB does not return error values to the
6337    # system in all cases that the script fails, so we must manually search the
6338    # output to check whether the pass string is present and no failure strings
6339    # were printed.
6340
6341    # Don't try to disable ASLR. We don't care about ASLR here. We do care
6342    # about a spurious message if Gdb tries and fails, so suppress that.
6343    gdb_disable_aslr=
6344    if [ -z "$(gdb -batch -nw -ex 'set disable-randomization off' 2>&1)" ]; then
6345        gdb_disable_aslr='set disable-randomization off'
6346    fi
6347
6348    for optimization_flag in -O2 -O3 -Ofast -Os; do
6349        for compiler in clang gcc; do
6350            msg "test: $compiler $optimization_flag, mbedtls_platform_zeroize()"
6351            make programs CC="$compiler" DEBUG=1 CFLAGS="$optimization_flag"
6352            gdb -ex "$gdb_disable_aslr" -x tests/scripts/test_zeroize.gdb -nw -batch -nx 2>&1 | tee test_zeroize.log
6353            grep "The buffer was correctly zeroized" test_zeroize.log
6354            not grep -i "error" test_zeroize.log
6355            rm -f test_zeroize.log
6356            make clean
6357        done
6358    done
6359}
6360
6361component_test_psa_compliance () {
6362    # The arch tests build with gcc, so require use of gcc here to link properly
6363    msg "build: make, default config (out-of-box), libmbedcrypto.a only"
6364    CC=gcc make -C library libmbedcrypto.a
6365
6366    msg "unit test: test_psa_compliance.py"
6367    CC=gcc ./tests/scripts/test_psa_compliance.py
6368}
6369
6370support_test_psa_compliance () {
6371    # psa-compliance-tests only supports CMake >= 3.10.0
6372    ver="$(cmake --version)"
6373    ver="${ver#cmake version }"
6374    ver_major="${ver%%.*}"
6375
6376    ver="${ver#*.}"
6377    ver_minor="${ver%%.*}"
6378
6379    [ "$ver_major" -eq 3 ] && [ "$ver_minor" -ge 10 ]
6380}
6381
6382component_check_code_style () {
6383    msg "Check C code style"
6384    ./scripts/code_style.py
6385}
6386
6387support_check_code_style() {
6388    case $(uncrustify --version) in
6389        *0.75.1*) true;;
6390        *) false;;
6391    esac
6392}
6393
6394component_check_python_files () {
6395    msg "Lint: Python scripts"
6396    tests/scripts/check-python-files.sh
6397}
6398
6399component_check_test_helpers () {
6400    msg "unit test: generate_test_code.py"
6401    # unittest writes out mundane stuff like number or tests run on stderr.
6402    # Our convention is to reserve stderr for actual errors, and write
6403    # harmless info on stdout so it can be suppress with --quiet.
6404    ./tests/scripts/test_generate_test_code.py 2>&1
6405
6406    msg "unit test: translate_ciphers.py"
6407    python3 -m unittest tests/scripts/translate_ciphers.py 2>&1
6408}
6409
6410
6411################################################################
6412#### Termination
6413################################################################
6414
6415post_report () {
6416    msg "Done, cleaning up"
6417    final_cleanup
6418
6419    final_report
6420}
6421
6422
6423
6424################################################################
6425#### Run all the things
6426################################################################
6427
6428# Function invoked by --error-test to test error reporting.
6429pseudo_component_error_test () {
6430    msg "Testing error reporting $error_test_i"
6431    if [ $KEEP_GOING -ne 0 ]; then
6432        echo "Expect three failing commands."
6433    fi
6434    # If the component doesn't run in a subshell, changing error_test_i to an
6435    # invalid integer will cause an error in the loop that runs this function.
6436    error_test_i=this_should_not_be_used_since_the_component_runs_in_a_subshell
6437    # Expected error: 'grep non_existent /dev/null -> 1'
6438    grep non_existent /dev/null
6439    # Expected error: '! grep -q . tests/scripts/all.sh -> 1'
6440    not grep -q . "$0"
6441    # Expected error: 'make unknown_target -> 2'
6442    make unknown_target
6443    false "this should not be executed"
6444}
6445
6446# Run one component and clean up afterwards.
6447run_component () {
6448    current_component="$1"
6449    export MBEDTLS_TEST_CONFIGURATION="$current_component"
6450
6451    # Unconditionally create a seedfile that's sufficiently long.
6452    # Do this before each component, because a previous component may
6453    # have messed it up or shortened it.
6454    local dd_cmd
6455    dd_cmd=(dd if=/dev/urandom of=./tests/seedfile bs=64 count=1)
6456    case $OSTYPE in
6457        linux*|freebsd*|openbsd*) dd_cmd+=(status=none)
6458    esac
6459    "${dd_cmd[@]}"
6460
6461    # Run the component in a subshell, with error trapping and output
6462    # redirection set up based on the relevant options.
6463    if [ $KEEP_GOING -eq 1 ]; then
6464        # We want to keep running if the subshell fails, so 'set -e' must
6465        # be off when the subshell runs.
6466        set +e
6467    fi
6468    (
6469        if [ $QUIET -eq 1 ]; then
6470            # msg() will be silenced, so just print the component name here.
6471            echo "${current_component#component_}"
6472            exec >/dev/null
6473        fi
6474        if [ $KEEP_GOING -eq 1 ]; then
6475            # Keep "set -e" off, and run an ERR trap instead to record failures.
6476            set -E
6477            trap err_trap ERR
6478        fi
6479        # The next line is what runs the component
6480        "$@"
6481        if [ $KEEP_GOING -eq 1 ]; then
6482            trap - ERR
6483            exit $last_failure_status
6484        fi
6485    )
6486    component_status=$?
6487    if [ $KEEP_GOING -eq 1 ]; then
6488        set -e
6489        if [ $component_status -ne 0 ]; then
6490            failure_count=$((failure_count + 1))
6491        fi
6492    fi
6493
6494    # Restore the build tree to a clean state.
6495    cleanup
6496    unset current_component
6497}
6498
6499# Preliminary setup
6500pre_check_environment
6501pre_initialize_variables
6502pre_parse_command_line "$@"
6503
6504setup_quiet_wrappers
6505pre_check_git
6506pre_restore_files
6507pre_back_up
6508
6509build_status=0
6510if [ $KEEP_GOING -eq 1 ]; then
6511    pre_setup_keep_going
6512fi
6513pre_prepare_outcome_file
6514pre_print_configuration
6515pre_check_tools
6516cleanup
6517if in_mbedtls_repo; then
6518    pre_generate_files
6519fi
6520
6521# Run the requested tests.
6522for ((error_test_i=1; error_test_i <= error_test; error_test_i++)); do
6523    run_component pseudo_component_error_test
6524done
6525unset error_test_i
6526for component in $RUN_COMPONENTS; do
6527    run_component "component_$component"
6528done
6529
6530# We're done.
6531post_report
6532