diff mbox series

[RFC,9/9] cpio_utils: skip full sha256 if chunked hash is set

Message ID 20240603085602.2351411-10-dominique.martinet@atmark-techno.com
State RFC
Headers show
Series Proof of concept of chunked checksums | expand

Commit Message

Dominique Martinet June 3, 2024, 8:56 a.m. UTC
This avoids double-compuation of the checksum

Signed-off-by: Dominique Martinet <dominique.martinet@atmark-techno.com>
---
 core/cpio_utils.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/core/cpio_utils.c b/core/cpio_utils.c
index fd52c9ef0ba6..18cd474eaa8c 100644
--- a/core/cpio_utils.c
+++ b/core/cpio_utils.c
@@ -615,12 +615,6 @@  int copyfile(struct swupdate_copy *args)
 	if (args->checksum)
 		*args->checksum = 0;
 
-	if (IsValidHash(args->hash)) {
-		input_state.dgst = swupdate_HASH_init(SHA_DEFAULT);
-		if (!input_state.dgst)
-			return -EFAULT;
-	}
-
 	if (args->encrypted) {
 		aes_key = get_aes_key();
 		if (args->imgivt && strlen(args->imgivt)) {
@@ -736,6 +730,9 @@  int copyfile(struct swupdate_copy *args)
 		close(tmpfd);
 		chunked_hash_state.chunked_hashes = args->chunked_hashes;
 
+		/* avoid computing sha256 twice */
+		args->hash = NULL;
+
 		chunked_hash_state.upstream_step = step;
 		chunked_hash_state.upstream_state = state;
 		step = &chunked_hash_step;
@@ -759,6 +756,12 @@  int copyfile(struct swupdate_copy *args)
 	}
 #endif
 
+	if (IsValidHash(args->hash)) {
+		input_state.dgst = swupdate_HASH_init(SHA_DEFAULT);
+		if (!input_state.dgst)
+			return -EFAULT;
+	}
+
 	for (;;) {
 		ret = step(state, buffer, sizeof buffer);
 		if (ret < 0) {