Skip to content

Commit 2287b6b

Browse files
committed
optiga: move config sanity check to factorysetup only
It causes a long delay before the orientation animation. It's a sanity check only, so doing it as part of the factory setup only suffices. We also enable it in debug builds, so it catches a misconfigured device (e.g. if you forgot to run factorysetup on a new PCB).
1 parent fb27fe0 commit 2287b6b

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

src/optiga/optiga.c

+32-14
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@
3535
// Must be 0 for the production firmware releases.
3636
#define FACTORY_DURING_PROD 0
3737

38+
// When to do a sanity check of the expected metadata configuration. The check takes a while and
39+
// causes a noticable delay at boot, so we don't enable it for production firmwares. We also enable
40+
// it in debug builds.
41+
#if FACTORYSETUP == 1 || FACTORY_DURING_PROD == 1 || !defined(NDEBUG)
42+
#define VERIFY_METADATA 1
43+
#else
44+
#define VERIFY_METADATA 0
45+
#endif
46+
3847
// Number of times the first kdf slot can be used over the lifetime of the device.
3948
// The maxmimum does not seem to be specified, so we use something a little below the endurance
4049
// indication of 600000 updates. See Solution Reference Manual Figure 32.
@@ -122,6 +131,7 @@ static const securechip_interface_functions_t* _ifs = NULL;
122131
// During development, set this to `LCSO_STATE_CREATION`.
123132
#define FINAL_LCSO_STATE LCSO_STATE_OPERATIONAL
124133

134+
#if FACTORYSETUP == 1 || FACTORY_DURING_PROD == 1 || VERIFY_METADATA == 1
125135
static const uint8_t _platform_binding_metadata[] = {
126136
// Metadata tag in the data object
127137
0x20,
@@ -411,18 +421,6 @@ static const uint8_t _password_metadata[] = {
411421
0x31,
412422
};
413423

414-
#if SMALL_MONOTONIC_COUNTER_MAX_USE > 255
415-
#error Max unlock attempts does not fit in one byte
416-
#endif
417-
// The intial/reset buffer for the small monotonic counter. Initial value: 0, threshold:
418-
// `SMALL_MONOTONIC_COUNTER_MAX_USE`. Table "Common data structures" -> "Counter":
419-
// 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
420-
// Bytes 0-3 are the initial counter value, set to 0.
421-
// Bytes 4-7 are the threshold.
422-
// Ints are encoded as uint32 big endian.
423-
static const uint8_t _counter_password_reset_buf[8] =
424-
{0, 0, 0, 0, 0, 0, 0, SMALL_MONOTONIC_COUNTER_MAX_USE};
425-
426424
static const uint8_t _counter_password_metadata[] = {
427425
// Metadata tag in the data object
428426
0x20,
@@ -455,6 +453,20 @@ static const uint8_t _counter_password_metadata[] = {
455453
0x00,
456454
};
457455

456+
#endif
457+
458+
#if SMALL_MONOTONIC_COUNTER_MAX_USE > 255
459+
#error Max unlock attempts does not fit in one byte
460+
#endif
461+
// The intial/reset buffer for the small monotonic counter. Initial value: 0, threshold:
462+
// `SMALL_MONOTONIC_COUNTER_MAX_USE`. Table "Common data structures" -> "Counter":
463+
// 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
464+
// Bytes 0-3 are the initial counter value, set to 0.
465+
// Bytes 4-7 are the threshold.
466+
// Ints are encoded as uint32 big endian.
467+
static const uint8_t _counter_password_reset_buf[8] =
468+
{0, 0, 0, 0, 0, 0, 0, SMALL_MONOTONIC_COUNTER_MAX_USE};
469+
458470
//
459471
// Sync wrappers around optiga util/crypt functions
460472
//
@@ -517,6 +529,7 @@ static optiga_lib_status_t _optiga_util_write_data_sync(
517529
return res;
518530
}
519531

532+
#if FACTORYSETUP == 1 || FACTORY_DURING_PROD == 1 || VERIFY_METADATA == 1
520533
static optiga_lib_status_t _optiga_util_read_metadata_sync(
521534
optiga_util_t* me,
522535
uint16_t optiga_oid,
@@ -528,6 +541,7 @@ static optiga_lib_status_t _optiga_util_read_metadata_sync(
528541
_WAIT(res, _optiga_lib_status);
529542
return res;
530543
}
544+
#endif
531545

532546
#if FACTORYSETUP == 1 || FACTORY_DURING_PROD == 1
533547
static optiga_lib_status_t _optiga_util_write_metadata_sync(
@@ -781,6 +795,7 @@ static int _write_arbitrary_data(const arbitrary_data_t* data)
781795
}
782796
#endif
783797

798+
#if VERIFY_METADATA == 1
784799
// In a metadata object (0x20 <len> <tag> <tag len> <tag data> ...),
785800
// extract tag data for a specific tag.
786801
// Returns false if the metadata is invalid or the tag is not present, or if the tag data is larger
@@ -834,6 +849,7 @@ static bool _read_metadata_tag(
834849
// Tag not found
835850
return false;
836851
}
852+
#endif
837853

838854
#if FACTORYSETUP == 1 || FACTORY_DURING_PROD == 1
839855
// Read the LcsO status from a metadata object. Returns false if the metadata is invalid or LcsO is
@@ -1212,6 +1228,7 @@ static int _factory_setup(void)
12121228
}
12131229
#endif // FACTORYSETUP == 1 || FACTORY_DURING_PROD == 1
12141230

1231+
#if VERIFY_METADATA == 1
12151232
static int _verify_metadata(
12161233
uint16_t oid,
12171234
const uint8_t* expected_metadata,
@@ -1257,6 +1274,7 @@ static int _verify_metadata(
12571274
}
12581275
return 0;
12591276
}
1277+
#endif
12601278

12611279
static int _set_password(
12621280
const uint8_t* password_secret,
@@ -1443,8 +1461,8 @@ static int _verify_config(void)
14431461
return res;
14441462
}
14451463

1464+
#if VERIFY_METADATA == 1
14461465
// Verify metadata tags are setup as expected.
1447-
14481466
{
14491467
const uint8_t check_tags[] = {0xC0, 0xD0, 0xD1, 0xD3, 0xE8};
14501468
res = _verify_metadata(
@@ -1558,7 +1576,7 @@ static int _verify_config(void)
15581576
return res;
15591577
}
15601578
}
1561-
1579+
#endif
15621580
return 0;
15631581
}
15641582

0 commit comments

Comments
 (0)