1#ifdef HAVE_CONFIG_H
2#include "config.h"
3
4/*
5  if (we have termios.h)
6    define TERMIOS
7  else if (we have termio.h)
8    define TERMIO
9*/
10#ifdef HAVE_TERMIOS_H
11
12#define TERMIOS
13
14#else /* !HAVE_TERMIOS_H */
15
16#ifdef HAVE_TERMIO_H
17#define TERMIO
18#endif
19
20#endif /* !HAVE_TERMIOS_H */
21
22#endif /* HAVE_CONFIG_H */
23
24/* crypto/des/des_locl.h */
25/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
26 * All rights reserved.
27 *
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.
31 *
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).
38 *
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.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
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)"
63 *
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
74 * SUCH DAMAGE.
75 *
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.]
80 */
81
82/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
83 *
84 * Always modify des_locl.org since des_locl.h is automatically generated from
85 * it during SSLeay configuration.
86 *
87 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
88 */
89
90#ifndef HEADER_DES_LOCL_H
91#define HEADER_DES_LOCL_H
92
93#if defined(WIN32) || defined(WIN16)
94#ifndef MSDOS
95#define MSDOS
96#endif
97#endif
98
99#include <stdio.h>
100#include <stdlib.h>
101#ifdef HAVE_UNISTD_H
102#include <unistd.h>
103#endif
104#ifdef HAVE_IO_H
105#include <io.h>
106#endif
107#include "des.h"
108
109#ifndef DES_DEFAULT_OPTIONS
110/* the following is tweaked from a config script, that is why it is a
111 * protected undef/define */
112#ifndef DES_PTR
113#undef DES_PTR
114#endif
115
116/* This helps C compiler generate the correct code for multiple functional
117 * units.  It reduces register dependancies at the expense of 2 more
118 * registers */
119#ifndef DES_RISC1
120#undef DES_RISC1
121#endif
122
123#ifndef DES_RISC2
124#undef DES_RISC2
125#endif
126
127#if defined(DES_RISC1) && defined(DES_RISC2)
128YOU SHOULD NOT HAVE BOTH DES_RISC1 AND DES_RISC2 DEFINED!!!!!
129#endif
130
131/* Unroll the inner loop, this sometimes helps, sometimes hinders.
132 * Very mucy CPU dependant */
133#ifndef DES_UNROLL
134#undef DES_UNROLL
135#endif
136
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 */
146
147#if defined( sun )		/* Newer Sparc's */
148  #define DES_PTR
149  #define DES_RISC1
150  #define DES_UNROLL
151#elif defined( __ultrix )	/* Older MIPS */
152  #define DES_PTR
153  #define DES_RISC2
154  #define DES_UNROLL
155#elif defined( __osf1__ )	/* Alpha */
156  #define DES_PTR
157  #define DES_RISC2
158#elif defined ( _AIX )		/* RS6000 */
159  /* Unknown */
160#elif defined( __hpux )		/* HP-PA */
161  #define DES_UNROLL
162#elif defined( __aux )		/* 68K */
163  /* Unknown */
164#elif defined( __dgux )		/* 88K (but P6 in latest boxes) */
165  #define DES_UNROLL
166#elif defined( __sgi )		/* Newer MIPS */
167  #define DES_PTR
168  #define DES_RISC2
169  #define DES_UNROLL
170#elif defined( i386 )		/* x86 boxes, should be gcc */
171  #define DES_PTR
172  #define DES_RISC1
173  #define DES_UNROLL
174#endif /* Systems-specific speed defines */
175#endif
176
177#endif /* DES_DEFAULT_OPTIONS */
178
179#ifdef MSDOS		/* Visual C++ 2.1 (Windows NT/95) */
180#include <stdlib.h>
181#include <errno.h>
182#include <time.h>
183#include <io.h>
184#ifndef RAND
185#define RAND
186#endif
187#undef NOPROTO
188#endif
189
190#if defined(__STDC__) || defined(VMS) || defined(M_XENIX) || defined(MSDOS) || defined(WIN32)
191#include <string.h>
192#endif
193
194#ifndef RAND
195#define RAND
196#endif
197
198#ifdef linux
199#undef RAND
200#endif
201
202#ifdef MSDOS
203#define getpid() 2
204#define RAND
205#undef NOPROTO
206#endif
207
208#if defined(NOCONST)
209#define const
210#endif
211
212#ifdef __STDC__
213#undef NOPROTO
214#endif
215
216#ifdef RAND
217#define srandom(s) srand(s)
218#define random rand
219#endif
220
221#define ITERATIONS 16
222#define HALF_ITERATIONS 8
223
224/* used in des_read and des_write */
225#define MAXWRITE	(1024*16)
226#define BSIZE		(MAXWRITE+4)
227
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)
232
233/* NOTE - c is not incremented as per c2l */
234#define c2ln(c,l1,l2,n)	{ \
235			c+=n; \
236			l1=l2=0; \
237			switch (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))));     \
246				} \
247			}
248
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))
253
254/* replacements for htonl and ntohl since I have no idea what to do
255 * when faced with machines with 8 byte longs. */
256#define HDRSIZE 4
257
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)++))))
262
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))
267
268/* NOTE - c is not incremented as per l2c */
269#define l2cn(l1,l2,c,n)	{ \
270			c+=n; \
271			switch (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); \
280				} \
281			}
282
283#if defined(WIN32)
284#define	ROTATE(a,n)	(_lrotr(a,n))
285#else
286#define	ROTATE(a,n)	(((a)>>(n))+((a)<<(32-(n))))
287#endif
288
289/* Don't worry about the LOAD_DATA() stuff, that is used by
290 * fcrypt() to add it's little bit to the front */
291
292#ifdef DES_FCRYPT
293
294#define LOAD_DATA_tmp(R,S,u,t,E0,E1) \
295	{ DES_LONG tmp; LOAD_DATA(R,S,u,t,E0,E1,tmp); }
296
297#define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
298	t=R^(R>>16L); \
299	u=t&E0; t&=E1; \
300	tmp=(u<<16); u^=R^s[S  ]; u^=tmp; \
301	tmp=(t<<16); t^=R^s[S+1]; t^=tmp
302#else
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) \
305	u=R^s[S  ]; \
306	t=R^s[S+1]
307#endif
308
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 :-( */
315#ifdef DES_PTR
316
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 */
320
321#if defined(DES_RISC1) || defined(DES_RISC2)
322#ifdef DES_RISC1
323#define D_ENCRYPT(LL,R,S) { \
324	unsigned int u1,u2,u3; \
325	LOAD_DATA(R,S,u,t,E0,E1,u1); \
326	u2=(int)u>>8L; \
327	u1=(int)u&0xfc; \
328	u2&=0xfc; \
329	t=ROTATE(t,4); \
330	u>>=16L; \
331	LL^= *(DES_LONG *)((unsigned char *)des_SP      +u1); \
332	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x200+u2); \
333	u3=(int)(u>>8L); \
334	u1=(int)u&0xfc; \
335	u3&=0xfc; \
336	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x400+u1); \
337	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x600+u3); \
338	u2=(int)t>>8L; \
339	u1=(int)t&0xfc; \
340	u2&=0xfc; \
341	t>>=16L; \
342	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x100+u1); \
343	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x300+u2); \
344	u3=(int)t>>8L; \
345	u1=(int)t&0xfc; \
346	u3&=0xfc; \
347	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x500+u1); \
348	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x700+u3); }
349#endif
350#ifdef DES_RISC2
351#define D_ENCRYPT(LL,R,S) { \
352	unsigned int u1,u2,s1,s2; \
353	LOAD_DATA(R,S,u,t,E0,E1,u1); \
354	u2=(int)u>>8L; \
355	u1=(int)u&0xfc; \
356	u2&=0xfc; \
357	t=ROTATE(t,4); \
358	LL^= *(DES_LONG *)((unsigned char *)des_SP      +u1); \
359	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x200+u2); \
360	s1=(int)(u>>16L); \
361	s2=(int)(u>>24L); \
362	s1&=0xfc; \
363	s2&=0xfc; \
364	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x400+s1); \
365	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x600+s2); \
366	u2=(int)t>>8L; \
367	u1=(int)t&0xfc; \
368	u2&=0xfc; \
369	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x100+u1); \
370	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x300+u2); \
371	s1=(int)(t>>16L); \
372	s2=(int)(t>>24L); \
373	s1&=0xfc; \
374	s2&=0xfc; \
375	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x500+s1); \
376	LL^= *(DES_LONG *)((unsigned char *)des_SP+0x700+s2); }
377#endif
378#else
379#define D_ENCRYPT(LL,R,S) { \
380	LOAD_DATA_tmp(R,S,u,t,E0,E1); \
381	t=ROTATE(t,4); \
382	LL^= \
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)); }
391#endif
392
393#else /* original version */
394
395#if defined(DES_RISC1) || defined(DES_RISC2)
396#ifdef DES_RISC1
397#define D_ENCRYPT(LL,R,S) {\
398	unsigned int u1,u2,u3; \
399	LOAD_DATA(R,S,u,t,E0,E1,u1); \
400	u>>=2L; \
401	t=ROTATE(t,6); \
402	u2=(int)u>>8L; \
403	u1=(int)u&0x3f; \
404	u2&=0x3f; \
405	u>>=16L; \
406	LL^=des_SPtrans[0][u1]; \
407	LL^=des_SPtrans[2][u2]; \
408	u3=(int)u>>8L; \
409	u1=(int)u&0x3f; \
410	u3&=0x3f; \
411	LL^=des_SPtrans[4][u1]; \
412	LL^=des_SPtrans[6][u3]; \
413	u2=(int)t>>8L; \
414	u1=(int)t&0x3f; \
415	u2&=0x3f; \
416	t>>=16L; \
417	LL^=des_SPtrans[1][u1]; \
418	LL^=des_SPtrans[3][u2]; \
419	u3=(int)t>>8L; \
420	u1=(int)t&0x3f; \
421	u3&=0x3f; \
422	LL^=des_SPtrans[5][u1]; \
423	LL^=des_SPtrans[7][u3]; }
424#endif
425#ifdef DES_RISC2
426#define D_ENCRYPT(LL,R,S) {\
427	unsigned int u1,u2,s1,s2; \
428	LOAD_DATA(R,S,u,t,E0,E1,u1); \
429	u>>=2L; \
430	t=ROTATE(t,6); \
431	u2=(int)u>>8L; \
432	u1=(int)u&0x3f; \
433	u2&=0x3f; \
434	LL^=des_SPtrans[0][u1]; \
435	LL^=des_SPtrans[2][u2]; \
436	s1=(int)u>>16L; \
437	s2=(int)u>>24L; \
438	s1&=0x3f; \
439	s2&=0x3f; \
440	LL^=des_SPtrans[4][s1]; \
441	LL^=des_SPtrans[6][s2]; \
442	u2=(int)t>>8L; \
443	u1=(int)t&0x3f; \
444	u2&=0x3f; \
445	LL^=des_SPtrans[1][u1]; \
446	LL^=des_SPtrans[3][u2]; \
447	s1=(int)t>>16; \
448	s2=(int)t>>24L; \
449	s1&=0x3f; \
450	s2&=0x3f; \
451	LL^=des_SPtrans[5][s1]; \
452	LL^=des_SPtrans[7][s2]; }
453#endif
454
455#else
456
457#define D_ENCRYPT(LL,R,S) {\
458	LOAD_DATA_tmp(R,S,u,t,E0,E1); \
459	t=ROTATE(t,4); \
460	LL^=\
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]; }
469#endif
470#endif
471
472	/* IP and FP
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
478
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
483
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
486	2 3    2 0
487	different words.  The main trick is to remember that
488	t=((l>>size)^r)&(mask);
489	r^=t;
490	l^=(t<<size);
491	can be used to swap and move bits between words.
492
493	So l =  0  1  2  3  r = 16 17 18 19
494	        4  5  6  7      20 21 22 23
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
502
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.
508	*/
509#define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
510	(b)^=(t),\
511	(a)^=((t)<<(n)))
512
513#define IP(l,r) \
514	{ \
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); \
521	}
522
523#define FP(l,r) \
524	{ \
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); \
531	}
532
533extern const DES_LONG des_SPtrans[8][64];
534
535#endif
536