Skip to content

Commit 454cae8

Browse files
committed
bootutil: Remove BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE
BOOT_ENC_KEY_SIZE is enough. BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE has been replaced with BOOT_ENC_BLOCK_SIZE. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 2367a60 commit 454cae8

File tree

4 files changed

+27
-32
lines changed

4 files changed

+27
-32
lines changed

boot/boot_serial/src/boot_serial_encryption.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "bootutil/bootutil_log.h"
1212
#include "bootutil/bootutil_public.h"
1313
#include "bootutil/fault_injection_hardening.h"
14-
#include "bootutil/enc_key.h"
1514

1615
#include "mcuboot_config/mcuboot_config.h"
1716

boot/bootutil/include/bootutil/crypto/aes_ctr.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,27 @@
1919
#error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT or PSA"
2020
#endif
2121

22+
#include "bootutil/enc_key_public.h"
23+
2224
#if defined(MCUBOOT_USE_MBED_TLS)
2325
#include <mbedtls/aes.h>
24-
#include "bootutil/enc_key_public.h"
25-
#define BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE BOOT_ENC_KEY_SIZE
26-
#define BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE (16)
26+
#define BOOT_ENC_BLOCK_SIZE (16)
2727
#endif /* MCUBOOT_USE_MBED_TLS */
2828

2929
#if defined(MCUBOOT_USE_TINYCRYPT)
30-
#if defined(MCUBOOT_AES_256)
31-
#error "Cannot use AES-256 for encryption with Tinycrypt library."
32-
#endif
3330
#include <string.h>
3431
#include <tinycrypt/aes.h>
3532
#include <tinycrypt/ctr_mode.h>
3633
#include <tinycrypt/constants.h>
37-
#define BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE TC_AES_KEY_SIZE
38-
#define BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE TC_AES_BLOCK_SIZE
34+
#if defined(MCUBOOT_AES_256) || (BOOT_ENC_KEY_SIZE != TC_AES_KEY_SIZE)
35+
#error "Cannot use AES-256 for encryption with Tinycrypt library."
36+
#endif
37+
#define BOOT_ENC_BLOCK_SIZE TC_AES_BLOCK_SIZE
3938
#endif /* MCUBOOT_USE_TINYCRYPT */
4039

4140
#if defined(MCUBOOT_USE_PSA_CRYPTO)
4241
#include <psa/crypto.h>
43-
#include "bootutil/enc_key_public.h"
44-
#define BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE BOOT_ENC_KEY_SIZE
45-
#define BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE (16)
42+
#define BOOT_ENC_BLOCK_SIZE (16)
4643
#endif
4744

4845
#include <stdint.h>
@@ -91,18 +88,18 @@ static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
9188

9289
static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
9390
{
94-
return mbedtls_aes_setkey_enc(ctx, k, BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE * 8);
91+
return mbedtls_aes_setkey_enc(ctx, k, BOOT_ENC_KEY_SIZE * 8);
9592
}
9693

9794
static inline int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *m, uint32_t mlen, size_t blk_off, uint8_t *c)
9895
{
99-
uint8_t stream_block[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE];
96+
uint8_t stream_block[BOOT_ENC_BLOCK_SIZE];
10097
return mbedtls_aes_crypt_ctr(ctx, mlen, &blk_off, counter, stream_block, m, c);
10198
}
10299

103100
static inline int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *c, uint32_t clen, size_t blk_off, uint8_t *m)
104101
{
105-
uint8_t stream_block[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE];
102+
uint8_t stream_block[BOOT_ENC_BLOCK_SIZE];
106103
return mbedtls_aes_crypt_ctr(ctx, clen, &blk_off, counter, stream_block, c, m);
107104
}
108105
#endif /* MCUBOOT_USE_MBED_TLS */

boot/bootutil/src/encrypted.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,11 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
422422
bootutil_aes_ctr_context aes_ctr;
423423
uint8_t tag[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
424424
uint8_t shared[SHARED_KEY_LEN];
425-
uint8_t derived_key[BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
425+
uint8_t derived_key[BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
426426
uint8_t *cp;
427427
uint8_t *cpend;
428428
uint8_t private_key[PRIV_KEY_LEN];
429-
uint8_t counter[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE];
429+
uint8_t counter[BOOT_ENC_BLOCK_SIZE];
430430
uint16_t len;
431431
#endif
432432
struct bootutil_key *bootutil_enc_key = NULL;
@@ -530,10 +530,10 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
530530
* Expand shared secret to create keys for AES-128-CTR + HMAC-SHA256
531531
*/
532532

533-
len = BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE;
533+
len = BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE;
534534
rc = hkdf(shared, SHARED_KEY_LEN, (uint8_t *)"MCUBoot_ECIES_v1", 16,
535535
derived_key, &len);
536-
if (rc != 0 || len != (BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE)) {
536+
if (rc != 0 || len != (BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE)) {
537537
return -1;
538538
}
539539

@@ -585,8 +585,8 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
585585
return -1;
586586
}
587587

588-
memset(counter, 0, BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE);
589-
rc = bootutil_aes_ctr_decrypt(&aes_ctr, counter, &buf[EC_CIPHERKEY_INDEX], BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE, 0, enckey);
588+
memset(counter, 0, BOOT_ENC_BLOCK_SIZE);
589+
rc = bootutil_aes_ctr_decrypt(&aes_ctr, counter, &buf[EC_CIPHERKEY_INDEX], BOOT_ENC_KEY_SIZE, 0, enckey);
590590
if (rc != 0) {
591591
bootutil_aes_ctr_drop(&aes_ctr);
592592
return -1;

boot/bootutil/src/encrypted_psa.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ extern const struct bootutil_key bootutil_enc_key;
114114
int
115115
boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
116116
{
117-
uint8_t derived_key[BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
117+
uint8_t derived_key[BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
118118
uint8_t *cp;
119119
uint8_t *cpend;
120120
uint8_t private_key[PRIV_KEY_LEN];
@@ -134,7 +134,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
134134
* the beginning of the input buffer.
135135
*/
136136
uint8_t iv_and_key[PSA_CIPHER_IV_LENGTH(PSA_KEY_TYPE_AES, PSA_ALG_CTR) +
137-
BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE];
137+
BOOT_ENC_KEY_SIZE];
138138

139139
psa_ret = psa_crypto_init();
140140
if (psa_ret != PSA_SUCCESS) {
@@ -208,7 +208,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
208208
return -1;
209209
}
210210

211-
len = BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE;
211+
len = BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE;
212212
psa_ret = psa_key_derivation_output_bytes(&key_do, derived_key, len);
213213
psa_cleanup_ret = psa_key_derivation_abort(&key_do);
214214
if (psa_cleanup_ret != PSA_SUCCESS) {
@@ -219,7 +219,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
219219
return -1;
220220
}
221221

222-
/* The derived key consists of BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE bytes
222+
/* The derived key consists of BOOT_ENC_KEY_SIZE bytes
223223
* followed by BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE bytes. Both parts will
224224
* be imported at the point where needed and discarded immediately after.
225225
*/
@@ -228,11 +228,11 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
228228
psa_set_key_algorithm(&kattr, PSA_ALG_HMAC(PSA_ALG_SHA_256));
229229

230230
/* Import the MAC tag key part of derived key, that is the part that starts
231-
* after BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE and has length of
231+
* after BOOT_ENC_KEY_SIZE and has length of
232232
* BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE bytes.
233233
*/
234234
psa_ret = psa_import_key(&kattr,
235-
&derived_key[BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE],
235+
&derived_key[BOOT_ENC_KEY_SIZE],
236236
BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE, &kid);
237237
psa_reset_key_attributes(&kattr);
238238
if (psa_ret != PSA_SUCCESS) {
@@ -262,8 +262,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
262262
psa_set_key_algorithm(&kattr, PSA_ALG_CTR);
263263

264264
/* Import the AES partition of derived key, the first 16 bytes */
265-
psa_ret = psa_import_key(&kattr, &derived_key[0],
266-
BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE, &kid);
265+
psa_ret = psa_import_key(&kattr, &derived_key[0], BOOT_ENC_KEY_SIZE, &kid);
267266
memset(derived_key, 0, sizeof(derived_key));
268267
if (psa_ret != PSA_SUCCESS) {
269268
BOOT_LOG_ERR("AES key import failed %d", psa_ret);
@@ -279,14 +278,14 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
279278

280279
len = 0;
281280
psa_ret = psa_cipher_decrypt(kid, PSA_ALG_CTR, iv_and_key, sizeof(iv_and_key),
282-
enckey, BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE, &len);
281+
enckey, BOOT_ENC_KEY_SIZE, &len);
283282
memset(iv_and_key, 0, sizeof(iv_and_key));
284283
psa_cleanup_ret = psa_destroy_key(kid);
285284
if (psa_cleanup_ret != PSA_SUCCESS) {
286285
BOOT_LOG_WRN("AES key destruction failed %d", psa_cleanup_ret);
287286
}
288-
if (psa_ret != PSA_SUCCESS || len != BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE) {
289-
memset(enckey, 0, BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE);
287+
if (psa_ret != PSA_SUCCESS || len != BOOT_ENC_KEY_SIZE) {
288+
memset(enckey, 0, BOOT_ENC_KEY_SIZE);
290289
BOOT_LOG_ERR("Random key decryption failed %d", psa_ret);
291290
return -1;
292291
}

0 commit comments

Comments
 (0)