From 2b0000dcd4848a3231831a79e2cd35c049909ec9 Mon Sep 17 00:00:00 2001 From: bg Date: Mon, 10 Oct 2011 23:08:29 +0200 Subject: [PATCH] fixing sha256 --- sha2/sha2_small_common.c | 32 ++++++++++++-------------------- sha2/sha2_small_common.h | 2 +- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/sha2/sha2_small_common.c b/sha2/sha2_small_common.c index d1c5d36..2422f0a 100644 --- a/sha2/sha2_small_common.c +++ b/sha2/sha2_small_common.c @@ -106,47 +106,39 @@ void sha2_small_common_nextBlock (sha2_small_common_ctx_t *state, const void* bl for (i=0; i<8; ++i){ state->h[i] += a[i]; } - state->length += 512; + state->length += 1; } void sha2_small_common_lastBlock(sha2_small_common_ctx_t *state, const void* block, uint16_t length_b){ uint8_t lb[512/8]; /* local block */ -// uint64_t len; + uint64_t len; while(length_b>=512){ sha2_small_common_nextBlock(state, block); length_b -= 512; block = (uint8_t*)block+64; } - - state->length += length_b; - memcpy (&(lb[0]), block, length_b/8); + len = state->length*512 + length_b; + memset(lb, 0, 64); + memcpy(lb, block, (length_b+7)/8); /* set the final one bit */ - if (length_b & 0x7){ // if we have single bits at the end - lb[length_b/8] = ((uint8_t*)(block))[length_b/8]; - } else { - lb[length_b/8] = 0; - } lb[length_b/8] |= 0x80>>(length_b & 0x7); - length_b =(length_b >> 3) + 1; /* from now on length contains the number of BYTES in lb*/ /* pad with zeros */ - if (length_b>64-8){ /* not enouth space for 64bit length value */ - memset((void*)(&(lb[length_b])), 0, 64-length_b); + if (length_b>512-64){ /* not enouth space for 64bit length value */ sha2_small_common_nextBlock(state, lb); - state->length -= 512; - length_b = 0; + memset(lb, 0, 64); } - memset((void*)(&(lb[length_b])), 0, 56-length_b); /* store the 64bit length value */ #if defined LITTLE_ENDIAN /* this is now rolled up */ uint8_t i; - for (i=1; i<=8; ++i){ - lb[55+i] = (uint8_t)(state->length>>(64- 8*i)); - } + i=7; + do{ + lb[63-i] = ((uint8_t*)&len)[i]; + }while(i--); #elif defined BIG_ENDIAN - *((uint64_t)&(lb[56])) = state->length; + *((uint64_t)&(lb[56])) = len; #endif sha2_small_common_nextBlock(state, lb); } diff --git a/sha2/sha2_small_common.h b/sha2/sha2_small_common.h index 9010cfb..a771d5d 100644 --- a/sha2/sha2_small_common.h +++ b/sha2/sha2_small_common.h @@ -22,7 +22,7 @@ typedef struct { uint32_t h[8]; - uint64_t length; + uint32_t length; } sha2_small_common_ctx_t; void sha2_small_common_nextBlock(sha2_small_common_ctx_t* state, const void* block); -- 2.39.5