Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +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 | 1a0bd77 | 2024-09-04 11:42:43 +0200 | [diff] [blame] | 6 | config = MbedTLSConfig() |
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 |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 15 | import sys |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 16 | |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 17 | import framework_scripts_path # pylint: disable=unused-import |
| 18 | from mbedtls_framework import config_common |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 19 | |
Gilles Peskine | 8e90cf4 | 2021-05-27 22:12:57 +0200 | [diff] [blame] | 20 | |
Gilles Peskine | bfdffc3 | 2024-09-19 19:57:58 +0200 | [diff] [blame^] | 21 | def is_boolean_setting(name, value): |
| 22 | """Is this a boolean setting? |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 23 | |
Gilles Peskine | bfdffc3 | 2024-09-19 19:57:58 +0200 | [diff] [blame^] | 24 | Mbed TLS boolean settings are enabled if the preprocessor macro is |
| 25 | defined, and disabled if the preprocessor macro is not defined. The |
| 26 | macro definition line in the configuration file has an empty expansion. |
| 27 | |
| 28 | PSA_WANT_xxx settings are also boolean, but when they are enabled, |
| 29 | they expand to a nonzero value. We leave them undefined when they |
| 30 | are disabled. (Setting them to 0 currently means to enable them, but |
| 31 | this might change to mean disabling them. Currently we just never set |
| 32 | them to 0.) |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 33 | """ |
Gilles Peskine | bfdffc3 | 2024-09-19 19:57:58 +0200 | [diff] [blame^] | 34 | if name.startswith('PSA_WANT_'): |
| 35 | return True |
| 36 | if not value: |
| 37 | return True |
| 38 | return False |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 39 | |
Gilles Peskine | 0ff1d98 | 2024-09-19 19:49:20 +0200 | [diff] [blame] | 40 | def realfull_adapter(_name, _value, active, section): |
Gilles Peskine | ba4162a | 2022-04-11 17:04:38 +0200 | [diff] [blame] | 41 | """Activate all symbols found in the global and boolean feature sections. |
| 42 | |
| 43 | This is intended for building the documentation, including the |
| 44 | documentation of settings that are activated by defining an optional |
| 45 | preprocessor macro. |
| 46 | |
| 47 | Do not activate definitions in the section containing symbols that are |
| 48 | supposed to be defined and documented in their own module. |
| 49 | """ |
| 50 | if section == 'Module configuration options': |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 51 | return active |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 52 | return True |
| 53 | |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 54 | PSA_UNSUPPORTED_FEATURE = frozenset([ |
| 55 | 'PSA_WANT_ALG_CBC_MAC', |
| 56 | 'PSA_WANT_ALG_XTS', |
| 57 | 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_DERIVE', |
| 58 | 'PSA_WANT_KEY_TYPE_DH_KEY_PAIR_DERIVE' |
| 59 | ]) |
| 60 | |
| 61 | PSA_DEPRECATED_FEATURE = frozenset([ |
| 62 | 'PSA_WANT_KEY_TYPE_ECC_KEY_PAIR', |
| 63 | 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR' |
| 64 | ]) |
| 65 | |
| 66 | PSA_UNSTABLE_FEATURE = frozenset([ |
| 67 | 'PSA_WANT_ECC_SECP_K1_224' |
| 68 | ]) |
| 69 | |
| 70 | EXCLUDE_FROM_CRYPTO = PSA_UNSUPPORTED_FEATURE | \ |
| 71 | PSA_DEPRECATED_FEATURE | \ |
| 72 | PSA_UNSTABLE_FEATURE |
| 73 | |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 74 | # The goal of the full configuration is to have everything that can be tested |
| 75 | # together. This includes deprecated or insecure options. It excludes: |
| 76 | # * Options that require additional build dependencies or unusual hardware. |
| 77 | # * Options that make testing less effective. |
Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 78 | # * Options that are incompatible with other options, or more generally that |
| 79 | # interact with other parts of the code in such a way that a bulk enabling |
| 80 | # is not a good way to test them. |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 81 | # * Options that remove features. |
Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 82 | EXCLUDE_FROM_FULL = frozenset([ |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 83 | #pylint: disable=line-too-long |
Yanray Wang | a870467 | 2023-04-20 17:16:48 +0800 | [diff] [blame] | 84 | '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] | 85 | 'MBEDTLS_AES_USE_HARDWARE_ONLY', # hardware dependency |
Yanray Wang | 42be1ba | 2023-11-23 14:28:47 +0800 | [diff] [blame] | 86 | '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] | 87 | 'MBEDTLS_CTR_DRBG_USE_128_BIT_KEY', # interacts with ENTROPY_FORCE_SHA256 |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 88 | 'MBEDTLS_DEPRECATED_REMOVED', # conflicts with deprecated options |
Gilles Peskine | 90581ee | 2020-04-12 14:02:47 +0200 | [diff] [blame] | 89 | 'MBEDTLS_DEPRECATED_WARNING', # conflicts with deprecated options |
Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 90 | 'MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED', # influences the use of ECDH in TLS |
Steven Cooreman | 77e09b6 | 2021-01-22 09:43:27 +0100 | [diff] [blame] | 91 | 'MBEDTLS_ECP_NO_FALLBACK', # removes internal ECP implementation |
Janos Follath | 5b7c38f | 2023-08-01 08:51:12 +0100 | [diff] [blame] | 92 | 'MBEDTLS_ECP_WITH_MPI_UINT', # disables the default ECP and is experimental |
Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 93 | 'MBEDTLS_ENTROPY_FORCE_SHA256', # interacts with CTR_DRBG_128_BIT_KEY |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 94 | 'MBEDTLS_HAVE_SSE2', # hardware dependency |
| 95 | 'MBEDTLS_MEMORY_BACKTRACE', # depends on MEMORY_BUFFER_ALLOC_C |
| 96 | 'MBEDTLS_MEMORY_BUFFER_ALLOC_C', # makes sanitizers (e.g. ASan) less effective |
| 97 | 'MBEDTLS_MEMORY_DEBUG', # depends on MEMORY_BUFFER_ALLOC_C |
Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 98 | 'MBEDTLS_NO_64BIT_MULTIPLICATION', # influences anything that uses bignum |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 99 | 'MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES', # removes a feature |
| 100 | 'MBEDTLS_NO_PLATFORM_ENTROPY', # removes a feature |
Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 101 | 'MBEDTLS_NO_UDBL_DIVISION', # influences anything that uses bignum |
Gilles Peskine | efaee9a | 2023-09-20 20:49:47 +0200 | [diff] [blame] | 102 | 'MBEDTLS_PSA_P256M_DRIVER_ENABLED', # influences SECP256R1 KeyGen/ECDH/ECDSA |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 103 | 'MBEDTLS_PLATFORM_NO_STD_FUNCTIONS', # removes a feature |
David Horstmann | 6f8c95b | 2024-03-14 14:52:45 +0000 | [diff] [blame] | 104 | 'MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS', # removes a feature |
Gilles Peskine | f08b3f8 | 2020-11-13 17:36:48 +0100 | [diff] [blame] | 105 | 'MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG', # behavior change + build dependency |
Gilles Peskine | 3415dc8 | 2024-09-19 13:43:57 +0200 | [diff] [blame] | 106 | 'MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER', # interface and behavior change |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 107 | 'MBEDTLS_PSA_CRYPTO_SPM', # platform dependency (PSA SPM) |
Gilles Peskine | a08def9 | 2023-04-28 21:01:49 +0200 | [diff] [blame] | 108 | 'MBEDTLS_PSA_INJECT_ENTROPY', # conflicts with platform entropy sources |
Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 109 | '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] | 110 | '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] | 111 | '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] | 112 | '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] | 113 | '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] | 114 | 'MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN', # build dependency (clang+memsan) |
Manuel Pégourié-Gonnard | 73afa37 | 2020-08-19 10:27:38 +0200 | [diff] [blame] | 115 | 'MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND', # build dependency (valgrind headers) |
Hanno Becker | e111356 | 2019-06-12 13:59:14 +0100 | [diff] [blame] | 116 | 'MBEDTLS_X509_REMOVE_INFO', # removes a feature |
Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 117 | ]) |
| 118 | |
Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 119 | def is_seamless_alt(name): |
Gilles Peskine | c34faba | 2020-04-20 15:44:14 +0200 | [diff] [blame] | 120 | """Whether the xxx_ALT symbol should be included in the full configuration. |
Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 121 | |
Gilles Peskine | c34faba | 2020-04-20 15:44:14 +0200 | [diff] [blame] | 122 | Include alternative implementations of platform functions, which are |
Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 123 | configurable function pointers that default to the built-in function. |
| 124 | This way we test that the function pointers exist and build correctly |
| 125 | without changing the behavior, and tests can verify that the function |
| 126 | pointers are used by modifying those pointers. |
| 127 | |
| 128 | Exclude alternative implementations of library functions since they require |
| 129 | an implementation of the relevant functions and an xxx_alt.h header. |
| 130 | """ |
Gilles Peskine | a8861e0 | 2023-09-05 20:20:51 +0200 | [diff] [blame] | 131 | if name in ( |
| 132 | 'MBEDTLS_PLATFORM_GMTIME_R_ALT', |
| 133 | 'MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT', |
| 134 | 'MBEDTLS_PLATFORM_MS_TIME_ALT', |
| 135 | 'MBEDTLS_PLATFORM_ZEROIZE_ALT', |
| 136 | ): |
Gilles Peskine | c34faba | 2020-04-20 15:44:14 +0200 | [diff] [blame] | 137 | # Similar to non-platform xxx_ALT, requires platform_alt.h |
| 138 | return False |
Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 139 | return name.startswith('MBEDTLS_PLATFORM_') |
| 140 | |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 141 | def include_in_full(name): |
| 142 | """Rules for symbols in the "full" configuration.""" |
Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 143 | if name in EXCLUDE_FROM_FULL: |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 144 | return False |
| 145 | if name.endswith('_ALT'): |
Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 146 | return is_seamless_alt(name) |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 147 | return True |
| 148 | |
Gilles Peskine | bfdffc3 | 2024-09-19 19:57:58 +0200 | [diff] [blame^] | 149 | def full_adapter(name, value, active, _section): |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 150 | """Config adapter for "full".""" |
Gilles Peskine | bfdffc3 | 2024-09-19 19:57:58 +0200 | [diff] [blame^] | 151 | if not is_boolean_setting(name, value): |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 152 | return active |
| 153 | return include_in_full(name) |
| 154 | |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 155 | # The baremetal configuration excludes options that require a library or |
| 156 | # operating system feature that is typically not present on bare metal |
| 157 | # systems. Features that are excluded from "full" won't be in "baremetal" |
| 158 | # either (unless explicitly turned on in baremetal_adapter) so they don't |
| 159 | # need to be repeated here. |
Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 160 | EXCLUDE_FROM_BAREMETAL = frozenset([ |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 161 | #pylint: disable=line-too-long |
Gilles Peskine | 98f8f95 | 2020-04-20 15:38:39 +0200 | [diff] [blame] | 162 | '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] | 163 | 'MBEDTLS_FS_IO', # requires a filesystem |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 164 | 'MBEDTLS_HAVE_TIME', # requires a clock |
| 165 | 'MBEDTLS_HAVE_TIME_DATE', # requires a clock |
| 166 | 'MBEDTLS_NET_C', # requires POSIX-like networking |
| 167 | 'MBEDTLS_PLATFORM_FPRINTF_ALT', # requires FILE* from stdio.h |
Gilles Peskine | 98f8f95 | 2020-04-20 15:38:39 +0200 | [diff] [blame] | 168 | 'MBEDTLS_PLATFORM_NV_SEED_ALT', # requires a filesystem and ENTROPY_NV_SEED |
| 169 | 'MBEDTLS_PLATFORM_TIME_ALT', # requires a clock and HAVE_TIME |
| 170 | '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] | 171 | 'MBEDTLS_PSA_CRYPTO_STORAGE_C', # requires a filesystem |
| 172 | 'MBEDTLS_PSA_ITS_FILE_C', # requires a filesystem |
| 173 | 'MBEDTLS_THREADING_C', # requires a threading interface |
| 174 | 'MBEDTLS_THREADING_PTHREAD', # requires pthread |
| 175 | 'MBEDTLS_TIMING_C', # requires a clock |
Dave Rodgman | 9be3cf0 | 2023-10-11 14:47:55 +0100 | [diff] [blame] | 176 | '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] | 177 | '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] | 178 | '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] | 179 | ]) |
| 180 | |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 181 | def keep_in_baremetal(name): |
| 182 | """Rules for symbols in the "baremetal" configuration.""" |
Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 183 | if name in EXCLUDE_FROM_BAREMETAL: |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 184 | return False |
| 185 | return True |
| 186 | |
Gilles Peskine | bfdffc3 | 2024-09-19 19:57:58 +0200 | [diff] [blame^] | 187 | def baremetal_adapter(name, value, active, _section): |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 188 | """Config adapter for "baremetal".""" |
Gilles Peskine | bfdffc3 | 2024-09-19 19:57:58 +0200 | [diff] [blame^] | 189 | if not is_boolean_setting(name, value): |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 190 | return active |
| 191 | if name == 'MBEDTLS_NO_PLATFORM_ENTROPY': |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 192 | # No OS-provided entropy source |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 193 | return True |
| 194 | return include_in_full(name) and keep_in_baremetal(name) |
| 195 | |
Gilles Peskine | 120f29d | 2021-09-01 19:51:19 +0200 | [diff] [blame] | 196 | # This set contains options that are mostly for debugging or test purposes, |
| 197 | # and therefore should be excluded when doing code size measurements. |
| 198 | # Options that are their own module (such as MBEDTLS_ERROR_C) are not listed |
| 199 | # and therefore will be included when doing code size measurements. |
| 200 | EXCLUDE_FOR_SIZE = frozenset([ |
| 201 | 'MBEDTLS_DEBUG_C', # large code size increase in TLS |
| 202 | 'MBEDTLS_SELF_TEST', # increases the size of many modules |
| 203 | 'MBEDTLS_TEST_HOOKS', # only useful with the hosted test framework, increases code size |
| 204 | ]) |
| 205 | |
Gilles Peskine | 0ff1d98 | 2024-09-19 19:49:20 +0200 | [diff] [blame] | 206 | def baremetal_size_adapter(name, value, active, section): |
Gilles Peskine | 120f29d | 2021-09-01 19:51:19 +0200 | [diff] [blame] | 207 | if name in EXCLUDE_FOR_SIZE: |
| 208 | return False |
Gilles Peskine | 0ff1d98 | 2024-09-19 19:49:20 +0200 | [diff] [blame] | 209 | return baremetal_adapter(name, value, active, section) |
Gilles Peskine | 120f29d | 2021-09-01 19:51:19 +0200 | [diff] [blame] | 210 | |
Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 211 | def include_in_crypto(name): |
| 212 | """Rules for symbols in a crypto configuration.""" |
| 213 | if name.startswith('MBEDTLS_X509_') or \ |
| 214 | name.startswith('MBEDTLS_SSL_') or \ |
| 215 | name.startswith('MBEDTLS_KEY_EXCHANGE_'): |
| 216 | return False |
| 217 | if name in [ |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 218 | 'MBEDTLS_DEBUG_C', # part of libmbedtls |
| 219 | 'MBEDTLS_NET_C', # part of libmbedtls |
Nayna Jain | c9deb18 | 2020-11-16 19:03:12 +0000 | [diff] [blame] | 220 | 'MBEDTLS_PKCS7_C', # part of libmbedx509 |
Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 221 | ]: |
| 222 | return False |
| 223 | return True |
| 224 | |
| 225 | def crypto_adapter(adapter): |
| 226 | """Modify an adapter to disable non-crypto symbols. |
| 227 | |
Gilles Peskine | 0ff1d98 | 2024-09-19 19:49:20 +0200 | [diff] [blame] | 228 | ``crypto_adapter(adapter)(name, value, active, section)`` is like |
| 229 | ``adapter(name, value, active, section)``, but unsets all X.509 and TLS symbols. |
Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 230 | """ |
Gilles Peskine | 0ff1d98 | 2024-09-19 19:49:20 +0200 | [diff] [blame] | 231 | def continuation(name, value, active, section): |
Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 232 | if not include_in_crypto(name): |
| 233 | return False |
| 234 | if adapter is None: |
| 235 | return active |
Gilles Peskine | 0ff1d98 | 2024-09-19 19:49:20 +0200 | [diff] [blame] | 236 | return adapter(name, value, active, section) |
Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 237 | return continuation |
| 238 | |
Gilles Peskine | ed5c21d | 2022-06-27 23:02:09 +0200 | [diff] [blame] | 239 | DEPRECATED = frozenset([ |
| 240 | 'MBEDTLS_PSA_CRYPTO_SE_C', |
| 241 | ]) |
Gilles Peskine | 30de2e8 | 2020-04-20 21:39:22 +0200 | [diff] [blame] | 242 | def no_deprecated_adapter(adapter): |
Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 243 | """Modify an adapter to disable deprecated symbols. |
| 244 | |
Gilles Peskine | 0ff1d98 | 2024-09-19 19:49:20 +0200 | [diff] [blame] | 245 | ``no_deprecated_adapter(adapter)(name, value, active, section)`` is like |
| 246 | ``adapter(name, value, active, section)``, but unsets all deprecated symbols |
Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 247 | and sets ``MBEDTLS_DEPRECATED_REMOVED``. |
| 248 | """ |
Gilles Peskine | 0ff1d98 | 2024-09-19 19:49:20 +0200 | [diff] [blame] | 249 | def continuation(name, value, active, section): |
Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 250 | if name == 'MBEDTLS_DEPRECATED_REMOVED': |
| 251 | return True |
Gilles Peskine | ed5c21d | 2022-06-27 23:02:09 +0200 | [diff] [blame] | 252 | if name in DEPRECATED: |
| 253 | return False |
Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 254 | if adapter is None: |
| 255 | return active |
Gilles Peskine | 0ff1d98 | 2024-09-19 19:49:20 +0200 | [diff] [blame] | 256 | return adapter(name, value, active, section) |
Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 257 | return continuation |
| 258 | |
Paul Elliott | fb81f77 | 2023-10-18 17:44:59 +0100 | [diff] [blame] | 259 | def no_platform_adapter(adapter): |
| 260 | """Modify an adapter to disable platform symbols. |
| 261 | |
Gilles Peskine | 0ff1d98 | 2024-09-19 19:49:20 +0200 | [diff] [blame] | 262 | ``no_platform_adapter(adapter)(name, value, active, section)`` is like |
| 263 | ``adapter(name, value, active, section)``, but unsets all platform symbols other |
Paul Elliott | fb81f77 | 2023-10-18 17:44:59 +0100 | [diff] [blame] | 264 | ``than MBEDTLS_PLATFORM_C. |
| 265 | """ |
Gilles Peskine | 0ff1d98 | 2024-09-19 19:49:20 +0200 | [diff] [blame] | 266 | def continuation(name, value, active, section): |
Paul Elliott | fb81f77 | 2023-10-18 17:44:59 +0100 | [diff] [blame] | 267 | # Allow MBEDTLS_PLATFORM_C but remove all other platform symbols. |
| 268 | if name.startswith('MBEDTLS_PLATFORM_') and name != 'MBEDTLS_PLATFORM_C': |
| 269 | return False |
| 270 | if adapter is None: |
| 271 | return active |
Gilles Peskine | 0ff1d98 | 2024-09-19 19:49:20 +0200 | [diff] [blame] | 272 | return adapter(name, value, active, section) |
Paul Elliott | fb81f77 | 2023-10-18 17:44:59 +0100 | [diff] [blame] | 273 | return continuation |
| 274 | |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 275 | |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 276 | class MbedTLSConfigFile(config_common.ConfigFile): |
| 277 | """Representation of an MbedTLS configuration file.""" |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 278 | |
Bence Szépkúti | bb0cfeb | 2021-05-28 09:42:25 +0200 | [diff] [blame] | 279 | _path_in_tree = 'include/mbedtls/mbedtls_config.h' |
Gilles Peskine | 208e4ec | 2019-07-29 23:43:20 +0200 | [diff] [blame] | 280 | default_path = [_path_in_tree, |
| 281 | os.path.join(os.path.dirname(__file__), |
| 282 | os.pardir, |
| 283 | _path_in_tree), |
| 284 | os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))), |
| 285 | _path_in_tree)] |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 286 | |
| 287 | def __init__(self, filename=None): |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 288 | super().__init__(self.default_path, 'Mbed TLS', filename) |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 289 | self.current_section = 'header' |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 290 | |
| 291 | |
| 292 | class CryptoConfigFile(config_common.ConfigFile): |
| 293 | """Representation of a Crypto configuration file.""" |
| 294 | |
| 295 | # Temporary, while Mbed TLS does not just rely on the TF-PSA-Crypto |
| 296 | # build system to build its crypto library. When it does, the |
| 297 | # condition can just be removed. |
| 298 | _path_in_tree = ('include/psa/crypto_config.h' |
| 299 | if not os.path.isdir(os.path.join(os.path.dirname(__file__), |
| 300 | os.pardir, |
| 301 | 'tf-psa-crypto')) else |
| 302 | 'tf-psa-crypto/include/psa/crypto_config.h') |
| 303 | default_path = [_path_in_tree, |
| 304 | os.path.join(os.path.dirname(__file__), |
| 305 | os.pardir, |
| 306 | _path_in_tree), |
| 307 | os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))), |
| 308 | _path_in_tree)] |
| 309 | |
| 310 | def __init__(self, filename=None): |
| 311 | super().__init__(self.default_path, 'Crypto', filename) |
| 312 | |
| 313 | |
| 314 | class MbedTLSConfig(config_common.Config): |
| 315 | """Representation of the Mbed TLS configuration. |
| 316 | |
| 317 | See the documentation of the `Config` class for methods to query |
| 318 | and modify the configuration. |
| 319 | """ |
| 320 | |
| 321 | def __init__(self, filename=None): |
| 322 | """Read the Mbed TLS configuration file.""" |
| 323 | |
| 324 | super().__init__() |
| 325 | configfile = MbedTLSConfigFile(filename) |
| 326 | self.configfiles.append(configfile) |
| 327 | self.settings.update({name: config_common.Setting(configfile, active, name, value, section) |
| 328 | for (active, name, value, section) |
| 329 | in configfile.parse_file()}) |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 330 | |
| 331 | def set(self, name, value=None): |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 332 | """Set name to the given value and make it active.""" |
| 333 | |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 334 | if name not in self.settings: |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 335 | self._get_configfile().templates.append((name, '', '#define ' + name + ' ')) |
| 336 | |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 337 | super().set(name, value) |
| 338 | |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 339 | |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 340 | class CryptoConfig(config_common.Config): |
| 341 | """Representation of the PSA crypto configuration. |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 342 | |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 343 | See the documentation of the `Config` class for methods to query |
| 344 | and modify the configuration. |
| 345 | """ |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 346 | |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 347 | def __init__(self, filename=None): |
| 348 | """Read the PSA crypto configuration file.""" |
| 349 | |
| 350 | super().__init__() |
| 351 | configfile = CryptoConfigFile(filename) |
| 352 | self.configfiles.append(configfile) |
| 353 | self.settings.update({name: config_common.Setting(configfile, active, name, value, section) |
| 354 | for (active, name, value, section) |
| 355 | in configfile.parse_file()}) |
| 356 | |
| 357 | def set(self, name, value='1'): |
| 358 | """Set name to the given value and make it active.""" |
| 359 | |
| 360 | if name in PSA_UNSUPPORTED_FEATURE: |
| 361 | raise ValueError(f'Feature is unsupported: \'{name}\'') |
| 362 | if name in PSA_UNSTABLE_FEATURE: |
| 363 | raise ValueError(f'Feature is unstable: \'{name}\'') |
| 364 | |
| 365 | if name not in self.settings: |
| 366 | self._get_configfile().templates.append((name, '', '#define ' + name + ' ')) |
| 367 | |
| 368 | super().set(name, value) |
| 369 | |
| 370 | |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 371 | class MbedTLSConfigTool(config_common.ConfigTool): |
| 372 | """Command line mbedtls_config.h and crypto_config.h manipulation tool.""" |
| 373 | |
| 374 | def __init__(self): |
Gabor Mezei | 8b54f0e | 2024-09-18 16:53:03 +0200 | [diff] [blame] | 375 | super().__init__(MbedTLSConfigFile.default_path) |
Gabor Mezei | 1a0bd77 | 2024-09-04 11:42:43 +0200 | [diff] [blame] | 376 | self.config = MbedTLSConfig(self.args.file) |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 377 | |
| 378 | def custom_parser_options(self): |
| 379 | """Adds MbedTLS specific options for the parser.""" |
| 380 | |
| 381 | self.parser.add_argument( |
| 382 | '--cryptofile', '-c', |
| 383 | help="""Crypto file to read (and modify if requested). Default: {}.""" |
| 384 | .format(CryptoConfigFile.default_path)) |
| 385 | |
| 386 | self.add_adapter( |
| 387 | 'baremetal', baremetal_adapter, |
| 388 | """Like full, but exclude features that require platform features |
| 389 | such as file input-output. |
| 390 | """) |
| 391 | self.add_adapter( |
| 392 | 'baremetal_size', baremetal_size_adapter, |
| 393 | """Like baremetal, but exclude debugging features. Useful for code size measurements. |
| 394 | """) |
| 395 | self.add_adapter( |
| 396 | 'full', full_adapter, |
| 397 | """Uncomment most features. |
| 398 | Exclude alternative implementations and platform support options, as well as |
| 399 | some options that are awkward to test. |
| 400 | """) |
| 401 | self.add_adapter( |
| 402 | 'full_no_deprecated', no_deprecated_adapter(full_adapter), |
| 403 | """Uncomment most non-deprecated features. |
| 404 | Like "full", but without deprecated features. |
| 405 | """) |
| 406 | self.add_adapter( |
| 407 | 'full_no_platform', no_platform_adapter(full_adapter), |
| 408 | """Uncomment most non-platform features. Like "full", but without platform features. |
| 409 | """) |
| 410 | self.add_adapter( |
| 411 | 'realfull', realfull_adapter, |
| 412 | """Uncomment all boolean #defines. |
| 413 | Suitable for generating documentation, but not for building. |
| 414 | """) |
| 415 | self.add_adapter( |
| 416 | 'crypto', crypto_adapter(None), |
| 417 | """Only include crypto features. Exclude X.509 and TLS.""") |
| 418 | self.add_adapter( |
| 419 | 'crypto_baremetal', crypto_adapter(baremetal_adapter), |
| 420 | """Like baremetal, but with only crypto features, excluding X.509 and TLS.""") |
| 421 | self.add_adapter( |
| 422 | 'crypto_full', crypto_adapter(full_adapter), |
| 423 | """Like full, but with only crypto features, excluding X.509 and TLS.""") |
| 424 | |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 425 | |
| 426 | if __name__ == '__main__': |
Gabor Mezei | 634103c | 2024-09-11 13:08:21 +0200 | [diff] [blame] | 427 | sys.exit(MbedTLSConfigTool().main()) |