-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptogator.c
executable file
·1158 lines (816 loc) · 28.6 KB
/
cryptogator.c
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 <stdio.h>
#include <gcrypt.h>
#include <time.h>
#include <string.h>
//clock_t begin, end;
//double time_spent;
//begin = clock();
//*here, do your time-consuming job */
//end = clock();
//time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
////////////////////////
//FUNCTION DEFINITIONS//
////////////////////////
void myMD5(FILE *file,double md5_buffer[],int index);
void myAES128(FILE *file,double aes128_encbuffer[],double aes128_decbuffer[],int index);
void mySHA1(FILE *file,double sha1_buffer[],int index);
unsigned char* mySHA256(FILE *file,double sha256_buffer[],int index);
void myRSA1024(FILE *file,double rsa1024_encbuffer[],double rsa1024_decbuffer[],int index);
void myAES256(FILE *file,double aes256_encbuffer[],double aes256_decbuffer[],int index);
void myDS(FILE *file);
void myRSA4096(FILE *file,double rsa4096_encbuffer[],double rsa4096_decbuffer[],int index);
void convertToBuffer(char *buffer,gcry_sexp_t *sexp );
void convertToSexpression( gcry_sexp_t sexp, char* buffer, int len);
void prettyPrint( char *input_sexp, int len );
char* fileToBuffer(FILE *file);
double getMean(double buffer[],double total);
double getMedian(double buffer[],double total);
int main(int argc, const char * argv[])
{
///////////////
///OPEN FILE///
//////////////
FILE *file;
file =fopen(argv[1], "rb");
if (!file)
{
printf("file not opened successfully");
return 1;
}
////////////////////////////
///GCRYPT INITIALIZATIONS///
////////////////////////////
gcry_check_version ( NULL );
gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
////////////////////
///METHOD CALLS////
//////////////////
/////////////////////
//////AES128 TIME////
////////////////////
double aes128_encbuffer[100];
double aes128_decbuffer[100];
int i;
printf("\nRUNNING AES 128...\n");
for( i=0;i<100;i++)
{
myAES128(file,aes128_encbuffer,aes128_decbuffer,i);
}
double aes128_encmean= getMean(aes128_encbuffer,100.0);
double aes128_encmedian= getMedian(aes128_encbuffer,100.0);
double aes128_decmean= getMean(aes128_decbuffer,100.0);
double aes128_decmedian= getMedian(aes128_decbuffer,100.0);
printf("\nMEAN TIME FOR AES128 ENCRYPTION IS :%f ms\n", aes128_encmean);
printf("\nMEDIAN TIME FOR AES128 ENCRYPTION IS :%f ms\n", aes128_encmedian);
printf("\nMEAN TIME FOR AES128 DECRYPTION IS :%f ms\n", aes128_decmean);
printf("\nMEDIAN TIME FOR AES128 DECRYPTION IS :%f ms\n", aes128_decmedian);
/////////////////////
//////AES256 TIME////
////////////////////
double aes256_encbuffer[100];
double aes256_decbuffer[100];
printf("\nRUNNING AES 256...\n");
for( i=0;i<100;i++)
{
myAES256(file,aes256_encbuffer,aes256_decbuffer,i);
}
double aes256_encmean= getMean(aes256_encbuffer,100.0);
double aes256_encmedian= getMedian(aes256_encbuffer,100.0);
double aes256_decmean= getMean(aes256_decbuffer,100.0);
double aes256_decmedian= getMedian(aes256_decbuffer,100.0);
printf("\nMEAN TIME FOR AES256 ENCRYPTION IS :%f ms\n", aes256_encmean);
printf("\nMEDIAN TIME FOR AES256 ENCRYPTION IS :%f ms\n", aes256_encmedian);
printf("\nMEAN TIME FOR AES256 DECRYPTION IS :%f ms\n", aes256_decmean);
printf("\nMEDIAN TIME FOR AES256 DECRYPTION IS :%f ms\n", aes256_decmedian);
///////////////////
//////MD5 TIME////
//////////////////
printf("\nRUNNING MD5...\n");
double md5_buffer[100];
for( i=0;i<100;i++)
{
myMD5(file,md5_buffer,i);
}
//printf("final i : %f",md5_buffer[99] );
double md5_mean= getMean(md5_buffer,100.0);
double md5_median= getMedian(md5_buffer,100.0);
printf("\nMEAN TIME FOR MD5 IS :%f ms\n", md5_mean);
printf("\nMEDIAN TIME FOR MD5 IS :%f ms\n", md5_median);
///////////////////
//////SHA1 TIME////
//////////////////
printf("\nRUNNING SHA 1...\n");
double sha1_buffer[100];
for(i=0;i<100;i++)
{
mySHA1(file,sha1_buffer,i);
}
//printf("final i : %f",md5_buffer[99] );
double sha1_mean= getMean(sha1_buffer,100.0);
double sha1_median= getMedian(sha1_buffer,100.0);
printf("\nMEAN TIME FOR SHA1 IS :%f ms\n", sha1_mean);
printf("\nMEDIAN TIME FOR SHA1 IS :%f ms\n", sha1_median);
/////////////////////
//////SHA256 TIME////
////////////////////
printf("\nRUNNING SHA 256...\n");
double sha256_buffer[100];
for( i=0;i<100;i++)
{
mySHA256(file,sha256_buffer,i);
}
//printf("final i : %f",md5_buffer[99] );
double sha256_mean= getMean(sha256_buffer,100.0);
double sha256_median= getMedian(sha256_buffer,100.0);
printf("\nMEAN TIME FOR SHA256 IS :%f ms\n", sha256_mean);
printf("\nMEDIAN TIME FOR SHA256 IS :%f ms\n", sha256_median);
///////////////////////////////
///////DIGITAL SIGNATURE/////////
////////////////////////////////
printf("\nRUNNING RSA 1024...\n");
double rsa1024_encbuffer[100];
double rsa1024_decbuffer[100];
for( i=0;i<100;i++) ////
{
myRSA1024(file,rsa1024_encbuffer,rsa1024_decbuffer,i);
}
double rsa1024_encmean= getMean(rsa1024_encbuffer,100.0);
double rsa1024_encmedian= getMedian(rsa1024_encbuffer,100.0);
double rsa1024_decmean= getMean(rsa1024_decbuffer,100.0);
double rsa1024_decmedian= getMedian(rsa1024_decbuffer,100.0);
printf("\nMEAN TIME FOR RSA1024 ENCRYPTION IS :%f ms\n", rsa1024_encmean);
printf("\nMEDIAN TIME FOR RSA1024 ENCRYPTION IS :%f ms\n", rsa1024_encmedian);
printf("\nMEAN TIME FOR RSA1024 DECRYPTION IS :%f ms\n", rsa1024_decmean);
printf("\nMEDIAN TIME FOR RSA1024 DECRYPTION IS :%f ms\n", rsa1024_decmedian);
printf("\nRUNNING RSA 4096...\n");
double rsa4096_encbuffer[100];
double rsa4096_decbuffer[100];
for(i=0;i<100;i++) ////
{
myRSA4096(file,rsa4096_encbuffer,rsa4096_decbuffer,i);
}
double rsa4096_encmean= getMean(rsa4096_encbuffer,100.0);
double rsa4096_encmedian= getMedian(rsa4096_encbuffer,100.0);
double rsa4096_decmean= getMean(rsa4096_decbuffer,100.0);
double rsa4096_decmedian= getMedian(rsa4096_decbuffer,100.0);
printf("\nMEAN TIME FOR RSA4096 ENCRYPTION IS :%f ms\n", rsa4096_encmean);
printf("\nMEDIAN TIME FOR RSA4096 ENCRYPTION IS :%f ms\n", rsa4096_encmedian);
printf("\nMEAN TIME FOR RSA4096 DECRYPTION IS :%f ms\n", rsa4096_decmean);
printf("\nMEDIAN TIME FOR RSA4096 DECRYPTION IS :%f ms\n", rsa4096_decmedian);
printf("\nRUNNING DIGITAL SIGNATURE 256...\n");
myDS(file);
fclose(file);
}
////////////////////
///////DS/////////
//////////////////
void myDS(FILE* file)
{
double temp[1];
unsigned char * sha=mySHA256(file,temp,0);
gcry_sexp_t rsatoken_sexp,rsakey_sexp;
gcry_sexp_t publickey_sexp,privatekey_sexp;
gcry_sexp_t sha_sexp;
gcry_sexp_t ds_sexp;
gcry_error_t err;
gcry_sexp_t output_sexp;
char buffer[6000];
char* publickeytoken="public-key";
char* privatekeytoken="private-key";
char* rsatoken="(genkey (rsa (nbits 4:4096)))";
err=gcry_sexp_new( &rsatoken_sexp, rsatoken, strlen(rsatoken), 1 );
if(err)
{
printf("error while while generating rsatoken_sexp");
}
err=gcry_pk_genkey( &rsakey_sexp, rsatoken_sexp );
if(err)
{
printf("error while while generating rsakey_sexp");
}
gcry_sexp_release(rsatoken_sexp);
publickey_sexp = gcry_sexp_find_token( rsakey_sexp, publickeytoken, 0 );
if(publickey_sexp==NULL)
{
printf("error while extracting public key");
}
privatekey_sexp = gcry_sexp_find_token( rsakey_sexp, privatekeytoken, 0 );
if(privatekey_sexp==NULL)
{
printf("error while extracting private key");
}
gcry_sexp_release(rsakey_sexp);
//PUBLIC AND PRIVATE KEY GENERATED
convertToBuffer( sha, &sha_sexp );
err=gcry_pk_sign (&ds_sexp, sha_sexp, privatekey_sexp);
convertToSexpression( ds_sexp, buffer, 6000 );
printf( "\ndigital signature: " );
prettyPrint( buffer, 6000 );
printf("the digital signature of sha256 was successfullly created.\n");
err=gcry_pk_verify (ds_sexp, sha_sexp, publickey_sexp);
if(err==0)
{
printf("Sucessfully verfied digital signature!\n");
}
}
//////////////////
///////RSA/////////
//////////////////
void myRSA1024(FILE *file,double rsa1024_encbuffer[],double rsa1024_decbuffer[],int index)
{
clock_t begin_enc, end_enc , begin_dec, end_dec;
double time_enc,time_dec;
double total_enc=0.0;
double total_dec=0.0;
//char* plaintext=fileToBuffer(fp);
//printf("%s",plaintext);
int size=filesize(file);
//fprintf(file, "%s", "aString");
//printf("\nfile size is :%d" , size);
//printf("\n");
char buffer[6000];
//output[size+100];
gcry_sexp_t rsatoken_sexp,rsakey_sexp;
gcry_sexp_t publickey_sexp,privatekey_sexp;
gcry_sexp_t input_sexp;
gcry_sexp_t ciphertext_sexp;
gcry_sexp_t plaintext_sexp = NULL;
gcry_error_t err;
gcry_mpi_t input_mpi;
int i;
char* publickeytoken="public-key";
char* privatekeytoken="private-key";
char* rsatoken="(genkey (rsa (nbits 4:1024)))";
err=gcry_sexp_new( &rsatoken_sexp, rsatoken, strlen(rsatoken), 1 );
if(err)
{
printf("error while while generating rsatoken_sexp");
}
err=gcry_pk_genkey( &rsakey_sexp, rsatoken_sexp );
if(err)
{
printf("error while while generating rsakey_sexp");
}
gcry_sexp_release(rsatoken_sexp);
publickey_sexp = gcry_sexp_find_token( rsakey_sexp, publickeytoken, 0 );
if(publickey_sexp==NULL)
{
printf("error while extracting public key");
}
privatekey_sexp = gcry_sexp_find_token( rsakey_sexp, privatekeytoken, 0 );
if(privatekey_sexp==NULL)
{
printf("error while extracting private key");
}
//convertToSexpression( publickey_sexp, buffer, 6000 );
//prettyPrint( buffer, 6000 );
//printf( "Public Key:\n%s\n\n", buffer );
//convertToSexpression( privatekey_sexp, buffer, 6000 );
//prettyPrint( buffer, 6000 );
//printf( "Private Key:\n%s\n\n", buffer );
gcry_sexp_release(rsakey_sexp);
///PRIVATE PUBLIC KEY GENERATED
char* source=malloc(64);
char* output=(char *)malloc(sizeof(char)*size);
gcry_sexp_t source_sexp;
gcry_sexp_t destination_sexp;
int ctr;
if(size%64==0)
{
ctr=size/64;
}
else
{
ctr=(size/64) + 1;
}
int counter=0;
while(ctr>0)
{
int numofbyte=fread(source, 1, 64, file);
//printf("num of bytes read : %d ",numofbyte);
//printf("\n");
if(numofbyte<64)
{
//printf("\n");
int padding=64 - numofbyte;
//printf("adding padding of %d bytes.", padding);
//printf("\n");
for(i=numofbyte;i<padding;i++)
{
source[i]="0";
}
}
//printf("iterations left :%d ",ctr);
//printf("\n");
convertToBuffer( source, &source_sexp );
//PRINT SEXP
//convertToSexpression( source_sexp, buffer, 6000 );
//prettyPrint( buffer, strlen(buffer) );
//printf( "source: \n%s\n\n", buffer );
begin_enc=clock();
err = gcry_pk_encrypt( &destination_sexp, source_sexp, publickey_sexp );
//convertToSexpression( destination_sexp, buffer, 6000 );
//prettyPrint( buffer, strlen(buffer) );
//printf( "destination: \n%s\n\n", buffer );
end_enc=clock();
time_enc = (double)(end_enc - begin_enc)*1000.0 / CLOCKS_PER_SEC;
total_enc+=time_enc;
begin_dec=clock();
err = gcry_pk_decrypt( &plaintext_sexp, destination_sexp, privatekey_sexp );
//convertToSexpression( plaintext_sexp, buffer, 6000 );
//prettyPrint( buffer, strlen(buffer) );
//printf( "plaintext: \n%s\n\n", buffer );
end_dec=clock();
time_dec=(double)(end_dec - begin_dec)*1000.0 / CLOCKS_PER_SEC;
total_dec+=time_dec;
char* destination=malloc(64);
//convertToSexpression(destination_sexp,destination,sizeof(destination));
//size_t s = gcry_sexp_sprint( plaintext_sexp, GCRYSEXP_FMT_DEFAULT, NULL, sizeof(destination) ) ;
//if(s==0)
//printf("%zu\n", s);
destination=gcry_sexp_nth_string (plaintext_sexp,0);
//printf("output is : %s",destination);
//printf("\n");
for(i=0;i<64;i++)
{
output[counter]=destination[i];
counter++;
}
ctr--;
free(destination);
}
rsa1024_encbuffer[index]=total_enc;
rsa1024_decbuffer[index]=total_dec;
/*
printf("output :");
for(int i=0;i<size;i++)
{
printf("%c ",output[i]);
}
*/
rewind( file );
}
void myRSA4096(FILE *file,double rsa4096_encbuffer[],double rsa4096_decbuffer[],int index)
{
clock_t begin_enc, end_enc , begin_dec, end_dec ;
double total_enc=0.0;
double total_dec=0.0;
double time_enc,time_dec;
int i;
//char* plaintext=fileToBuffer(fp);
//printf("%s",plaintext);
int size=filesize(file);
//fprintf(file, "%s", "aString");
//printf("\nfile size is :%d" , size);
//printf("\n");
char buffer[6000];
//output[size+100];
gcry_sexp_t rsatoken_sexp,rsakey_sexp;
gcry_sexp_t publickey_sexp,privatekey_sexp;
gcry_sexp_t input_sexp;
gcry_sexp_t ciphertext_sexp;
gcry_sexp_t plaintext_sexp = NULL;
gcry_error_t err;
gcry_mpi_t input_mpi;
char* publickeytoken="public-key";
char* privatekeytoken="private-key";
char* rsatoken="(genkey (rsa (nbits 4:4096)))";
err=gcry_sexp_new( &rsatoken_sexp, rsatoken, strlen(rsatoken), 1 );
if(err)
{
printf("error while while generating rsatoken_sexp");
}
err=gcry_pk_genkey( &rsakey_sexp, rsatoken_sexp );
if(err)
{
printf("error while while generating rsakey_sexp");
}
gcry_sexp_release(rsatoken_sexp);
publickey_sexp = gcry_sexp_find_token( rsakey_sexp, publickeytoken, 0 );
if(publickey_sexp==NULL)
{
printf("error while extracting public key");
}
privatekey_sexp = gcry_sexp_find_token( rsakey_sexp, privatekeytoken, 0 );
if(privatekey_sexp==NULL)
{
printf("error while extracting private key");
}
//convertToSexpression( publickey_sexp, buffer, 6000 );
//prettyPrint( buffer, 6000 );
//printf( "Public Key:\n%s\n\n", buffer );
//convertToSexpression( privatekey_sexp, buffer, 6000 );
//prettyPrint( buffer, 6000 );
//printf( "Private Key:\n%s\n\n", buffer );
gcry_sexp_release(rsakey_sexp);
///PRIVATE PUBLIC KEY GENERATED
char* source=malloc(64);
char* output=(char *)malloc(sizeof(char)*size);
gcry_sexp_t source_sexp;
gcry_sexp_t destination_sexp;
int ctr;
if(size%64==0)
{
ctr=size/64;
}
else
{
ctr=(size/64) + 1;
}
int counter=0;
while(ctr>0)
{
int numofbyte=fread(source, 1, 64, file);
//printf("num of bytes read : %d ",numofbyte);
//printf("\n");
if(numofbyte<64)
{
//printf("\n");
int padding=64 - numofbyte;
//printf("adding padding of %d bytes.", padding);
//printf("\n");
for( i=numofbyte;i<padding;i++)
{
source[i]="0";
}
}
//printf("iterations left :%d ",ctr);
//printf("\n");
convertToBuffer( source, &source_sexp );
//PRINT SEXP
//convertToSexpression( source_sexp, buffer, 6000 );
//prettyPrint( buffer, strlen(buffer) );
//printf( "source: \n%s\n\n", buffer );
begin_enc=clock();
err = gcry_pk_encrypt( &destination_sexp, source_sexp, publickey_sexp );
//convertToSexpression( destination_sexp, buffer, 6000 );
//prettyPrint( buffer, strlen(buffer) );
//printf( "destination: \n%s\n\n", buffer );
end_enc=clock();
time_enc = (double)(end_enc - begin_enc)*1000.0 / CLOCKS_PER_SEC;
total_enc+=time_enc;
begin_dec=clock();
err = gcry_pk_decrypt( &plaintext_sexp, destination_sexp, privatekey_sexp );
//convertToSexpression( plaintext_sexp, buffer, 6000 );
//prettyPrint( buffer, strlen(buffer) );
//printf( "plaintext: \n%s\n\n", buffer );
end_dec=clock();
time_dec=(double)(end_dec - begin_dec)*1000.0 / CLOCKS_PER_SEC;
total_dec+=time_dec;
char* destination=malloc(64);
//convertToSexpression(destination_sexp,destination,sizeof(destination));
//size_t s = gcry_sexp_sprint( plaintext_sexp, GCRYSEXP_FMT_DEFAULT, NULL, sizeof(destination) ) ;
//if(s==0)
//printf("%zu\n", s);
destination=gcry_sexp_nth_string (plaintext_sexp,0);
//printf("output is : %s",destination);
//printf("\n");
for( i=0;i<64;i++)
{
output[counter]=destination[i];
counter++;
}
ctr--;
free(destination);
}
rsa4096_encbuffer[index]=total_enc;
rsa4096_decbuffer[index]=total_dec;
/*
printf("output :");
for(int i=0;i<size;i++)
{
printf("%c ",output[i]);
}
*/
rewind( file );
}
////////////////////////////
/////RSA HELPER FUNCTIONS///
///////////////////////////
void convertToBuffer(char *buffer,gcry_sexp_t *sexp )
{
gcry_error_t err;
gcry_mpi_t buffer_mpi;
err=gcry_mpi_scan( &buffer_mpi, GCRYMPI_FMT_USG, buffer, strlen(buffer), NULL ) ;
err=gcry_sexp_build( sexp, NULL, "(data(flags raw)(value %m))", buffer_mpi ) ;
if(err)
{
printf("error while converting s-expression to buffer");
}
gcry_mpi_release( buffer_mpi );
return;
}
void convertToSexpression( gcry_sexp_t sexp, char* buffer, int len)
{
size_t size;
size = gcry_sexp_sprint( sexp, GCRYSEXP_FMT_DEFAULT, buffer, len ) ;
if(size==0)
{
printf("error while converting from buffer to s-expression");
}
}
void prettyPrint( char *input_sexp, int len )
{
int i;
for( i=0; i < len; i++ )
{
if( input_sexp[i] == ')' && input_sexp[i+1] == '\n' && input_sexp[i+2] == '\0' )
{
break;
}
if( input_sexp[i] == '\0' )
{
input_sexp[i] = ' ';
}
}
printf("%s",input_sexp);
printf("\n");
return;
}
///////////////////
////////MD5///////
//////////////////
void myMD5(FILE* file,double md5_buffer[],int index)
{
clock_t start, end;
double tim;
int i;
char* plaintext=fileToBuffer(file);
#define algo GCRY_MAC_HMAC_MD5
gcry_mac_hd_t handle;
gcry_error_t err;
unsigned int mlen;
size_t keylength= gcry_mac_get_algo_keylen(algo);
//printf("%u",keylength); // 64
unsigned char* buffer=plaintext;
size_t bufferlength = strlen(buffer)+1;
unsigned char * key = malloc(keylength); // 16 bytes
gcry_randomize (key, keylength, GCRY_VERY_STRONG_RANDOM);
//printf("\nMD5 KEY GENERATED!!\n");
mlen = gcry_mac_get_algo_maclen (algo);
start=clock();
err=gcry_mac_open (&handle,algo, 0, NULL);
err=gcry_mac_setkey (handle, key, keylength);
err=gcry_mac_write (handle, buffer,bufferlength );
unsigned int length=gcry_mac_get_algo_maclen (algo);
size_t hmaclength=length;
unsigned char* hmac=malloc(hmaclength);
gcry_mac_read (handle, hmac, &hmaclength);
end=clock();
tim=(double)(end-start)*1000/CLOCKS_PER_SEC;
md5_buffer[index]=tim;
/*
printf("\nMD5 GENERATED IS : ");
for( i = 0; i < hmaclength ; i++) { printf("%02x",hmac[i]); }
printf("\n");
*/
gcry_mac_close (handle);
}
///////////////////
////////SHA1///////
//////////////////
void mySHA1(FILE *file,double sha1_buffer[],int index)
{
clock_t start, end;
double tim;
unsigned char* plaintext=fileToBuffer(file);
size_t ptlength = strlen(plaintext)+1;
#define algo GCRY_MAC_HMAC_SHA1 // Pick the cipher here
gcry_mac_hd_t handle;
gcry_error_t err;
int i;
size_t keylength = 64;
unsigned int kl=gcry_mac_get_algo_keylen (algo);
unsigned char * key = malloc(kl); // 16 bytes
gcry_randomize (key, keylength, GCRY_VERY_STRONG_RANDOM);
//printf("\nSHA1 KEY GENERATED!!\n");
start=clock();
err=gcry_mac_open (&handle, algo, 0, NULL);
if (err) {
fprintf(stderr, "mac_open during encryption: %s/%s\n", gcry_strsource(err), gcry_strerror(err));
}
err=gcry_mac_setkey(handle, key, kl);
if (err) {
fprintf(stderr, "mac_setkey during encryption: %s/%s\n", gcry_strsource(err), gcry_strerror(err));
}
err=gcry_mac_write (handle, plaintext, ptlength);
if (err) {
fprintf(stderr, "mac_write during decryption: %s/%s\n", gcry_strsource(err), gcry_strerror(err));
}
unsigned int length=gcry_mac_get_algo_maclen (algo);
size_t hmaclength=length;
unsigned char* hmac=malloc(hmaclength);
gcry_mac_read (handle, hmac, &hmaclength);
end=clock();
tim=(double)(end-start)*1000/CLOCKS_PER_SEC;
sha1_buffer[index]=tim;
//printf("%s",hmac);
/*
printf("SHA1 IS : ");
for( i = 0; i < hmaclength ; i++) { printf("%02x",hmac[i]); }
printf("\n");
*/
gcry_mac_close (handle);
}
////////////////////
///SHA 256/////////
//////////////////
unsigned char* mySHA256(FILE *file,double sha256_buffer[],int index)
{
clock_t start, end;
double tim;
char* plaintext=fileToBuffer(file);
#define algo GCRY_MAC_HMAC_SHA256
gcry_mac_hd_t handle;
gcry_error_t err;
size_t keylength= gcry_mac_get_algo_keylen(algo);
//printf("%u",keylength); // 64
unsigned char* buffer=plaintext;
//"123456789 abcdefghijklmnopqrstuvwzyz ABCDEFGHIJKLMNOPQRSTUVWZYZ123456789123456789 abcdefghijklmnopqrstuvwzyz ABCDEFGHIJKLMNOPQRSTUVWZYZ123456789";
size_t bufferlength = strlen(buffer)+1;
unsigned char * key = malloc(keylength); // 16 bytes
gcry_randomize (key, keylength, GCRY_VERY_STRONG_RANDOM);
//printf("\nSHA256 KEY GENERATED!!\n");
start=clock();
err=gcry_mac_open (&handle,algo, 0, NULL);
err=gcry_mac_setkey (handle, key, keylength);
err=gcry_mac_write (handle, buffer,bufferlength);
unsigned int length=gcry_mac_get_algo_maclen (algo); //64
size_t hmaclength=length;
unsigned char* hmac=malloc(hmaclength);
gcry_mac_read (handle, hmac, &hmaclength);
end=clock();
tim=(double)(end-start)*1000/CLOCKS_PER_SEC;
sha256_buffer[index]=tim;
int i;
/*
printf("SHA256 IS : ");
for( i = 0; i < hmaclength ; i++) { printf("%02x",hmac[i]); }
printf("\n");
*/
gcry_mac_close (handle);
return hmac;
}
///////////////////
//////AES 128///////
//////////////////
void myAES128(FILE *file,double aes128_encbuffer[],double aes128_decbuffer[],int index)
{
clock_t start_enc, end_enc;
clock_t start_dec, end_dec;
double time_enc,time_dec;
char* plaintext=fileToBuffer(file);
#define GCRY_CIPHER GCRY_CIPHER_AES128 // Pick the cipher here
#define GCRY_MODE GCRY_CIPHER_MODE_CTR // Pick the cipher mode here
gcry_cipher_hd_t handle;
size_t keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER);
size_t blocklength=gcry_cipher_get_algo_blklen(GCRY_CIPHER);
size_t ctrLength = blocklength; //16
unsigned char* ctr=malloc(blocklength);
unsigned char * key = malloc(blocklength); // 16 bytes
gcry_randomize (ctr, blocklength, GCRY_STRONG_RANDOM);
gcry_randomize (key, blocklength, GCRY_VERY_STRONG_RANDOM);
//printf("\nAES 128 KEY GENERATED!!\n");
char* txtBuffer = plaintext;
size_t txtLength = strlen(txtBuffer)+1; // string plus termination
char * encBuffer = (char *)malloc(txtLength);
char * outBuffer = (char *)malloc(txtLength);
gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
gcry_cipher_open(&handle, GCRY_CIPHER, GCRY_MODE, 0);
gcry_cipher_setctr(handle, &ctr, 16);
gcry_cipher_setkey(handle, &key, keyLength);
start_enc=clock();
gcry_cipher_encrypt(handle, encBuffer, txtLength, txtBuffer, txtLength);
end_enc=clock();
time_enc=(double)(end_enc - start_enc)*1000/CLOCKS_PER_SEC;
aes128_encbuffer[index]=time_enc;
gcry_cipher_setctr(handle, &ctr, 16);
start_dec=clock();
gcry_cipher_decrypt(handle, outBuffer, txtLength, encBuffer, txtLength);
end_dec=clock();
time_dec=(double)(end_dec - start_dec)*1000/CLOCKS_PER_SEC;
aes128_decbuffer[index]=time_dec;
//printf("outBuffer for AES 128 : %s\n", outBuffer);
gcry_cipher_close(handle);
free(encBuffer);
free(outBuffer);
}