What changed, and why it matters
This commit is a straightforward code reorganization and addition of unit tests for the BitBox02's OPTIGA secure-chip driver. It moves synchronous OPTIGA wrapper functions into a new file (optiga_ops.c/h) and adds a test file (test_optiga.c) with fake implementations so the password-stretching algorithm can be tested against known test vectors. There is no security vulnerability here; it is purely a maintainability and test-coverage change.
No security action required. Reviewers may optionally verify that the refactored wrapper functions preserve the original _WAIT macro semantics and that optiga_ops_create() returns the same SC_OPTIGA_ERR_CREATE error on failure.
Security signals we found
No functional change to production password-stretching, HMAC/CMAC KDF, or monotonic-counter logic.
No new attack surface introduced; the new optiga_ops functions are direct synchronous wrappers around the existing OPTIGA library.
Unit-test fakes are isolated to the test binary and are not compiled into firmware.
No changes to memory allocation, bounds checking, or secret-handling semantics.
Evidence from the diff
The change refactors src/optiga/optiga.c by extracting the synchronous OPTIGA util/crypt wrappers into src/optiga/optiga_ops.c and exposing them via optiga_ops.h. Several OID and counter defines are moved to optiga.h so unit tests can reference them. A new unit test, test/unit-test/test_optiga.c, provides fake implementations of the optiga_ops API and a linker-wrapped salt_hash_data to exercise optiga_stretch_password() with deterministic fixtures. The production code behavior is preserved: all former static optigasync calls are replaced with equivalent optiga_ops_sync calls, and util/crypt object creation is centralized in optiga_ops_create().
Changed components
src/optiga/optiga.csrc/optiga/optiga.hsrc/optiga/optiga_ops.csrc/optiga/optiga_ops.htest/unit-test/test_optiga.csrc/CMakeLists.txttest/unit-test/CMakeLists.txtInspect captured patch +1143 / −350
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 134c20a..a62113f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -170,6 +170,7 @@ set(SECURECHIP-SOURCES
${CMAKE_SOURCE_DIR}/src/optiga/pal/pal_os_lock.c
${CMAKE_SOURCE_DIR}/src/optiga/pal/pal_os_timer.c
${CMAKE_SOURCE_DIR}/src/optiga/pal/pal_os_memory.c
+ ${CMAKE_SOURCE_DIR}/src/optiga/optiga_ops.c
${CMAKE_SOURCE_DIR}/src/optiga/optiga.c
)
set(SECURECHIP-SOURCES ${SECURECHIP-SOURCES} PARENT_SCOPE)
diff --git a/src/optiga/optiga.c b/src/optiga/optiga.c
index d0de94a..705c8d0 100644
--- a/src/optiga/optiga.c
+++ b/src/optiga/optiga.c
@@ -14,6 +14,8 @@
#include "optiga.h"
+#include "optiga_ops.h"
+
#include "pal/pal_i2c.h"
#include "pal/pal_os_datastore.h"
#include "pal/pal_os_timer.h"
@@ -47,52 +49,11 @@
// indication of 600000 updates. See Solution Reference Manual Figure 32.
#define MONOTONIC_COUNTER_MAX_USE (590000)
-// Number of times the password can be entered incorrectly before further password stretching fails.
-// The counter is reset when the correct password is entered.
-#define SMALL_MONOTONIC_COUNTER_MAX_USE (MAX_UNLOCK_ATTEMPTS)
-
// 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)
-// The Data Object IDs we use.
-
-// Stores a shared secret used for a shielded connection. Is is used to encrypt
-// communications. Read/write disabled.
-#define OID_PLATFORM_BINDING 0xE140
-// CMAC key slot, read/write prohibited. Used to perform KDF using CMAC. Key is regenerated at
-// factory setup and with each device reset. Monotonic counter `OID_COUNTER` attached.
-#define OID_AES_SYMKEY 0xE200
-// HMAC key slot, read prohibited, write allowed. 32 random bytes are written to it at factory setup
-// and with each device reset.
-#define OID_HMAC 0xF1D0
-// Arbitrary data object, stores up to 140 bytes. Used to store the U2F counter.
-#define OID_ARBITRARY_DATA 0xF1D1
-// ECC slot used for creating the device attestation key and signing with it. Read/write
-// disabled. The Key is internally generated at factory setup and used to sign the device
-// attestation host challenge.
-#define OID_ATTESTATION 0xE0F1
-// Monotonic counter, initialized at 0 and attached to `OID_AES_SYMKEY` - every CMAC generation
-// increments the counter. When the threshold `MONOTONIC_COUNTER_MAX_USE` is reached, further CMAC
-// computations return an error.
-#define OID_COUNTER 0xE120
-
-// The three objects below (`OID_PASSWORD_SECRET`, `OID_PASSWORD`, `OID_COUNTER_PASSWORD`) deal with
-// implementing the small monotonic counter that limits the number of unlocks to a small number.
-
-// A random shared key which authorizes updating `OID_PASSWORD` and `OID_COUNTER_PASSWORD`.
-// It is also part of the password stretching.
-#define OID_PASSWORD_SECRET 0xF1D2
-// A hmac digest of the device password.
-// Authorizes reading `OID_PASSWORD`. Monotonic counter `OID_COUNTER_PASSWORD` attached to
-// authorization, which limits the number of unlock attempts to a small number.
-#define OID_PASSWORD 0xF1D3
-// Monotonic counter with a small limit. Every password stretch increments the counter. A correct
-// password resets the counter. When the threshold `SMALL_MONOTONIC_COUNTER_MAX_USE` is reached,
-// further password stretches return an error.
-#define OID_COUNTER_PASSWORD 0xE121
-
// See Solution Reference Manual Table 79 "Data structure arbitrary data object".
#define ARBITRARY_DATA_OBJECT_TYPE_3_MAX_SIZE 140
@@ -464,268 +425,13 @@ static const uint8_t _counter_password_metadata[] = {
// 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};
-
-//
-// Sync wrappers around optiga util/crypt functions
-//
-
-// The OPTIGA library is asynchronous and will schedule a callback when the command is done. The
-// callback will set this shared variable to the result of the command.
-static volatile optiga_lib_status_t _optiga_lib_status;
-
-static void _optiga_lib_callback(void* callback_ctx, optiga_lib_status_t event)
-{
- (void)callback_ctx;
- _optiga_lib_status = event;
-}
-
-// Helper that is used in the main thread to busy wait for the callback to update the shared
-// variable.
-// It first checks the return status of the command, then busy waits, and then checks the
-// asynchronous return status.
-// Will return from caller if command failed.
-// `return_status` will be updated with the actual return status
-// Return statuses are documented in optiga_lib_return_codes.h
-#define _WAIT(return_status, optiga_lib_status) \
- do { \
- if ((return_status) != OPTIGA_UTIL_SUCCESS) { \
- return (return_status); \
- } \
- while (OPTIGA_LIB_BUSY == (optiga_lib_status)) { \
- } \
- if (OPTIGA_LIB_SUCCESS != (optiga_lib_status)) { \
- return (optiga_lib_status); \
- } \
- (return_status) = (optiga_lib_status); \
- } while (0)
-
-static optiga_lib_status_t _optiga_util_read_data_sync(
- optiga_util_t* me,
- uint16_t optiga_oid,
- uint16_t offset,
- uint8_t* buffer,
- uint16_t* length)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res = optiga_util_read_data(me, optiga_oid, offset, buffer, length);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-
-static optiga_lib_status_t _optiga_util_write_data_sync(
- optiga_util_t* me,
- uint16_t optiga_oid,
- uint8_t write_type,
- uint16_t offset,
- const uint8_t* buffer,
- uint16_t length)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res =
- optiga_util_write_data(me, optiga_oid, write_type, offset, buffer, length);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-
-#if FACTORYSETUP == 1 || FACTORY_DURING_PROD == 1 || VERIFY_METADATA == 1
-static optiga_lib_status_t _optiga_util_read_metadata_sync(
- optiga_util_t* me,
- uint16_t optiga_oid,
- uint8_t* buffer,
- uint16_t* length)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res = optiga_util_read_metadata(me, optiga_oid, buffer, length);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-#endif
-
-#if FACTORYSETUP == 1 || FACTORY_DURING_PROD == 1
-static optiga_lib_status_t _optiga_util_write_metadata_sync(
- optiga_util_t* me,
- uint16_t optiga_oid,
- const uint8_t* buffer,
- uint8_t length)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res = optiga_util_write_metadata(me, optiga_oid, buffer, length);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-#endif
-
-static optiga_lib_status_t _optiga_util_open_application_sync(
- optiga_util_t* me,
- bool_t perform_restore)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res = optiga_util_open_application(me, perform_restore);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-
-#if FACTORYSETUP == 1 || FACTORY_DURING_PROD == 1
-static optiga_lib_status_t _optiga_util_close_application_sync(
- optiga_util_t* me,
- bool_t perform_hibernate)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res = optiga_util_close_application(me, perform_hibernate);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-#endif
-
-static optiga_lib_status_t _optiga_crypt_hmac_sync(
- optiga_crypt_t* me,
- optiga_hmac_type_t type,
- uint16_t secret,
- const uint8_t* input_data,
- uint32_t input_data_length,
- uint8_t* mac,
- uint32_t* mac_length)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res =
- optiga_crypt_hmac(me, type, secret, input_data, input_data_length, mac, mac_length);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-
-static optiga_lib_status_t _optiga_crypt_ecc_generate_keypair_sync(
- optiga_crypt_t* me,
- optiga_ecc_curve_t curve_id,
- uint8_t key_usage,
- bool_t export_private_key,
- void* private_key,
- uint8_t* public_key,
- uint16_t* public_key_length)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res = optiga_crypt_ecc_generate_keypair(
- me, curve_id, key_usage, export_private_key, private_key, public_key, public_key_length);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-
-static optiga_lib_status_t _optiga_crypt_ecdsa_sign_sync(
- optiga_crypt_t* me,
- const uint8_t* digest,
- uint8_t digest_length,
- optiga_key_id_t private_key,
- uint8_t* signature,
- uint16_t* signature_length)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res = optiga_crypt_ecdsa_sign(
- me, digest, digest_length, private_key, signature, signature_length);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-
-static optiga_lib_status_t _optiga_crypt_symmetric_encrypt_sync(
- optiga_crypt_t* me,
- optiga_symmetric_encryption_mode_t encryption_mode,
- optiga_key_id_t symmetric_key_oid,
- const uint8_t* plain_data,
- uint32_t plain_data_length,
- const uint8_t* iv,
- uint16_t iv_length,
- const uint8_t* associated_data,
- uint16_t associated_data_length,
- uint8_t* encrypted_data,
- uint32_t* encrypted_data_length)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res = optiga_crypt_symmetric_encrypt(
- me,
- encryption_mode,
- symmetric_key_oid,
- plain_data,
- plain_data_length,
- iv,
- iv_length,
- associated_data,
- associated_data_length,
- encrypted_data,
- encrypted_data_length);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-
-static optiga_lib_status_t _optiga_crypt_random_sync(
- optiga_crypt_t* me,
- optiga_rng_type_t rng_type,
- uint8_t* random_data,
- uint16_t random_data_length)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res = optiga_crypt_random(me, rng_type, random_data, random_data_length);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-
-static optiga_lib_status_t _optiga_crypt_symmetric_generate_key_sync(
- optiga_crypt_t* me,
- optiga_symmetric_key_type_t key_type,
- uint8_t key_usage,
- bool_t export_symmetric_key,
- void* symmetric_key)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res = optiga_crypt_symmetric_generate_key(
- me, key_type, key_usage, export_symmetric_key, symmetric_key);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-
-static optiga_lib_status_t _optiga_crypt_generate_auth_code_sync(
- optiga_crypt_t* me,
- optiga_rng_type_t rng_type,
- const uint8_t* optional_data,
- uint16_t optional_data_length,
- uint8_t* random_data,
- uint16_t random_data_length)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res = optiga_crypt_generate_auth_code(
- me, rng_type, optional_data, optional_data_length, random_data, random_data_length);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-
-static optiga_lib_status_t _optiga_crypt_clear_auto_state_sync(optiga_crypt_t* me, uint16_t secret)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res = optiga_crypt_clear_auto_state(me, secret);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-
-static optiga_lib_status_t _optiga_crypt_hmac_verify_sync(
- optiga_crypt_t* me,
- optiga_hmac_type_t type,
- uint16_t secret,
- const uint8_t* input_data,
- uint32_t input_data_length,
- const uint8_t* hmac,
- uint32_t hmac_length)
-{
- _optiga_lib_status = OPTIGA_LIB_BUSY;
- optiga_lib_status_t res = optiga_crypt_hmac_verify(
- me, type, secret, input_data, input_data_length, hmac, hmac_length);
- _WAIT(res, _optiga_lib_status);
- return res;
-}
-
static int _authorize(uint16_t oid_auth, const uint8_t* auth_secret, size_t auth_secret_len)
{
optiga_lib_status_t res;
uint8_t random_data[32] = {0};
- res = _optiga_crypt_generate_auth_code_sync(
+ res = optiga_ops_crypt_generate_auth_code_sync(
_crypt, OPTIGA_RNG_TYPE_TRNG, NULL, 0, random_data, sizeof(random_data));
if (res != OPTIGA_CRYPT_SUCCESS) {
util_log("generate auth code failed: %x", res);
@@ -734,7 +440,7 @@ static int _authorize(uint16_t oid_auth, const uint8_t* auth_secret, size_t auth
uint8_t hmac[32] = {0};
rust_hmac_sha256(auth_secret, auth_secret_len, random_data, sizeof(random_data), hmac);
- res = _optiga_crypt_hmac_verify_sync(
+ res = optiga_ops_crypt_hmac_verify_sync(
_crypt,
OPTIGA_HMAC_SHA_256,
oid_auth,
@@ -755,7 +461,7 @@ static bool _read_arbitrary_data(arbitrary_data_t* data_out)
memset(data_out->bytes, 0x00, sizeof(data_out->bytes));
uint16_t len = sizeof(data_out->bytes);
optiga_lib_status_t res =
- _optiga_util_read_data_sync(_util, OID_ARBITRARY_DATA, 0, data_out->bytes, &len);
+ optiga_ops_util_read_data_sync(_util, OID_ARBITRARY_DATA, 0, data_out->bytes, &len);
if (res != OPTIGA_UTIL_SUCCESS) {
util_log("could not read arbitrary data: %x", res);
return false;
@@ -775,7 +481,7 @@ static bool _read_arbitrary_data(arbitrary_data_t* data_out)
#if APP_U2F == 1 || FACTORYSETUP == 1 || FACTORY_DURING_PROD == 1
static int _write_arbitrary_data(const arbitrary_data_t* data)
{
- optiga_lib_status_t res = _optiga_util_write_data_sync(
+ optiga_lib_status_t res = optiga_ops_util_write_data_sync(
_util,
OID_ARBITRARY_DATA,
OPTIGA_UTIL_ERASE_AND_WRITE,
@@ -872,7 +578,7 @@ static int _read_lcso_of_object(uint16_t optiga_oid, uint8_t* lcso_out, bool unp
OPTIGA_UTIL_SET_COMMS_PROTECTION_LEVEL(_util, OPTIGA_COMMS_NO_PROTECTION);
}
optiga_lib_status_t res =
- _optiga_util_read_metadata_sync(_util, optiga_oid, metadata, &metadata_size);
+ optiga_ops_util_read_metadata_sync(_util, optiga_oid, metadata, &metadata_size);
if (res != OPTIGA_LIB_SUCCESS) {
util_log("fail: read binding secret metadata: %x", res);
return res;
@@ -919,7 +625,7 @@ static int _setup_shielded_communication(void)
// slot. Shielded communication is disabled as it is not set up yet and not required for
// updating the platform binding object.
OPTIGA_UTIL_SET_COMMS_PROTECTION_LEVEL(_util, OPTIGA_COMMS_NO_PROTECTION);
- res = _optiga_util_write_data_sync(
+ res = optiga_ops_util_write_data_sync(
_util,
oid,
OPTIGA_UTIL_ERASE_AND_WRITE,
@@ -934,7 +640,7 @@ static int _setup_shielded_communication(void)
// Shielded communication is disabled as it is not set up yet and not required for updating the
// platform binding object.
OPTIGA_UTIL_SET_COMMS_PROTECTION_LEVEL(_util, OPTIGA_COMMS_NO_PROTECTION);
- res = _optiga_util_write_metadata_sync(
+ res = optiga_ops_util_write_metadata_sync(
_util, oid, _platform_binding_metadata, sizeof(_platform_binding_metadata));
if (res != OPTIGA_LIB_SUCCESS) {
util_log("fail: write metadata of platform binding: %x", res);
@@ -958,7 +664,7 @@ static int _configure_object_aes_symkey(void)
return 0;
}
util_log("_configure_object_aes_symkey: setting up");
- return _optiga_util_write_metadata_sync(
+ return optiga_ops_util_write_metadata_sync(
_util, oid, _aes_symkey_metadata, sizeof(_aes_symkey_metadata));
}
@@ -976,7 +682,7 @@ static int _configure_object_hmac(void)
return 0;
}
util_log("_configure_object_hmac: setting up");
- return _optiga_util_write_metadata_sync(_util, oid, _hmac_metadata, sizeof(_hmac_metadata));
+ return optiga_ops_util_write_metadata_sync(_util, oid, _hmac_metadata, sizeof(_hmac_metadata));
}
static int _configure_object_arbitrary_data(void)
@@ -994,7 +700,7 @@ static int _configure_object_arbitrary_data(void)
}
util_log("_configure_object_arbitrary_data: setting up");
- res = _optiga_util_write_metadata_sync(
+ res = optiga_ops_util_write_metadata_sync(
_util, oid, _arbitrary_data_metadata, sizeof(_arbitrary_data_metadata));
if (res != OPTIGA_LIB_SUCCESS) {
return res;
@@ -1033,13 +739,13 @@ static int _configure_object_counter(void)
// 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_util_write_data_sync(
+ 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;
}
- return _optiga_util_write_metadata_sync(
+ return optiga_ops_util_write_metadata_sync(
_util, oid, _counter_metadata, sizeof(_counter_metadata));
}
@@ -1058,7 +764,7 @@ static int _configure_object_attestation(void)
}
util_log("_configure_attestation: setting up");
- return _optiga_util_write_metadata_sync(
+ return optiga_ops_util_write_metadata_sync(
_util, oid, _attestation_metadata, sizeof(_attestation_metadata));
}
@@ -1076,7 +782,7 @@ static int _configure_object_password_secret(void)
return 0;
}
util_log("_configure_object_password_secret: setting up");
- return _optiga_util_write_metadata_sync(
+ return optiga_ops_util_write_metadata_sync(
_util, oid, _password_secret_metadata, sizeof(_password_secret_metadata));
}
@@ -1094,7 +800,7 @@ static int _configure_object_password(void)
return 0;
}
util_log("_configure_object_password: setting up");
- return _optiga_util_write_metadata_sync(
+ return optiga_ops_util_write_metadata_sync(
_util, oid, _password_metadata, sizeof(_password_metadata));
}
@@ -1113,7 +819,7 @@ static int _configure_object_counter_password(void)
}
util_log("_configure_object_counter_password: setting up");
- return _optiga_util_write_metadata_sync(
+ return optiga_ops_util_write_metadata_sync(
_util, oid, _counter_password_metadata, sizeof(_counter_password_metadata));
}
@@ -1175,16 +881,9 @@ static int _factory_setup(void)
{
optiga_lib_status_t res;
- _util = optiga_util_create(OPTIGA_INSTANCE_ID_0, _optiga_lib_callback, NULL);
- if (NULL == _util) {
- util_log("couldn't create optiga util");
- return SC_OPTIGA_ERR_CREATE;
- }
-
- _crypt = optiga_crypt_create(OPTIGA_INSTANCE_ID_0, _optiga_lib_callback, NULL);
- if (NULL == _crypt) {
- util_log("couldn't create optiga crypt");
- return SC_OPTIGA_ERR_CREATE;
+ res = optiga_ops_create(&_util, &_crypt);
+ if (res) {
+ return res;
}
OPTIGA_UTIL_SET_COMMS_PROTOCOL_VERSION(_util, OPTIGA_COMMS_PROTOCOL_VERSION_PRE_SHARED_SECRET);
@@ -1192,7 +891,7 @@ static int _factory_setup(void)
_crypt, OPTIGA_COMMS_PROTOCOL_VERSION_PRE_SHARED_SECRET);
OPTIGA_UTIL_SET_COMMS_PROTECTION_LEVEL(_util, OPTIGA_COMMS_NO_PROTECTION);
- res = _optiga_util_open_application_sync(_util, 0);
+ res = optiga_ops_util_open_application_sync(_util, 0);
if (res != OPTIGA_LIB_SUCCESS) {
util_log("failed to open util application: %x", res);
return res;
@@ -1203,7 +902,7 @@ static int _factory_setup(void)
return res;
}
- res = _optiga_util_close_application_sync(_util, 0);
+ res = optiga_ops_util_close_application_sync(_util, 0);
if (res != OPTIGA_LIB_SUCCESS) {
return res;
}
@@ -1234,7 +933,7 @@ static int _verify_metadata(
uint16_t actual_metadata_len = sizeof(actual_metadata);
optiga_lib_status_t res =
- _optiga_util_read_metadata_sync(_util, oid, actual_metadata, &actual_metadata_len);
+ optiga_ops_util_read_metadata_sync(_util, oid, actual_metadata, &actual_metadata_len);
if (res != OPTIGA_LIB_SUCCESS) {
util_log("fail: read binding secret metadata: %x", res);
return res;
@@ -1281,13 +980,13 @@ static int _set_password(
goto cleanup;
}
- res = _optiga_util_write_data_sync(
+ res = optiga_ops_util_write_data_sync(
_util, OID_PASSWORD, OPTIGA_UTIL_ERASE_AND_WRITE, 0x00, data, data_len);
if (res != OPTIGA_UTIL_SUCCESS) {
goto cleanup;
}
- res = _optiga_util_write_data_sync(
+ res = optiga_ops_util_write_data_sync(
_util,
OID_COUNTER_PASSWORD,
OPTIGA_UTIL_ERASE_AND_WRITE,
@@ -1300,7 +999,7 @@ static int _set_password(
cleanup: {
optiga_lib_status_t res_clear =
- _optiga_crypt_clear_auto_state_sync(_crypt, OID_PASSWORD_SECRET);
+ optiga_ops_crypt_clear_auto_state_sync(_crypt, OID_PASSWORD_SECRET);
if (res != OPTIGA_UTIL_SUCCESS) {
return res;
}
@@ -1313,7 +1012,7 @@ int optiga_init_new_password(const char* password)
// Set new hmac key.
uint8_t new_hmac_key[32] = {0};
_ifs->random_32_bytes(new_hmac_key);
- optiga_lib_status_t res = _optiga_util_write_data_sync(
+ optiga_lib_status_t res = optiga_ops_util_write_data_sync(
_util, OID_HMAC, OPTIGA_UTIL_ERASE_AND_WRITE, 0x00, new_hmac_key, sizeof(new_hmac_key));
if (res != OPTIGA_UTIL_SUCCESS) {
util_log("failed updating the hmac key: %x", res);
@@ -1322,7 +1021,7 @@ int optiga_init_new_password(const char* password)
// Set new symmetric key.
optiga_key_id_t keyid = OPTIGA_KEY_ID_SECRET_BASED;
- res = _optiga_crypt_symmetric_generate_key_sync(
+ res = optiga_ops_crypt_symmetric_generate_key_sync(
_crypt, OPTIGA_SYMMETRIC_AES_256, OPTIGA_KEY_USAGE_ENCRYPTION, false, &keyid);
if (res != OPTIGA_UTIL_SUCCESS) {
util_log("failed updating the sym key: %x", res);
@@ -1332,7 +1031,7 @@ int optiga_init_new_password(const char* password)
uint8_t password_secret[32] = {0};
_ifs->random_32_bytes(password_secret);
- res = _optiga_util_write_data_sync(
+ res = optiga_ops_util_write_data_sync(
_util,
OID_PASSWORD_SECRET,
OPTIGA_UTIL_ERASE_AND_WRITE,
@@ -1392,7 +1091,7 @@ static int _optiga_verify_password(const char* password, uint8_t* password_secre
}
uint16_t password_secret_size = 32;
- res = _optiga_util_read_data_sync(
+ res = optiga_ops_util_read_data_sync(
_util, OID_PASSWORD_SECRET, 0, password_secret_out, &password_secret_size);
if (res != OPTIGA_LIB_SUCCESS) {
goto cleanup;
@@ -1407,7 +1106,7 @@ static int _optiga_verify_password(const char* password, uint8_t* password_secre
goto cleanup;
}
- res = _optiga_util_write_data_sync(
+ res = optiga_ops_util_write_data_sync(
_util,
OID_COUNTER_PASSWORD,
OPTIGA_UTIL_ERASE_AND_WRITE,
@@ -1419,9 +1118,9 @@ static int _optiga_verify_password(const char* password, uint8_t* password_secre
}
cleanup: {
- optiga_lib_status_t res_clear1 = _optiga_crypt_clear_auto_state_sync(_crypt, OID_PASSWORD);
+ optiga_lib_status_t res_clear1 = optiga_ops_crypt_clear_auto_state_sync(_crypt, OID_PASSWORD);
optiga_lib_status_t res_clear2 =
- _optiga_crypt_clear_auto_state_sync(_crypt, OID_PASSWORD_SECRET);
+ optiga_ops_crypt_clear_auto_state_sync(_crypt, OID_PASSWORD_SECRET);
if (res != OPTIGA_UTIL_SUCCESS) {
return res;
}
@@ -1578,14 +1277,9 @@ int optiga_setup(const securechip_interface_functions_t* ifs)
}
#endif
- _util = optiga_util_create(OPTIGA_INSTANCE_ID_0, _optiga_lib_callback, NULL);
- if (NULL == _util) {
- return SC_OPTIGA_ERR_CREATE;
- }
-
- _crypt = optiga_crypt_create(OPTIGA_INSTANCE_ID_0, _optiga_lib_callback, NULL);
- if (NULL == _crypt) {
- return SC_OPTIGA_ERR_CREATE;
+ res = optiga_ops_create(&_util, &_crypt);
+ if (res) {
+ return res;
}
OPTIGA_UTIL_SET_COMMS_PROTOCOL_VERSION(_util, OPTIGA_COMMS_PROTOCOL_VERSION_PRE_SHARED_SECRET);
@@ -1602,7 +1296,7 @@ int optiga_setup(const securechip_interface_functions_t* ifs)
return SC_ERR_CONFIG_MISMATCH;
}
- res = _optiga_util_open_application_sync(_util, 0);
+ res = optiga_ops_util_open_application_sync(_util, 0);
if (res) {
return res;
}
@@ -1628,7 +1322,7 @@ int optiga_kdf_external(const uint8_t* msg, size_t len, uint8_t* mac_out)
uint32_t mac_out_len = 32;
- res = _optiga_crypt_hmac_sync(
+ res = optiga_ops_crypt_hmac_sync(
_crypt, OPTIGA_HMAC_SHA_256, OID_HMAC, msg, len, mac_out, &mac_out_len);
if (res != OPTIGA_LIB_SUCCESS) {
util_log("kdf fail err=%x", res);
@@ -1651,7 +1345,7 @@ static int _kdf_internal(const uint8_t* msg, size_t len, uint8_t* kdf_out)
uint8_t mac_out[16] = {0};
uint32_t mac_out_len = sizeof(mac_out);
- res = _optiga_crypt_symmetric_encrypt_sync(
+ res = optiga_ops_crypt_symmetric_encrypt_sync(
_crypt,
OPTIGA_SYMMETRIC_CMAC,
OID_AES_SYMKEY,
@@ -1733,7 +1427,7 @@ bool optiga_gen_attestation_key(uint8_t* pubkey_out)
optiga_key_id_t slot = OPTIGA_KEY_ID_E0F1;
uint8_t pubkey_der[68] = {0};
uint16_t pubkey_der_size = sizeof(pubkey_der);
- optiga_lib_status_t res = _optiga_crypt_ecc_generate_keypair_sync(
+ optiga_lib_status_t res = optiga_ops_crypt_ecc_generate_keypair_sync(
_crypt,
OPTIGA_ECC_CURVE_NIST_P_256,
OPTIGA_KEY_USAGE_SIGN,
@@ -1759,7 +1453,7 @@ bool optiga_attestation_sign(const uint8_t* challenge, uint8_t* signature_out)
{
uint8_t sig_der[70] = {0};
uint16_t sig_der_size = sizeof(sig_der);
- optiga_lib_status_t res = _optiga_crypt_ecdsa_sign_sync(
+ optiga_lib_status_t res = optiga_ops_crypt_ecdsa_sign_sync(
_crypt, challenge, 32, OPTIGA_KEY_ID_E0F1, sig_der, &sig_der_size);
if (res != OPTIGA_CRYPT_SUCCESS) {
util_log("sign failed: %x", res);
@@ -1776,7 +1470,7 @@ bool optiga_monotonic_increments_remaining(uint32_t* remaining_out)
{
uint8_t buf[4] = {0};
uint16_t size = sizeof(buf);
- optiga_lib_status_t res = _optiga_util_read_data_sync(_util, OID_COUNTER, 0, buf, &size);
+ optiga_lib_status_t res = optiga_ops_util_read_data_sync(_util, OID_COUNTER, 0, buf, &size);
if (res != OPTIGA_LIB_SUCCESS) {
return false;
}
@@ -1792,7 +1486,8 @@ bool optiga_monotonic_increments_remaining(uint32_t* remaining_out)
// rand_out must be 32 bytes
bool optiga_random(uint8_t* rand_out)
{
- optiga_lib_status_t res = _optiga_crypt_random_sync(_crypt, OPTIGA_RNG_TYPE_TRNG, rand_out, 32);
+ optiga_lib_status_t res =
+ optiga_ops_crypt_random_sync(_crypt, OPTIGA_RNG_TYPE_TRNG, rand_out, 32);
if (res != OPTIGA_CRYPT_SUCCESS) {
util_log("optiga_random failed: %x", res);
return false;
diff --git a/src/optiga/optiga.h b/src/optiga/optiga.h
index 3a4900a..b446e8a 100644
--- a/src/optiga/optiga.h
+++ b/src/optiga/optiga.h
@@ -20,11 +20,53 @@
#include "compiler_util.h"
#include "securechip/securechip.h"
+#include <memory/bitbox02_smarteeprom.h>
#include <platform/platform_config.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
+// The Data Object IDs we use.
+
+// Stores a shared secret used for a shielded connection. Is is used to encrypt
+// communications. Read/write disabled.
+#define OID_PLATFORM_BINDING 0xE140
+// CMAC key slot, read/write prohibited. Used to perform KDF using CMAC. Key is regenerated at
+// factory setup and with each device reset. Monotonic counter `OID_COUNTER` attached.
+#define OID_AES_SYMKEY 0xE200
+// HMAC key slot, read prohibited, write allowed. 32 random bytes are written to it at factory setup
+// and with each device reset.
+#define OID_HMAC 0xF1D0
+// Arbitrary data object, stores up to 140 bytes. Used to store the U2F counter.
+#define OID_ARBITRARY_DATA 0xF1D1
+// ECC slot used for creating the device attestation key and signing with it. Read/write
+// disabled. The Key is internally generated at factory setup and used to sign the device
+// attestation host challenge.
+#define OID_ATTESTATION 0xE0F1
+// Monotonic counter, initialized at 0 and attached to `OID_AES_SYMKEY` - every CMAC generation
+// increments the counter. When the threshold `MONOTONIC_COUNTER_MAX_USE` is reached, further CMAC
+// computations return an error.
+#define OID_COUNTER 0xE120
+
+// The three objects below (`OID_PASSWORD_SECRET`, `OID_PASSWORD`, `OID_COUNTER_PASSWORD`) deal with
+// implementing the small monotonic counter that limits the number of unlocks to a small number.
+
+// A random shared key which authorizes updating `OID_PASSWORD` and `OID_COUNTER_PASSWORD`.
+// It is also part of the password stretching.
+#define OID_PASSWORD_SECRET 0xF1D2
+// A hmac digest of the device password.
+// Authorizes reading `OID_PASSWORD`. Monotonic counter `OID_COUNTER_PASSWORD` attached to
+// authorization, which limits the number of unlock attempts to a small number.
+#define OID_PASSWORD 0xF1D3
+// Monotonic counter with a small limit. Every password stretch increments the counter. A correct
+// password resets the counter. When the threshold `SMALL_MONOTONIC_COUNTER_MAX_USE` is reached,
+// further password stretches return an error.
+#define OID_COUNTER_PASSWORD 0xE121
+
+// Number of times the password can be entered incorrectly before further password stretching fails.
+// The counter is reset when the correct password is entered.
+#define SMALL_MONOTONIC_COUNTER_MAX_USE (MAX_UNLOCK_ATTEMPTS)
+
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(const char* password);
diff --git a/src/optiga/optiga_ops.c b/src/optiga/optiga_ops.c
new file mode 100644
index 0000000..0b8523f
--- /dev/null
+++ b/src/optiga/optiga_ops.c
@@ -0,0 +1,265 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#include "optiga_ops.h"
+
+#include <securechip/securechip.h>
+#include <util.h>
+
+// The OPTIGA library is asynchronous and will schedule a callback when the command is done. The
+// callback will set this shared variable to the result of the command.
+static volatile optiga_lib_status_t _optiga_lib_status;
+
+static void _optiga_lib_callback(void* callback_ctx, optiga_lib_status_t event)
+{
+ (void)callback_ctx;
+ _optiga_lib_status = event;
+}
+
+optiga_lib_status_t optiga_ops_create(optiga_util_t** util_out, optiga_crypt_t** crypt_out)
+{
+ *util_out = optiga_util_create(OPTIGA_INSTANCE_ID_0, _optiga_lib_callback, NULL);
+ if (NULL == *util_out) {
+ util_log("couldn't create optiga util");
+ return SC_OPTIGA_ERR_CREATE;
+ }
+
+ *crypt_out = optiga_crypt_create(OPTIGA_INSTANCE_ID_0, _optiga_lib_callback, NULL);
+ if (NULL == *crypt_out) {
+ util_log("couldn't create optiga crypt");
+ return SC_OPTIGA_ERR_CREATE;
+ }
+
+ return 0;
+}
+
+// Helper that is used in the main thread to busy wait for the callback to update the shared
+// variable.
+// It first checks the return status of the command, then busy waits, and then checks the
+// asynchronous return status.
+// Will return from caller if command failed.
+// `return_status` will be updated with the actual return status
+// Return statuses are documented in optiga_lib_return_codes.h
+#define _WAIT(return_status, optiga_lib_status) \
+ do { \
+ if ((return_status) != OPTIGA_UTIL_SUCCESS) { \
+ return (return_status); \
+ } \
+ while (OPTIGA_LIB_BUSY == (optiga_lib_status)) { \
+ } \
+ if (OPTIGA_LIB_SUCCESS != (optiga_lib_status)) { \
+ return (optiga_lib_status); \
+ } \
+ (return_status) = (optiga_lib_status); \
+ } while (0)
+
+optiga_lib_status_t optiga_ops_util_read_data_sync(
+ optiga_util_t* me,
+ uint16_t optiga_oid,
+ uint16_t offset,
+ uint8_t* buffer,
+ uint16_t* length)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res = optiga_util_read_data(me, optiga_oid, offset, buffer, length);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_util_write_data_sync(
+ optiga_util_t* me,
+ uint16_t optiga_oid,
+ uint8_t write_type,
+ uint16_t offset,
+ const uint8_t* buffer,
+ uint16_t length)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res =
+ optiga_util_write_data(me, optiga_oid, write_type, offset, buffer, length);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_util_read_metadata_sync(
+ optiga_util_t* me,
+ uint16_t optiga_oid,
+ uint8_t* buffer,
+ uint16_t* length)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res = optiga_util_read_metadata(me, optiga_oid, buffer, length);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_util_write_metadata_sync(
+ optiga_util_t* me,
+ uint16_t optiga_oid,
+ const uint8_t* buffer,
+ uint8_t length)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res = optiga_util_write_metadata(me, optiga_oid, buffer, length);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_util_open_application_sync(optiga_util_t* me, bool_t perform_restore)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res = optiga_util_open_application(me, perform_restore);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_util_close_application_sync(
+ optiga_util_t* me,
+ bool_t perform_hibernate)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res = optiga_util_close_application(me, perform_hibernate);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_crypt_hmac_sync(
+ optiga_crypt_t* me,
+ optiga_hmac_type_t type,
+ uint16_t secret,
+ const uint8_t* input_data,
+ uint32_t input_data_length,
+ uint8_t* mac,
+ uint32_t* mac_length)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res =
+ optiga_crypt_hmac(me, type, secret, input_data, input_data_length, mac, mac_length);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_crypt_ecc_generate_keypair_sync(
+ optiga_crypt_t* me,
+ optiga_ecc_curve_t curve_id,
+ uint8_t key_usage,
+ bool_t export_private_key,
+ void* private_key,
+ uint8_t* public_key,
+ uint16_t* public_key_length)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res = optiga_crypt_ecc_generate_keypair(
+ me, curve_id, key_usage, export_private_key, private_key, public_key, public_key_length);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_crypt_ecdsa_sign_sync(
+ optiga_crypt_t* me,
+ const uint8_t* digest,
+ uint8_t digest_length,
+ optiga_key_id_t private_key,
+ uint8_t* signature,
+ uint16_t* signature_length)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res = optiga_crypt_ecdsa_sign(
+ me, digest, digest_length, private_key, signature, signature_length);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_crypt_symmetric_encrypt_sync(
+ optiga_crypt_t* me,
+ optiga_symmetric_encryption_mode_t encryption_mode,
+ optiga_key_id_t symmetric_key_oid,
+ const uint8_t* plain_data,
+ uint32_t plain_data_length,
+ const uint8_t* iv,
+ uint16_t iv_length,
+ const uint8_t* associated_data,
+ uint16_t associated_data_length,
+ uint8_t* encrypted_data,
+ uint32_t* encrypted_data_length)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res = optiga_crypt_symmetric_encrypt(
+ me,
+ encryption_mode,
+ symmetric_key_oid,
+ plain_data,
+ plain_data_length,
+ iv,
+ iv_length,
+ associated_data,
+ associated_data_length,
+ encrypted_data,
+ encrypted_data_length);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_crypt_random_sync(
+ optiga_crypt_t* me,
+ optiga_rng_type_t rng_type,
+ uint8_t* random_data,
+ uint16_t random_data_length)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res = optiga_crypt_random(me, rng_type, random_data, random_data_length);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_crypt_symmetric_generate_key_sync(
+ optiga_crypt_t* me,
+ optiga_symmetric_key_type_t key_type,
+ uint8_t key_usage,
+ bool_t export_symmetric_key,
+ void* symmetric_key)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res = optiga_crypt_symmetric_generate_key(
+ me, key_type, key_usage, export_symmetric_key, symmetric_key);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_crypt_generate_auth_code_sync(
+ optiga_crypt_t* me,
+ optiga_rng_type_t rng_type,
+ const uint8_t* optional_data,
+ uint16_t optional_data_length,
+ uint8_t* random_data,
+ uint16_t random_data_length)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res = optiga_crypt_generate_auth_code(
+ me, rng_type, optional_data, optional_data_length, random_data, random_data_length);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_crypt_clear_auto_state_sync(optiga_crypt_t* me, uint16_t secret)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res = optiga_crypt_clear_auto_state(me, secret);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
+
+optiga_lib_status_t optiga_ops_crypt_hmac_verify_sync(
+ optiga_crypt_t* me,
+ optiga_hmac_type_t type,
+ uint16_t secret,
+ const uint8_t* input_data,
+ uint32_t input_data_length,
+ const uint8_t* hmac,
+ uint32_t hmac_length)
+{
+ _optiga_lib_status = OPTIGA_LIB_BUSY;
+ optiga_lib_status_t res = optiga_crypt_hmac_verify(
+ me, type, secret, input_data, input_data_length, hmac, hmac_length);
+ _WAIT(res, _optiga_lib_status);
+ return res;
+}
diff --git a/src/optiga/optiga_ops.h b/src/optiga/optiga_ops.h
new file mode 100644
index 0000000..48aaf37
--- /dev/null
+++ b/src/optiga/optiga_ops.h
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#ifndef _OPTIGA_OPS_H_
+#define _OPTIGA_OPS_H_
+
+#include <optiga_crypt.h>
+#include <optiga_util.h>
+#include <stdint.h>
+
+optiga_lib_status_t optiga_ops_create(optiga_util_t** util_out, optiga_crypt_t** crypt_out);
+
+optiga_lib_status_t optiga_ops_util_read_data_sync(
+ optiga_util_t* me,
+ uint16_t optiga_oid,
+ uint16_t offset,
+ uint8_t* buffer,
+ uint16_t* length);
+
+optiga_lib_status_t optiga_ops_util_write_data_sync(
+ optiga_util_t* me,
+ uint16_t optiga_oid,
+ uint8_t write_type,
+ uint16_t offset,
+ const uint8_t* buffer,
+ uint16_t length);
+
+optiga_lib_status_t optiga_ops_util_read_metadata_sync(
+ optiga_util_t* me,
+ uint16_t optiga_oid,
+ uint8_t* buffer,
+ uint16_t* length);
+
+optiga_lib_status_t optiga_ops_util_write_metadata_sync(
+ optiga_util_t* me,
+ uint16_t optiga_oid,
+ const uint8_t* buffer,
+ uint8_t length);
+
+optiga_lib_status_t optiga_ops_util_open_application_sync(
+ optiga_util_t* me,
+ bool_t perform_restore);
+
+optiga_lib_status_t optiga_ops_util_close_application_sync(
+ optiga_util_t* me,
+ bool_t perform_hibernate);
+
+optiga_lib_status_t optiga_ops_crypt_hmac_sync(
+ optiga_crypt_t* me,
+ optiga_hmac_type_t type,
+ uint16_t secret,
+ const uint8_t* input_data,
+ uint32_t input_data_length,
+ uint8_t* mac,
+ uint32_t* mac_length);
+
+optiga_lib_status_t optiga_ops_crypt_ecc_generate_keypair_sync(
+ optiga_crypt_t* me,
+ optiga_ecc_curve_t curve_id,
+ uint8_t key_usage,
+ bool_t export_private_key,
+ void* private_key,
+ uint8_t* public_key,
+ uint16_t* public_key_length);
+
+optiga_lib_status_t optiga_ops_crypt_ecdsa_sign_sync(
+ optiga_crypt_t* me,
+ const uint8_t* digest,
+ uint8_t digest_length,
+ optiga_key_id_t private_key,
+ uint8_t* signature,
+ uint16_t* signature_length);
+
+optiga_lib_status_t optiga_ops_crypt_symmetric_encrypt_sync(
+ optiga_crypt_t* me,
+ optiga_symmetric_encryption_mode_t encryption_mode,
+ optiga_key_id_t symmetric_key_oid,
+ const uint8_t* plain_data,
+ uint32_t plain_data_length,
+ const uint8_t* iv,
+ uint16_t iv_length,
+ const uint8_t* associated_data,
+ uint16_t associated_data_length,
+ uint8_t* encrypted_data,
+ uint32_t* encrypted_data_length);
+
+optiga_lib_status_t optiga_ops_crypt_random_sync(
+ optiga_crypt_t* me,
+ optiga_rng_type_t rng_type,
+ uint8_t* random_data,
+ uint16_t random_data_length);
+
+optiga_lib_status_t optiga_ops_crypt_symmetric_generate_key_sync(
+ optiga_crypt_t* me,
+ optiga_symmetric_key_type_t key_type,
+ uint8_t key_usage,
+ bool_t export_symmetric_key,
+ void* symmetric_key);
+
+optiga_lib_status_t optiga_ops_crypt_generate_auth_code_sync(
+ optiga_crypt_t* me,
+ optiga_rng_type_t rng_type,
+ const uint8_t* optional_data,
+ uint16_t optional_data_length,
+ uint8_t* random_data,
+ uint16_t random_data_length);
+
+optiga_lib_status_t optiga_ops_crypt_clear_auto_state_sync(optiga_crypt_t* me, uint16_t secret);
+
+optiga_lib_status_t optiga_ops_crypt_hmac_verify_sync(
+ optiga_crypt_t* me,
+ optiga_hmac_type_t type,
+ uint16_t secret,
+ const uint8_t* input_data,
+ uint32_t input_data_length,
+ const uint8_t* hmac,
+ uint32_t hmac_length);
+
+#endif // _OPTIGA_OPS_H_
diff --git a/test/unit-test/CMakeLists.txt b/test/unit-test/CMakeLists.txt
index 463dea3..7fb3999 100644
--- a/test/unit-test/CMakeLists.txt
+++ b/test/unit-test/CMakeLists.txt
@@ -47,6 +47,8 @@ else()
""
random
"-Wl,--wrap=rand,--wrap=rust_sha256"
+ optiga
+ "-Wl,--wrap=salt_hash_data"
ui_components
""
ui_util
@@ -84,6 +86,26 @@ else()
target_include_directories(${EXE} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)
+ if(TEST_NAME STREQUAL "optiga")
+ target_sources(${EXE} PRIVATE
+ ${CMAKE_SOURCE_DIR}/src/optiga/optiga.c
+ )
+ # Needed by optiga-trust-m headers used by src/optiga/optiga.c.
+ target_include_directories(${EXE} SYSTEM PRIVATE
+ ${CMAKE_SOURCE_DIR}/external
+ ${CMAKE_SOURCE_DIR}/external/optiga-trust-m/config
+ ${CMAKE_SOURCE_DIR}/external/optiga-trust-m/include
+ ${CMAKE_SOURCE_DIR}/external/optiga-trust-m/include/cmd
+ ${CMAKE_SOURCE_DIR}/external/optiga-trust-m/include/common
+ ${CMAKE_SOURCE_DIR}/external/optiga-trust-m/include/ifx_i2c
+ ${CMAKE_SOURCE_DIR}/external/optiga-trust-m/include/pal
+ ${CMAKE_SOURCE_DIR}/external/optiga-trust-m/include/comms
+ ${CMAKE_SOURCE_DIR}/external/optiga-trust-m/external/mbedtls/include
+ )
+ # Optiga config must be defined both when compiling the optiga lib, and also when compiling
+ # our sources.
+ target_compile_definitions(${EXE} PRIVATE OPTIGA_LIB_EXTERNAL="optiga_config.h")
+ endif()
add_test(NAME test_${TEST_NAME} COMMAND ${EXE})
endforeach()
endif()
diff --git a/test/unit-test/test_optiga.c b/test/unit-test/test_optiga.c
new file mode 100644
index 0000000..6932cb2
--- /dev/null
+++ b/test/unit-test/test_optiga.c
@@ -0,0 +1,650 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <cmocka.h>
+
+#include <optiga/optiga.h>
+#include <optiga/optiga_ops.h>
+
+#include <common/optiga_lib_return_codes.h>
+#include <optiga_crypt.h>
+#include <optiga_util.h>
+#include <pal/pal_os_timer.h>
+#include <rust/rust.h>
+#include <salt.h>
+
+#include <stdint.h>
+#include <string.h>
+
+//------------------------------------------------------------------------------
+// Fixed test vectors / keys (deterministic fakes).
+
+static const uint8_t _salt_root_fixed[32] = {
+ 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
+ 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
+};
+
+static const uint8_t _password_secret_fixed[32] = {
+ 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
+ 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
+};
+
+static const uint8_t _kdf_cmac_key_fixed[32] = {
+ 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
+ 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
+};
+
+static const uint8_t _kdf_hmac_key_fixed[32] = {
+ 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0,
+ 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0,
+};
+
+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,
+ 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.
+//
+// Repro script (mirrors optiga_stretch_password() with the unit test fakes):
+// ```python
+// import hashlib, hmac
+//
+// def sha256(b: bytes) -> bytes:
+// return hashlib.sha256(b).digest()
+//
+// def hmac_sha256(key: bytes, msg: bytes) -> bytes:
+// return hmac.new(key, msg, hashlib.sha256).digest()
+//
+// def salt_hash_data(data: bytes, purpose: bytes, salt_root: bytes) -> bytes:
+// return sha256(salt_root + purpose + data)
+//
+// def kdf_internal(msg: bytes, cmac_key: bytes) -> bytes:
+// # optiga_ops_crypt_symmetric_encrypt_sync fake: HMAC-SHA256(cmac_key, msg)[:16]
+// return sha256(hmac_sha256(cmac_key, msg)[:16])
+//
+// def kdf_hmac(msg: bytes, hmac_key: bytes) -> bytes:
+// # optiga_ops_crypt_hmac_sync fake: HMAC-SHA256(hmac_key, msg)
+// return hmac_sha256(hmac_key, msg)
+//
+// salt_root = bytes([0x42]) * 32
+// cmac_key = bytes([0xA0]) * 32
+// hmac_key = bytes([0xB0]) * 32
+// password_secret = bytes([0x99]) * 32
+// password = b"pw"
+//
+// kdf_in = salt_hash_data(password, b"optiga_password_stretch_in", salt_root)
+// stretched = kdf_internal(kdf_in, cmac_key)
+// for _ in range(2):
+// stretched = kdf_hmac(stretched, hmac_key)
+// stretched = hmac_sha256(password_secret, stretched)
+// out_salt = salt_hash_data(password, b"optiga_password_stretch_out", salt_root)
+// stretched = hmac_sha256(out_salt, stretched)
+// print(stretched.hex())
+// ```
+static const uint8_t _expected_stretched_out[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,
+};
+
+//------------------------------------------------------------------------------
+// Minimal securechip interface fakes.
+
+static void _dummy_get_key(uint8_t* key_out)
+{
+ memset(key_out, 0, 32);
+}
+
+static void _fixed_random_32_bytes(uint8_t* buf)
+{
+ memcpy(buf, _password_secret_fixed, 32);
+}
+
+static const securechip_interface_functions_t _ifs = {
+ .get_auth_key = _dummy_get_key,
+ .get_io_protection_key = _dummy_get_key,
+ .get_encryption_key = _dummy_get_key,
+ .random_32_bytes = _fixed_random_32_bytes,
+};
+
+//------------------------------------------------------------------------------
+// Linker-wrapped salt_hash_data: same as salt.rs, but with a fixed salt_root.
+
+bool __wrap_salt_hash_data(
+ const uint8_t* data,
+ size_t data_len,
+ const char* purpose,
+ uint8_t* hash_out)
+{
+ if ((data_len > 0 && data == NULL) || purpose == NULL || hash_out == NULL) {
+ return false;
+ }
+
+ void* ctx = rust_sha256_new();
+ if (ctx == NULL) {
+ return false;
+ }
+ rust_sha256_update(ctx, _salt_root_fixed, sizeof(_salt_root_fixed));
+ rust_sha256_update(ctx, purpose, strlen(purpose));
+ rust_sha256_update(ctx, data, data_len);
+ rust_sha256_finish(&ctx, hash_out);
+ return true;
+}
+
+//------------------------------------------------------------------------------
+// Minimal OPTIGA + PAL stubs required by optiga.c during setup.
+
+static optiga_util_t _fake_util;
+static optiga_crypt_t _fake_crypt;
+
+static uint8_t _oid_password[32];
+static bool _oid_password_set;
+
+static uint8_t _oid_counter_password_buf[8];
+
+static bool _authorized_password;
+static bool _authorized_password_secret;
+
+static void _reset_fakes(void)
+{
+ memset(_oid_password, 0, sizeof(_oid_password));
+ _oid_password_set = false;
+ _authorized_password = false;
+ _authorized_password_secret = false;
+}
+
+static uint32_t _get_counter(uint16_t oid)
+{
+ switch (oid) {
+ case OID_COUNTER_PASSWORD:
+ return optiga_common_get_uint32(&_oid_counter_password_buf[0]);
+ default:
+ fail();
+ return 0;
+ }
+}
+
+static uint32_t _get_threshold(uint16_t oid)
+{
+ switch (oid) {
+ case OID_COUNTER_PASSWORD:
+ return optiga_common_get_uint32(&_oid_counter_password_buf[4]);
+ default:
+ fail();
+ return 0;
+ }
+}
+
+pal_status_t pal_timer_init(void)
+{
+ return PAL_STATUS_SUCCESS;
+}
+
+uint32_t optiga_common_get_uint32(const uint8_t* p_input_buffer)
+{
+ return ((uint32_t)p_input_buffer[0] << 24) | ((uint32_t)p_input_buffer[1] << 16) |
+ ((uint32_t)p_input_buffer[2] << 8) | (uint32_t)p_input_buffer[3];
+}
+
+void optiga_common_set_uint32(uint8_t* p_output_buffer, uint32_t four_byte_value)
+{
+ p_output_buffer[0] = (uint8_t)(four_byte_value >> 24);
+ p_output_buffer[1] = (uint8_t)(four_byte_value >> 16);
+ p_output_buffer[2] = (uint8_t)(four_byte_value >> 8);
+ p_output_buffer[3] = (uint8_t)(four_byte_value);
+}
+
+void optiga_util_set_comms_params(optiga_util_t* me, uint8_t parameter_type, uint8_t value)
+{
+#ifdef OPTIGA_COMMS_SHIELDED_CONNECTION
+ if (parameter_type == OPTIGA_COMMS_PROTECTION_LEVEL) {
+ me->protection_level = value;
+ } else if (parameter_type == OPTIGA_COMMS_PROTOCOL_VERSION) {
+ me->protocol_version = value;
+ }
+#else
+ (void)me;
+ (void)parameter_type;
+ (void)value;
+#endif
+}
+
+void optiga_crypt_set_comms_params(optiga_crypt_t* me, uint8_t parameter_type, uint8_t value)
+{
+#ifdef OPTIGA_COMMS_SHIELDED_CONNECTION
+ if (parameter_type == OPTIGA_COMMS_PROTECTION_LEVEL) {
+ me->protection_level = value;
+ } else if (parameter_type == OPTIGA_COMMS_PROTOCOL_VERSION) {
+ me->protocol_version = value;
+ }
+#else
+ (void)me;
+ (void)parameter_type;
+ (void)value;
+#endif
+}
+
+//------------------------------------------------------------------------------
+// Fake optiga_ops API surface (unit-test seam).
+
+optiga_lib_status_t optiga_ops_create(optiga_util_t** util_out, optiga_crypt_t** crypt_out)
+{
+ // Handler/callback not currently needed in tests, so it is set to NULL.
+
+ memset(&_fake_util, 0, sizeof(_fake_util));
+ _fake_util.caller_context = OPTIGA_INSTANCE_ID_0;
+ _fake_util.handler = NULL;
+ _fake_util.instance_state = 0;
+#ifdef OPTIGA_COMMS_SHIELDED_CONNECTION
+ _fake_util.protection_level = OPTIGA_COMMS_FULL_PROTECTION;
+ _fake_util.protocol_version = OPTIGA_COMMS_PROTOCOL_VERSION_PRE_SHARED_SECRET;
+#endif
+ *util_out = &_fake_util;
+
+ memset(&_fake_crypt, 0, sizeof(_fake_crypt));
+ _fake_crypt.my_cmd = NULL;
+ _fake_crypt.caller_context = OPTIGA_INSTANCE_ID_0;
+ _fake_crypt.handler = NULL;
+ _fake_crypt.instance_state = 0;
+#ifdef OPTIGA_COMMS_SHIELDED_CONNECTION
+ _fake_crypt.protection_level = OPTIGA_COMMS_FULL_PROTECTION;
+ _fake_crypt.protocol_version = OPTIGA_COMMS_PROTOCOL_VERSION_PRE_SHARED_SECRET;
+#endif
+ *crypt_out = &_fake_crypt;
+
+ return 0;
+}
+
+optiga_lib_status_t optiga_ops_util_open_application_sync(optiga_util_t* me, bool_t perform_restore)
+{
+ (void)me;
+ (void)perform_restore;
+ return OPTIGA_LIB_SUCCESS;
+}
+
+optiga_lib_status_t optiga_ops_util_close_application_sync(
+ optiga_util_t* me,
+ bool_t perform_hibernate)
+{
+ (void)me;
+ (void)perform_hibernate;
+ return OPTIGA_UTIL_ERROR;
+}
+
+optiga_lib_status_t optiga_ops_util_read_metadata_sync(
+ optiga_util_t* me,
+ uint16_t optiga_oid,
+ uint8_t* buffer,
+ uint16_t* length)
+{
+ (void)me;
+ (void)optiga_oid;
+ (void)buffer;
+ (void)length;
+ return OPTIGA_UTIL_ERROR;
+}
+
+optiga_lib_status_t optiga_ops_util_write_metadata_sync(
+ optiga_util_t* me,
+ uint16_t optiga_oid,
+ const uint8_t* buffer,
+ uint8_t length)
+{
+ (void)me;
+ (void)optiga_oid;
+ (void)buffer;
+ (void)length;
+ return OPTIGA_UTIL_ERROR;
+}
+
+optiga_lib_status_t optiga_ops_util_read_data_sync(
+ optiga_util_t* me,
+ uint16_t optiga_oid,
+ uint16_t offset,
+ uint8_t* buffer,
+ uint16_t* length)
+{
+ (void)me;
+ (void)offset;
+
+ if (optiga_oid == OID_PASSWORD_SECRET) {
+ if (!_authorized_password) {
+ return OPTIGA_UTIL_ERROR;
+ }
+ if (*length < 32) {
+ return OPTIGA_UTIL_ERROR_MEMORY_INSUFFICIENT;
+ }
+ memcpy(buffer, _password_secret_fixed, 32);
+ *length = 32;
+ return OPTIGA_UTIL_SUCCESS;
+ }
+ if (optiga_oid == OID_COUNTER) {
+ if (*length < 4) {
+ return OPTIGA_UTIL_ERROR_MEMORY_INSUFFICIENT;
+ }
+ memset(buffer, 0, 4);
+ *length = 4;
+ return OPTIGA_UTIL_SUCCESS;
+ }
+ if (optiga_oid == OID_ARBITRARY_DATA) {
+ memset(buffer, 0, *length);
+ return OPTIGA_UTIL_SUCCESS;
+ }
+
+ return OPTIGA_UTIL_ERROR_INVALID_INPUT;
+}
+
+optiga_lib_status_t optiga_ops_util_write_data_sync(
+ optiga_util_t* me,
+ uint16_t optiga_oid,
+ uint8_t write_type,
+ uint16_t offset,
+ const uint8_t* buffer,
+ uint16_t length)
+{
+ (void)me;
+ (void)write_type;
+ (void)offset;
+
+ if (optiga_oid == OID_PASSWORD) {
+ if (!_authorized_password_secret) {
+ return OPTIGA_UTIL_ERROR;
+ }
+ if (length != 32) {
+ return OPTIGA_UTIL_ERROR_INVALID_INPUT;
+ }
+ memcpy(_oid_password, buffer, 32);
+ _oid_password_set = true;
+ return OPTIGA_UTIL_SUCCESS;
+ }
+ if (optiga_oid == OID_COUNTER_PASSWORD) {
+ if (length != sizeof(_oid_counter_password_buf)) {
+ return OPTIGA_UTIL_ERROR_INVALID_INPUT;
+ }
+ memcpy(_oid_counter_password_buf, buffer, sizeof(_oid_counter_password_buf));
+ return OPTIGA_UTIL_SUCCESS;
+ }
+ // Accept other writes without emulating full semantics (counter reset, hmac key, etc.).
+ (void)buffer;
+ (void)length;
+ return OPTIGA_UTIL_SUCCESS;
+}
+
+optiga_lib_status_t optiga_ops_crypt_generate_auth_code_sync(
+ optiga_crypt_t* me,
+ optiga_rng_type_t rng_type,
+ const uint8_t* optional_data,
+ uint16_t optional_data_length,
+ uint8_t* random_data,
+ uint16_t random_data_length)
+{
+ (void)me;
+ (void)rng_type;
+ (void)optional_data;
+ (void)optional_data_length;
+ if (random_data_length != sizeof(_auth_code_random_fixed)) {
+ return OPTIGA_CRYPT_ERROR_INVALID_INPUT;
+ }
+ memcpy(random_data, _auth_code_random_fixed, sizeof(_auth_code_random_fixed));
+ return OPTIGA_CRYPT_SUCCESS;
+}
+
+optiga_lib_status_t optiga_ops_crypt_hmac_verify_sync(
+ optiga_crypt_t* me,
+ optiga_hmac_type_t type,
+ uint16_t secret,
+ const uint8_t* input_data,
+ uint32_t input_data_length,
+ const uint8_t* hmac,
+ uint32_t hmac_length)
+{
+ (void)me;
+ (void)type;
+
+ if (hmac_length != 32 || input_data_length != 32) {
+ return OPTIGA_CRYPT_ERROR_INVALID_INPUT;
+ }
+
+ const uint8_t* key = NULL;
+ uintptr_t key_len = 0;
+ if (secret == OID_PASSWORD_SECRET) {
+ key = _password_secret_fixed;
+ key_len = sizeof(_password_secret_fixed);
+ } else if (secret == OID_PASSWORD) {
+ // Emulate the small monotonic counter that is attached to password authorization.
+ // Stored as {counter_be_u32, threshold_be_u32}.
+ uint32_t counter = _get_counter(OID_COUNTER_PASSWORD);
+ uint32_t threshold = _get_threshold(OID_COUNTER_PASSWORD);
+ if (counter >= threshold) {
+ return 0x802F;
+ }
+ counter++;
+ optiga_common_set_uint32(&_oid_counter_password_buf[0], counter);
+
+ if (!_oid_password_set) {
+ return OPTIGA_CRYPT_ERROR;
+ }
+ key = _oid_password;
+ key_len = sizeof(_oid_password);
+ } else {
+ return OPTIGA_CRYPT_ERROR_INVALID_INPUT;
+ }
+
+ uint8_t computed[32] = {0};
+ rust_hmac_sha256(key, key_len, input_data, input_data_length, computed);
+ if (memcmp(computed, hmac, 32) != 0) {
+ return 0x802F;
+ }
+
+ if (secret == OID_PASSWORD) {
+ _authorized_password = true;
+ } else if (secret == OID_PASSWORD_SECRET) {
+ _authorized_password_secret = true;
+ }
+ return OPTIGA_CRYPT_SUCCESS;
+}
+
+optiga_lib_status_t optiga_ops_crypt_clear_auto_state_sync(optiga_crypt_t* me, uint16_t secret)
+{
+ (void)me;
+ if (secret == OID_PASSWORD) {
+ _authorized_password = false;
+ } else if (secret == OID_PASSWORD_SECRET) {
+ _authorized_password_secret = false;
+ }
+ return OPTIGA_CRYPT_SUCCESS;
+}
+
+optiga_lib_status_t optiga_ops_crypt_symmetric_generate_key_sync(
+ optiga_crypt_t* me,
+ optiga_symmetric_key_type_t key_type,
+ uint8_t key_usage,
+ bool_t export_symmetric_key,
+ void* symmetric_key)
+{
+ (void)me;
+ (void)key_type;
+ (void)key_usage;
+ (void)export_symmetric_key;
+ (void)symmetric_key;
+ return OPTIGA_CRYPT_ERROR;
+}
+
+optiga_lib_status_t optiga_ops_crypt_random_sync(
+ optiga_crypt_t* me,
+ optiga_rng_type_t rng_type,
+ uint8_t* random_data,
+ uint16_t random_data_length)
+{
+ (void)me;
+ (void)rng_type;
+ (void)random_data;
+ (void)random_data_length;
+ return OPTIGA_CRYPT_ERROR;
+}
+
+// Use rust_hmac_sha256 with a fixed key and msg as the value, truncated to 16 bytes.
+optiga_lib_status_t optiga_ops_crypt_symmetric_encrypt_sync(
+ optiga_crypt_t* me,
+ optiga_symmetric_encryption_mode_t encryption_mode,
+ optiga_key_id_t symmetric_key_oid,
+ const uint8_t* plain_data,
+ uint32_t plain_data_length,
+ const uint8_t* iv,
+ uint16_t iv_length,
+ const uint8_t* associated_data,
+ uint16_t associated_data_length,
+ uint8_t* encrypted_data,
+ uint32_t* encrypted_data_length)
+{
+ (void)me;
+ (void)encryption_mode;
+ assert_int_equal(symmetric_key_oid, OID_AES_SYMKEY);
+ (void)iv;
+ (void)iv_length;
+ (void)associated_data;
+ (void)associated_data_length;
+
+ uint8_t out[32] = {0};
+ rust_hmac_sha256(
+ _kdf_cmac_key_fixed, sizeof(_kdf_cmac_key_fixed), plain_data, plain_data_length, out);
+
+ if (*encrypted_data_length < 16) {
+ return OPTIGA_CRYPT_ERROR_MEMORY_INSUFFICIENT;
+ }
+ memcpy(encrypted_data, out, 16);
+ *encrypted_data_length = 16;
+ return OPTIGA_CRYPT_SUCCESS;
+}
+
+// Use rust_hmac_sha256 with a different fixed key and msg as the value.
+optiga_lib_status_t optiga_ops_crypt_hmac_sync(
+ optiga_crypt_t* me,
+ optiga_hmac_type_t type,
+ uint16_t secret,
+ const uint8_t* input_data,
+ uint32_t input_data_length,
+ uint8_t* mac,
+ uint32_t* mac_length)
+{
+ (void)me;
+ (void)type;
+ assert_int_equal(secret, OID_HMAC);
+ if (*mac_length < 32) {
+ return OPTIGA_CRYPT_ERROR_MEMORY_INSUFFICIENT;
+ }
+ rust_hmac_sha256(
+ _kdf_hmac_key_fixed, sizeof(_kdf_hmac_key_fixed), input_data, input_data_length, mac);
+ *mac_length = 32;
+ return OPTIGA_CRYPT_SUCCESS;
+}
+
+// Unused by these tests, but required to satisfy optiga.c link dependencies.
+optiga_lib_status_t optiga_ops_crypt_ecc_generate_keypair_sync(
+ optiga_crypt_t* me,
+ optiga_ecc_curve_t curve_id,
+ uint8_t key_usage,
+ bool_t export_private_key,
+ void* private_key,
+ uint8_t* public_key,
+ uint16_t* public_key_length)
+{
+ (void)me;
+ (void)curve_id;
+ (void)key_usage;
+ (void)export_private_key;
+ (void)private_key;
+ (void)public_key;
+ (void)public_key_length;
+ return OPTIGA_CRYPT_ERROR;
+}
+
+optiga_lib_status_t optiga_ops_crypt_ecdsa_sign_sync(
+ optiga_crypt_t* me,
+ const uint8_t* digest,
+ uint8_t digest_length,
+ optiga_key_id_t private_key,
+ uint8_t* signature,
+ uint16_t* signature_length)
+{
+ (void)me;
+ (void)digest;
+ (void)digest_length;
+ (void)private_key;
+ (void)signature;
+ (void)signature_length;
+ return OPTIGA_CRYPT_ERROR;
+}
+
+//------------------------------------------------------------------------------
+// Tests
+
+static void test_optiga_stretch_password_success(void** state)
+{
+ (void)state;
+ _reset_fakes();
+ assert_int_equal(optiga_setup(&_ifs), 0);
+
+ // 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));
+ _oid_password_set = true;
+ const uint8_t counter_reset_buf[8] = {0, 0, 0, 0, 0, 0, 0, SMALL_MONOTONIC_COUNTER_MAX_USE};
+ memcpy(_oid_counter_password_buf, counter_reset_buf, sizeof(_oid_counter_password_buf));
+
+ uint8_t stretched_out[32] = {0};
+ assert_int_equal(optiga_stretch_password("pw", stretched_out), 0);
+ assert_memory_equal(stretched_out, _expected_stretched_out, sizeof(_expected_stretched_out));
+ // 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)
+{
+ (void)state;
+ _reset_fakes();
+ assert_int_equal(optiga_setup(&_ifs), 0);
+
+ // 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));
+ _oid_password_set = true;
+ const uint8_t counter_reset_buf[8] = {0, 0, 0, 0, 0, 0, 0, SMALL_MONOTONIC_COUNTER_MAX_USE};
+ memcpy(_oid_counter_password_buf, counter_reset_buf, sizeof(_oid_counter_password_buf));
+
+ uint8_t stretched_out[32] = {0};
+
+ assert_int_equal(optiga_stretch_password("wrong", stretched_out), SC_ERR_INCORRECT_PASSWORD);
+ assert_int_equal(_get_counter(OID_COUNTER_PASSWORD), 1);
+ assert_int_equal(_get_threshold(OID_COUNTER_PASSWORD), SMALL_MONOTONIC_COUNTER_MAX_USE);
+
+ assert_int_equal(optiga_stretch_password("wrong", stretched_out), SC_ERR_INCORRECT_PASSWORD);
+ assert_int_equal(_get_counter(OID_COUNTER_PASSWORD), 2);
+ assert_int_equal(_get_threshold(OID_COUNTER_PASSWORD), SMALL_MONOTONIC_COUNTER_MAX_USE);
+
+ assert_int_equal(optiga_stretch_password("pw", stretched_out), 0);
+ assert_int_equal(_get_counter(OID_COUNTER_PASSWORD), 0);
+ assert_int_equal(_get_threshold(OID_COUNTER_PASSWORD), SMALL_MONOTONIC_COUNTER_MAX_USE);
+
+ for (int i = 0; i < SMALL_MONOTONIC_COUNTER_MAX_USE; i++) {
+ assert_int_equal(
+ optiga_stretch_password("wrong", stretched_out), SC_ERR_INCORRECT_PASSWORD);
+ }
+ assert_int_equal(
+ optiga_common_get_uint32(&_oid_counter_password_buf[0]), SMALL_MONOTONIC_COUNTER_MAX_USE);
+
+ // After exhausting all allowed attempts, a correct password fails as well.
+ assert_int_equal(optiga_stretch_password("pw", stretched_out), SC_ERR_INCORRECT_PASSWORD);
+ assert_int_equal(
+ optiga_common_get_uint32(&_oid_counter_password_buf[0]), SMALL_MONOTONIC_COUNTER_MAX_USE);
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_optiga_stretch_password_success),
+ cmocka_unit_test(test_optiga_stretch_password_attempt_counter),
+ };
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
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.