diff mbox series

encrypted images: fix one block sized image decryption

Message ID 20200916114502.33895-1-bage@linutronix.de
State Accepted
Headers show
Series encrypted images: fix one block sized image decryption | expand

Commit Message

Bastian Germann Sept. 16, 2020, 11:45 a.m. UTC
From: Bastian Germann <bage@linutronix.de>

SWUpdate has a bug in the decryption handling. For encrypted image
content that is exactly one AES block (16 bytes) of size (from an
unencrypted file of < 16 bytes), the output is empty.

The reason for this effect is the first swupdate_DECRYPT_update writing
0 bytes and the copyfile() implementation skipping the final decryption
step because it checks on the written bytes.

Check swupdate_DECRYPT_update return for zero, and call
swupdate_DECRYPT_final() directly afterwards.

Signed-off-by: Bastian Germann <bage@linutronix.de>
---
 core/cpio_utils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Stefano Babic Sept. 16, 2020, 12:30 p.m. UTC | #1
On 16.09.20 13:45, bage@linutronix.de wrote:
> From: Bastian Germann <bage@linutronix.de>
> 
> SWUpdate has a bug in the decryption handling. For encrypted image
> content that is exactly one AES block (16 bytes) of size (from an
> unencrypted file of < 16 bytes), the output is empty.
> 
> The reason for this effect is the first swupdate_DECRYPT_update writing
> 0 bytes and the copyfile() implementation skipping the final decryption
> step because it checks on the written bytes.
> 
> Check swupdate_DECRYPT_update return for zero, and call
> swupdate_DECRYPT_final() directly afterwards.
> 
> Signed-off-by: Bastian Germann <bage@linutronix.de>
> ---
>  core/cpio_utils.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/core/cpio_utils.c b/core/cpio_utils.c
> index e2e857c..0ba85fd 100644
> --- a/core/cpio_utils.c
> +++ b/core/cpio_utils.c
> @@ -243,7 +243,8 @@ static int decrypt_step(void *state, void *buffer, size_t size)
>  		if (inlen != 0) {
>  			ret = swupdate_DECRYPT_update(s->dcrypt,
>  				s->output, &s->outlen, s->input, inlen);
> -		} else {
> +		}
> +		if (inlen == 0 || ret == 0) {
>  			/*
>  			 * Finalise the decryption. Further plaintext bytes may
>  			 * be written at this stage.
> 

Tested with vlock size > 16:

Tested-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/core/cpio_utils.c b/core/cpio_utils.c
index e2e857c..0ba85fd 100644
--- a/core/cpio_utils.c
+++ b/core/cpio_utils.c
@@ -243,7 +243,8 @@  static int decrypt_step(void *state, void *buffer, size_t size)
 		if (inlen != 0) {
 			ret = swupdate_DECRYPT_update(s->dcrypt,
 				s->output, &s->outlen, s->input, inlen);
-		} else {
+		}
+		if (inlen == 0 || ret == 0) {
 			/*
 			 * Finalise the decryption. Further plaintext bytes may
 			 * be written at this stage.