From 80b4db88b6f302b1ff088f4312c7b4e9d4aa1fb4 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Wed, 11 Apr 2001 08:55:03 +0000 Subject: [PATCH] updates. --- CHANGES | 7 ++ includes/bitmove.h | 66 +++++++++--------- lib/silccrypt/aes.c | 6 +- lib/silccrypt/ciphers_def.h | 74 ++++++++++++++++++++ lib/silccrypt/mars.c | 84 +++++++---------------- lib/silccrypt/rc6.c | 90 ++++++++----------------- lib/silccrypt/tests/inst | 7 -- lib/silccrypt/tests/inst_aes | 7 ++ lib/silccrypt/tests/inst_rsa | 7 -- lib/silccrypt/tests/insth | 6 -- lib/silccrypt/tests/test_aes.c | 62 +++++++++++++++++ lib/silccrypt/tests/test_rijndael.c | 49 -------------- lib/silccrypt/tests/test_rsa.c | 90 ------------------------- lib/silccrypt/tests/test_twofish.c | 18 +---- lib/silccrypt/twofish.c | 100 ++++++++-------------------- 15 files changed, 266 insertions(+), 407 deletions(-) delete mode 100644 lib/silccrypt/tests/inst create mode 100644 lib/silccrypt/tests/inst_aes delete mode 100644 lib/silccrypt/tests/inst_rsa delete mode 100644 lib/silccrypt/tests/insth create mode 100644 lib/silccrypt/tests/test_aes.c delete mode 100644 lib/silccrypt/tests/test_rijndael.c delete mode 100644 lib/silccrypt/tests/test_rsa.c diff --git a/CHANGES b/CHANGES index df904e41..7e4fd78e 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,10 @@ +Wed Apr 11 16:59:59 EEST 2001 Pekka Riikonen + + * 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 * Fixed the make install. diff --git a/includes/bitmove.h b/includes/bitmove.h index aca83a4a..8c765cce 100644 --- a/includes/bitmove.h +++ b/includes/bitmove.h @@ -22,51 +22,51 @@ #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 diff --git a/lib/silccrypt/aes.c b/lib/silccrypt/aes.c index 163c51fa..568881d6 100644 --- a/lib/silccrypt/aes.c +++ b/lib/silccrypt/aes.c @@ -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; } diff --git a/lib/silccrypt/ciphers_def.h b/lib/silccrypt/ciphers_def.h index 085a9806..499b7db8 100644 --- a/lib/silccrypt/ciphers_def.h +++ b/lib/silccrypt/ciphers_def.h @@ -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 diff --git a/lib/silccrypt/mars.c b/lib/silccrypt/mars.c index addb8bd0..454b402a 100644 --- a/lib/silccrypt/mars.c +++ b/lib/silccrypt/mars.c @@ -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; } diff --git a/lib/silccrypt/rc6.c b/lib/silccrypt/rc6.c index 03c9cc56..07997d8f 100644 --- a/lib/silccrypt/rc6.c +++ b/lib/silccrypt/rc6.c @@ -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 index 06485bda..00000000 --- a/lib/silccrypt/tests/inst +++ /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 index 00000000..67855a35 --- /dev/null +++ b/lib/silccrypt/tests/inst_aes @@ -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 index 06485bda..00000000 --- a/lib/silccrypt/tests/inst_rsa +++ /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 index 30e225d8..00000000 --- a/lib/silccrypt/tests/insth +++ /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 index 00000000..7f7d81bc --- /dev/null +++ b/lib/silccrypt/tests/test_aes.c @@ -0,0 +1,62 @@ +#include +#include +#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 index ac07f547..00000000 --- a/lib/silccrypt/tests/test_rijndael.c +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include - -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 index d8ff4df4..00000000 --- a/lib/silccrypt/tests/test_rsa.c +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include - -#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; -} diff --git a/lib/silccrypt/tests/test_twofish.c b/lib/silccrypt/tests/test_twofish.c index 137f9172..cb5daec4 100644 --- a/lib/silccrypt/tests/test_twofish.c +++ b/lib/silccrypt/tests/test_twofish.c @@ -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]); diff --git a/lib/silccrypt/twofish.c b/lib/silccrypt/twofish.c index 88e83813..1dadee25 100644 --- a/lib/silccrypt/twofish.c +++ b/lib/silccrypt/twofish.c @@ -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; } -- 2.24.0