Software APIs
rom_e2e_keymgr_init_test.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include <stdbool.h>
6 
11 #include "sw/device/lib/testing/keymgr_testutils.h"
12 #include "sw/device/lib/testing/test_framework/check.h"
14 #include "sw/device/silicon_creator/lib/base/boot_measurements.h"
16 #include "sw/device/silicon_creator/lib/drivers/hmac.h"
17 #include "sw/device/silicon_creator/lib/drivers/otp.h"
18 #include "sw/device/silicon_creator/lib/manifest.h"
19 #include "sw/device/silicon_creator/lib/manifest_def.h"
20 
22 #include "otp_ctrl_regs.h" // Generated
23 
24 OTTF_DEFINE_TEST_CONFIG();
25 
26 static uint32_t otp_state[kHmacDigestNumWords + 4] = {0};
27 
28 bool test_main(void) {
29  dif_keymgr_t keymgr;
30  CHECK_DIF_OK(dif_keymgr_init(
32 
33  CHECK_STATUS_OK(keymgr_testutils_check_state(&keymgr, kDifKeymgrStateReset));
34 
36  CHECK_DIF_OK(dif_keymgr_read_binding(&keymgr, &bindings));
37 
38  const manifest_t *manifest = manifest_def_get();
39 
40  if (otp_read32(OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_KEYMGR_OTP_MEAS_EN_OFFSET) ==
42  // Check that the attestation is equal to the digest of concatenations of:
43  // - the digest of the CreatorSwCfg partition,
44  // - the digest of the OwnerSwCfg partition,
45  // - the SHA256 integrity hash of the first stage boot keys.
46  otp_dai_read(kOtpPartitionCreatorSwCfg,
47  /*relative_address=*/
48  kOtpPartitions[kOtpPartitionCreatorSwCfg].digest_addr -
49  OTP_CTRL_PARAM_CREATOR_SW_CFG_OFFSET,
50  otp_state,
51  /*num_words=*/2);
52  otp_dai_read(kOtpPartitionOwnerSwCfg,
53  /*relative_address=*/
54  kOtpPartitions[kOtpPartitionOwnerSwCfg].digest_addr -
55  OTP_CTRL_PARAM_OWNER_SW_CFG_OFFSET,
56  &otp_state[2],
57  /*num_words=*/2);
58  otp_dai_read(kOtpPartitionRotCreatorAuthCodesign,
59  /*relative_address=*/
60  OTP_CTRL_PARAM_ROTCREATORAUTHCODESIGNBLOCKSHA2_256HASHOFFSET -
61  OTP_CTRL_PARAM_ROT_CREATOR_AUTH_CODESIGN_OFFSET,
62  &otp_state[4],
63  /*num_words=*/kHmacDigestNumWords);
64  hmac_digest_t otp_measurement;
65  hmac_sha256(otp_state, (kHmacDigestNumWords + 4) * sizeof(uint32_t),
66  &otp_measurement);
67  CHECK_ARRAYS_EQ(bindings.attestation, otp_measurement.digest,
68  ARRAYSIZE(bindings.attestation));
69  } else {
70  // Check that the attestation is equal to `binding_value` field of the
71  // manifest.
72  CHECK_ARRAYS_EQ(bindings.attestation, manifest->binding_value.data,
73  ARRAYSIZE(bindings.attestation));
74  }
75 
76  // Check that the sealing is equal to `binding_value` field of the
77  // manifest.
78  CHECK_ARRAYS_EQ(bindings.sealing, manifest->binding_value.data,
79  ARRAYSIZE(bindings.sealing));
80 
81  // Check that the creator max version is equal to `max_key_version` field of
82  // the manifest.
84  CHECK_DIF_OK(dif_keymgr_read_max_key_version(&keymgr, &versions));
86  return true;
87 }