From: Pekka Riikonen Date: Wed, 2 May 2001 18:50:29 +0000 (+0000) Subject: updates. X-Git-Tag: SILC.0.2.1 X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=commitdiff_plain;h=4a474e9a2bd781802b9adfc7cac1f32e5e1c4704 updates. --- diff --git a/CHANGES b/CHANGES index eabc3f06..9cf38b37 100644 --- a/CHANGES +++ b/CHANGES @@ -7,6 +7,9 @@ Wed May 2 20:50:49 EEST 2001 Pekka Riikonen than the new one. For now, the client keys are saved with the new filename format. The affected file silc/client_ops.c. + * Implemented the Cipher API for the rest of the ciphers that + did not implement it or implemented it the wrong way. + Wed May 2 13:31:26 EEST 2001 Pekka Riikonen * Register default ciphers and stuff when using the -S option diff --git a/TODO b/TODO index 1dbb0e64..266b3f52 100644 --- a/TODO +++ b/TODO @@ -57,8 +57,7 @@ TODO/bugs In SILC Server TODO/bugs In SILC Libraries =========================== - o Some of the ciphers in lib/silccrypt does not implement the SILC - Crypto API correctly. + o IPv6 support for ID's and into the code. o Compression routines are missing. The protocol supports packet compression thus it must be implemented. SILC Comp API must be @@ -66,7 +65,8 @@ TODO/bugs In SILC Libraries not in distribution), but it is not used yet, and it requires some tweaking on the Makefiles (we want static lib not shared). - o IPv6 support for ID's and into the code. + o The CAST cipher is not compiled currently due to compilation errors; + check those. Cast is in lib/silccrypt/cast.c. TODO After 1.0 diff --git a/apps/silc/client_ops.c b/apps/silc/client_ops.c index 59079fb4..23c0b720 100644 --- a/apps/silc/client_ops.c +++ b/apps/silc/client_ops.c @@ -1015,7 +1015,7 @@ int silc_verify_public_key(SilcClient client, "server" : "client"); if (pk_type != SILC_SKE_PK_TYPE_SILC) { - silc_say(client, conn, "We don't support %s key type %d", + silc_say(client, conn, "We don't support %s public key type %d", entity, pk_type); return FALSE; } @@ -1024,19 +1024,29 @@ int silc_verify_public_key(SilcClient client, if (!pw) return FALSE; - /* Replace all whitespaces with `_'. */ - fingerprint = silc_hash_fingerprint(NULL, pk, pk_len); - for (i = 0; i < strlen(fingerprint); i++) - if (fingerprint[i] == ' ') - fingerprint[i] = '_'; - memset(filename, 0, sizeof(filename)); memset(file, 0, sizeof(file)); - snprintf(file, sizeof(file) - 1, "%skey_%s.pub", entity, fingerprint); - snprintf(filename, sizeof(filename) - 1, "%s/.silc/%skeys/%s", - pw->pw_dir, entity, file); - silc_free(fingerprint); + if (conn_type == SILC_SOCKET_TYPE_SERVER || + conn_type == SILC_SOCKET_TYPE_ROUTER) { + snprintf(file, sizeof(file) - 1, "%skey_%s_%d.pub", entity, + conn->sock->hostname, conn->sock->port); + snprintf(filename, sizeof(filename) - 1, "%s/.silc/%skeys/%s", + pw->pw_dir, entity, file); + } else { + /* Replace all whitespaces with `_'. */ + fingerprint = silc_hash_fingerprint(NULL, pk, pk_len); + for (i = 0; i < strlen(fingerprint); i++) + if (fingerprint[i] == ' ') + fingerprint[i] = '_'; + + snprintf(file, sizeof(file) - 1, "%skey_%s.pub", entity, fingerprint); + snprintf(filename, sizeof(filename) - 1, "%s/.silc/%skeys/%s", + pw->pw_dir, entity, file); + silc_free(fingerprint); + } + + /* Take fingerprint of the public key */ fingerprint = silc_hash_fingerprint(NULL, pk, pk_len); /* Check whether this key already exists */ diff --git a/apps/silc/clientutil.c b/apps/silc/clientutil.c index 95a37422..1bcc9c8b 100644 --- a/apps/silc/clientutil.c +++ b/apps/silc/clientutil.c @@ -357,7 +357,7 @@ New pair of keys will be created. Please, answer to following questions.\n\ } if (!silc_pkcs_is_supported(pkcs_name)) { - fprintf(stderr, "Unsupported PKCS `%s'", pkcs_name); + fprintf(stderr, "Unknown PKCS `%s'", pkcs_name); return FALSE; } diff --git a/apps/silcd/silcd.c b/apps/silcd/silcd.c index 186deb82..482df6b4 100644 --- a/apps/silcd/silcd.c +++ b/apps/silcd/silcd.c @@ -138,6 +138,10 @@ int main(int argc, char **argv) if (opt_create_keypair == TRUE) { /* Create new key pair and exit */ + silc_cipher_register_default(); + silc_pkcs_register_default(); + silc_hash_register_default(); + silc_hmac_register_default(); silc_server_create_key_pair(opt_pkcs, opt_bits, opt_keypath, NULL, NULL, NULL); exit(0); diff --git a/lib/silccrypt/Makefile.am b/lib/silccrypt/Makefile.am index 933f5e16..741b1dc0 100644 --- a/lib/silccrypt/Makefile.am +++ b/lib/silccrypt/Makefile.am @@ -22,7 +22,6 @@ noinst_LIBRARIES = libsilccrypt.a libsilccrypt_a_SOURCES = \ none.c \ - blowfish.c \ rc5.c \ rc6.c \ mars.c \ diff --git a/lib/silccrypt/aes.h b/lib/silccrypt/aes.h index 97d684d2..d505f90c 100644 --- a/lib/silccrypt/aes.h +++ b/lib/silccrypt/aes.h @@ -17,20 +17,6 @@ GNU General Public License for more details. */ -/* - * $Id$ - * $Log$ - * Revision 1.1 2001/02/26 17:32:08 priikone - * updates. - * - * Revision 1.2 2000/10/02 18:31:46 priikone - * Added rijndael (AES) to cipher list. - * - * Revision 1.1.1.1 2000/06/27 11:36:55 priikone - * Importet from internal CVS/Added Log headers. - * - * - */ #ifndef RIJNDAEL_H #define RIJNDAEL_H @@ -47,5 +33,4 @@ SILC_CIPHER_API_CONTEXT_LEN(aes); SILC_CIPHER_API_ENCRYPT_CBC(aes); SILC_CIPHER_API_DECRYPT_CBC(aes); - #endif diff --git a/lib/silccrypt/blowfish.c b/lib/silccrypt/blowfish.c index 03d381ae..cf6fe609 100644 --- a/lib/silccrypt/blowfish.c +++ b/lib/silccrypt/blowfish.c @@ -36,6 +36,91 @@ #include "silcincludes.h" #include "blowfish.h" +/* + * SILC Crypto API for Blowfish + */ + +/* Sets the key for the cipher. */ + +SILC_CIPHER_API_SET_KEY(blowfish) +{ + blowfish_set_key((BlowfishContext *)context, (unsigned char *)key, keylen); + return TRUE; +} + +/* Sets the string as a new key for the cipher. The string is first + hashed and then used as a new key. */ + +SILC_CIPHER_API_SET_KEY_WITH_STRING(blowfish) +{ + /* 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; +} + +/* Returns the size of the cipher context. */ + +SILC_CIPHER_API_CONTEXT_LEN(blowfish) +{ + return sizeof(BlowfishContext); +} + +/* Encrypts with the cipher in CBC mode. Source and destination buffers + maybe one and same. */ + +SILC_CIPHER_API_ENCRYPT_CBC(blowfish) +{ + uint32 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_CBC(blowfish) +{ + uint32 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, diff --git a/lib/silccrypt/blowfish.h b/lib/silccrypt/blowfish.h index 3bc8a18e..64634636 100644 --- a/lib/silccrypt/blowfish.h +++ b/lib/silccrypt/blowfish.h @@ -17,17 +17,6 @@ GNU General Public License for more details. */ -/* - * $Id$ - * $Log$ - * Revision 1.2 2001/04/03 19:54:10 priikone - * updates. New data types. - * - * Revision 1.1.1.1 2000/06/27 11:36:54 priikone - * Importet from internal CVS/Added Log headers. - * - * - */ #ifndef BLOWFISH_H #define BLOWFISH_H @@ -38,104 +27,10 @@ * SILC Crypto API for Blowfish */ -/* Sets the key for the cipher. */ - -SILC_CIPHER_API_SET_KEY(blowfish) -{ - blowfish_set_key((BlowfishContext *)context, - (unsigned char *)key, keylen); - return TRUE; -} - -/* Sets the string as a new key for the cipher. The string is first - hashed and then used as a new key. */ - -SILC_CIPHER_API_SET_KEY_WITH_STRING(blowfish) -{ - SilcHash hash; - unsigned char key[16]; - - silc_hash_alloc("md5", &hash); - hash->make_hash(hash, string, stringlen, key); - - blowfish_set_key((BlowfishContext *)context, key, sizeof(key)); - - silc_hash_free(hash); - memset(&key, 'F', sizeof(key)); - - return TRUE; -} - -/* Returns the size of the cipher context. */ - -SILC_CIPHER_API_CONTEXT_LEN(blowfish) -{ - return sizeof(BlowfishContext); -} - -/* Encrypts with the cipher in CBC mode. */ - -SILC_CIPHER_API_ENCRYPT_CBC(blowfish) -{ - uint32 *in, *out, *tiv; - uint32 tmp[4]; - int i; - - in = (uint32 *)src; - out = (uint32 *)dst; - tiv = (uint32 *)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]; - blowfish_encrypt((BlowfishContext *)context, tmp, out, 16); - in += 4; - out += 4; - - 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]; - blowfish_encrypt((BlowfishContext *)context, tmp, out, 16); - in += 4; - out += 4; - } - - return 1; -} - -/* Decrypts with the cipher in CBC mode. */ - -SILC_CIPHER_API_DECRYPT_CBC(blowfish) -{ - uint32 *in, *out, *tiv; - int i; - - in = (uint32 *)src; - out = (uint32 *)dst; - tiv = (uint32 *)iv; - - blowfish_decrypt((BlowfishContext *)context, in, out, 16); - out[0] ^= tiv[0]; - out[1] ^= tiv[1]; - out[2] ^= tiv[2]; - out[3] ^= tiv[3]; - in += 4; - out += 4; - - for (i = 16; i < len; i += 16) { - blowfish_decrypt((BlowfishContext *)context, in, out, 16); - out[0] ^= in[0 - 4]; - out[1] ^= in[1 - 4]; - out[2] ^= in[2 - 4]; - out[3] ^= in[3 - 4]; - in += 4; - out += 4; - } - - return 1; -} +SILC_CIPHER_API_SET_KEY(blowfish); +SILC_CIPHER_API_SET_KEY_WITH_STRING(blowfish); +SILC_CIPHER_API_CONTEXT_LEN(blowfish); +SILC_CIPHER_API_ENCRYPT_CBC(blowfish); +SILC_CIPHER_API_DECRYPT_CBC(blowfish); #endif diff --git a/lib/silccrypt/cast.c b/lib/silccrypt/cast.c index 1f066dff..565159cc 100644 --- a/lib/silccrypt/cast.c +++ b/lib/silccrypt/cast.c @@ -60,7 +60,98 @@ Mean: 674 cycles = 38.0 mbits/sec #include "silcincludes.h" #include "cast.h" + +#define io_swap +/* + * SILC Crypto API for Cast-256 + */ + +/* Sets the key for the cipher. */ + +SILC_CIPHER_API_SET_KEY(cast) +{ + uint32 k[8]; + + SILC_GET_WORD_KEY(key, k, keylen); + cast_set_key((CastContext *)context, k, keylen); + + return TRUE; +} + +/* Sets the string as a new key for the cipher. The string is first + hashed and then used as a new key. */ + +SILC_CIPHER_API_SET_KEY_WITH_STRING(cast) +{ + /* 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; +} + +/* Returns the size of the cipher context. */ + +SILC_CIPHER_API_CONTEXT_LEN(cast) +{ + return sizeof(CastContext); +} + +/* Encrypts with the cipher in CBC mode. Source and destination buffers + maybe one and same. */ + +SILC_CIPHER_API_ENCRYPT_CBC(cast) +{ + uint32 tiv[4]; + int i; + + SILC_CBC_GET_IV(tiv, iv); + + SILC_CBC_ENC_PRE(tiv, src); + cast_encrypt((CastContext *)context, tiv, tiv); + SILC_CBC_ENC_POST(tiv, dst, src); + + for (i = 16; i < len; i += 16) { + SILC_CBC_ENC_PRE(tiv, src); + cast_encrypt((CastContext *)context, tiv, tiv); + 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_CBC(cast) +{ + uint32 tmp[4], tmp2[4], tiv[4]; + int i; + + SILC_CBC_GET_IV(tiv, iv); + + SILC_CBC_DEC_PRE(tmp, src); + cast_decrypt((CastContext *)context, tmp, tmp2); + SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv); + + for (i = 16; i < len; i += 16) { + SILC_CBC_DEC_PRE(tmp, src); + cast_decrypt((CastContext *)context, tmp, tmp2); + SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv); + } + + SILC_CBC_PUT_IV(tiv, iv); + + return TRUE; +} + u4byte s_box[4][256] = { { 0x30fb40d4, 0x9fa0ff0b, 0x6beccd2f, 0x3f258c7a, 0x1e213f2f, 0x9C004dd3, @@ -244,50 +335,50 @@ u4byte s_box[4][256] = } }; -#define f1(y,x,kr,km) \ - t = rotl(km + x, kr); \ - u = s_box[0][byte(t,3)]; \ - u ^= s_box[1][byte(t,2)]; \ - u -= s_box[2][byte(t,1)]; \ - u += s_box[3][byte(t,0)]; \ +#define f1(y,x,kr,km) \ + t = rotl(km + x, kr); \ + u = s_box[0][byte(t,3)]; \ + u ^= s_box[1][byte(t,2)]; \ + u -= s_box[2][byte(t,1)]; \ + u += s_box[3][byte(t,0)]; \ y ^= u -#define f2(y,x,kr,km) \ - t = rotl(km ^ x, kr); \ - u = s_box[0][byte(t,3)]; \ - u -= s_box[1][byte(t,2)]; \ - u += s_box[2][byte(t,1)]; \ - u ^= s_box[3][byte(t,0)]; \ +#define f2(y,x,kr,km) \ + t = rotl(km ^ x, kr); \ + u = s_box[0][byte(t,3)]; \ + u -= s_box[1][byte(t,2)]; \ + u += s_box[2][byte(t,1)]; \ + u ^= s_box[3][byte(t,0)]; \ y ^= u -#define f3(y,x,kr,km) \ - t = rotl(km - x, kr); \ - u = s_box[0][byte(t,3)]; \ - u += s_box[1][byte(t,2)]; \ - u ^= s_box[2][byte(t,1)]; \ - u -= s_box[3][byte(t,0)]; \ +#define f3(y,x,kr,km) \ + t = rotl(km - x, kr); \ + u = s_box[0][byte(t,3)]; \ + u += s_box[1][byte(t,2)]; \ + u ^= s_box[2][byte(t,1)]; \ + u -= s_box[3][byte(t,0)]; \ y ^= u -#define f_rnd(x,n) \ - f1(x[2],x[3],l_key[n], l_key[n + 4]); \ - f2(x[1],x[2],l_key[n + 1],l_key[n + 5]); \ - f3(x[0],x[1],l_key[n + 2],l_key[n + 6]); \ +#define f_rnd(x,n) \ + f1(x[2],x[3],l_key[n], l_key[n + 4]); \ + f2(x[1],x[2],l_key[n + 1],l_key[n + 5]); \ + f3(x[0],x[1],l_key[n + 2],l_key[n + 6]); \ f1(x[3],x[0],l_key[n + 3],l_key[n + 7]) -#define i_rnd(x, n) \ - f1(x[3],x[0],l_key[n + 3],l_key[n + 7]); \ - f3(x[0],x[1],l_key[n + 2],l_key[n + 6]); \ - f2(x[1],x[2],l_key[n + 1],l_key[n + 5]); \ +#define i_rnd(x, n) \ + f1(x[3],x[0],l_key[n + 3],l_key[n + 7]); \ + f3(x[0],x[1],l_key[n + 2],l_key[n + 6]); \ + f2(x[1],x[2],l_key[n + 1],l_key[n + 5]); \ f1(x[2],x[3],l_key[n], l_key[n + 4]) -#define k_rnd(k,tr,tm) \ - f1(k[6],k[7],tr[0],tm[0]); \ - f2(k[5],k[6],tr[1],tm[1]); \ - f3(k[4],k[5],tr[2],tm[2]); \ - f1(k[3],k[4],tr[3],tm[3]); \ - f2(k[2],k[3],tr[4],tm[4]); \ - f3(k[1],k[2],tr[5],tm[5]); \ - f1(k[0],k[1],tr[6],tm[6]); \ +#define k_rnd(k,tr,tm) \ + f1(k[6],k[7],tr[0],tm[0]); \ + f2(k[5],k[6],tr[1],tm[1]); \ + f3(k[4],k[5],tr[2],tm[2]); \ + f1(k[3],k[4],tr[3],tm[3]); \ + f2(k[2],k[3],tr[4],tm[4]); \ + f3(k[1],k[2],tr[5],tm[5]); \ + f1(k[0],k[1],tr[6],tm[6]); \ f2(k[7],k[0],tr[7],tm[7]) /* initialise the key schedule from the user supplied key */ @@ -333,7 +424,7 @@ u4byte *cast_set_key(CastContext *ctx, } return l_key; -}; +} /* encrypt a block of text */ @@ -355,7 +446,7 @@ void cast_encrypt(CastContext *ctx, out_blk[0] = io_swap(blk[0]); out_blk[1] = io_swap(blk[1]); out_blk[2] = io_swap(blk[2]); out_blk[3] = io_swap(blk[3]); -}; +} /* decrypt a block of text */ @@ -377,5 +468,4 @@ void cast_decrypt(CastContext *ctx, out_blk[0] = io_swap(blk[0]); out_blk[1] = io_swap(blk[1]); out_blk[2] = io_swap(blk[2]); out_blk[3] = io_swap(blk[3]); -}; - +} diff --git a/lib/silccrypt/cast.h b/lib/silccrypt/cast.h index d7533d5f..9f361395 100644 --- a/lib/silccrypt/cast.h +++ b/lib/silccrypt/cast.h @@ -17,17 +17,6 @@ GNU General Public License for more details. */ -/* - * $Id$ - * $Log$ - * Revision 1.2 2001/04/03 19:54:10 priikone - * updates. New data types. - * - * Revision 1.1.1.1 2000/06/27 11:36:54 priikone - * Importet from internal CVS/Added Log headers. - * - * - */ #ifndef CAST_H #define CAST_H @@ -35,118 +24,13 @@ #include "cast_internal.h" /* - * SILC Crypto API for Cast + * SILC Crypto API for Cast-256 */ -/* Sets the key for the cipher. */ - -inline int silc_cast_init(void *context, - const unsigned char *key, - uint32 keylen) -{ - cast_set_key((CastContext *)context, (uint32 *)key, keylen); - return 1; -} - -/* Sets the string as a new key for the cipher. The string is first - hashed and then used as a new key. */ - -inline int silc_cast_set_string_as_key(void *context, - const unsigned char *string, - uint32 stringlen) -{ - /* SilcHash hash; - unsigned char key[16]; - - silc_hash_alloc("md5", &hash); - hash->make_hash(hash, string, stringlen, key); - - cast_set_key((CastContext *)context, (const u4byte *)key, sizeof(key)); - - silc_hash_free(hash); - memset(&key, 'F', sizeof(key)); - */ - return TRUE; -} - -/* Returns the size of the cipher context. */ - -inline uint32 silc_cast_context_len() -{ - return sizeof(CastContext); -} - -/* Encrypts with the cipher in CBC mode. */ - -inline int silc_cast_encrypt_cbc(void *context, - const unsigned char *src, - unsigned char *dst, - uint32 len, - unsigned char *iv) -{ - uint32 *in, *out, *tiv; - uint32 tmp[4]; - int i; - - in = (uint32 *)src; - out = (uint32 *)dst; - tiv = (uint32 *)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]; - cast_encrypt((CastContext *)context, tmp, out); - in += 4; - out += 4; - - 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]; - cast_encrypt((CastContext *)context, tmp, out); - in += 4; - out += 4; - } - - return 1; -} - -/* Decrypts with the cipher in CBC mode. */ - -inline int silc_cast_decrypt_cbc(void *context, - const unsigned char *src, - unsigned char *dst, - uint32 len, - unsigned char *iv) -{ - uint32 *in, *out, *tiv; - int i; - - in = (uint32 *)src; - out = (uint32 *)dst; - tiv = (uint32 *)iv; - - cast_decrypt((CastContext *)context, in, out); - out[0] ^= tiv[0]; - out[1] ^= tiv[1]; - out[2] ^= tiv[2]; - out[3] ^= tiv[3]; - in += 4; - out += 4; - - for (i = 16; i < len; i += 16) { - cast_decrypt((CastContext *)context, in, out); - out[0] ^= in[0 - 4]; - out[1] ^= in[1 - 4]; - out[2] ^= in[2 - 4]; - out[3] ^= in[3 - 4]; - in += 4; - out += 4; - } - - return 1; -} +SILC_CIPHER_API_SET_KEY(cast); +SILC_CIPHER_API_SET_KEY_WITH_STRING(cast); +SILC_CIPHER_API_CONTEXT_LEN(cast); +SILC_CIPHER_API_ENCRYPT_CBC(cast); +SILC_CIPHER_API_DECRYPT_CBC(cast); #endif diff --git a/lib/silccrypt/ciphers.h b/lib/silccrypt/ciphers.h index e13aa5f7..196995de 100644 --- a/lib/silccrypt/ciphers.h +++ b/lib/silccrypt/ciphers.h @@ -23,8 +23,10 @@ #include "none.h" #include "mars.h" +#include "rc5.h" #include "rc6.h" #include "twofish.h" #include "aes.h" +#include "blowfish.h" #endif diff --git a/lib/silccrypt/rc5.c b/lib/silccrypt/rc5.c index c0db8e95..f74dfd37 100644 --- a/lib/silccrypt/rc5.c +++ b/lib/silccrypt/rc5.c @@ -41,6 +41,95 @@ #include "silcincludes.h" #include "rc5.h" +/* + * SILC Crypto API for RC5 + */ + +/* Sets the key for the cipher. */ + +SILC_CIPHER_API_SET_KEY(aes) +{ + uint32 k[8]; + + SILC_GET_WORD_KEY(key, k, keylen); + rc5_set_key((RC5Context *)context, k, keylen); + + return TRUE; +} + +/* Sets the string as a new key for the cipher. The string is first + hashed and then used as a new key. */ + +SILC_CIPHER_API_SET_KEY_WITH_STRING(aes) +{ + /* 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; +} + +/* Returns the size of the cipher context. */ + +SILC_CIPHER_API_CONTEXT_LEN(aes) +{ + return sizeof(RC5Context); +} + +/* Encrypts with the cipher in CBC mode. Source and destination buffers + maybe one and same. */ + +SILC_CIPHER_API_ENCRYPT_CBC(aes) +{ + uint32 tiv[4]; + int i; + + SILC_CBC_GET_IV(tiv, iv); + + SILC_CBC_ENC_PRE(tiv, src); + rc5_encrypt((RC5Context *)context, tiv, tiv); + SILC_CBC_ENC_POST(tiv, dst, src); + + for (i = 16; i < len; i += 16) { + SILC_CBC_ENC_PRE(tiv, src); + rc5_encrypt((RC5Context *)context, tiv, tiv); + 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_CBC(aes) +{ + uint32 tmp[4], tmp2[4], tiv[4]; + int i; + + SILC_CBC_GET_IV(tiv, iv); + + SILC_CBC_DEC_PRE(tmp, src); + rc5_decrypt((RC5Context *)context, tmp, tmp2); + SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv); + + for (i = 16; i < len; i += 16) { + SILC_CBC_DEC_PRE(tmp, src); + rc5_decrypt((RC5Context *)context, tmp, tmp2); + SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv); + } + + SILC_CBC_PUT_IV(tiv, iv); + + return TRUE; +} + /* RC5 encryption */ #define RC5E(i, A, B) \ A = A ^ B; \ @@ -57,17 +146,14 @@ /* Sets RC5 key */ -int rc5_set_key(RC5Context *ctx, char *key, int key_len) +int rc5_set_key(RC5Context *ctx, const uint32 in_key[], int key_len) { - u32 *in_key = (u32 *)key; u32 i, j, k, A, B, L[c]; u32 *out_key = ctx->out_key; if (key_len < b || key_len > (2 * b)) return -1; - // key_len *= 8; - /* init L */ for (i = 0; i < key_len / w; i++) L[i] = in_key[i]; diff --git a/lib/silccrypt/rc5.h b/lib/silccrypt/rc5.h index d4dcebeb..b8f02416 100644 --- a/lib/silccrypt/rc5.h +++ b/lib/silccrypt/rc5.h @@ -17,17 +17,6 @@ GNU General Public License for more details. */ -/* - * $Id$ - * $Log$ - * Revision 1.2 2001/04/03 19:54:10 priikone - * updates. New data types. - * - * Revision 1.1.1.1 2000/06/27 11:36:54 priikone - * Importet from internal CVS/Added Log headers. - * - * - */ #ifndef RC5_H #define RC5_H @@ -38,100 +27,10 @@ * SILC Crypto API for RC5 */ -/* Sets the key for the cipher. */ - -SILC_CIPHER_API_SET_KEY(rc5) -{ - rc5_set_key((RC5Context *)context, (unsigned char *)key, keylen); - return 1; -} - -/* Sets the string as a new key for the cipher. The string is first - hashed and then used as a new key. */ - -SILC_CIPHER_API_SET_KEY_WITH_STRING(rc5) -{ - /* 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; -} - -/* Returns the size of the cipher context. */ - -SILC_CIPHER_API_CONTEXT_LEN(rc5) -{ - return sizeof(RC5Context); -} - -/* Encrypts with the cipher in CBC mode. */ - -SILC_CIPHER_API_ENCRYPT_CBC(rc5) -{ - uint32 *in, *out, *tiv; - uint32 tmp[2]; - int i; - - in = (uint32 *)src; - out = (uint32 *)dst; - tiv = (uint32 *)iv; - - tmp[0] = in[0] ^ tiv[0]; - tmp[1] = in[1] ^ tiv[1]; - rc5_encrypt((RC5Context *)context, tmp, out); - in += 2; - out += 2; - - for (i = 8; i < len; i += 8) { - tmp[0] = in[0] ^ out[0 - 2]; - tmp[1] = in[1] ^ out[1 - 2]; - rc5_encrypt((RC5Context *)context, tmp, out); - in += 2; - out += 2; - } - - return TRUE; -} - -/* Decrypts with the cipher in CBC mode. */ - -SILC_CIPHER_API_DECRYPT_CBC(rc5) -{ - uint32 *in, *out, *tiv; - uint32 tmp[2], tmp2[2]; - int i; - - in = (uint32 *)src; - out = (uint32 *)dst; - tiv = (uint32 *)iv; - - tmp[0] = in[0]; - tmp[1] = in[1]; - tmp[3] = in[3]; - rc5_decrypt((RC5Context *)context, in, out); - out[0] ^= tiv[0]; - out[1] ^= tiv[1]; - in += 2; - out += 2; - - for (i = 8; i < len; i += 8) { - tmp2[0] = tmp[0]; - tmp2[1] = tmp[1]; - tmp[0] = in[0]; - tmp[1] = in[1]; - rc5_decrypt((RC5Context *)context, in, out); - out[0] ^= tmp2[0]; - out[1] ^= tmp2[1]; - in += 2; - out += 2; - } - - return TRUE; -} +SILC_CIPHER_API_SET_KEY(rc5); +SILC_CIPHER_API_SET_KEY_WITH_STRING(rc5); +SILC_CIPHER_API_CONTEXT_LEN(rc5); +SILC_CIPHER_API_ENCRYPT_CBC(rc5); +SILC_CIPHER_API_DECRYPT_CBC(rc5); #endif diff --git a/lib/silccrypt/rc5_internal.h b/lib/silccrypt/rc5_internal.h index 94005670..c493fdc5 100644 --- a/lib/silccrypt/rc5_internal.h +++ b/lib/silccrypt/rc5_internal.h @@ -36,7 +36,7 @@ typedef struct { } RC5Context; /* Prototypes */ -int rc5_set_key(RC5Context *ctx, char *key, int key_len); +int rc5_set_key(RC5Context *ctx, const uint32 in_key[], int key_len); int rc5_encrypt(RC5Context *ctx, u32 *in, u32 *out); int rc5_decrypt(RC5Context *ctx, u32 *in, u32 *out);