From d16b33f55685499742934013b9d3247673065406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Tvrd=C3=ADk?= Date: Tue, 12 May 2015 12:38:12 +0200 Subject: [PATCH] SHA512: Fixing bug in SHA512 & SHA384 SHA512/SHA384 are using the SHA256 update and differs in the transform function. This adds a pointer to transform function into a SHA context. --- lib/sha256.c | 6 ++++-- lib/sha256.h | 5 +++++ lib/sha512.c | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/sha256.c b/lib/sha256.c index 8bf17395..84fcc737 100644 --- a/lib/sha256.c +++ b/lib/sha256.c @@ -33,6 +33,7 @@ sha256_init(sha256_context *ctx) ctx->nblocks_high = 0; ctx->count = 0; ctx->blocksize = 64; + ctx->transform = sha256_transform; } void @@ -51,6 +52,7 @@ sha224_init(sha224_context *ctx) ctx->nblocks_high = 0; ctx->count = 0; ctx->blocksize = 64; + ctx->transform = sha256_transform; } /* (4.2) same as SHA-1's F1. */ @@ -244,7 +246,7 @@ sha256_update(sha256_context *ctx, const byte *in_buf, size_t in_len) if (ctx->count == blocksize) /* Flush the buffer. */ { - sha256_transform(ctx, ctx->buf, 1); + ctx->transform(ctx, ctx->buf, 1); ctx->count = 0; if (!++ctx->nblocks) ctx->nblocks_high++; @@ -264,7 +266,7 @@ sha256_update(sha256_context *ctx, const byte *in_buf, size_t in_len) if (in_len >= blocksize) { inblocks = in_len / blocksize; - sha256_transform(ctx, in_buf, inblocks); + ctx->transform(ctx, in_buf, inblocks); ctx->count = 0; ctx->nblocks_high += (ctx->nblocks + inblocks < inblocks); ctx->nblocks += inblocks; diff --git a/lib/sha256.h b/lib/sha256.h index bdbc980d..682cab13 100644 --- a/lib/sha256.h +++ b/lib/sha256.h @@ -22,6 +22,8 @@ #define SHA256_HEX_SIZE 65 #define SHA256_BLOCK_SIZE 64 +typedef unsigned int sha_transform_fn (void *c, const unsigned char *blks, size_t nblks); + typedef struct { u32 h0,h1,h2,h3,h4,h5,h6,h7; byte buf[128]; /* 128 is for SHA384 and SHA512 support, otherwise for SHA224 and SHA256 is 64 enough */ @@ -29,6 +31,7 @@ typedef struct { u32 nblocks_high; int count; u32 blocksize; + sha_transform_fn *transform; } sha256_context; typedef sha256_context sha224_context; @@ -47,6 +50,8 @@ byte* sha224_final(sha224_context *ctx) return sha256_final(ctx); } +static unsigned int sha256_transform(void *ctx, const unsigned char *data, size_t nblks); + /** * HMAC-SHA256, HMAC-SHA224 */ diff --git a/lib/sha512.c b/lib/sha512.c index 249edccd..69bcd099 100644 --- a/lib/sha512.c +++ b/lib/sha512.c @@ -39,6 +39,7 @@ sha512_init(sha512_context *ctx) ctx->bctx.nblocks_high = 0; ctx->bctx.count = 0; ctx->bctx.blocksize = 128; + ctx->bctx.transform = sha512_transform; } void @@ -59,6 +60,7 @@ sha384_init(sha384_context *ctx) ctx->bctx.nblocks_high = 0; ctx->bctx.count = 0; ctx->bctx.blocksize = 128; + ctx->bctx.transform = sha512_transform; } void sha512_update(sha512_context *ctx, const byte *in_buf, size_t in_len)