X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilccrypt%2Fblowfish.c;h=fb3d8c8b65a8830cfeb728952225ad2f70bd27e1;hb=a818c5b5411bbc4436d1c5f011236985c96bb787;hp=03d381aece1538f5898555abb41e5e1b1023d12e;hpb=72cfa31520ebc0058763a30e21c13f6e9a964aa0;p=silc.git diff --git a/lib/silccrypt/blowfish.c b/lib/silccrypt/blowfish.c index 03d381ae..fb3d8c8b 100644 --- a/lib/silccrypt/blowfish.c +++ b/lib/silccrypt/blowfish.c @@ -34,8 +34,94 @@ */ #include "silcincludes.h" +#include "blowfish_internal.h" #include "blowfish.h" +/* + * SILC Crypto API for Blowfish + */ + +/* Sets the key for the cipher. */ + +SILC_CIPHER_API_SET_KEY(blowfish) +{ + 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) +{ + SilcUInt32 tiv[4]; + int i; + + SILC_CBC_GET_IV(tiv, iv); + + SILC_CBC_ENC_PRE(tiv, src); + blowfish_encrypt((BlowfishContext *)context, tiv, tiv, 16); + SILC_CBC_ENC_POST(tiv, dst, src); + + for (i = 16; i < len; i += 16) { + SILC_CBC_ENC_PRE(tiv, src); + blowfish_encrypt((BlowfishContext *)context, tiv, tiv, 16); + SILC_CBC_ENC_POST(tiv, dst, src); + } + + SILC_CBC_PUT_IV(tiv, iv); + + return TRUE; +} + +/* Decrypts with the cipher in CBC mode. Source and destination buffers + maybe one and same. */ + +SILC_CIPHER_API_DECRYPT_CBC(blowfish) +{ + SilcUInt32 tmp[4], tmp2[4], tiv[4]; + int i; + + SILC_CBC_GET_IV(tiv, iv); + + SILC_CBC_DEC_PRE(tmp, src); + blowfish_decrypt((BlowfishContext *)context, tmp, tmp2, 16); + SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv); + + for (i = 16; i < len; i += 16) { + SILC_CBC_DEC_PRE(tmp, src); + blowfish_decrypt((BlowfishContext *)context, tmp, tmp2, 16); + SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv); + } + + SILC_CBC_PUT_IV(tiv, iv); + + return TRUE; +} + static u32 bf_pbox[16 + 2] = { 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, @@ -307,7 +393,7 @@ static u32 bf_sbox[256 * 4] = /* * Round loop unrolling macros, S is a pointer to a S-Box array - * organized in 4 uint32s at a row. + * organized in 4 SilcUInt32s at a row. */ #define GET32_3(x) (((x) & 0xff))