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/>.
22 * \email daniel.otte@rub.de
26 * \brief Implementation of the CAST5 (aka CAST-128) cipher algorithm as described in RFC 2144
42 #include "cast5-sbox.h"
46 #define S5(x) pgm_read_dword(&s5[(x)])
47 #define S6(x) pgm_read_dword(&s6[(x)])
48 #define S7(x) pgm_read_dword(&s7[(x)])
49 #define S8(x) pgm_read_dword(&s8[(x)])
52 void cast5_init_A(uint8_t *dest, uint8_t *src, bool bmode){
53 uint8_t mask = bmode?0x8:0;
54 *((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]);
55 *((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]);
56 *((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]);
57 *((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]);
60 void cast5_init_M(uint8_t *dest, uint8_t *src, bool nmode, bool xmode){
61 uint8_t nmt[] = {0xB, 0xA, 0x9, 0x8, 0xF, 0xE, 0xD, 0xC, 0x3, 0x2, 0x1, 0x0, 0x7, 0x6, 0x5, 0x4}; /* nmode table */
62 uint8_t xmt[4][4] = {{0x2, 0x6, 0x9, 0xC}, {0x8, 0xD, 0x3, 0x7}, {0x3, 0x7, 0x8, 0xD}, {0x9, 0xC, 0x2, 0x6}};
63 #define NMT(x) (src[nmode?nmt[(x)]:(x)])
64 #define XMT(x) (src[xmt[(xmode<<1) + nmode][(x)]])
65 *((uint32_t*)(&dest[0x0])) = S5(NMT(0x8)) ^ S6(NMT(0x9)) ^ S7(NMT(0x7)) ^ S8(NMT(0x6)) ^ S5(XMT(0));
66 *((uint32_t*)(&dest[0x4])) = S5(NMT(0xA)) ^ S6(NMT(0xB)) ^ S7(NMT(0x5)) ^ S8(NMT(0x4)) ^ S6(XMT(1));
67 *((uint32_t*)(&dest[0x8])) = S5(NMT(0xC)) ^ S6(NMT(0xD)) ^ S7(NMT(0x3)) ^ S8(NMT(0x2)) ^ S7(XMT(2));
68 *((uint32_t*)(&dest[0xC])) = S5(NMT(0xE)) ^ S6(NMT(0xF)) ^ S7(NMT(0x1)) ^ S8(NMT(0x0)) ^ S8(XMT(3));
71 #define S5B(x) pgm_read_byte(3+(uint8_t*)(&s5[(x)]))
72 #define S6B(x) pgm_read_byte(3+(uint8_t*)(&s6[(x)]))
73 #define S7B(x) pgm_read_byte(3+(uint8_t*)(&s7[(x)]))
74 #define S8B(x) pgm_read_byte(3+(uint8_t*)(&s8[(x)]))
76 void cast5_init_rM(uint8_t *klo, uint8_t *khi, uint8_t offset, uint8_t *src, bool nmode, bool xmode){
77 uint8_t nmt[] = {0xB, 0xA, 0x9, 0x8, 0xF, 0xE, 0xD, 0xC, 0x3, 0x2, 0x1, 0x0, 0x7, 0x6, 0x5, 0x4}; /* nmode table */
78 uint8_t xmt[4][4] = {{0x2, 0x6, 0x9, 0xC}, {0x8, 0xD, 0x3, 0x7}, {0x3, 0x7, 0x8, 0xD}, {0x9, 0xC, 0x2, 0x6}};
80 t = S5B(NMT(0x8)) ^ S6B(NMT(0x9)) ^ S7B(NMT(0x7)) ^ S8B(NMT(0x6)) ^ S5B(XMT(0));
81 klo[offset*2] |= (t & 0x0f);
83 t = S5B(NMT(0xA)) ^ S6B(NMT(0xB)) ^ S7B(NMT(0x5)) ^ S8B(NMT(0x4)) ^ S6B(XMT(1));
84 klo[offset*2] |= (t<<4) & 0xf0;
86 t = S5B(NMT(0xC)) ^ S6B(NMT(0xD)) ^ S7B(NMT(0x3)) ^ S8B(NMT(0x2)) ^ S7B(XMT(2));
87 klo[offset*2+1] |= t&0xf;
89 t = S5B(NMT(0xE)) ^ S6B(NMT(0xF)) ^ S7B(NMT(0x1)) ^ S8B(NMT(0x0)) ^ S8B(XMT(3));
90 klo[offset*2+1] |= t<<4;
93 uart_putstr("\r\n\t h="); uart_hexdump(&h,1);
95 khi[offset>>1] |= h<<((offset&0x1)?4:0);
98 #define S_5X(s) pgm_read_dword(&s5[BPX[(s)]])
99 #define S_6X(s) pgm_read_dword(&s6[BPX[(s)]])
100 #define S_7X(s) pgm_read_dword(&s7[BPX[(s)]])
101 #define S_8X(s) pgm_read_dword(&s8[BPX[(s)]])
103 #define S_5Z(s) pgm_read_dword(&s5[BPZ[(s)]])
104 #define S_6Z(s) pgm_read_dword(&s6[BPZ[(s)]])
105 #define S_7Z(s) pgm_read_dword(&s7[BPZ[(s)]])
106 #define S_8Z(s) pgm_read_dword(&s8[BPZ[(s)]])
111 void cast5_init(const void* key, uint8_t keylength_b, cast5_ctx_t* s){
112 /* we migth return if the key is valid and if setup was sucessfull */
114 #define BPX ((uint8_t*)&(x[0]))
115 #define BPZ ((uint8_t*)&(z[0]))
116 s->shortkey = (keylength_b<=80);
117 /* littel endian only! */
118 memset(&(x[0]), 0 ,16); /* set x to zero */
119 if(keylength_b > 128)
121 memcpy(&(x[0]), key, (keylength_b+7)/8);
124 /* todo: merge a and b and compress the whole stuff */
126 cast5_init_A((uint8_t*)(&z[0]), (uint8_t*)(&x[0]), false);
128 cast5_init_M((uint8_t*)(&(s->mask[0])), (uint8_t*)(&z[0]), false, false);
130 cast5_init_A((uint8_t*)(&x[0]), (uint8_t*)(&z[0]), true);
132 cast5_init_M((uint8_t*)(&(s->mask[4])), (uint8_t*)(&x[0]), true, false);
134 cast5_init_A((uint8_t*)(&z[0]), (uint8_t*)(&x[0]), false);
136 cast5_init_M((uint8_t*)(&(s->mask[8])), (uint8_t*)(&z[0]), true, true);
138 cast5_init_A((uint8_t*)(&x[0]), (uint8_t*)(&z[0]), true);
140 cast5_init_M((uint8_t*)(&(s->mask[12])), (uint8_t*)(&x[0]), false, true);
142 /* that were the masking keys, now the rotation keys */
143 /* set the keys to zero */
144 memset(&(s->rotl[0]),0,8);
145 s->roth[0]=s->roth[1]=0;
147 cast5_init_A((uint8_t*)(&z[0]), (uint8_t*)(&x[0]), false);
149 cast5_init_rM(&(s->rotl[0]), &(s->roth[0]), 0, (uint8_t*)(&z[0]), false, false);
151 cast5_init_A((uint8_t*)(&x[0]), (uint8_t*)(&z[0]), true);
153 cast5_init_rM(&(s->rotl[0]), &(s->roth[0]), 1, (uint8_t*)(&x[0]), true, false);
155 cast5_init_A((uint8_t*)(&z[0]), (uint8_t*)(&x[0]), false);
157 cast5_init_rM(&(s->rotl[0]), &(s->roth[0]), 2, (uint8_t*)(&z[0]), true, true);
159 cast5_init_A((uint8_t*)(&x[0]), (uint8_t*)(&z[0]), true);
161 cast5_init_rM(&(s->rotl[0]), &(s->roth[0]), 3, (uint8_t*)(&x[0]), false, true);
167 /********************************************************************************************************/
169 #define ROTL32(a,n) ((a)<<(n) | (a)>>(32-(n)))
170 #define CHANGE_ENDIAN32(x) ((x)<<24 | (x)>>24 | ((x)&0xff00)<<8 | ((x)&0xff0000)>>8 )
172 typedef uint32_t cast5_f_t(uint32_t,uint32_t,uint8_t);
180 uint32_t cast5_f1(uint32_t d, uint32_t m, uint8_t r){
182 t = ROTL32((d + m),r);
184 uint32_t ia,ib,ic,id;
185 uart_putstr("\r\n f1("); uart_hexdump(&d, 4); uart_putc(',');
186 uart_hexdump(&m , 4); uart_putc(','); uart_hexdump(&r, 1);uart_putstr("): I=");
188 ia = pgm_read_dword(&s1[((uint8_t*)&t)[IA]] );
189 ib = pgm_read_dword(&s2[((uint8_t*)&t)[IB]] );
190 ic = pgm_read_dword(&s3[((uint8_t*)&t)[IC]] );
191 id = pgm_read_dword(&s4[((uint8_t*)&t)[ID]] );
192 uart_putstr("\r\n\tIA="); uart_hexdump(&ia, 4);
193 uart_putstr("\r\n\tIB="); uart_hexdump(&ib, 4);
194 uart_putstr("\r\n\tIC="); uart_hexdump(&ic, 4);
195 uart_putstr("\r\n\tID="); uart_hexdump(&id, 4);
197 return (((ia ^ ib) - ic) + id);
201 return ((( pgm_read_dword(&s1[((uint8_t*)&t)[IA]])
202 ^ pgm_read_dword(&s2[((uint8_t*)&t)[IB]]) )
203 - pgm_read_dword(&s3[((uint8_t*)&t)[IC]]) )
204 + pgm_read_dword(&s4[((uint8_t*)&t)[ID]]) );
210 uint32_t cast5_f2(uint32_t d, uint32_t m, uint8_t r){
212 t = ROTL32((d ^ m),r);
214 uint32_t ia,ib,ic,id;
215 uart_putstr("\r\n f2("); uart_hexdump(&d, 4); uart_putc(',');
216 uart_hexdump(&m , 4); uart_putc(','); uart_hexdump(&r, 1);uart_putstr("): I=");
219 ia = pgm_read_dword(&s1[((uint8_t*)&t)[IA]] );
220 ib = pgm_read_dword(&s2[((uint8_t*)&t)[IB]] );
221 ic = pgm_read_dword(&s3[((uint8_t*)&t)[IC]] );
222 id = pgm_read_dword(&s4[((uint8_t*)&t)[ID]] );
224 uart_putstr("\r\n\tIA="); uart_hexdump(&ia, 4);
225 uart_putstr("\r\n\tIB="); uart_hexdump(&ib, 4);
226 uart_putstr("\r\n\tIC="); uart_hexdump(&ic, 4);
227 uart_putstr("\r\n\tID="); uart_hexdump(&id, 4);
229 return (((ia - ib) + ic) ^ id);
232 return ((( pgm_read_dword(&s1[((uint8_t*)&t)[IA]])
233 - pgm_read_dword(&s2[((uint8_t*)&t)[IB]]) )
234 + pgm_read_dword(&s3[((uint8_t*)&t)[IC]]) )
235 ^ pgm_read_dword(&s4[((uint8_t*)&t)[ID]]) );
240 uint32_t cast5_f3(uint32_t d, uint32_t m, uint8_t r){
242 t = ROTL32((m - d),r);
245 uint32_t ia,ib,ic,id;
247 uart_putstr("\r\n f3("); uart_hexdump(&d, 4); uart_putc(',');
248 uart_hexdump(&m , 4); uart_putc(','); uart_hexdump(&r, 1);uart_putstr("): I=");
251 ia = pgm_read_dword(&s1[((uint8_t*)&t)[IA]] );
252 ib = pgm_read_dword(&s2[((uint8_t*)&t)[IB]] );
253 ic = pgm_read_dword(&s3[((uint8_t*)&t)[IC]] );
254 id = pgm_read_dword(&s4[((uint8_t*)&t)[ID]] );
256 uart_putstr("\r\n\tIA="); uart_hexdump(&ia, 4);
257 uart_putstr("\r\n\tIB="); uart_hexdump(&ib, 4);
258 uart_putstr("\r\n\tIC="); uart_hexdump(&ic, 4);
259 uart_putstr("\r\n\tID="); uart_hexdump(&id, 4);
260 return (((ia + ib) ^ ic) - id);
262 return (( pgm_read_dword(&s1[((uint8_t*)&t)[IA]] )
263 + pgm_read_dword(&s2[((uint8_t*)&t)[IB]] ))
264 ^ pgm_read_dword(&s3[((uint8_t*)&t)[IC]] ))
265 - pgm_read_dword(&s4[((uint8_t*)&t)[ID]] );
270 /*************************************************************************/
272 void cast5_enc(void* block, const cast5_ctx_t *s){
275 cast5_f_t* f[]={cast5_f1,cast5_f2,cast5_f3};
276 l=((uint32_t*)block)[0];
277 r=((uint32_t*)block)[1];
278 // uart_putstr("\r\n round[-1] = ");
279 // uart_hexdump(&r, 4);
280 for (i=0;i<(s->shortkey?12:16);++i){
282 y = (f[i%3])(CHANGE_ENDIAN32(r), CHANGE_ENDIAN32(s->mask[i]),
283 (((s->roth[i>>3]) & (1<<(i&0x7)))?0x10:0x00)
284 + ( ((s->rotl[i>>1])>>((i&1)?4:0)) & 0x0f) );
285 r = l ^ CHANGE_ENDIAN32(y);
286 // uart_putstr("\r\n round["); DEBUG_B(i); uart_putstr("] = ");
287 // uart_hexdump(&r, 4);
290 ((uint32_t*)block)[0]=r;
291 ((uint32_t*)block)[1]=l;
294 /*************************************************************************/
296 void cast5_dec(void* block, const cast5_ctx_t *s){
299 cast5_f_t* f[]={cast5_f1,cast5_f2,cast5_f3};
300 l=((uint32_t*)block)[0];
301 r=((uint32_t*)block)[1];
302 rounds = (s->shortkey?12:16);
303 for (i=rounds-1; i>=0 ;--i){
305 y = (f[i%3])(CHANGE_ENDIAN32(r), CHANGE_ENDIAN32(s->mask[i]),
306 (((s->roth[i>>3]) & (1<<(i&0x7)))?0x10:0x00)
307 + ( ((s->rotl[i>>1])>>((i&1)?4:0)) & 0x0f) );
308 r = l ^ CHANGE_ENDIAN32(y);
311 ((uint32_t*)block)[0]=r;
312 ((uint32_t*)block)[1]=l;
316 /*********************************************************************************************************/
317 /*********************************************************************************************************/
318 /*********************************************************************************************************/
322 void cast5_old_init(cast5_ctx_t* s, uint8_t* key, uint8_t keylength){
323 /* we migth return if the key is valid and if setup was sucessfull */
324 uint32_t x[4], z[4], t;
325 #define BPX ((uint8_t*)&(x[0]))
326 #define BPZ ((uint8_t*)&(z[0]))
327 s->shortkey = (keylength<=80);
328 /* littel endian only! */
329 memset(&(x[0]), 0 ,16); /* set x to zero */
330 memcpy(&(x[0]), key, keylength/8);
333 /* todo: merge a and b and compress the whole stuff */
335 z[0] = x[0] ^ S_5X(0xD) ^ S_6X(0xF) ^ S_7X(0xC) ^ S_8X(0xE) ^ S_7X(0x8);
336 z[1] = x[2] ^ S_5Z(0x0) ^ S_6Z(0x2) ^ S_7Z(0x1) ^ S_8Z(0x3) ^ S_8X(0xA);
337 z[2] = x[3] ^ S_5Z(0x7) ^ S_6Z(0x6) ^ S_7Z(0x5) ^ S_8Z(0x4) ^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S_5X(0xE) ^ S_6X(0xF) ^ S_7X(0x1) ^ S_8X(0x0) ^ S_8X(0xD);
375 /* that were the masking keys, now the rotation keys */
376 /* set the keys to zero */
377 memset(&(s->rotl[0]),0,8);
378 s->roth[0]=s->roth[1]=0;
380 z[0] = x[0] ^ S_5X(0xD) ^ S_6X(0xF) ^ S_7X(0xC) ^ S_8X(0xE) ^ S_7X(0x8);
381 z[1] = x[2] ^ S_5Z(0x0) ^ S_6Z(0x2) ^ S_7Z(0x1) ^ S_8Z(0x3) ^ S_8X(0xA);
382 z[2] = x[3] ^ S_5Z(0x7) ^ S_6Z(0x6) ^ S_7Z(0x5) ^ S_8Z(0x4) ^ S_5X(0x9);
383 z[3] = x[1] ^ S_5Z(0xA) ^ S_6Z(0x9) ^ S_7Z(0xB) ^ S_8Z(0x8) ^ S_6X(0xB);
385 t = S_5Z(0x8) ^ S_6Z(0x9) ^ S_7Z(0x7) ^ S_8Z(0x6) ^ S_5Z(0x2);
387 s->rotl[0] |= t & 0x0f;
388 s->roth[0] |= (t >> 4) & (1<<0);
389 t = S_5Z(0xA) ^ S_6Z(0xB) ^ S_7Z(0x5) ^ S_8Z(0x4) ^ S_6Z(0x6);
391 s->rotl[0] |= (t<<4) & 0xf0;
392 s->roth[0] |= (t >> 3) & (1<<1);
393 t = S_5Z(0xC) ^ S_6Z(0xD) ^ S_7Z(0x3) ^ S_8Z(0x2) ^ S_7Z(0x9);
395 s->rotl[1] |= t & 0x0f;
396 s->roth[0] |= (t >> 2) & (1<<2);
397 t = S_5Z(0xE) ^ S_6Z(0xF) ^ S_7Z(0x1) ^ S_8Z(0x0) ^ S_8Z(0xC);
399 s->rotl[1] |= (t<<4) & 0xf0;
400 s->roth[0] |= (t >> 1) & (1<<3);
402 x[0] = z[2] ^ S_5Z(0x5) ^ S_6Z(0x7) ^ S_7Z(0x4) ^ S_8Z(0x6) ^ S_7Z(0x0);
403 x[1] = z[0] ^ S_5X(0x0) ^ S_6X(0x2) ^ S_7X(0x1) ^ S_8X(0x3) ^ S_8Z(0x2);
404 x[2] = z[1] ^ S_5X(0x7) ^ S_6X(0x6) ^ S_7X(0x5) ^ S_8X(0x4) ^ S_5Z(0x1);
405 x[3] = z[3] ^ S_5X(0xA) ^ S_6X(0x9) ^ S_7X(0xB) ^ S_8X(0x8) ^ S_6Z(0x3);
407 t = S_5X(0x3) ^ S_6X(0x2) ^ S_7X(0xC) ^ S_8X(0xD) ^ S_5X(0x8);
409 s->rotl[2] |= t & 0x0f;
410 s->roth[0] |= t & (1<<4);
411 t = S_5X(0x1) ^ S_6X(0x0) ^ S_7X(0xE) ^ S_8X(0xF) ^ S_6X(0xD);
413 s->rotl[2] |= (t<<4) & 0xf0;
414 s->roth[0] |= (t<<1) & (1<<5);
415 t = S_5X(0x7) ^ S_6X(0x6) ^ S_7X(0x8) ^ S_8X(0x9) ^ S_7X(0x3);
417 s->rotl[3] |= t & 0x0f;
418 s->roth[0] |= (t<<2) & (1<<6);
419 t = S_5X(0x5) ^ S_6X(0x4) ^ S_7X(0xA) ^ S_8X(0xB) ^ S_8X(0x7);
421 s->rotl[3] |= (t<<4) & 0xf0;
422 s->roth[0] |= (t<<3) & (1<<7);
424 z[0] = x[0] ^ S_5X(0xD) ^ S_6X(0xF) ^ S_7X(0xC) ^ S_8X(0xE) ^ S_7X(0x8);
425 z[1] = x[2] ^ S_5Z(0x0) ^ S_6Z(0x2) ^ S_7Z(0x1) ^ S_8Z(0x3) ^ S_8X(0xA);
426 z[2] = x[3] ^ S_5Z(0x7) ^ S_6Z(0x6) ^ S_7Z(0x5) ^ S_8Z(0x4) ^ S_5X(0x9);
427 z[3] = x[1] ^ S_5Z(0xA) ^ S_6Z(0x9) ^ S_7Z(0xB) ^ S_8Z(0x8) ^ S_6X(0xB);
429 t = S_5Z(0x3) ^ S_6Z(0x2) ^ S_7Z(0xC) ^ S_8Z(0xD) ^ S_5Z(0x9);
431 s->rotl[4] |= t & 0x0f;
432 s->roth[1] |= (t>>4) & (1<<0);
433 t = S_5Z(0x1) ^ S_6Z(0x0) ^ S_7Z(0xE) ^ S_8Z(0xF) ^ S_6Z(0xC);
435 s->rotl[4] |= (t<<4) & 0xf0;
436 s->roth[1] |= (t>>3) & (1<<1);
437 t = S_5Z(0x7) ^ S_6Z(0x6) ^ S_7Z(0x8) ^ S_8Z(0x9) ^ S_7Z(0x2);
439 s->rotl[5] |= t & 0x0f;
440 s->roth[1] |= (t>>2) & (1<<2);
441 t = S_5Z(0x5) ^ S_6Z(0x4) ^ S_7Z(0xA) ^ S_8Z(0xB) ^ S_8Z(0x6);
443 s->rotl[5] |= (t<<4) & 0xf0;
444 s->roth[1] |= (t>>1) & (1<<3);
446 x[0] = z[2] ^ S_5Z(0x5) ^ S_6Z(0x7) ^ S_7Z(0x4) ^ S_8Z(0x6) ^ S_7Z(0x0);
447 x[1] = z[0] ^ S_5X(0x0) ^ S_6X(0x2) ^ S_7X(0x1) ^ S_8X(0x3) ^ S_8Z(0x2);
448 x[2] = z[1] ^ S_5X(0x7) ^ S_6X(0x6) ^ S_7X(0x5) ^ S_8X(0x4) ^ S_5Z(0x1);
449 x[3] = z[3] ^ S_5X(0xA) ^ S_6X(0x9) ^ S_7X(0xB) ^ S_8X(0x8) ^ S_6Z(0x3);
451 t = S_5X(0x8) ^ S_6X(0x9) ^ S_7X(0x7) ^ S_8X(0x6) ^ S_5X(0x3);
453 s->rotl[6] |= t & 0x0f;
454 s->roth[1] |= t & (1<<4);
455 t = S_5X(0xA) ^ S_6X(0xB) ^ S_7X(0x5) ^ S_8X(0x4) ^ S_6X(0x7);
457 s->rotl[6] |= (t<<4) & 0xf0;
458 s->roth[1] |= (t<<1) & (1<<5);
459 t = S_5X(0xC) ^ S_6X(0xD) ^ S_7X(0x3) ^ S_8X(0x2) ^ S_7X(0x8);
461 s->rotl[7] |= t & 0x0f;
462 s->roth[1] |= (t<<2) & (1<<6);
463 t = S_5X(0xE) ^ S_6X(0xF) ^ S_7X(0x1) ^ S_8X(0x0) ^ S_8X(0xD);
465 s->rotl[7] |= (t<<4) & 0xf0;
466 s->roth[1] |= (t<<3) & (1<<7);