This library implements some hash and cryptographic algorithms.
Dependents: ES_CW2_Starter_JIN EMBEDDED_CW2 EMBEDDED_CW2_Final Spinnybois ... more
Fork of Crypto by
Diff: hash/SHA2_32.cpp
- Revision:
- 13:ac8e23b98dae
- Parent:
- 10:bc9c23aa3870
- Child:
- 15:634f9c4cbab1
diff -r cb30c135cd5f -r ac8e23b98dae hash/SHA2_32.cpp --- a/hash/SHA2_32.cpp Tue Apr 08 19:39:25 2014 +0000 +++ b/hash/SHA2_32.cpp Sun May 11 11:14:51 2014 +0000 @@ -4,17 +4,18 @@ static const uint8_t MASK = 0x0F; + #define W(t) (w[(t)] = SSIG1(w[((t)+14)&MASK]) + w[((t)+9)&MASK] + SSIG0(w[((t)+1)&MASK]) + w[t]) #define ROTL(W,N) (((W) << (N)) | ((W) >> (32-(N)))) -#define ROTR(W,N) (__ror(W,N)) +#define ROTR(W,N) (rotRWord(W,N)) #define CH(X,Y,Z) (((X) & (Y)) ^ ((~(X)) & (Z))) #define MAJ(X,Y,Z) (((X) & (Y)) ^ ((X) & (Z)) ^ ((Y) & (Z))) #define BSIG0(X) (ROTR(X,2) ^ ROTR(X,13) ^ ROTR(X,22)) #define BSIG1(X) (ROTR(X,6) ^ ROTR(X,11) ^ ROTR(X,25)) #define SSIG0(X) (ROTR((X),7) ^ ROTR((X),18) ^ ((X) >> 3)) #define SSIG1(X) (ROTR((X),17) ^ ROTR((X),19) ^ ((X) >> 10)) -#define R(A,B,C,D,E,F,G,H,T,K) T1 = H + BSIG1(E) + CH(E,F,G) + K + (w[T] = __rev(buffer2[T])); \ +#define R(A,B,C,D,E,F,G,H,T,K) T1 = H + BSIG1(E) + CH(E,F,G) + K + (w[T] = revWord(buffer2[T])); \ T2 = BSIG0(A) + MAJ(A,B,C); \ D += T1; \ H = T1 + T2; @@ -34,6 +35,27 @@ 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; +static uint32_t revWord(const uint32_t w) +{ +#ifdef __CC_ARM + return __rev(w); +#else + return (w >> 24) + | ((w & 0x00FF0000) >> 8) + | ((w & 0x0000FF00) << 8) + | ((w & 0x000000FF) << 24); +#endif +} + +static uint32_t rotRWord(const uint32_t w, const uint32_t n) +{ +#ifdef __CC_ARM + return __ror(w, n); +#else + return (w >> n) | (w << (32-n)); +#endif +} + SHA2_32::SHA2_32(SHA_32_TYPE t): type(t), totalBufferLength(0), @@ -67,7 +89,7 @@ void SHA2_32::update(uint8_t *data, uint32_t length) { - if(length < 64-bufferLength) + if((int)length < 64-bufferLength) { memcpy(&buffer[bufferLength], data, length); bufferLength += length; @@ -83,7 +105,7 @@ computeBlock(&h0,&h1,&h2,&h3,&h4,&h5,&h6,&h7,buffer); offset += 64; } - if(offset > length) + if(offset > (int)length) offset -= 64; bufferLength = length - offset; memcpy(buffer, &data[offset], bufferLength); @@ -113,23 +135,23 @@ uint64_t lengthBit = totalBufferLength << 3; uint32_t lengthBitLow = lengthBit; uint32_t lengthBitHigh = lengthBit >> 32; - lengthBitLow = __rev(lengthBitLow); - lengthBitHigh = __rev(lengthBitHigh); + lengthBitLow = revWord(lengthBitLow); + lengthBitHigh = revWord(lengthBitHigh); memcpy(&buffer[60], &lengthBitLow, 4); memcpy(&buffer[56], &lengthBitHigh, 4); computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); - hash2[0] = __rev(h0); - hash2[1] = __rev(h1); - hash2[2] = __rev(h2); - hash2[3] = __rev(h3); - hash2[4] = __rev(h4); - hash2[5] = __rev(h5); - hash2[6] = __rev(h6); + hash2[0] = revWord(h0); + hash2[1] = revWord(h1); + hash2[2] = revWord(h2); + hash2[3] = revWord(h3); + hash2[4] = revWord(h4); + hash2[5] = revWord(h5); + hash2[6] = revWord(h6); if(type == SHA_256) - hash2[7] = __rev(h7); + hash2[7] = revWord(h7); // reset state switch(type) @@ -202,26 +224,30 @@ uint32_t lengthBitLow = lengthBit; uint32_t lengthBitHigh = lengthBit >> 32; - lengthBitLow = __rev(lengthBitLow); + lengthBitLow = revWord(lengthBitLow); memcpy(&buffer[60], &lengthBitLow, 4); - lengthBitHigh = __rev(lengthBitHigh); + lengthBitHigh = revWord(lengthBitHigh); memcpy(&buffer[56], &lengthBitHigh, 4); computeBlock(h, &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7], buffer); - hash2[0] = __rev(h[0]); - hash2[1] = __rev(h[1]); - hash2[2] = __rev(h[2]); - hash2[3] = __rev(h[3]); - hash2[4] = __rev(h[4]); - hash2[5] = __rev(h[5]); - hash2[6] = __rev(h[6]); + hash2[0] = revWord(h[0]); + hash2[1] = revWord(h[1]); + hash2[2] = revWord(h[2]); + hash2[3] = revWord(h[3]); + hash2[4] = revWord(h[4]); + hash2[5] = revWord(h[5]); + hash2[6] = revWord(h[6]); if(type == SHA_256) - hash2[7] = __rev(h[7]); + hash2[7] = revWord(h[7]); } -__forceinline void SHA2_32::computeBlock(uint32_t *h02, + +#ifdef __CC_ARM +__forceinline +#endif +void SHA2_32::computeBlock(uint32_t *h02, uint32_t *h12, uint32_t *h22, uint32_t *h32, @@ -318,4 +344,3 @@ *h62 += g; *h72 += h; } -