Skip to content

Commit 0d0e98e

Browse files
committed
[rom_ext] Cherry-pick pr27385 from earlgrey_1.0.0
Port pr27385 from earlgrey_1.0.0 to master. Signed-off-by: Siemen Dhooghe <[email protected]>
1 parent 06584dc commit 0d0e98e

File tree

12 files changed

+151
-82
lines changed

12 files changed

+151
-82
lines changed

sw/device/silicon_creator/lib/ownership/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ dual_cc_library(
8383
"//sw/device/silicon_creator/lib/drivers:keymgr",
8484
"//sw/device/silicon_creator/lib/drivers:kmac",
8585
"//sw/device/silicon_creator/lib/drivers:flash_ctrl",
86+
"//sw/device/silicon_creator/lib/sigverify:flash_exec",
8687
],
8788
host = [
8889
"//sw/device/lib/base:global_mock",
@@ -114,6 +115,7 @@ cc_library(
114115
"//sw/device/silicon_creator/lib/drivers:flash_ctrl",
115116
"//sw/device/silicon_creator/lib/drivers:lifecycle",
116117
"//sw/device/silicon_creator/lib/drivers:rnd",
118+
"//sw/device/silicon_creator/lib/sigverify:flash_exec",
117119
],
118120
)
119121

@@ -166,6 +168,7 @@ cc_library(
166168
"//sw/device/silicon_creator/lib/drivers:flash_ctrl",
167169
"//sw/device/silicon_creator/lib/drivers:hmac",
168170
"//sw/device/silicon_creator/lib/drivers:lifecycle",
171+
"//sw/device/silicon_creator/lib/sigverify:flash_exec",
169172
],
170173
)
171174

@@ -183,6 +186,7 @@ cc_test(
183186
"//sw/device/silicon_creator/lib/boot_svc:boot_svc_header",
184187
"//sw/device/silicon_creator/lib/drivers:lifecycle",
185188
"//sw/device/silicon_creator/lib/drivers:rnd",
189+
"//sw/device/silicon_creator/lib/sigverify:flash_exec",
186190
"//sw/device/silicon_creator/testing:rom_test",
187191
"@googletest//:gtest_main",
188192
],
@@ -202,6 +206,7 @@ cc_library(
202206
"//sw/device/silicon_creator/lib/boot_svc:boot_svc_msg",
203207
"//sw/device/silicon_creator/lib/drivers:flash_ctrl",
204208
"//sw/device/silicon_creator/lib/drivers:lifecycle",
209+
"//sw/device/silicon_creator/lib/sigverify:flash_exec",
205210
],
206211
)
207212

@@ -217,6 +222,7 @@ cc_test(
217222
"//sw/device/lib/base:hardened",
218223
"//sw/device/silicon_creator/lib:boot_data",
219224
"//sw/device/silicon_creator/lib/boot_svc:boot_svc_header",
225+
"//sw/device/silicon_creator/lib/sigverify:flash_exec",
220226
"//sw/device/silicon_creator/testing:rom_test",
221227
"@googletest//:gtest_main",
222228
],

sw/device/silicon_creator/lib/ownership/mock_ownership_key.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ extern "C" {
99

1010
hardened_bool_t ownership_key_validate(size_t page, ownership_key_t key,
1111
const owner_signature_t *signature,
12-
const void *message, size_t len) {
12+
const void *message, size_t len,
13+
uint32_t *flash_exec) {
1314
return MockOwnershipKey::Instance().validate(page, key, signature, message,
14-
len);
15+
len, flash_exec);
1516
}
1617

1718
rom_error_t ownership_seal_init() {

sw/device/silicon_creator/lib/ownership/mock_ownership_key.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class MockOwnershipKey : public global_mock::GlobalMock<MockOwnershipKey> {
1919
public:
2020
MOCK_METHOD(hardened_bool_t, validate,
2121
(size_t, ownership_key_t, const owner_signature_t *, const void *,
22-
size_t));
22+
size_t, uint32_t *));
2323
MOCK_METHOD(rom_error_t, seal_init, ());
2424
MOCK_METHOD(rom_error_t, seal_page, (size_t));
2525
MOCK_METHOD(rom_error_t, seal_check, (size_t));

sw/device/silicon_creator/lib/ownership/ownership.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ static owner_page_status_t owner_page_validity_check(size_t page) {
4747
return kOwnerPageStatusSealed;
4848
}
4949

50-
hardened_bool_t result = ownership_key_validate(page, kOwnershipKeyOwner,
51-
&owner_page[page].signature,
52-
&owner_page[page], sig_len);
50+
hardened_bool_t result = ownership_key_validate(
51+
page, kOwnershipKeyOwner, &owner_page[page].signature, &owner_page[page],
52+
sig_len, NULL);
5353
if (result == kHardenedBoolFalse) {
5454
// If the page is bad, destroy the RAM copy.
5555
memset(&owner_page[page], 0x5a, sizeof(owner_page[0]));

sw/device/silicon_creator/lib/ownership/ownership_activate.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "sw/device/silicon_creator/lib/error.h"
1414
#include "sw/device/silicon_creator/lib/ownership/owner_block.h"
1515
#include "sw/device/silicon_creator/lib/ownership/ownership_key.h"
16+
#include "sw/device/silicon_creator/lib/sigverify/flash_exec.h"
1617

1718
rom_error_t ownership_activate(boot_data_t *bootdata,
1819
hardened_bool_t write_both_pages) {
@@ -62,15 +63,21 @@ static rom_error_t activate_handler(boot_svc_msg_t *msg,
6263
return kErrorOwnershipInvalidInfoPage;
6364
}
6465

66+
// Set the variable checking whether the correct signatures have been
67+
// verified.
68+
uint32_t flash_exec = 0;
69+
6570
// Check the activation key and the nonce.
6671
size_t len = (uintptr_t)&msg->ownership_activate_req.signature -
6772
(uintptr_t)&msg->ownership_activate_req.primary_bl0_slot;
6873
if (ownership_key_validate(/*page=*/1, kOwnershipKeyActivate,
6974
&msg->ownership_activate_req.signature,
70-
&msg->ownership_activate_req.primary_bl0_slot,
71-
len) == kHardenedBoolFalse) {
75+
&msg->ownership_activate_req.primary_bl0_slot, len,
76+
&flash_exec) == kHardenedBoolFalse) {
7277
return kErrorOwnershipInvalidSignature;
7378
}
79+
// Verify that we passed signature verification for the message.
80+
HARDENED_CHECK_EQ(flash_exec, kSigverifyFlashExec);
7481
if (!nonce_equal(&msg->ownership_activate_req.nonce, &bootdata->nonce)) {
7582
return kErrorOwnershipInvalidNonce;
7683
}

sw/device/silicon_creator/lib/ownership/ownership_activate_unittest.cc

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
#include "sw/device/silicon_creator/lib/ownership/datatypes.h"
2222
#include "sw/device/silicon_creator/lib/ownership/mock_ownership_key.h"
2323
#include "sw/device/silicon_creator/lib/ownership/owner_block.h"
24+
#include "sw/device/silicon_creator/lib/sigverify/flash_exec.h"
2425
#include "sw/device/silicon_creator/testing/rom_test.h"
2526

2627
namespace {
2728
using ::testing::_;
29+
using ::testing::DoAll;
2830
using ::testing::Return;
2931
using ::testing::SetArgPointee;
3032

@@ -131,8 +133,9 @@ TEST_P(OwnershipActivateValidStateTest, InvalidVersion) {
131133
MakePage1Valid(true);
132134
owner_page[1].header.version.major = 5;
133135

134-
EXPECT_CALL(ownership_key_, validate(1, kOwnershipKeyActivate, _, _, _))
135-
.WillOnce(Return(kHardenedBoolTrue));
136+
EXPECT_CALL(ownership_key_, validate(1, kOwnershipKeyActivate, _, _, _, _))
137+
.WillOnce(DoAll(SetArgPointee<5>(kSigverifyFlashExec),
138+
Return(kHardenedBoolTrue)));
136139
EXPECT_CALL(lifecycle_, DeviceId(_))
137140
.WillOnce(SetArgPointee<0>((lifecycle_device_id_t){0}));
138141
EXPECT_CALL(hdr_, Finalize(_, _, _));
@@ -147,8 +150,8 @@ TEST_P(OwnershipActivateValidStateTest, InvalidSignature) {
147150
// We want to pass the page 1 validity test to check the signature on the
148151
// message.
149152
MakePage1Valid(true);
150-
EXPECT_CALL(ownership_key_, validate(1, kOwnershipKeyActivate, _, _, _))
151-
.WillOnce(Return(kHardenedBoolFalse));
153+
EXPECT_CALL(ownership_key_, validate(1, kOwnershipKeyActivate, _, _, _, _))
154+
.WillOnce(DoAll(SetArgPointee<5>(0), Return(kHardenedBoolFalse)));
152155
EXPECT_CALL(hdr_, Finalize(_, _, _));
153156

154157
rom_error_t error = ownership_activate_handler(&message_, &bootdata_);
@@ -162,8 +165,9 @@ TEST_P(OwnershipActivateValidStateTest, InvalidNonce) {
162165
// We want to pass the page 1 validity test to check the nonce of the
163166
// message.
164167
MakePage1Valid(true);
165-
EXPECT_CALL(ownership_key_, validate(1, kOwnershipKeyActivate, _, _, _))
166-
.WillOnce(Return(kHardenedBoolTrue));
168+
EXPECT_CALL(ownership_key_, validate(1, kOwnershipKeyActivate, _, _, _, _))
169+
.WillOnce(DoAll(SetArgPointee<5>(kSigverifyFlashExec),
170+
Return(kHardenedBoolTrue)));
167171
EXPECT_CALL(hdr_, Finalize(_, _, _));
168172

169173
rom_error_t error = ownership_activate_handler(&message_, &bootdata_);
@@ -176,8 +180,9 @@ TEST_P(OwnershipActivateValidStateTest, InvalidActivateDin) {
176180
// We want to pass the page 1 validity test to check the nonce of the
177181
// message.
178182
MakePage1Valid(true);
179-
EXPECT_CALL(ownership_key_, validate(1, kOwnershipKeyActivate, _, _, _))
180-
.WillOnce(Return(kHardenedBoolTrue));
183+
EXPECT_CALL(ownership_key_, validate(1, kOwnershipKeyActivate, _, _, _, _))
184+
.WillOnce(DoAll(SetArgPointee<5>(kSigverifyFlashExec),
185+
Return(kHardenedBoolTrue)));
181186
EXPECT_CALL(lifecycle_, DeviceId(_))
182187
.WillOnce(SetArgPointee<0>((lifecycle_device_id_t){0, 1, 1}));
183188
EXPECT_CALL(hdr_, Finalize(_, _, _));
@@ -228,8 +233,9 @@ TEST_P(OwnershipActivateValidStateTest, OwnerPageValid) {
228233
bootdata_.next_owner[0] = 12345;
229234
MakePage1Valid(true);
230235

231-
EXPECT_CALL(ownership_key_, validate(1, kOwnershipKeyActivate, _, _, _))
232-
.WillOnce(Return(kHardenedBoolTrue));
236+
EXPECT_CALL(ownership_key_, validate(1, kOwnershipKeyActivate, _, _, _, _))
237+
.WillOnce(DoAll(SetArgPointee<5>(kSigverifyFlashExec),
238+
Return(kHardenedBoolTrue)));
233239
EXPECT_CALL(lifecycle_, DeviceId(_))
234240
.WillOnce(SetArgPointee<0>((lifecycle_device_id_t){0}));
235241

@@ -297,8 +303,9 @@ TEST_P(OwnershipActivateValidStateTest, UpdateBootdataBl0) {
297303
MakePage1Valid(true);
298304
owner_page[1].min_security_version_bl0 = 5;
299305

300-
EXPECT_CALL(ownership_key_, validate(1, kOwnershipKeyActivate, _, _, _))
301-
.WillOnce(Return(kHardenedBoolTrue));
306+
EXPECT_CALL(ownership_key_, validate(1, kOwnershipKeyActivate, _, _, _, _))
307+
.WillOnce(DoAll(SetArgPointee<5>(kSigverifyFlashExec),
308+
Return(kHardenedBoolTrue)));
302309
EXPECT_CALL(lifecycle_, DeviceId(_))
303310
.WillOnce(SetArgPointee<0>((lifecycle_device_id_t){0}));
304311

sw/device/silicon_creator/lib/ownership/ownership_key.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "sw/device/silicon_creator/lib/drivers/keymgr.h"
1111
#include "sw/device/silicon_creator/lib/drivers/kmac.h"
1212
#include "sw/device/silicon_creator/lib/ownership/ecdsa.h"
13+
#include "sw/device/silicon_creator/lib/sigverify/flash_exec.h"
1314

1415
// RAM copy of the owner INFO pages from flash.
1516
extern owner_block_t owner_page[2];
@@ -18,7 +19,10 @@ OT_WEAK const owner_key_t *const kNoOwnerRecoveryKey;
1819

1920
hardened_bool_t ownership_key_validate(size_t page, ownership_key_t key,
2021
const owner_signature_t *signature,
21-
const void *message, size_t len) {
22+
const void *message, size_t len,
23+
uint32_t *flash_exec) {
24+
// TODO: Pipe this through to a secure ecdsa.
25+
*flash_exec = kSigverifyFlashExec;
2226
if ((key & kOwnershipKeyUnlock) == kOwnershipKeyUnlock) {
2327
if (ecdsa_verify_message(&owner_page[page].unlock_key.ecdsa,
2428
&signature->ecdsa, message,

sw/device/silicon_creator/lib/ownership/ownership_key.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ typedef struct owner_secret_page {
4949
* @param signature The signature over the message.
5050
* @param message Pointer to the message.
5151
* @param len Size of the message.
52+
* @param flash_exec The magic value signifying whether the signature was
53+
* verified.
5254
* @return kHardenedBoolTrue if the message is valid.
5355
*/
5456
hardened_bool_t ownership_key_validate(size_t page, ownership_key_t key,
5557
const owner_signature_t *signature,
56-
const void *message, size_t len);
58+
const void *message, size_t len,
59+
uint32_t *flash_exec);
5760

5861
/**
5962
* Initialize sealing.

sw/device/silicon_creator/lib/ownership/ownership_unlock.c

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "sw/device/silicon_creator/lib/error.h"
1414
#include "sw/device/silicon_creator/lib/ownership/owner_block.h"
1515
#include "sw/device/silicon_creator/lib/ownership/ownership_key.h"
16+
#include "sw/device/silicon_creator/lib/sigverify/flash_exec.h"
1617

1718
static hardened_bool_t is_locked_none(uint32_t ownership_state) {
1819
if (ownership_state == kOwnershipStateLockedOwner ||
@@ -24,12 +25,16 @@ static hardened_bool_t is_locked_none(uint32_t ownership_state) {
2425
return kHardenedBoolTrue;
2526
}
2627

27-
static rom_error_t do_unlock(boot_svc_msg_t *msg, boot_data_t *bootdata) {
28+
static rom_error_t do_unlock(boot_svc_msg_t *msg, boot_data_t *bootdata,
29+
uint32_t *flash_exec) {
2830
// Verify that the nonce is correct.
2931
if (!nonce_equal(&msg->ownership_unlock_req.nonce, &bootdata->nonce)) {
3032
return kErrorOwnershipInvalidNonce;
3133
}
3234

35+
// Verify that we passed signature verification for the message.
36+
HARDENED_CHECK_EQ(*flash_exec, kSigverifyFlashExec);
37+
3338
// Verify the device identification number is correct.
3439
lifecycle_device_id_t device_id;
3540
lifecycle_device_id_get(&device_id);
@@ -59,6 +64,9 @@ static rom_error_t unlock(boot_svc_msg_t *msg, boot_data_t *bootdata) {
5964
size_t len = (uintptr_t)&msg->ownership_unlock_req.signature -
6065
(uintptr_t)&msg->ownership_unlock_req.unlock_mode;
6166
if (bootdata->ownership_state == kOwnershipStateLockedOwner) {
67+
// Set the variable checking whether the correct signatures have been
68+
// verified
69+
uint32_t flash_exec = 0;
6270
switch (owner_page[0].update_mode) {
6371
case kOwnershipUpdateModeOpen:
6472
// The Open mode allows unlock to any unlock state.
@@ -79,21 +87,24 @@ static rom_error_t unlock(boot_svc_msg_t *msg, boot_data_t *bootdata) {
7987
if (ownership_key_validate(
8088
/*page=*/0, kOwnershipKeyUnlock | kOwnershipKeyRecovery,
8189
&msg->ownership_unlock_req.signature,
82-
&msg->ownership_unlock_req.unlock_mode,
83-
len) == kHardenedBoolFalse) {
90+
&msg->ownership_unlock_req.unlock_mode, len,
91+
&flash_exec) == kHardenedBoolFalse) {
8492
return kErrorOwnershipInvalidSignature;
8593
}
86-
return do_unlock(msg, bootdata);
94+
return do_unlock(msg, bootdata, &flash_exec);
8795
} else if (is_locked_none(bootdata->ownership_state) == kHardenedBoolTrue) {
8896
// In the No-Owner state, we check against the silicon_creator's
8997
// no_owner_recovery_key.
98+
// Set the variable checking whether the correct signatures have been
99+
// verified
100+
uint32_t flash_exec = 0;
90101
if (ownership_key_validate(/*page=*/0, kOwnershipKeyRecovery,
91102
&msg->ownership_unlock_req.signature,
92-
&msg->ownership_unlock_req.unlock_mode,
93-
len) == kHardenedBoolFalse) {
103+
&msg->ownership_unlock_req.unlock_mode, len,
104+
&flash_exec) == kHardenedBoolFalse) {
94105
return kErrorOwnershipInvalidSignature;
95106
}
96-
return do_unlock(msg, bootdata);
107+
return do_unlock(msg, bootdata, &flash_exec);
97108
} else {
98109
return kErrorOwnershipInvalidState;
99110
}
@@ -102,6 +113,9 @@ static rom_error_t unlock(boot_svc_msg_t *msg, boot_data_t *bootdata) {
102113
static rom_error_t unlock_update(boot_svc_msg_t *msg, boot_data_t *bootdata) {
103114
size_t len = (uintptr_t)&msg->ownership_unlock_req.signature -
104115
(uintptr_t)&msg->ownership_unlock_req.unlock_mode;
116+
// Set the variable checking whether the correct signatures have been
117+
// verified.
118+
uint32_t flash_exec = 0;
105119
if (bootdata->ownership_state == kOwnershipStateLockedOwner) {
106120
switch (owner_page[0].update_mode) {
107121
case kOwnershipUpdateModeNewVersion:
@@ -118,11 +132,11 @@ static rom_error_t unlock_update(boot_svc_msg_t *msg, boot_data_t *bootdata) {
118132
// Check the signature against the unlock key.
119133
if (ownership_key_validate(/*page=*/0, kOwnershipKeyUnlock,
120134
&msg->ownership_unlock_req.signature,
121-
&msg->ownership_unlock_req.unlock_mode,
122-
len) == kHardenedBoolFalse) {
135+
&msg->ownership_unlock_req.unlock_mode, len,
136+
&flash_exec) == kHardenedBoolFalse) {
123137
return kErrorOwnershipInvalidSignature;
124138
}
125-
return do_unlock(msg, bootdata);
139+
return do_unlock(msg, bootdata, &flash_exec);
126140
}
127141
return kErrorOwnershipInvalidState;
128142
}
@@ -133,13 +147,18 @@ static rom_error_t unlock_abort(boot_svc_msg_t *msg, boot_data_t *bootdata) {
133147
if (bootdata->ownership_state == kOwnershipStateUnlockedEndorsed ||
134148
bootdata->ownership_state == kOwnershipStateUnlockedAny ||
135149
bootdata->ownership_state == kOwnershipStateUnlockedSelf) {
150+
// Set the variable checking whether the correct signatures have been
151+
// verified.
152+
uint32_t flash_exec = 0;
136153
// Check the signature against the unlock key.
137154
if (ownership_key_validate(/*page=*/0, kOwnershipKeyUnlock,
138155
&msg->ownership_unlock_req.signature,
139-
&msg->ownership_unlock_req.unlock_mode,
140-
len) == kHardenedBoolFalse) {
156+
&msg->ownership_unlock_req.unlock_mode, len,
157+
&flash_exec) == kHardenedBoolFalse) {
141158
return kErrorOwnershipInvalidSignature;
142159
}
160+
// Verify that we passed signature verification for the message.
161+
HARDENED_CHECK_EQ(flash_exec, kSigverifyFlashExec);
143162
if (!nonce_equal(&msg->ownership_unlock_req.nonce, &bootdata->nonce)) {
144163
return kErrorOwnershipInvalidNonce;
145164
}

0 commit comments

Comments
 (0)