-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathencrypt.cc
More file actions
258 lines (231 loc) · 8.88 KB
/
encrypt.cc
File metadata and controls
258 lines (231 loc) · 8.88 KB
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
/* Copyright (c) 2023, 2025, GreatDB Software Co., Ltd.
All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
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, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#define MYSQL_SERVER 1
#include "encrypt.h"
#include "my_sys.h"
#include "mysql/service_mysql_alloc.h"
#include <cstring>
static char plain_buff[ENCRYPT_BUFFER_SIZE + 1] = {0};
static char encry_buffer[ENCRYPT_BUFFER_SIZE + 1] = {0};
TYPELIB set_clone_encrypt_mode_typelib = {MY_AES_END + 1, "",
my_aes_opmode_names, nullptr};
int encrypt_buffer(unsigned char *source, uint32 length, char *dest,
my_aes_opmode dump_encrypt_mode, char *dump_encrypt_key,
char *dump_encrypt_iv) {
uint32 main_len;
uint32 remain_len;
int enc_len = 0;
unsigned char remain_buf[MY_AES_BLOCK_SIZE * 2];
if (length < REMAIN_BLOCK_SIZE || length >= ENCRYPT_BUFFER_SIZE) return -1;
main_len = (length / MY_AES_BLOCK_SIZE) * MY_AES_BLOCK_SIZE;
remain_len = length - main_len;
enc_len =
my_aes_encrypt(reinterpret_cast<unsigned char *>(source), main_len,
reinterpret_cast<unsigned char *>(dest),
reinterpret_cast<unsigned char *>(dump_encrypt_key),
strlen(dump_encrypt_key), dump_encrypt_mode,
reinterpret_cast<unsigned char *>(dump_encrypt_iv), false);
if ((uint32)enc_len != main_len) return -1;
if (remain_len != 0) {
memcpy(dest + main_len, source + main_len, length - main_len);
remain_len = MY_AES_BLOCK_SIZE * 2;
enc_len = my_aes_encrypt(
reinterpret_cast<unsigned char *>(dest + length - remain_len),
remain_len, remain_buf,
reinterpret_cast<unsigned char *>(dump_encrypt_key),
strlen(dump_encrypt_key), dump_encrypt_mode,
reinterpret_cast<unsigned char *>(dump_encrypt_iv), false);
if ((uint32)enc_len != remain_len) return -1;
memcpy(dest + length - remain_len, remain_buf, remain_len);
}
return length;
}
/* Key file has 3 lines.
* First line: aes_opmode, max_size 32
* Second line: encrypt key, max_size 256
* Third line: aes_ebc encrypt iv, size 16
*/
int read_clone_encrypt_key(const char *opt_clone_encrypt_key,
my_aes_opmode *clone_encrypt_mode,
char **clone_encrypt_key, char **clone_encrypt_iv) {
int c;
int read_stage = 0;
char *read_pos = nullptr;
char *end_pos = nullptr;
bool got_error = false;
char *clone_encrypt_mode_str = nullptr;
FILE *file = fopen(opt_clone_encrypt_key, "r");
if (file == nullptr) {
return 1;
}
c = fgetc(file);
while (c != EOF) {
if (c == '\n') {
++read_stage;
if (read_pos != nullptr) *read_pos = 0;
} else {
if (read_stage == 0 && clone_encrypt_mode_str == nullptr) {
if (!(clone_encrypt_mode_str = (char *)my_malloc(
PSI_NOT_INSTRUMENTED, CLONE_ENCRYPT_MODE_SIZE + 1,
MYF(MY_WME)))) {
fclose(file);
return 2;
}
read_pos = clone_encrypt_mode_str;
end_pos = clone_encrypt_mode_str + CLONE_ENCRYPT_MODE_SIZE;
} else if (read_stage == 1 && *clone_encrypt_key == nullptr) {
if (!(*clone_encrypt_key =
(char *)my_malloc(PSI_NOT_INSTRUMENTED,
MAX_ENCRYPT_KEY_SIZE + 1, MYF(MY_WME)))) {
my_free(clone_encrypt_mode_str);
fclose(file);
return 2;
}
read_pos = *clone_encrypt_key;
end_pos = *clone_encrypt_key + MAX_ENCRYPT_KEY_SIZE;
} else if (read_stage == 2 && *clone_encrypt_iv == nullptr) {
if (!(*clone_encrypt_iv = (char *)my_malloc(
PSI_NOT_INSTRUMENTED, MY_AES_IV_SIZE + 1, MYF(MY_WME)))) {
my_free(clone_encrypt_mode_str);
my_free(*clone_encrypt_key);
fclose(file);
return 2;
}
read_pos = *clone_encrypt_iv;
end_pos = *clone_encrypt_iv + MY_AES_IV_SIZE;
} else if (read_stage > 2) {
got_error = true;
break;
}
if (read_pos == end_pos) {
got_error = true;
break;
}
*read_pos = c;
++read_pos;
}
c = fgetc(file);
}
if (read_pos != nullptr) *read_pos = 0;
fclose(file);
if (clone_encrypt_mode_str == nullptr || *clone_encrypt_key == nullptr ||
got_error || (strlen(*clone_encrypt_iv) != MY_AES_IV_SIZE)) {
my_free(clone_encrypt_mode_str);
my_free(*clone_encrypt_key);
my_free(*clone_encrypt_iv);
return 3;
}
int res = find_type(clone_encrypt_mode_str, &set_clone_encrypt_mode_typelib,
FIND_TYPE_BASIC);
if (res <= 0) {
my_free(clone_encrypt_mode_str);
my_free(*clone_encrypt_key);
my_free(*clone_encrypt_iv);
return 4;
}
*clone_encrypt_mode = static_cast<my_aes_opmode>(res - 1);
if (clone_encrypt_mode_str) my_free(clone_encrypt_mode_str);
return 0;
}
int clone_decrypt_buffer(char *source, uint32 length, char *dest,
my_aes_opmode clone_encrypt_mode,
char *clone_encrypt_key, char *clone_encrypt_iv) {
uint32 main_len;
uint32 remain_len;
int dec_len = 0;
unsigned char remain_buf[MY_AES_BLOCK_SIZE * 2];
if (length < REMAIN_BLOCK_SIZE || length >= ENCRYPT_BUFFER_SIZE) return -1;
main_len = (length / MY_AES_BLOCK_SIZE) * MY_AES_BLOCK_SIZE;
remain_len = length - main_len;
if (remain_len != 0) {
remain_len = MY_AES_BLOCK_SIZE * 2;
memcpy(remain_buf, source + length - remain_len, remain_len);
dec_len = my_aes_decrypt(
remain_buf, remain_len,
reinterpret_cast<unsigned char *>(source + length - remain_len),
reinterpret_cast<unsigned char *>(clone_encrypt_key),
strlen(clone_encrypt_key), clone_encrypt_mode,
reinterpret_cast<unsigned char *>(clone_encrypt_iv), false);
if ((uint32)dec_len != remain_len) return -1;
}
dec_len = my_aes_decrypt(reinterpret_cast<unsigned char *>(source), main_len,
reinterpret_cast<unsigned char *>(dest),
reinterpret_cast<unsigned char *>(clone_encrypt_key),
strlen(clone_encrypt_key), clone_encrypt_mode,
reinterpret_cast<unsigned char *>(clone_encrypt_iv),
false);
if ((uint32)dec_len != main_len) return -1;
if (remain_len != 0) {
memcpy(dest + main_len, source + main_len, length - main_len);
}
return length;
}
int decrypt_clone_file(FILE *file, const char *clone_file,
my_aes_opmode clone_encrypt_mode,
char *clone_encrypt_key, char *clone_encrypt_iv) {
int c;
int pos = 0;
int dec_len = 0;
uint total_len = 0;
FILE *clone = fopen(clone_file, "r");
if (clone == nullptr) {
fprintf(stderr, "Error: Fail to open encrypt clone file, errno: %d\n",
errno);
return 1;
}
c = fgetc(clone);
while (c != EOF) {
plain_buff[pos] = c;
++pos;
if (pos == ENCRYPT_BUFFER_SIZE) {
dec_len = clone_decrypt_buffer(plain_buff, ENCRYPT_BATCH_SIZE,
encry_buffer, clone_encrypt_mode,
clone_encrypt_key, clone_encrypt_iv);
if (dec_len != ENCRYPT_BATCH_SIZE) {
fclose(clone);
return 1;
}
int enc_pos = 0;
while (enc_pos < dec_len) {
fputc(encry_buffer[enc_pos], file);
++enc_pos;
}
memmove(plain_buff, plain_buff + ENCRYPT_BATCH_SIZE, REMAIN_BLOCK_SIZE);
pos = REMAIN_BLOCK_SIZE;
total_len += dec_len;
}
c = fgetc(clone);
}
if (pos > 0) {
dec_len =
clone_decrypt_buffer(plain_buff, pos, encry_buffer, clone_encrypt_mode,
clone_encrypt_key, clone_encrypt_iv);
if (dec_len != pos) {
fclose(clone);
return 1;
}
int enc_pos = 0;
while (enc_pos < pos) {
fputc(encry_buffer[enc_pos], file);
++enc_pos;
}
}
fclose(clone);
return 0;
}