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

Dependents:   AES_example shaun_larada Smartage

Fork of Crypto by Francois Berder

Files at this revision

API Documentation at this revision

Comitter:
feb11
Date:
Mon Sep 09 16:16:24 2013 +0000
Parent:
1:14a7cea431aa
Child:
3:85c6ee25cf3e
Commit message:
improved performance of MD2

Changed in this revision

AES.cpp Show annotated file Show diff for this revision Revisions of this file
MD2.cpp Show annotated file Show diff for this revision Revisions of this file
SHA1.cpp Show annotated file Show diff for this revision Revisions of this file
SHA2_32.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/AES.cpp	Mon Sep 09 12:15:26 2013 +0000
+++ b/AES.cpp	Mon Sep 09 16:16:24 2013 +0000
@@ -148,9 +148,9 @@
             --i;
         }
         state[r] = temp >> 24;
-        state[r+4] = (temp & 0x00FF0000) >> 16;
-        state[r+8] = (temp & 0x0000FF00) >> 8;
-        state[r+12] = temp & 0xFF;
+        state[r+4] = temp >> 16;
+        state[r+8] = temp >> 8;
+        state[r+12] = temp;
     }
 }
 
@@ -166,9 +166,9 @@
             --i;
         }
         state[r] = temp >> 24;
-        state[r+4] = (temp & 0x00FF0000) >> 16;
-        state[r+8] = (temp & 0x0000FF00) >> 8;
-        state[r+12] = temp & 0xFF;
+        state[r+4] = temp >> 16;
+        state[r+8] = temp >> 8;
+        state[r+12] = temp;
     }
 }
 
@@ -229,10 +229,10 @@
     {
         uint32_t temp = (state[4*c] << 24) + (state[4*c+1] << 16) + (state[4*c+2] << 8) + state[4*c+3];
         temp ^= w[round*4+c];
-        state[4*c] = (temp >> 24);
-        state[4*c+1] = ((temp & 0x00FF0000) >> 16);
-        state[4*c+2] = ((temp & 0x0000FF00) >> 8);
-        state[4*c+3] = temp & 0xFF;
+        state[4*c] = temp >> 24;
+        state[4*c+1] = temp >> 16;
+        state[4*c+2] = temp >> 8;
+        state[4*c+3] = temp;
     }
 }
 
--- a/MD2.cpp	Mon Sep 09 12:15:26 2013 +0000
+++ b/MD2.cpp	Mon Sep 09 16:16:24 2013 +0000
@@ -36,25 +36,26 @@
     for(int j = 0; j < 16; ++j)
     {
         uint8_t c = buffer2[j];
-        checksum2[j] ^= s[c^(*l2)];
-        *l2 = checksum2[j];
+        *l2 = (checksum2[j] ^= s[c^(*l2)]);
     }
 
-
-    for(int j = 0; j < 16; ++j)
-    {
-        x2[16+j] = buffer2[j];
-        x2[32+j] = x2[16+j] ^ x2[j];
-    }
-
+    uint32_t *x3 = (uint32_t*)x2;
+    uint32_t *buffer3 = (uint32_t*)buffer2;
+    
+    x3[4] = buffer3[0];
+    x3[5] = buffer3[1];
+    x3[6] = buffer3[2];
+    x3[7] = buffer3[3];
+    for(int j = 0; j < 4; ++j)
+        x3[8+j] = x3[4+j] ^ x3[j];
+    
     uint8_t t = 0;
     
     for(int j = 0; j < 18; ++j)
     {
         for(int k = 0; k < 48; ++k)
         {
-            x2[k] = x2[k] ^ s[t];
-            t = x2[k];
+            t = (x2[k] ^= s[t]);
         }
         t += j;
     }
@@ -89,7 +90,7 @@
     int padding = 16 - bufferLength;
     memset(&buffer[bufferLength], padding, padding);
     computeBlock(checksum, x, &l, buffer);
-
+    
     for(int j = 0; j < 16; ++j)
     {
         x[16+j] = checksum[j];
@@ -102,8 +103,7 @@
     {
         for(int k = 0; k < 48; ++k)
         {
-            x[k] = x[k] ^ s[t];
-            t = x[k];
+            t = (x[k] ^= s[t]);
         }
         t += j;
     }
@@ -123,11 +123,13 @@
 
 void MD2::computeDigest(uint8_t *digest, uint8_t *in, uint32_t length)
 {
-    uint8_t buffer[16];
-    uint8_t checksum[16];
-    memset(checksum, 0, 16);
-    uint8_t x[48];
-    memset(x,0,48);
+    uint8_t data[80];
+    memset(data, 0, 64);
+    uint8_t *x = data;
+    uint32_t *x2 = (uint32_t*)data;
+    uint8_t *checksum = &data[48];
+    uint32_t *checksum2 = (uint32_t*)&data[48];
+    uint8_t *buffer = &data[64];
     uint8_t l = 0;
     uint32_t offset = 0;
     while(length - offset >= 16)
@@ -138,24 +140,23 @@
 
     uint8_t bufferLength = length - offset;
     memcpy(buffer, &in[offset], bufferLength);
-    memset(&buffer[bufferLength], 16-bufferLength, 16-bufferLength);
+    uint8_t padding = 16-bufferLength;
+    memset(&buffer[bufferLength], padding, padding);
     computeBlock(checksum, x, &l, buffer);
 
-
-    for(int j = 0; j < 16; ++j)
+    for(int j = 0; j < 4; ++j)
     {
-        x[16+j] = checksum[j];
-        x[32+j] = x[16+j] ^ x[j];
-    }
+        x2[4+j] = checksum2[j];
+        x2[8+j] = x2[4+j] ^ x2[j];
+    }   
 
     uint8_t t = 0;
-        
+
     for(int j = 0; j < 18; ++j)
     {
         for(int k = 0; k < 48; ++k)
         {
-            x[k] = x[k] ^ s[t];
-            t = x[k];
+            t = (x[k] ^= s[t]);
         }
         t += j;
     }
--- a/SHA1.cpp	Mon Sep 09 12:15:26 2013 +0000
+++ b/SHA1.cpp	Mon Sep 09 16:16:24 2013 +0000
@@ -199,52 +199,36 @@
     memcpy(buffer, &in[offset], bufferLength);
     buffer[bufferLength++] = 0x80;
     padding--;
-    while(padding > 0)
+    if(padding+bufferLength+8 == 64)
+        memset(&buffer[bufferLength], 0, padding);
+    else
     {
-        if(bufferLength == 64)
-        {
-            computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
-            bufferLength++;
-        }
-        buffer[bufferLength++] = 0;
-        padding--;
+        memset(&buffer[bufferLength], 0, 64-bufferLength);
+        padding -= 64-bufferLength;
+        computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
+        memset(buffer, 0, 48);
     }
+
     uint64_t lengthBit = length * 8;
     uint32_t lengthBitLow = lengthBit;
     uint32_t lengthBitHigh = lengthBit >> 32;
-    uint8_t l[4];
-    l[0] = lengthBitLow >> 24;
-    l[1] = lengthBitLow >> 16;
-    l[2] = lengthBitLow >> 8;
-    l[3] = lengthBitLow;
-    memcpy(&buffer[60], l, 4);
-    l[0] = lengthBitHigh >> 24;
-    l[1] = lengthBitHigh >> 16;
-    l[2] = lengthBitHigh >> 8;
-    l[3] = lengthBitHigh;
-    memcpy(&buffer[56], l, 4);
+    lengthBitLow = __rev(lengthBitLow);
+    lengthBitHigh = __rev(lengthBitHigh);
+    memcpy(&buffer[60], &lengthBitLow, 4);
+    memcpy(&buffer[56], &lengthBitHigh, 4);
     
     computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
 
-    digest[0] = h0 >> 24;
-    digest[1] = h0 >> 16;
-    digest[2] = h0 >> 8;
-    digest[3] = h0;
-    digest[4] = h1 >> 24;
-    digest[5] = h1 >> 16;
-    digest[6] = h1 >> 8;
-    digest[7] = h1;
-    digest[8] = h2 >> 24;
-    digest[9] = h2 >> 16;
-    digest[10] = h2 >> 8;
-    digest[11] = h2;
-    digest[12] = h3 >> 24;
-    digest[13] = h3 >> 16;
-    digest[14] = h3 >> 8;
-    digest[15] = h3;
-    digest[16] = h4 >> 24;
-    digest[17] = h4 >> 16;
-    digest[18] = h4 >> 8;
-    digest[19] = h4;
+    h0 = __rev(h0);
+    h1 = __rev(h1);
+    h2 = __rev(h2);
+    h3 = __rev(h3);
+    h4 = __rev(h4);
+    
+    memcpy(digest, &h0, 4);
+    memcpy(&digest[4], &h1, 4);
+    memcpy(&digest[8], &h2, 4);
+    memcpy(&digest[12], &h3, 4);
+    memcpy(&digest[16], &h4, 4);
 }
 
--- a/SHA2_32.cpp	Mon Sep 09 12:15:26 2013 +0000
+++ b/SHA2_32.cpp	Mon Sep 09 16:16:24 2013 +0000
@@ -237,7 +237,9 @@
 {
     uint32_t w[64];
     for(int t = 0; t < 16; ++t)
+    {
         w[t] = (buffer[t*4] << 24) | (buffer[t*4+1] << 16) | (buffer[t*4+2] << 8) | buffer[t*4+3]; 
+    }
     for(int t = 16; t < 64; ++t)
         w[t] = SSIG1(w[t-2]) + w[t-7] + SSIG0(w[t-15]) + w[t-16];
     
@@ -301,53 +303,32 @@
     uint64_t lengthBit = length * 8;
     uint32_t lengthBitLow = lengthBit;
     uint32_t lengthBitHigh = lengthBit >> 32;
-    uint8_t tmp[4];
-    tmp[0] = lengthBitLow >> 24;
-    tmp[1] = lengthBitLow >> 16;
-    tmp[2] = lengthBitLow >> 8;
-    tmp[3] = lengthBitLow;
-    memcpy(&buffer[60], tmp, 4);
-    tmp[0] = lengthBitHigh >> 24;
-    tmp[1] = lengthBitHigh >> 16;
-    tmp[2] = lengthBitHigh >> 8;
-    tmp[3] = lengthBitHigh;
-    memcpy(&buffer[56], tmp, 4);    
+    lengthBitLow = __rev(lengthBitLow);
+    memcpy(&buffer[60], &lengthBitLow, 4);
+    lengthBitHigh = __rev(lengthBitHigh);
+    memcpy(&buffer[56], &lengthBitHigh, 4);    
     computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
 
-    digest[0] = h0 >> 24;
-    digest[1] = h0 >> 16;
-    digest[2] = h0 >> 8;
-    digest[3] = h0;
-    digest[4] = h1 >> 24;
-    digest[5] = h1 >> 16;
-    digest[6] = h1 >> 8;
-    digest[7] = h1;
-    digest[8] = h2 >> 24;
-    digest[9] = h2 >> 16;
-    digest[10] = h2 >> 8;
-    digest[11] = h2;
-    digest[12] = h3 >> 24;
-    digest[13] = h3 >> 16;
-    digest[14] = h3 >> 8;
-    digest[15] = h3;
-    digest[16] = h4 >> 24;
-    digest[17] = h4 >> 16;
-    digest[18] = h4 >> 8;
-    digest[19] = h4;
-    digest[20] = h5 >> 24;
-    digest[21] = h5 >> 16;
-    digest[22] = h5 >> 8;
-    digest[23] = h5;
-    digest[24] = h6 >> 24;
-    digest[25] = h6 >> 16;
-    digest[26] = h6 >> 8;
-    digest[27] = h6;
+    h0 = __rev(h0);
+    h1 = __rev(h1);
+    h2 = __rev(h2);
+    h3 = __rev(h3);
+    h4 = __rev(h4);
+    h5 = __rev(h5);
+    h6 = __rev(h6);
+
+    memcpy(digest, &h0, 4);
+    memcpy(&digest[4], &h1, 4);
+    memcpy(&digest[8], &h2, 4);
+    memcpy(&digest[12], &h3, 4);
+    memcpy(&digest[16], &h4, 4);
+    memcpy(&digest[20], &h5, 4);
+    memcpy(&digest[24], &h6, 4);
+
 
     if(type == SHA_256)
     {
-        digest[28] = h7 >> 24;
-        digest[29] = h7 >> 16;
-        digest[30] = h7 >> 8;
-        digest[31] = h7;
+        h7 = __rev(h7);
+        memcpy(&digest[28], &h7, 4);
     }
 }