POK
des_locl.h
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Tue Dec 8 15:53:28 2009
15  */
16 
17 /* crypto/des/des_locl.h */
18 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
19  * All rights reserved.
20  *
21  * This package is an SSL implementation written
22  * by Eric Young (eay@cryptsoft.com).
23  * The implementation was written so as to conform with Netscapes SSL.
24  *
25  * This library is free for commercial and non-commercial use as long as
26  * the following conditions are aheared to. The following conditions
27  * apply to all code found in this distribution, be it the RC4, RSA,
28  * lhash, DES, etc., code; not just the SSL code. The SSL documentation
29  * included with this distribution is covered by the same copyright terms
30  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
31  *
32  * Copyright remains Eric Young's, and as such any Copyright notices in
33  * the code are not to be removed.
34  * If this package is used in a product, Eric Young should be given attribution
35  * as the author of the parts of the library used.
36  * This can be in the form of a textual message at program startup or
37  * in documentation (online or textual) provided with the package.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the copyright
43  * notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  * notice, this list of conditions and the following disclaimer in the
46  * documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  * must display the following acknowledgement:
49  * "This product includes cryptographic software written by
50  * Eric Young (eay@cryptsoft.com)"
51  * The word 'cryptographic' can be left out if the rouines from the library
52  * being used are not cryptographic related :-).
53  * 4. If you include any Windows specific code (or a derivative thereof) from
54  * the apps directory (application code) you must include an acknowledgement:
55  * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
56  *
57  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  * The licence and distribution terms for any publically available version or
70  * derivative of this code cannot be changed. i.e. this code cannot simply be
71  * copied and put under another distribution licence
72  * [including the GNU Public Licence.]
73  */
74 
75 #ifdef POK_NEEDS_PROTOCOLS_DES
76 
77 #ifndef HEADER_DES_LOCL_H
78 #define HEADER_DES_LOCL_H
79 
80 /*
81 #include <openssl/e_os2.h>
82 */
83 
84 
85 #define DES_LONG unsigned long
86 #if defined(OPENSSL_SYS_VMS_NODECC)
87 # define OPENSSL_EXPORT globalref
88 # define OPENSSL_IMPORT globalref
89 # define OPENSSL_GLOBAL globaldef
90 #elif defined(OPENSSL_SYS_WINDOWS) && defined(OPENSSL_OPT_WINDLL)
91 # define OPENSSL_EXPORT extern __declspec(dllexport)
92 # define OPENSSL_IMPORT extern __declspec(dllimport)
93 # define OPENSSL_GLOBAL
94 #else
95 # define OPENSSL_EXPORT extern
96 # define OPENSSL_IMPORT extern
97 # define OPENSSL_GLOBAL
98 #endif
99 #define OPENSSL_EXTERN OPENSSL_IMPORT
100 
101 
102 #ifdef OPENSSL_EXPORT_VAR_AS_FUNCTION
103 # define OPENSSL_IMPLEMENT_GLOBAL(type,name) \
104  extern type _hide_##name; \
105  type *_shadow_##name(void) { return &_hide_##name; } \
106  static type _hide_##name
107 # define OPENSSL_DECLARE_GLOBAL(type,name) type *_shadow_##name(void)
108 # define OPENSSL_GLOBAL_REF(name) (*(_shadow_##name()))
109 #else
110 # define OPENSSL_IMPLEMENT_GLOBAL(type,name) OPENSSL_GLOBAL type _shadow_##name
111 # define OPENSSL_DECLARE_GLOBAL(type,name) OPENSSL_EXPORT type _shadow_##name
112 # define OPENSSL_GLOBAL_REF(name) _shadow_##name
113 #endif
114 
115 
116 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
117 #ifndef OPENSSL_SYS_MSDOS
118 #define OPENSSL_SYS_MSDOS
119 #endif
120 #endif
121 
122 #include <stdio.h>
123 #include <stdlib.h>
124 
125 #ifndef OPENSSL_SYS_MSDOS
126 #if !defined(OPENSSL_SYS_VMS) || defined(__DECC)
127 #ifdef OPENSSL_UNISTD
128 # include OPENSSL_UNISTD
129 #else
130 # include <libc/stdlib.h>
131 # include <libc/stdio.h>
132 #include <libc/string.h>
133 #endif
134 //#include <libm.h>
135 #endif
136 #endif
137 #include "des.h"
138 
139 #ifdef OPENSSL_SYS_MSDOS /* Visual C++ 2.1 (Windows NT/95) */
140 #include <stdlib.h>
141 #include <errno.h>
142 #include <time.h>
143 #include <io.h>
144 #endif
145 
146 #if defined(__STDC__) || defined(OPENSSL_SYS_VMS) || defined(M_XENIX) || defined(OPENSSL_SYS_MSDOS)
147 #include <string.h>
148 #endif
149 
150 #ifdef OPENSSL_BUILD_SHLIBCRYPTO
151 # undef OPENSSL_EXTERN
152 # define OPENSSL_EXTERN OPENSSL_EXPORT
153 #endif
154 
155 #define ITERATIONS 16
156 #define HALF_ITERATIONS 8
157 
158 /* used in des_read and des_write */
159 #define MAXWRITE (1024*16)
160 #define BSIZE (MAXWRITE+4)
161 
162 #define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
163  l|=((DES_LONG)(*((c)++)))<< 8L, \
164  l|=((DES_LONG)(*((c)++)))<<16L, \
165  l|=((DES_LONG)(*((c)++)))<<24L)
166 
167 /* NOTE - c is not incremented as per c2l */
168 #define c2ln(c,l1,l2,n) { \
169  c+=n; \
170  l1=l2=0; \
171  switch (n) { \
172  case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
173  case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
174  case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
175  case 5: l2|=((DES_LONG)(*(--(c)))); \
176  case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
177  case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
178  case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
179  case 1: l1|=((DES_LONG)(*(--(c)))); \
180  } \
181  }
182 
183 #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
184  *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
185  *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
186  *((c)++)=(unsigned char)(((l)>>24L)&0xff))
187 
188 /* replacements for htonl and ntohl since I have no idea what to do
189  * when faced with machines with 8 byte longs. */
190 #define HDRSIZE 4
191 
192 #define n2l(c,l) (l =((DES_LONG)(*((c)++)))<<24L, \
193  l|=((DES_LONG)(*((c)++)))<<16L, \
194  l|=((DES_LONG)(*((c)++)))<< 8L, \
195  l|=((DES_LONG)(*((c)++))))
196 
197 #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
198  *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
199  *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
200  *((c)++)=(unsigned char)(((l) )&0xff))
201 
202 /* NOTE - c is not incremented as per l2c */
203 #define l2cn(l1,l2,c,n) { \
204  c+=n; \
205  switch (n) { \
206  case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
207  case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
208  case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
209  case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
210  case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
211  case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
212  case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
213  case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
214  } \
215  }
216 
217 #if (defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER)) || defined(__ICC)
218 #define ROTATE(a,n) (_lrotr(a,n))
219 #elif defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
220 # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
221 # define ROTATE(a,n) ({ register unsigned int ret; \
222  asm ("rorl %1,%0" \
223  : "=r"(ret) \
224  : "I"(n),"0"(a) \
225  : "cc"); \
226  ret; \
227  })
228 # endif
229 #endif
230 #ifndef ROTATE
231 #define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n))))
232 #endif
233 
234 /* Don't worry about the LOAD_DATA() stuff, that is used by
235  * fcrypt() to add it's little bit to the front */
236 
237 #ifdef DES_FCRYPT
238 
239 #define LOAD_DATA_tmp(R,S,u,t,E0,E1) \
240  { DES_LONG tmp; LOAD_DATA(R,S,u,t,E0,E1,tmp); }
241 
242 #define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
243  t=R^(R>>16L); \
244  u=t&E0; t&=E1; \
245  tmp=(u<<16); u^=R^s[S ]; u^=tmp; \
246  tmp=(t<<16); t^=R^s[S+1]; t^=tmp
247 #else
248 #define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
249 #define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
250  u=R^s[S ]; \
251  t=R^s[S+1]
252 #endif
253 
254 /* The changes to this macro may help or hinder, depending on the
255  * compiler and the architecture. gcc2 always seems to do well :-).
256  * Inspired by Dana How <how@isl.stanford.edu>
257  * DO NOT use the alternative version on machines with 8 byte longs.
258  * It does not seem to work on the Alpha, even when DES_LONG is 4
259  * bytes, probably an issue of accessing non-word aligned objects :-( */
260 #ifdef DES_PTR
261 
262 /* It recently occurred to me that 0^0^0^0^0^0^0 == 0, so there
263  * is no reason to not xor all the sub items together. This potentially
264  * saves a register since things can be xored directly into L */
265 
266 #if defined(DES_RISC1) || defined(DES_RISC2)
267 #ifdef DES_RISC1
268 #define D_ENCRYPT(LL,R,S) { \
269  unsigned int u1,u2,u3; \
270  LOAD_DATA(R,S,u,t,E0,E1,u1); \
271  u2=(int)u>>8L; \
272  u1=(int)u&0xfc; \
273  u2&=0xfc; \
274  t=ROTATE(t,4); \
275  u>>=16L; \
276  LL^= *(const DES_LONG *)(des_SP +u1); \
277  LL^= *(const DES_LONG *)(des_SP+0x200+u2); \
278  u3=(int)(u>>8L); \
279  u1=(int)u&0xfc; \
280  u3&=0xfc; \
281  LL^= *(const DES_LONG *)(des_SP+0x400+u1); \
282  LL^= *(const DES_LONG *)(des_SP+0x600+u3); \
283  u2=(int)t>>8L; \
284  u1=(int)t&0xfc; \
285  u2&=0xfc; \
286  t>>=16L; \
287  LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
288  LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
289  u3=(int)t>>8L; \
290  u1=(int)t&0xfc; \
291  u3&=0xfc; \
292  LL^= *(const DES_LONG *)(des_SP+0x500+u1); \
293  LL^= *(const DES_LONG *)(des_SP+0x700+u3); }
294 #endif
295 #ifdef DES_RISC2
296 #define D_ENCRYPT(LL,R,S) { \
297  unsigned int u1,u2,s1,s2; \
298  LOAD_DATA(R,S,u,t,E0,E1,u1); \
299  u2=(int)u>>8L; \
300  u1=(int)u&0xfc; \
301  u2&=0xfc; \
302  t=ROTATE(t,4); \
303  LL^= *(const DES_LONG *)(des_SP +u1); \
304  LL^= *(const DES_LONG *)(des_SP+0x200+u2); \
305  s1=(int)(u>>16L); \
306  s2=(int)(u>>24L); \
307  s1&=0xfc; \
308  s2&=0xfc; \
309  LL^= *(const DES_LONG *)(des_SP+0x400+s1); \
310  LL^= *(const DES_LONG *)(des_SP+0x600+s2); \
311  u2=(int)t>>8L; \
312  u1=(int)t&0xfc; \
313  u2&=0xfc; \
314  LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
315  LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
316  s1=(int)(t>>16L); \
317  s2=(int)(t>>24L); \
318  s1&=0xfc; \
319  s2&=0xfc; \
320  LL^= *(const DES_LONG *)(des_SP+0x500+s1); \
321  LL^= *(const DES_LONG *)(des_SP+0x700+s2); }
322 #endif
323 #else
324 #define D_ENCRYPT(LL,R,S) { \
325  LOAD_DATA_tmp(R,S,u,t,E0,E1); \
326  t=ROTATE(t,4); \
327  LL^= \
328  *(const DES_LONG *)(des_SP +((u )&0xfc))^ \
329  *(const DES_LONG *)(des_SP+0x200+((u>> 8L)&0xfc))^ \
330  *(const DES_LONG *)(des_SP+0x400+((u>>16L)&0xfc))^ \
331  *(const DES_LONG *)(des_SP+0x600+((u>>24L)&0xfc))^ \
332  *(const DES_LONG *)(des_SP+0x100+((t )&0xfc))^ \
333  *(const DES_LONG *)(des_SP+0x300+((t>> 8L)&0xfc))^ \
334  *(const DES_LONG *)(des_SP+0x500+((t>>16L)&0xfc))^ \
335  *(const DES_LONG *)(des_SP+0x700+((t>>24L)&0xfc)); }
336 #endif
337 
338 #else /* original version */
339 
340 #if defined(DES_RISC1) || defined(DES_RISC2)
341 #ifdef DES_RISC1
342 #define D_ENCRYPT(LL,R,S) {\
343  unsigned int u1,u2,u3; \
344  LOAD_DATA(R,S,u,t,E0,E1,u1); \
345  u>>=2L; \
346  t=ROTATE(t,6); \
347  u2=(int)u>>8L; \
348  u1=(int)u&0x3f; \
349  u2&=0x3f; \
350  u>>=16L; \
351  LL^=DES_SPtrans[0][u1]; \
352  LL^=DES_SPtrans[2][u2]; \
353  u3=(int)u>>8L; \
354  u1=(int)u&0x3f; \
355  u3&=0x3f; \
356  LL^=DES_SPtrans[4][u1]; \
357  LL^=DES_SPtrans[6][u3]; \
358  u2=(int)t>>8L; \
359  u1=(int)t&0x3f; \
360  u2&=0x3f; \
361  t>>=16L; \
362  LL^=DES_SPtrans[1][u1]; \
363  LL^=DES_SPtrans[3][u2]; \
364  u3=(int)t>>8L; \
365  u1=(int)t&0x3f; \
366  u3&=0x3f; \
367  LL^=DES_SPtrans[5][u1]; \
368  LL^=DES_SPtrans[7][u3]; }
369 #endif
370 #ifdef DES_RISC2
371 #define D_ENCRYPT(LL,R,S) {\
372  unsigned int u1,u2,s1,s2; \
373  LOAD_DATA(R,S,u,t,E0,E1,u1); \
374  u>>=2L; \
375  t=ROTATE(t,6); \
376  u2=(int)u>>8L; \
377  u1=(int)u&0x3f; \
378  u2&=0x3f; \
379  LL^=DES_SPtrans[0][u1]; \
380  LL^=DES_SPtrans[2][u2]; \
381  s1=(int)u>>16L; \
382  s2=(int)u>>24L; \
383  s1&=0x3f; \
384  s2&=0x3f; \
385  LL^=DES_SPtrans[4][s1]; \
386  LL^=DES_SPtrans[6][s2]; \
387  u2=(int)t>>8L; \
388  u1=(int)t&0x3f; \
389  u2&=0x3f; \
390  LL^=DES_SPtrans[1][u1]; \
391  LL^=DES_SPtrans[3][u2]; \
392  s1=(int)t>>16; \
393  s2=(int)t>>24L; \
394  s1&=0x3f; \
395  s2&=0x3f; \
396  LL^=DES_SPtrans[5][s1]; \
397  LL^=DES_SPtrans[7][s2]; }
398 #endif
399 
400 #else
401 
402 #define D_ENCRYPT(LL,R,S) {\
403  LOAD_DATA_tmp(R,S,u,t,E0,E1); \
404  t=ROTATE(t,4); \
405  LL^=\
406  DES_SPtrans[0][(u>> 2L)&0x3f]^ \
407  DES_SPtrans[2][(u>>10L)&0x3f]^ \
408  DES_SPtrans[4][(u>>18L)&0x3f]^ \
409  DES_SPtrans[6][(u>>26L)&0x3f]^ \
410  DES_SPtrans[1][(t>> 2L)&0x3f]^ \
411  DES_SPtrans[3][(t>>10L)&0x3f]^ \
412  DES_SPtrans[5][(t>>18L)&0x3f]^ \
413  DES_SPtrans[7][(t>>26L)&0x3f]; }
414 #endif
415 #endif
416 
417  /* IP and FP
418  * The problem is more of a geometric problem that random bit fiddling.
419  0 1 2 3 4 5 6 7 62 54 46 38 30 22 14 6
420  8 9 10 11 12 13 14 15 60 52 44 36 28 20 12 4
421  16 17 18 19 20 21 22 23 58 50 42 34 26 18 10 2
422  24 25 26 27 28 29 30 31 to 56 48 40 32 24 16 8 0
423 
424  32 33 34 35 36 37 38 39 63 55 47 39 31 23 15 7
425  40 41 42 43 44 45 46 47 61 53 45 37 29 21 13 5
426  48 49 50 51 52 53 54 55 59 51 43 35 27 19 11 3
427  56 57 58 59 60 61 62 63 57 49 41 33 25 17 9 1
428 
429  The output has been subject to swaps of the form
430  0 1 -> 3 1 but the odd and even bits have been put into
431  2 3 2 0
432  different words. The main trick is to remember that
433  t=((l>>size)^r)&(mask);
434  r^=t;
435  l^=(t<<size);
436  can be used to swap and move bits between words.
437 
438  So l = 0 1 2 3 r = 16 17 18 19
439  4 5 6 7 20 21 22 23
440  8 9 10 11 24 25 26 27
441  12 13 14 15 28 29 30 31
442  becomes (for size == 2 and mask == 0x3333)
443  t = 2^16 3^17 -- -- l = 0 1 16 17 r = 2 3 18 19
444  6^20 7^21 -- -- 4 5 20 21 6 7 22 23
445  10^24 11^25 -- -- 8 9 24 25 10 11 24 25
446  14^28 15^29 -- -- 12 13 28 29 14 15 28 29
447 
448  Thanks for hints from Richard Outerbridge - he told me IP&FP
449  could be done in 15 xor, 10 shifts and 5 ands.
450  When I finally started to think of the problem in 2D
451  I first got ~42 operations without xors. When I remembered
452  how to use xors :-) I got it to its final state.
453  */
454 #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
455  (b)^=(t),\
456  (a)^=((t)<<(n)))
457 
458 #define IP(l,r) \
459  { \
460  register DES_LONG tt; \
461  PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
462  PERM_OP(l,r,tt,16,0x0000ffffL); \
463  PERM_OP(r,l,tt, 2,0x33333333L); \
464  PERM_OP(l,r,tt, 8,0x00ff00ffL); \
465  PERM_OP(r,l,tt, 1,0x55555555L); \
466  }
467 
468 #define FP(l,r) \
469  { \
470  register DES_LONG tt; \
471  PERM_OP(l,r,tt, 1,0x55555555L); \
472  PERM_OP(r,l,tt, 8,0x00ff00ffL); \
473  PERM_OP(l,r,tt, 2,0x33333333L); \
474  PERM_OP(r,l,tt,16,0x0000ffffL); \
475  PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
476  }
477 
478 extern const DES_LONG DES_SPtrans[8][64];
479 
480 #endif
481 
482 #endif /* POK_NEEDS_ ... */