xref: /onnv-gate/usr/src/common/openssl/crypto/des/des_enc.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/des/des_enc.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include "des_locl.h"
60*0Sstevel@tonic-gate 
DES_encrypt1(DES_LONG * data,DES_key_schedule * ks,int enc)61*0Sstevel@tonic-gate void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc)
62*0Sstevel@tonic-gate 	{
63*0Sstevel@tonic-gate 	register DES_LONG l,r,t,u;
64*0Sstevel@tonic-gate #ifdef DES_PTR
65*0Sstevel@tonic-gate 	register const unsigned char *des_SP=(const unsigned char *)DES_SPtrans;
66*0Sstevel@tonic-gate #endif
67*0Sstevel@tonic-gate #ifndef DES_UNROLL
68*0Sstevel@tonic-gate 	register int i;
69*0Sstevel@tonic-gate #endif
70*0Sstevel@tonic-gate 	register DES_LONG *s;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	r=data[0];
73*0Sstevel@tonic-gate 	l=data[1];
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	IP(r,l);
76*0Sstevel@tonic-gate 	/* Things have been modified so that the initial rotate is
77*0Sstevel@tonic-gate 	 * done outside the loop.  This required the
78*0Sstevel@tonic-gate 	 * DES_SPtrans values in sp.h to be rotated 1 bit to the right.
79*0Sstevel@tonic-gate 	 * One perl script later and things have a 5% speed up on a sparc2.
80*0Sstevel@tonic-gate 	 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
81*0Sstevel@tonic-gate 	 * for pointing this out. */
82*0Sstevel@tonic-gate 	/* clear the top bits on machines with 8byte longs */
83*0Sstevel@tonic-gate 	/* shift left by 2 */
84*0Sstevel@tonic-gate 	r=ROTATE(r,29)&0xffffffffL;
85*0Sstevel@tonic-gate 	l=ROTATE(l,29)&0xffffffffL;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	s=ks->ks->deslong;
88*0Sstevel@tonic-gate 	/* I don't know if it is worth the effort of loop unrolling the
89*0Sstevel@tonic-gate 	 * inner loop */
90*0Sstevel@tonic-gate 	if (enc)
91*0Sstevel@tonic-gate 		{
92*0Sstevel@tonic-gate #ifdef DES_UNROLL
93*0Sstevel@tonic-gate 		D_ENCRYPT(l,r, 0); /*  1 */
94*0Sstevel@tonic-gate 		D_ENCRYPT(r,l, 2); /*  2 */
95*0Sstevel@tonic-gate 		D_ENCRYPT(l,r, 4); /*  3 */
96*0Sstevel@tonic-gate 		D_ENCRYPT(r,l, 6); /*  4 */
97*0Sstevel@tonic-gate 		D_ENCRYPT(l,r, 8); /*  5 */
98*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,10); /*  6 */
99*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,12); /*  7 */
100*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,14); /*  8 */
101*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,16); /*  9 */
102*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,18); /*  10 */
103*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,20); /*  11 */
104*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,22); /*  12 */
105*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,24); /*  13 */
106*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,26); /*  14 */
107*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,28); /*  15 */
108*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,30); /*  16 */
109*0Sstevel@tonic-gate #else
110*0Sstevel@tonic-gate 		for (i=0; i<32; i+=8)
111*0Sstevel@tonic-gate 			{
112*0Sstevel@tonic-gate 			D_ENCRYPT(l,r,i+0); /*  1 */
113*0Sstevel@tonic-gate 			D_ENCRYPT(r,l,i+2); /*  2 */
114*0Sstevel@tonic-gate 			D_ENCRYPT(l,r,i+4); /*  3 */
115*0Sstevel@tonic-gate 			D_ENCRYPT(r,l,i+6); /*  4 */
116*0Sstevel@tonic-gate 			}
117*0Sstevel@tonic-gate #endif
118*0Sstevel@tonic-gate 		}
119*0Sstevel@tonic-gate 	else
120*0Sstevel@tonic-gate 		{
121*0Sstevel@tonic-gate #ifdef DES_UNROLL
122*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,30); /* 16 */
123*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,28); /* 15 */
124*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,26); /* 14 */
125*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,24); /* 13 */
126*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,22); /* 12 */
127*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,20); /* 11 */
128*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,18); /* 10 */
129*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,16); /*  9 */
130*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,14); /*  8 */
131*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,12); /*  7 */
132*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,10); /*  6 */
133*0Sstevel@tonic-gate 		D_ENCRYPT(r,l, 8); /*  5 */
134*0Sstevel@tonic-gate 		D_ENCRYPT(l,r, 6); /*  4 */
135*0Sstevel@tonic-gate 		D_ENCRYPT(r,l, 4); /*  3 */
136*0Sstevel@tonic-gate 		D_ENCRYPT(l,r, 2); /*  2 */
137*0Sstevel@tonic-gate 		D_ENCRYPT(r,l, 0); /*  1 */
138*0Sstevel@tonic-gate #else
139*0Sstevel@tonic-gate 		for (i=30; i>0; i-=8)
140*0Sstevel@tonic-gate 			{
141*0Sstevel@tonic-gate 			D_ENCRYPT(l,r,i-0); /* 16 */
142*0Sstevel@tonic-gate 			D_ENCRYPT(r,l,i-2); /* 15 */
143*0Sstevel@tonic-gate 			D_ENCRYPT(l,r,i-4); /* 14 */
144*0Sstevel@tonic-gate 			D_ENCRYPT(r,l,i-6); /* 13 */
145*0Sstevel@tonic-gate 			}
146*0Sstevel@tonic-gate #endif
147*0Sstevel@tonic-gate 		}
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	/* rotate and clear the top bits on machines with 8byte longs */
150*0Sstevel@tonic-gate 	l=ROTATE(l,3)&0xffffffffL;
151*0Sstevel@tonic-gate 	r=ROTATE(r,3)&0xffffffffL;
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	FP(r,l);
154*0Sstevel@tonic-gate 	data[0]=l;
155*0Sstevel@tonic-gate 	data[1]=r;
156*0Sstevel@tonic-gate 	l=r=t=u=0;
157*0Sstevel@tonic-gate 	}
158*0Sstevel@tonic-gate 
DES_encrypt2(DES_LONG * data,DES_key_schedule * ks,int enc)159*0Sstevel@tonic-gate void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc)
160*0Sstevel@tonic-gate 	{
161*0Sstevel@tonic-gate 	register DES_LONG l,r,t,u;
162*0Sstevel@tonic-gate #ifdef DES_PTR
163*0Sstevel@tonic-gate 	register const unsigned char *des_SP=(const unsigned char *)DES_SPtrans;
164*0Sstevel@tonic-gate #endif
165*0Sstevel@tonic-gate #ifndef DES_UNROLL
166*0Sstevel@tonic-gate 	register int i;
167*0Sstevel@tonic-gate #endif
168*0Sstevel@tonic-gate 	register DES_LONG *s;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	r=data[0];
171*0Sstevel@tonic-gate 	l=data[1];
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	/* Things have been modified so that the initial rotate is
174*0Sstevel@tonic-gate 	 * done outside the loop.  This required the
175*0Sstevel@tonic-gate 	 * DES_SPtrans values in sp.h to be rotated 1 bit to the right.
176*0Sstevel@tonic-gate 	 * One perl script later and things have a 5% speed up on a sparc2.
177*0Sstevel@tonic-gate 	 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
178*0Sstevel@tonic-gate 	 * for pointing this out. */
179*0Sstevel@tonic-gate 	/* clear the top bits on machines with 8byte longs */
180*0Sstevel@tonic-gate 	r=ROTATE(r,29)&0xffffffffL;
181*0Sstevel@tonic-gate 	l=ROTATE(l,29)&0xffffffffL;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	s=ks->ks->deslong;
184*0Sstevel@tonic-gate 	/* I don't know if it is worth the effort of loop unrolling the
185*0Sstevel@tonic-gate 	 * inner loop */
186*0Sstevel@tonic-gate 	if (enc)
187*0Sstevel@tonic-gate 		{
188*0Sstevel@tonic-gate #ifdef DES_UNROLL
189*0Sstevel@tonic-gate 		D_ENCRYPT(l,r, 0); /*  1 */
190*0Sstevel@tonic-gate 		D_ENCRYPT(r,l, 2); /*  2 */
191*0Sstevel@tonic-gate 		D_ENCRYPT(l,r, 4); /*  3 */
192*0Sstevel@tonic-gate 		D_ENCRYPT(r,l, 6); /*  4 */
193*0Sstevel@tonic-gate 		D_ENCRYPT(l,r, 8); /*  5 */
194*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,10); /*  6 */
195*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,12); /*  7 */
196*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,14); /*  8 */
197*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,16); /*  9 */
198*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,18); /*  10 */
199*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,20); /*  11 */
200*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,22); /*  12 */
201*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,24); /*  13 */
202*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,26); /*  14 */
203*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,28); /*  15 */
204*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,30); /*  16 */
205*0Sstevel@tonic-gate #else
206*0Sstevel@tonic-gate 		for (i=0; i<32; i+=8)
207*0Sstevel@tonic-gate 			{
208*0Sstevel@tonic-gate 			D_ENCRYPT(l,r,i+0); /*  1 */
209*0Sstevel@tonic-gate 			D_ENCRYPT(r,l,i+2); /*  2 */
210*0Sstevel@tonic-gate 			D_ENCRYPT(l,r,i+4); /*  3 */
211*0Sstevel@tonic-gate 			D_ENCRYPT(r,l,i+6); /*  4 */
212*0Sstevel@tonic-gate 			}
213*0Sstevel@tonic-gate #endif
214*0Sstevel@tonic-gate 		}
215*0Sstevel@tonic-gate 	else
216*0Sstevel@tonic-gate 		{
217*0Sstevel@tonic-gate #ifdef DES_UNROLL
218*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,30); /* 16 */
219*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,28); /* 15 */
220*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,26); /* 14 */
221*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,24); /* 13 */
222*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,22); /* 12 */
223*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,20); /* 11 */
224*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,18); /* 10 */
225*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,16); /*  9 */
226*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,14); /*  8 */
227*0Sstevel@tonic-gate 		D_ENCRYPT(r,l,12); /*  7 */
228*0Sstevel@tonic-gate 		D_ENCRYPT(l,r,10); /*  6 */
229*0Sstevel@tonic-gate 		D_ENCRYPT(r,l, 8); /*  5 */
230*0Sstevel@tonic-gate 		D_ENCRYPT(l,r, 6); /*  4 */
231*0Sstevel@tonic-gate 		D_ENCRYPT(r,l, 4); /*  3 */
232*0Sstevel@tonic-gate 		D_ENCRYPT(l,r, 2); /*  2 */
233*0Sstevel@tonic-gate 		D_ENCRYPT(r,l, 0); /*  1 */
234*0Sstevel@tonic-gate #else
235*0Sstevel@tonic-gate 		for (i=30; i>0; i-=8)
236*0Sstevel@tonic-gate 			{
237*0Sstevel@tonic-gate 			D_ENCRYPT(l,r,i-0); /* 16 */
238*0Sstevel@tonic-gate 			D_ENCRYPT(r,l,i-2); /* 15 */
239*0Sstevel@tonic-gate 			D_ENCRYPT(l,r,i-4); /* 14 */
240*0Sstevel@tonic-gate 			D_ENCRYPT(r,l,i-6); /* 13 */
241*0Sstevel@tonic-gate 			}
242*0Sstevel@tonic-gate #endif
243*0Sstevel@tonic-gate 		}
244*0Sstevel@tonic-gate 	/* rotate and clear the top bits on machines with 8byte longs */
245*0Sstevel@tonic-gate 	data[0]=ROTATE(l,3)&0xffffffffL;
246*0Sstevel@tonic-gate 	data[1]=ROTATE(r,3)&0xffffffffL;
247*0Sstevel@tonic-gate 	l=r=t=u=0;
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate 
DES_encrypt3(DES_LONG * data,DES_key_schedule * ks1,DES_key_schedule * ks2,DES_key_schedule * ks3)250*0Sstevel@tonic-gate void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
251*0Sstevel@tonic-gate 		  DES_key_schedule *ks2, DES_key_schedule *ks3)
252*0Sstevel@tonic-gate 	{
253*0Sstevel@tonic-gate 	register DES_LONG l,r;
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	l=data[0];
256*0Sstevel@tonic-gate 	r=data[1];
257*0Sstevel@tonic-gate 	IP(l,r);
258*0Sstevel@tonic-gate 	data[0]=l;
259*0Sstevel@tonic-gate 	data[1]=r;
260*0Sstevel@tonic-gate 	DES_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT);
261*0Sstevel@tonic-gate 	DES_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT);
262*0Sstevel@tonic-gate 	DES_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT);
263*0Sstevel@tonic-gate 	l=data[0];
264*0Sstevel@tonic-gate 	r=data[1];
265*0Sstevel@tonic-gate 	FP(r,l);
266*0Sstevel@tonic-gate 	data[0]=l;
267*0Sstevel@tonic-gate 	data[1]=r;
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate 
DES_decrypt3(DES_LONG * data,DES_key_schedule * ks1,DES_key_schedule * ks2,DES_key_schedule * ks3)270*0Sstevel@tonic-gate void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
271*0Sstevel@tonic-gate 		  DES_key_schedule *ks2, DES_key_schedule *ks3)
272*0Sstevel@tonic-gate 	{
273*0Sstevel@tonic-gate 	register DES_LONG l,r;
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	l=data[0];
276*0Sstevel@tonic-gate 	r=data[1];
277*0Sstevel@tonic-gate 	IP(l,r);
278*0Sstevel@tonic-gate 	data[0]=l;
279*0Sstevel@tonic-gate 	data[1]=r;
280*0Sstevel@tonic-gate 	DES_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT);
281*0Sstevel@tonic-gate 	DES_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT);
282*0Sstevel@tonic-gate 	DES_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT);
283*0Sstevel@tonic-gate 	l=data[0];
284*0Sstevel@tonic-gate 	r=data[1];
285*0Sstevel@tonic-gate 	FP(r,l);
286*0Sstevel@tonic-gate 	data[0]=l;
287*0Sstevel@tonic-gate 	data[1]=r;
288*0Sstevel@tonic-gate 	}
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate #ifndef DES_DEFAULT_OPTIONS
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate #undef CBC_ENC_C__DONT_UPDATE_IV
293*0Sstevel@tonic-gate #include "ncbc_enc.c" /* DES_ncbc_encrypt */
294*0Sstevel@tonic-gate 
DES_ede3_cbc_encrypt(const unsigned char * input,unsigned char * output,long length,DES_key_schedule * ks1,DES_key_schedule * ks2,DES_key_schedule * ks3,DES_cblock * ivec,int enc)295*0Sstevel@tonic-gate void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
296*0Sstevel@tonic-gate 			  long length, DES_key_schedule *ks1,
297*0Sstevel@tonic-gate 			  DES_key_schedule *ks2, DES_key_schedule *ks3,
298*0Sstevel@tonic-gate 			  DES_cblock *ivec, int enc)
299*0Sstevel@tonic-gate 	{
300*0Sstevel@tonic-gate 	register DES_LONG tin0,tin1;
301*0Sstevel@tonic-gate 	register DES_LONG tout0,tout1,xor0,xor1;
302*0Sstevel@tonic-gate 	register const unsigned char *in;
303*0Sstevel@tonic-gate 	unsigned char *out;
304*0Sstevel@tonic-gate 	register long l=length;
305*0Sstevel@tonic-gate 	DES_LONG tin[2];
306*0Sstevel@tonic-gate 	unsigned char *iv;
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	in=input;
309*0Sstevel@tonic-gate 	out=output;
310*0Sstevel@tonic-gate 	iv = &(*ivec)[0];
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	if (enc)
313*0Sstevel@tonic-gate 		{
314*0Sstevel@tonic-gate 		c2l(iv,tout0);
315*0Sstevel@tonic-gate 		c2l(iv,tout1);
316*0Sstevel@tonic-gate 		for (l-=8; l>=0; l-=8)
317*0Sstevel@tonic-gate 			{
318*0Sstevel@tonic-gate 			c2l(in,tin0);
319*0Sstevel@tonic-gate 			c2l(in,tin1);
320*0Sstevel@tonic-gate 			tin0^=tout0;
321*0Sstevel@tonic-gate 			tin1^=tout1;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 			tin[0]=tin0;
324*0Sstevel@tonic-gate 			tin[1]=tin1;
325*0Sstevel@tonic-gate 			DES_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
326*0Sstevel@tonic-gate 			tout0=tin[0];
327*0Sstevel@tonic-gate 			tout1=tin[1];
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 			l2c(tout0,out);
330*0Sstevel@tonic-gate 			l2c(tout1,out);
331*0Sstevel@tonic-gate 			}
332*0Sstevel@tonic-gate 		if (l != -8)
333*0Sstevel@tonic-gate 			{
334*0Sstevel@tonic-gate 			c2ln(in,tin0,tin1,l+8);
335*0Sstevel@tonic-gate 			tin0^=tout0;
336*0Sstevel@tonic-gate 			tin1^=tout1;
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 			tin[0]=tin0;
339*0Sstevel@tonic-gate 			tin[1]=tin1;
340*0Sstevel@tonic-gate 			DES_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
341*0Sstevel@tonic-gate 			tout0=tin[0];
342*0Sstevel@tonic-gate 			tout1=tin[1];
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 			l2c(tout0,out);
345*0Sstevel@tonic-gate 			l2c(tout1,out);
346*0Sstevel@tonic-gate 			}
347*0Sstevel@tonic-gate 		iv = &(*ivec)[0];
348*0Sstevel@tonic-gate 		l2c(tout0,iv);
349*0Sstevel@tonic-gate 		l2c(tout1,iv);
350*0Sstevel@tonic-gate 		}
351*0Sstevel@tonic-gate 	else
352*0Sstevel@tonic-gate 		{
353*0Sstevel@tonic-gate 		register DES_LONG t0,t1;
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 		c2l(iv,xor0);
356*0Sstevel@tonic-gate 		c2l(iv,xor1);
357*0Sstevel@tonic-gate 		for (l-=8; l>=0; l-=8)
358*0Sstevel@tonic-gate 			{
359*0Sstevel@tonic-gate 			c2l(in,tin0);
360*0Sstevel@tonic-gate 			c2l(in,tin1);
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 			t0=tin0;
363*0Sstevel@tonic-gate 			t1=tin1;
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 			tin[0]=tin0;
366*0Sstevel@tonic-gate 			tin[1]=tin1;
367*0Sstevel@tonic-gate 			DES_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
368*0Sstevel@tonic-gate 			tout0=tin[0];
369*0Sstevel@tonic-gate 			tout1=tin[1];
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 			tout0^=xor0;
372*0Sstevel@tonic-gate 			tout1^=xor1;
373*0Sstevel@tonic-gate 			l2c(tout0,out);
374*0Sstevel@tonic-gate 			l2c(tout1,out);
375*0Sstevel@tonic-gate 			xor0=t0;
376*0Sstevel@tonic-gate 			xor1=t1;
377*0Sstevel@tonic-gate 			}
378*0Sstevel@tonic-gate 		if (l != -8)
379*0Sstevel@tonic-gate 			{
380*0Sstevel@tonic-gate 			c2l(in,tin0);
381*0Sstevel@tonic-gate 			c2l(in,tin1);
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 			t0=tin0;
384*0Sstevel@tonic-gate 			t1=tin1;
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 			tin[0]=tin0;
387*0Sstevel@tonic-gate 			tin[1]=tin1;
388*0Sstevel@tonic-gate 			DES_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
389*0Sstevel@tonic-gate 			tout0=tin[0];
390*0Sstevel@tonic-gate 			tout1=tin[1];
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 			tout0^=xor0;
393*0Sstevel@tonic-gate 			tout1^=xor1;
394*0Sstevel@tonic-gate 			l2cn(tout0,tout1,out,l+8);
395*0Sstevel@tonic-gate 			xor0=t0;
396*0Sstevel@tonic-gate 			xor1=t1;
397*0Sstevel@tonic-gate 			}
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 		iv = &(*ivec)[0];
400*0Sstevel@tonic-gate 		l2c(xor0,iv);
401*0Sstevel@tonic-gate 		l2c(xor1,iv);
402*0Sstevel@tonic-gate 		}
403*0Sstevel@tonic-gate 	tin0=tin1=tout0=tout1=xor0=xor1=0;
404*0Sstevel@tonic-gate 	tin[0]=tin[1]=0;
405*0Sstevel@tonic-gate 	}
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate #endif /* DES_DEFAULT_OPTIONS */
408