b9036b4ef7e742baad81a0b9c2d17ffa6bc61bc8
[silc.git] / lib / silccrypt / twofish.c
1 /* Modified for SILC. -Pekka */
2
3 /* This is an independent implementation of the encryption algorithm:   */
4 /*                                                                      */
5 /*         Twofish by Bruce Schneier and colleagues                     */
6 /*                                                                      */
7 /* which is a candidate algorithm in the Advanced Encryption Standard   */
8 /* programme of the US National Institute of Standards and Technology.  */
9 /*                                                                      */
10 /* Copyright in this implementation is held by Dr B R Gladman but I     */
11 /* hereby give permission for its free direct or derivative use subject */
12 /* to acknowledgment of its origin and compliance with any conditions   */
13 /* that the originators of t he algorithm place on its exploitation.     */
14 /*                                                                      */
15 /* My thanks to Doug Whiting and Niels Ferguson for comments that led   */
16 /* to improvements in this implementation.                              */
17 /*                                                                      */
18 /* Dr Brian Gladman (gladman@seven77.demon.co.uk) 14th January 1999     */
19
20 /* Timing data for Twofish (twofish.c)
21
22 128 bit key:
23 Key Setup:    8414 cycles
24 Encrypt:       376 cycles =    68.1 mbits/sec
25 Decrypt:       374 cycles =    68.4 mbits/sec
26 Mean:          375 cycles =    68.3 mbits/sec
27
28 192 bit key:
29 Key Setup:   11628 cycles
30 Encrypt:       376 cycles =    68.1 mbits/sec
31 Decrypt:       374 cycles =    68.4 mbits/sec
32 Mean:          375 cycles =    68.3 mbits/sec
33
34 256 bit key:
35 Key Setup:   15457 cycles
36 Encrypt:       381 cycles =    67.2 mbits/sec
37 Decrypt:       374 cycles =    68.4 mbits/sec
38 Mean:          378 cycles =    67.8 mbits/sec
39
40 */
41
42 #include "silcincludes.h"
43 #include "twofish.h"
44
45 /* 
46  * SILC Crypto API for Twofish
47  */
48
49 /* Sets the key for the cipher. */
50
51 SILC_CIPHER_API_SET_KEY(twofish)
52 {
53   uint32 k[8];
54
55   SILC_GET_WORD_KEY(key, k, keylen);
56   twofish_set_key((TwofishContext *)context, k, keylen);
57
58   return TRUE;
59 }
60
61 /* Sets the string as a new key for the cipher. The string is first
62    hashed and then used as a new key. */
63
64 SILC_CIPHER_API_SET_KEY_WITH_STRING(twofish)
65 {
66   return FALSE;
67 }
68
69 /* Returns the size of the cipher context. */
70
71 SILC_CIPHER_API_CONTEXT_LEN(twofish)
72 {
73   return sizeof(TwofishContext);
74 }
75
76 /* Encrypts with the cipher in CBC mode. Source and destination buffers
77    maybe one and same. */
78
79 SILC_CIPHER_API_ENCRYPT_CBC(twofish)
80 {
81   uint32 tiv[4];
82   int i;
83
84   SILC_CBC_GET_IV(tiv, iv);
85
86   SILC_CBC_ENC_PRE(tiv, src);
87   twofish_encrypt((TwofishContext *)context, tiv, tiv);
88   SILC_CBC_ENC_POST(tiv, dst, src);
89
90   for (i = 16; i < len; i += 16) {
91     SILC_CBC_ENC_PRE(tiv, src);
92     twofish_encrypt((TwofishContext *)context, tiv, tiv);
93     SILC_CBC_ENC_POST(tiv, dst, src);
94   }
95
96   SILC_CBC_PUT_IV(tiv, iv);
97
98   return TRUE;
99 }
100
101 /* Decrypts with the cipher in CBC mode. Source and destination buffers
102    maybe one and same. */
103
104 SILC_CIPHER_API_DECRYPT_CBC(twofish)
105 {
106   uint32 tmp[4], tmp2[4], tiv[4];
107   int i;
108
109   SILC_CBC_GET_IV(tiv, iv);
110
111   SILC_CBC_DEC_PRE(tmp, src);
112   twofish_decrypt((TwofishContext *)context, tmp, tmp2);
113   SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
114
115   for (i = 16; i < len; i += 16) {
116     SILC_CBC_DEC_PRE(tmp, src);
117     twofish_decrypt((TwofishContext *)context, tmp, tmp2);
118     SILC_CBC_DEC_POST(tmp2, dst, src, tmp, tiv);
119   }
120   
121   SILC_CBC_PUT_IV(tiv, iv);
122   
123   return TRUE;
124 }
125
126 #if 0
127 #define Q_TABLES
128 #define M_TABLE
129 #define MK_TABLE
130 #define ONE_STEP
131 #endif
132
133 /* finite field arithmetic for GF(2**8) with the modular    */
134 /* polynomial x^8 + x^6 + x^5 + x^3 + 1 (0x169)             */
135
136 #define G_M 0x0169
137
138 u1byte  tab_5b[4] = { 0, G_M >> 2, G_M >> 1, (G_M >> 1) ^ (G_M >> 2) };
139 u1byte  tab_ef[4] = { 0, (G_M >> 1) ^ (G_M >> 2), G_M >> 1, G_M >> 2 };
140
141 #define ffm_01(x)    (x)
142 #define ffm_5b(x)   ((x) ^ ((x) >> 2) ^ tab_5b[(x) & 3])
143 #define ffm_ef(x)   ((x) ^ ((x) >> 1) ^ ((x) >> 2) ^ tab_ef[(x) & 3])
144
145 u1byte ror4[16] = { 0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15 };
146 u1byte ashx[16] = { 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, 5, 14, 7 };
147
148 u1byte qt0[2][16] = 
149 {   { 8, 1, 7, 13, 6, 15, 3, 2, 0, 11, 5, 9, 14, 12, 10, 4 },
150     { 2, 8, 11, 13, 15, 7, 6, 14, 3, 1, 9, 4, 0, 10, 12, 5 }
151 };
152
153 u1byte qt1[2][16] =
154 {   { 14, 12, 11, 8, 1, 2, 3, 5, 15, 4, 10, 6, 7, 0, 9, 13 }, 
155     { 1, 14, 2, 11, 4, 12, 3, 7, 6, 13, 10, 5, 15, 9, 0, 8 }
156 };
157
158 u1byte qt2[2][16] = 
159 {   { 11, 10, 5, 14, 6, 13, 9, 0, 12, 8, 15, 3, 2, 4, 7, 1 },
160     { 4, 12, 7, 5, 1, 6, 9, 10, 0, 14, 13, 8, 2, 11, 3, 15 }
161 };
162
163 u1byte qt3[2][16] = 
164 {   { 13, 7, 15, 4, 1, 2, 6, 14, 9, 11, 3, 0, 8, 5, 12, 10 },
165     { 11, 9, 5, 1, 12, 3, 13, 14, 6, 4, 7, 15, 2, 0, 8, 10 }
166 };
167  
168 u1byte qp(const u4byte n, const u1byte x)
169 {   u1byte  a0, a1, a2, a3, a4, b0, b1, b2, b3, b4;
170
171     a0 = x >> 4; b0 = x & 15;
172     a1 = a0 ^ b0; b1 = ror4[b0] ^ ashx[a0];
173     a2 = qt0[n][a1]; b2 = qt1[n][b1];
174     a3 = a2 ^ b2; b3 = ror4[b2] ^ ashx[a2];
175     a4 = qt2[n][a3]; b4 = qt3[n][b3];
176     return (b4 << 4) | a4;
177 };
178
179 #ifdef  Q_TABLES
180
181 u4byte  qt_gen = 0;
182 u1byte  q_tab[2][256];
183
184 #define q(n,x)  q_tab[n][x]
185
186 void gen_qtab(void)
187 {   u4byte  i;
188
189     for(i = 0; i < 256; ++i)
190     {       
191         q(0,i) = qp(0, (u1byte)i);
192         q(1,i) = qp(1, (u1byte)i);
193     }
194 };
195
196 #else
197
198 #define q(n,x)  qp(n, x)
199
200 #endif
201
202 #ifdef  M_TABLE
203
204 u4byte  mt_gen = 0;
205 u4byte  m_tab[4][256];
206
207 void gen_mtab(void)
208 {   u4byte  i, f01, f5b, fef;
209     
210     for(i = 0; i < 256; ++i)
211     {
212         f01 = q(1,i); f5b = ffm_5b(f01); fef = ffm_ef(f01);
213         m_tab[0][i] = f01 + (f5b << 8) + (fef << 16) + (fef << 24);
214         m_tab[2][i] = f5b + (fef << 8) + (f01 << 16) + (fef << 24);
215
216         f01 = q(0,i); f5b = ffm_5b(f01); fef = ffm_ef(f01);
217         m_tab[1][i] = fef + (fef << 8) + (f5b << 16) + (f01 << 24);
218         m_tab[3][i] = f5b + (f01 << 8) + (fef << 16) + (f5b << 24);
219     }
220 };
221
222 #define mds(n,x)    m_tab[n][x]
223
224 #else
225
226 #define fm_00   ffm_01
227 #define fm_10   ffm_5b
228 #define fm_20   ffm_ef
229 #define fm_30   ffm_ef
230 #define q_0(x)  q(1,x)
231
232 #define fm_01   ffm_ef
233 #define fm_11   ffm_ef
234 #define fm_21   ffm_5b
235 #define fm_31   ffm_01
236 #define q_1(x)  q(0,x)
237
238 #define fm_02   ffm_5b
239 #define fm_12   ffm_ef
240 #define fm_22   ffm_01
241 #define fm_32   ffm_ef
242 #define q_2(x)  q(1,x)
243
244 #define fm_03   ffm_5b
245 #define fm_13   ffm_01
246 #define fm_23   ffm_ef
247 #define fm_33   ffm_5b
248 #define q_3(x)  q(0,x)
249
250 #define f_0(n,x)    ((u4byte)fm_0##n(x))
251 #define f_1(n,x)    ((u4byte)fm_1##n(x) << 8)
252 #define f_2(n,x)    ((u4byte)fm_2##n(x) << 16)
253 #define f_3(n,x)    ((u4byte)fm_3##n(x) << 24)
254
255 #define mds(n,x)    f_0(n,q_##n(x)) ^ f_1(n,q_##n(x)) ^ f_2(n,q_##n(x)) ^ f_3(n,q_##n(x))
256
257 #endif
258
259 u4byte h_fun(TwofishContext *ctx, const u4byte x, const u4byte key[])
260 {   u4byte  b0, b1, b2, b3;
261
262 #ifndef M_TABLE
263     u4byte  m5b_b0, m5b_b1, m5b_b2, m5b_b3;
264     u4byte  mef_b0, mef_b1, mef_b2, mef_b3;
265 #endif
266
267     b0 = byte(x, 0); b1 = byte(x, 1); b2 = byte(x, 2); b3 = byte(x, 3);
268
269     switch(ctx->k_len)
270     {
271     case 4: b0 = q(1, b0) ^ byte(key[3],0);
272             b1 = q(0, b1) ^ byte(key[3],1);
273             b2 = q(0, b2) ^ byte(key[3],2);
274             b3 = q(1, b3) ^ byte(key[3],3);
275     case 3: b0 = q(1, b0) ^ byte(key[2],0);
276             b1 = q(1, b1) ^ byte(key[2],1);
277             b2 = q(0, b2) ^ byte(key[2],2);
278             b3 = q(0, b3) ^ byte(key[2],3);
279     case 2: b0 = q(0,q(0,b0) ^ byte(key[1],0)) ^ byte(key[0],0);
280             b1 = q(0,q(1,b1) ^ byte(key[1],1)) ^ byte(key[0],1);
281             b2 = q(1,q(0,b2) ^ byte(key[1],2)) ^ byte(key[0],2);
282             b3 = q(1,q(1,b3) ^ byte(key[1],3)) ^ byte(key[0],3);
283     }
284 #ifdef  M_TABLE
285
286     return  mds(0, b0) ^ mds(1, b1) ^ mds(2, b2) ^ mds(3, b3);
287
288 #else
289
290     b0 = q(1, b0); b1 = q(0, b1); b2 = q(1, b2); b3 = q(0, b3);
291     m5b_b0 = ffm_5b(b0); m5b_b1 = ffm_5b(b1); m5b_b2 = ffm_5b(b2); m5b_b3 = ffm_5b(b3);
292     mef_b0 = ffm_ef(b0); mef_b1 = ffm_ef(b1); mef_b2 = ffm_ef(b2); mef_b3 = ffm_ef(b3);
293     b0 ^= mef_b1 ^ m5b_b2 ^ m5b_b3; b3 ^= m5b_b0 ^ mef_b1 ^ mef_b2;
294     b2 ^= mef_b0 ^ m5b_b1 ^ mef_b3; b1 ^= mef_b0 ^ mef_b2 ^ m5b_b3;
295
296     return b0 | (b3 << 8) | (b2 << 16) | (b1 << 24);
297
298 #endif
299 };
300
301 #ifdef  MK_TABLE
302
303 #ifdef  ONE_STEP
304 u4byte  mk_tab[4][256];
305 #else
306 u1byte  sb[4][256];
307 #endif
308
309 #define q20(x)  q(0,q(0,x) ^ byte(key[1],0)) ^ byte(key[0],0)
310 #define q21(x)  q(0,q(1,x) ^ byte(key[1],1)) ^ byte(key[0],1)
311 #define q22(x)  q(1,q(0,x) ^ byte(key[1],2)) ^ byte(key[0],2)
312 #define q23(x)  q(1,q(1,x) ^ byte(key[1],3)) ^ byte(key[0],3)
313
314 #define q30(x)  q(0,q(0,q(1, x) ^ byte(key[2],0)) ^ byte(key[1],0)) ^ byte(key[0],0)
315 #define q31(x)  q(0,q(1,q(1, x) ^ byte(key[2],1)) ^ byte(key[1],1)) ^ byte(key[0],1)
316 #define q32(x)  q(1,q(0,q(0, x) ^ byte(key[2],2)) ^ byte(key[1],2)) ^ byte(key[0],2)
317 #define q33(x)  q(1,q(1,q(0, x) ^ byte(key[2],3)) ^ byte(key[1],3)) ^ byte(key[0],3)
318
319 #define q40(x)  q(0,q(0,q(1, q(1, x) ^ byte(key[3],0)) ^ byte(key[2],0)) ^ byte(key[1],0)) ^ byte(key[0],0)
320 #define q41(x)  q(0,q(1,q(1, q(0, x) ^ byte(key[3],1)) ^ byte(key[2],1)) ^ byte(key[1],1)) ^ byte(key[0],1)
321 #define q42(x)  q(1,q(0,q(0, q(0, x) ^ byte(key[3],2)) ^ byte(key[2],2)) ^ byte(key[1],2)) ^ byte(key[0],2)
322 #define q43(x)  q(1,q(1,q(0, q(1, x) ^ byte(key[3],3)) ^ byte(key[2],3)) ^ byte(key[1],3)) ^ byte(key[0],3)
323
324 void gen_mk_tab(TwofishContext *ctx, u4byte key[])
325 {   u4byte  i;
326     u1byte  by;
327
328     switch(ctx->k_len)
329     {
330     case 2: for(i = 0; i < 256; ++i)
331             {
332                 by = (u1byte)i;
333 #ifdef ONE_STEP
334                 mk_tab[0][i] = mds(0, q20(by)); mk_tab[1][i] = mds(1, q21(by));
335                 mk_tab[2][i] = mds(2, q22(by)); mk_tab[3][i] = mds(3, q23(by));
336 #else
337                 sb[0][i] = q20(by); sb[1][i] = q21(by); 
338                 sb[2][i] = q22(by); sb[3][i] = q23(by);
339 #endif
340             }
341             break;
342     
343     case 3: for(i = 0; i < 256; ++i)
344             {
345                 by = (u1byte)i;
346 #ifdef ONE_STEP
347                 mk_tab[0][i] = mds(0, q30(by)); mk_tab[1][i] = mds(1, q31(by));
348                 mk_tab[2][i] = mds(2, q32(by)); mk_tab[3][i] = mds(3, q33(by));
349 #else
350                 sb[0][i] = q30(by); sb[1][i] = q31(by); 
351                 sb[2][i] = q32(by); sb[3][i] = q33(by);
352 #endif
353             }
354             break;
355     
356     case 4: for(i = 0; i < 256; ++i)
357             {
358                 by = (u1byte)i;
359 #ifdef ONE_STEP
360                 mk_tab[0][i] = mds(0, q40(by)); mk_tab[1][i] = mds(1, q41(by));
361                 mk_tab[2][i] = mds(2, q42(by)); mk_tab[3][i] = mds(3, q43(by));
362 #else
363                 sb[0][i] = q40(by); sb[1][i] = q41(by); 
364                 sb[2][i] = q42(by); sb[3][i] = q43(by);
365 #endif
366             }
367     }
368 };
369
370 #  ifdef ONE_STEP
371 #    define g0_fun(x) ( mk_tab[0][byte(x,0)] ^ mk_tab[1][byte(x,1)] \
372                       ^ mk_tab[2][byte(x,2)] ^ mk_tab[3][byte(x,3)] )
373 #    define g1_fun(x) ( mk_tab[0][byte(x,3)] ^ mk_tab[1][byte(x,0)] \
374                       ^ mk_tab[2][byte(x,1)] ^ mk_tab[3][byte(x,2)] )
375 #  else
376 #    define g0_fun(x) ( mds(0, sb[0][byte(x,0)]) ^ mds(1, sb[1][byte(x,1)]) \
377                       ^ mds(2, sb[2][byte(x,2)]) ^ mds(3, sb[3][byte(x,3)]) )
378 #    define g1_fun(x) ( mds(0, sb[0][byte(x,3)]) ^ mds(1, sb[1][byte(x,0)]) \
379                       ^ mds(2, sb[2][byte(x,1)]) ^ mds(3, sb[3][byte(x,2)]) )
380 #  endif
381
382 #else
383
384 #define g0_fun(x)   h_fun(ctx,x,s_key)
385 #define g1_fun(x)   h_fun(ctx,rotl(x,8),s_key)
386
387 #endif
388
389 /* The (12,8) Reed Soloman code has the generator polynomial
390
391   g(x) = x^4 + (a + 1/a) * x^3 + a * x^2 + (a + 1/a) * x + 1
392
393 where the coefficients are in the finite field GF(2^8) with a
394 modular polynomial a^8 + a^6 + a^3 + a^2 + 1. To generate the
395 remainder we have to start with a 12th order polynomial with our
396 eight input bytes as the coefficients of the 4th to 11th terms. 
397 That is:
398
399   m[7] * x^11 + m[6] * x^10 ... + m[0] * x^4 + 0 * x^3 +... + 0
400   
401 We then multiply the generator polynomial by m[7] * x^7 and subtract
402 it - xor in GF(2^8) - from the above to eliminate the x^7 term (the 
403 artihmetic on the coefficients is done in GF(2^8). We then multiply 
404 the generator polynomial by x^6 * coeff(x^10) and use this to remove
405 the x^10 term. We carry on in this way until the x^4 term is removed
406 so that we are left with:
407
408   r[3] * x^3 + r[2] * x^2 + r[1] 8 x^1 + r[0]
409
410 which give the resulting 4 bytes of the remainder. This is equivalent 
411 to the matrix multiplication in the Twofish description but much faster 
412 to implement.
413
414 */
415
416 #define G_MOD   0x0000014d
417
418 u4byte mds_rem(u4byte p0, u4byte p1)
419 {   u4byte  i, t, u;
420
421     for(i = 0; i < 8; ++i)
422     {
423         t = p1 >> 24;   /* get most significant coefficient */
424         
425         p1 = (p1 << 8) | (p0 >> 24); p0 <<= 8;  /* shift others up */
426             
427         /* multiply t by a (the primitive element - i.e. left shift) */
428
429         u = (t << 1); 
430         
431         if(t & 0x80)            /* subtract modular polynomial on overflow */
432         
433             u ^= G_MOD; 
434
435         p1 ^= t ^ (u << 16);    /* remove t * (a * x^2 + 1) */
436
437         u ^= (t >> 1);          /* form u = a * t + t / a = t * (a + 1 / a); */
438         
439         if(t & 0x01)            /* add the modular polynomial on underflow */
440         
441             u ^= G_MOD >> 1;
442
443         p1 ^= (u << 24) | (u << 8); /* remove t * (a + 1/a) * (x^3 + x) */
444     }
445
446     return p1;
447 };
448
449 /* initialise the key schedule from the user supplied key   */
450
451 u4byte *twofish_set_key(TwofishContext *ctx,
452                         const u4byte in_key[], const u4byte key_len)
453 {   
454     u4byte  i, a, b, me_key[4], mo_key[4];
455     u4byte *l_key = ctx->l_key;
456     u4byte *s_key = ctx->s_key;
457     
458 #ifdef Q_TABLES
459     if(!qt_gen)
460     {
461         gen_qtab(); qt_gen = 1;
462     }
463 #endif
464
465 #ifdef M_TABLE
466     if(!mt_gen)
467     {
468         gen_mtab(); mt_gen = 1;
469     }
470 #endif
471
472     ctx->k_len = ctx->k_len = key_len / 64;   /* 2, 3 or 4 */
473
474     for(i = 0; i < ctx->k_len; ++i)
475     {
476         a = in_key[i + i];     me_key[i] = a;
477         b = in_key[i + i + 1]; mo_key[i] = b;
478         s_key[ctx->k_len - i - 1] = mds_rem(a, b);
479     }
480
481     for(i = 0; i < 40; i += 2)
482     {
483         a = 0x01010101 * i; b = a + 0x01010101;
484         a = h_fun(ctx,a, me_key);
485         b = rotl(h_fun(ctx,b, mo_key), 8);
486         l_key[i] = a + b;
487         l_key[i + 1] = rotl(a + 2 * b, 9);
488     }
489
490 #ifdef MK_TABLE
491     gen_mk_tab(ctx,s_key);
492 #endif
493
494     return l_key;
495 };
496
497 /* encrypt a block of text  */
498
499 #define f_rnd(i)                                                    \
500     t1 = g1_fun(blk[1]); t0 = g0_fun(blk[0]);                       \
501     blk[2] = rotr(blk[2] ^ (t0 + t1 + l_key[4 * (i) + 8]), 1);      \
502     blk[3] = rotl(blk[3], 1) ^ (t0 + 2 * t1 + l_key[4 * (i) + 9]);  \
503     t1 = g1_fun(blk[3]); t0 = g0_fun(blk[2]);                       \
504     blk[0] = rotr(blk[0] ^ (t0 + t1 + l_key[4 * (i) + 10]), 1);     \
505     blk[1] = rotl(blk[1], 1) ^ (t0 + 2 * t1 + l_key[4 * (i) + 11])
506
507 void twofish_encrypt(TwofishContext *ctx,
508                      const u4byte in_blk[4], u4byte out_blk[])
509 {   
510     u4byte  t0, t1, blk[4];
511     u4byte *l_key = ctx->l_key;
512     u4byte *s_key = ctx->s_key;
513
514     blk[0] = in_blk[0] ^ l_key[0];
515     blk[1] = in_blk[1] ^ l_key[1];
516     blk[2] = in_blk[2] ^ l_key[2];
517     blk[3] = in_blk[3] ^ l_key[3];
518
519     f_rnd(0); f_rnd(1); f_rnd(2); f_rnd(3);
520     f_rnd(4); f_rnd(5); f_rnd(6); f_rnd(7);
521
522     out_blk[0] = blk[2] ^ l_key[4];
523     out_blk[1] = blk[3] ^ l_key[5];
524     out_blk[2] = blk[0] ^ l_key[6];
525     out_blk[3] = blk[1] ^ l_key[7]; 
526 };
527
528 /* decrypt a block of text  */
529
530 #define i_rnd(i)                                                        \
531         t1 = g1_fun(blk[1]); t0 = g0_fun(blk[0]);                       \
532         blk[2] = rotl(blk[2], 1) ^ (t0 + t1 + l_key[4 * (i) + 10]);     \
533         blk[3] = rotr(blk[3] ^ (t0 + 2 * t1 + l_key[4 * (i) + 11]), 1); \
534         t1 = g1_fun(blk[3]); t0 = g0_fun(blk[2]);                       \
535         blk[0] = rotl(blk[0], 1) ^ (t0 + t1 + l_key[4 * (i) +  8]);     \
536         blk[1] = rotr(blk[1] ^ (t0 + 2 * t1 + l_key[4 * (i) +  9]), 1)
537
538 void twofish_decrypt(TwofishContext *ctx,
539                      const u4byte in_blk[4], u4byte out_blk[4])
540 {   
541     u4byte  t0, t1, blk[4];
542     u4byte *l_key = ctx->l_key;
543     u4byte *s_key = ctx->s_key;
544
545     blk[0] = in_blk[0] ^ l_key[4];
546     blk[1] = in_blk[1] ^ l_key[5];
547     blk[2] = in_blk[2] ^ l_key[6];
548     blk[3] = in_blk[3] ^ l_key[7];
549
550     i_rnd(7); i_rnd(6); i_rnd(5); i_rnd(4);
551     i_rnd(3); i_rnd(2); i_rnd(1); i_rnd(0);
552
553     out_blk[0] = blk[2] ^ l_key[0];
554     out_blk[1] = blk[3] ^ l_key[1];
555     out_blk[2] = blk[0] ^ l_key[2];
556     out_blk[3] = blk[1] ^ l_key[3]; 
557 };