2 cipher.c -- wrapper functions for encryption algorithms
3 Copyright (C) 1999,2000 Ivo Timmermans <zarq@iname.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "blowfish/blowfish.h"
32 void (*blowfish_cfb64_encrypt) (unsigned char*, unsigned char*, int,
33 BF_KEY*, unsigned char*, int*, int) = NULL;
34 void (*blowfish_set_key) (BF_KEY*, int, char*) = NULL;
36 unsigned char initvec[] = { 0x22, 0x7b, 0xad, 0x55, 0x41, 0xf4, 0x3e, 0xf3 };
37 BF_KEY encryption_key;
39 void low_crypt_key(unsigned char *in, unsigned char *out, BF_KEY *k, long len, int c)
42 unsigned char ivec[8];
44 memcpy(ivec, initvec, 8);
46 blowfish_cfb64_encrypt(in, out, len, k, &ivec[0], &count, c);
49 void do_encrypt(vpn_packet_t *in, real_packet_t *out, enc_key_t *key)
51 unsigned char ivec[8];
54 memcpy(ivec, initvec, 8);
55 cipher_set_key(&encryption_key, key->length, key->key);
56 low_crypt_key((char*)(&in->data), (char*)(&out->data.data),
57 &encryption_key, in->len, BF_ENCRYPT);
59 out->len = in->len + 2;
60 r = (in->len + 2) % 8;
64 /* The smallest multiple of 8 greater
65 than or equal to in->len + 8 */
67 out->data.len = in->len;
70 void do_decrypt(real_packet_t *in, vpn_packet_t *out, enc_key_t *key)
72 unsigned char ivec[8];
74 memcpy(ivec, initvec, 8);
75 cipher_set_key(&encryption_key, key->length, key->key);
76 low_crypt_key((char*)(&in->data.data), (char*)(&out->data),
77 &encryption_key, in->data.len, BF_DECRYPT);
78 out->len = in->data.len;
81 void cipher_set_key(BF_KEY *k, int l, char *t)
83 blowfish_set_key(k, l, t);
86 int cipher_init(int which)
91 if((dlhandle = dlopen(PKGLIBDIR "libblowfish.so.0", RTLD_LAZY)) == NULL)
93 syslog(LOG_ERR, "%s: %m", PKGLIBDIR "libblowfish.so.0");
97 blowfish_cfb64_encrypt = dlsym(dlhandle, "BF_cfb64_encrypt");
98 if((error = dlerror()) != NULL)
100 syslog(LOG_ERR, "%s", error);
103 blowfish_set_key = dlsym(dlhandle, "BF_set_key");