optiga: also include config update in unit tests
What changed, and why it matters
This commit only adds unit tests for an existing configuration update routine in the Optiga secure chip driver. It moves some internal constants from a source file to a header so the test code can use them, and updates test fakes so the config-update path is exercised during automated testing. There is no change to production firmware behavior and no security fix.
No security action required; this is a test-coverage improvement. Review the tests for correctness as part of normal QA.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff removes a TESTING-only early return in _maybe_update_config_v1() so the config update code can run under tests, and moves METADATA_MAX_SIZE, LCSO_STATE_*, and FINAL_LCSO_STATE_* definitions from src/optiga/optiga.c to src/optiga/optiga.h for test visibility. The unit test fakes are extended to simulate metadata read/write for two OIDs, and _reset_fakes() is renamed _setup_test() to include fake_memory_factoryreset() and optiga_setup(). No production logic is altered.
Changed components
src/optiga/optiga.csrc/optiga/optiga.htest/unit-test/test_optiga.cInspect captured patch +81 / −38
diff --git a/src/optiga/optiga.c b/src/optiga/optiga.c
index 3471de5..b1d2e73 100644
--- a/src/optiga/optiga.c
+++ b/src/optiga/optiga.c
@@ -38,11 +38,6 @@
// indication of 600000 updates. See Solution Reference Manual Figure 32.
#define MONOTONIC_COUNTER_MAX_USE (590000)
-// Maximum size of metadata. See "Metadata Update Identifier":
-// https://github.com/Infineon/optiga-trust-m-overview/blob/98b2b9c178f0391b1ab26b52082899704dab688a/docs/OPTIGA%E2%84%A2%20Trust%20M%20Solution%20Reference%20Manual.md#linka946a953_def2_41cf_850a_74fb7899fe11
-// Two extra bytes for the `0x20 <len>` header bytes.
-#define METADATA_MAX_SIZE (44 + 2)
-
// See Solution Reference Manual Table 79 "Data structure arbitrary data object".
#define ARBITRARY_DATA_OBJECT_TYPE_3_MAX_SIZE 140
@@ -67,19 +62,8 @@ static optiga_crypt_t* _crypt;
static const securechip_interface_functions_t* _ifs = NULL;
-// Values of life cycle states.
-// See Table "Life Cycle Status":
-// https://github.com/Infineon/optiga-trust-m-overview/blob/98b2b9c178f0391b1ab26b52082899704dab688a/docs/OPTIGA%E2%84%A2%20Trust%20M%20Solution%20Reference%20Manual.md#link05d4c12a_5c94_4a05_a05d_102c53684d3d
-#define LCSO_STATE_CREATION (0x01)
-#define LCSO_STATE_OPERATIONAL (0x07)
-
#define TAG_LCSO 0xC0
-// Set the object LcsO flag to Operational. After this, the metadata cannot be changed anymore.
-// During development, set this to `LCSO_STATE_CREATION`.
-#define FINAL_LCSO_STATE_V0 LCSO_STATE_OPERATIONAL
-#define FINAL_LCSO_STATE_V1 LCSO_STATE_OPERATIONAL
-
#if FACTORYSETUP == 1 || FACTORY_DURING_PROD == 1 || VERIFY_METADATA == 1
static const uint8_t _platform_binding_metadata[] = {
// Metadata tag in the data object
@@ -1068,11 +1052,6 @@ static int _verify_metadata(
// Updates Optiga config to V1 if not already done.
static int _maybe_update_config_v1(void)
{
-#ifdef TESTING
- // We don't yet unit-test the config update.
- return 0;
-#endif
-
memory_optiga_config_version_t config_version;
if (!memory_get_optiga_config_version(&config_version)) {
return SC_ERR_MEMORY;
diff --git a/src/optiga/optiga.h b/src/optiga/optiga.h
index 54a86dd..85fcb70 100644
--- a/src/optiga/optiga.h
+++ b/src/optiga/optiga.h
@@ -65,6 +65,22 @@
// The counter is reset when the correct password is entered.
#define SMALL_MONOTONIC_COUNTER_MAX_USE (MAX_UNLOCK_ATTEMPTS)
+// Values of life cycle states.
+// See Table "Life Cycle Status":
+// https://github.com/Infineon/optiga-trust-m-overview/blob/98b2b9c178f0391b1ab26b52082899704dab688a/docs/OPTIGA%E2%84%A2%20Trust%20M%20Solution%20Reference%20Manual.md#link05d4c12a_5c94_4a05_a05d_102c53684d3d
+#define LCSO_STATE_CREATION (0x01)
+#define LCSO_STATE_OPERATIONAL (0x07)
+
+// Set the object LcsO flag to Operational. After this, the metadata cannot be changed anymore.
+// During development, set this to `LCSO_STATE_CREATION`.
+#define FINAL_LCSO_STATE_V0 LCSO_STATE_OPERATIONAL
+#define FINAL_LCSO_STATE_V1 LCSO_STATE_OPERATIONAL
+
+// Maximum size of metadata. See "Metadata Update Identifier":
+// https://github.com/Infineon/optiga-trust-m-overview/blob/98b2b9c178f0391b1ab26b52082899704dab688a/docs/OPTIGA%E2%84%A2%20Trust%20M%20Solution%20Reference%20Manual.md#linka946a953_def2_41cf_850a_74fb7899fe11
+// Two extra bytes for the `0x20 <len>` header bytes.
+#define METADATA_MAX_SIZE (44 + 2)
+
USE_RESULT int optiga_setup(const securechip_interface_functions_t* ifs);
USE_RESULT int optiga_kdf_external(const uint8_t* msg, size_t len, uint8_t* mac_out);
USE_RESULT int optiga_init_new_password(
diff --git a/test/unit-test/test_optiga.c b/test/unit-test/test_optiga.c
index bc18e4f..02688b0 100644
--- a/test/unit-test/test_optiga.c
+++ b/test/unit-test/test_optiga.c
@@ -5,6 +5,7 @@
#include <stddef.h>
#include <cmocka.h>
+#include <fake_memory.h>
#include <memory/memory.h>
#include <optiga/optiga.h>
#include <optiga/optiga_ops.h>
@@ -137,6 +138,11 @@ static const uint8_t _expected_stretched_out_v1[32] = {
0X4D, 0XC5, 0X08, 0XF3, 0XB5, 0XB9, 0X57, 0X7D, 0XD9, 0XDD, 0X57, 0XEE, 0XCF, 0X49, 0X67, 0X51,
};
+static uint8_t _hmac_writeprotected_metadata[METADATA_MAX_SIZE] = {0};
+static uint16_t _hmac_writeprotected_metadata_len = 0;
+static uint8_t _counter_hmac_writeprotected_metadata[METADATA_MAX_SIZE] = {0};
+static uint16_t _counter_hmac_writeprotected_metadata_len = 0;
+
//------------------------------------------------------------------------------
// Minimal securechip interface fakes.
@@ -205,12 +211,39 @@ static uint8_t _oid_counter_hmac_writeprotected_buf[8];
static bool _authorized_password;
static bool _authorized_password_secret;
-static void _reset_fakes(void)
+static void _setup_test(void)
{
+ fake_memory_factoryreset();
+ memory_optiga_config_version_t config_version;
+ assert_true(memory_get_optiga_config_version(&config_version));
+ assert_int_equal(config_version, MEMORY_OPTIGA_CONFIG_V0);
+
memset(_oid_password, 0, sizeof(_oid_password));
_oid_password_set = false;
_authorized_password = false;
_authorized_password_secret = false;
+
+ // Initial metadata mock is minimal and only contains the LCSO state, as we read that out first
+ // to determine if these slot needs to be configured.
+ uint8_t metadata[5] = {
+ // Metadata tag in the data object
+ 0x20,
+ // Number of bytes that follow
+ 0x03,
+ 0xC0,
+ 0x01,
+ // Forces a config update, as it is not OPERATIONAL.
+ LCSO_STATE_CREATION,
+ };
+ memcpy(_hmac_writeprotected_metadata, metadata, sizeof(metadata));
+ _hmac_writeprotected_metadata_len = sizeof(metadata);
+ memcpy(_counter_hmac_writeprotected_metadata, metadata, sizeof(metadata));
+ _counter_hmac_writeprotected_metadata_len = sizeof(metadata);
+
+ assert_int_equal(optiga_setup(&_ifs), 0);
+ // After setup, the config is updated.
+ assert_true(memory_get_optiga_config_version(&config_version));
+ assert_int_equal(config_version, MEMORY_OPTIGA_CONFIG_V1);
}
static uint32_t _get_counter(uint16_t oid)
@@ -342,10 +375,21 @@ optiga_lib_status_t optiga_ops_util_read_metadata_sync(
uint16_t* length)
{
(void)me;
- (void)optiga_oid;
- (void)buffer;
- (void)length;
- return OPTIGA_UTIL_ERROR;
+ switch (optiga_oid) {
+ case OID_HMAC_WRITEPROTECTED:
+ memcpy(buffer, _hmac_writeprotected_metadata, _hmac_writeprotected_metadata_len);
+ *length = _hmac_writeprotected_metadata_len;
+ return OPTIGA_UTIL_SUCCESS;
+ case OID_COUNTER_HMAC_WRITEPROTECTED:
+ memcpy(
+ buffer,
+ _counter_hmac_writeprotected_metadata,
+ _counter_hmac_writeprotected_metadata_len);
+ *length = _counter_hmac_writeprotected_metadata_len;
+ return OPTIGA_UTIL_SUCCESS;
+ default:
+ return OPTIGA_UTIL_ERROR;
+ }
}
optiga_lib_status_t optiga_ops_util_write_metadata_sync(
@@ -355,10 +399,18 @@ optiga_lib_status_t optiga_ops_util_write_metadata_sync(
uint8_t length)
{
(void)me;
- (void)optiga_oid;
- (void)buffer;
- (void)length;
- return OPTIGA_UTIL_ERROR;
+ switch (optiga_oid) {
+ case OID_HMAC_WRITEPROTECTED:
+ memcpy(_hmac_writeprotected_metadata, buffer, length);
+ _hmac_writeprotected_metadata_len = length;
+ return OPTIGA_UTIL_SUCCESS;
+ case OID_COUNTER_HMAC_WRITEPROTECTED:
+ memcpy(_counter_hmac_writeprotected_metadata, buffer, length);
+ _counter_hmac_writeprotected_metadata_len = length;
+ return OPTIGA_UTIL_SUCCESS;
+ default:
+ return OPTIGA_UTIL_ERROR;
+ }
}
optiga_lib_status_t optiga_ops_util_read_data_sync(
@@ -698,8 +750,7 @@ optiga_lib_status_t optiga_ops_crypt_ecdsa_sign_sync(
static void test_optiga_stretch_password_v0_success(void** state)
{
(void)state;
- _reset_fakes();
- assert_int_equal(optiga_setup(&_ifs), 0);
+ _setup_test();
// Seed the OID_PASSWORD and OID_PASSWORD_COUNTER objects as if they were provisioned earlier.
assert_true(salt_hash_data((const uint8_t*)"pw", 2, "optiga_password", _oid_password));
@@ -720,8 +771,7 @@ static void test_optiga_stretch_password_v0_success(void** state)
static void test_optiga_stretch_password_v0_attempt_counter(void** state)
{
(void)state;
- _reset_fakes();
- assert_int_equal(optiga_setup(&_ifs), 0);
+ _setup_test();
// Seed the OID_PASSWORD and OID_PASSWORD_COUNTER objects as if they were provisioned earlier.
assert_true(salt_hash_data((const uint8_t*)"pw", 2, "optiga_password", _oid_password));
@@ -770,8 +820,7 @@ static void test_optiga_stretch_password_v0_attempt_counter(void** state)
static void test_optiga_password_v1_stretch_exhaust_fails_after_init(void** state)
{
(void)state;
- _reset_fakes();
- assert_int_equal(optiga_setup(&_ifs), 0);
+ _setup_test();
uint8_t stretched[32] = {0};
assert_int_equal(optiga_init_new_password("pw", MEMORY_PASSWORD_STRETCH_ALGO_V1, stretched), 0);
@@ -815,8 +864,7 @@ static void test_optiga_password_v1_stretch_exhaust_fails_after_init(void** stat
static void test_optiga_password_v1(void** state)
{
(void)state;
- _reset_fakes();
- assert_int_equal(optiga_setup(&_ifs), 0);
+ _setup_test();
uint8_t stretched[32] = {0};
assert_int_equal(optiga_init_new_password("pw", MEMORY_PASSWORD_STRETCH_ALGO_V1, stretched), 0);
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.