mkimage: fit: add support to encrypt image with aes
PATCH 1/2 mkimage: fit: add support to encrypt image with aes
This commit add the support of encrypting image with aes in mkimage. To enable the ciphering, a node cipher with a reference to a key and IV (Initialization Vector) must be added to the its file. Then mkimage add the encrypted image to the FIT and add the key and IV to the u-boot device tree.
--- Kconfig | 8 ++ common/Makefile | 1 + common/image-cipher.c | 63 +++++++++++ common/image-fit.c | 27 +++++ include/image.h | 63 +++++++++++ include/u-boot/aes.h | 31 ++++++ lib/aes/aes-encrypt.c | 136 +++++++++++++++++++++++ tools/Makefile | 14 ++- tools/fit_image.c | 83 +++++++++++++- tools/image-host.c | 292 ++++++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 716 insertions(+), 2 deletions(-) create mode 100644 common/image-cipher.c create mode 100644 include/u-boot/aes.h create mode 100644 lib/aes/aes-encrypt.c
Kconfig +8 -0
@@ -383,6 +383,14 @@ config FIT_ENABLE_RSASSA_PSS_SUPPORT Enable this to support the pss padding algorithm as described in the rfc8017 (https://tools.ietf.org/html/rfc8017). +config FIT_CIPHER + bool "Enable ciphering data in a FIT uImages" + depends on DM + select AES + help + Enable the feature of data ciphering/unciphering in the tool mkimage + and in the u-boot support of the FIT image. + config FIT_VERBOSE bool "Show verbose messages when FIT images fail" help
common/Makefile +1 -0
@@ -113,6 +113,7 @@ obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += image-fdt.o obj-$(CONFIG_$(SPL_TPL_)FIT) += image-fit.o obj-$(CONFIG_$(SPL_)MULTI_DTB_FIT) += boot_fit.o common_fit.o obj-$(CONFIG_$(SPL_TPL_)FIT_SIGNATURE) += image-sig.o +obj-$(CONFIG_$(SPL_TPL_)FIT_CIPHER) += image-cipher.o obj-$(CONFIG_IO_TRACE) += iotrace.o obj-y += memsize.o obj-y += stdio.o
common/image-cipher.c new file +63 -0
63 lines not on this page · raw message
common/image-fit.c +27 -0
@@ -1080,6 +1080,33 @@ static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)return 0 ; } +/** +* fit_image_cipher_get_algo - get cipher algorithm name +* @fit: pointer to the FIT format image header +* @noffset: cipher node offset +* @algo: double pointer to char, will hold pointer to the algorithm name +* +* fit_image_cipher_get_algo() finds cipher algorithm property in a given +* cipher node. If the property is found its data start address is returned +* to the caller. +* +* returns: +* 0, on success +* -1, on failure +*/ +int fit_image_cipher_get_algo (const void *fit,int noffset,char **algo) +{ +int len; + + *algo = (char *)fdt_getprop (fit, noffset,FIT_ALGO_PROP , &len); +if (!*algo) { +fit_get_debug (fit, noffset,FIT_ALGO_PROP , len); +return -1 ; + } + +return 0 ; +} +ulong fit_get_end (const void *fit) {return map_to_sysmem ((void *)(fit +fdt_totalsize (fit)));
include/image.h +63 -0
87 lines not on this page · raw message
include/u-boot/aes.h new file +31 -0
@@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* +* Copyright (c) 2019, Softathome +*/ + +#ifndef _AES_H +#define _AES_H + +#include <errno.h> +#include <image.h> + +#if IMAGE_ENABLE_ENCRYPT +int image_aes_encrypt (struct image_cipher_info *info, +const unsigned char *data,int size, +unsigned char **cipher,int *cipher_len); +int image_aes_add_cipher_data (struct image_cipher_info *info,void *keydest); +#else +int image_aes_encrypt (struct image_cipher_info *info, +const unsigned char *data,int size, +unsigned char **cipher,int *cipher_len) +{ +return -ENXIO ; +} + +int image_aes_add_cipher_data (struct image_cipher_info *info,void *keydest) +{ +return -ENXIO ; +} +#endif /* IMAGE_ENABLE_ENCRYPT */ + +#endif
lib/aes/aes-encrypt.c new file +136 -0
136 lines not on this page · raw message
tools/Makefile +13 -1
@@ -59,6 +59,7 @@ hostprogs-$(CONFIG_CMD_BOOTEFI_SELFTEST) += file2include FIT_OBJS-$(CONFIG_FIT) := fit_common.o fit_image.o image-host.o common/image-fit.o FIT_SIG_OBJS-$(CONFIG_FIT_SIGNATURE) := common/image-sig.o +FIT_CIPHER_OBJS-$(CONFIG_FIT_CIPHER) := common/image-cipher.o # The following files are synced with upstream DTC. # Use synced versions from scripts/dtc/libfdt/.
@@ -75,6 +76,9 @@ RSA_OBJS-$(CONFIG_FIT_SIGNATURE) := $(addprefix lib/rsa/, \ rsa-sign.o rsa-verify.o rsa-checksum.o \ rsa-mod-exp.o) +AES_OBJS-$(CONFIG_FIT_CIPHER) := $(addprefix lib/aes/, \ + aes-encrypt.o) + ROCKCHIP_OBS = lib/rc4.o rkcommon.o rkimage.o rksd.o rkspi.o # common objs for dumpimage and mkimage
@@ -82,6 +86,7 @@ dumpimage-mkimage-objs := aisimage.o \ atmelimage.o \ $(FIT_OBJS-y) \ $(FIT_SIG_OBJS-y) \ + $(FIT_CIPHER_OBJS-y) \ common/bootm.o \ lib/crc32.o \ default_image.o \
@@ -116,7 +121,8 @@ dumpimage-mkimage-objs := aisimage.o \ gpimage.o \ gpimage-common.o \ mtk_image.o \ - $(RSA_OBJS-y) + $(RSA_OBJS-y) \ + $(AES_OBJS-y) dumpimage-objs := $(dumpimage-mkimage-objs) dumpimage.o mkimage-objs := $(dumpimage-mkimage-objs) mkimage.o
@@ -137,6 +143,12 @@ HOST_EXTRACFLAGS += -DCONFIG_FIT_SIGNATURE HOST_EXTRACFLAGS += -DCONFIG_FIT_SIGNATURE_MAX_SIZE=$(CONFIG_FIT_SIGNATURE_MAX_SIZE) endif +ifdef CONFIG_FIT_CIPHER +# This affects include/image.h, but including the board config file +# is tricky, so manually define this options here. +HOST_EXTRACFLAGS += -DCONFIG_FIT_CIPHER +endif + ifdef CONFIG_SYS_U_BOOT_OFFS HOSTCFLAGS_kwbimage.o += -DCONFIG_SYS_U_BOOT_OFFS=$(CONFIG_SYS_U_BOOT_OFFS) endif
tools/fit_image.c +82 -1
138 lines not on this page · raw message
tools/image-host.c +292 -0
310 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 2/2 u-boot: fit: add support to decrypt fit with aes
This commit add to u-boot the support to decrypt fit image encrypted with aes. The FIT image contains the key name and the IV name. Then u-boot look for the key and IV in his device tree and decrypt images before moving to the next stage.
--- common/image-cipher.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++ common/image-fit.c | 63 ++++++++++++++++++++++++++++++ include/image.h | 12 ++++++ include/u-boot/aes.h | 13 +++++++ lib/Makefile | 1 + lib/aes/Makefile | 5 +++ lib/aes/aes-decrypt.c | 41 ++++++++++++++++++++ tools/Makefile | 2 +- 8 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 lib/aes/Makefile create mode 100644 lib/aes/aes-decrypt.c
common/image-cipher.c +104 -0
@@ -24,6 +24,7 @@ struct cipher_algo cipher_algos[] = { .calculate_type = EVP_aes_128_cbc, #endif .encrypt = image_aes_encrypt, + .decrypt = image_aes_decrypt, .add_cipher_data = image_aes_add_cipher_data }, {
@@ -34,6 +35,7 @@ struct cipher_algo cipher_algos[] = { .calculate_type = EVP_aes_192_cbc, #endif .encrypt = image_aes_encrypt, + .decrypt = image_aes_decrypt, .add_cipher_data = image_aes_add_cipher_data }, {
@@ -44,6 +46,7 @@ struct cipher_algo cipher_algos[] = { .calculate_type = EVP_aes_256_cbc, #endif .encrypt = image_aes_encrypt, + .decrypt = image_aes_decrypt, .add_cipher_data = image_aes_add_cipher_data } };
@@ -61,3 +64,104 @@ struct cipher_algo *image_get_cipher_algo(const char *full_name)return NULL ; } + +static int fit_image_setup_decrypt (struct image_cipher_info *info, +const void *fit,int image_noffset, +int cipher_noffset) +{ +const void *fdt =gd_fdt_blob (); +const char *node_name; +char node_path[128 ]; +int noffset; +char *algo_name; +int ret; + + node_name =fit_get_name (fit, image_noffset,NULL ); +if (!node_name) { +printf ("Can't get node name\n" ); +return -1 ; + } + +if (fit_image_cipher_get_algo (fit, cipher_noffset, &algo_name)) { +printf ("Can't get algo name for cipher '%s' in image '%s'\n" , + node_name, node_name); +return -1 ; + } + + info->keyname =fdt_getprop (fit, cipher_noffset,"key-name-hint" ,NULL ); +if (!info->keyname) { +printf ("Can't get key name\n" ); +return -1 ; + } + + info->ivname =fdt_getprop (fit, cipher_noffset,"iv-name-hint" ,NULL ); +if (!info->ivname) { +printf ("Can't get IV name\n" ); +return -1 ; + } + + info->fit = fit; + info->node_noffset = image_noffset; + info->name = algo_name; + info->cipher =image_get_cipher_algo (algo_name); +if (!info->cipher) { +printf ("Can't get cipher\n" ); +return -1 ; + } + + ret =fit_image_get_data_size_unciphered (fit, image_noffset, + &info->size_unciphered); +if (ret) { +printf ("Can't get size of unciphered data\n" ); +return -1 ; + } + +/* +* Search the cipher node in the u-boot fdt +* the path should be: /cipher/key-<algo>-<key>-<iv> +*/ +snprintf (node_path,sizeof (node_path),"/%s/key-%s-%s-%s" , +FIT_CIPHER_NODENAME , algo_name, info->keyname, info->ivname); + + noffset =fdt_path_offset (fdt, node_path); +if (noffset <0 ) { +printf ("Can't found cipher node offset\n" ); +return -1 ; + } + +/* read key */ + info->key =fdt_getprop (fdt, noffset,"key" ,NULL ); +if (!info->key) { +printf ("Can't get key in cipher node '%s'\n" , node_path); +return -1 ; + } + +/* read iv */ + info->iv =fdt_getprop (fdt, noffset,"iv" ,NULL ); +if (!info->iv) { +printf ("Can't get IV in cipher node '%s'\n" , node_path); +return -1 ; + } + +return 0 ; +} + +int fit_image_decrypt_data (const void *fit, +int image_noffset,int cipher_noffset, +const void *data_ciphered,size_t size_ciphered, +void **data_unciphered,size_t *size_unciphered) +{ +struct image_cipher_info info; +int ret; + + ret =fit_image_setup_decrypt (&info, fit, image_noffset, + cipher_noffset); +if (ret <0 ) + goto out; + + ret = info.cipher->decrypt (&info, data_ciphered, size_ciphered, + data_unciphered, size_unciphered); + + out: +return ret; +}
common/image-fit.c +63 -0
81 lines not on this page · raw message
include/image.h +12 -0
@@ -1022,6 +1022,8 @@ int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset);int fit_image_get_data_position (const void *fit,int noffset,int *data_position);int fit_image_get_data_size (const void *fit,int noffset,int *data_size); +int fit_image_get_data_size_unciphered (const void *fit,int noffset, +size_t *data_size);int fit_image_get_data_and_size (const void *fit,int noffset,const void **data,size_t *size);
@@ -1065,6 +1067,7 @@ int fit_image_verify_with_data(const void *fit, int image_noffset,int fit_image_verify (const void *fit,int noffset);int fit_config_verify (const void *fit,int conf_noffset);int fit_all_image_verify (const void *fit); +int fit_config_decrypt (const void *fit,int conf_noffset);int fit_image_check_os (const void *fit,int noffset,uint8_t os);int fit_image_check_arch (const void *fit,int noffset,uint8_t arch);int fit_image_check_type (const void *fit,int noffset,uint8_t type);
@@ -1292,6 +1295,11 @@ int fit_image_verify_required_sigs(const void *fit, int image_noffset,int fit_image_check_sig (const void *fit,int noffset,const void *data,size_t size,int required_keynode,char **err_msgp); +int fit_image_decrypt_data (const void *fit, +int image_noffset,int cipher_noffset, +const void *data,size_t size, +void **data_unciphered,size_t *size_unciphered); +/** * fit_region_make_list() - Make a list of regions to hash *
@@ -1366,6 +1374,10 @@ struct cipher_algo {int (*add_cipher_data)(struct image_cipher_info *info,void *keydest); + +int (*decrypt)(struct image_cipher_info *info, +const void *cipher,size_t cipher_len, +void **data,size_t *data_len); };int fit_image_cipher_get_algo (const void *fit,int noffset,char **algo);
include/u-boot/aes.h +13 -0
@@ -28,4 +28,17 @@ int image_aes_add_cipher_data(struct image_cipher_info *info, void *keydest) }#endif /* IMAGE_ENABLE_ENCRYPT */ +#if IMAGE_ENABLE_DECRYPT +int image_aes_decrypt (struct image_cipher_info *info, +const void *cipher,size_t cipher_len, +void **data,size_t *size); +#else +int image_aes_decrypt (struct image_cipher_info *info, +const void *cipher,size_t cipher_len, +void **data,size_t *size) +{ +return -ENXIO ; +} +#endif /* IMAGE_ENABLE_DECRYPT */ +#endif
lib/Makefile +1 -0
@@ -21,6 +21,7 @@ obj-$(CONFIG_ASN1_DECODER) += asn1_decoder.o obj-y += crypto/ obj-$(CONFIG_AES) += aes.o +obj-$(CONFIG_AES) += aes/ ifndef API_BUILD ifneq ($(CONFIG_UT_UNICODE)$(CONFIG_EFI_LOADER),)
lib/aes/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright (c) 2019, Softathome + +obj-$(CONFIG_$(SPL_)FIT_CIPHER) += aes-decrypt.o
lib/aes/aes-decrypt.c new file +41 -0
41 lines not on this page · raw message
tools/Makefile +1 -1
@@ -77,7 +77,7 @@ RSA_OBJS-$(CONFIG_FIT_SIGNATURE) := $(addprefix lib/rsa/, \ rsa-mod-exp.o) AES_OBJS-$(CONFIG_FIT_CIPHER) := $(addprefix lib/aes/, \ - aes-encrypt.o) + aes-encrypt.o aes-decrypt.o) ROCKCHIP_OBS = lib/rc4.o rkcommon.o rkimage.o rksd.o rkspi.o
Signature · 1 line
2.7.4
PATCH 2/2 u-boot: fit: add support to decrypt fit with aes
Quoted 7 lines · Philippe Reynes
This commit add to u-boot the support to decrypt fit image encrypted with aes. The FIT image contains the key name and the IV name. Then u-boot look for the key and IV in his device tree and decrypt images before moving to the next stage.
Signed-off-by Philippe Reynes <[email protected]>
Applied to u-boot/master, thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: not available URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200117/1963a92d/attachment.sig>
PATCH 1/2 mkimage: fit: add support to encrypt image with aes
In reply to Philippe ReynesQuoted 8 lines · Philippe Reynes
This commit add the support of encrypting image with aes in mkimage. To enable the ciphering, a node cipher with a reference to a key and IV (Initialization Vector) must be added to the its file. Then mkimage add the encrypted image to the FIT and add the key and IV to the u-boot device tree.
Signed-off-by Philippe Reynes <[email protected]>
Applied to u-boot/master, thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: not available URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200117/3793b53f/attachment.sig>