-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenssl.cpp
331 lines (312 loc) · 11.4 KB
/
openssl.cpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "pipellm.h"
#include "hack.h"
#include <openssl/ssl.h>
#include <dlfcn.h>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <thread>
#include <cassert>
#include <cstring>
#include <sys/mman.h>
#include <vector>
#include <algorithm>
static uint8_t iv_seq[profile_bytes][256] = {};
static uint8_t iv_idx[profile_bytes][256] = {};
static void init_ividx()
{
for (int i = 0; i < profile_bytes; i++) {
for (int j = 0; j < 256; j++) {
iv_idx[i][iv_seq[i][j]] = (uint8_t)j;
}
}
}
uint64_t next_iv(uint8_t cur_iv[], uint8_t dest_iv[], uint64_t incr)
{
uint64_t ret = 0;
uint64_t idx[profile_bytes + 1];
for (int i = 0; i < profile_bytes; i++) {
idx[i] = iv_idx[i][cur_iv[i]];
}
idx[0] += incr;
for (int i = 0; i < profile_bytes; i++) {
if (i == profile_bytes - 1) {
ret = idx[i] / 256;
}
if (idx[i] >= 256) {
idx[i + 1] += idx[i] / 256;
idx[i] %= 256;
}
}
for (int i = 0; i < profile_bytes; i++) {
dest_iv[i] = iv_seq[i][idx[i]];
}
for (int i = profile_bytes; i < iv_length; i++) {
dest_iv[i] = cur_iv[i];
}
return ret;
}
void memcpy_worker(void *entry)
{
auto x = (memcpy_entry *)entry;
bind_core(x->core);
while (true) {
while (!x->busy);
memcpy(x->dst, x->src, x->size);
x->busy = false;
}
}
static bool encrypt_iv_inited;
static int memcpy_core = 22;
extern "C" int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
const unsigned char *in, int inl)
{
bool exist0 = m_ctx_metadata.find(ctx) != m_ctx_metadata.end();
if (!exist0) {
if (s_magic.find(inl) != s_magic.end()) {
m_magic_encctx.insert(std::make_pair(inl, ctx));
auto metadata = std::make_shared<encrypt_metadata>();
metadata->encrypt_ctx = ctx;
metadata->remain = 0;
m_ctx_metadata.insert(std::make_pair(ctx, metadata));
s_magic.erase(s_magic.find(inl));
// Memcpy worker
for (int i = 0; i < memcpy_thread_num - 1; i++) {
metadata->memcpy_entries[i].busy = false;
metadata->memcpy_entries[i].core = memcpy_core++;
metadata->memcpy_threads[i] = new std::thread(memcpy_worker, (void *)&metadata->memcpy_entries[i]);
}
}
} else {
auto &metadata = *m_ctx_metadata[ctx];
auto &m_iv_encentry = metadata.m_iv_encentry;
auto cur_iv_offset = metadata.cur_iv_offset;
if (m_iv_encentry.find(cur_iv_offset) != m_iv_encentry.end()) {
auto iter = m_iv_encentry.find(cur_iv_offset);
auto &encentry = iter->second;
while (encentry->busy);
// memcpy(out, encentry->buffer, encentry->size);
auto src = encentry->buffer;
auto dst = out;
auto div = (inl + memcpy_thread_num - 1) / memcpy_thread_num;
auto size = inl;
for (int i = 0; i < memcpy_thread_num; i++) {
if (i == memcpy_thread_num - 1) {
memcpy(dst, src, std::min(div, size));
break;
}
metadata.memcpy_entries[i].src = src;
metadata.memcpy_entries[i].dst = dst;
metadata.memcpy_entries[i].size = std::min(div, size);
std::atomic_thread_fence(std::memory_order_seq_cst);
metadata.memcpy_entries[i].busy = true;
src += div;
dst += div;
size -= div;
if (size == 0) break;
}
for (int i = 0; i < memcpy_thread_num - 1; i++) {
while (metadata.memcpy_entries[i].busy);
}
assert(inl == encentry->size);
*outl = inl;
metadata.remain++;
return 1;
}
}
auto ret = real_EVP_EncryptUpdate(ctx, out, outl, in, inl);
return ret;
}
extern "C" int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
ENGINE *impl, const unsigned char *key,
const unsigned char *iv)
{
if (m_ctx_metadata.find(ctx) != m_ctx_metadata.end()) {
auto &metadata = *m_ctx_metadata[ctx];
if (!encrypt_iv_inited) {
std::ifstream iv_profile(iv_profile_path);
static bool existed = iv_profile.good();
if (existed) {
// Profile iv
for (int i = 0; i < profile_bytes; i++) {
for (int j = 0; j < 256; j++) {
int x;
iv_profile >> x;
iv_seq[i][j] = x;
}
}
init_ividx();
encrypt_iv_inited = true;
} else {
static int sel = 0;
static uint8_t cur = 0;
static uint8_t carry = 0;
static bool record = false;
static int record_idx = 0;
if (cur != iv[sel]) {
cur = iv[sel];
if (carry != 0 && carry != iv[sel + 1]) {
if (!record) {
// Record from Carry
record = true;
} else {
// Record next
record = false;
// Save already recorded in file
std::ofstream iv_profile_write(iv_profile_path, std::ios::app);
for (int i = 0; i < 256; i++) {
iv_profile_write << (int)iv_seq[sel][i] << (i == 255 ? '\n' : ' ');
}
record_idx = 0;
iv_profile_write.close();
std::cerr << "Profiled sel " << sel << std::endl;
sel++;
if (sel == profile_bytes) {
exit(0);
}
}
}
carry = iv[sel + 1];
if (record) {
iv_seq[sel][record_idx++] = iv[sel];
}
}
}
}
if (!metadata.iv_inited) {
memcpy(metadata.init_iv, iv, iv_length);
memcpy(metadata.key, key, key_length);
metadata.cur_iv_offset = 0;
metadata.iv_inited = true;
} else {
metadata.cur_iv_offset++;
}
}
return real_EVP_EncryptInit_ex(ctx, cipher, impl, key, iv);
}
extern "C" int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
{
bool exist0 = m_ctx_metadata.find(ctx) != m_ctx_metadata.end();
if (exist0) {
auto &metadata = *m_ctx_metadata[ctx];
if (metadata.remain > 0) {
*outl = 0;
return 1;
}
}
auto ret = real_EVP_EncryptFinal_ex(ctx, out, outl);
return ret;
}
extern "C" int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
{
bool exist0 = m_ctx_metadata.find(ctx) != m_ctx_metadata.end();
if (exist0) {
auto &metadata = *m_ctx_metadata[ctx];
if (metadata.remain > 0 && type == EVP_CTRL_GCM_GET_TAG) {
memcpy(ptr, metadata.m_iv_encentry.at(metadata.cur_iv_offset)->tag, 16);
metadata.remain--;
// Must execute in the end
metadata.allocator.free(metadata.m_iv_encentry.at(metadata.cur_iv_offset)->buffer);
delete metadata.m_iv_encentry.at(metadata.cur_iv_offset);
metadata.m_iv_encentry.erase(metadata.cur_iv_offset);
return 1;
}
}
bool exist1 = m_ctx_dmetadata.find(ctx) != m_ctx_dmetadata.end();
if (exist1) {
auto &metadata = *m_ctx_dmetadata[ctx];
if (metadata.remain == 2 && type == EVP_CTRL_GCM_SET_TAG) {
memcpy(metadata.cur_tag, ptr, 16);
return 1;
}
}
auto ret = real_EVP_CIPHER_CTX_ctrl(ctx, type, arg, ptr);
return ret;
}
extern "C" int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
const unsigned char *in, int inl)
{
bool exist0 = m_ctx_dmetadata.find(ctx) != m_ctx_dmetadata.end();
if (!exist0) {
if (s_magic_dec.find(inl) != s_magic_dec.end()) {
m_magic_decctx.insert(std::make_pair(inl, ctx));
auto metadata = std::make_shared<decrypt_metadata>();
metadata->remain = 0;
m_ctx_dmetadata.insert(std::make_pair(ctx, metadata));
s_magic_dec.erase(s_magic_dec.find(inl));
// Memcpy worker
for (int i = 0; i < memcpy_thread_num - 1; i++) {
metadata->memcpy_entries[i].busy = false;
metadata->memcpy_entries[i].core = memcpy_core++;
metadata->memcpy_threads[i] = new std::thread(memcpy_worker, (void *)&metadata->memcpy_entries[i]);
}
}
} else {
// Allocate memory
auto &metadata = *m_ctx_dmetadata[ctx];
if (metadata.remain != 2) {
auto ret = real_EVP_DecryptUpdate(ctx, out, outl, in, inl);
return ret;
}
auto buffer = metadata.allocator.alloc();
// Memcpy into buffer
{
auto src = in;
auto dst = buffer;
auto div = (inl + memcpy_thread_num - 1) / memcpy_thread_num;
auto size = inl;
for (int i = 0; i < memcpy_thread_num; i++) {
if (i == memcpy_thread_num - 1) {
memcpy(dst, src, std::min(div, size));
break;
}
metadata.memcpy_entries[i].src = src;
metadata.memcpy_entries[i].dst = dst;
metadata.memcpy_entries[i].size = std::min(div, size);
std::atomic_thread_fence(std::memory_order_seq_cst);
metadata.memcpy_entries[i].busy = true;
src += div;
dst += div;
size -= div;
if (size == 0) break;
}
for (int i = 0; i < memcpy_thread_num - 1; i++) {
while (metadata.memcpy_entries[i].busy);
}
}
*outl = inl;
metadata.cur_buffer = buffer;
return 1;
}
auto ret = real_EVP_DecryptUpdate(ctx, out, outl, in, inl);
return ret;
}
extern "C" int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
ENGINE *impl, const unsigned char *key,
const unsigned char *iv)
{
bool exist0 = m_ctx_dmetadata.find(ctx) != m_ctx_dmetadata.end();
if (exist0) {
auto &metadata = *m_ctx_dmetadata[ctx];
metadata.remain++;
if (metadata.remain == 2) {
memcpy(metadata.key, key, key_length);
memcpy(metadata.cur_iv, iv, iv_length);
return 1;
}
}
return real_EVP_DecryptInit_ex(ctx, cipher, impl, key, iv);
}
extern "C" int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
{
bool exist0 = m_ctx_dmetadata.find(ctx) != m_ctx_dmetadata.end();
if (exist0) {
auto &metadata = *m_ctx_dmetadata[ctx];
if (metadata.remain == 2) {
*outl = 0;
metadata.remain--;
return 1;
}
}
return real_EVP_DecryptFinal_ex(ctx, out, outl);
}