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