summaryrefslogtreecommitdiff
path: root/lib/sha256.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sha256.c')
-rw-r--r--lib/sha256.c67
1 files changed, 41 insertions, 26 deletions
diff --git a/lib/sha256.c b/lib/sha256.c
index fb195d988f1..c2e77c854b9 100644
--- a/lib/sha256.c
+++ b/lib/sha256.c
@@ -265,38 +265,53 @@ void sha256_finish(sha256_context * ctx, uint8_t digest[32])
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)
+int sha256_hmac(const unsigned char *key, int keylen,
+ const unsigned char *input, unsigned int ilen,
+ unsigned char *output)
{
+ int i;
sha256_context ctx;
-#if !defined(USE_HOSTCC) && \
- (defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG))
- const unsigned char *end;
- unsigned char *curr;
- int chunk;
-#endif
+ unsigned char keybuf[64];
+ unsigned char k_ipad[64];
+ unsigned char k_opad[64];
+ unsigned char tmpbuf[32];
+ int keybuf_len;
+
+ if (keylen > 64) {
+ sha256_starts(&ctx);
+ sha256_update(&ctx, key, keylen);
+ sha256_finish(&ctx, keybuf);
+
+ keybuf_len = 32;
+ } else {
+ memset(keybuf, 0, sizeof(keybuf));
+ memcpy(keybuf, key, keylen);
+ keybuf_len = keylen;
+ }
- sha256_starts(&ctx);
+ memset(k_ipad, 0x36, 64);
+ memset(k_opad, 0x5C, 64);
-#if !defined(USE_HOSTCC) && \
- (defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG))
- curr = (unsigned char *)input;
- end = input + ilen;
- while (curr < end) {
- chunk = end - curr;
- if (chunk > chunk_sz)
- chunk = chunk_sz;
- sha256_update(&ctx, curr, chunk);
- curr += chunk;
- schedule();
+ for (i = 0; i < keybuf_len; i++) {
+ k_ipad[i] ^= keybuf[i];
+ k_opad[i] ^= keybuf[i];
}
-#else
+
+ sha256_starts(&ctx);
+ sha256_update(&ctx, k_ipad, sizeof(k_ipad));
sha256_update(&ctx, input, ilen);
-#endif
+ sha256_finish(&ctx, tmpbuf);
+ sha256_starts(&ctx);
+ sha256_update(&ctx, k_opad, sizeof(k_opad));
+ sha256_update(&ctx, tmpbuf, sizeof(tmpbuf));
sha256_finish(&ctx, output);
+
+ memset(k_ipad, 0, sizeof(k_ipad));
+ memset(k_opad, 0, sizeof(k_opad));
+ memset(tmpbuf, 0, sizeof(tmpbuf));
+ memset(keybuf, 0, sizeof(keybuf));
+ memset(&ctx, 0, sizeof(sha256_context));
+
+ return 0;
}