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

Subversion Repositories hight

[/] [hight/] [trunk/] [sw/] [hight_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 top test function 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 "hight_test.h"
45
#include <stdio.h>
46
#include <string.h>
47
#include <stdlib.h>
48
 
49
const byte vec_pt[4][8] = { // vec_pt[vec_num-1]
50
        {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
51
        {0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00},
52
        {0xef,0xcd,0xab,0x89,0x67,0x45,0x23,0x01},
53
        {0x14,0x4a,0xa8,0xeb,0xe2,0x6b,0x1e,0xb4}
54
};
55
 
56
const byte vec_mk[4][16] = { // vec_mk[vec_num-1]
57
        {0xff,0xee,0xdd,0xcc,0xbb,0xaa,0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00},
58
        {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff},
59
        {0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00},
60
        {0xe7,0x2b,0x42,0x1d,0xb1,0x09,0xa5,0xcf,0x7d,0xd8,0xff,0x49,0xbc,0xc3,0xdb,0x28}
61
};
62
 
63
const byte vec_ct[4][8] = { // vec_ct[vec_num-1]
64
        {0xf2,0x03,0x4f,0xd9,0xae,0x18,0xf4,0x00},
65
        {0xd8,0xe6,0x43,0xe5,0x72,0x9f,0xce,0x23},
66
        {0x66,0xf4,0x23,0x8d,0xa2,0xb2,0x6f,0x7a},
67
        {0xc6,0x1f,0x9c,0x20,0x75,0x7a,0x04,0xcc}
68
};
69
 
70
 
71
/* =====================================
72
 
73
    HightTopTest
74
 
75
=======================================*/
76
void HightTopTest(const char *vec_id, int op_mode, int dump_opt, const char *dump_fname, HIGHT_DATA *p_hight_vec)
77
{
78
        enum{SUCC=0,FAIL};
79
 
80
        FILE *fdump = NULL;
81
        int compResult = FAIL;
82
        int i;
83
        byte expected1[8] = {0};
84
        byte expected2[8] = {0};
85
 
86
        // setup input vectors
87
        if(!strcmp(vec_id, "v1")) {
88
                if(op_mode == ENC) {
89
                        memcpy(p_hight_vec->i_pct, vec_pt[0], 8);
90
                } else if(op_mode == DEC) {
91
                        memcpy(p_hight_vec->i_pct, vec_ct[0], 8);
92
                } else {
93
                        printf("Error\n");
94
                        return;
95
                }
96
                memcpy(p_hight_vec->i_mk, vec_mk[0], 16);
97
 
98
        } else if (!strcmp(vec_id, "v2")) {
99
                if(op_mode == ENC) {
100
                        memcpy(p_hight_vec->i_pct, vec_pt[1], 8);
101
                } else if(op_mode == DEC) {
102
                        memcpy(p_hight_vec->i_pct, vec_ct[1], 8);
103
                } else {
104
                        printf("Error!!\n");
105
                        return;
106
                }
107
                memcpy(p_hight_vec->i_mk, vec_mk[1], 16);
108
 
109
        } else if (!strcmp(vec_id, "v3")) {
110
                if(op_mode == ENC) {
111
                        memcpy(p_hight_vec->i_pct, vec_pt[2], 8);
112
                } else if(op_mode == DEC) {
113
                        memcpy(p_hight_vec->i_pct, vec_ct[2], 8);
114
                } else {
115
                        printf("Error!!!\n");
116
                        return;
117
                }
118
                memcpy(p_hight_vec->i_mk, vec_mk[2], 16);
119
 
120
        } else if (!strcmp(vec_id, "v4")) {
121
                if(op_mode == ENC) {
122
                        memcpy(p_hight_vec->i_pct, vec_pt[3], 8);
123
                } else if(op_mode == DEC) {
124
                        memcpy(p_hight_vec->i_pct, vec_ct[3], 8);
125
                } else {
126
                        printf("Error!!!!\n");
127
                        return;
128
                }
129
                memcpy(p_hight_vec->i_mk, vec_mk[3], 16);
130
 
131
        } else if (!strcmp(vec_id, "rv")) {
132
 
133
        } else {
134
                printf("Error!!!!!!!!\n");
135
                return;
136
        }
137
 
138
        // Run !!!
139
        HightTop(op_mode, p_hight_vec);
140
 
141
        // Compare
142
        if(op_mode == ENC) {
143
                compResult = (((!strcmp(vec_id, "v1") && !memcmp(vec_ct[0], p_hight_vec->o_cpt, 8)) ||
144
                                   (!strcmp(vec_id, "v2") && !memcmp(vec_ct[1], p_hight_vec->o_cpt, 8)) ||
145
                                           (!strcmp(vec_id, "v3") && !memcmp(vec_ct[2], p_hight_vec->o_cpt, 8)) ||
146
                                           (!strcmp(vec_id, "v4") && !memcmp(vec_ct[3], p_hight_vec->o_cpt, 8)))  ? SUCC : FAIL);
147
        } else if(op_mode == DEC) {
148
                compResult = (((!strcmp(vec_id, "v1") && !memcmp(vec_pt[0], p_hight_vec->o_cpt, 8)) ||
149
                                   (!strcmp(vec_id, "v2") && !memcmp(vec_pt[1], p_hight_vec->o_cpt, 8)) ||
150
                                           (!strcmp(vec_id, "v3") && !memcmp(vec_pt[2], p_hight_vec->o_cpt, 8)) ||
151
                                           (!strcmp(vec_id, "v4") && !memcmp(vec_pt[3], p_hight_vec->o_cpt, 8)))  ? SUCC : FAIL);
152
        } else {
153
                printf("Error!!!\n");
154
                return;
155
        }
156
 
157
        // Display result
158
        // * ^.~ *
159
        printf("//===== %s =====//\n", (op_mode == ENC) ? "Encryption" :
160
                                           (op_mode == DEC) ? "Decryption" : "==Error==");
161
        printf("// %s [7..0]\n", (op_mode == ENC) ? "Plain text" :
162
                                     (op_mode == DEC) ? "Cipher text" : "Error~");
163
        printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
164
                                                           p_hight_vec->i_pct[7],
165
                                                           p_hight_vec->i_pct[6],
166
                                                                                                   p_hight_vec->i_pct[5],
167
                                                                                                   p_hight_vec->i_pct[4],
168
                                                                                                   p_hight_vec->i_pct[3],
169
                                                                                                   p_hight_vec->i_pct[2],
170
                                                                                                   p_hight_vec->i_pct[1],
171
                                                                                                   p_hight_vec->i_pct[0]);
172
        // * ^.~ *
173
        printf("// Master key [15..0]\n");
174
        printf("%02x %02x %02x %02x %02x %02x %02x %02x\n%02x %02x %02x %02x %02x %02x %02x %02x\n",
175
                                                   p_hight_vec->i_mk[15],
176
                                                                                                   p_hight_vec->i_mk[14],
177
                                                                                                   p_hight_vec->i_mk[13],
178
                                                                                                   p_hight_vec->i_mk[12],
179
                                                                                                   p_hight_vec->i_mk[11],
180
                                                                                                   p_hight_vec->i_mk[10],
181
                                                                                                   p_hight_vec->i_mk[9],
182
                                                                                                   p_hight_vec->i_mk[8],
183
                                                                                                   p_hight_vec->i_mk[7],
184
                                                                                                   p_hight_vec->i_mk[6],
185
                                                                                                   p_hight_vec->i_mk[5],
186
                                                                                                   p_hight_vec->i_mk[4],
187
                                                                                                   p_hight_vec->i_mk[3],
188
                                                                                                   p_hight_vec->i_mk[2],
189
                                                                                                   p_hight_vec->i_mk[1],
190
                                                                                                   p_hight_vec->i_mk[0]);
191
        // * ^.~ *
192
        printf("// Whitening key [7..0]\n");
193
        printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
194
                                                                                                   p_hight_vec->wk[7],
195
                                                           p_hight_vec->wk[6],
196
                                                                                                   p_hight_vec->wk[5],
197
                                                                                                   p_hight_vec->wk[4],
198
                                                                                                   p_hight_vec->wk[3],
199
                                                                                                   p_hight_vec->wk[2],
200
                                                                                                   p_hight_vec->wk[1],
201
                                                                                                   p_hight_vec->wk[0]);
202
        // * ^.~ *
203
        printf("// Delta [0..127]\n");
204
        for(i=0; i<128; i+=8)
205
                printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
206
                                                           p_hight_vec->delta[i],
207
                                                                                                   p_hight_vec->delta[i+1],
208
                                                                                                   p_hight_vec->delta[i+2],
209
                                                                                                   p_hight_vec->delta[i+3],
210
                                                                                                   p_hight_vec->delta[i+4],
211
                                                                                                   p_hight_vec->delta[i+5],
212
                                                                                                   p_hight_vec->delta[i+6],
213
                                                                                                   p_hight_vec->delta[i+7]);
214
        // * ^.~ *
215
        printf("// Sub key [0..127]\n");
216
        for(i=0; i<128; i+=8)
217
                printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
218
                                                           p_hight_vec->sk[i],
219
                                                                                                   p_hight_vec->sk[i+1],
220
                                                                                                   p_hight_vec->sk[i+2],
221
                                                                                                   p_hight_vec->sk[i+3],
222
                                                                                                   p_hight_vec->sk[i+4],
223
                                                                                                   p_hight_vec->sk[i+5],
224
                                                                                                   p_hight_vec->sk[i+6],
225
                                                                                                   p_hight_vec->sk[i+7]);
226
        // * ^.~ *
227
        printf("// %s whitening function(1st) [7..0]\n", (op_mode == ENC) ? "Initial" :
228
                                                             (op_mode == DEC) ? "Final" : "Error~");
229
        if(op_mode == ENC){
230
                printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
231
                                                           p_hight_vec->iwf[7],
232
                                                           p_hight_vec->iwf[6],
233
                                                                                                   p_hight_vec->iwf[5],
234
                                                                                                   p_hight_vec->iwf[4],
235
                                                                                                   p_hight_vec->iwf[3],
236
                                                                                                   p_hight_vec->iwf[2],
237
                                                                                                   p_hight_vec->iwf[1],
238
                                                                                                   p_hight_vec->iwf[0]);
239
        } else if(op_mode == DEC){
240
                        printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
241
                                                           p_hight_vec->fwf[7],
242
                                                           p_hight_vec->fwf[6],
243
                                                                                                   p_hight_vec->fwf[5],
244
                                                                                                   p_hight_vec->fwf[4],
245
                                                                                                   p_hight_vec->fwf[3],
246
                                                                                                   p_hight_vec->fwf[2],
247
                                                                                                   p_hight_vec->fwf[1],
248
                                                                                                   p_hight_vec->fwf[0]);
249
        }
250
 
251
        // * ^.~ *
252
        printf("// Round function 1~32 [7..0]\n");
253
        for(i=1; i<=32; i++)
254
                printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
255
                                                           p_hight_vec->rf[i][7],
256
                                                                                                   p_hight_vec->rf[i][6],
257
                                                                                                   p_hight_vec->rf[i][5],
258
                                                                                                   p_hight_vec->rf[i][4],
259
                                                                                                   p_hight_vec->rf[i][3],
260
                                                                                                   p_hight_vec->rf[i][2],
261
                                                                                                   p_hight_vec->rf[i][1],
262
                                                                                                   p_hight_vec->rf[i][0]);
263
 
264
        // * ^.~ *
265
        printf("// %s whitening function(2nd) [7..0]\n", (op_mode == ENC) ? "Final" :
266
                                                             (op_mode == DEC) ? "Initial" : "Error~");
267
        if(op_mode == ENC){
268
                printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
269
                                                           p_hight_vec->fwf[7],
270
                                                           p_hight_vec->fwf[6],
271
                                                                                                   p_hight_vec->fwf[5],
272
                                                                                                   p_hight_vec->fwf[4],
273
                                                                                                   p_hight_vec->fwf[3],
274
                                                                                                   p_hight_vec->fwf[2],
275
                                                                                                   p_hight_vec->fwf[1],
276
                                                                                                   p_hight_vec->fwf[0]);
277
        } else if(op_mode == DEC){
278
                        printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
279
                                                           p_hight_vec->iwf[7],
280
                                                           p_hight_vec->iwf[6],
281
                                                                                                   p_hight_vec->iwf[5],
282
                                                                                                   p_hight_vec->iwf[4],
283
                                                                                                   p_hight_vec->iwf[3],
284
                                                                                                   p_hight_vec->iwf[2],
285
                                                                                                   p_hight_vec->iwf[1],
286
                                                                                                   p_hight_vec->iwf[0]);
287
        }
288
 
289
        // * ^.~ *
290
        printf("// %s [7..0]\n", (op_mode == ENC) ? "Cipher text" :
291
                                     (op_mode == DEC) ? "Plain text" : "Error~");
292
        printf("%02x %02x %02x %02x %02x %02x %02x %02x (%s)\n\n\n",
293
                                                           p_hight_vec->o_cpt[7],
294
                                                           p_hight_vec->o_cpt[6],
295
                                                                                                   p_hight_vec->o_cpt[5],
296
                                                                                                   p_hight_vec->o_cpt[4],
297
                                                                                                   p_hight_vec->o_cpt[3],
298
                                                                                                   p_hight_vec->o_cpt[2],
299
                                                                                                   p_hight_vec->o_cpt[1],
300
                                                                                                   p_hight_vec->o_cpt[0],
301
                                                                                                   (compResult == SUCC) ? "Correct" : "Wrong");
302
        // Dump
303
        if(dump_opt) {
304
                if(!(fdump = fopen(dump_fname, "w+"))){
305
                        printf("fail to create dump file... Error!!!\n");
306
                        return;
307
                }
308
 
309
                fclose(fdump);
310
        }
311
}

powered by: WebSVN 2.1.0

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