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:
Geremia
Date:
Wed Jan 28 17:15:32 2015 +0000
Parent:
14:f04410cef037
Child:
16:4399e2e6260b
Commit message:
AES: bugfixed CBC, added PCBC, added public setIV(iv), moved keyExpansion() to public

Changed in this revision

cipher/AES.cpp Show annotated file Show diff for this revision Revisions of this file
cipher/AES.h Show annotated file Show diff for this revision Revisions of this file
cipher/BlockCipher.cpp Show annotated file Show diff for this revision Revisions of this file
cipher/BlockCipher.h Show annotated file Show diff for this revision Revisions of this file
--- a/cipher/AES.cpp	Sun May 11 13:36:45 2014 +0000
+++ b/cipher/AES.cpp	Wed Jan 28 17:15:32 2015 +0000
@@ -76,8 +76,8 @@
     keyExpansion(key);
 }
 
-AES::AES(const AES_TYPE t, uint8_t *key, uint8_t *iv):
-BlockCipher(16,CBC_MODE, iv),
+AES::AES(const AES_TYPE t, uint8_t *key, uint8_t *iv, BLOCK_CIPHER_MODE m):
+BlockCipher(16,m, iv),
 state()
 {
     switch(t)
--- a/cipher/AES.h	Sun May 11 13:36:45 2014 +0000
+++ b/cipher/AES.h	Wed Jan 28 17:15:32 2015 +0000
@@ -15,14 +15,14 @@
     public :
     
         AES(const AES_TYPE type, uint8_t *key);
-        AES(const AES_TYPE type, uint8_t *key, uint8_t *iv);
-                
+        AES(const AES_TYPE type, uint8_t *key, uint8_t *iv, BLOCK_CIPHER_MODE m=CBC_MODE);
+        void keyExpansion(uint8_t *key);
+               
     private :
     
         virtual void encryptBlock(uint8_t *out, uint8_t *in);
         virtual void decryptBlock(uint8_t *out, uint8_t *in);
         
-        void keyExpansion(uint8_t *key);
         uint32_t rotWord(uint32_t w);
         uint32_t invRotWord(uint32_t w);        
         uint32_t subWord(uint32_t w);
--- a/cipher/BlockCipher.cpp	Sun May 11 13:36:45 2014 +0000
+++ b/cipher/BlockCipher.cpp	Wed Jan 28 17:15:32 2015 +0000
@@ -5,19 +5,26 @@
 Cipher(),
 blockSize(bs),
 mode(m),
-IV(0)
+IV(0),
+tmpIV(0),
+tmpdata(0)
 {
-    if(mode == CBC_MODE)
+    if(mode != ECB_MODE)
     {
         IV = new uint8_t[blockSize];
+        tmpIV = new uint8_t[blockSize];
+        tmpdatain = new uint8_t[blockSize];
+        tmpdata = new uint8_t[blockSize];
         memcpy(IV, iv, blockSize); 
     }
 }
 
 BlockCipher::~BlockCipher()
 {
-    if(IV != 0)
-        delete[] IV;
+    if(IV != 0) delete[] IV;
+    if(tmpIV != 0) delete[] tmpIV;
+    if(tmpdatain != 0) delete[] tmpdatain;
+    if(tmpdata != 0) delete[] tmpdata;
 }
 
 CIPHER_TYPE BlockCipher::getType() const
@@ -32,41 +39,64 @@
 
 void BlockCipher::encrypt(uint8_t *out, uint8_t *in, uint32_t length)
 {
-    uint8_t *tmp = 0;
-    if(mode == CBC_MODE)
-        tmp = new uint8_t[getBlockSize()];
-    for(uint32_t i = 0; i < length; i += getBlockSize())
+    
+    switch (mode)
     {
-        if(mode == CBC_MODE)
-        {
-            memcpy(tmp, &in[i], getBlockSize());
-            for(int j = 0; j < (int)getBlockSize(); ++j)
-                tmp[j] ^= IV[j];
-                
-            encryptBlock(&out[i], tmp);
-        
-            memcpy(IV, &out[i], getBlockSize());
-        }
-        else        
-            encryptBlock(&out[i], &in[i]);
+        case ECB_MODE:
+            for(uint32_t i = 0; i < length; i += blockSize)
+            {
+                encryptBlock(out+i, in+i);
+            }
+            break;
+        case PCBC_MODE:
+        case CBC_MODE:
+            memcpy(tmpIV, IV, blockSize);  
+            for(uint32_t i = 0; i < length; i += blockSize)
+            {
+                if(mode==PCBC_MODE) memcpy(tmpdata, in+i, blockSize);
+                memcpy(tmpdatain, in+i, blockSize);
+                for(int j = 0; j < blockSize; ++j) tmpdatain[j] ^= tmpIV[j];
+                encryptBlock(out+i, tmpdatain);
+                memcpy(tmpIV, out+i, blockSize);
+                if(mode==PCBC_MODE)
+                {
+                    for(int j = 0; j < blockSize; ++j) tmpIV[j] ^= tmpdata[j];
+                }
+            }
+            break;
     }
-    if(mode == CBC_MODE)
-        delete[] tmp;
 }
 
 void BlockCipher::decrypt(uint8_t *out, uint8_t *in, uint32_t length)
 {
-    for(uint32_t i = 0; i < length; i += getBlockSize())
+    switch (mode)
     {
-        if(mode == CBC_MODE)
-        {
-            decryptBlock(&out[i], &in[i]);
-            for(int j = 0; j < (int)getBlockSize(); ++j)
-                out[i+j] ^= IV[j];
-        
-            memcpy(IV, &in[i], getBlockSize());
-        }
-        else        
-            decryptBlock(&out[i], &in[i]);
+        case ECB_MODE:
+            for(uint32_t i = 0; i < length; i += blockSize)
+            {
+                decryptBlock(out+i, in+i);
+            }
+            break;
+        case PCBC_MODE:
+        case CBC_MODE:
+            memcpy(tmpIV, IV, blockSize);
+            for(uint32_t i = 0; i < length; i += blockSize)
+            {
+              //  if(mode==PCBC_MODE) memcpy(tmpdata, in+i, blockSize);
+                memcpy(tmpdatain, in+i, blockSize);
+                decryptBlock(out+i, tmpdatain);
+                for(int j = 0; j < blockSize; ++j) out[i+j] ^= tmpIV[j];
+                memcpy(tmpIV, tmpdatain, blockSize); 
+                if(mode==PCBC_MODE)
+                {
+                    for(int j = 0; j < blockSize; ++j) tmpIV[j] ^= out[i+j];
+                }
+            }
+            break;
     }
 }
+
+void BlockCipher::setIV(uint8_t *iv)
+{
+    if(IV!=0) memcpy(IV, iv, blockSize);
+}
--- a/cipher/BlockCipher.h	Sun May 11 13:36:45 2014 +0000
+++ b/cipher/BlockCipher.h	Wed Jan 28 17:15:32 2015 +0000
@@ -6,7 +6,8 @@
 enum BLOCK_CIPHER_MODE
 {
     ECB_MODE,
-    CBC_MODE
+    CBC_MODE,
+    PCBC_MODE
 };
 
 class BlockCipher : public Cipher
@@ -18,6 +19,7 @@
         
         virtual CIPHER_TYPE getType() const;        
         uint32_t getBlockSize() const;
+        void setIV(uint8_t *iv);
 
         virtual void encrypt(uint8_t *out, uint8_t *in, uint32_t length);        
         virtual void decrypt(uint8_t *out, uint8_t *in, uint32_t length);        
@@ -32,6 +34,9 @@
         uint32_t blockSize;
         BLOCK_CIPHER_MODE mode;
         uint8_t *IV;
+        uint8_t *tmpIV;
+        uint8_t *tmpdatain;
+        uint8_t *tmpdata;
 };
 
 #endif