-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.c
More file actions
655 lines (565 loc) · 15.2 KB
/
code.c
File metadata and controls
655 lines (565 loc) · 15.2 KB
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
#include <stdio.h>
#include <stdlib.h>
//#define DEBUG
//#define DECRYPT_STEP
//#define ENCRYPT_BMP
#include "bitwiseop.h"
typedef enum
{
encrypt = 1,
decrypt = 0
} Mode;
char *initial_permutation,
*final_permutation,
*expansion,
*permutation_32,
*compress_56,
*shift_left,
*compress_48,
**sbox_table;
Mode mode;
uint64_t permutation(uint64_t value, char *positions, int sizef, int sizei);
uint64_t swapLR(uint64_t value, int axis);
uint64_t shiftL_circular_split(uint64_t value, int axis, int shift);
uint64_t shiftR_circular_split(uint64_t value, int axis, int shift);
uint64_t s_box(uint64_t value, char **table);
uint64_t key_i(uint64_t value, char i);
uint64_t iteration(uint64_t value, uint64_t key_48);
uint64_t block_encrypt(uint64_t value, uint64_t key);
void import_values();
void print_tables();
void sort_sbox_table();
uint64_t key_prepare(char * key);
void test_block();
int message_prepare(uint64_t * blocks, char * message);
void test_string();
void open_files(int argc,char ** argv,FILE ** plain, FILE ** cypher);
int fread_person(uint64_t *value,FILE * file);
void fwrite_person(uint64_t value,FILE * file);
uint64_t permutation(uint64_t value, char *positions, int sizef, int sizei)
{
uint64_t ret = 0;
char i;
for (i = 0; i < sizef; i++)
{
// printf("%c %2d",(i%8)?'\0':'\n',positions[i]);
bit_recive(ret, sizef-i-1, bit_value(value,sizei-positions[i]));
}
#ifdef DEBUG
printf("%016llx %016llx\n",value,ret);
print_8bytes(ret);
printf("\n");
#endif
return ret;
}
uint64_t swapLR(uint64_t value, int axis)
{
return (value >> axis) & bit_range(0, axis) // L to R
| (value << axis) & bit_range(axis, 2 * axis); // R to L
}
uint64_t shiftL_circular_split(uint64_t value, int axis, int shift)
{
uint64_t temp = (value >> axis - shift) & (bit_range(0, shift) | bit_range(axis, axis + shift));
//Salva os que estourariam já na posição nova, com todos os outros valores zerados
uint64_t ret = (value << shift) // Faz o deslocamento normal (com os zeros no inicio)
& (bit_range(shift, axis) | bit_range(axis + shift, axis * 2))
// zera todos os valores que entraram nas posições dos estourados
| temp; // inclui os estourados em suas específicas posições
#ifdef DEBUG
printf("%016llx %016llx\n",value,ret);
print_8bytes(ret);
printf("\n");
#endif
return ret;
}
uint64_t shiftR_circular_split(uint64_t value, int axis, int shift)
{
uint64_t temp = (value << axis - shift) & (bit_range(axis - shift, axis) | bit_range(2 * axis - shift, 2 * axis));
//Salva os que estourariam já na posição nova, com todos os outros valores zerados
uint64_t ret = (value >> shift) // Faz o deslocamento normal (com os zeros no final)
& (bit_range(0, axis - shift) | bit_range(axis, 2 * axis - shift))
// zera todos os valores que entraram nas posições dos estourados
| temp; // inclui os estourados em suas específicas posições
#ifdef DEBUG
printf("%016llx %016llx\n",value,ret);
print_8bytes(ret);
printf("\n");
#endif
return ret;
}
uint64_t s_box(uint64_t value, char **table)
{
char i;
uint64_t ret = 0;
int index;
for (i = 0; i < 8; i++)
{
index = (value >> (i * 6)) & bit_range(0, 6);
ret = ret | (((uint64_t )(table[7-i][index])) << i * 4);
}
#ifdef DEBUG
printf("sbox %c%c%c%c%c%c %c%c%c%c%c%c %c%c%c%c%c%c %c%c%c%c%c%c %c%c%c%c%c%c %c%c%c%c%c%c %c%c%c%c%c%c %c%c%c%c%c%c\n",TO_BIN(value>>40),TO_BIN(value>>32),TO_BIN(value>>24),TO_BIN(value>>16),TO_BIN(value>>8),TO_BIN(value));
printf(" %c%c%c%c %c%c%c%c %c%c%c%c %c%c%c%c %c%c%c%c %c%c%c%c %c%c%c%c %c%c%c%c \n",TO_BIN(ret>>24),TO_BIN(ret>>16),TO_BIN(ret>>8),TO_BIN(ret));
printf("\n");
#endif
return ret;
}
void sort_sbox_table()
{
char **table2 = (char **)malloc(sizeof(char *) * 8);
if(!table2)
{
printf("ERROR! Missing memory for 'table2'");
exit(1);
}
char i, j, temp;
for (i = 0; i < 8; i++)
{
table2[i] = (char *)malloc(sizeof(char) * 64);
}
for (i = 0; i < 8; i++)
{
for (j = 0; j < 16; j++)
{
table2[i][2 * j] = sbox_table[i][j];
table2[i][2 * j + 1] = sbox_table[i][j + 16];
}
for (j = 0; j < 16; j++)
{
table2[i][32 + 2 * j] = sbox_table[i][32+j];
table2[i][32 + 2 * j + 1] = sbox_table[i][32 + j + 16];
}
}
free(sbox_table);
sbox_table = table2;
}
uint64_t iteration(uint64_t value, uint64_t key_48)
{
uint64_t ret = 0;
//F (R,Ki)
#ifdef DEBUG
printf("expansion 48 ");
#endif
uint64_t f = permutation(value, expansion, 48,32);
#ifdef DEBUG
printf("xor %016llx + %016llx = %016llx\n\n",f,key_48,f ^ key_48);
#endif
f = f ^ key_48;
f = s_box(f, sbox_table);
#ifdef DEBUG
printf("permutation 32 ");
#endif
f = permutation(f, permutation_32, 32,32);
//swap
ret = swapLR(value, 32);
#ifdef DEBUG
printf("swap %016llx %016llx\n\n",value,ret);
#endif
//xor
#ifdef DEBUG
printf("xor %016llx + %016llx = %016llx\n\n",ret,f,ret ^ f);
#endif
return ret ^ f;
}
uint64_t key_i(uint64_t value, char i)
{
switch (mode)
{
case encrypt:
#ifdef DEBUG
printf("shift split L %d %d ",i,shift_left[i]);
#endif
return shiftL_circular_split(value, 28, shift_left[i]);
case decrypt:
#ifdef DEBUG
printf("shift split R %d %d ",i,shift_left[i]);
#endif
return shiftR_circular_split(value, 28, shift_left[i]);
}
}
uint64_t key_prepare(char * key)
{
uint64_t value = 0;
char i=0;
while(key[i]){
value = value<<8 | key[i++];
}
return value;
}
int message_prepare(uint64_t * blocks, char * message){
int i=0,j=-1;
while (message[i]){
if(!(i%8))
blocks[++j]=0;
blocks[j] = blocks[j]<<8 | message[i];
i++;
}
while (i%8){
blocks[j] = blocks[j]<<8;
i++;
}
blocks[j+1] = 0;
return j+1;
}
uint64_t block_encrypt(uint64_t value, uint64_t key)
{
char i;
uint64_t ret;
#ifdef DEBUG
printf("initial permutation ");
#endif
value = permutation(value, initial_permutation, 64,64);
for (i = 0; i < 16; i++)
{
if(mode == encrypt)
key = key_i(key, i);
#ifdef DEBUG
printf("compress key 48 ");
#endif
value = iteration(value, permutation(key, compress_48, 48,56));
if(mode == decrypt)
key = key_i(key, 15 - i);
}
ret = swapLR(value, 32);
#ifdef DEBUG
printf("swap %016llx %016llx\n\n",value,ret);
#endif
#ifdef DEBUG
printf("final permutation ");
#endif
return permutation(ret, final_permutation, 64,64);
}
void import_values()
{
char j;
FILE *tables;
char i = 0;
//alloc spaces
initial_permutation = (char *)malloc(sizeof(char) * 64);
if (!initial_permutation)
{
printf("ERROR! Over memory to 'initial_permutation'");
exit(1);
}
final_permutation = (char *)malloc(sizeof(char) * 64);
if (!final_permutation)
{
printf("ERROR! Over memory to 'final_permutation'");
exit(1);
}
expansion = (char *)malloc(sizeof(char) * 48);
if (!expansion)
{
printf("ERROR! Over memory to 'expansion'");
exit(1);
}
permutation_32 = (char *)malloc(sizeof(char) * 32);
if (!permutation_32)
{
printf("ERROR! Over memory to 'permutation_32'");
exit(1);
}
compress_56 = (char *)malloc(sizeof(char) * 56);
if (!compress_56)
{
printf("ERROR! Over memory to 'compress_56'");
exit(1);
}
shift_left = (char *)malloc(sizeof(char) * 16);
if (!shift_left)
{
printf("ERROR! Over memory to 'shift_left'");
exit(1);
}
compress_48 = (char *)malloc(sizeof(char) * 48);
if (!compress_48)
{
printf("ERROR! Over memory to 'compress_48'");
exit(1);
}
sbox_table = (char **)malloc(sizeof(char *) * 8);
if (!sbox_table)
{
printf("ERROR! Over memory to 'sblock_table'");
exit(1);
}
for (j = 0; j < 8; j++)
{
sbox_table[j] = (char *)malloc(sizeof(char) * 64);
if (!sbox_table[j])
{
printf("ERROR! Over memory to 'sbox_table[%d]'", j);
exit(1);
}
}
//Open tables
tables = fopen("tables.txt", "r");
if (!tables)
{
printf("ERROR! File 'tables.txt' not found!");
exit(1);
}
//initial_permutation
for (i = 0; i < 64; i++)
{
fscanf(tables, "%d", initial_permutation + i);
}
//final_permutation
for (i = 0; i < 64; i++)
{
fscanf(tables, "%d", final_permutation + i);
}
//expansion
for (i = 0; i < 48; i++)
{
fscanf(tables, "%d", expansion + i);
}
//permutation_32
for (i = 0; i < 32; i++)
{
fscanf(tables, "%d", permutation_32 + i);
}
//compress_56
for (i = 0; i < 56; i++)
{
fscanf(tables, "%d", compress_56 + i);
}
//shift_left
for (i = 0; i < 16; i++)
{
fscanf(tables, "%d", shift_left + i);
}
//compress_48
for (i = 0; i < 48; i++)
{
fscanf(tables, "%d", compress_48 + i);
}
//sbox_table
for (j = 0; j < 8; j++)
{
for (i = 0; i < 64; i++)
{
fscanf(tables, "%d", sbox_table[j] + i);
}
}
//Close Tables
fclose(tables);
}
void open_files(int argc,char ** argv,FILE ** plain, FILE ** cypher){
if(argc<=1){
printf("ERRO! Inform the file's name to be encrypted.");
exit(1);
}
*plain = fopen(argv[1],"rb");
if(!*plain){
printf("ERRO! File not found");
exit(1);
}
if(argc<=2){
*cypher = fopen("cypher.txt","ab+");
}else{
*cypher = fopen(argv[2],"ab+");
}
if(!*cypher){
printf("ERRO! Can't open or create out file!");
exit(1);
}
}
void print_tables(){
int i;
printf("\ninitial_permutation");
for (i = 0; i < 64; i++)
{
if (!(i % 8))
printf("\n");
printf("%2d ", initial_permutation[i]);
}
//final_permutation
printf("\n\nfinal_permutation");
for (i = 0; i < 64; i++)
{
if (!(i % 8))
printf("\n");
printf("%2d ", final_permutation[i]);
}
//expansion
printf("\n\nexpansion");
for (i = 0; i < 48; i++)
{
if (!(i % 8))
printf("\n");
printf("%2d ", expansion[i]);
}
//permutation_32
printf("\n\npermutation_32");
for (i = 0; i < 32; i++)
{
if (!(i % 8))
printf("\n");
printf("%2d ", permutation_32[i]);
}
//compress_56
printf("\n\ncompress_56");
for (i = 0; i < 56; i++)
{
if (!(i % 8))
printf("\n");
printf("%2d ", compress_56[i]);
}
//shift_left
printf("\n\nshift_left");
for (i = 0; i < 16; i++)
{
if (!(i % 8))
printf("\n");
printf("%d ", shift_left[i]);
}
//compress_48
printf("\n\ncompress_48");
for (i = 0; i < 48; i++)
{
if (!(i % 8))
printf("\n");
printf("%2d ", compress_48[i]);
}
//sblock_table
printf("\n\nsblock_table");
char j;
for (j = 0; j < 8; j++)
{
printf("\n\ns%d", j);
for (i = 0; i < 64; i++)
{
if (!(i % 8))
printf("\n");
printf("%2d ", sbox_table[j][i]);
}
}
}
void test_block(){
uint64_t key,value;
import_values();
sort_sbox_table();
sscanf(" 133457799BBCDFF1 0123456789ABCDEF","%llx %llx",&key,&value);//85E813540F0AB405
//sscanf(" 0E329232EA6D0D73 8787878787878787","%llx %llx",&key,&value);//0000000000000000
mode = encrypt;
#ifdef DEBUG
printf("compress_56 ");
#endif
key = permutation(key, compress_56, 56,64);
printf("Key_56: %016llx \t Plain:%016llx\n\n",key,value);
value = block_encrypt(value,key);
printf("encrypted: %016llx \n",value);
#ifdef DECRYPT_STEP
mode = decrypt;
printf("%016llx %016llx\n\n",key,value);
value = block_encrypt(value,key);
printf("decrypted: %016llx \n",value);
#endif
}
void test_string(){
char i;
char string[] = "Your lips are smoother than vaseline";
uint64_t message[80];
uint64_t key;
int message_length;
import_values();
sort_sbox_table();
message_length = message_prepare(message,string);
message[message_length-1] = message[message_length-1] | 0x0D0A0000;
key = 0x0E329232EA6D0D73;
#ifdef DEBUG
printf("compress_56 ");
#endif
key = permutation(key, compress_56, 56,64);
printf("string:\n%s\n",string);
mode = encrypt;
printf("plain text:\n");
for(i=0;i<message_length;i++){
printf("%016llx ",message[i]);
}
printf("\ncypher text:\n");
for(i=0;i<message_length;i++){
message[i] = block_encrypt(message[i],key);
printf("%016llx ",message[i]);
}
#ifdef DECRYPT_STEP
mode = decrypt;
printf("\nplain text:\n");
for(i=0;i<message_length;i++){
message[i] = block_encrypt(message[i],key);
printf("%016llx ",message[i]);
}
#endif
}
int fread_person(uint64_t *value,FILE * file){
int i,j;
char k[8];
*value = 0;
j=fread(&k,1,8,file);
if(j==0)
return 0;
else{
for(i=0;i<j;i++){
*value = *value<<8 | (k[i] & bit_range(0,8));
}
for(;j<8;j++){
*value = *value<<8;
}
return 1;
}
}
void fwrite_person(uint64_t value,FILE * file){
int i;
char j[8];
for(i=0;i<8;i++){
j[i] = (value>>(8*(7-i))) & bit_range(0,8);
}
fwrite(&j,1,8,file);
}
int main(int argc,char ** argv)
{
//test_block();
//test_string();
//*
int i,j,k;
char key_string[9];
char v;
uint64_t key;
uint64_t value;
FILE *fplain_text,
*fcypher_text;
open_files(argc,argv,&fplain_text,&fcypher_text);
import_values();
sort_sbox_table();
printf("Inform key: ");
scanf("%s",key_string);
key = key_prepare(key_string);//12345678 //.0[@>?z$
//sscanf(" 0E329232EA6D0D73 ","%llx",&key);
printf("\nKey - %016llx ;\n",key);
#ifdef DEBUG
printf("compress_56 ");
#endif
key = permutation(key, compress_56, 56,64);
printf("\nKey 56 - %016llx ;\n",key);
printf("Choose the process:\n\t1-encrypt\t0-decrypt\n\tinput number:");
scanf("%d",&i);
mode = i!=0;
#ifdef ENCRYPT_BMP
for(j=0;j<54;j++){
fread(&v,1,1,fplain_text);
fwrite(&v,1,1,fcypher_text);
}
#endif
while(i = fread_person(&value,fplain_text),i==1){
//printf("%016llx %d\n",value,i);
value = block_encrypt(value,key);
//printf("%016llx\n\n",value);
fwrite_person(value,fcypher_text);
}
//printf("\n");
//printf("%016llx %d\n",value,i);
fclose(fplain_text);
fclose(fcypher_text);
/**/
}