[U-Boot] [PATCH 1/3] fit: Add support for SHA256 hash

Marek Vasut
2014-02-06T03:47:04Z

This patch adds support for SHA-256 hash into the FIT image. The usage is
as with the other hashing algorithms:

"
hash at 1 {
algo = "sha256";
};
"

Signed-off-by: Marek Vasut <marex@denx.de>
---
common/image-fit.c | 5 +++++
include/image.h | 15 ++++++++++++++-
tools/Makefile | 2 ++
3 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/common/image-fit.c b/common/image-fit.c
index cf4b67e..a7ecf8b 100644
--- a/common/image-fit.c
+++ b/common/image-fit.c
@@ -22,6 +22,7 @@ DECLARE_GLOBAL_DATA_PTR;

#include <bootstage.h>
#include <sha1.h>
+#include <sha256.h>
#include <u-boot/crc.h>
#include <u-boot/md5.h>

@@ -882,6 +883,10 @@ int calculate_hash(const void *data, int data_len, const char *algo,
sha1_csum_wd((unsigned char *)data, data_len,
(unsigned char *)value, CHUNKSZ_SHA1);
*value_len = 20;
+ } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
+ sha256_csum_wd((unsigned char *)data, data_len,
+ (unsigned char *)value, CHUNKSZ_SHA256);
+ *value_len = 32;
} else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
*value_len = 16;
diff --git a/include/image.h b/include/image.h
index 7de2bb2..e5c76e7 100644
--- a/include/image.h
+++ b/include/image.h
@@ -57,13 +57,18 @@ struct lmb;
# ifdef CONFIG_SPL_SHA1_SUPPORT
# define IMAGE_ENABLE_SHA1 1
# endif
+# ifdef CONFIG_SPL_SHA256_SUPPORT
+# define IMAGE_ENABLE_SHA256 1
+# endif
# else
# define CONFIG_CRC32 /* FIT images need CRC32 support */
# define CONFIG_MD5 /* and MD5 */
# define CONFIG_SHA1 /* and SHA1 */
+# define CONFIG_SHA256 /* and SHA256 */
# define IMAGE_ENABLE_CRC32 1
# define IMAGE_ENABLE_MD5 1
# define IMAGE_ENABLE_SHA1 1
+# define IMAGE_ENABLE_SHA256 1
# endif

#ifndef IMAGE_ENABLE_CRC32
@@ -78,6 +83,10 @@ struct lmb;
#define IMAGE_ENABLE_SHA1 0
#endif

+#ifndef IMAGE_ENABLE_SHA256
+#define IMAGE_ENABLE_SHA256 0
+#endif
+
#endif /* CONFIG_FIT */

#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
@@ -345,6 +354,10 @@ extern bootm_headers_t images;
#define CHUNKSZ_SHA1 (64 * 1024)
#endif

+#ifndef CHUNKSZ_SHA256
+#define CHUNKSZ_SHA256 (64 * 1024)
+#endif
+
#define uimage_to_cpu(x) be32_to_cpu(x)
#define cpu_to_uimage(x) cpu_to_be32(x)

@@ -691,7 +704,7 @@ int bootz_setup(ulong image, ulong *start, ulong *end);
#define FIT_FDT_PROP "fdt"
#define FIT_DEFAULT_PROP "default"

-#define FIT_MAX_HASH_LEN 20 /* max(crc32_len(4), sha1_len(20)) */
+#define FIT_MAX_HASH_LEN 32 /* max(crc32_len(4), sha1_len(20), sha256_len(32)) */

/* cmdline argument format parsing */
int fit_parse_conf(const char *spec, ulong addr_curr,
diff --git a/tools/Makefile b/tools/Makefile
index 328cea3..e025004 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -71,6 +71,7 @@ EXT_OBJ_FILES-y += common/image-sig.o
EXT_OBJ_FILES-y += lib/crc32.o
EXT_OBJ_FILES-y += lib/md5.o
EXT_OBJ_FILES-y += lib/sha1.o
+EXT_OBJ_FILES-y += lib/sha256.o

# Source files located in the tools directory
NOPED_OBJ_FILES-y += aisimage.o
@@ -252,6 +253,7 @@ $(obj)mkimage$(SFX): $(obj)aisimage.o \
$(obj)os_support.o \
$(obj)pblimage.o \
$(obj)sha1.o \
+ $(obj)sha256.o \
$(obj)ublimage.o \
$(LIBFDT_OBJS) \
$(RSA_OBJS)
--
1.8.5.3