optiga: add _reset_counter util function
What changed, and why it matters
This commit is a simple code cleanup: it replaces three separate places that reset a hardware security counter with one shared helper function. The actual behavior of the firmware does not change. There is no security bug being fixed here.
No security action needed. Treat as normal refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors counter reset logic in src/optiga/optiga.c by introducing _reset_counter(uint16_t oid, uint32_t limit). It removes the static _counter_password_reset_buf and the inline counter_buf initializations, replacing them with calls to the new helper. The helper builds the same 8-byte counter structure (4 bytes initial value 0, 4 bytes big-endian threshold) and calls optiga_ops_util_write_data_sync with OPTIGA_UTIL_ERASE_AND_WRITE. The data written and the OIDs used remain identical; only duplication is reduced.
Changed components
src/optiga/optiga.cInspect captured patch +17 / −38
diff --git a/src/optiga/optiga.c b/src/optiga/optiga.c
index 705c8d0..2771996 100644
--- a/src/optiga/optiga.c
+++ b/src/optiga/optiga.c
@@ -414,17 +414,6 @@ static const uint8_t _counter_password_metadata[] = {
#endif
-#if SMALL_MONOTONIC_COUNTER_MAX_USE > 255
- #error Max unlock attempts does not fit in one byte
-#endif
-// The intial/reset buffer for the small monotonic counter. Initial value: 0, threshold:
-// `SMALL_MONOTONIC_COUNTER_MAX_USE`. Table "Common data structures" -> "Counter":
-// https://github.com/Infineon/optiga-trust-m-overview/blob/98b2b9c178f0391b1ab26b52082899704dab688a/docs/OPTIGA%E2%84%A2%20Trust%20M%20Solution%20Reference%20Manual.md#link24b48059_db81_40f5_8b65_7afca4918ab1
-// Bytes 0-3 are the initial counter value, set to 0.
-// Bytes 4-7 are the threshold.
-// Ints are encoded as uint32 big endian.
-static const uint8_t _counter_password_reset_buf[8] =
- {0, 0, 0, 0, 0, 0, 0, SMALL_MONOTONIC_COUNTER_MAX_USE};
static int _authorize(uint16_t oid_auth, const uint8_t* auth_secret, size_t auth_secret_len)
{
optiga_lib_status_t res;
@@ -455,6 +444,20 @@ static int _authorize(uint16_t oid_auth, const uint8_t* auth_secret, size_t auth
return 0;
}
+static int _reset_counter(uint16_t oid, uint32_t limit)
+{
+ // Configure the monotonic counter.
+ // Table "Common data structures" -> "Counter":
+ // https://github.com/Infineon/optiga-trust-m-overview/blob/98b2b9c178f0391b1ab26b52082899704dab688a/docs/OPTIGA%E2%84%A2%20Trust%20M%20Solution%20Reference%20Manual.md#link24b48059_db81_40f5_8b65_7afca4918ab1
+ // Bytes 0-3 are the initial counter value, set to 0.
+ // Bytes 4-7 are the threshold.
+ // Ints are encoded as uint32 big endian.
+ uint8_t counter_buf[8] = {0};
+ optiga_common_set_uint32(&counter_buf[4], limit);
+ return optiga_ops_util_write_data_sync(
+ _util, oid, OPTIGA_UTIL_ERASE_AND_WRITE, 0, counter_buf, sizeof(counter_buf));
+}
+
#if APP_U2F == 1 || FACTORYSETUP == 1
static bool _read_arbitrary_data(arbitrary_data_t* data_out)
{
@@ -731,19 +734,7 @@ static int _configure_object_counter(void)
}
util_log("_configure_object_counter: setting up");
- // Configure the monotonic counter.
- // Table "Common data structures" -> "Counter":
- // https://github.com/Infineon/optiga-trust-m-overview/blob/98b2b9c178f0391b1ab26b52082899704dab688a/docs/OPTIGA%E2%84%A2%20Trust%20M%20Solution%20Reference%20Manual.md#link24b48059_db81_40f5_8b65_7afca4918ab1
- // Bytes 0-3 are the initial counter value, set to 0.
- // Bytes 4-7 are the threshold.
- // Ints are encoded as uint32 big endian.
- uint8_t counter_buf[8] = {0};
- optiga_common_set_uint32(&counter_buf[4], MONOTONIC_COUNTER_MAX_USE);
- res = optiga_ops_util_write_data_sync(
- _util, oid, OPTIGA_UTIL_ERASE_AND_WRITE, 0, counter_buf, sizeof(counter_buf));
- if (res != OPTIGA_LIB_SUCCESS) {
- return res;
- }
+ _reset_counter(oid, MONOTONIC_COUNTER_MAX_USE);
return optiga_ops_util_write_metadata_sync(
_util, oid, _counter_metadata, sizeof(_counter_metadata));
@@ -986,13 +977,7 @@ static int _set_password(
goto cleanup;
}
- res = optiga_ops_util_write_data_sync(
- _util,
- OID_COUNTER_PASSWORD,
- OPTIGA_UTIL_ERASE_AND_WRITE,
- 0,
- _counter_password_reset_buf,
- sizeof(_counter_password_reset_buf));
+ res = _reset_counter(OID_COUNTER_PASSWORD, SMALL_MONOTONIC_COUNTER_MAX_USE);
if (res != OPTIGA_LIB_SUCCESS) {
goto cleanup;
}
@@ -1106,13 +1091,7 @@ static int _optiga_verify_password(const char* password, uint8_t* password_secre
goto cleanup;
}
- res = optiga_ops_util_write_data_sync(
- _util,
- OID_COUNTER_PASSWORD,
- OPTIGA_UTIL_ERASE_AND_WRITE,
- 0,
- _counter_password_reset_buf,
- sizeof(_counter_password_reset_buf));
+ res = _reset_counter(OID_COUNTER_PASSWORD, SMALL_MONOTONIC_COUNTER_MAX_USE);
if (res != OPTIGA_LIB_SUCCESS) {
goto cleanup;
}
Why this scored 15/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.