Skip to content

Commit de51807

Browse files
Bohdan Kovalchukutzig
authored andcommitted
Cypress: add encryption with mbedTLS
Signed-off-by: Bohdan Kovalchuk <bohd@cypress.com> Signed-off-by: Roman Okhrimenko <roman.okhrimenko@infineon.com>
1 parent 25c7a0f commit de51807

File tree

10 files changed

+67
-13
lines changed

10 files changed

+67
-13
lines changed

boot/cypress/BlinkyApp/BlinkyApp.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ OUT_CFG := $(OUT_TARGET)/$(BUILDCFG)
9595

9696
# Set build directory for BOOT and UPGRADE images
9797
ifeq ($(IMG_TYPE), UPGRADE)
98+
ifeq ($(ENC_IMG), 1)
99+
SIGN_ARGS += --encrypt ../../$(ENC_KEY_FILE).pem
100+
endif
98101
SIGN_ARGS += --pad
99102
UPGRADE_SUFFIX :=_upgrade
100103
OUT_CFG := $(OUT_CFG)/upgrade

boot/cypress/BlinkyApp/Readme.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ To get appropriate artifact for second image PRIMARY slot run this command:
8383

8484
To prepare MCUBootApp for work with external memory please refer to `MCUBootApp/ExternalMemory.md`.
8585

86-
For build BlinkyApp upgarde image for external memory use command:
86+
For build BlinkyApp upgrade image for external memory use command:
8787

8888
make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x7FE8000 ERASED_VALUE=0xff
8989

@@ -97,6 +97,16 @@ In case of using muti-image configuration, upgrade image for second application
9797

9898
Note: for S25FL512S block address shuld be mutiple by 0x40000
9999

100+
**How to build encrypted upgrade image :**
101+
102+
To prepare MCUBootApp for work with encrypted upgrade image please refer to `MCUBootApp/Readme.md`.
103+
104+
To obtain encrypted upgrade image of BlinkyApp extra flag `ENC_IMG=1` should be passed in command line, for example:
105+
106+
make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x20000 ENC_IMG=1
107+
108+
This also suggests user already placed corresponing `*.pem` key in `\keys` folder. The key variables are defined in root `Makefile` as `SIGN_KEY_FILE` and `ENC_KEY_FILE`
109+
100110
### Post-Build
101111

102112
Post build action is executed at compile time for `BlinkyApp`. In case of build for `PSOC_062_2M` platform it calls `imgtool` from `MCUBoot` scripts and adds signature to compiled image.

boot/cypress/MCUBootApp/MCUBootApp.mk

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ COMPILER ?= GCC_ARM
3030
USE_CRYPTO_HW ?= 1
3131
USE_EXTERNAL_FLASH ?= 0
3232
MCUBOOT_IMAGE_NUMBER ?= 1
33+
ENC_IMG ?= 0
3334

3435
ifneq ($(COMPILER), GCC_ARM)
3536
$(error Only GCC ARM is supported at this moment)
@@ -53,12 +54,17 @@ ifeq ($(USE_EXTERNAL_FLASH), 1)
5354
DEFINES_APP += -DCY_BOOT_USE_EXTERNAL_FLASH
5455
endif
5556
DEFINES_APP += -DMCUBOOT_MAX_IMG_SECTORS=$(MAX_IMG_SECTORS)
56-
57+
# Hardrware acceleration support
5758
ifeq ($(USE_CRYPTO_HW), 1)
5859
DEFINES_APP += -DMBEDTLS_USER_CONFIG_FILE="\"mcuboot_crypto_acc_config.h\""
5960
DEFINES_APP += -DCY_CRYPTO_HAL_DISABLE
6061
DEFINES_APP += -DCY_MBEDTLS_HW_ACCELERATION
6162
endif
63+
# Encrypted image support
64+
ifeq ($(ENC_IMG), 1)
65+
DEFINES_APP += -DENC_IMG=1
66+
endif
67+
6268
# Collect MCUBoot sourses
6369
SOURCES_MCUBOOT := $(wildcard $(CURDIR)/../bootutil/src/*.c)
6470
# Collect MCUBoot Application sources

boot/cypress/MCUBootApp/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ This folder contains make files infrastructure for building MCUBoot Bootloader.
148148

149149
Root directory for build is **boot/cypress.**
150150

151+
**Encrypted Image Support**
152+
153+
To protect user image from unwanted read Upgrade Image Encryption can be applied. The ECDH/HKDF with EC256 scheme is used in a given solution as well as mbedTLS as a crypto provider.
154+
155+
To enable image encryption support `MCUBOOT_ENC_IMAGES` and `MCUBOOT_ENCRYPT_EC256` have to be defined (can be done by uncommenting in `mcuboot_config.h`).
156+
User is also responsible on providing corresponding binary key data in `enc_priv_key[]` (file `\MCUBootApp\keys.c`). The public part will be used by imgtool when signing and encrypting upgrade image. Signing image with encryption is described in `\BlinkyApp\readme.md`.
157+
158+
After MCUBootApp is built with these settings unencrypted and encrypted images will be accepted in secondary (upgrade) slot.
159+
151160
**Programming solution**
152161

153162
There are couple ways of programming hex of MCUBootApp and BlinkyApp. Following instructions assume one of Cypress development kits, for example `CY8CPROTO_062_4343W`.

boot/cypress/MCUBootApp/config/mcuboot_config/mcuboot_config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,11 @@
144144
/* TODO: to be implemented */ \
145145
} while (0)
146146

147+
/* Uncomment these if support of encrypted upgrade image is needed */
148+
#ifdef ENC_IMG
149+
#define MCUBOOT_ENC_IMAGES
150+
#define MCUBOOT_ENCRYPT_EC256
151+
#define NUM_ECC_BYTES (256 / 8)
152+
#endif /* ENC_IMG */
153+
147154
#endif /* MCUBOOT_CONFIG_H */

boot/cypress/MCUBootApp/config/mcuboot_crypto_acc_config.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
// #define MBEDTLS_CIPHER_MODE_CBC
3636
// #define MBEDTLS_CIPHER_MODE_CFB
3737
// #define MBEDTLS_CIPHER_MODE_OFB
38-
// #define MBEDTLS_CIPHER_MODE_CTR
38+
#ifdef MCUBOOT_ENC_IMAGES
39+
#define MBEDTLS_CIPHER_MODE_CTR
40+
#endif
3941
// #define MBEDTLS_CIPHER_MODE_XTS
4042

4143
/* Only NIST-P curves are currently supported */

boot/cypress/MCUBootApp/config/mcuboot_crypto_config.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3598,4 +3598,9 @@
35983598

35993599
#include "mbedtls/check_config.h"
36003600

3601+
#ifdef MCUBOOT_ENC_IMAGES
3602+
#define MBEDTLS_SHA256_DIGEST_SIZE (32)
3603+
#define MBEDTLS_AES_KEY_SIZE 16
3604+
#endif
3605+
36013606
#endif /* MBEDTLS_CONFIG_H */

boot/cypress/MCUBootApp/keys.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,23 @@ struct bootutil_key bootutil_keys[1] = {
174174
};
175175
const int bootutil_key_cnt = 1;
176176
#endif /* !MCUBOOT_HW_KEY */
177+
178+
unsigned char enc_priv_key[] = {
179+
0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
180+
0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
181+
0x03, 0x01, 0x07, 0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20,
182+
0xf6, 0x1e, 0x51, 0x9d, 0xf8, 0xfa, 0xdd, 0xa1, 0xb7, 0xd9, 0xa9, 0x64,
183+
0x64, 0x3b, 0x54, 0xd0, 0x3d, 0xd0, 0x1f, 0xe5, 0x78, 0xd9, 0x17, 0x98,
184+
0xa5, 0x28, 0xca, 0xcc, 0x6b, 0x67, 0x9e, 0x06, 0xa1, 0x44, 0x03, 0x42,
185+
0x00, 0x04, 0x8a, 0x44, 0x73, 0x00, 0x94, 0xc9, 0x80, 0x27, 0x31, 0x0d,
186+
0x23, 0x36, 0x6b, 0xe9, 0x69, 0x9f, 0xcb, 0xc5, 0x7c, 0xc8, 0x44, 0x1a,
187+
0x93, 0xe6, 0xee, 0x7d, 0x86, 0xa6, 0xae, 0x5e, 0x93, 0x72, 0x74, 0xd9,
188+
0xe1, 0x5a, 0x1c, 0x9b, 0x65, 0x1a, 0x2b, 0x61, 0x41, 0x28, 0x02, 0x73,
189+
0x84, 0x12, 0x97, 0x3a, 0x2d, 0xa2, 0xa0, 0x67, 0x77, 0x02, 0xda, 0x67,
190+
0x1a, 0x4b, 0xdd, 0xd7, 0x71, 0xcc,
191+
};
192+
static unsigned int enc_priv_key_len = 138;
193+
const struct bootutil_key bootutil_enc_key = {
194+
.key = enc_priv_key,
195+
.len = &enc_priv_key_len,
196+
};

boot/cypress/MCUBootApp/libs.mk

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,10 @@ INCLUDE_DIRS_PDL += $(CUR_LIBS_PATH)/pdl/psoc6pdl/cmsis/include
4545
INCLUDE_DIRS_CORE_LIB := $(CUR_LIBS_PATH)/core-lib/include
4646
INCLUDE_DIRS_WATCHDOG := $(CUR_LIBS_PATH)/watchdog
4747

48-
SOURCES_HAL += $(CUR_LIBS_PATH)/psoc6hal/COMPONENT_PSOC6HAL/source/cyhal_crypto_common.c
49-
SOURCES_HAL += $(CUR_LIBS_PATH)/psoc6hal/COMPONENT_PSOC6HAL/source/cyhal_hwmgr.c
50-
5148
# Collected source files for libraries
5249
SOURCES_LIBS := $(SOURCES_PDL)
5350
SOURCES_LIBS += $(SOURCES_WATCHDOG)
5451
SOURCES_LIBS += $(SOURCES_PLATFORM)
55-
SOURCES_LIBS += $(SOURCES_HAL)
56-
57-
# needed for Crypto HW Acceleration and headers inclusion, do not use for peripherals
58-
# peripherals should be accessed
59-
INCLUDE_DIRS_HAL := $(CUR_LIBS_PATH)/psoc6hal/COMPONENT_PSOC6HAL/include
60-
INCLUDE_DIRS_HAL += $(CUR_LIBS_PATH)/psoc6hal/include
61-
INCLUDE_DIRS_HAL += $(CUR_LIBS_PATH)/psoc6hal/COMPONENT_PSOC6HAL/include/pin_packages
6252

6353
# Collected include directories for libraries
6454
INCLUDE_DIRS_LIBS := $(addprefix -I,$(INCLUDE_DIRS_PDL))

boot/cypress/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ APP_NAME ?= MCUBootApp
4343
POST_BUILD ?= 1
4444

4545
SIGN_KEY_FILE ?= cypress-test-ec-p256
46+
ENC_KEY_FILE ?= enc-ec256-pub
47+
ENC_IMG ?= 0
4648

4749
# set this variable to a path, where cysecuretools python package is installed
4850
# use command `python -m pip show cysecuretools` to find out this path

0 commit comments

Comments
 (0)