Skip to contentSkip to conversation

fit: cipher: aes: allow to store the IV in the FIT image

PATCH New Philippe Reynes 2 participants 4 messages Sep 17, 2020 – Oct 13, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 fit: cipher: aes: allow to store the IV in the FIT image

Binaries may be encrypted in a FIT image with AES. This algo needs a key and an IV (Initialization Vector). The IV is provided in a file (pointer by iv-name-hint in the ITS file) when building the ITB file.

This commits adds provide an alternative way to manage the IV. If the property iv-name-hint is not provided in the ITS file, the tool mkimage will generate an random IV and store it in the FIT image.

---
 include/image.h       |  2 +-
 include/u-boot/aes.h  |  6 +++--
 lib/aes/aes-encrypt.c | 22 +++++++++++++++----
 tools/image-host.c    | 61 +++++++++++++++++++++++++++++++++++++++++----------
 4 files changed, 72 insertions(+), 19 deletions(-)
include/image.h +1 -1
index 9a5a87d..10995b8 100644
@@ -1463,7 +1463,7 @@ struct cipher_algo {
 		       unsigned char **cipher, int *cipher_len);
 
 	int (*add_cipher_data)(struct image_cipher_info *info,
-			       void *keydest);
+			       void *keydest, void *fit, int node_noffset);
 
 	int (*decrypt)(struct image_cipher_info *info,
 		       const void *cipher, size_t cipher_len,
include/u-boot/aes.h +4 -2
index 3228104..acbc50b 100644
@@ -13,7 +13,8 @@
 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);
+int image_aes_add_cipher_data(struct image_cipher_info *info, void *keydest,
+			      void *fit, int node_noffset);
 #else
 int image_aes_encrypt(struct image_cipher_info *info,
 		      const unsigned char *data, int size,
@@ -22,7 +23,8 @@ int image_aes_encrypt(struct image_cipher_info *info,
 	return -ENXIO;
 }
 
-int image_aes_add_cipher_data(struct image_cipher_info *info, void *keydest)
+int image_aes_add_cipher_data(struct image_cipher_info *info, void *keydest,
+			      void *fit, int node_noffset)
 {
 	return -ENXIO;
 }
lib/aes/aes-encrypt.c +18 -4
index de00a83..a6d1720 100644
@@ -74,7 +74,8 @@ int image_aes_encrypt(struct image_cipher_info *info,
 	return ret;
 }
 
-int image_aes_add_cipher_data(struct image_cipher_info *info, void *keydest)
+int image_aes_add_cipher_data(struct image_cipher_info *info, void *keydest,
+			      void *fit, int node_noffset)
 {
 	int parent, node;
 	char name[128];
@@ -97,8 +98,13 @@ int image_aes_add_cipher_data(struct image_cipher_info *info, void *keydest)
 		goto done;
 
 	/* Either create or overwrite the named key node */
-	snprintf(name, sizeof(name), "key-%s-%s-%s",
-		 info->name, info->keyname, info->ivname);
+	if (info->ivname)
+		snprintf(name, sizeof(name), "key-%s-%s-%s",
+			 info->name, info->keyname, info->ivname);
+	else
+		snprintf(name, sizeof(name), "key-%s-%s",
+			 info->name, info->keyname);
+
 	node = fdt_subnode_offset(keydest, parent, name);
 	if (node == -FDT_ERR_NOTFOUND) {
 		node = fdt_add_subnode(keydest, parent, name);
@@ -116,9 +122,17 @@ int image_aes_add_cipher_data(struct image_cipher_info *info, void *keydest)
 		ret = node;
 	}
 
-	if (!ret)
+	if (ret)
+		goto done;
+
+	if (info->ivname)
+		/* Store the IV in the u-boot device tree */
 		ret = fdt_setprop(keydest, node, "iv",
 				  info->iv, info->cipher->iv_len);
+	else
+		/* Store the IV in the FIT image */
+		ret = fdt_setprop(fit, node_noffset, "iv",
+				  info->iv, info->cipher->iv_len);
 
 	if (!ret)
 		ret = fdt_setprop(keydest, node, "key",
tools/image-host.c +49 -12

94 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw

PATCH 2/2 fit: cipher: aes: allow to read the IV in the FIT image

This commit add the support in u-boot to read the IV in the FIT image instead of u-boot device tree.

---
 common/image-cipher.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
common/image-cipher.c +15 -7
index 09869f7..4ca9eec 100644
@@ -94,9 +94,11 @@ static int fit_image_setup_decrypt(struct image_cipher_info *info,
 		return -1;
 	}
 
+	info->iv = fdt_getprop(fit, cipher_noffset, "iv", NULL);
 	info->ivname = fdt_getprop(fit, cipher_noffset, "iv-name-hint", NULL);
-	if (!info->ivname) {
-		printf("Can't get IV name\n");
+
+	if (!info->iv && !info->ivname) {
+		printf("Can't get IV or IV name\n");
 		return -1;
 	}
 
@@ -120,8 +122,12 @@ static int fit_image_setup_decrypt(struct image_cipher_info *info,
 	 * 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);
+	if (info->ivname)
+		snprintf(node_path, sizeof(node_path), "/%s/key-%s-%s-%s",
+			 FIT_CIPHER_NODENAME, algo_name, info->keyname, info->ivname);
+	else
+		snprintf(node_path, sizeof(node_path), "/%s/key-%s-%s",
+			 FIT_CIPHER_NODENAME, algo_name, info->keyname);
 
 	noffset = fdt_path_offset(fdt, node_path);
 	if (noffset < 0) {
@@ -137,10 +143,12 @@ static int fit_image_setup_decrypt(struct image_cipher_info *info,
 	}
 
 	/* 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;
+		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;
Signature · 1 line
2.7.4
#2 lore raw

PATCH 2/2 fit: cipher: aes: allow to read the IV in the FIT image

Quoted 4 lines · Philippe Reynes

This commit add the support in u-boot to read the IV in the FIT image instead of u-boot device tree.

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: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201013/a07bf967/attachment.sig>
#3 lore raw

PATCH 1/2 fit: cipher: aes: allow to store the IV in the FIT image

In reply to Philippe Reynes
Quoted 11 lines · Philippe Reynes

Binaries may be encrypted in a FIT image with AES. This algo needs a key and an IV (Initialization Vector). The IV is provided in a file (pointer by iv-name-hint in the ITS file) when building the ITB file.

This commits adds provide an alternative way to manage the IV. If the property iv-name-hint is not provided in the ITS file, the tool mkimage will generate an random IV and store it in the FIT image.

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: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201013/d49249ef/attachment.sig>