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 /*****************************************************************************/
35 uint64_t camellia_f(uint64_t x, uint64_t k);
36 /*****************************************************************************/
37 uint64_t camellia_fl(uint64_t x, uint64_t k);
38 /*****************************************************************************/
39 uint64_t camellia_fl_inv(uint64_t y, uint64_t k);
40 /*****************************************************************************/
41 void change_endian(void* data, uint8_t length);
43 uint64_t PROGMEM camellia_sigma[6]={ / * 64 byte table * /
52 uint32_t PROGMEM camellia_sigma[12]={ /* 64 byte table */
53 0x3BCC908BL, 0xA09E667FL,
54 0x4CAA73B2L, 0xB67AE858L,
55 0xE94F82BEL, 0xC6EF372FL,
56 0xF1D36F1CL, 0x54FF53A5L,
57 0xDE682D1DL, 0x10E527FAL,
58 0xB3E6C1FDL, 0xB05688C2L
61 /* an ugly macro to load an entry form the table above */
63 #define SIGMA(p) (( ((uint64_t)(pgm_read_dword((prog_uint32_t*)camellia_sigma+2*(p)+1)))<<32) | \
64 ((uint64_t)(pgm_read_dword((prog_uint32_t*)camellia_sigma+2*(p)+0))) )
66 #define SIGMA(p) (( ((uint64_t)(pgm_read_dword(((prog_uint32_t*)camellia_sigma)[2*(p)+1])))<<32) | \
67 ((uint64_t)(pgm_read_dword(((prog_uint32_t*)camellia_sigma)[2*(p)+0]))) )
71 /*****************************************************************************/
73 void camellia128_ctx_dump(camellia128_ctx_t *s){
74 uart_putstr_P(PSTR("\r\n==State Dump=="));
75 uart_putstr_P(PSTR("\n\rKAl: ")); uart_hexdump(&(s->kal), 8);
76 uart_putstr_P(PSTR("\n\rKAr: ")); uart_hexdump(&(s->kar), 8);
77 uart_putstr_P(PSTR("\n\rKLl: ")); uart_hexdump(&(s->kll), 8);
78 uart_putstr_P(PSTR("\n\rKLr: ")); uart_hexdump(&(s->klr), 8);
82 /*****************************************************************************/
83 /* extern prog_uint64_t camellia_sigma[6]; */
85 void camellia128_init(uint8_t* key, camellia128_ctx_t* s){
87 s->kll = 0; /* ((uint64_t*)key)[0]; */
89 /* load the key, endian-adjusted, to kll,klr */
102 s->kar ^= camellia_f(s->kal, SIGMA(0));
103 s->kal ^= camellia_f(s->kar, SIGMA(1));
108 s->kar ^= camellia_f(s->kal, SIGMA(2));
109 s->kal ^= camellia_f(s->kar, SIGMA(3));
112 /*****************************************************************************/
113 void camellia128_keyop(camellia128_ctx_t* s, int8_t q);
114 /*****************************************************************************/
115 void camellia128_keyop_inv(camellia128_ctx_t* s, int8_t q);
116 /*****************************************************************************/
121 #define KEY_POSTC1 0x00
122 #define KEY_POSTC2 0x01
123 #define KEY_INC2 0x02
126 #define KEY_DIR_NORM 0x00
127 #define KEY_DIR_INV 0x04
129 #define KEY_AMMOUNT 0x08
130 #define KEY_ROL17 0x08
131 #define KEY_ROL15 0x00
133 void camellia_6rounds(camellia128_ctx_t* s, uint64_t* bl, uint64_t* br, uint8_t roundop, uint8_t keychoice);
134 /*****************************************************************************/
137 void camellia128_enc(void* block, camellia128_ctx_t* s){
139 #define BL (((uint64_t*)block)[0])
140 #define BR (((uint64_t*)block)[1])
141 /* endian adjustment */
149 change_endian(&BL, 64/8);
150 change_endian(&BR, 64/8);
157 camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_POSTC1 , 0x33);
159 camellia128_keyop(s, -1);
160 BL = camellia_fl(BL, s->kal);
161 BR = camellia_fl_inv(BR, s->kar);
162 camellia128_keyop(s, -1);
164 camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_INC2 , 0x34);
166 camellia128_keyop(s, 1);
167 BL = camellia_fl(BL, s->kll);
168 BR = camellia_fl_inv(BR, s->klr);
169 camellia128_keyop(s, 1);
171 camellia_6rounds(s, &BL, &BR, KEY_ROL17 | KEY_DIR_NORM | KEY_POSTC2 , 0x0C);
180 camellia128_keyop(s,1);
182 change_endian(&BL, 64/8);
183 change_endian(&BR, 64/8);
189 /*****************************************************************************/
191 void camellia128_dec(void* block, camellia128_ctx_t* s){
193 #define BL (((uint64_t*)block)[1])
194 #define BR (((uint64_t*)block)[0])
195 /* endian adjustment */
203 change_endian(&BL, 64/8);
204 change_endian(&BR, 64/8);
206 camellia128_keyop_inv(s, 1);
208 BR ^= s->kal; /* kw3 */
209 BL ^= s->kar; /* kw4 */
211 camellia_6rounds(s, &BR, &BL, KEY_ROL17 | KEY_DIR_INV | KEY_POSTC1 , 0x0C);
213 camellia128_keyop_inv(s, 1);
214 BR = camellia_fl(BR, s->klr);
215 BL = camellia_fl_inv(BL, s->kll);
216 camellia128_keyop_inv(s, 1);
218 camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_INC2 , 0x0B);
220 camellia128_keyop_inv(s, -1);
221 BR = camellia_fl(BR, s->kar);
222 BL = camellia_fl_inv(BL, s->kal);
223 camellia128_keyop_inv(s, -1);
225 camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_POSTC2 , 0x33);
228 BL ^= s->kll; /* kw1 */
229 BR ^= s->klr; /* kw2 */
235 change_endian(&BL, 64/8);
236 change_endian(&BR, 64/8);
240 /*****************************************************************************/
241 /*****************************************************************************/