2 cipher.c -- wrapper functions for encryption algorithms
3 Copyright (C) 1999 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"
29 #include "idea/idea.h"
33 void (*blowfish_cfb64_encrypt) (unsigned char*, unsigned char*, int,
34 BF_KEY*, unsigned char*, int*, int) = NULL;
35 void (*blowfish_set_key) (BF_KEY*, int, char*) = NULL;
37 unsigned char initvec[] = { 0x22, 0x7b, 0xad, 0x55, 0x41, 0xf4, 0x3e, 0xf3 };
38 BF_KEY encryption_key;
40 void low_crypt_key(unsigned char *in, unsigned char *out, BF_KEY *k, long len, int c)
43 unsigned char ivec[8];
45 memcpy(ivec, initvec, 8);
47 blowfish_cfb64_encrypt(in, out, len, k, &ivec[0], &count, c);
50 void do_encrypt(vpn_packet_t *in, real_packet_t *out, enc_key_t *key)
52 unsigned char ivec[8];
55 memcpy(ivec, initvec, 8);
56 cipher_set_key(&encryption_key, key->length, key->key);
57 low_crypt_key((char*)(&in->data), (char*)(&out->data.data),
58 &encryption_key, in->len, BF_ENCRYPT);
60 out->len = in->len + 2;
61 r = (in->len + 2) % 8;
65 /* The smallest multiple of 8 greater
66 than or equal to in->len + 8 */
68 out->data.len = in->len;
71 void do_decrypt(real_packet_t *in, vpn_packet_t *out, enc_key_t *key)
73 unsigned char ivec[8];
75 memcpy(ivec, initvec, 8);
76 cipher_set_key(&encryption_key, key->length, key->key);
77 low_crypt_key((char*)(&in->data.data), (char*)(&out->data),
78 &encryption_key, in->data.len, BF_DECRYPT);
79 out->len = in->data.len;
82 void cipher_set_key(BF_KEY *k, int l, char *t)
84 blowfish_set_key(k, l, t);
87 int cipher_init(int which)
92 if((dlhandle = dlopen(PKGLIBDIR "libblowfish.so.0", RTLD_LAZY)) == NULL)
94 syslog(LOG_ERR, "%s: %m", PKGLIBDIR "libblowfish.so.0");
98 blowfish_cfb64_encrypt = dlsym(dlhandle, "BF_cfb64_encrypt");
99 if((error = dlerror()) != NULL)
101 syslog(LOG_ERR, "%s", error);
104 blowfish_set_key = dlsym(dlhandle, "BF_set_key");