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