]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
sha256: Use const where possible and add watchdog function
authorSimon Glass <sjg@chromium.org>
Wed, 5 Dec 2012 14:46:34 +0000 (14:46 +0000)
committerTom Rini <trini@ti.com>
Tue, 11 Dec 2012 20:17:33 +0000 (13:17 -0700)
In preparation for making the hash function common, we may as well use
const where we can.

Also add a watchdog version of the hashing function.

Signed-off-by: Simon Glass <sjg@chromium.org>
include/sha256.h
lib/sha256.c

index e38ea898c3e825c7c72c683a78bf328f764ff946..beadab35ffc445a6b7d19b41cc9dc85f095facdd 100644 (file)
@@ -3,6 +3,9 @@
 
 #define SHA256_SUM_LEN 32
 
+/* Reset watchdog each time we process this many bytes */
+#define CHUNKSZ_SHA256 (64 * 1024)
+
 typedef struct {
        uint32_t total[2];
        uint32_t state[8];
@@ -10,7 +13,10 @@ typedef struct {
 } sha256_context;
 
 void sha256_starts(sha256_context * ctx);
-void sha256_update(sha256_context * ctx, uint8_t * input, uint32_t length);
+void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length);
 void sha256_finish(sha256_context * ctx, uint8_t digest[SHA256_SUM_LEN]);
 
+void sha256_csum_wd(const unsigned char *input, unsigned int ilen,
+               unsigned char *output, unsigned int chunk_sz);
+
 #endif /* _SHA256_H */
index deb63a40b6d7218145e807850ff51cce488a7e4b..ab2db4890ae7eb4bc432320bb2a044a0d56aa518 100644 (file)
@@ -60,7 +60,7 @@ void sha256_starts(sha256_context * ctx)
        ctx->state[7] = 0x5BE0CD19;
 }
 
-void sha256_process(sha256_context * ctx, uint8_t data[64])
+static void sha256_process(sha256_context *ctx, const uint8_t data[64])
 {
        uint32_t temp1, temp2;
        uint32_t W[64];
@@ -191,7 +191,7 @@ void sha256_process(sha256_context * ctx, uint8_t data[64])
        ctx->state[7] += H;
 }
 
-void sha256_update(sha256_context * ctx, uint8_t * input, uint32_t length)
+void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 {
        uint32_t left, fill;
 
@@ -260,3 +260,36 @@ void sha256_finish(sha256_context * ctx, uint8_t digest[32])
        PUT_UINT32_BE(ctx->state[6], digest, 24);
        PUT_UINT32_BE(ctx->state[7], digest, 28);
 }
+
+/*
+ * Output = SHA-256( input buffer ). Trigger the watchdog every 'chunk_sz'
+ * bytes of input processed.
+ */
+void sha256_csum_wd(const unsigned char *input, unsigned int ilen,
+               unsigned char *output, unsigned int chunk_sz)
+{
+       sha256_context ctx;
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+       unsigned char *end, *curr;
+       int chunk;
+#endif
+
+       sha256_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;
+               sha256_update(&ctx, curr, chunk);
+               curr += chunk;
+               WATCHDOG_RESET();
+       }
+#else
+       sha256_update(&ctx, input, ilen);
+#endif
+
+       sha256_finish(&ctx, output);
+}