blob: 8b6aa9a24fc7dbdb02e1b63ed2f12d14a5f9c9e9 [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 Peskine702d75a2024-09-19 19:49:20 +020041def realfull_adapter(_name, _value, active, section):
Gilles Peskineba4162a2022-04-11 17:04:38 +020042 """Activate all symbols found in the global and boolean feature sections.
43
44 This is intended for building the documentation, including the
45 documentation of settings that are activated by defining an optional
46 preprocessor macro.
47
48 Do not activate definitions in the section containing symbols that are
49 supposed to be defined and documented in their own module.
50 """
51 if section == 'Module configuration options':
Gilles Peskine53d41ae2019-07-27 23:31:53 +020052 return active
Gilles Peskineb4063892019-07-27 21:36:44 +020053 return True
54
Gabor Mezei542fd382024-06-10 14:07:42 +020055PSA_UNSUPPORTED_FEATURE = frozenset([
Gabor Mezei3678dee2024-06-04 19:58:43 +020056 'PSA_WANT_ALG_CBC_MAC',
57 'PSA_WANT_ALG_XTS',
58 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_DERIVE',
59 'PSA_WANT_KEY_TYPE_DH_KEY_PAIR_DERIVE'
60])
61
Gabor Mezei542fd382024-06-10 14:07:42 +020062PSA_DEPRECATED_FEATURE = frozenset([
Gabor Mezei3678dee2024-06-04 19:58:43 +020063 'PSA_WANT_KEY_TYPE_ECC_KEY_PAIR',
64 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR'
65])
66
Gabor Mezei542fd382024-06-10 14:07:42 +020067PSA_UNSTABLE_FEATURE = frozenset([
Gabor Mezei3678dee2024-06-04 19:58:43 +020068 'PSA_WANT_ECC_SECP_K1_224'
69])
70
Gabor Mezei9b0f9e72024-06-26 18:08:17 +020071EXCLUDE_FROM_CRYPTO = PSA_UNSUPPORTED_FEATURE | \
72 PSA_DEPRECATED_FEATURE | \
73 PSA_UNSTABLE_FEATURE
Gabor Mezei542fd382024-06-10 14:07:42 +020074
Gilles Peskinecfffc282020-04-12 13:55:45 +020075# The goal of the full configuration is to have everything that can be tested
76# together. This includes deprecated or insecure options. It excludes:
77# * Options that require additional build dependencies or unusual hardware.
78# * Options that make testing less effective.
Gilles Peskinec9d04332020-04-16 20:50:17 +020079# * Options that are incompatible with other options, or more generally that
80# interact with other parts of the code in such a way that a bulk enabling
81# is not a good way to test them.
Gilles Peskinecfffc282020-04-12 13:55:45 +020082# * Options that remove features.
Gilles Peskinebbaa2b72020-04-12 13:33:57 +020083EXCLUDE_FROM_FULL = frozenset([
Gilles Peskinecfffc282020-04-12 13:55:45 +020084 #pylint: disable=line-too-long
Yanray Wanga8704672023-04-20 17:16:48 +080085 'MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH', # interacts with CTR_DRBG_128_BIT_KEY
Gilles Peskinea8861e02023-09-05 20:20:51 +020086 'MBEDTLS_AES_USE_HARDWARE_ONLY', # hardware dependency
Yanray Wang42be1ba2023-11-23 14:28:47 +080087 'MBEDTLS_BLOCK_CIPHER_NO_DECRYPT', # incompatible with ECB in PSA, CBC/XTS/NIST_KW/DES
Gilles Peskinec9d04332020-04-16 20:50:17 +020088 'MBEDTLS_CTR_DRBG_USE_128_BIT_KEY', # interacts with ENTROPY_FORCE_SHA256
Gilles Peskinecfffc282020-04-12 13:55:45 +020089 'MBEDTLS_DEPRECATED_REMOVED', # conflicts with deprecated options
Gilles Peskine90581ee2020-04-12 14:02:47 +020090 'MBEDTLS_DEPRECATED_WARNING', # conflicts with deprecated options
Gilles Peskinec9d04332020-04-16 20:50:17 +020091 'MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED', # influences the use of ECDH in TLS
Janos Follath5b7c38f2023-08-01 08:51:12 +010092 'MBEDTLS_ECP_WITH_MPI_UINT', # disables the default ECP and is experimental
Gilles Peskinec9d04332020-04-16 20:50:17 +020093 'MBEDTLS_ENTROPY_FORCE_SHA256', # interacts with CTR_DRBG_128_BIT_KEY
Gilles Peskinecfffc282020-04-12 13:55:45 +020094 '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 Peskinec9d04332020-04-16 20:50:17 +020098 'MBEDTLS_NO_64BIT_MULTIPLICATION', # influences anything that uses bignum
Gilles Peskinecfffc282020-04-12 13:55:45 +020099 'MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES', # removes a feature
100 'MBEDTLS_NO_PLATFORM_ENTROPY', # removes a feature
Gilles Peskinec9d04332020-04-16 20:50:17 +0200101 'MBEDTLS_NO_UDBL_DIVISION', # influences anything that uses bignum
Gilles Peskineefaee9a2023-09-20 20:49:47 +0200102 'MBEDTLS_PSA_P256M_DRIVER_ENABLED', # influences SECP256R1 KeyGen/ECDH/ECDSA
Gilles Peskinecfffc282020-04-12 13:55:45 +0200103 'MBEDTLS_PLATFORM_NO_STD_FUNCTIONS', # removes a feature
David Horstmann6f8c95b2024-03-14 14:52:45 +0000104 'MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS', # removes a feature
Gilles Peskinef08b3f82020-11-13 17:36:48 +0100105 'MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG', # behavior change + build dependency
Ronald Cronc3623db2020-10-29 10:51:32 +0100106 'MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER', # incompatible with USE_PSA_CRYPTO
Gilles Peskinecfffc282020-04-12 13:55:45 +0200107 'MBEDTLS_PSA_CRYPTO_SPM', # platform dependency (PSA SPM)
Gilles Peskinea08def92023-04-28 21:01:49 +0200108 'MBEDTLS_PSA_INJECT_ENTROPY', # conflicts with platform entropy sources
Gilles Peskinec9d04332020-04-16 20:50:17 +0200109 'MBEDTLS_RSA_NO_CRT', # influences the use of RSA in X.509 and TLS
Tom Cosgrove87fbfb52022-03-15 10:51:52 +0000110 'MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT
Dave Rodgman9be3cf02023-10-11 14:47:55 +0100111 'MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY', # interacts with *_USE_ARMV8_A_CRYPTO_IF_PRESENT
Tom Cosgrove87fbfb52022-03-15 10:51:52 +0000112 'MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT
Dave Rodgman7cb635a2023-10-12 16:14:51 +0100113 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', # setting *_USE_ARMV8_A_CRYPTO is sufficient
Manuel Pégourié-Gonnard6240def2020-07-10 09:35:54 +0200114 'MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN', # build dependency (clang+memsan)
Manuel Pégourié-Gonnard73afa372020-08-19 10:27:38 +0200115 'MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND', # build dependency (valgrind headers)
Hanno Beckere1113562019-06-12 13:59:14 +0100116 'MBEDTLS_X509_REMOVE_INFO', # removes a feature
Gabor Mezei542fd382024-06-10 14:07:42 +0200117 *PSA_UNSUPPORTED_FEATURE,
118 *PSA_DEPRECATED_FEATURE,
119 *PSA_UNSTABLE_FEATURE
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200120])
121
Gilles Peskine32e889d2020-04-12 23:43:28 +0200122def is_seamless_alt(name):
Gilles Peskinec34faba2020-04-20 15:44:14 +0200123 """Whether the xxx_ALT symbol should be included in the full configuration.
Gilles Peskine32e889d2020-04-12 23:43:28 +0200124
Gilles Peskinec34faba2020-04-20 15:44:14 +0200125 Include alternative implementations of platform functions, which are
Gilles Peskine32e889d2020-04-12 23:43:28 +0200126 configurable function pointers that default to the built-in function.
127 This way we test that the function pointers exist and build correctly
128 without changing the behavior, and tests can verify that the function
129 pointers are used by modifying those pointers.
130
131 Exclude alternative implementations of library functions since they require
132 an implementation of the relevant functions and an xxx_alt.h header.
133 """
Gilles Peskinea8861e02023-09-05 20:20:51 +0200134 if name in (
135 'MBEDTLS_PLATFORM_GMTIME_R_ALT',
136 'MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT',
137 'MBEDTLS_PLATFORM_MS_TIME_ALT',
138 'MBEDTLS_PLATFORM_ZEROIZE_ALT',
139 ):
Gilles Peskinec34faba2020-04-20 15:44:14 +0200140 # Similar to non-platform xxx_ALT, requires platform_alt.h
141 return False
Gilles Peskine32e889d2020-04-12 23:43:28 +0200142 return name.startswith('MBEDTLS_PLATFORM_')
143
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200144def include_in_full(name):
145 """Rules for symbols in the "full" configuration."""
Gabor Mezei542fd382024-06-10 14:07:42 +0200146 if name in EXCLUDE_FROM_FULL:
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200147 return False
148 if name.endswith('_ALT'):
Gilles Peskine32e889d2020-04-12 23:43:28 +0200149 return is_seamless_alt(name)
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200150 return True
151
Gilles Peskinee4c69552024-09-19 19:57:58 +0200152def full_adapter(name, value, active, _section):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200153 """Config adapter for "full"."""
Gilles Peskinee4c69552024-09-19 19:57:58 +0200154 if not is_boolean_setting(name, value):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200155 return active
156 return include_in_full(name)
157
Gilles Peskinecfffc282020-04-12 13:55:45 +0200158# The baremetal configuration excludes options that require a library or
159# operating system feature that is typically not present on bare metal
160# systems. Features that are excluded from "full" won't be in "baremetal"
161# either (unless explicitly turned on in baremetal_adapter) so they don't
162# need to be repeated here.
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200163EXCLUDE_FROM_BAREMETAL = frozenset([
Gilles Peskinecfffc282020-04-12 13:55:45 +0200164 #pylint: disable=line-too-long
Gilles Peskine98f8f952020-04-20 15:38:39 +0200165 'MBEDTLS_ENTROPY_NV_SEED', # requires a filesystem and FS_IO or alternate NV seed hooks
Gilles Peskinecfffc282020-04-12 13:55:45 +0200166 'MBEDTLS_FS_IO', # requires a filesystem
Gilles Peskinecfffc282020-04-12 13:55:45 +0200167 'MBEDTLS_HAVE_TIME', # requires a clock
168 'MBEDTLS_HAVE_TIME_DATE', # requires a clock
169 'MBEDTLS_NET_C', # requires POSIX-like networking
170 'MBEDTLS_PLATFORM_FPRINTF_ALT', # requires FILE* from stdio.h
Gilles Peskine98f8f952020-04-20 15:38:39 +0200171 'MBEDTLS_PLATFORM_NV_SEED_ALT', # requires a filesystem and ENTROPY_NV_SEED
172 'MBEDTLS_PLATFORM_TIME_ALT', # requires a clock and HAVE_TIME
173 'MBEDTLS_PSA_CRYPTO_SE_C', # requires a filesystem and PSA_CRYPTO_STORAGE_C
Gilles Peskinecfffc282020-04-12 13:55:45 +0200174 'MBEDTLS_PSA_CRYPTO_STORAGE_C', # requires a filesystem
175 'MBEDTLS_PSA_ITS_FILE_C', # requires a filesystem
176 'MBEDTLS_THREADING_C', # requires a threading interface
177 'MBEDTLS_THREADING_PTHREAD', # requires pthread
178 'MBEDTLS_TIMING_C', # requires a clock
Dave Rodgman9be3cf02023-10-11 14:47:55 +0100179 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection
Dave Rodgman5b89c552023-10-10 14:59:02 +0100180 'MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection
Dave Rodgmanbe7915a2023-10-11 10:46:38 +0100181 'MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200182])
183
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200184def keep_in_baremetal(name):
185 """Rules for symbols in the "baremetal" configuration."""
Gilles Peskinebbaa2b72020-04-12 13:33:57 +0200186 if name in EXCLUDE_FROM_BAREMETAL:
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200187 return False
188 return True
189
Gilles Peskinee4c69552024-09-19 19:57:58 +0200190def baremetal_adapter(name, value, active, _section):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200191 """Config adapter for "baremetal"."""
Gilles Peskinee4c69552024-09-19 19:57:58 +0200192 if not is_boolean_setting(name, value):
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200193 return active
194 if name == 'MBEDTLS_NO_PLATFORM_ENTROPY':
Gilles Peskinecfffc282020-04-12 13:55:45 +0200195 # No OS-provided entropy source
Gilles Peskine53d41ae2019-07-27 23:31:53 +0200196 return True
197 return include_in_full(name) and keep_in_baremetal(name)
198
Gilles Peskine120f29d2021-09-01 19:51:19 +0200199# This set contains options that are mostly for debugging or test purposes,
200# and therefore should be excluded when doing code size measurements.
201# Options that are their own module (such as MBEDTLS_ERROR_C) are not listed
202# and therefore will be included when doing code size measurements.
203EXCLUDE_FOR_SIZE = frozenset([
204 'MBEDTLS_DEBUG_C', # large code size increase in TLS
205 'MBEDTLS_SELF_TEST', # increases the size of many modules
206 'MBEDTLS_TEST_HOOKS', # only useful with the hosted test framework, increases code size
207])
208
Gilles Peskine702d75a2024-09-19 19:49:20 +0200209def baremetal_size_adapter(name, value, active, section):
Gilles Peskine120f29d2021-09-01 19:51:19 +0200210 if name in EXCLUDE_FOR_SIZE:
211 return False
Gilles Peskine702d75a2024-09-19 19:49:20 +0200212 return baremetal_adapter(name, value, active, section)
Gilles Peskine120f29d2021-09-01 19:51:19 +0200213
Gilles Peskine31987c62020-01-31 14:23:30 +0100214def include_in_crypto(name):
215 """Rules for symbols in a crypto configuration."""
216 if name.startswith('MBEDTLS_X509_') or \
217 name.startswith('MBEDTLS_SSL_') or \
218 name.startswith('MBEDTLS_KEY_EXCHANGE_'):
219 return False
220 if name in [
Gilles Peskinecfffc282020-04-12 13:55:45 +0200221 'MBEDTLS_DEBUG_C', # part of libmbedtls
222 'MBEDTLS_NET_C', # part of libmbedtls
Nayna Jainc9deb182020-11-16 19:03:12 +0000223 'MBEDTLS_PKCS7_C', # part of libmbedx509
Gilles Peskine31987c62020-01-31 14:23:30 +0100224 ]:
225 return False
Gabor Mezei542fd382024-06-10 14:07:42 +0200226 if name in EXCLUDE_FROM_CRYPTO:
227 return False
Gilles Peskine31987c62020-01-31 14:23:30 +0100228 return True
229
230def crypto_adapter(adapter):
231 """Modify an adapter to disable non-crypto symbols.
232
Gilles Peskine702d75a2024-09-19 19:49:20 +0200233 ``crypto_adapter(adapter)(name, value, active, section)`` is like
234 ``adapter(name, value, active, section)``, but unsets all X.509 and TLS symbols.
Gilles Peskine31987c62020-01-31 14:23:30 +0100235 """
Gilles Peskine702d75a2024-09-19 19:49:20 +0200236 def continuation(name, value, active, section):
Gilles Peskine31987c62020-01-31 14:23:30 +0100237 if not include_in_crypto(name):
238 return False
239 if adapter is None:
240 return active
Gilles Peskine702d75a2024-09-19 19:49:20 +0200241 return adapter(name, value, active, section)
Gilles Peskine31987c62020-01-31 14:23:30 +0100242 return continuation
243
Gilles Peskineed5c21d2022-06-27 23:02:09 +0200244DEPRECATED = frozenset([
245 'MBEDTLS_PSA_CRYPTO_SE_C',
Gabor Mezei542fd382024-06-10 14:07:42 +0200246 *PSA_DEPRECATED_FEATURE
Gilles Peskineed5c21d2022-06-27 23:02:09 +0200247])
Gilles Peskine30de2e82020-04-20 21:39:22 +0200248def no_deprecated_adapter(adapter):
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200249 """Modify an adapter to disable deprecated symbols.
250
Gilles Peskine702d75a2024-09-19 19:49:20 +0200251 ``no_deprecated_adapter(adapter)(name, value, active, section)`` is like
252 ``adapter(name, value, active, section)``, but unsets all deprecated symbols
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200253 and sets ``MBEDTLS_DEPRECATED_REMOVED``.
254 """
Gilles Peskine702d75a2024-09-19 19:49:20 +0200255 def continuation(name, value, active, section):
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200256 if name == 'MBEDTLS_DEPRECATED_REMOVED':
257 return True
Gilles Peskineed5c21d2022-06-27 23:02:09 +0200258 if name in DEPRECATED:
259 return False
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200260 if adapter is None:
261 return active
Gilles Peskine702d75a2024-09-19 19:49:20 +0200262 return adapter(name, value, active, section)
Gilles Peskinebe1d6092020-04-12 14:17:16 +0200263 return continuation
264
Paul Elliottfb81f772023-10-18 17:44:59 +0100265def no_platform_adapter(adapter):
266 """Modify an adapter to disable platform symbols.
267
Gilles Peskine702d75a2024-09-19 19:49:20 +0200268 ``no_platform_adapter(adapter)(name, value, active, section)`` is like
269 ``adapter(name, value, active, section)``, but unsets all platform symbols other
Paul Elliottfb81f772023-10-18 17:44:59 +0100270 ``than MBEDTLS_PLATFORM_C.
271 """
Gilles Peskine702d75a2024-09-19 19:49:20 +0200272 def continuation(name, value, active, section):
Paul Elliottfb81f772023-10-18 17:44:59 +0100273 # Allow MBEDTLS_PLATFORM_C but remove all other platform symbols.
274 if name.startswith('MBEDTLS_PLATFORM_') and name != 'MBEDTLS_PLATFORM_C':
275 return False
276 if adapter is None:
277 return active
Gilles Peskine702d75a2024-09-19 19:49:20 +0200278 return adapter(name, value, active, section)
Paul Elliottfb81f772023-10-18 17:44:59 +0100279 return continuation
280
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200281
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200282class MbedTLSConfigFile(config_common.ConfigFile):
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200283 """Representation of an MbedTLS configuration file."""
284
Gabor Mezei3678dee2024-06-04 19:58:43 +0200285 _path_in_tree = 'include/mbedtls/mbedtls_config.h'
286 default_path = [_path_in_tree,
287 os.path.join(os.path.dirname(__file__),
288 os.pardir,
289 _path_in_tree),
290 os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))),
291 _path_in_tree)]
292
293 def __init__(self, filename=None):
Gabor Mezei93a6d1f2024-06-26 18:01:09 +0200294 super().__init__(self.default_path, 'Mbed TLS', filename)
Gabor Mezei3678dee2024-06-04 19:58:43 +0200295 self.current_section = 'header'
296
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200297
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200298class CryptoConfigFile(config_common.ConfigFile):
Gabor Mezei4706fe72024-07-08 17:00:55 +0200299 """Representation of a Crypto configuration file."""
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200300
Gabor Mezei3de65862024-07-08 16:14:10 +0200301 # Temporary, while Mbed TLS does not just rely on the TF-PSA-Crypto
302 # build system to build its crypto library. When it does, the
303 # condition can just be removed.
Gabor Mezei776ee902024-09-09 17:00:50 +0200304 _path_in_tree = ('include/psa/crypto_config.h'
305 if not os.path.isdir(os.path.join(os.path.dirname(__file__),
306 os.pardir,
307 'tf-psa-crypto')) else
308 'tf-psa-crypto/include/psa/crypto_config.h')
Gabor Mezei3678dee2024-06-04 19:58:43 +0200309 default_path = [_path_in_tree,
310 os.path.join(os.path.dirname(__file__),
311 os.pardir,
312 _path_in_tree),
313 os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))),
314 _path_in_tree)]
315
316 def __init__(self, filename=None):
Gabor Mezei93a6d1f2024-06-26 18:01:09 +0200317 super().__init__(self.default_path, 'Crypto', filename)
Gabor Mezei3678dee2024-06-04 19:58:43 +0200318
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200319
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200320class MbedTLSConfig(config_common.Config):
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200321 """Representation of the Mbed TLS configuration.
Gabor Mezei3678dee2024-06-04 19:58:43 +0200322
323 See the documentation of the `Config` class for methods to query
324 and modify the configuration.
325 """
Gabor Mezei4706fe72024-07-08 17:00:55 +0200326
Gabor Mezeiee521b62024-06-07 13:50:41 +0200327 def __init__(self, filename=None):
Gabor Mezei3678dee2024-06-04 19:58:43 +0200328 """Read the Mbed TLS configuration file."""
Gabor Mezei4706fe72024-07-08 17:00:55 +0200329
Gabor Mezei3678dee2024-06-04 19:58:43 +0200330 super().__init__()
Gabor Mezeid53080d2024-08-27 14:06:54 +0200331 configfile = MbedTLSConfigFile(filename)
332 self.configfiles.append(configfile)
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200333 self.settings.update({name: config_common.Setting(configfile, active, name, value, section)
Gabor Mezei92065ed2024-06-07 13:47:59 +0200334 for (active, name, value, section)
Gabor Mezeid53080d2024-08-27 14:06:54 +0200335 in configfile.parse_file()})
Gabor Mezei3678dee2024-06-04 19:58:43 +0200336
337 def set(self, name, value=None):
Gabor Mezei4706fe72024-07-08 17:00:55 +0200338 """Set name to the given value and make it active."""
339
Gabor Mezei3678dee2024-06-04 19:58:43 +0200340 if name not in self.settings:
Gabor Mezeid53080d2024-08-27 14:06:54 +0200341 self._get_configfile().templates.append((name, '', '#define ' + name + ' '))
Gabor Mezeiee521b62024-06-07 13:50:41 +0200342
Gabor Mezei3678dee2024-06-04 19:58:43 +0200343 super().set(name, value)
Gilles Peskineb4063892019-07-27 21:36:44 +0200344
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200345
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200346class CryptoConfig(config_common.Config):
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200347 """Representation of the PSA crypto configuration.
Gabor Mezei3678dee2024-06-04 19:58:43 +0200348
349 See the documentation of the `Config` class for methods to query
350 and modify the configuration.
351 """
Gabor Mezei4706fe72024-07-08 17:00:55 +0200352
Gabor Mezeiee521b62024-06-07 13:50:41 +0200353 def __init__(self, filename=None):
Gabor Mezei3678dee2024-06-04 19:58:43 +0200354 """Read the PSA crypto configuration file."""
Gabor Mezei4706fe72024-07-08 17:00:55 +0200355
Gabor Mezei3678dee2024-06-04 19:58:43 +0200356 super().__init__()
Gabor Mezeid53080d2024-08-27 14:06:54 +0200357 configfile = CryptoConfigFile(filename)
358 self.configfiles.append(configfile)
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200359 self.settings.update({name: config_common.Setting(configfile, active, name, value, section)
Gabor Mezei92065ed2024-06-07 13:47:59 +0200360 for (active, name, value, section)
Gabor Mezeid53080d2024-08-27 14:06:54 +0200361 in configfile.parse_file()})
Gabor Mezei3678dee2024-06-04 19:58:43 +0200362
Gabor Mezeid723b512024-06-07 15:31:52 +0200363 def set(self, name, value='1'):
Gabor Mezei4706fe72024-07-08 17:00:55 +0200364 """Set name to the given value and make it active."""
365
Gabor Mezei542fd382024-06-10 14:07:42 +0200366 if name in PSA_UNSUPPORTED_FEATURE:
Gabor Mezei92065ed2024-06-07 13:47:59 +0200367 raise ValueError(f'Feature is unsupported: \'{name}\'')
Gabor Mezei542fd382024-06-10 14:07:42 +0200368 if name in PSA_UNSTABLE_FEATURE:
Gabor Mezei92065ed2024-06-07 13:47:59 +0200369 raise ValueError(f'Feature is unstable: \'{name}\'')
Gabor Mezei3678dee2024-06-04 19:58:43 +0200370
371 if name not in self.settings:
Gabor Mezeid53080d2024-08-27 14:06:54 +0200372 self._get_configfile().templates.append((name, '', '#define ' + name + ' '))
Gabor Mezeiee521b62024-06-07 13:50:41 +0200373
Gabor Mezei3678dee2024-06-04 19:58:43 +0200374 super().set(name, value)
375
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200376
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200377class CombinedConfig(config_common.Config):
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200378 """Representation of MbedTLS and PSA crypto configuration
379
380 See the documentation of the `Config` class for methods to query
381 and modify the configuration.
382 """
Gabor Mezei3678dee2024-06-04 19:58:43 +0200383
Gabor Mezei3e2a5502024-06-28 17:27:19 +0200384 def __init__(self, *configs):
Gabor Mezeiee521b62024-06-07 13:50:41 +0200385 super().__init__()
Gabor Mezei3e2a5502024-06-28 17:27:19 +0200386 for config in configs:
387 if isinstance(config, MbedTLSConfigFile):
388 self.mbedtls_configfile = config
389 elif isinstance(config, CryptoConfigFile):
390 self.crypto_configfile = config
391 else:
392 raise ValueError(f'Invalid configfile: {config}')
Gabor Mezeid53080d2024-08-27 14:06:54 +0200393 self.configfiles.append(config)
Gabor Mezei3e2a5502024-06-28 17:27:19 +0200394
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200395 self.settings.update({name: config_common.Setting(configfile, active, name, value, section)
Gabor Mezeiee521b62024-06-07 13:50:41 +0200396 for configfile in [self.mbedtls_configfile, self.crypto_configfile]
397 for (active, name, value, section) in configfile.parse_file()})
Gabor Mezei3678dee2024-06-04 19:58:43 +0200398
399 _crypto_regexp = re.compile(r'$PSA_.*')
Gabor Mezeid53080d2024-08-27 14:06:54 +0200400 def _get_configfile(self, name=None):
Gabor Mezei4706fe72024-07-08 17:00:55 +0200401 """Find a config type for a setting name"""
402
Gabor Mezeiee521b62024-06-07 13:50:41 +0200403 if name in self.settings:
404 return self.settings[name].configfile
405 elif re.match(self._crypto_regexp, name):
406 return self.crypto_configfile
Gabor Mezei3678dee2024-06-04 19:58:43 +0200407 else:
Gabor Mezeiee521b62024-06-07 13:50:41 +0200408 return self.mbedtls_configfile
Gabor Mezei3678dee2024-06-04 19:58:43 +0200409
410 def set(self, name, value=None):
Gabor Mezei4706fe72024-07-08 17:00:55 +0200411 """Set name to the given value and make it active."""
412
Gabor Mezeiee521b62024-06-07 13:50:41 +0200413 configfile = self._get_configfile(name)
Gabor Mezei3678dee2024-06-04 19:58:43 +0200414
Gabor Mezeiee521b62024-06-07 13:50:41 +0200415 if configfile == self.crypto_configfile:
Gabor Mezei542fd382024-06-10 14:07:42 +0200416 if name in PSA_UNSUPPORTED_FEATURE:
Gabor Mezeiee521b62024-06-07 13:50:41 +0200417 raise ValueError(f'Feature is unsupported: \'{name}\'')
Gabor Mezei542fd382024-06-10 14:07:42 +0200418 if name in PSA_UNSTABLE_FEATURE:
Gabor Mezeiee521b62024-06-07 13:50:41 +0200419 raise ValueError(f'Feature is unstable: \'{name}\'')
420
Gabor Mezeid723b512024-06-07 15:31:52 +0200421 # The default value in the crypto config is '1'
422 if not value:
423 value = '1'
424
Gabor Mezeic659c1b2024-08-06 17:37:55 +0200425 if name not in self.settings:
Gabor Mezeiee521b62024-06-07 13:50:41 +0200426 configfile.templates.append((name, '', '#define ' + name + ' '))
427
Gabor Mezeid53080d2024-08-27 14:06:54 +0200428 super().set(name, value)
Gabor Mezeiee521b62024-06-07 13:50:41 +0200429
Gabor Mezeidaf807f2024-08-14 11:33:46 +0200430 #pylint: disable=arguments-differ
Gabor Mezei3678dee2024-06-04 19:58:43 +0200431 def write(self, mbedtls_file=None, crypto_file=None):
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200432 """Write the whole configuration to the file it was read from.
433
434 If mbedtls_file or crypto_file is specified, write the specific configuration
435 to the corresponding file instead.
Gabor Mezeif5f13082024-09-18 13:02:16 +0200436
Gabor Mezei317a2a32024-09-18 16:51:27 +0200437 Two file name parameters and not only one as in the super class as we handle
438 two configuration files in this class.
Gabor Mezei62a9bd02024-06-07 13:44:40 +0200439 """
Gabor Mezei4706fe72024-07-08 17:00:55 +0200440
Gabor Mezeiee521b62024-06-07 13:50:41 +0200441 self.mbedtls_configfile.write(self.settings, mbedtls_file)
442 self.crypto_configfile.write(self.settings, crypto_file)
Gabor Mezei3678dee2024-06-04 19:58:43 +0200443
Gabor Mezeiee521b62024-06-07 13:50:41 +0200444 def filename(self, name=None):
Gabor Mezeif5f13082024-09-18 13:02:16 +0200445 """Get the name of the config files.
Gabor Mezei4706fe72024-07-08 17:00:55 +0200446
447 If 'name' is specified return the name of the config file where it is defined.
448 """
449
Gabor Mezeiee521b62024-06-07 13:50:41 +0200450 if not name:
451 return [config.filename for config in [self.mbedtls_configfile, self.crypto_configfile]]
452
453 return self._get_configfile(name).filename
Gilles Peskineb4063892019-07-27 21:36:44 +0200454
Gabor Mezei24d7cc72024-08-06 15:11:24 +0200455
Gabor Mezei0e9e4cb2024-09-10 16:16:35 +0200456class MbedTLSConfigTool(config_common.ConfigTool):
Gabor Mezei24d7cc72024-08-06 15:11:24 +0200457 """Command line mbedtls_config.h and crypto_config.h manipulation tool."""
458
459 def __init__(self):
Gabor Mezeicd326bf2024-09-18 16:53:03 +0200460 super().__init__(MbedTLSConfigFile.default_path)
Gabor Mezei568808a2024-09-18 13:02:42 +0200461 self.config = CombinedConfig(MbedTLSConfigFile(self.args.file),
462 CryptoConfigFile(self.args.cryptofile))
Gabor Mezei24d7cc72024-08-06 15:11:24 +0200463
464 def custom_parser_options(self):
465 """Adds MbedTLS specific options for the parser."""
466
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200467 self.parser.add_argument(
468 '--cryptofile', '-c',
469 help="""Crypto file to read (and modify if requested). Default: {}."""
470 .format(CryptoConfigFile.default_path))
Gabor Mezei24d7cc72024-08-06 15:11:24 +0200471
Gabor Mezeia12ed6b2024-09-09 17:20:49 +0200472 self.add_adapter(
473 'baremetal', baremetal_adapter,
474 """Like full, but exclude features that require platform features
475 such as file input-output.
476 """)
477 self.add_adapter(
478 'baremetal_size', baremetal_size_adapter,
479 """Like baremetal, but exclude debugging features. Useful for code size measurements.
480 """)
481 self.add_adapter(
482 'full', full_adapter,
483 """Uncomment most features.
484 Exclude alternative implementations and platform support options, as well as
485 some options that are awkward to test.
486 """)
487 self.add_adapter(
488 'full_no_deprecated', no_deprecated_adapter(full_adapter),
489 """Uncomment most non-deprecated features.
490 Like "full", but without deprecated features.
491 """)
492 self.add_adapter(
493 'full_no_platform', no_platform_adapter(full_adapter),
494 """Uncomment most non-platform features. Like "full", but without platform features.
495 """)
496 self.add_adapter(
497 'realfull', realfull_adapter,
498 """Uncomment all boolean #defines.
499 Suitable for generating documentation, but not for building.
500 """)
501 self.add_adapter(
502 'crypto', crypto_adapter(None),
503 """Only include crypto features. Exclude X.509 and TLS.""")
504 self.add_adapter(
505 'crypto_baremetal', crypto_adapter(baremetal_adapter),
506 """Like baremetal, but with only crypto features, excluding X.509 and TLS.""")
507 self.add_adapter(
508 'crypto_full', crypto_adapter(full_adapter),
509 """Like full, but with only crypto features, excluding X.509 and TLS.""")
Gilles Peskineb4063892019-07-27 21:36:44 +0200510
Gilles Peskineb4063892019-07-27 21:36:44 +0200511
Gabor Mezei24d7cc72024-08-06 15:11:24 +0200512if __name__ == '__main__':
513 sys.exit(MbedTLSConfigTool().main())