Merge branch 'topic/mm-fixes' of git://208.110.73.182/silc into silc.1.1.branch
[silc.git] / lib / silccrypt / twofish.c
index e388a951caf083baff2150a90fe7365d68718d02..e8f3bd44d88dc0fc961dea93b21b029b3ebed6f2 100644 (file)
@@ -49,7 +49,7 @@ Mean:          378 cycles =    67.8 mbits/sec
 
 /* Sets the key for the cipher. */
 
-SILC_CIPHER_API_SET_KEY(twofish)
+SILC_CIPHER_API_SET_KEY(twofish_cbc)
 {
   SilcUInt32 k[8];
 
@@ -59,9 +59,16 @@ SILC_CIPHER_API_SET_KEY(twofish)
   return TRUE;
 }
 
+/* Sets IV for the cipher. */
+
+SILC_CIPHER_API_SET_IV(twofish_cbc)
+{
+
+}
+
 /* Returns the size of the cipher context. */
 
-SILC_CIPHER_API_CONTEXT_LEN(twofish)
+SILC_CIPHER_API_CONTEXT_LEN(twofish_cbc)
 {
   return sizeof(TwofishContext);
 }
@@ -69,11 +76,14 @@ SILC_CIPHER_API_CONTEXT_LEN(twofish)
 /* Encrypts with the cipher in CBC mode. Source and destination buffers
    maybe one and same. */
 
-SILC_CIPHER_API_ENCRYPT_CBC(twofish)
+SILC_CIPHER_API_ENCRYPT(twofish_cbc)
 {
   SilcUInt32 tiv[4];
   int i;
 
+  SILC_ASSERT((len & (16 - 1)) == 0);
+  if (len & (16 - 1))
+    return FALSE;
   SILC_CBC_GET_IV(tiv, iv);
 
   SILC_CBC_ENC_PRE(tiv, src);
@@ -94,11 +104,14 @@ SILC_CIPHER_API_ENCRYPT_CBC(twofish)
 /* Decrypts with the cipher in CBC mode. Source and destination buffers
    maybe one and same. */
 
-SILC_CIPHER_API_DECRYPT_CBC(twofish)
+SILC_CIPHER_API_DECRYPT(twofish_cbc)
 {
   SilcUInt32 tmp[4], tmp2[4], tiv[4];
   int i;
 
+  if (len & (16 - 1))
+    return FALSE;
+
   SILC_CBC_GET_IV(tiv, iv);
 
   SILC_CBC_DEC_PRE(tmp, src);
@@ -167,7 +180,7 @@ u1byte qp(const u4byte n, const u1byte x)
     a3 = a2 ^ b2; b3 = ror4[b2] ^ ashx[a2];
     a4 = qt2[n][a3]; b4 = qt3[n][b3];
     return (b4 << 4) | a4;
-};
+}
 
 #ifdef  Q_TABLES
 
@@ -184,7 +197,7 @@ void gen_qtab(void)
         q(0,i) = qp(0, (u1byte)i);
         q(1,i) = qp(1, (u1byte)i);
     }
-};
+}
 
 #else
 
@@ -210,7 +223,7 @@ void gen_mtab(void)
         m_tab[1][i] = fef + (fef << 8) + (f5b << 16) + (f01 << 24);
         m_tab[3][i] = f5b + (f01 << 8) + (fef << 16) + (f5b << 24);
     }
-};
+}
 
 #define mds(n,x)    m_tab[n][x]
 
@@ -289,7 +302,7 @@ u4byte h_fun(TwofishContext *ctx, const u4byte x, const u4byte key[])
     return b0 | (b3 << 8) | (b2 << 16) | (b1 << 24);
 
 #endif
-};
+}
 
 #ifdef  MK_TABLE
 
@@ -358,7 +371,7 @@ void gen_mk_tab(TwofishContext *ctx, u4byte key[])
 #endif
             }
     }
-};
+}
 
 #  ifdef ONE_STEP
 #    define g0_fun(x) ( mk_tab[0][byte(x,0)] ^ mk_tab[1][byte(x,1)] \
@@ -437,7 +450,7 @@ u4byte mds_rem(u4byte p0, u4byte p1)
     }
 
     return p1;
-};
+}
 
 /* initialise the key schedule from the user supplied key   */
 
@@ -485,7 +498,7 @@ u4byte *twofish_set_key(TwofishContext *ctx,
 #endif
 
     return l_key;
-};
+}
 
 /* encrypt a block of text  */
 
@@ -516,7 +529,7 @@ void twofish_encrypt(TwofishContext *ctx,
     out_blk[1] = blk[3] ^ l_key[5];
     out_blk[2] = blk[0] ^ l_key[6];
     out_blk[3] = blk[1] ^ l_key[7];
-};
+}
 
 /* decrypt a block of text  */
 
@@ -547,4 +560,4 @@ void twofish_decrypt(TwofishContext *ctx,
     out_blk[1] = blk[3] ^ l_key[1];
     out_blk[2] = blk[0] ^ l_key[2];
     out_blk[3] = blk[1] ^ l_key[3];
-};
+}