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