-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptool.c
769 lines (667 loc) · 16.5 KB
/
cryptool.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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Code was provided within the course `Introduction to Information Security Systems` *
* expecting the students to develop the code needed from keygen till main function. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/conf.h>
#include <openssl/cmac.h>
#define BLOCK_SIZE 16
#define CMAC_SIZE 16
#define TRUE 1
#define FALSE 0
/* function prototypes */
void print_hex(unsigned char *, size_t);
void print_string(unsigned char *, size_t);
void usage(void);
void check_args(char *, char *, unsigned char *, int, int);
void keygen(unsigned char *, unsigned char *, unsigned char *, int);
void myencrypt(unsigned char *, int, unsigned char *, unsigned char *,
unsigned char **, int);
int decrypt(unsigned char *, int, unsigned char *, unsigned char *,
unsigned char **, int);
void gen_cmac(unsigned char *, size_t, unsigned char *, unsigned char *, int);
int verify_cmac(unsigned char *, unsigned char *);
unsigned char *readPlaintext(char *);
/* Global variables */
unsigned char *globalkey = NULL;
int ssize;
int ciphertext_len;
int blvr = 0;
/* TODO Declare your function prototypes here... */
void write_file(char *file, unsigned char *text, int length, int mode);
/*
* Prints the hex value of the input
* 16 values per line
*/
void print_hex(unsigned char *data, size_t len)
{
size_t i;
if (!data)
printf("NULL data\n");
else
{
for (i = 0; i < len; i++)
{
if (!(i % 16) && (i != 0))
printf("\n");
printf("%02X ", data[i]);
}
printf("\n");
}
}
/*
* Prints the input as string
*/
void print_string(unsigned char *data, size_t len)
{
size_t i;
if (!data)
printf("NULL data\n");
else
{
for (i = 0; i < len; i++)
printf("%c", data[i]);
printf("\n");
}
}
/*
* Prints the usage message
* Describe the usage of the new arguments you introduce
*/
void usage(void)
{
printf(
"\n"
"Usage:\n"
" assign_1 -i in_file -o out_file -p passwd -b bits"
" [-d | -e | -s | -v]\n"
" assign_1 -h\n");
printf(
"\n"
"Options:\n"
" -i path Path to input file\n"
" -o path Path to output file\n"
" -p psswd Password for key generation\n"
" -b bits Bit mode (128 or 256 only)\n"
" -d Decrypt input and store results to output\n"
" -e Encrypt input and store results to output\n"
" -s Encrypt+sign input and store results to output\n"
" -v Decrypt+verify input and store results to output\n"
" -h This help message\n"
"*** Optionally you add '1' (without '') as last argument to enable debug mode ***\n");
exit(EXIT_FAILURE);
}
/*
* Checks the validity of the arguments
* Check the new arguments you introduce
*/
void check_args(char *input_file, char *output_file, unsigned char *password,
int bit_mode, int op_mode)
{
if (!input_file)
{
printf("Error: No input file!\n");
usage();
}
if (!output_file)
{
printf("Error: No output file!\n");
usage();
}
if (!password)
{
printf("Error: No user key!\n");
usage();
}
if ((bit_mode != 128) && (bit_mode != 256))
{
printf("Error: Bit Mode <%d> is invalid!\n", bit_mode);
usage();
}
if (op_mode == -1)
{
printf("Error: No mode\n");
usage();
}
}
/*
* Generates a key using a password
*/
void keygen(unsigned char *password, unsigned char *key, unsigned char *iv,
int bit_mode)
{
/* TODO Task A */
const unsigned char *salt = NULL;
const EVP_CIPHER *cipher;
const EVP_MD *hash_function = NULL;
/* select a cipher mode and key size */
if (bit_mode == 128)
cipher = EVP_get_cipherbyname("aes-128-ecb");
else if (bit_mode == 256)
cipher = EVP_get_cipherbyname("aes-256-ecb");
else
{
fprintf(stderr, "Error in cipher param.\n");
exit(1);
}
/* select a hash function before calling the keygen */
globalkey = malloc(bit_mode / 8);
hash_function = EVP_get_digestbyname("sha1");
/* convert the password bytes to key */
if (EVP_BytesToKey(cipher, hash_function, salt, (unsigned char *)password, strlen((char *)password), 1, globalkey, iv) == 0)
{
fprintf(stderr, "Error in EVP_BytesToKey in keygen.\n");
exit(1);
}
if (blvr)
print_hex(globalkey, bit_mode / 8);
}
/*
* Encrypts the data
*/
void myencrypt(
unsigned char *plaintext,
int plaintext_len,
unsigned char *key,
unsigned char *iv,
unsigned char **ciphertext,
int bit_mode)
{
/* TODO Task B */
EVP_CIPHER_CTX context;
const EVP_CIPHER *cipher;
unsigned char *cipher_;
int cipher_len;
int cipher_len_final_addition;
if (blvr)
{
printf("__START OF ENCRYPT\n");
print_hex(plaintext, plaintext_len);
}
if (bit_mode == 128)
cipher = EVP_get_cipherbyname("aes-128-ecb");
else if (bit_mode == 256)
cipher = EVP_get_cipherbyname("aes-256-ecb");
else
{
fprintf(stderr, "Error in cipher param.\n");
exit(1);
}
if (blvr)
printf("before init\n");
/* create a new encryption context */
EVP_CIPHER_CTX_init(&context);
if (blvr)
printf("before init\n");
/* initialize with appropriate mode and key size */
if (EVP_EncryptInit_ex(&context, cipher, NULL, key, iv) == 0)
{
fprintf(stderr, "Error in EVP_EncryptInit_ex in encrypt.\n");
exit(1);
}
/* update the ciphertext */
cipher_ = malloc(plaintext_len + BLOCK_SIZE);
if (blvr)
{
printf("before update\n");
printf("%d\n", plaintext_len);
}
if (EVP_EncryptUpdate(&context, cipher_, &cipher_len, plaintext, plaintext_len) == 0)
{
fprintf(stderr, "Error in EVP_EncryptUpdate, first cipher, in encrypt.\n");
exit(1);
}
if (blvr)
printf("before finalize\n");
/* finalize the encryption */
if (EVP_EncryptFinal_ex(&context, &cipher_[cipher_len], &cipher_len_final_addition) == 0)
{
fprintf(stderr, "Error in EVP_EncryptFinal_ex, second cipher, in encrypt.\n");
exit(1);
}
if (blvr)
printf("before ciphertext_malloc\n");
ciphertext_len = cipher_len + cipher_len_final_addition;
*ciphertext = malloc(ciphertext_len);
memcpy(*ciphertext, cipher_, ciphertext_len);
if (blvr)
{
printf("ciph_len: %d\n", ciphertext_len);
print_hex(*ciphertext, ciphertext_len);
printf("____END OF ENCRYPT \n");
}
/* free the context */
EVP_CIPHER_CTX_cleanup(&context);
}
/*
* Decrypts the data and returns the plaintext size
*/
int decrypt(
unsigned char *ciphertext,
int ciphertext_len,
unsigned char *key,
unsigned char *iv,
unsigned char **plaintext,
int bit_mode)
{
int plaintext_len;
unsigned char *plain_;
int plain_len;
int plain_len_final_addition = 16;
plaintext_len = 0;
/*TODO Task C */
EVP_CIPHER_CTX context;
const EVP_CIPHER *cipher;
if (blvr)
{
printf("__START OF DECRYPT\n");
print_hex(ciphertext, ciphertext_len);
}
if (bit_mode == 128)
cipher = EVP_get_cipherbyname("aes-128-ecb");
else if (bit_mode == 256)
cipher = EVP_get_cipherbyname("aes-256-ecb");
else
{
fprintf(stderr, "Error in cipher param.\n");
exit(1);
}
if (blvr)
printf("before dec init\n");
/* initialize with appropriate mode and key size */
EVP_CIPHER_CTX_init(&context);
if (EVP_DecryptInit_ex(&context, cipher, NULL, key, iv) == 0)
{
fprintf(stderr, "Error in EVP_DecryptInit_ex in dencrypt.\n");
exit(1);
}
if (blvr)
printf("before mallonc\n");
plain_ = malloc(ciphertext_len + BLOCK_SIZE);
if (blvr)
printf("before dec update\n");
/* update the plaintext */
if (EVP_DecryptUpdate(&context, plain_, &plain_len, ciphertext, ciphertext_len) == 0)
{
fprintf(stderr, "Error in EVP_DecryptUpdate, first plaintext, in encrypt.\n");
exit(1);
}
if (blvr)
{
printf("before dec finalize \n");
printf("%d \n", plain_len);
print_string(plain_, plain_len);
}
/* finalize the dencryption */
if (EVP_DecryptFinal_ex(&context, &plain_[plain_len], &plain_len_final_addition) == 0)
{
fprintf(stderr, "Error in EVP_DecryptFinal_ex, second plaintext, in encrypt.\n");
exit(1);
}
plaintext_len = plain_len + plain_len_final_addition;
*plaintext = malloc(plaintext_len);
memcpy(*plaintext, plain_, plaintext_len);
if (blvr)
print_string(*plaintext, plaintext_len);
/* free the context */
EVP_CIPHER_CTX_cleanup(&context);
if (blvr)
printf("__END OF DECRYPT\n");
return plaintext_len;
}
/*
* Generates a CMAC
*/
void gen_cmac(
unsigned char *data,
size_t data_len,
unsigned char *key,
unsigned char *cmac,
int bit_mode)
{
/* TODO Task D */
const EVP_CIPHER *cipher;
size_t cmac_new_size;
int keysize;
if (blvr)
{
printf("__START OF GEN_CMAC\n");
print_string(data, data_len);
}
/* Create a new CMAC context */
CMAC_CTX *cmac_new_context = CMAC_CTX_new();
/* Initialize (mode/key size)*/
if (bit_mode == 128)
{
cipher = EVP_get_cipherbyname("aes-128-ecb");
keysize = 16;
}
else if (bit_mode == 256)
{
cipher = EVP_get_cipherbyname("aes-256-ecb");
keysize = 32;
}
else
{
fprintf(stderr, "Error in cipher param.\n");
exit(1);
}
if (CMAC_Init(cmac_new_context, key, keysize, cipher, NULL) == 0)
{
fprintf(stderr, "Error in CMAC_Init.\n");
exit(1);
}
/* Update CMAC */
if (CMAC_Update(cmac_new_context, data, data_len) == 0)
{
fprintf(stderr, "Error in CMAC_Update.\n");
exit(1);
}
/* Finalize CMAC */
if (CMAC_Final(cmac_new_context, cmac, &cmac_new_size) == 0)
{
fprintf(stderr, "Error in CMAC_Final.\n");
exit(1);
}
/* Free the context */
CMAC_CTX_free(cmac_new_context);
keysize = -1;
cipher = NULL;
if (blvr)
printf("__END OF GEN_CMAC\n");
}
/*
* Verifies a CMAC
*/
int verify_cmac(unsigned char *first, unsigned char *second)
{
/* TODO Task E */
/* The CMAC appended at the end of the message and has a fixed size */
/* Decrypt the message and save the CMAC */
/* Recalculate the CMAC as when generating it */
/* Compare the two CMACs*/
int i;
for (i = 0; i < CMAC_SIZE; i++)
{
if (first[i] != second[i])
return FALSE;
}
return TRUE;
}
/* TODO Develop your functions here... */
int verify_wrapper(
int message_len,
unsigned char *message,
int bit_mode,
char *outfile)
{
int plaintext_len;
unsigned char *cmac;
unsigned char *cmac_2;
unsigned char *ciphertext;
unsigned char *plaintext;
if (blvr)
{
printf("__START OF VERIFY_MAC\n");
printf("%d\n", message_len);
}
/* The CMAC is appended at the end of the message and has a fixed size */
cmac = malloc(CMAC_SIZE);
ciphertext = malloc(message_len - CMAC_SIZE);
plaintext = malloc(message_len - CMAC_SIZE);
memcpy(cmac, &message[message_len - CMAC_SIZE], CMAC_SIZE);
if (blvr)
print_hex(cmac, CMAC_SIZE);
memcpy(ciphertext, message, message_len - CMAC_SIZE);
/* Decrypt the message and save the CMAC */
plaintext_len = decrypt(ciphertext, message_len - CMAC_SIZE, globalkey, NULL, &plaintext, bit_mode);
/* Recalculate the CMAC as when generating it */
cmac_2 = malloc(CMAC_SIZE);
gen_cmac(plaintext, strlen((const char *)plaintext), globalkey, cmac_2, bit_mode);
if (blvr)
{
print_hex(cmac_2, CMAC_SIZE);
print_hex(cmac, CMAC_SIZE);
}
if (blvr)
{
printf("END OF VERIFY_MAC\n");
}
// return verify_cmac(cmac, cmac_2);
if (verify_cmac(cmac, cmac_2))
{
write_file(outfile, plaintext, plaintext_len, 0);
return TRUE;
}
return FALSE;
}
unsigned char *
readPlaintext(char *filename)
{
int chr;
unsigned int i;
unsigned char *string;
FILE *file;
file = fopen(filename, "r");
i = 0;
string = malloc(ssize * (sizeof(char)));
chr = fgetc(file);
while ((chr != EOF))
{
if (i >= ssize)
{
ssize += ssize;
string = realloc(string, ssize);
}
string[i++] = chr;
chr = fgetc(file);
}
if (i == 0)
return NULL;
string[i] = '\0';
fclose(file);
return string;
}
int readByteText(char *filename, unsigned char **buffer)
{
FILE *fp;
int filelen;
fp = fopen(filename, "rb");
if (fp == NULL)
{
fprintf(stderr, "Error in write_file.\n");
exit(-1);
}
fseek(fp, 0, SEEK_END);
filelen = ftell(fp);
rewind(fp);
*buffer = malloc(filelen + 1);
fread(*buffer, filelen, 1, fp);
fclose(fp);
return filelen;
}
void write_file(char *file, unsigned char *text, int length, int mode)
{
if (blvr)
printf("__START WRITE FILE\n");
FILE *fp;
if (mode)
{
if (blvr)
printf("writing bytes...\n");
fp = fopen(file, "wb");
}
else
{
if (blvr)
printf("writing text...\n");
fp = fopen(file, "w");
}
if (fp == NULL)
{
fprintf(stderr, "Error in write_file.\n");
exit(-1);
}
// fwrite(address_data,size_data,numbers_data,pointer_to_file);
fwrite(text, sizeof(unsigned char), length, fp);
fclose(fp);
if (blvr)
printf("\n__END OF WRITE FILE\n");
}
/*
* Encrypts the input file and stores the ciphertext to the output file
*
* Decrypts the input file and stores the plaintext to the output file
*
* Encrypts and signs the input file and stores the ciphertext concatenated with
* the CMAC to the output file
*
* Decrypts and verifies the input file and stores the plaintext to the output
* file
*/
int main(int argc, char **argv)
{
int opt; /* used for command line arguments */
int bit_mode; /* defines the key-size 128 or 256 */
int op_mode; /* operation mode */
char *input_file; /* path to the ivenput file */
char *output_file; /* path to the output file */
unsigned char *password; /* the user defined password */
/* Init arguments */
input_file = NULL;
output_file = NULL;
password = NULL;
bit_mode = -1;
op_mode = -1;
ssize = 512;
/* My Arguments */
int plaintext_len;
int size_return;
unsigned char *readplaintext = NULL;
unsigned char *ciphertext = NULL;
unsigned char *plaintext = NULL;
unsigned char *cmac = NULL;
unsigned char *concatenated = NULL;
/*
* Get arguments
*/
while ((opt = getopt(argc, argv, "b:i:m:o:p:desvh:")) != -1)
{
switch (opt)
{
case 'b':
bit_mode = atoi(optarg);
break;
case 'i':
input_file = strdup(optarg);
break;
case 'o':
output_file = strdup(optarg);
break;
case 'p':
password = (unsigned char *)strdup(optarg);
break;
case 'd':
/* if op_mode == 1 the tool decrypts */
op_mode = 1;
break;
case 'e':
/* if op_mode == 1 the tool encrypts */
op_mode = 0;
break;
case 's':
/* if op_mode == 1 the tool signs */
op_mode = 2;
break;
case 'v':
/* if op_mode == 1 the tool verifies */
op_mode = 3;
break;
case 'h':
default:
usage();
}
}
/* check arguments */
check_args(input_file, output_file, password, bit_mode, op_mode);
/* TODO Develop the logic of your tool here... */
/* Custom Debug Mode */
if (argc == 11 && atoi(argv[argc - 1]) == 1)
{
printf("debuging...\n");
blvr = 1;
}
/* Initialize the library */
OpenSSL_add_all_algorithms();
/* Keygen from password */
keygen(password, NULL, NULL, bit_mode);
/* Operate on the data according to the mode */
switch (op_mode)
{
/* encrypt */
case 0:
if (blvr)
printf("op_mode 0, encrypt\n");
readplaintext = readPlaintext(input_file);
myencrypt(readplaintext, strlen((const char *)readplaintext), globalkey, NULL, &ciphertext, bit_mode);
if (blvr)
print_hex(ciphertext, ciphertext_len);
write_file(output_file, ciphertext, ciphertext_len, 1);
if (blvr)
{
printf("Decrypting instantly the encrypted file...\n");
plaintext_len = decrypt(ciphertext, ciphertext_len, globalkey, NULL, &plaintext, bit_mode);
}
break;
/* decrypt */
case 1:
if (blvr)
printf("op_mode 1, decrypt\n");
size_return = readByteText(input_file, &ciphertext);
plaintext_len = decrypt(ciphertext, size_return, globalkey, NULL, &plaintext, bit_mode);
/* write mode 0: plaintext */
write_file(output_file, plaintext, plaintext_len, 0);
break;
/* sign */
case 2:
if (blvr)
printf("op_mode 2, sign\n");
readplaintext = readPlaintext(input_file);
cmac = malloc(CMAC_SIZE);
gen_cmac(readplaintext, strlen((const char *)readplaintext), globalkey, cmac, bit_mode);
myencrypt(readplaintext, strlen((const char *)readplaintext), globalkey, NULL, &ciphertext, bit_mode);
concatenated = malloc(ciphertext_len + CMAC_SIZE);
/* copy ciphertext */
memcpy(concatenated, ciphertext, ciphertext_len);
/* copy cmac after ciphertext */
memcpy((concatenated + ciphertext_len), cmac, CMAC_SIZE);
/* write mode 1: byte */
write_file(output_file, concatenated, ciphertext_len + CMAC_SIZE, 1);
break;
/* verify */
case 3:
if (blvr)
printf("op_mode 3, verify\n");
size_return = readByteText(input_file, &ciphertext);
if (verify_wrapper(size_return, ciphertext, bit_mode, output_file) == 1)
{
printf("Verified\n");
}
else
printf("Verification Failed\n");
break;
default:
printf("not 0-3 op_mode");
}
/* Clean up */
free(input_file);
free(output_file);
free(password);
/* END */
return 0;
}