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:
6:19aa835f2bbb
Parent:
5:06cd9c8afa0b
diff -r 06cd9c8afa0b -r 19aa835f2bbb SHA2_64.cpp
--- a/SHA2_64.cpp	Thu Sep 12 15:08:51 2013 +0000
+++ b/SHA2_64.cpp	Thu Sep 12 16:03:43 2013 +0000
@@ -65,33 +65,34 @@
     }
 }
 
-void SHA2_64::update(uint8_t *in, uint32_t length)
+void SHA2_64::update(uint8_t *data, uint32_t length)
 {
     if(length < 128-bufferLength)
     {
-        memcpy(&buffer[bufferLength], in, length);
+        memcpy(&buffer[bufferLength], data, length);
         bufferLength += length;
         totalBufferLength += length;
         return;
     }
     int offset = 128-bufferLength;
-    memcpy(&buffer[bufferLength], in, offset);
+    memcpy(&buffer[bufferLength], data, offset);
     computeBlock(&h0,&h1,&h2,&h3,&h4,&h5,&h6,&h7,buffer);
     while(length-offset > 128)
     {
-        memcpy(buffer, &in[offset], 128);
+        memcpy(buffer, &data[offset], 128);
         computeBlock(&h0,&h1,&h2,&h3,&h4,&h5,&h6,&h7,buffer);
         offset += 128;
     }
     if(offset > length)
         offset -= 128;
     bufferLength = length - offset;
-    memcpy(buffer, &in[offset], bufferLength);
+    memcpy(buffer, &data[offset], bufferLength);
     totalBufferLength += length;
 }
 
-void SHA2_64::finalize(uint8_t *digest)
+void SHA2_64::finalize(uint8_t *hash)
 {
+    uint64_t *hash2 = (uint64_t*)hash;
     uint64_t lengthBit = totalBufferLength << 3;
     uint32_t padding;
     if(totalBufferLength % 128 < 112)
@@ -116,27 +117,18 @@
     computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
 
 
-    h0 = revWord(h0);
-    h1 = revWord(h1);
-    h2 = revWord(h2);
-    h3 = revWord(h3);
-    h4 = revWord(h4);
-    h5 = revWord(h5);
+    hash2[0] = revWord(h0);
+    hash2[1] = revWord(h1);
+    hash2[2] = revWord(h2);
+    hash2[3] = revWord(h3);
+    hash2[4] = revWord(h4);
+    hash2[5] = revWord(h5);
 
-    
-    memcpy(digest, &h0, 8);
-    memcpy(&digest[8], &h1, 8);
-    memcpy(&digest[16], &h2, 8);
-    memcpy(&digest[24], &h3, 8);
-    memcpy(&digest[32], &h4, 8);
-    memcpy(&digest[40], &h5, 8);
 
     if(type == SHA_512)
     {
-        h6 = revWord(h6);
-        h7 = revWord(h7);
-        memcpy(&digest[48], &h6, 8);
-        memcpy(&digest[56], &h7, 8);
+        hash2[6] = revWord(h6);
+        hash2[7] = revWord(h7);
     }
     
     // reset state
@@ -168,6 +160,60 @@
     bufferLength = 0;
 }
 
+void SHA2_64::computeHash(SHA2_64_TYPE type, uint8_t *hash, uint8_t *data, uint32_t length)
+{
+    uint64_t *hash2 = (uint64_t*)hash;
+    uint64_t lengthBit = length * 8;
+    uint64_t h0 = H[type*8], h1 = H[type*8+1], h2 = H[type*8+2], h3 = H[type*8+3];
+    uint64_t h4 = H[type*8+4], h5 = H[type*8+5], h6 = H[type*8+6], h7 = H[type*8+7];
+    
+    int padding;
+    if(length % 128 < 112)
+        padding = 112 - (length % 128);
+    else
+        padding = 112 + (128 - (length % 128));
+        
+    while(length >= 128)
+    {
+        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, data);
+        data += 128;
+        length -= 128;
+    }
+    uint8_t buffer[128];
+    memcpy(buffer, data,length); 
+    buffer[length] = 0x80;
+    length++;
+    padding--;
+
+    if(padding+length == 112)
+        memset(&buffer[length], 0, padding);
+    else
+    {
+        memset(&buffer[length], 0, 128-length);
+        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
+        memset(buffer, 0, 112);
+    }
+    
+    lengthBit = revWord(lengthBit);
+    memset(&buffer[112], 0, 8); 
+    memcpy(&buffer[120], &lengthBit, 8);
+    computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
+
+    hash2[0] = revWord(h0);
+    hash2[1] = revWord(h1);
+    hash2[2] = revWord(h2);
+    hash2[3] = revWord(h3);
+    hash2[4] = revWord(h4);
+    hash2[5] = revWord(h5);
+
+
+    if(type == SHA_512)
+    {
+        hash2[6] = revWord(h6);
+        hash2[7] = revWord(h7);
+    }
+}
+
 void SHA2_64::computeBlock(uint64_t *h02, 
                      uint64_t *h12, 
                      uint64_t *h22, 
@@ -306,66 +352,3 @@
     *h62 += g;
     *h72 += h;
 }
-
-void SHA2_64::computeDigest(SHA2_64_TYPE type, uint8_t *digest, uint8_t *in, uint32_t length)
-{
-    uint64_t lengthBit = length * 8;
-    uint64_t h0 = H[type*8], h1 = H[type*8+1], h2 = H[type*8+2], h3 = H[type*8+3];
-    uint64_t h4 = H[type*8+4], h5 = H[type*8+5], h6 = H[type*8+6], h7 = H[type*8+7];
-    
-    int padding;
-    if(length % 128 < 112)
-        padding = 112 - (length % 128);
-    else
-        padding = 112 + (128 - (length % 128));
-        
-    while(length >= 128)
-    {
-        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, in);
-        in += 128;
-        length -= 128;
-    }
-    uint8_t buffer[128];
-    memcpy(buffer, in,length); 
-    buffer[length] = 0x80;
-    length++;
-    padding--;
-
-    if(padding+length == 112)
-        memset(&buffer[length], 0, padding);
-    else
-    {
-        memset(&buffer[length], 0, 128-length);
-        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
-        memset(buffer, 0, 112);
-    }
-    
-    lengthBit = revWord(lengthBit);
-    memset(&buffer[112], 0, 8); 
-    memcpy(&buffer[120], &lengthBit, 8);
-    computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
-
-    h0 = revWord(h0);
-    h1 = revWord(h1);
-    h2 = revWord(h2);
-    h3 = revWord(h3);
-    h4 = revWord(h4);
-    h5 = revWord(h5);
-
-    
-    memcpy(digest, &h0, 8);
-    memcpy(&digest[8], &h1, 8);
-    memcpy(&digest[16], &h2, 8);
-    memcpy(&digest[24], &h3, 8);
-    memcpy(&digest[32], &h4, 8);
-    memcpy(&digest[40], &h5, 8);
-
-    if(type == SHA_512)
-    {
-        h6 = revWord(h6);
-        h7 = revWord(h7);
-        memcpy(&digest[48], &h6, 8);
-        memcpy(&digest[56], &h7, 8);
-    }
-}
-