-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfd_legacy_encrypt.c
154 lines (121 loc) · 3.55 KB
/
lfd_legacy_encrypt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
VTun - Virtual Tunnel over TCP/IP network.
Copyright (C) 1998-2008 Maxim Krasnyansky <[email protected]>
VTun has been derived from VPPP package by Maxim Krasnyansky.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/*
* $Id$
* Code added wholesale temporarily from lfd_encrypt 1.2.2.8
*/
/*
Encryption module uses software developed by the OpenSSL Project
for use in the OpenSSL Toolkit. (http://www.openssl.org/)
Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
*/
/*
* This lfd_encrypt module uses MD5 to create 128 bits encryption
* keys and BlowFish for actual data encryption.
* It is based on code written by Chris Todd<[email protected]> with
* several improvements and modifications.
*/
#include "config.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <strings.h>
#include <string.h>
#include "vtun.h"
#include "linkfd.h"
#include "lib.h"
#ifdef HAVE_SSL
#ifndef __APPLE_CC__
/* OpenSSL includes */
#include <openssl/md5.h>
#include <openssl/blowfish.h>
#else /* YAY - We're MAC OS */
#include <sys/md5.h>
#include <crypto/blowfish.h>
#endif /* __APPLE_CC__ */
#define ENC_BUF_SIZE VTUN_FRAME_SIZE + 16
#define ENC_KEY_SIZE 16
static BF_KEY key;
static char * enc_buf;
static int alloc_legacy_encrypt(struct vtun_host *host)
{
if( !(enc_buf = lfd_alloc(ENC_BUF_SIZE)) ){
vtun_syslog(LOG_ERR,"Can't allocate buffer for legacy encryptor");
return -1;
}
BF_set_key(&key, ENC_KEY_SIZE, MD5(host->passwd,strlen(host->passwd),NULL));
vtun_syslog(LOG_INFO, "BlowFish legacy encryption initialized");
return 0;
}
static int free_legacy_encrypt()
{
lfd_free(enc_buf); enc_buf = NULL;
return 0;
}
static int legacy_encrypt_buf(int len, char *in, char **out)
{
register int pad, p;
register char *in_ptr = in, *out_ptr = enc_buf;
/* 8 - ( len % 8 ) */
pad = (~len & 0x07) + 1; p = 8 - pad;
memset(out_ptr, 0, pad);
*out_ptr = (char) pad;
memcpy(out_ptr + pad, in_ptr, p);
BF_ecb_encrypt(out_ptr, out_ptr, &key, BF_ENCRYPT);
out_ptr += 8; in_ptr += p;
len = len - p;
for (p=0; p < len; p += 8)
BF_ecb_encrypt(in_ptr + p, out_ptr + p, &key, BF_ENCRYPT);
*out = enc_buf;
return len + 8;
}
static int legacy_decrypt_buf(int len, char *in, char **out)
{
register int p;
for (p = 0; p < len; p += 8)
BF_ecb_encrypt(in + p, in + p, &key, BF_DECRYPT);
p = *in;
if (p < 1 || p > 8) {
vtun_syslog(LOG_INFO, "legacy_decrypt_buf: bad pad length");
return 0;
}
*out = in + p;
return len - p;
}
/*
* Module structure.
*/
struct lfd_mod lfd_legacy_encrypt = {
"Encryptor",
alloc_legacy_encrypt,
legacy_encrypt_buf,
NULL,
legacy_decrypt_buf,
NULL,
free_legacy_encrypt,
NULL,
NULL
};
#else /* HAVE_SSL */
static int no_legacy_encrypt(struct vtun_host *host)
{
vtun_syslog(LOG_INFO, "Encryption is not supported");
return -1;
}
struct lfd_mod lfd_legacy_encrypt = {
"Encryptor",
no_legacy_encrypt, NULL, NULL, NULL, NULL, NULL, NULL, NULL
};
#endif /* HAVE_SSL */