optiga: split stretch_password into v0 and v1
What changed, and why it matters
This commit is a straightforward internal code refactor. It splits one password-stretching function into two versions (v0 and v1), keeps the existing v0 behavior unchanged, and adds a v1 placeholder that simply returns an error because it is not yet implemented. There is no indication this fixes or introduces a security bug.
No security action required. Treat as normal maintenance/refactoring. If v1 implementation is planned, review it separately when submitted.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change renames _optiga_verify_password to _optiga_verify_password_v0, extracts the existing optiga_stretch_password body into _stretch_password_v0, and adds _stretch_password_v1 returning SC_ERR_INVALID_PASSWORD_STRETCH_ALGO. The public optiga_stretch_password now dispatches via a switch on memory_password_stretch_algo_t. Unit tests are renamed and updated to reference v0 constants only. No functional behavior changes for v0; v1 is explicitly unimplemented.
Changed components
src/optiga/optiga.ctest/unit-test/test_optiga.cInspect captured patch +36 / −15
diff --git a/src/optiga/optiga.c b/src/optiga/optiga.c
index 30537eb..7da7f71 100644
--- a/src/optiga/optiga.c
+++ b/src/optiga/optiga.c
@@ -1300,7 +1300,7 @@ bool optiga_reset_keys(void)
return optiga_init_new_password("", MEMORY_PASSWORD_STRETCH_ALGO_V1) == 0;
}
-static int _optiga_verify_password(const char* password, uint8_t* password_secret_out)
+static int _optiga_verify_password_v0(const char* password, uint8_t* password_secret_out)
{
uint8_t password_salted_hashed[32] = {0};
UTIL_CLEANUP_32(password_salted_hashed);
@@ -1545,13 +1545,8 @@ int optiga_kdf_external(const uint8_t* msg, size_t len, uint8_t* mac_out)
return _kdf_hmac(OID_HMAC, msg, len, mac_out);
}
-int optiga_stretch_password(
- const char* password,
- memory_password_stretch_algo_t password_stretch_algo,
- uint8_t* stretched_out)
+static int _stretch_password_v0(const char* password, uint8_t* stretched_out)
{
- (void)password_stretch_algo;
-
uint8_t password_salted_hashed[32] = {0};
UTIL_CLEANUP_32(password_salted_hashed);
if (!salt_hash_data(
@@ -1583,7 +1578,7 @@ int optiga_stretch_password(
// Verify password incrementing the small monotonic counter.
// We do this after the above KDF stretch so the big monotonic counter is also incremented.
uint8_t password_secret[32] = {0};
- int res = _optiga_verify_password(password, password_secret);
+ int res = _optiga_verify_password_v0(password, password_secret);
if (res) {
if (res == 0x802F) {
return SC_ERR_INCORRECT_PASSWORD;
@@ -1605,6 +1600,31 @@ int optiga_stretch_password(
return 0;
}
+static int _stretch_password_v1(const char* password, uint8_t* stretched_out)
+{
+ // TODO implement
+ (void)password;
+ (void)stretched_out;
+ return SC_ERR_INVALID_PASSWORD_STRETCH_ALGO;
+}
+
+int optiga_stretch_password(
+ const char* password,
+ memory_password_stretch_algo_t password_stretch_algo,
+ uint8_t* stretched_out)
+{
+ switch (password_stretch_algo) {
+ case MEMORY_PASSWORD_STRETCH_ALGO_V0:
+ util_log("stretching password using algo v0");
+ return _stretch_password_v0(password, stretched_out);
+ case MEMORY_PASSWORD_STRETCH_ALGO_V1:
+ util_log("stretching password using algo v1");
+ return _stretch_password_v1(password, stretched_out);
+ default:
+ return SC_ERR_INVALID_PASSWORD_STRETCH_ALGO;
+ }
+}
+
bool optiga_gen_attestation_key(uint8_t* pubkey_out)
{
optiga_key_id_t slot = OPTIGA_KEY_ID_E0F1;
diff --git a/test/unit-test/test_optiga.c b/test/unit-test/test_optiga.c
index 0e97bb8..a751218 100644
--- a/test/unit-test/test_optiga.c
+++ b/test/unit-test/test_optiga.c
@@ -47,7 +47,7 @@ static const uint8_t _auth_code_random_fixed[32] = {
0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
};
-// Expected stretched_out for password "pw" given the above fakes.
+// Expected stretched_out for password "pw" for the V0 algorithm given the above fakes.
//
// Repro script (mirrors optiga_stretch_password() with the unit test fakes):
// ```python
@@ -85,7 +85,7 @@ static const uint8_t _auth_code_random_fixed[32] = {
// stretched = hmac_sha256(out_salt, stretched)
// print(stretched.hex())
// ```
-static const uint8_t _expected_stretched_out[32] = {
+static const uint8_t _expected_stretched_out_v0[32] = {
0xC4, 0x1F, 0x87, 0xB7, 0xC9, 0xF3, 0x16, 0x9C, 0x14, 0xF3, 0xF2, 0x62, 0x87, 0x09, 0x3C, 0x31,
0x18, 0x19, 0x06, 0x77, 0x76, 0xF6, 0x16, 0x3B, 0x8A, 0x0F, 0xDF, 0x3D, 0xFB, 0x8B, 0x8E, 0xBB,
};
@@ -582,7 +582,7 @@ optiga_lib_status_t optiga_ops_crypt_ecdsa_sign_sync(
//------------------------------------------------------------------------------
// Tests
-static void test_optiga_stretch_password_success(void** state)
+static void test_optiga_stretch_password_v0_success(void** state)
{
(void)state;
_reset_fakes();
@@ -597,13 +597,14 @@ static void test_optiga_stretch_password_success(void** state)
uint8_t stretched_out[32] = {0};
assert_int_equal(
optiga_stretch_password("pw", MEMORY_PASSWORD_STRETCH_ALGO_V0, stretched_out), 0);
- assert_memory_equal(stretched_out, _expected_stretched_out, sizeof(_expected_stretched_out));
+ assert_memory_equal(
+ stretched_out, _expected_stretched_out_v0, sizeof(_expected_stretched_out_v0));
// Successful password verification resets the small monotonic counter/threshold.
assert_int_equal(_get_counter(OID_COUNTER_PASSWORD), 0);
assert_int_equal(_get_threshold(OID_COUNTER_PASSWORD), SMALL_MONOTONIC_COUNTER_MAX_USE);
}
-static void test_optiga_stretch_password_attempt_counter(void** state)
+static void test_optiga_stretch_password_v0_attempt_counter(void** state)
{
(void)state;
_reset_fakes();
@@ -653,8 +654,8 @@ static void test_optiga_stretch_password_attempt_counter(void** state)
int main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_optiga_stretch_password_success),
- cmocka_unit_test(test_optiga_stretch_password_attempt_counter),
+ cmocka_unit_test(test_optiga_stretch_password_v0_success),
+ cmocka_unit_test(test_optiga_stretch_password_v0_attempt_counter),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
Why this scored 11/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.