blob: fadbc7efd60efe8268bb24a6a406706c877e7877 [file] [log] [blame]
Gilles Peskineb4063892019-07-27 21:36:44 +02001#!/usr/bin/env python3
2
Gabor Mezei634103c2024-09-11 13:08:21 +02003"""Mbed TLS and PSA configuration file manipulation library and tool
Gilles Peskineb4063892019-07-27 21:36:44 +02004
Fredrik Hessecc207bc2021-09-28 21:06:08 +02005Basic usage, to read the Mbed TLS configuration:
Gabor Mezei1a0bd772024-09-04 11:42:43 +02006 config = MbedTLSConfig()
Gilles Peskineb4063892019-07-27 21:36:44 +02007 if 'MBEDTLS_RSA_C' in config: print('RSA is enabled')
8"""
9
Bence Szépkúti1e148272020-08-07 13:07:28 +020010## Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000011## SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskineb4063892019-07-27 21:36:44 +020012##
Gilles Peskineb4063892019-07-27 21:36:44 +020013
Gilles Peskine208e4ec2019-07-29 23:43:20 +020014import os
Gabor Mezei634103c2024-09-11 13:08:21 +020015import sys
Gilles Peskineb4063892019-07-27 21:36:44 +020016
Gabor Mezei634103c2024-09-11 13:08:21 +020017import framework_scripts_path # pylint: disable=unused-import
18from mbedtls_framework import config_common
Gilles Peskineb4063892019-07-27 21:36:44 +020019
Gilles Peskine8e90cf42021-05-27 22:12:57 +020020
Gilles Peskine53d41ae2019-07-27 23:31:53 +020021def is_full_section(section):
Gabor Mezei634103c2024-09-11 13:08:21 +020022 """Is this section affected by "config.py full" and friends?
23
24 In a config file where the sections are not used the whole config file
25 is an empty section (with value None) and the whole file is affected.
26 """
27 return section is None or section.endswith('support') or section.endswith('modules')
Gilles Peskine53d41ae2019-07-27 23:31:53 +020028
Gilles Peskine0ff1d982024-09-19 19:49:20 +020029def realfull_adapter(_name, _value, active, section):
Gilles Peskineba4162a2022-04-11 17:04:38 +020030 """Activate all symbols found in the global and boolean feature sections.
31
32 This is intended for building the documentation, including the
33 documentation of settings that are activated by defining an optional
34 preprocessor macro.
35
36 Do not activate definitions in the section containing symbols that are
37 supposed to be defined and documented in their own module.
38 """
39 if section == 'Module configuration options':
Gilles Peskine53d41ae2019-07-27 23:31:53 +020040 return active
Gilles Peskineb4063892019-07-27 21:36:44 +020041 return True
42
Gabor Mezei634103c2024-09-11 13:08:21 +020043PSA_UNSUPPORTED_FEATURE = frozenset([
44 'PSA_WANT_ALG_CBC_MAC',
45 'PSA_WANT_ALG_XTS',
46 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_DERIVE',
47 'PSA_WANT_KEY_TYPE_DH_KEY_PAIR_DERIVE'
48])
49
50PSA_DEPRECATED_FEATURE = frozenset([
51 'PSA_WANT_KEY_TYPE_ECC_KEY_PAIR',
52 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR'
53])
54
55PSA_UNSTABLE_FEATURE = frozenset([
56 'PSA_WANT_ECC_SECP_K1_224'
57])
58
59EXCLUDE_FROM_CRYPTO = PSA_UNSUPPORTED_FEATURE | \
60 PSA_DEPRECATED_FEATURE | \
61 PSA_UNSTABLE_FEATURE
62
Gilles Peskinecfffc282020-04-12 13:55:45 +020063# The goal of the full configuration is to have everything that can be tested
64# together. This includes deprecated or insecure options. It excludes:
65# * Options that require additional build dependencies or unusual hardware.
66# * Options that make testing less effective.
Gilles Peskinec9d04332020-04-16 20:50:17 +020067# * Options that are incompatible with other options, or more generally that
68# interact with other parts of the code in such a way that a bulk enabling
69# is not a good way to test them.
Gilles Peskinecfffc282020-04-12 13:55:45 +020070# * Options that remove features.
Gilles Peskinebbaa2b72020-04-12 13:33:57 +020071EXCLUDE_FROM_FULL = frozenset([
Gilles Peskinecfffc282020-04-12 13:55:45 +020072 #pylint: disable=line-too-long
Yanray Wanga8704672023-04-20 17:16:48 +080073 'MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH', # interacts with CTR_DRBG_128_BIT_KEY
Gilles Peskinea8861e02023-09-05 20:20:51 +020074 'MBEDTLS_AES_USE_HARDWARE_ONLY', # hardware dependency
Yanray Wang42be1ba2023-11-23 14:28:47 +080075 'MBEDTLS_BLOCK_CIPHER_NO_DECRYPT', # incompatible with ECB in PSA, CBC/XTS/NIST_KW/DES
Gilles Peskinec9d04332020-04-16 20:50:17 +020076 'MBEDTLS_CTR_DRBG_USE_128_BIT_KEY', # interacts with ENTROPY_FORCE_SHA256
Gilles Peskinecfffc282020-04-12 13:55:45 +020077 'MBEDTLS_DEPRECATED_REMOVED', # conflicts with deprecated options
Gilles Peskine90581ee2020-04-12 14:02:47 +020078 'MBEDTLS_DEPRECATED_WARNING', # conflicts with deprecated options
Gilles Peskinec9d04332020-04-16 20:50:17 +020079 'MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED', # influences the use of ECDH in TLS
Steven Cooreman77e09b62021-01-22 09:43:27 +010080 'MBEDTLS_ECP_NO_FALLBACK', # removes internal ECP implementation
Janos Follath5b7c38f2023-08-01 08:51:12 +010081 'MBEDTLS_ECP_WITH_MPI_UINT', # disables the default ECP and is experimental
Gilles Peskinec9d04332020-04-16 20:50:17 +020082 'MBEDTLS_ENTROPY_FORCE_SHA256', # interacts with CTR_DRBG_128_BIT_KEY
Gilles Peskinecfffc282020-04-12 13:55:45 +020083 'MBEDTLS_HAVE_SSE2', # hardware dependency
84 'MBEDTLS_MEMORY_BACKTRACE', # depends on MEMORY_BUFFER_ALLOC_C
85 'MBEDTLS_MEMORY_BUFFER_ALLOC_C', # makes sanitizers (e.g. ASan) less effective
86 'MBEDTLS_MEMORY_DEBUG', # depends on MEMORY_BUFFER_ALLOC_C
Gilles Peskinec9d04332020-04-16 20:50:17 +020087 'MBEDTLS_NO_64BIT_MULTIPLICATION', # influences anything that uses bignum
Gilles Peskinecfffc282020-04-12 13:55:45 +020088 'MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES', # removes a feature
89 'MBEDTLS_NO_PLATFORM_ENTROPY', # removes a feature
Gilles Peskinec9d04332020-04-16 20:50:17 +020090 'MBEDTLS_NO_UDBL_DIVISION', # influences anything that uses bignum
Gilles Peskineefaee9a2023-09-20 20:49:47 +020091 'MBEDTLS_PSA_P256M_DRIVER_ENABLED', # influences SECP256R1 KeyGen/ECDH/ECDSA
Gilles Peskinecfffc282020-04-12 13:55:45 +020092 'MBEDTLS_PLATFORM_NO_STD_FUNCTIONS', # removes a feature
David Horstmann6f8c95b2024-03-14 14:52:45 +000093 'MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS', # removes a feature
Gilles Peskinef08b3f82020-11-13 17:36:48 +010094 'MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG', # behavior change + build dependency
Gilles Peskine3415dc82024-09-19 13:43:57 +020095 'MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER', # interface and behavior change
Gilles Peskinecfffc282020-04-12 13:55:45 +020096 'MBEDTLS_PSA_CRYPTO_SPM', # platform dependency (PSA SPM)
Gilles Peskinea08def92023-04-28 21:01:49 +020097 'MBEDTLS_PSA_INJECT_ENTROPY', # conflicts with platform entropy sources
Gilles Peskinec9d04332020-04-16 20:50:17 +020098 'MBEDTLS_RSA_NO_CRT', # influences the use of RSA in X.509 and TLS
Tom Cosgrove87fbfb52022-03-15 10:51:52 +000099 'MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT
Dave Rodgman9be3cf02023-10-11 14:47:55 +0100100 'MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY', # interacts with *_USE_ARMV8_A_CRYPTO_IF_PRESENT
Tom Cosgrove87fbfb52022-03-15 10:51:52 +0000101 'MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT
Dave Rodgman7cb635a2023-10-12 16:14:51 +0100102 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', # setting *_USE_ARMV8_A_CRYPTO is sufficient
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +0200103 'MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN', # build dependency (clang+memsan)
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +0200104 'MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND', # build dependency (valgrind headers)
Hanno Beckere1113562019-06-12 13:59:14 +0100105 'MBEDTLS_X509_REMOVE_INFO', # removes a feature
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200106])
107
Gilles Peskine32e889d2020-04-12 23:43:28 +0200108def is_seamless_alt(name):
Gilles Peskinec34faba2020-04-20 15:44:14 +0200109 """Whether the xxx_ALT symbol should be included in the full configuration.
Gilles Peskine32e889d2020-04-12 23:43:28 +0200110
Gilles Peskinec34faba2020-04-20 15:44:14 +0200111 Include alternative implementations of platform functions, which are
Gilles Peskine32e889d2020-04-12 23:43:28 +0200112 configurable function pointers that default to the built-in function.
113 This way we test that the function pointers exist and build correctly
114 without changing the behavior, and tests can verify that the function
115 pointers are used by modifying those pointers.
116
117 Exclude alternative implementations of library functions since they require
118 an implementation of the relevant functions and an xxx_alt.h header.
119 """
Gilles Peskinea8861e02023-09-05 20:20:51 +0200120 if name in (
121 'MBEDTLS_PLATFORM_GMTIME_R_ALT',
122 'MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT',
123 'MBEDTLS_PLATFORM_MS_TIME_ALT',
124 'MBEDTLS_PLATFORM_ZEROIZE_ALT',
125 ):
Gilles Peskinec34faba2020-04-20 15:44:14 +0200126 # Similar to non-platform xxx_ALT, requires platform_alt.h
127 return False
Gilles Peskine32e889d2020-04-12 23:43:28 +0200128 return name.startswith('MBEDTLS_PLATFORM_')
129
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200130def include_in_full(name):
131 """Rules for symbols in the "full" configuration."""
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200132 if name in EXCLUDE_FROM_FULL:
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200133 return False
134 if name.endswith('_ALT'):
Gilles Peskine32e889d2020-04-12 23:43:28 +0200135 return is_seamless_alt(name)
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200136 return True
137
Gilles Peskine0ff1d982024-09-19 19:49:20 +0200138def full_adapter(name, _value, active, section):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200139 """Config adapter for "full"."""
140 if not is_full_section(section):
141 return active
142 return include_in_full(name)
143
Gilles Peskinecfffc282020-04-12 13:55:45 +0200144# The baremetal configuration excludes options that require a library or
145# operating system feature that is typically not present on bare metal
146# systems. Features that are excluded from "full" won't be in "baremetal"
147# either (unless explicitly turned on in baremetal_adapter) so they don't
148# need to be repeated here.
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200149EXCLUDE_FROM_BAREMETAL = frozenset([
Gilles Peskinecfffc282020-04-12 13:55:45 +0200150 #pylint: disable=line-too-long
Gilles Peskine98f8f952020-04-20 15:38:39 +0200151 'MBEDTLS_ENTROPY_NV_SEED', # requires a filesystem and FS_IO or alternate NV seed hooks
Gilles Peskinecfffc282020-04-12 13:55:45 +0200152 'MBEDTLS_FS_IO', # requires a filesystem
Gilles Peskinecfffc282020-04-12 13:55:45 +0200153 'MBEDTLS_HAVE_TIME', # requires a clock
154 'MBEDTLS_HAVE_TIME_DATE', # requires a clock
155 'MBEDTLS_NET_C', # requires POSIX-like networking
156 'MBEDTLS_PLATFORM_FPRINTF_ALT', # requires FILE* from stdio.h
Gilles Peskine98f8f952020-04-20 15:38:39 +0200157 'MBEDTLS_PLATFORM_NV_SEED_ALT', # requires a filesystem and ENTROPY_NV_SEED
158 'MBEDTLS_PLATFORM_TIME_ALT', # requires a clock and HAVE_TIME
159 'MBEDTLS_PSA_CRYPTO_SE_C', # requires a filesystem and PSA_CRYPTO_STORAGE_C
Gilles Peskinecfffc282020-04-12 13:55:45 +0200160 'MBEDTLS_PSA_CRYPTO_STORAGE_C', # requires a filesystem
161 'MBEDTLS_PSA_ITS_FILE_C', # requires a filesystem
162 'MBEDTLS_THREADING_C', # requires a threading interface
163 'MBEDTLS_THREADING_PTHREAD', # requires pthread
164 'MBEDTLS_TIMING_C', # requires a clock
Dave Rodgman9be3cf02023-10-11 14:47:55 +0100165 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection
Dave Rodgman5b89c552023-10-10 14:59:02 +0100166 'MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection
Dave Rodgmanbe7915a2023-10-11 10:46:38 +0100167 'MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200168])
169
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200170def keep_in_baremetal(name):
171 """Rules for symbols in the "baremetal" configuration."""
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200172 if name in EXCLUDE_FROM_BAREMETAL:
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200173 return False
174 return True
175
Gilles Peskine0ff1d982024-09-19 19:49:20 +0200176def baremetal_adapter(name, _value, active, section):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200177 """Config adapter for "baremetal"."""
178 if not is_full_section(section):
179 return active
180 if name == 'MBEDTLS_NO_PLATFORM_ENTROPY':
Gilles Peskinecfffc282020-04-12 13:55:45 +0200181 # No OS-provided entropy source
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200182 return True
183 return include_in_full(name) and keep_in_baremetal(name)
184
Gilles Peskine120f29d2021-09-01 19:51:19 +0200185# This set contains options that are mostly for debugging or test purposes,
186# and therefore should be excluded when doing code size measurements.
187# Options that are their own module (such as MBEDTLS_ERROR_C) are not listed
188# and therefore will be included when doing code size measurements.
189EXCLUDE_FOR_SIZE = frozenset([
190 'MBEDTLS_DEBUG_C', # large code size increase in TLS
191 'MBEDTLS_SELF_TEST', # increases the size of many modules
192 'MBEDTLS_TEST_HOOKS', # only useful with the hosted test framework, increases code size
193])
194
Gilles Peskine0ff1d982024-09-19 19:49:20 +0200195def baremetal_size_adapter(name, value, active, section):
Gilles Peskine120f29d2021-09-01 19:51:19 +0200196 if name in EXCLUDE_FOR_SIZE:
197 return False
Gilles Peskine0ff1d982024-09-19 19:49:20 +0200198 return baremetal_adapter(name, value, active, section)
Gilles Peskine120f29d2021-09-01 19:51:19 +0200199
Gilles Peskine31987c62020-01-31 14:23:30 +0100200def include_in_crypto(name):
201 """Rules for symbols in a crypto configuration."""
202 if name.startswith('MBEDTLS_X509_') or \
203 name.startswith('MBEDTLS_SSL_') or \
204 name.startswith('MBEDTLS_KEY_EXCHANGE_'):
205 return False
206 if name in [
Gilles Peskinecfffc282020-04-12 13:55:45 +0200207 'MBEDTLS_DEBUG_C', # part of libmbedtls
208 'MBEDTLS_NET_C', # part of libmbedtls
Nayna Jainc9deb182020-11-16 19:03:12 +0000209 'MBEDTLS_PKCS7_C', # part of libmbedx509
Gilles Peskine31987c62020-01-31 14:23:30 +0100210 ]:
211 return False
212 return True
213
214def crypto_adapter(adapter):
215 """Modify an adapter to disable non-crypto symbols.
216
Gilles Peskine0ff1d982024-09-19 19:49:20 +0200217 ``crypto_adapter(adapter)(name, value, active, section)`` is like
218 ``adapter(name, value, active, section)``, but unsets all X.509 and TLS symbols.
Gilles Peskine31987c62020-01-31 14:23:30 +0100219 """
Gilles Peskine0ff1d982024-09-19 19:49:20 +0200220 def continuation(name, value, active, section):
Gilles Peskine31987c62020-01-31 14:23:30 +0100221 if not include_in_crypto(name):
222 return False
223 if adapter is None:
224 return active
Gilles Peskine0ff1d982024-09-19 19:49:20 +0200225 return adapter(name, value, active, section)
Gilles Peskine31987c62020-01-31 14:23:30 +0100226 return continuation
227
Gilles Peskineed5c21d2022-06-27 23:02:09 +0200228DEPRECATED = frozenset([
229 'MBEDTLS_PSA_CRYPTO_SE_C',
230])
Gilles Peskine30de2e82020-04-20 21:39:22 +0200231def no_deprecated_adapter(adapter):
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200232 """Modify an adapter to disable deprecated symbols.
233
Gilles Peskine0ff1d982024-09-19 19:49:20 +0200234 ``no_deprecated_adapter(adapter)(name, value, active, section)`` is like
235 ``adapter(name, value, active, section)``, but unsets all deprecated symbols
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200236 and sets ``MBEDTLS_DEPRECATED_REMOVED``.
237 """
Gilles Peskine0ff1d982024-09-19 19:49:20 +0200238 def continuation(name, value, active, section):
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200239 if name == 'MBEDTLS_DEPRECATED_REMOVED':
240 return True
Gilles Peskineed5c21d2022-06-27 23:02:09 +0200241 if name in DEPRECATED:
242 return False
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200243 if adapter is None:
244 return active
Gilles Peskine0ff1d982024-09-19 19:49:20 +0200245 return adapter(name, value, active, section)
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200246 return continuation
247
Paul Elliottfb81f772023-10-18 17:44:59 +0100248def no_platform_adapter(adapter):
249 """Modify an adapter to disable platform symbols.
250
Gilles Peskine0ff1d982024-09-19 19:49:20 +0200251 ``no_platform_adapter(adapter)(name, value, active, section)`` is like
252 ``adapter(name, value, active, section)``, but unsets all platform symbols other
Paul Elliottfb81f772023-10-18 17:44:59 +0100253 ``than MBEDTLS_PLATFORM_C.
254 """
Gilles Peskine0ff1d982024-09-19 19:49:20 +0200255 def continuation(name, value, active, section):
Paul Elliottfb81f772023-10-18 17:44:59 +0100256 # Allow MBEDTLS_PLATFORM_C but remove all other platform symbols.
257 if name.startswith('MBEDTLS_PLATFORM_') and name != 'MBEDTLS_PLATFORM_C':
258 return False
259 if adapter is None:
260 return active
Gilles Peskine0ff1d982024-09-19 19:49:20 +0200261 return adapter(name, value, active, section)
Paul Elliottfb81f772023-10-18 17:44:59 +0100262 return continuation
263
Gilles Peskineb4063892019-07-27 21:36:44 +0200264
Gabor Mezei634103c2024-09-11 13:08:21 +0200265class MbedTLSConfigFile(config_common.ConfigFile):
266 """Representation of an MbedTLS configuration file."""
Gilles Peskineb4063892019-07-27 21:36:44 +0200267
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +0200268 _path_in_tree = 'include/mbedtls/mbedtls_config.h'
Gilles Peskine208e4ec2019-07-29 23:43:20 +0200269 default_path = [_path_in_tree,
270 os.path.join(os.path.dirname(__file__),
271 os.pardir,
272 _path_in_tree),
273 os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))),
274 _path_in_tree)]
Gilles Peskineb4063892019-07-27 21:36:44 +0200275
276 def __init__(self, filename=None):
Gabor Mezei634103c2024-09-11 13:08:21 +0200277 super().__init__(self.default_path, 'Mbed TLS', filename)
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200278 self.current_section = 'header'
Gabor Mezei634103c2024-09-11 13:08:21 +0200279
280
281class CryptoConfigFile(config_common.ConfigFile):
282 """Representation of a Crypto configuration file."""
283
284 # Temporary, while Mbed TLS does not just rely on the TF-PSA-Crypto
285 # build system to build its crypto library. When it does, the
286 # condition can just be removed.
287 _path_in_tree = ('include/psa/crypto_config.h'
288 if not os.path.isdir(os.path.join(os.path.dirname(__file__),
289 os.pardir,
290 'tf-psa-crypto')) else
291 'tf-psa-crypto/include/psa/crypto_config.h')
292 default_path = [_path_in_tree,
293 os.path.join(os.path.dirname(__file__),
294 os.pardir,
295 _path_in_tree),
296 os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))),
297 _path_in_tree)]
298
299 def __init__(self, filename=None):
300 super().__init__(self.default_path, 'Crypto', filename)
301
302
303class MbedTLSConfig(config_common.Config):
304 """Representation of the Mbed TLS configuration.
305
306 See the documentation of the `Config` class for methods to query
307 and modify the configuration.
308 """
309
310 def __init__(self, filename=None):
311 """Read the Mbed TLS configuration file."""
312
313 super().__init__()
314 configfile = MbedTLSConfigFile(filename)
315 self.configfiles.append(configfile)
316 self.settings.update({name: config_common.Setting(configfile, active, name, value, section)
317 for (active, name, value, section)
318 in configfile.parse_file()})
Gilles Peskineb4063892019-07-27 21:36:44 +0200319
320 def set(self, name, value=None):
Gabor Mezei634103c2024-09-11 13:08:21 +0200321 """Set name to the given value and make it active."""
322
Gilles Peskineb4063892019-07-27 21:36:44 +0200323 if name not in self.settings:
Gabor Mezei634103c2024-09-11 13:08:21 +0200324 self._get_configfile().templates.append((name, '', '#define ' + name + ' '))
325
Gilles Peskineb4063892019-07-27 21:36:44 +0200326 super().set(name, value)
327
Gilles Peskineb4063892019-07-27 21:36:44 +0200328
Gabor Mezei634103c2024-09-11 13:08:21 +0200329class CryptoConfig(config_common.Config):
330 """Representation of the PSA crypto configuration.
Gilles Peskineb4063892019-07-27 21:36:44 +0200331
Gabor Mezei634103c2024-09-11 13:08:21 +0200332 See the documentation of the `Config` class for methods to query
333 and modify the configuration.
334 """
Gilles Peskineb4063892019-07-27 21:36:44 +0200335
Gabor Mezei634103c2024-09-11 13:08:21 +0200336 def __init__(self, filename=None):
337 """Read the PSA crypto configuration file."""
338
339 super().__init__()
340 configfile = CryptoConfigFile(filename)
341 self.configfiles.append(configfile)
342 self.settings.update({name: config_common.Setting(configfile, active, name, value, section)
343 for (active, name, value, section)
344 in configfile.parse_file()})
345
346 def set(self, name, value='1'):
347 """Set name to the given value and make it active."""
348
349 if name in PSA_UNSUPPORTED_FEATURE:
350 raise ValueError(f'Feature is unsupported: \'{name}\'')
351 if name in PSA_UNSTABLE_FEATURE:
352 raise ValueError(f'Feature is unstable: \'{name}\'')
353
354 if name not in self.settings:
355 self._get_configfile().templates.append((name, '', '#define ' + name + ' '))
356
357 super().set(name, value)
358
359
Gabor Mezei634103c2024-09-11 13:08:21 +0200360class MbedTLSConfigTool(config_common.ConfigTool):
361 """Command line mbedtls_config.h and crypto_config.h manipulation tool."""
362
363 def __init__(self):
Gabor Mezei8b54f0e2024-09-18 16:53:03 +0200364 super().__init__(MbedTLSConfigFile.default_path)
Gabor Mezei1a0bd772024-09-04 11:42:43 +0200365 self.config = MbedTLSConfig(self.args.file)
Gabor Mezei634103c2024-09-11 13:08:21 +0200366
367 def custom_parser_options(self):
368 """Adds MbedTLS specific options for the parser."""
369
370 self.parser.add_argument(
371 '--cryptofile', '-c',
372 help="""Crypto file to read (and modify if requested). Default: {}."""
373 .format(CryptoConfigFile.default_path))
374
375 self.add_adapter(
376 'baremetal', baremetal_adapter,
377 """Like full, but exclude features that require platform features
378 such as file input-output.
379 """)
380 self.add_adapter(
381 'baremetal_size', baremetal_size_adapter,
382 """Like baremetal, but exclude debugging features. Useful for code size measurements.
383 """)
384 self.add_adapter(
385 'full', full_adapter,
386 """Uncomment most features.
387 Exclude alternative implementations and platform support options, as well as
388 some options that are awkward to test.
389 """)
390 self.add_adapter(
391 'full_no_deprecated', no_deprecated_adapter(full_adapter),
392 """Uncomment most non-deprecated features.
393 Like "full", but without deprecated features.
394 """)
395 self.add_adapter(
396 'full_no_platform', no_platform_adapter(full_adapter),
397 """Uncomment most non-platform features. Like "full", but without platform features.
398 """)
399 self.add_adapter(
400 'realfull', realfull_adapter,
401 """Uncomment all boolean #defines.
402 Suitable for generating documentation, but not for building.
403 """)
404 self.add_adapter(
405 'crypto', crypto_adapter(None),
406 """Only include crypto features. Exclude X.509 and TLS.""")
407 self.add_adapter(
408 'crypto_baremetal', crypto_adapter(baremetal_adapter),
409 """Like baremetal, but with only crypto features, excluding X.509 and TLS.""")
410 self.add_adapter(
411 'crypto_full', crypto_adapter(full_adapter),
412 """Like full, but with only crypto features, excluding X.509 and TLS.""")
413
Gilles Peskineb4063892019-07-27 21:36:44 +0200414
415if __name__ == '__main__':
Gabor Mezei634103c2024-09-11 13:08:21 +0200416 sys.exit(MbedTLSConfigTool().main())