]> git.cryptolib.org Git - arm-crypto-lib.git/blob - camellia/camellia_C.c
4dfd1619f3f9f101e161dfdf225ee9843c5f0c37
[arm-crypto-lib.git] / camellia / camellia_C.c
1 /* camellia_C.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /**
20  * 
21  * 
22  * 
23  * 
24  */
25  
26 #include <stdint.h>
27 #include "camellia.h"
28 #if 0
29  #include "cli.h"
30  #include "debug.h"
31  #include <util/delay.h>
32 #endif 
33 /*****************************************************************************/
34
35 static
36 uint8_t rol(uint8_t a, uint8_t n){
37         return ((a<<n) | (a>>(8-n)));
38 }
39
40 /*****************************************************************************/
41
42 static
43 uint8_t ror(uint8_t a, uint8_t n){
44         return ((a<<(8-n)) | (a>>n));
45 }
46
47 /*****************************************************************************/
48
49 static
50 uint32_t rol32(uint32_t a, uint8_t n){
51         return ((a<<n)|(a>>(32-n)));
52 }
53
54 /*****************************************************************************/
55 /*
56 static
57 uint64_t rol64(uint64_t a, uint8_t n){
58         return ((a<<n)|(a>>(64-n)));
59 }
60 */
61 /*****************************************************************************/
62  
63 static
64 const uint8_t camellia_s1_table[256] = {
65  112, 130,  44, 236, 179,  39, 192, 229, 228, 133,  87,  53, 234,  12, 174,  65,
66   35, 239, 107, 147,  69,  25, 165,  33, 237,  14,  79,  78,  29, 101, 146, 189,
67  134, 184, 175, 143, 124, 235,  31, 206,  62,  48, 220,  95,  94, 197,  11,  26,
68  166, 225,  57, 202, 213,  71,  93,  61, 217,   1,  90, 214,  81,  86, 108,  77,
69  139,  13, 154, 102, 251, 204, 176,  45, 116,  18,  43,  32, 240, 177, 132, 153,
70  223,  76, 203, 194,  52, 126, 118,   5, 109, 183, 169,  49, 209,  23,   4, 215,
71   20,  88,  58,  97, 222,  27,  17,  28,  50,  15, 156,  22,  83,  24, 242,  34,
72  254,  68, 207, 178, 195, 181, 122, 145,  36,   8, 232, 168,  96, 252, 105,  80,
73  170, 208, 160, 125, 161, 137,  98, 151,  84,  91,  30, 149, 224, 255, 100, 210,
74   16, 196,   0,  72, 163, 247, 117, 219, 138,   3, 230, 218,   9,  63, 221, 148,
75  135,  92, 131,   2, 205,  74, 144,  51, 115, 103, 246, 243, 157, 127, 191, 226,
76   82, 155, 216,  38, 200,  55, 198,  59, 129, 150, 111,  75,  19, 190,  99,  46,
77  233, 121, 167, 140, 159, 110, 188, 142,  41, 245, 249, 182,  47, 253, 180,  89,
78  120, 152,   6, 106, 231,  70, 113, 186, 212,  37, 171,  66, 136, 162, 141, 250,
79  114,   7, 185,  85, 248, 238, 172,  10,  54,  73,  42, 104,  60,  56, 241, 164,
80   64,  40, 211, 123, 187, 201,  67, 193,  21, 227, 173, 244, 119, 199, 128, 158
81 };
82
83 /*****************************************************************************/
84
85 static
86 uint8_t camellia_s1(uint8_t b){
87         return camellia_s1_table[b];
88 }
89
90 /*****************************************************************************/
91
92 static
93 uint8_t camellia_s2(uint8_t b){
94         return rol(camellia_s1_table[b], 1);
95 }
96
97 /*****************************************************************************/
98
99 static
100 uint8_t camellia_s3(uint8_t b){
101         return ror(camellia_s1_table[b], 1);
102 }
103
104 /*****************************************************************************/
105
106 static
107 uint8_t camellia_s4(uint8_t b){
108         return camellia_s1_table[rol(b, 1)];
109 }
110
111 /*****************************************************************************/
112
113 static
114 uint64_t camellia_s(uint64_t d){
115 //      cli_putstr("\n\r S von "); cli_hexdump(&(d), 8);
116         #define D ((uint8_t*)(&d))
117         D[7] = camellia_s1(D[7]);
118         D[6] = camellia_s2(D[6]);
119         D[5] = camellia_s3(D[5]);
120         D[4] = camellia_s4(D[4]);
121         
122         D[3] = camellia_s2(D[3]);
123         D[2] = camellia_s3(D[2]);
124         D[1] = camellia_s4(D[1]);
125         D[0] = camellia_s1(D[0]);
126         #undef D
127 //      cli_putstr(" ist "); cli_hexdump(&(d), 8);
128         return d;
129 }
130
131 /*****************************************************************************/
132
133 static
134 uint64_t camellia_p(uint64_t d){
135         uint64_t z=0;
136         #define D ((uint8_t*)(&d))
137         #define Z ((uint8_t*)(&z))
138 /*
139         Z[0] = D[4] ^ D[3] ^ D[1];
140         Z[1] = D[5] ^ D[0] ^ D[2];
141         Z[2] = D[6] ^ D[1] ^ D[3];
142         Z[3] = D[7] ^ D[2] ^ D[0];
143         Z[4] = D[0] ^ D[6] ^ D[5];
144         Z[5] = D[1] ^ D[7] ^ D[6];
145         Z[6] = D[2] ^ D[4] ^ D[7];
146         Z[7] = D[3] ^ D[5] ^ D[4];
147 */
148 //      Z[7] = z1 z3 z4 z6 z7 z8
149 //      cli_putstr("\n\r P von "); cli_hexdump(&(d), 8);
150         
151         Z[7] = D[7] ^        D[5] ^ D[4] ^        D[2] ^ D[1] ^ D[0];
152         Z[6] = D[7] ^ D[6]        ^ D[4] ^ D[3] ^        D[1] ^ D[0];
153         Z[5] = D[7] ^ D[6] ^ D[5] ^        D[3] ^ D[2] ^        D[0];
154         Z[4] =        D[6] ^ D[5] ^ D[4] ^ D[3] ^ D[2] ^ D[1]       ;
155         Z[3] = D[7] ^ D[6] ^                      D[2] ^ D[1] ^ D[0];
156         Z[2] =        D[6] ^ D[5] ^        D[3] ^        D[1] ^ D[0];
157         Z[1] =               D[5] ^ D[4] ^ D[3] ^ D[2] ^        D[0];
158         Z[0] = D[7] ^               D[4] ^ D[3] ^ D[2] ^ D[1]       ;
159         
160 //      cli_putstr(" ist "); cli_hexdump(&(z), 8);
161                         
162         #undef Z
163         #undef D
164         return z;
165 }
166
167 /*****************************************************************************/
168
169 static
170 uint64_t camellia_f(uint64_t x, uint64_t k){
171         uint64_t y;
172         y = camellia_p(camellia_s(x ^ k));
173         return y;
174 }
175
176 /*****************************************************************************/
177
178 static
179 uint64_t camellia_fl(uint64_t x, uint64_t k){
180 //      uint64_t lx, lk, y;
181         uint32_t lx[2], lk[2], yr, yl;
182         lx[0]=(uint32_t)x;
183         lx[1]=(uint32_t)(x>>32); 
184         lk[0]=(uint32_t)k; 
185         lk[1]=(uint32_t)(k>>32); 
186         #define Y ((uint32_t*)y)
187         #define X ((uint32_t*)lx)
188         #define K ((uint32_t*)lk)
189
190         yr = rol32((X[1]) & (K[1]) ,1) ^ (X[0]); /* Yr */
191         yl = (yr | K[0]) ^ (X[1]);           /* Yl */
192         
193 /*      
194         cli_putstr("\r\nFL(");
195         cli_hexdump(&(x), 8);
196         cli_putstr(", ");
197         cli_hexdump(&(k), 8);
198         cli_putstr(") = ");
199         cli_hexdump(y, 8);
200 */
201         #undef K
202         #undef X
203         #undef Y
204         return (((uint64_t)yl)<<32 | yr);       
205 }
206
207 /*****************************************************************************/
208
209 static
210 uint64_t camellia_fl_inv(uint64_t y, uint64_t k){
211 //volatile      uint32_t xl, xr;
212         uint32_t ly[2], lk[2], x[2];
213         ly[0]=(uint32_t)y;
214         ly[1]=(uint32_t)(y>>32);
215         lk[0]=(uint32_t)k; 
216         lk[1]=(uint32_t)(k>>32); 
217         #define Y ((uint32_t*)ly) 
218         #define X ((uint32_t*)x) 
219         #define K ((uint32_t*)lk) 
220         
221         X[1]=(Y[0] | K[0]) ^ Y[1];
222         X[0]=rol32((X[1] & K[1]),1) ^ Y[0];
223         
224 /*      
225         cli_putstr("\r\nFL_inv(");
226         cli_hexdump(&(y), 8);
227         cli_putstr(", ");
228         cli_hexdump(&(k), 8);
229         cli_putstr(") = ");
230 */      
231         #undef K
232         #undef X
233         #undef Y
234         return ((uint64_t)(x[1]))<<32 | x[0];   
235 }
236
237 /*****************************************************************************/
238
239 static
240 const uint64_t camellia_sigma_table[6] = {
241         0xA09E667F3BCC908BLL,
242         0xB67AE8584CAA73B2LL,
243         0xC6EF372FE94F82BELL,
244         0x54FF53A5F1D36F1CLL,
245         0x10E527FADE682D1DLL,
246         0xB05688C2B3E6C1FDLL
247 };      
248
249 /*****************************************************************************/
250 #if 0
251 void camellia128_ctx_dump(camellia128_ctx_t *s){
252         cli_putstr("\r\n==State Dump==");
253         cli_putstr("\n\rKAl: "); cli_hexdump(&(s->kal), 8);
254         cli_putstr("\n\rKAr: "); cli_hexdump(&(s->kar), 8);
255         cli_putstr("\n\rKLl: "); cli_hexdump(&(s->kll), 8);
256         cli_putstr("\n\rKLr: "); cli_hexdump(&(s->klr), 8);     
257         return;
258 }
259 #endif
260 /*****************************************************************************/
261
262 void camellia128_init(const void* key, camellia128_ctx_t* s){
263         uint8_t i;
264         s->kll = 0; //((uint64_t*)key)[0];
265         
266         /* load the key, endian-adjusted, to kll,klr */
267         for(i=0; i<8; ++i){
268                 s->kll <<= 8;
269                 s->kll |= *((uint8_t*)key);
270                 key = (uint8_t*)key+1;
271         }
272         for(i=0; i<8; ++i){
273                 s->klr <<= 8;
274                 s->klr |= *((uint8_t*)key);
275                 key = (uint8_t*)key+1;
276         }
277         
278         s->kal = s->kll;
279         s->kar = s->klr;
280         
281         s->kar ^= camellia_f(s->kal, camellia_sigma_table[0]);
282         s->kal ^= camellia_f(s->kar, camellia_sigma_table[1]);
283         
284         s->kal ^= s->kll;
285         s->kar ^= s->klr;
286         
287         s->kar ^= camellia_f(s->kal, camellia_sigma_table[2]);
288         s->kal ^= camellia_f(s->kar, camellia_sigma_table[3]);
289         /**/
290 //      cli_putstr("\n\r----------------init finished--------------------");
291 }
292
293 /*****************************************************************************/
294
295 static
296 void camellia128_keyop(camellia128_ctx_t* s, int8_t q){
297         /* first we do 16 bit left-rols for kl and ka (128bit each) */
298         uint32_t temp;
299         
300         temp = (s->kal)>>(64-16-q);
301         s->kal = s->kal<<(16+q) | s->kar>>(64-16-q);
302         s->kar = s->kar<<(16+q) | temp;
303         
304         temp = (s->kll)>>(64-16-q);
305         s->kll = s->kll<<(16+q) | s->klr>>(64-16-q);
306         s->klr = s->klr<<(16+q) | temp;
307         /* after doing the 16-bit rol we have to rol 1 bit left or rigth depending on q */
308 }
309
310 /*****************************************************************************/
311
312 static
313 void camellia128_keyop_inv(camellia128_ctx_t* s, int8_t q){
314         /* first we do 16 bit right-rols for kl and ka (128bit each) */
315         uint32_t temp;
316         
317         temp = (s->kar)&(0xffffff>>(24-16-q));
318         s->kar = s->kar>>(16+q) | s->kal<<(64-16-q);
319         s->kal = s->kal>>(16+q) | ((uint64_t)temp)<<(64-16-q);
320         
321         temp = (s->klr)&(0xffffff>>(24-16-q));
322         s->klr = s->klr>>(16+q) | s->kll<<(64-16-q);
323         s->kll = s->kll>>(16+q) | ((uint64_t)temp)<<(64-16-q);
324         /* after doing the 16-bit rol we have to rol 1 bit left or rigth depending on q */
325 }
326
327 /*****************************************************************************/
328
329 #define SEL_KA 1
330 #define SEL_KL 0
331
332 #define KEY_POSTC1              0x00
333 #define KEY_POSTC2              0x01
334 #define KEY_INC2                0x02
335
336 #define KEY_DIR                 0x04
337 #define KEY_DIR_NORM            0x00
338 #define KEY_DIR_INV             0x04
339
340 #define KEY_AMMOUNT             0x08 
341 #define KEY_ROL17               0x08
342 #define KEY_ROL15               0x00
343
344 static
345 void camellia_6rounds(const camellia128_ctx_t* s, uint64_t* bl, uint64_t* br, uint8_t roundop, uint8_t keychoice){
346         uint8_t i;
347         uint64_t* k[4];
348         k[0] = &(((camellia128_ctx_t*)s)->kll);
349         k[1] = &(((camellia128_ctx_t*)s)->klr);
350         k[2] = &(((camellia128_ctx_t*)s)->kal);
351         k[3] = &(((camellia128_ctx_t*)s)->kar);
352         for(i=0; i<3; ++i){ /* each cycle */
353                 br[0] ^= camellia_f(bl[0],*(k[(keychoice&1)*2+((roundop&KEY_DIR)?1:0)]));
354                 keychoice >>= 1;
355                 
356                 if((i == 1) && (roundop&KEY_INC2)){
357                         ((roundop&KEY_DIR)?camellia128_keyop_inv:camellia128_keyop)(((camellia128_ctx_t*)s),(roundop&KEY_AMMOUNT)?1:-1);
358                 }
359                 
360                 bl[0] ^= camellia_f(br[0],*(k[(keychoice&1)*2+((roundop&KEY_DIR)?0:1)]));
361                 keychoice >>= 1;
362                 
363                 /* check if we should do some keyop */
364                 if((i == (roundop&1)) && (!(roundop&KEY_INC2)) ){
365                         ((roundop&KEY_DIR)?camellia128_keyop_inv:camellia128_keyop)(((camellia128_ctx_t*)s),(roundop&KEY_AMMOUNT)?1:-1);
366                         /* isn't it fuckin nice what we can do in C?! */
367                 }
368         }
369 }
370
371 /*****************************************************************************/
372
373 static
374 void change_endian(void* data, uint8_t length){
375         uint8_t i,a;
376         for(i=0; i<length/2; ++i){
377                 a = ((uint8_t*)data)[i];
378                 ((uint8_t*)data)[i] = ((uint8_t*)data)[length-i-1];
379                 ((uint8_t*)data)[length-i-1] = a;
380         }
381 }
382
383 /*****************************************************************************/
384
385 void camellia128_enc(void* block, const camellia128_ctx_t* s){
386
387         #define BL (((uint64_t*)block)[0])
388         #define BR (((uint64_t*)block)[1])
389         /* endian adjustment */
390          /*BL*/
391          /* 1 2 3 4 5 6 7 8
392           *     8 7 6 5 4 3 2 1
393           */
394          
395         uint64_t temp64;
396         
397         change_endian(&BL, 64/8);       
398         change_endian(&BR, 64/8);
399         
400         /* Prewhitening */
401         BL ^= s->kll;
402         BR ^= s->klr;
403         
404         /* the first 6 */
405         camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_POSTC1 , 0x33);
406         /* FL injection  */
407    camellia128_keyop((camellia128_ctx_t*)s, -1);
408         BL = camellia_fl(BL, s->kal);
409         BR = camellia_fl_inv(BR, s->kar);
410    camellia128_keyop((camellia128_ctx_t*)s, -1);
411         /* middle 6 */
412         camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_INC2 , 0x34);
413         /* FL injection  */
414    camellia128_keyop((camellia128_ctx_t*)s, 1);
415         BL = camellia_fl(BL, s->kll);
416         BR = camellia_fl_inv(BR, s->klr);
417    camellia128_keyop((camellia128_ctx_t*)s, 1);
418    /* last 6 */
419         camellia_6rounds(s, &BL, &BR, KEY_ROL17 | KEY_DIR_NORM | KEY_POSTC2 , 0x0C);
420         /* Postwhitening */
421         BR ^= s->kal;
422         BL ^= s->kar;
423         
424         temp64 = BR;
425         BR = BL;
426         BL = temp64;
427
428         camellia128_keyop((camellia128_ctx_t*)s,1);
429         
430         change_endian(&BL, 64/8);       
431         change_endian(&BR, 64/8);
432                 
433         #undef BL
434         #undef BR       
435 }
436
437 /*****************************************************************************/
438
439 void camellia128_dec(void* block, const camellia128_ctx_t* s){
440
441         #define BL (((uint64_t*)block)[1])
442         #define BR (((uint64_t*)block)[0])
443         /* endian adjustment */
444          /*BL*/
445          /* 1 2 3 4 5 6 7 8
446           * 8 7 6 5 4 3 2 1
447           */
448          
449         uint64_t temp64;
450         change_endian(&BL, 64/8);       
451         change_endian(&BR, 64/8);
452                 
453         camellia128_keyop_inv((camellia128_ctx_t*)s, 1);
454         /* Prewhitening */
455         BR ^= s->kal; /* kw3 */
456         BL ^= s->kar; /* kw4 */
457         /* the first 6 */
458         camellia_6rounds(s, &BR, &BL, KEY_ROL17 | KEY_DIR_INV | KEY_POSTC1 , 0x0C);
459         /* FL injection  */
460    camellia128_keyop_inv((camellia128_ctx_t*)s, 1);
461         BR = camellia_fl(BR, s->klr);
462         BL = camellia_fl_inv(BL, s->kll);
463    camellia128_keyop_inv((camellia128_ctx_t*)s, 1);
464         /* middle 6 */  
465         camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_INC2 , 0x0B);
466         /* FL injection  */
467    camellia128_keyop_inv((camellia128_ctx_t*)s, -1);
468         BR = camellia_fl(BR, s->kar);
469         BL = camellia_fl_inv(BL, s->kal);
470    camellia128_keyop_inv((camellia128_ctx_t*)s, -1);
471    /* last 6 */
472         camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_POSTC2 , 0x33);
473         
474         /* Postwhitening */
475         BL ^= s->kll; /* kw1 */ 
476         BR ^= s->klr; /* kw2 */
477         
478         temp64 = BR;
479         BR = BL;
480         BL = temp64;
481         
482         change_endian(&BL, 64/8);       
483         change_endian(&BR, 64/8);
484                 
485 }
486
487
488
489 /*****************************************************************************/
490 /*****************************************************************************/
491
492
493
494 /* EOF */