From 215b01bba8bc662d35f72b084700b192d367dfb4 Mon Sep 17 00:00:00 2001
From: Bartlomiej Sieka <tur@semihalf.com>
Date: Tue, 22 Apr 2008 12:27:56 +0200
Subject: [PATCH] Add support for calculating hashes with watchdog triggering

Implement watchodg-aware variants of hash calculation functions:
- crc32_wd()
- md5_wd()
- sha1_csum_wd()
The above functions calculate the hash of the input buffer in chunks,
triggering the watchdog after processing each chunk. The chunk size
is given as a function call parameter.

Signed-off-by: Bartlomiej Sieka <tur@semihalf.com>
---
 include/common.h     |  1 +
 include/sha1.h       | 11 +++++++++++
 include/u-boot/md5.h |  8 ++++++++
 lib_generic/crc32.c  | 27 +++++++++++++++++++++++++++
 lib_generic/md5.c    | 36 ++++++++++++++++++++++++++++++++++++
 lib_generic/sha1.c   | 33 +++++++++++++++++++++++++++++++++
 6 files changed, 116 insertions(+)

diff --git a/include/common.h b/include/common.h
index fa1d0528e1..0ac1f80174 100644
--- a/include/common.h
+++ b/include/common.h
@@ -605,6 +605,7 @@ int	vsprintf(char *buf, const char *fmt, va_list args);
 
 /* lib_generic/crc32.c */
 uint32_t crc32 (uint32_t, const unsigned char *, uint);
+uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
 uint32_t crc32_no_comp (uint32_t, const unsigned char *, uint);
 
 /* common/console.c */
diff --git a/include/sha1.h b/include/sha1.h
index 15ea13cd3a..734d1fb153 100644
--- a/include/sha1.h
+++ b/include/sha1.h
@@ -79,6 +79,17 @@ void sha1_finish( sha1_context *ctx, unsigned char output[20] );
 void sha1_csum( unsigned char *input, int ilen,
 		unsigned char output[20] );
 
+/**
+ * \brief	   Output = SHA-1( input buffer ), with watchdog triggering
+ *
+ * \param input    buffer holding the  data
+ * \param ilen	   length of the input data
+ * \param output   SHA-1 checksum result
+ * \param chunk_sz watchdog triggering period (in bytes of input processed)
+ */
+void sha1_csum_wd (unsigned char *input, int ilen,
+		unsigned char output[20], unsigned int chunk_sz);
+
 /**
  * \brief	   Output = SHA-1( file contents )
  *
diff --git a/include/u-boot/md5.h b/include/u-boot/md5.h
index 046d1eee78..8b44a7f844 100644
--- a/include/u-boot/md5.h
+++ b/include/u-boot/md5.h
@@ -20,4 +20,12 @@ struct MD5Context {
  */
 void md5 (unsigned char *input, int len, unsigned char output[16]);
 
+/*
+ * Calculate and store in 'output' the MD5 digest of 'len' bytes at 'input'.
+ * 'output' must have enough space to hold 16 bytes. If 'chunk' Trigger the
+ * watchdog every 'chunk_sz' bytes of input processed.
+ */
+void md5_wd (unsigned char *input, int len, unsigned char output[16],
+		unsigned int chunk_sz);
+
 #endif /* _MD5_H */
diff --git a/lib_generic/crc32.c b/lib_generic/crc32.c
index b882daa10d..58cd22eb7d 100644
--- a/lib_generic/crc32.c
+++ b/lib_generic/crc32.c
@@ -198,3 +198,30 @@ uint32_t ZEXPORT crc32_no_comp(uint32_t crc, const Bytef *buf, uInt len)
 }
 
 #endif
+
+/*
+ * Calculate the crc32 checksum triggering the watchdog every 'chunk_sz' bytes
+ * of input.
+ */
+ulong crc32_wd (ulong crc, const unsigned char *buf, uint len, uint chunk_sz)
+{
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+	const unsigned char *end, *curr;
+	int chunk;
+
+	curr = buf;
+	end = buf + len;
+	while (curr < end) {
+		chunk = end - curr;
+		if (chunk > chunk_sz)
+			chunk = chunk_sz;
+		crc = crc32 (crc, curr, chunk);
+		curr += chunk;
+		WATCHDOG_RESET ();
+	}
+#else
+        crc = crc32 (crc, buf, len);
+#endif
+
+	return crc;
+}
diff --git a/lib_generic/md5.c b/lib_generic/md5.c
index 3cee431c71..20178b8dcd 100644
--- a/lib_generic/md5.c
+++ b/lib_generic/md5.c
@@ -272,3 +272,39 @@ md5 (unsigned char *input, int len, unsigned char output[16])
 	MD5Update(&context, input, len);
 	MD5Final(output, &context);
 }
+
+
+/*
+ * Calculate and store in 'output' the MD5 digest of 'len' bytes at 'input'.
+ * 'output' must have enough space to hold 16 bytes. If 'chunk' Trigger the
+ * watchdog every 'chunk_sz' bytes of input processed.
+ */
+void
+md5_wd (unsigned char *input, int len, unsigned char output[16],
+	unsigned int chunk_sz)
+{
+	struct MD5Context context;
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+	unsigned char *end, *curr;
+	int chunk;
+#endif
+
+	MD5Init(&context);
+
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+	curr = input;
+	end = input + len;
+	while (curr < end) {
+		chunk = end - curr;
+		if (chunk > chunk_sz)
+			chunk = chunk_sz;
+		MD5Update(&context, curr, chunk);
+		curr += chunk;
+		WATCHDOG_RESET ();
+	}
+#else
+	MD5Update(&context, input, len);
+#endif
+
+	MD5Final(output, &context);
+}
diff --git a/lib_generic/sha1.c b/lib_generic/sha1.c
index 08ffa6b9ba..69506592f7 100644
--- a/lib_generic/sha1.c
+++ b/lib_generic/sha1.c
@@ -308,6 +308,39 @@ void sha1_csum (unsigned char *input, int ilen, unsigned char output[20])
 	sha1_finish (&ctx, output);
 }
 
+/*
+ * Output = SHA-1( input buffer ). Trigger the watchdog every 'chunk_sz'
+ * bytes of input processed.
+ */
+void sha1_csum_wd (unsigned char *input, int ilen, unsigned char output[20],
+			unsigned int chunk_sz)
+{
+	sha1_context ctx;
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+	unsigned char *end, *curr;
+	int chunk;
+#endif
+
+	sha1_starts (&ctx);
+
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+	curr = input;
+	end = input + ilen;
+	while (curr < end) {
+		chunk = end - curr;
+		if (chunk > chunk_sz)
+			chunk = chunk_sz;
+		sha1_update (&ctx, curr, chunk);
+		curr += chunk;
+		WATCHDOG_RESET ();
+	}
+#else
+	sha1_update (&ctx, input, ilen);
+#endif
+
+	sha1_finish (&ctx, output);
+}
+
 /*
  * Output = HMAC-SHA-1( input buffer, hmac key )
  */
-- 
2.39.5