-
Notifications
You must be signed in to change notification settings - Fork 8
/
Enclave.cpp
452 lines (344 loc) · 10.3 KB
/
Enclave.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include "Enclave.h"
#include <queue>
#include <vector>
int num = 0;
int exit_count = 0;
hashtable* ht_enclave = NULL;
MACbuffer* MACbuf_enclave = NULL;
BucketMAC* MACTable = NULL;
Arg arg_enclave;
int ratio_root_per_buckets = 0;
sgx_thread_mutex_t global_mutex;
sgx_thread_mutex_t* queue_mutex;
sgx_thread_cond_t* job_cond;
std::vector<std::queue<job *> > queue;
/**
* init enclave values
**/
void enclave_init_values(hashtable* ht_, MACbuffer* MACbuf_, Arg arg) {
ht_enclave = ht_;
MACbuf_enclave = MACbuf_;
arg_enclave = arg;
//set the ratio of subtree root node inside the enclave memory
//over total hash value of buckets
ratio_root_per_buckets = ht_enclave->size/arg_enclave.tree_root_size;
MACTable = (BucketMAC*)malloc(sizeof(BucketMAC)* arg_enclave.tree_root_size);
for(int i = 0; i < arg_enclave.tree_root_size; i++)
{
memset(MACTable[i].mac ,0 ,MAC_SIZE);
}
//Initialize mutex variables
sgx_thread_mutex_init(&global_mutex, NULL);
queue_mutex = (sgx_thread_mutex_t*)malloc(sizeof(sgx_thread_mutex_t)*arg_enclave.num_threads);
job_cond = (sgx_thread_cond_t*)malloc(sizeof(sgx_thread_cond_t)*arg_enclave.num_threads);
for(int i = 0 ; i < arg_enclave.num_threads; i++) {
queue.push_back(std::queue<job*>());
}
}
/**
* processing set operation
**/
void enclave_set(char *cipher) {
char* key;
char* val;
char* key_val;
char* plain_key_val = NULL;
uint8_t nac[NAC_SIZE];
uint8_t mac[MAC_SIZE];
uint8_t prev_mac[MAC_SIZE];
uint8_t updated_nac[NAC_SIZE];
uint32_t key_size;
uint32_t val_size;
uint8_t key_idx;
char *tok;
char *temp_;
entry * ret_entry;
bool is_insert = false;
tok = strtok_r(cipher+4," ",&temp_);
key_size = strlen(tok)+1;
key = (char*)malloc(sizeof(char)*key_size);
memset(key, 0, key_size);
memcpy(key, tok, key_size-1);
val_size = strlen(temp_)+1;
val = (char*)malloc(sizeof(char)*val_size);
memset(val, 0, val_size);
memcpy(val, temp_, val_size-1);
int kv_pos = -1;
ret_entry = ht_get_o(key, key_size, &plain_key_val, &kv_pos, updated_nac);
int hash_val = ht_hash(key);
key_idx = key_hash_func(key);
sgx_status_t ret = SGX_SUCCESS;
/* verifying integrity of data */
//update
if(ret_entry != NULL)
{
ret = enclave_verification(ret_entry->key_val, ret_entry->key_hash, ret_entry->key_size, ret_entry->val_size, ret_entry->nac, ret_entry->mac);
if(ret != SGX_SUCCESS)
{
print("MAC verification failed");
}
memcpy(nac, updated_nac, NAC_SIZE);
free(plain_key_val);
is_insert = false;
memcpy(prev_mac, ret_entry->mac, MAC_SIZE);
}
//insert
else
{
/* Make initial nac */
sgx_read_rand(nac, NAC_SIZE);
assert(plain_key_val == NULL);
is_insert = true;
}
/* We have to encrypt key and value together, so make con field */
key_val = (char*)malloc(sizeof(char)*(key_size+val_size));
memcpy(key_val, key, key_size);
memcpy(key_val + key_size, val, val_size);
enclave_encrypt(key_val, key_val, key_idx, key_size, val_size, nac, mac);
ht_set_o(ret_entry, key, key_val, nac, mac, key_size, val_size, kv_pos);
ret = enclave_rebuild_tree_root(hash_val, kv_pos, is_insert, prev_mac);
if(ret != SGX_SUCCESS)
{
print("Tree verification failed");
}
memset(cipher, 0, arg_enclave.max_buf_size);
memcpy(cipher, key, key_size);
free(key);
free(val);
free(key_val);
}
/**
* processing get operation
**/
void enclave_get(char *cipher){
char* key;
char* plain_key_val = NULL;
uint32_t key_size;
char *tok;
char *temp_;
entry * ret_entry;
uint8_t updated_nac[NAC_SIZE];
tok = strtok_r(cipher+4," ",&temp_);
key_size = strlen(tok)+1;
key = (char*)malloc(sizeof(char)*key_size);
memset(key, 0, key_size);
memcpy(key, tok, key_size-1);
int kv_pos = -1;
ret_entry = ht_get_o(key, key_size, &plain_key_val, &kv_pos, updated_nac);
if(ret_entry == NULL){
print("GET FAILED: No data in database");
return;
}
int hash_val = ht_hash(key);
sgx_status_t ret = SGX_SUCCESS;
/* verifying integrity of data */
ret = enclave_verification(ret_entry->key_val, ret_entry->key_hash, ret_entry->key_size, ret_entry->val_size, ret_entry->nac, ret_entry->mac);
if(ret != SGX_SUCCESS)
{
print("MAC verification failed");
}
ret = enclave_verify_tree_root(hash_val);
if(ret != SGX_SUCCESS)
{
print("Tree verification failed");
}
memset(cipher, 0, arg_enclave.max_buf_size);
memcpy(cipher, plain_key_val+ret_entry->key_size, ret_entry->val_size);
free(key);
free(plain_key_val);
}
/**
* processing append operation
**/
void enclave_append(char *cipher){
char* key;
char* val;
char* key_val;
char* plain_key_val;
uint8_t nac[NAC_SIZE];
uint8_t mac[MAC_SIZE];
uint8_t prev_mac[MAC_SIZE];
uint8_t updated_nac[NAC_SIZE];
uint32_t key_size;
uint32_t val_size;
uint8_t key_idx;
char *tok;
char *temp_;
entry * ret_entry;
tok = strtok_r(cipher+4," ",&temp_);
key_size = strlen(tok)+1;
key = (char*)malloc(sizeof(char)*key_size);
memset(key, 0, key_size);
memcpy(key, tok, key_size-1);
val_size = strlen(temp_)+1;
val = (char*)malloc(sizeof(char)*val_size);
memset(val, 0, val_size);
memcpy(val, temp_, val_size-1);
int kv_pos = -1;
ret_entry = ht_get_o(key, key_size, &plain_key_val, &kv_pos, updated_nac);
int hash_val = ht_hash(key);
key_idx = key_hash_func(key);
sgx_status_t ret = SGX_SUCCESS;
/* verifying integrity of data */
//update
if(ret_entry != NULL)
{
ret = enclave_verification(ret_entry->key_val, ret_entry->key_hash, ret_entry->key_size, ret_entry->val_size, ret_entry->nac, ret_entry->mac);
if(ret != SGX_SUCCESS)
{
print("MAC verification failed");
}
memcpy(nac, updated_nac, NAC_SIZE);
memcpy(prev_mac, ret_entry->mac, MAC_SIZE);
}
//insert
else
{
print("There's no data in the database");
return;
}
/* Make appended key-value */
key_val = (char*)malloc(sizeof(char)*(ret_entry->key_size + ret_entry->val_size + val_size - 1));
memcpy(key_val, plain_key_val, ret_entry->key_size + ret_entry->val_size);
memcpy(key_val + ret_entry->key_size + ret_entry->val_size - 1, val, val_size);
enclave_encrypt(key_val, key_val, key_idx, key_size, ret_entry->val_size + val_size - 1, nac, mac);
ht_append_o(ret_entry, key, key_val, nac, mac, key_size, ret_entry->val_size + val_size - 1, kv_pos);
ret = enclave_rebuild_tree_root(hash_val, kv_pos, false, prev_mac);
if(ret != SGX_SUCCESS)
{
print("Tree verification failed");
}
memset(cipher, 0, arg_enclave.max_buf_size);
memcpy(cipher, key, key_size);
free(key);
free(val);
free(plain_key_val);
free(key_val);
}
/**
* processing server working threads
**/
void enclave_worker_thread(hashtable *ht_, MACbuffer *MACbuf_) {
int thread_id;
job *cur_job = NULL;
char *cipher = NULL;
ht_enclave = ht_;
MACbuf_enclave = MACbuf_;
sgx_thread_mutex_lock(&global_mutex);
thread_id = num;
num+=1;
sgx_thread_mutex_init(&queue_mutex[thread_id], NULL);
sgx_thread_cond_init(&job_cond[thread_id], NULL);
sgx_thread_mutex_unlock(&global_mutex);
sgx_thread_mutex_lock(&queue_mutex[thread_id]);
while(1) {
if(queue[thread_id].size() == 0) {
sgx_thread_cond_wait(&job_cond[thread_id], &queue_mutex[thread_id]);
continue;
}
cur_job = queue[thread_id].front();
cipher = cur_job->buf;
sgx_thread_mutex_unlock(&queue_mutex[thread_id]);
if(strncmp(cipher, "GET", 3) == 0 || strncmp(cipher, "get", 3) == 0) {
enclave_get(cipher);
}
else if(strncmp(cipher, "SET", 3) == 0 || strncmp(cipher, "set", 3) == 0) {
enclave_set(cipher);
}
else if(strncmp(cipher, "APP", 3) == 0 || strncmp(cipher, "app", 3) == 0) {
enclave_append(cipher);
}
else if(strncmp(cipher, "quit", 4) == 0 ) {
sgx_thread_mutex_lock(&queue_mutex[thread_id]);
queue[thread_id].pop();
free(cipher);
free(cur_job);
sgx_thread_mutex_unlock(&queue_mutex[thread_id]);
sgx_thread_mutex_destroy(&queue_mutex[thread_id]);
sgx_thread_cond_destroy(&job_cond[thread_id]);
return;
}
else{
print("Untyped request");
break;
}
message_return(cipher, arg_enclave.max_buf_size, cur_job->client_sock);
sgx_thread_mutex_lock(&queue_mutex[thread_id]);
queue[thread_id].pop();
free(cipher);
free(cur_job);
}
return;
}
/**
* Bring the resquest to enclave
* parsing the key and send the requests to specific thread
**/
void enclave_message_pass(void* data) {
EcallParams *ecallParams = (EcallParams *)data;
char* cipher = ecallParams->buf;
int client_sock = ecallParams->client_sock_;
int num_clients = ecallParams->num_clients_;
char* key;
uint32_t key_size;
char *tok;
char *temp_;
int thread_id = 0;
job* new_job = NULL;
if(strncmp(cipher, "EXIT", 4) == 0 || strncmp(cipher, "exit", 4) == 0 ){
message_return(cipher, arg_enclave.max_buf_size, client_sock);
exit_count++;
if(exit_count == num_clients) {
print("All threads are finished");
//Send exit message to all of the worker threads
for(int i = 0 ; i < arg_enclave.num_threads; i++) {
new_job = (job*)malloc(sizeof(job));
new_job->buf = (char*)malloc(sizeof(char)*arg_enclave.max_buf_size);
memset(new_job->buf, 0, arg_enclave.max_buf_size);
memcpy(new_job->buf, "quit" , 4);
new_job->client_sock = -1;
sgx_thread_mutex_lock(&queue_mutex[i]);
queue[i].push(new_job);
sgx_thread_cond_signal(&job_cond[i]);
sgx_thread_mutex_unlock(&queue_mutex[i]);
}
}
}
else if(strncmp(cipher, "LOADDONE", 8) == 0)
{
print("Load process is done");
message_return(cipher, arg_enclave.max_buf_size, client_sock);
}
else {
new_job = (job*)malloc(sizeof(job));
new_job->buf = (char*)malloc(sizeof(char)*arg_enclave.max_buf_size);
memcpy(new_job->buf, cipher, arg_enclave.max_buf_size);
new_job->client_sock = client_sock;
//parsing key
tok = strtok_r(cipher+4," ",&temp_);
key_size = strlen(tok)+1;
key = (char*)malloc(sizeof(char)*key_size);
memset(key, 0, key_size);
memcpy(key, tok, key_size-1);
//send the requests to specific worker thread
thread_id = (int)(ht_hash(key)/(ht_enclave->size/arg_enclave.num_threads));
free(key);
sgx_thread_mutex_lock(&queue_mutex[thread_id]);
queue[thread_id].push(new_job);
sgx_thread_cond_signal(&job_cond[thread_id]);
sgx_thread_mutex_unlock(&queue_mutex[thread_id]);
}
return;
}
/**
* Hotcalls responder
**/
void EcallStartResponder( HotCall* hotEcall )
{
void (*callbacks[1])(void*);
callbacks[0] = enclave_message_pass;
HotCallTable callTable;
callTable.numEntries = 1;
callTable.callbacks = callbacks;
HotCall_waitForCall( hotEcall, &callTable );
}