7ed783cb37647bba7795d0401e60e125012183f0
[openssl.git] / crypto / des / des_locl.h
1 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57
58 #ifndef HEADER_DES_LOCL_H
59 # define HEADER_DES_LOCL_H
60
61 # include <openssl/e_os2.h>
62
63 # if defined(OPENSSL_SYS_WIN32)
64 #  ifndef OPENSSL_SYS_MSDOS
65 #   define OPENSSL_SYS_MSDOS
66 #  endif
67 # endif
68
69 # include <stdio.h>
70 # include <stdlib.h>
71
72 # ifndef OPENSSL_SYS_MSDOS
73 #  if !defined(OPENSSL_SYS_VMS) || defined(__DECC)
74 #   ifdef OPENSSL_UNISTD
75 #    include OPENSSL_UNISTD
76 #   else
77 #    include <unistd.h>
78 #   endif
79 #   include <math.h>
80 #  endif
81 # endif
82 # include <openssl/des.h>
83
84 # ifdef OPENSSL_SYS_MSDOS       /* Visual C++ 2.1 (Windows NT/95) */
85 #  include <stdlib.h>
86 #  include <errno.h>
87 #  include <time.h>
88 #  include <io.h>
89 # endif
90
91 # if defined(__STDC__) || defined(OPENSSL_SYS_VMS) || defined(M_XENIX) || defined(OPENSSL_SYS_MSDOS)
92 #  include <string.h>
93 # endif
94
95 # ifdef OPENSSL_BUILD_SHLIBCRYPTO
96 #  undef OPENSSL_EXTERN
97 #  define OPENSSL_EXTERN OPENSSL_EXPORT
98 # endif
99
100 # define ITERATIONS 16
101 # define HALF_ITERATIONS 8
102
103 /* used in des_read and des_write */
104 # define MAXWRITE        (1024*16)
105 # define BSIZE           (MAXWRITE+4)
106
107 # define c2l(c,l)        (l =((DES_LONG)(*((c)++)))    , \
108                          l|=((DES_LONG)(*((c)++)))<< 8L, \
109                          l|=((DES_LONG)(*((c)++)))<<16L, \
110                          l|=((DES_LONG)(*((c)++)))<<24L)
111
112 /* NOTE - c is not incremented as per c2l */
113 # define c2ln(c,l1,l2,n) { \
114                         c+=n; \
115                         l1=l2=0; \
116                         switch (n) { \
117                         case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
118                         case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
119                         case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
120                         case 5: l2|=((DES_LONG)(*(--(c))));     \
121                         case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
122                         case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
123                         case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
124                         case 1: l1|=((DES_LONG)(*(--(c))));     \
125                                 } \
126                         }
127
128 # define l2c(l,c)        (*((c)++)=(unsigned char)(((l)     )&0xff), \
129                          *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
130                          *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
131                          *((c)++)=(unsigned char)(((l)>>24L)&0xff))
132
133 /*
134  * replacements for htonl and ntohl since I have no idea what to do when
135  * faced with machines with 8 byte longs.
136  */
137 # define HDRSIZE 4
138
139 # define n2l(c,l)        (l =((DES_LONG)(*((c)++)))<<24L, \
140                          l|=((DES_LONG)(*((c)++)))<<16L, \
141                          l|=((DES_LONG)(*((c)++)))<< 8L, \
142                          l|=((DES_LONG)(*((c)++))))
143
144 # define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
145                          *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
146                          *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
147                          *((c)++)=(unsigned char)(((l)     )&0xff))
148
149 /* NOTE - c is not incremented as per l2c */
150 # define l2cn(l1,l2,c,n) { \
151                         c+=n; \
152                         switch (n) { \
153                         case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
154                         case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
155                         case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
156                         case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); \
157                         case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
158                         case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
159                         case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
160                         case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); \
161                                 } \
162                         }
163
164 # if (defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER))
165 #  define ROTATE(a,n)     (_lrotr(a,n))
166 # elif defined(__ICC)
167 #  define ROTATE(a,n)     (_rotr(a,n))
168 # elif defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
169 #  if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
170 #   define ROTATE(a,n)   ({ register unsigned int ret;   \
171                                 asm ("rorl %1,%0"       \
172                                         : "=r"(ret)     \
173                                         : "I"(n),"0"(a) \
174                                         : "cc");        \
175                            ret;                         \
176                         })
177 #  endif
178 # endif
179 # ifndef ROTATE
180 #  define ROTATE(a,n)     (((a)>>(n))+((a)<<(32-(n))))
181 # endif
182
183 /*
184  * Don't worry about the LOAD_DATA() stuff, that is used by fcrypt() to add
185  * it's little bit to the front
186  */
187
188 # ifdef DES_FCRYPT
189
190 #  define LOAD_DATA_tmp(R,S,u,t,E0,E1) \
191         { DES_LONG tmp; LOAD_DATA(R,S,u,t,E0,E1,tmp); }
192
193 #  define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
194         t=R^(R>>16L); \
195         u=t&E0; t&=E1; \
196         tmp=(u<<16); u^=R^s[S  ]; u^=tmp; \
197         tmp=(t<<16); t^=R^s[S+1]; t^=tmp
198 # else
199 #  define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
200 #  define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
201         u=R^s[S  ]; \
202         t=R^s[S+1]
203 # endif
204
205 /*
206  * It recently occurred to me that 0^0^0^0^0^0^0 == 0, so there is no reason
207  * to not xor all the sub items together.  This potentially saves a register
208  * since things can be xored directly into L
209  */
210
211 # define D_ENCRYPT(LL,R,S) { \
212         LOAD_DATA_tmp(R,S,u,t,E0,E1); \
213         t=ROTATE(t,4); \
214         LL^= \
215             DES_SPtrans[0][(u>> 2L)&0x3f]^ \
216             DES_SPtrans[2][(u>>10L)&0x3f]^ \
217             DES_SPtrans[4][(u>>18L)&0x3f]^ \
218             DES_SPtrans[6][(u>>26L)&0x3f]^ \
219             DES_SPtrans[1][(t>> 2L)&0x3f]^ \
220             DES_SPtrans[3][(t>>10L)&0x3f]^ \
221             DES_SPtrans[5][(t>>18L)&0x3f]^ \
222             DES_SPtrans[7][(t>>26L)&0x3f]; }
223
224         /*-
225          * IP and FP
226          * The problem is more of a geometric problem that random bit fiddling.
227          0  1  2  3  4  5  6  7      62 54 46 38 30 22 14  6
228          8  9 10 11 12 13 14 15      60 52 44 36 28 20 12  4
229         16 17 18 19 20 21 22 23      58 50 42 34 26 18 10  2
230         24 25 26 27 28 29 30 31  to  56 48 40 32 24 16  8  0
231
232         32 33 34 35 36 37 38 39      63 55 47 39 31 23 15  7
233         40 41 42 43 44 45 46 47      61 53 45 37 29 21 13  5
234         48 49 50 51 52 53 54 55      59 51 43 35 27 19 11  3
235         56 57 58 59 60 61 62 63      57 49 41 33 25 17  9  1
236
237         The output has been subject to swaps of the form
238         0 1 -> 3 1 but the odd and even bits have been put into
239         2 3    2 0
240         different words.  The main trick is to remember that
241         t=((l>>size)^r)&(mask);
242         r^=t;
243         l^=(t<<size);
244         can be used to swap and move bits between words.
245
246         So l =  0  1  2  3  r = 16 17 18 19
247                 4  5  6  7      20 21 22 23
248                 8  9 10 11      24 25 26 27
249                12 13 14 15      28 29 30 31
250         becomes (for size == 2 and mask == 0x3333)
251            t =   2^16  3^17 -- --   l =  0  1 16 17  r =  2  3 18 19
252                  6^20  7^21 -- --        4  5 20 21       6  7 22 23
253                 10^24 11^25 -- --        8  9 24 25      10 11 24 25
254                 14^28 15^29 -- --       12 13 28 29      14 15 28 29
255
256         Thanks for hints from Richard Outerbridge - he told me IP&FP
257         could be done in 15 xor, 10 shifts and 5 ands.
258         When I finally started to think of the problem in 2D
259         I first got ~42 operations without xors.  When I remembered
260         how to use xors :-) I got it to its final state.
261         */
262 # define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
263         (b)^=(t),\
264         (a)^=((t)<<(n)))
265
266 # define IP(l,r) \
267         { \
268         register DES_LONG tt; \
269         PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
270         PERM_OP(l,r,tt,16,0x0000ffffL); \
271         PERM_OP(r,l,tt, 2,0x33333333L); \
272         PERM_OP(l,r,tt, 8,0x00ff00ffL); \
273         PERM_OP(r,l,tt, 1,0x55555555L); \
274         }
275
276 # define FP(l,r) \
277         { \
278         register DES_LONG tt; \
279         PERM_OP(l,r,tt, 1,0x55555555L); \
280         PERM_OP(r,l,tt, 8,0x00ff00ffL); \
281         PERM_OP(l,r,tt, 2,0x33333333L); \
282         PERM_OP(r,l,tt,16,0x0000ffffL); \
283         PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
284         }
285
286 extern const DES_LONG DES_SPtrans[8][64];
287
288 void fcrypt_body(DES_LONG *out, DES_key_schedule *ks,
289                  DES_LONG Eswap0, DES_LONG Eswap1);
290
291 #endif