This library implements some hash and cryptographic algorithms.

Dependents:   ES_CW2_Starter_JIN EMBEDDED_CW2 EMBEDDED_CW2_Final Spinnybois ... more

Fork of Crypto by Francois Berder

Files at this revision

API Documentation at this revision

Comitter:
feb11
Date:
Sun May 11 11:14:51 2014 +0000
Parent:
12:cb30c135cd5f
Child:
14:f04410cef037
Commit message:
fixed warnings + fixed errors if compiling with gcc arm compiler

Changed in this revision

hash/MD2.cpp Show annotated file Show diff for this revision Revisions of this file
hash/MD4.cpp Show annotated file Show diff for this revision Revisions of this file
hash/MD5.cpp Show annotated file Show diff for this revision Revisions of this file
hash/SHA1.cpp Show annotated file Show diff for this revision Revisions of this file
hash/SHA2_32.cpp Show annotated file Show diff for this revision Revisions of this file
hash/SHA2_64.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/hash/MD2.cpp	Tue Apr 08 19:39:25 2014 +0000
+++ b/hash/MD2.cpp	Sun May 11 11:14:51 2014 +0000
@@ -55,7 +55,7 @@
         bufferLength = length;
         memcpy(buffer, data, length);
     }
-    else if(length < 16-bufferLength)
+    else if((int)length < 16-bufferLength)
     {
         memcpy(&buffer[bufferLength], data, length);
         bufferLength += length;
@@ -123,11 +123,16 @@
     memcpy(hash, x, 16);
 }
 
-__forceinline void MD2::computeBlock(uint8_t *checksum2, uint8_t *x2, uint8_t *l2, uint8_t *buffer2)
+#ifdef __CC_ARM
+__forceinline 
+#endif
+void MD2::computeBlock(uint8_t *checksum2, uint8_t *x2, uint8_t *l2, uint8_t *buffer2)
 {
     if(checksum2 != buffer2)
     {
-        #pragma unroll_completely   
+        #ifdef __CC_ARM
+            #pragma unroll_completely   
+        #endif
         for(int j = 0; j < 16; ++j)
         {
             uint8_t c = buffer2[j];
@@ -146,7 +151,9 @@
         x3[8+j] = x3[4+j] ^ x3[j];
     
     uint8_t t = 0;
-    #pragma unroll_completely   
+    #ifdef __CC_ARM
+        #pragma unroll_completely   
+    #endif
     for(int j = 0; j < 18; ++j)
     {
         t = (x2[0] ^= s[t]);
--- a/hash/MD4.cpp	Tue Apr 08 19:39:25 2014 +0000
+++ b/hash/MD4.cpp	Sun May 11 11:14:51 2014 +0000
@@ -43,7 +43,7 @@
 
 void MD4::update(uint8_t *data, uint32_t length)
 {
-    if(length < 64-bufferLength)
+    if((int)length < 64-bufferLength)
     {
         memcpy(&buffer[bufferLength], data, length);
         bufferLength += length;
@@ -59,7 +59,7 @@
         computeRounds(&a, &b, &c, &d, buffer);
         offset += 64;
     }
-    if(offset > length)
+    if(offset > (int)length)
         offset -= 64;
     bufferLength = length - offset;
     memcpy(buffer, &data[offset], bufferLength);
--- a/hash/MD5.cpp	Tue Apr 08 19:39:25 2014 +0000
+++ b/hash/MD5.cpp	Sun May 11 11:14:51 2014 +0000
@@ -52,7 +52,7 @@
 
 void MD5::update(uint8_t *data, uint32_t length)
 {
-    if(length < 64-bufferLength)
+    if((int)length < 64-bufferLength)
     {
         memcpy(&buffer[bufferLength], data, length);
         bufferLength += length;
@@ -68,7 +68,7 @@
         computeRounds(&a, &b, &c, &d, buffer);
         offset += 64;
     }
-    if(offset > length)
+    if(offset > (int)length)
         offset -= 64;
     bufferLength = length - offset;
     memcpy(buffer, &data[offset], bufferLength);
@@ -157,7 +157,12 @@
     hash2[3] = d;
 }
 
-__forceinline void MD5::computeRounds(uint32_t *a2, uint32_t *b2, uint32_t *c2, uint32_t *d2, uint8_t *buffer)
+
+
+#ifdef __CC_ARM
+__forceinline 
+#endif
+void MD5::computeRounds(uint32_t *a2, uint32_t *b2, uint32_t *c2, uint32_t *d2, uint8_t *buffer)
 {
     uint32_t a = *a2, b = *b2, c = *c2, d = *d2;
     uint32_t tmpA = a, tmpB = b, tmpC = c, tmpD = d;
@@ -195,4 +200,4 @@
     *b2 = b + tmpB;
     *c2 = c + tmpC;
     *d2 = d + tmpD;
-}
+}
\ No newline at end of file
--- 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;
 }
-
--- 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;
 }
-
--- a/hash/SHA2_64.cpp	Tue Apr 08 19:39:25 2014 +0000
+++ b/hash/SHA2_64.cpp	Sun May 11 11:14:51 2014 +0000
@@ -15,8 +15,19 @@
 
 static uint64_t revWord(uint64_t w)
 {
+#ifdef __CC_ARM
     return __rev(w >> 32) 
          | ((uint64_t)(__rev(w)) << 32);
+#else
+    return (w >> 56)
+         | ((w & 0x00FF000000000000) >> 40)
+         | ((w & 0x0000FF0000000000) >> 24)
+         | ((w & 0x000000FF00000000) >> 8)
+         | ((w & 0x00000000FF000000) << 8)
+         | ((w & 0x0000000000FF0000) << 24)
+         | ((w & 0x000000000000FF00) << 40)
+         | ((w & 0x00000000000000FF) << 56);
+#endif
 }
 
 #define ROTL(W,N) (((W) << (N)) | ((W) >> (64-(N))))
@@ -67,7 +78,7 @@
 
 void SHA2_64::update(uint8_t *data, uint32_t length)
 {
-    if(length < 128-bufferLength)
+    if((int)length < 128-bufferLength)
     {
         memcpy(&buffer[bufferLength], data, length);
         bufferLength += length;
@@ -83,7 +94,7 @@
         computeBlock(&h0,&h1,&h2,&h3,&h4,&h5,&h6,&h7,buffer);
         offset += 128;
     }
-    if(offset > length)
+    if(offset > (int)length)
         offset -= 128;
     bufferLength = length - offset;
     memcpy(buffer, &data[offset], bufferLength);
@@ -351,4 +362,4 @@
     *h52 += f;
     *h62 += g;
     *h72 += h;
-}
+}
\ No newline at end of file