1
1
2
2
#include < stdio.h>
3
3
4
+ const int ROUNDS = 9 ; // 9
5
+
4
6
unsigned char known_102308[240 ] = {
5
7
0x60 , 0xed , 0x82 , 0x39 , 0x43 , 0x0c , 0xd9 , 0xc9 , 0x46 , 0x20 , 0x13 , 0xab , 0x10 , 0xe4 , 0x52 , 0x2d ,
6
8
0x30 , 0x06 , 0x2f , 0xc4 , 0xc0 , 0x45 , 0xc7 , 0x3a , 0xe9 , 0x4e , 0x8f , 0x2a , 0x89 , 0x5c , 0xc8 , 0x64 ,
@@ -37,7 +39,7 @@ unsigned char simulated_schedule[160] = {
37
39
0x43 , 0x43 , 0x4c , 0xf9 , 0x74 , 0x4d , 0x81 , 0x78 , 0x31 , 0xcc , 0x92 , 0x69 , 0x02 , 0x8a , 0x5c , 0x1c ,
38
40
};
39
41
40
- unsigned char byte_102210 [16 ] = {
42
+ unsigned char mixer_102210 [16 ] = {
41
43
0x94 , 0x20 , 0x85 , 0x10 , 0xC2 , 0xC0 , 0x01 , 0xFB , 0x01 , 0xC0 , 0xC2 , 0x10 , 0x85 , 0x20 , 0x94 , 0x01 ,
42
44
};
43
45
@@ -79,11 +81,56 @@ unsigned char inverse_sbox_102110[256] = {
79
81
0x42 , 0xB4 , 0xCA , 0xA2 , 0x78 , 0xF9 , 0x4C , 0x49 , 0xD6 , 0x68 , 0x0A , 0xF6 , 0x00 , 0x05 , 0xD7 , 0x81 ,
80
82
};
81
83
84
+ unsigned char expected_decrypt[256 ] = {
85
+ 0x46 , 0xe0 , 0x5f , 0x62 , 0x98 , 0x08 , 0x29 , 0xb4 , 0x6e , 0x97 , 0x4e , 0xf5 , 0x78 , 0x65 , 0x87 , 0xec ,
86
+ 0x52 , 0x2f , 0x39 , 0x08 , 0x5a , 0xc5 , 0xdb , 0xa7 , 0x22 , 0x72 , 0xb5 , 0xf3 , 0xe0 , 0xdb , 0x52 , 0xfc ,
87
+ 0xc4 , 0x86 , 0xf0 , 0x83 , 0x25 , 0xa3 , 0x6d , 0x69 , 0xf6 , 0x8a , 0xfa , 0x14 , 0xaa , 0x85 , 0x80 , 0xc1 ,
88
+ 0x9b , 0xd7 , 0xe3 , 0xc8 , 0x2c , 0x82 , 0x6c , 0x93 , 0x21 , 0x22 , 0x60 , 0x27 , 0x91 , 0xe7 , 0x1a , 0xdf ,
89
+ 0x46 , 0xe0 , 0x5f , 0x62 , 0x98 , 0x08 , 0x29 , 0xb4 , 0x6e , 0x97 , 0x4e , 0xf5 , 0x78 , 0x65 , 0x87 , 0xec ,
90
+ 0x92 , 0x48 , 0xbb , 0x28 , 0x4b , 0x5e , 0x2d , 0xe1 , 0x28 , 0xe8 , 0xb0 , 0x2f , 0x20 , 0x6b , 0xc0 , 0x1b ,
91
+ 0xe2 , 0x39 , 0x38 , 0xce , 0x36 , 0xaa , 0x52 , 0xc1 , 0x56 , 0x24 , 0x09 , 0x2c , 0x24 , 0x0c , 0x77 , 0xe1 ,
92
+ 0x46 , 0xe0 , 0x5f , 0x62 , 0x98 , 0x08 , 0x29 , 0xb4 , 0x6e , 0x97 , 0x4e , 0xf5 , 0x78 , 0x65 , 0x87 , 0xec ,
93
+ 0x86 , 0x6f , 0xba , 0xa9 , 0x71 , 0x5f , 0x4e , 0xf9 , 0xea , 0x34 , 0x06 , 0xad , 0x06 , 0x3c , 0xef , 0x05 ,
94
+ 0x70 , 0xed , 0x3f , 0x46 , 0x31 , 0xb5 , 0xe1 , 0x3a , 0x5e , 0x8a , 0x13 , 0xe7 , 0x6c , 0xa9 , 0x05 , 0xc6 ,
95
+ 0xba , 0x7c , 0x20 , 0xf1 , 0x68 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
96
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
97
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
98
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
99
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
100
+ };
101
+
102
+ unsigned char expected_encrypt[256 ] = {
103
+ 0x7d , 0xef , 0x55 , 0x25 , 0xa0 , 0xb8 , 0xfe , 0x1e , 0x54 , 0x4a , 0x54 , 0x78 , 0x78 , 0xbb , 0x47 , 0x75 ,
104
+ 0xc5 , 0x84 , 0x42 , 0xa7 , 0x23 , 0x35 , 0x78 , 0x87 , 0x1e , 0xce , 0xab , 0xeb , 0x6c , 0x17 , 0x51 , 0x9d ,
105
+ 0xeb , 0xd4 , 0x74 , 0x5b , 0x78 , 0x49 , 0xc9 , 0xe1 , 0xff , 0x10 , 0x3a , 0xef , 0x70 , 0x1c , 0xf6 , 0x56 ,
106
+ 0x02 , 0x65 , 0xb7 , 0x41 , 0x38 , 0x0f , 0x0c , 0x36 , 0xba , 0x30 , 0x45 , 0x35 , 0xaa , 0x4f , 0x0c , 0x57 ,
107
+ 0x7d , 0xef , 0x55 , 0x25 , 0xa0 , 0xb8 , 0xfe , 0x1e , 0x54 , 0x4a , 0x54 , 0x78 , 0x78 , 0xbb , 0x47 , 0x75 ,
108
+ 0x24 , 0xf1 , 0x85 , 0xa5 , 0x68 , 0xa2 , 0xb7 , 0xb8 , 0x0d , 0x1d , 0x94 , 0xe1 , 0x23 , 0x60 , 0xcb , 0x48 ,
109
+ 0x84 , 0xf2 , 0xab , 0xb1 , 0x5c , 0x93 , 0x37 , 0x39 , 0x2a , 0xe3 , 0xca , 0x7e , 0x14 , 0xc4 , 0x3f , 0xf6 ,
110
+ 0x7d , 0xef , 0x55 , 0x25 , 0xa0 , 0xb8 , 0xfe , 0x1e , 0x54 , 0x4a , 0x54 , 0x78 , 0x78 , 0xbb , 0x47 , 0x75 ,
111
+ 0x2c , 0x15 , 0x1b , 0xeb , 0x00 , 0xf2 , 0x55 , 0x46 , 0x5c , 0xa3 , 0x74 , 0xf3 , 0x7b , 0x17 , 0x23 , 0x32 ,
112
+ 0xef , 0x9f , 0xc2 , 0xd6 , 0xef , 0x91 , 0x40 , 0xcf , 0x8f , 0x1c , 0xbf , 0xdd , 0x4a , 0xb0 , 0x28 , 0x62 ,
113
+ 0x70 , 0xaa , 0xaa , 0x76 , 0xec , 0x26 , 0x35 , 0x86 , 0x3c , 0x0e , 0x7a , 0x49 , 0xcb , 0xa7 , 0x3d , 0x43 ,
114
+ 0x2b , 0xea , 0xdf , 0xf4 , 0x43 , 0x95 , 0x4a , 0xc3 , 0xc0 , 0x30 , 0x34 , 0x11 , 0x69 , 0xef , 0x52 , 0x1b ,
115
+ 0x1e , 0x51 , 0xb3 , 0xb1 , 0x7c , 0x85 , 0x7c , 0xea , 0x3b , 0x20 , 0xab , 0x95 , 0xe1 , 0x41 , 0x9f , 0x70 ,
116
+ 0xa1 , 0x22 , 0x56 , 0x69 , 0xa4 , 0xa8 , 0x9b , 0x6b , 0x1e , 0x10 , 0xa8 , 0x9e , 0xc4 , 0x95 , 0x90 , 0x9d ,
117
+ 0xff , 0xbc , 0x21 , 0xbc , 0xc9 , 0x5e , 0xff , 0x59 , 0x6a , 0x59 , 0x74 , 0xa8 , 0xf8 , 0xe5 , 0xc1 , 0x5e ,
118
+ };
119
+
120
+
82
121
void print_data (unsigned char buffer[], int buflen) {
83
- for (int i=0 ; i<buflen; i++) {
84
- printf (" %02x " , buffer[i]);
85
- if ( (i+1 )%16 == 8 ) printf (" " );
86
- if ( (i+1 )%16 == 0 ) printf (" \n " );
122
+ for (int i=0 ; i<buflen; i+=16 ) {
123
+ for (int j=i; j<i+16 ; j++) {
124
+ printf (" %02x " , buffer[j]);
125
+ if ( (j+1 )%16 == 8 ) printf (" " );
126
+ }
127
+ printf (" " );
128
+ for (int j=i; j<i+16 ; j++) {
129
+ unsigned char c = buffer[j];
130
+ if (0x20 <= c && c <= 0x7e ) printf (" %c" ,c);
131
+ else printf (" ." );
132
+ }
133
+ printf (" \n " );
87
134
}
88
135
}
89
136
@@ -109,7 +156,7 @@ void mixcolumns_155(unsigned char state[16]) {
109
156
c = state[0 +15 ];
110
157
for (j = 14 ; j>=0 ; j--) {
111
158
state[j+1 ] = state[j];
112
- c ^= sub_129 (state[j], byte_102210 [j]);
159
+ c ^= sub_129 (state[j], mixer_102210 [j]);
113
160
}
114
161
state[0 ] = c;
115
162
// printf("intermediate mixing: "); print_data(state, 16);
@@ -126,7 +173,7 @@ void inv_mixcolumns_1af(unsigned char state[16]) {
126
173
c = state[0 ];
127
174
for (j = 0 ; j<15 ; j++) {
128
175
state[j] = state[j+1 ];
129
- c ^= sub_129 (state[j], byte_102210 [j]);
176
+ c ^= sub_129 (state[j], mixer_102210 [j]);
130
177
}
131
178
state[15 ] = c;
132
179
// printf("intermediate mixing: "); print_data(state, 16);
@@ -135,8 +182,6 @@ void inv_mixcolumns_1af(unsigned char state[16]) {
135
182
}
136
183
137
184
void block_encrypt_568 (unsigned char schedule[160 ], unsigned char *buffer) {
138
- // very much like decrypt
139
- // calls sub_155 instead of mixcolumns_1af
140
185
// stack frame: 0x30 -- 48 bytes -- 6 qwords
141
186
short i; // Y+0x01 .. Y+0x02
142
187
short j; // Y+0x03 .. Y+0x04
@@ -145,22 +190,48 @@ void block_encrypt_568(unsigned char schedule[160], unsigned char *buffer) {
145
190
// char** buffer; // Y+0x17 .. Y+0x18 -- 0x3faa
146
191
// char unused1[8]; // Y+0x19 .. Y+0x20 -- temp swap
147
192
// char unused2[16]; // Y+0x21 .. Y+0x30 -- temp swap
193
+ unsigned char a[16 ], b[16 ];
148
194
149
195
for (i=0 ; i<16 ; i++) {
150
196
state[i] = buffer[i];
151
197
}
198
+ // printf("------------------------------------------------------------------------------------------------\n");
199
+ // printf("entry line: ");
200
+ // print_data(buffer, 16);
201
+ // printf("initial sched: ");
202
+ // print_data(schedule+0x90, 16);
203
+ // printf("initial state: ");
204
+ // print_data(state, 16);
205
+ // printf("\n");
152
206
153
- for (i=0 ; i<= 8 ; i++) {
207
+ for (i=0 ; i<ROUNDS ; i++) {
154
208
for (j=0 ; j<16 ; j++) {
155
- state[j] = schedule[i*16 +j] ^ forward_sbox_102010[state[j]];
209
+ if (true ) {
210
+ a[j] = schedule[i*16 +j];
211
+ b[j] = state[j]^a[j];
212
+ state[j] = forward_sbox_102010[b[j]];
213
+ } else {
214
+ state[j] ^= schedule[i*16 +j];
215
+ state[j] = forward_sbox_102010[state[j]];
216
+ }
156
217
}
218
+ // printf("schedule line: ");
219
+ // print_data(a, 16);
220
+ // printf("post sbox: ");
221
+ // print_data(b, 16);
222
+ // printf("next state: ");
223
+ // print_data(state, 16);
157
224
mixcolumns_155 (state);
225
+ // printf("mix-columns: ");
226
+ // print_data(state, 16);
227
+ // printf("\n");
158
228
}
159
229
160
230
for (i=0 ; i<16 ; i++) {
161
231
buffer[i] = state[i] ^ schedule[0x90 +i];
162
232
}
163
- return ;
233
+ // printf("final state: ");
234
+ // print_data(buffer, 16);
164
235
}
165
236
166
237
void block_decrypt_702 (unsigned char schedule[160 ], unsigned char *buffer) {
@@ -172,8 +243,8 @@ void block_decrypt_702(unsigned char schedule[160], unsigned char *buffer) {
172
243
// char** buffer; // Y+0x17 .. Y+0x18 -- 0x3faa
173
244
// char unused1[8]; // Y+0x19 .. Y+0x20 -- temp swap
174
245
// char unused2[16]; // Y+0x21 .. Y+0x30 -- temp swap
175
-
176
246
unsigned char a[16 ], b[16 ];
247
+
177
248
for (i=0 ; i<16 ; i++) {
178
249
state[i] = buffer[i] ^ schedule[0x90 +i];
179
250
}
@@ -186,37 +257,36 @@ void block_decrypt_702(unsigned char schedule[160], unsigned char *buffer) {
186
257
// print_data(state, 16);
187
258
// printf("\n");
188
259
189
- for (short i = 8 ; i>=0 ; i--) {
260
+ for (i=ROUNDS- 1 ; i>=0 ; i--) {
190
261
inv_mixcolumns_1af (state);
191
262
// printf("mix-columns: ");
192
263
// print_data(state, 16);
193
264
for (j=0 ; j<16 ; j++) {
194
- a[j] = schedule[i*16 + j];
195
- b[j] = inverse_sbox_102110[(int )state[j]];
196
- state[j] = a[j]^b[j];
265
+ if (true ) {
266
+ a[j] = schedule[i*16 + j];
267
+ b[j] = inverse_sbox_102110[state[j]];
268
+ state[j] = a[j]^b[j];
269
+ } else {
270
+ state[j] = inverse_sbox_102110[state[j]];
271
+ state[j] ^= schedule[i*16 + j];
272
+ }
197
273
}
198
- // printf("schedule line: ");
199
- // print_data(a, 16);
200
274
// printf("post sbox: ");
201
275
// print_data(b, 16);
276
+ // printf("schedule line: ");
277
+ // print_data(a, 16);
202
278
// printf("next state: ");
203
279
// print_data(state, 16);
204
280
// printf("\n");
205
- /*
206
- for (j=0; j<16; j++) {
207
- state[j] = inverse_sbox_102110[(int)state[j]];
208
- }
209
- for (j = 0; j<16; j++) {
210
- state[j] = state[j] ^ schedule[i*16 + j]
211
- }
212
- */
213
281
}
214
282
215
283
for (i=0 ; i<16 ; i++) {
216
284
buffer[i] = state[i];
217
285
}
286
+ // printf("final state: ");
287
+ // print_data(buffer, 16);
218
288
219
- // printf("------------------------------------------------------------------------------------------------\n");
289
+ // // printf("------------------------------------------------------------------------------------------------\n");
220
290
return ;
221
291
}
222
292
@@ -247,40 +317,85 @@ void encrypt_data(unsigned char buffer[24], short length) {
247
317
}
248
318
}
249
319
320
+ void fix_sbox () {
321
+ for (int i=0 ; i<256 ; i++) {
322
+ inverse_sbox_102110[forward_sbox_102010[i]] = i;
323
+ }
324
+ }
325
+
250
326
int main (int argc, char ** argv) {
251
327
// short decoded = 0; // Y+1,2
252
328
short length = 240 ; // Y+3,4
253
329
// char choice = 0; // Y+5
254
- unsigned char known[240 +15 ]; // Y+6..0xF6 -- 240 bytes, plus null terminator
330
+ unsigned char known[240 ]; // Y+6..0xF6 -- 240 bytes, plus null terminator
331
+ unsigned char reverse[240 ];
255
332
256
- // init_clock();
257
- // some_init_a6b(); // more init
333
+ fix_sbox ();
334
+
335
+ printf (" ********************************************************************************\n " );
336
+ unsigned char testing[17 ] = " 0123456789abcdef" ;
337
+ print_data (testing, 16 ); printf (" \n " );
338
+ encrypt_data (testing, 16 );
339
+ print_data (testing, 16 ); printf (" \n " );
340
+ decrypt_data_8f1 (testing, 16 );
341
+ print_data (testing, 16 ); printf (" \n " );
342
+
343
+ // return 0;
344
+
345
+ printf (" ********************************************************************************\n " );
346
+ // // Try a Decrypt ////
258
347
for (int i=0 ; i<length; i++) {
259
348
known[i] = known_102308[i];
260
349
}
261
- // init of decoded and length happen here
262
350
printf (" Raw Ciphertext\n " );
263
351
print_data (known, length);
264
352
printf (" \n " );
265
353
266
354
decrypt_data_8f1 (known, length);
267
355
268
- printf (" Possible Plaintext \n " );
356
+ printf (" Decryption \n " );
269
357
print_data (known, length);
270
358
printf (" \n " );
271
359
272
360
printf (" Garbage: " );
273
361
for (int i=0 ;i<240 ;i++) printf (" %c" , known[i]);
274
362
printf (" \n " );
275
363
276
- // Reverse Path
277
- encrypt_data (known, length);
364
+ printf (" Deltas expected decrypt\n " );
365
+ for (int i=0 ; i<length; i++) {
366
+ known[i] ^= expected_decrypt[i];
367
+ }
368
+ print_data (known, length);
278
369
279
- printf (" Re-Encrypted\n " );
370
+ printf (" ********************************************************************************\n " );
371
+ // // Reverse Path ////
372
+ for (int i=0 ; i<length; i++) {
373
+ reverse[i] = known_102308[i];
374
+ }
375
+ encrypt_data (reverse, length);
376
+
377
+ printf (" Try Encryption Instead\n " );
378
+ print_data (reverse, length);
379
+ printf (" \n " );
380
+
381
+ printf (" Deltas expected encrypt\n " );
382
+ for (int i=0 ; i<length; i++) {
383
+ reverse[i] ^= expected_encrypt[i];
384
+ }
385
+ print_data (reverse, length);
386
+
387
+ printf (" ********************************************************************************\n " );
388
+ // // Checking ////
389
+ for (int i=0 ; i<length; i++) {
390
+ known[i] = known_102308[i];
391
+ }
392
+ decrypt_data_8f1 (known, length);
393
+ encrypt_data (known, length);
394
+ printf (" Re-Encryption\n " );
280
395
print_data (known, length);
281
396
printf (" \n " );
282
397
283
- printf (" Delta \n " );
398
+ printf (" Deltas \n " );
284
399
for (int i=0 ; i<length; i++) {
285
400
known[i] ^= known_102308[i];
286
401
}
0 commit comments