Skip to content

Commit e3a271c

Browse files
de-nordicnvlsianpu
authored andcommitted
bootutil: Move all encryption TLV helper identifiers into one place
Make enc_key_public.h single point of definitions for key sizes, TLV indexes and so on. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 68a4c96 commit e3a271c

File tree

6 files changed

+86
-58
lines changed

6 files changed

+86
-58
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* Copyright (c) 2025 Nordic Semiconductor ASA
5+
*
6+
*/
7+
8+
#ifndef H_BOOTUTIL_MACROS
9+
#define H_BOOTUTIL_MACROS
10+
11+
#ifndef ALIGN_UP
12+
#define ALIGN_UP(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
13+
#endif
14+
15+
#ifndef ALIGN_DOWN
16+
#define ALIGN_DOWN(num, align) ((num) & ~((align) - 1))
17+
#endif
18+
19+
#endif

boot/bootutil/include/bootutil/crypto/rsa.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ static int bootutil_rsa_oaep_decrypt(
100100
return -1;
101101
}
102102
size_t input_size = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attr));
103-
if (input_size != TLV_ENC_RSA_SZ) {
103+
if (input_size != BOOT_ENC_TLV_SIZE) {
104104
return -1;
105105
}
106106

107107
status = psa_asymmetric_decrypt(ctx->key_id, PSA_ALG_RSA_OAEP(PSA_ALG_SHA_256),
108-
input, TLV_ENC_RSA_SZ, NULL, 0,
108+
input, BOOT_ENC_TLV_SIZE, NULL, 0,
109109
output, output_max_len, olen);
110110
return (int)status;
111111
}

boot/bootutil/include/bootutil/enc_key.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
extern "C" {
4040
#endif
4141

42-
#define BOOT_ENC_TLV_ALIGN_SIZE ALIGN_UP(BOOT_ENC_TLV_SIZE, BOOT_MAX_ALIGN)
43-
4442
struct enc_key_data {
4543
uint8_t valid;
4644
bootutil_aes_ctr_context aes_ctr;

boot/bootutil/include/bootutil/enc_key_public.h

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,80 @@
2828
#ifndef BOOTUTIL_ENC_KEY_PUBLIC_H
2929
#define BOOTUTIL_ENC_KEY_PUBLIC_H
3030
#include <mcuboot_config/mcuboot_config.h>
31+
#include <bootutil/bootutil_macros.h>
32+
3133
#ifdef __cplusplus
3234
extern "C" {
3335
#endif
3436

35-
#ifndef ALIGN_UP
36-
#define ALIGN_UP(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
37-
#endif
37+
/* The unit provides following system wide definitions:
38+
* BOOT_ENC_TLV_SIZE -- is the complete size of TLV with encryption data.
39+
* BOOT_ENC_TLV -- is the encryption TLV type, should be given value
40+
* of one of IMAGE_TVL_ENC_ identifiers.
41+
* BOOT_ENC_KEY_SIZE -- is the encryption key size; this includes portion
42+
* of TLV data stream taken by key.
43+
*
44+
* For ECIES based key exchange there is additionally provided:
45+
* EC_PUBK_LEN -- is the length, in bytes, of a public key; depends
46+
* selected key exchange.
47+
* EC_PRIVK_LEN -- is the length, in bytes, of a private key; depends
48+
* on selected key exchange.
49+
* EC_SHARED_LEN -- is the length, in bytes, of a shared key resulting
50+
* from processing of private and public key; depends
51+
* on selected key exchange parameters.
52+
*
53+
* ECIES TLV processing uses following TLVs, from this header:
54+
* EC_TAG_INDEX -- is the HMAC tag of encryption key index within TLV data
55+
* stream.
56+
* EC_TAG_LEN -- is the HMAC tag length.
57+
* EC_PUBK_INDEX -- is the index of shared public key within TLV data stream;
58+
* EC_PUBK_LEN represents length in bytes.
59+
* EC_CIPHERKEY_INDEX -- is the encryption key index within TLV data stream.
60+
* EC_CIPHERKEY_LEN -- is the length of an encryption key; depends on selected
61+
* encryption.
62+
*
63+
* Note that in case of ECIES, the BOOT_ENC_TLV_SIZE will be defined as
64+
* a sum of EC_*_LEN TLV components, defined for selected key exchange.
65+
*/
3866

3967
#ifdef MCUBOOT_AES_256
40-
#define BOOT_ENC_KEY_SIZE 32
68+
# define BOOT_ENC_KEY_SIZE 32
4169
#else
42-
#define BOOT_ENC_KEY_SIZE 16
70+
# define BOOT_ENC_KEY_SIZE 16
4371
#endif
4472

45-
#define BOOT_ENC_KEY_ALIGN_SIZE ALIGN_UP(BOOT_ENC_KEY_SIZE, BOOT_MAX_ALIGN)
46-
47-
#define TLV_ENC_RSA_SZ 256
48-
#define TLV_ENC_KW_SZ (BOOT_ENC_KEY_SIZE + 8)
49-
#define TLV_ENC_EC256_SZ (65 + 32 + BOOT_ENC_KEY_SIZE)
50-
#define TLV_ENC_X25519_SZ (32 + 32 + BOOT_ENC_KEY_SIZE)
51-
5273
#if defined(MCUBOOT_ENCRYPT_RSA)
53-
#define BOOT_ENC_TLV_SIZE TLV_ENC_RSA_SZ
74+
# define BOOT_ENC_TLV_SIZE (256)
75+
# define BOOT_ENC_TLV IMAGE_TLV_ENC_RSA2048
5476
#elif defined(MCUBOOT_ENCRYPT_EC256)
55-
#define BOOT_ENC_TLV_SIZE TLV_ENC_EC256_SZ
77+
# define EC_PUBK_LEN (65)
78+
# define EC_PRIVK_LEN (32)
79+
# define EC_SHARED_LEN (32)
80+
# define BOOT_ENC_TLV IMAGE_TLV_ENC_EC256
5681
#elif defined(MCUBOOT_ENCRYPT_X25519)
57-
#define BOOT_ENC_TLV_SIZE TLV_ENC_X25519_SZ
58-
#else
59-
#define BOOT_ENC_TLV_SIZE TLV_ENC_KW_SZ
82+
# define EC_PUBK_LEN (32)
83+
# define EC_PRIVK_LEN (32)
84+
# define EC_SHARED_LEN (32)
85+
# define BOOT_ENC_TLV IMAGE_TLV_ENC_X25519
86+
#elif defined(MCUBOOT_ENCRYPT_KW)
87+
# define BOOT_ENC_TLV_SIZE (BOOT_ENC_KEY_SIZE + 8)
88+
# define BOOT_ENC_TLV IMAGE_TLV_ENC_KW
6089
#endif
6190

91+
/* Common ECIES definitions */
92+
#if defined(EC_PUBK_LEN)
93+
# define EC_PUBK_INDEX (0)
94+
# define EC_TAG_LEN (32)
95+
# define EC_TAG_INDEX (EC_PUBK_INDEX + EC_PUBK_LEN)
96+
# define EC_CIPHERKEY_INDEX (EC_TAG_INDEX + EC_TAG_LEN)
97+
# define EC_CIPHERKEY_LEN BOOT_ENC_KEY_SIZE
98+
# define EC_SHARED_KEY_LEN (32)
99+
# define BOOT_ENC_TLV_SIZE (EC_PUBK_LEN + EC_TAG_LEN + EC_CIPHERKEY_LEN)
100+
#endif
101+
102+
#define BOOT_ENC_KEY_ALIGN_SIZE ALIGN_UP(BOOT_ENC_KEY_SIZE, BOOT_MAX_ALIGN)
103+
#define BOOT_ENC_TLV_ALIGN_SIZE ALIGN_UP(BOOT_ENC_TLV_SIZE, BOOT_MAX_ALIGN)
104+
62105
#ifdef __cplusplus
63106
}
64107
#endif

boot/bootutil/src/encrypted.c

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,6 @@
4646

4747
#include "bootutil_priv.h"
4848

49-
#define EXPECTED_ENC_LEN BOOT_ENC_TLV_SIZE
50-
51-
#if defined(MCUBOOT_ENCRYPT_RSA)
52-
# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_RSA2048
53-
#elif defined(MCUBOOT_ENCRYPT_KW)
54-
# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_KW
55-
#elif defined(MCUBOOT_ENCRYPT_EC256)
56-
# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_EC256
57-
# define EC_PUBK_INDEX (0)
58-
# define EC_TAG_INDEX (65)
59-
# define EC_CIPHERKEY_INDEX (65 + 32)
60-
_Static_assert(EC_CIPHERKEY_INDEX + BOOT_ENC_KEY_SIZE == EXPECTED_ENC_LEN,
61-
"Please fix ECIES-P256 component indexes");
62-
#elif defined(MCUBOOT_ENCRYPT_X25519)
63-
# define EXPECTED_ENC_TLV IMAGE_TLV_ENC_X25519
64-
# define EC_PUBK_INDEX (0)
65-
# define EC_TAG_INDEX (32)
66-
# define EC_CIPHERKEY_INDEX (32 + 32)
67-
_Static_assert(EC_CIPHERKEY_INDEX + BOOT_ENC_KEY_SIZE == EXPECTED_ENC_LEN,
68-
"Please fix ECIES-X25519 component indexes");
69-
#endif
70-
7149
/* NOUP Fixme: */
7250
#if !defined(CONFIG_BOOT_ED25519_PSA)
7351
#if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519)
@@ -104,7 +82,7 @@ key_unwrap(const uint8_t *wrapped, uint8_t *enckey, struct bootutil_key *bootuti
10482
if (rc != 0) {
10583
goto done;
10684
}
107-
rc = bootutil_aes_kw_unwrap(&aes_kw, wrapped, TLV_ENC_KW_SZ, enckey, BOOT_ENC_KEY_SIZE);
85+
rc = bootutil_aes_kw_unwrap(&aes_kw, wrapped, BOOT_ENC_TLV_SIZE, enckey, BOOT_ENC_KEY_SIZE);
10886
if (rc != 0) {
10987
goto done;
11088
}
@@ -618,7 +596,7 @@ boot_enc_load(struct boot_loader_state *state, int slot,
618596
#if MCUBOOT_SWAP_SAVE_ENCTLV
619597
uint8_t *buf;
620598
#else
621-
uint8_t buf[EXPECTED_ENC_LEN];
599+
uint8_t buf[BOOT_ENC_TLV_SIZE];
622600
#endif
623601
int rc;
624602

@@ -638,7 +616,7 @@ boot_enc_load(struct boot_loader_state *state, int slot,
638616
#endif
639617
#endif
640618

641-
rc = bootutil_tlv_iter_begin(&it, hdr, fap, EXPECTED_ENC_TLV, false);
619+
rc = bootutil_tlv_iter_begin(&it, hdr, fap, BOOT_ENC_TLV, false);
642620
if (rc) {
643621
return -1;
644622
}
@@ -648,7 +626,7 @@ boot_enc_load(struct boot_loader_state *state, int slot,
648626
return rc;
649627
}
650628

651-
if (len != EXPECTED_ENC_LEN) {
629+
if (len != BOOT_ENC_TLV_SIZE) {
652630
return -1;
653631
}
654632

@@ -657,7 +635,7 @@ boot_enc_load(struct boot_loader_state *state, int slot,
657635
memset(buf, 0xff, BOOT_ENC_TLV_ALIGN_SIZE);
658636
#endif
659637

660-
rc = flash_area_read(fap, off, buf, EXPECTED_ENC_LEN);
638+
rc = flash_area_read(fap, off, buf, BOOT_ENC_TLV_SIZE);
661639
if (rc) {
662640
return -1;
663641
}

boot/bootutil/src/encrypted_psa.c

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

2828
BOOT_LOG_MODULE_DECLARE(mcuboot_psa_enc);
2929

30-
#define EXPECTED_ENC_LEN BOOT_ENC_TLV_SIZE
31-
#define EC_PUBK_INDEX (0)
32-
#define EC_PUBK_LEN (32)
33-
#define EC_TAG_INDEX (EC_PUBK_INDEX + EC_PUBK_LEN)
34-
#define EC_TAG_LEN (32)
35-
#define EC_CIPHERKEY_INDEX (EC_TAG_INDEX + EC_TAG_LEN)
36-
#define EC_CIPHERKEY_LEN BOOT_ENC_KEY_SIZE
37-
_Static_assert(EC_CIPHERKEY_INDEX + BOOT_ENC_KEY_SIZE == EXPECTED_ENC_LEN,
38-
"Please fix ECIES-X25519 component indexes");
39-
4030
#define X25519_OID "\x6e"
4131
static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG \
4232
MBEDTLS_OID_ORG_GOV X25519_OID;

0 commit comments

Comments
 (0)