Skip to contentSkip to conversation

Fix signing problems with large keys

PATCH New Mario Six 3 participants 6 messages Jul 22, 2016 – Jul 26, 2016

lore
Share & tools
lore

Mario Six

author
#0 lore raw

v2 cover Fix signing problems with large keys

This patch series fixes a problem that occurs when signing FIT images with large keys (e.g. RSA with 4096 bits).

Signing sometimes fails because of unexpected return values from fit_add_file_data() and the functions called by it.

Some error messages are also removed, since we tolerate failure of the corresponding functions.

This is probably related to 1152a05 ("tools: Correct error handling in fit_image_process_hash()") and the corresponding error reported here:

https://www.mail-archive.com/u-boot at lists.denx.de/msg217417.html

Mario Six (2):
tools: Fix return code of fit_image_process_sig()
rsa: Fix return value and masked error

 lib/rsa/rsa-sign.c | 12 +++++++-----
 tools/image-host.c | 16 +++++++++++-----
 2 files changed, 18 insertions(+), 10 deletions(-)

--
2.9.0

author
#1 lore raw

PATCH v2 1/2 tools: Fix return code of fit_image_process_sig()

When signing images, we repeatedly call fit_add_file_data() with successively increasing size values to include the keys in the DTB.

Unfortunately, if large keys are used (such as 4096 bit RSA keys), this process fails sometimes, and mkimage needs to be called repeatedly to integrate the keys into the DTB.

This is because fit_add_file_data actually returns the wrong error code, and the loop terminates prematurely, instead of trying again with a larger size value.

This patch corrects the return value and also removes a error message, which is misleading, since we actually allow the function to fail. A (hopefully helpful) comment is also added to explain the lack of error message.

This is probably related to 1152a05 ("tools: Correct error handling in fit_image_process_hash()") and the corresponding error reported here:

https://www.mail-archive.com/u-boot at lists.denx.de/msg217417.html

---

Changes in v2:
- Added an additional NULL check, as suggested by Simon Glass
- Re-formatted the comment block

---
 tools/image-host.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
tools/image-host.c +11 -5
index 3e14fdc..1104695 100644
@@ -238,12 +238,18 @@ static int fit_image_process_sig(const char *keydir, void *keydest,
 	/* Get keyname again, as FDT has changed and invalidated our pointer */
 	info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
 
-	/* Write the public key into the supplied FDT file */
-	if (keydest && info.algo->add_verify_data(&info, keydest)) {
-		printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
-		       node_name, image_name);
+	if (keydest)
+		ret = info.algo->add_verify_data(&info, keydest);
+	else
 		return -1;
-	}
+
+	/*
+	 * Write the public key into the supplied FDT file; this might fail
+	 * several times, since we try signing with successively increasing
+	 * size values
+	 */
+	if (keydest && ret)
+		return ret;
 
 	return 0;
 }

--
2.9.0

#2 lore raw

PATCH v2 1/2 tools: Fix return code of fit_image_process_sig()

Quoted 16 lines · mario.six at gdsys.cc

When signing images, we repeatedly call fit_add_file_data() with successively increasing size values to include the keys in the DTB.

Unfortunately, if large keys are used (such as 4096 bit RSA keys), this process fails sometimes, and mkimage needs to be called repeatedly to integrate the keys into the DTB.

This is because fit_add_file_data actually returns the wrong error code, and the loop terminates prematurely, instead of trying again with a larger size value.

This patch corrects the return value and also removes a error message, which is misleading, since we actually allow the function to fail. A (hopefully helpful) comment is also added to explain the lack of error message.

Quoted 6 lines

This is probably related to 1152a05 ("tools: Correct error handling in fit_image_process_hash()") and the corresponding error reported here:

https://www.mail-archive.com/u-boot at lists.denx.de/msg217417.html

The delta between v1 and v2 has been 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: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160725/0af2542c/attachment.sig>
author
#3 lore raw

PATCH v2 2/2 rsa: Fix return value and masked error

In reply to Mario Six

When signing images, we repeatedly call fit_add_file_data() with successively increasing size values to include the keys in the DTB.

Unfortunately, if large keys are used (such as 4096 bit RSA keys), this process fails sometimes, and mkimage needs to be called repeatedly to integrate the keys into the DTB.

This is because fit_add_file_data actually returns the wrong error code, and the loop terminates prematurely, instead of trying again with a larger size value.

This patch corrects the return value by fixing the return value of fdt_add_bignum, fixes a case where an error is masked by a unconditional setting of a return value variable, and also removes a error message, which is misleading, since we actually allow the function to fail. A (hopefully helpful) comment is also added to explain the lack of error message.

This is probably related to 1152a05 ("tools: Correct error handling in fit_image_process_hash()") and the corresponding error reported here:

https://www.mail-archive.com/u-boot at lists.denx.de/msg217417.html

---

Changes in v2:
- Re-formatted the comment block

---
 lib/rsa/rsa-sign.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
lib/rsa/rsa-sign.c +7 -5
index 5d9716f..c26f741 100644
@@ -420,11 +420,13 @@ static int fdt_add_bignum(void *blob, int noffset, const char *prop_name,
 		BN_rshift(num, num, 32); /*  N = N/B */
 	}
 
+	/*
+	 * We try signing with successively increasing size values, so this
+	 * might fail several times
+	 */
 	ret = fdt_setprop(blob, noffset, prop_name, buf, size);
-	if (ret) {
-		fprintf(stderr, "Failed to write public key to FIT\n");
-		return -ENOSPC;
-	}
+	if (ret)
+		return -FDT_ERR_NOSPACE;
 	free(buf);
 	BN_free(tmp);
 	BN_free(big2);
@@ -508,7 +510,7 @@ int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
 		ret = fdt_setprop_string(keydest, node, FIT_ALGO_PROP,
 					 info->algo->name);
 	}
-	if (info->require_keys) {
+	if (!ret && info->require_keys) {
 		ret = fdt_setprop_string(keydest, node, "required",
 					 info->require_keys);
 	}

--
2.9.0

#4 lore raw

PATCH v2 2/2 rsa: Fix return value and masked error

Quoted 24 lines · Mario Six

When signing images, we repeatedly call fit_add_file_data() with successively increasing size values to include the keys in the DTB.

Unfortunately, if large keys are used (such as 4096 bit RSA keys), this process fails sometimes, and mkimage needs to be called repeatedly to integrate the keys into the DTB.

This is because fit_add_file_data actually returns the wrong error code, and the loop terminates prematurely, instead of trying again with a larger size value.

This patch corrects the return value by fixing the return value of fdt_add_bignum, fixes a case where an error is masked by a unconditional setting of a return value variable, and also removes a error message, which is misleading, since we actually allow the function to fail. A (hopefully helpful) comment is also added to explain the lack of error message.

This is probably related to 1152a05 ("tools: Correct error handling in fit_image_process_hash()") and the corresponding error reported here:

https://www.mail-archive.com/u-boot at lists.denx.de/msg217417.html

Quoted 8 lines

---

Changes in v2:
- Re-formatted the comment block

---
 lib/rsa/rsa-sign.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
#5 lore raw

PATCH v2 2/2 rsa: Fix return value and masked error

In reply to Mario Six
Quoted 18 lines · mario.six at gdsys.cc

When signing images, we repeatedly call fit_add_file_data() with successively increasing size values to include the keys in the DTB.

Unfortunately, if large keys are used (such as 4096 bit RSA keys), this process fails sometimes, and mkimage needs to be called repeatedly to integrate the keys into the DTB.

This is because fit_add_file_data actually returns the wrong error code, and the loop terminates prematurely, instead of trying again with a larger size value.

This patch corrects the return value by fixing the return value of fdt_add_bignum, fixes a case where an error is masked by a unconditional setting of a return value variable, and also removes a error message, which is misleading, since we actually allow the function to fail. A (hopefully helpful) comment is also added to explain the lack of error message.

Quoted 7 lines

This is probably related to 1152a05 ("tools: Correct error handling in fit_image_process_hash()") and the corresponding error reported here:

https://www.mail-archive.com/u-boot at lists.denx.de/msg217417.html

The delta between v1 and v2 has been 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: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160725/7d3575fe/attachment.sig>