updates.
authorPekka Riikonen <priikone@silcnet.org>
Wed, 11 Apr 2001 08:55:03 +0000 (08:55 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Wed, 11 Apr 2001 08:55:03 +0000 (08:55 +0000)
15 files changed:
CHANGES
includes/bitmove.h
lib/silccrypt/aes.c
lib/silccrypt/ciphers_def.h
lib/silccrypt/mars.c
lib/silccrypt/rc6.c
lib/silccrypt/tests/inst [deleted file]
lib/silccrypt/tests/inst_aes [new file with mode: 0644]
lib/silccrypt/tests/inst_rsa [deleted file]
lib/silccrypt/tests/insth [deleted file]
lib/silccrypt/tests/test_aes.c [new file with mode: 0644]
lib/silccrypt/tests/test_rijndael.c [deleted file]
lib/silccrypt/tests/test_rsa.c [deleted file]
lib/silccrypt/tests/test_twofish.c
lib/silccrypt/twofish.c

diff --git a/CHANGES b/CHANGES
index df904e4113cc5e494164d9328be1302faf8f254d..7e4fd78e418356cdb5936d9ada5697af5e43709b 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,10 @@
+Wed Apr 11 16:59:59 EEST 2001  Pekka Riikonen <priikone@poseidon.pspt.fi>
+
+       * Made the configure.in.pre work on Solaris.  Patch by salo.
+
+       * Made all ciphers compatible with non-x86 machines.  Defined
+         CBC mode macros into lib/silccrypt/ciphers_def.h.
+
 Tue Apr 10 20:32:44 EEST 2001  Pekka Riikonen <priikone@poseidon.pspt.fi>
 
        * Fixed the make install.
index aca83a4abdaf5cf650b2f660e0414ed504387d44..8c765cceeda4007e567f24098b575df613eb59c0 100644 (file)
 #define BITMOVE_H
 
 /* Returns four 8-bit bytes, most significant bytes first. */
-#define SILC_GET32_MSB(l, cp) \
-       (l) = ((unsigned long)(unsigned char)(cp)[0]) << 24 \
-           | ((unsigned long)(unsigned char)(cp)[1] << 16) \
-           | ((unsigned long)(unsigned char)(cp)[2] << 8) \
-           | ((unsigned long)(unsigned char)(cp)[3])
-#define SILC_PUT32_MSB(l, cp) \
-       (cp)[0] = l >> 24; \
-       (cp)[1] = l >> 16; \
-       (cp)[2] = l >> 8; \
+#define SILC_GET32_MSB(l, cp)                  \
+       (l) = ((uint32)(uint8)(cp)[0]) << 24    \
+           | ((uint32)(uint8)(cp)[1] << 16)    \
+           | ((uint32)(uint8)(cp)[2] << 8)     \
+           | ((uint32)(uint8)(cp)[3])
+#define SILC_PUT32_MSB(l, cp)                  \
+       (cp)[0] = l >> 24;                      \
+       (cp)[1] = l >> 16;                      \
+       (cp)[2] = l >> 8;                       \
        (cp)[3] = l;
 
 
 /* Returns four 8-bit bytes, less significant bytes first. */
-#define SILC_GET32_LSB(l, cp) \
-       (l) = ((unsigned long)(unsigned char)(cp)[0]) \
-           | ((unsigned long)(unsigned char)(cp)[1] << 8) \
-           | ((unsigned long)(unsigned char)(cp)[2] << 16) \
-           | ((unsigned long)(unsigned char)(cp)[3] << 24)
+#define SILC_GET32_LSB(l, cp)                  \
+       (l) = ((uint32)(uint8)(cp)[0])          \
+           | ((uint32)(uint8)(cp)[1] << 8)     \
+           | ((uint32)(uint8)(cp)[2] << 16)    \
+           | ((uint32)(uint8)(cp)[3] << 24)
 /* same as upper but XOR the result always */
-#define SILC_GET32_X_LSB(l, cp) \
-       (l) ^= ((unsigned long)(unsigned char)(cp)[0]) \
-           | ((unsigned long)(unsigned char)(cp)[1] << 8) \
-           | ((unsigned long)(unsigned char)(cp)[2] << 16) \
-           | ((unsigned long)(unsigned char)(cp)[3] << 24)
-#define SILC_PUT32_LSB(l, cp) \
-       (cp)[0] = l; \
-       (cp)[1] = l >> 8; \
-       (cp)[2] = l >> 16; \
+#define SILC_GET32_X_LSB(l, cp)                        \
+       (l) ^= ((uint32)(uint8)(cp)[0])         \
+           | ((uint32)(uint8)(cp)[1] << 8)     \
+           | ((uint32)(uint8)(cp)[2] << 16)    \
+           | ((uint32)(uint8)(cp)[3] << 24)
+#define SILC_PUT32_LSB(l, cp)                  \
+       (cp)[0] = l;                            \
+       (cp)[1] = l >> 8;                       \
+       (cp)[2] = l >> 16;                      \
        (cp)[3] = l >> 24;
 
 
 /* Returns two 8-bit bytes, most significant bytes first. */
-#define SILC_GET16_MSB(l, cp) \
-       (l) = ((unsigned long)(unsigned char)(cp)[0] << 8) \
-           | ((unsigned long)(unsigned char)(cp)[1])
-#define SILC_PUT16_MSB(l, cp) \
-       (cp)[0] = l >> 8; \
+#define SILC_GET16_MSB(l, cp)                  \
+       (l) = ((uint32)(uint8)(cp)[0] << 8)     \
+           | ((uint32)(uint8)(cp)[1])
+#define SILC_PUT16_MSB(l, cp)                  \
+       (cp)[0] = l >> 8;                       \
        (cp)[1] = l;
 
 /* Returns two 8-bit bytes, less significant bytes first. */
-#define SILC_GET16_LSB(l, cp) \
-       (l) = ((unsigned long)(unsigned char)(cp)[0]) \
-           | ((unsigned long)(unsigned char)(cp)[1] << 8)
-#define SILC_PUT16_LSB(l, cp) \
-       (cp)[0] = l; \
+#define SILC_GET16_LSB(l, cp)                  \
+       (l) = ((uint32)(uint8)(cp)[0])          \
+           | ((uint32)(uint8)(cp)[1] << 8)
+#define SILC_PUT16_LSB(l, cp)                  \
+       (cp)[0] = l;                            \
        (cp)[1] = l >> 8;
 
 #endif
index 163c51fa8d65b8bc27bb3d52036fc6666c5fcc6c..568881d62026f85991cfa67c5d6bb2818884071f 100644 (file)
@@ -122,12 +122,12 @@ SILC_CIPHER_API_DECRYPT_CBC(aes)
 
   for (i = 16; i < len; i += 16) {
     SILC_CBC_DEC_PRE(tmp, src);
-    rijndael_decrypt((RijndaelContext *)context, tmp, tmp2);
+    rijndael_decrypt((RijndaelContext *)context, tmp, tmp2); 
     SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
   }
-
+  
   SILC_CBC_PUT_IV(tiv, iv);
-
+  
   return TRUE;
 }
 
index 085a980656c989202362bf1eef7e7211121b7da5..499b7db89135ccc8344b8a2e81a80e26e2735991 100644 (file)
@@ -30,4 +30,78 @@ typedef uint32 u32;
 #define rotl(x, nr) (((x) << ((int)(nr))) | ((x) >> (32 - (int)(nr))))
 #define byte(x, nr) ((x) >> (nr * 8) & 255)
 
+/* Byte key to words */
+#define SILC_GET_WORD_KEY(s, d, len)           \
+do {                                           \
+  int _i;                                      \
+  for (_i = 0; _i < (len / 8) / 4; _i++)       \
+    SILC_GET32_LSB(d[_i], s + (_i * 4));       \
+} while(0);
+
+/* CBC mode macros. */
+
+#define SILC_CBC_GET_IV(d, s)                  \
+do {                                           \
+  SILC_GET32_LSB(d[0], &s[0]);                 \
+  SILC_GET32_LSB(d[1], &s[4]);                 \
+  SILC_GET32_LSB(d[2], &s[8]);                 \
+  SILC_GET32_LSB(d[3], &s[12]);                        \
+} while(0);
+
+#define SILC_CBC_PUT_IV(s, d)                  \
+do {                                           \
+  SILC_PUT32_LSB(s[0], &d[0]);                 \
+  SILC_PUT32_LSB(s[1], &d[4]);                 \
+  SILC_PUT32_LSB(s[2], &d[8]);                 \
+  SILC_PUT32_LSB(s[3], &d[12]);                        \
+} while(0);
+
+#define SILC_CBC_ENC_PRE(d, s)                 \
+do {                                           \
+  SILC_GET32_X_LSB(d[0], &s[0]);               \
+  SILC_GET32_X_LSB(d[1], &s[4]);               \
+  SILC_GET32_X_LSB(d[2], &s[8]);               \
+  SILC_GET32_X_LSB(d[3], &s[12]);              \
+} while(0);
+
+#define SILC_CBC_ENC_POST(s, d, t)             \
+do {                                           \
+  SILC_PUT32_LSB(s[0], &d[0]);                 \
+  SILC_PUT32_LSB(s[1], &d[4]);                 \
+  SILC_PUT32_LSB(s[2], &d[8]);                 \
+  SILC_PUT32_LSB(s[3], &d[12]);                        \
+                                               \
+  d += 16;                                     \
+  t += 16;                                     \
+} while(0);
+
+#define SILC_CBC_DEC_PRE(d, s)                 \
+do {                                           \
+  SILC_GET32_LSB(d[0], &s[0]);                 \
+  SILC_GET32_LSB(d[1], &s[4]);                 \
+  SILC_GET32_LSB(d[2], &s[8]);                 \
+  SILC_GET32_LSB(d[3], &s[12]);                        \
+} while(0);
+
+#define SILC_CBC_DEC_POST(s, d, p, t, iv)      \
+do {                                           \
+  s[0] ^= iv[0];                               \
+  s[1] ^= iv[1];                               \
+  s[2] ^= iv[2];                               \
+  s[3] ^= iv[3];                               \
+                                               \
+  SILC_PUT32_LSB(s[0], &d[0]);                 \
+  SILC_PUT32_LSB(s[1], &d[4]);                 \
+  SILC_PUT32_LSB(s[2], &d[8]);                 \
+  SILC_PUT32_LSB(s[3], &d[12]);                        \
+                                               \
+  iv[0] = t[0];                                        \
+  iv[1] = t[1];                                        \
+  iv[2] = t[2];                                        \
+  iv[3] = t[3];                                        \
+                                               \
+  d += 16;                                     \
+  p += 16;                                     \
+} while(0);
+
 #endif
index addb8bd0eed80bd72b7c6a318dafdb7586832b2a..454b402ae19881d8171448c3372fc0bd8e9ac6fd 100644 (file)
@@ -49,7 +49,11 @@ Mean:          373 cycles =    68.7 mbits/sec
 
 SILC_CIPHER_API_SET_KEY(mars)
 {
-  mars_set_key((MarsContext *)context, (uint32 *)key, keylen);
+  uint32 k[8];
+
+  SILC_GET_WORD_KEY(key, k, keylen);
+  mars_set_key((MarsContext *)context, k, keylen);
+
   return TRUE;
 }
 
@@ -80,36 +84,22 @@ SILC_CIPHER_API_CONTEXT_LEN(mars)
 
 SILC_CIPHER_API_ENCRYPT_CBC(mars)
 {
-  uint32 *in, *out, *tiv;
-  uint32 tmp[4];
+  uint32 tiv[4];
   int i;
 
-  in = (uint32 *)src;
-  out = (uint32 *)dst;
-  tiv = (uint32 *)iv;
+  SILC_CBC_GET_IV(tiv, iv);
 
-  tmp[0] = in[0] ^ tiv[0];
-  tmp[1] = in[1] ^ tiv[1];
-  tmp[2] = in[2] ^ tiv[2];
-  tmp[3] = in[3] ^ tiv[3];
-  mars_encrypt((MarsContext *)context, tmp, out);
-  in += 4;
-  out += 4;
+  SILC_CBC_ENC_PRE(tiv, src);
+  mars_encrypt((MarsContext *)context, tiv, tiv);
+  SILC_CBC_ENC_POST(tiv, dst, src);
 
   for (i = 16; i < len; i += 16) {
-    tmp[0] = in[0] ^ out[0 - 4];
-    tmp[1] = in[1] ^ out[1 - 4];
-    tmp[2] = in[2] ^ out[2 - 4];
-    tmp[3] = in[3] ^ out[3 - 4];
-    mars_encrypt((MarsContext *)context, tmp, out);
-    in += 4;
-    out += 4;
+    SILC_CBC_ENC_PRE(tiv, src);
+    mars_encrypt((MarsContext *)context, tiv, tiv);
+    SILC_CBC_ENC_POST(tiv, dst, src);
   }
 
-  tiv[0] = out[0 - 4];
-  tiv[1] = out[1 - 4];
-  tiv[2] = out[2 - 4];
-  tiv[3] = out[3 - 4];
+  SILC_CBC_PUT_IV(tiv, iv);
 
   return TRUE;
 }
@@ -119,48 +109,22 @@ SILC_CIPHER_API_ENCRYPT_CBC(mars)
 
 SILC_CIPHER_API_DECRYPT_CBC(mars)
 {
-  uint32 *in, *out, *tiv;
-  uint32 tmp[4], tmp2[4];
+  uint32 tmp[4], tmp2[4], tiv[4];
   int i;
 
-  in = (uint32 *)src;
-  out = (uint32 *)dst;
-  tiv = (uint32 *)iv;
-
-  tmp[0] = in[0];
-  tmp[1] = in[1];
-  tmp[2] = in[2];
-  tmp[3] = in[3];
-  mars_decrypt((MarsContext *)context, in, out);
-  out[0] ^= tiv[0];
-  out[1] ^= tiv[1];
-  out[2] ^= tiv[2];
-  out[3] ^= tiv[3];
-  in += 4;
-  out += 4;
+  SILC_CBC_GET_IV(tiv, iv);
+
+  SILC_CBC_DEC_PRE(tmp, src);
+  mars_decrypt((MarsContext *)context, tmp, tmp2);
+  SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
 
   for (i = 16; i < len; i += 16) {
-    tmp2[0] = tmp[0];
-    tmp2[1] = tmp[1];
-    tmp2[2] = tmp[2];
-    tmp2[3] = tmp[3];
-    tmp[0] = in[0];
-    tmp[1] = in[1];
-    tmp[2] = in[2];
-    tmp[3] = in[3];
-    mars_decrypt((MarsContext *)context, in, out);
-    out[0] ^= tmp2[0];
-    out[1] ^= tmp2[1];
-    out[2] ^= tmp2[2];
-    out[3] ^= tmp2[3];
-    in += 4;
-    out += 4;
+    SILC_CBC_DEC_PRE(tmp, src);
+    mars_decrypt((MarsContext *)context, tmp, tmp2);
+    SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
   }
 
-  tiv[0] = tmp[0];
-  tiv[1] = tmp[1];
-  tiv[2] = tmp[2];
-  tiv[3] = tmp[3];
+  SILC_CBC_PUT_IV(tiv, iv);
 
   return TRUE;
 }
index 03c9cc568bb34ac68d48b27d446510d45ead9cc7..07997d8fa6dc956265484884367a5de3215520b4 100644 (file)
@@ -47,8 +47,12 @@ Mean:          249 cycles =   103.0 mbits/sec
 
 SILC_CIPHER_API_SET_KEY(rc6)
 {
-  rc6_set_key((RC6Context *)context, (uint32 *)key, keylen);
-  return 1;
+  uint32 k[8];
+
+  SILC_GET_WORD_KEY(key, k, keylen);
+  rc6_set_key((RC6Context *)context, k, keylen);
+
+  return TRUE;
 }
 
 /* Sets the string as a new key for the cipher. The string is first
@@ -56,7 +60,7 @@ SILC_CIPHER_API_SET_KEY(rc6)
 
 SILC_CIPHER_API_SET_KEY_WITH_STRING(rc6)
 {
-  return 1;
+  return FALSE;
 }
 
 /* Encrypts with the cipher in CBC mode. Source and destination buffers
@@ -64,36 +68,22 @@ SILC_CIPHER_API_SET_KEY_WITH_STRING(rc6)
 
 SILC_CIPHER_API_ENCRYPT_CBC(rc6)
 {
-  uint32 *in, *out, *tiv;
-  uint32 tmp[4];
+  uint32 tiv[4];
   int i;
 
-  in = (uint32 *)src;
-  out = (uint32 *)dst;
-  tiv = (uint32 *)iv;
+  SILC_CBC_GET_IV(tiv, iv);
 
-  tmp[0] = in[0] ^ tiv[0];
-  tmp[1] = in[1] ^ tiv[1];
-  tmp[2] = in[2] ^ tiv[2];
-  tmp[3] = in[3] ^ tiv[3];
-  rc6_encrypt((RC6Context *)context, tmp, out);
-  in += 4;
-  out += 4;
+  SILC_CBC_ENC_PRE(tiv, src);
+  rc6_encrypt((RC6Context *)context, tiv, tiv);
+  SILC_CBC_ENC_POST(tiv, dst, src);
 
   for (i = 16; i < len; i += 16) {
-    tmp[0] = in[0] ^ out[0 - 4];
-    tmp[1] = in[1] ^ out[1 - 4];
-    tmp[2] = in[2] ^ out[2 - 4];
-    tmp[3] = in[3] ^ out[3 - 4];
-    rc6_encrypt((RC6Context *)context, tmp, out);
-    in += 4;
-    out += 4;
+    SILC_CBC_ENC_PRE(tiv, src);
+    rc6_encrypt((RC6Context *)context, tiv, tiv);
+    SILC_CBC_ENC_POST(tiv, dst, src);
   }
 
-  tiv[0] = out[0 - 4];
-  tiv[1] = out[1 - 4];
-  tiv[2] = out[2 - 4];
-  tiv[3] = out[3 - 4];
+  SILC_CBC_PUT_IV(tiv, iv);
 
   return TRUE;
 }
@@ -103,48 +93,22 @@ SILC_CIPHER_API_ENCRYPT_CBC(rc6)
 
 SILC_CIPHER_API_DECRYPT_CBC(rc6)
 {
-  uint32 *in, *out, *tiv;
-  uint32 tmp[4], tmp2[4];
+  uint32 tmp[4], tmp2[4], tiv[4];
   int i;
 
-  in = (uint32 *)src;
-  out = (uint32 *)dst;
-  tiv = (uint32 *)iv;
-
-  tmp[0] = in[0];
-  tmp[1] = in[1];
-  tmp[2] = in[2];
-  tmp[3] = in[3];
-  rc6_decrypt((RC6Context *)context, in, out);
-  out[0] ^= tiv[0];
-  out[1] ^= tiv[1];
-  out[2] ^= tiv[2];
-  out[3] ^= tiv[3];
-  in += 4;
-  out += 4;
+  SILC_CBC_GET_IV(tiv, iv);
+
+  SILC_CBC_DEC_PRE(tmp, src);
+  rc6_decrypt((RC6Context *)context, tmp, tmp2);
+  SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
 
   for (i = 16; i < len; i += 16) {
-    tmp2[0] = tmp[0];
-    tmp2[1] = tmp[1];
-    tmp2[2] = tmp[2];
-    tmp2[3] = tmp[3];
-    tmp[0] = in[0];
-    tmp[1] = in[1];
-    tmp[2] = in[2];
-    tmp[3] = in[3];
-    rc6_decrypt((RC6Context *)context, in, out);
-    out[0] ^= tmp2[0];
-    out[1] ^= tmp2[1];
-    out[2] ^= tmp2[2];
-    out[3] ^= tmp2[3];
-    in += 4;
-    out += 4;
+    SILC_CBC_DEC_PRE(tmp, src);
+    rc6_decrypt((RC6Context *)context, tmp, tmp2);
+    SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
   }
-
-  tiv[0] = tmp[0];
-  tiv[1] = tmp[1];
-  tiv[2] = tmp[2];
-  tiv[3] = tmp[3];
+  
+  SILC_CBC_PUT_IV(tiv, iv);
 
   return TRUE;
 }
diff --git a/lib/silccrypt/tests/inst b/lib/silccrypt/tests/inst
deleted file mode 100644 (file)
index 06485bd..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-gcc -I.. \
--I../../../includes -I../../silccore \
--I../.. -I../../silccore -I../../silcmath \
--I../../silcmath/gmp-2.0.2 -I../../silcske -I../../silcsim \
--Wall -finline-functions \
--o test_rsa test_rsa.c -L../.. -lsilc
-
diff --git a/lib/silccrypt/tests/inst_aes b/lib/silccrypt/tests/inst_aes
new file mode 100644 (file)
index 0000000..67855a3
--- /dev/null
@@ -0,0 +1,7 @@
+gcc -I.. \
+-I../../../includes -I../../silccore -I../../trq -I../../silcske \
+-I../.. -I../../silccore -I../../silcmath -I../../silcutil \
+-I../../silcmath/gmp -I../../silcske -I../../silcsim \
+-Wall -finline-functions \
+-o test_aes test_aes.c -L../.. -lsilc
+
diff --git a/lib/silccrypt/tests/inst_rsa b/lib/silccrypt/tests/inst_rsa
deleted file mode 100644 (file)
index 06485bd..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-gcc -I.. \
--I../../../includes -I../../silccore \
--I../.. -I../../silccore -I../../silcmath \
--I../../silcmath/gmp-2.0.2 -I../../silcske -I../../silcsim \
--Wall -finline-functions \
--o test_rsa test_rsa.c -L../.. -lsilc
-
diff --git a/lib/silccrypt/tests/insth b/lib/silccrypt/tests/insth
deleted file mode 100644 (file)
index 30e225d..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-gcc -I../ 
--I../../../includes -I../../silccore \
--I../
--Wall -finline-functions
--o test_rsa test_rsa.c -L../.. -lsilc
-
diff --git a/lib/silccrypt/tests/test_aes.c b/lib/silccrypt/tests/test_aes.c
new file mode 100644 (file)
index 0000000..7f7d81b
--- /dev/null
@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "silcincludes.h"
+
+#include "aes.h"
+
+int main()
+{
+       int i;
+       unsigned char key[256];
+       unsigned char plain[256];
+       unsigned char plain2[256];
+       unsigned char cipher[256];
+       unsigned char iv[256];
+       void *context;
+
+       memset(&key, 0, sizeof(key));
+       memset(&plain, 0, sizeof(plain));
+       memset(&plain2, 0, sizeof(plain2));
+       memset(&cipher, 0, sizeof(cipher));
+       memset(&iv, 0, sizeof(iv));
+
+       context = malloc(silc_aes_context_len());
+
+       fprintf(stderr, "\nKey:\n");
+       for (i = 0; i < (sizeof(key) / 2); i += 2) {
+               fprintf(stderr, "%02x%02x ", key[i], key[i+1]);
+       }
+
+       fprintf(stderr, "\nSetting key\n");
+       silc_aes_set_key(context, key, 256);
+
+       fprintf(stderr, "\nPlaintext:\n");
+       for (i = 0; i < (sizeof(plain) / 2); i += 2) {
+               plain[i] = i;
+               plain[i+1] = i+1;
+               fprintf(stderr, "%02x%02x ", plain[i], plain[i+1]);
+       }
+
+       fprintf(stderr, "\n\nEncrypting\n");
+       silc_aes_encrypt_cbc(context, plain, cipher, 256, iv);
+
+       fprintf(stderr, "Ciphertext:\n");
+       for (i = 0; i < (sizeof(cipher)/2); i += 2) {
+               fprintf(stderr, "%02x", cipher[i]);
+               fprintf(stderr, "%02x ", cipher[i+1]);
+       }
+
+       memset(&iv, 0, sizeof(iv));
+
+       fprintf(stderr, "\n\nDecrypting\n");
+       silc_aes_decrypt_cbc(context, cipher, plain2, 256, iv);
+
+       fprintf(stderr, "Decryptedtext:\n");
+       for (i = 0; i < (sizeof(plain2)/2); i += 2) {
+               fprintf(stderr, "%02x", plain2[i]);
+               fprintf(stderr, "%02x ", plain2[i+1]);
+       }
+       fprintf(stderr, "\nDone\n");
+
+       return 0;
+}
diff --git a/lib/silccrypt/tests/test_rijndael.c b/lib/silccrypt/tests/test_rijndael.c
deleted file mode 100644 (file)
index ac07f54..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-main()
-{
-       int i, k;
-       unsigned char key[256];
-       unsigned char plain[256];
-       unsigned char plain2[256];
-       unsigned char cipher[256];
-       memset(&key, 0, sizeof(key));
-       memset(&plain, 0, sizeof(plain));
-       memset(&plain2, 0, sizeof(plain2));
-       memset(&cipher, 0, sizeof(cipher));
-
-       fprintf(stderr, "\nKey:\n");
-       for (i = 0; i < sizeof(key) / 2; i++) {
-               key[i] = i;
-               key[i+1] = i+1;
-               fprintf(stderr, "%02x%02x ", key[i], key[i+1]);
-       }
-
-       fprintf(stderr, "\nSetting key\n");
-       set_key(key, 128);
-
-       fprintf(stderr, "\nPlaintext:\n");
-       for (i = 0; i < sizeof(plain) / 2; i++) {
-               plain[i] = i;
-               plain[i+1] = i+1;
-               fprintf(stderr, "%02x%02x ", plain[i], plain[i+1]);
-       }
-
-       fprintf(stderr, "Encrypting\n");
-       encrypt(plain, cipher);
-
-       fprintf(stderr, "\nCiphertext:\n");
-       for (i = 0; i < sizeof(cipher); i++) {
-               fprintf(stderr, "%02x", cipher[i]);
-       }
-
-       fprintf(stderr, "Decrypting\n");
-       decrypt(cipher, plain2);
-
-       fprintf(stderr, "\nDecryptedtext:\n");
-       for (i = 0; i < sizeof(plain2); i++) {
-               fprintf(stderr, "%02x", plain2[i]);
-       }
-
-}
diff --git a/lib/silccrypt/tests/test_rsa.c b/lib/silccrypt/tests/test_rsa.c
deleted file mode 100644 (file)
index d8ff4df..0000000
+++ /dev/null
@@ -1,90 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "silcincludes.h"
-#include "rsa.h"
-#include "rsa_internal.h"
-
-void testi(SilcRng rng, void *context)
-{
-        char *numbuf;
-        unsigned int bytes;
-        unsigned int i;
-        MP_INT tnum;            /* number we'll encrypt */
-        MP_INT test;            /* en/decrypted result of tnum */
-       RsaKey *key = (RsaKey *)context;
-       int bits = 1024;        
-
-        numbuf = (char *)malloc((bits / 3) + 1);
-        bytes = bits / 10;
-            
-        mpz_init(&tnum);
-        mpz_init(&test);
-        
-        fprintf(stderr, "\nTesting encryption and decryption ... ");
-
-        for(i = 0; i < bytes; i++)
-            sprintf(numbuf + 2 * i, "%02x", silc_rng_get_byte(rng));
-        
-        mpz_set_str(&tnum, numbuf, 16);
-
-        /* empty buffer */
-        memset(numbuf, 0, bits / 3);
-        free(numbuf);
-
-        /* make tnum smaller than n */
-        mpz_div_ui(&tnum, &tnum, 10);
-        /* encrypt */
-        rsa_en_de_crypt(&test, &tnum, &key->e, &key->n);
-        /* decrypt */
-        rsa_en_de_crypt(&test, &test, &key->d, &key->n);
-        /* see if decrypted result is same than the original one is */
-        if (mpz_cmp(&test, &tnum) != 0) {
-            fprintf(stderr, "Error in encryption and decryption!\n");
-            return -1;
-        }
-
-        mpz_clear(&tnum);
-        mpz_clear(&test);
-
-        fprintf(stderr, "Keys are Ok.\n");
-}
-
-int main()
-{
-       SilcPKCS pkcs;
-       SilcRng rng;
-       unsigned char *pk, *prv;
-       unsigned int pk_len, prv_len;
-       unsigned char *src, *dst, *new;
-       unsigned int src_len, dst_len, new_len;
-       SilcInt tnum, test;
-
-       silc_pkcs_alloc("rsa", &pkcs);
-
-       rng = silc_rng_alloc();
-       silc_rng_init(rng);
-       silc_math_primegen_init();
-
-       pkcs->pkcs->init(pkcs->context, 1024, rng);
-       
-       pk = silc_pkcs_get_public_key(pkcs, &pk_len);
-       prv = silc_pkcs_get_public_key(pkcs, &prv_len);
-
-       src = "PEKKA RIIKONEN";
-       src_len = 5;
-       dst = silc_calloc(200, sizeof(unsigned char));
-       pkcs->pkcs->encrypt(pkcs->context, src, src_len, dst, &dst_len);
-
-       SILC_LOG_HEXDUMP(("src"), src, src_len);
-       SILC_LOG_HEXDUMP(("dst"), dst, dst_len);
-
-       new = silc_calloc(200, sizeof(unsigned char));
-       pkcs->pkcs->decrypt(pkcs->context, dst, dst_len, new, &new_len);
-
-       SILC_LOG_HEXDUMP(("new"), new, new_len);
-
-       testi(rng, pkcs->context);
-
-       return 0;
-}
index 137f91727d616c5bd0b5cb725a75fc0cc045b912..cb5daec4b30d86fbb6a8dc447af5c2460528372b 100644 (file)
@@ -37,12 +37,6 @@ int main()
                fprintf(stderr, "%02x%02x ", plain[i], plain[i+1]);
        }
 
-       fprintf(stderr, "IV:\n");
-       for (i = 0; i < (sizeof(iv)/2); i += 2) {
-               fprintf(stderr, "%02x", iv[i]);
-               fprintf(stderr, "%02x ", iv[i+1]);
-       }
-
        fprintf(stderr, "\n\nEncrypting\n");
        silc_twofish_encrypt_cbc(context, plain, cipher, 256, iv);
 
@@ -52,21 +46,11 @@ int main()
                fprintf(stderr, "%02x ", cipher[i+1]);
        }
 
-       fprintf(stderr, "IV:\n");
-       for (i = 0; i < (sizeof(iv)/2); i += 2) {
-               fprintf(stderr, "%02x", iv[i]);
-               fprintf(stderr, "%02x ", iv[i+1]);
-       }
+       memset(&iv, 0, sizeof(iv));
 
        fprintf(stderr, "\n\nDecrypting\n");
        silc_twofish_decrypt_cbc(context, cipher, plain2, 256, iv);
 
-       fprintf(stderr, "IV:\n");
-       for (i = 0; i < (sizeof(iv)/2); i += 2) {
-               fprintf(stderr, "%02x", iv[i]);
-               fprintf(stderr, "%02x ", iv[i+1]);
-       }
-
        fprintf(stderr, "Decryptedtext:\n");
        for (i = 0; i < (sizeof(plain2)/2); i += 2) {
                fprintf(stderr, "%02x", plain2[i]);
index 88e838138a3207cf26fc88bf890864237069add7..1dadee2595b146fdda33728a0c80037f1fc8f409 100644 (file)
@@ -50,8 +50,12 @@ Mean:          378 cycles =    67.8 mbits/sec
 
 SILC_CIPHER_API_SET_KEY(twofish)
 {
-  twofish_set_key((TwofishContext *)context, (uint32 *)key, keylen);
-  return 1;
+  uint32 k[8];
+
+  SILC_GET_WORD_KEY(key, k, keylen);
+  twofish_set_key((TwofishContext *)context, k, keylen);
+
+  return TRUE;
 }
 
 /* Sets the string as a new key for the cipher. The string is first
@@ -59,15 +63,7 @@ SILC_CIPHER_API_SET_KEY(twofish)
 
 SILC_CIPHER_API_SET_KEY_WITH_STRING(twofish)
 {
-  /*  unsigned char key[md5_hash_len];
-  SilcMarsContext *ctx = (SilcMarsContext *)context;
-
-  make_md5_hash(string, &key);
-  memcpy(&ctx->key, mars_set_key(&key, keylen), keylen);
-  memset(&key, 'F', sizeoof(key));
-  */
-
-  return 1;
+  return FALSE;
 }
 
 /* Returns the size of the cipher context. */
@@ -82,36 +78,22 @@ SILC_CIPHER_API_CONTEXT_LEN(twofish)
 
 SILC_CIPHER_API_ENCRYPT_CBC(twofish)
 {
-  uint32 *in, *out, *tiv;
-  uint32 tmp[4];
+  uint32 tiv[4];
   int i;
 
-  in = (uint32 *)src;
-  out = (uint32 *)dst;
-  tiv = (uint32 *)iv;
+  SILC_CBC_GET_IV(tiv, iv);
 
-  tmp[0] = in[0] ^ tiv[0];
-  tmp[1] = in[1] ^ tiv[1];
-  tmp[2] = in[2] ^ tiv[2];
-  tmp[3] = in[3] ^ tiv[3];
-  twofish_encrypt((TwofishContext *)context, tmp, out);
-  in += 4;
-  out += 4;
+  SILC_CBC_ENC_PRE(tiv, src);
+  twofish_encrypt((TwofishContext *)context, tiv, tiv);
+  SILC_CBC_ENC_POST(tiv, dst, src);
 
   for (i = 16; i < len; i += 16) {
-    tmp[0] = in[0] ^ out[0 - 4];
-    tmp[1] = in[1] ^ out[1 - 4];
-    tmp[2] = in[2] ^ out[2 - 4];
-    tmp[3] = in[3] ^ out[3 - 4];
-    twofish_encrypt((TwofishContext *)context, tmp, out);
-    in += 4;
-    out += 4;
+    SILC_CBC_ENC_PRE(tiv, src);
+    twofish_encrypt((TwofishContext *)context, tiv, tiv);
+    SILC_CBC_ENC_POST(tiv, dst, src);
   }
 
-  tiv[0] = out[0 - 4];
-  tiv[1] = out[1 - 4];
-  tiv[2] = out[2 - 4];
-  tiv[3] = out[3 - 4];
+  SILC_CBC_PUT_IV(tiv, iv);
 
   return TRUE;
 }
@@ -121,49 +103,23 @@ SILC_CIPHER_API_ENCRYPT_CBC(twofish)
 
 SILC_CIPHER_API_DECRYPT_CBC(twofish)
 {
-  uint32 *tiv, *in, *out;
-  uint32 tmp[4], tmp2[4];
+  uint32 tmp[4], tmp2[4], tiv[4];
   int i;
 
-  in = (uint32 *)src;
-  out = (uint32 *)dst;
-  tiv = (uint32 *)iv;
-
-  tmp[0] = in[0];
-  tmp[1] = in[1];
-  tmp[2] = in[2];
-  tmp[3] = in[3];
-  twofish_decrypt((TwofishContext *)context, in, out);
-  out[0] ^= tiv[0];
-  out[1] ^= tiv[1];
-  out[2] ^= tiv[2];
-  out[3] ^= tiv[3];
-  in += 4;
-  out += 4;
+  SILC_CBC_GET_IV(tiv, iv);
+
+  SILC_CBC_DEC_PRE(tmp, src);
+  twofish_decrypt((TwofishContext *)context, tmp, tmp2);
+  SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
 
   for (i = 16; i < len; i += 16) {
-    tmp2[0] = tmp[0];
-    tmp2[1] = tmp[1];
-    tmp2[2] = tmp[2];
-    tmp2[3] = tmp[3];
-    tmp[0] = in[0];
-    tmp[1] = in[1];
-    tmp[2] = in[2];
-    tmp[3] = in[3];
-    twofish_decrypt((TwofishContext *)context, in, out);
-    out[0] ^= tmp2[0];
-    out[1] ^= tmp2[1];
-    out[2] ^= tmp2[2];
-    out[3] ^= tmp2[3];
-    in += 4;
-    out += 4;
+    SILC_CBC_DEC_PRE(tmp, src);
+    twofish_decrypt((TwofishContext *)context, tmp, tmp2);
+    SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
   }
-
-  tiv[0] = tmp[0];
-  tiv[1] = tmp[1];
-  tiv[2] = tmp[2];
-  tiv[3] = tmp[3];
-
+  
+  SILC_CBC_PUT_IV(tiv, iv);
+  
   return TRUE;
 }