+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.
#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
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;
}
#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
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;
}
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;
}
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;
}
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
SILC_CIPHER_API_SET_KEY_WITH_STRING(rc6)
{
- return 1;
+ return FALSE;
}
/* Encrypts with the cipher in CBC mode. Source and destination buffers
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;
}
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;
}
+++ /dev/null
-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
-
--- /dev/null
+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
+
+++ /dev/null
-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
-
+++ /dev/null
-gcc -I../
--I../../../includes -I../../silccore \
--I../
--Wall -finline-functions
--o test_rsa test_rsa.c -L../.. -lsilc
-
--- /dev/null
+#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;
+}
+++ /dev/null
-#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]);
- }
-
-}
+++ /dev/null
-#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;
-}
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);
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]);
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
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. */
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;
}
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;
}