|
21 | 21 | #include "psa_crypto_driver_wrappers_no_static.h"
|
22 | 22 | #if defined(MCUBOOT_BUILTIN_KEY)
|
23 | 23 | #include "tfm_plat_crypto_keys.h"
|
24 |
| -#include "tfm_plat_otp.h" |
25 | 24 | #endif /* MCUBOOT_BUILTIN_KEY */
|
26 | 25 | #ifdef PSA_WANT_ALG_LMS
|
27 | 26 | #include "mbedtls/lms.h"
|
@@ -169,6 +168,77 @@ static bool is_key_builtin(psa_key_id_t key_id)
|
169 | 168 | return true;
|
170 | 169 | }
|
171 | 170 |
|
| 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 | + |
172 | 242 | /**
|
173 | 243 | * @brief Check in constant time if the \a a buffer matches the \a b
|
174 | 244 | * buffer
|
|
0 commit comments