optiga: move initialization code out of verify function
What changed, and why it matters
This is a small internal cleanup in the firmware that talks to the Optiga secure chip. A function that both initialized the chip and checked chip settings was split so that one part only does the settings check. The actual setup steps are now done in the main setup function instead. There is no direct evidence this fixes a security bug; it appears to be a code-quality refactor to make the verification function do only what its name says.
Treat as a non-security refactor. No immediate action required. If reviewing for security, confirm that the moved initialization steps still execute unconditionally before metadata verification and that error paths return early as before.
Security signals we found
Refactor of secure-element initialization and verification flow
Protection-level checks and application-open moved to caller
No new bounds checks, authentication, or error handling introduced
Evidence from the diff
The commit refactors optiga_setup() in src/optiga/optiga.c. Previously, _verify_config() set the comms protocol version to pre-shared-secret, checked that crypt/util protection levels are FULL, opened the util application, and (when VERIFY_METADATA==1) verified slot metadata. The patch moves the protocol-version setup, protection-level checks, and open-application call into optiga_setup(), leaving _verify_metadata_config() to only verify metadata tags. The behavior is functionally equivalent because the moved code still runs in the same order before the metadata verification. No logic changes or additional checks were added.
Changed components
src/optiga/optiga.coptiga_setup()_verify_config() / _verify_metadata_config()Inspect captured patch +33 / −24
diff --git a/src/optiga/optiga.c b/src/optiga/optiga.c
index 65fe8b9..4aff2cb 100644
--- a/src/optiga/optiga.c
+++ b/src/optiga/optiga.c
@@ -1433,30 +1433,11 @@ cleanup: {
}
}
-static int _verify_config(void)
+#if VERIFY_METADATA == 1
+static int _verify_metadata_config(void)
{
int res;
- OPTIGA_UTIL_SET_COMMS_PROTOCOL_VERSION(_util, OPTIGA_COMMS_PROTOCOL_VERSION_PRE_SHARED_SECRET);
- OPTIGA_CRYPT_SET_COMMS_PROTOCOL_VERSION(
- _crypt, OPTIGA_COMMS_PROTOCOL_VERSION_PRE_SHARED_SECRET);
-
- // Verify shielded connection is active.
- if (_crypt->protection_level != OPTIGA_COMMS_FULL_PROTECTION) {
- util_log("crypt protection level expected to be FULL");
- return SC_ERR_CONFIG_MISMATCH;
- }
- if (_util->protection_level != OPTIGA_COMMS_FULL_PROTECTION) {
- util_log("util protection level expected to be FULL");
- return SC_ERR_CONFIG_MISMATCH;
- }
-
- res = _optiga_util_open_application_sync(_util, 0);
- if (res) {
- return res;
- }
-
-#if VERIFY_METADATA == 1
// Verify metadata tags are setup as expected.
{
const uint8_t check_tags[] = {0xC0, 0xD0, 0xD1, 0xD3, 0xE8};
@@ -1571,9 +1552,9 @@ static int _verify_config(void)
return res;
}
}
-#endif
return 0;
}
+#endif
int optiga_setup(const securechip_interface_functions_t* ifs)
{
@@ -1588,8 +1569,10 @@ int optiga_setup(const securechip_interface_functions_t* ifs)
// event loop
pal_timer_init();
+ int res;
+
#if FACTORYSETUP == 1 || FACTORY_DURING_PROD == 1
- int res = _factory_setup();
+ res = _factory_setup();
if (res) {
util_log("factory setup failed");
return res;
@@ -1606,7 +1589,33 @@ int optiga_setup(const securechip_interface_functions_t* ifs)
return SC_OPTIGA_ERR_CREATE;
}
- return _verify_config();
+ OPTIGA_UTIL_SET_COMMS_PROTOCOL_VERSION(_util, OPTIGA_COMMS_PROTOCOL_VERSION_PRE_SHARED_SECRET);
+ OPTIGA_CRYPT_SET_COMMS_PROTOCOL_VERSION(
+ _crypt, OPTIGA_COMMS_PROTOCOL_VERSION_PRE_SHARED_SECRET);
+
+ // Verify shielded connection is active.
+ if (_crypt->protection_level != OPTIGA_COMMS_FULL_PROTECTION) {
+ util_log("crypt protection level expected to be FULL");
+ return SC_ERR_CONFIG_MISMATCH;
+ }
+ if (_util->protection_level != OPTIGA_COMMS_FULL_PROTECTION) {
+ util_log("util protection level expected to be FULL");
+ return SC_ERR_CONFIG_MISMATCH;
+ }
+
+ res = _optiga_util_open_application_sync(_util, 0);
+ if (res) {
+ return res;
+ }
+
+#if VERIFY_METADATA == 1
+ res = _verify_metadata_config();
+ if (res) {
+ return res;
+ }
+#endif
+
+ return 0;
}
int optiga_kdf_external(const uint8_t* msg, size_t len, uint8_t* mac_out)
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.