-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSALSCF.ccneedchangea
1218 lines (916 loc) · 30 KB
/
SALSCF.ccneedchangea
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
#include <iostream>
#include <fstream>
#include <string>
#include <openssl/des.h>
#include <openssl/rand.h>
#include <vector>
#include <sstream>
#include <time.h>
#include <openssl/sha.h>
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/md5.h>
#include <openssl/bio.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/ssl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdexcept>
using namespace std;
int DEBUG = 1;
string W[] = {"LogleInitializationType","MessageType","NormalCloseMessage","ResponseMessageType","AbnormalCloseType"};
ofstream file;
int lock = 0;
int id;
time_t d;
time_t dplus;
string idlog;
string A0;
string LastA;
int padding = RSA_PKCS1_PADDING;
int glb_k0_len;
const char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
/*
unsigned char* strHex(string data){
unsigned char ret[data.length()/2];
return ret;
}
*/
std::string hexStr(unsigned char *data, int len)
{
std::string s(len * 2, ' ');
for (int i = 0; i < len; ++i) {
s[2 * i] = hexmap[(data[i] & 0xF0) >> 4];
s[2 * i + 1] = hexmap[data[i] & 0x0F];
}
return s;
}
std::string string_to_hex(const std::string& input)
{
static const char* const lut = "0123456789ABCDEF";
size_t len = input.length();
std::string output;
output.reserve(2 * len);
for (size_t i = 0; i < len; ++i)
{
const unsigned char c = input[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
return output;
}
std::string hex_to_string(const std::string& input)
{
static const char* const lut = "0123456789ABCDEF";
size_t len = input.length();
if (len & 1) throw std::invalid_argument("odd length");
std::string output;
output.reserve(len / 2);
for (size_t i = 0; i < len; i += 2)
{
char a = input[i];
const char* p = std::lower_bound(lut, lut + 16, a);
if (*p != a) throw std::invalid_argument(a+": not a hex digit");
char b = input[i + 1];
const char* q = std::lower_bound(lut, lut + 16, b);
if (*q != b) throw std::invalid_argument(a+": not a hex digit");
output.push_back(((p - lut) << 4) | (q - lut));
}
return output;
}
std::string hex_to_string_len(const std::string& input, int len)
{
static const char* const lut = "0123456789ABCDEF";
if (len & 1) throw std::invalid_argument("odd length");
std::string output;
output.reserve(len / 2);
for (int i = 0; i < len; i += 2)
{
char a = input[i];
const char* p = std::lower_bound(lut, lut + 16, a);
if (*p != a) throw std::invalid_argument(a+": not a hex digit");
char b = input[i + 1];
const char* q = std::lower_bound(lut, lut + 16, b);
if (*q != b) throw std::invalid_argument(a+": not a hex digit");
output.push_back(((p - lut) << 4) | (q - lut));
}
return output;
}
RSA * createRSA(unsigned char * key,int publica)
{
RSA *rsa= NULL;
BIO *keybio ;
keybio = BIO_new_mem_buf(key, -1);
if (keybio==NULL)
{
printf( "Failed to create key BIO");
return 0;
}
if(publica)
{
rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa,NULL, NULL);
}
else
{
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa,NULL, NULL);
}
return rsa;
}
unsigned char* public_encrypt(unsigned char * data,int data_len,unsigned char * key)
{
unsigned char encrypted[4098] = {};
RSA * rsa = createRSA(key,1);
int result = RSA_public_encrypt(data_len,data,encrypted,rsa,padding);
unsigned char* ret = encrypted;
if(result == -1){
cout<<"public_encrypt fails!"<<endl;
exit(-1);
}
return ret;
}
unsigned char* private_decrypt(unsigned char * enc_data,int data_len,unsigned char * key)
{
unsigned char decrypted[4098];
RSA * rsa = createRSA(key,0);
int result = RSA_private_decrypt( data_len,enc_data,decrypted,rsa,padding );
if(result == -1){
cout<<"private_decrypt fails!"<<endl;
exit(-1);
}
unsigned char* ret = decrypted;
return ret;
}
unsigned char* private_encrypt(unsigned char * data,int data_len,unsigned char * key)
{
unsigned char encrypted[4098];
RSA * rsa = createRSA(key,0);
//data = (unsigned char*)"abcde";
int result = RSA_private_encrypt(data_len,data,encrypted,rsa,padding);
if(result == -1){
cout<<"private_encrypt fails!"<<endl;
exit(-1);
}
if(DEBUG)
cout<<"private_encrypt: "<<result<<endl;
unsigned char* ret = encrypted;
return ret;
}
unsigned char* public_decrypt(unsigned char * enc_data,int data_len,unsigned char * key)
{
unsigned char decrypted[4098];
RSA * rsa = createRSA(key,1);
unsigned char cmp[128];
memcpy(cmp, enc_data, 128);
int result = RSA_public_decrypt(data_len,enc_data,decrypted,rsa,padding);
if(result == -1){
cout<<"public_decrypt fails!"<<endl;
exit(-1);
}
// cout<<"MEM Compare-public decrypt: "<<memcmp(enc_data,cmp,128)<<endl;
enc_data = cmp;
unsigned char* ret = decrypted;
return ret;
}
vector<string> split(string orignal, char target){
vector<string> ret;
stringstream ss(orignal);
string token;
while ( getline(ss, token, target) ){
ret.push_back(token);
if(DEBUG)
cout<<"Split out: "<<token<<endl;
}
return ret;
}
string Hash(const char* string){
char outputBuffer[64];
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, string, strlen(string));
SHA256_Final(hash, &sha256);
int i = 0;
for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
}
outputBuffer[64] = 0;
std::string ret(outputBuffer);
return ret;
}
int aes_init(unsigned char *key_data, int key_data_len, unsigned char *salt, EVP_CIPHER_CTX *e_ctx,
EVP_CIPHER_CTX *d_ctx)
{
int i, nrounds = 8;
unsigned char key[32], iv[32];
/*
* Gen key & IV for AES 256 CBC mode. A SHA1 digest is used to hash the supplied key material.
* nrounds is the number of times the we hash the material. More rounds are more secure but
* slower.
*/
i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt, key_data, key_data_len, nrounds, key, iv);
if (i != 32) {
printf("Key size is %d bits - should be 256 bits\n", i);
return -1;
}
EVP_CIPHER_CTX_init(e_ctx);
EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), NULL, key, iv);
EVP_CIPHER_CTX_init(d_ctx);
EVP_DecryptInit_ex(d_ctx, EVP_aes_256_cbc(), NULL, key, iv);
return 0;
}
char *
Encrypt(EVP_CIPHER_CTX *e, unsigned char *plaintext, int *len)
{ /* max ciphertext len for a n bytes of plaintext is n + AES_BLOCK_SIZE -1 bytes */
int c_len = *len + AES_BLOCK_SIZE, f_len = 0;
unsigned char *ciphertext = (unsigned char*)malloc(c_len);
/* allows reusing of 'e' for multiple encryption cycles */
EVP_EncryptInit_ex(e, NULL, NULL, NULL, NULL);
/* update ciphertext, c_len is filled with the length of ciphertext generated,
*len is the size of plaintext in bytes */
EVP_EncryptUpdate(e, ciphertext, &c_len, plaintext, *len);
/* update ciphertext with the final remaining bytes */
EVP_EncryptFinal_ex(e, ciphertext+c_len, &f_len);
*len = c_len + f_len;
return (char*)ciphertext;
}
char *
Decrypt(EVP_CIPHER_CTX *e, unsigned char *ciphertext, int *len)
{
/* plaintext will always be equal to or lesser than length of ciphertext*/
int p_len = *len, f_len = 0;
unsigned char *plaintext = (unsigned char*)malloc(p_len);
EVP_DecryptInit_ex(e, NULL, NULL, NULL, NULL);
EVP_DecryptUpdate(e, plaintext, &p_len, ciphertext, *len);
EVP_DecryptFinal_ex(e, plaintext+p_len, &f_len);
*len = p_len + f_len;
return (char*)plaintext;
}
string send_M0_to_t(string M0, int EK0_len){
std::stringstream ss(M0);
int count = 0;
string piece;
string temp_enc;
string hex_EK0;
while(std::getline(ss, piece, ',')) {
if(count == 2){
temp_enc = piece;
}else if(count == 3){
hex_EK0 = piece;
}
count++;
}
if(DEBUG){
cout<<"PKEK0 is:"<<endl<<temp_enc<<endl;
}
ifstream tpri("SALSCFt.pri");
string t_pri((std::istreambuf_iterator<char>(tpri)),
std::istreambuf_iterator<char>());
cout<<"Private key for t is:\n"<<t_pri<<endl;
temp_enc = hex_to_string(temp_enc);
unsigned char* enc = (unsigned char*)temp_enc.c_str();
unsigned char* dec = private_decrypt((unsigned char*)enc,128, (unsigned char*)t_pri.c_str());
if(DEBUG){
string debug = hexStr(dec,64);
cout<<"Decrypted PKE_K0 is:\n"<<debug<<endl;
}
string temp_EK0 = hex_to_string(hex_EK0);
unsigned char* enc_EK0 = (unsigned char*) temp_EK0.c_str();
EVP_CIPHER_CTX en, de;
unsigned int salt[] = {12345, 54321};
if (aes_init( dec, 64, (unsigned char *)&salt, &en, &de)) {
printf("Couldn't initialize AES cipher\n");
return "fail";
}
int temp_EK0_len = EK0_len;
cout<<"EK0_len_dec: "<<EK0_len<<endl;
string dec_EK0 = string( Decrypt( &de, enc_EK0, &temp_EK0_len) );
if(DEBUG)
cout<<"X0, sign(X0):"<<endl<<dec_EK0<<endl;
string p;
string idlog;
string hex_A0;
string hex_cer;
string hex_signed_hash_X0;
std::stringstream ss1(dec_EK0);
count = 0;
while(std::getline(ss1, piece, ',')) {
if(count == 2){
hex_cer = piece;
}else if(count == 3){
hex_A0 = piece;
}else if(count == 1){
idlog = piece;
}else if(count == 4){
hex_signed_hash_X0 = piece;
}else{
p = piece;
}
count++;
}
string X0(p);
X0 += ","+idlog;
X0 += ",";
X0 += hex_cer;
X0 += ",";
X0 += hex_A0;
if(DEBUG){
// cout<<"Orignal X0 :: X0: "<<endl<<X0.compare(OX0)<<endl;
cout<<"X0: "<<endl<<X0<<endl;
}
unsigned char X0_hash[20];
SHA1((const unsigned char*)X0.c_str(), X0.length(), X0_hash);
ifstream upub("SALSCFu.pub");
string u_pub((std::istreambuf_iterator<char>(upub)),
std::istreambuf_iterator<char>());
unsigned char signed_X0[256]={};
unsigned char* temp_X0 = (unsigned char*)hex_to_string_len(hex_signed_hash_X0,256).c_str();
memcpy(signed_X0,temp_X0, 128);
//cout<<"Compare: "<<memcmp(signed_X0, sig, 256)<<endl;
cout<<"signed: "<<endl<<hexStr(signed_X0, 256)<<endl<<"-------------------------"<<endl;
//cout<<"signed: "<<endl<<hexStr(sig, 256)<<endl<<"-------------------------"<<endl;
unsigned char* org = public_decrypt( signed_X0, 128, (unsigned char*)u_pub.c_str());
if(DEBUG){
cout<<"Orignial X0 Hash is:"<<endl<<hexStr(org, 20)<<endl;
cout<<"Compare with orignal one: "<<memcmp(org, X0_hash, 20)<<endl;
}
if(memcmp(org, X0_hash, 20)!= 0){
cout<<"Signature verify failed!\nExiting!"<<endl;
exit(-1);
}
string A1 = Hash( LastA.c_str() );
string k1_temp = Hash( (W[3]+A1).c_str() );
unsigned char* k1 = (unsigned char*) k1_temp.c_str();
int k1_len = k1_temp.length();
if(DEBUG){
cout<<"Public key for u is:\n"<<u_pub<<endl;
cout<<"k0:"<<endl<<hexStr(k1,k1_len)<<endl;
}
enc = public_encrypt(k1,k1_len, (unsigned char*)u_pub.c_str());
string PKEK1 = hexStr(enc, 128);
if(DEBUG){
cout<<"PKE_K1:"<<endl<<PKEK1<<endl;
ifstream upri("SALSCFu.pri");
string u_pri((std::istreambuf_iterator<char>(upri)),
std::istreambuf_iterator<char>());
cout<<"Private key for u is:\n"<<u_pri<<endl;
unsigned char* dec = private_decrypt(enc,128, (unsigned char*)u_pri.c_str());
string debug = hexStr(dec,k1_len);
cout<<"Decrypted PKE_K1 is:\n"<<debug<<endl;
}
string X1("0,");
X1 += idlog+","+Hash(X0.c_str());
//# Sign X1
if(DEBUG){
cout<<"X1: "<<endl<<X0<<endl;
cout<<"private key for t is:"<<endl<<t_pri<<endl;
}
unsigned char X1_hash[20];
SHA1((const unsigned char*)X1.c_str(), X1.length(), X1_hash);
unsigned char* sig = private_encrypt(X1_hash, 20, (unsigned char*)t_pri.c_str());
string sign((const char*)sig);
string temp_EK1 = X1+","+hexStr(sig, 128);
if(DEBUG){
cout<<"LEN: "<<hexStr(sig, 128).length()<<endl;
cout<<"Message in EK0 is:"<<endl<<temp_EK0<<endl;
}
int temp_EK1_len = temp_EK1.length();
unsigned char* EK1 = (unsigned char*)Encrypt(&en, (unsigned char*)temp_EK1.c_str(), &temp_EK1_len);
int EK1_len = temp_EK1_len;
string M1("1,");
M1 += idlog+","+PKEK1+","+hexStr(EK1,EK1_len);
return M1;
}
void write( int message_size, string w, string encipher,string y, string z){
file<<message_size<<"|"<<w<<"|"<<string_to_hex(encipher)<<"|"<<string_to_hex(y)<<"|"<<string_to_hex(z)<<endl;
}
void send_M1_to_u(string message){
if(DEBUG)
cout<<"Adding message: "<<message<<endl;
string A = Hash( LastA.c_str() );
string Df = message;
//################################################
unsigned int salt[] = {12345, 54321};
string key_data = Hash( (W[3]+A).c_str() );
int key_data_len = key_data.length();
int plaintext_len = Df.length()+1;
EVP_CIPHER_CTX en, de;
if(DEBUG)
cout<<"Key for Df is: "<<key_data<<", with size: "<<key_data_len<<endl;
if (aes_init( (unsigned char*)key_data.c_str(), key_data_len, (unsigned char *)&salt, &en, &de)) {
printf("Couldn't initialize AES cipher\n");
return;
}
unsigned char* ciphertext = (unsigned char*)Encrypt(&en, (unsigned char*)Df.c_str(), &plaintext_len);
string encipher( (const char*)ciphertext);
//###############################################
if(DEBUG){
cout<<"---------------\nEncrypt data: \n"<<string_to_hex(encipher)<<endl;
string decipher = string( Decrypt( &de, ciphertext, &plaintext_len) );
cout<<"---------------\nDecrypt data: \n"<<decipher<<endl;
}
string y = Hash( (encipher+W[3]).c_str() );
if(DEBUG)
cout<<"Y: "<<y<<", with size: "<<y.size()<<endl;
char tempZ[21];
unsigned int zlength = 20;
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx,A.c_str(),20,EVP_sha256(),NULL);
HMAC_Update(&ctx,(const unsigned char *)y.c_str(),20);
HMAC_Final(&ctx,(unsigned char *)tempZ,&zlength);
tempZ[20] = '\0';
string z(tempZ);
LastA = A;
if(DEBUG)
cout<<"Z: "<<string_to_hex(z)<<", with size: "<<z.size()<<endl;
write(Df.size(), W[3], encipher,y,z);
cout<<"Added log entry number "<< id++<<endl;
}
void createlog(string filename){
if(DEBUG)
cout<<"Creating file: "<<filename<<endl;
if( lock ){
cout<<"You already open a log!"<<endl;
return;
}
ifstream ifile(filename.c_str());
if (ifile) {
cout<<"file already exist, please choose another file!"<<endl;
return;
// The file exists, and is open for input
}
if( idlog.empty() ){
//file.open( filename.c_str(), ios::out | ios::app | ios::trunc );
file.open(filename.c_str());
}
else{
cout<<"You already open a log!"<<endl;
lock = 0;
return;
}
// Initialize
id = 0;
time(&d);
ifstream myfile ("SALSCF.symm");
if (myfile.is_open())
{
string a0((std::istreambuf_iterator<char>(myfile)),
std::istreambuf_iterator<char>());
A0 = a0;
myfile.close();
}
if(DEBUG)
cout<<"A0: "<<string_to_hex(A0)<<endl;
unsigned int salt[] = {12345, 54321};
string key_data = Hash( (W[0]+A0).c_str() );
int key_data_len = key_data.length();
EVP_CIPHER_CTX en, de;
if(DEBUG){
cout<<"key_data is:"<<endl<<string_to_hex(key_data)<<endl<<"With key len: "<<key_data_len<<endl;
}
if (aes_init( (unsigned char*)key_data.c_str(), key_data_len, (unsigned char *)&salt, &en, &de)) {
printf("Couldn't initialize AES cipher\n");
return;
}
//##################################
// Startup
struct tm * timeinfo;
timeinfo = localtime (&d);
timeinfo->tm_min += 100;
dplus = mktime(timeinfo);
idlog = filename;
LastA = A0;
int k0_len = key_data_len;
/*
unsigned char k0[k0_len];
memset(k0, 0, sizeof(k0));
if (!RAND_bytes(k0, k0_len))
{
exit(-1);
}
*/
unsigned char* k0 = (unsigned char*)key_data.c_str();
unsigned char iv[AES_BLOCK_SIZE];
if (!RAND_bytes(iv, AES_BLOCK_SIZE))
{
exit(-1);
}
AES_KEY enc_key, dec_key;
unsigned char enc_out[AES_BLOCK_SIZE];
unsigned char dec_out[AES_BLOCK_SIZE];
AES_set_encrypt_key(k0, k0_len, &enc_key);
// Deal with certificate
ifstream t("SALSCFu.pem");
string cer((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
// Conver pem to X509
const unsigned char *data = (const unsigned char*)cer.c_str();
BIO *bio;
X509 *certificate;
bio = BIO_new(BIO_s_mem());
BIO_puts(bio, (const char*)data);
certificate = PEM_read_bio_X509(bio, NULL, NULL, NULL);
// Conver ends here
string D0( ctime(&d) );
string temp( ctime(&dplus) );
D0 = D0 + temp;
D0 += idlog;
string X0("0,");
X0 += idlog;
X0 += ",";
X0 += string_to_hex(cer);
X0 += ",";
X0 += string_to_hex(A0);
//# Use t's public key to encrypt K0
ifstream tpub("SALSCFt.pub");
string t_pub((std::istreambuf_iterator<char>(tpub)),
std::istreambuf_iterator<char>());
if(DEBUG){
cout<<"Public key for t is:\n"<<t_pub<<endl;
cout<<"k0:"<<endl<<hexStr(k0,k0_len)<<endl;
}
unsigned char* enc = public_encrypt(k0,k0_len, (unsigned char*)t_pub.c_str());
string PKEK0 = hexStr(enc, 128);
if(DEBUG){
cout<<"PKE_K0:"<<endl<<PKEK0<<endl;
ifstream tpri("SALSCFt.pri");
string t_pri((std::istreambuf_iterator<char>(tpri)),
std::istreambuf_iterator<char>());
cout<<"Private key for t is:\n"<<t_pri<<endl;
unsigned char* dec = private_decrypt(enc,128, (unsigned char*)t_pri.c_str());
string debug = hexStr(dec,k0_len);
cout<<"Decrypted PKE_K0 is:\n"<<debug<<endl;
}
//# Sign X0
ifstream upri("SALSCFu.pri");
string u_pri((std::istreambuf_iterator<char>(upri)),
std::istreambuf_iterator<char>());
if(DEBUG){
cout<<"X0: "<<endl<<X0<<endl;
cout<<"private key for u is:"<<endl<<u_pri<<endl;
}
unsigned char X0_hash[20];
SHA1((const unsigned char*)X0.c_str(), X0.length(), X0_hash);
unsigned char* sig = private_encrypt(X0_hash, 20, (unsigned char*)u_pri.c_str());
if(DEBUG){
cout<<"X0 Hash is:"<<endl<<hexStr(X0_hash, 20)<<endl;
}
if(!DEBUG){
ifstream upub("SALSCFu.pub");
string u_pub((std::istreambuf_iterator<char>(upub)),
std::istreambuf_iterator<char>());
cout<<"public key for u is:"<<endl<<u_pub<<endl;
unsigned char cmp[128];
memcpy(cmp, sig, 128);
cout<<"MEM Compare: "<<memcmp(sig,cmp,128)<<endl;
unsigned char* org = public_decrypt(cmp, 128, (unsigned char*)u_pub.c_str());
cout<<"MEM Compare: "<<memcmp(sig,cmp,128)<<endl;
//org = public_decrypt(sig, 128, (unsigned char*)u_pub.c_str());
cout<<"Orignial X0 Hash is:"<<endl<<hexStr(org, 20)<<endl;
}
string sign((const char*)sig);
string temp_EK0 = X0+","+hexStr(sig, 128);
if(DEBUG){
cout<<"LEN: "<<hexStr(sig, 128).length()<<endl;
cout<<"Message in EK0 is:"<<endl<<temp_EK0<<endl;
}
int temp_EK0_len = temp_EK0.length();
unsigned char* EK0 = (unsigned char*)Encrypt(&en, (unsigned char*)temp_EK0.c_str(), &temp_EK0_len);
int EK0_len = temp_EK0_len;
if(DEBUG){
cout<<"EK0_len: "<<EK0_len<<endl;
string dec_EK0 = string( Decrypt( &de, EK0, &temp_EK0_len) );
cout<<"---------------\nDecrypt data: \n"<<dec_EK0<<endl;
}
string M0("0");
M0 += ","+idlog +","+PKEK0 + ","+hexStr(EK0, EK0_len);
D0 += M0;
string M1 = send_M0_to_t(M0, EK0_len);
// Startup ends here
//##################################
//################################################
int plaintext_len = D0.length()+1;
if(DEBUG)
cout<<"Key for D0 is: "<<key_data<<", with size: "<<key_data_len<<endl;
unsigned char* ciphertext = (unsigned char*)Encrypt(&en, (unsigned char*)D0.c_str(), &plaintext_len);
string encipher( (const char*)ciphertext);
//###############################################
if(DEBUG){
cout<<"---------------\nEncrypt data: \n"<<string_to_hex(encipher)<<endl;
string decipher = string( Decrypt( &de, ciphertext, &plaintext_len) );
cout<<"---------------\nDecrypt data: \n"<<decipher<<endl;
}
//###############################################
string y = Hash( (encipher+W[0]).c_str() );
if(DEBUG)
cout<<"Y: "<<y<<", with size: "<<y.size()<<endl;
char tempZ[21];
unsigned int zlength = 20;
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx,A0.c_str(),20,EVP_sha256(),NULL);
HMAC_Update(&ctx,(const unsigned char *)y.c_str(),20);
HMAC_Final(&ctx,(unsigned char *)tempZ,&zlength);
tempZ[20] = '\0';
string z(tempZ);
if(DEBUG)
cout<<"Z: "<<string_to_hex(z)<<", with size: "<<z.size()<<endl;
if(file.is_open()){
//file<<W[0]<<"|"<<encipher<<endl;
write(D0.size(), W[0], encipher,y,z);
id++;
}else{
cout<<"File open failed\n";
exit(0);
}
lock = 1;
send_M1_to_u(M1);
}
void add(string message){
if(DEBUG)
cout<<"Adding message: "<<message<<endl;
string A = Hash( LastA.c_str() );
string Df = message;
//################################################
unsigned int salt[] = {12345, 54321};
string key_data = Hash( (W[1]+A).c_str() );
int key_data_len = key_data.length();
int plaintext_len = Df.length()+1;
EVP_CIPHER_CTX en, de;
if(DEBUG)
cout<<"Key for Df is: "<<key_data<<", with size: "<<key_data_len<<endl;
if (aes_init( (unsigned char*)key_data.c_str(), key_data_len, (unsigned char *)&salt, &en, &de)) {
printf("Couldn't initialize AES cipher\n");
return;
}
unsigned char* ciphertext = (unsigned char*)Encrypt(&en, (unsigned char*)Df.c_str(), &plaintext_len);
string encipher( (const char*)ciphertext);
//###############################################
if(DEBUG){
cout<<"---------------\nEncrypt data: \n"<<string_to_hex(encipher)<<endl;
string decipher = string( Decrypt( &de, ciphertext, &plaintext_len) );
cout<<"---------------\nDecrypt data: \n"<<decipher<<endl;
}
string y = Hash( (encipher+W[1]).c_str() );
if(DEBUG)
cout<<"Y: "<<y<<", with size: "<<y.size()<<endl;
char tempZ[21];
unsigned int zlength = 20;
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx,A.c_str(),20,EVP_sha256(),NULL);
HMAC_Update(&ctx,(const unsigned char *)y.c_str(),20);
HMAC_Final(&ctx,(unsigned char *)tempZ,&zlength);
tempZ[20] = '\0';
string z(tempZ);
LastA = A;
if(DEBUG)
cout<<"Z: "<<string_to_hex(z)<<", with size: "<<z.size()<<endl;
write(Df.size(), W[1], encipher,y,z);
cout<<"Added log entry number "<< id++<<endl;
}
void closelog(){
string A = Hash( LastA.c_str() );
string Df = "Close";
//################################################
unsigned int salt[] = {12345, 54321};
string key_data = Hash( (W[2]+A).c_str() );
int key_data_len = key_data.length();
int plaintext_len = Df.length()+1;
EVP_CIPHER_CTX en, de;
if(DEBUG)
cout<<"Key for Df is: "<<key_data<<", with size: "<<key_data_len<<endl;
if (aes_init( (unsigned char*)key_data.c_str(), key_data_len, (unsigned char *)&salt, &en, &de)) {
printf("Couldn't initialize AES cipher\n");
return;
}
unsigned char* ciphertext = (unsigned char*)Encrypt(&en, (unsigned char*)Df.c_str(), &plaintext_len);
string encipher( (const char*)ciphertext);
//###############################################
if(DEBUG){
cout<<"---------------\nEncrypt data: \n"<<string_to_hex(encipher)<<endl;
string decipher = string( Decrypt( &de, ciphertext, &plaintext_len) );
cout<<"---------------\nDecrypt data: \n"<<decipher<<endl;
}
string y = Hash( (encipher+W[2]).c_str() );
if(DEBUG)
cout<<"Y: "<<y<<", with size: "<<y.size()<<endl;
char tempZ[21];
unsigned int zlength = 20;
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx,A.c_str(),20,EVP_sha256(),NULL);
HMAC_Update(&ctx,(const unsigned char *)y.c_str(),20);
HMAC_Final(&ctx,(unsigned char *)tempZ,&zlength);
tempZ[20] = '\0';
string z(tempZ);
LastA = A;
if(DEBUG)
cout<<"Z: "<<string_to_hex(z)<<", with size: "<<z.size()<<endl;
write(Df.size(), W[2], encipher,y,z);
lock = 0;
file.close();
}
void verify(int number){
if(DEBUG)
cout<<"Verifying "<<number<<" entry!"<<endl;
int lineID = 0;
string last_y;
int find = 0;
string line;
string A = A0;
file.close();
ifstream myfile (idlog.c_str() );
if (myfile.is_open())
{
while ( getline (myfile,line) ){
if(lineID == number){
find = 1;
break;
}else if(lineID+1 == number){
last_y = hex_to_string( split(line,'|').at(3) );
}
lineID++;
}
myfile.close();
}
lineID = 0;
while(lineID < number){
A = Hash(A.c_str());
lineID++;
}
if(DEBUG)
cout<<"A0: "<<string_to_hex(A0)<<", A"<<number<<": "<<string_to_hex(A)<<endl;
if(find){
vector<string> stringList = split(line,'|');
int message_size = atoi(stringList.at(0).c_str() );
string w = stringList.at(1);
if( !w.compare(W[0]) || !w.compare(W[2]) || !w.compare(W[3]) || !w.compare(W[4]) )
{
cout<<"Invalid index!\n";
return;
}
string encipher = hex_to_string( stringList.at(2) );
string y = hex_to_string( stringList.at(3) );
string z = hex_to_string( stringList.at(4) );
char tempZ[21];
unsigned int zlength = 20;
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx,A.c_str(),20,EVP_sha256(),NULL);
HMAC_Update(&ctx,(const unsigned char *)y.c_str(),20);
HMAC_Final(&ctx,(unsigned char *)tempZ,&zlength);