| Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
|  | 2 |  | 
| Gabor Mezei | 9f2b817 | 2024-08-06 12:02:18 +0200 | [diff] [blame] | 3 | """Mbed TLS and PSA configuration file manipulation library and tool | 
| Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 4 |  | 
| Fredrik Hesse | cc207bc | 2021-09-28 21:06:08 +0200 | [diff] [blame] | 5 | Basic usage, to read the Mbed TLS configuration: | 
| Gabor Mezei | 9f2b817 | 2024-08-06 12:02:18 +0200 | [diff] [blame] | 6 | config = CombinedConfigFile() | 
| Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 7 | if 'MBEDTLS_RSA_C' in config: print('RSA is enabled') | 
|  | 8 | """ | 
|  | 9 |  | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 10 | ## Copyright The Mbed TLS Contributors | 
| Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 11 | ## SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
| Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 12 | ## | 
| Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 13 |  | 
| Gilles Peskine | 208e4ec | 2019-07-29 23:43:20 +0200 | [diff] [blame] | 14 | import os | 
| Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 15 | import re | 
| Gabor Mezei | 24d7cc7 | 2024-08-06 15:11:24 +0200 | [diff] [blame] | 16 | import sys | 
| Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 17 |  | 
| Gabor Mezei | 0e9e4cb | 2024-09-10 16:16:35 +0200 | [diff] [blame] | 18 | import framework_scripts_path # pylint: disable=unused-import | 
|  | 19 | from mbedtls_framework import config_common | 
| Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 20 |  | 
| Gabor Mezei | a12ed6b | 2024-09-09 17:20:49 +0200 | [diff] [blame] | 21 |  | 
| Gilles Peskine | e4c6955 | 2024-09-19 19:57:58 +0200 | [diff] [blame] | 22 | def is_boolean_setting(name, value): | 
|  | 23 | """Is this a boolean setting? | 
| Gabor Mezei | de6e192 | 2024-06-28 17:10:50 +0200 | [diff] [blame] | 24 |  | 
| Gilles Peskine | e4c6955 | 2024-09-19 19:57:58 +0200 | [diff] [blame] | 25 | Mbed TLS boolean settings are enabled if the preprocessor macro is | 
|  | 26 | defined, and disabled if the preprocessor macro is not defined. The | 
|  | 27 | macro definition line in the configuration file has an empty expansion. | 
|  | 28 |  | 
|  | 29 | PSA_WANT_xxx settings are also boolean, but when they are enabled, | 
|  | 30 | they expand to a nonzero value. We leave them undefined when they | 
|  | 31 | are disabled. (Setting them to 0 currently means to enable them, but | 
|  | 32 | this might change to mean disabling them. Currently we just never set | 
|  | 33 | them to 0.) | 
| Gabor Mezei | de6e192 | 2024-06-28 17:10:50 +0200 | [diff] [blame] | 34 | """ | 
| Gilles Peskine | e4c6955 | 2024-09-19 19:57:58 +0200 | [diff] [blame] | 35 | if name.startswith('PSA_WANT_'): | 
|  | 36 | return True | 
|  | 37 | if not value: | 
|  | 38 | return True | 
|  | 39 | return False | 
| Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 40 |  | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 41 | def realfull_adapter(_name, _value, _active): | 
| Gilles Peskine | 36571d6 | 2024-09-19 19:58:56 +0200 | [diff] [blame] | 42 | """Activate all symbols. | 
| Gilles Peskine | ba4162a | 2022-04-11 17:04:38 +0200 | [diff] [blame] | 43 |  | 
|  | 44 | This is intended for building the documentation, including the | 
|  | 45 | documentation of settings that are activated by defining an optional | 
| Gilles Peskine | 36571d6 | 2024-09-19 19:58:56 +0200 | [diff] [blame] | 46 | preprocessor macro. There is no expectation that the resulting | 
|  | 47 | configuration can be built. | 
| Gilles Peskine | ba4162a | 2022-04-11 17:04:38 +0200 | [diff] [blame] | 48 | """ | 
| Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 49 | return True | 
|  | 50 |  | 
| Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 51 | PSA_UNSUPPORTED_FEATURE = frozenset([ | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 52 | 'PSA_WANT_ALG_CBC_MAC', | 
|  | 53 | 'PSA_WANT_ALG_XTS', | 
|  | 54 | 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_DERIVE', | 
|  | 55 | 'PSA_WANT_KEY_TYPE_DH_KEY_PAIR_DERIVE' | 
|  | 56 | ]) | 
|  | 57 |  | 
| Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 58 | PSA_DEPRECATED_FEATURE = frozenset([ | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 59 | 'PSA_WANT_KEY_TYPE_ECC_KEY_PAIR', | 
|  | 60 | 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR' | 
|  | 61 | ]) | 
|  | 62 |  | 
| Gabor Mezei | 9b0f9e7 | 2024-06-26 18:08:17 +0200 | [diff] [blame] | 63 | EXCLUDE_FROM_CRYPTO = PSA_UNSUPPORTED_FEATURE | \ | 
| Gilles Peskine | bc7c523 | 2025-01-02 11:30:55 +0100 | [diff] [blame] | 64 | PSA_DEPRECATED_FEATURE | 
| Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 65 |  | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 66 | # The goal of the full configuration is to have everything that can be tested | 
|  | 67 | # together. This includes deprecated or insecure options. It excludes: | 
|  | 68 | # * Options that require additional build dependencies or unusual hardware. | 
|  | 69 | # * Options that make testing less effective. | 
| Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 70 | # * Options that are incompatible with other options, or more generally that | 
|  | 71 | #   interact with other parts of the code in such a way that a bulk enabling | 
|  | 72 | #   is not a good way to test them. | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 73 | # * Options that remove features. | 
| Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 74 | EXCLUDE_FROM_FULL = frozenset([ | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 75 | #pylint: disable=line-too-long | 
| Yanray Wang | a870467 | 2023-04-20 17:16:48 +0800 | [diff] [blame] | 76 | 'MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH', # interacts with CTR_DRBG_128_BIT_KEY | 
| Gilles Peskine | a8861e0 | 2023-09-05 20:20:51 +0200 | [diff] [blame] | 77 | 'MBEDTLS_AES_USE_HARDWARE_ONLY', # hardware dependency | 
| Yanray Wang | 42be1ba | 2023-11-23 14:28:47 +0800 | [diff] [blame] | 78 | 'MBEDTLS_BLOCK_CIPHER_NO_DECRYPT', # incompatible with ECB in PSA, CBC/XTS/NIST_KW/DES | 
| Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 79 | 'MBEDTLS_CTR_DRBG_USE_128_BIT_KEY', # interacts with ENTROPY_FORCE_SHA256 | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 80 | 'MBEDTLS_DEPRECATED_REMOVED', # conflicts with deprecated options | 
| Gilles Peskine | 90581ee | 2020-04-12 14:02:47 +0200 | [diff] [blame] | 81 | 'MBEDTLS_DEPRECATED_WARNING', # conflicts with deprecated options | 
| Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 82 | 'MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED', # influences the use of ECDH in TLS | 
| Janos Follath | 5b7c38f | 2023-08-01 08:51:12 +0100 | [diff] [blame] | 83 | 'MBEDTLS_ECP_WITH_MPI_UINT', # disables the default ECP and is experimental | 
| Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 84 | 'MBEDTLS_ENTROPY_FORCE_SHA256', # interacts with CTR_DRBG_128_BIT_KEY | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 85 | 'MBEDTLS_HAVE_SSE2', # hardware dependency | 
|  | 86 | 'MBEDTLS_MEMORY_BACKTRACE', # depends on MEMORY_BUFFER_ALLOC_C | 
|  | 87 | 'MBEDTLS_MEMORY_BUFFER_ALLOC_C', # makes sanitizers (e.g. ASan) less effective | 
|  | 88 | 'MBEDTLS_MEMORY_DEBUG', # depends on MEMORY_BUFFER_ALLOC_C | 
| Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 89 | 'MBEDTLS_NO_64BIT_MULTIPLICATION', # influences anything that uses bignum | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 90 | 'MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES', # removes a feature | 
|  | 91 | 'MBEDTLS_NO_PLATFORM_ENTROPY', # removes a feature | 
| Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 92 | 'MBEDTLS_NO_UDBL_DIVISION', # influences anything that uses bignum | 
| Gilles Peskine | efaee9a | 2023-09-20 20:49:47 +0200 | [diff] [blame] | 93 | 'MBEDTLS_PSA_P256M_DRIVER_ENABLED', # influences SECP256R1 KeyGen/ECDH/ECDSA | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 94 | 'MBEDTLS_PLATFORM_NO_STD_FUNCTIONS', # removes a feature | 
| David Horstmann | 6f8c95b | 2024-03-14 14:52:45 +0000 | [diff] [blame] | 95 | 'MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS', # removes a feature | 
| Gilles Peskine | f08b3f8 | 2020-11-13 17:36:48 +0100 | [diff] [blame] | 96 | 'MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG', # behavior change + build dependency | 
| Gilles Peskine | a22b95a | 2024-09-19 13:43:57 +0200 | [diff] [blame] | 97 | 'MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER', # interface and behavior change | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 98 | 'MBEDTLS_PSA_CRYPTO_SPM', # platform dependency (PSA SPM) | 
| Gilles Peskine | a08def9 | 2023-04-28 21:01:49 +0200 | [diff] [blame] | 99 | 'MBEDTLS_PSA_INJECT_ENTROPY', # conflicts with platform entropy sources | 
| Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 100 | 'MBEDTLS_RSA_NO_CRT', # influences the use of RSA in X.509 and TLS | 
| Tom Cosgrove | 87fbfb5 | 2022-03-15 10:51:52 +0000 | [diff] [blame] | 101 | 'MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT | 
| Dave Rodgman | 9be3cf0 | 2023-10-11 14:47:55 +0100 | [diff] [blame] | 102 | 'MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY', # interacts with *_USE_ARMV8_A_CRYPTO_IF_PRESENT | 
| Tom Cosgrove | 87fbfb5 | 2022-03-15 10:51:52 +0000 | [diff] [blame] | 103 | 'MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT | 
| Dave Rodgman | 7cb635a | 2023-10-12 16:14:51 +0100 | [diff] [blame] | 104 | 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', # setting *_USE_ARMV8_A_CRYPTO is sufficient | 
| Manuel Pégourié-Gonnard | 6240def | 2020-07-10 09:35:54 +0200 | [diff] [blame] | 105 | 'MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN', # build dependency (clang+memsan) | 
| Manuel Pégourié-Gonnard | 73afa37 | 2020-08-19 10:27:38 +0200 | [diff] [blame] | 106 | 'MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND', # build dependency (valgrind headers) | 
| Hanno Becker | e111356 | 2019-06-12 13:59:14 +0100 | [diff] [blame] | 107 | 'MBEDTLS_X509_REMOVE_INFO', # removes a feature | 
| Valerio Setti | 678e0fb | 2024-06-14 07:49:05 +0200 | [diff] [blame] | 108 | 'MBEDTLS_PSA_STATIC_KEY_SLOTS', # only relevant for embedded devices | 
|  | 109 | 'MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE', # only relevant for embedded devices | 
| Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 110 | *PSA_UNSUPPORTED_FEATURE, | 
|  | 111 | *PSA_DEPRECATED_FEATURE, | 
| Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 112 | ]) | 
|  | 113 |  | 
| Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 114 | def is_seamless_alt(name): | 
| Gilles Peskine | c34faba | 2020-04-20 15:44:14 +0200 | [diff] [blame] | 115 | """Whether the xxx_ALT symbol should be included in the full configuration. | 
| Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 116 |  | 
| Gilles Peskine | c34faba | 2020-04-20 15:44:14 +0200 | [diff] [blame] | 117 | Include alternative implementations of platform functions, which are | 
| Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 118 | configurable function pointers that default to the built-in function. | 
|  | 119 | This way we test that the function pointers exist and build correctly | 
|  | 120 | without changing the behavior, and tests can verify that the function | 
|  | 121 | pointers are used by modifying those pointers. | 
|  | 122 |  | 
|  | 123 | Exclude alternative implementations of library functions since they require | 
|  | 124 | an implementation of the relevant functions and an xxx_alt.h header. | 
|  | 125 | """ | 
| Gilles Peskine | a8861e0 | 2023-09-05 20:20:51 +0200 | [diff] [blame] | 126 | if name in ( | 
|  | 127 | 'MBEDTLS_PLATFORM_GMTIME_R_ALT', | 
|  | 128 | 'MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT', | 
|  | 129 | 'MBEDTLS_PLATFORM_MS_TIME_ALT', | 
|  | 130 | 'MBEDTLS_PLATFORM_ZEROIZE_ALT', | 
|  | 131 | ): | 
| Gilles Peskine | c34faba | 2020-04-20 15:44:14 +0200 | [diff] [blame] | 132 | # Similar to non-platform xxx_ALT, requires platform_alt.h | 
|  | 133 | return False | 
| Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 134 | return name.startswith('MBEDTLS_PLATFORM_') | 
|  | 135 |  | 
| Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 136 | def include_in_full(name): | 
|  | 137 | """Rules for symbols in the "full" configuration.""" | 
| Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 138 | if name in EXCLUDE_FROM_FULL: | 
| Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 139 | return False | 
|  | 140 | if name.endswith('_ALT'): | 
| Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 141 | return is_seamless_alt(name) | 
| Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 142 | return True | 
|  | 143 |  | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 144 | def full_adapter(name, value, active): | 
| Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 145 | """Config adapter for "full".""" | 
| Gilles Peskine | e4c6955 | 2024-09-19 19:57:58 +0200 | [diff] [blame] | 146 | if not is_boolean_setting(name, value): | 
| Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 147 | return active | 
|  | 148 | return include_in_full(name) | 
|  | 149 |  | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 150 | # The baremetal configuration excludes options that require a library or | 
|  | 151 | # operating system feature that is typically not present on bare metal | 
|  | 152 | # systems. Features that are excluded from "full" won't be in "baremetal" | 
|  | 153 | # either (unless explicitly turned on in baremetal_adapter) so they don't | 
|  | 154 | # need to be repeated here. | 
| Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 155 | EXCLUDE_FROM_BAREMETAL = frozenset([ | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 156 | #pylint: disable=line-too-long | 
| Gilles Peskine | 98f8f95 | 2020-04-20 15:38:39 +0200 | [diff] [blame] | 157 | 'MBEDTLS_ENTROPY_NV_SEED', # requires a filesystem and FS_IO or alternate NV seed hooks | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 158 | 'MBEDTLS_FS_IO', # requires a filesystem | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 159 | 'MBEDTLS_HAVE_TIME', # requires a clock | 
|  | 160 | 'MBEDTLS_HAVE_TIME_DATE', # requires a clock | 
|  | 161 | 'MBEDTLS_NET_C', # requires POSIX-like networking | 
|  | 162 | 'MBEDTLS_PLATFORM_FPRINTF_ALT', # requires FILE* from stdio.h | 
| Gilles Peskine | 98f8f95 | 2020-04-20 15:38:39 +0200 | [diff] [blame] | 163 | 'MBEDTLS_PLATFORM_NV_SEED_ALT', # requires a filesystem and ENTROPY_NV_SEED | 
|  | 164 | 'MBEDTLS_PLATFORM_TIME_ALT', # requires a clock and HAVE_TIME | 
|  | 165 | 'MBEDTLS_PSA_CRYPTO_SE_C', # requires a filesystem and PSA_CRYPTO_STORAGE_C | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 166 | 'MBEDTLS_PSA_CRYPTO_STORAGE_C', # requires a filesystem | 
|  | 167 | 'MBEDTLS_PSA_ITS_FILE_C', # requires a filesystem | 
|  | 168 | 'MBEDTLS_THREADING_C', # requires a threading interface | 
|  | 169 | 'MBEDTLS_THREADING_PTHREAD', # requires pthread | 
|  | 170 | 'MBEDTLS_TIMING_C', # requires a clock | 
| Dave Rodgman | 9be3cf0 | 2023-10-11 14:47:55 +0100 | [diff] [blame] | 171 | 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection | 
| Dave Rodgman | 5b89c55 | 2023-10-10 14:59:02 +0100 | [diff] [blame] | 172 | 'MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection | 
| Dave Rodgman | be7915a | 2023-10-11 10:46:38 +0100 | [diff] [blame] | 173 | 'MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection | 
| Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 174 | ]) | 
|  | 175 |  | 
| Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 176 | def keep_in_baremetal(name): | 
|  | 177 | """Rules for symbols in the "baremetal" configuration.""" | 
| Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 178 | if name in EXCLUDE_FROM_BAREMETAL: | 
| Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 179 | return False | 
|  | 180 | return True | 
|  | 181 |  | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 182 | def baremetal_adapter(name, value, active): | 
| Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 183 | """Config adapter for "baremetal".""" | 
| Gilles Peskine | e4c6955 | 2024-09-19 19:57:58 +0200 | [diff] [blame] | 184 | if not is_boolean_setting(name, value): | 
| Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 185 | return active | 
|  | 186 | if name == 'MBEDTLS_NO_PLATFORM_ENTROPY': | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 187 | # No OS-provided entropy source | 
| Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 188 | return True | 
|  | 189 | return include_in_full(name) and keep_in_baremetal(name) | 
|  | 190 |  | 
| Gilles Peskine | 120f29d | 2021-09-01 19:51:19 +0200 | [diff] [blame] | 191 | # This set contains options that are mostly for debugging or test purposes, | 
|  | 192 | # and therefore should be excluded when doing code size measurements. | 
|  | 193 | # Options that are their own module (such as MBEDTLS_ERROR_C) are not listed | 
|  | 194 | # and therefore will be included when doing code size measurements. | 
|  | 195 | EXCLUDE_FOR_SIZE = frozenset([ | 
|  | 196 | 'MBEDTLS_DEBUG_C', # large code size increase in TLS | 
|  | 197 | 'MBEDTLS_SELF_TEST', # increases the size of many modules | 
|  | 198 | 'MBEDTLS_TEST_HOOKS', # only useful with the hosted test framework, increases code size | 
|  | 199 | ]) | 
|  | 200 |  | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 201 | def baremetal_size_adapter(name, value, active): | 
| Gilles Peskine | 120f29d | 2021-09-01 19:51:19 +0200 | [diff] [blame] | 202 | if name in EXCLUDE_FOR_SIZE: | 
|  | 203 | return False | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 204 | return baremetal_adapter(name, value, active) | 
| Gilles Peskine | 120f29d | 2021-09-01 19:51:19 +0200 | [diff] [blame] | 205 |  | 
| Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 206 | def include_in_crypto(name): | 
|  | 207 | """Rules for symbols in a crypto configuration.""" | 
|  | 208 | if name.startswith('MBEDTLS_X509_') or \ | 
| Harry Ramsey | c19f8ae | 2024-10-03 11:18:32 +0100 | [diff] [blame] | 209 | name.startswith('MBEDTLS_VERSION_') or \ | 
| Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 210 | name.startswith('MBEDTLS_SSL_') or \ | 
|  | 211 | name.startswith('MBEDTLS_KEY_EXCHANGE_'): | 
|  | 212 | return False | 
|  | 213 | if name in [ | 
| Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 214 | 'MBEDTLS_DEBUG_C', # part of libmbedtls | 
|  | 215 | 'MBEDTLS_NET_C', # part of libmbedtls | 
| Nayna Jain | c9deb18 | 2020-11-16 19:03:12 +0000 | [diff] [blame] | 216 | 'MBEDTLS_PKCS7_C', # part of libmbedx509 | 
| Ronald Cron | 6924564 | 2024-11-12 18:14:23 +0100 | [diff] [blame] | 217 | 'MBEDTLS_TIMING_C', # part of libmbedtls | 
| Harry Ramsey | 08007ed | 2024-10-22 14:18:17 +0100 | [diff] [blame] | 218 | 'MBEDTLS_ERROR_C', # part of libmbedx509 | 
|  | 219 | 'MBEDTLS_ERROR_STRERROR_DUMMY', # part of libmbedx509 | 
| Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 220 | ]: | 
|  | 221 | return False | 
| Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 222 | if name in EXCLUDE_FROM_CRYPTO: | 
|  | 223 | return False | 
| Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 224 | return True | 
|  | 225 |  | 
|  | 226 | def crypto_adapter(adapter): | 
|  | 227 | """Modify an adapter to disable non-crypto symbols. | 
|  | 228 |  | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 229 | ``crypto_adapter(adapter)(name, value, active)`` is like | 
|  | 230 | ``adapter(name, value, active)``, but unsets all X.509 and TLS symbols. | 
| Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 231 | """ | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 232 | def continuation(name, value, active): | 
| Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 233 | if not include_in_crypto(name): | 
|  | 234 | return False | 
|  | 235 | if adapter is None: | 
|  | 236 | return active | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 237 | return adapter(name, value, active) | 
| Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 238 | return continuation | 
|  | 239 |  | 
| Gilles Peskine | ed5c21d | 2022-06-27 23:02:09 +0200 | [diff] [blame] | 240 | DEPRECATED = frozenset([ | 
|  | 241 | 'MBEDTLS_PSA_CRYPTO_SE_C', | 
| Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 242 | *PSA_DEPRECATED_FEATURE | 
| Gilles Peskine | ed5c21d | 2022-06-27 23:02:09 +0200 | [diff] [blame] | 243 | ]) | 
| Gilles Peskine | 30de2e8 | 2020-04-20 21:39:22 +0200 | [diff] [blame] | 244 | def no_deprecated_adapter(adapter): | 
| Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 245 | """Modify an adapter to disable deprecated symbols. | 
|  | 246 |  | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 247 | ``no_deprecated_adapter(adapter)(name, value, active)`` is like | 
|  | 248 | ``adapter(name, value, active)``, but unsets all deprecated symbols | 
| Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 249 | and sets ``MBEDTLS_DEPRECATED_REMOVED``. | 
|  | 250 | """ | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 251 | def continuation(name, value, active): | 
| Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 252 | if name == 'MBEDTLS_DEPRECATED_REMOVED': | 
|  | 253 | return True | 
| Gilles Peskine | ed5c21d | 2022-06-27 23:02:09 +0200 | [diff] [blame] | 254 | if name in DEPRECATED: | 
|  | 255 | return False | 
| Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 256 | if adapter is None: | 
|  | 257 | return active | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 258 | return adapter(name, value, active) | 
| Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 259 | return continuation | 
|  | 260 |  | 
| Paul Elliott | fb81f77 | 2023-10-18 17:44:59 +0100 | [diff] [blame] | 261 | def no_platform_adapter(adapter): | 
|  | 262 | """Modify an adapter to disable platform symbols. | 
|  | 263 |  | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 264 | ``no_platform_adapter(adapter)(name, value, active)`` is like | 
|  | 265 | ``adapter(name, value, active)``, but unsets all platform symbols other | 
| Paul Elliott | fb81f77 | 2023-10-18 17:44:59 +0100 | [diff] [blame] | 266 | ``than MBEDTLS_PLATFORM_C. | 
|  | 267 | """ | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 268 | def continuation(name, value, active): | 
| Paul Elliott | fb81f77 | 2023-10-18 17:44:59 +0100 | [diff] [blame] | 269 | # Allow MBEDTLS_PLATFORM_C but remove all other platform symbols. | 
|  | 270 | if name.startswith('MBEDTLS_PLATFORM_') and name != 'MBEDTLS_PLATFORM_C': | 
|  | 271 | return False | 
|  | 272 | if adapter is None: | 
|  | 273 | return active | 
| Gilles Peskine | 00b9144 | 2024-09-19 20:13:49 +0200 | [diff] [blame] | 274 | return adapter(name, value, active) | 
| Paul Elliott | fb81f77 | 2023-10-18 17:44:59 +0100 | [diff] [blame] | 275 | return continuation | 
|  | 276 |  | 
| Gabor Mezei | a12ed6b | 2024-09-09 17:20:49 +0200 | [diff] [blame] | 277 |  | 
| Gabor Mezei | 0e9e4cb | 2024-09-10 16:16:35 +0200 | [diff] [blame] | 278 | class MbedTLSConfigFile(config_common.ConfigFile): | 
| Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 279 | """Representation of an MbedTLS configuration file.""" | 
|  | 280 |  | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 281 | _path_in_tree = 'include/mbedtls/mbedtls_config.h' | 
|  | 282 | default_path = [_path_in_tree, | 
|  | 283 | os.path.join(os.path.dirname(__file__), | 
|  | 284 | os.pardir, | 
|  | 285 | _path_in_tree), | 
|  | 286 | os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))), | 
|  | 287 | _path_in_tree)] | 
|  | 288 |  | 
|  | 289 | def __init__(self, filename=None): | 
| Gabor Mezei | 93a6d1f | 2024-06-26 18:01:09 +0200 | [diff] [blame] | 290 | super().__init__(self.default_path, 'Mbed TLS', filename) | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 291 | self.current_section = 'header' | 
|  | 292 |  | 
| Gabor Mezei | a12ed6b | 2024-09-09 17:20:49 +0200 | [diff] [blame] | 293 |  | 
| Gabor Mezei | 0e9e4cb | 2024-09-10 16:16:35 +0200 | [diff] [blame] | 294 | class CryptoConfigFile(config_common.ConfigFile): | 
| Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 295 | """Representation of a Crypto configuration file.""" | 
| Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 296 |  | 
| Gabor Mezei | 3de6586 | 2024-07-08 16:14:10 +0200 | [diff] [blame] | 297 | # Temporary, while Mbed TLS does not just rely on the TF-PSA-Crypto | 
|  | 298 | # build system to build its crypto library. When it does, the | 
|  | 299 | # condition can just be removed. | 
| Gabor Mezei | 776ee90 | 2024-09-09 17:00:50 +0200 | [diff] [blame] | 300 | _path_in_tree = ('include/psa/crypto_config.h' | 
|  | 301 | if not os.path.isdir(os.path.join(os.path.dirname(__file__), | 
|  | 302 | os.pardir, | 
|  | 303 | 'tf-psa-crypto')) else | 
|  | 304 | 'tf-psa-crypto/include/psa/crypto_config.h') | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 305 | default_path = [_path_in_tree, | 
|  | 306 | os.path.join(os.path.dirname(__file__), | 
|  | 307 | os.pardir, | 
|  | 308 | _path_in_tree), | 
|  | 309 | os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))), | 
|  | 310 | _path_in_tree)] | 
|  | 311 |  | 
|  | 312 | def __init__(self, filename=None): | 
| Gabor Mezei | 93a6d1f | 2024-06-26 18:01:09 +0200 | [diff] [blame] | 313 | super().__init__(self.default_path, 'Crypto', filename) | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 314 |  | 
| Gabor Mezei | a12ed6b | 2024-09-09 17:20:49 +0200 | [diff] [blame] | 315 |  | 
| Gabor Mezei | 0e9e4cb | 2024-09-10 16:16:35 +0200 | [diff] [blame] | 316 | class MbedTLSConfig(config_common.Config): | 
| Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 317 | """Representation of the Mbed TLS configuration. | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 318 |  | 
|  | 319 | See the documentation of the `Config` class for methods to query | 
|  | 320 | and modify the configuration. | 
|  | 321 | """ | 
| Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 322 |  | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 323 | def __init__(self, filename=None): | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 324 | """Read the Mbed TLS configuration file.""" | 
| Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 325 |  | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 326 | super().__init__() | 
| Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 327 | configfile = MbedTLSConfigFile(filename) | 
|  | 328 | self.configfiles.append(configfile) | 
| Gabor Mezei | 0e9e4cb | 2024-09-10 16:16:35 +0200 | [diff] [blame] | 329 | self.settings.update({name: config_common.Setting(configfile, active, name, value, section) | 
| Gabor Mezei | 92065ed | 2024-06-07 13:47:59 +0200 | [diff] [blame] | 330 | for (active, name, value, section) | 
| Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 331 | in configfile.parse_file()}) | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 332 |  | 
|  | 333 | def set(self, name, value=None): | 
| Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 334 | """Set name to the given value and make it active.""" | 
|  | 335 |  | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 336 | if name not in self.settings: | 
| Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 337 | self._get_configfile().templates.append((name, '', '#define ' + name + ' ')) | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 338 |  | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 339 | super().set(name, value) | 
| Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 340 |  | 
| Gabor Mezei | a12ed6b | 2024-09-09 17:20:49 +0200 | [diff] [blame] | 341 |  | 
| Gabor Mezei | 0e9e4cb | 2024-09-10 16:16:35 +0200 | [diff] [blame] | 342 | class CryptoConfig(config_common.Config): | 
| Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 343 | """Representation of the PSA crypto configuration. | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 344 |  | 
|  | 345 | See the documentation of the `Config` class for methods to query | 
|  | 346 | and modify the configuration. | 
|  | 347 | """ | 
| Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 348 |  | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 349 | def __init__(self, filename=None): | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 350 | """Read the PSA crypto configuration file.""" | 
| Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 351 |  | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 352 | super().__init__() | 
| Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 353 | configfile = CryptoConfigFile(filename) | 
|  | 354 | self.configfiles.append(configfile) | 
| Gabor Mezei | 0e9e4cb | 2024-09-10 16:16:35 +0200 | [diff] [blame] | 355 | self.settings.update({name: config_common.Setting(configfile, active, name, value, section) | 
| Gabor Mezei | 92065ed | 2024-06-07 13:47:59 +0200 | [diff] [blame] | 356 | for (active, name, value, section) | 
| Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 357 | in configfile.parse_file()}) | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 358 |  | 
| Gabor Mezei | d723b51 | 2024-06-07 15:31:52 +0200 | [diff] [blame] | 359 | def set(self, name, value='1'): | 
| Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 360 | """Set name to the given value and make it active.""" | 
|  | 361 |  | 
| Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 362 | if name in PSA_UNSUPPORTED_FEATURE: | 
| Gabor Mezei | 92065ed | 2024-06-07 13:47:59 +0200 | [diff] [blame] | 363 | raise ValueError(f'Feature is unsupported: \'{name}\'') | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 364 |  | 
|  | 365 | if name not in self.settings: | 
| Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 366 | self._get_configfile().templates.append((name, '', '#define ' + name + ' ')) | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 367 |  | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 368 | super().set(name, value) | 
|  | 369 |  | 
| Gabor Mezei | a12ed6b | 2024-09-09 17:20:49 +0200 | [diff] [blame] | 370 |  | 
| Gabor Mezei | 0e9e4cb | 2024-09-10 16:16:35 +0200 | [diff] [blame] | 371 | class CombinedConfig(config_common.Config): | 
| Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 372 | """Representation of MbedTLS and PSA crypto configuration | 
|  | 373 |  | 
|  | 374 | See the documentation of the `Config` class for methods to query | 
|  | 375 | and modify the configuration. | 
|  | 376 | """ | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 377 |  | 
| Gabor Mezei | 3e2a550 | 2024-06-28 17:27:19 +0200 | [diff] [blame] | 378 | def __init__(self, *configs): | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 379 | super().__init__() | 
| Gabor Mezei | 3e2a550 | 2024-06-28 17:27:19 +0200 | [diff] [blame] | 380 | for config in configs: | 
|  | 381 | if isinstance(config, MbedTLSConfigFile): | 
|  | 382 | self.mbedtls_configfile = config | 
|  | 383 | elif isinstance(config, CryptoConfigFile): | 
|  | 384 | self.crypto_configfile = config | 
|  | 385 | else: | 
|  | 386 | raise ValueError(f'Invalid configfile: {config}') | 
| Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 387 | self.configfiles.append(config) | 
| Gabor Mezei | 3e2a550 | 2024-06-28 17:27:19 +0200 | [diff] [blame] | 388 |  | 
| Gabor Mezei | 0e9e4cb | 2024-09-10 16:16:35 +0200 | [diff] [blame] | 389 | self.settings.update({name: config_common.Setting(configfile, active, name, value, section) | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 390 | for configfile in [self.mbedtls_configfile, self.crypto_configfile] | 
|  | 391 | for (active, name, value, section) in configfile.parse_file()}) | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 392 |  | 
| Gabor Mezei | 5034a1f | 2024-12-05 19:06:19 +0100 | [diff] [blame] | 393 | _crypto_regexp = re.compile(r'^PSA_.*') | 
| Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 394 | def _get_configfile(self, name=None): | 
| Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 395 | """Find a config type for a setting name""" | 
|  | 396 |  | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 397 | if name in self.settings: | 
|  | 398 | return self.settings[name].configfile | 
|  | 399 | elif re.match(self._crypto_regexp, name): | 
|  | 400 | return self.crypto_configfile | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 401 | else: | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 402 | return self.mbedtls_configfile | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 403 |  | 
|  | 404 | def set(self, name, value=None): | 
| Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 405 | """Set name to the given value and make it active.""" | 
|  | 406 |  | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 407 | configfile = self._get_configfile(name) | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 408 |  | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 409 | if configfile == self.crypto_configfile: | 
| Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 410 | if name in PSA_UNSUPPORTED_FEATURE: | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 411 | raise ValueError(f'Feature is unsupported: \'{name}\'') | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 412 |  | 
| Gabor Mezei | d723b51 | 2024-06-07 15:31:52 +0200 | [diff] [blame] | 413 | # The default value in the crypto config is '1' | 
| Minos Galanakis | a4a3737 | 2024-09-26 14:41:41 +0100 | [diff] [blame] | 414 | if not value and re.match(self._crypto_regexp, name): | 
| Gabor Mezei | d723b51 | 2024-06-07 15:31:52 +0200 | [diff] [blame] | 415 | value = '1' | 
|  | 416 |  | 
| Gabor Mezei | c659c1b | 2024-08-06 17:37:55 +0200 | [diff] [blame] | 417 | if name not in self.settings: | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 418 | configfile.templates.append((name, '', '#define ' + name + ' ')) | 
|  | 419 |  | 
| Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 420 | super().set(name, value) | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 421 |  | 
| Gabor Mezei | daf807f | 2024-08-14 11:33:46 +0200 | [diff] [blame] | 422 | #pylint: disable=arguments-differ | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 423 | def write(self, mbedtls_file=None, crypto_file=None): | 
| Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 424 | """Write the whole configuration to the file it was read from. | 
|  | 425 |  | 
|  | 426 | If mbedtls_file or crypto_file is specified, write the specific configuration | 
|  | 427 | to the corresponding file instead. | 
| Gabor Mezei | f5f1308 | 2024-09-18 13:02:16 +0200 | [diff] [blame] | 428 |  | 
| Gabor Mezei | 317a2a3 | 2024-09-18 16:51:27 +0200 | [diff] [blame] | 429 | Two file name parameters and not only one as in the super class as we handle | 
|  | 430 | two configuration files in this class. | 
| Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 431 | """ | 
| Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 432 |  | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 433 | self.mbedtls_configfile.write(self.settings, mbedtls_file) | 
|  | 434 | self.crypto_configfile.write(self.settings, crypto_file) | 
| Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 435 |  | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 436 | def filename(self, name=None): | 
| Gabor Mezei | f5f1308 | 2024-09-18 13:02:16 +0200 | [diff] [blame] | 437 | """Get the name of the config files. | 
| Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 438 |  | 
|  | 439 | If 'name' is specified return the name of the config file where it is defined. | 
|  | 440 | """ | 
|  | 441 |  | 
| Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 442 | if not name: | 
|  | 443 | return [config.filename for config in [self.mbedtls_configfile, self.crypto_configfile]] | 
|  | 444 |  | 
|  | 445 | return self._get_configfile(name).filename | 
| Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 446 |  | 
| Gabor Mezei | 24d7cc7 | 2024-08-06 15:11:24 +0200 | [diff] [blame] | 447 |  | 
| Gabor Mezei | 0e9e4cb | 2024-09-10 16:16:35 +0200 | [diff] [blame] | 448 | class MbedTLSConfigTool(config_common.ConfigTool): | 
| Gabor Mezei | 24d7cc7 | 2024-08-06 15:11:24 +0200 | [diff] [blame] | 449 | """Command line mbedtls_config.h and crypto_config.h manipulation tool.""" | 
|  | 450 |  | 
|  | 451 | def __init__(self): | 
| Gabor Mezei | cd326bf | 2024-09-18 16:53:03 +0200 | [diff] [blame] | 452 | super().__init__(MbedTLSConfigFile.default_path) | 
| Gabor Mezei | 568808a | 2024-09-18 13:02:42 +0200 | [diff] [blame] | 453 | self.config = CombinedConfig(MbedTLSConfigFile(self.args.file), | 
|  | 454 | CryptoConfigFile(self.args.cryptofile)) | 
| Gabor Mezei | 24d7cc7 | 2024-08-06 15:11:24 +0200 | [diff] [blame] | 455 |  | 
|  | 456 | def custom_parser_options(self): | 
|  | 457 | """Adds MbedTLS specific options for the parser.""" | 
|  | 458 |  | 
| Gabor Mezei | a12ed6b | 2024-09-09 17:20:49 +0200 | [diff] [blame] | 459 | self.parser.add_argument( | 
|  | 460 | '--cryptofile', '-c', | 
|  | 461 | help="""Crypto file to read (and modify if requested). Default: {}.""" | 
|  | 462 | .format(CryptoConfigFile.default_path)) | 
| Gabor Mezei | 24d7cc7 | 2024-08-06 15:11:24 +0200 | [diff] [blame] | 463 |  | 
| Gabor Mezei | a12ed6b | 2024-09-09 17:20:49 +0200 | [diff] [blame] | 464 | self.add_adapter( | 
|  | 465 | 'baremetal', baremetal_adapter, | 
|  | 466 | """Like full, but exclude features that require platform features | 
|  | 467 | such as file input-output. | 
|  | 468 | """) | 
|  | 469 | self.add_adapter( | 
|  | 470 | 'baremetal_size', baremetal_size_adapter, | 
|  | 471 | """Like baremetal, but exclude debugging features. Useful for code size measurements. | 
|  | 472 | """) | 
|  | 473 | self.add_adapter( | 
|  | 474 | 'full', full_adapter, | 
|  | 475 | """Uncomment most features. | 
|  | 476 | Exclude alternative implementations and platform support options, as well as | 
|  | 477 | some options that are awkward to test. | 
|  | 478 | """) | 
|  | 479 | self.add_adapter( | 
|  | 480 | 'full_no_deprecated', no_deprecated_adapter(full_adapter), | 
|  | 481 | """Uncomment most non-deprecated features. | 
|  | 482 | Like "full", but without deprecated features. | 
|  | 483 | """) | 
|  | 484 | self.add_adapter( | 
|  | 485 | 'full_no_platform', no_platform_adapter(full_adapter), | 
|  | 486 | """Uncomment most non-platform features. Like "full", but without platform features. | 
|  | 487 | """) | 
|  | 488 | self.add_adapter( | 
|  | 489 | 'realfull', realfull_adapter, | 
|  | 490 | """Uncomment all boolean #defines. | 
|  | 491 | Suitable for generating documentation, but not for building. | 
|  | 492 | """) | 
|  | 493 | self.add_adapter( | 
|  | 494 | 'crypto', crypto_adapter(None), | 
|  | 495 | """Only include crypto features. Exclude X.509 and TLS.""") | 
|  | 496 | self.add_adapter( | 
|  | 497 | 'crypto_baremetal', crypto_adapter(baremetal_adapter), | 
|  | 498 | """Like baremetal, but with only crypto features, excluding X.509 and TLS.""") | 
|  | 499 | self.add_adapter( | 
|  | 500 | 'crypto_full', crypto_adapter(full_adapter), | 
|  | 501 | """Like full, but with only crypto features, excluding X.509 and TLS.""") | 
| Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 502 |  | 
| Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 503 |  | 
| Gabor Mezei | 24d7cc7 | 2024-08-06 15:11:24 +0200 | [diff] [blame] | 504 | if __name__ == '__main__': | 
|  | 505 | sys.exit(MbedTLSConfigTool().main()) |