]> git.cryptolib.org Git - arm-crypto-lib.git/commitdiff
fixing sha256
authorbg <daniel.otte@rub.de>
Mon, 10 Oct 2011 21:08:29 +0000 (23:08 +0200)
committerbg <daniel.otte@rub.de>
Mon, 10 Oct 2011 21:08:29 +0000 (23:08 +0200)
sha2/sha2_small_common.c
sha2/sha2_small_common.h

index d1c5d36f7b29885f27618310d8eee1813ea54833..2422f0a0f709f8e978184135f2ee778fd27f9171 100644 (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){
                        state->h[i] += a[i];
                }
                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 */
 }
 
 
 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;
        }
        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 */
 
        /* 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 */
-               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);
                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;
        /* 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
 #elif defined BIG_ENDIAN
-       *((uint64_t)&(lb[56])) = state->length;
+       *((uint64_t)&(lb[56])) = len;
 #endif
        sha2_small_common_nextBlock(state, lb);
 }
 #endif
        sha2_small_common_nextBlock(state, lb);
 }
index 9010cfb3f968e94045029c5d4bbd96bb95d9aa80..a771d5dffdc3f9e07e8a2eda329d02d4b8e224fd 100644 (file)
@@ -22,7 +22,7 @@
 
 typedef struct {
        uint32_t h[8];
 
 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);
 } sha2_small_common_ctx_t;
 
 void sha2_small_common_nextBlock(sha2_small_common_ctx_t* state, const void* block);