]> git.cryptolib.org Git - avr-crypto-lib.git/blob - camellia.c
insereated GPLv3 stub
[avr-crypto-lib.git] / camellia.c
1 /* camellia.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-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 #include "uart.h"
31 #include "debug.h"
32 #include <util/delay.h>
33  
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);
42 /*
43 uint64_t PROGMEM camellia_sigma[6]={ / * 64 byte table * /
44         0xA09E667F3BCC908BLL,
45         0xB67AE8584CAA73B2LL,
46         0xC6EF372FE94F82BELL,
47         0x54FF53A5F1D36F1CLL,
48         0x10E527FADE682D1DLL,
49         0xB05688C2B3E6C1FDLL
50 };      
51 */
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
59 };
60
61 /* an ugly macro to load an entry form the table above */
62 /*
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))) )
65 */
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]))) )
68
69
70
71 /*****************************************************************************/
72
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);   
79         return;
80 }
81
82 /*****************************************************************************/
83 /* extern prog_uint64_t camellia_sigma[6]; */
84
85 void camellia128_init(camellia128_ctx_t* s, uint8_t* key){
86         uint8_t i;
87         s->kll = 0; /* ((uint64_t*)key)[0]; */
88         
89         /* load the key, endian-adjusted, to kll,klr */
90         for(i=0; i<8; ++i){
91                 s->kll <<= 8;
92                 s->kll |= *key++;
93         }
94         for(i=0; i<8; ++i){
95                 s->klr <<= 8;
96                 s->klr |= *key++;
97         }
98
99         s->kal = s->kll;
100         s->kar = s->klr;
101         
102         s->kar ^= camellia_f(s->kal, SIGMA(0));
103         s->kal ^= camellia_f(s->kar, SIGMA(1));
104         
105         s->kal ^= s->kll;
106         s->kar ^= s->klr;
107         
108         s->kar ^= camellia_f(s->kal, SIGMA(2));
109         s->kal ^= camellia_f(s->kar, SIGMA(3));
110 }
111
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 /*****************************************************************************/
117
118 #define SEL_KA 1
119 #define SEL_KL 0
120
121 #define KEY_POSTC1              0x00
122 #define KEY_POSTC2              0x01
123 #define KEY_INC2                0x02
124
125 #define KEY_DIR                 0x04
126 #define KEY_DIR_NORM    0x00
127 #define KEY_DIR_INV             0x04
128
129 #define KEY_AMMOUNT             0x08 
130 #define KEY_ROL17               0x08
131 #define KEY_ROL15               0x00
132
133 void camellia_6rounds(camellia128_ctx_t* s, uint64_t* bl, uint64_t* br, uint8_t roundop, uint8_t keychoice);
134 /*****************************************************************************/
135
136
137 void camellia128_enc(camellia128_ctx_t* s, void* block){
138
139         #define BL (((uint64_t*)block)[0])
140         #define BR (((uint64_t*)block)[1])
141         /* endian adjustment */
142          /*BL*/
143          /*     1 2 3 4 5 6 7 8
144           *             8 7 6 5 4 3 2 1
145           */
146          
147         uint64_t temp64;
148         
149         change_endian(&BL, 64/8);       
150         change_endian(&BR, 64/8);
151         
152         /* Prewhitening */
153         BL ^= s->kll;
154         BR ^= s->klr;
155         
156         /* the first 6 */
157         camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_POSTC1 , 0x33);
158         /* FL injection  */
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);
163         /* middle 6 */
164         camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_INC2 , 0x34);
165         /* FL injection  */
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);
170    /* last 6 */
171         camellia_6rounds(s, &BL, &BR, KEY_ROL17 | KEY_DIR_NORM | KEY_POSTC2 , 0x0C);
172         /* Postwhitening */
173         BR ^= s->kal;
174         BL ^= s->kar;
175         
176         temp64 = BR;
177         BR = BL;
178         BL = temp64;
179
180         camellia128_keyop(s,1);
181         
182         change_endian(&BL, 64/8);       
183         change_endian(&BR, 64/8);
184                 
185         #undef BL
186         #undef BR       
187 }
188
189 /*****************************************************************************/
190
191 void camellia128_dec(camellia128_ctx_t* s, void* block){
192
193         #define BL (((uint64_t*)block)[1])
194         #define BR (((uint64_t*)block)[0])
195         /* endian adjustment */
196          /*BL*/
197          /*     1 2 3 4 5 6 7 8
198           *             8 7 6 5 4 3 2 1
199           */
200          
201         uint64_t temp64;
202                 
203         change_endian(&BL, 64/8);       
204         change_endian(&BR, 64/8);
205                 
206         camellia128_keyop_inv(s, 1);
207         /* Prewhitening */
208         BR ^= s->kal; /* kw3 */
209         BL ^= s->kar; /* kw4 */
210         /* the first 6 */
211         camellia_6rounds(s, &BR, &BL, KEY_ROL17 | KEY_DIR_INV | KEY_POSTC1 , 0x0C);
212         /* FL injection  */
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);
217         /* middle 6 */  
218         camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_INC2 , 0x0B);
219         /* FL injection  */
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);
224    /* last 6 */
225         camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_POSTC2 , 0x33);
226         
227         /* Postwhitening */
228         BL ^= s->kll; /* kw1 */ 
229         BR ^= s->klr; /* kw2 */
230         
231         temp64 = BR;
232         BR = BL;
233         BL = temp64;
234         
235         change_endian(&BL, 64/8);       
236         change_endian(&BR, 64/8);
237                 
238 }
239
240 /*****************************************************************************/
241 /*****************************************************************************/
242
243
244
245 /* EOF */