From 0accf9832f1348bf40ae69c708dd559f2878f9ae Mon Sep 17 00:00:00 2001 From: David Vincze Date: Wed, 10 Apr 2024 15:29:18 +0200 Subject: [PATCH] BL2: provision raw keys instead of RFC5480 encoded ones Instead of provisioning encoded public keys (as described in RFC5480) provision only the raw key bit string (subjectPublicKey). Signed-off-by: David Vincze Change-Id: Ic94ed76b28f408e78c7d6a6c90057c490fdf0915 --- bl2/src/provisioning.c | 110 ++++++++---------- .../template/flash_otp_nv_counters_backend.h | 34 +++++- platform/ext/common/template/otp_flash.c | 4 +- 3 files changed, 81 insertions(+), 67 deletions(-) diff --git a/bl2/src/provisioning.c b/bl2/src/provisioning.c index fcd7cb097..abe84f6fd 100644 --- a/bl2/src/provisioning.c +++ b/bl2/src/provisioning.c @@ -19,10 +19,10 @@ #ifdef MCUBOOT_SIGN_EC384 #define PUB_KEY_HASH_SIZE (48) -#define PUB_KEY_SIZE (120) +#define PUB_KEY_SIZE (100) /* Size must be aligned to 4 Bytes */ #else #define PUB_KEY_HASH_SIZE (32) -#define PUB_KEY_SIZE (92) /* Size must be aligned to 4 Bytes */ +#define PUB_KEY_SIZE (68) /* Size must be aligned to 4 Bytes */ #endif /* MCUBOOT_SIGN_EC384 */ #ifdef MCUBOOT_BUILTIN_KEY @@ -132,76 +132,66 @@ __PACKED_STRUCT bl2_assembly_and_test_provisioning_data_t { #else /* !MCUBOOT_BUILTIN_KEY */ /* List of BL2 Root of Trust public keys */ #if defined(MCUBOOT_SIGN_EC256) -/* EC-P256 public keys (in SubjectPublicKeyInfo format, see RFC5480) +/* EC-P256 public keys + * (raw keys as subjectPublicKey (bit string), see RFC5480) * DATA_KIND_0: PK(bl2/ext/mcuboot/root-EC-P256.pem) * DATA_KIND_1: PK(bl2/ext/mcuboot/root-EC-P256_1.pem) */ #define ASSEMBLY_AND_TEST_PROV_DATA_KIND_0 { \ - 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, \ - 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, \ - 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, \ - 0x42, 0x00, 0x04, 0x2a, 0xcb, 0x40, 0x3c, 0xe8, \ - 0xfe, 0xed, 0x5b, 0xa4, 0x49, 0x95, 0xa1, 0xa9, \ - 0x1d, 0xae, 0xe8, 0xdb, 0xbe, 0x19, 0x37, 0xcd, \ - 0x14, 0xfb, 0x2f, 0x24, 0x57, 0x37, 0xe5, 0x95, \ - 0x39, 0x88, 0xd9, 0x94, 0xb9, 0xd6, 0x5a, 0xeb, \ - 0xd7, 0xcd, 0xd5, 0x30, 0x8a, 0xd6, 0xfe, 0x48, \ - 0xb2, 0x4a, 0x6a, 0x81, 0x0e, 0xe5, 0xf0, 0x7d, \ - 0x8b, 0x68, 0x34, 0xcc, 0x3a, 0x6a, 0xfc, 0x53, \ - 0x8e, 0xfa, 0xc1, \ + 0x04, 0x2a, 0xcb, 0x40, 0x3c, 0xe8, 0xfe, 0xed, \ + 0x5b, 0xa4, 0x49, 0x95, 0xa1, 0xa9, 0x1d, 0xae, \ + 0xe8, 0xdb, 0xbe, 0x19, 0x37, 0xcd, 0x14, 0xfb, \ + 0x2f, 0x24, 0x57, 0x37, 0xe5, 0x95, 0x39, 0x88, \ + 0xd9, 0x94, 0xb9, 0xd6, 0x5a, 0xeb, 0xd7, 0xcd, \ + 0xd5, 0x30, 0x8a, 0xd6, 0xfe, 0x48, 0xb2, 0x4a, \ + 0x6a, 0x81, 0x0e, 0xe5, 0xf0, 0x7d, 0x8b, 0x68, \ + 0x34, 0xcc, 0x3a, 0x6a, 0xfc, 0x53, 0x8e, 0xfa, \ + 0xc1, \ } #define ASSEMBLY_AND_TEST_PROV_DATA_KIND_1 { \ - 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, \ - 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, \ - 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, \ - 0x42, 0x00, 0x04, 0xe8, 0x09, 0x08, 0x12, 0x3a, \ - 0x0f, 0xad, 0x40, 0xe0, 0x33, 0x8a, 0xa6, 0x54, \ - 0xf8, 0x83, 0x95, 0x41, 0x8e, 0x44, 0x99, 0xa2, \ - 0x0f, 0xae, 0x85, 0x69, 0x2b, 0xf9, 0x26, 0xb5, \ - 0xe9, 0x9e, 0x16, 0x2c, 0x87, 0x76, 0x62, 0x7f, \ - 0x32, 0x6c, 0x9b, 0x70, 0x78, 0x06, 0x52, 0x52, \ - 0x52, 0xca, 0x2b, 0xd2, 0xb7, 0xc7, 0x50, 0x07, \ - 0x66, 0x3b, 0x3b, 0xdf, 0xe1, 0x99, 0x69, 0x00, \ - 0x26, 0x2c, 0x33, \ + 0x04, 0xe8, 0x09, 0x08, 0x12, 0x3a, 0x0f, 0xad, \ + 0x40, 0xe0, 0x33, 0x8a, 0xa6, 0x54, 0xf8, 0x83, \ + 0x95, 0x41, 0x8e, 0x44, 0x99, 0xa2, 0x0f, 0xae, \ + 0x85, 0x69, 0x2b, 0xf9, 0x26, 0xb5, 0xe9, 0x9e, \ + 0x16, 0x2c, 0x87, 0x76, 0x62, 0x7f, 0x32, 0x6c, \ + 0x9b, 0x70, 0x78, 0x06, 0x52, 0x52, 0x52, 0xca, \ + 0x2b, 0xd2, 0xb7, 0xc7, 0x50, 0x07, 0x66, 0x3b, \ + 0x3b, 0xdf, 0xe1, 0x99, 0x69, 0x00, 0x26, 0x2c, \ + 0x33, \ } #elif defined(MCUBOOT_SIGN_EC384) -/* EC-P384 public keys (in SubjectPublicKeyInfo format, see RFC5480) +/* EC-P384 public keys + * (raw keys as subjectPublicKey (bit string), see RFC5480) * DATA_KIND_0: PK(bl2/ext/mcuboot/root-EC-P384.pem) * DATA_KIND_1: PK(bl2/ext/mcuboot/root-EC-P384_1.pem) */ -#define ASSEMBLY_AND_TEST_PROV_DATA_KIND_0 { \ - 0x30, 0x76, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, \ - 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, \ - 0x81, 0x04, 0x00, 0x22, 0x03, 0x62, 0x00, 0x04, \ - 0x0c, 0x76, 0xca, 0xae, 0x72, 0x3a, 0xa5, 0xe8, \ - 0xf0, 0xd4, 0xf1, 0x16, 0xb5, 0x02, 0xef, 0x77, \ - 0xa1, 0x1b, 0x93, 0x61, 0x78, 0xc0, 0x09, 0x26, \ - 0x7b, 0x3b, 0x40, 0x9c, 0xee, 0x49, 0x85, 0xe0, \ - 0xc9, 0x4f, 0xe7, 0xf2, 0xba, 0x97, 0x6c, 0xf3, \ - 0x82, 0x65, 0x14, 0x2c, 0xf5, 0x0c, 0x73, 0x33, \ - 0x4d, 0x32, 0xe7, 0x9b, 0xd3, 0x42, 0xcc, 0x95, \ - 0x5a, 0xe5, 0xe2, 0xf5, 0xf4, 0x6e, 0x45, 0xe0, \ - 0xed, 0x20, 0x35, 0x5c, 0xaf, 0x52, 0x35, 0x81, \ - 0xd4, 0xdc, 0x9c, 0xe3, 0x9e, 0x22, 0x3e, 0xfb, \ - 0x3f, 0x22, 0x10, 0xda, 0x70, 0x03, 0x37, 0xad, \ - 0xa8, 0xf2, 0x48, 0xfe, 0x3a, 0x60, 0x69, 0xa5, \ +#define ASSEMBLY_AND_TEST_PROV_DATA_KIND_0 { 0x04, \ + 0x0c, 0x76, 0xca, 0xae, 0x72, 0x3a, 0xa5, 0xe8, \ + 0xf0, 0xd4, 0xf1, 0x16, 0xb5, 0x02, 0xef, 0x77, \ + 0xa1, 0x1b, 0x93, 0x61, 0x78, 0xc0, 0x09, 0x26, \ + 0x7b, 0x3b, 0x40, 0x9c, 0xee, 0x49, 0x85, 0xe0, \ + 0xc9, 0x4f, 0xe7, 0xf2, 0xba, 0x97, 0x6c, 0xf3, \ + 0x82, 0x65, 0x14, 0x2c, 0xf5, 0x0c, 0x73, 0x33, \ + 0x4d, 0x32, 0xe7, 0x9b, 0xd3, 0x42, 0xcc, 0x95, \ + 0x5a, 0xe5, 0xe2, 0xf5, 0xf4, 0x6e, 0x45, 0xe0, \ + 0xed, 0x20, 0x35, 0x5c, 0xaf, 0x52, 0x35, 0x81, \ + 0xd4, 0xdc, 0x9c, 0xe3, 0x9e, 0x22, 0x3e, 0xfb, \ + 0x3f, 0x22, 0x10, 0xda, 0x70, 0x03, 0x37, 0xad, \ + 0xa8, 0xf2, 0x48, 0xfe, 0x3a, 0x60, 0x69, 0xa5, \ } -#define ASSEMBLY_AND_TEST_PROV_DATA_KIND_1 { \ - 0x30, 0x76, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, \ - 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, \ - 0x81, 0x04, 0x00, 0x22, 0x03, 0x62, 0x00, 0x04, \ - 0x34, 0x43, 0xad, 0x59, 0x83, 0xd9, 0x41, 0x65, \ - 0xdc, 0x20, 0xb8, 0x62, 0x35, 0xf8, 0x7d, 0x94, \ - 0x13, 0x5e, 0x75, 0xe6, 0xa8, 0x79, 0xe9, 0xcb, \ - 0xfd, 0xa7, 0x2e, 0x92, 0x95, 0x82, 0xa6, 0xc5, \ - 0xdd, 0x53, 0xc7, 0x3d, 0x46, 0xed, 0x75, 0xd5, \ - 0x20, 0xb5, 0xbe, 0x74, 0x2a, 0x6d, 0x30, 0xe2, \ - 0x31, 0x50, 0x1c, 0x7f, 0xc7, 0x7b, 0x4a, 0x73, \ - 0x55, 0xf8, 0x92, 0x60, 0xff, 0x2f, 0x18, 0x04, \ - 0xbc, 0xc7, 0xd9, 0xce, 0xda, 0xa6, 0x36, 0x52, \ - 0xec, 0x2b, 0x64, 0x6e, 0x7a, 0x97, 0x60, 0x9d, \ - 0x8c, 0xba, 0xfe, 0xec, 0x9a, 0xb0, 0xc2, 0x6e, \ - 0x3d, 0x75, 0x2a, 0x98, 0xb2, 0xa3, 0x09, 0x84, \ +#define ASSEMBLY_AND_TEST_PROV_DATA_KIND_1 { 0x04, \ + 0x34, 0x43, 0xad, 0x59, 0x83, 0xd9, 0x41, 0x65, \ + 0xdc, 0x20, 0xb8, 0x62, 0x35, 0xf8, 0x7d, 0x94, \ + 0x13, 0x5e, 0x75, 0xe6, 0xa8, 0x79, 0xe9, 0xcb, \ + 0xfd, 0xa7, 0x2e, 0x92, 0x95, 0x82, 0xa6, 0xc5, \ + 0xdd, 0x53, 0xc7, 0x3d, 0x46, 0xed, 0x75, 0xd5, \ + 0x20, 0xb5, 0xbe, 0x74, 0x2a, 0x6d, 0x30, 0xe2, \ + 0x31, 0x50, 0x1c, 0x7f, 0xc7, 0x7b, 0x4a, 0x73, \ + 0x55, 0xf8, 0x92, 0x60, 0xff, 0x2f, 0x18, 0x04, \ + 0xbc, 0xc7, 0xd9, 0xce, 0xda, 0xa6, 0x36, 0x52, \ + 0xec, 0x2b, 0x64, 0x6e, 0x7a, 0x97, 0x60, 0x9d, \ + 0x8c, 0xba, 0xfe, 0xec, 0x9a, 0xb0, 0xc2, 0x6e, \ + 0x3d, 0x75, 0x2a, 0x98, 0xb2, 0xa3, 0x09, 0x84, \ } #endif /* MCUBOOT_SIGN_EC256 */ #endif /* !MCUBOOT_BUILTIN_KEY */ diff --git a/platform/ext/common/template/flash_otp_nv_counters_backend.h b/platform/ext/common/template/flash_otp_nv_counters_backend.h index c7a954d28..8491100c1 100644 --- a/platform/ext/common/template/flash_otp_nv_counters_backend.h +++ b/platform/ext/common/template/flash_otp_nv_counters_backend.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022, Arm Limited. All rights reserved. + * Copyright (c) 2021-2024, Arm Limited. All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause * @@ -17,6 +17,30 @@ extern "C" { #endif #define OTP_NV_COUNTERS_INITIALIZED 0xC0DE8112U +#ifdef BL2 +#if defined(MCUBOOT_SIGN_EC384) +#define BL2_ROTPK_HASH_SIZE (48) +#define BL2_ROTPK_KEY_SIZE (100) /* Aligned to 4 bytes */ +#else +#define BL2_ROTPK_HASH_SIZE (32) +#endif /* MCUBOOT_SIGN_EC384 */ +#if defined(MCUBOOT_SIGN_EC256) +#define BL2_ROTPK_KEY_SIZE (68) /* Aligned to 4 bytes */ +#endif /* MCUBOOT_SIGN_EC256 */ + +#ifdef MCUBOOT_BUILTIN_KEY +#define BL2_ROTPK_SIZE BL2_ROTPK_KEY_SIZE +#else +#define BL2_ROTPK_SIZE BL2_ROTPK_HASH_SIZE +#endif /* MCUBOOT_BUILTIN_KEY */ +#endif /* BL2 */ + +#if (defined(BL2_ROTPK_SIZE) && (BL2_ROTPK_SIZE > 64)) +#define OTP_ELEMENT_SIZE_MAX BL2_ROTPK_SIZE +#else +#define OTP_ELEMENT_SIZE_MAX (64) +#endif + __PACKED_STRUCT flash_otp_nv_counters_region_t { /* Must be the first item */ uint32_t init_value; @@ -37,16 +61,16 @@ __PACKED_STRUCT flash_otp_nv_counters_region_t { uint8_t profile_definition[32]; #ifdef BL2 - uint8_t bl2_rotpk_0[32]; - uint8_t bl2_rotpk_1[32]; + uint8_t bl2_rotpk_0[BL2_ROTPK_SIZE]; + uint8_t bl2_rotpk_1[BL2_ROTPK_SIZE]; uint8_t bl2_nv_counter_0[64]; uint8_t bl2_nv_counter_1[64]; uint8_t bl2_nv_counter_2[64]; uint8_t bl2_nv_counter_3[64]; - uint8_t bl2_rotpk_2[32]; - uint8_t bl2_rotpk_3[32]; + uint8_t bl2_rotpk_2[BL2_ROTPK_SIZE]; + uint8_t bl2_rotpk_3[BL2_ROTPK_SIZE]; #endif /* BL2 */ #ifdef BL1 diff --git a/platform/ext/common/template/otp_flash.c b/platform/ext/common/template/otp_flash.c index 96561426e..ebfc23a0e 100644 --- a/platform/ext/common/template/otp_flash.c +++ b/platform/ext/common/template/otp_flash.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023, Arm Limited. All rights reserved. + * Copyright (c) 2021-2024, Arm Limited. All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause * @@ -134,7 +134,7 @@ static enum tfm_plat_err_t read_from_input(enum tfm_otp_element_id_t id, { enum tfm_plat_err_t err = TFM_PLAT_ERR_SUCCESS; size_t value_size; - uint8_t buffer[64]; + uint8_t buffer[OTP_ELEMENT_SIZE_MAX]; size_t idx; err = tfm_plat_otp_get_size(id, &value_size);