3 This file is part of the Crypto-avr-lib/microcrypt-lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
28 #include <avr/pgmspace.h>
32 #include <util/delay.h>
34 /*****************************************************************************/
36 uint8_t rol(uint8_t a, uint8_t n){return ((a<<n) | (a>>(8-n)));}
38 /*****************************************************************************/
40 uint8_t ror(uint8_t a, uint8_t n){return ((a<<(8-n)) | (a>>n));}
42 /*****************************************************************************/
44 uint32_t rol32(uint32_t a, uint8_t n){
45 return ((a<<n)|(a>>(32-n)));
48 /*****************************************************************************/
50 uint64_t rol64(uint64_t a, uint8_t n){
51 return ((a<<n)|(a>>(64-n)));
54 /*****************************************************************************/
56 uint8_t camellia_s1_table[256] PROGMEM = {
57 112, 130, 44, 236, 179, 39, 192, 229, 228, 133, 87, 53, 234, 12, 174, 65,
58 35, 239, 107, 147, 69, 25, 165, 33, 237, 14, 79, 78, 29, 101, 146, 189,
59 134, 184, 175, 143, 124, 235, 31, 206, 62, 48, 220, 95, 94, 197, 11, 26,
60 166, 225, 57, 202, 213, 71, 93, 61, 217, 1, 90, 214, 81, 86, 108, 77,
61 139, 13, 154, 102, 251, 204, 176, 45, 116, 18, 43, 32, 240, 177, 132, 153,
62 223, 76, 203, 194, 52, 126, 118, 5, 109, 183, 169, 49, 209, 23, 4, 215,
63 20, 88, 58, 97, 222, 27, 17, 28, 50, 15, 156, 22, 83, 24, 242, 34,
64 254, 68, 207, 178, 195, 181, 122, 145, 36, 8, 232, 168, 96, 252, 105, 80,
65 170, 208, 160, 125, 161, 137, 98, 151, 84, 91, 30, 149, 224, 255, 100, 210,
66 16, 196, 0, 72, 163, 247, 117, 219, 138, 3, 230, 218, 9, 63, 221, 148,
67 135, 92, 131, 2, 205, 74, 144, 51, 115, 103, 246, 243, 157, 127, 191, 226,
68 82, 155, 216, 38, 200, 55, 198, 59, 129, 150, 111, 75, 19, 190, 99, 46,
69 233, 121, 167, 140, 159, 110, 188, 142, 41, 245, 249, 182, 47, 253, 180, 89,
70 120, 152, 6, 106, 231, 70, 113, 186, 212, 37, 171, 66, 136, 162, 141, 250,
71 114, 7, 185, 85, 248, 238, 172, 10, 54, 73, 42, 104, 60, 56, 241, 164,
72 64, 40, 211, 123, 187, 201, 67, 193, 21, 227, 173, 244, 119, 199, 128, 158
75 /*****************************************************************************/
77 uint8_t camellia_s1(uint8_t b){
78 return pgm_read_byte_near(&(camellia_s1_table[b]));
81 /*****************************************************************************/
83 uint8_t camellia_s2(uint8_t b){
84 return rol(pgm_read_byte_near(&(camellia_s1_table[b])),1);
87 /*****************************************************************************/
89 uint8_t camellia_s3(uint8_t b){
90 return ror(pgm_read_byte_near(&(camellia_s1_table[b])),1);
93 /*****************************************************************************/
95 uint8_t camellia_s4(uint8_t b){
96 return pgm_read_byte_near(&(camellia_s1_table[rol(b,1)]));
99 /*****************************************************************************/
101 uint64_t camellia_s(uint64_t d){
102 // uart_putstr("\n\r S von "); uart_hexdump(&(d), 8);
103 #define D ((uint8_t*)(&d))
104 D[7] = camellia_s1(D[7]);
105 D[6] = camellia_s2(D[6]);
106 D[5] = camellia_s3(D[5]);
107 D[4] = camellia_s4(D[4]);
109 D[3] = camellia_s2(D[3]);
110 D[2] = camellia_s3(D[2]);
111 D[1] = camellia_s4(D[1]);
112 D[0] = camellia_s1(D[0]);
114 // uart_putstr(" ist "); uart_hexdump(&(d), 8);
118 /*****************************************************************************/
120 uint64_t camellia_p(uint64_t d){
122 #define D ((uint8_t*)(&d))
123 #define Z ((uint8_t*)(&z))
125 Z[0] = D[4] ^ D[3] ^ D[1];
126 Z[1] = D[5] ^ D[0] ^ D[2];
127 Z[2] = D[6] ^ D[1] ^ D[3];
128 Z[3] = D[7] ^ D[2] ^ D[0];
129 Z[4] = D[0] ^ D[6] ^ D[5];
130 Z[5] = D[1] ^ D[7] ^ D[6];
131 Z[6] = D[2] ^ D[4] ^ D[7];
132 Z[7] = D[3] ^ D[5] ^ D[4];
134 // Z[7] = z1 z3 z4 z6 z7 z8
135 // uart_putstr("\n\r P von "); uart_hexdump(&(d), 8);
137 Z[7] = D[7] ^ D[5] ^ D[4] ^ D[2] ^ D[1] ^ D[0];
138 Z[6] = D[7] ^ D[6] ^ D[4] ^ D[3] ^ D[1] ^ D[0];
139 Z[5] = D[7] ^ D[6] ^ D[5] ^ D[3] ^ D[2] ^ D[0];
140 Z[4] = D[6] ^ D[5] ^ D[4] ^ D[3] ^ D[2] ^ D[1] ;
141 Z[3] = D[7] ^ D[6] ^ D[2] ^ D[1] ^ D[0];
142 Z[2] = D[6] ^ D[5] ^ D[3] ^ D[1] ^ D[0];
143 Z[1] = D[5] ^ D[4] ^ D[3] ^ D[2] ^ D[0];
144 Z[0] = D[7] ^ D[4] ^ D[3] ^ D[2] ^ D[1] ;
146 // uart_putstr(" ist "); uart_hexdump(&(z), 8);
153 /*****************************************************************************/
155 uint64_t camellia_f(uint64_t x, uint64_t k){
157 y = camellia_p(camellia_s(x ^ k));
159 uart_putstr("\r\nEfunc X=");
160 uart_hexdump(&(x), 8);
162 uart_hexdump(&(k), 8);
164 uart_hexdump(&(y), 8);
169 /*****************************************************************************/
171 uint64_t camellia_fl(uint64_t x, uint64_t k){
172 volatile uint64_t lx[1], lk[1], y[1];
175 #define Y ((uint32_t*)y)
176 #define X ((uint32_t*)lx)
177 #define K ((uint32_t*)lk)
179 Y[0] = rol32((X[1]) & K[1],1) ^ (X[0]); /* Yr */
180 Y[1] = (Y[0] | K[0]) ^ (X[1]); /* Yl */
183 uart_putstr("\r\nFL(");
184 uart_hexdump(&(x), 8);
186 uart_hexdump(&(k), 8);
196 /*****************************************************************************/
198 uint64_t camellia_fl_inv(uint64_t y, uint64_t k){
199 //volatile uint32_t xl, xr;
200 volatile uint64_t ly[1], lk[1], x[1];
202 #define Y ((uint32_t*)ly)
203 #define X ((uint32_t*)x)
204 #define K ((uint32_t*)lk)
206 X[1]=(Y[0] | K[0]) ^ Y[1];
207 X[0]=rol32((X[1] & K[1]),1) ^ Y[0];
210 uart_putstr("\r\nFL_inv(");
211 uart_hexdump(&(y), 8);
213 uart_hexdump(&(k), 8);
222 /*****************************************************************************/
224 uint64_t camellia_sigma[6]={
225 0xA09E667F3BCC908BLL,
226 0xB67AE8584CAA73B2LL,
227 0xC6EF372FE94F82BELL,
228 0x54FF53A5F1D36F1CLL,
229 0x10E527FADE682D1DLL,
233 /*****************************************************************************/
235 void camellia128_ctx_dump(camellia128_ctx_t *s){
236 uart_putstr("\r\n==State Dump==");
237 uart_putstr("\n\rKAl: "); uart_hexdump(&(s->kal), 8);
238 uart_putstr("\n\rKAr: "); uart_hexdump(&(s->kar), 8);
239 uart_putstr("\n\rKLl: "); uart_hexdump(&(s->kll), 8);
240 uart_putstr("\n\rKLr: "); uart_hexdump(&(s->klr), 8);
244 /*****************************************************************************/
246 void camellia128_init(camellia128_ctx_t* s, uint8_t* key){
248 s->kll = 0; //((uint64_t*)key)[0];
250 /* load the key, endian-adjusted, to kll,klr */
263 s->kar ^= camellia_f(s->kal, camellia_sigma[0]);
264 s->kal ^= camellia_f(s->kar, camellia_sigma[1]);
269 s->kar ^= camellia_f(s->kal, camellia_sigma[2]);
270 s->kal ^= camellia_f(s->kar, camellia_sigma[3]);
272 // uart_putstr("\n\r----------------init finished--------------------");
275 /*****************************************************************************/
277 void camellia128_keyop(camellia128_ctx_t* s, int8_t q){
278 /* first we do 16 bit left-rols for kl and ka (128bit each) */
281 temp = (s->kal)>>(64-16-q);
282 s->kal = s->kal<<(16+q) | s->kar>>(64-16-q);
283 s->kar = s->kar<<(16+q) | temp;
285 temp = (s->kll)>>(64-16-q);
286 s->kll = s->kll<<(16+q) | s->klr>>(64-16-q);
287 s->klr = s->klr<<(16+q) | temp;
288 /* after doing the 16-bit rol we have to rol 1 bit left or rigth depending on q */
291 /*****************************************************************************/
293 void camellia128_keyop_inv(camellia128_ctx_t* s, int8_t q){
294 /* first we do 16 bit right-rols for kl and ka (128bit each) */
297 temp = (s->kar)&(0xffffff>>(24-16-q));
298 s->kar = s->kar>>(16+q) | s->kal<<(64-16-q);
299 s->kal = s->kal>>(16+q) | ((uint64_t)temp)<<(64-16-q);
301 temp = (s->klr)&(0xffffff>>(24-16-q));
302 s->klr = s->klr>>(16+q) | s->kll<<(64-16-q);
303 s->kll = s->kll>>(16+q) | ((uint64_t)temp)<<(64-16-q);
304 /* after doing the 16-bit rol we have to rol 1 bit left or rigth depending on q */
307 /*****************************************************************************/
312 #define KEY_POSTC1 0x00
313 #define KEY_POSTC2 0x01
314 #define KEY_INC2 0x02
317 #define KEY_DIR_NORM 0x00
318 #define KEY_DIR_INV 0x04
320 #define KEY_AMMOUNT 0x08
321 #define KEY_ROL17 0x08
322 #define KEY_ROL15 0x00
324 void camellia_6rounds(camellia128_ctx_t* s, uint64_t* bl, uint64_t* br, uint8_t roundop, uint8_t keychoice){
331 for(i=0; i<3; ++i){ /* each cycle */
332 br[0] ^= camellia_f(bl[0],*(k[(keychoice&1)*2+((roundop&KEY_DIR)?1:0)]));
335 if((i == 1) && (roundop&KEY_INC2)){
336 ((roundop&KEY_DIR)?camellia128_keyop_inv:camellia128_keyop)(s,(roundop&KEY_AMMOUNT)?1:-1);
339 bl[0] ^= camellia_f(br[0],*(k[(keychoice&1)*2+((roundop&KEY_DIR)?0:1)]));
342 /* check if we should do some keyop */
343 if((i == (roundop&1)) && (!(roundop&KEY_INC2)) ){
344 ((roundop&KEY_DIR)?camellia128_keyop_inv:camellia128_keyop)(s,(roundop&KEY_AMMOUNT)?1:-1);
345 /* isn't it fuckin nice what we can do in C?! */
350 /*****************************************************************************/
353 void camellia128_enc(camellia128_ctx_t* s, void* block){
355 #define BL (((uint64_t*)block)[0])
356 #define BR (((uint64_t*)block)[1])
357 /* endian adjustment */
366 BL = temp64 >> 56 | temp64 << 56 /* swap the most out bytes (1 & 8) */
367 | (temp64 & (0xffLL<<48))>>(5*8) | (temp64 & (0xffLL<< 8))<<(5*8) /* 2 & 7 */
368 | (temp64 & (0xffLL<<40))>>(3*8) | (temp64 & (0xffLL<<16))<<(3*8) /* 3 & 6 */
369 | (temp64 & (0xffLL<<32))>>(1*8) | (temp64 & (0xffLL<<24))<<(1*8); /* 4 & 5 */
371 BR = temp64 >> 56 | temp64 << 56 /* swap the most out bytes (1 & 8) */
372 | (temp64 & (0xffLL<<48))>>(5*8) | (temp64 & (0xffLL<< 8))<<(5*8) /* 2 & 7 */
373 | (temp64 & (0xffLL<<40))>>(3*8) | (temp64 & (0xffLL<<16))<<(3*8) /* 3 & 6 */
374 | (temp64 & (0xffLL<<32))>>(1*8) | (temp64 & (0xffLL<<24))<<(1*8); /* 4 & 5 */
382 BR ^= camellia_f(BL, s->kal);
383 BL ^= camellia_f(BR, s->kar);
384 camellia128_keyop(s, -1);
385 BR ^= camellia_f(BL, s->kll);
386 BL ^= camellia_f(BR, s->klr);
387 BR ^= camellia_f(BL, s->kal);
388 BL ^= camellia_f(BR, s->kar);
390 camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_POSTC1 , 0x33);
392 camellia128_keyop(s, -1);
393 BL = camellia_fl(BL, s->kal);
394 BR = camellia_fl_inv(BR, s->kar);
395 camellia128_keyop(s, -1);
397 /* BR ^= camellia_f(BL, s->kll);
398 BL ^= camellia_f(BR, s->klr);
399 BR ^= camellia_f(BL, s->kal);
400 camellia128_keyop(s, -1);
401 BL ^= camellia_f(BR, s->klr);
402 BR ^= camellia_f(BL, s->kal);
403 BL ^= camellia_f(BR, s->kar);
405 camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_INC2 , 0x34);
407 camellia128_keyop(s, 1);
408 BL = camellia_fl(BL, s->kll);
409 BR = camellia_fl_inv(BR, s->klr);
410 camellia128_keyop(s, 1);
412 /* BR ^= camellia_f(BL, s->kll);
413 BL ^= camellia_f(BR, s->klr);
414 BR ^= camellia_f(BL, s->kal);
415 BL ^= camellia_f(BR, s->kar);
416 camellia128_keyop(s, 1);
418 BR ^= camellia_f(BL, s->kll);
419 BL ^= camellia_f(BR, s->klr);
421 camellia_6rounds(s, &BL, &BR, KEY_ROL17 | KEY_DIR_NORM | KEY_POSTC2 , 0x0C);
431 BL = temp64 >> 56 | temp64 << 56 /* swap the most out bytes (1 & 8) */
432 | (temp64 & (0xffLL<<48))>>(5*8) | (temp64 & (0xffLL<< 8))<<(5*8) /* 2 & 7 */
433 | (temp64 & (0xffLL<<40))>>(3*8) | (temp64 & (0xffLL<<16))<<(3*8) /* 3 & 6 */
434 | (temp64 & (0xffLL<<32))>>(1*8) | (temp64 & (0xffLL<<24))<<(1*8); /* 4 & 5 */
436 BR = temp64 >> 56 | temp64 << 56 /* swap the most out bytes (1 & 8) */
437 | (temp64 & (0xffLL<<48))>>(5*8) | (temp64 & (0xffLL<< 8))<<(5*8) /* 2 & 7 */
438 | (temp64 & (0xffLL<<40))>>(3*8) | (temp64 & (0xffLL<<16))<<(3*8) /* 3 & 6 */
439 | (temp64 & (0xffLL<<32))>>(1*8) | (temp64 & (0xffLL<<24))<<(1*8); /* 4 & 5 */
445 /*****************************************************************************/
447 void camellia128_dec(camellia128_ctx_t* s, void* block){
449 #define BL (((uint64_t*)block)[1])
450 #define BR (((uint64_t*)block)[0])
451 /* endian adjustment */
461 BL = temp64 >> 56 | temp64 << 56 /* swap the most out bytes (1 & 8) */
462 | (temp64 & (0xffLL<<48))>>(5*8) | (temp64 & (0xffLL<< 8))<<(5*8) /* 2 & 7 */
463 | (temp64 & (0xffLL<<40))>>(3*8) | (temp64 & (0xffLL<<16))<<(3*8) /* 3 & 6 */
464 | (temp64 & (0xffLL<<32))>>(1*8) | (temp64 & (0xffLL<<24))<<(1*8); /* 4 & 5 */
466 BR = temp64 >> 56 | temp64 << 56 /* swap the most out bytes (1 & 8) */
467 | (temp64 & (0xffLL<<48))>>(5*8) | (temp64 & (0xffLL<< 8))<<(5*8) /* 2 & 7 */
468 | (temp64 & (0xffLL<<40))>>(3*8) | (temp64 & (0xffLL<<16))<<(3*8) /* 3 & 6 */
469 | (temp64 & (0xffLL<<32))>>(1*8) | (temp64 & (0xffLL<<24))<<(1*8); /* 4 & 5 */
471 camellia128_keyop_inv(s, 1);
473 BR ^= s->kal; /* kw3 */
474 BL ^= s->kar; /* kw4 */
478 BL ^= camellia_f(BR, s->klr); /* k18 * /
479 BR ^= camellia_f(BL, s->kll); /* k17 * /
480 camellia128_keyop_inv(s, 1);
481 BL ^= camellia_f(BR, s->kar);
482 BR ^= camellia_f(BL, s->kal);
483 BL ^= camellia_f(BR, s->klr);
484 BR ^= camellia_f(BL, s->kll);
486 camellia_6rounds(s, &BR, &BL, KEY_ROL17 | KEY_DIR_INV | KEY_POSTC1 , 0x0C);
489 camellia128_keyop_inv(s, 1);
490 BR = camellia_fl(BR, s->klr);
491 BL = camellia_fl_inv(BL, s->kll);
492 camellia128_keyop_inv(s, 1);
494 /* BL ^= camellia_f(BR, s->kar);
495 BR ^= camellia_f(BL, s->kal);
496 BL ^= camellia_f(BR, s->klr);
497 camellia128_keyop_inv(s, -1);
498 BR ^= camellia_f(BL, s->kal);
499 BL ^= camellia_f(BR, s->klr);
500 BR ^= camellia_f(BL, s->kll);
502 camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_INC2 , 0x0B);
505 camellia128_keyop_inv(s, -1);
506 BR = camellia_fl(BR, s->kar);
507 BL = camellia_fl_inv(BL, s->kal);
508 camellia128_keyop_inv(s, -1);
511 BL ^= camellia_f(BR, s->kar);
512 BR ^= camellia_f(BL, s->kal);
513 BL ^= camellia_f(BR, s->klr);
514 BR ^= camellia_f(BL, s->kll);
515 camellia128_keyop_inv(s, -1);
516 BL ^= camellia_f(BR, s->kar);
517 BR ^= camellia_f(BL, s->kal);
519 camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_POSTC2 , 0x33);
522 BL ^= s->kll; /* kw1 */
523 BR ^= s->klr; /* kw2 */
530 BL = temp64 >> 56 | temp64 << 56 /* swap the most out bytes (1 & 8) */
531 | (temp64 & (0xffLL<<48))>>(5*8) | (temp64 & (0xffLL<< 8))<<(5*8) /* 2 & 7 */
532 | (temp64 & (0xffLL<<40))>>(3*8) | (temp64 & (0xffLL<<16))<<(3*8) /* 3 & 6 */
533 | (temp64 & (0xffLL<<32))>>(1*8) | (temp64 & (0xffLL<<24))<<(1*8); /* 4 & 5 */
535 BR = temp64 >> 56 | temp64 << 56 /* swap the most out bytes (1 & 8) */
536 | (temp64 & (0xffLL<<48))>>(5*8) | (temp64 & (0xffLL<< 8))<<(5*8) /* 2 & 7 */
537 | (temp64 & (0xffLL<<40))>>(3*8) | (temp64 & (0xffLL<<16))<<(3*8) /* 3 & 6 */
538 | (temp64 & (0xffLL<<32))>>(1*8) | (temp64 & (0xffLL<<24))<<(1*8); /* 4 & 5 */
542 /*****************************************************************************/
543 /*****************************************************************************/