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

Subversion Repositories hight

[/] [hight/] [trunk/] [sw/] [hight.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 hight core 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
#include <string.h>
46
 
47
//#define SUB_KEY_GEN_DUMP
48
 
49
 
50
/* =====================================
51
 
52
    WhiteningKeyGen (WKG)
53
 
54
=======================================*/
55
void WhiteningKeyGen(byte *i_mk, byte *o_wk)
56
{
57
 
58
 
59
        o_wk[0] = i_mk[12];
60
        o_wk[1] = i_mk[13];
61
        o_wk[2] = i_mk[14];
62
        o_wk[3] = i_mk[15];
63
        o_wk[4] = i_mk[ 0];
64
        o_wk[5] = i_mk[ 1];
65
        o_wk[6] = i_mk[ 2];
66
        o_wk[7] = i_mk[ 3];
67
}
68
 
69
 
70
 
71
/* =====================================
72
 
73
    DeltaGen (DG)
74
 
75
=======================================*/
76
void DeltaGen(byte *o_delta)
77
{
78
        static byte s[133];
79
        int i;
80
 
81
        s[0] = 0 ;
82
        s[1] = 1 ;
83
        s[2] = 0 ;
84
        s[3] = 1 ;
85
        s[4] = 1 ;
86
        s[5] = 0 ;
87
    s[6] = 1 ;
88
 
89
 
90
        for(i=1; i<128; i++){
91
                s[i+6]= s[i+2]^s[i-1];
92
        }
93
 
94
        o_delta[0] = s[0]|s[1]<<1|s[2]<<2|s[3]<<3|s[4]<<4|s[5]<<5|s[6]<<6;
95
        for(i=1; i<128; i++){
96
                o_delta[i] = s[i]|s[i+1]<<1|s[i+2]<<2|s[i+3]<<3|s[i+4]<<4|s[i+5]<<5|s[i+6]<<6;
97
        }
98
}
99
 
100
 
101
 
102
/* =====================================
103
 
104
    SubKeyGen (SKG)
105
 
106
=======================================*/
107
void SubKeyGen(byte *i_mk, byte *i_delta, byte *o_sk)
108
{
109
        int i, j;
110
#ifdef SUB_KEY_GEN_DUMP
111
        FILE *fp = fopen("result.txt", "w+");
112
#endif
113
 
114
        /*
115
        // print delta values
116
        for (i=0; i<128; i++){
117
                printf("%02X ", i_delta[i]);
118
                if(i%8 == 7)
119
                        printf("\n");
120
        }
121
        */
122
 
123
        for(i=0; i<8; i++){
124
                for(j=0; j<8; j++){
125
                        o_sk[16*i+j]=(byte)(i_mk[(j-i)&7]+i_delta[16*i+j]);
126
#ifdef SUB_KEY_GEN_DUMP
127
                        fprintf(fp, "rk[%d]:%02X = (mk[%d]:%02X + delta[%d]:%02X\n", 16*i+j, o_sk[16*i+j],
128
                                                                                       (j-i)&7, i_mk[(j-i)&7],
129
                                                                       16*i+j, i_delta[16*i+j]); // comp
130
#endif
131
                }
132
                for(j=0; j<8; j++){
133
                        o_sk[16*i+j+8]=(byte)(i_mk[((j-i)&7)+8]+i_delta[16*i+j+8]);
134
#ifdef SUB_KEY_GEN_DUMP
135
                        fprintf(fp, "rk[%d]:%02X = (mk[%d]:%02X + delta[%d]:%02X\n", 16*i+j+8, o_sk[16*i+j+8],
136
                                                                                       ((j-i)&7)+8, i_mk[((j-i)&7)+8],
137
                                                                               16*i+j+8, i_delta[16*i+j+8]); // comp
138
#endif
139
                }
140
        }
141
 
142
#ifdef SUB_KEY_GEN_DUMP
143
        fclose(fp); // comp
144
#endif
145
}
146
 
147
 
148
/* =====================================
149
 
150
    InitialWhiteningFunction (IWF)
151
 
152
=======================================*/
153
void InitialWhiteningFunction(int i_op, byte *i_pc_text, byte *i_wk, byte *o_xx)
154
{
155
        o_xx[7] =  i_pc_text[7];
156
        o_xx[6] =  (i_op == ENC) ? i_pc_text[6] ^ i_wk[3] :
157
                                       i_pc_text[6] ^ i_wk[3] ; // op == DEC   
158
        o_xx[5] =  i_pc_text[5];
159
        o_xx[4] =  (i_op == ENC) ? (i_pc_text[4] + i_wk[2])&0xff :
160
                                       (i_pc_text[4] - i_wk[2])&0xff ; // op == DEC
161
        o_xx[3] =  i_pc_text[3];
162
        o_xx[2] =  (i_op == ENC) ? i_pc_text[2] ^ i_wk[1] :
163
                                       i_pc_text[2] ^ i_wk[1] ; // op == DEC
164
        o_xx[1] =  i_pc_text[1];
165
        o_xx[0] =  (i_op == ENC) ? (i_pc_text[0] + i_wk[0])&0xff :
166
                                   (i_pc_text[0] - i_wk[0])&0xff ; // op == DEC
167
 
168
}
169
 
170
 
171
/* =====================================
172
 
173
    InterRoundFunction (IRF)
174
 
175
=======================================*/
176
void InterRoundFunction(int i_op, byte *i_xx, byte *i_rsk, byte*o_xx)
177
{
178
        o_xx[7] = (i_op == ENC) ? i_xx[6] :
179
                                      i_xx[0] ;
180
 
181
        o_xx[6] = (i_op == ENC) ? i_xx[5] + (((i_xx[4]<<3 | i_xx[4]>>5) ^ (i_xx[4]<<4 | i_xx[4]>>4)
182
                      ^ (i_xx[4]<<6 | i_xx[4]>>2))^i_rsk[2])&0xff :
183
                                  i_xx[7] ^ (((i_xx[6]<<1 | i_xx[6]>>7) ^ (i_xx[6]<<2 | i_xx[6]>>6)
184
                          ^ (i_xx[6]<<7 | i_xx[6]>>1))+i_rsk[0]&0xff) ;
185
 
186
        o_xx[5] = (i_op == ENC) ? i_xx[4] :
187
                                      i_xx[6] ;
188
 
189
        o_xx[4] = (i_op == ENC) ? i_xx[3] ^ (((i_xx[2]<<1 | i_xx[2]>>7) ^ (i_xx[2]<<2 | i_xx[2]>>6)
190
                      ^ (i_xx[2]<<7 | i_xx[2]>>1))+i_rsk[1]&0xff) :
191
                                  i_xx[5] - (((i_xx[4]<<3 | i_xx[4]>>5) ^ (i_xx[4]<<4 | i_xx[4]>>4)
192
                      ^ (i_xx[4]<<6 | i_xx[4]>>2))^i_rsk[1])&0xff ;
193
 
194
        o_xx[3] = (i_op == ENC) ? i_xx[2] :
195
                                      i_xx[4] ;
196
 
197
        o_xx[2] = (i_op == ENC) ? i_xx[1] + (((i_xx[0]<<3 | i_xx[0]>>5) ^ (i_xx[0]<<4 | i_xx[0]>>4)
198
                      ^ (i_xx[0]<<6 | i_xx[0]>>2))^i_rsk[0])&0xff :
199
                                  i_xx[3] ^ (((i_xx[2]<<1 | i_xx[2]>>7) ^ (i_xx[2]<<2 | i_xx[2]>>6)
200
                          ^ (i_xx[2]<<7 | i_xx[2]>>1))+i_rsk[2]&0xff) ;
201
        o_xx[1] = (i_op == ENC) ? i_xx[0] :
202
                                      i_xx[2] ;
203
 
204
        o_xx[0] = (i_op == ENC) ? i_xx[7] ^ (((i_xx[6]<<1 | i_xx[6]>>7) ^ (i_xx[6]<<2 | i_xx[6]>>6)
205
                      ^ (i_xx[6]<<7 | i_xx[6]>>1))+i_rsk[3]&0xff) :
206
                                  i_xx[1] - (((i_xx[0]<<3 | i_xx[0]>>5) ^ (i_xx[0]<<4 | i_xx[0]>>4)
207
                      ^ (i_xx[0]<<6 | i_xx[0]>>2))^i_rsk[3])&0xff ;
208
}
209
 
210
 
211
/* =====================================
212
 
213
    FinalRoundFunction (FRF)
214
 
215
=======================================*/
216
void FinalRoundFunction(int i_op, byte *i_xx, byte *i_rsk, byte *o_rf)
217
{
218
 
219
        o_rf[7] = (i_op == ENC) ? i_xx[7] ^ (((i_xx[6]<<1 | i_xx[6]>>7) ^ (i_xx[6]<<2 | i_xx[6]>>6) ^
220
                                      (i_xx[6]<<7 | i_xx[6]>>1))+i_rsk[3]&0xff) :
221
                                  i_xx[7] ^ (((i_xx[6]<<1 | i_xx[6]>>7) ^ (i_xx[6]<<2 | i_xx[6]>>6) ^
222
                                      (i_xx[6]<<7 | i_xx[6]>>1))+i_rsk[0]&0xff) ;
223
 
224
        o_rf[6] = i_xx[6];
225
 
226
        o_rf[5] = (i_op == ENC) ? i_xx[5] + (((i_xx[4]<<3 | i_xx[4]>>5) ^ (i_xx[4]<<4 | i_xx[4]>>4) ^
227
                                      (i_xx[4]<<6 | i_xx[4]>>2))^i_rsk[2])&0xff :
228
                                  i_xx[5] - (((i_xx[4]<<3 | i_xx[4]>>5) ^ (i_xx[4]<<4 | i_xx[4]>>4) ^
229
                                      (i_xx[4]<<6 | i_xx[4]>>2))^i_rsk[1])&0xff;
230
 
231
        o_rf[4] = i_xx[4];
232
 
233
        o_rf[3] = (i_op == ENC) ? i_xx[3] ^ (((i_xx[2]<<1 | i_xx[2]>>7) ^ (i_xx[2]<<2 | i_xx[2]>>6) ^
234
                                      (i_xx[2]<<7 | i_xx[2]>>1))+i_rsk[1]&0xff) :
235
                                  i_xx[3] ^ (((i_xx[2]<<1 | i_xx[2]>>7) ^ (i_xx[2]<<2 | i_xx[2]>>6) ^
236
                                      (i_xx[2]<<7 | i_xx[2]>>1))+i_rsk[2]&0xff);
237
 
238
        o_rf[2] =  i_xx[2];
239
 
240
        o_rf[1] = (i_op == ENC) ? i_xx[1] + (((i_xx[0]<<3 | i_xx[0]>>5) ^ (i_xx[0]<<4 | i_xx[0]>>4) ^
241
                                      (i_xx[0]<<6 | i_xx[0]>>2))^i_rsk[0])&0xff :
242
                                  i_xx[1] - (((i_xx[0]<<3 | i_xx[0]>>5) ^ (i_xx[0]<<4 | i_xx[0]>>4) ^
243
                                      (i_xx[0]<<6 | i_xx[0]>>2))^i_rsk[3])&0xff;
244
 
245
        o_rf[0] =  i_xx[0];
246
 
247
}
248
 
249
 
250
/* =====================================
251
 
252
    FinalWhiteningFunction (FWF)
253
 
254
=======================================*/
255
void FinalWhiteningFunction(int i_op, byte *i_rf, byte *i_wk, byte *o_cp_text)
256
{
257
 
258
        o_cp_text[7] = (i_op == ENC) ? i_rf[7] :
259
                                       i_rf[7] ;
260
        o_cp_text[6] = (i_op == ENC) ? i_rf[6] ^ i_wk[7] :
261
                                           i_rf[6] ^ i_wk[7] ; // op == DEC  ;    
262
        o_cp_text[5] = (i_op == ENC) ? i_rf[5] :
263
                                       i_rf[5];
264
        o_cp_text[4] = (i_op == ENC) ? (i_rf[4] + i_wk[6])&0xff :
265
                                           (i_rf[4] - i_wk[6])&0xff  ;
266
        o_cp_text[3] = (i_op == ENC) ? i_rf[3] :
267
                                           i_rf[3] ;
268
        o_cp_text[2] = (i_op == ENC) ? i_rf[2] ^ i_wk[5] :
269
                                           i_rf[2] ^ i_wk[5] ; // op == DEC
270
        o_cp_text[1] = (i_op == ENC) ? i_rf[1] :
271
                                           i_rf[1] ;
272
        o_cp_text[0] = (i_op == ENC) ? (i_rf[0] + i_wk[4])&0xff :
273
                                           (i_rf[0] - i_wk[4])&0xff ; // op == DEC;
274
 
275
}
276
 
277
 
278
/* =====================================
279
 
280
    HightTop (HT)
281
 
282
=======================================*/
283
void HightTop(int i_op, HIGHT_DATA *p_hight_data)
284
{
285
        int i;
286
        byte w_sk[128] = {0};
287
        byte w_wf1[8] = {0};
288
        byte w_wf2[8] = {0};
289
 
290
        // Whitening Key Generation
291
        WhiteningKeyGen(p_hight_data->i_mk, p_hight_data->wk);
292
 
293
        // Delta Generation
294
        DeltaGen(p_hight_data->delta);
295
 
296
        // Sub Key Generation
297
        SubKeyGen(p_hight_data->i_mk,
298
                          p_hight_data->delta,
299
                          p_hight_data->sk);
300
 
301
        // re-arrange subkey index 
302
        if(i_op == ENC) {
303
                for(i=0; i<=127; i++)
304
                        w_sk[i] = p_hight_data->sk[i];
305
        } else if(i_op == DEC) {
306
                for(i=0; i<=127; i++)
307
                        w_sk[127-i] = p_hight_data->sk[i];
308
        } else {
309
                printf("Error\n");
310
        }
311
 
312
        // select proper WF related to i_op 
313
        if(i_op == ENC) {
314
                InitialWhiteningFunction(i_op, p_hight_data->i_pct, p_hight_data->wk, p_hight_data->iwf);
315
                memcpy(w_wf1, p_hight_data->iwf, 8);
316
        } else if(i_op == DEC) {
317
                FinalWhiteningFunction(i_op, p_hight_data->i_pct, p_hight_data->wk, p_hight_data->fwf);
318
                memcpy(w_wf1, p_hight_data->fwf, 8);
319
        } else {
320
                printf("Error\n");
321
        }
322
 
323
 
324
        // RoundFunction1
325
        InterRoundFunction(i_op,
326
                                        w_wf1,
327
                                        w_sk,
328
                                        p_hight_data->rf[1]);
329
 
330
        // InterRoundFunction2~31
331
        for(i=1; i<31; i++){
332
                InterRoundFunction(i_op,
333
                                                p_hight_data->rf[i],
334
                                                w_sk+(i*4),
335
                                                p_hight_data->rf[i+1]);
336
        }
337
        // RoundFunction32
338
        FinalRoundFunction(i_op,
339
                                        p_hight_data->rf[31],
340
                                        w_sk+(31*4),
341
                                        p_hight_data->rf[32]);
342
 
343
        // select proper WF related to i_op 
344
        if(i_op == ENC){
345
                FinalWhiteningFunction(i_op, p_hight_data->rf[32], p_hight_data->wk, p_hight_data->fwf);
346
                memcpy(w_wf2, p_hight_data->fwf, 8);
347
        } else if(i_op == DEC) {
348
                InitialWhiteningFunction(i_op, p_hight_data->rf[32], p_hight_data->wk, p_hight_data->iwf);
349
                memcpy(w_wf2, p_hight_data->iwf, 8);
350
        } else {
351
                printf("Error\n");
352
        }
353
 
354
        // assign output
355
        memcpy(p_hight_data->o_cpt, w_wf2, 8);
356
}

powered by: WebSVN 2.1.0

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