-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyscheduler.c
311 lines (258 loc) · 6.6 KB
/
keyscheduler.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
/* PSU CRYPT
Written by Ben Totten
CS585 Cryptography
DES Project
*/
#include "psucrypt.h"
/* Future TODO: use trees instead of for loops and do this in parallel */
int generateSubkeys(unsigned char * key, unsigned char subkeyTable[ROUNDS][COLS])
{
int round; /* Outer row: Rounds */
int subkeyNumber; /* Inner row: Subkeys */
int constant; /* constant that is added onto round number when sending to keyscheduler*/
int keygenInput;
unsigned char keyPrime[KEYLENGTH] = { '0' };
copyKey(keyPrime, key);
if (initializeTable(subkeyTable) != 0)
return 1;
for(round = 0; round < ROUNDS; ++round) {
for (subkeyNumber = 0; subkeyNumber < COLS; ++subkeyNumber) {
/* Determine Constant for G() and F() subkey generation*/
if (subkeyNumber == 0 || subkeyNumber == 4 || subkeyNumber == 8)
constant = 0;
else if (subkeyNumber == 1 || subkeyNumber == 5 || subkeyNumber == 9)
constant = 1;
else if (subkeyNumber == 2 || subkeyNumber == 6 || subkeyNumber == 10)
constant = 2;
else if (subkeyNumber == 3 || subkeyNumber == 7 || subkeyNumber == 11)
constant = 3;
else
return 1;
keygenInput = ((4 * round) + constant);
/* Send to keyscheduler */
subkeyTable[round][subkeyNumber] = K(keyPrime, keygenInput);
if (!subkeyTable[round][subkeyNumber])
return 1;
}
}
return 0;
}
/*
NOTE: This is the version that mod's 10, per the test vector. A version that mods 8 (per the lecture) is commented out below.
The x input gets modded by 8 to give us which byte of the key we will be returning as the subkey.
We then bitshift the key left (with a carry) to give us Key` or "shifted".
The keys are from the right, so we must switch the mod results with its key_byte equivelant.
*/
unsigned char K(unsigned char * key, int x)
{
int byteNumber;
int bytePrime;
byteNumber = x % 10;
switch (byteNumber) {
case 0:
bytePrime = 9;
break;
case 1:
bytePrime = 8;
break;
case 2:
bytePrime = 7;
break;
case 3:
bytePrime = 6;
break;
case 4:
bytePrime = 5;
break;
case 5:
bytePrime = 4;
break;
case 6:
bytePrime = 3;
break;
case 7:
bytePrime = 2;
break;
case 8:
bytePrime = 1;
break;
case 9:
bytePrime = 0;
break;
default:
printf("Input error in Key Scheduler");
return '0';
}
leftRotate(key);
return key[bytePrime];
}
/* Saves the first high byte to eventually move the last bit of to the front of the array.
* Starts at the high bytes and begins shifting over. temp1 and temp2 hold the shifted in
* order to move the last bit to the new element via bit shifting 7 bits over (one shy of
* a byte) and then OR'ing it to ensure the proper bit stays.
*/
void leftRotate(unsigned char * shifted)
{
unsigned char temp1 = 0;
unsigned char temp2 = 0;
unsigned char hold = 0;
int i;
hold = shifted[0] & 0xFF;
for (i = KEYLENGTH - 1; i >= 0; --i) {
temp2 = shifted[i] & 0xFF;
temp1 >>= 7;
shifted[i] <<= 1;
shifted[i] |= temp1;
temp1 = temp2;
}
hold >>= 7;
shifted[KEYLENGTH-1] |= hold;
return;
}
int initializeTable(unsigned char subkeyTable[ROUNDS][COLS])
{
int round; /* Outer row: Rounds */
int subkeyNumber; /* Inner row: Subkeys */
for (round = 0; round < ROUNDS; ++round) {
for (subkeyNumber = 0; subkeyNumber < COLS; ++subkeyNumber) {
subkeyTable[round][subkeyNumber] = (unsigned) '0';
}
}
return 0;
}
void printTable(unsigned char table[ROUNDS][COLS], char flag)
{
int round; /* Outer row: Rounds */
int subkeyNumber; /* Inner row: Subkeys */
if (tolower(flag) == 'c')
{
printf("\n\nPrinting Table as Chars\n\n");
for (round = 0; round < ROUNDS; ++round) {
for (subkeyNumber = 0; subkeyNumber < COLS; ++subkeyNumber) {
printf("%c ", table[round][subkeyNumber]);
}
}
}
if (tolower(flag) == 'h')
{
printf("\n\nPrinting Table as Hex\n\n");
for (round = 0; round < ROUNDS; ++round) {
for (subkeyNumber = 0; subkeyNumber < COLS; ++subkeyNumber) {
printf("(0x%02X) ", table[round][subkeyNumber]);
}
putchar('\n');
}
}
return;
}
void copyKey(unsigned char* keyPrime, unsigned char* key)
{
int i;
for (i = 0; i < KEYLENGTH; ++i) {
keyPrime[i] = key[i];
}
return;
}
/*
NOTE: This is the version that mod's 8, per instructor lecture. It has been commented out because it does not match the Test Vectors, which mods by 10.
Leaving it here in case of last minute grading changes.
We intentionally do not use the 9th and 10th byte of the key (so bytePrime will never be 1 or 0.
the x input gets modded by 8 to give us which byte of the key we will be returning as the subkey.
We then bitshift the key left (with a carry) to give us Key` or "shifted".
The keys are from the right, so we must switch the mod results with its key_byte equivelant.
*/
/*
unsigned char K(unsigned char * key, int x)
{
int byteNumber;
int bytePrime;
byteNumber = x % 8;
printf("\nByte number: %d", byteNumber);
switch (byteNumber) {
case 0:
bytePrime = 9;
break;
case 1:
bytePrime = 8;
break;
case 2:
bytePrime = 7;
break;
case 3:
bytePrime = 6;
break;
case 4:
bytePrime = 5;
break;
case 5:
bytePrime = 4;
break;
case 6:
bytePrime = 3;
break;
case 7:
bytePrime = 2;
break;
default:
printf("Input error in Key Scheduler");
return (unsigned char)'00';
}
leftRotate(key);
printf("\nShifted Key should be ...\n (0x57)(0x9B)(0xDE)(0x02)(0x46)(0x8A)(0xCF)(0x13)(0x57)(0x9B): ");
printKey(key);
printf(" Byte Prime: %d", bytePrime);
printf(" Byte Prime: (0x %02X)", key[bytePrime]);
return key[bytePrime];
}
*/
/*
NOTE: This is the 64 bit key version
It has been left here in case someone changes their minds before launch.
The x input gets modded by 8 to give us which byte of the key we will be returning as the subkey.
We then bitshift the key left (with a carry) to give us Key` or "shifted".
The keys are from the right, so we must switch the mod results with its key_byte equivelant.
*/
/*
unsigned char K(unsigned char * key, int x)
{
int byteNumber;
int bytePrime;
byteNumber = x % 8;
printf("\nByte number: %d", byteNumber);
switch (byteNumber) {
case 0:
bytePrime = 7;
break;
case 1:
bytePrime = 6;
break;
case 2:
bytePrime = 5;
break;
case 3:
bytePrime = 4;
break;
case 4:
bytePrime = 3;
break;
case 5:
bytePrime = 2;
break;
case 6:
bytePrime = 1;
break;
case 7:
bytePrime = 0;
break;
default:
printf("Input error in Key Scheduler");
return (unsigned char)'00';
}
leftRotate(key);
printf("\nShifted Key should be ...\n (0x57)(0x9B)(0xDE)(0x02)(0x46)(0x8A)(0xCF)(0x13)(0x57)(0x9B): ");
printKey(key);
printf(" Byte Prime: %d", bytePrime);
printf(" Byte Prime: (0x %02X)", key[bytePrime]);
return key[bytePrime];
}
*/