Added SILC Thread Queue API
[silc.git] / lib / silccrypt / blowfish.c
index 03d381aece1538f5898555abb41e5e1b1023d12e..c1b11b29fe268371f1e2df7d26e0dd9580adc22a 100644 (file)
  *
  */
 
-#include "silcincludes.h"
+#include "silc.h"
+#include "blowfish_internal.h"
 #include "blowfish.h"
 
+/*
+ * SILC Crypto API for Blowfish
+ */
+
+/* Sets the key for the cipher. */
+
+SILC_CIPHER_API_SET_KEY(blowfish_cbc)
+{
+  blowfish_set_key((BlowfishContext *)context, (unsigned char *)key, keylen);
+  return TRUE;
+}
+
+/* Sets IV for the cipher. */
+
+SILC_CIPHER_API_SET_IV(blowfish_cbc)
+{
+
+}
+
+/* Returns the size of the cipher context. */
+
+SILC_CIPHER_API_CONTEXT_LEN(blowfish_cbc)
+{
+  return sizeof(BlowfishContext);
+}
+
+/* Encrypts with the cipher in CBC mode. Source and destination buffers
+   maybe one and same. */
+
+SILC_CIPHER_API_ENCRYPT(blowfish_cbc)
+{
+  SilcUInt32 tiv[4];
+  int i;
+
+  SILC_CBC_GET_IV(tiv, iv);
+
+  SILC_CBC_ENC_PRE(tiv, src);
+  blowfish_encrypt((BlowfishContext *)context, tiv, tiv, 16);
+  SILC_CBC_ENC_POST(tiv, dst, src);
+
+  for (i = 16; i < len; i += 16) {
+    SILC_CBC_ENC_PRE(tiv, src);
+    blowfish_encrypt((BlowfishContext *)context, tiv, tiv, 16);
+    SILC_CBC_ENC_POST(tiv, dst, src);
+  }
+
+  SILC_CBC_PUT_IV(tiv, iv);
+
+  return TRUE;
+}
+
+/* Decrypts with the cipher in CBC mode. Source and destination buffers
+   maybe one and same. */
+
+SILC_CIPHER_API_DECRYPT(blowfish_cbc)
+{
+  SilcUInt32 tmp[4], tmp2[4], tiv[4];
+  int i;
+
+  SILC_CBC_GET_IV(tiv, iv);
+
+  SILC_CBC_DEC_PRE(tmp, src);
+  blowfish_decrypt((BlowfishContext *)context, tmp, tmp2, 16);
+  SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
+
+  for (i = 16; i < len; i += 16) {
+    SILC_CBC_DEC_PRE(tmp, src);
+    blowfish_decrypt((BlowfishContext *)context, tmp, tmp2, 16);
+    SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
+  }
+
+  SILC_CBC_PUT_IV(tiv, iv);
+
+  return TRUE;
+}
+
 static u32 bf_pbox[16 + 2] =
 {
     0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,
@@ -305,9 +382,9 @@ static u32 bf_sbox[256 * 4] =
     0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6,
 };
 
-/* 
+/*
  * Round loop unrolling macros, S is a pointer to a S-Box array
- * organized in 4 uint32s at a row.
+ * organized in 4 SilcUInt32s at a row.
  */
 
 #define GET32_3(x) (((x) & 0xff))
@@ -322,7 +399,7 @@ static u32 bf_sbox[256 * 4] =
 
 /*
  * The blowfish encipher, processes 64-bit blocks.
- * NOTE: This function MUSTN'T respect endianess 
+ * NOTE: This function MUSTN'T respect endianess
  */
 
 int blowfish_encrypt(BlowfishContext *ctx,
@@ -448,7 +525,7 @@ int blowfish_set_key(BlowfishContext *ctx,
     for (i = 0; i < 16 + 2; i += 2)
     {
         blowfish_encrypt(ctx, data, data, 8);
-        
+
         P[i] = data[0];
         P[i + 1] = data[1];
     }