1 /* camellia128-stub.c */
3 This file is part of the AVR-Crypto-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>
33 #include <util/delay.h>
35 /*****************************************************************************/
36 uint64_t camellia_f(uint64_t x, uint64_t k);
37 /*****************************************************************************/
38 uint64_t camellia_fl(uint64_t x, uint64_t k);
39 /*****************************************************************************/
40 uint64_t camellia_fl_inv(uint64_t y, uint64_t k);
41 /*****************************************************************************/
42 void change_endian(void *data, uint8_t length);
44 uint64_t PROGMEM camellia_sigma[6]={ / * 64 byte table * /
53 const uint32_t camellia_sigma[12] PROGMEM = { /* 48 byte table */
54 0x3BCC908BL, 0xA09E667FL,
55 0x4CAA73B2L, 0xB67AE858L,
56 0xE94F82BEL, 0xC6EF372FL,
57 0xF1D36F1CL, 0x54FF53A5L,
58 0xDE682D1DL, 0x10E527FAL,
59 0xB3E6C1FDL, 0xB05688C2L
62 /* an ugly macro to load an entry form the table above */
64 #define SIGMA(p) (( ((uint64_t)(pgm_read_dword((uint32_t*)camellia_sigma+2*(p)+1)))<<32) | \
65 ((uint64_t)(pgm_read_dword((uint32_t*)camellia_sigma+2*(p)+0))) )
67 #define SIGMA(p) (( ((uint64_t)(pgm_read_dword(&(((uint32_t*)camellia_sigma)[2*(p)+1]))))<<32) | \
68 ((uint64_t)(pgm_read_dword(&(((uint32_t*)camellia_sigma)[2*(p)+0])))) )
72 /*****************************************************************************/
74 void camellia128_ctx_dump(camellia128_ctx_t *s){
75 cli_putstr_P(PSTR("\r\n==State Dump=="));
76 cli_putstr_P(PSTR("\n\rKAl: ")); cli_hexdump(&(s->kal), 8);
77 cli_putstr_P(PSTR("\n\rKAr: ")); cli_hexdump(&(s->kar), 8);
78 cli_putstr_P(PSTR("\n\rKLl: ")); cli_hexdump(&(s->kll), 8);
79 cli_putstr_P(PSTR("\n\rKLr: ")); cli_hexdump(&(s->klr), 8);
83 /*****************************************************************************/
84 /* extern uint64_t camellia_sigma[6]; */
86 void camellia128_init(const void *key, camellia128_ctx_t *s){
88 s->kll = 0; /* ((uint64_t*)key)[0]; */
90 /* load the key, endian-adjusted, to kll,klr */
93 s->kll |= *((uint8_t*)key);
94 key = (uint8_t*)key+1;
98 s->klr |= *((uint8_t*)key);
99 key = (uint8_t*)key+1;
105 s->kar ^= camellia_f(s->kal, SIGMA(0));
106 s->kal ^= camellia_f(s->kar, SIGMA(1));
111 s->kar ^= camellia_f(s->kal, SIGMA(2));
112 s->kal ^= camellia_f(s->kar, SIGMA(3));
115 /*****************************************************************************/
116 void camellia128_keyop(camellia128_ctx_t *s, int8_t q);
117 /*****************************************************************************/
118 void camellia128_keyop_inv(camellia128_ctx_t *s, int8_t q);
119 /*****************************************************************************/
124 #define KEY_POSTC1 0x00
125 #define KEY_POSTC2 0x01
126 #define KEY_INC2 0x02
129 #define KEY_DIR_NORM 0x00
130 #define KEY_DIR_INV 0x04
132 #define KEY_AMMOUNT 0x08
133 #define KEY_ROL17 0x08
134 #define KEY_ROL15 0x00
136 void camellia_6rounds(const camellia128_ctx_t *s, uint64_t *bl, uint64_t *br,
137 uint8_t roundop, uint8_t keychoice);
138 /*****************************************************************************/
141 void camellia128_enc(void *block, const camellia128_ctx_t *s){
143 #define BL (((uint64_t*)block)[0])
144 #define BR (((uint64_t*)block)[1])
145 /* endian adjustment */
153 change_endian(&BL, 64/8);
154 change_endian(&BR, 64/8);
161 camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_POSTC1 , 0x33);
163 camellia128_keyop((camellia128_ctx_t*)s, -1);
164 BL = camellia_fl(BL, s->kal);
165 BR = camellia_fl_inv(BR, s->kar);
166 camellia128_keyop((camellia128_ctx_t*)s, -1);
168 camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_INC2 , 0x34);
170 camellia128_keyop((camellia128_ctx_t*)s, 1);
171 BL = camellia_fl(BL, s->kll);
172 BR = camellia_fl_inv(BR, s->klr);
173 camellia128_keyop((camellia128_ctx_t*)s, 1);
175 camellia_6rounds(s, &BL, &BR, KEY_ROL17 | KEY_DIR_NORM | KEY_POSTC2 , 0x0C);
184 camellia128_keyop((camellia128_ctx_t*)s,1);
186 change_endian(&BL, 64/8);
187 change_endian(&BR, 64/8);
193 /*****************************************************************************/
195 void camellia128_dec(void *block, const camellia128_ctx_t *s){
197 #define BL (((uint64_t*)block)[1])
198 #define BR (((uint64_t*)block)[0])
199 /* endian adjustment */
207 change_endian(&BL, 64/8);
208 change_endian(&BR, 64/8);
210 camellia128_keyop_inv((camellia128_ctx_t*)s, 1);
212 BR ^= s->kal; /* kw3 */
213 BL ^= s->kar; /* kw4 */
215 camellia_6rounds(s, &BR, &BL, KEY_ROL17 | KEY_DIR_INV | KEY_POSTC1 , 0x0C);
217 camellia128_keyop_inv((camellia128_ctx_t*)s, 1);
218 BR = camellia_fl(BR, s->klr);
219 BL = camellia_fl_inv(BL, s->kll);
220 camellia128_keyop_inv((camellia128_ctx_t*)s, 1);
222 camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_INC2 , 0x0B);
224 camellia128_keyop_inv((camellia128_ctx_t*)s, -1);
225 BR = camellia_fl(BR, s->kar);
226 BL = camellia_fl_inv(BL, s->kal);
227 camellia128_keyop_inv((camellia128_ctx_t*)s, -1);
229 camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_POSTC2 , 0x33);
232 BL ^= s->kll; /* kw1 */
233 BR ^= s->klr; /* kw2 */
239 change_endian(&BL, 64/8);
240 change_endian(&BR, 64/8);
244 /*****************************************************************************/
245 /*****************************************************************************/