Skip to content

Commit 19a104c

Browse files
maulik-armadeaarm
authored andcommitted
RSE: BL2: Add multi sign support for MCUBOOT_HW_KEY
By default MCUBOOT_IMAGE_MULTI_SIG_SUPPORT is set to OFF, maintaining the existing single-signature behavior. When enabled, RSE secure image is also signed with additional ROTPK key. And both the signaures are verified during boot. This patch works with changes to mcuboot for multi-signature support (PR: mcu-tools/mcuboot#2305) Signed-off-by: Maulik Patel <[email protected]> Change-Id: Ic72d1dcfa3f3ada6c4d275281122f6d919a2d8e1
1 parent db0b457 commit 19a104c

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed

bl2/ext/mcuboot/keys.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,11 @@ struct bootutil_key bootutil_keys[1] = {
526526
const int bootutil_key_cnt = 1;
527527

528528
int boot_retrieve_public_key_hash(uint8_t image_index,
529+
uint8_t key_index,
529530
uint8_t *public_key_hash,
530531
size_t *key_hash_size)
531532
{
533+
(void) key_index;
532534
return tfm_plat_get_rotpk_hash(image_index,
533535
public_key_hash,
534536
(uint32_t *)key_hash_size);

platform/ext/target/arm/rse/common/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ target_compile_definitions(platform_bl2
425425
$<$<BOOL:${PLATFORM_HOST_HAS_SCP}>:PLATFORM_HOST_HAS_SCP>
426426
$<$<BOOL:${RSE_USE_SDS_LIB}>:RSE_USE_SDS_LIB>
427427
$<$<BOOL:${RSE_BL2_ENABLE_IMAGE_STAGING}>:RSE_BL2_ENABLE_IMAGE_STAGING>
428+
$<$<BOOL:${MCUBOOT_IMAGE_MULTI_SIG_SUPPORT}>:MCUBOOT_IMAGE_MULTI_SIG_SUPPORT>
429+
$<$<BOOL:${MCUBOOT_ROTPK_SIGN_POLICY}>:MCUBOOT_ROTPK_SIGN_POLICY>
430+
MCUBOOT_ROTPK_MAX_KEYS_PER_IMAGE=${MCUBOOT_ROTPK_MAX_KEYS_PER_IMAGE}
428431
)
429432

430433
target_compile_options(platform_bl2

platform/ext/target/arm/rse/common/bl2/rse_bl2_rotpk.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
*
66
*/
77

8+
#include <assert.h>
89
#include <stdint.h>
910
#include "tfm_plat_otp.h"
1011

1112
#include <bootutil/sign_key.h>
1213
#include "rse_rotpk_mapping.h"
14+
#include "rse_rotpk_policy.h"
15+
#include "tfm_plat_crypto_keys.h"
1316

1417
#ifdef MCUBOOT_HW_KEY
1518
static enum tfm_plat_err_t get_rotpk_hash(enum tfm_otp_element_id_t id,
@@ -41,7 +44,15 @@ struct bootutil_key bootutil_keys[1] = {
4144
.len = &pub_key_len,
4245
},
4346
};
47+
#ifdef MCUBOOT_IMAGE_MULTI_SIG_SUPPORT
48+
#define MAX_KEYS_PER_IMAGE MCUBOOT_ROTPK_MAX_KEYS_PER_IMAGE
49+
50+
const int bootutil_key_cnt = MCUBOOT_IMAGE_NUMBER * MAX_KEYS_PER_IMAGE;
51+
#else
4452
const int bootutil_key_cnt = 1;
53+
#endif /* MCUBOOT_IMAGE_MULTI_SIG_SUPPORT */
54+
55+
#ifndef MCUBOOT_IMAGE_MULTI_SIG_SUPPORT
4556

4657
static enum tfm_plat_err_t get_otp_id(uint32_t image_index,
4758
enum tfm_otp_element_id_t *otp_id)
@@ -64,9 +75,11 @@ static enum tfm_plat_err_t get_otp_id(uint32_t image_index,
6475
}
6576

6677
int boot_retrieve_public_key_hash(uint8_t image_index,
78+
uint8_t key_index,
6779
uint8_t *public_key_hash,
6880
size_t *key_hash_size)
6981
{
82+
(void)key_index;
7083
enum tfm_otp_element_id_t otp_id;
7184
enum tfm_plat_err_t err;
7285

@@ -78,6 +91,101 @@ int boot_retrieve_public_key_hash(uint8_t image_index,
7891
return get_rotpk_hash(otp_id, public_key_hash, key_hash_size);
7992
}
8093

94+
#else
95+
static enum tfm_bl2_key_policy_t rse_policy_to_bl2_policy(enum rse_rotpk_policy policy)
96+
{
97+
switch(policy) {
98+
case RSE_ROTPK_POLICY_SIG_OPTIONAL:
99+
return TFM_BL2_KEY_MIGHT_SIGN;
100+
case RSE_ROTPK_POLICY_SIG_REQUIRED:
101+
return TFM_BL2_KEY_MUST_SIGN;
102+
default:
103+
assert(0 && "Invalid RSE ROTPK policy");
104+
return (enum tfm_bl2_key_policy_t)policy;
105+
}
106+
}
107+
108+
/* Since for MCUBOOT_HW_KEY, key has is attached to the image, so inorder to
109+
* to identify the key policy after the signature is verified in mcuboot,
110+
* policy associated with the key is stored statically while the hash is matched
111+
*/
112+
static enum tfm_bl2_key_policy_t key_policy;
113+
114+
int bl2_otp_get_key_policy(enum tfm_otp_element_id_t otp_id,
115+
enum tfm_bl2_key_policy_t *key_policy)
116+
{
117+
enum tfm_plat_err_t err;
118+
enum rse_rotpk_policy rse_policy;
119+
120+
err = rse_rotpk_get_policy(otp_id, &rse_policy);
121+
if (err != TFM_PLAT_ERR_SUCCESS) {
122+
return -1;
123+
}
124+
125+
*key_policy = rse_policy_to_bl2_policy(rse_policy);
126+
127+
return 0;
128+
}
129+
130+
int boot_retrieve_public_key_hash(uint8_t image_index,
131+
uint8_t key_index,
132+
uint8_t *public_key_hash,
133+
size_t *key_hash_size)
134+
{
135+
int rc;
136+
enum tfm_otp_element_id_t otp_id;
137+
138+
switch (key_index) {
139+
case 0:
140+
/* Check CM key */
141+
otp_id = rse_cm_get_bl2_rotpk(image_index);
142+
break;
143+
case 1:
144+
/* Check DM key */
145+
otp_id = rse_dm_get_bl2_rotpk(image_index);
146+
break;
147+
default:
148+
/* Invalid key_index: only two keys are supported */
149+
return -1;
150+
}
151+
152+
if (otp_id != PLAT_OTP_ID_INVALID) {
153+
rc = get_rotpk_hash(otp_id, public_key_hash, key_hash_size);
154+
if (rc != TFM_PLAT_ERR_SUCCESS) {
155+
return -1;
156+
}
157+
158+
/* Get the key policy */
159+
rc = bl2_otp_get_key_policy(otp_id, &key_policy);
160+
if (rc != 0) {
161+
return -1;
162+
}
163+
}
164+
165+
return 0;
166+
}
167+
168+
int boot_plat_check_key_policy(bool valid_sig, psa_key_id_t key,
169+
bool *key_might_sign, bool *key_must_sign,
170+
uint8_t *key_must_sign_count)
171+
{
172+
(void)key;
173+
#ifndef MCUBOOT_ROTPK_SIGN_POLICY
174+
/* By default key policy is a MUST SIGN */
175+
key_policy = TFM_BL2_KEY_MUST_SIGN;
176+
#endif /* !MCUBOOT_ROTPK_SIGN_POLICY */
177+
178+
if (key_policy == TFM_BL2_KEY_MIGHT_SIGN) {
179+
*key_might_sign |= valid_sig;
180+
} else {
181+
*key_must_sign_count += 1;
182+
*key_might_sign |= valid_sig;
183+
*key_must_sign &= valid_sig;
184+
}
185+
return 0;
186+
}
187+
#endif /* !MCUBOOT_IMAGE_MULTI_SIG_SUPPORT */
188+
81189
#else
82190

83191
/**

platform/ext/target/arm/rse/common/config.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ set(CONFIG_BOOT_RAM_LOAD ON CACHE BOOL "Whether
126126
set(RSE_USE_HOST_FLASH ON CACHE BOOL "Enable RSE using the host flash.")
127127
set(RSE_LOAD_NS_IMAGE ON CACHE BOOL "Whether to load an RSE NSPE image")
128128
set(RSE_BL2_ENABLE_IMAGE_STAGING OFF CACHE BOOL "Whether to enable staging of the images to be loaded by BL2")
129+
set(MCUBOOT_IMAGE_MULTI_SIG_SUPPORT OFF CACHE BOOL "Whether to enable multi-signature support in MCUBoot")
129130

130131
######################### Provisioning #########################################
131132

platform/ext/target/arm/rse/common/subplatform_pal_default_config/rse_rotpk_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ enum rse_host_rotpk_id {
5252
(RSE_ROTPK_DM_BL1_AMOUNT ? DM_ROTPK_BL1_0 : PLAT_OTP_ID_INVALID - PLAT_OTP_ID_DM_ROTPK)
5353

5454
#define RSE_DM_OTP_ID_FOR_BL2_IMAGE(id) \
55-
(RSE_ROTPK_DM_BL2_AMOUNT && \
56-
(id) == 1 ? DM_ROTPK_BL2_0 : PLAT_OTP_ID_INVALID - PLAT_OTP_ID_DM_ROTPK)
55+
((RSE_ROTPK_DM_BL2_AMOUNT && \
56+
((id) == 0 || (id) == 1)) ? DM_ROTPK_BL2_0 : PLAT_OTP_ID_INVALID - PLAT_OTP_ID_DM_ROTPK)
5757

5858
#define RSE_DM_OTP_ID_FOR_HOST(id) \
5959
(RSE_ROTPK_DM_HOST_AMOUNT ? DM_ROTPK_HOST_0 + (id) : PLAT_OTP_ID_INVALID - PLAT_OTP_ID_DM_ROTPK)

0 commit comments

Comments
 (0)