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
1518static 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
4452const int bootutil_key_cnt = 1 ;
53+ #endif /* MCUBOOT_IMAGE_MULTI_SIG_SUPPORT */
54+
55+ #ifndef MCUBOOT_IMAGE_MULTI_SIG_SUPPORT
4556
4657static 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
6677int 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/**
0 commit comments