OpenCores
URL https://opencores.org/ocsvn/hight/hight/trunk

Subversion Repositories hight

[/] [hight/] [trunk/] [sw/] [hight_unit_test.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 truemind
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  Source file of unit test functions for HIGHT Integer Model  ////
4
////                                                              ////
5
////  This file is part of the HIGHT Crypto Core project          ////
6
////  http://github.com/OpenSoCPlus/hight_crypto_core             ////
7
////  http://www.opencores.org/project,hight                      ////
8
////                                                              ////
9
////  Description                                                 ////
10
////  __description__                                             ////
11
////                                                              ////
12
////  Author(s):                                                  ////
13
////      - JoonSoo Ha, json.ha@gmail.com                         ////
14
////      - Younjoo Kim, younjookim.kr@gmail.com                  ////
15
////                                                              ////
16
//////////////////////////////////////////////////////////////////////
17
////                                                              ////
18
//// Copyright (C) 2015 Authors, OpenSoCPlus and OPENCORES.ORG    ////
19
////                                                              ////
20
//// This source file may be used and distributed without         ////
21
//// restriction provided that this copyright statement is not    ////
22
//// removed from the file and that any derivative work contains  ////
23
//// the original copyright notice and the associated disclaimer. ////
24
////                                                              ////
25
//// This source file is free software; you can redistribute it   ////
26
//// and/or modify it under the terms of the GNU Lesser General   ////
27
//// Public License as published by the Free Software Foundation; ////
28
//// either version 2.1 of the License, or (at your option) any   ////
29
//// later version.                                               ////
30
////                                                              ////
31
//// This source is distributed in the hope that it will be       ////
32
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
33
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
34
//// PURPOSE.  See the GNU Lesser General Public License for more ////
35
//// details.                                                     ////
36
////                                                              ////
37
//// You should have received a copy of the GNU Lesser General    ////
38
//// Public License along with this source; if not, download it   ////
39
//// from http://www.opencores.org/lgpl.shtml                     ////
40
////                                                              ////
41
//////////////////////////////////////////////////////////////////////
42
 
43
#include "hight.h"
44
#include <stdio.h>
45
 
46
extern HIGHT_DATA *p_hight_data;
47
 
48
/* =====================================
49
 
50
    DeltaGenTest()
51
 
52
=======================================*/
53
void DeltaGenTest()
54
{
55
        int i;
56
 
57
        printf("\n\n===== DeltaGenTest =====\n\n");
58
 
59
        DeltaGen(p_hight_data->delta);
60
 
61
        for (i=0; i<128; i++){
62
                printf("%02X ", p_hight_data->delta[i]);
63
                if(i%8 == 7)
64
                        printf("\n");
65
        }
66
}
67
 
68
 
69
/* =====================================
70
 
71
    SubKeyGenTest()
72
 
73
=======================================*/
74
void SubKeyGenTest()
75
{
76
        int i ;
77
 
78
        printf("\n\n===== SubKeyGenTest =====\n\n");
79
 
80
        /*
81
        // Test vectors 1
82
        p_hight_data->i_mk[15] = 0x00;
83
        p_hight_data->i_mk[14] = 0x11;
84
        p_hight_data->i_mk[13] = 0x22;
85
        p_hight_data->i_mk[12] = 0x33;
86
        p_hight_data->i_mk[11] = 0x44;
87
        p_hight_data->i_mk[10] = 0x55;
88
        p_hight_data->i_mk[9] = 0x66;
89
        p_hight_data->i_mk[8] = 0x77;
90
        p_hight_data->i_mk[7] = 0x88;
91
        p_hight_data->i_mk[6] = 0x99;
92
        p_hight_data->i_mk[5] = 0xaa;
93
        p_hight_data->i_mk[4] = 0xbb;
94
        p_hight_data->i_mk[3] = 0xcc;
95
        p_hight_data->i_mk[2] = 0xdd;
96
        p_hight_data->i_mk[1] = 0xee;
97
        p_hight_data->i_mk[0] = 0xff;
98
        */
99
 
100
        /*
101
        // Test vectors 2
102
        p_hight_data->i_mk[15] = 0xff;
103
        p_hight_data->i_mk[14] = 0xee;
104
        p_hight_data->i_mk[13] = 0xdd;
105
        p_hight_data->i_mk[12] = 0xcc;
106
        p_hight_data->i_mk[11] = 0xbb;
107
        p_hight_data->i_mk[10] = 0xaa;
108
        p_hight_data->i_mk[9] = 0x99;
109
        p_hight_data->i_mk[8] = 0x88;
110
        p_hight_data->i_mk[7] = 0x77;
111
        p_hight_data->i_mk[6] = 0x66;
112
        p_hight_data->i_mk[5] = 0x55;
113
        p_hight_data->i_mk[4] = 0x44;
114
        p_hight_data->i_mk[3] = 0x33;
115
        p_hight_data->i_mk[2] = 0x22;
116
        p_hight_data->i_mk[1] = 0x11;
117
        p_hight_data->i_mk[0] = 0x00;
118
        */
119
 
120
 
121
        /*
122
        // Test vector 3
123
        p_hight_data->i_mk[15] = 0x00;
124
        p_hight_data->i_mk[14] = 0x01;
125
        p_hight_data->i_mk[13] = 0x02;
126
        p_hight_data->i_mk[12] = 0x03;
127
        p_hight_data->i_mk[11] = 0x04;
128
        p_hight_data->i_mk[10] = 0x05;
129
        p_hight_data->i_mk[9] = 0x06;
130
        p_hight_data->i_mk[8] = 0x07;
131
        p_hight_data->i_mk[7] = 0x08;
132
        p_hight_data->i_mk[6] = 0x09;
133
        p_hight_data->i_mk[5] = 0x0a;
134
        p_hight_data->i_mk[4] = 0x0b;
135
        p_hight_data->i_mk[3] = 0x0c;
136
        p_hight_data->i_mk[2] = 0x0d;
137
        p_hight_data->i_mk[1] = 0x0e;
138
        p_hight_data->i_mk[0] = 0x0f;
139
        */
140
 
141
        // Test vector 4
142
        p_hight_data->i_mk[15] = 0x28;
143
        p_hight_data->i_mk[14] = 0xdb;
144
        p_hight_data->i_mk[13] = 0xc3;
145
        p_hight_data->i_mk[12] = 0xbc;
146
        p_hight_data->i_mk[11] = 0x49;
147
        p_hight_data->i_mk[10] = 0xff;
148
        p_hight_data->i_mk[9] = 0xd8;
149
        p_hight_data->i_mk[8] = 0x7d;
150
        p_hight_data->i_mk[7] = 0xcf;
151
        p_hight_data->i_mk[6] = 0xa5;
152
        p_hight_data->i_mk[5] = 0x09;
153
        p_hight_data->i_mk[4] = 0xb1;
154
        p_hight_data->i_mk[3] = 0x1d;
155
        p_hight_data->i_mk[2] = 0x42;
156
        p_hight_data->i_mk[1] = 0x2b;
157
        p_hight_data->i_mk[0] = 0xe7;
158
 
159
        DeltaGen(p_hight_data->delta);
160
 
161
        SubKeyGen(p_hight_data->i_mk,
162
                        p_hight_data->delta,
163
                        p_hight_data->sk);
164
 
165
        for (i=0; i<128; i+=4){
166
                printf("%02x ", p_hight_data->sk[i+3]);
167
                printf("%02x ", p_hight_data->sk[i+2]);
168
                printf("%02x ", p_hight_data->sk[i+1]);
169
                printf("%02x ", p_hight_data->sk[i]);
170
                printf("\n");
171
        }
172
}
173
 
174
 
175
/* =====================================
176
 
177
    WhiteningKeyGenTest()
178
 
179
=======================================*/
180
void WhiteningKeyGenTest ()
181
{
182
        p_hight_data->i_mk[0] = 0;
183
        p_hight_data->i_mk[1] = 1;
184
        p_hight_data->i_mk[2] = 2;
185
        p_hight_data->i_mk[3] = 3;
186
        p_hight_data->i_mk[4] = -1;
187
        p_hight_data->i_mk[5] = -1;
188
        p_hight_data->i_mk[6] = -1;
189
        p_hight_data->i_mk[7] = -1;
190
        p_hight_data->i_mk[8] = -1;
191
        p_hight_data->i_mk[9] = -1;
192
        p_hight_data->i_mk[10] = -1;
193
        p_hight_data->i_mk[11] = -1;
194
        p_hight_data->i_mk[12] = 12;
195
        p_hight_data->i_mk[13] = 13;
196
        p_hight_data->i_mk[14] = 14;
197
        p_hight_data->i_mk[15] = 15;
198
 
199
        printf("\n\n===== WhiteningKeyGenTest =====\n\n");
200
 
201
        WhiteningKeyGen(p_hight_data->i_mk, p_hight_data->wk);
202
 
203
        if ( p_hight_data->wk[0] == 12)
204
                printf("wk[0] = %d (Correct) \n",p_hight_data->wk[0]);
205
        else
206
                printf("wk[0] = %d (Wrong => expected = 12) \n",p_hight_data->wk[0]);
207
 
208
        if ( p_hight_data->wk[1] == 13)
209
                printf("wk[1] = %d (Correct) \n",p_hight_data->wk[1]);
210
        else
211
                printf("wk[1] = %d (Wrong => expected = 13) \n",p_hight_data->wk[1]);
212
 
213
        if ( p_hight_data->wk[2] == 14)
214
                printf("wk[2] = %d (Correct) \n",p_hight_data->wk[2]);
215
        else
216
                printf("wk[2] = %d (Wrong => expected = 14) \n",p_hight_data->wk[2]);
217
 
218
        if ( p_hight_data->wk[3] == 15)
219
                printf("wk[3] = %d (Correct) \n",p_hight_data->wk[3]);
220
        else
221
                printf("wk[3] = %d (Wrong => expected = 15) \n",p_hight_data->wk[3]);
222
 
223
        if ( p_hight_data->wk[4] == 0)
224
                printf("wk[4] = %d (Correct) \n",p_hight_data->wk[4]);
225
        else
226
                printf("wk[4] = %d (Wrong => expected = 0) \n",p_hight_data->wk[4]);
227
 
228
        if ( p_hight_data->wk[5] == 1)
229
                printf("wk[5] = %d (Correct) \n",p_hight_data->wk[5]);
230
        else
231
                printf("wk[5] = %d (Wrong => expected = 1) \n",p_hight_data->wk[5]);
232
 
233
        if ( p_hight_data->wk[6] == 2)
234
                printf("wk[6] = %d (Correct) \n",p_hight_data->wk[6]);
235
        else
236
                printf("wk[6] = %d (Wrong => expected = 2) \n",p_hight_data->wk[6]);
237
 
238
        if ( p_hight_data->wk[7] == 3)
239
                printf("wk[7] = %d (Correct) \n",p_hight_data->wk[7]);
240
        else
241
                printf("wk[7] = %d (Wrong => expected = [3]) \n",p_hight_data->wk[7]);
242
}
243
 
244
 
245
/* =====================================
246
 
247
    InitialWhiteningFunctionTest()
248
 
249
=======================================*/
250
void InitialWhiteningFunctionTest ()
251
{
252
        byte expected_iwf[8] = {0};
253
        int i_op = -1;
254
        int i;
255
 
256
        printf("\n\n===== InitialWhiteningFunctionTest =====\n\n");
257
 
258
        /////////////////////////////////////////////////
259
        //
260
        // Encryption vector
261
        //
262
        /////////////////////////////////////////////////
263
        printf("\n\n===== Encryption operation =====\n\n");
264
 
265
#if 0
266
        // Test vectors 1
267
        printf("test vectors 1\n");
268
 
269
        i_op = ENC;
270
 
271
        //MasterKey
272
        p_hight_data->i_mk[15] = 0x00;
273
        p_hight_data->i_mk[14] = 0x11;
274
        p_hight_data->i_mk[13] = 0x22;
275
        p_hight_data->i_mk[12] = 0x33;
276
        p_hight_data->i_mk[11] = 0x44;
277
        p_hight_data->i_mk[10] = 0x55;
278
        p_hight_data->i_mk[9]  = 0x66;
279
        p_hight_data->i_mk[8]  = 0x77;
280
        p_hight_data->i_mk[7]  = 0x88;
281
        p_hight_data->i_mk[6]  = 0x99;
282
        p_hight_data->i_mk[5]  = 0xaa;
283
        p_hight_data->i_mk[4]  = 0xbb;
284
        p_hight_data->i_mk[3]  = 0xcc;
285
        p_hight_data->i_mk[2]  = 0xdd;
286
        p_hight_data->i_mk[1]  = 0xee;
287
        p_hight_data->i_mk[0]  = 0xff;
288
 
289
        //PlainText
290
        p_hight_data->i_pct[7]  = 0x00;
291
        p_hight_data->i_pct[6]  = 0x00;
292
        p_hight_data->i_pct[5]  = 0x00;
293
        p_hight_data->i_pct[4]  = 0x00;
294
        p_hight_data->i_pct[3]  = 0x00;
295
        p_hight_data->i_pct[2]  = 0x00;
296
        p_hight_data->i_pct[1]  = 0x00;
297
        p_hight_data->i_pct[0]  = 0x00;
298
 
299
        // expected iwf
300
        expected_iwf[7]        = 0x00;
301
        expected_iwf[6]        = 0x00;
302
        expected_iwf[5]        = 0x00;
303
        expected_iwf[4]        = 0x11;
304
        expected_iwf[3]        = 0x00;
305
        expected_iwf[2]        = 0x22;
306
        expected_iwf[1]        = 0x00;
307
        expected_iwf[0]        = 0x33;
308
 
309
#elif 0
310
        // Test vectors 2
311
        printf("test vectors 2\n");
312
 
313
        i_op = ENC;
314
 
315
        //MasterKey
316
        p_hight_data->i_mk[15] = 0xff;
317
        p_hight_data->i_mk[14] = 0xee;
318
        p_hight_data->i_mk[13] = 0xdd;
319
        p_hight_data->i_mk[12] = 0xcc;
320
        p_hight_data->i_mk[11] = 0xbb;
321
        p_hight_data->i_mk[10] = 0xaa;
322
        p_hight_data->i_mk[9]  = 0x99;
323
        p_hight_data->i_mk[8]  = 0x88;
324
        p_hight_data->i_mk[7]  = 0x77;
325
        p_hight_data->i_mk[6]  = 0x66;
326
        p_hight_data->i_mk[5]  = 0x55;
327
        p_hight_data->i_mk[4]  = 0x44;
328
        p_hight_data->i_mk[3]  = 0x33;
329
        p_hight_data->i_mk[2]  = 0x22;
330
        p_hight_data->i_mk[1]  = 0x11;
331
        p_hight_data->i_mk[0]  = 0x00;
332
 
333
        //PlainText
334
        p_hight_data->i_pct[7]  = 0x00;
335
        p_hight_data->i_pct[6]  = 0x11;
336
        p_hight_data->i_pct[5]  = 0x22;
337
        p_hight_data->i_pct[4]  = 0x33;
338
        p_hight_data->i_pct[3]  = 0x44;
339
        p_hight_data->i_pct[2]  = 0x55;
340
        p_hight_data->i_pct[1]  = 0x66;
341
        p_hight_data->i_pct[0]  = 0x77;
342
 
343
        // expected iwf
344
        expected_iwf[7]        = 0x00;
345
        expected_iwf[6]        = 0xee;
346
        expected_iwf[5]        = 0x22;
347
        expected_iwf[4]        = 0x21;
348
        expected_iwf[3]        = 0x44;
349
        expected_iwf[2]        = 0x88;
350
        expected_iwf[1]        = 0x66;
351
        expected_iwf[0]        = 0x43;
352
 
353
#elif 0
354
        // Test vector 3
355
        printf("test vectors 3\n");
356
 
357
        i_op = ENC;
358
 
359
        //MasterKey
360
        p_hight_data->i_mk[15] = 0x00;
361
        p_hight_data->i_mk[14] = 0x01;
362
        p_hight_data->i_mk[13] = 0x02;
363
        p_hight_data->i_mk[12] = 0x03;
364
        p_hight_data->i_mk[11] = 0x04;
365
        p_hight_data->i_mk[10] = 0x05;
366
        p_hight_data->i_mk[9]  = 0x06;
367
        p_hight_data->i_mk[8]  = 0x07;
368
        p_hight_data->i_mk[7]  = 0x08;
369
        p_hight_data->i_mk[6]  = 0x09;
370
        p_hight_data->i_mk[5]  = 0x0a;
371
        p_hight_data->i_mk[4]  = 0x0b;
372
        p_hight_data->i_mk[3]  = 0x0c;
373
        p_hight_data->i_mk[2]  = 0x0d;
374
        p_hight_data->i_mk[1]  = 0x0e;
375
        p_hight_data->i_mk[0]  = 0x0f;
376
 
377
        //PlainText
378
        p_hight_data->i_pct[7]  = 0x01;
379
        p_hight_data->i_pct[6]  = 0x23;
380
        p_hight_data->i_pct[5]  = 0x45;
381
        p_hight_data->i_pct[4]  = 0x67;
382
        p_hight_data->i_pct[3]  = 0x89;
383
        p_hight_data->i_pct[2]  = 0xab;
384
        p_hight_data->i_pct[1]  = 0xcd;
385
        p_hight_data->i_pct[0]  = 0xef;
386
 
387
        // expected iwf
388
        expected_iwf[7]        = 0x01;
389
        expected_iwf[6]        = 0x23;
390
        expected_iwf[5]        = 0x45;
391
        expected_iwf[4]        = 0x68;
392
        expected_iwf[3]        = 0x89;
393
        expected_iwf[2]        = 0xa9;
394
        expected_iwf[1]        = 0xcd;
395
        expected_iwf[0]        = 0xf2;
396
 
397
#elif 0
398
        // Test vector 4
399
        printf("test vectors 4\n");
400
 
401
        i_op = ENC;
402
 
403
        //MasterKey
404
        p_hight_data->i_mk[15] = 0x28;
405
        p_hight_data->i_mk[14] = 0xdb;
406
        p_hight_data->i_mk[13] = 0xc3;
407
        p_hight_data->i_mk[12] = 0xbc;
408
        p_hight_data->i_mk[11] = 0x49;
409
        p_hight_data->i_mk[10] = 0xff;
410
        p_hight_data->i_mk[9]  = 0xd8;
411
        p_hight_data->i_mk[8]  = 0x7d;
412
        p_hight_data->i_mk[7]  = 0xcf;
413
        p_hight_data->i_mk[6]  = 0xa5;
414
        p_hight_data->i_mk[5]  = 0x09;
415
        p_hight_data->i_mk[4]  = 0xb1;
416
        p_hight_data->i_mk[3]  = 0x1d;
417
        p_hight_data->i_mk[2]  = 0x42;
418
        p_hight_data->i_mk[1]  = 0x2b;
419
        p_hight_data->i_mk[0]  = 0xe7;
420
 
421
        //PlainText
422
        p_hight_data->i_pct[7]  = 0xb4;
423
        p_hight_data->i_pct[6]  = 0x1e;
424
        p_hight_data->i_pct[5]  = 0x6b;
425
        p_hight_data->i_pct[4]  = 0xe2;
426
        p_hight_data->i_pct[3]  = 0xeb;
427
        p_hight_data->i_pct[2]  = 0xa8;
428
        p_hight_data->i_pct[1]  = 0x4a;
429
        p_hight_data->i_pct[0]  = 0x14;
430
 
431
        // expected iwf
432
        expected_iwf[7]        = 0xb4;
433
        expected_iwf[6]        = 0x36;
434
        expected_iwf[5]        = 0x6b;
435
        expected_iwf[4]        = 0xbd;
436
        expected_iwf[3]        = 0xeb;
437
        expected_iwf[2]        = 0x6b;
438
        expected_iwf[1]        = 0x4a;
439
        expected_iwf[0]        = 0xd0;
440
#endif
441
 
442
 
443
        /////////////////////////////////////////////////
444
        //
445
        // Decrytion vector
446
        //
447
        /////////////////////////////////////////////////
448
 
449
        printf("\n\n===== Decrytion operation =====\n\n");
450
 
451
#if 1
452
        // Test vectors 1
453
        printf("test vectors 1\n");
454
 
455
        i_op = DEC;
456
 
457
        //MasterKey
458
        p_hight_data->i_mk[15]    = 0x00;
459
        p_hight_data->i_mk[14]    = 0x11;
460
        p_hight_data->i_mk[13]    = 0x22;
461
        p_hight_data->i_mk[12]    = 0x33;
462
        p_hight_data->i_mk[11]    = 0x44;
463
        p_hight_data->i_mk[10]    = 0x55;
464
        p_hight_data->i_mk[9]     = 0x66;
465
        p_hight_data->i_mk[8]     = 0x77;
466
        p_hight_data->i_mk[7]     = 0x88;
467
        p_hight_data->i_mk[6]     = 0x99;
468
        p_hight_data->i_mk[5]     = 0xaa;
469
        p_hight_data->i_mk[4]     = 0xbb;
470
        p_hight_data->i_mk[3]     = 0xcc;
471
        p_hight_data->i_mk[2]     = 0xdd;
472
        p_hight_data->i_mk[1]     = 0xee;
473
        p_hight_data->i_mk[0]     = 0xff;
474
 
475
        // Round32 output
476
        p_hight_data->i_pct[7]  = 0x00;
477
        p_hight_data->i_pct[6]  = 0xf4;
478
        p_hight_data->i_pct[5]  = 0x18;
479
        p_hight_data->i_pct[4]  = 0xae;
480
        p_hight_data->i_pct[3]  = 0xd9;
481
        p_hight_data->i_pct[2]  = 0x4f;
482
        p_hight_data->i_pct[1]  = 0x03;
483
        p_hight_data->i_pct[0]  = 0xf2;
484
 
485
        // expected iwf
486
        expected_iwf[7]        = 0x00;
487
        expected_iwf[6]        = 0x38;
488
        expected_iwf[5]        = 0x18;
489
        expected_iwf[4]        = 0xd1;
490
        expected_iwf[3]        = 0xd9;
491
        expected_iwf[2]        = 0xa1;
492
        expected_iwf[1]        = 0x03;
493
        expected_iwf[0]        = 0xf3;
494
 
495
 
496
#elif 0
497
        // Test vectors 2
498
        printf("test vectors 2\n");
499
 
500
        i_op = DEC;
501
 
502
        //MasterKey
503
        p_hight_data->i_mk[15]   = 0xff;
504
        p_hight_data->i_mk[14]   = 0xee;
505
        p_hight_data->i_mk[13]   = 0xdd;
506
        p_hight_data->i_mk[12]   = 0xcc;
507
        p_hight_data->i_mk[11]   = 0xbb;
508
        p_hight_data->i_mk[10]   = 0xaa;
509
        p_hight_data->i_mk[9]    = 0x99;
510
        p_hight_data->i_mk[8]    = 0x88;
511
        p_hight_data->i_mk[7]    = 0x77;
512
        p_hight_data->i_mk[6]    = 0x66;
513
        p_hight_data->i_mk[5]    = 0x55;
514
        p_hight_data->i_mk[4]    = 0x44;
515
        p_hight_data->i_mk[3]    = 0x33;
516
        p_hight_data->i_mk[2]    = 0x22;
517
        p_hight_data->i_mk[1]    = 0x11;
518
        p_hight_data->i_mk[0]    = 0x00;
519
 
520
        //Round32 output
521
        p_hight_data->rf[32][7]    = 0x00;
522
        p_hight_data->rf[32][6]    = 0xee;
523
        p_hight_data->rf[32][5]    = 0x22;
524
        p_hight_data->rf[32][4]    = 0x21;
525
        p_hight_data->rf[32][3]    = 0x44;
526
        p_hight_data->rf[32][2]    = 0x88;
527
        p_hight_data->rf[32][1]    = 0x66;
528
        p_hight_data->rf[32][0]    = 0x43;
529
 
530
        // expected iwf
531
        expected_iwf[7]          = 0x00;
532
        expected_iwf[6]          = 0x11;
533
        expected_iwf[5]          = 0x22;
534
        expected_iwf[4]          = 0x33;
535
        expected_iwf[3]          = 0x44;
536
        expected_iwf[2]          = 0x55;
537
        expected_iwf[1]          = 0x66;
538
        expected_iwf[0]          = 0x77;
539
 
540
#elif 0
541
        // Test vector 3
542
        printf("test vectors 3\n");
543
 
544
        i_op = DEC;
545
 
546
        //MasterKey
547
        p_hight_data->i_mk[15]   = 0x00;
548
        p_hight_data->i_mk[14]   = 0x01;
549
        p_hight_data->i_mk[13]   = 0x02;
550
        p_hight_data->i_mk[12]   = 0x03;
551
        p_hight_data->i_mk[11]   = 0x04;
552
        p_hight_data->i_mk[10]   = 0x05;
553
        p_hight_data->i_mk[9]    = 0x06;
554
        p_hight_data->i_mk[8]    = 0x07;
555
        p_hight_data->i_mk[7]    = 0x08;
556
        p_hight_data->i_mk[6]    = 0x09;
557
        p_hight_data->i_mk[5]    = 0x0a;
558
        p_hight_data->i_mk[4]    = 0x0b;
559
        p_hight_data->i_mk[3]    = 0x0c;
560
        p_hight_data->i_mk[2]    = 0x0d;
561
        p_hight_data->i_mk[1]    = 0x0e;
562
        p_hight_data->i_mk[0]    = 0x0f;
563
 
564
        //Round32 output
565
        p_hight_data->rf[32][7]  = 0x01;
566
        p_hight_data->rf[32][6]  = 0x23;
567
        p_hight_data->rf[32][5]  = 0x45;
568
        p_hight_data->rf[32][4]  = 0x68;
569
        p_hight_data->rf[32][3]  = 0x89;
570
        p_hight_data->rf[32][2]  = 0xa9;
571
        p_hight_data->rf[32][1]  = 0xcd;
572
        p_hight_data->rf[32][0]  = 0xf2;
573
 
574
        // expected iwf
575
        expected_iwf[7]          = 0x01;
576
        expected_iwf[6]          = 0x23;
577
        expected_iwf[5]          = 0x45;
578
        expected_iwf[4]          = 0x67;
579
        expected_iwf[3]          = 0x89;
580
        expected_iwf[2]          = 0xab;
581
        expected_iwf[1]          = 0xcd;
582
        expected_iwf[0]          = 0xef;
583
 
584
#elif 0
585
        // Test vector 4
586
        printf("test vectors 4\n");
587
 
588
        i_op = DEC;
589
 
590
        //MasterKey
591
        p_hight_data->i_mk[15]   = 0x28;
592
        p_hight_data->i_mk[14]   = 0xdb;
593
        p_hight_data->i_mk[13]   = 0xc3;
594
        p_hight_data->i_mk[12]   = 0xbc;
595
        p_hight_data->i_mk[11]   = 0x49;
596
        p_hight_data->i_mk[10]   = 0xff;
597
        p_hight_data->i_mk[9]    = 0xd8;
598
        p_hight_data->i_mk[8]    = 0x7d;
599
        p_hight_data->i_mk[7]    = 0xcf;
600
        p_hight_data->i_mk[6]    = 0xa5;
601
        p_hight_data->i_mk[5]    = 0x09;
602
        p_hight_data->i_mk[4]    = 0xb1;
603
        p_hight_data->i_mk[3]    = 0x1d;
604
        p_hight_data->i_mk[2]    = 0x42;
605
        p_hight_data->i_mk[1]    = 0x2b;
606
        p_hight_data->i_mk[0]    = 0xe7;
607
 
608
        //Round32 output
609
        p_hight_data->rf[32][7]  = 0xb4;
610
        p_hight_data->rf[32][6]  = 0x36;
611
        p_hight_data->rf[32][5]  = 0x6b;
612
        p_hight_data->rf[32][4]  = 0xbd;
613
        p_hight_data->rf[32][3]  = 0xeb;
614
        p_hight_data->rf[32][2]  = 0x6b;
615
        p_hight_data->rf[32][1]  = 0x4a;
616
        p_hight_data->rf[32][0]  = 0xd0;
617
 
618
        // expected iwf
619
        expected_iwf[7]          = 0xb4;
620
        expected_iwf[6]          = 0x1e;
621
        expected_iwf[5]          = 0x6b;
622
        expected_iwf[4]          = 0xe2;
623
        expected_iwf[3]          = 0xeb;
624
        expected_iwf[2]          = 0xa8;
625
        expected_iwf[1]          = 0x4a;
626
        expected_iwf[0]          = 0x14;
627
#endif
628
 
629
 
630
 
631
        /////////////////////////////////////////////////
632
        //
633
        // Run test
634
        //
635
        /////////////////////////////////////////////////
636
        WhiteningKeyGen(p_hight_data->i_mk, p_hight_data->wk);
637
 
638
        InitialWhiteningFunction(i_op ,p_hight_data->i_pct, p_hight_data->wk, p_hight_data->iwf);
639
 
640
        printf("\n>> operation : %s <<<\n", (i_op == ENC) ? "encryption" : "decrytion");
641
 
642
        printf("\n>>> %s text <<<\n", (i_op == ENC) ? "plain" : "cipher");
643
        for(i=7; i>=0; i--) {
644
                printf("pct[%02d] = %02x\n", i, p_hight_data->i_pct[i]);
645
        }
646
 
647
        printf("\n>>> master key <<<\n");
648
        for(i=15; i>=0; i--) {
649
                printf("mk[%02d] = %02x\n", i, p_hight_data->i_mk[i]);
650
        }
651
 
652
        printf("\n>>> InitialWhiteningFunction result <<<\n");
653
        for(i=7; i>=0; i--) {
654
                printf("iwf[%02d] = %02x ",i, p_hight_data->iwf[i]);
655
                if(p_hight_data->iwf[i] == expected_iwf[i])
656
                        printf("Correct\n");
657
                else
658
                        printf("Wrong (expected = %02x) \n", expected_iwf[i]);
659
        }
660
}
661
 
662
 
663
/* =====================================
664
 
665
    FinalWhiteningFunctionTest()
666
 
667
=======================================*/
668
void FinalWhiteningFunctionTest ()
669
{
670
        byte expected_fwf[8] = {0};
671
        int i_op = -1;
672
        int i;
673
 
674
        printf("\n\n===== FinalWhiteningFunctionTest =====\n\n");
675
 
676
        /////////////////////////////////////////////////
677
        //
678
        // Encryption vector
679
        //
680
        /////////////////////////////////////////////////
681
        printf("\n\n===== Encryption operation =====\n\n");
682
 
683
#if 0
684
        // Test vectors 1
685
        printf("test vectors 1\n");
686
 
687
        i_op = ENC;
688
 
689
        //MasterKey
690
        p_hight_data->i_mk[15]   = 0x00 ;
691
        p_hight_data->i_mk[14]   = 0x11 ;
692
        p_hight_data->i_mk[13]   = 0x22 ;
693
        p_hight_data->i_mk[12]   = 0x33 ;
694
        p_hight_data->i_mk[11]   = 0x44 ;
695
        p_hight_data->i_mk[10]   = 0x55 ;
696
        p_hight_data->i_mk[9]    = 0x66 ;
697
        p_hight_data->i_mk[8]    = 0x77 ;
698
        p_hight_data->i_mk[7]    = 0x88 ;
699
        p_hight_data->i_mk[6]    = 0x99 ;
700
        p_hight_data->i_mk[5]    = 0xaa ;
701
        p_hight_data->i_mk[4]    = 0xbb ;
702
        p_hight_data->i_mk[3]    = 0xcc ;
703
        p_hight_data->i_mk[2]    = 0xdd ;
704
        p_hight_data->i_mk[1]    = 0xee ;
705
        p_hight_data->i_mk[0]    = 0xff;
706
 
707
        // Round32 output
708
        p_hight_data->rf[32][7]  = 0x00;
709
        p_hight_data->rf[32][6]  = 0x38;
710
        p_hight_data->rf[32][5]  = 0x18;
711
        p_hight_data->rf[32][4]  = 0xd1;
712
        p_hight_data->rf[32][3]  = 0xd9;
713
        p_hight_data->rf[32][2]  = 0xa1;
714
        p_hight_data->rf[32][1]  = 0x03;
715
        p_hight_data->rf[32][0]  = 0xf3;
716
 
717
        // expected fwf
718
        expected_fwf[7]          = 0x00;
719
        expected_fwf[6]          = 0xf4;
720
        expected_fwf[5]          = 0x18;
721
        expected_fwf[4]          = 0xae;
722
        expected_fwf[3]          = 0xd9;
723
        expected_fwf[2]          = 0x4f;
724
        expected_fwf[1]          = 0x03;
725
        expected_fwf[0]          = 0xf2;
726
 
727
#elif 0
728
        // Test vectors 2
729
        printf("test vectors 2\n");
730
 
731
        i_op = ENC;
732
 
733
        //MasterKey
734
        p_hight_data->i_mk[15] = 0xff;
735
        p_hight_data->i_mk[14] = 0xee;
736
        p_hight_data->i_mk[13] = 0xdd;
737
        p_hight_data->i_mk[12] = 0xcc;
738
        p_hight_data->i_mk[11] = 0xbb;
739
        p_hight_data->i_mk[10] = 0xaa;
740
        p_hight_data->i_mk[9]  = 0x99;
741
        p_hight_data->i_mk[8]  = 0x88;
742
        p_hight_data->i_mk[7]  = 0x77;
743
        p_hight_data->i_mk[6]  = 0x66;
744
        p_hight_data->i_mk[5]  = 0x55;
745
        p_hight_data->i_mk[4]  = 0x44;
746
        p_hight_data->i_mk[3]  = 0x33;
747
        p_hight_data->i_mk[2]  = 0x22;
748
        p_hight_data->i_mk[1]  = 0x11;
749
        p_hight_data->i_mk[0]  = 0x00;
750
 
751
        //Round32 output
752
        p_hight_data->rf[32][7]  = 0x23;
753
        p_hight_data->rf[32][6]  = 0xfd;
754
        p_hight_data->rf[32][5]  = 0x9f;
755
        p_hight_data->rf[32][4]  = 0x50;
756
        p_hight_data->rf[32][3]  = 0xe5;
757
        p_hight_data->rf[32][2]  = 0x52;
758
        p_hight_data->rf[32][1]  = 0xe6;
759
        p_hight_data->rf[32][0]  = 0xd8;
760
 
761
        // expected fwf
762
        expected_fwf[7]        = 0x23;
763
        expected_fwf[6]        = 0xce;
764
        expected_fwf[5]        = 0x9f;
765
        expected_fwf[4]        = 0x72;
766
        expected_fwf[3]        = 0xe5;
767
        expected_fwf[2]        = 0x43;
768
        expected_fwf[1]        = 0xe6;
769
        expected_fwf[0]        = 0xd8;
770
 
771
#elif 0
772
        // Test vector 3
773
        printf("test vectors 3\n");
774
 
775
        i_op = ENC;
776
 
777
        //MasterKey
778
        p_hight_data->i_mk[15] = 0x00;
779
        p_hight_data->i_mk[14] = 0x01;
780
        p_hight_data->i_mk[13] = 0x02;
781
        p_hight_data->i_mk[12] = 0x03;
782
        p_hight_data->i_mk[11] = 0x04;
783
        p_hight_data->i_mk[10] = 0x05;
784
        p_hight_data->i_mk[9]  = 0x06;
785
        p_hight_data->i_mk[8]  = 0x07;
786
        p_hight_data->i_mk[7]  = 0x08;
787
        p_hight_data->i_mk[6]  = 0x09;
788
        p_hight_data->i_mk[5]  = 0x0a;
789
        p_hight_data->i_mk[4]  = 0x0b;
790
        p_hight_data->i_mk[3]  = 0x0c;
791
        p_hight_data->i_mk[2]  = 0x0d;
792
        p_hight_data->i_mk[1]  = 0x0e;
793
        p_hight_data->i_mk[0]  = 0x0f;
794
 
795
        //Round32 output
796
        p_hight_data->rf[1][7]  = 0x7a;
797
        p_hight_data->rf[1][6]  = 0x63;
798
        p_hight_data->rf[1][5]  = 0xb2;
799
        p_hight_data->rf[1][4]  = 0x95;
800
        p_hight_data->rf[1][3]  = 0x8d;
801
        p_hight_data->rf[1][2]  = 0x2d;
802
        p_hight_data->rf[1][1]  = 0xf4;
803
        p_hight_data->rf[1][0]  = 0x57;
804
 
805
        // expected fwf
806
        expected_fwf[7]        = 0x7a;
807
        expected_fwf[6]        = 0x6f;
808
        expected_fwf[5]        = 0xb2;
809
        expected_fwf[4]        = 0xa2;
810
        expected_fwf[3]        = 0x8d;
811
        expected_fwf[2]        = 0x23;
812
        expected_fwf[1]        = 0xf4;
813
        expected_fwf[0]        = 0x66;
814
 
815
#elif 0
816
        // Test vector 4
817
        printf("test vectors 4\n");
818
 
819
        i_op = ENC;
820
 
821
        //MasterKey
822
        p_hight_data->i_mk[15] = 0x28;
823
        p_hight_data->i_mk[14] = 0xdb;
824
        p_hight_data->i_mk[13] = 0xc3;
825
        p_hight_data->i_mk[12] = 0xbc;
826
        p_hight_data->i_mk[11] = 0x49;
827
        p_hight_data->i_mk[10] = 0xff;
828
        p_hight_data->i_mk[9]  = 0xd8;
829
        p_hight_data->i_mk[8]  = 0x7d;
830
        p_hight_data->i_mk[7]  = 0xcf;
831
        p_hight_data->i_mk[6]  = 0xa5;
832
        p_hight_data->i_mk[5]  = 0x09;
833
        p_hight_data->i_mk[4]  = 0xb1;
834
        p_hight_data->i_mk[3]  = 0x1d;
835
        p_hight_data->i_mk[2]  = 0x42;
836
        p_hight_data->i_mk[1]  = 0x2b;
837
        p_hight_data->i_mk[0]  = 0xe7;
838
 
839
        //Round32 output
840
        p_hight_data->rf[32][7]  = 0xcc;
841
        p_hight_data->rf[32][6]  = 0x19;
842
        p_hight_data->rf[32][5]  = 0x7a;
843
        p_hight_data->rf[32][4]  = 0x33;
844
        p_hight_data->rf[32][3]  = 0x20;
845
        p_hight_data->rf[32][2]  = 0xb7;
846
        p_hight_data->rf[32][1]  = 0x1f;
847
        p_hight_data->rf[32][0]  = 0xdf;
848
 
849
        // expected fwf
850
        expected_fwf[7]        = 0xcc;
851
        expected_fwf[6]        = 0x04;
852
        expected_fwf[5]        = 0x7a;
853
        expected_fwf[4]        = 0x75;
854
        expected_fwf[3]        = 0x20;
855
        expected_fwf[2]        = 0x9c;
856
        expected_fwf[1]        = 0x1f;
857
        expected_fwf[0]        = 0xc6;
858
#endif
859
 
860
/*
861
        /////////////////////////////////////////////////
862
        //
863
        // Decrytion vector
864
        //
865
        /////////////////////////////////////////////////
866
        printf("\n\n===== Decrytion operation =====\n\n");
867
 
868
#if 0
869
 
870
// Test vectors 1
871
        printf("test vectors 1\n");
872
 
873
        i_op = DEC;
874
 
875
        // MasterKey
876
        p_hight_data->i_mk[15] = 0x00;
877
        p_hight_data->i_mk[14] = 0x11;
878
        p_hight_data->i_mk[13] = 0x22;
879
        p_hight_data->i_mk[12] = 0x33;
880
        p_hight_data->i_mk[11] = 0x44;
881
        p_hight_data->i_mk[10] = 0x55;
882
        p_hight_data->i_mk[9]  = 0x66;
883
        p_hight_data->i_mk[8]  = 0x77;
884
        p_hight_data->i_mk[7]  = 0x88;
885
        p_hight_data->i_mk[6]  = 0x99;
886
        p_hight_data->i_mk[5]  = 0xaa;
887
        p_hight_data->i_mk[4]  = 0xbb;
888
        p_hight_data->i_mk[3]  = 0xcc;
889
        p_hight_data->i_mk[2]  = 0xdd;
890
        p_hight_data->i_mk[1]  = 0xee;
891
        p_hight_data->i_mk[0]  = 0xff;
892
 
893
        // CipherText
894
        p_hight_data->i_pct[7]  = 0x00;
895
        p_hight_data->i_pct[6]  = 0xf4;
896
        p_hight_data->i_pct[5]  = 0x18;
897
        p_hight_data->i_pct[4]  = 0xae;
898
        p_hight_data->i_pct[3]  = 0xd9;
899
        p_hight_data->i_pct[2]  = 0x4f;
900
        p_hight_data->i_pct[1]  = 0x03;
901
        p_hight_data->i_pct[0]  = 0xf2;
902
 
903
        // expected fwf
904
        expected_fwf[7]        = 0x00;
905
        expected_fwf[6]        = 0x38;
906
        expected_fwf[5]        = 0x18;
907
        expected_fwf[4]        = 0xd1;
908
        expected_fwf[3]        = 0xd9;
909
        expected_fwf[2]        = 0xa1;
910
        expected_fwf[1]        = 0x03;
911
        expected_fwf[0]        = 0xf3;
912
 
913
#elif 0
914
        // Test vectors 2
915
        printf("test vectors 2\n");
916
 
917
        i_op = DEC;
918
 
919
        // MasterKey
920
        p_hight_data->i_mk[15] = 0xff;
921
        p_hight_data->i_mk[14] = 0xee;
922
        p_hight_data->i_mk[13] = 0xdd;
923
        p_hight_data->i_mk[12] = 0xcc;
924
        p_hight_data->i_mk[11] = 0xbb;
925
        p_hight_data->i_mk[10] = 0xaa;
926
        p_hight_data->i_mk[9]  = 0x99;
927
        p_hight_data->i_mk[8]  = 0x88;
928
        p_hight_data->i_mk[7]  = 0x77;
929
        p_hight_data->i_mk[6]  = 0x66;
930
        p_hight_data->i_mk[5]  = 0x55;
931
        p_hight_data->i_mk[4]  = 0x44;
932
        p_hight_data->i_mk[3]  = 0x33;
933
        p_hight_data->i_mk[2]  = 0x22;
934
        p_hight_data->i_mk[1]  = 0x11;
935
        p_hight_data->i_mk[0]  = 0x00;
936
 
937
        // CipherText
938
        p_hight_data->i_pct[7]  = 0x23;
939
        p_hight_data->i_pct[6]  = 0xce;
940
        p_hight_data->i_pct[5]  = 0x9f;
941
        p_hight_data->i_pct[4]  = 0x72;
942
        p_hight_data->i_pct[3]  = 0xe5;
943
        p_hight_data->i_pct[2]  = 0x43;
944
        p_hight_data->i_pct[1]  = 0xe6;
945
        p_hight_data->i_pct[0]  = 0xd8;
946
 
947
        // expected fwf
948
        expected_fwf[7]        = 0x23;
949
        expected_fwf[6]        = 0xfd;
950
        expected_fwf[5]        = 0x9f;
951
        expected_fwf[4]        = 0x50;
952
        expected_fwf[3]        = 0xe5;
953
        expected_fwf[2]        = 0x52;
954
        expected_fwf[1]        = 0xe6;
955
        expected_fwf[0]        = 0xd8;
956
 
957
#elif 0
958
        // Test vector 3
959
        printf("test vectors 3\n");
960
 
961
        i_op = DEC;
962
 
963
        //MasterKey
964
        p_hight_data->i_mk[15] = 0x00;
965
        p_hight_data->i_mk[14] = 0x01;
966
        p_hight_data->i_mk[13] = 0x02;
967
        p_hight_data->i_mk[12] = 0x03;
968
        p_hight_data->i_mk[11] = 0x04;
969
        p_hight_data->i_mk[10] = 0x05;
970
        p_hight_data->i_mk[9]  = 0x06;
971
        p_hight_data->i_mk[8]  = 0x07;
972
        p_hight_data->i_mk[7]  = 0x08;
973
        p_hight_data->i_mk[6]  = 0x09;
974
        p_hight_data->i_mk[5]  = 0x0a;
975
        p_hight_data->i_mk[4]  = 0x0b;
976
        p_hight_data->i_mk[3]  = 0x0c;
977
        p_hight_data->i_mk[2]  = 0x0d;
978
        p_hight_data->i_mk[1]  = 0x0e;
979
        p_hight_data->i_mk[0]  = 0x0f;
980
 
981
        //CipherText
982
        p_hight_data->i_pct[7]  = 0x7a;
983
        p_hight_data->i_pct[6]  = 0x6f;
984
        p_hight_data->i_pct[5]  = 0xb2;
985
        p_hight_data->i_pct[4]  = 0xa2;
986
        p_hight_data->i_pct[3]  = 0x8d;
987
        p_hight_data->i_pct[2]  = 0x23;
988
        p_hight_data->i_pct[1]  = 0xf4;
989
        p_hight_data->i_pct[0]  = 0x66;
990
 
991
        // expected fwf
992
        expected_fwf[7]        = 0x7a;
993
        expected_fwf[6]        = 0x63;
994
        expected_fwf[5]        = 0xb2;
995
        expected_fwf[4]        = 0x95;
996
        expected_fwf[3]        = 0x8d;
997
        expected_fwf[2]        = 0x2d;
998
        expected_fwf[1]        = 0xf4;
999
        expected_fwf[0]        = 0x57;
1000
 
1001
#elif 1
1002
        // Test vector 4
1003
        printf("test vectors 4\n");
1004
 
1005
        i_op = DEC;
1006
 
1007
        //MasterKey
1008
        p_hight_data->i_mk[15] = 0x28;
1009
        p_hight_data->i_mk[14] = 0xdb;
1010
        p_hight_data->i_mk[13] = 0xc3;
1011
        p_hight_data->i_mk[12] = 0xbc;
1012
        p_hight_data->i_mk[11] = 0x49;
1013
        p_hight_data->i_mk[10] = 0xff;
1014
        p_hight_data->i_mk[9]  = 0xd8;
1015
        p_hight_data->i_mk[8]  = 0x7d;
1016
        p_hight_data->i_mk[7]  = 0xcf;
1017
        p_hight_data->i_mk[6]  = 0xa5;
1018
        p_hight_data->i_mk[5]  = 0x09;
1019
        p_hight_data->i_mk[4]  = 0xb1;
1020
        p_hight_data->i_mk[3]  = 0x1d;
1021
        p_hight_data->i_mk[2]  = 0x42;
1022
        p_hight_data->i_mk[1]  = 0x2b;
1023
        p_hight_data->i_mk[0]  = 0xe7;
1024
 
1025
        //CipherText
1026
        p_hight_data->i_pct[7]  = 0xcc;
1027
        p_hight_data->i_pct[6]  = 0x04;
1028
        p_hight_data->i_pct[5]  = 0x7a;
1029
        p_hight_data->i_pct[4]  = 0x75;
1030
        p_hight_data->i_pct[3]  = 0x20;
1031
        p_hight_data->i_pct[2]  = 0x9c;
1032
        p_hight_data->i_pct[1]  = 0x1f;
1033
        p_hight_data->i_pct[0]  = 0xc6;
1034
 
1035
        // expected fwf
1036
        expected_fwf[7]        = 0xcc;
1037
        expected_fwf[6]        = 0x19;
1038
        expected_fwf[5]        = 0x7a;
1039
        expected_fwf[4]        = 0x33;
1040
        expected_fwf[3]        = 0x20;
1041
        expected_fwf[2]        = 0xb7;
1042
        expected_fwf[1]        = 0x1f;
1043
        expected_fwf[0]        = 0xdf;
1044
#endif
1045
*/
1046
 
1047
 
1048
        WhiteningKeyGen(p_hight_data->i_mk, p_hight_data->wk);
1049
 
1050
        FinalWhiteningFunction(i_op, p_hight_data->rf[32], p_hight_data->wk, p_hight_data->fwf);
1051
 
1052
 
1053
        printf("\n>> operation : %s <<<\n", (i_op == ENC) ? "encryption" : "decrytion");
1054
 
1055
        printf("\n>>> master key <<<\n");
1056
        for(i=15; i>=0; i--) {
1057
                printf("mk[%02d] = %02x\n", i, p_hight_data->i_mk[i]);
1058
        }
1059
 
1060
        printf("\n>>> round32 output <<<\n");
1061
        for(i=7; i>=0; i--) {
1062
                printf("rf[32][%02d] = %02x\n", i, p_hight_data->rf[32][i]);
1063
        }
1064
 
1065
        printf("\n>>> FinalWhiteningFunction result <<<\n");
1066
        for(i=7; i>=0; i--) {
1067
                printf("fwf[%02d] = %02x ",i, p_hight_data->fwf[i]);
1068
                if(p_hight_data->fwf[i] == expected_fwf[i])
1069
                        printf("Correct\n");
1070
                else
1071
                        printf("Wrong (expected = %02x) \n", expected_fwf[i]);
1072
        }
1073
}
1074
 
1075
/* =====================================
1076
 
1077
    InterRoundFunctionTest()
1078
 
1079
=======================================*/
1080
void InterRoundFunctionTest()
1081
{
1082
#define IRF_TV1
1083
//#define IRF_TV2
1084
//#define IRF_TV3
1085
//#define IRF_TV4
1086
 
1087
#define IRF_ENC
1088
//#define IRF_DEC
1089
 
1090
//#define ROUND_FUNCTION_DUMP
1091
 
1092
#ifdef ROUND_FUNCTION_DUMP
1093
        FILE *fp= fopen("roundfunction_4.txt","w+");
1094
#endif
1095
        int i,j;
1096
        int i_op = -1;
1097
        byte expected_rf[32][8];
1098
 
1099
/*
1100
        // F0(X) & F1(X)
1101
        int a, b, c;
1102
 
1103
        a = 0xa1;
1104
        //a = 0xc4;
1105
        //a = 0x48  ;
1106
        //a = 0x8   ;
1107
 
1108
        b = ((a<<1 | a>>7) ^ (a<<2 | a>>6) ^ (a<<7 | a>>1));
1109
        printf("\n\n%02x\n%02x\n" ,a,b);
1110
 
1111
        c = (a<<3 | a>>5) ^ (a<<4 | a>>4) ^ (a<<6 | a>>2);
1112
        printf("%x" ,c);
1113
 
1114
*/
1115
 
1116
        printf("\n\n===== InterRoundFunctionTest =====\n\n");
1117
 
1118
#if defined(IRF_TV1)
1119
        // Test vectors 1
1120
 
1121
        p_hight_data-> sk[3]     = 0xe7;
1122
        p_hight_data-> sk[2]     = 0x13;
1123
        p_hight_data-> sk[1]     = 0x5b;
1124
        p_hight_data-> sk[0]     = 0x59;
1125
 
1126
        p_hight_data-> sk[7]     = 0xc9;
1127
        p_hight_data-> sk[6]     = 0x9c;
1128
        p_hight_data-> sk[5]     = 0xb0;
1129
        p_hight_data-> sk[4]     = 0xc8;
1130
 
1131
        p_hight_data-> sk[11]    = 0x90;
1132
        p_hight_data-> sk[10]    = 0x6d;
1133
        p_hight_data-> sk[9]     = 0x96;
1134
        p_hight_data-> sk[8]     = 0xd7;
1135
 
1136
        p_hight_data-> sk[15]    = 0x2c;
1137
        p_hight_data-> sk[14]    = 0x6a;
1138
        p_hight_data-> sk[13]    = 0x55;
1139
        p_hight_data-> sk[12]    = 0x99;
1140
 
1141
        p_hight_data-> sk[19]    = 0x27;
1142
        p_hight_data-> sk[18]    = 0x03;
1143
        p_hight_data-> sk[17]    = 0x2a;
1144
        p_hight_data-> sk[16]    = 0xde;
1145
 
1146
        p_hight_data-> sk[23]    = 0xb5;
1147
        p_hight_data-> sk[22]    = 0xe3;
1148
        p_hight_data-> sk[21]    = 0x2d;
1149
        p_hight_data-> sk[20]    = 0x31;
1150
 
1151
        p_hight_data-> sk[27]    = 0xce;
1152
        p_hight_data-> sk[26]    = 0xd9;
1153
        p_hight_data-> sk[25]    = 0xde;
1154
        p_hight_data-> sk[24]    = 0x4e;
1155
 
1156
        p_hight_data-> sk[31]    = 0x48;
1157
        p_hight_data-> sk[30]    = 0x91;
1158
        p_hight_data-> sk[29]    = 0x91;
1159
        p_hight_data-> sk[28]    = 0x80;
1160
 
1161
        p_hight_data-> sk[35]    = 0xf9;
1162
        p_hight_data-> sk[34]    = 0x15;
1163
        p_hight_data-> sk[33]    = 0xb5;
1164
        p_hight_data-> sk[32]    = 0xf4;
1165
 
1166
        p_hight_data-> sk[39]    = 0xfa;
1167
        p_hight_data-> sk[38]    = 0xdc;
1168
        p_hight_data-> sk[37]    = 0x0e;
1169
        p_hight_data-> sk[36]    = 0xe2;
1170
 
1171
        p_hight_data-> sk[43]    = 0xbb;
1172
        p_hight_data-> sk[42]    = 0xa1;
1173
        p_hight_data-> sk[41]    = 0x54;
1174
        p_hight_data-> sk[40]    = 0x39;
1175
 
1176
        p_hight_data-> sk[47]    = 0x9f;
1177
        p_hight_data-> sk[46]    = 0xad;
1178
        p_hight_data-> sk[45]    = 0xb9;
1179
        p_hight_data-> sk[44]    = 0xbf;
1180
 
1181
        p_hight_data-> sk[51]    = 0x16;
1182
        p_hight_data-> sk[50]    = 0xb7;
1183
        p_hight_data-> sk[49]    = 0xf8;
1184
        p_hight_data-> sk[48]    = 0xe8;
1185
 
1186
        p_hight_data-> sk[55]    = 0xe4;
1187
        p_hight_data-> sk[54]    = 0x1e;
1188
        p_hight_data-> sk[53]    = 0x02;
1189
        p_hight_data-> sk[52]    = 0x39;
1190
 
1191
        p_hight_data-> sk[59]    = 0xd9;
1192
        p_hight_data-> sk[58]    = 0x45;
1193
        p_hight_data-> sk[57]    = 0x1b;
1194
        p_hight_data-> sk[56]    = 0x36;
1195
 
1196
        p_hight_data-> sk[63]    = 0xa9;
1197
        p_hight_data-> sk[62]    = 0xb0;
1198
        p_hight_data-> sk[61]    = 0xad;
1199
        p_hight_data-> sk[60]    = 0x97;
1200
 
1201
        p_hight_data-> sk[67]    = 0xcf;
1202
        p_hight_data-> sk[66]    = 0xa7;
1203
        p_hight_data-> sk[65]    = 0xc7;
1204
        p_hight_data-> sk[64]    = 0xf6;
1205
 
1206
        p_hight_data-> sk[71]    = 0x48;
1207
        p_hight_data-> sk[70]    = 0x55;
1208
        p_hight_data-> sk[69]    = 0x5f;
1209
        p_hight_data-> sk[68]    = 0x62;
1210
 
1211
        p_hight_data-> sk[75]    = 0x1f;
1212
        p_hight_data-> sk[74]    = 0x50;
1213
        p_hight_data-> sk[73]    = 0xa1;
1214
        p_hight_data-> sk[72]    = 0xb1;
1215
 
1216
        p_hight_data-> sk[79]    = 0xa5;
1217
        p_hight_data-> sk[78]    = 0x98;
1218
        p_hight_data-> sk[77]    = 0x6d;
1219
        p_hight_data-> sk[76]    = 0x86;
1220
 
1221
        p_hight_data-> sk[83]    = 0x07;
1222
        p_hight_data-> sk[82]    = 0x06;
1223
        p_hight_data-> sk[81]    = 0xf3;
1224
        p_hight_data-> sk[80]    = 0x3c;
1225
 
1226
        p_hight_data-> sk[87]    = 0xfb;
1227
        p_hight_data-> sk[86]    = 0x2b;
1228
        p_hight_data-> sk[85]    = 0x7a;
1229
        p_hight_data-> sk[84]    = 0xff;
1230
 
1231
        p_hight_data-> sk[91]    = 0x7a;
1232
        p_hight_data-> sk[90]    = 0x75;
1233
        p_hight_data-> sk[89]    = 0x5a;
1234
        p_hight_data-> sk[88]    = 0x93;
1235
 
1236
        p_hight_data-> sk[95]    = 0x7b;
1237
        p_hight_data-> sk[94]    = 0xb3;
1238
        p_hight_data-> sk[93]    = 0x91;
1239
        p_hight_data-> sk[92]    = 0x34;
1240
 
1241
        p_hight_data-> sk[99]    = 0xbc;
1242
        p_hight_data-> sk[98]    = 0xdf;
1243
        p_hight_data-> sk[97]    = 0x15;
1244
        p_hight_data-> sk[96]    = 0xf0;
1245
 
1246
        p_hight_data-> sk[103]   = 0xef;
1247
        p_hight_data-> sk[102]   = 0x01;
1248
        p_hight_data-> sk[101]   = 0x8c;
1249
        p_hight_data-> sk[100]   = 0xa2;
1250
 
1251
        p_hight_data-> sk[107]   = 0x2a;
1252
        p_hight_data-> sk[106]   = 0x43;
1253
        p_hight_data-> sk[105]   = 0x64;
1254
        p_hight_data-> sk[104]   = 0x95;
1255
 
1256
        p_hight_data-> sk[111]   = 0xae;
1257
        p_hight_data-> sk[110]   = 0x88;
1258
        p_hight_data-> sk[109]   = 0x22;
1259
        p_hight_data-> sk[108]   = 0x55;
1260
 
1261
        p_hight_data-> sk[115]   = 0xc7;
1262
        p_hight_data-> sk[114]   = 0xe5;
1263
        p_hight_data-> sk[113]   = 0x0f;
1264
        p_hight_data-> sk[112]   = 0x52;
1265
 
1266
        p_hight_data-> sk[119]   = 0x67;
1267
        p_hight_data-> sk[118]   = 0xd9;
1268
        p_hight_data-> sk[117]   = 0xbc;
1269
        p_hight_data-> sk[116]   = 0xf0;
1270
 
1271
        p_hight_data-> sk[123]   = 0x61;
1272
        p_hight_data-> sk[122]   = 0xa1;
1273
        p_hight_data-> sk[121]   = 0x8f;
1274
        p_hight_data-> sk[120]   = 0xda;
1275
 
1276
        p_hight_data-> sk[127]   = 0xd1;
1277
        p_hight_data-> sk[126]   = 0x35;
1278
        p_hight_data-> sk[125]   = 0x7c;
1279
        p_hight_data-> sk[124]   = 0x79;
1280
 
1281
        expected_rf[1][7]        = 0x00;
1282
        expected_rf[1][6]        = 0xce;
1283
        expected_rf[1][5]        = 0x11;
1284
        expected_rf[1][4]        = 0x38;
1285
        expected_rf[1][3]        = 0x22;
1286
        expected_rf[1][2]        = 0x3f;
1287
        expected_rf[1][1]        = 0x33;
1288
        expected_rf[1][0]        = 0xe7;
1289
 
1290
        expected_rf[2][7]        = 0xce;
1291
        expected_rf[2][6]        = 0xe1;
1292
        expected_rf[2][5]        = 0x38;
1293
        expected_rf[2][4]        = 0xef;
1294
        expected_rf[2][3]        = 0x3f;
1295
        expected_rf[2][2]        = 0xa3;
1296
        expected_rf[2][1]        = 0xe7;
1297
        expected_rf[2][0]        = 0x8a;
1298
 
1299
        expected_rf[3][7]        = 0xe1;
1300
        expected_rf[3][6]        = 0x4f;
1301
        expected_rf[3][5]        = 0xef;
1302
        expected_rf[3][4]        = 0x91;
1303
        expected_rf[3][3]        = 0xa3;
1304
        expected_rf[3][2]        = 0x70;
1305
        expected_rf[3][1]        = 0x8a;
1306
        expected_rf[3][0]        = 0x8a;
1307
 
1308
        expected_rf[4][7]        = 0x4f;
1309
        expected_rf[4][6]        = 0x8a;
1310
        expected_rf[4][5]        = 0x91;
1311
        expected_rf[4][4]        = 0xcd;
1312
        expected_rf[4][3]        = 0x70;
1313
        expected_rf[4][2]        = 0x51;
1314
        expected_rf[4][1]        = 0x8a;
1315
        expected_rf[4][0]        = 0xd1;
1316
 
1317
        expected_rf[5][7]        = 0x8a;
1318
        expected_rf[5][6]        = 0x53;
1319
        expected_rf[5][5]        = 0xcd;
1320
        expected_rf[5][4]        = 0x09;
1321
        expected_rf[5][3]        = 0x51;
1322
        expected_rf[5][2]        = 0xc3;
1323
        expected_rf[5][1]        = 0xd1;
1324
        expected_rf[5][0]        = 0xee;
1325
 
1326
        expected_rf[6][7]        = 0x53;
1327
        expected_rf[6][6]        = 0x46;
1328
        expected_rf[6][5]        = 0x09;
1329
        expected_rf[6][4]        = 0xc7;
1330
        expected_rf[6][3]        = 0xc3;
1331
        expected_rf[6][2]        = 0xe4;
1332
        expected_rf[6][1]        = 0xee;
1333
        expected_rf[6][0]        = 0x7d;
1334
 
1335
        expected_rf[7][7]        = 0x46;
1336
        expected_rf[7][6]        = 0x73;
1337
        expected_rf[7][5]        = 0xc7;
1338
        expected_rf[7][4]        = 0xc5;
1339
        expected_rf[7][3]        = 0xe4;
1340
        expected_rf[7][2]        = 0x1b;
1341
        expected_rf[7][1]        = 0x7d;
1342
        expected_rf[7][0]        = 0xd7;
1343
 
1344
        expected_rf[8][7]        = 0x73;
1345
        expected_rf[8][6]        = 0x59;
1346
        expected_rf[8][5]        = 0xc5;
1347
        expected_rf[8][4]        = 0x8c;
1348
        expected_rf[8][3]        = 0x1b;
1349
        expected_rf[8][2]        = 0x33;
1350
        expected_rf[8][1]        = 0xd7;
1351
        expected_rf[8][0]        = 0x9c;
1352
 
1353
        expected_rf[9][7]        = 0x59;
1354
        expected_rf[9][6]        = 0x5f;
1355
        expected_rf[9][5]        = 0x8c;
1356
        expected_rf[9][4]        = 0xf3;
1357
        expected_rf[9][3]        = 0x33;
1358
        expected_rf[9][2]        = 0xd5;
1359
        expected_rf[9][1]        = 0x9c;
1360
        expected_rf[9][0]        = 0x07;
1361
 
1362
        expected_rf[10][7]       = 0x5f;
1363
        expected_rf[10][6]       = 0x0c;
1364
        expected_rf[10][5]       = 0xf3;
1365
        expected_rf[10][4]       = 0x17;
1366
        expected_rf[10][3]       = 0xd5;
1367
        expected_rf[10][2]       = 0x07;
1368
        expected_rf[10][1]       = 0x07;
1369
        expected_rf[10][0]       = 0x3f;
1370
 
1371
        expected_rf[11][7]       = 0x0c;
1372
        expected_rf[11][6]       = 0xa0;
1373
        expected_rf[11][5]       = 0x17;
1374
        expected_rf[11][4]       = 0x30;
1375
        expected_rf[11][3]       = 0x07;
1376
        expected_rf[11][2]       = 0x03;
1377
        expected_rf[11][1]       = 0x3f;
1378
        expected_rf[11][0]       = 0xb6;
1379
 
1380
        expected_rf[12][7]       = 0xa0;
1381
        expected_rf[12][6]       = 0x3a;
1382
        expected_rf[12][5]       = 0x30;
1383
        expected_rf[12][4]       = 0x43;
1384
        expected_rf[12][3]       = 0x03;
1385
        expected_rf[12][2]       = 0x0b;
1386
        expected_rf[12][1]       = 0xb6;
1387
        expected_rf[12][0]       = 0x3e;
1388
 
1389
        expected_rf[13][7]       = 0x3a;
1390
        expected_rf[13][6]       = 0x79;
1391
        expected_rf[13][5]       = 0x43;
1392
        expected_rf[13][4]       = 0xb4;
1393
        expected_rf[13][3]       = 0x0b;
1394
        expected_rf[13][2]       = 0x2b;
1395
        expected_rf[13][1]       = 0x3e;
1396
        expected_rf[13][0]       = 0x37;
1397
 
1398
        expected_rf[14][7]       = 0x79;
1399
        expected_rf[14][6]       = 0x20;
1400
        expected_rf[14][5]       = 0xb4;
1401
        expected_rf[14][4]       = 0x7a;
1402
        expected_rf[14][3]       = 0x2b;
1403
        expected_rf[14][2]       = 0x7c;
1404
        expected_rf[14][1]       = 0x37;
1405
        expected_rf[14][0]       = 0xb5;
1406
 
1407
        expected_rf[15][7]       = 0x20;
1408
        expected_rf[15][6]       = 0x63;
1409
        expected_rf[15][5]       = 0x7a;
1410
        expected_rf[15][4]       = 0x79;
1411
        expected_rf[15][3]       = 0x7c;
1412
        expected_rf[15][2]       = 0xe4;
1413
        expected_rf[15][1]       = 0xb5;
1414
        expected_rf[15][0]       = 0xd0;
1415
 
1416
        expected_rf[16][7]       = 0x63;
1417
        expected_rf[16][6]       = 0x2c;
1418
        expected_rf[16][5]       = 0x79;
1419
        expected_rf[16][4]       = 0xa9;
1420
        expected_rf[16][3]       = 0xe4;
1421
        expected_rf[16][2]       = 0xdd;
1422
        expected_rf[16][1]       = 0xd0;
1423
        expected_rf[16][0]       = 0x83;
1424
 
1425
        expected_rf[17][7]       = 0x2c;
1426
        expected_rf[17][6]       = 0x93;
1427
        expected_rf[17][5]       = 0xa9;
1428
        expected_rf[17][4]       = 0x0d;
1429
        expected_rf[17][3]       = 0xdd;
1430
        expected_rf[17][2]       = 0x02;
1431
        expected_rf[17][1]       = 0x83;
1432
        expected_rf[17][0]       = 0xae;
1433
 
1434
        expected_rf[18][7]       = 0x93;
1435
        expected_rf[18][6]       = 0x57;
1436
        expected_rf[18][5]       = 0x0d;
1437
        expected_rf[18][4]       = 0xb1;
1438
        expected_rf[18][3]       = 0x02;
1439
        expected_rf[18][2]       = 0xd9;
1440
        expected_rf[18][1]       = 0xae;
1441
        expected_rf[18][0]       = 0xc4;
1442
 
1443
        expected_rf[19][7]       = 0x57;
1444
        expected_rf[19][6]       = 0xb7;
1445
        expected_rf[19][5]       = 0xb1;
1446
        expected_rf[19][4]       = 0xdb;
1447
        expected_rf[19][3]       = 0xd9;
1448
        expected_rf[19][2]       = 0x98;
1449
        expected_rf[19][1]       = 0xc4;
1450
        expected_rf[19][0]       = 0xe4;
1451
 
1452
        expected_rf[20][7]       = 0xb7;
1453
        expected_rf[20][6]       = 0xbe;
1454
        expected_rf[20][5]       = 0xdb;
1455
        expected_rf[20][4]       = 0x55;
1456
        expected_rf[20][3]       = 0x98;
1457
        expected_rf[20][2]       = 0x9a;
1458
        expected_rf[20][1]       = 0xe4;
1459
        expected_rf[20][0]       = 0x58;
1460
 
1461
        expected_rf[21][7]       = 0xbe;
1462
        expected_rf[21][6]       = 0x87;
1463
        expected_rf[21][5]       = 0x55;
1464
        expected_rf[21][4]       = 0x9d;
1465
        expected_rf[21][3]       = 0x9a;
1466
        expected_rf[21][2]       = 0x51;
1467
        expected_rf[21][1]       = 0x58;
1468
        expected_rf[21][0]       = 0x68;
1469
 
1470
        expected_rf[22][7]       = 0x87;
1471
        expected_rf[22][6]       = 0xce;
1472
        expected_rf[22][5]       = 0x9d;
1473
        expected_rf[22][4]       = 0x53;
1474
        expected_rf[22][3]       = 0x51;
1475
        expected_rf[22][2]       = 0x78;
1476
        expected_rf[22][1]       = 0x68;
1477
        expected_rf[22][0]       = 0x73;
1478
 
1479
        expected_rf[23][7]       = 0xce;
1480
        expected_rf[23][6]       = 0xab;
1481
        expected_rf[23][5]       = 0x53;
1482
        expected_rf[23][4]       = 0xd6;
1483
        expected_rf[23][3]       = 0x78;
1484
        expected_rf[23][2]       = 0x4b;
1485
        expected_rf[23][1]       = 0x73;
1486
        expected_rf[23][0]       = 0xbc;
1487
 
1488
        expected_rf[24][7]       = 0xab;
1489
        expected_rf[24][6]       = 0x30;
1490
        expected_rf[24][5]       = 0xd6;
1491
        expected_rf[24][4]       = 0xd7;
1492
        expected_rf[24][3]       = 0x4b;
1493
        expected_rf[24][2]       = 0xa8;
1494
        expected_rf[24][1]       = 0xbc;
1495
        expected_rf[24][0]       = 0x69;
1496
 
1497
        expected_rf[25][7]       = 0x30;
1498
        expected_rf[25][6]       = 0xbf;
1499
        expected_rf[25][5]       = 0xd7;
1500
        expected_rf[25][4]       = 0xf7;
1501
        expected_rf[25][3]       = 0xa8;
1502
        expected_rf[25][2]       = 0x33;
1503
        expected_rf[25][1]       = 0x69;
1504
        expected_rf[25][0]       = 0xdf;
1505
 
1506
        expected_rf[26][7]       = 0xbf;
1507
        expected_rf[26][6]       = 0x13;
1508
        expected_rf[26][5]       = 0xf7;
1509
        expected_rf[26][4]       = 0x17;
1510
        expected_rf[26][3]       = 0x33;
1511
        expected_rf[26][2]       = 0xbf;
1512
        expected_rf[26][1]       = 0xdf;
1513
        expected_rf[26][0]       = 0x7d;
1514
 
1515
        expected_rf[27][7]       = 0x13;
1516
        expected_rf[27][6]       = 0x46;
1517
        expected_rf[27][5]       = 0x17;
1518
        expected_rf[27][4]       = 0xf1;
1519
        expected_rf[27][3]       = 0xbf;
1520
        expected_rf[27][2]       = 0xd5;
1521
        expected_rf[27][1]       = 0x7d;
1522
        expected_rf[27][0]       = 0xb2;
1523
 
1524
        expected_rf[28][7]       = 0x46;
1525
        expected_rf[28][6]       = 0x7b;
1526
        expected_rf[28][5]       = 0xf1;
1527
        expected_rf[28][4]       = 0x87;
1528
        expected_rf[28][3]       = 0xd5;
1529
        expected_rf[28][2]       = 0xc4;
1530
        expected_rf[28][1]       = 0xb2;
1531
        expected_rf[28][0]       = 0x77;
1532
 
1533
        expected_rf[29][7]       = 0x7b;
1534
        expected_rf[29][6]       = 0x31;
1535
        expected_rf[29][5]       = 0x87;
1536
        expected_rf[29][4]       = 0xd2;
1537
        expected_rf[29][3]       = 0xc4;
1538
        expected_rf[29][2]       = 0xf5;
1539
        expected_rf[29][1]       = 0x77;
1540
        expected_rf[29][0]       = 0x2b;
1541
 
1542
        expected_rf[30][7]       = 0x31;
1543
        expected_rf[30][6]       = 0x5d;
1544
        expected_rf[30][5]       = 0xd2;
1545
        expected_rf[30][4]       = 0x46;
1546
        expected_rf[30][3]       = 0xf5;
1547
        expected_rf[30][2]       = 0x48;
1548
        expected_rf[30][1]       = 0x2b;
1549
        expected_rf[30][0]       = 0xde;
1550
 
1551
        expected_rf[31][7]       = 0x5d;
1552
        expected_rf[31][6]       = 0x38;
1553
        expected_rf[31][5]       = 0x46;
1554
        expected_rf[31][4]       = 0xd1;
1555
        expected_rf[31][3]       = 0x48;
1556
        expected_rf[31][2]       = 0xa1;
1557
        expected_rf[31][1]       = 0xde;
1558
        expected_rf[31][0]       = 0xf3;
1559
 
1560
#elif defined(IRF_TV2)
1561
        printf("IRF_TV2");
1562
 
1563
#elif defined(IRF_TV3)
1564
        printf("IRF_TV3");
1565
 
1566
#elif defined(IRF_TV4)
1567
        printf("IRF_TV4");
1568
 
1569
#endif
1570
 
1571
 
1572
 
1573
#if defined(IRF_ENC)
1574
        /////////////////////////////////////////////////
1575
        //
1576
        // Encryption vector
1577
        //
1578
        /////////////////////////////////////////////////
1579
        printf("\n\n===== Encryption operation =====\n\n");
1580
 
1581
#if defined(IRF_TV1)
1582
 
1583
        // Test vectors 1
1584
        printf("test vectors 1\n");
1585
 
1586
        i_op = ENC;
1587
 
1588
        p_hight_data-> iwf[7]    = 0x00;
1589
        p_hight_data-> iwf[6]    = 0x00;
1590
        p_hight_data-> iwf[5]    = 0x00;
1591
        p_hight_data-> iwf[4]    = 0x11;
1592
        p_hight_data-> iwf[3]    = 0x00;
1593
        p_hight_data-> iwf[2]    = 0x22;
1594
        p_hight_data-> iwf[1]    = 0x00;
1595
        p_hight_data-> iwf[0]    = 0x33;
1596
 
1597
#elif defined(IRF_TV2)
1598
        printf("IRF_TV2");
1599
 
1600
#elif defined(IRF_TV3)
1601
        printf("IRF_TV3");
1602
 
1603
#elif defined(IRF_TV4)
1604
        printf("IRF_TV4");
1605
 
1606
#endif
1607
#endif
1608
 
1609
#if defined(IRF_DEC) && !defined(IRF_ENC)
1610
        /////////////////////////////////////////////////
1611
        //
1612
        // Decrytion vector
1613
        //
1614
        /////////////////////////////////////////////////
1615
 
1616
        printf("\n\n===== Decrytion operation =====\n\n");
1617
 
1618
#if defined(IRF_TV1)
1619
        // Test vectors 1
1620
        printf("test vectors 1\n");
1621
 
1622
        i_op = DEC;
1623
 
1624
        /* ??
1625
        p_hight_data-> fwf[7]   = ;
1626
        p_hight_data-> fwf[6]   = ;
1627
        p_hight_data-> fwf[5]   = ;
1628
        p_hight_data-> fwf[4]   = ;
1629
        p_hight_data-> fwf[3]   = ;
1630
        p_hight_data-> fwf[2]   = ;
1631
        p_hight_data-> fwf[1]   = ;
1632
        p_hight_data-> fwf[0]   = ;
1633
        */
1634
 
1635
#elif defined(IRF_TV2)
1636
        printf("IRF_TV2");
1637
 
1638
#elif defined(IRF_TV3)
1639
        printf("IRF_TV3");
1640
 
1641
#elif defined(IRF_TV4)
1642
        printf("IRF_TV4");
1643
 
1644
#endif
1645
#endif
1646
 
1647
        /////////////////////////////////////////////////
1648
        //
1649
        // Run test
1650
        //
1651
        /////////////////////////////////////////////////
1652
        if(i_op == -1) {
1653
                printf("Wrong operation\n");
1654
                printf("You should set operation mode for ENC or DEC");
1655
                return;
1656
        }
1657
 
1658
        // RoundFunction1
1659
        if(i_op == ENC) {
1660
                InterRoundFunction(i_op,
1661
                                                        p_hight_data->iwf,
1662
                                                        p_hight_data->sk,
1663
                                                        p_hight_data->rf[1]);
1664
        } else { // DEC
1665
 
1666
 
1667
                InterRoundFunction(i_op,
1668
                                                        p_hight_data->fwf,
1669
                                                        p_hight_data->sk,
1670
                                                        p_hight_data->rf[1]);
1671
        }
1672
 
1673
        // RoundFunction2~31
1674
        for(i=1; i<31; i++){
1675
                if(i_op == ENC) {
1676
                        InterRoundFunction(i_op,
1677
                                                                p_hight_data->rf[i],
1678
                                                                p_hight_data->sk+(i*4),
1679
                                                                p_hight_data->rf[i+1]);
1680
                } else { // DEC
1681
                        InterRoundFunction(i_op,
1682
                                                                p_hight_data->rf[i],
1683
                                                                p_hight_data->sk+((32-i)*4),
1684
                                                                p_hight_data->rf[i+1]);
1685
                }
1686
        }
1687
 
1688
 
1689
        // RoundFunction 1 
1690
        printf("\n>> operation : %s <<<\n", (i_op == ENC) ? "encryption" : "decrytion");
1691
#ifdef ROUND_FUNCTION_DUMP
1692
        fprintf(fp,"\n>> operation : %s <<<\n", (i_op == ENC) ? "encryption" : "decrytion");
1693
#endif
1694
 
1695
        printf("\n\n===== RoundFunction 1 =====\n\n");
1696
#ifdef ROUND_FUNCTION_DUMP
1697
        fprintf(fp,"\n\n===== RoundFunction 1 =====\n\n");
1698
#endif
1699
 
1700
        printf("\n>>> InitialWhiteningFunction  <<<\n");
1701
#ifdef ROUND_FUNCTION_DUMP
1702
        fprintf(fp,"\n>>> InitialWhiteningFunction  <<<\n");
1703
#endif
1704
 
1705
        for(i=7; i>=0; i--){
1706
                printf("iwf[%02d] = %02x\n",i, p_hight_data->iwf[i]);
1707
#ifdef ROUND_FUNCTION_DUMP
1708
                fprintf(fp,"iwf[%02d] = %02x\n",i, p_hight_data->iwf[i]);
1709
#endif
1710
        }
1711
 
1712
        printf("\n>>> Sub Key <<<\n");
1713
#ifdef ROUND_FUNCTION_DUMP
1714
        fprintf(fp,"\n>>> Sub Key <<<\n");
1715
#endif
1716
 
1717
        for(i=3; i>=0; i--){
1718
                printf("sk[%02d] = %02x\n", i, p_hight_data->sk[i]);
1719
#ifdef ROUND_FUNCTION_DUMP
1720
                fprintf(fp,"sk[%02d] = %02x\n", i, p_hight_data->sk[i]);
1721
#endif
1722
        }
1723
 
1724
        printf("\n>>> Round #1 Result  <<<\n");
1725
#ifdef ROUND_FUNCTION_DUMP
1726
        fprintf(fp,"\n>>> Round #1 Result  <<<\n");
1727
#endif
1728
 
1729
        for(i=7; i>=0; i--){
1730
        if((i_op == ENC) ? p_hight_data->rf[1][i] == expected_rf[1][i]:
1731
                                                   p_hight_data->rf[1][i] == expected_rf[31][i]){
1732
                        printf("rf[1][%02d] = %02x : Correct\n", i, p_hight_data->rf[1][i]);
1733
 
1734
#ifdef ROUND_FUNCTION_DUMP
1735
                        fprintf(fp,"rf[1][%02d] = %02x : Correct\n", i, p_hight_data->rf[1][i]);
1736
#endif
1737
                } else {
1738
                        printf("rf[1][%02d] = %02x : Wrong (expected = %02x) \n",i, p_hight_data->rf[1][i],
1739
                                                                                                                                        (i_op == ENC) ? expected_rf[1][i]: expected_rf[31][i]);
1740
 
1741
#ifdef ROUND_FUNCTION_DUMP
1742
                        fprintf(fp,"rf[1][%02d] : Wrong (expected = %02x) \n",i, (i_op == ENC) ? expected_rf[1][i]:
1743
                                                                                                     expected_rf[31][i]);
1744
#endif  
1745
                }
1746
        }
1747
 
1748
        // RoundFunction 2~31
1749
        for(i=2; i<32; i++){
1750
 
1751
                printf("\n\n===== RoundFunction %d =====\n\n",i);
1752
#ifdef ROUND_FUNCTION_DUMP
1753
                fprintf(fp,"\n\n===== RoundFunction %d =====\n\n",i);
1754
#endif
1755
 
1756
                printf("\n>>> Round #%d Result <<<\n", i-1);
1757
#ifdef ROUND_FUNCTION_DUMP
1758
                fprintf(fp,"\n>>> Round #%d Result <<<\n", i-1);
1759
#endif
1760
 
1761
                for(j=7; j>=0; j--){
1762
                        printf("rf[%02d][%02d] = %02x\n",i-1, j, p_hight_data->rf[i-1][j]);
1763
#ifdef ROUND_FUNCTION_DUMP
1764
                        fprintf(fp,"rf[%02d][%02d] = %02x\n",i-1, j, p_hight_data->rf[i-1][j]);
1765
#endif
1766
                }
1767
 
1768
                printf("\n>>> Sub Key <<<\n");
1769
#ifdef ROUND_FUNCTION_DUMP
1770
                fprintf(fp,"\n>>> Sub Key <<<\n");
1771
#endif
1772
                printf("\n sk[%02d] = %02x\n sk[%02d] = %02x\n sk[%02d] = %02x\n sk[%02d] = %02x\n  ",
1773
                                (i-1)*4+3, p_hight_data->sk[(i-1)*4+3],(i-1)*4+2, p_hight_data->sk[(i-1)*4+2],(i-1)*4+1, p_hight_data->sk[(i-1)*4+1],(i-1)*4, p_hight_data->sk[(i-1)*4]);
1774
#ifdef ROUND_FUNCTION_DUMP
1775
                fprintf(fp,"\n sk[%02d] = %02x\n sk[%02d] = %02x\n sk[%02d] = %02x\n sk[%02d] = %02x\n  ",
1776
                                (i-1)*4+3, p_hight_data->sk[(i-1)*4+3],(i-1)*4+2, p_hight_data->sk[(i-1)*4+2],(i-1)*4+1, p_hight_data->sk[(i-1)*4+1],(i-1)*4, p_hight_data->sk[(i-1)*4]);
1777
#endif
1778
                printf("\n>>> Round #%d Result  <<<\n",i);
1779
#ifdef ROUND_FUNCTION_DUMP
1780
                fprintf(fp,"\n>>> Round #%d Result  <<<\n",i);
1781
#endif
1782
 
1783
                for(j=7; j>=0; j--){
1784
                        if(p_hight_data->rf[i][j] == expected_rf[i][j]){
1785
                                printf("rf[%02d][%02d] = %02x : Correct\n", i,j,p_hight_data->rf[i][j]);
1786
#ifdef ROUND_FUNCTION_DUMP
1787
                                fprintf(fp,"rf[%02d][%02d] = %02x : Correct\n", i,j,p_hight_data->rf[i][j]);
1788
#endif
1789
                        } else {
1790
                                printf("rf[%02d][%02d] : Wrong (expected = %02x) \n", i,j,expected_rf[i][j]);
1791
#ifdef ROUND_FUNCTION_DUMP
1792
                                fprintf(fp,"rf[%02d][%02d] : Wrong (expected = %02x) \n", i,j,expected_rf[i][j]);
1793
#endif 
1794
                        }
1795
                }
1796
        }
1797
 
1798
 
1799
#if defined(IRF_DEC)
1800
        // RoundFunction32
1801
        FinalRoundFunction(i_op,
1802
                                                p_hight_data->rf[31],
1803
                                                p_hight_data->sk,
1804
                                                p_hight_data->rf[32]);
1805
 
1806
        printf("\n>>> Round 32 Result  <<<\n");
1807
 
1808
        for(i=7; i>=0; i--){
1809
                        printf("rf[32][%02d] = %02x\n" , i, p_hight_data->rf[32][i]);
1810
        }
1811
#endif
1812
 
1813
#ifdef ROUND_FUNCTION_DUMP
1814
        fclose(fp);
1815
#endif
1816
}
1817
 
1818
 
1819
/* =====================================
1820
 
1821
    FinalRoundFunctionTest ()
1822
 
1823
=======================================*/
1824
void FinalRoundFunctionTest()
1825
{
1826
        int i;
1827
        byte expected_rf[33][8];
1828
 
1829
 
1830
        printf("\n\n===== FinalRoundFunctionTest =====\n\n");
1831
 
1832
#if 0
1833
 
1834
        // Test vector 1
1835
        printf("test vectors 1\n");
1836
 
1837
        p_hight_data->rf[31][7]   = 0x5d;
1838
        p_hight_data->rf[31][6]   = 0x38;
1839
        p_hight_data->rf[31][5]   = 0x46;
1840
        p_hight_data->rf[31][4]   = 0xd1;
1841
        p_hight_data->rf[31][3]   = 0x48;
1842
        p_hight_data->rf[31][2]   = 0xa1;
1843
        p_hight_data->rf[31][1]   = 0xde;
1844
        p_hight_data->rf[31][0]   = 0xf3;
1845
 
1846
        p_hight_data-> sk[127]    = 0xd1;
1847
        p_hight_data-> sk[126]    = 0x35;
1848
        p_hight_data-> sk[125]    = 0x7c;
1849
        p_hight_data-> sk[124]    = 0x79;
1850
 
1851
        expected_rf[32][7]        = 0x00;
1852
        expected_rf[32][6]        = 0x38;
1853
        expected_rf[32][5]        = 0x18;
1854
        expected_rf[32][4]        = 0xd1;
1855
        expected_rf[32][3]        = 0xd9;
1856
        expected_rf[32][2]        = 0xa1;
1857
        expected_rf[32][1]        = 0x03;
1858
        expected_rf[32][0]        = 0xf3;
1859
 
1860
#elif 0
1861
        // Test vector 2
1862
        printf("test vectors 2\n");
1863
 
1864
        p_hight_data->rf[31][7]   = 0xf7;
1865
        p_hight_data->rf[31][6]   = 0xfd;
1866
        p_hight_data->rf[31][5]   = 0xf8;
1867
        p_hight_data->rf[31][4]   = 0x50;
1868
        p_hight_data->rf[31][3]   = 0xf8;
1869
        p_hight_data->rf[31][2]   = 0x52;
1870
        p_hight_data->rf[31][1]   = 0x9d;
1871
        p_hight_data->rf[31][0]   = 0xd8;
1872
 
1873
        p_hight_data-> sk[127]    = 0xe2;
1874
        p_hight_data-> sk[126]    = 0x34;
1875
        p_hight_data-> sk[125]    = 0x59;
1876
        p_hight_data-> sk[124]    = 0x34;
1877
 
1878
        expected_rf[32][7]        = 0x23;
1879
        expected_rf[32][6]        = 0xfd;
1880
        expected_rf[32][5]        = 0x9f;
1881
        expected_rf[32][4]        = 0x50;
1882
        expected_rf[32][3]        = 0xe5;
1883
        expected_rf[32][2]        = 0x52;
1884
        expected_rf[32][1]        = 0xe6;
1885
        expected_rf[32][0]        = 0xd8;
1886
 
1887
#elif 0
1888
        // Test vector 3
1889
        printf("test vectors 3\n");
1890
 
1891
        p_hight_data->rf[31][7]   = 0x21;
1892
        p_hight_data->rf[31][6]   = 0x63;
1893
        p_hight_data->rf[31][5]   = 0x0d;
1894
        p_hight_data->rf[31][4]   = 0x95;
1895
        p_hight_data->rf[31][3]   = 0x69;
1896
        p_hight_data->rf[31][2]   = 0x2d;
1897
        p_hight_data->rf[31][1]   = 0xb1;
1898
        p_hight_data->rf[31][0]   = 0x57;
1899
 
1900
        p_hight_data-> sk[127]    = 0x61;
1901
        p_hight_data-> sk[126]    = 0x35;
1902
        p_hight_data-> sk[125]    = 0x6c;
1903
        p_hight_data-> sk[124]    = 0x59;
1904
 
1905
        expected_rf[32][7]        = 0x7a;
1906
        expected_rf[32][6]        = 0x63;
1907
        expected_rf[32][5]        = 0xb2;
1908
        expected_rf[32][4]        = 0x95;
1909
        expected_rf[32][3]        = 0x8d;
1910
        expected_rf[32][2]        = 0x2d;
1911
        expected_rf[32][1]        = 0xf4;
1912
        expected_rf[32][0]        = 0x57;
1913
#elif 4
1914
        // Test vector 4
1915
        printf("test vectors 4\n");
1916
 
1917
        p_hight_data->rf[31][7]   = 0x7d;
1918
        p_hight_data->rf[31][6]   = 0x19;
1919
        p_hight_data->rf[31][5]   = 0x3f;
1920
        p_hight_data->rf[31][4]   = 0x33;
1921
        p_hight_data->rf[31][3]   = 0x90;
1922
        p_hight_data->rf[31][2]   = 0xb7;
1923
        p_hight_data->rf[31][1]   = 0x31;
1924
        p_hight_data->rf[31][0]   = 0xdf;
1925
 
1926
        p_hight_data-> sk[127]    = 0xd7;
1927
        p_hight_data-> sk[126]    = 0x5d;
1928
        p_hight_data-> sk[125]    = 0x46;
1929
        p_hight_data-> sk[124]    = 0x1a;
1930
 
1931
        expected_rf[32][7]        = 0xcc;
1932
        expected_rf[32][6]        = 0x19;
1933
        expected_rf[32][5]        = 0x7a;
1934
        expected_rf[32][4]        = 0x33;
1935
        expected_rf[32][3]        = 0x20;
1936
        expected_rf[32][2]        = 0xb7;
1937
        expected_rf[32][1]        = 0x1f;
1938
        expected_rf[32][0]        = 0xdf;
1939
 
1940
#endif 
1941
 
1942
 
1943
 
1944
        // RoundFunction32
1945
        for(i=31; i<32; i++){
1946
                FinalRoundFunction(ENC,
1947
                                                        p_hight_data->rf[i],
1948
                                                        p_hight_data->sk+(i*4),
1949
                                                        p_hight_data->rf[i+1]);
1950
        }
1951
 
1952
        // RoundFunction 1 
1953
        printf("\n\n===== RoundFunction 32 =====\n\n");
1954
 
1955
        printf("\n>>> Round 31 Result <<<\n");
1956
 
1957
        for(i=7; i>=0; i--){
1958
                printf("rf[31][%02d] = %02x\n",i, p_hight_data->rf[31][i]);
1959
        }
1960
 
1961
        printf("\n>>> Sub Key <<<\n");
1962
 
1963
        for(i=127; i>=124; i--){
1964
                printf("sk[%02d] = %02x\n", i, p_hight_data->sk[i]);
1965
        }
1966
 
1967
        printf("\n>>> Round 32 Result  <<<\n");
1968
 
1969
        for(i=7; i>=0; i--){
1970
        if(p_hight_data->rf[32][i] == expected_rf[32][i])
1971
                        printf("rf[32][%02d] = %02x : Correct\n", i, p_hight_data->rf[32][i]);
1972
                else
1973
                        printf("rf[32][%02d] = %02x: Wrong (expected = %02x) \n",i, p_hight_data->rf[32][i], expected_rf[32][i]);
1974
                }
1975
        }
1976
 
1977
 
1978
/* =====================================
1979
 
1980
    HightEncryptionTest()
1981
 
1982
=======================================*/
1983
void HightEncryptionTest()
1984
{
1985
        int i;
1986
        byte expected_c_text[8] ={0};
1987
 
1988
        printf("\n\n=====  HightEncryptionTest =====\n\n");
1989
 
1990
#if 1
1991
 
1992
        // Test vector 1
1993
        printf("test vectors 1\n");
1994
 
1995
        //Master Key 
1996
        p_hight_data->i_mk[15] = 0x00;
1997
        p_hight_data->i_mk[14] = 0x11;
1998
        p_hight_data->i_mk[13] = 0x22;
1999
        p_hight_data->i_mk[12] = 0x33;
2000
        p_hight_data->i_mk[11] = 0x44;
2001
        p_hight_data->i_mk[10] = 0x55;
2002
        p_hight_data->i_mk[9] = 0x66;
2003
        p_hight_data->i_mk[8] = 0x77;
2004
        p_hight_data->i_mk[7] = 0x88;
2005
        p_hight_data->i_mk[6] = 0x99;
2006
        p_hight_data->i_mk[5] = 0xaa;
2007
        p_hight_data->i_mk[4] = 0xbb;
2008
        p_hight_data->i_mk[3] = 0xcc;
2009
        p_hight_data->i_mk[2] = 0xdd;
2010
        p_hight_data->i_mk[1] = 0xee;
2011
        p_hight_data->i_mk[0] = 0xff;
2012
 
2013
        // Plain Text 
2014
        p_hight_data->i_pct[7]  = 0x00;
2015
        p_hight_data->i_pct[6]  = 0x00;
2016
        p_hight_data->i_pct[5]  = 0x00;
2017
        p_hight_data->i_pct[4]  = 0x00;
2018
        p_hight_data->i_pct[3]  = 0x00;
2019
        p_hight_data->i_pct[2]  = 0x00;
2020
        p_hight_data->i_pct[1]  = 0x00;
2021
        p_hight_data->i_pct[0]  = 0x00;
2022
 
2023
        // Expected Ciphered Text 
2024
        expected_c_text[7]     = 0x00;
2025
        expected_c_text[6]     = 0xf4;
2026
        expected_c_text[5]     = 0x18;
2027
        expected_c_text[4]     = 0xae;
2028
        expected_c_text[3]     = 0xd9;
2029
        expected_c_text[2]     = 0x4f;
2030
        expected_c_text[1]     = 0x03;
2031
        expected_c_text[0]     = 0xf2;
2032
 
2033
#elif 0
2034
        // Test vectors 2
2035
        printf("test vectors 2\n");
2036
 
2037
        //MasterKey
2038
        p_hight_data->i_mk[15] = 0xff;
2039
        p_hight_data->i_mk[14] = 0xee;
2040
        p_hight_data->i_mk[13] = 0xdd;
2041
        p_hight_data->i_mk[12] = 0xcc;
2042
        p_hight_data->i_mk[11] = 0xbb;
2043
        p_hight_data->i_mk[10] = 0xaa;
2044
        p_hight_data->i_mk[9]  = 0x99;
2045
        p_hight_data->i_mk[8]  = 0x88;
2046
        p_hight_data->i_mk[7]  = 0x77;
2047
        p_hight_data->i_mk[6]  = 0x66;
2048
        p_hight_data->i_mk[5]  = 0x55;
2049
        p_hight_data->i_mk[4]  = 0x44;
2050
        p_hight_data->i_mk[3]  = 0x33;
2051
        p_hight_data->i_mk[2]  = 0x22;
2052
        p_hight_data->i_mk[1]  = 0x11;
2053
        p_hight_data->i_mk[0]  = 0x00;
2054
 
2055
        //PlainText
2056
        p_hight_data->i_pct[7]  = 0x00;
2057
        p_hight_data->i_pct[6]  = 0x11;
2058
        p_hight_data->i_pct[5]  = 0x22;
2059
        p_hight_data->i_pct[4]  = 0x33;
2060
        p_hight_data->i_pct[3]  = 0x44;
2061
        p_hight_data->i_pct[2]  = 0x55;
2062
        p_hight_data->i_pct[1]  = 0x66;
2063
        p_hight_data->i_pct[0]  = 0x77;
2064
 
2065
        // Expected Ciphered Text 
2066
        expected_c_text[7]     = 0x23;
2067
        expected_c_text[6]     = 0xce;
2068
        expected_c_text[5]     = 0x9f;
2069
        expected_c_text[4]     = 0x72;
2070
        expected_c_text[3]     = 0xe5;
2071
        expected_c_text[2]     = 0x43;
2072
        expected_c_text[1]     = 0xe6;
2073
        expected_c_text[0]     = 0xd8;
2074
 
2075
#elif 0
2076
        // Test vector 3
2077
        printf("test vectors 3\n");
2078
 
2079
        //MasterKey
2080
        p_hight_data->i_mk[15] = 0x00;
2081
        p_hight_data->i_mk[14] = 0x01;
2082
        p_hight_data->i_mk[13] = 0x02;
2083
        p_hight_data->i_mk[12] = 0x03;
2084
        p_hight_data->i_mk[11] = 0x04;
2085
        p_hight_data->i_mk[10] = 0x05;
2086
        p_hight_data->i_mk[9]  = 0x06;
2087
        p_hight_data->i_mk[8]  = 0x07;
2088
        p_hight_data->i_mk[7]  = 0x08;
2089
        p_hight_data->i_mk[6]  = 0x09;
2090
        p_hight_data->i_mk[5]  = 0x0a;
2091
        p_hight_data->i_mk[4]  = 0x0b;
2092
        p_hight_data->i_mk[3]  = 0x0c;
2093
        p_hight_data->i_mk[2]  = 0x0d;
2094
        p_hight_data->i_mk[1]  = 0x0e;
2095
        p_hight_data->i_mk[0]  = 0x0f;
2096
 
2097
        //PlainText
2098
        p_hight_data->i_pct[7]  = 0x01;
2099
        p_hight_data->i_pct[6]  = 0x23;
2100
        p_hight_data->i_pct[5]  = 0x45;
2101
        p_hight_data->i_pct[4]  = 0x67;
2102
        p_hight_data->i_pct[3]  = 0x89;
2103
        p_hight_data->i_pct[2]  = 0xab;
2104
        p_hight_data->i_pct[1]  = 0xcd;
2105
        p_hight_data->i_pct[0]  = 0xef;
2106
 
2107
        // Expected Ciphered Text 
2108
        expected_c_text[7]     = 0x7a;
2109
        expected_c_text[6]     = 0x6f;
2110
        expected_c_text[5]     = 0xb2;
2111
        expected_c_text[4]     = 0xa2;
2112
        expected_c_text[3]     = 0x8d;
2113
        expected_c_text[2]     = 0x23;
2114
        expected_c_text[1]     = 0xf4;
2115
        expected_c_text[0]     = 0x66;
2116
 
2117
#elif 0
2118
        // Test vector 4
2119
        printf("test vectors 4\n");
2120
 
2121
        //MasterKey
2122
        p_hight_data->i_mk[15] = 0x28;
2123
        p_hight_data->i_mk[14] = 0xdb;
2124
        p_hight_data->i_mk[13] = 0xc3;
2125
        p_hight_data->i_mk[12] = 0xbc;
2126
        p_hight_data->i_mk[11] = 0x49;
2127
        p_hight_data->i_mk[10] = 0xff;
2128
        p_hight_data->i_mk[9]  = 0xd8;
2129
        p_hight_data->i_mk[8]  = 0x7d;
2130
        p_hight_data->i_mk[7]  = 0xcf;
2131
        p_hight_data->i_mk[6]  = 0xa5;
2132
        p_hight_data->i_mk[5]  = 0x09;
2133
        p_hight_data->i_mk[4]  = 0xb1;
2134
        p_hight_data->i_mk[3]  = 0x1d;
2135
        p_hight_data->i_mk[2]  = 0x42;
2136
        p_hight_data->i_mk[1]  = 0x2b;
2137
        p_hight_data->i_mk[0]  = 0xe7;
2138
 
2139
        //PlainText
2140
        p_hight_data->i_pct[7]  = 0xb4;
2141
        p_hight_data->i_pct[6]  = 0x1e;
2142
        p_hight_data->i_pct[5]  = 0x6b;
2143
        p_hight_data->i_pct[4]  = 0xe2;
2144
        p_hight_data->i_pct[3]  = 0xeb;
2145
        p_hight_data->i_pct[2]  = 0xa8;
2146
        p_hight_data->i_pct[1]  = 0x4a;
2147
        p_hight_data->i_pct[0]  = 0x14;
2148
 
2149
        // Expected Ciphered Text 
2150
        expected_c_text[7]     = 0xcc;
2151
        expected_c_text[6]     = 0x04;
2152
        expected_c_text[5]     = 0x7a;
2153
        expected_c_text[4]     = 0x75;
2154
        expected_c_text[3]     = 0x20;
2155
        expected_c_text[2]     = 0x9c;
2156
        expected_c_text[1]     = 0x1f;
2157
        expected_c_text[0]     = 0xc6;
2158
 
2159
 
2160
#endif
2161
 
2162
        WhiteningKeyGen(p_hight_data->i_mk, p_hight_data->wk);
2163
 
2164
        DeltaGen(p_hight_data->delta);
2165
 
2166
        SubKeyGen(p_hight_data->i_mk,
2167
                          p_hight_data->delta,
2168
                          p_hight_data->sk);
2169
 
2170
        InitialWhiteningFunction(ENC, p_hight_data->i_pct, p_hight_data->wk, p_hight_data->iwf);
2171
 
2172
        // RoundFunction1
2173
        InterRoundFunction(ENC,
2174
                                                p_hight_data->iwf,
2175
                                                p_hight_data->sk,
2176
                                                p_hight_data->rf[1]);
2177
 
2178
        // InterRoundFunction2~31
2179
        for(i=1; i<31; i++){
2180
                InterRoundFunction(ENC,
2181
                                                        p_hight_data->rf[i],
2182
                                                        p_hight_data->sk+(i*4),
2183
                                                        p_hight_data->rf[i+1]);
2184
        }
2185
        // RoundFunction32
2186
        FinalRoundFunction(ENC,
2187
                                                p_hight_data->rf[31],
2188
                                                p_hight_data->sk+(31*4),
2189
                                                p_hight_data->rf[32]);
2190
 
2191
        FinalWhiteningFunction(ENC, p_hight_data->rf[32], p_hight_data->wk, p_hight_data->fwf);
2192
 
2193
        for(i=7; i>=0; i--) {
2194
                p_hight_data->o_cpt[i] = p_hight_data->fwf[i];
2195
        }
2196
 
2197
        printf("\n>>> plaint text <<<\n");
2198
        for(i=7; i>=0; i--) {
2199
                printf("pt[%02d] = %02x\n", i, p_hight_data->i_pct[i]);
2200
        }
2201
 
2202
        printf("\n>>> master key <<<\n");
2203
        for(i=15; i>=0; i--) {
2204
                printf("mk[%02d] = %02x\n", i, p_hight_data->i_mk[i]);
2205
        }
2206
 
2207
        printf("\n>>>  HightEncryption result <<<\n");
2208
        for(i=7; i>=0; i--) {
2209
                if(p_hight_data->o_cpt[i] == expected_c_text[i])
2210
                        printf("o_c_text[%02d] = %02x : Correct\n", i , p_hight_data->o_cpt[i]);
2211
                else
2212
                        printf("o_c_text[%02d] = %02x : Wrong (expected = %02x) \n", i, p_hight_data->o_cpt[i], expected_c_text[i]);
2213
        }
2214
 
2215
}
2216
 
2217
/* =====================================
2218
 
2219
    HightDecryptionTest()
2220
 
2221
=======================================*/
2222
void HightDecryptionTest()
2223
{
2224
        int i;
2225
        byte expected_p_text[8] ={0};
2226
        byte dec_sk[128] = {0};
2227
 
2228
        printf("\n\n=====  HightDecryptionTest =====\n\n");
2229
 
2230
#if 0
2231
 
2232
        // Test vector 1
2233
        printf("test vectors 1\n");
2234
 
2235
        //Master Key 
2236
        p_hight_data->i_mk[15] = 0x00;
2237
        p_hight_data->i_mk[14] = 0x11;
2238
        p_hight_data->i_mk[13] = 0x22;
2239
        p_hight_data->i_mk[12] = 0x33;
2240
        p_hight_data->i_mk[11] = 0x44;
2241
        p_hight_data->i_mk[10] = 0x55;
2242
        p_hight_data->i_mk[9]  = 0x66;
2243
        p_hight_data->i_mk[8]  = 0x77;
2244
        p_hight_data->i_mk[7]  = 0x88;
2245
        p_hight_data->i_mk[6]  = 0x99;
2246
        p_hight_data->i_mk[5]  = 0xaa;
2247
        p_hight_data->i_mk[4]  = 0xbb;
2248
        p_hight_data->i_mk[3]  = 0xcc;
2249
        p_hight_data->i_mk[2]  = 0xdd;
2250
        p_hight_data->i_mk[1]  = 0xee;
2251
        p_hight_data->i_mk[0]  = 0xff;
2252
 
2253
        // Cipher Text 
2254
        p_hight_data->i_pct[7]  = 0x00;
2255
        p_hight_data->i_pct[6]  = 0xf4;
2256
        p_hight_data->i_pct[5]  = 0x18;
2257
        p_hight_data->i_pct[4]  = 0xae;
2258
        p_hight_data->i_pct[3]  = 0xd9;
2259
        p_hight_data->i_pct[2]  = 0x4f;
2260
        p_hight_data->i_pct[1]  = 0x03;
2261
        p_hight_data->i_pct[0]  = 0xf2;
2262
 
2263
        // Expected plain Text 
2264
        expected_p_text[7]     = 0x00;
2265
        expected_p_text[6]     = 0x00;
2266
        expected_p_text[5]     = 0x00;
2267
        expected_p_text[4]     = 0x00;
2268
        expected_p_text[3]     = 0x00;
2269
        expected_p_text[2]     = 0x00;
2270
        expected_p_text[1]     = 0x00;
2271
        expected_p_text[0]     = 0x00;
2272
 
2273
#elif 0
2274
        // Test vector 2
2275
        printf("test vectors 2\n");
2276
 
2277
        //MasterKey
2278
        p_hight_data->i_mk[15] = 0xff;
2279
        p_hight_data->i_mk[14] = 0xee;
2280
        p_hight_data->i_mk[13] = 0xdd;
2281
        p_hight_data->i_mk[12] = 0xcc;
2282
        p_hight_data->i_mk[11] = 0xbb;
2283
        p_hight_data->i_mk[10] = 0xaa;
2284
        p_hight_data->i_mk[9]  = 0x99;
2285
        p_hight_data->i_mk[8]  = 0x88;
2286
        p_hight_data->i_mk[7]  = 0x77;
2287
        p_hight_data->i_mk[6]  = 0x66;
2288
        p_hight_data->i_mk[5]  = 0x55;
2289
        p_hight_data->i_mk[4]  = 0x44;
2290
        p_hight_data->i_mk[3]  = 0x33;
2291
        p_hight_data->i_mk[2]  = 0x22;
2292
        p_hight_data->i_mk[1]  = 0x11;
2293
        p_hight_data->i_mk[0]  = 0x00;
2294
 
2295
        // Cipher Text 
2296
        p_hight_data->i_pct[7]  = 0x23;
2297
        p_hight_data->i_pct[6]  = 0xce;
2298
        p_hight_data->i_pct[5]  = 0x9f;
2299
        p_hight_data->i_pct[4]  = 0x72;
2300
        p_hight_data->i_pct[3]  = 0xe5;
2301
        p_hight_data->i_pct[2]  = 0x43;
2302
        p_hight_data->i_pct[1]  = 0xe6;
2303
        p_hight_data->i_pct[0]  = 0xd8;
2304
 
2305
        // Expected Plain Text 
2306
        expected_p_text[7]     = 0x00;
2307
        expected_p_text[6]     = 0x11;
2308
        expected_p_text[5]     = 0x22;
2309
        expected_p_text[4]     = 0x33;
2310
        expected_p_text[3]     = 0x44;
2311
        expected_p_text[2]     = 0x55;
2312
        expected_p_text[1]     = 0x66;
2313
        expected_p_text[0]     = 0x77;
2314
 
2315
 
2316
#elif 0
2317
        // Test vector 3
2318
        printf("test vectors 3\n");
2319
 
2320
        //MasterKey
2321
        p_hight_data->i_mk[15] = 0x00;
2322
        p_hight_data->i_mk[14] = 0x01;
2323
        p_hight_data->i_mk[13] = 0x02;
2324
        p_hight_data->i_mk[12] = 0x03;
2325
        p_hight_data->i_mk[11] = 0x04;
2326
        p_hight_data->i_mk[10] = 0x05;
2327
        p_hight_data->i_mk[9]  = 0x06;
2328
        p_hight_data->i_mk[8]  = 0x07;
2329
        p_hight_data->i_mk[7]  = 0x08;
2330
        p_hight_data->i_mk[6]  = 0x09;
2331
        p_hight_data->i_mk[5]  = 0x0a;
2332
        p_hight_data->i_mk[4]  = 0x0b;
2333
        p_hight_data->i_mk[3]  = 0x0c;
2334
        p_hight_data->i_mk[2]  = 0x0d;
2335
        p_hight_data->i_mk[1]  = 0x0e;
2336
        p_hight_data->i_mk[0]  = 0x0f;
2337
 
2338
        // Cipher Text 
2339
        p_hight_data->i_pct[7]  = 0x7a;
2340
        p_hight_data->i_pct[6]  = 0x6f;
2341
        p_hight_data->i_pct[5]  = 0xb2;
2342
        p_hight_data->i_pct[4]  = 0xa2;
2343
        p_hight_data->i_pct[3]  = 0x8d;
2344
        p_hight_data->i_pct[2]  = 0x23;
2345
        p_hight_data->i_pct[1]  = 0xf4;
2346
        p_hight_data->i_pct[0]  = 0x66;
2347
 
2348
        // Expected plain Text 
2349
        expected_p_text[7]     = 0x01;
2350
        expected_p_text[6]     = 0x23;
2351
        expected_p_text[5]     = 0x45;
2352
        expected_p_text[4]     = 0x67;
2353
        expected_p_text[3]     = 0x89;
2354
        expected_p_text[2]     = 0xab;
2355
        expected_p_text[1]     = 0xcd;
2356
        expected_p_text[0]     = 0xef;
2357
 
2358
#elif 1
2359
        // Test vector 4
2360
        printf("test vectors 4\n");
2361
 
2362
        //MasterKey
2363
        p_hight_data->i_mk[15] = 0x28;
2364
        p_hight_data->i_mk[14] = 0xdb;
2365
        p_hight_data->i_mk[13] = 0xc3;
2366
        p_hight_data->i_mk[12] = 0xbc;
2367
        p_hight_data->i_mk[11] = 0x49;
2368
        p_hight_data->i_mk[10] = 0xff;
2369
        p_hight_data->i_mk[9]  = 0xd8;
2370
        p_hight_data->i_mk[8]  = 0x7d;
2371
        p_hight_data->i_mk[7]  = 0xcf;
2372
        p_hight_data->i_mk[6]  = 0xa5;
2373
        p_hight_data->i_mk[5]  = 0x09;
2374
        p_hight_data->i_mk[4]  = 0xb1;
2375
        p_hight_data->i_mk[3]  = 0x1d;
2376
        p_hight_data->i_mk[2]  = 0x42;
2377
        p_hight_data->i_mk[1]  = 0x2b;
2378
        p_hight_data->i_mk[0]  = 0xe7;
2379
 
2380
        // Cipher Text 
2381
        p_hight_data->i_pct[7]  = 0xcc;
2382
        p_hight_data->i_pct[6]  = 0x04;
2383
        p_hight_data->i_pct[5]  = 0x7a;
2384
        p_hight_data->i_pct[4]  = 0x75;
2385
        p_hight_data->i_pct[3]  = 0x20;
2386
        p_hight_data->i_pct[2]  = 0x9c;
2387
        p_hight_data->i_pct[1]  = 0x1f;
2388
        p_hight_data->i_pct[0]  = 0xc6;
2389
 
2390
        // Expected plain Text 
2391
        expected_p_text[7]     = 0xb4;
2392
        expected_p_text[6]     = 0x1e;
2393
        expected_p_text[5]     = 0x6b;
2394
        expected_p_text[4]     = 0xe2;
2395
        expected_p_text[3]     = 0xeb;
2396
        expected_p_text[2]     = 0xa8;
2397
        expected_p_text[1]     = 0x4a;
2398
        expected_p_text[0]     = 0x14;
2399
 
2400
#endif
2401
 
2402
        WhiteningKeyGen(p_hight_data->i_mk, p_hight_data->wk);
2403
 
2404
        DeltaGen(p_hight_data->delta);
2405
 
2406
        SubKeyGen(p_hight_data->i_mk,
2407
                          p_hight_data->delta,
2408
                          p_hight_data->sk);
2409
 
2410
        // Reverse of sk 
2411
        for(i=0; i<=127; i++){
2412
                dec_sk[127-i] = p_hight_data->sk[i];
2413
        }
2414
 
2415
        FinalWhiteningFunction(DEC, p_hight_data->i_pct, p_hight_data->wk, p_hight_data->fwf);
2416
 
2417
        // RoundFunction1
2418
        InterRoundFunction(DEC,
2419
                                                p_hight_data->fwf,
2420
                                                dec_sk,//p_hight_data->sk+124,
2421
                                                p_hight_data->rf[1]);
2422
 
2423
        // InterRoundFunction2~31
2424
        for(i=1; i<31; i++){
2425
                InterRoundFunction(DEC,
2426
                                                        p_hight_data->rf[i],
2427
                                                        dec_sk+i*4, //p_hight_data->sk+((31-i)*4),
2428
                                                        p_hight_data->rf[i+1]);
2429
        }
2430
        // RoundFunction32
2431
        FinalRoundFunction(DEC,
2432
                                                p_hight_data->rf[31],
2433
                                                dec_sk+124, //p_hight_data->sk,
2434
                                                p_hight_data->rf[32]);
2435
 
2436
        InitialWhiteningFunction(DEC, p_hight_data->rf[32], p_hight_data->wk, p_hight_data->iwf);
2437
 
2438
        for(i=7; i>=0; i--) {
2439
                p_hight_data->o_cpt[i] = p_hight_data->iwf[i];
2440
        }
2441
 
2442
        printf("\n>>> Cipher text <<<\n");
2443
        for(i=7; i>=0; i--) {
2444
                printf("pt[%02d] = %02x\n", i, p_hight_data->i_pct[i]);
2445
        }
2446
 
2447
        printf("\n>>> Master key <<<\n");
2448
        for(i=15; i>=0; i--) {
2449
                printf("mk[%02d] = %02x\n", i, p_hight_data->i_mk[i]);
2450
        }
2451
 
2452
        printf("\n>>>  HightDecryption result <<<\n");
2453
        for(i=7; i>=0; i--) {
2454
                if(p_hight_data->o_cpt[i] == expected_p_text[i])
2455
                        printf("o_c_text[%02d] = %02x : Correct\n", i , p_hight_data->o_cpt[i]);
2456
                else
2457
                        printf("o_c_text[%02d] = %02x : Wrong (expected = %02x) \n", i, p_hight_data->o_cpt[i], expected_p_text[i]);
2458
        }
2459
 
2460
        printf("\n\n====  Verbose Log  ====\n");
2461
 
2462
        printf("\n>>>  FinalWhiFunction <<<\n");
2463
        printf("Final     : %02x %02x %02x %02x %02x %02x %02x %02x \n",
2464
                    p_hight_data->fwf[7], p_hight_data->fwf[6], p_hight_data->fwf[5], p_hight_data->fwf[4],
2465
                        p_hight_data->fwf[3], p_hight_data->fwf[2], p_hight_data->fwf[1], p_hight_data->fwf[0]);
2466
 
2467
        printf("\n>>>  RoundFunction <<<\n");
2468
        for(i=1; i<=32; i++){
2469
                printf("Round[%02d] : %02x %02x %02x %02x %02x %02x %02x %02x\n",
2470
                            i, p_hight_data->rf[i][7], p_hight_data->rf[i][6],
2471
                                   p_hight_data->rf[i][5], p_hight_data->rf[i][4],
2472
                                   p_hight_data->rf[i][3], p_hight_data->rf[i][2],
2473
                                   p_hight_data->rf[i][1], p_hight_data->rf[i][0]);
2474
        }
2475
 
2476
        printf("\n>>>  InitialWhiteningFunction <<<\n");
2477
        printf("Initial   : %02x %02x %02x %02x %02x %02x %02x %02x\n",
2478
                    p_hight_data->iwf[7], p_hight_data->iwf[6], p_hight_data->iwf[5], p_hight_data->iwf[4],
2479
                        p_hight_data->iwf[3], p_hight_data->iwf[2], p_hight_data->iwf[1], p_hight_data->iwf[0]);
2480
 
2481
 
2482
}
2483
 
2484
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.