7 else if (we have termio.h)
14 #else /* !HAVE_TERMIOS_H */
20 #endif /* !HAVE_TERMIOS_H */
22 #endif /* HAVE_CONFIG_H */
24 /* crypto/des/des_locl.h */
25 /* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
26 * All rights reserved.
28 * This package is an SSL implementation written
29 * by Eric Young (eay@mincom.oz.au).
30 * The implementation was written so as to conform with Netscapes SSL.
32 * This library is free for commercial and non-commercial use as long as
33 * the following conditions are aheared to. The following conditions
34 * apply to all code found in this distribution, be it the RC4, RSA,
35 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
36 * included with this distribution is covered by the same copyright terms
37 * except that the holder is Tim Hudson (tjh@mincom.oz.au).
39 * Copyright remains Eric Young's, and as such any Copyright notices in
40 * the code are not to be removed.
41 * If this package is used in a product, Eric Young should be given attribution
42 * as the author of the parts of the library used.
43 * This can be in the form of a textual message at program startup or
44 * in documentation (online or textual) provided with the package.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
49 * 1. Redistributions of source code must retain the copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * "This product includes cryptographic software written by
57 * Eric Young (eay@mincom.oz.au)"
58 * The word 'cryptographic' can be left out if the rouines from the library
59 * being used are not cryptographic related :-).
60 * 4. If you include any Windows specific code (or a derivative thereof) from
61 * the apps directory (application code) you must include an acknowledgement:
62 * "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"
64 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 * The licence and distribution terms for any publically available version or
77 * derivative of this code cannot be changed. i.e. this code cannot simply be
78 * copied and put under another distribution licence
79 * [including the GNU Public Licence.]
82 /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
84 * Always modify des_locl.org since des_locl.h is automatically generated from
85 * it during SSLeay configuration.
87 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
90 #ifndef HEADER_DES_LOCL_H
91 #define HEADER_DES_LOCL_H
93 #if defined(WIN32) || defined(WIN16)
109 #ifndef DES_DEFAULT_OPTIONS
110 /* the following is tweaked from a config script, that is why it is a
111 * protected undef/define */
116 /* This helps C compiler generate the correct code for multiple functional
117 * units. It reduces register dependancies at the expense of 2 more
127 #if defined(DES_RISC1) && defined(DES_RISC2)
128 YOU SHOULD NOT HAVE BOTH DES_RISC1 AND DES_RISC2 DEFINED!!!!!
131 /* Unroll the inner loop, this sometimes helps, sometimes hinders.
132 * Very mucy CPU dependant */
137 /* These default values were supplied by
138 * Peter Gutman <pgut001@cs.auckland.ac.nz>
139 * They are only used if nothing else has been defined */
140 #if !defined(DES_PTR) && !defined(DES_RISC1) && !defined(DES_RISC2) && !defined(DES_UNROLL)
141 /* Special defines which change the way the code is built depending on the
142 CPU and OS. For SGI machines you can use _MIPS_SZLONG (32 or 64) to find
143 even newer MIPS CPU's, but at the moment one size fits all for
144 optimization options. Older Sparc's work better with only UNROLL, but
145 there's no way to tell at compile time what it is you're running on */
147 #if defined( sun ) /* Newer Sparc's */
151 #elif defined( __ultrix ) /* Older MIPS */
155 #elif defined( __osf1__ ) /* Alpha */
158 #elif defined ( _AIX ) /* RS6000 */
160 #elif defined( __hpux ) /* HP-PA */
162 #elif defined( __aux ) /* 68K */
164 #elif defined( __dgux ) /* 88K (but P6 in latest boxes) */
166 #elif defined( __sgi ) /* Newer MIPS */
170 #elif defined( i386 ) /* x86 boxes, should be gcc */
174 #endif /* Systems-specific speed defines */
177 #endif /* DES_DEFAULT_OPTIONS */
179 #ifdef MSDOS /* Visual C++ 2.1 (Windows NT/95) */
190 #if defined(__STDC__) || defined(VMS) || defined(M_XENIX) || defined(MSDOS) || defined(WIN32)
217 #define srandom(s) srand(s)
221 #define ITERATIONS 16
222 #define HALF_ITERATIONS 8
224 /* used in des_read and des_write */
225 #define MAXWRITE (1024*16)
226 #define des_BSIZE (MAXWRITE+4)
228 #define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
229 l|=((DES_LONG)(*((c)++)))<< 8L, \
230 l|=((DES_LONG)(*((c)++)))<<16L, \
231 l|=((DES_LONG)(*((c)++)))<<24L)
233 /* NOTE - c is not incremented as per c2l */
234 #define c2ln(c,l1,l2,n) { \
238 case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
239 case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
240 case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
241 case 5: l2|=((DES_LONG)(*(--(c)))); \
242 case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
243 case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
244 case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
245 case 1: l1|=((DES_LONG)(*(--(c)))); \
249 #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
250 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
251 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
252 *((c)++)=(unsigned char)(((l)>>24L)&0xff))
254 /* replacements for htonl and ntohl since I have no idea what to do
255 * when faced with machines with 8 byte longs. */
258 #define n2l(c,l) (l =((DES_LONG)(*((c)++)))<<24L, \
259 l|=((DES_LONG)(*((c)++)))<<16L, \
260 l|=((DES_LONG)(*((c)++)))<< 8L, \
261 l|=((DES_LONG)(*((c)++))))
263 #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
264 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
265 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
266 *((c)++)=(unsigned char)(((l) )&0xff))
268 /* NOTE - c is not incremented as per l2c */
269 #define l2cn(l1,l2,c,n) { \
272 case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
273 case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
274 case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
275 case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
276 case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
277 case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
278 case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
279 case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
284 #define ROTATE(a,n) (_lrotr(a,n))
286 #define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n))))
289 /* Don't worry about the LOAD_DATA() stuff, that is used by
290 * fcrypt() to add it's little bit to the front */
294 #define LOAD_DATA_tmp(R,S,u,t,E0,E1) \
295 { DES_LONG tmp; LOAD_DATA(R,S,u,t,E0,E1,tmp); }
297 #define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
300 tmp=(u<<16); u^=R^s[S ]; u^=tmp; \
301 tmp=(t<<16); t^=R^s[S+1]; t^=tmp
303 #define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
304 #define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
309 /* The changes to this macro may help or hinder, depending on the
310 * compiler and the achitecture. gcc2 always seems to do well :-).
311 * Inspired by Dana How <how@isl.stanford.edu>
312 * DO NOT use the alternative version on machines with 8 byte longs.
313 * It does not seem to work on the Alpha, even when DES_LONG is 4
314 * bytes, probably an issue of accessing non-word aligned objects :-( */
317 /* It recently occured to me that 0^0^0^0^0^0^0 == 0, so there
318 * is no reason to not xor all the sub items together. This potentially
319 * saves a register since things can be xored directly into L */
321 #if defined(DES_RISC1) || defined(DES_RISC2)
323 #define D_ENCRYPT(LL,R,S) { \
324 unsigned int u1,u2,u3; \
325 LOAD_DATA(R,S,u,t,E0,E1,u1); \
331 LL^= *(DES_LONG *)((unsigned char *)des_SP +u1); \
332 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x200+u2); \
336 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x400+u1); \
337 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x600+u3); \
342 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x100+u1); \
343 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x300+u2); \
347 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x500+u1); \
348 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x700+u3); }
351 #define D_ENCRYPT(LL,R,S) { \
352 unsigned int u1,u2,s1,s2; \
353 LOAD_DATA(R,S,u,t,E0,E1,u1); \
358 LL^= *(DES_LONG *)((unsigned char *)des_SP +u1); \
359 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x200+u2); \
364 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x400+s1); \
365 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x600+s2); \
369 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x100+u1); \
370 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x300+u2); \
375 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x500+s1); \
376 LL^= *(DES_LONG *)((unsigned char *)des_SP+0x700+s2); }
379 #define D_ENCRYPT(LL,R,S) { \
380 LOAD_DATA_tmp(R,S,u,t,E0,E1); \
383 *(DES_LONG *)((unsigned char *)des_SP +((u )&0xfc))^ \
384 *(DES_LONG *)((unsigned char *)des_SP+0x200+((u>> 8L)&0xfc))^ \
385 *(DES_LONG *)((unsigned char *)des_SP+0x400+((u>>16L)&0xfc))^ \
386 *(DES_LONG *)((unsigned char *)des_SP+0x600+((u>>24L)&0xfc))^ \
387 *(DES_LONG *)((unsigned char *)des_SP+0x100+((t )&0xfc))^ \
388 *(DES_LONG *)((unsigned char *)des_SP+0x300+((t>> 8L)&0xfc))^ \
389 *(DES_LONG *)((unsigned char *)des_SP+0x500+((t>>16L)&0xfc))^ \
390 *(DES_LONG *)((unsigned char *)des_SP+0x700+((t>>24L)&0xfc)); }
393 #else /* original version */
395 #if defined(DES_RISC1) || defined(DES_RISC2)
397 #define D_ENCRYPT(LL,R,S) {\
398 unsigned int u1,u2,u3; \
399 LOAD_DATA(R,S,u,t,E0,E1,u1); \
406 LL^=des_SPtrans[0][u1]; \
407 LL^=des_SPtrans[2][u2]; \
411 LL^=des_SPtrans[4][u1]; \
412 LL^=des_SPtrans[6][u3]; \
417 LL^=des_SPtrans[1][u1]; \
418 LL^=des_SPtrans[3][u2]; \
422 LL^=des_SPtrans[5][u1]; \
423 LL^=des_SPtrans[7][u3]; }
426 #define D_ENCRYPT(LL,R,S) {\
427 unsigned int u1,u2,s1,s2; \
428 LOAD_DATA(R,S,u,t,E0,E1,u1); \
434 LL^=des_SPtrans[0][u1]; \
435 LL^=des_SPtrans[2][u2]; \
440 LL^=des_SPtrans[4][s1]; \
441 LL^=des_SPtrans[6][s2]; \
445 LL^=des_SPtrans[1][u1]; \
446 LL^=des_SPtrans[3][u2]; \
451 LL^=des_SPtrans[5][s1]; \
452 LL^=des_SPtrans[7][s2]; }
457 #define D_ENCRYPT(LL,R,S) {\
458 LOAD_DATA_tmp(R,S,u,t,E0,E1); \
461 des_SPtrans[0][(u>> 2L)&0x3f]^ \
462 des_SPtrans[2][(u>>10L)&0x3f]^ \
463 des_SPtrans[4][(u>>18L)&0x3f]^ \
464 des_SPtrans[6][(u>>26L)&0x3f]^ \
465 des_SPtrans[1][(t>> 2L)&0x3f]^ \
466 des_SPtrans[3][(t>>10L)&0x3f]^ \
467 des_SPtrans[5][(t>>18L)&0x3f]^ \
468 des_SPtrans[7][(t>>26L)&0x3f]; }
473 * The problem is more of a geometric problem that random bit fiddling.
474 0 1 2 3 4 5 6 7 62 54 46 38 30 22 14 6
475 8 9 10 11 12 13 14 15 60 52 44 36 28 20 12 4
476 16 17 18 19 20 21 22 23 58 50 42 34 26 18 10 2
477 24 25 26 27 28 29 30 31 to 56 48 40 32 24 16 8 0
479 32 33 34 35 36 37 38 39 63 55 47 39 31 23 15 7
480 40 41 42 43 44 45 46 47 61 53 45 37 29 21 13 5
481 48 49 50 51 52 53 54 55 59 51 43 35 27 19 11 3
482 56 57 58 59 60 61 62 63 57 49 41 33 25 17 9 1
484 The output has been subject to swaps of the form
485 0 1 -> 3 1 but the odd and even bits have been put into
487 different words. The main trick is to remember that
488 t=((l>>size)^r)&(mask);
491 can be used to swap and move bits between words.
493 So l = 0 1 2 3 r = 16 17 18 19
495 8 9 10 11 24 25 26 27
496 12 13 14 15 28 29 30 31
497 becomes (for size == 2 and mask == 0x3333)
498 t = 2^16 3^17 -- -- l = 0 1 16 17 r = 2 3 18 19
499 6^20 7^21 -- -- 4 5 20 21 6 7 22 23
500 10^24 11^25 -- -- 8 9 24 25 10 11 24 25
501 14^28 15^29 -- -- 12 13 28 29 14 15 28 29
503 Thanks for hints from Richard Outerbridge - he told me IP&FP
504 could be done in 15 xor, 10 shifts and 5 ands.
505 When I finally started to think of the problem in 2D
506 I first got ~42 operations without xors. When I remembered
507 how to use xors :-) I got it to its final state.
509 #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
515 register DES_LONG tt; \
516 PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
517 PERM_OP(l,r,tt,16,0x0000ffffL); \
518 PERM_OP(r,l,tt, 2,0x33333333L); \
519 PERM_OP(l,r,tt, 8,0x00ff00ffL); \
520 PERM_OP(r,l,tt, 1,0x55555555L); \
525 register DES_LONG tt; \
526 PERM_OP(l,r,tt, 1,0x55555555L); \
527 PERM_OP(r,l,tt, 8,0x00ff00ffL); \
528 PERM_OP(l,r,tt, 2,0x33333333L); \
529 PERM_OP(r,l,tt,16,0x0000ffffL); \
530 PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
533 extern const DES_LONG des_SPtrans[8][64];