blob: ef13062936975e0e582d93900bf2036a260303f7 [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 Peskinebfdffc32024-09-19 19:57:58 +020021def is_boolean_setting(name, value):
22 """Is this a boolean setting?
Gabor Mezei634103c2024-09-11 13:08:21 +020023
Gilles Peskinebfdffc32024-09-19 19:57:58 +020024 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 Mezei634103c2024-09-11 13:08:21 +020033 """
Gilles Peskinebfdffc32024-09-19 19:57:58 +020034 if name.startswith('PSA_WANT_'):
35 return True
36 if not value:
37 return True
38 return False
Gilles Peskine53d41ae2019-07-27 23:31:53 +020039
Gilles Peskinef5f90d52024-09-19 20:13:49 +020040def realfull_adapter(_name, _value, _active):
Gilles Peskinee5920a42024-09-19 19:58:56 +020041 """Activate all symbols.
Gilles Peskineba4162a2022-04-11 17:04:38 +020042
43 This is intended for building the documentation, including the
44 documentation of settings that are activated by defining an optional
Gilles Peskinee5920a42024-09-19 19:58:56 +020045 preprocessor macro. There is no expectation that the resulting
46 configuration can be built.
Gilles Peskineba4162a2022-04-11 17:04:38 +020047 """
Gilles Peskineb4063892019-07-27 21:36:44 +020048 return True
49
Gabor Mezei634103c2024-09-11 13:08:21 +020050PSA_UNSUPPORTED_FEATURE = frozenset([
51 'PSA_WANT_ALG_CBC_MAC',
52 'PSA_WANT_ALG_XTS',
53 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_DERIVE',
54 'PSA_WANT_KEY_TYPE_DH_KEY_PAIR_DERIVE'
55])
56
57PSA_DEPRECATED_FEATURE = frozenset([
58 'PSA_WANT_KEY_TYPE_ECC_KEY_PAIR',
59 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR'
60])
61
62PSA_UNSTABLE_FEATURE = frozenset([
63 'PSA_WANT_ECC_SECP_K1_224'
64])
65
66EXCLUDE_FROM_CRYPTO = PSA_UNSUPPORTED_FEATURE | \
67 PSA_DEPRECATED_FEATURE | \
68 PSA_UNSTABLE_FEATURE
69
Gilles Peskinecfffc282020-04-12 13:55:45 +020070# The goal of the full configuration is to have everything that can be tested
71# together. This includes deprecated or insecure options. It excludes:
72# * Options that require additional build dependencies or unusual hardware.
73# * Options that make testing less effective.
Gilles Peskinec9d04332020-04-16 20:50:17 +020074# * Options that are incompatible with other options, or more generally that
75# interact with other parts of the code in such a way that a bulk enabling
76# is not a good way to test them.
Gilles Peskinecfffc282020-04-12 13:55:45 +020077# * Options that remove features.
Gilles Peskinebbaa2b72020-04-12 13:33:57 +020078EXCLUDE_FROM_FULL = frozenset([
Gilles Peskinecfffc282020-04-12 13:55:45 +020079 #pylint: disable=line-too-long
Yanray Wanga8704672023-04-20 17:16:48 +080080 'MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH', # interacts with CTR_DRBG_128_BIT_KEY
Gilles Peskinea8861e02023-09-05 20:20:51 +020081 'MBEDTLS_AES_USE_HARDWARE_ONLY', # hardware dependency
Yanray Wang42be1ba2023-11-23 14:28:47 +080082 'MBEDTLS_BLOCK_CIPHER_NO_DECRYPT', # incompatible with ECB in PSA, CBC/XTS/NIST_KW/DES
Gilles Peskinec9d04332020-04-16 20:50:17 +020083 'MBEDTLS_CTR_DRBG_USE_128_BIT_KEY', # interacts with ENTROPY_FORCE_SHA256
Gilles Peskinecfffc282020-04-12 13:55:45 +020084 'MBEDTLS_DEPRECATED_REMOVED', # conflicts with deprecated options
Gilles Peskine90581ee2020-04-12 14:02:47 +020085 'MBEDTLS_DEPRECATED_WARNING', # conflicts with deprecated options
Gilles Peskinec9d04332020-04-16 20:50:17 +020086 'MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED', # influences the use of ECDH in TLS
Steven Cooreman77e09b62021-01-22 09:43:27 +010087 'MBEDTLS_ECP_NO_FALLBACK', # removes internal ECP implementation
Janos Follath5b7c38f2023-08-01 08:51:12 +010088 'MBEDTLS_ECP_WITH_MPI_UINT', # disables the default ECP and is experimental
Gilles Peskinec9d04332020-04-16 20:50:17 +020089 'MBEDTLS_ENTROPY_FORCE_SHA256', # interacts with CTR_DRBG_128_BIT_KEY
Gilles Peskinecfffc282020-04-12 13:55:45 +020090 'MBEDTLS_HAVE_SSE2', # hardware dependency
91 'MBEDTLS_MEMORY_BACKTRACE', # depends on MEMORY_BUFFER_ALLOC_C
92 'MBEDTLS_MEMORY_BUFFER_ALLOC_C', # makes sanitizers (e.g. ASan) less effective
93 'MBEDTLS_MEMORY_DEBUG', # depends on MEMORY_BUFFER_ALLOC_C
Gilles Peskinec9d04332020-04-16 20:50:17 +020094 'MBEDTLS_NO_64BIT_MULTIPLICATION', # influences anything that uses bignum
Gilles Peskinecfffc282020-04-12 13:55:45 +020095 'MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES', # removes a feature
96 'MBEDTLS_NO_PLATFORM_ENTROPY', # removes a feature
Gilles Peskinec9d04332020-04-16 20:50:17 +020097 'MBEDTLS_NO_UDBL_DIVISION', # influences anything that uses bignum
Gilles Peskineefaee9a2023-09-20 20:49:47 +020098 'MBEDTLS_PSA_P256M_DRIVER_ENABLED', # influences SECP256R1 KeyGen/ECDH/ECDSA
Gilles Peskinecfffc282020-04-12 13:55:45 +020099 'MBEDTLS_PLATFORM_NO_STD_FUNCTIONS', # removes a feature
David Horstmann6f8c95b2024-03-14 14:52:45 +0000100 'MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS', # removes a feature
Gilles Peskinef08b3f82020-11-13 17:36:48 +0100101 'MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG', # behavior change + build dependency
Gilles Peskine3415dc82024-09-19 13:43:57 +0200102 'MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER', # interface and behavior change
Gilles Peskinecfffc282020-04-12 13:55:45 +0200103 'MBEDTLS_PSA_CRYPTO_SPM', # platform dependency (PSA SPM)
Gilles Peskinea08def92023-04-28 21:01:49 +0200104 'MBEDTLS_PSA_INJECT_ENTROPY', # conflicts with platform entropy sources
Gilles Peskinec9d04332020-04-16 20:50:17 +0200105 'MBEDTLS_RSA_NO_CRT', # influences the use of RSA in X.509 and TLS
Tom Cosgrove87fbfb52022-03-15 10:51:52 +0000106 'MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT
Dave Rodgman9be3cf02023-10-11 14:47:55 +0100107 'MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY', # interacts with *_USE_ARMV8_A_CRYPTO_IF_PRESENT
Tom Cosgrove87fbfb52022-03-15 10:51:52 +0000108 'MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT
Dave Rodgman7cb635a2023-10-12 16:14:51 +0100109 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', # setting *_USE_ARMV8_A_CRYPTO is sufficient
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +0200110 'MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN', # build dependency (clang+memsan)
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +0200111 'MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND', # build dependency (valgrind headers)
Hanno Beckere1113562019-06-12 13:59:14 +0100112 'MBEDTLS_X509_REMOVE_INFO', # removes a feature
Valerio Setti8d4f1502024-06-14 07:49:05 +0200113 'MBEDTLS_PSA_STATIC_KEY_SLOTS', # only relevant for embedded devices
114 'MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE', # only relevant for embedded devices
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200115])
116
Gilles Peskine32e889d2020-04-12 23:43:28 +0200117def is_seamless_alt(name):
Gilles Peskinec34faba2020-04-20 15:44:14 +0200118 """Whether the xxx_ALT symbol should be included in the full configuration.
Gilles Peskine32e889d2020-04-12 23:43:28 +0200119
Gilles Peskinec34faba2020-04-20 15:44:14 +0200120 Include alternative implementations of platform functions, which are
Gilles Peskine32e889d2020-04-12 23:43:28 +0200121 configurable function pointers that default to the built-in function.
122 This way we test that the function pointers exist and build correctly
123 without changing the behavior, and tests can verify that the function
124 pointers are used by modifying those pointers.
125
126 Exclude alternative implementations of library functions since they require
127 an implementation of the relevant functions and an xxx_alt.h header.
128 """
Gilles Peskinea8861e02023-09-05 20:20:51 +0200129 if name in (
130 'MBEDTLS_PLATFORM_GMTIME_R_ALT',
131 'MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT',
132 'MBEDTLS_PLATFORM_MS_TIME_ALT',
133 'MBEDTLS_PLATFORM_ZEROIZE_ALT',
134 ):
Gilles Peskinec34faba2020-04-20 15:44:14 +0200135 # Similar to non-platform xxx_ALT, requires platform_alt.h
136 return False
Gilles Peskine32e889d2020-04-12 23:43:28 +0200137 return name.startswith('MBEDTLS_PLATFORM_')
138
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200139def include_in_full(name):
140 """Rules for symbols in the "full" configuration."""
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200141 if name in EXCLUDE_FROM_FULL:
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200142 return False
143 if name.endswith('_ALT'):
Gilles Peskine32e889d2020-04-12 23:43:28 +0200144 return is_seamless_alt(name)
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200145 return True
146
Gilles Peskinef5f90d52024-09-19 20:13:49 +0200147def full_adapter(name, value, active):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200148 """Config adapter for "full"."""
Gilles Peskinebfdffc32024-09-19 19:57:58 +0200149 if not is_boolean_setting(name, value):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200150 return active
151 return include_in_full(name)
152
Gilles Peskinecfffc282020-04-12 13:55:45 +0200153# The baremetal configuration excludes options that require a library or
154# operating system feature that is typically not present on bare metal
155# systems. Features that are excluded from "full" won't be in "baremetal"
156# either (unless explicitly turned on in baremetal_adapter) so they don't
157# need to be repeated here.
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200158EXCLUDE_FROM_BAREMETAL = frozenset([
Gilles Peskinecfffc282020-04-12 13:55:45 +0200159 #pylint: disable=line-too-long
Gilles Peskine98f8f952020-04-20 15:38:39 +0200160 'MBEDTLS_ENTROPY_NV_SEED', # requires a filesystem and FS_IO or alternate NV seed hooks
Gilles Peskinecfffc282020-04-12 13:55:45 +0200161 'MBEDTLS_FS_IO', # requires a filesystem
Gilles Peskinecfffc282020-04-12 13:55:45 +0200162 'MBEDTLS_HAVE_TIME', # requires a clock
163 'MBEDTLS_HAVE_TIME_DATE', # requires a clock
164 'MBEDTLS_NET_C', # requires POSIX-like networking
165 'MBEDTLS_PLATFORM_FPRINTF_ALT', # requires FILE* from stdio.h
Gilles Peskine98f8f952020-04-20 15:38:39 +0200166 'MBEDTLS_PLATFORM_NV_SEED_ALT', # requires a filesystem and ENTROPY_NV_SEED
167 'MBEDTLS_PLATFORM_TIME_ALT', # requires a clock and HAVE_TIME
168 'MBEDTLS_PSA_CRYPTO_SE_C', # requires a filesystem and PSA_CRYPTO_STORAGE_C
Gilles Peskinecfffc282020-04-12 13:55:45 +0200169 'MBEDTLS_PSA_CRYPTO_STORAGE_C', # requires a filesystem
170 'MBEDTLS_PSA_ITS_FILE_C', # requires a filesystem
171 'MBEDTLS_THREADING_C', # requires a threading interface
172 'MBEDTLS_THREADING_PTHREAD', # requires pthread
173 'MBEDTLS_TIMING_C', # requires a clock
Dave Rodgman9be3cf02023-10-11 14:47:55 +0100174 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection
Dave Rodgman5b89c552023-10-10 14:59:02 +0100175 'MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection
Dave Rodgmanbe7915a2023-10-11 10:46:38 +0100176 'MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200177])
178
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200179def keep_in_baremetal(name):
180 """Rules for symbols in the "baremetal" configuration."""
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200181 if name in EXCLUDE_FROM_BAREMETAL:
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200182 return False
183 return True
184
Gilles Peskinef5f90d52024-09-19 20:13:49 +0200185def baremetal_adapter(name, value, active):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200186 """Config adapter for "baremetal"."""
Gilles Peskinebfdffc32024-09-19 19:57:58 +0200187 if not is_boolean_setting(name, value):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200188 return active
189 if name == 'MBEDTLS_NO_PLATFORM_ENTROPY':
Gilles Peskinecfffc282020-04-12 13:55:45 +0200190 # No OS-provided entropy source
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200191 return True
192 return include_in_full(name) and keep_in_baremetal(name)
193
Gilles Peskine120f29d2021-09-01 19:51:19 +0200194# This set contains options that are mostly for debugging or test purposes,
195# and therefore should be excluded when doing code size measurements.
196# Options that are their own module (such as MBEDTLS_ERROR_C) are not listed
197# and therefore will be included when doing code size measurements.
198EXCLUDE_FOR_SIZE = frozenset([
199 'MBEDTLS_DEBUG_C', # large code size increase in TLS
200 'MBEDTLS_SELF_TEST', # increases the size of many modules
201 'MBEDTLS_TEST_HOOKS', # only useful with the hosted test framework, increases code size
202])
203
Gilles Peskinef5f90d52024-09-19 20:13:49 +0200204def baremetal_size_adapter(name, value, active):
Gilles Peskine120f29d2021-09-01 19:51:19 +0200205 if name in EXCLUDE_FOR_SIZE:
206 return False
Gilles Peskinef5f90d52024-09-19 20:13:49 +0200207 return baremetal_adapter(name, value, active)
Gilles Peskine120f29d2021-09-01 19:51:19 +0200208
Gilles Peskine31987c62020-01-31 14:23:30 +0100209def include_in_crypto(name):
210 """Rules for symbols in a crypto configuration."""
211 if name.startswith('MBEDTLS_X509_') or \
212 name.startswith('MBEDTLS_SSL_') or \
213 name.startswith('MBEDTLS_KEY_EXCHANGE_'):
214 return False
215 if name in [
Gilles Peskinecfffc282020-04-12 13:55:45 +0200216 'MBEDTLS_DEBUG_C', # part of libmbedtls
217 'MBEDTLS_NET_C', # part of libmbedtls
Nayna Jainc9deb182020-11-16 19:03:12 +0000218 'MBEDTLS_PKCS7_C', # part of libmbedx509
Gilles Peskine31987c62020-01-31 14:23:30 +0100219 ]:
220 return False
221 return True
222
223def crypto_adapter(adapter):
224 """Modify an adapter to disable non-crypto symbols.
225
Gilles Peskinef5f90d52024-09-19 20:13:49 +0200226 ``crypto_adapter(adapter)(name, value, active)`` is like
227 ``adapter(name, value, active)``, but unsets all X.509 and TLS symbols.
Gilles Peskine31987c62020-01-31 14:23:30 +0100228 """
Gilles Peskinef5f90d52024-09-19 20:13:49 +0200229 def continuation(name, value, active):
Gilles Peskine31987c62020-01-31 14:23:30 +0100230 if not include_in_crypto(name):
231 return False
232 if adapter is None:
233 return active
Gilles Peskinef5f90d52024-09-19 20:13:49 +0200234 return adapter(name, value, active)
Gilles Peskine31987c62020-01-31 14:23:30 +0100235 return continuation
236
Gilles Peskineed5c21d2022-06-27 23:02:09 +0200237DEPRECATED = frozenset([
238 'MBEDTLS_PSA_CRYPTO_SE_C',
239])
Gilles Peskine30de2e82020-04-20 21:39:22 +0200240def no_deprecated_adapter(adapter):
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200241 """Modify an adapter to disable deprecated symbols.
242
Gilles Peskinef5f90d52024-09-19 20:13:49 +0200243 ``no_deprecated_adapter(adapter)(name, value, active)`` is like
244 ``adapter(name, value, active)``, but unsets all deprecated symbols
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200245 and sets ``MBEDTLS_DEPRECATED_REMOVED``.
246 """
Gilles Peskinef5f90d52024-09-19 20:13:49 +0200247 def continuation(name, value, active):
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200248 if name == 'MBEDTLS_DEPRECATED_REMOVED':
249 return True
Gilles Peskineed5c21d2022-06-27 23:02:09 +0200250 if name in DEPRECATED:
251 return False
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200252 if adapter is None:
253 return active
Gilles Peskinef5f90d52024-09-19 20:13:49 +0200254 return adapter(name, value, active)
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200255 return continuation
256
Paul Elliottfb81f772023-10-18 17:44:59 +0100257def no_platform_adapter(adapter):
258 """Modify an adapter to disable platform symbols.
259
Gilles Peskinef5f90d52024-09-19 20:13:49 +0200260 ``no_platform_adapter(adapter)(name, value, active)`` is like
261 ``adapter(name, value, active)``, but unsets all platform symbols other
Paul Elliottfb81f772023-10-18 17:44:59 +0100262 ``than MBEDTLS_PLATFORM_C.
263 """
Gilles Peskinef5f90d52024-09-19 20:13:49 +0200264 def continuation(name, value, active):
Paul Elliottfb81f772023-10-18 17:44:59 +0100265 # Allow MBEDTLS_PLATFORM_C but remove all other platform symbols.
266 if name.startswith('MBEDTLS_PLATFORM_') and name != 'MBEDTLS_PLATFORM_C':
267 return False
268 if adapter is None:
269 return active
Gilles Peskinef5f90d52024-09-19 20:13:49 +0200270 return adapter(name, value, active)
Paul Elliottfb81f772023-10-18 17:44:59 +0100271 return continuation
272
Gilles Peskineb4063892019-07-27 21:36:44 +0200273
Gabor Mezei634103c2024-09-11 13:08:21 +0200274class MbedTLSConfigFile(config_common.ConfigFile):
275 """Representation of an MbedTLS configuration file."""
Gilles Peskineb4063892019-07-27 21:36:44 +0200276
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +0200277 _path_in_tree = 'include/mbedtls/mbedtls_config.h'
Gilles Peskine208e4ec2019-07-29 23:43:20 +0200278 default_path = [_path_in_tree,
279 os.path.join(os.path.dirname(__file__),
280 os.pardir,
281 _path_in_tree),
282 os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))),
283 _path_in_tree)]
Gilles Peskineb4063892019-07-27 21:36:44 +0200284
285 def __init__(self, filename=None):
Gabor Mezei634103c2024-09-11 13:08:21 +0200286 super().__init__(self.default_path, 'Mbed TLS', filename)
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200287 self.current_section = 'header'
Gabor Mezei634103c2024-09-11 13:08:21 +0200288
289
290class CryptoConfigFile(config_common.ConfigFile):
291 """Representation of a Crypto configuration file."""
292
293 # Temporary, while Mbed TLS does not just rely on the TF-PSA-Crypto
294 # build system to build its crypto library. When it does, the
295 # condition can just be removed.
296 _path_in_tree = ('include/psa/crypto_config.h'
297 if not os.path.isdir(os.path.join(os.path.dirname(__file__),
298 os.pardir,
299 'tf-psa-crypto')) else
300 'tf-psa-crypto/include/psa/crypto_config.h')
301 default_path = [_path_in_tree,
302 os.path.join(os.path.dirname(__file__),
303 os.pardir,
304 _path_in_tree),
305 os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))),
306 _path_in_tree)]
307
308 def __init__(self, filename=None):
309 super().__init__(self.default_path, 'Crypto', filename)
310
311
312class MbedTLSConfig(config_common.Config):
313 """Representation of the Mbed TLS configuration.
314
315 See the documentation of the `Config` class for methods to query
316 and modify the configuration.
317 """
318
319 def __init__(self, filename=None):
320 """Read the Mbed TLS configuration file."""
321
322 super().__init__()
323 configfile = MbedTLSConfigFile(filename)
324 self.configfiles.append(configfile)
325 self.settings.update({name: config_common.Setting(configfile, active, name, value, section)
326 for (active, name, value, section)
327 in configfile.parse_file()})
Gilles Peskineb4063892019-07-27 21:36:44 +0200328
329 def set(self, name, value=None):
Gabor Mezei634103c2024-09-11 13:08:21 +0200330 """Set name to the given value and make it active."""
331
Gilles Peskineb4063892019-07-27 21:36:44 +0200332 if name not in self.settings:
Gabor Mezei634103c2024-09-11 13:08:21 +0200333 self._get_configfile().templates.append((name, '', '#define ' + name + ' '))
334
Gilles Peskineb4063892019-07-27 21:36:44 +0200335 super().set(name, value)
336
Gilles Peskineb4063892019-07-27 21:36:44 +0200337
Gabor Mezei634103c2024-09-11 13:08:21 +0200338class CryptoConfig(config_common.Config):
339 """Representation of the PSA crypto configuration.
Gilles Peskineb4063892019-07-27 21:36:44 +0200340
Gabor Mezei634103c2024-09-11 13:08:21 +0200341 See the documentation of the `Config` class for methods to query
342 and modify the configuration.
343 """
Gilles Peskineb4063892019-07-27 21:36:44 +0200344
Gabor Mezei634103c2024-09-11 13:08:21 +0200345 def __init__(self, filename=None):
346 """Read the PSA crypto configuration file."""
347
348 super().__init__()
349 configfile = CryptoConfigFile(filename)
350 self.configfiles.append(configfile)
351 self.settings.update({name: config_common.Setting(configfile, active, name, value, section)
352 for (active, name, value, section)
353 in configfile.parse_file()})
354
355 def set(self, name, value='1'):
356 """Set name to the given value and make it active."""
357
358 if name in PSA_UNSUPPORTED_FEATURE:
359 raise ValueError(f'Feature is unsupported: \'{name}\'')
360 if name in PSA_UNSTABLE_FEATURE:
361 raise ValueError(f'Feature is unstable: \'{name}\'')
362
363 if name not in self.settings:
364 self._get_configfile().templates.append((name, '', '#define ' + name + ' '))
365
366 super().set(name, value)
367
368
Gabor Mezei634103c2024-09-11 13:08:21 +0200369class MbedTLSConfigTool(config_common.ConfigTool):
370 """Command line mbedtls_config.h and crypto_config.h manipulation tool."""
371
372 def __init__(self):
Gabor Mezei8b54f0e2024-09-18 16:53:03 +0200373 super().__init__(MbedTLSConfigFile.default_path)
Gabor Mezei1a0bd772024-09-04 11:42:43 +0200374 self.config = MbedTLSConfig(self.args.file)
Gabor Mezei634103c2024-09-11 13:08:21 +0200375
376 def custom_parser_options(self):
377 """Adds MbedTLS specific options for the parser."""
378
379 self.parser.add_argument(
380 '--cryptofile', '-c',
381 help="""Crypto file to read (and modify if requested). Default: {}."""
382 .format(CryptoConfigFile.default_path))
383
384 self.add_adapter(
385 'baremetal', baremetal_adapter,
386 """Like full, but exclude features that require platform features
387 such as file input-output.
388 """)
389 self.add_adapter(
390 'baremetal_size', baremetal_size_adapter,
391 """Like baremetal, but exclude debugging features. Useful for code size measurements.
392 """)
393 self.add_adapter(
394 'full', full_adapter,
395 """Uncomment most features.
396 Exclude alternative implementations and platform support options, as well as
397 some options that are awkward to test.
398 """)
399 self.add_adapter(
400 'full_no_deprecated', no_deprecated_adapter(full_adapter),
401 """Uncomment most non-deprecated features.
402 Like "full", but without deprecated features.
403 """)
404 self.add_adapter(
405 'full_no_platform', no_platform_adapter(full_adapter),
406 """Uncomment most non-platform features. Like "full", but without platform features.
407 """)
408 self.add_adapter(
409 'realfull', realfull_adapter,
410 """Uncomment all boolean #defines.
411 Suitable for generating documentation, but not for building.
412 """)
413 self.add_adapter(
414 'crypto', crypto_adapter(None),
415 """Only include crypto features. Exclude X.509 and TLS.""")
416 self.add_adapter(
417 'crypto_baremetal', crypto_adapter(baremetal_adapter),
418 """Like baremetal, but with only crypto features, excluding X.509 and TLS.""")
419 self.add_adapter(
420 'crypto_full', crypto_adapter(full_adapter),
421 """Like full, but with only crypto features, excluding X.509 and TLS.""")
422
Gilles Peskineb4063892019-07-27 21:36:44 +0200423
424if __name__ == '__main__':
Gabor Mezei634103c2024-09-11 13:08:21 +0200425 sys.exit(MbedTLSConfigTool().main())