Skip to content

Commit db0b457

Browse files
maulik-armadeaarm
authored andcommitted
BL2: Provision RoTPK sign policies
For multisignature, if policy support is enabled, provision the corresponding key policy to the OTP. For built in keys, also check the signature verification result against the policy. Signed-off-by: Maulik Patel <maulik.patel@arm.com> Change-Id: If91a5967f3cea59cb12546c25f53280a2c7d4cb2
1 parent f99c51e commit db0b457

File tree

13 files changed

+195
-5
lines changed

13 files changed

+195
-5
lines changed

bl2/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ list(APPEND BL2_CRYPTO_SRC
9494

9595
add_library(bl2_crypto STATIC ${BL2_CRYPTO_SRC})
9696

97+
target_compile_definitions(bl2_crypto
98+
PRIVATE
99+
$<$<BOOL:${MCUBOOT_ROTPK_SIGN_POLICY}>:MCUBOOT_ROTPK_SIGN_POLICY>
100+
)
101+
97102
target_include_directories(bl2_crypto
98103
PUBLIC
99104
${MBEDCRYPTO_PATH}/library

bl2/ext/mcuboot/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ target_compile_definitions(bl2
8181
$<$<BOOL:${MCUBOOT_BUILTIN_KEY}>:TFM_NS_KEY_ID=${TFM_NS_KEY_ID}>
8282
MCUBOOT_ROTPK_MAX_KEYS_PER_IMAGE=${MCUBOOT_ROTPK_MAX_KEYS_PER_IMAGE}
8383
$<$<BOOL:${MCUBOOT_IMAGE_MULTI_SIG_SUPPORT}>:MCUBOOT_IMAGE_MULTI_SIG_SUPPORT>
84+
$<$<BOOL:${MCUBOOT_ROTPK_SIGN_POLICY}>:MCUBOOT_ROTPK_SIGN_POLICY>
8485
)
8586

8687
target_link_libraries(bl2

bl2/ext/mcuboot/keys.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,14 @@ int boot_retrieve_public_key_hash(uint8_t image_index,
534534
(uint32_t *)key_hash_size);
535535
}
536536
#elif defined(MCUBOOT_BUILTIN_KEY)
537+
538+
#ifdef MCUBOOT_ROTPK_SIGN_POLICY
539+
enum tfm_plat_err_t tfm_plat_get_bl2_rotpk_policies(uint8_t *buf, size_t buf_len)
540+
{
541+
return tfm_plat_otp_read(PLAT_OTP_ID_BL2_ROTPK_POLICIES, buf_len, buf);
542+
}
543+
#endif /* MCUBOOT_ROTPK_SIGN_POLICY */
544+
537545
/* Maximum number of keys per image */
538546
#define MAX_KEYS_PER_IMAGE MCUBOOT_ROTPK_MAX_KEYS_PER_IMAGE
539547

bl2/ext/mcuboot/mcuboot_default_config.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ set(TFM_NS_KEY_ID 1 CACHE STRING "Key ID
6060
set(MCUBOOT_IMAGE_MULTI_SIG_SUPPORT OFF CACHE BOOL "Enable multiple signature support for images")
6161
if(MCUBOOT_IMAGE_MULTI_SIG_SUPPORT)
6262
set(MCUBOOT_ROTPK_MAX_KEYS_PER_IMAGE 2 CACHE STRING "Maximum number of RoTPK keys per image to be used in BL2")
63+
set(MCUBOOT_ROTPK_SIGN_POLICY ON CACHE BOOL "Enable ROTPK signing policy")
6364
else()
6465
set(MCUBOOT_ROTPK_MAX_KEYS_PER_IMAGE 1 CACHE STRING "Maximum number of RoTPK keys per image to be used in BL2")
6566
endif()

bl2/src/provisioning.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717

1818
#define ASSEMBLY_AND_TEST_PROV_DATA_MAGIC 0xC0DEFEED
1919

20+
#ifdef MCUBOOT_ROTPK_SIGN_POLICY
21+
/* Key policy for each ROTPK
22+
* bit 0 corresponds to bl2_rotpk_0,
23+
* bit 1 coresponds to bl2_rotpk_1,
24+
* ...
25+
* If bit set, key is compulsory else optional
26+
*/
27+
#define BL2_ROTPK_POLICIES 0b00000111
28+
#endif /* MCUBOOT_ROTPK_SIGN_POLICY */
29+
2030
#ifdef MCUBOOT_SIGN_EC384
2131
#define PUB_KEY_HASH_SIZE (48)
2232
#define PUB_KEY_SIZE (100) /* Size must be aligned to 4 Bytes */
@@ -33,6 +43,9 @@
3343

3444
__PACKED_STRUCT bl2_assembly_and_test_provisioning_data_t {
3545
uint32_t magic;
46+
#ifdef MCUBOOT_ROTPK_SIGN_POLICY
47+
uint32_t bl2_rotpk_policies;
48+
#endif /* MCUBOOT_ROTPK_SIGN_POLICY */
3649
uint8_t bl2_rotpk_0[PROV_ROTPK_DATA_SIZE];
3750
uint8_t bl2_rotpk_1[PROV_ROTPK_DATA_SIZE];
3851
#if (MCUBOOT_IMAGE_NUMBER > 2)
@@ -198,6 +211,10 @@ __PACKED_STRUCT bl2_assembly_and_test_provisioning_data_t {
198211

199212
static const struct bl2_assembly_and_test_provisioning_data_t bl2_assembly_and_test_prov_data = {
200213
ASSEMBLY_AND_TEST_PROV_DATA_MAGIC,
214+
#ifdef MCUBOOT_ROTPK_SIGN_POLICY
215+
BL2_ROTPK_POLICIES,
216+
#endif /* MCUBOOT_ROTPK_SIGN_POLICY */
217+
201218
#if defined(MCUBOOT_SIGN_RSA)
202219
#if (MCUBOOT_SIGN_RSA_LEN == 2048)
203220
ASSEMBLY_AND_TEST_PROV_DATA_KIND_0, /* bl2 rotpk 0 */
@@ -326,6 +343,16 @@ enum tfm_plat_err_t provision_assembly_and_test(void)
326343
}
327344
#endif /* MCUBOOT_IMAGE_NUMBER > 3 */
328345

346+
#ifdef MCUBOOT_ROTPK_SIGN_POLICY
347+
/* Write the ROTPK policies */
348+
err = tfm_plat_otp_write(PLAT_OTP_ID_BL2_ROTPK_POLICIES,
349+
sizeof(bl2_assembly_and_test_prov_data.bl2_rotpk_policies),
350+
(uint8_t *)&bl2_assembly_and_test_prov_data.bl2_rotpk_policies);
351+
if ((err != TFM_PLAT_ERR_SUCCESS) && (err != TFM_PLAT_ERR_UNSUPPORTED)) {
352+
return err;
353+
}
354+
#endif /* MCUBOOT_ROTPK_SIGN_POLICY */
355+
329356
#ifdef PLATFORM_PSA_ADAC_SECURE_DEBUG
330357
err = tfm_plat_otp_write(PLAT_OTP_ID_SECURE_DEBUG_PK,
331358
sizeof(bl2_assembly_and_test_prov_data.secure_debug_pk),

bl2/src/thin_psa_crypto_core.c

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "psa_crypto_driver_wrappers_no_static.h"
2222
#if defined(MCUBOOT_BUILTIN_KEY)
2323
#include "tfm_plat_crypto_keys.h"
24-
#include "tfm_plat_otp.h"
2524
#endif /* MCUBOOT_BUILTIN_KEY */
2625
#ifdef PSA_WANT_ALG_LMS
2726
#include "mbedtls/lms.h"
@@ -169,6 +168,77 @@ static bool is_key_builtin(psa_key_id_t key_id)
169168
return true;
170169
}
171170

171+
#if defined(MCUBOOT_BUILTIN_KEY)
172+
#ifdef MCUBOOT_ROTPK_SIGN_POLICY
173+
/**
174+
* @brief Retrieve the signing policy for a given key ID.
175+
*
176+
* @details This function fetches the signing policy associated with the
177+
* specified key ID and populates the provided policy structure.
178+
*
179+
* @param[in] key_id The identifier of the key whose signing policy is to
180+
* be retrieved.
181+
* @param[out] policy Pointer to a structure where the signing policy
182+
* information will be stored.
183+
*
184+
* @return PSA_SUCCESS If the policy was successfully retrieved.
185+
* @return PSA_ERROR_DOES_NOT_EXIST If the key ID does not exist.
186+
* @return PSA_ERROR_INVALID_ARGUMENT If the input arguments are invalid.
187+
*/
188+
static psa_status_t get_key_sign_policy(psa_key_id_t key_id,
189+
enum tfm_bl2_key_policy_t *policy)
190+
{
191+
uint32_t policies;
192+
enum tfm_plat_err_t err;
193+
194+
err = tfm_plat_get_bl2_rotpk_policies((uint8_t *)&policies, sizeof(policies));
195+
if (err != TFM_PLAT_ERR_SUCCESS) {
196+
return PSA_ERROR_GENERIC_ERROR;
197+
}
198+
199+
/* Check if the key id bit from the policies is set */
200+
if (policies & (1 << key_id)) {
201+
*policy = TFM_BL2_KEY_MUST_SIGN;
202+
} else {
203+
*policy = TFM_BL2_KEY_MIGHT_SIGN;
204+
}
205+
206+
return PSA_SUCCESS;
207+
}
208+
#else
209+
static inline psa_status_t get_key_sign_policy(psa_key_id_t key,
210+
enum tfm_bl2_key_policy_t *policy)
211+
{
212+
(void)key; /* Unused parameter */
213+
/* By default key policy is a MUST SIGN */
214+
*policy = TFM_BL2_KEY_MUST_SIGN;
215+
216+
return PSA_SUCCESS;
217+
}
218+
#endif /* MCUBOOT_ROTPK_SIGN_POLICY */
219+
220+
int boot_plat_check_key_policy(bool valid_sig, psa_key_id_t key,
221+
bool *key_might_sign, bool *key_must_sign,
222+
uint8_t *key_must_sign_count)
223+
{
224+
enum tfm_bl2_key_policy_t policy;
225+
226+
if (get_key_sign_policy(key, &policy) != PSA_SUCCESS) {
227+
return -1;
228+
}
229+
230+
if (policy == TFM_BL2_KEY_MIGHT_SIGN) {
231+
*key_might_sign |= valid_sig;
232+
} else {
233+
*key_must_sign_count += 1;
234+
*key_might_sign |= valid_sig;
235+
*key_must_sign &= valid_sig;
236+
}
237+
238+
return 0;
239+
}
240+
#endif /* MCUBOOT_BUILTIN_KEY */
241+
172242
/**
173243
* @brief Check in constant time if the \a a buffer matches the \a b
174244
* buffer

platform/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ if(BL2)
230230
BL2
231231
MCUBOOT_${MCUBOOT_UPGRADE_STRATEGY}
232232
$<$<BOOL:${MCUBOOT_DIRECT_XIP_REVERT}>:MCUBOOT_DIRECT_XIP_REVERT>
233+
$<$<BOOL:${MCUBOOT_ROTPK_SIGN_POLICY}>:MCUBOOT_ROTPK_SIGN_POLICY>
233234
$<$<BOOL:${SYMMETRIC_INITIAL_ATTESTATION}>:SYMMETRIC_INITIAL_ATTESTATION>
234235
MCUBOOT_FIH_PROFILE_${MCUBOOT_FIH_PROFILE}
235236
$<$<BOOL:${PLATFORM_DEFAULT_OTP}>:PLATFORM_DEFAULT_OTP>

platform/ext/common/provisioning_bundle/provisioning_bundle.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ __PACKED_STRUCT tfm_psa_rot_provisioning_data_t {
5454
};
5555

5656
__PACKED_STRUCT bl2_assembly_and_test_provisioning_data_t {
57+
#ifdef MCUBOOT_ROTPK_SIGN_POLICY
58+
uint32_t bl2_rotpk_policies;
59+
#endif /* MCUBOOT_ROTPK_SIGN_POLICY */
5760
uint8_t bl2_rotpk_0[PROV_ROTPK_DATA_SIZE];
5861
uint8_t bl2_rotpk_1[PROV_ROTPK_DATA_SIZE];
5962
#if (MCUBOOT_IMAGE_NUMBER > 2)

platform/ext/common/provisioning_bundle/provisioning_code.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ enum tfm_plat_err_t __attribute__((section("DO_PROVISION"))) do_provision(void)
5353
}
5454
#endif /* MCUBOOT_IMAGE_NUMBER > 3 */
5555

56+
#ifdef MCUBOOT_ROTPK_SIGN_POLICY
57+
err = tfm_plat_otp_write(PLAT_OTP_ID_BL2_ROTPK_POLICIES,
58+
sizeof(data.psa_rot_prov_data.bl2_rotpk_policies),
59+
(uint8_t*)&data.psa_rot_prov_data.bl2_rotpk_policies);
60+
if (err != TFM_PLAT_ERR_SUCCESS && err != TFM_PLAT_ERR_UNSUPPORTED) {
61+
return err;
62+
}
63+
#endif /* MCUBOOT_ROTPK_SIGN_POLICY */
64+
5665
#ifdef PLATFORM_PSA_ADAC_SECURE_DEBUG
5766
err = tfm_plat_otp_write(PLAT_OTP_ID_SECURE_DEBUG_PK,
5867
sizeof(data.bl2_assembly_and_test_prov_data.secure_debug_pk),

platform/ext/common/template/flash_otp_nv_counters_backend.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2024, Arm Limited. All rights reserved.
2+
* SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors
33
*
44
* SPDX-License-Identifier: BSD-3-Clause
55
*
@@ -71,6 +71,7 @@ __PACKED_STRUCT flash_otp_nv_counters_region_t {
7171

7272
uint8_t bl2_rotpk_2[BL2_ROTPK_SIZE];
7373
uint8_t bl2_rotpk_3[BL2_ROTPK_SIZE];
74+
uint32_t bl2_rotpk_policies;
7475
#endif /* BL2 */
7576

7677
#ifdef BL1

0 commit comments

Comments
 (0)