forked from dndx/shadowsocks-libuv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.c
220 lines (194 loc) · 5.62 KB
/
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (c) 2012 dndx (idndx.com)
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "config.h"
#include <string.h>
#include <openssl/evp.h>
#include <assert.h>
#include "md5.h"
#include "encrypt.h"
#include "utils.h"
#ifdef HIGHFIRST
uint64_t swap_uint64( uint64_t val )
{
val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );
val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );
return (val << 32) | (val >> 32);
}
#endif /* BIG ENDIAN */
/*
* message must be uint8_t[16]
*/
void md5(const uint8_t *text, uint8_t *message)
{
struct MD5Context context;
MD5Init(&context);
MD5Update(&context, text, strlen((const char*)text));
MD5Final(message, &context);
}
static void merge(uint8_t *arr, int start, int end, int mid, int ei, uint64_t keynum)
{
int asize = mid - start + 1;
uint8_t a[asize];
memcpy(a, arr + start, asize);
uint8_t *b = arr;
uint8_t *result = arr;
int i = 0;
int j = mid + 1;
int imax = asize;
int jmax = end + 1;
int k = start;
while (i < imax && j < jmax) {
if (keynum % (a[i] + ei) <= keynum % (b[j] + ei)) {
result[k] = a[i];
i++;
k++;
} else {
result[k] = b[j];
j++;
k++;
}
}
while (i < imax) {
result[k] = a[i];
i++;
k++;
}
while (j < jmax) {
result[k] = b[j];
j++;
k++;
}
}
static void merge_sort(uint8_t *arr, int start, int end, int ei, uint64_t keynum)
{
if (end - start <= 0) {
return;
}
int mid = (start + end) / 2;
merge_sort(arr, start, mid, ei, keynum);
merge_sort(arr, mid + 1, end, ei, keynum);
merge(arr, start, end, mid, ei, keynum);
}
static void mergesrt(uint8_t *arr, int length, int ei, uint64_t keynum)
{
merge_sort(arr, 0, length - 1, ei, keynum);
}
/*
* encrypt_table and decrypt_table must be uint8_t[TABLE_SIZE]
*/
void make_tables(const uint8_t *key, uint8_t *encrypt_table, uint8_t *decrypt_table)
{
uint8_t digest[16];
int ei;
uint64_t keynum;
md5(key, digest);
memcpy(&keynum, digest, 8);
#ifdef HIGHFIRST
keynum = swap_uint64(keynum);
#endif /* BIG ENDIAN */
uint8_t temp_table[TABLE_SIZE];
for (ei=0; ei<TABLE_SIZE; ei++) {
temp_table[ei] = ei;
}
for (ei=1; ei<1024; ei++) {
mergesrt(temp_table, TABLE_SIZE, ei, keynum);
}
memcpy(encrypt_table, temp_table, TABLE_SIZE);
for (ei=0; ei<TABLE_SIZE; ei++) {
decrypt_table[encrypt_table[ei]] = ei;
}
}
void shadow_encrypt(uint8_t *data, struct encryptor *enc, register unsigned int length)
{
if (enc->encrypt_table) {
while (length--) {
data[length] = enc->encrypt_table[data[length]];
}
} else if (enc->rc4_en) {
rc4_crypt(enc->rc4_en, data, data, length);
} else {
FATAL("Crypto unknown!");
}
}
void shadow_decrypt(uint8_t *data, struct encryptor *enc, register unsigned int length)
{
if (enc->encrypt_table) {
while (length--) {
data[length] = enc->decrypt_table[data[length]];
}
} else if (enc->rc4_de) {
rc4_crypt(enc->rc4_de, data, data, length);
} else {
FATAL("Crypto unknown!");
}
}
static int init_rc4_key(struct encryptor *enc)
{
unsigned char *key = calloc(1, EVP_MAX_IV_LENGTH);
if (!key)
FATAL("RC4 Cliper Init Failed");
unsigned char iv[EVP_MAX_IV_LENGTH];
int key_len = EVP_BytesToKey(EVP_rc4(), EVP_md5(), NULL, (uint8_t*) enc->key, strlen((char *)enc->key), 1, key, iv);
if (!key_len)
FATAL("RC4 Cliper Init Failed");
enc->key = key;
return key_len;
}
// key should end with \0
void make_encryptor(struct encryptor *tpl, struct encryptor *enc, uint8_t method, uint8_t *key)
{
memset(enc, 0, sizeof(struct encryptor));
if (!tpl) {
if (method == METHOD_SHADOWCRYPT) {
assert(key);
enc->key = key;
enc->encrypt_table = (uint8_t *)calloc(TABLE_SIZE, sizeof(uint8_t));
enc->decrypt_table = (uint8_t *)calloc(TABLE_SIZE, sizeof(uint8_t));
if (!(enc->encrypt_table && enc->decrypt_table))
FATAL("malloc() failed!");
make_tables(key, enc->encrypt_table, enc->decrypt_table);
} else if (method == METHOD_RC4) {
assert(key);
enc->key = key;
init_rc4_key(enc);
}
} else {
assert(!method);
if (tpl->encrypt_table) {
assert(!key);
enc->encrypt_table = tpl->encrypt_table;
enc->decrypt_table = tpl->decrypt_table;
} else {
assert(!key);
assert(tpl->key);
enc->key = tpl->key;
enc->rc4_en = (struct rc4_state *)malloc(sizeof(struct rc4_state));
enc->rc4_de = (struct rc4_state *)malloc(sizeof(struct rc4_state));
if (!(enc->rc4_en && enc->rc4_de))
FATAL("malloc() failed!");
rc4_init(enc->rc4_en, enc->key, MD5_LEN);
rc4_init(enc->rc4_de, enc->key, MD5_LEN);
}
}
}
void destroy_encryptor(struct encryptor *enc)
{
if (enc->rc4_en) {
free(enc->rc4_en);
free(enc->rc4_de);
}
}