Skip to content

Commit 7193b65

Browse files
timothytrippelpamaury
authored andcommitted
[rom] fix bug in ROM that computes OTP measurements
This fixes a bug in the ROM that computes OTP measurements to populate the keymgr attestation binding CSRs, if the OTP fuse (`OWNER_SW_CFG_ROM_KEYMGR_OTP_MEAS_EN`) to request this is set. Additionally, this fixes the corresponding ROM E2E test as well, and enhances its robustness. Signed-off-by: Tim Trippel <[email protected]> (cherry picked from commit d1e4892)
1 parent e8fccdb commit 7193b65

File tree

3 files changed

+65
-26
lines changed

3 files changed

+65
-26
lines changed

sw/device/silicon_creator/rom/e2e/keymgr/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ load(
1616
)
1717
load(
1818
"//rules/opentitan:defs.bzl",
19-
"cw310_params",
2019
"dv_params",
2120
"fpga_params",
2221
"opentitan_test",
@@ -98,7 +97,11 @@ rom_e2e_keymgr_init_configs = [
9897
),
9998
deps = [
10099
"//sw/device/lib/dif:keymgr",
100+
"//sw/device/lib/dif:otp_ctrl",
101+
"//sw/device/lib/dif:rstmgr",
101102
"//sw/device/lib/testing:keymgr_testutils",
103+
"//sw/device/lib/testing:otp_ctrl_testutils",
104+
"//sw/device/lib/testing:rstmgr_testutils",
102105
"//sw/device/lib/testing/test_framework:ottf_ld_silicon_creator_slot_a",
103106
"//sw/device/lib/testing/test_framework:ottf_main",
104107
"//sw/device/silicon_creator/lib/drivers:hmac",

sw/device/silicon_creator/rom/e2e/keymgr/rom_e2e_keymgr_init_test.c

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
#include "sw/device/lib/base/mmio.h"
88
#include "sw/device/lib/dif/dif_keymgr.h"
9+
#include "sw/device/lib/dif/dif_otp_ctrl.h"
10+
#include "sw/device/lib/dif/dif_rstmgr.h"
911
#include "sw/device/lib/runtime/ibex.h"
1012
#include "sw/device/lib/runtime/log.h"
1113
#include "sw/device/lib/testing/keymgr_testutils.h"
14+
#include "sw/device/lib/testing/otp_ctrl_testutils.h"
15+
#include "sw/device/lib/testing/rstmgr_testutils.h"
1216
#include "sw/device/lib/testing/test_framework/check.h"
1317
#include "sw/device/lib/testing/test_framework/ottf_main.h"
1418
#include "sw/device/silicon_creator/lib/base/boot_measurements.h"
@@ -23,12 +27,49 @@
2327

2428
OTTF_DEFINE_TEST_CONFIG();
2529

30+
static dif_keymgr_t keymgr;
31+
static dif_otp_ctrl_t otp_ctrl;
32+
static dif_rstmgr_t rstmgr;
2633
static uint32_t otp_state[kHmacDigestNumWords + 4] = {0};
2734

35+
static void print_otp_sw_cfg_digests(void) {
36+
uint64_t creator_digest, owner_digest = 0;
37+
CHECK_DIF_OK(dif_otp_ctrl_get_digest(
38+
&otp_ctrl, kDifOtpCtrlPartitionCreatorSwCfg, &creator_digest));
39+
CHECK_DIF_OK(dif_otp_ctrl_get_digest(
40+
&otp_ctrl, kDifOtpCtrlPartitionOwnerSwCfg, &owner_digest));
41+
LOG_INFO("CreatorSwCfg Digest: 0x%08x%08x", (uint32_t)(creator_digest >> 32),
42+
(uint32_t)creator_digest);
43+
LOG_INFO("OwnerSwCfg Digest: 0x%08x%08x", (uint32_t)(owner_digest >> 32),
44+
(uint32_t)owner_digest);
45+
}
46+
2847
bool test_main(void) {
29-
dif_keymgr_t keymgr;
3048
CHECK_DIF_OK(dif_keymgr_init(
3149
mmio_region_from_addr(TOP_EARLGREY_KEYMGR_BASE_ADDR), &keymgr));
50+
CHECK_DIF_OK(dif_otp_ctrl_init(
51+
mmio_region_from_addr(TOP_EARLGREY_OTP_CTRL_CORE_BASE_ADDR), &otp_ctrl));
52+
CHECK_DIF_OK(dif_rstmgr_init(
53+
mmio_region_from_addr(TOP_EARLGREY_RSTMGR_AON_BASE_ADDR), &rstmgr));
54+
55+
/*// Lock OTP *SwCfg partitions if this is the first boot and reset.*/
56+
if (UNWRAP(rstmgr_testutils_is_reset_info(&rstmgr, kDifRstmgrResetInfoPor))) {
57+
LOG_INFO("Power on reset. Locking OTP *SwCfg partitions ...");
58+
const uint64_t kFakeOtpDigest = 0xaaaabbbbccccdddd;
59+
CHECK_STATUS_OK(otp_ctrl_testutils_lock_partition(
60+
&otp_ctrl, kDifOtpCtrlPartitionCreatorSwCfg,
61+
/*digest=*/kFakeOtpDigest));
62+
CHECK_STATUS_OK(otp_ctrl_testutils_lock_partition(
63+
&otp_ctrl, kDifOtpCtrlPartitionOwnerSwCfg,
64+
/*digest=*/kFakeOtpDigest));
65+
rstmgr_testutils_reason_clear();
66+
LOG_INFO("Issuing a software reset ...");
67+
CHECK_DIF_OK(dif_rstmgr_software_device_reset(&rstmgr));
68+
wait_for_interrupt();
69+
} else {
70+
print_otp_sw_cfg_digests();
71+
LOG_INFO("SW reset. Executing test ...");
72+
}
3273

3374
CHECK_STATUS_OK(keymgr_testutils_check_state(&keymgr, kDifKeymgrStateReset));
3475

@@ -39,36 +80,31 @@ bool test_main(void) {
3980

4081
if (otp_read32(OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_KEYMGR_OTP_MEAS_EN_OFFSET) ==
4182
kHardenedBoolTrue) {
83+
LOG_INFO("ROM OTP measurement feature ENABLED.");
4284
// Check that the attestation is equal to the digest of concatenations of:
4385
// - the digest of the CreatorSwCfg partition,
4486
// - the digest of the OwnerSwCfg partition,
4587
// - the SHA256 integrity hash of the first stage boot keys.
46-
otp_dai_read(
47-
kOtpPartitionCreatorSwCfg,
48-
/*relative_address=*/
49-
otp_readable_partition_info(kOtpPartitionCreatorSwCfg).digest_addr -
50-
OTP_CTRL_PARAM_CREATOR_SW_CFG_OFFSET,
51-
otp_state,
52-
/*num_words=*/2);
53-
otp_dai_read(
54-
kOtpPartitionOwnerSwCfg,
55-
/*relative_address=*/
56-
otp_readable_partition_info(kOtpPartitionOwnerSwCfg).digest_addr -
57-
OTP_CTRL_PARAM_OWNER_SW_CFG_OFFSET,
58-
&otp_state[2],
59-
/*num_words=*/2);
60-
otp_dai_read(kOtpPartitionRotCreatorAuthCodesign,
61-
/*relative_address=*/
62-
OTP_CTRL_PARAM_ROTCREATORAUTHCODESIGNBLOCKSHA2_256HASHOFFSET -
63-
OTP_CTRL_PARAM_ROT_CREATOR_AUTH_CODESIGN_OFFSET,
64-
&otp_state[4],
65-
/*num_words=*/kHmacDigestNumWords);
88+
CHECK_DIF_OK(dif_otp_ctrl_get_digest(
89+
&otp_ctrl, kDifOtpCtrlPartitionCreatorSwCfg, (uint64_t *)otp_state));
90+
CHECK_DIF_OK(dif_otp_ctrl_get_digest(
91+
&otp_ctrl, kDifOtpCtrlPartitionOwnerSwCfg, (uint64_t *)&otp_state[2]));
92+
CHECK_STATUS_OK(otp_ctrl_testutils_dai_read32_array(
93+
&otp_ctrl, kDifOtpCtrlPartitionRotCreatorAuthCodesign,
94+
OTP_CTRL_PARAM_ROTCREATORAUTHCODESIGNBLOCKSHA2_256HASHOFFSET -
95+
OTP_CTRL_PARAM_ROT_CREATOR_AUTH_CODESIGN_OFFSET,
96+
&otp_state[4], /*num_words=*/kHmacDigestNumWords));
6697
hmac_digest_t otp_measurement;
67-
hmac_sha256(otp_state, (kHmacDigestNumWords + 4) * sizeof(uint32_t),
68-
&otp_measurement);
98+
hmac_sha256(otp_state, sizeof(otp_state), &otp_measurement);
99+
LOG_INFO("OTP CreatorSwCfg Digest: 0x%08x%08x", otp_state[1], otp_state[0]);
100+
LOG_INFO("OTP OwnerSwCfg Digest: 0x%08x%08x", otp_state[3], otp_state[2]);
101+
LOG_INFO("OTP Root Keys Digest: 0x%08x%08x%08x%08x%08x%08x%08x%08x",
102+
otp_state[11], otp_state[10], otp_state[9], otp_state[8],
103+
otp_state[7], otp_state[6], otp_state[5], otp_state[4]);
69104
CHECK_ARRAYS_EQ(bindings.attestation, otp_measurement.digest,
70105
ARRAYSIZE(bindings.attestation));
71106
} else {
107+
LOG_INFO("ROM OTP measurement feature DISABLED.");
72108
// Check that the attestation is equal to `binding_value` field of the
73109
// manifest.
74110
CHECK_ARRAYS_EQ(bindings.attestation, manifest->binding_value.data,

sw/device/silicon_creator/rom/rom.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,12 @@ static rom_error_t rom_measure_otp_partitions(
516516
hmac_sha256_update(
517517
(unsigned char *)(TOP_EARLGREY_OTP_CTRL_CORE_BASE_ADDR +
518518
OTP_CTRL_SW_CFG_WINDOW_REG_OFFSET +
519-
OTP_CTRL_CREATOR_SW_CFG_DIGEST_0_REG_OFFSET),
519+
OTP_CTRL_PARAM_CREATOR_SW_CFG_DIGEST_OFFSET),
520520
sizeof(uint64_t));
521521
hmac_sha256_update(
522522
(unsigned char *)(TOP_EARLGREY_OTP_CTRL_CORE_BASE_ADDR +
523523
OTP_CTRL_SW_CFG_WINDOW_REG_OFFSET +
524-
OTP_CTRL_OWNER_SW_CFG_DIGEST_0_REG_OFFSET),
524+
OTP_CTRL_PARAM_OWNER_SW_CFG_DIGEST_OFFSET),
525525
sizeof(uint64_t));
526526
hmac_sha256_update(sigverify_ctx.keys.integrity_measurement.digest,
527527
kHmacDigestNumBytes);

0 commit comments

Comments
 (0)