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