10 #include <avr/pgmspace.h>
14 #include <util/delay.h>
16 /*****************************************************************************/
17 uint64_t camellia_f(uint64_t x, uint64_t k);
18 /*****************************************************************************/
19 uint64_t camellia_fl(uint64_t x, uint64_t k);
20 /*****************************************************************************/
21 uint64_t camellia_fl_inv(uint64_t y, uint64_t k);
22 /*****************************************************************************/
23 void change_endian(void* data, uint8_t length);
25 uint64_t camellia_sigma[6]={
34 /*****************************************************************************/
36 void camellia128_ctx_dump(camellia128_ctx_t *s){
37 uart_putstr("\r\n==State Dump==");
38 uart_putstr("\n\rKAl: "); uart_hexdump(&(s->kal), 8);
39 uart_putstr("\n\rKAr: "); uart_hexdump(&(s->kar), 8);
40 uart_putstr("\n\rKLl: "); uart_hexdump(&(s->kll), 8);
41 uart_putstr("\n\rKLr: "); uart_hexdump(&(s->klr), 8);
45 /*****************************************************************************/
47 //extern prog_uint64_t camellia_sigma[6];
49 void camellia128_init(camellia128_ctx_t* s, uint8_t* key){
51 s->kll = 0; //((uint64_t*)key)[0];
53 // / * load the key, endian-adjusted, to kll,klr * /
66 s->kar ^= camellia_f(s->kal, camellia_sigma[0]);
67 s->kal ^= camellia_f(s->kar, camellia_sigma[1]);
72 s->kar ^= camellia_f(s->kal, camellia_sigma[2]);
73 s->kal ^= camellia_f(s->kar, camellia_sigma[3]);
75 // uart_putstr("\n\r----------------init finished--------------------");
78 /*****************************************************************************/
79 void camellia128_keyop(camellia128_ctx_t* s, int8_t q);
80 /*****************************************************************************/
81 void camellia128_keyop_inv(camellia128_ctx_t* s, int8_t q);
82 /*****************************************************************************/
87 #define KEY_POSTC1 0x00
88 #define KEY_POSTC2 0x01
92 #define KEY_DIR_NORM 0x00
93 #define KEY_DIR_INV 0x04
95 #define KEY_AMMOUNT 0x08
96 #define KEY_ROL17 0x08
97 #define KEY_ROL15 0x00
99 void camellia_6rounds(camellia128_ctx_t* s, uint64_t* bl, uint64_t* br, uint8_t roundop, uint8_t keychoice);
100 /*****************************************************************************/
103 void camellia128_enc(camellia128_ctx_t* s, void* block){
105 #define BL (((uint64_t*)block)[0])
106 #define BR (((uint64_t*)block)[1])
107 /* endian adjustment */
115 change_endian(&BL, 64/8);
116 change_endian(&BR, 64/8);
123 camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_POSTC1 , 0x33);
125 camellia128_keyop(s, -1);
126 BL = camellia_fl(BL, s->kal);
127 BR = camellia_fl_inv(BR, s->kar);
128 camellia128_keyop(s, -1);
130 camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_INC2 , 0x34);
132 camellia128_keyop(s, 1);
133 BL = camellia_fl(BL, s->kll);
134 BR = camellia_fl_inv(BR, s->klr);
135 camellia128_keyop(s, 1);
137 camellia_6rounds(s, &BL, &BR, KEY_ROL17 | KEY_DIR_NORM | KEY_POSTC2 , 0x0C);
147 change_endian(&BL, 64/8);
148 change_endian(&BR, 64/8);
154 /*****************************************************************************/
156 void camellia128_dec(camellia128_ctx_t* s, void* block){
158 #define BL (((uint64_t*)block)[1])
159 #define BR (((uint64_t*)block)[0])
160 /* endian adjustment */
168 change_endian(&BL, 64/8);
169 change_endian(&BR, 64/8);
171 camellia128_keyop_inv(s, 1);
173 BR ^= s->kal; /* kw3 */
174 BL ^= s->kar; /* kw4 */
176 camellia_6rounds(s, &BR, &BL, KEY_ROL17 | KEY_DIR_INV | KEY_POSTC1 , 0x0C);
178 camellia128_keyop_inv(s, 1);
179 BR = camellia_fl(BR, s->klr);
180 BL = camellia_fl_inv(BL, s->kll);
181 camellia128_keyop_inv(s, 1);
183 camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_INC2 , 0x0B);
185 camellia128_keyop_inv(s, -1);
186 BR = camellia_fl(BR, s->kar);
187 BL = camellia_fl_inv(BL, s->kal);
188 camellia128_keyop_inv(s, -1);
190 camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_POSTC2 , 0x33);
193 BL ^= s->kll; /* kw1 */
194 BR ^= s->klr; /* kw2 */
200 change_endian(&BL, 64/8);
201 change_endian(&BR, 64/8);
205 /*****************************************************************************/
206 /*****************************************************************************/