-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrypto.c
279 lines (233 loc) · 6.75 KB
/
crypto.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <syslog.h>
#include <openssl/ssl.h>
#include "fastpbkdf2.h"
#include "crypto.h"
#include "config.h"
#include "common.h"
#include "debug.h"
// #define ENC_DEBUG 1
static const char *default_salt = "frp";
static const size_t block_size = 16;
static struct frp_coder *main_encoder = NULL;
static struct frp_coder *main_decoder = NULL;
size_t get_block_size()
{
return block_size;
}
// TODO: NEED free
struct frp_coder *new_coder(const char *privilege_token, const char *salt)
{
struct frp_coder *enc = calloc(sizeof(struct frp_coder), 1);
assert(enc);
enc->privilege_token = privilege_token ? strdup(privilege_token) : "\0";
assert(enc->privilege_token);
enc->key_len = block_size;
enc->salt = strdup(salt);
assert(enc->salt);
enc->key = encrypt_key(enc->privilege_token, strlen(enc->privilege_token), enc->salt);
enc->iv = calloc(block_size, 1);
encrypt_iv(enc->iv, block_size);
return enc;
}
size_t get_encrypt_block_size()
{
return block_size;
}
struct frp_coder *init_main_encoder()
{
struct common_conf *c_conf = get_common_config();
main_encoder = new_coder(c_conf->privilege_token, default_salt);
assert(main_encoder);
assert(main_encoder->key);
return main_encoder;
}
struct frp_coder *init_main_decoder(unsigned char *iv)
{
struct common_conf *c_conf = get_common_config();
main_decoder = new_coder(c_conf->privilege_token, default_salt);
assert(main_encoder);
assert(main_encoder->key);
memcpy(main_decoder->iv, iv, block_size);
return main_decoder;
}
struct frp_coder *get_main_encoder()
{
return main_encoder;
}
struct frp_coder *get_main_decoder()
{
return main_decoder;
}
int is_encoder_inited()
{
struct frp_coder *e = get_main_encoder();
return e != NULL;
}
int is_decoder_inited()
{
struct frp_coder *d = get_main_decoder();
return d != NULL;
}
// 29 201 136 254 206 150 233 65 13 82 120 149 203 228 122 128
// key_ret buffer len must be 16
// the result should be free after using
unsigned char *encrypt_key(const char *token, size_t token_len, const char *salt)
{
unsigned char *key_ret = calloc(block_size, 1);
fastpbkdf2_hmac_sha1((void *) token, token_len, (void *) salt, strlen(salt), 64,
(void *) key_ret, block_size);
/* debug */
#ifdef ENC_DEBUG
printf("encrypt_key = ");
int i = 0;
for (i = 0; i < block_size; i++) {
printf("%u ", *(key_ret + i));
}
printf("\n");
/* debug end */
#endif // ENC_DEBUG
if (!key_ret)
fprintf(stderr, "key result buffer not applied!\n");
return key_ret;
}
// the result should be free after using
unsigned char *encrypt_iv(unsigned char *iv_buf, size_t iv_len)
{
if (iv_len < block_size || iv_buf == NULL) {
return NULL;
}
srand((unsigned int) time(NULL));
size_t i;
for (i = 0; i < iv_len; i++) {
iv_buf[i] = (rand() % 254) + 1;
}
return iv_buf;
}
// using aes-128-cfb and nopadding
size_t encrypt_data(const unsigned char *src_data, size_t srclen, struct frp_coder *encoder,
unsigned char **ret)
{
unsigned char *intext = calloc(srclen, 1); // free in func
assert(intext);
memcpy(intext, src_data, srclen);
unsigned char *outbuf = calloc(srclen, 1);
assert(outbuf);
*ret = outbuf;
int outlen = 0, tmplen = 0;
struct frp_coder *c = encoder;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_EncryptInit_ex(&ctx, EVP_aes_128_cfb(), NULL, c->key, c->iv);
if (!EVP_EncryptUpdate(&ctx, outbuf, &outlen, intext, (int) srclen)) {
debug(LOG_ERR, "EVP_EncryptUpdate error!");
goto E_END;
}
if (!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) {
debug(LOG_ERR, "EVP_EncryptFinal_ex error!");
goto E_END;
}
outlen += tmplen;
EVP_CIPHER_CTX_cleanup(&ctx);
#ifdef ENC_DEBUG
int j = 0;
debug(LOG_DEBUG, "encoder iv=");
for (j = 0; j < 16; j++) {
printf("%u ", (unsigned char) c->iv[j]);
}
printf("\n");
debug(LOG_DEBUG, "encoder KEY=");
for (j = 0; j < 16; j++) {
printf("%u ", (unsigned char) c->key[j]);
}
printf("\n");
debug(LOG_DEBUG, "encoder result 10 =");
for (j = 0; j < outlen; j++) {
printf("%d ", (unsigned char) outbuf[j]);
}
printf("\n");
#endif // ENC_DEBUG
E_END:
free(intext);
return outlen;
}
size_t decrypt_data(const unsigned char *enc_data, size_t enc_len, struct frp_coder *decoder,
unsigned char **ret)
{
unsigned char *inbuf = malloc(enc_len);
assert(inbuf);
memcpy(inbuf, enc_data, enc_len);
unsigned char *outbuf = malloc(enc_len);
assert(outbuf);
*ret = outbuf;
int outlen = 0, tmplen = 0;
struct frp_coder *c = decoder;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_DecryptInit_ex(&ctx, EVP_aes_128_cfb(), NULL, c->key, c->iv);
EVP_CIPHER_CTX_set_padding(&ctx, 0);
int loop_times = enc_len / 10;
int latest_len = enc_len % 10;
int i = 0;
int totol_len = 0;
int enc_per_len;
for (i = 0; i <= loop_times; i++) {
if (i == loop_times) {
enc_per_len = latest_len;
} else {
enc_per_len = 10;
}
if (!EVP_DecryptUpdate(&ctx, outbuf + (i * 10), &outlen, inbuf + (i * 10), enc_per_len)) {
debug(LOG_ERR, "EVP_DecryptUpdate error!");
goto D_END;
}
totol_len += outlen;
}
if (!EVP_DecryptFinal_ex(&ctx, outbuf + totol_len, &tmplen)) {
debug(LOG_ERR, "EVP_DecryptFinal_ex error");
goto D_END;
}
totol_len += tmplen;
EVP_CIPHER_CTX_cleanup(&ctx);
#ifdef ENC_DEBUG
debug(LOG_DEBUG, "DEC_LEN:%lu", enc_len);
int j = 0;
debug(LOG_DEBUG, "decoder IV=");
for (j = 0; j < 16; j++) {
printf("%u ", (unsigned char) c->iv[j]);
}
printf("\n");
debug(LOG_DEBUG, "decoder KEY=");
for (j = 0; j < 16; j++) {
printf("%u ", (unsigned char) c->key[j]);
}
printf("\n");
debug(LOG_DEBUG, "decoder source=");
for (j = 0; j < enc_len; j++) {
printf("%u ", (unsigned char) inbuf[j]);
}
printf("\n");
debug(LOG_DEBUG, "decoder result=");
for (j = 0; j < totol_len; j++) {
printf("%u ", (unsigned char) (*ret)[j]);
}
printf("\n");
debug(LOG_DEBUG, "decode string=%s", outbuf);
#endif // ENC_DEBUG
D_END:
return totol_len;
}
void free_encoder(struct frp_coder *encoder)
{
if (encoder) {
SAFE_FREE(encoder->privilege_token);
SAFE_FREE(encoder->salt);
SAFE_FREE(encoder->key);
SAFE_FREE(encoder->iv);
SAFE_FREE(encoder);
}
}