Don't compile in `idea'.
[tinc] / cipher / cipher.c
1 /*
2     cipher.c -- wrapper functions for encryption algorithms
3     Copyright (C) 1999 Ivo Timmermans <zarq@iname.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "config.h"
21
22 #include <dlfcn.h>
23 #include <string.h>
24 #include <syslog.h>
25
26 #include <cipher.h>
27
28 #include "blowfish/blowfish.h"
29 #include "idea/idea.h"
30
31 #include "net.h"
32
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;
36
37 unsigned char initvec[] = { 0x22, 0x7b, 0xad, 0x55, 0x41, 0xf4, 0x3e, 0xf3 };
38 BF_KEY encryption_key;
39
40 void low_crypt_key(unsigned char *in, unsigned char *out, BF_KEY *k, long len, int c)
41 {
42   int count = 7;
43   unsigned char ivec[8];
44
45   memcpy(ivec, initvec, 8);
46
47   blowfish_cfb64_encrypt(in, out, len, k, &ivec[0], &count, c);
48 }
49
50 void do_encrypt(vpn_packet_t *in, real_packet_t *out, enc_key_t *key)
51 {
52   unsigned char ivec[8];
53   int r;
54
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);
59   
60   out->len = in->len + 2;
61   r = (in->len + 2) % 8;
62   if(r)
63     out->len += (8-r);
64   out->len += 8;
65   /* The smallest multiple of 8 greater
66      than or equal to in->len + 8 */
67
68   out->data.len = in->len;
69 }
70
71 void do_decrypt(real_packet_t *in, vpn_packet_t *out, enc_key_t *key)
72 {
73   unsigned char ivec[8];
74
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;
80 }
81
82 void cipher_set_key(BF_KEY *k, int l, char *t)
83 {
84   blowfish_set_key(k, l, t);
85 }
86
87 int cipher_init(int which)
88 {
89   void *dlhandle;
90   char *error;
91
92   if((dlhandle = dlopen(PKGLIBDIR "libblowfish.so.0", RTLD_LAZY)) == NULL)
93     {
94       syslog(LOG_ERR, "%s: %m", PKGLIBDIR "libblowfish.so.0");
95       return -1;
96     }
97
98   blowfish_cfb64_encrypt = dlsym(dlhandle, "BF_cfb64_encrypt");
99   if((error = dlerror()) != NULL)
100     {
101       syslog(LOG_ERR, "%s", error);
102       return -1;
103     }
104   blowfish_set_key = dlsym(dlhandle, "BF_set_key");
105
106   return 0;
107 }