Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | """TF PSA Crypto configuration file manipulation library and tool |
| 4 | |
| 5 | Basic usage, to read the TF PSA Crypto configuration: |
Gabor Mezei | afc5fa5 | 2024-11-29 12:57:53 +0100 | [diff] [blame] | 6 | config = TFPSACryptoConfig() |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 7 | if 'PSA_WANT_ALG_MD5' in config: print('MD5 is enabled') |
| 8 | """ |
| 9 | |
| 10 | ## Copyright The Mbed TLS Contributors |
| 11 | ## SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 12 | ## |
| 13 | |
Gabor Mezei | fc719d6 | 2024-12-05 18:40:22 +0100 | [diff] [blame] | 14 | import re |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 15 | import os |
| 16 | import sys |
| 17 | |
| 18 | import framework_scripts_path # pylint: disable=unused-import |
| 19 | from mbedtls_framework import config_common |
| 20 | |
| 21 | |
Gabor Mezei | fc719d6 | 2024-12-05 18:40:22 +0100 | [diff] [blame] | 22 | PSA_SYMBOL_REGEXP = re.compile(r'^PSA_.*') |
| 23 | |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 24 | PSA_UNSUPPORTED_FEATURE = frozenset([ |
| 25 | 'PSA_WANT_ALG_CBC_MAC', |
| 26 | 'PSA_WANT_ALG_XTS', |
| 27 | 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_DERIVE', |
| 28 | 'PSA_WANT_KEY_TYPE_DH_KEY_PAIR_DERIVE' |
| 29 | ]) |
| 30 | |
Gabor Mezei | 871cde6 | 2024-11-20 17:08:19 +0100 | [diff] [blame] | 31 | PSA_DEPRECATED_FEATURE = frozenset([ |
| 32 | 'PSA_WANT_KEY_TYPE_ECC_KEY_PAIR', |
| 33 | 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR' |
| 34 | ]) |
| 35 | |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 36 | PSA_UNSTABLE_FEATURE = frozenset([ |
| 37 | 'PSA_WANT_ECC_SECP_K1_224' |
| 38 | ]) |
| 39 | |
Gabor Mezei | 871cde6 | 2024-11-20 17:08:19 +0100 | [diff] [blame] | 40 | # The goal of the full configuration is to have everything that can be tested |
| 41 | # together. This includes deprecated or insecure options. It excludes: |
| 42 | # * Options that require additional build dependencies or unusual hardware. |
| 43 | # * Options that make testing less effective. |
| 44 | # * Options that are incompatible with other options, or more generally that |
| 45 | # interact with other parts of the code in such a way that a bulk enabling |
| 46 | # is not a good way to test them. |
| 47 | # * Options that remove features. |
| 48 | EXCLUDE_FROM_FULL = frozenset([ |
| 49 | #pylint: disable=line-too-long |
| 50 | 'MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH', # interacts with CTR_DRBG_128_BIT_KEY |
| 51 | 'MBEDTLS_AES_USE_HARDWARE_ONLY', # hardware dependency |
| 52 | 'MBEDTLS_BLOCK_CIPHER_NO_DECRYPT', # incompatible with ECB in PSA, CBC/XTS/NIST_KW/DES |
| 53 | 'MBEDTLS_CTR_DRBG_USE_128_BIT_KEY', # interacts with ENTROPY_FORCE_SHA256 |
| 54 | 'MBEDTLS_DEPRECATED_REMOVED', # conflicts with deprecated options |
| 55 | 'MBEDTLS_DEPRECATED_WARNING', # conflicts with deprecated options |
| 56 | 'MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED', # influences the use of ECDH in TLS |
| 57 | 'MBEDTLS_ECP_WITH_MPI_UINT', # disables the default ECP and is experimental |
| 58 | 'MBEDTLS_ENTROPY_FORCE_SHA256', # interacts with CTR_DRBG_128_BIT_KEY |
| 59 | 'MBEDTLS_HAVE_SSE2', # hardware dependency |
| 60 | 'MBEDTLS_MEMORY_BACKTRACE', # depends on MEMORY_BUFFER_ALLOC_C |
| 61 | 'MBEDTLS_MEMORY_DEBUG', # depends on MEMORY_BUFFER_ALLOC_C |
| 62 | 'MBEDTLS_NO_64BIT_MULTIPLICATION', # influences anything that uses bignum |
| 63 | 'MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES', # removes a feature |
| 64 | 'MBEDTLS_NO_PLATFORM_ENTROPY', # removes a feature |
| 65 | 'MBEDTLS_NO_UDBL_DIVISION', # influences anything that uses bignum |
| 66 | 'MBEDTLS_PSA_P256M_DRIVER_ENABLED', # influences SECP256R1 KeyGen/ECDH/ECDSA |
| 67 | 'MBEDTLS_PLATFORM_NO_STD_FUNCTIONS', # removes a feature |
| 68 | 'MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS', # removes a feature |
| 69 | 'MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG', # behavior change + build dependency |
| 70 | 'MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER', # interface and behavior change |
| 71 | 'MBEDTLS_PSA_CRYPTO_SPM', # platform dependency (PSA SPM) |
| 72 | 'MBEDTLS_PSA_INJECT_ENTROPY', # conflicts with platform entropy sources |
| 73 | 'MBEDTLS_RSA_NO_CRT', # influences the use of RSA in X.509 and TLS |
| 74 | 'MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT |
| 75 | 'MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY', # interacts with *_USE_ARMV8_A_CRYPTO_IF_PRESENT |
| 76 | 'MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT |
| 77 | 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', # setting *_USE_ARMV8_A_CRYPTO is sufficient |
| 78 | 'MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN', # build dependency (clang+memsan) |
| 79 | 'MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND', # build dependency (valgrind headers) |
Gabor Mezei | 871cde6 | 2024-11-20 17:08:19 +0100 | [diff] [blame] | 80 | *PSA_UNSUPPORTED_FEATURE, |
| 81 | *PSA_DEPRECATED_FEATURE, |
| 82 | *PSA_UNSTABLE_FEATURE |
| 83 | ]) |
| 84 | |
| 85 | def is_boolean_setting(name, value): |
| 86 | """Is this a boolean setting? |
| 87 | |
| 88 | Mbed TLS boolean settings are enabled if the preprocessor macro is |
| 89 | defined, and disabled if the preprocessor macro is not defined. The |
| 90 | macro definition line in the configuration file has an empty expansion. |
| 91 | |
| 92 | PSA_WANT_xxx settings are also boolean, but when they are enabled, |
| 93 | they expand to a nonzero value. We leave them undefined when they |
| 94 | are disabled. (Setting them to 0 currently means to enable them, but |
| 95 | this might change to mean disabling them. Currently we just never set |
| 96 | them to 0.) |
| 97 | """ |
Gabor Mezei | fc719d6 | 2024-12-05 18:40:22 +0100 | [diff] [blame] | 98 | if re.match(PSA_SYMBOL_REGEXP, name): |
Gabor Mezei | 871cde6 | 2024-11-20 17:08:19 +0100 | [diff] [blame] | 99 | return True |
| 100 | if not value: |
| 101 | return True |
| 102 | return False |
| 103 | |
Gabor Mezei | 1a7bbe1 | 2024-12-05 10:17:13 +0100 | [diff] [blame] | 104 | def is_seamless_alt(name): |
| 105 | """Whether the xxx_ALT symbol should be included in the full configuration. |
| 106 | |
| 107 | Include alternative implementations of platform functions, which are |
| 108 | configurable function pointers that default to the built-in function. |
| 109 | This way we test that the function pointers exist and build correctly |
| 110 | without changing the behavior, and tests can verify that the function |
| 111 | pointers are used by modifying those pointers. |
| 112 | |
| 113 | Exclude alternative implementations of library functions since they require |
| 114 | an implementation of the relevant functions and an xxx_alt.h header. |
| 115 | """ |
| 116 | if name in ( |
| 117 | 'MBEDTLS_PLATFORM_GMTIME_R_ALT', |
| 118 | 'MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT', |
| 119 | 'MBEDTLS_PLATFORM_MS_TIME_ALT', |
| 120 | 'MBEDTLS_PLATFORM_ZEROIZE_ALT', |
| 121 | ): |
| 122 | # Similar to non-platform xxx_ALT, requires platform_alt.h |
| 123 | return False |
| 124 | return name.startswith('MBEDTLS_PLATFORM_') |
| 125 | |
Gabor Mezei | 871cde6 | 2024-11-20 17:08:19 +0100 | [diff] [blame] | 126 | def include_in_full(name): |
| 127 | """Rules for symbols in the "full" configuration.""" |
| 128 | if name in EXCLUDE_FROM_FULL: |
| 129 | return False |
Gabor Mezei | 1a7bbe1 | 2024-12-05 10:17:13 +0100 | [diff] [blame] | 130 | if name.endswith('_ALT'): |
| 131 | return is_seamless_alt(name) |
Gabor Mezei | 871cde6 | 2024-11-20 17:08:19 +0100 | [diff] [blame] | 132 | return True |
| 133 | |
| 134 | def full_adapter(name, value, active): |
| 135 | """Config adapter for "full".""" |
| 136 | if not is_boolean_setting(name, value): |
| 137 | return active |
| 138 | return include_in_full(name) |
| 139 | |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 140 | |
Gabor Mezei | afc5fa5 | 2024-11-29 12:57:53 +0100 | [diff] [blame] | 141 | class TFPSACryptoConfigFile(config_common.ConfigFile): |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 142 | """Representation of a TF PSA Crypto configuration file.""" |
| 143 | |
Gabor Mezei | 37bf61e | 2024-11-29 12:59:26 +0100 | [diff] [blame] | 144 | _path_in_tree = 'include/psa/crypto_config.h' |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 145 | default_path = [_path_in_tree, |
| 146 | os.path.join(os.path.dirname(__file__), |
| 147 | os.pardir, |
| 148 | _path_in_tree), |
Gabor Mezei | c716aad | 2024-12-05 18:41:27 +0100 | [diff] [blame] | 149 | os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 150 | _path_in_tree)] |
| 151 | |
| 152 | def __init__(self, filename=None): |
Gabor Mezei | 1044a80 | 2024-11-29 12:58:44 +0100 | [diff] [blame] | 153 | super().__init__(self.default_path, 'TF-PSA-Crypto', filename) |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 154 | |
| 155 | |
Gabor Mezei | afc5fa5 | 2024-11-29 12:57:53 +0100 | [diff] [blame] | 156 | class TFPSACryptoConfig(config_common.Config): |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 157 | """Representation of the TF PSA Crypto configuration. |
| 158 | |
| 159 | See the documentation of the `Config` class for methods to query |
| 160 | and modify the configuration. |
| 161 | """ |
| 162 | |
Gabor Mezei | fb36814 | 2024-12-05 10:11:46 +0100 | [diff] [blame] | 163 | def __init__(self, filename=None): |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 164 | """Read the PSA crypto configuration files.""" |
| 165 | |
| 166 | super().__init__() |
Gabor Mezei | 680a7c3 | 2024-11-29 13:09:10 +0100 | [diff] [blame] | 167 | configfile = TFPSACryptoConfigFile(filename) |
| 168 | self.configfiles.append(configfile) |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 169 | self.settings.update({name: config_common.Setting(configfile, active, name, value, section) |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 170 | for (active, name, value, section) in configfile.parse_file()}) |
| 171 | |
| 172 | def set(self, name, value=None): |
| 173 | """Set name to the given value and make it active.""" |
| 174 | |
| 175 | if name in PSA_UNSUPPORTED_FEATURE: |
| 176 | raise ValueError(f'Feature is unsupported: \'{name}\'') |
| 177 | if name in PSA_UNSTABLE_FEATURE: |
| 178 | raise ValueError(f'Feature is unstable: \'{name}\'') |
| 179 | |
| 180 | if name not in self.settings: |
| 181 | self._get_configfile().templates.append((name, '', f'#define {name} ')) |
| 182 | |
| 183 | # Default value for PSA macros is '1' |
Gabor Mezei | fc719d6 | 2024-12-05 18:40:22 +0100 | [diff] [blame] | 184 | if not value and re.match(PSA_SYMBOL_REGEXP, name): |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 185 | value = '1' |
| 186 | |
| 187 | super().set(name, value) |
| 188 | |
| 189 | |
Gabor Mezei | afc5fa5 | 2024-11-29 12:57:53 +0100 | [diff] [blame] | 190 | class TFPSACryptoConfigTool(config_common.ConfigTool): |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 191 | """Command line TF PSA Crypto config file manipulation tool.""" |
| 192 | |
| 193 | def __init__(self): |
Gabor Mezei | 680a7c3 | 2024-11-29 13:09:10 +0100 | [diff] [blame] | 194 | super().__init__(TFPSACryptoConfigFile.default_path[0]) |
| 195 | self.config = TFPSACryptoConfig(self.args.file) |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 196 | |
| 197 | def custom_parser_options(self): |
| 198 | """Adds TF PSA Crypto specific options for the parser.""" |
| 199 | |
Gabor Mezei | 871cde6 | 2024-11-20 17:08:19 +0100 | [diff] [blame] | 200 | self.add_adapter( |
| 201 | 'full', full_adapter, |
| 202 | """Uncomment most features. |
| 203 | Exclude alternative implementations and platform support options, as well as |
| 204 | some options that are awkward to test. |
| 205 | """) |
| 206 | |
Gabor Mezei | 3191144 | 2024-11-20 17:05:16 +0100 | [diff] [blame] | 207 | |
| 208 | if __name__ == '__main__': |
Gabor Mezei | afc5fa5 | 2024-11-29 12:57:53 +0100 | [diff] [blame] | 209 | sys.exit(TFPSACryptoConfigTool().main()) |