Fork of François Berder Crypto, fixed AES CBC and small rework

Dependents:   AES_example shaun_larada Smartage

Fork of Crypto by Francois Berder

Revision:
13:ac8e23b98dae
Parent:
10:bc9c23aa3870
--- a/hash/SHA1.cpp	Tue Apr 08 19:39:25 2014 +0000
+++ b/hash/SHA1.cpp	Sun May 11 11:14:51 2014 +0000
@@ -38,6 +38,18 @@
 #define R4(A,B,C,D,E,T) E += ROTL(A, 5) + F1(B, C, D) + W(T & MASK) + K3; \
                         B = ROTL(B,30); 
 
+
+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
+} 
                         
 SHA1::SHA1():
 HashAlgorithm(),
@@ -59,7 +71,7 @@
 
 void SHA1::update(uint8_t *data, uint32_t length)
 {
-    if(length < 64-bufferLength)
+    if((int)length < 64-bufferLength)
     {
         memcpy(&buffer[bufferLength], data, length);
         bufferLength += length;
@@ -75,7 +87,7 @@
         computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
         offset += 64;
     }
-    if(offset > length)
+    if(offset > (int)length)
         offset -= 64;
     bufferLength = length - offset;
     memcpy(buffer, &data[offset], bufferLength);
@@ -105,17 +117,17 @@
     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[56], &lengthBitHigh, 4);
     memcpy(&buffer[60], &lengthBitLow, 4);
     computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
     
-    hash2[0] = __rev(h0);
-    hash2[1] = __rev(h1);
-    hash2[2] = __rev(h2);
-    hash2[3] = __rev(h3);
-    hash2[4] = __rev(h4);
+    hash2[0] = revWord(h0);
+    hash2[1] = revWord(h1);
+    hash2[2] = revWord(h2);
+    hash2[3] = revWord(h3);
+    hash2[4] = revWord(h4);
     
     // reset state
     h0 = H0;
@@ -161,18 +173,18 @@
 
     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, buffer);
 
-    hash2[0] = __rev(h0);
-    hash2[1] = __rev(h1);
-    hash2[2] = __rev(h2);
-    hash2[3] = __rev(h3);
-    hash2[4] = __rev(h4);
+    hash2[0] = revWord(h0);
+    hash2[1] = revWord(h1);
+    hash2[2] = revWord(h2);
+    hash2[3] = revWord(h3);
+    hash2[4] = revWord(h4);
 }
 
 void SHA1::computeBlock(uint32_t *h02, uint32_t *h12, uint32_t *h22, uint32_t *h32, uint32_t *h42, uint8_t *buffer)
@@ -181,7 +193,7 @@
     uint32_t w[16];
 
     for(int t = 0; t < 16; ++t)
-        w[t] = __rev(buffer2[t]);
+        w[t] = revWord(buffer2[t]);
     
     uint32_t a = *h02, b = *h12, c = *h22, d = *h32, e = *h42;
     
@@ -217,4 +229,3 @@
     *h32 += d;
     *h42 += e;
 }
-