Coverage for tropicsquare / config / __init__.py: 100%
69 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 21:24 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 21:24 +0000
1"""Configuration objects for TROPIC01 secure element
3 This module provides classes for parsing and manipulating TROPIC01
4 configuration registers. Configuration is split into two memory spaces:
6 - R-CONFIG (Reversible): Can be written and erased freely
7 - I-CONFIG (Irreversible): Bits can only change from 1 to 0 permanently
9 The effective configuration is the AND of R-CONFIG and I-CONFIG values.
11 Example::
13 from tropicsquare import TropicSquare
14 from tropicsquare.constants.config import CFG_START_UP
15 from tropicsquare.config.startup import StartUpConfig
17 transport = ... # Initialize transport (e.g., I2C, SPI)
18 ts = TropicSquare(transport)
19 ts.start_secure_session(...)
21 # Read R-CONFIG startup register (auto-parsed)
22 config = ts.r_config_read(CFG_START_UP)
23 print(config.mbist_dis)
25 # Read I-CONFIG and compute effective value
26 r_config = ts.r_config_read(CFG_START_UP)
27 i_config = ts.i_config_read(CFG_START_UP)
28 effective = StartUpConfig(r_config._value & i_config._value)
29"""
31from tropicsquare.config.base import BaseConfig
32from tropicsquare.config.startup import StartUpConfig
33from tropicsquare.config.sensors import SensorsConfig
34from tropicsquare.config.debug import DebugConfig
35from tropicsquare.config.gpo import GpoConfig
36from tropicsquare.config.sleep_mode import SleepModeConfig
37from tropicsquare.config.uap_pairing_key import (
38 PairingKeyWriteConfig,
39 PairingKeyReadConfig,
40 PairingKeyInvalidateConfig
41)
42from tropicsquare.config.uap_rconfig_iconfig import (
43 RConfigWriteEraseConfig,
44 RConfigReadConfig,
45 IConfigWriteConfig,
46 IConfigReadConfig
47)
48from tropicsquare.config.uap_operations import (
49 PingConfig,
50 RandomValueGetConfig,
51 MacAndDestroyConfig
52)
53from tropicsquare.config.uap_memory import (
54 RMemDataWriteConfig,
55 RMemDataReadConfig,
56 RMemDataEraseConfig
57)
58from tropicsquare.config.uap_ecc import (
59 EccKeyGenerateConfig,
60 EccKeyStoreConfig,
61 EccKeyReadConfig,
62 EccKeyEraseConfig,
63 EcdsaSignConfig,
64 EddsaSignConfig
65)
66from tropicsquare.config.uap_mcounter import (
67 MCounterInitConfig,
68 MCounterGetConfig,
69 MCounterUpdateConfig
70)
72from tropicsquare.constants.config import (
73 CFG_START_UP,
74 CFG_SENSORS,
75 CFG_DEBUG,
76 CFG_GPO,
77 CFG_SLEEP_MODE,
78 CFG_UAP_PAIRING_KEY_WRITE,
79 CFG_UAP_PAIRING_KEY_READ,
80 CFG_UAP_PAIRING_KEY_INVALIDATE,
81 CFG_UAP_R_CONFIG_WRITE_ERASE,
82 CFG_UAP_R_CONFIG_READ,
83 CFG_UAP_I_CONFIG_WRITE,
84 CFG_UAP_I_CONFIG_READ,
85 CFG_UAP_PING,
86 CFG_UAP_R_MEM_DATA_WRITE,
87 CFG_UAP_R_MEM_DATA_READ,
88 CFG_UAP_R_MEM_DATA_ERASE,
89 CFG_UAP_RANDOM_VALUE_GET,
90 CFG_UAP_ECC_KEY_GENERATE,
91 CFG_UAP_ECC_KEY_STORE,
92 CFG_UAP_ECC_KEY_READ,
93 CFG_UAP_ECC_KEY_ERASE,
94 CFG_UAP_ECDSA_SIGN,
95 CFG_UAP_EDDSA_SIGN,
96 CFG_UAP_MCOUNTER_INIT,
97 CFG_UAP_MCOUNTER_GET,
98 CFG_UAP_MCOUNTER_UPDATE,
99 CFG_UAP_MAC_AND_DESTROY
100)
103def parse_config(register: int, data: bytes) -> BaseConfig:
104 """Parse config data into appropriate Config object.
106 Factory function that creates the correct config object type
107 based on the register address.
109 :Note: This function is used internally by r_config_read() and i_config_read().
110 You typically don't need to call this directly as those methods return
111 already-parsed config objects.
113 :param register: Registry address (use CFG_* constants from tropicsquare.constants.config)
114 :param data: 4 bytes of raw config data
116 :returns: Appropriate config object (StartUpConfig, SensorsConfig, etc.)
117 :rtype: BaseConfig
119 :raises ValueError: If register address is unknown
122 Example::
124 from tropicsquare.constants.config import CFG_START_UP
125 from tropicsquare.config import parse_config
127 raw_data = b'\\x12\\x34\\x56\\x78'
128 config = parse_config(CFG_START_UP, raw_data)
129 print(config.mbist_dis)
130 """
131 if register == CFG_START_UP:
132 return StartUpConfig.from_bytes(data)
133 elif register == CFG_SENSORS:
134 return SensorsConfig.from_bytes(data)
135 elif register == CFG_DEBUG:
136 return DebugConfig.from_bytes(data)
137 elif register == CFG_GPO:
138 return GpoConfig.from_bytes(data)
139 elif register == CFG_SLEEP_MODE:
140 return SleepModeConfig.from_bytes(data)
141 elif register == CFG_UAP_PAIRING_KEY_WRITE:
142 return PairingKeyWriteConfig.from_bytes(data)
143 elif register == CFG_UAP_PAIRING_KEY_READ:
144 return PairingKeyReadConfig.from_bytes(data)
145 elif register == CFG_UAP_PAIRING_KEY_INVALIDATE:
146 return PairingKeyInvalidateConfig.from_bytes(data)
147 elif register == CFG_UAP_R_CONFIG_WRITE_ERASE:
148 return RConfigWriteEraseConfig.from_bytes(data)
149 elif register == CFG_UAP_R_CONFIG_READ:
150 return RConfigReadConfig.from_bytes(data)
151 elif register == CFG_UAP_I_CONFIG_WRITE:
152 return IConfigWriteConfig.from_bytes(data)
153 elif register == CFG_UAP_I_CONFIG_READ:
154 return IConfigReadConfig.from_bytes(data)
155 elif register == CFG_UAP_PING:
156 return PingConfig.from_bytes(data)
157 elif register == CFG_UAP_R_MEM_DATA_WRITE:
158 return RMemDataWriteConfig.from_bytes(data)
159 elif register == CFG_UAP_R_MEM_DATA_READ:
160 return RMemDataReadConfig.from_bytes(data)
161 elif register == CFG_UAP_R_MEM_DATA_ERASE:
162 return RMemDataEraseConfig.from_bytes(data)
163 elif register == CFG_UAP_RANDOM_VALUE_GET:
164 return RandomValueGetConfig.from_bytes(data)
165 elif register == CFG_UAP_ECC_KEY_GENERATE:
166 return EccKeyGenerateConfig.from_bytes(data)
167 elif register == CFG_UAP_ECC_KEY_STORE:
168 return EccKeyStoreConfig.from_bytes(data)
169 elif register == CFG_UAP_ECC_KEY_READ:
170 return EccKeyReadConfig.from_bytes(data)
171 elif register == CFG_UAP_ECC_KEY_ERASE:
172 return EccKeyEraseConfig.from_bytes(data)
173 elif register == CFG_UAP_ECDSA_SIGN:
174 return EcdsaSignConfig.from_bytes(data)
175 elif register == CFG_UAP_EDDSA_SIGN:
176 return EddsaSignConfig.from_bytes(data)
177 elif register == CFG_UAP_MCOUNTER_INIT:
178 return MCounterInitConfig.from_bytes(data)
179 elif register == CFG_UAP_MCOUNTER_GET:
180 return MCounterGetConfig.from_bytes(data)
181 elif register == CFG_UAP_MCOUNTER_UPDATE:
182 return MCounterUpdateConfig.from_bytes(data)
183 elif register == CFG_UAP_MAC_AND_DESTROY:
184 return MacAndDestroyConfig.from_bytes(data)
185 else:
186 raise ValueError("Unknown config register: 0x{:02x}".format(register))