-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathisv.cpp
3777 lines (2950 loc) · 96.2 KB
/
isv.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2018 Intel Corporation
This software and the related documents are Intel copyrighted materials,
and your use of them is governed by the express license under which they
were provided to you (License). Unless the License provides otherwise,
you may not use, modify, copy, publish, distribute, disclose or transmit
this software or the related documents without Intel's prior written
permission.
This software and the related documents are provided as is, with no
express or implied warranties, other than those that are expressly stated
in the License.
*/
using namespace std;
#ifdef _WIN32
#pragma comment(lib, "crypt32.lib")
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#else
#include "config.h"
#endif
#ifdef _WIN32
// *sigh*
# include "vs/client/Enclave_u.h"
#else
# include "Enclave_u.h"
#endif
#if !defined(SGX_HW_SIM)&&!defined(_WIN32)
#include "sgx_stub.h"
#endif
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>
#include <sgx_urts.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <intrin.h>
#include <wincrypt.h>
#include "win32/getopt.h"
#else
#include <openssl/evp.h>
#include <openssl/conf.h>
#include <openssl/err.h>
#include <getopt.h>
#include <unistd.h>
#endif
#include <sgx_uae_service.h>
#include <sgx_uae_launch.h>
#include <sgx_uae_epid.h>
#include <sgx_uae_quote_ex.h>
#include <sgx_ukey_exchange.h>
#include <sgx_uswitchless.h>
#include <string>
#include "common.h"
#include "protocol.h"
#include "sgx_detect.h"
#include "hexutil.h"
#include "fileio.h"
#include "base64.h"
#include "crypto.h"
#include "msgio.h"
#include "logfile.h"
#include "quote_size.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <random>
#include <chrono>
#include "error_print.hpp"
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <mysql_error.h>
#include <cppconn/statement.h>
#include <cppconn/resultset.h>
#include <cppconn/prepared_statement.h>
#define MAX_LEN 80
#define ROUND_UNIT 100000000
#ifdef _WIN32
# define strdup(x) _strdup(x)
#else
# define _rdrand64_step(x) ({ unsigned char err; asm volatile("rdrand %0; setc %1":"=r"(*x), "=qm"(err)); err; })
#endif
#ifdef __x86_64__
#define DEF_LIB_SEARCHPATH "/lib:/lib64:/usr/lib:/usr/lib64"
#else
#define DEF_LIB_SEARCHPATH "/lib:/usr/lib"
#endif
typedef struct config_struct {
char mode;
uint32_t flags;
sgx_spid_t spid;
sgx_ec256_public_t pubkey;
sgx_quote_nonce_t nonce;
char *server;
char *port;
} config_t;
int file_in_searchpath (const char *file, const char *search, char *fullpath,
size_t len);
sgx_status_t sgx_create_enclave_search (
const char *filename,
const int edebug,
sgx_launch_token_t *token,
int *updated,
sgx_enclave_id_t *eid,
sgx_misc_attribute_t *attr
);
sgx_status_t sgx_create_enclave_search_ex (
const char *filename,
const int edebug,
sgx_launch_token_t *token,
int *updated,
sgx_enclave_id_t *eid,
sgx_misc_attribute_t *attr
);
void usage();
int do_quote(sgx_enclave_id_t eid, config_t *config);
int do_attestation(sgx_enclave_id_t eid, config_t *config);
char debug= 0;
char verbose= 0;
MsgIO *msgio = NULL;
sgx_ra_context_t g_ra_ctx = 0xDEADDEAD;
sgx_status_t g_sgxrv = SGX_SUCCESS;
chrono::system_clock::time_point chrono_start, chrono_end;
#define MODE_ATTEST 0x0
#define MODE_EPID 0x1
#define MODE_QUOTE 0x2
#define OPT_PSE 0x01
#define OPT_NONCE 0x02
#define OPT_LINK 0x04
#define OPT_PUBKEY 0x08
/* Macros to set, clear, and get the mode and options */
#define SET_OPT(x,y) x|=y
#define CLEAR_OPT(x,y) x=x&~y
#define OPT_ISSET(x,y) x&y
#ifdef _WIN32
# define ENCLAVE_NAME "Enclave.signed.dll"
#else
# define ENCLAVE_NAME "Enclave.signed.so"
#endif
class BISGX_Database
{
public:
void initDB();
int do_login(string username, string password_hash, string privilege);
void switchTable(string tbname);
void storeDB(string data_to_store, string datatype, int cipherlen);
void setUsername(string username);
string do_executeQuery(string sentence, string cond);
string do_executeQuery_Annotation(string sentence,
int vcf_or_list, int clinvar_flag);
int do_executeQueryInt(string sentence, string cond);
string do_inquiryDB(); // for interpreter
int do_store_vctx(string whitelist, string chrom, string nation,
string disease_type, string filename, string username,
int div_total, string iv_array, string tag_array);
int do_inquiryVCTX(string chrom, string nation, string disease_type,
string *result);
size_t get_divnum(string filename);
int get_IV_and_tag(uint8_t *iv_b64, uint8_t *tag_b64, string filename);
int inquiryStoredData(string &inquiried_str);
int get_data_for_download(string misc_info, uint8_t *&sealed_b64);
/*
should be added is:
- username searcher
- data inserter (for data owner)
- data loader (for interpreter)
*/
private:
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt, *stmt2;
sql::ResultSet *res, *res2;
sql::PreparedStatement *prep_stmt;
string host;
string user;
string password;
string database;
string table;
string username_internal;
};
BISGX_Database bdb;
void BISGX_Database::initDB()
{
cout << "CAUTION: Auto login for debug is enabled." << endl;
host = "localhost";
user = "BI-SGX";
password = "bisgx_sample";
database = "`BI-SGX`";
driver = get_driver_instance();
con = driver->connect(host, user, password);
stmt = con->createStatement();
stmt2 = con->createStatement();
stmt->execute("USE " + database);
table = "userinfo";
cout << "Database initialization completed." << endl << endl;
}
void BISGX_Database::switchTable(string tbname)
{
table = tbname;
}
int BISGX_Database::do_login(string username, string password_hash, string privilege)
{
string tmp;
bool isRegistered = false;
int privilege_flag;
res = stmt->executeQuery("SELECT * FROM " + table);
while(res->next())
{
tmp = res->getString("username");
if(username == tmp)
{
isRegistered = true;
break;
}
}
if(isRegistered)
{
res = stmt->executeQuery("SELECT pass_hash FROM " + table
+ " WHERE username = '" + username + "'");
string passhash_tmp;
while(res->next())
{
passhash_tmp = res->getString("pass_hash");
}
if(passhash_tmp == password_hash)
{
cout << "password confirmed." << endl;
}
else
{
cout << "wrong password." << endl;
return 2;
}
res = stmt->executeQuery("SELECT privilege FROM " + table
+ " WHERE username = '" + username + "'");
string priv_temp;
while(res->next())
{
priv_temp = res->getString("privilege");
}
if(priv_temp[0] == 'O')
{
privilege_flag = 0;
}
else if(priv_temp[0] == 'R')
{
privilege_flag = 1;
}
else
{
cerr << "priv:" << priv_temp << endl;
cerr << "Error while obtaining privilege." << endl;
privilege_flag = 2;
}
}
else
{
stmt->execute("INSERT INTO " + table + "(username, pass_hash, privilege) "
+ "VALUES('" + username + "', '" + password_hash + "', '" + privilege + "')");
if(privilege == "O")
{
privilege_flag = 0;
}
else if(privilege == "R")
{
privilege_flag = 1;
}
else
{
privilege_flag = 2;
}
}
return privilege_flag;
}
string BISGX_Database::do_executeQuery(string sentence, string cond)
{
res = stmt->executeQuery(sentence);
string retstr;
while(res->next())
{
retstr = res->getString(cond);
}
return retstr;
}
string BISGX_Database::do_executeQuery_Annotation(string sentence,
int vcf_or_list, int clinvar_flag)
{
res = stmt->executeQuery(sentence);
string retstr, vcf_pos;
while(res->next())
{
if(res->getString("CHROM") == "")
{
return string("");
}
if(vcf_or_list == 0)
{
retstr += res->getString("CHROM") + string("\t");
retstr += res->getString("POS") + string("\t");
retstr += res->getString("ID") + string("\t");
retstr += res->getString("REF") + string("\t");
retstr += res->getString("ALT") + string("\t");
retstr += res->getString("QUAL") + string("\t");
retstr += res->getString("FILTER") + string("\t");
retstr += res->getString("INFO");
}
else
{
retstr += "#ID\n";
retstr += res->getString("ID") + std::string("\n\n");
retstr += "#CHROM\n";
retstr += res->getString("CHROM") + std::string("\n\n");
retstr += "#POS\n";
retstr += res->getString("POS") + std::string("\n\n");
retstr += "#REF\n";
retstr += res->getString("REF") + std::string("\n\n");
retstr += "#ALT\n";
retstr += res->getString("ALT") + std::string("\n\n");
retstr += "#QUAL\n";
retstr += res->getString("QUAL") + std::string("\n\n");
retstr += "#FILTER\n";
retstr += res->getString("FILTER") + std::string("\n\n");
retstr += "#INFO\n";
retstr += res->getString("INFO") + std::string("\n\n");
}
vcf_pos = res->getString("POS");
if(clinvar_flag != 0)
{
string sentence_clinvar = "SELECT INFO FROM clinvar WHERE POS = '";
sentence_clinvar += vcf_pos;
sentence_clinvar += "'";
res2 = stmt2->executeQuery(sentence_clinvar);
std::string clinvar_info = "";
while(res2->next())
{
clinvar_info = res2->getString("INFO");
if(clinvar_info == "")
{
clinvar_info = "N/A";
}
if(vcf_or_list == 0)
{
retstr += "\t";
retstr += clinvar_info;
}
else
{
retstr += "#INFO(CLINVAR)\n";
retstr += clinvar_info + std::string("\n");
}
}
if(clinvar_info == "")
{
if(vcf_or_list == 0)
{
retstr += "\tN/A";
}
else
{
retstr += "#INFO(CLINVAR)\nN/A\n";
}
}
}
retstr += "\n";
}
return retstr;
}
int BISGX_Database::do_executeQueryInt(string sentence, string cond)
{
res = stmt->executeQuery(sentence);
int retint;
while(res->next())
{
retint = res->getInt(cond);
}
return retint;
}
void BISGX_Database::storeDB(string data_to_store, string datatype, int cipherlen)
{
table = "stored_data";
res = stmt->executeQuery("SELECT COUNT(*) FROM " + table);
int datanum = -9999;
while(res->next())
{
datanum = res->getInt("COUNT(*)");
}
cout << "COUNT:" << datanum << endl;
string dataset_name = "dataset";
dataset_name += to_string(datanum);
stmt->execute("INSERT INTO " + table + "(dataname, owner, data, datatype, cipherlen)"
+ "VALUES('" + dataset_name + "', '" + username_internal + "', '"
+ data_to_store + "', '" + datatype + "', '" + to_string(cipherlen) + "')");
}
void BISGX_Database::setUsername(string username)
{
username_internal = username;
}
string BISGX_Database::do_inquiryDB()
{
res = stmt->executeQuery
("SELECT dataname, datatype FROM stored_data");
string inquiry_res;
while(res->next())
{
inquiry_res += res->getString("dataname");
inquiry_res += " -> ";
inquiry_res += res->getString("datatype");
inquiry_res += "\n";
}
return inquiry_res;
}
int BISGX_Database::do_store_vctx(string whitelist, string chrom,
string nation, string disease_type, string filename, string username,
int div_total, string iv_array, string tag_array)
{
table = "vcf_context";
try
{
stmt->execute("INSERT INTO " + table + " (whitelist, chrom, nation, "
+ "disease_type, tar_filename, username, div_total, iv_array, tag_array) "
+ "VALUES('" + whitelist + "', '" + chrom + "', '" + nation + "', '"
+ disease_type + "', '" + filename + "', '" + username + "', '"
+ to_string(div_total) + "', '" + iv_array + "', '" + tag_array + "')");
}
catch(sql::SQLException &e)
{
cerr << "# ERR: SQLException in " << __FILE__;
cerr <<" on line " << __LINE__ << endl;
cerr << "# ERR: " << e.what() << endl;
cerr << " (MySQL error code: " << e.getErrorCode();
cerr << ", SQLState: " << e.getSQLState() << ")" << endl;
return -1;
}
return 0;
}
int BISGX_Database::do_inquiryVCTX(string chrom, string nation, string disease_type,
string *result)
{
/* NEED TO IMPLEMENT REJECTION FEATURE USING WHITELIST */
table = "vcf_context";
string null_str = "";
for(int i = 0; i < 64; i++)
{
null_str += "0";
}
try
{
string cmd = "SELECT tar_filename FROM vcf_context";
if(chrom != null_str || nation != null_str || disease_type != null_str)
{
cmd += " WHERE ";
if(chrom != null_str)
{
cmd += "chrom='";
cmd += chrom;
cmd += "'";
if(nation != null_str || disease_type != null_str)
{
cmd += " AND ";
}
}
if(nation != null_str)
{
cmd += "nation='";
cmd += nation;
cmd += "'";
if(disease_type != null_str)
{
cmd += " AND ";
}
}
if(disease_type != null_str)
{
cmd += "disease_type='";
cmd += disease_type;
cmd += "'";
}
}
// cout << "\n" << cmd << endl << endl;
res = stmt->executeQuery(cmd);
while(res->next())
{
*result += res->getString("tar_filename");
*result += "\n";
}
result->pop_back();
}
catch(sql::SQLException &e)
{
cerr << "# ERR: SQLException in " << __FILE__;
cerr <<" on line " << __LINE__ << endl;
cerr << "# ERR: " << e.what() << endl;
cerr << " (MySQL error code: " << e.getErrorCode();
cerr << ", SQLState: " << e.getSQLState() << ")" << endl;
return -1;
}
return 0;
}
size_t BISGX_Database::get_divnum(string filename)
{
string cmd = "SELECT div_total FROM vcf_context WHERE tar_filename='";
cmd += filename;
cmd += "'";
size_t divnum = 0;
try
{
res = stmt->executeQuery(cmd);
res->next();
divnum = atoi(res->getString("div_total").c_str());
}
catch(sql::SQLException &e)
{
cerr << "# ERR: SQLException in " << __FILE__;
cerr <<" on line " << __LINE__ << endl;
cerr << "# ERR: " << e.what() << endl;
cerr << " (MySQL error code: " << e.getErrorCode();
cerr << ", SQLState: " << e.getSQLState() << ")" << endl;
return -1;
}
return divnum;
}
int BISGX_Database::get_IV_and_tag(uint8_t *iv_b64, uint8_t *tag_b64, string filename)
{
string cmd = "SELECT iv_array, tag_array FROM vcf_context WHERE tar_filename='";
cmd += filename;
cmd += "'";
string ivb64_str, tagb64_str;
try
{
res = stmt->executeQuery(cmd);
res->next();
ivb64_str = res->getString("iv_array");
tagb64_str = res->getString("tag_array");
}
catch(sql::SQLException &e)
{
cerr << "# ERR: SQLException in " << __FILE__;
cerr <<" on line " << __LINE__ << endl;
cerr << "# ERR: " << e.what() << endl;
cerr << " (MySQL error code: " << e.getErrorCode();
cerr << ", SQLState: " << e.getSQLState() << ")" << endl;
return -1;
}
size_t ivb64_len, tagb64_len;
ivb64_len = ivb64_str.length();
tagb64_len = tagb64_str.length();
for(int i = 0; i < ivb64_len; i++)
{
iv_b64[i] = (uint8_t)ivb64_str.c_str()[i];
}
for(int i = 0; i < tagb64_len; i++)
{
tag_b64[i] = (uint8_t)tagb64_str.c_str()[i];
}
return 0;
}
int BISGX_Database::inquiryStoredData(string &inquiried_str)
{
try
{
vector<string> datatype_vec;
datatype_vec.emplace_back("integer");
datatype_vec.emplace_back("genome");
datatype_vec.emplace_back("FASTA");
size_t dtvec_sz = datatype_vec.size();
for(int i = 0; i < dtvec_sz; i++)
{
string query = "SELECT dataname FROM stored_data WHERE owner='";
query += username_internal;
query += "' AND datatype='";
query += datatype_vec[i];
query += "'";
inquiried_str += datatype_vec[i];
inquiried_str += "->\n";
res = stmt->executeQuery(query);
while(res->next())
{
inquiried_str += res->getString("dataname");
inquiried_str += "\n";
}
inquiried_str += "\n";
}
inquiried_str.pop_back();
}
catch(sql::SQLException &e)
{
cerr << "# ERR: SQLException in " << __FILE__;
cerr <<" on line " << __LINE__ << endl;
cerr << "# ERR: " << e.what() << endl;
cerr << " (MySQL error code: " << e.getErrorCode();
cerr << ", SQLState: " << e.getSQLState() << ")" << endl;
return -1;
}
return 0;
}
int BISGX_Database::get_data_for_download(string misc_info, uint8_t *&sealed_b64)
{
try
{
string cmd = "SELECT data FROM stored_data WHERE dataname='";
cmd += misc_info;
cmd += "'";
res = stmt->executeQuery(cmd);
/* candidate must be only one data */
res->next();
string data_body = res->getString("data");
size_t data_sz = data_body.length();
sealed_b64 = new uint8_t[data_sz + 1]();
for(int i = 0; i < data_sz; i++)
{
sealed_b64[i] = data_body.c_str()[i];
}
return 0;
}
catch(sql::SQLException &e)
{
cerr << "# ERR: SQLException in " << __FILE__;
cerr <<" on line " << __LINE__ << endl;
cerr << "# ERR: " << e.what() << endl;
cerr << " (MySQL error code: " << e.getErrorCode();
cerr << ", SQLState: " << e.getSQLState() << ")" << endl;
return -1;
}
}
void OCALL_print(const char* message)
{
printf("%s\n", message);
return;
}
void OCALL_print_status(sgx_status_t st)
{
sgx_error_print(st);
return;
}
void OCALL_print_int(int num)
{
cout << "OCALL_INT_PRINT: " << dec<< num << endl;
return;
}
void OCALL_dump(uint8_t *char_to_dump, int bufsize)
{
BIO_dump_fp(stdout, (const char*)char_to_dump, bufsize);
return;
}
void OCALL_generate_nonce(uint8_t *ivbuf, int bufsize)
{
random_device rnd;
mt19937 mt(rnd());
uniform_int_distribution<> randchar(0, 255);
for(int i = 0; i < bufsize; i++)
{
ivbuf[i] = (uint8_t)randchar(mt);
}
cout << "Generated nonce is:" << endl;
BIO_dump_fp(stdout, (const char*)ivbuf, bufsize);
cout << endl;
return;
}
void OCALL_get_time(uint8_t *timebuf, int bufsize)
{
time_t t = time(NULL);
strftime(reinterpret_cast<char*>(timebuf), 64, "%Y/%m/%d %a %H:%M:%S", localtime(&t));
}
int OCALL_fwrite(uint8_t *filename, size_t fnlen,
uint8_t *buf, size_t buflen)
{
string filename_str = (char*)filename;
ofstream ofs(filename_str, ios::binary | ios::trunc);
ofs.write(reinterpret_cast<const char*>(buf), buflen);
if(!ofs)
{
cerr << "Failed to write designated file." << endl;
return -1;
}
return 0;
}
void OCALL_fread(uint8_t *buf, int buflen)
{
string tmp, tmp2;
ifstream ifs("sealed2.txt", ios::binary);
if(!ifs)
{
cout << "failed to open file." << endl;
}
ifs.read(reinterpret_cast<char*>(buf), buflen);
}
void OCALL_get_sealed_length(char *dataset_name, int *sealed_length)
{
string dataname_str(dataset_name);
string query = "SELECT * FROM stored_data WHERE dataname = '";
query += dataname_str;
query += "'";
string cond = "cipherlen";
*sealed_length = bdb.do_executeQueryInt(query, cond);
}
void OCALL_chrono_start()
{
chrono_start = chrono::system_clock::now();
}
void OCALL_chrono_end()
{
chrono_end = chrono::system_clock::now();
double elapsed = chrono::duration_cast<chrono::milliseconds>
(chrono_end - chrono_start).count();
cout << endl;
cout << "-----------------------------------------------" << endl;
cout << "Elapsed time is: " << elapsed << "[ms]" << endl;
cout << "-----------------------------------------------" << endl;
cout << endl;
}
void OCALL_chrono_end_get_time(double *elapsed)
{
chrono_end = chrono::system_clock::now();
*elapsed = chrono::duration_cast<chrono::milliseconds>
(chrono_end - chrono_start).count();
}
/*referred: https://ryozi.hatenadiary.jp/entry/20101203/1291380670 in 12/30/2018*/
int base64_encrypt(uint8_t *src, int srclen, uint8_t *dst, int dstlen)
{
const char Base64char[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int i,j;
int calclength = (srclen/3*4) + (srclen%3?4:0);
if(calclength > dstlen) return -1;
j=0;
for(i=0; i+2<srclen; i+=3){
dst[j++] = Base64char[ (src[i] >> 2) & 0x3F ];
dst[j++] = Base64char[ (src[i] << 4 | src[i+1] >> 4) & 0x3F ];
dst[j++] = Base64char[ (src[i+1] << 2 | src[i+2] >> 6) & 0x3F ];
dst[j++] = Base64char[ (src[i+2]) & 0x3F ];
}
if(i<srclen){
dst[j++] = Base64char[ (src[i] >> 2) & 0x3F ];
if(i+1<srclen){
dst[j++] = Base64char[ (src[i] << 4 | src[i+1] >> 4) & 0x3F ];
if(i+2<srclen){
dst[j++] = Base64char[ (src[i+1] << 2 | src[i+2] >> 6) & 0x3F ];
}else{
dst[j++] = Base64char[ (src[i+1] << 2) & 0x3F ];
}
}else{
dst[j++] = Base64char[ (src[i] << 4) & 0x3F ];
}
}
while(j%4) dst[j++] = '=';
if(j<dstlen) dst[j] = '\0';
return j;
}
int base64_decrypt(uint8_t *src, int srclen, uint8_t *dst, int dstlen)
{
const unsigned char Base64num[] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x3E,0xFF,0xFF,0xFF,0x3F,
0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0xFF,0xFF,0xFF,0x00,0xFF,0xFF,
0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,
0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
};
int calclength = (srclen/4*3);
//cout << "\nINFO: calclength -> " << calclength << endl << endl;
int i,j;
if(calclength > dstlen || srclen % 4 != 0) return 0;
j=0;
for(i=0; i+3<srclen; i+=4){
if((Base64num[src[i+0]]|Base64num[src[i+1]]|Base64num[src[i+2]]|Base64num[src[i+3]]) > 0x3F){
return -1;
}
dst[j++] = Base64num[src[i+0]]<<2 | Base64num[src[i+1]] >> 4;
dst[j++] = Base64num[src[i+1]]<<4 | Base64num[src[i+2]] >> 2;
dst[j++] = Base64num[src[i+2]]<<6 | Base64num[src[i+3]];
}
if(j<dstlen) dst[j] = '\0';
return j;
}
void OCALL_load_db(uint8_t *sealed_data, int buflen, char *dataset_name)
{
string dataname_str(dataset_name);
string query = "SELECT * FROM stored_data WHERE dataname = '";
query += dataname_str;
query += "'";
string cond = "data";
string str_to_load = bdb.do_executeQuery(query, cond);
int sealedlen;
int sealedb64len = str_to_load.length();
uint8_t *sealedb64 =
reinterpret_cast<uint8_t*>(const_cast<char*>(str_to_load.c_str()));
sealedlen = base64_decrypt(sealedb64,
sealedb64len, sealed_data, sealedb64len);
}
int OCALL_select_annotation(char *id, char *record,
int vcf_or_list, int clinvar_flag)
{
try
{
string id_str(id);
string query = "SELECT * FROM vcf WHERE ID = '";
query += id_str;