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:
1:14a7cea431aa
Parent:
0:7a1237bd2d13
Child:
2:473bac39ae7c
diff -r 7a1237bd2d13 -r 14a7cea431aa SHA1.cpp
--- a/SHA1.cpp	Sat Sep 07 23:47:28 2013 +0000
+++ b/SHA1.cpp	Mon Sep 09 12:15:26 2013 +0000
@@ -185,11 +185,30 @@
         padding = 56 - (length % 64);
     else
         padding = 56 + (64 - (length % 64));
-    uint32_t totalLength = length + padding + 8;
-    uint8_t *buffer = new uint8_t[totalLength];
-    memcpy(buffer, in, length);
-    buffer[length] = 0x80;
-    memset(&buffer[length+1], 0, padding-1);
+        
+    uint32_t h0 = H0, h1 = H1, h2 = H2, h3 = H3, h4 = H4;
+    uint32_t offset = 0;
+    while(length - offset >= 64)
+    {
+        computeBlock(&h0,&h1,&h2,&h3,&h4, &in[offset]);
+        offset += 64;
+    }
+
+    uint8_t bufferLength = length - offset;
+    uint8_t buffer[64];
+    memcpy(buffer, &in[offset], bufferLength);
+    buffer[bufferLength++] = 0x80;
+    padding--;
+    while(padding > 0)
+    {
+        if(bufferLength == 64)
+        {
+            computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
+            bufferLength++;
+        }
+        buffer[bufferLength++] = 0;
+        padding--;
+    }
     uint64_t lengthBit = length * 8;
     uint32_t lengthBitLow = lengthBit;
     uint32_t lengthBitHigh = lengthBit >> 32;
@@ -198,19 +217,15 @@
     l[1] = lengthBitLow >> 16;
     l[2] = lengthBitLow >> 8;
     l[3] = lengthBitLow;
-    memcpy(&buffer[length+padding+4], l, 4);
+    memcpy(&buffer[60], l, 4);
     l[0] = lengthBitHigh >> 24;
     l[1] = lengthBitHigh >> 16;
     l[2] = lengthBitHigh >> 8;
     l[3] = lengthBitHigh;
-    memcpy(&buffer[length+padding], l, 4);
+    memcpy(&buffer[56], l, 4);
     
-    uint32_t h0 = H0, h1 = H1, h2 = H2, h3 = H3, h4 = H4;
-    for(int i = 0; i < totalLength/64;  ++i)
-        computeBlock(&h0,&h1,&h2,&h3,&h4, &buffer[64*i]);
+    computeBlock(&h0,&h1,&h2,&h3,&h4, buffer);
 
-    delete[] buffer;
-    
     digest[0] = h0 >> 24;
     digest[1] = h0 >> 16;
     digest[2] = h0 >> 8;