fixing sha256

This commit is contained in:
bg 2011-10-10 23:08:29 +02:00
parent 38d2de57df
commit 2b0000dcd4
2 changed files with 13 additions and 21 deletions

View File

@ -106,47 +106,39 @@ void sha2_small_common_nextBlock (sha2_small_common_ctx_t *state, const void* bl
for (i=0; i<8; ++i){ for (i=0; i<8; ++i){
state->h[i] += a[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){ 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 */ uint8_t lb[512/8]; /* local block */
// uint64_t len; uint64_t len;
while(length_b>=512){ while(length_b>=512){
sha2_small_common_nextBlock(state, block); sha2_small_common_nextBlock(state, block);
length_b -= 512; length_b -= 512;
block = (uint8_t*)block+64; block = (uint8_t*)block+64;
} }
len = state->length*512 + length_b;
state->length += length_b; memset(lb, 0, 64);
memcpy (&(lb[0]), block, length_b/8); memcpy(lb, block, (length_b+7)/8);
/* set the final one bit */ /* 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); 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 */ /* pad with zeros */
if (length_b>64-8){ /* not enouth space for 64bit length value */ if (length_b>512-64){ /* not enouth space for 64bit length value */
memset((void*)(&(lb[length_b])), 0, 64-length_b);
sha2_small_common_nextBlock(state, lb); sha2_small_common_nextBlock(state, lb);
state->length -= 512; memset(lb, 0, 64);
length_b = 0;
} }
memset((void*)(&(lb[length_b])), 0, 56-length_b);
/* store the 64bit length value */ /* store the 64bit length value */
#if defined LITTLE_ENDIAN #if defined LITTLE_ENDIAN
/* this is now rolled up */ /* this is now rolled up */
uint8_t i; uint8_t i;
for (i=1; i<=8; ++i){ i=7;
lb[55+i] = (uint8_t)(state->length>>(64- 8*i)); do{
} lb[63-i] = ((uint8_t*)&len)[i];
}while(i--);
#elif defined BIG_ENDIAN #elif defined BIG_ENDIAN
*((uint64_t)&(lb[56])) = state->length; *((uint64_t)&(lb[56])) = len;
#endif #endif
sha2_small_common_nextBlock(state, lb); sha2_small_common_nextBlock(state, lb);
} }

View File

@ -22,7 +22,7 @@
typedef struct { typedef struct {
uint32_t h[8]; uint32_t h[8];
uint64_t length; uint32_t length;
} sha2_small_common_ctx_t; } sha2_small_common_ctx_t;
void sha2_small_common_nextBlock(sha2_small_common_ctx_t* state, const void* block); void sha2_small_common_nextBlock(sha2_small_common_ctx_t* state, const void* block);