Skip to content

Commit 116c226

Browse files
esnguyentimothytrippel
authored andcommitted
[rom_ext] Add additional unit tests
This change fills out the BootSvcEnterRescue, BootSvcOwnershipUnlock, and BootSvcOwnershipActivate unit tests with the required inputs to pass. Signed-off-by: Ellis Sarza-Nguyen <[email protected]>
1 parent 4228a37 commit 116c226

File tree

1 file changed

+163
-2
lines changed

1 file changed

+163
-2
lines changed

sw/device/silicon_creator/rom_ext/rom_ext_boot_services_unittest.cc

Lines changed: 163 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@
1111
#include "sw/device/silicon_creator/lib/drivers/mock_rnd.h"
1212
#include "sw/device/silicon_creator/lib/mock_boot_data.h"
1313
#include "sw/device/silicon_creator/lib/mock_manifest.h"
14+
#include "sw/device/silicon_creator/lib/drivers/mock_flash_ctrl.h"
1415
#include "sw/device/silicon_creator/lib/ownership/mock_owner_verify.h"
16+
#include "sw/device/silicon_creator/lib/ownership/mock_ownership_key.h"
17+
#include "sw/device/silicon_creator/lib/ownership/datatypes.h"
18+
#include "sw/device/silicon_creator/lib/ownership/ownership_activate.h"
19+
#include "sw/device/silicon_creator/lib/ownership/owner_block.h"
1520
#include "sw/device/silicon_creator/rom_ext/mock_rom_ext_boot_policy_ptrs.h"
21+
#include "sw/device/silicon_creator/lib/boot_svc/mock_boot_svc_header.h"
1622
#include "sw/device/silicon_creator/testing/rom_test.h"
1723

1824
namespace boot_services_unittest {
@@ -24,6 +30,12 @@ using ::testing::Each;
2430
using ::testing::Return;
2531
using ::testing::SetArgPointee;
2632

33+
constexpr uint32_t kActivate =
34+
static_cast<uint32_t>(kBootSvcOwnershipActivateReqType);
35+
36+
constexpr uint32_t kUnlock =
37+
static_cast<uint32_t>(kBootSvcOwnershipUnlockReqType);
38+
2739
class RomExtBootServicesTest : public rom_test::RomTest {
2840
protected:
2941
boot_svc_msg_t boot_svc_msg{};
@@ -42,7 +54,49 @@ class RomExtBootServicesTest : public rom_test::RomTest {
4254
rom_test::MockRnd mock_rnd_;
4355
rom_test::MockLifecycle mock_lifecycle_;
4456
rom_test::MockOtp mock_otp_;
57+
rom_test::MockFlashCtrl mock_flash_ctrl_;
4558
rom_test::MockOwnerVerify mock_owner_verify_;
59+
rom_test::MockOwnershipKey mock_ownership_key_;
60+
rom_test::MockBootSvcHeader boot_svc_header_;
61+
62+
void MakePage1StructValid() {
63+
owner_page[1].header.tag = kTlvTagOwner;
64+
owner_page[1].header.length = sizeof(owner_page[1]);
65+
owner_page[1].header.version = (struct_version_t){0, 0};
66+
owner_page[1].config_version = 0;
67+
owner_page[1].min_security_version_bl0 = UINT32_MAX;
68+
owner_page[1].lock_constraint = 0;
69+
memset(owner_page[1].device_id, 0x7e, sizeof(owner_page[1].device_id));
70+
memset(owner_page[1].data, 0x5a, sizeof(owner_page[1].data));
71+
}
72+
73+
void MakePage1Valid(bool valid) {
74+
MakePage1StructValid();
75+
ownership_state_t state =
76+
static_cast<ownership_state_t>(boot_data.ownership_state);
77+
owner_page_valid[1] = kOwnerPageStatusSigned;
78+
uint32_t modifier = valid ? 0 : 1;
79+
80+
switch (state) {
81+
case kOwnershipStateUnlockedEndorsed:
82+
// In UnlockedEndorsed, the hash of the owner key in page1 must be equal
83+
// to the value stored in boot_data.
84+
case kOwnershipStateUnlockedSelf:
85+
86+
owner_page[1].owner_key = owner_page[0].owner_key;
87+
owner_page[1].owner_key.raw[0] += modifier;
88+
break;
89+
case kOwnershipStateUnlockedAny:
90+
// In UnlockedAny, there are no conditions that page1 must meet.
91+
break;
92+
case kOwnershipStateLockedOwner:
93+
owner_page_valid[1] = kOwnerPageStatusSealed;
94+
break;
95+
case kOwnershipStateRecovery:
96+
owner_page_valid[1] = kOwnerPageStatusInvalid;
97+
break;
98+
}
99+
}
46100
};
47101

48102
TEST_F(RomExtBootServicesTest, BootSvcDefault) {
@@ -78,7 +132,16 @@ TEST_F(RomExtBootServicesTest, BootSvcEmpty) {
78132
}
79133

80134
TEST_F(RomExtBootServicesTest, BootSvcEnterRescue) {
135+
boot_svc_msg.header.identifier = kBootSvcIdentifier;
81136
boot_svc_msg.header.type = kBootSvcEnterRescueReqType;
137+
boot_svc_msg.header.length = sizeof(boot_svc_enter_rescue_req_t);
138+
boot_svc_msg.header.digest = hmac_digest_t{0x1234};
139+
140+
EXPECT_CALL(mock_hmac_, sha256)
141+
.WillOnce(SetArgPointee<2>(hmac_digest_t{0x1234}));
142+
143+
EXPECT_CALL(mock_hmac_, sha256)
144+
.WillOnce(SetArgPointee<2>(hmac_digest_t{0x1234}));
82145

83146
EXPECT_EQ(
84147
boot_svc_handler(&boot_svc_msg, &boot_data, &boot_log, lc_state, &keyring,
@@ -237,26 +300,124 @@ TEST_F(RomExtBootServicesTest, BootSvcMinBl0SecVer) {
237300
}
238301

239302
TEST_F(RomExtBootServicesTest, BootSvcOwnershipUnlock) {
303+
boot_svc_msg.header.identifier = kBootSvcIdentifier;
240304
boot_svc_msg.header.type = kBootSvcOwnershipUnlockReqType;
305+
boot_svc_msg.header.digest = hmac_digest_t{0x1234};
306+
boot_svc_msg.header.length = sizeof(boot_svc_ownership_unlock_req_t);
307+
308+
boot_svc_msg.ownership_unlock_req.unlock_mode = kBootSvcUnlockAbort;
309+
310+
boot_data.ownership_state = kOwnershipStateUnlockedAny;
311+
boot_data.nonce = {0x55555555, 0xAAAAAAAA};
312+
boot_svc_msg.ownership_unlock_req.nonce = boot_data.nonce;
313+
boot_svc_msg.ownership_unlock_req.signature = {{100, 101, 102, 103, 104, 105,
314+
106, 107, 108, 109, 110, 111, 112, 113,
315+
114, 115}};
316+
317+
318+
EXPECT_CALL(mock_hmac_, sha256)
319+
.WillOnce(SetArgPointee<2>(hmac_digest_t{0x1234}));
320+
321+
EXPECT_CALL(mock_ownership_key_,
322+
validate(0, static_cast<ownership_key_t>(kOwnershipKeyUnlock),
323+
kUnlock, _, _, _, _))
324+
.WillOnce(Return(kErrorOk));
325+
EXPECT_CALL(mock_lifecycle_, DeviceId(_))
326+
.WillOnce(SetArgPointee<0>((lifecycle_device_id_t){0}));
327+
328+
EXPECT_CALL(mock_rnd_, Uint32()).WillRepeatedly(Return(5));
329+
330+
EXPECT_CALL(mock_hmac_, sha256)
331+
.WillOnce(SetArgPointee<2>(hmac_digest_t{0x1234}));
241332

242333
EXPECT_EQ(
243334
boot_svc_handler(&boot_svc_msg, &boot_data, &boot_log, lc_state, &keyring,
244335
&verify_key, &owner_config, &isfb_check_count),
245-
kErrorOk);
336+
kErrorWriteBootdataThenReboot);
246337

247338
EXPECT_EQ(boot_svc_msg.ownership_unlock_res.status, kErrorOk);
248339
}
249340

250341
TEST_F(RomExtBootServicesTest, BootSvcOwnershipActivate) {
342+
boot_svc_msg.header.identifier = kBootSvcIdentifier;
251343
boot_svc_msg.header.type = kBootSvcOwnershipActivateReqType;
344+
boot_svc_msg.header.digest = hmac_digest_t{0x1234};
345+
boot_svc_msg.header.length = sizeof(boot_svc_ownership_activate_req_t);
346+
347+
boot_svc_msg.ownership_activate_req.erase_previous = 1;
348+
boot_svc_msg.ownership_activate_req.primary_bl0_slot = 0;
349+
boot_svc_msg.ownership_activate_req.nonce = {0x55555555, 0xAAAAAAAA};
350+
boot_svc_msg.ownership_activate_req.signature = {{100, 101, 102, 103, 104, 105, 106,
351+
107, 108, 109, 110, 111, 112, 113,
352+
114, 115}};
353+
354+
355+
boot_data.ownership_state = kOwnershipStateUnlockedEndorsed;
356+
boot_data.nonce = {0x55555555, 0xAAAAAAAA};
357+
358+
owner_page[0].owner_key = {{1}};
359+
memset(boot_data.next_owner, 0, sizeof(boot_data.next_owner));
360+
boot_data.next_owner[0] = 0x1234;
361+
362+
MakePage1Valid(true);
363+
364+
EXPECT_CALL(mock_hmac_, sha256)
365+
.WillOnce(SetArgPointee<2>(hmac_digest_t{0x1234}));
366+
367+
EXPECT_CALL(mock_hmac_, sha256)
368+
.WillOnce(SetArgPointee<2>(hmac_digest_t{0x1234}));
369+
370+
EXPECT_CALL(mock_ownership_key_,
371+
validate(1, kOwnershipKeyActivate, kActivate, _, _, _, _))
372+
.WillOnce(Return(kErrorOk));
373+
374+
EXPECT_CALL(mock_lifecycle_, DeviceId(_))
375+
.WillOnce(SetArgPointee<0>((lifecycle_device_id_t){0}));
376+
377+
// Once the new owner page is determined to be valid, the page will be sealed.
378+
EXPECT_CALL(mock_ownership_key_, seal_page(1));
379+
380+
// The sealed page will be written into flash owner slot 1 first.
381+
EXPECT_CALL(mock_flash_ctrl_,
382+
InfoErase(&kFlashCtrlInfoPageOwnerSlot1, kFlashCtrlEraseTypePage))
383+
.WillOnce(Return(kErrorOk));
384+
385+
EXPECT_CALL(mock_flash_ctrl_, InfoWrite(&kFlashCtrlInfoPageOwnerSlot1, 0,
386+
sizeof(owner_page[1]) / sizeof(uint32_t),
387+
&owner_page[1]))
388+
.WillOnce(Return(kErrorOk));
389+
390+
EXPECT_CALL(mock_flash_ctrl_,
391+
InfoErase(&kFlashCtrlInfoPageOwnerSlot0, kFlashCtrlEraseTypePage))
392+
.WillOnce(Return(kErrorOk));
393+
394+
EXPECT_CALL(mock_flash_ctrl_, InfoWrite(&kFlashCtrlInfoPageOwnerSlot0, 0,
395+
sizeof(owner_page[1]) / sizeof(uint32_t),
396+
&owner_page[1]))
397+
.WillOnce(Return(kErrorOk));
398+
399+
if (boot_data.ownership_state != kOwnershipStateUnlockedSelf) {
400+
EXPECT_CALL(mock_ownership_key_, secret_new(_, _)).WillOnce(Return(kErrorOk));
401+
}
402+
403+
EXPECT_CALL(mock_rnd_, Uint32()).WillRepeatedly(Return(99));
404+
405+
EXPECT_CALL(mock_hmac_, sha256)
406+
.WillOnce(SetArgPointee<2>(hmac_digest_t{0x1234}));
252407

253408
EXPECT_EQ(
254409
boot_svc_handler(&boot_svc_msg, &boot_data, &boot_log, lc_state, &keyring,
255410
&verify_key, &owner_config, &isfb_check_count),
256-
kErrorOk);
411+
kErrorWriteBootdataThenReboot);
257412

258413
EXPECT_EQ(boot_svc_msg.ownership_activate_res.status, kErrorOk);
414+
259415
}
260416

261417
} // namespace
262418
} // namespace boot_services_unittest
419+
420+
/*
421+
422+
423+
*/

0 commit comments

Comments
 (0)