-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.c
216 lines (174 loc) · 5.42 KB
/
crypto.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
#include <stdio.h>
#include <memory.h>
#include <arpa/inet.h>
#include "crypto.h"
/* An Encryption fn to produce 64 bit cipher text from 64 bit
plain txt using feistel cipher Algorithm */
void
feistel_encrypt (feistel_cipher_t *f_cipher, uint64_t *plain_txt) {
uint8_t i;
uint32_t *LE_OLD, *RE_OLD;
uint32_t *LE_NEW, *RE_NEW;
uint32_t key, temp;
uint32_t rnd_fn_output;
LE_OLD = (uint32_t *)plain_txt;
RE_OLD = (uint32_t *)plain_txt + 1;
LE_NEW = LE_OLD;
RE_NEW = RE_OLD;
printf ("\n");
printf ("Input :: ");
print_hex(*LE_OLD);
printf (" ");
print_hex(*RE_OLD);
printf ("\n");
for (i = 0; i < FEISTEL_ROUNDS; i++) {
key = f_cipher->round[i].key;
printf ("Round No : %d, key = %x\n", i, key);
rnd_fn_output = f_cipher->round_fn_ptr(RE_OLD, key);
printf ("rnd_fn_output :: ");
print_hex(rnd_fn_output);
printf ("\n");
temp = *RE_OLD;
*RE_NEW = rnd_fn_output ^ (*LE_OLD);
printf ("*RE_NEW = rnd_fn_output ^ (*LE_OLD) = ");
print_hex(rnd_fn_output);
printf (" ^ ");
print_hex(*LE_OLD);
printf (" = ");
print_hex(*RE_NEW);
printf ("\n");
*LE_NEW = temp;
printf ("*LE_NEW = RE_OLD = ");
print_hex(*LE_NEW);
printf ("\n");
/* Save the output for record */
f_cipher->round[i].LE = *LE_NEW;
f_cipher->round[i].RE = *RE_NEW;
}
/* now SWAP LE_NEW and RE_NEW */
temp = *LE_NEW;
*LE_NEW = *RE_NEW;
*RE_NEW = temp;
f_cipher->FE_OUT[0] = *LE_NEW;
f_cipher->FE_OUT[1] = *RE_NEW;
printf ("Final Cipher = ");
print_hex(f_cipher->FE_OUT[0]);
printf (" ");
print_hex(f_cipher->FE_OUT[1]);
printf ("\n");
}
/* A Decryption fn to produce 64 bit plain text from 64 bit
cipher text */
void
feistel_decrypt (feistel_cipher_t *f_cipher, uint64_t *cipher_txt) {
int8_t i;
uint32_t *LD_OLD, *RD_OLD;
uint32_t *LD_NEW, *RD_NEW;
uint32_t key;
uint32_t rnd_fn_output;
uint32_t temp;
LD_OLD = (uint32_t *)cipher_txt;
RD_OLD = (uint32_t *)cipher_txt + 1;
LD_NEW = LD_OLD;
RD_NEW = RD_OLD;
printf ("\n");
printf ("Input :: ");
print_hex(*LD_OLD);
printf (" ");
print_hex(*RD_OLD);
printf ("\n");
for (i = FEISTEL_ROUNDS -1; i >= 0; i--) {
key = f_cipher->round[i].key;
printf ("Round No : %d, key = %x\n", i, key);
rnd_fn_output = f_cipher->round_fn_ptr(RD_OLD, key);
printf ("rnd_fn_output :: ");
print_hex(rnd_fn_output);
printf ("\n");
temp = *RD_OLD;
*RD_NEW = rnd_fn_output ^ (*LD_OLD);
printf ("*RD_NEW = rnd_fn_output ^ (*LD_OLD) = ");
print_hex(rnd_fn_output);
printf (" ^ ");
print_hex(*LD_OLD);
printf (" = ");
print_hex(*RD_NEW);
printf ("\n");
*LD_NEW = temp;
printf ("LD_NEW = RD_OLD = ");
print_hex(*LD_NEW);
printf ("\n");
/* Save the output for record */
f_cipher->round[i].LD = *LD_NEW;
f_cipher->round[i].RD = *RD_NEW;
}
/* now SWAP LD_NEW and RD_NEW */
temp = *LD_NEW;
*LD_NEW = *RD_NEW;
*RD_NEW = temp;
f_cipher->FD_OUT[0] = *LD_NEW;
f_cipher->FD_OUT[1] = *RD_NEW;
printf ("Final De Cipher = ");
print_hex(f_cipher->FD_OUT[0]);
printf (" ");
print_hex(f_cipher->FD_OUT[1]);
printf ("\n");
}
void
feistel_cipher_instance_init (feistel_cipher_t *f_cipher,
round_fn round_fn_ptr,
uint32_t keys[FEISTEL_ROUNDS]) {
uint8_t i = 0;
memset (f_cipher, 0 , sizeof (feistel_cipher_t));
f_cipher->round_fn_ptr = round_fn_ptr;
for (i = 0; i < FEISTEL_ROUNDS; i++) {
f_cipher->round[i].key = keys[i];
f_cipher->round[i].LE = 0;
f_cipher->round[i].RE = 0;
f_cipher->round[i].LD = 0;
f_cipher->round[i].RD = 0;
}
f_cipher->FE_OUT[0] = 0;
f_cipher->FE_OUT[1] = 0;
f_cipher->FD_OUT[0] = 0;
f_cipher->FD_OUT[1] = 0;
}
void
feistel_print (feistel_cipher_t *f_cipher) {
uint8_t i = 0;
for (i = 0; i < FEISTEL_ROUNDS - 1; i++) {
printf ("Rnd %d. LE : %-8x RE : %-8x LD : %-8x RD : %-8x\n",
i, f_cipher->round[i].LE, f_cipher->round[i].RE,
f_cipher->round[ i + 1].LD,
f_cipher->round[i + 1].RD);
}
}
/* Driver program to test Feistel Cipher Implemtation */
uint32_t
feistel_cipher_round_test_fn (uint32_t *input, uint32_t key) {
printf ("%s() :: ", __FUNCTION__);
print_hex(*input);
printf (" xor ");
print_hex(key);
printf("\n");
return (*input) ^ key;
}
/* Keys for each round, taken randomly*/
static uint32_t keys[] = {0xccd5, 0x1e240, 0x9fbf1};
static void
feistel_cipher_driver_program () {
feistel_cipher_t f_cipher;
feistel_cipher_instance_init (&f_cipher, feistel_cipher_round_test_fn, keys);
uint64_t input = 9686081839;
printf ("Original Input plain text = %lu\n", input);
feistel_encrypt (&f_cipher, &input);
printf ("Cipher = %lu %lu\n", input, *(uint64_t *)f_cipher.FE_OUT);
feistel_print (&f_cipher);
feistel_decrypt (&f_cipher, &input);
printf ("Recovered plain txt = %lu %lu\n", input, *(uint64_t *)f_cipher.FD_OUT);
feistel_print (&f_cipher);
}
int
main(int argc, char **argv) {
feistel_cipher_driver_program();
return 0;
}