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