blob: 14d60c053a4d993cb05892c278288dfc10f51ca5 [file] [log] [blame]
Minos Galanakis6aab5b72024-07-25 14:24:37 +01001# components-configuration-crypto.sh
2#
3# Copyright The Mbed TLS Contributors
4# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5
6# This file contains the test components that are executed by all.sh
7
8################################################################
9#### Configuration Testing - Crypto
10################################################################
11
Minos Galanakis471b34c2024-07-26 15:39:24 +010012component_test_psa_crypto_key_id_encodes_owner () {
13 msg "build: full config + PSA_CRYPTO_KEY_ID_ENCODES_OWNER, cmake, gcc, ASan"
14 scripts/config.py full
15 scripts/config.py set MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
16 CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
17 make
18
19 msg "test: full config - USE_PSA_CRYPTO + PSA_CRYPTO_KEY_ID_ENCODES_OWNER, cmake, gcc, ASan"
20 make test
21}
22
23component_test_psa_assume_exclusive_buffers () {
24 msg "build: full config + MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS, cmake, gcc, ASan"
25 scripts/config.py full
26 scripts/config.py set MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS
27 CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
28 make
29
30 msg "test: full config + MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS, cmake, gcc, ASan"
31 make test
32}
33
34# check_renamed_symbols HEADER LIB
35# Check that if HEADER contains '#define MACRO ...' then MACRO is not a symbol
36# name is LIB.
37
38check_renamed_symbols () {
39 ! nm "$2" | sed 's/.* //' |
40 grep -x -F "$(sed -n 's/^ *# *define *\([A-Z_a-z][0-9A-Z_a-z]*\)..*/\1/p' "$1")"
41}
42
43component_build_psa_crypto_spm () {
44 msg "build: full config + PSA_CRYPTO_KEY_ID_ENCODES_OWNER + PSA_CRYPTO_SPM, make, gcc"
45 scripts/config.py full
46 scripts/config.py unset MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS
47 scripts/config.py set MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
48 scripts/config.py set MBEDTLS_PSA_CRYPTO_SPM
49 # We can only compile, not link, since our test and sample programs
50 # aren't equipped for the modified names used when MBEDTLS_PSA_CRYPTO_SPM
51 # is active.
52 make CC=gcc CFLAGS='-Werror -Wall -Wextra -I../tests/include/spe' lib
53
54 # Check that if a symbol is renamed by crypto_spe.h, the non-renamed
55 # version is not present.
56 echo "Checking for renamed symbols in the library"
57 check_renamed_symbols tests/include/spe/crypto_spe.h library/libmbedcrypto.a
58}
59
Minos Galanakisf78447f2024-07-26 20:49:51 +010060component_test_no_rsa_key_pair_generation () {
Minos Galanakisdc0f73a2024-07-26 20:41:42 +010061 msg "build: default config minus PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE"
62 scripts/config.py set MBEDTLS_PSA_CRYPTO_CONFIG
63 scripts/config.py unset MBEDTLS_GENPRIME
64 scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE
65 make
66
67 msg "test: default config minus PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE"
68 make test
69}
70
Minos Galanakis471b34c2024-07-26 15:39:24 +010071component_test_psa_external_rng_use_psa_crypto () {
72 msg "build: full + PSA_CRYPTO_EXTERNAL_RNG + USE_PSA_CRYPTO minus CTR_DRBG"
73 scripts/config.py full
74 scripts/config.py set MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
75 scripts/config.py set MBEDTLS_USE_PSA_CRYPTO
76 scripts/config.py unset MBEDTLS_CTR_DRBG_C
77 make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
78
79 msg "test: full + PSA_CRYPTO_EXTERNAL_RNG + USE_PSA_CRYPTO minus CTR_DRBG"
80 make test
81
82 msg "test: full + PSA_CRYPTO_EXTERNAL_RNG + USE_PSA_CRYPTO minus CTR_DRBG"
83 tests/ssl-opt.sh -f 'Default\|opaque'
84}
85
86component_test_psa_inject_entropy () {
87 msg "build: full + MBEDTLS_PSA_INJECT_ENTROPY"
88 scripts/config.py full
89 scripts/config.py set MBEDTLS_PSA_INJECT_ENTROPY
90 scripts/config.py set MBEDTLS_ENTROPY_NV_SEED
91 scripts/config.py set MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
92 scripts/config.py unset MBEDTLS_PLATFORM_NV_SEED_ALT
93 scripts/config.py unset MBEDTLS_PLATFORM_STD_NV_SEED_READ
94 scripts/config.py unset MBEDTLS_PLATFORM_STD_NV_SEED_WRITE
95 make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS '-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/user-config-for-test.h\"'" LDFLAGS="$ASAN_CFLAGS"
96
97 msg "test: full + MBEDTLS_PSA_INJECT_ENTROPY"
98 make test
99}
100
Minos Galanakisf78447f2024-07-26 20:49:51 +0100101component_full_no_pkparse_pkwrite () {
Minos Galanakis471b34c2024-07-26 15:39:24 +0100102 msg "build: full without pkparse and pkwrite"
103
104 scripts/config.py crypto_full
105 scripts/config.py unset MBEDTLS_PK_PARSE_C
106 scripts/config.py unset MBEDTLS_PK_WRITE_C
107
108 make CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
109
110 # Ensure that PK_[PARSE|WRITE]_C were not re-enabled accidentally (additive config).
111 not grep mbedtls_pk_parse_key ${BUILTIN_SRC_PATH}/pkparse.o
112 not grep mbedtls_pk_write_key_der ${BUILTIN_SRC_PATH}/pkwrite.o
113
114 msg "test: full without pkparse and pkwrite"
115 make test
116}
117
118component_test_crypto_full_md_light_only () {
119 msg "build: crypto_full with only the light subset of MD"
120 scripts/config.py crypto_full
121
122 # Disable MD
123 scripts/config.py unset MBEDTLS_MD_C
124 # Disable direct dependencies of MD_C
125 scripts/config.py unset MBEDTLS_HKDF_C
126 scripts/config.py unset MBEDTLS_HMAC_DRBG_C
127 scripts/config.py unset MBEDTLS_PKCS7_C
128 # Disable indirect dependencies of MD_C
129 scripts/config.py unset MBEDTLS_ECDSA_DETERMINISTIC # needs HMAC_DRBG
130 scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_DETERMINISTIC_ECDSA
131 # Disable things that would auto-enable MD_C
132 scripts/config.py unset MBEDTLS_PKCS5_C
133
134 # Note: MD-light is auto-enabled in build_info.h by modules that need it,
135 # which we haven't disabled, so no need to explicitly enable it.
136 make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
137
138 # Make sure we don't have the HMAC functions, but the hashing functions
139 not grep mbedtls_md_hmac ${BUILTIN_SRC_PATH}/md.o
140 grep mbedtls_md ${BUILTIN_SRC_PATH}/md.o
141
142 msg "test: crypto_full with only the light subset of MD"
143 make test
144}
145
Minos Galanakiscd5668f2024-07-26 20:36:23 +0100146component_test_full_no_cipher () {
147 msg "build: full no CIPHER"
148
149 scripts/config.py full
150 scripts/config.py unset MBEDTLS_CIPHER_C
151
152 # The built-in implementation of the following algs/key-types depends
153 # on CIPHER_C so we disable them.
154 # This does not hold for KEY_TYPE_CHACHA20 and ALG_CHACHA20_POLY1305
155 # so we keep them enabled.
156 scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_CCM_STAR_NO_TAG
157 scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_CMAC
158 scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_CBC_NO_PADDING
159 scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_CBC_PKCS7
160 scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_CFB
161 scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_CTR
162 scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_ECB_NO_PADDING
163 scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_OFB
164 scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128
165 scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_ALG_STREAM_CIPHER
166 scripts/config.py -f $CRYPTO_CONFIG_H unset PSA_WANT_KEY_TYPE_DES
167
168 # The following modules directly depends on CIPHER_C
169 scripts/config.py unset MBEDTLS_CMAC_C
170 scripts/config.py unset MBEDTLS_NIST_KW_C
171
172 make
173
174 # Ensure that CIPHER_C was not re-enabled
175 not grep mbedtls_cipher_init ${BUILTIN_SRC_PATH}/cipher.o
176
177 msg "test: full no CIPHER"
178 make test
179}
180
Minos Galanakisf78447f2024-07-26 20:49:51 +0100181component_test_full_no_ccm () {
Minos Galanakis471b34c2024-07-26 15:39:24 +0100182 msg "build: full no PSA_WANT_ALG_CCM"
183
184 # Full config enables:
185 # - USE_PSA_CRYPTO so that TLS code dispatches cipher/AEAD to PSA
186 # - CRYPTO_CONFIG so that PSA_WANT config symbols are evaluated
187 scripts/config.py full
188
189 # Disable PSA_WANT_ALG_CCM so that CCM is not supported in PSA. CCM_C is still
190 # enabled, but not used from TLS since USE_PSA is set.
191 # This is helpful to ensure that TLS tests below have proper dependencies.
192 #
193 # Note: also PSA_WANT_ALG_CCM_STAR_NO_TAG is enabled, but it does not cause
194 # PSA_WANT_ALG_CCM to be re-enabled.
195 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CCM
196
197 make
198
199 msg "test: full no PSA_WANT_ALG_CCM"
200 make test
201}
202
Minos Galanakisf78447f2024-07-26 20:49:51 +0100203component_test_full_no_ccm_star_no_tag () {
Minos Galanakis471b34c2024-07-26 15:39:24 +0100204 msg "build: full no PSA_WANT_ALG_CCM_STAR_NO_TAG"
205
206 # Full config enables CRYPTO_CONFIG so that PSA_WANT config symbols are evaluated
207 scripts/config.py full
208
209 # Disable CCM_STAR_NO_TAG, which is the target of this test, as well as all
210 # other components that enable MBEDTLS_PSA_BUILTIN_CIPHER internal symbol.
211 # This basically disables all unauthenticated ciphers on the PSA side, while
212 # keeping AEADs enabled.
213 #
214 # Note: PSA_WANT_ALG_CCM is enabled, but it does not cause
215 # PSA_WANT_ALG_CCM_STAR_NO_TAG to be re-enabled.
216 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CCM_STAR_NO_TAG
217 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_STREAM_CIPHER
218 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CTR
219 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CFB
220 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_OFB
221 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_ECB_NO_PADDING
222 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CBC_NO_PADDING
223 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CBC_PKCS7
224
225 make
226
227 # Ensure MBEDTLS_PSA_BUILTIN_CIPHER was not enabled
228 not grep mbedtls_psa_cipher ${PSA_CORE_PATH}/psa_crypto_cipher.o
229
230 msg "test: full no PSA_WANT_ALG_CCM_STAR_NO_TAG"
231 make test
232}
233
234component_test_psa_collect_statuses () {
235 msg "build+test: psa_collect_statuses" # ~30s
236 scripts/config.py full
237 tests/scripts/psa_collect_statuses.py
238 # Check that psa_crypto_init() succeeded at least once
239 grep -q '^0:psa_crypto_init:' tests/statuses.log
240 rm -f tests/statuses.log
241}
242
243# Check that the specified libraries exist and are empty.
244are_empty_libraries () {
245 nm "$@" >/dev/null 2>/dev/null
246 ! nm "$@" 2>/dev/null | grep -v ':$' | grep .
247}
248
249component_build_crypto_default () {
250 msg "build: make, crypto only"
251 scripts/config.py crypto
252 make CFLAGS='-O1 -Werror'
253 are_empty_libraries library/libmbedx509.* library/libmbedtls.*
254}
255
256component_build_crypto_full () {
257 msg "build: make, crypto only, full config"
258 scripts/config.py crypto_full
259 make CFLAGS='-O1 -Werror'
260 are_empty_libraries library/libmbedx509.* library/libmbedtls.*
261}
262
263component_test_crypto_for_psa_service () {
264 msg "build: make, config for PSA crypto service"
265 scripts/config.py crypto
266 scripts/config.py set MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
267 # Disable things that are not needed for just cryptography, to
268 # reach a configuration that would be typical for a PSA cryptography
269 # service providing all implemented PSA algorithms.
270 # System stuff
271 scripts/config.py unset MBEDTLS_ERROR_C
272 scripts/config.py unset MBEDTLS_TIMING_C
273 scripts/config.py unset MBEDTLS_VERSION_FEATURES
274 # Crypto stuff with no PSA interface
275 scripts/config.py unset MBEDTLS_BASE64_C
276 # Keep MBEDTLS_CIPHER_C because psa_crypto_cipher, CCM and GCM need it.
277 scripts/config.py unset MBEDTLS_HKDF_C # PSA's HKDF is independent
278 # Keep MBEDTLS_MD_C because deterministic ECDSA needs it for HMAC_DRBG.
279 scripts/config.py unset MBEDTLS_NIST_KW_C
280 scripts/config.py unset MBEDTLS_PEM_PARSE_C
281 scripts/config.py unset MBEDTLS_PEM_WRITE_C
282 scripts/config.py unset MBEDTLS_PKCS12_C
283 scripts/config.py unset MBEDTLS_PKCS5_C
284 # MBEDTLS_PK_PARSE_C and MBEDTLS_PK_WRITE_C are actually currently needed
285 # in PSA code to work with RSA keys. We don't require users to set those:
286 # they will be reenabled in build_info.h.
287 scripts/config.py unset MBEDTLS_PK_C
288 scripts/config.py unset MBEDTLS_PK_PARSE_C
289 scripts/config.py unset MBEDTLS_PK_WRITE_C
290 make CFLAGS='-O1 -Werror' all test
291 are_empty_libraries library/libmbedx509.* library/libmbedtls.*
292}
293
294component_build_crypto_baremetal () {
295 msg "build: make, crypto only, baremetal config"
296 scripts/config.py crypto_baremetal
297 make CFLAGS="-O1 -Werror -I$PWD/tests/include/baremetal-override/"
298 are_empty_libraries library/libmbedx509.* library/libmbedtls.*
299}
300
301support_build_crypto_baremetal () {
302 support_build_baremetal "$@"
303}
304
305# depends.py family of tests
306component_test_depends_py_cipher_id () {
307 msg "test/build: depends.py cipher_id (gcc)"
308 tests/scripts/depends.py cipher_id --unset-use-psa
309}
310
311component_test_depends_py_cipher_chaining () {
312 msg "test/build: depends.py cipher_chaining (gcc)"
313 tests/scripts/depends.py cipher_chaining --unset-use-psa
314}
315
316component_test_depends_py_cipher_padding () {
317 msg "test/build: depends.py cipher_padding (gcc)"
318 tests/scripts/depends.py cipher_padding --unset-use-psa
319}
320
321component_test_depends_py_curves () {
322 msg "test/build: depends.py curves (gcc)"
323 tests/scripts/depends.py curves --unset-use-psa
324}
325
326component_test_depends_py_hashes () {
327 msg "test/build: depends.py hashes (gcc)"
328 tests/scripts/depends.py hashes --unset-use-psa
329}
330
331component_test_depends_py_kex () {
332 msg "test/build: depends.py kex (gcc)"
333 tests/scripts/depends.py kex --unset-use-psa
334}
335
336component_test_depends_py_pkalgs () {
337 msg "test/build: depends.py pkalgs (gcc)"
338 tests/scripts/depends.py pkalgs --unset-use-psa
339}
340
341# PSA equivalents of the depends.py tests
342
343component_test_depends_py_cipher_id_psa () {
344 msg "test/build: depends.py cipher_id (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
345 tests/scripts/depends.py cipher_id
346}
347
348component_test_depends_py_cipher_chaining_psa () {
349 msg "test/build: depends.py cipher_chaining (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
350 tests/scripts/depends.py cipher_chaining
351}
352
353component_test_depends_py_cipher_padding_psa () {
354 msg "test/build: depends.py cipher_padding (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
355 tests/scripts/depends.py cipher_padding
356}
357
358component_test_depends_py_curves_psa () {
359 msg "test/build: depends.py curves (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
360 tests/scripts/depends.py curves
361}
362
363component_test_depends_py_hashes_psa () {
364 msg "test/build: depends.py hashes (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
365 tests/scripts/depends.py hashes
366}
367
368component_test_depends_py_kex_psa () {
369 msg "test/build: depends.py kex (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
370 tests/scripts/depends.py kex
371}
372
373component_test_depends_py_pkalgs_psa () {
374 msg "test/build: depends.py pkalgs (gcc) with MBEDTLS_USE_PSA_CRYPTO defined"
375 tests/scripts/depends.py pkalgs
376}
377
378component_test_psa_crypto_config_ffdh_2048_only () {
379 msg "build: full config - only DH 2048"
380
381 scripts/config.py full
382
383 # Disable all DH groups other than 2048.
384 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_DH_RFC7919_3072
385 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_DH_RFC7919_4096
386 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_DH_RFC7919_6144
387 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_DH_RFC7919_8192
388
389 make CFLAGS="$ASAN_CFLAGS -Werror" LDFLAGS="$ASAN_CFLAGS"
390
391 msg "test: full config - only DH 2048"
392 make test
393
394 msg "ssl-opt: full config - only DH 2048"
395 tests/ssl-opt.sh -f "ffdh"
396}
397
398component_build_no_pk_rsa_alt_support () {
399 msg "build: !MBEDTLS_PK_RSA_ALT_SUPPORT" # ~30s
400
401 scripts/config.py full
402 scripts/config.py unset MBEDTLS_PK_RSA_ALT_SUPPORT
403 scripts/config.py set MBEDTLS_RSA_C
404 scripts/config.py set MBEDTLS_X509_CRT_WRITE_C
405
406 # Only compile - this is primarily to test for compile issues
407 make CFLAGS='-Werror -Wall -Wextra -I../tests/include/alt-dummy'
408}
409
410component_build_module_alt () {
411 msg "build: MBEDTLS_XXX_ALT" # ~30s
412 scripts/config.py full
413
414 # Disable options that are incompatible with some ALT implementations:
415 # aesni.c references mbedtls_aes_context fields directly.
416 scripts/config.py unset MBEDTLS_AESNI_C
417 scripts/config.py unset MBEDTLS_AESCE_C
418 # MBEDTLS_ECP_RESTARTABLE is documented as incompatible.
419 scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
420 # You can only have one threading implementation: alt or pthread, not both.
421 scripts/config.py unset MBEDTLS_THREADING_PTHREAD
422 # The SpecifiedECDomain parsing code accesses mbedtls_ecp_group fields
423 # directly and assumes the implementation works with partial groups.
424 scripts/config.py unset MBEDTLS_PK_PARSE_EC_EXTENDED
425 # MBEDTLS_SHA256_*ALT can't be used with MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_*
426 scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
427 scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY
428 # MBEDTLS_SHA512_*ALT can't be used with MBEDTLS_SHA512_USE_A64_CRYPTO_*
429 scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
430 scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY
431
432 # Enable all MBEDTLS_XXX_ALT for whole modules. Do not enable
433 # MBEDTLS_XXX_YYY_ALT which are for single functions.
434 scripts/config.py set-all 'MBEDTLS_([A-Z0-9]*|NIST_KW)_ALT'
435
436 # We can only compile, not link, since we don't have any implementations
437 # suitable for testing with the dummy alt headers.
438 make CFLAGS='-Werror -Wall -Wextra -I../tests/include/alt-dummy' lib
439}
440
441component_test_psa_crypto_config_accel_ecdsa () {
442 msg "build: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated ECDSA"
443
444 # Algorithms and key types to accelerate
445 loc_accel_list="ALG_ECDSA ALG_DETERMINISTIC_ECDSA \
446 $(helper_get_psa_key_type_list "ECC") \
447 $(helper_get_psa_curve_list)"
448
449 # Configure
450 # ---------
451
452 # Start from default config (no USE_PSA) + TLS 1.3
453 helper_libtestdriver1_adjust_config "default"
454
455 # Disable the module that's accelerated
456 scripts/config.py unset MBEDTLS_ECDSA_C
457
458 # Disable things that depend on it
459 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
460 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
461
462 # Build
463 # -----
464
465 # These hashes are needed for some ECDSA signature tests.
466 loc_extra_list="ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
467 ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
468
469 helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
470
471 helper_libtestdriver1_make_main "$loc_accel_list"
472
473 # Make sure this was not re-enabled by accident (additive config)
474 not grep mbedtls_ecdsa_ ${BUILTIN_SRC_PATH}/ecdsa.o
475
476 # Run the tests
477 # -------------
478
479 msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated ECDSA"
480 make test
481}
482
483component_test_psa_crypto_config_accel_ecdh () {
484 msg "build: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated ECDH"
485
486 # Algorithms and key types to accelerate
487 loc_accel_list="ALG_ECDH \
488 $(helper_get_psa_key_type_list "ECC") \
489 $(helper_get_psa_curve_list)"
490
491 # Configure
492 # ---------
493
494 # Start from default config (no USE_PSA)
495 helper_libtestdriver1_adjust_config "default"
496
497 # Disable the module that's accelerated
498 scripts/config.py unset MBEDTLS_ECDH_C
499
500 # Disable things that depend on it
501 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
502 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
503 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
504 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
505 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED
506
507 # Build
508 # -----
509
510 helper_libtestdriver1_make_drivers "$loc_accel_list"
511
512 helper_libtestdriver1_make_main "$loc_accel_list"
513
514 # Make sure this was not re-enabled by accident (additive config)
515 not grep mbedtls_ecdh_ ${BUILTIN_SRC_PATH}/ecdh.o
516
517 # Run the tests
518 # -------------
519
520 msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated ECDH"
521 make test
522}
523
524component_test_psa_crypto_config_accel_ffdh () {
525 msg "build: full with accelerated FFDH"
526
527 # Algorithms and key types to accelerate
528 loc_accel_list="ALG_FFDH \
529 $(helper_get_psa_key_type_list "DH") \
530 $(helper_get_psa_dh_group_list)"
531
532 # Configure
533 # ---------
534
535 # start with full (USE_PSA and TLS 1.3)
536 helper_libtestdriver1_adjust_config "full"
537
538 # Disable the module that's accelerated
539 scripts/config.py unset MBEDTLS_DHM_C
540
541 # Disable things that depend on it
542 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
543 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
544
545 # Build
546 # -----
547
548 helper_libtestdriver1_make_drivers "$loc_accel_list"
549
550 helper_libtestdriver1_make_main "$loc_accel_list"
551
552 # Make sure this was not re-enabled by accident (additive config)
553 not grep mbedtls_dhm_ ${BUILTIN_SRC_PATH}/dhm.o
554
555 # Run the tests
556 # -------------
557
558 msg "test: full with accelerated FFDH"
559 make test
560
561 msg "ssl-opt: full with accelerated FFDH alg"
562 tests/ssl-opt.sh -f "ffdh"
563}
564
565component_test_psa_crypto_config_reference_ffdh () {
566 msg "build: full with non-accelerated FFDH"
567
568 # Start with full (USE_PSA and TLS 1.3)
569 helper_libtestdriver1_adjust_config "full"
570
571 # Disable things that are not supported
572 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
573 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
574 make
575
576 msg "test suites: full with non-accelerated FFDH alg"
577 make test
578
579 msg "ssl-opt: full with non-accelerated FFDH alg"
580 tests/ssl-opt.sh -f "ffdh"
581}
582
Minos Galanakisf78447f2024-07-26 20:49:51 +0100583component_test_psa_crypto_config_accel_pake () {
Minos Galanakis471b34c2024-07-26 15:39:24 +0100584 msg "build: full with accelerated PAKE"
585
586 loc_accel_list="ALG_JPAKE \
587 $(helper_get_psa_key_type_list "ECC") \
588 $(helper_get_psa_curve_list)"
589
590 # Configure
591 # ---------
592
593 helper_libtestdriver1_adjust_config "full"
594
595 # Make built-in fallback not available
596 scripts/config.py unset MBEDTLS_ECJPAKE_C
597 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
598
599 # Build
600 # -----
601
602 helper_libtestdriver1_make_drivers "$loc_accel_list"
603
604 helper_libtestdriver1_make_main "$loc_accel_list"
605
606 # Make sure this was not re-enabled by accident (additive config)
607 not grep mbedtls_ecjpake_init ${BUILTIN_SRC_PATH}/ecjpake.o
608
609 # Run the tests
610 # -------------
611
612 msg "test: full with accelerated PAKE"
613 make test
614}
615
616component_test_psa_crypto_config_accel_ecc_some_key_types () {
617 msg "build: full with accelerated EC algs and some key types"
618
619 # Algorithms and key types to accelerate
620 # For key types, use an explicitly list to omit GENERATE (and DERIVE)
621 loc_accel_list="ALG_ECDSA ALG_DETERMINISTIC_ECDSA \
622 ALG_ECDH \
623 ALG_JPAKE \
624 KEY_TYPE_ECC_PUBLIC_KEY \
625 KEY_TYPE_ECC_KEY_PAIR_BASIC \
626 KEY_TYPE_ECC_KEY_PAIR_IMPORT \
627 KEY_TYPE_ECC_KEY_PAIR_EXPORT \
628 $(helper_get_psa_curve_list)"
629
630 # Configure
631 # ---------
632
633 # start with config full for maximum coverage (also enables USE_PSA)
634 helper_libtestdriver1_adjust_config "full"
635
636 # Disable modules that are accelerated - some will be re-enabled
637 scripts/config.py unset MBEDTLS_ECDSA_C
638 scripts/config.py unset MBEDTLS_ECDH_C
639 scripts/config.py unset MBEDTLS_ECJPAKE_C
640 scripts/config.py unset MBEDTLS_ECP_C
641
642 # Disable all curves - those that aren't accelerated should be re-enabled
643 helper_disable_builtin_curves
644
645 # Restartable feature is not yet supported by PSA. Once it will in
646 # the future, the following line could be removed (see issues
647 # 6061, 6332 and following ones)
648 scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
649
650 # this is not supported by the driver API yet
651 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE
652
653 # Build
654 # -----
655
656 # These hashes are needed for some ECDSA signature tests.
657 loc_extra_list="ALG_SHA_1 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
658 ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
659 helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
660
661 helper_libtestdriver1_make_main "$loc_accel_list"
662
663 # ECP should be re-enabled but not the others
664 not grep mbedtls_ecdh_ ${BUILTIN_SRC_PATH}/ecdh.o
665 not grep mbedtls_ecdsa ${BUILTIN_SRC_PATH}/ecdsa.o
666 not grep mbedtls_ecjpake ${BUILTIN_SRC_PATH}/ecjpake.o
667 grep mbedtls_ecp ${BUILTIN_SRC_PATH}/ecp.o
668
669 # Run the tests
670 # -------------
671
672 msg "test suites: full with accelerated EC algs and some key types"
673 make test
674}
675
676# Run tests with only (non-)Weierstrass accelerated
677# Common code used in:
678# - component_test_psa_crypto_config_accel_ecc_weierstrass_curves
679# - component_test_psa_crypto_config_accel_ecc_non_weierstrass_curves
680
681common_test_psa_crypto_config_accel_ecc_some_curves () {
682 weierstrass=$1
683 if [ $weierstrass -eq 1 ]; then
684 desc="Weierstrass"
685 else
686 desc="non-Weierstrass"
687 fi
688
689 msg "build: crypto_full minus PK with accelerated EC algs and $desc curves"
690
691 # Note: Curves are handled in a special way by the libtestdriver machinery,
692 # so we only want to include them in the accel list when building the main
693 # libraries, hence the use of a separate variable.
694 # Note: the following loop is a modified version of
695 # helper_get_psa_curve_list that only keeps Weierstrass families.
696 loc_weierstrass_list=""
697 loc_non_weierstrass_list=""
698 for item in $(sed -n 's/^#define PSA_WANT_\(ECC_[0-9A-Z_a-z]*\).*/\1/p' <"$CRYPTO_CONFIG_H"); do
699 case $item in
700 ECC_BRAINPOOL*|ECC_SECP*)
701 loc_weierstrass_list="$loc_weierstrass_list $item"
702 ;;
703 *)
704 loc_non_weierstrass_list="$loc_non_weierstrass_list $item"
705 ;;
706 esac
707 done
708 if [ $weierstrass -eq 1 ]; then
709 loc_curve_list=$loc_weierstrass_list
710 else
711 loc_curve_list=$loc_non_weierstrass_list
712 fi
713
714 # Algorithms and key types to accelerate
715 loc_accel_list="ALG_ECDSA ALG_DETERMINISTIC_ECDSA \
716 ALG_ECDH \
717 ALG_JPAKE \
718 $(helper_get_psa_key_type_list "ECC") \
719 $loc_curve_list"
720
721 # Configure
722 # ---------
723
724 # Start with config crypto_full and remove PK_C:
725 # that's what's supported now, see docs/driver-only-builds.md.
726 helper_libtestdriver1_adjust_config "crypto_full"
727 scripts/config.py unset MBEDTLS_PK_C
728 scripts/config.py unset MBEDTLS_PK_PARSE_C
729 scripts/config.py unset MBEDTLS_PK_WRITE_C
730
731 # Disable modules that are accelerated - some will be re-enabled
732 scripts/config.py unset MBEDTLS_ECDSA_C
733 scripts/config.py unset MBEDTLS_ECDH_C
734 scripts/config.py unset MBEDTLS_ECJPAKE_C
735 scripts/config.py unset MBEDTLS_ECP_C
736
737 # Disable all curves - those that aren't accelerated should be re-enabled
738 helper_disable_builtin_curves
739
740 # Restartable feature is not yet supported by PSA. Once it will in
741 # the future, the following line could be removed (see issues
742 # 6061, 6332 and following ones)
743 scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
744
745 # this is not supported by the driver API yet
746 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE
747
748 # Build
749 # -----
750
751 # These hashes are needed for some ECDSA signature tests.
752 loc_extra_list="ALG_SHA_1 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
753 ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
754 helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
755
756 helper_libtestdriver1_make_main "$loc_accel_list"
757
758 # We expect ECDH to be re-enabled for the missing curves
759 grep mbedtls_ecdh_ ${BUILTIN_SRC_PATH}/ecdh.o
760 # We expect ECP to be re-enabled, however the parts specific to the
761 # families of curves that are accelerated should be ommited.
762 # - functions with mxz in the name are specific to Montgomery curves
763 # - ecp_muladd is specific to Weierstrass curves
764 ##nm ${BUILTIN_SRC_PATH}/ecp.o | tee ecp.syms
765 if [ $weierstrass -eq 1 ]; then
766 not grep mbedtls_ecp_muladd ${BUILTIN_SRC_PATH}/ecp.o
767 grep mxz ${BUILTIN_SRC_PATH}/ecp.o
768 else
769 grep mbedtls_ecp_muladd ${BUILTIN_SRC_PATH}/ecp.o
770 not grep mxz ${BUILTIN_SRC_PATH}/ecp.o
771 fi
772 # We expect ECDSA and ECJPAKE to be re-enabled only when
773 # Weierstrass curves are not accelerated
774 if [ $weierstrass -eq 1 ]; then
775 not grep mbedtls_ecdsa ${BUILTIN_SRC_PATH}/ecdsa.o
776 not grep mbedtls_ecjpake ${BUILTIN_SRC_PATH}/ecjpake.o
777 else
778 grep mbedtls_ecdsa ${BUILTIN_SRC_PATH}/ecdsa.o
779 grep mbedtls_ecjpake ${BUILTIN_SRC_PATH}/ecjpake.o
780 fi
781
782 # Run the tests
783 # -------------
784
785 msg "test suites: crypto_full minus PK with accelerated EC algs and $desc curves"
786 make test
787}
788
789component_test_psa_crypto_config_accel_ecc_weierstrass_curves () {
790 common_test_psa_crypto_config_accel_ecc_some_curves 1
791}
792
793component_test_psa_crypto_config_accel_ecc_non_weierstrass_curves () {
794 common_test_psa_crypto_config_accel_ecc_some_curves 0
795}
796
797# Auxiliary function to build config for all EC based algorithms (EC-JPAKE,
798# ECDH, ECDSA) with and without drivers.
799# The input parameter is a boolean value which indicates:
800# - 0 keep built-in EC algs,
801# - 1 exclude built-in EC algs (driver only).
802#
803# This is used by the two following components to ensure they always use the
804# same config, except for the use of driver or built-in EC algorithms:
805# - component_test_psa_crypto_config_accel_ecc_ecp_light_only;
806# - component_test_psa_crypto_config_reference_ecc_ecp_light_only.
807# This supports comparing their test coverage with analyze_outcomes.py.
808
809config_psa_crypto_config_ecp_light_only () {
810 driver_only="$1"
811 # start with config full for maximum coverage (also enables USE_PSA)
812 helper_libtestdriver1_adjust_config "full"
813 if [ "$driver_only" -eq 1 ]; then
814 # Disable modules that are accelerated
815 scripts/config.py unset MBEDTLS_ECDSA_C
816 scripts/config.py unset MBEDTLS_ECDH_C
817 scripts/config.py unset MBEDTLS_ECJPAKE_C
818 scripts/config.py unset MBEDTLS_ECP_C
819 fi
820
821 # Restartable feature is not yet supported by PSA. Once it will in
822 # the future, the following line could be removed (see issues
823 # 6061, 6332 and following ones)
824 scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
825}
826
827# Keep in sync with component_test_psa_crypto_config_reference_ecc_ecp_light_only
828
829component_test_psa_crypto_config_accel_ecc_ecp_light_only () {
830 msg "build: full with accelerated EC algs"
831
832 # Algorithms and key types to accelerate
833 loc_accel_list="ALG_ECDSA ALG_DETERMINISTIC_ECDSA \
834 ALG_ECDH \
835 ALG_JPAKE \
836 $(helper_get_psa_key_type_list "ECC") \
837 $(helper_get_psa_curve_list)"
838
839 # Configure
840 # ---------
841
842 # Use the same config as reference, only without built-in EC algs
843 config_psa_crypto_config_ecp_light_only 1
844
845 # Do not disable builtin curves because that support is required for:
846 # - MBEDTLS_PK_PARSE_EC_EXTENDED
847 # - MBEDTLS_PK_PARSE_EC_COMPRESSED
848
849 # Build
850 # -----
851
852 # These hashes are needed for some ECDSA signature tests.
853 loc_extra_list="ALG_SHA_1 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
854 ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
855 helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
856
857 helper_libtestdriver1_make_main "$loc_accel_list"
858
859 # Make sure any built-in EC alg was not re-enabled by accident (additive config)
860 not grep mbedtls_ecdsa_ ${BUILTIN_SRC_PATH}/ecdsa.o
861 not grep mbedtls_ecdh_ ${BUILTIN_SRC_PATH}/ecdh.o
862 not grep mbedtls_ecjpake_ ${BUILTIN_SRC_PATH}/ecjpake.o
863 not grep mbedtls_ecp_mul ${BUILTIN_SRC_PATH}/ecp.o
864
865 # Run the tests
866 # -------------
867
868 msg "test suites: full with accelerated EC algs"
869 make test
870
871 msg "ssl-opt: full with accelerated EC algs"
872 tests/ssl-opt.sh
873}
874
875# Keep in sync with component_test_psa_crypto_config_accel_ecc_ecp_light_only
876
877component_test_psa_crypto_config_reference_ecc_ecp_light_only () {
878 msg "build: MBEDTLS_PSA_CRYPTO_CONFIG with non-accelerated EC algs"
879
880 config_psa_crypto_config_ecp_light_only 0
881
882 make
883
884 msg "test suites: full with non-accelerated EC algs"
885 make test
886
887 msg "ssl-opt: full with non-accelerated EC algs"
888 tests/ssl-opt.sh
889}
890
891# This helper function is used by:
892# - component_test_psa_crypto_config_accel_ecc_no_ecp_at_all()
893# - component_test_psa_crypto_config_reference_ecc_no_ecp_at_all()
894# to ensure that both tests use the same underlying configuration when testing
895# driver's coverage with analyze_outcomes.py.
896#
897# This functions accepts 1 boolean parameter as follows:
898# - 1: building with accelerated EC algorithms (ECDSA, ECDH, ECJPAKE), therefore
899# excluding their built-in implementation as well as ECP_C & ECP_LIGHT
900# - 0: include built-in implementation of EC algorithms.
901#
902# PK_C and RSA_C are always disabled to ensure there is no remaining dependency
903# on the ECP module.
904
905config_psa_crypto_no_ecp_at_all () {
906 driver_only="$1"
907 # start with full config for maximum coverage (also enables USE_PSA)
908 helper_libtestdriver1_adjust_config "full"
909
910 if [ "$driver_only" -eq 1 ]; then
911 # Disable modules that are accelerated
912 scripts/config.py unset MBEDTLS_ECDSA_C
913 scripts/config.py unset MBEDTLS_ECDH_C
914 scripts/config.py unset MBEDTLS_ECJPAKE_C
915 # Disable ECP module (entirely)
916 scripts/config.py unset MBEDTLS_ECP_C
917 fi
918
919 # Disable all the features that auto-enable ECP_LIGHT (see build_info.h)
920 scripts/config.py unset MBEDTLS_PK_PARSE_EC_EXTENDED
921 scripts/config.py unset MBEDTLS_PK_PARSE_EC_COMPRESSED
922 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE
923
924 # Restartable feature is not yet supported by PSA. Once it will in
925 # the future, the following line could be removed (see issues
926 # 6061, 6332 and following ones)
927 scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
928}
929
930# Build and test a configuration where driver accelerates all EC algs while
931# all support and dependencies from ECP and ECP_LIGHT are removed on the library
932# side.
933#
934# Keep in sync with component_test_psa_crypto_config_reference_ecc_no_ecp_at_all()
935
936component_test_psa_crypto_config_accel_ecc_no_ecp_at_all () {
937 msg "build: full + accelerated EC algs - ECP"
938
939 # Algorithms and key types to accelerate
940 loc_accel_list="ALG_ECDSA ALG_DETERMINISTIC_ECDSA \
941 ALG_ECDH \
942 ALG_JPAKE \
943 $(helper_get_psa_key_type_list "ECC") \
944 $(helper_get_psa_curve_list)"
945
946 # Configure
947 # ---------
948
949 # Set common configurations between library's and driver's builds
950 config_psa_crypto_no_ecp_at_all 1
951 # Disable all the builtin curves. All the required algs are accelerated.
952 helper_disable_builtin_curves
953
954 # Build
955 # -----
956
957 # Things we wanted supported in libtestdriver1, but not accelerated in the main library:
958 # SHA-1 and all SHA-2/3 variants, as they are used by ECDSA deterministic.
959 loc_extra_list="ALG_SHA_1 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
960 ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
961
962 helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
963
964 helper_libtestdriver1_make_main "$loc_accel_list"
965
966 # Make sure any built-in EC alg was not re-enabled by accident (additive config)
967 not grep mbedtls_ecdsa_ ${BUILTIN_SRC_PATH}/ecdsa.o
968 not grep mbedtls_ecdh_ ${BUILTIN_SRC_PATH}/ecdh.o
969 not grep mbedtls_ecjpake_ ${BUILTIN_SRC_PATH}/ecjpake.o
970 # Also ensure that ECP module was not re-enabled
971 not grep mbedtls_ecp_ ${BUILTIN_SRC_PATH}/ecp.o
972
973 # Run the tests
974 # -------------
975
976 msg "test: full + accelerated EC algs - ECP"
977 make test
978
979 msg "ssl-opt: full + accelerated EC algs - ECP"
980 tests/ssl-opt.sh
981}
982
983# Reference function used for driver's coverage analysis in analyze_outcomes.py
984# in conjunction with component_test_psa_crypto_config_accel_ecc_no_ecp_at_all().
985# Keep in sync with its accelerated counterpart.
986
987component_test_psa_crypto_config_reference_ecc_no_ecp_at_all () {
988 msg "build: full + non accelerated EC algs"
989
990 config_psa_crypto_no_ecp_at_all 0
991
992 make
993
994 msg "test: full + non accelerated EC algs"
995 make test
996
997 msg "ssl-opt: full + non accelerated EC algs"
998 tests/ssl-opt.sh
999}
1000
1001# This is a common configuration helper used directly from:
1002# - common_test_psa_crypto_config_accel_ecc_ffdh_no_bignum
1003# - common_test_psa_crypto_config_reference_ecc_ffdh_no_bignum
1004# and indirectly from:
1005# - component_test_psa_crypto_config_accel_ecc_no_bignum
1006# - accelerate all EC algs, disable RSA and FFDH
1007# - component_test_psa_crypto_config_reference_ecc_no_bignum
1008# - this is the reference component of the above
1009# - it still disables RSA and FFDH, but it uses builtin EC algs
1010# - component_test_psa_crypto_config_accel_ecc_ffdh_no_bignum
1011# - accelerate all EC and FFDH algs, disable only RSA
1012# - component_test_psa_crypto_config_reference_ecc_ffdh_no_bignum
1013# - this is the reference component of the above
1014# - it still disables RSA, but it uses builtin EC and FFDH algs
1015#
1016# This function accepts 2 parameters:
1017# $1: a boolean value which states if we are testing an accelerated scenario
1018# or not.
1019# $2: a string value which states which components are tested. Allowed values
1020# are "ECC" or "ECC_DH".
1021
Minos Galanakisf78447f2024-07-26 20:49:51 +01001022config_psa_crypto_config_accel_ecc_ffdh_no_bignum () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001023 driver_only="$1"
1024 test_target="$2"
1025 # start with full config for maximum coverage (also enables USE_PSA)
1026 helper_libtestdriver1_adjust_config "full"
1027
1028 if [ "$driver_only" -eq 1 ]; then
1029 # Disable modules that are accelerated
1030 scripts/config.py unset MBEDTLS_ECDSA_C
1031 scripts/config.py unset MBEDTLS_ECDH_C
1032 scripts/config.py unset MBEDTLS_ECJPAKE_C
1033 # Disable ECP module (entirely)
1034 scripts/config.py unset MBEDTLS_ECP_C
1035 # Also disable bignum
1036 scripts/config.py unset MBEDTLS_BIGNUM_C
1037 fi
1038
1039 # Disable all the features that auto-enable ECP_LIGHT (see build_info.h)
1040 scripts/config.py unset MBEDTLS_PK_PARSE_EC_EXTENDED
1041 scripts/config.py unset MBEDTLS_PK_PARSE_EC_COMPRESSED
1042 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE
1043
1044 # RSA support is intentionally disabled on this test because RSA_C depends
1045 # on BIGNUM_C.
1046 scripts/config.py -f "$CRYPTO_CONFIG_H" unset-all "PSA_WANT_KEY_TYPE_RSA_[0-9A-Z_a-z]*"
1047 scripts/config.py -f "$CRYPTO_CONFIG_H" unset-all "PSA_WANT_ALG_RSA_[0-9A-Z_a-z]*"
1048 scripts/config.py unset MBEDTLS_RSA_C
1049 scripts/config.py unset MBEDTLS_PKCS1_V15
1050 scripts/config.py unset MBEDTLS_PKCS1_V21
1051 scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT
1052 # Also disable key exchanges that depend on RSA
1053 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
1054 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
1055 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
1056 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
1057 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
1058
1059 if [ "$test_target" = "ECC" ]; then
1060 # When testing ECC only, we disable FFDH support, both from builtin and
1061 # PSA sides, and also disable the key exchanges that depend on DHM.
1062 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_FFDH
1063 scripts/config.py -f "$CRYPTO_CONFIG_H" unset-all "PSA_WANT_KEY_TYPE_DH_[0-9A-Z_a-z]*"
1064 scripts/config.py -f "$CRYPTO_CONFIG_H" unset-all "PSA_WANT_DH_RFC7919_[0-9]*"
1065 scripts/config.py unset MBEDTLS_DHM_C
1066 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
1067 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
1068 else
1069 # When testing ECC and DH instead, we disable DHM and depending key
1070 # exchanges only in the accelerated build
1071 if [ "$driver_only" -eq 1 ]; then
1072 scripts/config.py unset MBEDTLS_DHM_C
1073 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
1074 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
1075 fi
1076 fi
1077
1078 # Restartable feature is not yet supported by PSA. Once it will in
1079 # the future, the following line could be removed (see issues
1080 # 6061, 6332 and following ones)
1081 scripts/config.py unset MBEDTLS_ECP_RESTARTABLE
1082}
1083
1084# Common helper used by:
1085# - component_test_psa_crypto_config_accel_ecc_no_bignum
1086# - component_test_psa_crypto_config_accel_ecc_ffdh_no_bignum
1087#
1088# The goal is to build and test accelerating either:
1089# - ECC only or
1090# - both ECC and FFDH
1091#
1092# It is meant to be used in conjunction with
1093# common_test_psa_crypto_config_reference_ecc_ffdh_no_bignum() for drivers
1094# coverage analysis in the "analyze_outcomes.py" script.
1095
1096common_test_psa_crypto_config_accel_ecc_ffdh_no_bignum () {
1097 test_target="$1"
1098
1099 # This is an internal helper to simplify text message handling
1100 if [ "$test_target" = "ECC_DH" ]; then
1101 accel_text="ECC/FFDH"
1102 removed_text="ECP - DH"
1103 else
1104 accel_text="ECC"
1105 removed_text="ECP"
1106 fi
1107
1108 msg "build: full + accelerated $accel_text algs + USE_PSA - $removed_text - BIGNUM"
1109
1110 # By default we accelerate all EC keys/algs
1111 loc_accel_list="ALG_ECDSA ALG_DETERMINISTIC_ECDSA \
1112 ALG_ECDH \
1113 ALG_JPAKE \
1114 $(helper_get_psa_key_type_list "ECC") \
1115 $(helper_get_psa_curve_list)"
1116 # Optionally we can also add DH to the list of accelerated items
1117 if [ "$test_target" = "ECC_DH" ]; then
1118 loc_accel_list="$loc_accel_list \
1119 ALG_FFDH \
1120 $(helper_get_psa_key_type_list "DH") \
1121 $(helper_get_psa_dh_group_list)"
1122 fi
1123
1124 # Configure
1125 # ---------
1126
1127 # Set common configurations between library's and driver's builds
1128 config_psa_crypto_config_accel_ecc_ffdh_no_bignum 1 "$test_target"
1129 # Disable all the builtin curves. All the required algs are accelerated.
1130 helper_disable_builtin_curves
1131
1132 # Build
1133 # -----
1134
1135 # Things we wanted supported in libtestdriver1, but not accelerated in the main library:
1136 # SHA-1 and all SHA-2/3 variants, as they are used by ECDSA deterministic.
1137 loc_extra_list="ALG_SHA_1 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
1138 ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
1139
1140 helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
1141
1142 helper_libtestdriver1_make_main "$loc_accel_list"
1143
1144 # Make sure any built-in EC alg was not re-enabled by accident (additive config)
1145 not grep mbedtls_ecdsa_ ${BUILTIN_SRC_PATH}/ecdsa.o
1146 not grep mbedtls_ecdh_ ${BUILTIN_SRC_PATH}/ecdh.o
1147 not grep mbedtls_ecjpake_ ${BUILTIN_SRC_PATH}/ecjpake.o
1148 # Also ensure that ECP, RSA, [DHM] or BIGNUM modules were not re-enabled
1149 not grep mbedtls_ecp_ ${BUILTIN_SRC_PATH}/ecp.o
1150 not grep mbedtls_rsa_ ${BUILTIN_SRC_PATH}/rsa.o
1151 not grep mbedtls_mpi_ ${BUILTIN_SRC_PATH}/bignum.o
1152 not grep mbedtls_dhm_ ${BUILTIN_SRC_PATH}/dhm.o
1153
1154 # Run the tests
1155 # -------------
1156
1157 msg "test suites: full + accelerated $accel_text algs + USE_PSA - $removed_text - DHM - BIGNUM"
1158
1159 make test
1160
1161 msg "ssl-opt: full + accelerated $accel_text algs + USE_PSA - $removed_text - BIGNUM"
1162 tests/ssl-opt.sh
1163}
1164
1165# Common helper used by:
1166# - component_test_psa_crypto_config_reference_ecc_no_bignum
1167# - component_test_psa_crypto_config_reference_ecc_ffdh_no_bignum
1168#
1169# The goal is to build and test a reference scenario (i.e. with builtin
1170# components) compared to the ones used in
1171# common_test_psa_crypto_config_accel_ecc_ffdh_no_bignum() above.
1172#
1173# It is meant to be used in conjunction with
1174# common_test_psa_crypto_config_accel_ecc_ffdh_no_bignum() for drivers'
1175# coverage analysis in "analyze_outcomes.py" script.
1176
1177common_test_psa_crypto_config_reference_ecc_ffdh_no_bignum () {
1178 test_target="$1"
1179
1180 # This is an internal helper to simplify text message handling
1181 if [ "$test_target" = "ECC_DH" ]; then
1182 accel_text="ECC/FFDH"
1183 else
1184 accel_text="ECC"
1185 fi
1186
1187 msg "build: full + non accelerated $accel_text algs + USE_PSA"
1188
1189 config_psa_crypto_config_accel_ecc_ffdh_no_bignum 0 "$test_target"
1190
1191 make
1192
1193 msg "test suites: full + non accelerated EC algs + USE_PSA"
1194 make test
1195
1196 msg "ssl-opt: full + non accelerated $accel_text algs + USE_PSA"
1197 tests/ssl-opt.sh
1198}
1199
1200component_test_psa_crypto_config_accel_ecc_no_bignum () {
1201 common_test_psa_crypto_config_accel_ecc_ffdh_no_bignum "ECC"
1202}
1203
1204component_test_psa_crypto_config_reference_ecc_no_bignum () {
1205 common_test_psa_crypto_config_reference_ecc_ffdh_no_bignum "ECC"
1206}
1207
1208component_test_psa_crypto_config_accel_ecc_ffdh_no_bignum () {
1209 common_test_psa_crypto_config_accel_ecc_ffdh_no_bignum "ECC_DH"
1210}
1211
1212component_test_psa_crypto_config_reference_ecc_ffdh_no_bignum () {
1213 common_test_psa_crypto_config_reference_ecc_ffdh_no_bignum "ECC_DH"
1214}
1215
1216# Helper for setting common configurations between:
1217# - component_test_tfm_config_p256m_driver_accel_ec()
1218# - component_test_tfm_config()
1219
1220common_tfm_config () {
1221 # Enable TF-M config
1222 cp configs/config-tfm.h "$CONFIG_H"
1223 echo "#undef MBEDTLS_PSA_CRYPTO_CONFIG_FILE" >> "$CONFIG_H"
1224 cp configs/ext/crypto_config_profile_medium.h "$CRYPTO_CONFIG_H"
1225
1226 # Other config adjustment to make the tests pass.
1227 # This should probably be adopted upstream.
1228 #
1229 # - USE_PSA_CRYPTO for PK_HAVE_ECC_KEYS
1230 echo "#define MBEDTLS_USE_PSA_CRYPTO" >> "$CONFIG_H"
1231
1232 # Config adjustment for better test coverage in our environment.
1233 # This is not needed just to build and pass tests.
1234 #
1235 # Enable filesystem I/O for the benefit of PK parse/write tests.
1236 echo "#define MBEDTLS_FS_IO" >> "$CONFIG_H"
1237}
1238
1239# Keep this in sync with component_test_tfm_config() as they are both meant
1240# to be used in analyze_outcomes.py for driver's coverage analysis.
1241
1242
1243
1244component_test_tfm_config_p256m_driver_accel_ec () {
1245 msg "build: TF-M config + p256m driver + accel ECDH(E)/ECDSA"
1246
1247 common_tfm_config
1248
1249 # Build crypto library
1250 make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS -I../tests/include/spe" LDFLAGS="$ASAN_CFLAGS"
1251
1252 # Make sure any built-in EC alg was not re-enabled by accident (additive config)
1253 not grep mbedtls_ecdsa_ ${BUILTIN_SRC_PATH}/ecdsa.o
1254 not grep mbedtls_ecdh_ ${BUILTIN_SRC_PATH}/ecdh.o
1255 not grep mbedtls_ecjpake_ ${BUILTIN_SRC_PATH}/ecjpake.o
1256 # Also ensure that ECP, RSA, DHM or BIGNUM modules were not re-enabled
1257 not grep mbedtls_ecp_ ${BUILTIN_SRC_PATH}/ecp.o
1258 not grep mbedtls_rsa_ ${BUILTIN_SRC_PATH}/rsa.o
1259 not grep mbedtls_dhm_ ${BUILTIN_SRC_PATH}/dhm.o
1260 not grep mbedtls_mpi_ ${BUILTIN_SRC_PATH}/bignum.o
1261 # Check that p256m was built
1262 grep -q p256_ecdsa_ library/libmbedcrypto.a
1263
1264 # In "config-tfm.h" we disabled CIPHER_C tweaking TF-M's configuration
1265 # files, so we want to ensure that it has not be re-enabled accidentally.
1266 not grep mbedtls_cipher ${BUILTIN_SRC_PATH}/cipher.o
1267
1268 # Run the tests
1269 msg "test: TF-M config + p256m driver + accel ECDH(E)/ECDSA"
1270 make test
1271}
1272
1273# Keep this in sync with component_test_tfm_config_p256m_driver_accel_ec() as
1274# they are both meant to be used in analyze_outcomes.py for driver's coverage
1275# analysis.
Minos Galanakisf78447f2024-07-26 20:49:51 +01001276component_test_tfm_config () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001277 common_tfm_config
1278
1279 # Disable P256M driver, which is on by default, so that analyze_outcomes
1280 # can compare this test with test_tfm_config_p256m_driver_accel_ec
1281 echo "#undef MBEDTLS_PSA_P256M_DRIVER_ENABLED" >> "$CONFIG_H"
1282
1283 msg "build: TF-M config"
1284 make CFLAGS='-Werror -Wall -Wextra -I../tests/include/spe' tests
1285
1286 # Check that p256m was not built
1287 not grep p256_ecdsa_ library/libmbedcrypto.a
1288
1289 # In "config-tfm.h" we disabled CIPHER_C tweaking TF-M's configuration
1290 # files, so we want to ensure that it has not be re-enabled accidentally.
1291 not grep mbedtls_cipher ${BUILTIN_SRC_PATH}/cipher.o
1292
1293 msg "test: TF-M config"
1294 make test
1295}
1296
1297# Common helper for component_full_without_ecdhe_ecdsa() and
1298# component_full_without_ecdhe_ecdsa_and_tls13() which:
1299# - starts from the "full" configuration minus the list of symbols passed in
1300# as 1st parameter
1301# - build
1302# - test only TLS (i.e. test_suite_tls and ssl-opt)
1303
1304# This is an helper used by:
1305# - component_test_psa_ecc_key_pair_no_derive
1306# - component_test_psa_ecc_key_pair_no_generate
1307# The goal is to test with all PSA_WANT_KEY_TYPE_xxx_KEY_PAIR_yyy symbols
1308# enabled, but one. Input arguments are as follows:
1309# - $1 is the key type under test, i.e. ECC/RSA/DH
1310# - $2 is the key option to be unset (i.e. generate, derive, etc)
Minos Galanakisf78447f2024-07-26 20:49:51 +01001311build_and_test_psa_want_key_pair_partial () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001312 key_type=$1
1313 unset_option=$2
1314 disabled_psa_want="PSA_WANT_KEY_TYPE_${key_type}_KEY_PAIR_${unset_option}"
1315
1316 msg "build: full - MBEDTLS_USE_PSA_CRYPTO - ${disabled_psa_want}"
1317 scripts/config.py full
1318 scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO
1319 scripts/config.py unset MBEDTLS_SSL_PROTO_TLS1_3
1320
1321 # All the PSA_WANT_KEY_TYPE_xxx_KEY_PAIR_yyy are enabled by default in
1322 # crypto_config.h so we just disable the one we don't want.
1323 scripts/config.py -f "$CRYPTO_CONFIG_H" unset "$disabled_psa_want"
1324
1325 make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
1326
1327 msg "test: full - MBEDTLS_USE_PSA_CRYPTO - ${disabled_psa_want}"
1328 make test
1329}
1330
Minos Galanakisf78447f2024-07-26 20:49:51 +01001331component_test_psa_ecc_key_pair_no_derive () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001332 build_and_test_psa_want_key_pair_partial "ECC" "DERIVE"
1333}
1334
Minos Galanakisf78447f2024-07-26 20:49:51 +01001335component_test_psa_ecc_key_pair_no_generate () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001336 build_and_test_psa_want_key_pair_partial "ECC" "GENERATE"
1337}
1338
1339config_psa_crypto_accel_rsa () {
1340 driver_only=$1
1341
1342 # Start from crypto_full config (no X.509, no TLS)
1343 helper_libtestdriver1_adjust_config "crypto_full"
1344
1345 if [ "$driver_only" -eq 1 ]; then
1346 # Remove RSA support and its dependencies
1347 scripts/config.py unset MBEDTLS_RSA_C
1348 scripts/config.py unset MBEDTLS_PKCS1_V15
1349 scripts/config.py unset MBEDTLS_PKCS1_V21
1350
1351 # We need PEM parsing in the test library as well to support the import
1352 # of PEM encoded RSA keys.
1353 scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_PEM_PARSE_C
1354 scripts/config.py -f "$CONFIG_TEST_DRIVER_H" set MBEDTLS_BASE64_C
1355 fi
1356}
1357
1358component_test_psa_crypto_config_accel_rsa_crypto () {
1359 msg "build: crypto_full with accelerated RSA"
1360
1361 loc_accel_list="ALG_RSA_OAEP ALG_RSA_PSS \
1362 ALG_RSA_PKCS1V15_CRYPT ALG_RSA_PKCS1V15_SIGN \
1363 KEY_TYPE_RSA_PUBLIC_KEY \
1364 KEY_TYPE_RSA_KEY_PAIR_BASIC \
1365 KEY_TYPE_RSA_KEY_PAIR_GENERATE \
1366 KEY_TYPE_RSA_KEY_PAIR_IMPORT \
1367 KEY_TYPE_RSA_KEY_PAIR_EXPORT"
1368
1369 # Configure
1370 # ---------
1371
1372 config_psa_crypto_accel_rsa 1
1373
1374 # Build
1375 # -----
1376
1377 # These hashes are needed for unit tests.
1378 loc_extra_list="ALG_SHA_1 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
1379 ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512 ALG_MD5"
1380 helper_libtestdriver1_make_drivers "$loc_accel_list" "$loc_extra_list"
1381
1382 helper_libtestdriver1_make_main "$loc_accel_list"
1383
1384 # Make sure this was not re-enabled by accident (additive config)
1385 not grep mbedtls_rsa ${BUILTIN_SRC_PATH}/rsa.o
1386
1387 # Run the tests
1388 # -------------
1389
1390 msg "test: crypto_full with accelerated RSA"
1391 make test
1392}
1393
1394component_test_psa_crypto_config_reference_rsa_crypto () {
1395 msg "build: crypto_full with non-accelerated RSA"
1396
1397 # Configure
1398 # ---------
1399 config_psa_crypto_accel_rsa 0
1400
1401 # Build
1402 # -----
1403 make
1404
1405 # Run the tests
1406 # -------------
1407 msg "test: crypto_full with non-accelerated RSA"
1408 make test
1409}
1410
1411# This is a temporary test to verify that full RSA support is present even when
1412# only one single new symbols (PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) is defined.
1413
Minos Galanakisf78447f2024-07-26 20:49:51 +01001414component_test_new_psa_want_key_pair_symbol () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001415 msg "Build: crypto config - MBEDTLS_RSA_C + PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC"
1416
1417 # Create a temporary output file unless there is already one set
1418 if [ "$MBEDTLS_TEST_OUTCOME_FILE" ]; then
1419 REMOVE_OUTCOME_ON_EXIT="no"
1420 else
1421 REMOVE_OUTCOME_ON_EXIT="yes"
1422 MBEDTLS_TEST_OUTCOME_FILE="$PWD/out.csv"
1423 export MBEDTLS_TEST_OUTCOME_FILE
1424 fi
1425
1426 # Start from crypto configuration
1427 scripts/config.py crypto
1428
1429 # Remove RSA support and its dependencies
1430 scripts/config.py unset MBEDTLS_PKCS1_V15
1431 scripts/config.py unset MBEDTLS_PKCS1_V21
1432 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
1433 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
1434 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
1435 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
1436 scripts/config.py unset MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
1437 scripts/config.py unset MBEDTLS_RSA_C
1438 scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT
1439
1440 # Enable PSA support
1441 scripts/config.py set MBEDTLS_PSA_CRYPTO_CONFIG
1442
1443 # Keep only PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC enabled in order to ensure
1444 # that proper translations is done in crypto_legacy.h.
1445 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_IMPORT
1446 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_EXPORT
1447 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE
1448
1449 make
1450
1451 msg "Test: crypto config - MBEDTLS_RSA_C + PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC"
1452 make test
1453
1454 # Parse only 1 relevant line from the outcome file, i.e. a test which is
1455 # performing RSA signature.
1456 msg "Verify that 'RSA PKCS1 Sign #1 (SHA512, 1536 bits RSA)' is PASS"
1457 cat $MBEDTLS_TEST_OUTCOME_FILE | grep 'RSA PKCS1 Sign #1 (SHA512, 1536 bits RSA)' | grep -q "PASS"
1458
1459 if [ "$REMOVE_OUTCOME_ON_EXIT" == "yes" ]; then
1460 rm $MBEDTLS_TEST_OUTCOME_FILE
1461 fi
1462}
1463
1464component_test_psa_crypto_config_accel_hash () {
1465 msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated hash"
1466
1467 loc_accel_list="ALG_MD5 ALG_RIPEMD160 ALG_SHA_1 \
1468 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
1469 ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
1470
1471 # Configure
1472 # ---------
1473
1474 # Start from default config (no USE_PSA)
1475 helper_libtestdriver1_adjust_config "default"
1476
1477 # Disable the things that are being accelerated
1478 scripts/config.py unset MBEDTLS_MD5_C
1479 scripts/config.py unset MBEDTLS_RIPEMD160_C
1480 scripts/config.py unset MBEDTLS_SHA1_C
1481 scripts/config.py unset MBEDTLS_SHA224_C
1482 scripts/config.py unset MBEDTLS_SHA256_C
1483 scripts/config.py unset MBEDTLS_SHA384_C
1484 scripts/config.py unset MBEDTLS_SHA512_C
1485 scripts/config.py unset MBEDTLS_SHA3_C
1486
1487 # Build
1488 # -----
1489
1490 helper_libtestdriver1_make_drivers "$loc_accel_list"
1491
1492 helper_libtestdriver1_make_main "$loc_accel_list"
1493
1494 # There's a risk of something getting re-enabled via config_psa.h;
1495 # make sure it did not happen. Note: it's OK for MD_C to be enabled.
1496 not grep mbedtls_md5 ${BUILTIN_SRC_PATH}/md5.o
1497 not grep mbedtls_sha1 ${BUILTIN_SRC_PATH}/sha1.o
1498 not grep mbedtls_sha256 ${BUILTIN_SRC_PATH}/sha256.o
1499 not grep mbedtls_sha512 ${BUILTIN_SRC_PATH}/sha512.o
1500 not grep mbedtls_ripemd160 ${BUILTIN_SRC_PATH}/ripemd160.o
1501
1502 # Run the tests
1503 # -------------
1504
1505 msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated hash"
1506 make test
1507}
1508
1509# Auxiliary function to build config for hashes with and without drivers
1510
1511config_psa_crypto_hash_use_psa () {
1512 driver_only="$1"
1513 # start with config full for maximum coverage (also enables USE_PSA)
1514 helper_libtestdriver1_adjust_config "full"
1515 if [ "$driver_only" -eq 1 ]; then
1516 # disable the built-in implementation of hashes
1517 scripts/config.py unset MBEDTLS_MD5_C
1518 scripts/config.py unset MBEDTLS_RIPEMD160_C
1519 scripts/config.py unset MBEDTLS_SHA1_C
1520 scripts/config.py unset MBEDTLS_SHA224_C
1521 scripts/config.py unset MBEDTLS_SHA256_C # see external RNG below
1522 scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
1523 scripts/config.py unset MBEDTLS_SHA384_C
1524 scripts/config.py unset MBEDTLS_SHA512_C
1525 scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
1526 scripts/config.py unset MBEDTLS_SHA3_C
1527 fi
1528}
1529
1530# Note that component_test_psa_crypto_config_reference_hash_use_psa
1531# is related to this component and both components need to be kept in sync.
1532# For details please see comments for component_test_psa_crypto_config_reference_hash_use_psa.
1533
1534component_test_psa_crypto_config_accel_hash_use_psa () {
1535 msg "test: full with accelerated hashes"
1536
1537 loc_accel_list="ALG_MD5 ALG_RIPEMD160 ALG_SHA_1 \
1538 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
1539 ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
1540
1541 # Configure
1542 # ---------
1543
1544 config_psa_crypto_hash_use_psa 1
1545
1546 # Build
1547 # -----
1548
1549 helper_libtestdriver1_make_drivers "$loc_accel_list"
1550
1551 helper_libtestdriver1_make_main "$loc_accel_list"
1552
1553 # There's a risk of something getting re-enabled via config_psa.h;
1554 # make sure it did not happen. Note: it's OK for MD_C to be enabled.
1555 not grep mbedtls_md5 ${BUILTIN_SRC_PATH}/md5.o
1556 not grep mbedtls_sha1 ${BUILTIN_SRC_PATH}/sha1.o
1557 not grep mbedtls_sha256 ${BUILTIN_SRC_PATH}/sha256.o
1558 not grep mbedtls_sha512 ${BUILTIN_SRC_PATH}/sha512.o
1559 not grep mbedtls_ripemd160 ${BUILTIN_SRC_PATH}/ripemd160.o
1560
1561 # Run the tests
1562 # -------------
1563
1564 msg "test: full with accelerated hashes"
1565 make test
1566
1567 # This is mostly useful so that we can later compare outcome files with
1568 # the reference config in analyze_outcomes.py, to check that the
1569 # dependency declarations in ssl-opt.sh and in TLS code are correct.
1570 msg "test: ssl-opt.sh, full with accelerated hashes"
1571 tests/ssl-opt.sh
1572
1573 # This is to make sure all ciphersuites are exercised, but we don't need
1574 # interop testing (besides, we already got some from ssl-opt.sh).
1575 msg "test: compat.sh, full with accelerated hashes"
1576 tests/compat.sh -p mbedTLS -V YES
1577}
1578
1579# This component provides reference configuration for test_psa_crypto_config_accel_hash_use_psa
1580# without accelerated hash. The outcome from both components are used by the analyze_outcomes.py
1581# script to find regression in test coverage when accelerated hash is used (tests and ssl-opt).
1582# Both components need to be kept in sync.
1583
Minos Galanakisf78447f2024-07-26 20:49:51 +01001584component_test_psa_crypto_config_reference_hash_use_psa () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001585 msg "test: full without accelerated hashes"
1586
1587 config_psa_crypto_hash_use_psa 0
1588
1589 make
1590
1591 msg "test: full without accelerated hashes"
1592 make test
1593
1594 msg "test: ssl-opt.sh, full without accelerated hashes"
1595 tests/ssl-opt.sh
1596}
1597
1598# Auxiliary function to build config for hashes with and without drivers
1599
1600config_psa_crypto_hmac_use_psa () {
1601 driver_only="$1"
1602 # start with config full for maximum coverage (also enables USE_PSA)
1603 helper_libtestdriver1_adjust_config "full"
1604
1605 if [ "$driver_only" -eq 1 ]; then
1606 # Disable MD_C in order to disable the builtin support for HMAC. MD_LIGHT
1607 # is still enabled though (for ENTROPY_C among others).
1608 scripts/config.py unset MBEDTLS_MD_C
1609 # Disable also the builtin hashes since they are supported by the driver
1610 # and MD module is able to perform PSA dispathing.
1611 scripts/config.py unset-all MBEDTLS_SHA
1612 scripts/config.py unset MBEDTLS_MD5_C
1613 scripts/config.py unset MBEDTLS_RIPEMD160_C
1614 fi
1615
1616 # Direct dependencies of MD_C. We disable them also in the reference
1617 # component to work with the same set of features.
1618 scripts/config.py unset MBEDTLS_PKCS7_C
1619 scripts/config.py unset MBEDTLS_PKCS5_C
1620 scripts/config.py unset MBEDTLS_HMAC_DRBG_C
1621 scripts/config.py unset MBEDTLS_HKDF_C
1622 # Dependencies of HMAC_DRBG
1623 scripts/config.py unset MBEDTLS_ECDSA_DETERMINISTIC
1624 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_DETERMINISTIC_ECDSA
1625}
1626
Minos Galanakisf78447f2024-07-26 20:49:51 +01001627component_test_psa_crypto_config_accel_hmac () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001628 msg "test: full with accelerated hmac"
1629
1630 loc_accel_list="ALG_HMAC KEY_TYPE_HMAC \
1631 ALG_MD5 ALG_RIPEMD160 ALG_SHA_1 \
1632 ALG_SHA_224 ALG_SHA_256 ALG_SHA_384 ALG_SHA_512 \
1633 ALG_SHA3_224 ALG_SHA3_256 ALG_SHA3_384 ALG_SHA3_512"
1634
1635 # Configure
1636 # ---------
1637
1638 config_psa_crypto_hmac_use_psa 1
1639
1640 # Build
1641 # -----
1642
1643 helper_libtestdriver1_make_drivers "$loc_accel_list"
1644
1645 helper_libtestdriver1_make_main "$loc_accel_list"
1646
1647 # Ensure that built-in support for HMAC is disabled.
1648 not grep mbedtls_md_hmac ${BUILTIN_SRC_PATH}/md.o
1649
1650 # Run the tests
1651 # -------------
1652
1653 msg "test: full with accelerated hmac"
1654 make test
1655}
1656
Minos Galanakisf78447f2024-07-26 20:49:51 +01001657component_test_psa_crypto_config_reference_hmac () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001658 msg "test: full without accelerated hmac"
1659
1660 config_psa_crypto_hmac_use_psa 0
1661
1662 make
1663
1664 msg "test: full without accelerated hmac"
1665 make test
1666}
1667
1668component_test_psa_crypto_config_accel_des () {
1669 msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated DES"
1670
1671 # Albeit this components aims at accelerating DES which should only support
1672 # CBC and ECB modes, we need to accelerate more than that otherwise DES_C
1673 # would automatically be re-enabled by "config_adjust_legacy_from_psa.c"
1674 loc_accel_list="ALG_ECB_NO_PADDING ALG_CBC_NO_PADDING ALG_CBC_PKCS7 \
1675 ALG_CTR ALG_CFB ALG_OFB ALG_XTS ALG_CMAC \
1676 KEY_TYPE_DES"
1677
1678 # Note: we cannot accelerate all ciphers' key types otherwise we would also
1679 # have to either disable CCM/GCM or accelerate them, but that's out of scope
1680 # of this component. This limitation will be addressed by #8598.
1681
1682 # Configure
1683 # ---------
1684
1685 # Start from the full config
1686 helper_libtestdriver1_adjust_config "full"
1687
1688 # Disable the things that are being accelerated
1689 scripts/config.py unset MBEDTLS_CIPHER_MODE_CBC
1690 scripts/config.py unset MBEDTLS_CIPHER_PADDING_PKCS7
1691 scripts/config.py unset MBEDTLS_CIPHER_MODE_CTR
1692 scripts/config.py unset MBEDTLS_CIPHER_MODE_CFB
1693 scripts/config.py unset MBEDTLS_CIPHER_MODE_OFB
1694 scripts/config.py unset MBEDTLS_CIPHER_MODE_XTS
1695 scripts/config.py unset MBEDTLS_DES_C
1696 scripts/config.py unset MBEDTLS_CMAC_C
1697
1698 # Build
1699 # -----
1700
1701 helper_libtestdriver1_make_drivers "$loc_accel_list"
1702
1703 helper_libtestdriver1_make_main "$loc_accel_list"
1704
1705 # Make sure this was not re-enabled by accident (additive config)
1706 not grep mbedtls_des* ${BUILTIN_SRC_PATH}/des.o
1707
1708 # Run the tests
1709 # -------------
1710
1711 msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated DES"
1712 make test
1713}
1714
1715component_test_psa_crypto_config_accel_aead () {
1716 msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated AEAD"
1717
1718 loc_accel_list="ALG_GCM ALG_CCM ALG_CHACHA20_POLY1305 \
1719 KEY_TYPE_AES KEY_TYPE_CHACHA20 KEY_TYPE_ARIA KEY_TYPE_CAMELLIA"
1720
1721 # Configure
1722 # ---------
1723
1724 # Start from full config
1725 helper_libtestdriver1_adjust_config "full"
1726
1727 # Disable things that are being accelerated
1728 scripts/config.py unset MBEDTLS_GCM_C
1729 scripts/config.py unset MBEDTLS_CCM_C
1730 scripts/config.py unset MBEDTLS_CHACHAPOLY_C
1731
1732 # Disable CCM_STAR_NO_TAG because this re-enables CCM_C.
1733 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CCM_STAR_NO_TAG
1734
1735 # Build
1736 # -----
1737
1738 helper_libtestdriver1_make_drivers "$loc_accel_list"
1739
1740 helper_libtestdriver1_make_main "$loc_accel_list"
1741
1742 # Make sure this was not re-enabled by accident (additive config)
1743 not grep mbedtls_ccm ${BUILTIN_SRC_PATH}/ccm.o
1744 not grep mbedtls_gcm ${BUILTIN_SRC_PATH}/gcm.o
1745 not grep mbedtls_chachapoly ${BUILTIN_SRC_PATH}/chachapoly.o
1746
1747 # Run the tests
1748 # -------------
1749
1750 msg "test: MBEDTLS_PSA_CRYPTO_CONFIG with accelerated AEAD"
1751 make test
1752}
1753
1754# This is a common configuration function used in:
1755# - component_test_psa_crypto_config_accel_cipher_aead_cmac
1756# - component_test_psa_crypto_config_reference_cipher_aead_cmac
1757
Minos Galanakisf78447f2024-07-26 20:49:51 +01001758common_psa_crypto_config_accel_cipher_aead_cmac () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001759 # Start from the full config
1760 helper_libtestdriver1_adjust_config "full"
1761
1762 scripts/config.py unset MBEDTLS_NIST_KW_C
1763}
1764
1765# The 2 following test components, i.e.
1766# - component_test_psa_crypto_config_accel_cipher_aead_cmac
1767# - component_test_psa_crypto_config_reference_cipher_aead_cmac
1768# are meant to be used together in analyze_outcomes.py script in order to test
1769# driver's coverage for ciphers and AEADs.
1770
1771component_test_psa_crypto_config_accel_cipher_aead_cmac () {
1772 msg "build: full config with accelerated cipher inc. AEAD and CMAC"
1773
1774 loc_accel_list="ALG_ECB_NO_PADDING ALG_CBC_NO_PADDING ALG_CBC_PKCS7 ALG_CTR ALG_CFB \
1775 ALG_OFB ALG_XTS ALG_STREAM_CIPHER ALG_CCM_STAR_NO_TAG \
1776 ALG_GCM ALG_CCM ALG_CHACHA20_POLY1305 ALG_CMAC \
1777 KEY_TYPE_DES KEY_TYPE_AES KEY_TYPE_ARIA KEY_TYPE_CHACHA20 KEY_TYPE_CAMELLIA"
1778
1779 # Configure
1780 # ---------
1781
1782 common_psa_crypto_config_accel_cipher_aead_cmac
1783
1784 # Disable the things that are being accelerated
1785 scripts/config.py unset MBEDTLS_CIPHER_MODE_CBC
1786 scripts/config.py unset MBEDTLS_CIPHER_PADDING_PKCS7
1787 scripts/config.py unset MBEDTLS_CIPHER_MODE_CTR
1788 scripts/config.py unset MBEDTLS_CIPHER_MODE_CFB
1789 scripts/config.py unset MBEDTLS_CIPHER_MODE_OFB
1790 scripts/config.py unset MBEDTLS_CIPHER_MODE_XTS
1791 scripts/config.py unset MBEDTLS_GCM_C
1792 scripts/config.py unset MBEDTLS_CCM_C
1793 scripts/config.py unset MBEDTLS_CHACHAPOLY_C
1794 scripts/config.py unset MBEDTLS_CMAC_C
1795 scripts/config.py unset MBEDTLS_DES_C
1796 scripts/config.py unset MBEDTLS_AES_C
1797 scripts/config.py unset MBEDTLS_ARIA_C
1798 scripts/config.py unset MBEDTLS_CHACHA20_C
1799 scripts/config.py unset MBEDTLS_CAMELLIA_C
1800
1801 # Disable CIPHER_C entirely as all ciphers/AEADs are accelerated and PSA
1802 # does not depend on it.
1803 scripts/config.py unset MBEDTLS_CIPHER_C
1804
1805 # Build
1806 # -----
1807
1808 helper_libtestdriver1_make_drivers "$loc_accel_list"
1809
1810 helper_libtestdriver1_make_main "$loc_accel_list"
1811
1812 # Make sure this was not re-enabled by accident (additive config)
1813 not grep mbedtls_cipher ${BUILTIN_SRC_PATH}/cipher.o
1814 not grep mbedtls_des ${BUILTIN_SRC_PATH}/des.o
1815 not grep mbedtls_aes ${BUILTIN_SRC_PATH}/aes.o
1816 not grep mbedtls_aria ${BUILTIN_SRC_PATH}/aria.o
1817 not grep mbedtls_camellia ${BUILTIN_SRC_PATH}/camellia.o
1818 not grep mbedtls_ccm ${BUILTIN_SRC_PATH}/ccm.o
1819 not grep mbedtls_gcm ${BUILTIN_SRC_PATH}/gcm.o
1820 not grep mbedtls_chachapoly ${BUILTIN_SRC_PATH}/chachapoly.o
1821 not grep mbedtls_cmac ${BUILTIN_SRC_PATH}/cmac.o
1822
1823 # Run the tests
1824 # -------------
1825
1826 msg "test: full config with accelerated cipher inc. AEAD and CMAC"
1827 make test
1828
1829 msg "ssl-opt: full config with accelerated cipher inc. AEAD and CMAC"
1830 tests/ssl-opt.sh
1831
1832 msg "compat.sh: full config with accelerated cipher inc. AEAD and CMAC"
1833 tests/compat.sh -V NO -p mbedTLS
1834}
1835
1836component_test_psa_crypto_config_reference_cipher_aead_cmac () {
1837 msg "build: full config with non-accelerated cipher inc. AEAD and CMAC"
1838 common_psa_crypto_config_accel_cipher_aead_cmac
1839
1840 make
1841
1842 msg "test: full config with non-accelerated cipher inc. AEAD and CMAC"
1843 make test
1844
1845 msg "ssl-opt: full config with non-accelerated cipher inc. AEAD and CMAC"
1846 tests/ssl-opt.sh
1847
1848 msg "compat.sh: full config with non-accelerated cipher inc. AEAD and CMAC"
1849 tests/compat.sh -V NO -p mbedTLS
1850}
1851
Minos Galanakisf78447f2024-07-26 20:49:51 +01001852common_block_cipher_dispatch () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001853 TEST_WITH_DRIVER="$1"
1854
1855 # Start from the full config
1856 helper_libtestdriver1_adjust_config "full"
1857
1858 if [ "$TEST_WITH_DRIVER" -eq 1 ]; then
1859 # Disable key types that are accelerated (there is no legacy equivalent
1860 # symbol for ECB)
1861 scripts/config.py unset MBEDTLS_AES_C
1862 scripts/config.py unset MBEDTLS_ARIA_C
1863 scripts/config.py unset MBEDTLS_CAMELLIA_C
1864 fi
1865
1866 # Disable cipher's modes that, when not accelerated, cause
1867 # legacy key types to be re-enabled in "config_adjust_legacy_from_psa.h".
1868 # Keep this also in the reference component in order to skip the same tests
1869 # that were skipped in the accelerated one.
1870 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CTR
1871 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CFB
1872 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_OFB
1873 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CBC_NO_PADDING
1874 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CBC_PKCS7
1875 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CMAC
1876 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CCM_STAR_NO_TAG
1877 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128
1878
1879 # Disable direct dependency on AES_C
1880 scripts/config.py unset MBEDTLS_NIST_KW_C
1881
1882 # Prevent the cipher module from using deprecated PSA path. The reason is
1883 # that otherwise there will be tests relying on "aes_info" (defined in
1884 # "cipher_wrap.c") whose functions are not available when AES_C is
1885 # not defined. ARIA and Camellia are not a problem in this case because
1886 # the PSA path is not tested for these key types.
1887 scripts/config.py set MBEDTLS_DEPRECATED_REMOVED
1888}
1889
1890component_test_full_block_cipher_psa_dispatch () {
1891 msg "build: full + PSA dispatch in block_cipher"
1892
1893 loc_accel_list="ALG_ECB_NO_PADDING \
1894 KEY_TYPE_AES KEY_TYPE_ARIA KEY_TYPE_CAMELLIA"
1895
1896 # Configure
1897 # ---------
1898
1899 common_block_cipher_dispatch 1
1900
1901 # Build
1902 # -----
1903
1904 helper_libtestdriver1_make_drivers "$loc_accel_list"
1905
1906 helper_libtestdriver1_make_main "$loc_accel_list"
1907
1908 # Make sure disabled components were not re-enabled by accident (additive
1909 # config)
1910 not grep mbedtls_aes_ ${BUILTIN_SRC_PATH}/aes.o
1911 not grep mbedtls_aria_ ${BUILTIN_SRC_PATH}/aria.o
1912 not grep mbedtls_camellia_ ${BUILTIN_SRC_PATH}/camellia.o
1913
1914 # Run the tests
1915 # -------------
1916
1917 msg "test: full + PSA dispatch in block_cipher"
1918 make test
1919}
1920
1921# This is the reference component of component_test_full_block_cipher_psa_dispatch
1922
1923component_test_full_block_cipher_legacy_dispatch () {
1924 msg "build: full + legacy dispatch in block_cipher"
1925
1926 common_block_cipher_dispatch 0
1927
1928 make
1929
1930 msg "test: full + legacy dispatch in block_cipher"
1931 make test
1932}
1933
Minos Galanakisf78447f2024-07-26 20:49:51 +01001934component_test_aead_chachapoly_disabled () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001935 msg "build: full minus CHACHAPOLY"
1936 scripts/config.py full
1937 scripts/config.py unset MBEDTLS_CHACHAPOLY_C
1938 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CHACHA20_POLY1305
1939 make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
1940
1941 msg "test: full minus CHACHAPOLY"
1942 make test
1943}
1944
Minos Galanakisf78447f2024-07-26 20:49:51 +01001945component_test_aead_only_ccm () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001946 msg "build: full minus CHACHAPOLY and GCM"
1947 scripts/config.py full
1948 scripts/config.py unset MBEDTLS_CHACHAPOLY_C
1949 scripts/config.py unset MBEDTLS_GCM_C
1950 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CHACHA20_POLY1305
1951 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_GCM
1952 make CC=$ASAN_CC CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS"
1953
1954 msg "test: full minus CHACHAPOLY and GCM"
1955 make test
1956}
1957
Minos Galanakisf78447f2024-07-26 20:49:51 +01001958component_test_ccm_aes_sha256 () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001959 msg "build: CCM + AES + SHA256 configuration"
1960
1961 cp "$CONFIG_TEST_DRIVER_H" "$CONFIG_H"
1962 cp configs/crypto-config-ccm-aes-sha256.h "$CRYPTO_CONFIG_H"
1963
1964 make
1965
1966 msg "test: CCM + AES + SHA256 configuration"
1967 make test
1968}
1969
1970# Test that the given .o file builds with all (valid) combinations of the given options.
1971#
1972# Syntax: build_test_config_combos FILE VALIDATOR_FUNCTION OPT1 OPT2 ...
1973#
1974# The validator function is the name of a function to validate the combination of options.
1975# It may be "" if all combinations are valid.
1976# It receives a string containing a combination of options, as passed to the compiler,
1977# e.g. "-DOPT1 -DOPT2 ...". It must return 0 iff the combination is valid, non-zero if invalid.
Minos Galanakisf78447f2024-07-26 20:49:51 +01001978build_test_config_combos () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01001979 file=$1
1980 shift
1981 validate_options=$1
1982 shift
1983 options=("$@")
1984
1985 # clear all of the options so that they can be overridden on the clang commandline
1986 for opt in "${options[@]}"; do
1987 ./scripts/config.py unset ${opt}
1988 done
1989
1990 # enter the library directory
1991 cd library
1992
1993 # The most common issue is unused variables/functions, so ensure -Wunused is set.
1994 warning_flags="-Werror -Wall -Wextra -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral -Wshadow -Wasm-operand-widths -Wunused"
1995
1996 # Extract the command generated by the Makefile to build the target file.
1997 # This ensures that we have any include paths, macro definitions, etc
1998 # that may be applied by make.
1999 # Add -fsyntax-only as we only want a syntax check and don't need to generate a file.
2000 compile_cmd="clang \$(LOCAL_CFLAGS) ${warning_flags} -fsyntax-only -c"
2001
2002 makefile=$(TMPDIR=. mktemp)
2003 deps=""
2004
2005 len=${#options[@]}
2006 source_file=../${file%.o}.c
2007
2008 targets=0
2009 echo 'include Makefile' >${makefile}
2010
2011 for ((i = 0; i < $((2**${len})); i++)); do
2012 # generate each of 2^n combinations of options
2013 # each bit of $i is used to determine if options[i] will be set or not
2014 target="t"
2015 clang_args=""
2016 for ((j = 0; j < ${len}; j++)); do
2017 if (((i >> j) & 1)); then
2018 opt=-D${options[$j]}
2019 clang_args="${clang_args} ${opt}"
2020 target="${target}${opt}"
2021 fi
2022 done
2023
2024 # if combination is not known to be invalid, add it to the makefile
2025 if [[ -z $validate_options ]] || $validate_options "${clang_args}"; then
2026 cmd="${compile_cmd} ${clang_args}"
2027 echo "${target}: ${source_file}; $cmd ${source_file}" >> ${makefile}
2028
2029 deps="${deps} ${target}"
2030 ((++targets))
2031 fi
2032 done
2033
2034 echo "build_test_config_combos: ${deps}" >> ${makefile}
2035
2036 # execute all of the commands via Make (probably in parallel)
2037 make -s -f ${makefile} build_test_config_combos
2038 echo "$targets targets checked"
2039
2040 # clean up the temporary makefile
2041 rm ${makefile}
2042}
2043
Minos Galanakisf78447f2024-07-26 20:49:51 +01002044validate_aes_config_variations () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01002045 if [[ "$1" == *"MBEDTLS_AES_USE_HARDWARE_ONLY"* ]]; then
2046 if [[ !(("$HOSTTYPE" == "aarch64" && "$1" != *"MBEDTLS_AESCE_C"*) || \
2047 ("$HOSTTYPE" == "x86_64" && "$1" != *"MBEDTLS_AESNI_C"*)) ]]; then
2048 return 1
2049 fi
2050 fi
2051 return 0
2052}
2053
Minos Galanakisf78447f2024-07-26 20:49:51 +01002054component_build_aes_variations () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01002055 # 18s - around 90ms per clang invocation on M1 Pro
2056 #
2057 # aes.o has many #if defined(...) guards that intersect in complex ways.
2058 # Test that all the combinations build cleanly.
2059
2060 MBEDTLS_ROOT_DIR="$PWD"
2061 msg "build: aes.o for all combinations of relevant config options"
2062
2063 build_test_config_combos ${BUILTIN_SRC_PATH}/aes.o validate_aes_config_variations \
2064 "MBEDTLS_AES_SETKEY_ENC_ALT" "MBEDTLS_AES_DECRYPT_ALT" \
2065 "MBEDTLS_AES_ROM_TABLES" "MBEDTLS_AES_ENCRYPT_ALT" "MBEDTLS_AES_SETKEY_DEC_ALT" \
2066 "MBEDTLS_AES_FEWER_TABLES" "MBEDTLS_AES_USE_HARDWARE_ONLY" \
2067 "MBEDTLS_AESNI_C" "MBEDTLS_AESCE_C" "MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH"
2068
2069 cd "$MBEDTLS_ROOT_DIR"
2070 msg "build: aes.o for all combinations of relevant config options + BLOCK_CIPHER_NO_DECRYPT"
2071
2072 # MBEDTLS_BLOCK_CIPHER_NO_DECRYPT is incompatible with ECB in PSA, CBC/XTS/NIST_KW/DES,
2073 # manually set or unset those configurations to check
2074 # MBEDTLS_BLOCK_CIPHER_NO_DECRYPT with various combinations in aes.o.
2075 scripts/config.py set MBEDTLS_BLOCK_CIPHER_NO_DECRYPT
2076 scripts/config.py unset MBEDTLS_CIPHER_MODE_CBC
2077 scripts/config.py unset MBEDTLS_CIPHER_MODE_XTS
2078 scripts/config.py unset MBEDTLS_DES_C
2079 scripts/config.py unset MBEDTLS_NIST_KW_C
2080 build_test_config_combos ${BUILTIN_SRC_PATH}/aes.o validate_aes_config_variations \
2081 "MBEDTLS_AES_SETKEY_ENC_ALT" "MBEDTLS_AES_DECRYPT_ALT" \
2082 "MBEDTLS_AES_ROM_TABLES" "MBEDTLS_AES_ENCRYPT_ALT" "MBEDTLS_AES_SETKEY_DEC_ALT" \
2083 "MBEDTLS_AES_FEWER_TABLES" "MBEDTLS_AES_USE_HARDWARE_ONLY" \
2084 "MBEDTLS_AESNI_C" "MBEDTLS_AESCE_C" "MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH"
2085}
2086
Minos Galanakisf78447f2024-07-26 20:49:51 +01002087support_test_aesni () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01002088 # Check that gcc targets x86_64 (we can build AESNI), and check for
2089 # AESNI support on the host (we can run AESNI).
2090 #
2091 # The name of this function is possibly slightly misleading, but needs to align
2092 # with the name of the corresponding test, component_test_aesni.
2093 #
2094 # In principle 32-bit x86 can support AESNI, but our implementation does not
2095 # support 32-bit x86, so we check for x86-64.
2096 # We can only grep /proc/cpuinfo on Linux, so this also checks for Linux
2097 (gcc -v 2>&1 | grep Target | grep -q x86_64) &&
2098 [[ "$HOSTTYPE" == "x86_64" && "$OSTYPE" == "linux-gnu" ]] &&
2099 (lscpu | grep -qw aes)
2100}
2101
2102component_test_aesni () { # ~ 60s
2103 # This tests the two AESNI implementations (intrinsics and assembly), and also the plain C
2104 # fallback. It also tests the logic that is used to select which implementation(s) to build.
2105 #
2106 # This test does not require the host to have support for AESNI (if it doesn't, the run-time
2107 # AESNI detection will fallback to the plain C implementation, so the tests will instead
2108 # exercise the plain C impl).
2109
2110 msg "build: default config with different AES implementations"
2111 scripts/config.py set MBEDTLS_AESNI_C
2112 scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
2113 scripts/config.py set MBEDTLS_HAVE_ASM
2114
2115 # test the intrinsics implementation
2116 msg "AES tests, test intrinsics"
2117 make clean
2118 make CC=gcc CFLAGS='-Werror -Wall -Wextra -mpclmul -msse2 -maes'
2119 # check that we built intrinsics - this should be used by default when supported by the compiler
2120 ./programs/test/selftest aes | grep "AESNI code" | grep -q "intrinsics"
2121
2122 # test the asm implementation
2123 msg "AES tests, test assembly"
2124 make clean
2125 make CC=gcc CFLAGS='-Werror -Wall -Wextra -mno-pclmul -mno-sse2 -mno-aes'
2126 # check that we built assembly - this should be built if the compiler does not support intrinsics
2127 ./programs/test/selftest aes | grep "AESNI code" | grep -q "assembly"
2128
2129 # test the plain C implementation
2130 scripts/config.py unset MBEDTLS_AESNI_C
2131 scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
2132 msg "AES tests, plain C"
2133 make clean
2134 make CC=gcc CFLAGS='-O2 -Werror'
2135 # check that there is no AESNI code present
2136 ./programs/test/selftest aes | not grep -q "AESNI code"
2137 not grep -q "AES note: using AESNI" ./programs/test/selftest
2138 grep -q "AES note: built-in implementation." ./programs/test/selftest
2139
2140 # test the intrinsics implementation
2141 scripts/config.py set MBEDTLS_AESNI_C
2142 scripts/config.py set MBEDTLS_AES_USE_HARDWARE_ONLY
2143 msg "AES tests, test AESNI only"
2144 make clean
2145 make CC=gcc CFLAGS='-Werror -Wall -Wextra -mpclmul -msse2 -maes'
2146 ./programs/test/selftest aes | grep -q "AES note: using AESNI"
2147 ./programs/test/selftest aes | not grep -q "AES note: built-in implementation."
2148 grep -q "AES note: using AESNI" ./programs/test/selftest
2149 not grep -q "AES note: built-in implementation." ./programs/test/selftest
2150}
2151
Minos Galanakisf78447f2024-07-26 20:49:51 +01002152component_test_sha3_variations () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01002153 msg "sha3 loop unroll variations"
2154
2155 # define minimal config sufficient to test SHA3
2156 cat > include/mbedtls/mbedtls_config.h << END
2157 #define MBEDTLS_SELF_TEST
2158 #define MBEDTLS_SHA3_C
2159END
2160
2161 msg "all loops unrolled"
2162 make clean
2163 make -C tests ../tf-psa-crypto/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"
2164 ./tf-psa-crypto/tests/test_suite_shax
2165
2166 msg "all loops rolled up"
2167 make clean
2168 make -C tests ../tf-psa-crypto/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"
2169 ./tf-psa-crypto/tests/test_suite_shax
2170}
2171
Minos Galanakisf78447f2024-07-26 20:49:51 +01002172support_test_aesni_m32 () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01002173 support_test_m32_no_asm && (lscpu | grep -qw aes)
2174}
2175
2176component_test_aesni_m32 () { # ~ 60s
2177 # This tests are duplicated from component_test_aesni for i386 target
2178 #
2179 # AESNI intrinsic code supports i386 and assembly code does not support it.
2180
2181 msg "build: default config with different AES implementations"
2182 scripts/config.py set MBEDTLS_AESNI_C
2183 scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
2184 scripts/config.py set MBEDTLS_HAVE_ASM
2185
2186 # test the intrinsics implementation with gcc
2187 msg "AES tests, test intrinsics (gcc)"
2188 make clean
2189 make CC=gcc CFLAGS='-m32 -Werror -Wall -Wextra' LDFLAGS='-m32'
2190 # check that we built intrinsics - this should be used by default when supported by the compiler
2191 ./programs/test/selftest aes | grep "AESNI code" | grep -q "intrinsics"
2192 grep -q "AES note: using AESNI" ./programs/test/selftest
2193 grep -q "AES note: built-in implementation." ./programs/test/selftest
2194 grep -q mbedtls_aesni_has_support ./programs/test/selftest
2195
2196 scripts/config.py set MBEDTLS_AESNI_C
2197 scripts/config.py set MBEDTLS_AES_USE_HARDWARE_ONLY
2198 msg "AES tests, test AESNI only"
2199 make clean
2200 make CC=gcc CFLAGS='-m32 -Werror -Wall -Wextra -mpclmul -msse2 -maes' LDFLAGS='-m32'
2201 ./programs/test/selftest aes | grep -q "AES note: using AESNI"
2202 ./programs/test/selftest aes | not grep -q "AES note: built-in implementation."
2203 grep -q "AES note: using AESNI" ./programs/test/selftest
2204 not grep -q "AES note: built-in implementation." ./programs/test/selftest
2205 not grep -q mbedtls_aesni_has_support ./programs/test/selftest
2206}
2207
2208# For timebeing, no aarch64 gcc available in CI and no arm64 CI node.
2209component_build_aes_aesce_armcc () {
2210 msg "Build: AESCE test on arm64 platform without plain C."
2211 scripts/config.py baremetal
2212
2213 # armc[56] don't support SHA-512 intrinsics
2214 scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
2215
2216 # Stop armclang warning about feature detection for A64_CRYPTO.
2217 # With this enabled, the library does build correctly under armclang,
2218 # but in baremetal builds (as tested here), feature detection is
2219 # unavailable, and the user is notified via a #warning. So enabling
2220 # this feature would prevent us from building with -Werror on
2221 # armclang. Tracked in #7198.
2222 scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
2223 scripts/config.py set MBEDTLS_HAVE_ASM
2224
2225 msg "AESCE, build with default configuration."
2226 scripts/config.py set MBEDTLS_AESCE_C
2227 scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
2228 armc6_build_test "-O1 --target=aarch64-arm-none-eabi -march=armv8-a+crypto"
2229
2230 msg "AESCE, build AESCE only"
2231 scripts/config.py set MBEDTLS_AESCE_C
2232 scripts/config.py set MBEDTLS_AES_USE_HARDWARE_ONLY
2233 armc6_build_test "-O1 --target=aarch64-arm-none-eabi -march=armv8-a+crypto"
2234}
2235
Minos Galanakisf78447f2024-07-26 20:49:51 +01002236support_build_aes_armce () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01002237 # clang >= 11 is required to build with AES extensions
2238 [[ $(clang_version) -ge 11 ]]
2239}
2240
2241component_build_aes_armce () {
2242 # Test variations of AES with Armv8 crypto extensions
2243 scripts/config.py set MBEDTLS_AESCE_C
2244 scripts/config.py set MBEDTLS_AES_USE_HARDWARE_ONLY
2245
2246 msg "MBEDTLS_AES_USE_HARDWARE_ONLY, clang, aarch64"
2247 make -B library/../${BUILTIN_SRC_PATH}/aesce.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a+crypto"
2248
2249 msg "MBEDTLS_AES_USE_HARDWARE_ONLY, clang, arm"
2250 make -B library/../${BUILTIN_SRC_PATH}/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm"
2251
2252 msg "MBEDTLS_AES_USE_HARDWARE_ONLY, clang, thumb"
2253 make -B library/../${BUILTIN_SRC_PATH}/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb"
2254
2255 scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
2256
2257 msg "no MBEDTLS_AES_USE_HARDWARE_ONLY, clang, aarch64"
2258 make -B library/../${BUILTIN_SRC_PATH}/aesce.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a+crypto"
2259
2260 msg "no MBEDTLS_AES_USE_HARDWARE_ONLY, clang, arm"
2261 make -B library/../${BUILTIN_SRC_PATH}/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm"
2262
2263 msg "no MBEDTLS_AES_USE_HARDWARE_ONLY, clang, thumb"
2264 make -B library/../${BUILTIN_SRC_PATH}/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb"
2265
2266 # test for presence of AES instructions
2267 scripts/config.py set MBEDTLS_AES_USE_HARDWARE_ONLY
2268 msg "clang, test A32 crypto instructions built"
2269 make -B library/../${BUILTIN_SRC_PATH}/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm -S"
2270 grep -E 'aes[0-9a-z]+.[0-9]\s*[qv]' ${BUILTIN_SRC_PATH}/aesce.o
2271 msg "clang, test T32 crypto instructions built"
2272 make -B library/../${BUILTIN_SRC_PATH}/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb -S"
2273 grep -E 'aes[0-9a-z]+.[0-9]\s*[qv]' ${BUILTIN_SRC_PATH}/aesce.o
2274 msg "clang, test aarch64 crypto instructions built"
2275 make -B library/../${BUILTIN_SRC_PATH}/aesce.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a -S"
2276 grep -E 'aes[a-z]+\s*[qv]' ${BUILTIN_SRC_PATH}/aesce.o
2277
2278 # test for absence of AES instructions
2279 scripts/config.py unset MBEDTLS_AES_USE_HARDWARE_ONLY
2280 scripts/config.py unset MBEDTLS_AESCE_C
2281 msg "clang, test A32 crypto instructions not built"
2282 make -B library/../${BUILTIN_SRC_PATH}/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm -S"
2283 not grep -E 'aes[0-9a-z]+.[0-9]\s*[qv]' ${BUILTIN_SRC_PATH}/aesce.o
2284 msg "clang, test T32 crypto instructions not built"
2285 make -B library/../${BUILTIN_SRC_PATH}/aesce.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb -S"
2286 not grep -E 'aes[0-9a-z]+.[0-9]\s*[qv]' ${BUILTIN_SRC_PATH}/aesce.o
2287 msg "clang, test aarch64 crypto instructions not built"
2288 make -B library/../${BUILTIN_SRC_PATH}/aesce.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a -S"
2289 not grep -E 'aes[a-z]+\s*[qv]' ${BUILTIN_SRC_PATH}/aesce.o
2290}
2291
Minos Galanakisf78447f2024-07-26 20:49:51 +01002292support_build_sha_armce () {
Minos Galanakis471b34c2024-07-26 15:39:24 +01002293 # clang >= 4 is required to build with SHA extensions
2294 [[ $(clang_version) -ge 4 ]]
2295}
2296
2297component_build_sha_armce () {
2298 scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
2299
2300
2301 # Test variations of SHA256 Armv8 crypto extensions
2302 scripts/config.py set MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY
2303 msg "MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY clang, aarch64"
2304 make -B library/../${BUILTIN_SRC_PATH}/sha256.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a"
2305 msg "MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY clang, arm"
2306 make -B library/../${BUILTIN_SRC_PATH}/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm"
2307 scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY
2308
2309
2310 # test the deprecated form of the config option
2311 scripts/config.py set MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY
2312 msg "MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY clang, thumb"
2313 make -B library/../${BUILTIN_SRC_PATH}/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb"
2314 scripts/config.py unset MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY
2315
2316 scripts/config.py set MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
2317 msg "MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT clang, aarch64"
2318 make -B library/../${BUILTIN_SRC_PATH}/sha256.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a"
2319 scripts/config.py unset MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
2320
2321
2322 # test the deprecated form of the config option
2323 scripts/config.py set MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
2324 msg "MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT clang, arm"
2325 make -B library/../${BUILTIN_SRC_PATH}/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm -std=c99"
2326 msg "MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT clang, thumb"
2327 make -B library/../${BUILTIN_SRC_PATH}/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb"
2328 scripts/config.py unset MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
2329
2330
2331 # examine the disassembly for presence of SHA instructions
2332 for opt in MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT; do
2333 scripts/config.py set ${opt}
2334 msg "${opt} clang, test A32 crypto instructions built"
2335 make -B library/../${BUILTIN_SRC_PATH}/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm -S"
2336 grep -E 'sha256[a-z0-9]+.32\s+[qv]' ${BUILTIN_SRC_PATH}/sha256.o
2337
2338 msg "${opt} clang, test T32 crypto instructions built"
2339 make -B library/../${BUILTIN_SRC_PATH}/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb -S"
2340 grep -E 'sha256[a-z0-9]+.32\s+[qv]' ${BUILTIN_SRC_PATH}/sha256.o
2341
2342 msg "${opt} clang, test aarch64 crypto instructions built"
2343 make -B library/../${BUILTIN_SRC_PATH}/sha256.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a -S"
2344 grep -E 'sha256[a-z0-9]+\s+[qv]' ${BUILTIN_SRC_PATH}/sha256.o
2345 scripts/config.py unset ${opt}
2346 done
2347
2348
2349 # examine the disassembly for absence of SHA instructions
2350 msg "clang, test A32 crypto instructions not built"
2351 make -B library/../${BUILTIN_SRC_PATH}/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a72+crypto -marm -S"
2352 not grep -E 'sha256[a-z0-9]+.32\s+[qv]' ${BUILTIN_SRC_PATH}/sha256.o
2353
2354 msg "clang, test T32 crypto instructions not built"
2355 make -B library/../${BUILTIN_SRC_PATH}/sha256.o CC=clang CFLAGS="--target=arm-linux-gnueabihf -mcpu=cortex-a32+crypto -mthumb -S"
2356 not grep -E 'sha256[a-z0-9]+.32\s+[qv]' ${BUILTIN_SRC_PATH}/sha256.o
2357
2358 msg "clang, test aarch64 crypto instructions not built"
2359 make -B library/../${BUILTIN_SRC_PATH}/sha256.o CC=clang CFLAGS="--target=aarch64-linux-gnu -march=armv8-a -S"
2360 not grep -E 'sha256[a-z0-9]+\s+[qv]' ${BUILTIN_SRC_PATH}/sha256.o
2361}
2362
2363component_test_aes_only_128_bit_keys () {
2364 msg "build: default config + AES_ONLY_128_BIT_KEY_LENGTH"
2365 scripts/config.py set MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
2366
2367 make CFLAGS='-O2 -Werror -Wall -Wextra'
2368
2369 msg "test: default config + AES_ONLY_128_BIT_KEY_LENGTH"
2370 make test
2371}
2372
2373component_test_no_ctr_drbg_aes_only_128_bit_keys () {
2374 msg "build: default config + AES_ONLY_128_BIT_KEY_LENGTH - CTR_DRBG_C"
2375 scripts/config.py set MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
2376 scripts/config.py unset MBEDTLS_CTR_DRBG_C
2377
2378 make CC=clang CFLAGS='-Werror -Wall -Wextra'
2379
2380 msg "test: default config + AES_ONLY_128_BIT_KEY_LENGTH - CTR_DRBG_C"
2381 make test
2382}
2383
2384component_test_aes_only_128_bit_keys_have_builtins () {
2385 msg "build: default config + AES_ONLY_128_BIT_KEY_LENGTH - AESNI_C - AESCE_C"
2386 scripts/config.py set MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
2387 scripts/config.py unset MBEDTLS_AESNI_C
2388 scripts/config.py unset MBEDTLS_AESCE_C
2389
2390 make CFLAGS='-O2 -Werror -Wall -Wextra'
2391
2392 msg "test: default config + AES_ONLY_128_BIT_KEY_LENGTH - AESNI_C - AESCE_C"
2393 make test
2394
2395 msg "selftest: default config + AES_ONLY_128_BIT_KEY_LENGTH - AESNI_C - AESCE_C"
2396 programs/test/selftest
2397}
2398
2399component_test_gcm_largetable () {
2400 msg "build: default config + GCM_LARGE_TABLE - AESNI_C - AESCE_C"
2401 scripts/config.py set MBEDTLS_GCM_LARGE_TABLE
2402 scripts/config.py unset MBEDTLS_AESNI_C
2403 scripts/config.py unset MBEDTLS_AESCE_C
2404
2405 make CFLAGS='-O2 -Werror -Wall -Wextra'
2406
2407 msg "test: default config - GCM_LARGE_TABLE - AESNI_C - AESCE_C"
2408 make test
2409}
2410
2411component_test_aes_fewer_tables () {
2412 msg "build: default config with AES_FEWER_TABLES enabled"
2413 scripts/config.py set MBEDTLS_AES_FEWER_TABLES
2414 make CFLAGS='-O2 -Werror -Wall -Wextra'
2415
2416 msg "test: AES_FEWER_TABLES"
2417 make test
2418}
2419
2420component_test_aes_rom_tables () {
2421 msg "build: default config with AES_ROM_TABLES enabled"
2422 scripts/config.py set MBEDTLS_AES_ROM_TABLES
2423 make CFLAGS='-O2 -Werror -Wall -Wextra'
2424
2425 msg "test: AES_ROM_TABLES"
2426 make test
2427}
2428
2429component_test_aes_fewer_tables_and_rom_tables () {
2430 msg "build: default config with AES_ROM_TABLES and AES_FEWER_TABLES enabled"
2431 scripts/config.py set MBEDTLS_AES_FEWER_TABLES
2432 scripts/config.py set MBEDTLS_AES_ROM_TABLES
2433 make CFLAGS='-O2 -Werror -Wall -Wextra'
2434
2435 msg "test: AES_FEWER_TABLES + AES_ROM_TABLES"
2436 make test
2437}
2438
2439# helper for common_block_cipher_no_decrypt() which:
2440# - enable/disable the list of config options passed from -s/-u respectively.
2441# - build
2442# - test for tests_suite_xxx
2443# - selftest
2444#
2445# Usage: helper_block_cipher_no_decrypt_build_test
2446# [-s set_opts] [-u unset_opts] [-c cflags] [-l ldflags] [option [...]]
2447# Options: -s set_opts the list of config options to enable
2448# -u unset_opts the list of config options to disable
2449# -c cflags the list of options passed to CFLAGS
2450# -l ldflags the list of options passed to LDFLAGS
2451
2452helper_block_cipher_no_decrypt_build_test () {
2453 while [ $# -gt 0 ]; do
2454 case "$1" in
2455 -s)
2456 shift; local set_opts="$1";;
2457 -u)
2458 shift; local unset_opts="$1";;
2459 -c)
2460 shift; local cflags="-Werror -Wall -Wextra $1";;
2461 -l)
2462 shift; local ldflags="$1";;
2463 esac
2464 shift
2465 done
2466 set_opts="${set_opts:-}"
2467 unset_opts="${unset_opts:-}"
2468 cflags="${cflags:-}"
2469 ldflags="${ldflags:-}"
2470
2471 [ -n "$set_opts" ] && echo "Enabling: $set_opts" && scripts/config.py set-all $set_opts
2472 [ -n "$unset_opts" ] && echo "Disabling: $unset_opts" && scripts/config.py unset-all $unset_opts
2473
2474 msg "build: default config + BLOCK_CIPHER_NO_DECRYPT${set_opts:+ + $set_opts}${unset_opts:+ - $unset_opts} with $cflags${ldflags:+, $ldflags}"
2475 make clean
2476 make CFLAGS="-O2 $cflags" LDFLAGS="$ldflags"
2477
2478 # Make sure we don't have mbedtls_xxx_setkey_dec in AES/ARIA/CAMELLIA
2479 not grep mbedtls_aes_setkey_dec ${BUILTIN_SRC_PATH}/aes.o
2480 not grep mbedtls_aria_setkey_dec ${BUILTIN_SRC_PATH}/aria.o
2481 not grep mbedtls_camellia_setkey_dec ${BUILTIN_SRC_PATH}/camellia.o
2482 # Make sure we don't have mbedtls_internal_aes_decrypt in AES
2483 not grep mbedtls_internal_aes_decrypt ${BUILTIN_SRC_PATH}/aes.o
2484 # Make sure we don't have mbedtls_aesni_inverse_key in AESNI
2485 not grep mbedtls_aesni_inverse_key ${BUILTIN_SRC_PATH}/aesni.o
2486
2487 msg "test: default config + BLOCK_CIPHER_NO_DECRYPT${set_opts:+ + $set_opts}${unset_opts:+ - $unset_opts} with $cflags${ldflags:+, $ldflags}"
2488 make test
2489
2490 msg "selftest: default config + BLOCK_CIPHER_NO_DECRYPT${set_opts:+ + $set_opts}${unset_opts:+ - $unset_opts} with $cflags${ldflags:+, $ldflags}"
2491 programs/test/selftest
2492}
2493
2494# This is a common configuration function used in:
2495# - component_test_block_cipher_no_decrypt_aesni_legacy()
2496# - component_test_block_cipher_no_decrypt_aesni_use_psa()
2497# in order to test BLOCK_CIPHER_NO_DECRYPT with AESNI intrinsics,
2498# AESNI assembly and AES C implementation on x86_64 and with AESNI intrinsics
2499# on x86.
2500
2501common_block_cipher_no_decrypt () {
2502 # test AESNI intrinsics
2503 helper_block_cipher_no_decrypt_build_test \
2504 -s "MBEDTLS_AESNI_C" \
2505 -c "-mpclmul -msse2 -maes"
2506
2507 # test AESNI assembly
2508 helper_block_cipher_no_decrypt_build_test \
2509 -s "MBEDTLS_AESNI_C" \
2510 -c "-mno-pclmul -mno-sse2 -mno-aes"
2511
2512 # test AES C implementation
2513 helper_block_cipher_no_decrypt_build_test \
2514 -u "MBEDTLS_AESNI_C"
2515
2516 # test AESNI intrinsics for i386 target
2517 helper_block_cipher_no_decrypt_build_test \
2518 -s "MBEDTLS_AESNI_C" \
2519 -c "-m32 -mpclmul -msse2 -maes" \
2520 -l "-m32"
2521}
2522
2523# This is a configuration function used in component_test_block_cipher_no_decrypt_xxx:
2524# usage: 0: no PSA crypto configuration
2525# 1: use PSA crypto configuration
2526
2527config_block_cipher_no_decrypt () {
2528 use_psa=$1
2529
2530 scripts/config.py set MBEDTLS_BLOCK_CIPHER_NO_DECRYPT
2531 scripts/config.py unset MBEDTLS_CIPHER_MODE_CBC
2532 scripts/config.py unset MBEDTLS_CIPHER_MODE_XTS
2533 scripts/config.py unset MBEDTLS_DES_C
2534 scripts/config.py unset MBEDTLS_NIST_KW_C
2535
2536 if [ "$use_psa" -eq 1 ]; then
2537 # Enable support for cryptographic mechanisms through the PSA API.
2538 # Note: XTS, KW are not yet supported via the PSA API in Mbed TLS.
2539 scripts/config.py set MBEDTLS_PSA_CRYPTO_CONFIG
2540 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CBC_NO_PADDING
2541 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_CBC_PKCS7
2542 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_ECB_NO_PADDING
2543 scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_KEY_TYPE_DES
2544 fi
2545}
2546
2547component_test_block_cipher_no_decrypt_aesni () {
2548 # This consistently causes an llvm crash on clang 3.8, so use gcc
2549 export CC=gcc
2550 config_block_cipher_no_decrypt 0
2551 common_block_cipher_no_decrypt
2552}
2553
2554component_test_block_cipher_no_decrypt_aesni_use_psa () {
2555 # This consistently causes an llvm crash on clang 3.8, so use gcc
2556 export CC=gcc
2557 config_block_cipher_no_decrypt 1
2558 common_block_cipher_no_decrypt
2559}
2560
2561component_test_block_cipher_no_decrypt_aesce_armcc () {
2562 scripts/config.py baremetal
2563
2564 # armc[56] don't support SHA-512 intrinsics
2565 scripts/config.py unset MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
2566
2567 # Stop armclang warning about feature detection for A64_CRYPTO.
2568 # With this enabled, the library does build correctly under armclang,
2569 # but in baremetal builds (as tested here), feature detection is
2570 # unavailable, and the user is notified via a #warning. So enabling
2571 # this feature would prevent us from building with -Werror on
2572 # armclang. Tracked in #7198.
2573 scripts/config.py unset MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
2574 scripts/config.py set MBEDTLS_HAVE_ASM
2575
2576 config_block_cipher_no_decrypt 1
2577
2578 # test AESCE baremetal build
2579 scripts/config.py set MBEDTLS_AESCE_C
2580 msg "build: default config + BLOCK_CIPHER_NO_DECRYPT with AESCE"
2581 armc6_build_test "-O1 --target=aarch64-arm-none-eabi -march=armv8-a+crypto -Werror -Wall -Wextra"
2582
2583 # Make sure we don't have mbedtls_xxx_setkey_dec in AES/ARIA/CAMELLIA
2584 not grep mbedtls_aes_setkey_dec ${BUILTIN_SRC_PATH}/aes.o
2585 not grep mbedtls_aria_setkey_dec ${BUILTIN_SRC_PATH}/aria.o
2586 not grep mbedtls_camellia_setkey_dec ${BUILTIN_SRC_PATH}/camellia.o
2587 # Make sure we don't have mbedtls_internal_aes_decrypt in AES
2588 not grep mbedtls_internal_aes_decrypt ${BUILTIN_SRC_PATH}/aes.o
2589 # Make sure we don't have mbedtls_aesce_inverse_key and aesce_decrypt_block in AESCE
2590 not grep mbedtls_aesce_inverse_key ${BUILTIN_SRC_PATH}/aesce.o
2591 not grep aesce_decrypt_block ${BUILTIN_SRC_PATH}/aesce.o
2592}
2593
2594component_test_ctr_drbg_aes_256_sha_256 () {
2595 msg "build: full + MBEDTLS_ENTROPY_FORCE_SHA256 (ASan build)"
2596 scripts/config.py full
2597 scripts/config.py unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
2598 scripts/config.py set MBEDTLS_ENTROPY_FORCE_SHA256
2599 CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
2600 make
2601
2602 msg "test: full + MBEDTLS_ENTROPY_FORCE_SHA256 (ASan build)"
2603 make test
2604}
2605
2606component_test_ctr_drbg_aes_128_sha_512 () {
2607 msg "build: full + MBEDTLS_CTR_DRBG_USE_128_BIT_KEY (ASan build)"
2608 scripts/config.py full
2609 scripts/config.py unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
2610 scripts/config.py set MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
2611 CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
2612 make
2613
2614 msg "test: full + MBEDTLS_CTR_DRBG_USE_128_BIT_KEY (ASan build)"
2615 make test
2616}
2617
2618component_test_ctr_drbg_aes_128_sha_256 () {
2619 msg "build: full + MBEDTLS_CTR_DRBG_USE_128_BIT_KEY + MBEDTLS_ENTROPY_FORCE_SHA256 (ASan build)"
2620 scripts/config.py full
2621 scripts/config.py unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
2622 scripts/config.py set MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
2623 scripts/config.py set MBEDTLS_ENTROPY_FORCE_SHA256
2624 CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
2625 make
2626
2627 msg "test: full + MBEDTLS_CTR_DRBG_USE_128_BIT_KEY + MBEDTLS_ENTROPY_FORCE_SHA256 (ASan build)"
2628 make test
2629}
2630
2631component_test_se_default () {
2632 msg "build: default config + MBEDTLS_PSA_CRYPTO_SE_C"
2633 scripts/config.py set MBEDTLS_PSA_CRYPTO_SE_C
2634 make CC=clang CFLAGS="$ASAN_CFLAGS -Os" LDFLAGS="$ASAN_CFLAGS"
2635
2636 msg "test: default config + MBEDTLS_PSA_CRYPTO_SE_C"
2637 make test
2638}
2639
2640component_test_psa_crypto_drivers () {
2641 msg "build: full + test drivers dispatching to builtins"
2642 scripts/config.py full
2643 scripts/config.py unset MBEDTLS_PSA_CRYPTO_CONFIG
2644 loc_cflags="$ASAN_CFLAGS -DPSA_CRYPTO_DRIVER_TEST_ALL"
2645 loc_cflags="${loc_cflags} '-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/user-config-for-test.h\"'"
2646 loc_cflags="${loc_cflags} -I../tests/include -O2"
2647
2648 make CC=$ASAN_CC CFLAGS="${loc_cflags}" LDFLAGS="$ASAN_CFLAGS"
2649
2650 msg "test: full + test drivers dispatching to builtins"
2651 make test
2652}
2653
2654component_build_psa_config_file () {
2655 msg "build: make with MBEDTLS_PSA_CRYPTO_CONFIG_FILE" # ~40s
2656 scripts/config.py set MBEDTLS_PSA_CRYPTO_CONFIG
2657 cp "$CRYPTO_CONFIG_H" psa_test_config.h
2658 echo '#error "MBEDTLS_PSA_CRYPTO_CONFIG_FILE is not working"' >"$CRYPTO_CONFIG_H"
2659 make CFLAGS="-I '$PWD' -DMBEDTLS_PSA_CRYPTO_CONFIG_FILE='\"psa_test_config.h\"'"
2660 # Make sure this feature is enabled. We'll disable it in the next phase.
2661 programs/test/query_compile_time_config MBEDTLS_CMAC_C
2662 make clean
2663
2664 msg "build: make with MBEDTLS_PSA_CRYPTO_CONFIG_FILE + MBEDTLS_PSA_CRYPTO_USER_CONFIG_FILE" # ~40s
2665 # In the user config, disable one feature and its dependencies, which will
2666 # reflect on the mbedtls configuration so we can query it with
2667 # query_compile_time_config.
2668 echo '#undef PSA_WANT_ALG_CMAC' >psa_user_config.h
2669 echo '#undef PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128' >> psa_user_config.h
2670 scripts/config.py unset MBEDTLS_CMAC_C
2671 make CFLAGS="-I '$PWD' -DMBEDTLS_PSA_CRYPTO_CONFIG_FILE='\"psa_test_config.h\"' -DMBEDTLS_PSA_CRYPTO_USER_CONFIG_FILE='\"psa_user_config.h\"'"
2672 not programs/test/query_compile_time_config MBEDTLS_CMAC_C
2673
2674 rm -f psa_test_config.h psa_user_config.h
2675}
2676
2677component_build_psa_alt_headers () {
2678 msg "build: make with PSA alt headers" # ~20s
2679
2680 # Generate alternative versions of the substitutable headers with the
2681 # same content except different include guards.
2682 make -C tests include/alt-extra/psa/crypto_platform_alt.h include/alt-extra/psa/crypto_struct_alt.h
2683
2684 # Build the library and some programs.
2685 # Don't build the fuzzers to avoid having to go through hoops to set
2686 # a correct include path for programs/fuzz/Makefile.
2687 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
2688 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\"'"
2689
2690 # Check that we're getting the alternative include guards and not the
2691 # original include guards.
2692 programs/test/query_included_headers | grep -x PSA_CRYPTO_PLATFORM_ALT_H
2693 programs/test/query_included_headers | grep -x PSA_CRYPTO_STRUCT_ALT_H
2694 programs/test/query_included_headers | not grep -x PSA_CRYPTO_PLATFORM_H
2695 programs/test/query_included_headers | not grep -x PSA_CRYPTO_STRUCT_H
2696}
2697
2698component_test_min_mpi_window_size () {
2699 msg "build: Default + MBEDTLS_MPI_WINDOW_SIZE=1 (ASan build)" # ~ 10s
2700 scripts/config.py set MBEDTLS_MPI_WINDOW_SIZE 1
2701 CC=$ASAN_CC cmake -D CMAKE_BUILD_TYPE:String=Asan .
2702 make
2703
2704 msg "test: MBEDTLS_MPI_WINDOW_SIZE=1 - main suites (inc. selftests) (ASan build)" # ~ 10s
2705 make test
2706}
2707
Minos Galanakis6aab5b72024-07-25 14:24:37 +01002708