]> git.cryptolib.org Git - avr-crypto-lib.git/blob - camellia.c
+ fix hmac-sha256
[avr-crypto-lib.git] / camellia.c
1 /**
2  * 
3  * 
4  * 
5  * 
6  */
7  
8 #include <stdint.h>
9 #include <avr/io.h>
10 #include <avr/pgmspace.h>
11 #include "camellia.h"
12 #include "uart.h"
13 #include "debug.h"
14 #include <util/delay.h>
15  
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);
24
25 uint64_t camellia_sigma[6]={
26         0xA09E667F3BCC908BLL,
27         0xB67AE8584CAA73B2LL,
28         0xC6EF372FE94F82BELL,
29         0x54FF53A5F1D36F1CLL,
30         0x10E527FADE682D1DLL,
31         0xB05688C2B3E6C1FDLL
32 };      
33
34 /*****************************************************************************/
35
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);   
42         return;
43 }
44
45 /*****************************************************************************/
46 //*
47 //extern prog_uint64_t camellia_sigma[6];
48
49 void camellia128_init(camellia128_ctx_t* s, uint8_t* key){
50         uint8_t i;
51         s->kll = 0; //((uint64_t*)key)[0];
52         
53 //      / * load the key, endian-adjusted, to kll,klr * /
54         for(i=0; i<8; ++i){
55                 s->kll <<= 8;
56                 s->kll |= *key++;
57         }
58         for(i=0; i<8; ++i){
59                 s->klr <<= 8;
60                 s->klr |= *key++;
61         }
62         
63         s->kal = s->kll;
64         s->kar = s->klr;
65         
66         s->kar ^= camellia_f(s->kal, camellia_sigma[0]);
67         s->kal ^= camellia_f(s->kar, camellia_sigma[1]);
68         
69         s->kal ^= s->kll;
70         s->kar ^= s->klr;
71         
72         s->kar ^= camellia_f(s->kal, camellia_sigma[2]);
73         s->kal ^= camellia_f(s->kar, camellia_sigma[3]);
74 //      / ** /
75 //      uart_putstr("\n\r----------------init finished--------------------");
76 }
77 //*/
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 /*****************************************************************************/
83
84 #define SEL_KA 1
85 #define SEL_KL 0
86
87 #define KEY_POSTC1              0x00
88 #define KEY_POSTC2              0x01
89 #define KEY_INC2                0x02
90
91 #define KEY_DIR                 0x04
92 #define KEY_DIR_NORM    0x00
93 #define KEY_DIR_INV             0x04
94
95 #define KEY_AMMOUNT             0x08 
96 #define KEY_ROL17               0x08
97 #define KEY_ROL15               0x00
98
99 void camellia_6rounds(camellia128_ctx_t* s, uint64_t* bl, uint64_t* br, uint8_t roundop, uint8_t keychoice);
100 /*****************************************************************************/
101
102
103 void camellia128_enc(camellia128_ctx_t* s, void* block){
104
105         #define BL (((uint64_t*)block)[0])
106         #define BR (((uint64_t*)block)[1])
107         /* endian adjustment */
108          /*BL*/
109          /*     1 2 3 4 5 6 7 8
110           *             8 7 6 5 4 3 2 1
111           */
112          
113         uint64_t temp64;
114         
115         change_endian(&BL, 64/8);       
116         change_endian(&BR, 64/8);
117         
118         /* Prewhitening */
119         BL ^= s->kll;
120         BR ^= s->klr;
121         
122         /* the first 6 */
123         camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_POSTC1 , 0x33);
124         /* FL injection  */
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);
129         /* middle 6 */
130         camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_INC2 , 0x34);
131         /* FL injection  */
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);
136    /* last 6 */
137         camellia_6rounds(s, &BL, &BR, KEY_ROL17 | KEY_DIR_NORM | KEY_POSTC2 , 0x0C);
138         /* Postwhitening */
139         BR ^= s->kal;
140         BL ^= s->kar;
141         
142         temp64 = BR;
143         BR = BL;
144         BL = temp64;
145
146         
147         change_endian(&BL, 64/8);       
148         change_endian(&BR, 64/8);
149                 
150         #undef BL
151         #undef BR       
152 }
153
154 /*****************************************************************************/
155
156 void camellia128_dec(camellia128_ctx_t* s, void* block){
157
158         #define BL (((uint64_t*)block)[1])
159         #define BR (((uint64_t*)block)[0])
160         /* endian adjustment */
161          /*BL*/
162          /*     1 2 3 4 5 6 7 8
163           *             8 7 6 5 4 3 2 1
164           */
165          
166         uint64_t temp64;
167                 
168         change_endian(&BL, 64/8);       
169         change_endian(&BR, 64/8);
170                 
171         camellia128_keyop_inv(s, 1);
172         /* Prewhitening */
173         BR ^= s->kal; /* kw3 */
174         BL ^= s->kar; /* kw4 */
175         /* the first 6 */
176         camellia_6rounds(s, &BR, &BL, KEY_ROL17 | KEY_DIR_INV | KEY_POSTC1 , 0x0C);
177         /* FL injection  */
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);
182         /* middle 6 */  
183         camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_INC2 , 0x0B);
184         /* FL injection  */
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);
189    /* last 6 */
190         camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_POSTC2 , 0x33);
191         
192         /* Postwhitening */
193         BL ^= s->kll; /* kw1 */ 
194         BR ^= s->klr; /* kw2 */
195         
196         temp64 = BR;
197         BR = BL;
198         BL = temp64;
199         
200         change_endian(&BL, 64/8);       
201         change_endian(&BR, 64/8);
202                 
203 }
204
205 /*****************************************************************************/
206 /*****************************************************************************/
207
208
209
210 /* EOF */