blob: 6b30c54c702fa67967b12797127b9827f8425db9 [file] [log] [blame]
Gilles Peskineb4063892019-07-27 21:36:44 +02001#!/usr/bin/env python3
2
Gabor Mezei9f2b8172024-08-06 12:02:18 +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 Mezei9f2b8172024-08-06 12:02:18 +02006 config = CombinedConfigFile()
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
Gilles Peskineb4063892019-07-27 21:36:44 +020015import re
Gabor Mezei24d7cc72024-08-06 15:11:24 +020016import sys
Gilles Peskineb4063892019-07-27 21:36:44 +020017
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +020018import framework_scripts_path # pylint: disable=unused-import
19from mbedtls_framework import config_common
Gabor Mezeid53080d2024-08-27 14:06:54 +020020
Gabor Mezeia12ed6b2024-09-09 17:20:49 +020021
Gilles Peskinee4c69552024-09-19 19:57:58 +020022def is_boolean_setting(name, value):
23 """Is this a boolean setting?
Gabor Mezeide6e1922024-06-28 17:10:50 +020024
Gilles Peskinee4c69552024-09-19 19:57:58 +020025 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 Mezeide6e1922024-06-28 17:10:50 +020034 """
Gilles Peskinee4c69552024-09-19 19:57:58 +020035 if name.startswith('PSA_WANT_'):
36 return True
37 if not value:
38 return True
39 return False
Gilles Peskine53d41ae2019-07-27 23:31:53 +020040
Gilles Peskine00b91442024-09-19 20:13:49 +020041def realfull_adapter(_name, _value, _active):
Gilles Peskine36571d62024-09-19 19:58:56 +020042 """Activate all symbols.
Gilles Peskineba4162a2022-04-11 17:04:38 +020043
44 This is intended for building the documentation, including the
45 documentation of settings that are activated by defining an optional
Gilles Peskine36571d62024-09-19 19:58:56 +020046 preprocessor macro. There is no expectation that the resulting
47 configuration can be built.
Gilles Peskineba4162a2022-04-11 17:04:38 +020048 """
Gilles Peskineb4063892019-07-27 21:36:44 +020049 return True
50
Gabor Mezei542fd382024-06-10 14:07:42 +020051PSA_UNSUPPORTED_FEATURE = frozenset([
Gabor Mezei3678dee2024-06-04 19:58:43 +020052 '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 Mezei542fd382024-06-10 14:07:42 +020058PSA_DEPRECATED_FEATURE = frozenset([
Gabor Mezei3678dee2024-06-04 19:58:43 +020059 'PSA_WANT_KEY_TYPE_ECC_KEY_PAIR',
60 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR'
61])
62
Gabor Mezei9b0f9e72024-06-26 18:08:17 +020063EXCLUDE_FROM_CRYPTO = PSA_UNSUPPORTED_FEATURE | \
Gilles Peskinebc7c5232025-01-02 11:30:55 +010064 PSA_DEPRECATED_FEATURE
Gabor Mezei542fd382024-06-10 14:07:42 +020065
Gilles Peskinecfffc282020-04-12 13:55:45 +020066# 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 Peskinec9d04332020-04-16 20:50:17 +020070# * 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 Peskinecfffc282020-04-12 13:55:45 +020073# * Options that remove features.
Gilles Peskinebbaa2b72020-04-12 13:33:57 +020074EXCLUDE_FROM_FULL = frozenset([
Gilles Peskinecfffc282020-04-12 13:55:45 +020075 #pylint: disable=line-too-long
Yanray Wanga8704672023-04-20 17:16:48 +080076 'MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH', # interacts with CTR_DRBG_128_BIT_KEY
Gilles Peskinea8861e02023-09-05 20:20:51 +020077 'MBEDTLS_AES_USE_HARDWARE_ONLY', # hardware dependency
Yanray Wang42be1ba2023-11-23 14:28:47 +080078 'MBEDTLS_BLOCK_CIPHER_NO_DECRYPT', # incompatible with ECB in PSA, CBC/XTS/NIST_KW/DES
Gilles Peskinec9d04332020-04-16 20:50:17 +020079 'MBEDTLS_CTR_DRBG_USE_128_BIT_KEY', # interacts with ENTROPY_FORCE_SHA256
Gilles Peskinecfffc282020-04-12 13:55:45 +020080 'MBEDTLS_DEPRECATED_REMOVED', # conflicts with deprecated options
Gilles Peskine90581ee2020-04-12 14:02:47 +020081 'MBEDTLS_DEPRECATED_WARNING', # conflicts with deprecated options
Gilles Peskinec9d04332020-04-16 20:50:17 +020082 'MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED', # influences the use of ECDH in TLS
Janos Follath5b7c38f2023-08-01 08:51:12 +010083 'MBEDTLS_ECP_WITH_MPI_UINT', # disables the default ECP and is experimental
Gilles Peskinec9d04332020-04-16 20:50:17 +020084 'MBEDTLS_ENTROPY_FORCE_SHA256', # interacts with CTR_DRBG_128_BIT_KEY
Gilles Peskinecfffc282020-04-12 13:55:45 +020085 '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 Peskinec9d04332020-04-16 20:50:17 +020089 'MBEDTLS_NO_64BIT_MULTIPLICATION', # influences anything that uses bignum
Gilles Peskinecfffc282020-04-12 13:55:45 +020090 'MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES', # removes a feature
91 'MBEDTLS_NO_PLATFORM_ENTROPY', # removes a feature
Gilles Peskinec9d04332020-04-16 20:50:17 +020092 'MBEDTLS_NO_UDBL_DIVISION', # influences anything that uses bignum
Gilles Peskineefaee9a2023-09-20 20:49:47 +020093 'MBEDTLS_PSA_P256M_DRIVER_ENABLED', # influences SECP256R1 KeyGen/ECDH/ECDSA
Gilles Peskinecfffc282020-04-12 13:55:45 +020094 'MBEDTLS_PLATFORM_NO_STD_FUNCTIONS', # removes a feature
David Horstmann6f8c95b2024-03-14 14:52:45 +000095 'MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS', # removes a feature
Gilles Peskinef08b3f82020-11-13 17:36:48 +010096 'MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG', # behavior change + build dependency
Gilles Peskinea22b95a2024-09-19 13:43:57 +020097 'MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER', # interface and behavior change
Gilles Peskinecfffc282020-04-12 13:55:45 +020098 'MBEDTLS_PSA_CRYPTO_SPM', # platform dependency (PSA SPM)
Gilles Peskinec9d04332020-04-16 20:50:17 +020099 'MBEDTLS_RSA_NO_CRT', # influences the use of RSA in X.509 and TLS
Tom Cosgrove87fbfb52022-03-15 10:51:52 +0000100 'MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT
Dave Rodgman9be3cf02023-10-11 14:47:55 +0100101 'MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY', # interacts with *_USE_ARMV8_A_CRYPTO_IF_PRESENT
Tom Cosgrove87fbfb52022-03-15 10:51:52 +0000102 'MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT
Dave Rodgman7cb635a2023-10-12 16:14:51 +0100103 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', # setting *_USE_ARMV8_A_CRYPTO is sufficient
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +0200104 'MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN', # build dependency (clang+memsan)
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +0200105 'MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND', # build dependency (valgrind headers)
Hanno Beckere1113562019-06-12 13:59:14 +0100106 'MBEDTLS_X509_REMOVE_INFO', # removes a feature
Valerio Setti678e0fb2024-06-14 07:49:05 +0200107 'MBEDTLS_PSA_STATIC_KEY_SLOTS', # only relevant for embedded devices
108 'MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE', # only relevant for embedded devices
Gabor Mezei542fd382024-06-10 14:07:42 +0200109 *PSA_UNSUPPORTED_FEATURE,
110 *PSA_DEPRECATED_FEATURE,
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200111])
112
Gilles Peskine32e889d2020-04-12 23:43:28 +0200113def is_seamless_alt(name):
Gilles Peskinec34faba2020-04-20 15:44:14 +0200114 """Whether the xxx_ALT symbol should be included in the full configuration.
Gilles Peskine32e889d2020-04-12 23:43:28 +0200115
Gilles Peskinec34faba2020-04-20 15:44:14 +0200116 Include alternative implementations of platform functions, which are
Gilles Peskine32e889d2020-04-12 23:43:28 +0200117 configurable function pointers that default to the built-in function.
118 This way we test that the function pointers exist and build correctly
119 without changing the behavior, and tests can verify that the function
120 pointers are used by modifying those pointers.
121
122 Exclude alternative implementations of library functions since they require
123 an implementation of the relevant functions and an xxx_alt.h header.
124 """
Gilles Peskinea8861e02023-09-05 20:20:51 +0200125 if name in (
Valerio Settic5686882025-03-24 15:55:27 +0100126 'MBEDTLS_PLATFORM_GET_ENTROPY_ALT',
Gilles Peskinea8861e02023-09-05 20:20:51 +0200127 'MBEDTLS_PLATFORM_GMTIME_R_ALT',
128 'MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT',
129 'MBEDTLS_PLATFORM_MS_TIME_ALT',
130 'MBEDTLS_PLATFORM_ZEROIZE_ALT',
131 ):
Gilles Peskinec34faba2020-04-20 15:44:14 +0200132 # Similar to non-platform xxx_ALT, requires platform_alt.h
133 return False
Gilles Peskine32e889d2020-04-12 23:43:28 +0200134 return name.startswith('MBEDTLS_PLATFORM_')
135
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200136def include_in_full(name):
137 """Rules for symbols in the "full" configuration."""
Gabor Mezei542fd382024-06-10 14:07:42 +0200138 if name in EXCLUDE_FROM_FULL:
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200139 return False
140 if name.endswith('_ALT'):
Gilles Peskine32e889d2020-04-12 23:43:28 +0200141 return is_seamless_alt(name)
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200142 return True
143
Gilles Peskine00b91442024-09-19 20:13:49 +0200144def full_adapter(name, value, active):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200145 """Config adapter for "full"."""
Gilles Peskinee4c69552024-09-19 19:57:58 +0200146 if not is_boolean_setting(name, value):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200147 return active
148 return include_in_full(name)
149
Gilles Peskinecfffc282020-04-12 13:55:45 +0200150# 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 Peskinebbaa2b72020-04-12 13:33:57 +0200155EXCLUDE_FROM_BAREMETAL = frozenset([
Gilles Peskinecfffc282020-04-12 13:55:45 +0200156 #pylint: disable=line-too-long
Gilles Peskine98f8f952020-04-20 15:38:39 +0200157 'MBEDTLS_ENTROPY_NV_SEED', # requires a filesystem and FS_IO or alternate NV seed hooks
Gilles Peskinecfffc282020-04-12 13:55:45 +0200158 'MBEDTLS_FS_IO', # requires a filesystem
Gilles Peskinecfffc282020-04-12 13:55:45 +0200159 '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 Peskine98f8f952020-04-20 15:38:39 +0200163 'MBEDTLS_PLATFORM_NV_SEED_ALT', # requires a filesystem and ENTROPY_NV_SEED
164 'MBEDTLS_PLATFORM_TIME_ALT', # requires a clock and HAVE_TIME
Gilles Peskinecfffc282020-04-12 13:55:45 +0200165 'MBEDTLS_PSA_CRYPTO_STORAGE_C', # requires a filesystem
166 'MBEDTLS_PSA_ITS_FILE_C', # requires a filesystem
167 'MBEDTLS_THREADING_C', # requires a threading interface
168 'MBEDTLS_THREADING_PTHREAD', # requires pthread
169 'MBEDTLS_TIMING_C', # requires a clock
Dave Rodgman9be3cf02023-10-11 14:47:55 +0100170 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection
Dave Rodgman5b89c552023-10-10 14:59:02 +0100171 'MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection
Dave Rodgmanbe7915a2023-10-11 10:46:38 +0100172 'MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200173])
174
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200175def keep_in_baremetal(name):
176 """Rules for symbols in the "baremetal" configuration."""
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200177 if name in EXCLUDE_FROM_BAREMETAL:
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200178 return False
179 return True
180
Gilles Peskine00b91442024-09-19 20:13:49 +0200181def baremetal_adapter(name, value, active):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200182 """Config adapter for "baremetal"."""
Gilles Peskinee4c69552024-09-19 19:57:58 +0200183 if not is_boolean_setting(name, value):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200184 return active
185 if name == 'MBEDTLS_NO_PLATFORM_ENTROPY':
Gilles Peskinecfffc282020-04-12 13:55:45 +0200186 # No OS-provided entropy source
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200187 return True
188 return include_in_full(name) and keep_in_baremetal(name)
189
Gilles Peskine120f29d2021-09-01 19:51:19 +0200190# This set contains options that are mostly for debugging or test purposes,
191# and therefore should be excluded when doing code size measurements.
192# Options that are their own module (such as MBEDTLS_ERROR_C) are not listed
193# and therefore will be included when doing code size measurements.
194EXCLUDE_FOR_SIZE = frozenset([
195 'MBEDTLS_DEBUG_C', # large code size increase in TLS
196 'MBEDTLS_SELF_TEST', # increases the size of many modules
197 'MBEDTLS_TEST_HOOKS', # only useful with the hosted test framework, increases code size
198])
199
Gilles Peskine00b91442024-09-19 20:13:49 +0200200def baremetal_size_adapter(name, value, active):
Gilles Peskine120f29d2021-09-01 19:51:19 +0200201 if name in EXCLUDE_FOR_SIZE:
202 return False
Gilles Peskine00b91442024-09-19 20:13:49 +0200203 return baremetal_adapter(name, value, active)
Gilles Peskine120f29d2021-09-01 19:51:19 +0200204
Gilles Peskine31987c62020-01-31 14:23:30 +0100205def include_in_crypto(name):
206 """Rules for symbols in a crypto configuration."""
207 if name.startswith('MBEDTLS_X509_') or \
Harry Ramseyc19f8ae2024-10-03 11:18:32 +0100208 name.startswith('MBEDTLS_VERSION_') or \
Gilles Peskine31987c62020-01-31 14:23:30 +0100209 name.startswith('MBEDTLS_SSL_') or \
210 name.startswith('MBEDTLS_KEY_EXCHANGE_'):
211 return False
212 if name in [
Gilles Peskinecfffc282020-04-12 13:55:45 +0200213 'MBEDTLS_DEBUG_C', # part of libmbedtls
214 'MBEDTLS_NET_C', # part of libmbedtls
Nayna Jainc9deb182020-11-16 19:03:12 +0000215 'MBEDTLS_PKCS7_C', # part of libmbedx509
Ronald Cron69245642024-11-12 18:14:23 +0100216 'MBEDTLS_TIMING_C', # part of libmbedtls
Harry Ramsey08007ed2024-10-22 14:18:17 +0100217 'MBEDTLS_ERROR_C', # part of libmbedx509
218 'MBEDTLS_ERROR_STRERROR_DUMMY', # part of libmbedx509
Gilles Peskine31987c62020-01-31 14:23:30 +0100219 ]:
220 return False
Gabor Mezei542fd382024-06-10 14:07:42 +0200221 if name in EXCLUDE_FROM_CRYPTO:
222 return False
Gilles Peskine31987c62020-01-31 14:23:30 +0100223 return True
224
225def crypto_adapter(adapter):
226 """Modify an adapter to disable non-crypto symbols.
227
Gilles Peskine00b91442024-09-19 20:13:49 +0200228 ``crypto_adapter(adapter)(name, value, active)`` is like
229 ``adapter(name, value, active)``, but unsets all X.509 and TLS symbols.
Gilles Peskine31987c62020-01-31 14:23:30 +0100230 """
Gilles Peskine00b91442024-09-19 20:13:49 +0200231 def continuation(name, value, active):
Gilles Peskine31987c62020-01-31 14:23:30 +0100232 if not include_in_crypto(name):
233 return False
234 if adapter is None:
235 return active
Gilles Peskine00b91442024-09-19 20:13:49 +0200236 return adapter(name, value, active)
Gilles Peskine31987c62020-01-31 14:23:30 +0100237 return continuation
238
Gilles Peskineed5c21d2022-06-27 23:02:09 +0200239DEPRECATED = frozenset([
Gabor Mezei542fd382024-06-10 14:07:42 +0200240 *PSA_DEPRECATED_FEATURE
Gilles Peskineed5c21d2022-06-27 23:02:09 +0200241])
Gilles Peskine30de2e82020-04-20 21:39:22 +0200242def no_deprecated_adapter(adapter):
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200243 """Modify an adapter to disable deprecated symbols.
244
Gilles Peskine00b91442024-09-19 20:13:49 +0200245 ``no_deprecated_adapter(adapter)(name, value, active)`` is like
246 ``adapter(name, value, active)``, but unsets all deprecated symbols
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200247 and sets ``MBEDTLS_DEPRECATED_REMOVED``.
248 """
Gilles Peskine00b91442024-09-19 20:13:49 +0200249 def continuation(name, value, active):
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200250 if name == 'MBEDTLS_DEPRECATED_REMOVED':
251 return True
Gilles Peskineed5c21d2022-06-27 23:02:09 +0200252 if name in DEPRECATED:
253 return False
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200254 if adapter is None:
255 return active
Gilles Peskine00b91442024-09-19 20:13:49 +0200256 return adapter(name, value, active)
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200257 return continuation
258
Paul Elliottfb81f772023-10-18 17:44:59 +0100259def no_platform_adapter(adapter):
260 """Modify an adapter to disable platform symbols.
261
Gilles Peskine00b91442024-09-19 20:13:49 +0200262 ``no_platform_adapter(adapter)(name, value, active)`` is like
263 ``adapter(name, value, active)``, but unsets all platform symbols other
Paul Elliottfb81f772023-10-18 17:44:59 +0100264 ``than MBEDTLS_PLATFORM_C.
265 """
Gilles Peskine00b91442024-09-19 20:13:49 +0200266 def continuation(name, value, active):
Paul Elliottfb81f772023-10-18 17:44:59 +0100267 # 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 Peskine00b91442024-09-19 20:13:49 +0200272 return adapter(name, value, active)
Paul Elliottfb81f772023-10-18 17:44:59 +0100273 return continuation
274
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200275
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200276class MbedTLSConfigFile(config_common.ConfigFile):
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200277 """Representation of an MbedTLS configuration file."""
278
Gabor Mezei3678dee2024-06-04 19:58:43 +0200279 _path_in_tree = 'include/mbedtls/mbedtls_config.h'
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)]
286
287 def __init__(self, filename=None):
Gabor Mezei93a6d1f2024-06-26 18:01:09 +0200288 super().__init__(self.default_path, 'Mbed TLS', filename)
Gabor Mezei3678dee2024-06-04 19:58:43 +0200289 self.current_section = 'header'
290
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200291
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200292class CryptoConfigFile(config_common.ConfigFile):
Gabor Mezei4706fe72024-07-08 17:00:55 +0200293 """Representation of a Crypto configuration file."""
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200294
Gabor Mezei3de65862024-07-08 16:14:10 +0200295 # 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.
Gabor Mezei776ee902024-09-09 17:00:50 +0200298 _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')
Gabor Mezei3678dee2024-06-04 19:58:43 +0200303 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):
Gabor Mezei93a6d1f2024-06-26 18:01:09 +0200311 super().__init__(self.default_path, 'Crypto', filename)
Gabor Mezei3678dee2024-06-04 19:58:43 +0200312
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200313
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200314class MbedTLSConfig(config_common.Config):
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200315 """Representation of the Mbed TLS configuration.
Gabor Mezei3678dee2024-06-04 19:58:43 +0200316
317 See the documentation of the `Config` class for methods to query
318 and modify the configuration.
319 """
Gabor Mezei4706fe72024-07-08 17:00:55 +0200320
Gabor Mezeiee521b62024-06-07 13:50:41 +0200321 def __init__(self, filename=None):
Gabor Mezei3678dee2024-06-04 19:58:43 +0200322 """Read the Mbed TLS configuration file."""
Gabor Mezei4706fe72024-07-08 17:00:55 +0200323
Gabor Mezei3678dee2024-06-04 19:58:43 +0200324 super().__init__()
Gabor Mezeid53080d2024-08-27 14:06:54 +0200325 configfile = MbedTLSConfigFile(filename)
326 self.configfiles.append(configfile)
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200327 self.settings.update({name: config_common.Setting(configfile, active, name, value, section)
Gabor Mezei92065ed2024-06-07 13:47:59 +0200328 for (active, name, value, section)
Gabor Mezeid53080d2024-08-27 14:06:54 +0200329 in configfile.parse_file()})
Gabor Mezei3678dee2024-06-04 19:58:43 +0200330
331 def set(self, name, value=None):
Gabor Mezei4706fe72024-07-08 17:00:55 +0200332 """Set name to the given value and make it active."""
333
Gabor Mezei3678dee2024-06-04 19:58:43 +0200334 if name not in self.settings:
Gabor Mezeid53080d2024-08-27 14:06:54 +0200335 self._get_configfile().templates.append((name, '', '#define ' + name + ' '))
Gabor Mezeiee521b62024-06-07 13:50:41 +0200336
Gabor Mezei3678dee2024-06-04 19:58:43 +0200337 super().set(name, value)
Gilles Peskineb4063892019-07-27 21:36:44 +0200338
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200339
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200340class CryptoConfig(config_common.Config):
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200341 """Representation of the PSA crypto configuration.
Gabor Mezei3678dee2024-06-04 19:58:43 +0200342
343 See the documentation of the `Config` class for methods to query
344 and modify the configuration.
345 """
Gabor Mezei4706fe72024-07-08 17:00:55 +0200346
Gabor Mezeiee521b62024-06-07 13:50:41 +0200347 def __init__(self, filename=None):
Gabor Mezei3678dee2024-06-04 19:58:43 +0200348 """Read the PSA crypto configuration file."""
Gabor Mezei4706fe72024-07-08 17:00:55 +0200349
Gabor Mezei3678dee2024-06-04 19:58:43 +0200350 super().__init__()
Gabor Mezeid53080d2024-08-27 14:06:54 +0200351 configfile = CryptoConfigFile(filename)
352 self.configfiles.append(configfile)
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200353 self.settings.update({name: config_common.Setting(configfile, active, name, value, section)
Gabor Mezei92065ed2024-06-07 13:47:59 +0200354 for (active, name, value, section)
Gabor Mezeid53080d2024-08-27 14:06:54 +0200355 in configfile.parse_file()})
Gabor Mezei3678dee2024-06-04 19:58:43 +0200356
Gabor Mezeid723b512024-06-07 15:31:52 +0200357 def set(self, name, value='1'):
Gabor Mezei4706fe72024-07-08 17:00:55 +0200358 """Set name to the given value and make it active."""
359
Gabor Mezei542fd382024-06-10 14:07:42 +0200360 if name in PSA_UNSUPPORTED_FEATURE:
Gabor Mezei92065ed2024-06-07 13:47:59 +0200361 raise ValueError(f'Feature is unsupported: \'{name}\'')
Gabor Mezei3678dee2024-06-04 19:58:43 +0200362
363 if name not in self.settings:
Gabor Mezeid53080d2024-08-27 14:06:54 +0200364 self._get_configfile().templates.append((name, '', '#define ' + name + ' '))
Gabor Mezeiee521b62024-06-07 13:50:41 +0200365
Gabor Mezei3678dee2024-06-04 19:58:43 +0200366 super().set(name, value)
367
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200368
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200369class CombinedConfig(config_common.Config):
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200370 """Representation of MbedTLS and PSA crypto configuration
371
372 See the documentation of the `Config` class for methods to query
373 and modify the configuration.
374 """
Gabor Mezei3678dee2024-06-04 19:58:43 +0200375
Gabor Mezei3e2a5502024-06-28 17:27:19 +0200376 def __init__(self, *configs):
Gabor Mezeiee521b62024-06-07 13:50:41 +0200377 super().__init__()
Gabor Mezei3e2a5502024-06-28 17:27:19 +0200378 for config in configs:
379 if isinstance(config, MbedTLSConfigFile):
380 self.mbedtls_configfile = config
381 elif isinstance(config, CryptoConfigFile):
382 self.crypto_configfile = config
383 else:
384 raise ValueError(f'Invalid configfile: {config}')
Gabor Mezeid53080d2024-08-27 14:06:54 +0200385 self.configfiles.append(config)
Gabor Mezei3e2a5502024-06-28 17:27:19 +0200386
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200387 self.settings.update({name: config_common.Setting(configfile, active, name, value, section)
Gabor Mezeiee521b62024-06-07 13:50:41 +0200388 for configfile in [self.mbedtls_configfile, self.crypto_configfile]
389 for (active, name, value, section) in configfile.parse_file()})
Gabor Mezei3678dee2024-06-04 19:58:43 +0200390
Gabor Mezei5034a1f2024-12-05 19:06:19 +0100391 _crypto_regexp = re.compile(r'^PSA_.*')
Gabor Mezeid53080d2024-08-27 14:06:54 +0200392 def _get_configfile(self, name=None):
Gabor Mezei4706fe72024-07-08 17:00:55 +0200393 """Find a config type for a setting name"""
394
Gabor Mezeiee521b62024-06-07 13:50:41 +0200395 if name in self.settings:
396 return self.settings[name].configfile
397 elif re.match(self._crypto_regexp, name):
398 return self.crypto_configfile
Gabor Mezei3678dee2024-06-04 19:58:43 +0200399 else:
Gabor Mezeiee521b62024-06-07 13:50:41 +0200400 return self.mbedtls_configfile
Gabor Mezei3678dee2024-06-04 19:58:43 +0200401
402 def set(self, name, value=None):
Gabor Mezei4706fe72024-07-08 17:00:55 +0200403 """Set name to the given value and make it active."""
404
Gabor Mezeiee521b62024-06-07 13:50:41 +0200405 configfile = self._get_configfile(name)
Gabor Mezei3678dee2024-06-04 19:58:43 +0200406
Gabor Mezeiee521b62024-06-07 13:50:41 +0200407 if configfile == self.crypto_configfile:
Gabor Mezei542fd382024-06-10 14:07:42 +0200408 if name in PSA_UNSUPPORTED_FEATURE:
Gabor Mezeiee521b62024-06-07 13:50:41 +0200409 raise ValueError(f'Feature is unsupported: \'{name}\'')
Gabor Mezeiee521b62024-06-07 13:50:41 +0200410
Gabor Mezeid723b512024-06-07 15:31:52 +0200411 # The default value in the crypto config is '1'
Minos Galanakisa4a37372024-09-26 14:41:41 +0100412 if not value and re.match(self._crypto_regexp, name):
Gabor Mezeid723b512024-06-07 15:31:52 +0200413 value = '1'
414
Gabor Mezeic659c1b2024-08-06 17:37:55 +0200415 if name not in self.settings:
Gabor Mezeiee521b62024-06-07 13:50:41 +0200416 configfile.templates.append((name, '', '#define ' + name + ' '))
417
Gabor Mezeid53080d2024-08-27 14:06:54 +0200418 super().set(name, value)
Gabor Mezeiee521b62024-06-07 13:50:41 +0200419
Gabor Mezeidaf807f2024-08-14 11:33:46 +0200420 #pylint: disable=arguments-differ
Gabor Mezei3678dee2024-06-04 19:58:43 +0200421 def write(self, mbedtls_file=None, crypto_file=None):
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200422 """Write the whole configuration to the file it was read from.
423
424 If mbedtls_file or crypto_file is specified, write the specific configuration
425 to the corresponding file instead.
Gabor Mezeif5f13082024-09-18 13:02:16 +0200426
Gabor Mezei317a2a32024-09-18 16:51:27 +0200427 Two file name parameters and not only one as in the super class as we handle
428 two configuration files in this class.
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200429 """
Gabor Mezei4706fe72024-07-08 17:00:55 +0200430
Gabor Mezeiee521b62024-06-07 13:50:41 +0200431 self.mbedtls_configfile.write(self.settings, mbedtls_file)
432 self.crypto_configfile.write(self.settings, crypto_file)
Gabor Mezei3678dee2024-06-04 19:58:43 +0200433
Gabor Mezeiee521b62024-06-07 13:50:41 +0200434 def filename(self, name=None):
Gabor Mezeif5f13082024-09-18 13:02:16 +0200435 """Get the name of the config files.
Gabor Mezei4706fe72024-07-08 17:00:55 +0200436
437 If 'name' is specified return the name of the config file where it is defined.
438 """
439
Gabor Mezeiee521b62024-06-07 13:50:41 +0200440 if not name:
441 return [config.filename for config in [self.mbedtls_configfile, self.crypto_configfile]]
442
443 return self._get_configfile(name).filename
Gilles Peskineb4063892019-07-27 21:36:44 +0200444
Gabor Mezei24d7cc72024-08-06 15:11:24 +0200445
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200446class MbedTLSConfigTool(config_common.ConfigTool):
Gabor Mezei24d7cc72024-08-06 15:11:24 +0200447 """Command line mbedtls_config.h and crypto_config.h manipulation tool."""
448
449 def __init__(self):
Gabor Mezeicd326bf2024-09-18 16:53:03 +0200450 super().__init__(MbedTLSConfigFile.default_path)
Gabor Mezei568808a2024-09-18 13:02:42 +0200451 self.config = CombinedConfig(MbedTLSConfigFile(self.args.file),
452 CryptoConfigFile(self.args.cryptofile))
Gabor Mezei24d7cc72024-08-06 15:11:24 +0200453
454 def custom_parser_options(self):
455 """Adds MbedTLS specific options for the parser."""
456
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200457 self.parser.add_argument(
458 '--cryptofile', '-c',
459 help="""Crypto file to read (and modify if requested). Default: {}."""
460 .format(CryptoConfigFile.default_path))
Gabor Mezei24d7cc72024-08-06 15:11:24 +0200461
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200462 self.add_adapter(
463 'baremetal', baremetal_adapter,
464 """Like full, but exclude features that require platform features
465 such as file input-output.
466 """)
467 self.add_adapter(
468 'baremetal_size', baremetal_size_adapter,
469 """Like baremetal, but exclude debugging features. Useful for code size measurements.
470 """)
471 self.add_adapter(
472 'full', full_adapter,
473 """Uncomment most features.
474 Exclude alternative implementations and platform support options, as well as
475 some options that are awkward to test.
476 """)
477 self.add_adapter(
478 'full_no_deprecated', no_deprecated_adapter(full_adapter),
479 """Uncomment most non-deprecated features.
480 Like "full", but without deprecated features.
481 """)
482 self.add_adapter(
483 'full_no_platform', no_platform_adapter(full_adapter),
484 """Uncomment most non-platform features. Like "full", but without platform features.
485 """)
486 self.add_adapter(
487 'realfull', realfull_adapter,
488 """Uncomment all boolean #defines.
489 Suitable for generating documentation, but not for building.
490 """)
491 self.add_adapter(
492 'crypto', crypto_adapter(None),
493 """Only include crypto features. Exclude X.509 and TLS.""")
494 self.add_adapter(
495 'crypto_baremetal', crypto_adapter(baremetal_adapter),
496 """Like baremetal, but with only crypto features, excluding X.509 and TLS.""")
497 self.add_adapter(
498 'crypto_full', crypto_adapter(full_adapter),
499 """Like full, but with only crypto features, excluding X.509 and TLS.""")
Gilles Peskineb4063892019-07-27 21:36:44 +0200500
Gilles Peskineb4063892019-07-27 21:36:44 +0200501
Gabor Mezei24d7cc72024-08-06 15:11:24 +0200502if __name__ == '__main__':
503 sys.exit(MbedTLSConfigTool().main())