]> git.cryptolib.org Git - avr-crypto-lib.git/blob - cast5.c
documentation upgrade; most docu still missing but a first step is done
[avr-crypto-lib.git] / cast5.c
1 /* 
2  * \file        cast5.c
3  * \author      Daniel Otte
4  * \date        26.07.2006
5  * \par License:
6  *  GPL
7  * \brief Implementation of the CAST5 (aka CAST-128) cipher algorithm as described in RFC 2144
8  * 
9  */
10  
11  #include <stdint.h>
12  #include <string.h>
13  #include "cast5.h"
14  #include "config.h"
15  #include "debug.h"
16  
17  #undef DEBUG
18  
19  #ifdef DEBUG
20   #include "uart.h"
21  #endif
22  
23 #include "cast5-sbox.h"
24
25
26  
27 #define S5(x) pgm_read_dword(&s5[(x)])
28 #define S6(x) pgm_read_dword(&s6[(x)])
29 #define S7(x) pgm_read_dword(&s7[(x)])
30 #define S8(x) pgm_read_dword(&s8[(x)])
31
32  
33 void cast5_init_A(uint8_t *dest, uint8_t *src, bool bmode){
34         uint8_t mask = bmode?0x8:0;
35         *((uint32_t*)(&dest[0x0])) = *((uint32_t*)(&src[0x0^mask])) ^ S5(src[0xD^mask]) ^ S6(src[0xF^mask]) ^ S7(src[0xC^mask]) ^ S8(src[0xE^mask]) ^ S7(src[0x8^mask]);
36         *((uint32_t*)(&dest[0x4])) = *((uint32_t*)(&src[0x8^mask])) ^ S5(dest[0x0]) ^ S6(dest[0x2]) ^ S7(dest[0x1]) ^ S8(dest[0x3]) ^ S8(src[0xA^mask]);
37         *((uint32_t*)(&dest[0x8])) = *((uint32_t*)(&src[0xC^mask])) ^ S5(dest[0x7]) ^ S6(dest[0x6]) ^ S7(dest[0x5]) ^ S8(dest[0x4]) ^ S5(src[0x9^mask]);
38         *((uint32_t*)(&dest[0xC])) = *((uint32_t*)(&src[0x4^mask])) ^ S5(dest[0xA]) ^ S6(dest[0x9]) ^ S7(dest[0xB]) ^ S8(dest[0x8]) ^ S6(src[0xB^mask]);
39 }
40
41 void cast5_init_M(uint8_t *dest, uint8_t *src, bool nmode, bool xmode){
42         uint8_t nmt[] = {0xB, 0xA, 0x9, 0x8, 0xF, 0xE, 0xD, 0xC, 0x3, 0x2, 0x1, 0x0, 0x7, 0x6, 0x5, 0x4}; /* nmode table */
43         uint8_t xmt[4][4] = {{0x2, 0x6, 0x9, 0xC}, {0x8, 0xD, 0x3, 0x7}, {0x3, 0x7, 0x8, 0xD}, {0x9, 0xC, 0x2, 0x6}};
44         #define NMT(x) (src[nmode?nmt[(x)]:(x)])
45         #define XMT(x) (src[xmt[(xmode<<1) + nmode][(x)]])
46         *((uint32_t*)(&dest[0x0])) = S5(NMT(0x8)) ^ S6(NMT(0x9)) ^ S7(NMT(0x7)) ^ S8(NMT(0x6)) ^ S5(XMT(0));
47         *((uint32_t*)(&dest[0x4])) = S5(NMT(0xA)) ^ S6(NMT(0xB)) ^ S7(NMT(0x5)) ^ S8(NMT(0x4)) ^ S6(XMT(1));
48         *((uint32_t*)(&dest[0x8])) = S5(NMT(0xC)) ^ S6(NMT(0xD)) ^ S7(NMT(0x3)) ^ S8(NMT(0x2)) ^ S7(XMT(2));
49         *((uint32_t*)(&dest[0xC])) = S5(NMT(0xE)) ^ S6(NMT(0xF)) ^ S7(NMT(0x1)) ^ S8(NMT(0x0)) ^ S8(XMT(3));
50 }
51
52 #define S5B(x) pgm_read_byte(3+(uint8_t*)(&s5[(x)]))
53 #define S6B(x) pgm_read_byte(3+(uint8_t*)(&s6[(x)]))
54 #define S7B(x) pgm_read_byte(3+(uint8_t*)(&s7[(x)]))
55 #define S8B(x) pgm_read_byte(3+(uint8_t*)(&s8[(x)]))
56
57 void cast5_init_rM(uint8_t *klo, uint8_t *khi, uint8_t offset, uint8_t *src, bool nmode, bool xmode){
58         uint8_t nmt[] = {0xB, 0xA, 0x9, 0x8, 0xF, 0xE, 0xD, 0xC, 0x3, 0x2, 0x1, 0x0, 0x7, 0x6, 0x5, 0x4}; /* nmode table */
59         uint8_t xmt[4][4] = {{0x2, 0x6, 0x9, 0xC}, {0x8, 0xD, 0x3, 0x7}, {0x3, 0x7, 0x8, 0xD}, {0x9, 0xC, 0x2, 0x6}};
60         uint8_t t, h=0; 
61         t = S5B(NMT(0x8)) ^ S6B(NMT(0x9)) ^ S7B(NMT(0x7)) ^ S8B(NMT(0x6)) ^ S5B(XMT(0));
62                 klo[offset*2] |= (t & 0x0f);
63                 h |= (t&0x10); h>>=1;
64         t = S5B(NMT(0xA)) ^ S6B(NMT(0xB)) ^ S7B(NMT(0x5)) ^ S8B(NMT(0x4)) ^ S6B(XMT(1));
65                 klo[offset*2] |= (t<<4) & 0xf0;
66                 h |= t&0x10; h>>=1;
67         t = S5B(NMT(0xC)) ^ S6B(NMT(0xD)) ^ S7B(NMT(0x3)) ^ S8B(NMT(0x2)) ^ S7B(XMT(2));
68                 klo[offset*2+1] |= t&0xf;
69                 h |= t&0x10; h>>=1;
70         t = S5B(NMT(0xE)) ^ S6B(NMT(0xF)) ^ S7B(NMT(0x1)) ^ S8B(NMT(0x0)) ^ S8B(XMT(3));
71                 klo[offset*2+1] |= t<<4;
72                 h |= t&0x10; h >>=1;
73         #ifdef DEBUG
74                 uart_putstr("\r\n\t h="); uart_hexdump(&h,1);
75         #endif
76         khi[offset>>1] |= h<<((offset&0x1)?4:0);
77 }
78
79 #define S_5X(s) pgm_read_dword(&s5[BPX[(s)]])
80 #define S_6X(s) pgm_read_dword(&s6[BPX[(s)]])
81 #define S_7X(s) pgm_read_dword(&s7[BPX[(s)]])
82 #define S_8X(s) pgm_read_dword(&s8[BPX[(s)]])
83
84 #define S_5Z(s) pgm_read_dword(&s5[BPZ[(s)]])
85 #define S_6Z(s) pgm_read_dword(&s6[BPZ[(s)]])
86 #define S_7Z(s) pgm_read_dword(&s7[BPZ[(s)]])
87 #define S_8Z(s) pgm_read_dword(&s8[BPZ[(s)]])
88
89
90
91 /**
92  * \brief sets up round keys (context) for cast5 en/decryption.
93  * @param s Pointer to cast5 context.
94  * @param key Pointer to binary key.
95  * @param keylength length of keydata in bits.
96  */
97 void cast5_init(cast5_ctx_t* s, uint8_t* key, uint8_t keylength){
98          /* we migth return if the key is valid and if setup was sucessfull */
99         uint32_t x[4], z[4];
100         #define BPX ((uint8_t*)&(x[0]))
101         #define BPZ ((uint8_t*)&(z[0]))
102         s->shortkey = (keylength<=80);
103         /* littel endian only! */
104         memset(&(x[0]), 0 ,16); /* set x to zero */
105         memcpy(&(x[0]), key, keylength/8);
106         
107
108         /* todo: merge a and b and compress the whole stuff */
109         /***** A *****/
110         cast5_init_A((uint8_t*)(&z[0]), (uint8_t*)(&x[0]), false);      
111         /***** M *****/
112         cast5_init_M((uint8_t*)(&(s->mask[0])), (uint8_t*)(&z[0]), false, false);
113         /***** B *****/
114         cast5_init_A((uint8_t*)(&x[0]), (uint8_t*)(&z[0]), true);
115         /***** N *****/
116         cast5_init_M((uint8_t*)(&(s->mask[4])), (uint8_t*)(&x[0]), true, false);
117         /***** A *****/
118         cast5_init_A((uint8_t*)(&z[0]), (uint8_t*)(&x[0]), false);
119         /***** N' *****/
120         cast5_init_M((uint8_t*)(&(s->mask[8])), (uint8_t*)(&z[0]), true, true);
121         /***** B *****/
122         cast5_init_A((uint8_t*)(&x[0]), (uint8_t*)(&z[0]), true);
123         /***** M' *****/
124         cast5_init_M((uint8_t*)(&(s->mask[12])), (uint8_t*)(&x[0]), false, true);
125         
126         /* that were the masking keys, now the rotation keys */
127         /* set the keys to zero */
128         memset(&(s->rotl[0]),0,8);
129         s->roth[0]=s->roth[1]=0;
130         /***** A *****/
131         cast5_init_A((uint8_t*)(&z[0]), (uint8_t*)(&x[0]), false);
132         /***** M *****/
133         cast5_init_rM(&(s->rotl[0]), &(s->roth[0]), 0, (uint8_t*)(&z[0]), false, false);
134         /***** B *****/
135         cast5_init_A((uint8_t*)(&x[0]), (uint8_t*)(&z[0]), true);
136         /***** N *****/
137         cast5_init_rM(&(s->rotl[0]), &(s->roth[0]), 1, (uint8_t*)(&x[0]), true, false);
138         /***** A *****/
139         cast5_init_A((uint8_t*)(&z[0]), (uint8_t*)(&x[0]), false);
140         /***** N' *****/
141         cast5_init_rM(&(s->rotl[0]), &(s->roth[0]), 2, (uint8_t*)(&z[0]), true, true);
142         /***** B *****/
143         cast5_init_A((uint8_t*)(&x[0]), (uint8_t*)(&z[0]), true);
144         /***** M' *****/
145         cast5_init_rM(&(s->rotl[0]), &(s->roth[0]), 3, (uint8_t*)(&x[0]), false, true);
146         /* done ;-) */
147 }
148
149
150
151 /********************************************************************************************************/
152
153 #define ROTL32(a,n) ((a)<<(n) | (a)>>(32-(n)))
154 #define CHANGE_ENDIAN32(x) ((x)<<24 | (x)>>24 | ((x)&0xff00)<<8 | ((x)&0xff0000)>>8 )
155
156 typedef uint32_t cast5_f_t(uint32_t,uint32_t,uint8_t);
157
158 #define IA 3
159 #define IB 2
160 #define IC 1
161 #define ID 0
162
163
164 uint32_t cast5_f1(uint32_t d, uint32_t m, uint8_t r){
165         uint32_t t;
166         t = ROTL32((d + m),r);
167 #ifdef DEBUG
168         uint32_t ia,ib,ic,id;
169         uart_putstr("\r\n f1("); uart_hexdump(&d, 4); uart_putc(',');
170                 uart_hexdump(&m , 4); uart_putc(','); uart_hexdump(&r, 1);uart_putstr("): I=");
171                 uart_hexdump(&t, 4);
172         ia = pgm_read_dword(&s1[((uint8_t*)&t)[IA]] );
173         ib = pgm_read_dword(&s2[((uint8_t*)&t)[IB]] );
174         ic = pgm_read_dword(&s3[((uint8_t*)&t)[IC]] );
175         id = pgm_read_dword(&s4[((uint8_t*)&t)[ID]] );
176         uart_putstr("\r\n\tIA="); uart_hexdump(&ia, 4);
177         uart_putstr("\r\n\tIB="); uart_hexdump(&ib, 4);
178         uart_putstr("\r\n\tIC="); uart_hexdump(&ic, 4);
179         uart_putstr("\r\n\tID="); uart_hexdump(&id, 4);
180
181         return (((ia ^ ib) - ic) + id);
182
183 #else
184         
185         return (((pgm_read_dword(&s1[((uint8_t*)&t)[IA]] ) ^ pgm_read_dword(&s2[((uint8_t*)&t)[IB]] )) 
186                 - pgm_read_dword(&s3[((uint8_t*)&t)[IC]] )) + pgm_read_dword(&s4[((uint8_t*)&t)[ID]]));
187
188 #endif
189 }
190
191
192 uint32_t cast5_f2(uint32_t d, uint32_t m, uint8_t r){
193         uint32_t t;
194         t = ROTL32((d ^ m),r);
195 #ifdef DEBUG
196         uint32_t ia,ib,ic,id;
197         uart_putstr("\r\n f2("); uart_hexdump(&d, 4); uart_putc(',');
198                 uart_hexdump(&m , 4); uart_putc(','); uart_hexdump(&r, 1);uart_putstr("): I=");
199                 uart_hexdump(&t, 4);
200
201         ia = pgm_read_dword(&s1[((uint8_t*)&t)[IA]] );
202         ib = pgm_read_dword(&s2[((uint8_t*)&t)[IB]] );
203         ic = pgm_read_dword(&s3[((uint8_t*)&t)[IC]] );
204         id = pgm_read_dword(&s4[((uint8_t*)&t)[ID]] );
205         
206         uart_putstr("\r\n\tIA="); uart_hexdump(&ia, 4);
207         uart_putstr("\r\n\tIB="); uart_hexdump(&ib, 4);
208         uart_putstr("\r\n\tIC="); uart_hexdump(&ic, 4);
209         uart_putstr("\r\n\tID="); uart_hexdump(&id, 4);
210
211         return (((ia - ib) + ic) ^ id);
212 #else
213         
214         return (((pgm_read_dword(&s1[((uint8_t*)&t)[IA]] ) - pgm_read_dword(&s2[((uint8_t*)&t)[IB]] )) 
215                 + pgm_read_dword(&s3[((uint8_t*)&t)[IC]] )) ^ pgm_read_dword(&s4[((uint8_t*)&t)[ID]]));
216
217 #endif
218 }
219
220 uint32_t cast5_f3(uint32_t d, uint32_t m, uint8_t r){
221         uint32_t t;
222         t = ROTL32((m - d),r);
223
224 #ifdef DEBUG
225         uint32_t ia,ib,ic,id;
226
227         uart_putstr("\r\n f3("); uart_hexdump(&d, 4); uart_putc(',');
228                 uart_hexdump(&m , 4); uart_putc(','); uart_hexdump(&r, 1);uart_putstr("): I=");
229                 uart_hexdump(&t, 4);
230
231         ia = pgm_read_dword(&s1[((uint8_t*)&t)[IA]] );
232         ib = pgm_read_dword(&s2[((uint8_t*)&t)[IB]] );
233         ic = pgm_read_dword(&s3[((uint8_t*)&t)[IC]] );
234         id = pgm_read_dword(&s4[((uint8_t*)&t)[ID]] );
235         
236         uart_putstr("\r\n\tIA="); uart_hexdump(&ia, 4);
237         uart_putstr("\r\n\tIB="); uart_hexdump(&ib, 4);
238         uart_putstr("\r\n\tIC="); uart_hexdump(&ic, 4);
239         uart_putstr("\r\n\tID="); uart_hexdump(&id, 4);
240         return (((ia + ib) ^ ic) - id);
241 #else
242         return ((pgm_read_dword(&s1[((uint8_t*)&t)[IA]] ) + pgm_read_dword(&s2[((uint8_t*)&t)[IB]] )) 
243                 ^ pgm_read_dword(&s3[((uint8_t*)&t)[IC]] )) - pgm_read_dword(&s4[((uint8_t*)&t)[ID]] );
244
245 #endif
246 }
247
248 /*************************************************************************/
249
250 /**
251  * \brief encrypts a datablock with cast5
252  * @param s Pointer to cast5 roundkeys (context)
253  * @param block Pointer to datablock
254  */
255 void cast5_enc(cast5_ctx_t *s, void* block){
256         uint32_t l,r, x, y;
257         uint8_t i;
258         cast5_f_t* f[]={cast5_f1,cast5_f2,cast5_f3};
259         l=((uint32_t*)block)[0];
260         r=((uint32_t*)block)[1];
261 //      uart_putstr("\r\n round[-1] = ");
262 //      uart_hexdump(&r, 4);
263         for (i=0;i<(s->shortkey?12:16);++i){
264                 x = r;
265                 y = (f[i%3])(CHANGE_ENDIAN32(r), CHANGE_ENDIAN32(s->mask[i]), 
266                         (((s->roth[i>>3]) & (1<<(i&0x7)))?0x10:0x00) 
267                          + ( ((s->rotl[i>>1])>>((i&1)?4:0)) & 0x0f) );
268                 r = l ^ CHANGE_ENDIAN32(y);
269 //              uart_putstr("\r\n round["); DEBUG_B(i); uart_putstr("] = ");
270 //              uart_hexdump(&r, 4);
271                 l = x;
272         }
273         ((uint32_t*)block)[0]=r;
274         ((uint32_t*)block)[1]=l;
275 }
276
277 /*************************************************************************/
278
279 /**
280  * \brief decrypts a datablock with cast5
281  * @param s Pointer to cast5 roundkeys (context)
282  * @param block Pointer to datablock
283  */
284 void cast5_dec(cast5_ctx_t *s, void* block){
285         uint32_t l,r, x, y;
286         int8_t i, rounds;
287         cast5_f_t* f[]={cast5_f1,cast5_f2,cast5_f3};
288         l=((uint32_t*)block)[0];
289         r=((uint32_t*)block)[1];
290         rounds = (s->shortkey?12:16);
291         for (i=rounds-1; i>=0 ;--i){
292                 x = r;
293                 y = (f[i%3])(CHANGE_ENDIAN32(r), CHANGE_ENDIAN32(s->mask[i]), 
294                         (((s->roth[i>>3]) & (1<<(i&0x7)))?0x10:0x00) 
295                          + ( ((s->rotl[i>>1])>>((i&1)?4:0)) & 0x0f) );
296                 r = l ^ CHANGE_ENDIAN32(y);
297                 l = x;
298         }
299         ((uint32_t*)block)[0]=r;
300         ((uint32_t*)block)[1]=l;
301 }
302
303
304 /*********************************************************************************************************/
305 /*********************************************************************************************************/
306 /*********************************************************************************************************/
307
308 #if 0
309
310 void cast5_old_init(cast5_ctx_t* s, uint8_t* key, uint8_t keylength){
311          /* we migth return if the key is valid and if setup was sucessfull */
312         uint32_t x[4], z[4], t;
313         #define BPX ((uint8_t*)&(x[0]))
314         #define BPZ ((uint8_t*)&(z[0]))
315         s->shortkey = (keylength<=80);
316         /* littel endian only! */
317         memset(&(x[0]), 0 ,16); /* set x to zero */
318         memcpy(&(x[0]), key, keylength/8);
319         
320
321         /* todo: merge a and b and compress the whole stuff */
322         /***** A *****/
323         z[0] = x[0] ^ S_5X(0xD) ^ S_6X(0xF) ^ S_7X(0xC) ^ S_8X(0xE) ^ S_7X(0x8);        
324         z[1] = x[2] ^ S_5Z(0x0) ^ S_6Z(0x2) ^ S_7Z(0x1) ^ S_8Z(0x3) ^ S_8X(0xA);
325         z[2] = x[3] ^ S_5Z(0x7) ^ S_6Z(0x6) ^ S_7Z(0x5) ^ S_8Z(0x4) ^ S_5X(0x9);
326         z[3] = x[1] ^ S_5Z(0xA) ^ S_6Z(0x9) ^ S_7Z(0xB) ^ S_8Z(0x8) ^ S_6X(0xB);
327         /***** M *****/
328         s->mask[0] = S_5Z(0x8) ^ S_6Z(0x9) ^ S_7Z(0x7) ^ S_8Z(0x6) ^ S_5Z(0x2);
329         s->mask[1] = S_5Z(0xA) ^ S_6Z(0xB) ^ S_7Z(0x5) ^ S_8Z(0x4) ^ S_6Z(0x6);
330         s->mask[2] = S_5Z(0xC) ^ S_6Z(0xD) ^ S_7Z(0x3) ^ S_8Z(0x2) ^ S_7Z(0x9);
331         s->mask[3] = S_5Z(0xE) ^ S_6Z(0xF) ^ S_7Z(0x1) ^ S_8Z(0x0) ^ S_8Z(0xC);
332         /***** B *****/
333         x[0] = z[2] ^ S_5Z(0x5) ^ S_6Z(0x7) ^ S_7Z(0x4) ^ S_8Z(0x6) ^ S_7Z(0x0);
334         x[1] = z[0] ^ S_5X(0x0) ^ S_6X(0x2) ^ S_7X(0x1) ^ S_8X(0x3) ^ S_8Z(0x2);
335         x[2] = z[1] ^ S_5X(0x7) ^ S_6X(0x6) ^ S_7X(0x5) ^ S_8X(0x4) ^ S_5Z(0x1);
336         x[3] = z[3] ^ S_5X(0xA) ^ S_6X(0x9) ^ S_7X(0xB) ^ S_8X(0x8) ^ S_6Z(0x3);
337         /***** N *****/
338         s->mask[4] = S_5X(0x3) ^ S_6X(0x2) ^ S_7X(0xC) ^ S_8X(0xD) ^ S_5X(0x8);
339         s->mask[5] = S_5X(0x1) ^ S_6X(0x0) ^ S_7X(0xE) ^ S_8X(0xF) ^ S_6X(0xD);
340         s->mask[6] = S_5X(0x7) ^ S_6X(0x6) ^ S_7X(0x8) ^ S_8X(0x9) ^ S_7X(0x3);
341         s->mask[7] = S_5X(0x5) ^ S_6X(0x4) ^ S_7X(0xA) ^ S_8X(0xB) ^ S_8X(0x7);
342         /***** A *****/
343         z[0] = x[0] ^ S_5X(0xD) ^ S_6X(0xF) ^ S_7X(0xC) ^ S_8X(0xE) ^ S_7X(0x8);
344         z[1] = x[2] ^ S_5Z(0x0) ^ S_6Z(0x2) ^ S_7Z(0x1) ^ S_8Z(0x3) ^ S_8X(0xA);
345         z[2] = x[3] ^ S_5Z(0x7) ^ S_6Z(0x6) ^ S_7Z(0x5) ^ S_8Z(0x4) ^ S_5X(0x9);
346         z[3] = x[1] ^ S_5Z(0xA) ^ S_6Z(0x9) ^ S_7Z(0xB) ^ S_8Z(0x8) ^ S_6X(0xB);
347         /***** N' *****/
348         s->mask[8] = S_5Z(0x3) ^ S_6Z(0x2) ^ S_7Z(0xC) ^ S_8Z(0xD) ^ S_5Z(0x9);
349         s->mask[9] = S_5Z(0x1) ^ S_6Z(0x0) ^ S_7Z(0xE) ^ S_8Z(0xF) ^ S_6Z(0xC);
350         s->mask[10] = S_5Z(0x7) ^ S_6Z(0x6) ^ S_7Z(0x8) ^ S_8Z(0x9) ^ S_7Z(0x2);
351         s->mask[11] = S_5Z(0x5) ^ S_6Z(0x4) ^ S_7Z(0xA) ^ S_8Z(0xB) ^ S_8Z(0x6);
352         /***** B *****/
353         x[0] = z[2] ^ S_5Z(0x5) ^ S_6Z(0x7) ^ S_7Z(0x4) ^ S_8Z(0x6) ^ S_7Z(0x0);
354         x[1] = z[0] ^ S_5X(0x0) ^ S_6X(0x2) ^ S_7X(0x1) ^ S_8X(0x3) ^ S_8Z(0x2);
355         x[2] = z[1] ^ S_5X(0x7) ^ S_6X(0x6) ^ S_7X(0x5) ^ S_8X(0x4) ^ S_5Z(0x1);
356         x[3] = z[3] ^ S_5X(0xA) ^ S_6X(0x9) ^ S_7X(0xB) ^ S_8X(0x8) ^ S_6Z(0x3);
357         /***** M' *****/
358         s->mask[12] = S_5X(0x8) ^ S_6X(0x9) ^ S_7X(0x7) ^ S_8X(0x6) ^ S_5X(0x3);
359         s->mask[13] = S_5X(0xA) ^ S_6X(0xB) ^ S_7X(0x5) ^ S_8X(0x4) ^ S_6X(0x7);
360         s->mask[14] = S_5X(0xC) ^ S_6X(0xD) ^ S_7X(0x3) ^ S_8X(0x2) ^ S_7X(0x8);
361         s->mask[15] = S_5X(0xE) ^ S_6X(0xF) ^ S_7X(0x1) ^ S_8X(0x0) ^ S_8X(0xD);
362
363         /* that were the masking keys, now the rotation keys */
364         /* set the keys to zero */
365         memset(&(s->rotl[0]),0,8);
366         s->roth[0]=s->roth[1]=0;
367         /***** A *****/
368         z[0] = x[0] ^ S_5X(0xD) ^ S_6X(0xF) ^ S_7X(0xC) ^ S_8X(0xE) ^ S_7X(0x8);
369         z[1] = x[2] ^ S_5Z(0x0) ^ S_6Z(0x2) ^ S_7Z(0x1) ^ S_8Z(0x3) ^ S_8X(0xA);
370         z[2] = x[3] ^ S_5Z(0x7) ^ S_6Z(0x6) ^ S_7Z(0x5) ^ S_8Z(0x4) ^ S_5X(0x9);
371         z[3] = x[1] ^ S_5Z(0xA) ^ S_6Z(0x9) ^ S_7Z(0xB) ^ S_8Z(0x8) ^ S_6X(0xB);
372         /***** M *****/
373         t = S_5Z(0x8) ^ S_6Z(0x9) ^ S_7Z(0x7) ^ S_8Z(0x6) ^ S_5Z(0x2);
374         t >>= 24;
375         s->rotl[0] |= t & 0x0f;         
376         s->roth[0] |= (t >> 4) & (1<<0);
377         t = S_5Z(0xA) ^ S_6Z(0xB) ^ S_7Z(0x5) ^ S_8Z(0x4) ^ S_6Z(0x6);
378         t >>= 24;
379         s->rotl[0] |= (t<<4) & 0xf0;
380         s->roth[0] |= (t >> 3) & (1<<1);
381         t = S_5Z(0xC) ^ S_6Z(0xD) ^ S_7Z(0x3) ^ S_8Z(0x2) ^ S_7Z(0x9);
382         t >>= 24;
383         s->rotl[1] |= t & 0x0f;         
384         s->roth[0] |= (t >> 2) & (1<<2);
385         t = S_5Z(0xE) ^ S_6Z(0xF) ^ S_7Z(0x1) ^ S_8Z(0x0) ^ S_8Z(0xC);
386         t >>= 24;
387         s->rotl[1] |= (t<<4) & 0xf0;
388         s->roth[0] |= (t >> 1) & (1<<3);
389         /***** B *****/
390         x[0] = z[2] ^ S_5Z(0x5) ^ S_6Z(0x7) ^ S_7Z(0x4) ^ S_8Z(0x6) ^ S_7Z(0x0);
391         x[1] = z[0] ^ S_5X(0x0) ^ S_6X(0x2) ^ S_7X(0x1) ^ S_8X(0x3) ^ S_8Z(0x2);
392         x[2] = z[1] ^ S_5X(0x7) ^ S_6X(0x6) ^ S_7X(0x5) ^ S_8X(0x4) ^ S_5Z(0x1);
393         x[3] = z[3] ^ S_5X(0xA) ^ S_6X(0x9) ^ S_7X(0xB) ^ S_8X(0x8) ^ S_6Z(0x3);
394         /***** N *****/
395         t = S_5X(0x3) ^ S_6X(0x2) ^ S_7X(0xC) ^ S_8X(0xD) ^ S_5X(0x8);
396         t >>= 24;
397         s->rotl[2] |= t & 0x0f;         
398         s->roth[0] |= t & (1<<4);
399         t = S_5X(0x1) ^ S_6X(0x0) ^ S_7X(0xE) ^ S_8X(0xF) ^ S_6X(0xD);
400         t >>= 24;
401         s->rotl[2] |= (t<<4) & 0xf0;            
402         s->roth[0] |= (t<<1) & (1<<5);
403         t = S_5X(0x7) ^ S_6X(0x6) ^ S_7X(0x8) ^ S_8X(0x9) ^ S_7X(0x3);
404         t >>= 24;
405         s->rotl[3] |= t & 0x0f;         
406         s->roth[0] |= (t<<2) & (1<<6);
407         t = S_5X(0x5) ^ S_6X(0x4) ^ S_7X(0xA) ^ S_8X(0xB) ^ S_8X(0x7);
408         t >>= 24;
409         s->rotl[3] |= (t<<4) & 0xf0;            
410         s->roth[0] |= (t<<3) & (1<<7);
411         /***** A *****/
412         z[0] = x[0] ^ S_5X(0xD) ^ S_6X(0xF) ^ S_7X(0xC) ^ S_8X(0xE) ^ S_7X(0x8);
413         z[1] = x[2] ^ S_5Z(0x0) ^ S_6Z(0x2) ^ S_7Z(0x1) ^ S_8Z(0x3) ^ S_8X(0xA);
414         z[2] = x[3] ^ S_5Z(0x7) ^ S_6Z(0x6) ^ S_7Z(0x5) ^ S_8Z(0x4) ^ S_5X(0x9);
415         z[3] = x[1] ^ S_5Z(0xA) ^ S_6Z(0x9) ^ S_7Z(0xB) ^ S_8Z(0x8) ^ S_6X(0xB);
416         /***** N' *****/
417         t = S_5Z(0x3) ^ S_6Z(0x2) ^ S_7Z(0xC) ^ S_8Z(0xD) ^ S_5Z(0x9);
418         t >>= 24;
419         s->rotl[4] |= t & 0x0f;         
420         s->roth[1] |= (t>>4) & (1<<0);
421         t = S_5Z(0x1) ^ S_6Z(0x0) ^ S_7Z(0xE) ^ S_8Z(0xF) ^ S_6Z(0xC);
422         t >>= 24;
423         s->rotl[4] |= (t<<4) & 0xf0;            
424         s->roth[1] |= (t>>3) & (1<<1);
425         t = S_5Z(0x7) ^ S_6Z(0x6) ^ S_7Z(0x8) ^ S_8Z(0x9) ^ S_7Z(0x2);
426         t >>= 24;
427         s->rotl[5] |= t & 0x0f;         
428         s->roth[1] |= (t>>2) & (1<<2);
429         t = S_5Z(0x5) ^ S_6Z(0x4) ^ S_7Z(0xA) ^ S_8Z(0xB) ^ S_8Z(0x6);
430         t >>= 24;
431         s->rotl[5] |= (t<<4) & 0xf0;            
432         s->roth[1] |= (t>>1) & (1<<3);
433         /***** B *****/
434         x[0] = z[2] ^ S_5Z(0x5) ^ S_6Z(0x7) ^ S_7Z(0x4) ^ S_8Z(0x6) ^ S_7Z(0x0);
435         x[1] = z[0] ^ S_5X(0x0) ^ S_6X(0x2) ^ S_7X(0x1) ^ S_8X(0x3) ^ S_8Z(0x2);
436         x[2] = z[1] ^ S_5X(0x7) ^ S_6X(0x6) ^ S_7X(0x5) ^ S_8X(0x4) ^ S_5Z(0x1);
437         x[3] = z[3] ^ S_5X(0xA) ^ S_6X(0x9) ^ S_7X(0xB) ^ S_8X(0x8) ^ S_6Z(0x3);
438         /***** M' *****/
439         t = S_5X(0x8) ^ S_6X(0x9) ^ S_7X(0x7) ^ S_8X(0x6) ^ S_5X(0x3);
440         t >>= 24;
441         s->rotl[6] |= t & 0x0f;         
442         s->roth[1] |= t & (1<<4);
443         t = S_5X(0xA) ^ S_6X(0xB) ^ S_7X(0x5) ^ S_8X(0x4) ^ S_6X(0x7);
444         t >>= 24;
445         s->rotl[6] |= (t<<4) & 0xf0;            
446         s->roth[1] |= (t<<1) & (1<<5);
447         t = S_5X(0xC) ^ S_6X(0xD) ^ S_7X(0x3) ^ S_8X(0x2) ^ S_7X(0x8);
448         t >>= 24;
449         s->rotl[7] |= t & 0x0f;         
450         s->roth[1] |= (t<<2) & (1<<6);
451         t = S_5X(0xE) ^ S_6X(0xF) ^ S_7X(0x1) ^ S_8X(0x0) ^ S_8X(0xD);
452         t >>= 24;
453         s->rotl[7] |= (t<<4) & 0xf0;            
454         s->roth[1] |= (t<<3) & (1<<7);
455         
456         /* done ;-) */
457 }
458
459 #endif
460
461
462
463