Skip to content

Commit 82deb79

Browse files
committed
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 15b8991 commit 82deb79

File tree

6 files changed

+75
-58
lines changed

6 files changed

+75
-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: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,69 @@
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 TLV format is additionally defined by:
45+
* EC_PUBK_INDEX -- is the index of shared public key within TLV data stream.
46+
* EC_PUBK_LEN -- is the length, in bytes, of a public key; depends
47+
* selected key exchange.
48+
* EC_TAG_INDEX -- is the HMAC tag of encryption key index within TLV data
49+
* stream.
50+
* EC_TAG_LEN -- is the HMAC tag length.
51+
* EC_CIPHERKEY_INDEX -- is the encryption key index within TLV data stream.
52+
* EC_CIPHERKEY_LEN -- s the length of an encryption key; depends on selected
53+
* encryption.
54+
*
55+
* Note that in case of ECIES, the BOOT_ENC_TLV_SIZE will be defined as
56+
* a sum of EC_*_LEN TLV components, defined for selected key exchange.
57+
*/
3858

3959
#ifdef MCUBOOT_AES_256
40-
#define BOOT_ENC_KEY_SIZE 32
60+
# define BOOT_ENC_KEY_SIZE 32
4161
#else
42-
#define BOOT_ENC_KEY_SIZE 16
62+
# define BOOT_ENC_KEY_SIZE 16
4363
#endif
4464

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-
5265
#if defined(MCUBOOT_ENCRYPT_RSA)
53-
#define BOOT_ENC_TLV_SIZE TLV_ENC_RSA_SZ
66+
# define BOOT_ENC_TLV_SIZE (256)
67+
# define BOOT_ENC_TLV IMAGE_TLV_ENC_RSA2048
5468
#elif defined(MCUBOOT_ENCRYPT_EC256)
55-
#define BOOT_ENC_TLV_SIZE TLV_ENC_EC256_SZ
69+
# define EC_PUBK_INDEX (0)
70+
# define EC_PUBK_LEN (65)
71+
# define EC_TAG_LEN (32)
72+
# define BOOT_ENC_TLV IMAGE_TLV_ENC_EC256
5673
#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
74+
# define EC_PUBK_INDEX (0)
75+
# define EC_PUBK_LEN (32)
76+
# define EC_TAG_LEN (32)
77+
# define BOOT_ENC_TLV IMAGE_TLV_ENC_X25519
78+
#elif defined(MCUBOOT_ENCRYPT_KW)
79+
# define BOOT_ENC_TLV_SIZE (BOOT_ENC_KEY_SIZE + 8)
80+
# define BOOT_ENC_TLV IMAGE_TLV_ENC_KW
81+
#endif
82+
83+
/* Common ECIES definitions */
84+
#if defined(EC_PUBK_LEN)
85+
# define EC_TAG_INDEX (EC_PUBK_INDEX + EC_PUBK_LEN)
86+
# define EC_CIPHERKEY_INDEX (EC_TAG_INDEX + EC_TAG_LEN)
87+
# define EC_CIPHERKEY_LEN BOOT_ENC_KEY_SIZE
88+
# define BOOT_ENC_TLV_SIZE (EC_PUBK_LEN + EC_TAG_LEN + EC_CIPHERKEY_LEN)
6089
#endif
6190

91+
#define BOOT_ENC_KEY_ALIGN_SIZE ALIGN_UP(BOOT_ENC_KEY_SIZE, BOOT_MAX_ALIGN)
92+
#define BOOT_ENC_TLV_ALIGN_SIZE ALIGN_UP(BOOT_ENC_TLV_SIZE, BOOT_MAX_ALIGN)
93+
6294
#ifdef __cplusplus
6395
}
6496
#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
}
@@ -621,7 +599,7 @@ boot_enc_load(struct boot_loader_state *state, int slot,
621599
#if MCUBOOT_SWAP_SAVE_ENCTLV
622600
uint8_t *buf;
623601
#else
624-
uint8_t buf[EXPECTED_ENC_LEN];
602+
uint8_t buf[BOOT_ENC_TLV_SIZE];
625603
#endif
626604
int rc;
627605

@@ -641,7 +619,7 @@ boot_enc_load(struct boot_loader_state *state, int slot,
641619
#endif
642620
#endif
643621

644-
rc = bootutil_tlv_iter_begin(&it, hdr, fap, EXPECTED_ENC_TLV, false);
622+
rc = bootutil_tlv_iter_begin(&it, hdr, fap, BOOT_ENC_TLV, false);
645623
if (rc) {
646624
return -1;
647625
}
@@ -651,7 +629,7 @@ boot_enc_load(struct boot_loader_state *state, int slot,
651629
return rc;
652630
}
653631

654-
if (len != EXPECTED_ENC_LEN) {
632+
if (len != BOOT_ENC_TLV_SIZE) {
655633
return -1;
656634
}
657635

@@ -660,7 +638,7 @@ boot_enc_load(struct boot_loader_state *state, int slot,
660638
memset(buf, 0xff, BOOT_ENC_TLV_ALIGN_SIZE);
661639
#endif
662640

663-
rc = flash_area_read(fap, off, buf, EXPECTED_ENC_LEN);
641+
rc = flash_area_read(fap, off, buf, BOOT_ENC_TLV_SIZE);
664642
if (rc) {
665643
return -1;
666644
}

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)