5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
23 * Revision 1.2 2001/04/03 19:54:10 priikone
24 * updates. New data types.
26 * Revision 1.1.1.1 2000/06/27 11:36:54 priikone
27 * Importet from internal CVS/Added Log headers.
35 #include "blowfish_internal.h"
38 * SILC Crypto API for Blowfish
41 /* Sets the key for the cipher. */
43 SILC_CIPHER_API_SET_KEY(blowfish)
45 blowfish_set_key((BlowfishContext *)context,
46 (unsigned char *)key, keylen);
50 /* Sets the string as a new key for the cipher. The string is first
51 hashed and then used as a new key. */
53 SILC_CIPHER_API_SET_KEY_WITH_STRING(blowfish)
56 unsigned char key[16];
58 silc_hash_alloc("md5", &hash);
59 hash->make_hash(hash, string, stringlen, key);
61 blowfish_set_key((BlowfishContext *)context, key, sizeof(key));
64 memset(&key, 'F', sizeof(key));
69 /* Returns the size of the cipher context. */
71 SILC_CIPHER_API_CONTEXT_LEN(blowfish)
73 return sizeof(BlowfishContext);
76 /* Encrypts with the cipher in CBC mode. */
78 SILC_CIPHER_API_ENCRYPT_CBC(blowfish)
80 uint32 *in, *out, *tiv;
88 tmp[0] = in[0] ^ tiv[0];
89 tmp[1] = in[1] ^ tiv[1];
90 tmp[2] = in[2] ^ tiv[2];
91 tmp[3] = in[3] ^ tiv[3];
92 blowfish_encrypt((BlowfishContext *)context, tmp, out, 16);
96 for (i = 16; i < len; i += 16) {
97 tmp[0] = in[0] ^ out[0 - 4];
98 tmp[1] = in[1] ^ out[1 - 4];
99 tmp[2] = in[2] ^ out[2 - 4];
100 tmp[3] = in[3] ^ out[3 - 4];
101 blowfish_encrypt((BlowfishContext *)context, tmp, out, 16);
109 /* Decrypts with the cipher in CBC mode. */
111 SILC_CIPHER_API_DECRYPT_CBC(blowfish)
113 uint32 *in, *out, *tiv;
120 blowfish_decrypt((BlowfishContext *)context, in, out, 16);
128 for (i = 16; i < len; i += 16) {
129 blowfish_decrypt((BlowfishContext *)context, in, out, 16);