Skip to content
Draft
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
115c7bc
[nrf fromtree] imgtool: support producing images in test mode
danieldegrasse Sep 27, 2025
530f60f
[nrf fromtree] bootutil: Refactor boot_read_enc_key
de-nordic Oct 21, 2025
00656da
[nrf fromtree] bootutil: In boot_swap_image add return check from boo…
de-nordic Oct 21, 2025
278e1f7
[nrf fromtree] boot: zephyr: Allow disabling default multiple RAM reg…
nordicjm Oct 15, 2025
1f0ad35
[nrf fromtree] boot: zephyr: Remove weird custom key directory handling
thedjnK Nov 29, 2025
1825735
[nrf fromtree] boot: zephyr: kconfig: Only show signature file when n…
thedjnK Nov 29, 2025
014e1e0
[nrf fromtree] bootutil: Split header with encryption functions
de-nordic Oct 24, 2025
728deab
[nrf fromtree] bootutil: Allow using psa_key_id_t in AES crypto context
de-nordic Oct 28, 2025
a04481e
[nrf fromtree] bootutil: Add boot_loader_state_init
de-nordic Oct 24, 2025
8215f7b
[nrf fromtree] boot_serial: Initialize state with boot_state_init
de-nordic Oct 28, 2025
4cf9c00
[nrf fromtree] bootutil: Use boot_state_init instead of boot_state_clear
de-nordic Oct 28, 2025
53e3ad3
[nrf fromtree] bootutil: Remove NULL state logic from boot_state_clear
de-nordic Oct 28, 2025
6a8c5fc
[nrf fromtree] bootutil: Fix encryption context de initialization in …
de-nordic Oct 28, 2025
0812a81
[nrf fromtree] bootutil: Temporarly drop mem cleanup from boot_state_…
de-nordic Nov 12, 2025
d8bcee0
[nrf noup] imgtool: Add support for encrypting image with raw AES key
de-nordic Oct 16, 2025
b37f991
[nrf noup] bootutil: Provide support for embedded AES keys
de-nordic Oct 16, 2025
f308c37
[nrf noup] zephyr: Add support for embedded AES key
de-nordic Oct 21, 2025
894ec54
[nrf noup] bootutil: Use boot_state_init/clear in loader manifest
de-nordic Dec 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion boot/boot_serial/src/boot_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ bs_list_set(uint8_t op, char *buf, int len)
bool area_opened = false;

state = boot_get_loader_state();
boot_state_clear(state);
boot_state_init(state);

rc = boot_open_all_flash_areas(state);
if (rc != 0) {
Expand Down Expand Up @@ -696,6 +696,7 @@ bs_list_set(uint8_t op, char *buf, int len)
if (area_opened) {
boot_close_all_flash_areas(state);
}
boot_state_clear(state);

if (rc != 0) {
bs_rc_rsp(rc);
Expand Down
8 changes: 8 additions & 0 deletions boot/boot_serial/src/boot_serial_encryption.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ boot_image_validate_encrypted(struct boot_loader_state *state,
int rc;

if (MUST_DECRYPT(fa_p, BOOT_CURR_IMG(state), hdr)) {
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
rc = boot_en_take_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CUR_IMG(state), BOOT_SLOT_SECONDARY);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't that be boot_take_enc_key?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh. I can not figure out how come I keep on bringing this bug. I thought that I have already fixed this two times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is in upstream again. I think I have messed up moving commits. Need to look through what ales get broken.

#else
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY, hdr, fa_p, bs);
#endif
if (rc < 0) {
FIH_RET(fih_rc);
}
Expand Down Expand Up @@ -235,7 +239,11 @@ decrypt_image_inplace(const struct flash_area *fa_p,
#endif
memset(&boot_data, 0, sizeof(struct boot_loader_state));
/* Load the encryption keys into cache */
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_PRIMARY], BOOT_CURR_IMG(state), BOOT_SLOT_PRIMARY);
#else
rc = boot_enc_load(state, BOOT_SLOT_PRIMARY, hdr, fa_p, bs);
#endif
if (rc < 0) {
FIH_RET(fih_rc);
}
Expand Down
7 changes: 7 additions & 0 deletions boot/bootutil/include/bootutil/bootutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ fih_ret context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp);
*/
struct boot_loader_state *boot_get_loader_state(void);

/**
* Initialize boot_loader_state object
*
* @param state Bootloader state.
*/
void boot_state_init(struct boot_loader_state *state);

#if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO) || defined(MCUBOOT_DATA_SHARING)
/**
* Returns pointer to array of image maximum sizes.
Expand Down
128 changes: 3 additions & 125 deletions boot/bootutil/include/bootutil/crypto/aes_ctr.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,145 +10,23 @@
#ifndef __BOOTUTIL_CRYPTO_AES_CTR_H_
#define __BOOTUTIL_CRYPTO_AES_CTR_H_

#include <string.h>

#include "mcuboot_config/mcuboot_config.h"

#if (defined(MCUBOOT_USE_MBED_TLS) + \
defined(MCUBOOT_USE_TINYCRYPT) + defined(MCUBOOT_USE_PSA_CRYPTO)) != 1
#error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT or PSA"
#endif

#include "bootutil/enc_key_public.h"

#if defined(MCUBOOT_USE_MBED_TLS)
#include <mbedtls/aes.h>
#define BOOT_ENC_BLOCK_SIZE (16)
#include "bootutil/crypto/aes_ctr_mbedtls.h"
#endif /* MCUBOOT_USE_MBED_TLS */

#if defined(MCUBOOT_USE_TINYCRYPT)
#include <string.h>
#include <tinycrypt/aes.h>
#include <tinycrypt/ctr_mode.h>
#include <tinycrypt/constants.h>
#if defined(MCUBOOT_AES_256) || (BOOT_ENC_KEY_SIZE != TC_AES_KEY_SIZE)
#error "Cannot use AES-256 for encryption with Tinycrypt library."
#endif
#define BOOT_ENC_BLOCK_SIZE TC_AES_BLOCK_SIZE
#include "bootutil/crypto/aes_ctr_tinycrypt.h"
#endif /* MCUBOOT_USE_TINYCRYPT */

#if defined(MCUBOOT_USE_PSA_CRYPTO)
#include <psa/crypto.h>
#define BOOT_ENC_BLOCK_SIZE (16)
#endif

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#if defined(MCUBOOT_USE_PSA_CRYPTO)
typedef struct {
/* Fixme: This should not be, here, psa_key_id should be passed */
uint8_t key[BOOT_ENC_KEY_SIZE];
} bootutil_aes_ctr_context;

void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx);

static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
{
memset(ctx, 0, sizeof(*ctx));
}

static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
{
memcpy(ctx->key, k, sizeof(ctx->key));

return 0;
}

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);
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);
#endif

#if defined(MCUBOOT_USE_MBED_TLS)
typedef mbedtls_aes_context bootutil_aes_ctr_context;
static inline void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx)
{
(void)mbedtls_aes_init(ctx);
}

static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
{
mbedtls_aes_free(ctx);
}

static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
{
return mbedtls_aes_setkey_enc(ctx, k, BOOT_ENC_KEY_SIZE * 8);
}

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)
{
uint8_t stream_block[BOOT_ENC_BLOCK_SIZE];
return mbedtls_aes_crypt_ctr(ctx, mlen, &blk_off, counter, stream_block, m, c);
}

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)
{
uint8_t stream_block[BOOT_ENC_BLOCK_SIZE];
return mbedtls_aes_crypt_ctr(ctx, clen, &blk_off, counter, stream_block, c, m);
}
#endif /* MCUBOOT_USE_MBED_TLS */

#if defined(MCUBOOT_USE_TINYCRYPT)
typedef struct tc_aes_key_sched_struct bootutil_aes_ctr_context;
static inline void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx)
{
(void)ctx;
}

static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
{
(void)ctx;
}

static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
{
int rc;
rc = tc_aes128_set_encrypt_key(ctx, k);
if (rc != TC_CRYPTO_SUCCESS) {
return -1;
}
return 0;
}

static int _bootutil_aes_ctr_crypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *in, uint32_t inlen, uint32_t blk_off, uint8_t *out)
{
int rc;
rc = tc_ctr_mode(out, inlen, in, inlen, counter, &blk_off, ctx);
if (rc != TC_CRYPTO_SUCCESS) {
return -1;
}
return 0;
}

static inline int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *m, uint32_t mlen, uint32_t blk_off, uint8_t *c)
{
return _bootutil_aes_ctr_crypt(ctx, counter, m, mlen, blk_off, c);
}

static inline int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *c, uint32_t clen, uint32_t blk_off, uint8_t *m)
{
return _bootutil_aes_ctr_crypt(ctx, counter, c, clen, blk_off, m);
}
#endif /* MCUBOOT_USE_TINYCRYPT */

#ifdef __cplusplus
}
#include "bootutil/crypto/aes_ctr_psa.h"
#endif

#endif /* __BOOTUTIL_CRYPTO_AES_CTR_H_ */
58 changes: 58 additions & 0 deletions boot/bootutil/include/bootutil/crypto/aes_ctr_mbedtls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* This module provides a thin abstraction over some of the crypto
* primitives to make it easier to swap out the used crypto library.
*
* At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or
* MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly
* one of these defined.
*/

#ifndef __BOOTUTIL_CRYPTO_AES_CTR_MBEDTLS_H_
#define __BOOTUTIL_CRYPTO_AES_CTR_MBEDTLS_H_

#include <string.h>
#include <stdint.h>
#include "mcuboot_config/mcuboot_config.h"
#include "bootutil/enc_key_public.h"
#include <mbedtls/aes.h>

#define BOOT_ENC_BLOCK_SIZE (16)

#ifdef __cplusplus
extern "C" {
#endif

typedef mbedtls_aes_context bootutil_aes_ctr_context;

static inline void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx)
{
(void)mbedtls_aes_init(ctx);
}

static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
{
mbedtls_aes_free(ctx);
}

static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
{
return mbedtls_aes_setkey_enc(ctx, k, BOOT_ENC_KEY_SIZE * 8);
}

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)
{
uint8_t stream_block[BOOT_ENC_BLOCK_SIZE];
return mbedtls_aes_crypt_ctr(ctx, mlen, &blk_off, counter, stream_block, m, c);
}

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)
{
uint8_t stream_block[BOOT_ENC_BLOCK_SIZE];
return mbedtls_aes_crypt_ctr(ctx, clen, &blk_off, counter, stream_block, c, m);
}

#ifdef __cplusplus
}
#endif

#endif /* __BOOTUTIL_CRYPTO_AES_CTR_MBEDTLS_H_ */
41 changes: 41 additions & 0 deletions boot/bootutil/include/bootutil/crypto/aes_ctr_psa.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This module provides a thin abstraction over some of the crypto
* primitives to make it easier to swap out the used crypto library.
*
* At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or
* MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly
* one of these defined.
*/

#ifndef __BOOTUTIL_CRYPTO_AES_CTR_PSA_H_
#define __BOOTUTIL_CRYPTO_AES_CTR_PSA_H_

#include <string.h>
#include <stdint.h>
#include "mcuboot_config/mcuboot_config.h"
#include "bootutil/enc_key_public.h"
#include <psa/crypto.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
psa_key_id_t key;
} bootutil_aes_ctr_context;

void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be great to see some docs for these functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in commit directly taken from upstream.


void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx);
int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k);

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);
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);

#ifdef __cplusplus
}
#endif

#endif /* __BOOTUTIL_CRYPTO_AES_CTR_H_ */
77 changes: 77 additions & 0 deletions boot/bootutil/include/bootutil/crypto/aes_ctr_tinycrypt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* This module provides a thin abstraction over some of the crypto
* primitives to make it easier to swap out the used crypto library.
*
* At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or
* MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly
* one of these defined.
*/

#ifndef __BOOTUTIL_CRYPTO_AES_CTR_TINYCRYPT_H_
#define __BOOTUTIL_CRYPTO_AES_CTR_TINYCRYPT_H_

#include <string.h>
#include <stdint.h>
#include "mcuboot_config/mcuboot_config.h"
#include "bootutil/enc_key_public.h"

#include <tinycrypt/aes.h>
#include <tinycrypt/ctr_mode.h>
#include <tinycrypt/constants.h>

#if defined(MCUBOOT_AES_256) || (BOOT_ENC_KEY_SIZE != TC_AES_KEY_SIZE)
#error "Cannot use AES-256 for encryption with Tinycrypt library."
#endif

#define BOOT_ENC_BLOCK_SIZE TC_AES_BLOCK_SIZE

#ifdef __cplusplus
extern "C" {
#endif

typedef struct tc_aes_key_sched_struct bootutil_aes_ctr_context;
static inline void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx)
{
(void)ctx;
}

static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
{
(void)ctx;
}

static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
{
int rc;
rc = tc_aes128_set_encrypt_key(ctx, k);
if (rc != TC_CRYPTO_SUCCESS) {
return -1;
}
return 0;
}

static int common_bootutil_aes_ctr_crypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *in, uint32_t inlen, uint32_t blk_off, uint8_t *out)
{
int rc;
rc = tc_ctr_mode(out, inlen, in, inlen, counter, &blk_off, ctx);
if (rc != TC_CRYPTO_SUCCESS) {
return -1;
}
return 0;
}

static inline int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *m, uint32_t mlen, uint32_t blk_off, uint8_t *c)
{
return common_bootutil_aes_ctr_crypt(ctx, counter, m, mlen, blk_off, c);
}

static inline int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *c, uint32_t clen, uint32_t blk_off, uint8_t *m)
{
return common_bootutil_aes_ctr_crypt(ctx, counter, c, clen, blk_off, m);
}

#ifdef __cplusplus
}
#endif

#endif /* __BOOTUTIL_CRYPTO_AES_CTR_TINYCRYPT_H_ */
3 changes: 3 additions & 0 deletions boot/bootutil/include/bootutil/enc_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ void boot_enc_decrypt(struct enc_key_data *enc_state,
/* Note that boot_enc_zeorize takes BOOT_CURR_ENC, not BOOT_CURR_ENC_SLOT */
void boot_enc_zeroize(struct enc_key_data *enc_state);

/* Retrieve key for a slot */
int boot_take_enc_key(uint8_t *key, int image, int slot);

#ifdef __cplusplus
}
#endif
Expand Down
Loading