X-Git-Url: https://git.cryptolib.org/?p=arm-crypto-lib.git;a=blobdiff_plain;f=sha1%2Fsha1.c;h=342e5b7670c0fff660a49c6ee8a1e4166d486b82;hp=d8249a4ca9346816f2e4a174521848c95ed24446;hb=HEAD;hpb=1cdc772d04bae747c339badaa99f6132eedbcd2f diff --git a/sha1/sha1.c b/sha1/sha1.c index d8249a4..342e5b7 100644 --- a/sha1/sha1.c +++ b/sha1/sha1.c @@ -57,11 +57,12 @@ uint32_t rotl32(uint32_t n, uint8_t bits){ return ((n<>(32-bits))); } +/* static const uint32_t change_endian32(uint32_t x){ return (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8)); } - +*/ /* three SHA-1 inner functions */ const @@ -90,6 +91,22 @@ uint32_t parity(uint32_t x, uint32_t y, uint32_t z){ typedef const uint32_t (*pf_t)(uint32_t x, uint32_t y, uint32_t z); +static +void load_endian32_changed(uint8_t* dest, uint8_t* src, uint16_t words){ +#if defined LITTLE_ENDIAN + while(words--){ + *dest++ = src[3]; + *dest++ = src[2]; + *dest++ = src[1]; + *dest++ = src[0]; + src += 4; + } +#elif defined BIG_ENDIAN + memcpy(dest, src, words * sizeof(uint32_t)); +#endif +} + + void sha1_nextBlock (sha1_ctx_t *state, const void* block){ uint32_t a[5]; uint32_t w[16]; @@ -100,11 +117,8 @@ void sha1_nextBlock (sha1_ctx_t *state, const void* block){ 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6}; - /* load the w array (changing the endian and so) */ - for(t=0; t<16; ++t){ - w[t] = change_endian32(((uint32_t*)block)[t]); - } + load_endian32_changed((uint8_t*)w, (uint8_t*)block, 16); #if DEBUG uint8_t dbgi; @@ -176,7 +190,7 @@ void sha1_lastBlock(sha1_ctx_t *state, const void* block, uint16_t length){ lb[56+i] = ((uint8_t*)&(state->length))[7-i]; } #elif defined BIG_ENDIAN - *((uint64_t)&(lb[56])) = state->length; + *((uint64_t*)&(lb[56])) = state->length; #endif sha1_nextBlock(state, lb); } @@ -185,13 +199,11 @@ void sha1_lastBlock(sha1_ctx_t *state, const void* block, uint16_t length){ void sha1_ctx2hash (void *dest, sha1_ctx_t *state){ #if defined LITTLE_ENDIAN - uint8_t i; - for(i=0; i<5; ++i){ - ((uint32_t*)dest)[i] = change_endian32(state->h[i]); - } -#elif BIG_ENDIAN - if (dest != state->h) + load_endian32_changed((uint8_t*)dest, (uint8_t*)state->h, 5); +#elif defined BIG_ENDIAN + if (dest != state->h) { memcpy(dest, state->h, SHA1_HASH_BITS/8); + } #else # error unsupported endian type! #endif