Home | History | Annotate | Line # | Download | only in des
      1  1.6   andvar /*	$NetBSD: des_locl.h,v 1.6 2025/02/24 07:11:24 andvar Exp $	*/
      2  1.3   itojun /*	$KAME: des_locl.h,v 1.6 2000/11/06 13:58:09 itojun Exp $	*/
      3  1.1  thorpej 
      4  1.4      tls /* crypto/des/des_locl.h */
      5  1.4      tls /* Copyright (C) 1995-1997 Eric Young (eay (at) mincom.oz.au)
      6  1.1  thorpej  * All rights reserved.
      7  1.1  thorpej  *
      8  1.1  thorpej  * This file is part of an SSL implementation written
      9  1.1  thorpej  * by Eric Young (eay (at) mincom.oz.au).
     10  1.1  thorpej  * The implementation was written so as to conform with Netscapes SSL
     11  1.1  thorpej  * specification.  This library and applications are
     12  1.1  thorpej  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
     13  1.1  thorpej  * as long as the following conditions are aheared to.
     14  1.1  thorpej  *
     15  1.1  thorpej  * Copyright remains Eric Young's, and as such any Copyright notices in
     16  1.1  thorpej  * the code are not to be removed.  If this code is used in a product,
     17  1.1  thorpej  * Eric Young should be given attribution as the author of the parts used.
     18  1.1  thorpej  * This can be in the form of a textual message at program startup or
     19  1.1  thorpej  * in documentation (online or textual) provided with the package.
     20  1.1  thorpej  *
     21  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     22  1.1  thorpej  * modification, are permitted provided that the following conditions
     23  1.1  thorpej  * are met:
     24  1.1  thorpej  * 1. Redistributions of source code must retain the copyright
     25  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     26  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     27  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     28  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     29  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     30  1.1  thorpej  *    must display the following acknowledgement:
     31  1.1  thorpej  *    This product includes software developed by Eric Young (eay (at) mincom.oz.au)
     32  1.1  thorpej  *
     33  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
     34  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     35  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     36  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     37  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     38  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     39  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     40  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     41  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     42  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     43  1.1  thorpej  * SUCH DAMAGE.
     44  1.1  thorpej  *
     45  1.1  thorpej  * The licence and distribution terms for any publically available version or
     46  1.1  thorpej  * derivative of this code cannot be changed.  i.e. this code cannot simply be
     47  1.1  thorpej  * copied and put under another distribution licence
     48  1.1  thorpej  * [including the GNU Public Licence.]
     49  1.1  thorpej  */
     50  1.1  thorpej 
     51  1.1  thorpej #ifndef HEADER_DES_LOCL_H
     52  1.1  thorpej #define HEADER_DES_LOCL_H
     53  1.1  thorpej 
     54  1.1  thorpej #include <crypto/des/des.h>
     55  1.1  thorpej 
     56  1.1  thorpej #undef DES_PTR
     57  1.1  thorpej 
     58  1.1  thorpej #ifdef __STDC__
     59  1.1  thorpej #undef NOPROTO
     60  1.1  thorpej #endif
     61  1.1  thorpej 
     62  1.1  thorpej #define ITERATIONS 16
     63  1.1  thorpej #define HALF_ITERATIONS 8
     64  1.1  thorpej 
     65  1.1  thorpej /* used in des_read and des_write */
     66  1.1  thorpej #define MAXWRITE	(1024*16)
     67  1.1  thorpej #define BSIZE		(MAXWRITE+4)
     68  1.1  thorpej 
     69  1.1  thorpej #define c2l(c,l)	(l =((DES_LONG)(*((c)++)))    , \
     70  1.1  thorpej 			 l|=((DES_LONG)(*((c)++)))<< 8L, \
     71  1.1  thorpej 			 l|=((DES_LONG)(*((c)++)))<<16L, \
     72  1.1  thorpej 			 l|=((DES_LONG)(*((c)++)))<<24L)
     73  1.1  thorpej 
     74  1.1  thorpej /* NOTE - c is not incremented as per c2l */
     75  1.1  thorpej #define c2ln(c,l1,l2,n)	{ \
     76  1.1  thorpej 			c+=n; \
     77  1.1  thorpej 			l1=l2=0; \
     78  1.1  thorpej 			switch (n) { \
     79  1.5      mrg 			case 8: l2 =((DES_LONG)(*(--(c))))<<24L; /* FALLTHROUGH */\
     80  1.5      mrg 			case 7: l2|=((DES_LONG)(*(--(c))))<<16L; /* FALLTHROUGH */\
     81  1.5      mrg 			case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; /* FALLTHROUGH */\
     82  1.5      mrg 			case 5: l2|=((DES_LONG)(*(--(c))));     /* FALLTHROUGH */\
     83  1.5      mrg 			case 4: l1 =((DES_LONG)(*(--(c))))<<24L; /* FALLTHROUGH */\
     84  1.5      mrg 			case 3: l1|=((DES_LONG)(*(--(c))))<<16L; /* FALLTHROUGH */\
     85  1.5      mrg 			case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; /* FALLTHROUGH */\
     86  1.5      mrg 			case 1: l1|=((DES_LONG)(*(--(c))));     /* FALLTHROUGH */\
     87  1.1  thorpej 				} \
     88  1.1  thorpej 			}
     89  1.1  thorpej 
     90  1.1  thorpej #define l2c(l,c)	(*((c)++)=(unsigned char)(((l)     )&0xff), \
     91  1.1  thorpej 			 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
     92  1.1  thorpej 			 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
     93  1.1  thorpej 			 *((c)++)=(unsigned char)(((l)>>24L)&0xff))
     94  1.1  thorpej 
     95  1.1  thorpej /* replacements for htonl and ntohl since I have no idea what to do
     96  1.1  thorpej  * when faced with machines with 8 byte longs. */
     97  1.1  thorpej #define HDRSIZE 4
     98  1.1  thorpej 
     99  1.1  thorpej #define n2l(c,l)	(l =((DES_LONG)(*((c)++)))<<24L, \
    100  1.1  thorpej 			 l|=((DES_LONG)(*((c)++)))<<16L, \
    101  1.1  thorpej 			 l|=((DES_LONG)(*((c)++)))<< 8L, \
    102  1.1  thorpej 			 l|=((DES_LONG)(*((c)++))))
    103  1.1  thorpej 
    104  1.1  thorpej #define l2n(l,c)	(*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
    105  1.1  thorpej 			 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
    106  1.1  thorpej 			 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
    107  1.1  thorpej 			 *((c)++)=(unsigned char)(((l)     )&0xff))
    108  1.1  thorpej 
    109  1.1  thorpej /* NOTE - c is not incremented as per l2c */
    110  1.1  thorpej #define l2cn(l1,l2,c,n)	{ \
    111  1.1  thorpej 			c+=n; \
    112  1.1  thorpej 			switch (n) { \
    113  1.5      mrg 			case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); /* FALLTHROUGH */\
    114  1.5      mrg 			case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); /* FALLTHROUGH */\
    115  1.5      mrg 			case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); /* FALLTHROUGH */\
    116  1.5      mrg 			case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); /* FALLTHROUGH */\
    117  1.5      mrg 			case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); /* FALLTHROUGH */\
    118  1.5      mrg 			case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); /* FALLTHROUGH */\
    119  1.5      mrg 			case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); /* FALLTHROUGH */\
    120  1.5      mrg 			case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); /* FALLTHROUGH */\
    121  1.1  thorpej 				} \
    122  1.1  thorpej 			}
    123  1.1  thorpej 
    124  1.1  thorpej #define	ROTATE(a,n)	(((a)>>(n))+((a)<<(32-(n))))
    125  1.1  thorpej 
    126  1.4      tls #define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
    127  1.4      tls #define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
    128  1.4      tls 	u=R^s[S  ]; \
    129  1.4      tls 	t=R^s[S+1]
    130  1.4      tls 
    131  1.1  thorpej /* The changes to this macro may help or hinder, depending on the
    132  1.6   andvar  * compiler and the architecture.  gcc2 always seems to do well :-).
    133  1.1  thorpej  * Inspired by Dana How <how (at) isl.stanford.edu>
    134  1.1  thorpej  * DO NOT use the alternative version on machines with 8 byte longs.
    135  1.1  thorpej  * It does not seem to work on the Alpha, even when DES_LONG is 4
    136  1.1  thorpej  * bytes, probably an issue of accessing non-word aligned objects :-( */
    137  1.1  thorpej #ifdef DES_PTR
    138  1.1  thorpej 
    139  1.4      tls /* It recently occurred to me that 0^0^0^0^0^0^0 == 0, so there
    140  1.4      tls  * is no reason to not xor all the sub items together.  This potentially
    141  1.4      tls  * saves a register since things can be xored directly into L */
    142  1.4      tls 
    143  1.4      tls #if defined(DES_RISC1) || defined(DES_RISC2)
    144  1.4      tls #ifdef DES_RISC1
    145  1.4      tls #define D_ENCRYPT(LL,R,S) { \
    146  1.4      tls         unsigned int u1,u2,u3; \
    147  1.4      tls         LOAD_DATA(R,S,u,t,E0,E1,u1); \
    148  1.4      tls         u2=(int)u>>8L; \
    149  1.4      tls         u1=(int)u&0xfc; \
    150  1.4      tls         u2&=0xfc; \
    151  1.4      tls         t=ROTATE(t,4); \
    152  1.4      tls         u>>=16L; \
    153  1.4      tls         LL^= *(const DES_LONG *)(des_SP      +u1); \
    154  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x200+u2); \
    155  1.4      tls         u3=(int)(u>>8L); \
    156  1.4      tls         u1=(int)u&0xfc; \
    157  1.4      tls         u3&=0xfc; \
    158  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x400+u1); \
    159  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x600+u3); \
    160  1.4      tls         u2=(int)t>>8L; \
    161  1.4      tls         u1=(int)t&0xfc; \
    162  1.4      tls         u2&=0xfc; \
    163  1.4      tls         t>>=16L; \
    164  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
    165  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
    166  1.4      tls         u3=(int)t>>8L; \
    167  1.4      tls         u1=(int)t&0xfc; \
    168  1.4      tls         u3&=0xfc; \
    169  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x500+u1); \
    170  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x700+u3); }
    171  1.4      tls #endif /* DES_RISC1 */
    172  1.4      tls #ifdef DES_RISC2
    173  1.4      tls #define D_ENCRYPT(LL,R,S) { \
    174  1.4      tls         unsigned int u1,u2,s1,s2; \
    175  1.4      tls         LOAD_DATA(R,S,u,t,E0,E1,u1); \
    176  1.4      tls         u2=(int)u>>8L; \
    177  1.4      tls         u1=(int)u&0xfc; \
    178  1.4      tls         u2&=0xfc; \
    179  1.4      tls         t=ROTATE(t,4); \
    180  1.4      tls         LL^= *(const DES_LONG *)(des_SP      +u1); \
    181  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x200+u2); \
    182  1.4      tls         s1=(int)(u>>16L); \
    183  1.4      tls         s2=(int)(u>>24L); \
    184  1.4      tls         s1&=0xfc; \
    185  1.4      tls         s2&=0xfc; \
    186  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x400+s1); \
    187  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x600+s2); \
    188  1.4      tls         u2=(int)t>>8L; \
    189  1.4      tls         u1=(int)t&0xfc; \
    190  1.4      tls         u2&=0xfc; \
    191  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
    192  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
    193  1.4      tls         s1=(int)(t>>16L); \
    194  1.4      tls         s2=(int)(t>>24L); \
    195  1.4      tls         s1&=0xfc; \
    196  1.4      tls         s2&=0xfc; \
    197  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x400+s1); \
    198  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x600+s2); \
    199  1.4      tls         u2=(int)t>>8L; \
    200  1.4      tls         u1=(int)t&0xfc; \
    201  1.4      tls         u2&=0xfc; \
    202  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
    203  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
    204  1.4      tls         s1=(int)(t>>16L); \
    205  1.4      tls         s2=(int)(t>>24L); \
    206  1.4      tls         s1&=0xfc; \
    207  1.4      tls         s2&=0xfc; \
    208  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x500+s1); \
    209  1.4      tls         LL^= *(const DES_LONG *)(des_SP+0x700+s2); }
    210  1.4      tls #endif /* DES_RISC2 */
    211  1.4      tls #else  /* DES_RISC1 || DES_RISC2 */
    212  1.4      tls #define D_ENCRYPT(LL,R,S) { \
    213  1.4      tls 	LOAD_DATA_tmp(R,S,u,t,E0,E1); \
    214  1.4      tls 	t=ROTATE(t,4); \
    215  1.4      tls 	LL^= \
    216  1.4      tls 	*(const DES_LONG *)(des_SP      +((u     )&0xfc))^ \
    217  1.4      tls 	*(const DES_LONG *)(des_SP+0x200+((u>> 8L)&0xfc))^ \
    218  1.4      tls 	*(const DES_LONG *)(des_SP+0x400+((u>>16L)&0xfc))^ \
    219  1.4      tls 	*(const DES_LONG *)(des_SP+0x600+((u>>24L)&0xfc))^ \
    220  1.4      tls 	*(const DES_LONG *)(des_SP+0x100+((t     )&0xfc))^ \
    221  1.4      tls 	*(const DES_LONG *)(des_SP+0x300+((t>> 8L)&0xfc))^ \
    222  1.4      tls 	*(const DES_LONG *)(des_SP+0x500+((t>>16L)&0xfc))^ \
    223  1.4      tls 	*(const DES_LONG *)(des_SP+0x700+((t>>24L)&0xfc)); }
    224  1.4      tls #endif /* DES_RISC1 || DES_RISC2 */
    225  1.1  thorpej #else /* original version */
    226  1.4      tls 
    227  1.4      tls #if defined(DES_RISC1) || defined(DES_RISC2)
    228  1.4      tls #ifdef DES_RISC1
    229  1.4      tls #define D_ENCRYPT(LL,R,S) {\
    230  1.4      tls 	unsigned int u1,u2,u3; \
    231  1.4      tls 	LOAD_DATA(R,S,u,t,E0,E1,u1); \
    232  1.4      tls 	u>>=2L; \
    233  1.4      tls 	t=ROTATE(t,6); \
    234  1.4      tls 	u2=(int)u>>8L; \
    235  1.4      tls 	u1=(int)u&0x3f; \
    236  1.4      tls 	u2&=0x3f; \
    237  1.4      tls 	u>>=16L; \
    238  1.4      tls 	LL^=des_SPtrans[0][u1]; \
    239  1.4      tls 	LL^=des_SPtrans[2][u2]; \
    240  1.4      tls 	u3=(int)u>>8L; \
    241  1.4      tls 	u1=(int)u&0x3f; \
    242  1.4      tls 	u3&=0x3f; \
    243  1.4      tls 	LL^=des_SPtrans[4][u1]; \
    244  1.4      tls 	LL^=des_SPtrans[6][u3]; \
    245  1.4      tls 	u2=(int)t>>8L; \
    246  1.4      tls 	u1=(int)t&0x3f; \
    247  1.4      tls 	u2&=0x3f; \
    248  1.4      tls 	t>>=16L; \
    249  1.4      tls 	LL^=des_SPtrans[1][u1]; \
    250  1.4      tls 	LL^=des_SPtrans[3][u2]; \
    251  1.4      tls 	u3=(int)t>>8L; \
    252  1.4      tls 	u1=(int)t&0x3f; \
    253  1.4      tls 	u3&=0x3f; \
    254  1.4      tls 	LL^=des_SPtrans[5][u1]; \
    255  1.4      tls 	LL^=des_SPtrans[7][u3]; }
    256  1.4      tls #endif /* DES_RISC1 */
    257  1.4      tls #ifdef DES_RISC2
    258  1.4      tls #define D_ENCRYPT(LL,R,S) {\
    259  1.4      tls 	unsigned int u1,u2,s1,s2; \
    260  1.4      tls 	LOAD_DATA(R,S,u,t,E0,E1,u1); \
    261  1.4      tls 	u>>=2L; \
    262  1.4      tls 	t=ROTATE(t,6); \
    263  1.4      tls 	u2=(int)u>>8L; \
    264  1.4      tls 	u1=(int)u&0x3f; \
    265  1.4      tls 	u2&=0x3f; \
    266  1.4      tls 	LL^=des_SPtrans[0][u1]; \
    267  1.4      tls 	LL^=des_SPtrans[2][u2]; \
    268  1.4      tls 	s1=(int)u>>16L; \
    269  1.4      tls 	s2=(int)u>>24L; \
    270  1.4      tls 	s1&=0x3f; \
    271  1.4      tls 	s2&=0x3f; \
    272  1.4      tls 	LL^=des_SPtrans[4][s1]; \
    273  1.4      tls 	LL^=des_SPtrans[6][s2]; \
    274  1.4      tls 	u2=(int)t>>8L; \
    275  1.4      tls 	u1=(int)t&0x3f; \
    276  1.4      tls 	u2&=0x3f; \
    277  1.4      tls 	LL^=des_SPtrans[1][u1]; \
    278  1.4      tls 	LL^=des_SPtrans[3][u2]; \
    279  1.4      tls 	s1=(int)t>>16; \
    280  1.4      tls 	s2=(int)t>>24L; \
    281  1.4      tls 	s1&=0x3f; \
    282  1.4      tls 	s2&=0x3f; \
    283  1.4      tls 	LL^=des_SPtrans[5][s1]; \
    284  1.4      tls 	LL^=des_SPtrans[7][s2]; }
    285  1.4      tls #endif /* DES_RISC2 */
    286  1.4      tls 
    287  1.4      tls #else /* DES_RISC1 || DES_RISC2 */
    288  1.4      tls 
    289  1.4      tls #define D_ENCRYPT(LL,R,S) {\
    290  1.4      tls 	LOAD_DATA_tmp(R,S,u,t,E0,E1); \
    291  1.1  thorpej 	t=ROTATE(t,4); \
    292  1.4      tls 	LL^=\
    293  1.4      tls         	des_SPtrans[0][(u>> 2L)&0x3f]^ \
    294  1.4      tls         	des_SPtrans[2][(u>>10L)&0x3f]^ \
    295  1.4      tls         	des_SPtrans[4][(u>>18L)&0x3f]^ \
    296  1.4      tls         	des_SPtrans[6][(u>>26L)&0x3f]^ \
    297  1.4      tls         	des_SPtrans[1][(t>> 2L)&0x3f]^ \
    298  1.4      tls         	des_SPtrans[3][(t>>10L)&0x3f]^ \
    299  1.4      tls         	des_SPtrans[5][(t>>18L)&0x3f]^ \
    300  1.4      tls         	des_SPtrans[7][(t>>26L)&0x3f]; }
    301  1.4      tls #endif /* DES_RISC1 || DES_RISC2 */
    302  1.4      tls #endif /* DES_PTR */
    303  1.1  thorpej 
    304  1.1  thorpej 	/* IP and FP
    305  1.1  thorpej 	 * The problem is more of a geometric problem that random bit fiddling.
    306  1.1  thorpej 	 0  1  2  3  4  5  6  7      62 54 46 38 30 22 14  6
    307  1.1  thorpej 	 8  9 10 11 12 13 14 15      60 52 44 36 28 20 12  4
    308  1.1  thorpej 	16 17 18 19 20 21 22 23      58 50 42 34 26 18 10  2
    309  1.1  thorpej 	24 25 26 27 28 29 30 31  to  56 48 40 32 24 16  8  0
    310  1.1  thorpej 
    311  1.1  thorpej 	32 33 34 35 36 37 38 39      63 55 47 39 31 23 15  7
    312  1.1  thorpej 	40 41 42 43 44 45 46 47      61 53 45 37 29 21 13  5
    313  1.1  thorpej 	48 49 50 51 52 53 54 55      59 51 43 35 27 19 11  3
    314  1.1  thorpej 	56 57 58 59 60 61 62 63      57 49 41 33 25 17  9  1
    315  1.1  thorpej 
    316  1.1  thorpej 	The output has been subject to swaps of the form
    317  1.1  thorpej 	0 1 -> 3 1 but the odd and even bits have been put into
    318  1.1  thorpej 	2 3    2 0
    319  1.1  thorpej 	different words.  The main trick is to remember that
    320  1.1  thorpej 	t=((l>>size)^r)&(mask);
    321  1.1  thorpej 	r^=t;
    322  1.1  thorpej 	l^=(t<<size);
    323  1.1  thorpej 	can be used to swap and move bits between words.
    324  1.1  thorpej 
    325  1.1  thorpej 	So l =  0  1  2  3  r = 16 17 18 19
    326  1.1  thorpej 	        4  5  6  7      20 21 22 23
    327  1.1  thorpej 	        8  9 10 11      24 25 26 27
    328  1.1  thorpej 	       12 13 14 15      28 29 30 31
    329  1.1  thorpej 	becomes (for size == 2 and mask == 0x3333)
    330  1.1  thorpej 	   t =   2^16  3^17 -- --   l =  0  1 16 17  r =  2  3 18 19
    331  1.1  thorpej 		 6^20  7^21 -- --        4  5 20 21       6  7 22 23
    332  1.1  thorpej 		10^24 11^25 -- --        8  9 24 25      10 11 24 25
    333  1.1  thorpej 		14^28 15^29 -- --       12 13 28 29      14 15 28 29
    334  1.1  thorpej 
    335  1.1  thorpej 	Thanks for hints from Richard Outerbridge - he told me IP&FP
    336  1.1  thorpej 	could be done in 15 xor, 10 shifts and 5 ands.
    337  1.1  thorpej 	When I finally started to think of the problem in 2D
    338  1.1  thorpej 	I first got ~42 operations without xors.  When I remembered
    339  1.1  thorpej 	how to use xors :-) I got it to its final state.
    340  1.1  thorpej 	*/
    341  1.1  thorpej #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
    342  1.1  thorpej 	(b)^=(t),\
    343  1.1  thorpej 	(a)^=((t)<<(n)))
    344  1.1  thorpej 
    345  1.1  thorpej #define IP(l,r) \
    346  1.1  thorpej 	{ \
    347  1.1  thorpej 	register DES_LONG tt; \
    348  1.1  thorpej 	PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
    349  1.1  thorpej 	PERM_OP(l,r,tt,16,0x0000ffffL); \
    350  1.1  thorpej 	PERM_OP(r,l,tt, 2,0x33333333L); \
    351  1.1  thorpej 	PERM_OP(l,r,tt, 8,0x00ff00ffL); \
    352  1.1  thorpej 	PERM_OP(r,l,tt, 1,0x55555555L); \
    353  1.1  thorpej 	}
    354  1.1  thorpej 
    355  1.1  thorpej #define FP(l,r) \
    356  1.1  thorpej 	{ \
    357  1.1  thorpej 	register DES_LONG tt; \
    358  1.1  thorpej 	PERM_OP(l,r,tt, 1,0x55555555L); \
    359  1.1  thorpej 	PERM_OP(r,l,tt, 8,0x00ff00ffL); \
    360  1.1  thorpej 	PERM_OP(l,r,tt, 2,0x33333333L); \
    361  1.1  thorpej 	PERM_OP(r,l,tt,16,0x0000ffffL); \
    362  1.1  thorpej 	PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
    363  1.1  thorpej 	}
    364  1.1  thorpej #endif
    365