155714Skris/* crypto/des/des_locl.h */
255714Skris/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280304Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280304Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280304Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280304Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280304Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#ifndef HEADER_DES_LOCL_H
60280304Sjkim# define HEADER_DES_LOCL_H
6155714Skris
62280304Sjkim# include <openssl/e_os2.h>
63109998Smarkm
64280304Sjkim# if defined(OPENSSL_SYS_WIN32)
65280304Sjkim#  ifndef OPENSSL_SYS_MSDOS
66280304Sjkim#   define OPENSSL_SYS_MSDOS
67280304Sjkim#  endif
68280304Sjkim# endif
6955714Skris
70280304Sjkim# include <stdio.h>
71280304Sjkim# include <stdlib.h>
7255714Skris
73280304Sjkim# ifndef OPENSSL_SYS_MSDOS
74280304Sjkim#  if !defined(OPENSSL_SYS_VMS) || defined(__DECC)
75280304Sjkim#   ifdef OPENSSL_UNISTD
76280304Sjkim#    include OPENSSL_UNISTD
77280304Sjkim#   else
78280304Sjkim#    include <unistd.h>
79280304Sjkim#   endif
80280304Sjkim#   include <math.h>
81280304Sjkim#  endif
82280304Sjkim# endif
83280304Sjkim# include <openssl/des.h>
8455714Skris
85280304Sjkim# ifdef OPENSSL_SYS_MSDOS       /* Visual C++ 2.1 (Windows NT/95) */
86280304Sjkim#  include <stdlib.h>
87280304Sjkim#  include <errno.h>
88280304Sjkim#  include <time.h>
89280304Sjkim#  include <io.h>
90280304Sjkim# endif
9155714Skris
92280304Sjkim# if defined(__STDC__) || defined(OPENSSL_SYS_VMS) || defined(M_XENIX) || defined(OPENSSL_SYS_MSDOS)
93280304Sjkim#  include <string.h>
94280304Sjkim# endif
9555714Skris
96280304Sjkim# ifdef OPENSSL_BUILD_SHLIBCRYPTO
97280304Sjkim#  undef OPENSSL_EXTERN
98280304Sjkim#  define OPENSSL_EXTERN OPENSSL_EXPORT
99280304Sjkim# endif
100109998Smarkm
101280304Sjkim# define ITERATIONS 16
102280304Sjkim# define HALF_ITERATIONS 8
10355714Skris
10455714Skris/* used in des_read and des_write */
105280304Sjkim# define MAXWRITE        (1024*16)
106280304Sjkim# define BSIZE           (MAXWRITE+4)
10755714Skris
108280304Sjkim# define c2l(c,l)        (l =((DES_LONG)(*((c)++)))    , \
109280304Sjkim                         l|=((DES_LONG)(*((c)++)))<< 8L, \
110280304Sjkim                         l|=((DES_LONG)(*((c)++)))<<16L, \
111280304Sjkim                         l|=((DES_LONG)(*((c)++)))<<24L)
11255714Skris
11355714Skris/* NOTE - c is not incremented as per c2l */
114280304Sjkim# define c2ln(c,l1,l2,n) { \
115280304Sjkim                        c+=n; \
116280304Sjkim                        l1=l2=0; \
117280304Sjkim                        switch (n) { \
118280304Sjkim                        case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
119280304Sjkim                        case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
120280304Sjkim                        case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
121280304Sjkim                        case 5: l2|=((DES_LONG)(*(--(c))));     \
122280304Sjkim                        case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
123280304Sjkim                        case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
124280304Sjkim                        case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
125280304Sjkim                        case 1: l1|=((DES_LONG)(*(--(c))));     \
126280304Sjkim                                } \
127280304Sjkim                        }
12855714Skris
129280304Sjkim# define l2c(l,c)        (*((c)++)=(unsigned char)(((l)     )&0xff), \
130280304Sjkim                         *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
131280304Sjkim                         *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
132280304Sjkim                         *((c)++)=(unsigned char)(((l)>>24L)&0xff))
13355714Skris
134280304Sjkim/*
135280304Sjkim * replacements for htonl and ntohl since I have no idea what to do when
136280304Sjkim * faced with machines with 8 byte longs.
137280304Sjkim */
138280304Sjkim# define HDRSIZE 4
13955714Skris
140280304Sjkim# define n2l(c,l)        (l =((DES_LONG)(*((c)++)))<<24L, \
141280304Sjkim                         l|=((DES_LONG)(*((c)++)))<<16L, \
142280304Sjkim                         l|=((DES_LONG)(*((c)++)))<< 8L, \
143280304Sjkim                         l|=((DES_LONG)(*((c)++))))
14455714Skris
145280304Sjkim# define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
146280304Sjkim                         *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
147280304Sjkim                         *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
148280304Sjkim                         *((c)++)=(unsigned char)(((l)     )&0xff))
14955714Skris
15055714Skris/* NOTE - c is not incremented as per l2c */
151280304Sjkim# define l2cn(l1,l2,c,n) { \
152280304Sjkim                        c+=n; \
153280304Sjkim                        switch (n) { \
154280304Sjkim                        case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
155280304Sjkim                        case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
156280304Sjkim                        case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
157280304Sjkim                        case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); \
158280304Sjkim                        case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
159280304Sjkim                        case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
160280304Sjkim                        case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
161280304Sjkim                        case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); \
162280304Sjkim                                } \
163280304Sjkim                        }
16455714Skris
165280304Sjkim# if (defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER)) || defined(__ICC)
166280304Sjkim#  define ROTATE(a,n)     (_lrotr(a,n))
167280304Sjkim# elif defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
168280304Sjkim#  if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
169280304Sjkim#   define ROTATE(a,n)   ({ register unsigned int ret;   \
170280304Sjkim                                asm ("rorl %1,%0"       \
171280304Sjkim                                        : "=r"(ret)     \
172280304Sjkim                                        : "I"(n),"0"(a) \
173280304Sjkim                                        : "cc");        \
174280304Sjkim                           ret;                         \
175280304Sjkim                        })
176280304Sjkim#  endif
177109998Smarkm# endif
178280304Sjkim# ifndef ROTATE
179280304Sjkim#  define ROTATE(a,n)     (((a)>>(n))+((a)<<(32-(n))))
180280304Sjkim# endif
18155714Skris
182280304Sjkim/*
183280304Sjkim * Don't worry about the LOAD_DATA() stuff, that is used by fcrypt() to add
184280304Sjkim * it's little bit to the front
185280304Sjkim */
18655714Skris
187280304Sjkim# ifdef DES_FCRYPT
18855714Skris
189280304Sjkim#  define LOAD_DATA_tmp(R,S,u,t,E0,E1) \
190280304Sjkim        { DES_LONG tmp; LOAD_DATA(R,S,u,t,E0,E1,tmp); }
19155714Skris
192280304Sjkim#  define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
193280304Sjkim        t=R^(R>>16L); \
194280304Sjkim        u=t&E0; t&=E1; \
195280304Sjkim        tmp=(u<<16); u^=R^s[S  ]; u^=tmp; \
196280304Sjkim        tmp=(t<<16); t^=R^s[S+1]; t^=tmp
197280304Sjkim# else
198280304Sjkim#  define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
199280304Sjkim#  define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
200280304Sjkim        u=R^s[S  ]; \
201280304Sjkim        t=R^s[S+1]
202280304Sjkim# endif
20355714Skris
204280304Sjkim/*
205280304Sjkim * The changes to this macro may help or hinder, depending on the compiler
206280304Sjkim * and the architecture.  gcc2 always seems to do well :-). Inspired by Dana
207280304Sjkim * How <how@isl.stanford.edu> DO NOT use the alternative version on machines
208280304Sjkim * with 8 byte longs. It does not seem to work on the Alpha, even when
209280304Sjkim * DES_LONG is 4 bytes, probably an issue of accessing non-word aligned
210280304Sjkim * objects :-(
211280304Sjkim */
212280304Sjkim# ifdef DES_PTR
21355714Skris
214280304Sjkim/*
215280304Sjkim * It recently occurred to me that 0^0^0^0^0^0^0 == 0, so there is no reason
216280304Sjkim * to not xor all the sub items together.  This potentially saves a register
217280304Sjkim * since things can be xored directly into L
218280304Sjkim */
21955714Skris
220280304Sjkim#  if defined(DES_RISC1) || defined(DES_RISC2)
221280304Sjkim#   ifdef DES_RISC1
222280304Sjkim#    define D_ENCRYPT(LL,R,S) { \
223280304Sjkim        unsigned int u1,u2,u3; \
224280304Sjkim        LOAD_DATA(R,S,u,t,E0,E1,u1); \
225280304Sjkim        u2=(int)u>>8L; \
226280304Sjkim        u1=(int)u&0xfc; \
227280304Sjkim        u2&=0xfc; \
228280304Sjkim        t=ROTATE(t,4); \
229280304Sjkim        u>>=16L; \
230280304Sjkim        LL^= *(const DES_LONG *)(des_SP      +u1); \
231280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x200+u2); \
232280304Sjkim        u3=(int)(u>>8L); \
233280304Sjkim        u1=(int)u&0xfc; \
234280304Sjkim        u3&=0xfc; \
235280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x400+u1); \
236280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x600+u3); \
237280304Sjkim        u2=(int)t>>8L; \
238280304Sjkim        u1=(int)t&0xfc; \
239280304Sjkim        u2&=0xfc; \
240280304Sjkim        t>>=16L; \
241280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
242280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
243280304Sjkim        u3=(int)t>>8L; \
244280304Sjkim        u1=(int)t&0xfc; \
245280304Sjkim        u3&=0xfc; \
246280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x500+u1); \
247280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x700+u3); }
248280304Sjkim#   endif
249280304Sjkim#   ifdef DES_RISC2
250280304Sjkim#    define D_ENCRYPT(LL,R,S) { \
251280304Sjkim        unsigned int u1,u2,s1,s2; \
252280304Sjkim        LOAD_DATA(R,S,u,t,E0,E1,u1); \
253280304Sjkim        u2=(int)u>>8L; \
254280304Sjkim        u1=(int)u&0xfc; \
255280304Sjkim        u2&=0xfc; \
256280304Sjkim        t=ROTATE(t,4); \
257280304Sjkim        LL^= *(const DES_LONG *)(des_SP      +u1); \
258280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x200+u2); \
259280304Sjkim        s1=(int)(u>>16L); \
260280304Sjkim        s2=(int)(u>>24L); \
261280304Sjkim        s1&=0xfc; \
262280304Sjkim        s2&=0xfc; \
263280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x400+s1); \
264280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x600+s2); \
265280304Sjkim        u2=(int)t>>8L; \
266280304Sjkim        u1=(int)t&0xfc; \
267280304Sjkim        u2&=0xfc; \
268280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
269280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
270280304Sjkim        s1=(int)(t>>16L); \
271280304Sjkim        s2=(int)(t>>24L); \
272280304Sjkim        s1&=0xfc; \
273280304Sjkim        s2&=0xfc; \
274280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x500+s1); \
275280304Sjkim        LL^= *(const DES_LONG *)(des_SP+0x700+s2); }
276280304Sjkim#   endif
277280304Sjkim#  else
278280304Sjkim#   define D_ENCRYPT(LL,R,S) { \
279280304Sjkim        LOAD_DATA_tmp(R,S,u,t,E0,E1); \
280280304Sjkim        t=ROTATE(t,4); \
281280304Sjkim        LL^= \
282280304Sjkim        *(const DES_LONG *)(des_SP      +((u     )&0xfc))^ \
283280304Sjkim        *(const DES_LONG *)(des_SP+0x200+((u>> 8L)&0xfc))^ \
284280304Sjkim        *(const DES_LONG *)(des_SP+0x400+((u>>16L)&0xfc))^ \
285280304Sjkim        *(const DES_LONG *)(des_SP+0x600+((u>>24L)&0xfc))^ \
286280304Sjkim        *(const DES_LONG *)(des_SP+0x100+((t     )&0xfc))^ \
287280304Sjkim        *(const DES_LONG *)(des_SP+0x300+((t>> 8L)&0xfc))^ \
288280304Sjkim        *(const DES_LONG *)(des_SP+0x500+((t>>16L)&0xfc))^ \
289280304Sjkim        *(const DES_LONG *)(des_SP+0x700+((t>>24L)&0xfc)); }
290280304Sjkim#  endif
29155714Skris
292280304Sjkim# else                          /* original version */
29355714Skris
294280304Sjkim#  if defined(DES_RISC1) || defined(DES_RISC2)
295280304Sjkim#   ifdef DES_RISC1
296280304Sjkim#    define D_ENCRYPT(LL,R,S) {\
297280304Sjkim        unsigned int u1,u2,u3; \
298280304Sjkim        LOAD_DATA(R,S,u,t,E0,E1,u1); \
299280304Sjkim        u>>=2L; \
300280304Sjkim        t=ROTATE(t,6); \
301280304Sjkim        u2=(int)u>>8L; \
302280304Sjkim        u1=(int)u&0x3f; \
303280304Sjkim        u2&=0x3f; \
304280304Sjkim        u>>=16L; \
305280304Sjkim        LL^=DES_SPtrans[0][u1]; \
306280304Sjkim        LL^=DES_SPtrans[2][u2]; \
307280304Sjkim        u3=(int)u>>8L; \
308280304Sjkim        u1=(int)u&0x3f; \
309280304Sjkim        u3&=0x3f; \
310280304Sjkim        LL^=DES_SPtrans[4][u1]; \
311280304Sjkim        LL^=DES_SPtrans[6][u3]; \
312280304Sjkim        u2=(int)t>>8L; \
313280304Sjkim        u1=(int)t&0x3f; \
314280304Sjkim        u2&=0x3f; \
315280304Sjkim        t>>=16L; \
316280304Sjkim        LL^=DES_SPtrans[1][u1]; \
317280304Sjkim        LL^=DES_SPtrans[3][u2]; \
318280304Sjkim        u3=(int)t>>8L; \
319280304Sjkim        u1=(int)t&0x3f; \
320280304Sjkim        u3&=0x3f; \
321280304Sjkim        LL^=DES_SPtrans[5][u1]; \
322280304Sjkim        LL^=DES_SPtrans[7][u3]; }
323280304Sjkim#   endif
324280304Sjkim#   ifdef DES_RISC2
325280304Sjkim#    define D_ENCRYPT(LL,R,S) {\
326280304Sjkim        unsigned int u1,u2,s1,s2; \
327280304Sjkim        LOAD_DATA(R,S,u,t,E0,E1,u1); \
328280304Sjkim        u>>=2L; \
329280304Sjkim        t=ROTATE(t,6); \
330280304Sjkim        u2=(int)u>>8L; \
331280304Sjkim        u1=(int)u&0x3f; \
332280304Sjkim        u2&=0x3f; \
333280304Sjkim        LL^=DES_SPtrans[0][u1]; \
334280304Sjkim        LL^=DES_SPtrans[2][u2]; \
335280304Sjkim        s1=(int)u>>16L; \
336280304Sjkim        s2=(int)u>>24L; \
337280304Sjkim        s1&=0x3f; \
338280304Sjkim        s2&=0x3f; \
339280304Sjkim        LL^=DES_SPtrans[4][s1]; \
340280304Sjkim        LL^=DES_SPtrans[6][s2]; \
341280304Sjkim        u2=(int)t>>8L; \
342280304Sjkim        u1=(int)t&0x3f; \
343280304Sjkim        u2&=0x3f; \
344280304Sjkim        LL^=DES_SPtrans[1][u1]; \
345280304Sjkim        LL^=DES_SPtrans[3][u2]; \
346280304Sjkim        s1=(int)t>>16; \
347280304Sjkim        s2=(int)t>>24L; \
348280304Sjkim        s1&=0x3f; \
349280304Sjkim        s2&=0x3f; \
350280304Sjkim        LL^=DES_SPtrans[5][s1]; \
351280304Sjkim        LL^=DES_SPtrans[7][s2]; }
352280304Sjkim#   endif
35355714Skris
354280304Sjkim#  else
35555714Skris
356280304Sjkim#   define D_ENCRYPT(LL,R,S) {\
357280304Sjkim        LOAD_DATA_tmp(R,S,u,t,E0,E1); \
358280304Sjkim        t=ROTATE(t,4); \
359280304Sjkim        LL^=\
360280304Sjkim                DES_SPtrans[0][(u>> 2L)&0x3f]^ \
361280304Sjkim                DES_SPtrans[2][(u>>10L)&0x3f]^ \
362280304Sjkim                DES_SPtrans[4][(u>>18L)&0x3f]^ \
363280304Sjkim                DES_SPtrans[6][(u>>26L)&0x3f]^ \
364280304Sjkim                DES_SPtrans[1][(t>> 2L)&0x3f]^ \
365280304Sjkim                DES_SPtrans[3][(t>>10L)&0x3f]^ \
366280304Sjkim                DES_SPtrans[5][(t>>18L)&0x3f]^ \
367280304Sjkim                DES_SPtrans[7][(t>>26L)&0x3f]; }
368280304Sjkim#  endif
369280304Sjkim# endif
37055714Skris
371280304Sjkim        /*-
372280304Sjkim         * IP and FP
373280304Sjkim         * The problem is more of a geometric problem that random bit fiddling.
374280304Sjkim         0  1  2  3  4  5  6  7      62 54 46 38 30 22 14  6
375280304Sjkim         8  9 10 11 12 13 14 15      60 52 44 36 28 20 12  4
376280304Sjkim        16 17 18 19 20 21 22 23      58 50 42 34 26 18 10  2
377280304Sjkim        24 25 26 27 28 29 30 31  to  56 48 40 32 24 16  8  0
37855714Skris
379280304Sjkim        32 33 34 35 36 37 38 39      63 55 47 39 31 23 15  7
380280304Sjkim        40 41 42 43 44 45 46 47      61 53 45 37 29 21 13  5
381280304Sjkim        48 49 50 51 52 53 54 55      59 51 43 35 27 19 11  3
382280304Sjkim        56 57 58 59 60 61 62 63      57 49 41 33 25 17  9  1
38355714Skris
384280304Sjkim        The output has been subject to swaps of the form
385280304Sjkim        0 1 -> 3 1 but the odd and even bits have been put into
386280304Sjkim        2 3    2 0
387280304Sjkim        different words.  The main trick is to remember that
388280304Sjkim        t=((l>>size)^r)&(mask);
389280304Sjkim        r^=t;
390280304Sjkim        l^=(t<<size);
391280304Sjkim        can be used to swap and move bits between words.
39255714Skris
393280304Sjkim        So l =  0  1  2  3  r = 16 17 18 19
394280304Sjkim                4  5  6  7      20 21 22 23
395280304Sjkim                8  9 10 11      24 25 26 27
396280304Sjkim               12 13 14 15      28 29 30 31
397280304Sjkim        becomes (for size == 2 and mask == 0x3333)
398280304Sjkim           t =   2^16  3^17 -- --   l =  0  1 16 17  r =  2  3 18 19
399280304Sjkim                 6^20  7^21 -- --        4  5 20 21       6  7 22 23
400280304Sjkim                10^24 11^25 -- --        8  9 24 25      10 11 24 25
401280304Sjkim                14^28 15^29 -- --       12 13 28 29      14 15 28 29
40255714Skris
403280304Sjkim        Thanks for hints from Richard Outerbridge - he told me IP&FP
404280304Sjkim        could be done in 15 xor, 10 shifts and 5 ands.
405280304Sjkim        When I finally started to think of the problem in 2D
406280304Sjkim        I first got ~42 operations without xors.  When I remembered
407280304Sjkim        how to use xors :-) I got it to its final state.
408280304Sjkim        */
409280304Sjkim# define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
410280304Sjkim        (b)^=(t),\
411280304Sjkim        (a)^=((t)<<(n)))
41255714Skris
413280304Sjkim# define IP(l,r) \
414280304Sjkim        { \
415280304Sjkim        register DES_LONG tt; \
416280304Sjkim        PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
417280304Sjkim        PERM_OP(l,r,tt,16,0x0000ffffL); \
418280304Sjkim        PERM_OP(r,l,tt, 2,0x33333333L); \
419280304Sjkim        PERM_OP(l,r,tt, 8,0x00ff00ffL); \
420280304Sjkim        PERM_OP(r,l,tt, 1,0x55555555L); \
421280304Sjkim        }
42255714Skris
423280304Sjkim# define FP(l,r) \
424280304Sjkim        { \
425280304Sjkim        register DES_LONG tt; \
426280304Sjkim        PERM_OP(l,r,tt, 1,0x55555555L); \
427280304Sjkim        PERM_OP(r,l,tt, 8,0x00ff00ffL); \
428280304Sjkim        PERM_OP(l,r,tt, 2,0x33333333L); \
429280304Sjkim        PERM_OP(r,l,tt,16,0x0000ffffL); \
430280304Sjkim        PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
431280304Sjkim        }
43255714Skris
433160814Ssimonextern const DES_LONG DES_SPtrans[8][64];
43455714Skris
435280304Sjkimvoid fcrypt_body(DES_LONG *out, DES_key_schedule *ks,
436280304Sjkim                 DES_LONG Eswap0, DES_LONG Eswap1);
437238405Sjkim
438280304Sjkim# ifdef OPENSSL_SMALL_FOOTPRINT
439280304Sjkim#  undef DES_UNROLL
440280304Sjkim# endif
44155714Skris#endif
442