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:
3:85c6ee25cf3e
Parent:
0:7a1237bd2d13
Child:
4:0da19393bd57
diff -r 473bac39ae7c -r 85c6ee25cf3e SHA2_64.cpp
--- a/SHA2_64.cpp	Mon Sep 09 16:16:24 2013 +0000
+++ b/SHA2_64.cpp	Wed Sep 11 17:22:40 2013 +0000
@@ -39,74 +39,26 @@
 
 static uint64_t revWord(uint64_t w)
 {
-    uint8_t buffer[8];
-    buffer[0] = w >> 56;
-    buffer[1] = w >> 48;
-    buffer[2] = w >> 40;
-    buffer[3] = w >> 32;
-    buffer[4] = w >> 24;
-    buffer[5] = w >> 16;
-    buffer[6] = w >> 8;
-    buffer[7] = w;
-    
-    uint64_t res = buffer[7];
-    res <<= 8;
-    res |= buffer[6];
-    res <<= 8;
-    res |= buffer[5];
-    res <<= 8;
-    res |= buffer[4];
-    res <<= 8;
-    res |= buffer[3];
-    res <<= 8;
-    res |= buffer[2];
-    res <<= 8;
-    res |= buffer[1];
-    res <<= 8;
-    res |= buffer[0];
-
-    return res;
-}
-    
-static uint64_t rotLeft(uint64_t w, uint8_t n)
-{
-    return (w << n) | (w >> (64-n));
+    return (w >> 56) 
+        | ((w & 0x00FF000000000000) >> 40) 
+        | ((w & 0x0000FF0000000000) >> 24) 
+        | ((w & 0x000000FF00000000) >> 8) 
+        | ((w & 0x00000000FF000000) << 8) 
+        | ((w & 0x0000000000FF0000) << 24) 
+        | ((w & 0x000000000000FF00) << 40) 
+        | ((w & 0x00000000000000FF) << 56);
 }
 
-static uint64_t rotRight(uint64_t w, uint8_t n)
-{
-    return rotLeft(w,64-n);
-}
-
-static uint64_t CH(uint64_t x, uint64_t y, uint64_t z)
-{
-    return (x & y) ^ ((~x) & z);
-}
-
-static uint64_t MAJ(uint64_t x, uint64_t y, uint64_t z)
-{
-    return (x & y) ^ (x & z) ^ (y & z);
-}
+#define ROTL(W,N) (((W) << (N)) | ((W) >> (64-(N))))
+#define ROTR(W,N) (((W) >> (N)) | ((W) << (64-(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,28) ^ ROTR(X,34) ^ ROTR(X,39))
+#define BSIG1(X) (ROTR(X,14) ^ ROTR(X,18) ^ ROTR(X,41))
+#define SSIG0(X) (ROTR((X),1) ^ ROTR((X),8) ^ ((X) >> 7))
+#define SSIG1(X) (ROTR((X),19) ^ ROTR((X),61) ^ ((X) >> 6))
 
-static uint64_t BSIG0(uint64_t x)
-{
-    return rotRight(x,28) ^ rotRight(x,34) ^ rotRight(x,39);
-}
-
-static uint64_t BSIG1(uint64_t x)
-{
-    return rotRight(x,14) ^ rotRight(x,18) ^ rotRight(x,41);
-}
-
-static uint64_t SSIG0(uint64_t x)
-{
-    return rotRight(x,1) ^ rotRight(x,8) ^ (x >> 7);
-}
-
-static uint64_t SSIG1(uint64_t x)
-{
-    return rotRight(x,19) ^ rotRight(x,61) ^ (x>>6);
-}
+    
 
 SHA2_64::SHA2_64(SHA2_64_TYPE t):
 type(t),
@@ -280,37 +232,37 @@
 
 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 offset = 0;
-    while(length - offset >= 128)
-    {
-        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, &in[offset]);
-        offset += 128;
-    }
-    uint8_t bufferLength = length-offset;
-    uint8_t buffer[128];
-    memcpy(buffer, &in[offset],bufferLength); 
-    uint16_t padding;
+    
+    int padding;
     if(length % 128 < 112)
         padding = 112 - (length % 128);
     else
         padding = 112 + (128 - (length % 128));
-    buffer[bufferLength] = 0x80;
-    bufferLength++;
-    padding--;
-    while(padding > 0)
+        
+    while(length >= 128)
     {
-        if(bufferLength == 128)
-        {
-            computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer);
-            bufferLength = 0;
-        }
-        buffer[bufferLength] = 0;
-        bufferLength++;
-        padding--;
+        computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, in);
+        in += 128;
+        length -= 128;
     }
-    uint64_t lengthBit = length * 8;
+    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, length);
+    }
+    
     lengthBit = revWord(lengthBit);
     memset(&buffer[112], 0, 8); 
     memcpy(&buffer[120], &lengthBit, 8);