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