Skip to contentSkip to conversation

image: Avoid erroneous double byte-swap in CRC value

PATCH In review Alexandru Gagniuc 2 participants 2 messages Sep 14, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 image: Avoid erroneous double byte-swap in CRC value

The hash algorithm selection was streamlined in commit 92055e138f28 ("image: Drop if/elseif hash selection in calculate_hash()"). Said commit kept the call to cpu_to_uimage() to convert the CRC to big endian format.

This would have been correct when calling crc32_wd(). However, the ->hash_func_ws member of crc32 points to crc32_wd_buf(), which already converts the CRC to big endian. On a little endian host, doing both conversions results in a little-endian CRC. This is incorrect.

To remedy this, simply drop the call to cpu_to_uimage(), thus only doing the byte-order conversion once.

Fixes 92055e138f28 ("image: Drop if/elseif hash selection in
       calculate_hash()")
---
 common/image-fit.c | 9 ---------
 1 file changed, 9 deletions(-)
common/image-fit.c +0 -9
index 92d9141bcd..f02d437539 100644
@@ -1193,12 +1193,6 @@ int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
 	return 0;
 }
 
-static void crc32_uimage_fixup(void *value)
-{
-	/* TODO: In C, this type punning is undefined behavior: */
-	*((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
-}
-
 /**
  * calculate_hash - calculate and return hash for provided input data
  * @data: pointer to the input data
@@ -1232,9 +1226,6 @@ int calculate_hash(const void *data, int data_len, const char *name,
 	algo->hash_func_ws(data, data_len, value, algo->chunk_size);
 	*value_len = algo->digest_size;
 
-	if (!strcmp(name, "crc32"))
-		crc32_uimage_fixup(value);
-
 	return 0;
 }
 
Signature · 1 line
2.31.1
#1 lore raw
Quoted 10 lines · Alexandru Gagniuc

The hash algorithm selection was streamlined in commit 92055e138f28 ("image: Drop if/elseif hash selection in calculate_hash()"). Said commit kept the call to cpu_to_uimage() to convert the CRC to big endian format.

This would have been correct when calling crc32_wd(). However, the ->hash_func_ws member of crc32 points to crc32_wd_buf(), which already converts the CRC to big endian. On a little endian host, doing both conversions results in a little-endian CRC. This is incorrect.

Quoted 7 lines

To remedy this, simply drop the call to cpu_to_uimage(), thus only doing the byte-order conversion once.

Fixes 92055e138f28 ("image: Drop if/elseif hash selection in
       calculate_hash()")

Applied to u-boot/master, thanks!

Signature · 1 line
Tom