xref: /onnv-gate/usr/src/common/openssl/crypto/des/des.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/des/des.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 <stdio.h>
60*0Sstevel@tonic-gate #include <stdlib.h>
61*0Sstevel@tonic-gate #include <string.h>
62*0Sstevel@tonic-gate #include <openssl/opensslconf.h>
63*0Sstevel@tonic-gate #ifndef OPENSSL_SYS_MSDOS
64*0Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
65*0Sstevel@tonic-gate #include OPENSSL_UNISTD
66*0Sstevel@tonic-gate #else /* OPENSSL_SYS_VMS */
67*0Sstevel@tonic-gate #ifdef __DECC
68*0Sstevel@tonic-gate #include <unistd.h>
69*0Sstevel@tonic-gate #else /* not __DECC */
70*0Sstevel@tonic-gate #include <math.h>
71*0Sstevel@tonic-gate #endif /* __DECC */
72*0Sstevel@tonic-gate #endif /* OPENSSL_SYS_VMS */
73*0Sstevel@tonic-gate #else /* OPENSSL_SYS_MSDOS */
74*0Sstevel@tonic-gate #include <io.h>
75*0Sstevel@tonic-gate #endif
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate #include <time.h>
78*0Sstevel@tonic-gate #include "des_ver.h"
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
81*0Sstevel@tonic-gate #include <types.h>
82*0Sstevel@tonic-gate #include <stat.h>
83*0Sstevel@tonic-gate #else
84*0Sstevel@tonic-gate #ifndef _IRIX
85*0Sstevel@tonic-gate #include <sys/types.h>
86*0Sstevel@tonic-gate #endif
87*0Sstevel@tonic-gate #include <sys/stat.h>
88*0Sstevel@tonic-gate #endif
89*0Sstevel@tonic-gate #include <openssl/des.h>
90*0Sstevel@tonic-gate #include <openssl/rand.h>
91*0Sstevel@tonic-gate #include <openssl/ui_compat.h>
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate void usage(void);
94*0Sstevel@tonic-gate void doencryption(void);
95*0Sstevel@tonic-gate int uufwrite(unsigned char *data, int size, unsigned int num, FILE *fp);
96*0Sstevel@tonic-gate void uufwriteEnd(FILE *fp);
97*0Sstevel@tonic-gate int uufread(unsigned char *out,int size,unsigned int num,FILE *fp);
98*0Sstevel@tonic-gate int uuencode(unsigned char *in,int num,unsigned char *out);
99*0Sstevel@tonic-gate int uudecode(unsigned char *in,int num,unsigned char *out);
100*0Sstevel@tonic-gate void DES_3cbc_encrypt(DES_cblock *input,DES_cblock *output,long length,
101*0Sstevel@tonic-gate 	DES_key_schedule sk1,DES_key_schedule sk2,
102*0Sstevel@tonic-gate 	DES_cblock *ivec1,DES_cblock *ivec2,int enc);
103*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
104*0Sstevel@tonic-gate #define EXIT(a) exit(a&0x10000000L)
105*0Sstevel@tonic-gate #else
106*0Sstevel@tonic-gate #define EXIT(a) exit(a)
107*0Sstevel@tonic-gate #endif
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate #define BUFSIZE (8*1024)
110*0Sstevel@tonic-gate #define VERIFY  1
111*0Sstevel@tonic-gate #define KEYSIZ	8
112*0Sstevel@tonic-gate #define KEYSIZB 1024 /* should hit tty line limit first :-) */
113*0Sstevel@tonic-gate char key[KEYSIZB+1];
114*0Sstevel@tonic-gate int do_encrypt,longk=0;
115*0Sstevel@tonic-gate FILE *DES_IN,*DES_OUT,*CKSUM_OUT;
116*0Sstevel@tonic-gate char uuname[200];
117*0Sstevel@tonic-gate unsigned char uubuf[50];
118*0Sstevel@tonic-gate int uubufnum=0;
119*0Sstevel@tonic-gate #define INUUBUFN	(45*100)
120*0Sstevel@tonic-gate #define OUTUUBUF	(65*100)
121*0Sstevel@tonic-gate unsigned char b[OUTUUBUF];
122*0Sstevel@tonic-gate unsigned char bb[300];
123*0Sstevel@tonic-gate DES_cblock cksum={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
124*0Sstevel@tonic-gate char cksumname[200]="";
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate int vflag,cflag,eflag,dflag,kflag,bflag,fflag,sflag,uflag,flag3,hflag,error;
127*0Sstevel@tonic-gate 
main(int argc,char ** argv)128*0Sstevel@tonic-gate int main(int argc, char **argv)
129*0Sstevel@tonic-gate 	{
130*0Sstevel@tonic-gate 	int i;
131*0Sstevel@tonic-gate 	struct stat ins,outs;
132*0Sstevel@tonic-gate 	char *p;
133*0Sstevel@tonic-gate 	char *in=NULL,*out=NULL;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	vflag=cflag=eflag=dflag=kflag=hflag=bflag=fflag=sflag=uflag=flag3=0;
136*0Sstevel@tonic-gate 	error=0;
137*0Sstevel@tonic-gate 	memset(key,0,sizeof(key));
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	for (i=1; i<argc; i++)
140*0Sstevel@tonic-gate 		{
141*0Sstevel@tonic-gate 		p=argv[i];
142*0Sstevel@tonic-gate 		if ((p[0] == '-') && (p[1] != '\0'))
143*0Sstevel@tonic-gate 			{
144*0Sstevel@tonic-gate 			p++;
145*0Sstevel@tonic-gate 			while (*p)
146*0Sstevel@tonic-gate 				{
147*0Sstevel@tonic-gate 				switch (*(p++))
148*0Sstevel@tonic-gate 					{
149*0Sstevel@tonic-gate 				case '3':
150*0Sstevel@tonic-gate 					flag3=1;
151*0Sstevel@tonic-gate 					longk=1;
152*0Sstevel@tonic-gate 					break;
153*0Sstevel@tonic-gate 				case 'c':
154*0Sstevel@tonic-gate 					cflag=1;
155*0Sstevel@tonic-gate 					strncpy(cksumname,p,200);
156*0Sstevel@tonic-gate 					cksumname[sizeof(cksumname)-1]='\0';
157*0Sstevel@tonic-gate 					p+=strlen(cksumname);
158*0Sstevel@tonic-gate 					break;
159*0Sstevel@tonic-gate 				case 'C':
160*0Sstevel@tonic-gate 					cflag=1;
161*0Sstevel@tonic-gate 					longk=1;
162*0Sstevel@tonic-gate 					strncpy(cksumname,p,200);
163*0Sstevel@tonic-gate 					cksumname[sizeof(cksumname)-1]='\0';
164*0Sstevel@tonic-gate 					p+=strlen(cksumname);
165*0Sstevel@tonic-gate 					break;
166*0Sstevel@tonic-gate 				case 'e':
167*0Sstevel@tonic-gate 					eflag=1;
168*0Sstevel@tonic-gate 					break;
169*0Sstevel@tonic-gate 				case 'v':
170*0Sstevel@tonic-gate 					vflag=1;
171*0Sstevel@tonic-gate 					break;
172*0Sstevel@tonic-gate 				case 'E':
173*0Sstevel@tonic-gate 					eflag=1;
174*0Sstevel@tonic-gate 					longk=1;
175*0Sstevel@tonic-gate 					break;
176*0Sstevel@tonic-gate 				case 'd':
177*0Sstevel@tonic-gate 					dflag=1;
178*0Sstevel@tonic-gate 					break;
179*0Sstevel@tonic-gate 				case 'D':
180*0Sstevel@tonic-gate 					dflag=1;
181*0Sstevel@tonic-gate 					longk=1;
182*0Sstevel@tonic-gate 					break;
183*0Sstevel@tonic-gate 				case 'b':
184*0Sstevel@tonic-gate 					bflag=1;
185*0Sstevel@tonic-gate 					break;
186*0Sstevel@tonic-gate 				case 'f':
187*0Sstevel@tonic-gate 					fflag=1;
188*0Sstevel@tonic-gate 					break;
189*0Sstevel@tonic-gate 				case 's':
190*0Sstevel@tonic-gate 					sflag=1;
191*0Sstevel@tonic-gate 					break;
192*0Sstevel@tonic-gate 				case 'u':
193*0Sstevel@tonic-gate 					uflag=1;
194*0Sstevel@tonic-gate 					strncpy(uuname,p,200);
195*0Sstevel@tonic-gate 					uuname[sizeof(uuname)-1]='\0';
196*0Sstevel@tonic-gate 					p+=strlen(uuname);
197*0Sstevel@tonic-gate 					break;
198*0Sstevel@tonic-gate 				case 'h':
199*0Sstevel@tonic-gate 					hflag=1;
200*0Sstevel@tonic-gate 					break;
201*0Sstevel@tonic-gate 				case 'k':
202*0Sstevel@tonic-gate 					kflag=1;
203*0Sstevel@tonic-gate 					if ((i+1) == argc)
204*0Sstevel@tonic-gate 						{
205*0Sstevel@tonic-gate 						fputs("must have a key with the -k option\n",stderr);
206*0Sstevel@tonic-gate 						error=1;
207*0Sstevel@tonic-gate 						}
208*0Sstevel@tonic-gate 					else
209*0Sstevel@tonic-gate 						{
210*0Sstevel@tonic-gate 						int j;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 						i++;
213*0Sstevel@tonic-gate 						strncpy(key,argv[i],KEYSIZB);
214*0Sstevel@tonic-gate 						for (j=strlen(argv[i])-1; j>=0; j--)
215*0Sstevel@tonic-gate 							argv[i][j]='\0';
216*0Sstevel@tonic-gate 						}
217*0Sstevel@tonic-gate 					break;
218*0Sstevel@tonic-gate 				default:
219*0Sstevel@tonic-gate 					fprintf(stderr,"'%c' unknown flag\n",p[-1]);
220*0Sstevel@tonic-gate 					error=1;
221*0Sstevel@tonic-gate 					break;
222*0Sstevel@tonic-gate 					}
223*0Sstevel@tonic-gate 				}
224*0Sstevel@tonic-gate 			}
225*0Sstevel@tonic-gate 		else
226*0Sstevel@tonic-gate 			{
227*0Sstevel@tonic-gate 			if (in == NULL)
228*0Sstevel@tonic-gate 				in=argv[i];
229*0Sstevel@tonic-gate 			else if (out == NULL)
230*0Sstevel@tonic-gate 				out=argv[i];
231*0Sstevel@tonic-gate 			else
232*0Sstevel@tonic-gate 				error=1;
233*0Sstevel@tonic-gate 			}
234*0Sstevel@tonic-gate 		}
235*0Sstevel@tonic-gate 	if (error) usage();
236*0Sstevel@tonic-gate 	/* We either
237*0Sstevel@tonic-gate 	 * do checksum or
238*0Sstevel@tonic-gate 	 * do encrypt or
239*0Sstevel@tonic-gate 	 * do decrypt or
240*0Sstevel@tonic-gate 	 * do decrypt then ckecksum or
241*0Sstevel@tonic-gate 	 * do checksum then encrypt
242*0Sstevel@tonic-gate 	 */
243*0Sstevel@tonic-gate 	if (((eflag+dflag) == 1) || cflag)
244*0Sstevel@tonic-gate 		{
245*0Sstevel@tonic-gate 		if (eflag) do_encrypt=DES_ENCRYPT;
246*0Sstevel@tonic-gate 		if (dflag) do_encrypt=DES_DECRYPT;
247*0Sstevel@tonic-gate 		}
248*0Sstevel@tonic-gate 	else
249*0Sstevel@tonic-gate 		{
250*0Sstevel@tonic-gate 		if (vflag)
251*0Sstevel@tonic-gate 			{
252*0Sstevel@tonic-gate #ifndef _Windows
253*0Sstevel@tonic-gate 			fprintf(stderr,"des(1) built with %s\n",libdes_version);
254*0Sstevel@tonic-gate #endif
255*0Sstevel@tonic-gate 			EXIT(1);
256*0Sstevel@tonic-gate 			}
257*0Sstevel@tonic-gate 		else usage();
258*0Sstevel@tonic-gate 		}
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate #ifndef _Windows
261*0Sstevel@tonic-gate 	if (vflag) fprintf(stderr,"des(1) built with %s\n",libdes_version);
262*0Sstevel@tonic-gate #endif
263*0Sstevel@tonic-gate 	if (	(in != NULL) &&
264*0Sstevel@tonic-gate 		(out != NULL) &&
265*0Sstevel@tonic-gate #ifndef OPENSSL_SYS_MSDOS
266*0Sstevel@tonic-gate 		(stat(in,&ins) != -1) &&
267*0Sstevel@tonic-gate 		(stat(out,&outs) != -1) &&
268*0Sstevel@tonic-gate 		(ins.st_dev == outs.st_dev) &&
269*0Sstevel@tonic-gate 		(ins.st_ino == outs.st_ino))
270*0Sstevel@tonic-gate #else /* OPENSSL_SYS_MSDOS */
271*0Sstevel@tonic-gate 		(strcmp(in,out) == 0))
272*0Sstevel@tonic-gate #endif
273*0Sstevel@tonic-gate 			{
274*0Sstevel@tonic-gate 			fputs("input and output file are the same\n",stderr);
275*0Sstevel@tonic-gate 			EXIT(3);
276*0Sstevel@tonic-gate 			}
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	if (!kflag)
279*0Sstevel@tonic-gate 		if (des_read_pw_string(key,KEYSIZB+1,"Enter key:",eflag?VERIFY:0))
280*0Sstevel@tonic-gate 			{
281*0Sstevel@tonic-gate 			fputs("password error\n",stderr);
282*0Sstevel@tonic-gate 			EXIT(2);
283*0Sstevel@tonic-gate 			}
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	if (in == NULL)
286*0Sstevel@tonic-gate 		DES_IN=stdin;
287*0Sstevel@tonic-gate 	else if ((DES_IN=fopen(in,"r")) == NULL)
288*0Sstevel@tonic-gate 		{
289*0Sstevel@tonic-gate 		perror("opening input file");
290*0Sstevel@tonic-gate 		EXIT(4);
291*0Sstevel@tonic-gate 		}
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	CKSUM_OUT=stdout;
294*0Sstevel@tonic-gate 	if (out == NULL)
295*0Sstevel@tonic-gate 		{
296*0Sstevel@tonic-gate 		DES_OUT=stdout;
297*0Sstevel@tonic-gate 		CKSUM_OUT=stderr;
298*0Sstevel@tonic-gate 		}
299*0Sstevel@tonic-gate 	else if ((DES_OUT=fopen(out,"w")) == NULL)
300*0Sstevel@tonic-gate 		{
301*0Sstevel@tonic-gate 		perror("opening output file");
302*0Sstevel@tonic-gate 		EXIT(5);
303*0Sstevel@tonic-gate 		}
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_MSDOS
306*0Sstevel@tonic-gate 	/* This should set the file to binary mode. */
307*0Sstevel@tonic-gate 	{
308*0Sstevel@tonic-gate #include <fcntl.h>
309*0Sstevel@tonic-gate 	if (!(uflag && dflag))
310*0Sstevel@tonic-gate 		setmode(fileno(DES_IN),O_BINARY);
311*0Sstevel@tonic-gate 	if (!(uflag && eflag))
312*0Sstevel@tonic-gate 		setmode(fileno(DES_OUT),O_BINARY);
313*0Sstevel@tonic-gate 	}
314*0Sstevel@tonic-gate #endif
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	doencryption();
317*0Sstevel@tonic-gate 	fclose(DES_IN);
318*0Sstevel@tonic-gate 	fclose(DES_OUT);
319*0Sstevel@tonic-gate 	EXIT(0);
320*0Sstevel@tonic-gate 	}
321*0Sstevel@tonic-gate 
usage(void)322*0Sstevel@tonic-gate void usage(void)
323*0Sstevel@tonic-gate 	{
324*0Sstevel@tonic-gate 	char **u;
325*0Sstevel@tonic-gate 	static const char *Usage[]={
326*0Sstevel@tonic-gate "des <options> [input-file [output-file]]",
327*0Sstevel@tonic-gate "options:",
328*0Sstevel@tonic-gate "-v         : des(1) version number",
329*0Sstevel@tonic-gate "-e         : encrypt using SunOS compatible user key to DES key conversion.",
330*0Sstevel@tonic-gate "-E         : encrypt ",
331*0Sstevel@tonic-gate "-d         : decrypt using SunOS compatible user key to DES key conversion.",
332*0Sstevel@tonic-gate "-D         : decrypt ",
333*0Sstevel@tonic-gate "-c[ckname] : generate a cbc_cksum using SunOS compatible user key to",
334*0Sstevel@tonic-gate "             DES key conversion and output to ckname (stdout default,",
335*0Sstevel@tonic-gate "             stderr if data being output on stdout).  The checksum is",
336*0Sstevel@tonic-gate "             generated before encryption and after decryption if used",
337*0Sstevel@tonic-gate "             in conjunction with -[eEdD].",
338*0Sstevel@tonic-gate "-C[ckname] : generate a cbc_cksum as for -c but compatible with -[ED].",
339*0Sstevel@tonic-gate "-k key     : use key 'key'",
340*0Sstevel@tonic-gate "-h         : the key that is entered will be a hexadecimal number",
341*0Sstevel@tonic-gate "             that is used directly as the des key",
342*0Sstevel@tonic-gate "-u[uuname] : input file is uudecoded if -[dD] or output uuencoded data if -[eE]",
343*0Sstevel@tonic-gate "             (uuname is the filename to put in the uuencode header).",
344*0Sstevel@tonic-gate "-b         : encrypt using DES in ecb encryption mode, the default is cbc mode.",
345*0Sstevel@tonic-gate "-3         : encrypt using triple DES encryption.  This uses 2 keys",
346*0Sstevel@tonic-gate "             generated from the input key.  If the input key is less",
347*0Sstevel@tonic-gate "             than 8 characters long, this is equivalent to normal",
348*0Sstevel@tonic-gate "             encryption.  Default is triple cbc, -b makes it triple ecb.",
349*0Sstevel@tonic-gate NULL
350*0Sstevel@tonic-gate };
351*0Sstevel@tonic-gate 	for (u=(char **)Usage; *u; u++)
352*0Sstevel@tonic-gate 		{
353*0Sstevel@tonic-gate 		fputs(*u,stderr);
354*0Sstevel@tonic-gate 		fputc('\n',stderr);
355*0Sstevel@tonic-gate 		}
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	EXIT(1);
358*0Sstevel@tonic-gate 	}
359*0Sstevel@tonic-gate 
doencryption(void)360*0Sstevel@tonic-gate void doencryption(void)
361*0Sstevel@tonic-gate 	{
362*0Sstevel@tonic-gate #ifdef _LIBC
363*0Sstevel@tonic-gate 	extern unsigned long time();
364*0Sstevel@tonic-gate #endif
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 	register int i;
367*0Sstevel@tonic-gate 	DES_key_schedule ks,ks2;
368*0Sstevel@tonic-gate 	DES_cblock iv,iv2;
369*0Sstevel@tonic-gate 	char *p;
370*0Sstevel@tonic-gate 	int num=0,j,k,l,rem,ll,len,last,ex=0;
371*0Sstevel@tonic-gate 	DES_cblock kk,k2;
372*0Sstevel@tonic-gate 	FILE *O;
373*0Sstevel@tonic-gate 	int Exit=0;
374*0Sstevel@tonic-gate #ifndef OPENSSL_SYS_MSDOS
375*0Sstevel@tonic-gate 	static unsigned char buf[BUFSIZE+8],obuf[BUFSIZE+8];
376*0Sstevel@tonic-gate #else
377*0Sstevel@tonic-gate 	static unsigned char *buf=NULL,*obuf=NULL;
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	if (buf == NULL)
380*0Sstevel@tonic-gate 		{
381*0Sstevel@tonic-gate 		if (    (( buf=OPENSSL_malloc(BUFSIZE+8)) == NULL) ||
382*0Sstevel@tonic-gate 			((obuf=OPENSSL_malloc(BUFSIZE+8)) == NULL))
383*0Sstevel@tonic-gate 			{
384*0Sstevel@tonic-gate 			fputs("Not enough memory\n",stderr);
385*0Sstevel@tonic-gate 			Exit=10;
386*0Sstevel@tonic-gate 			goto problems;
387*0Sstevel@tonic-gate 			}
388*0Sstevel@tonic-gate 		}
389*0Sstevel@tonic-gate #endif
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	if (hflag)
392*0Sstevel@tonic-gate 		{
393*0Sstevel@tonic-gate 		j=(flag3?16:8);
394*0Sstevel@tonic-gate 		p=key;
395*0Sstevel@tonic-gate 		for (i=0; i<j; i++)
396*0Sstevel@tonic-gate 			{
397*0Sstevel@tonic-gate 			k=0;
398*0Sstevel@tonic-gate 			if ((*p <= '9') && (*p >= '0'))
399*0Sstevel@tonic-gate 				k=(*p-'0')<<4;
400*0Sstevel@tonic-gate 			else if ((*p <= 'f') && (*p >= 'a'))
401*0Sstevel@tonic-gate 				k=(*p-'a'+10)<<4;
402*0Sstevel@tonic-gate 			else if ((*p <= 'F') && (*p >= 'A'))
403*0Sstevel@tonic-gate 				k=(*p-'A'+10)<<4;
404*0Sstevel@tonic-gate 			else
405*0Sstevel@tonic-gate 				{
406*0Sstevel@tonic-gate 				fputs("Bad hex key\n",stderr);
407*0Sstevel@tonic-gate 				Exit=9;
408*0Sstevel@tonic-gate 				goto problems;
409*0Sstevel@tonic-gate 				}
410*0Sstevel@tonic-gate 			p++;
411*0Sstevel@tonic-gate 			if ((*p <= '9') && (*p >= '0'))
412*0Sstevel@tonic-gate 				k|=(*p-'0');
413*0Sstevel@tonic-gate 			else if ((*p <= 'f') && (*p >= 'a'))
414*0Sstevel@tonic-gate 				k|=(*p-'a'+10);
415*0Sstevel@tonic-gate 			else if ((*p <= 'F') && (*p >= 'A'))
416*0Sstevel@tonic-gate 				k|=(*p-'A'+10);
417*0Sstevel@tonic-gate 			else
418*0Sstevel@tonic-gate 				{
419*0Sstevel@tonic-gate 				fputs("Bad hex key\n",stderr);
420*0Sstevel@tonic-gate 				Exit=9;
421*0Sstevel@tonic-gate 				goto problems;
422*0Sstevel@tonic-gate 				}
423*0Sstevel@tonic-gate 			p++;
424*0Sstevel@tonic-gate 			if (i < 8)
425*0Sstevel@tonic-gate 				kk[i]=k;
426*0Sstevel@tonic-gate 			else
427*0Sstevel@tonic-gate 				k2[i-8]=k;
428*0Sstevel@tonic-gate 			}
429*0Sstevel@tonic-gate 		DES_set_key_unchecked(&k2,&ks2);
430*0Sstevel@tonic-gate 		OPENSSL_cleanse(k2,sizeof(k2));
431*0Sstevel@tonic-gate 		}
432*0Sstevel@tonic-gate 	else if (longk || flag3)
433*0Sstevel@tonic-gate 		{
434*0Sstevel@tonic-gate 		if (flag3)
435*0Sstevel@tonic-gate 			{
436*0Sstevel@tonic-gate 			DES_string_to_2keys(key,&kk,&k2);
437*0Sstevel@tonic-gate 			DES_set_key_unchecked(&k2,&ks2);
438*0Sstevel@tonic-gate 			OPENSSL_cleanse(k2,sizeof(k2));
439*0Sstevel@tonic-gate 			}
440*0Sstevel@tonic-gate 		else
441*0Sstevel@tonic-gate 			DES_string_to_key(key,&kk);
442*0Sstevel@tonic-gate 		}
443*0Sstevel@tonic-gate 	else
444*0Sstevel@tonic-gate 		for (i=0; i<KEYSIZ; i++)
445*0Sstevel@tonic-gate 			{
446*0Sstevel@tonic-gate 			l=0;
447*0Sstevel@tonic-gate 			k=key[i];
448*0Sstevel@tonic-gate 			for (j=0; j<8; j++)
449*0Sstevel@tonic-gate 				{
450*0Sstevel@tonic-gate 				if (k&1) l++;
451*0Sstevel@tonic-gate 				k>>=1;
452*0Sstevel@tonic-gate 				}
453*0Sstevel@tonic-gate 			if (l & 1)
454*0Sstevel@tonic-gate 				kk[i]=key[i]&0x7f;
455*0Sstevel@tonic-gate 			else
456*0Sstevel@tonic-gate 				kk[i]=key[i]|0x80;
457*0Sstevel@tonic-gate 			}
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 	DES_set_key_unchecked(&kk,&ks);
460*0Sstevel@tonic-gate 	OPENSSL_cleanse(key,sizeof(key));
461*0Sstevel@tonic-gate 	OPENSSL_cleanse(kk,sizeof(kk));
462*0Sstevel@tonic-gate 	/* woops - A bug that does not showup under unix :-( */
463*0Sstevel@tonic-gate 	memset(iv,0,sizeof(iv));
464*0Sstevel@tonic-gate 	memset(iv2,0,sizeof(iv2));
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 	l=1;
467*0Sstevel@tonic-gate 	rem=0;
468*0Sstevel@tonic-gate 	/* first read */
469*0Sstevel@tonic-gate 	if (eflag || (!dflag && cflag))
470*0Sstevel@tonic-gate 		{
471*0Sstevel@tonic-gate 		for (;;)
472*0Sstevel@tonic-gate 			{
473*0Sstevel@tonic-gate 			num=l=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
474*0Sstevel@tonic-gate 			l+=rem;
475*0Sstevel@tonic-gate 			num+=rem;
476*0Sstevel@tonic-gate 			if (l < 0)
477*0Sstevel@tonic-gate 				{
478*0Sstevel@tonic-gate 				perror("read error");
479*0Sstevel@tonic-gate 				Exit=6;
480*0Sstevel@tonic-gate 				goto problems;
481*0Sstevel@tonic-gate 				}
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 			rem=l%8;
484*0Sstevel@tonic-gate 			len=l-rem;
485*0Sstevel@tonic-gate 			if (feof(DES_IN))
486*0Sstevel@tonic-gate 				{
487*0Sstevel@tonic-gate 				for (i=7-rem; i>0; i--)
488*0Sstevel@tonic-gate 					RAND_pseudo_bytes(buf + l++, 1);
489*0Sstevel@tonic-gate 				buf[l++]=rem;
490*0Sstevel@tonic-gate 				ex=1;
491*0Sstevel@tonic-gate 				len+=rem;
492*0Sstevel@tonic-gate 				}
493*0Sstevel@tonic-gate 			else
494*0Sstevel@tonic-gate 				l-=rem;
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 			if (cflag)
497*0Sstevel@tonic-gate 				{
498*0Sstevel@tonic-gate 				DES_cbc_cksum(buf,&cksum,
499*0Sstevel@tonic-gate 					(long)len,&ks,&cksum);
500*0Sstevel@tonic-gate 				if (!eflag)
501*0Sstevel@tonic-gate 					{
502*0Sstevel@tonic-gate 					if (feof(DES_IN)) break;
503*0Sstevel@tonic-gate 					else continue;
504*0Sstevel@tonic-gate 					}
505*0Sstevel@tonic-gate 				}
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 			if (bflag && !flag3)
508*0Sstevel@tonic-gate 				for (i=0; i<l; i+=8)
509*0Sstevel@tonic-gate 					DES_ecb_encrypt(
510*0Sstevel@tonic-gate 						(DES_cblock *)&(buf[i]),
511*0Sstevel@tonic-gate 						(DES_cblock *)&(obuf[i]),
512*0Sstevel@tonic-gate 						&ks,do_encrypt);
513*0Sstevel@tonic-gate 			else if (flag3 && bflag)
514*0Sstevel@tonic-gate 				for (i=0; i<l; i+=8)
515*0Sstevel@tonic-gate 					DES_ecb2_encrypt(
516*0Sstevel@tonic-gate 						(DES_cblock *)&(buf[i]),
517*0Sstevel@tonic-gate 						(DES_cblock *)&(obuf[i]),
518*0Sstevel@tonic-gate 						&ks,&ks2,do_encrypt);
519*0Sstevel@tonic-gate 			else if (flag3 && !bflag)
520*0Sstevel@tonic-gate 				{
521*0Sstevel@tonic-gate 				char tmpbuf[8];
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 				if (rem) memcpy(tmpbuf,&(buf[l]),
524*0Sstevel@tonic-gate 					(unsigned int)rem);
525*0Sstevel@tonic-gate 				DES_3cbc_encrypt(
526*0Sstevel@tonic-gate 					(DES_cblock *)buf,(DES_cblock *)obuf,
527*0Sstevel@tonic-gate 					(long)l,ks,ks2,&iv,
528*0Sstevel@tonic-gate 					&iv2,do_encrypt);
529*0Sstevel@tonic-gate 				if (rem) memcpy(&(buf[l]),tmpbuf,
530*0Sstevel@tonic-gate 					(unsigned int)rem);
531*0Sstevel@tonic-gate 				}
532*0Sstevel@tonic-gate 			else
533*0Sstevel@tonic-gate 				{
534*0Sstevel@tonic-gate 				DES_cbc_encrypt(
535*0Sstevel@tonic-gate 					buf,obuf,
536*0Sstevel@tonic-gate 					(long)l,&ks,&iv,do_encrypt);
537*0Sstevel@tonic-gate 				if (l >= 8) memcpy(iv,&(obuf[l-8]),8);
538*0Sstevel@tonic-gate 				}
539*0Sstevel@tonic-gate 			if (rem) memcpy(buf,&(buf[l]),(unsigned int)rem);
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 			i=0;
542*0Sstevel@tonic-gate 			while (i < l)
543*0Sstevel@tonic-gate 				{
544*0Sstevel@tonic-gate 				if (uflag)
545*0Sstevel@tonic-gate 					j=uufwrite(obuf,1,(unsigned int)l-i,
546*0Sstevel@tonic-gate 						DES_OUT);
547*0Sstevel@tonic-gate 				else
548*0Sstevel@tonic-gate 					j=fwrite(obuf,1,(unsigned int)l-i,
549*0Sstevel@tonic-gate 						DES_OUT);
550*0Sstevel@tonic-gate 				if (j == -1)
551*0Sstevel@tonic-gate 					{
552*0Sstevel@tonic-gate 					perror("Write error");
553*0Sstevel@tonic-gate 					Exit=7;
554*0Sstevel@tonic-gate 					goto problems;
555*0Sstevel@tonic-gate 					}
556*0Sstevel@tonic-gate 				i+=j;
557*0Sstevel@tonic-gate 				}
558*0Sstevel@tonic-gate 			if (feof(DES_IN))
559*0Sstevel@tonic-gate 				{
560*0Sstevel@tonic-gate 				if (uflag) uufwriteEnd(DES_OUT);
561*0Sstevel@tonic-gate 				break;
562*0Sstevel@tonic-gate 				}
563*0Sstevel@tonic-gate 			}
564*0Sstevel@tonic-gate 		}
565*0Sstevel@tonic-gate 	else /* decrypt */
566*0Sstevel@tonic-gate 		{
567*0Sstevel@tonic-gate 		ex=1;
568*0Sstevel@tonic-gate 		for (;;)
569*0Sstevel@tonic-gate 			{
570*0Sstevel@tonic-gate 			if (ex) {
571*0Sstevel@tonic-gate 				if (uflag)
572*0Sstevel@tonic-gate 					l=uufread(buf,1,BUFSIZE,DES_IN);
573*0Sstevel@tonic-gate 				else
574*0Sstevel@tonic-gate 					l=fread(buf,1,BUFSIZE,DES_IN);
575*0Sstevel@tonic-gate 				ex=0;
576*0Sstevel@tonic-gate 				rem=l%8;
577*0Sstevel@tonic-gate 				l-=rem;
578*0Sstevel@tonic-gate 				}
579*0Sstevel@tonic-gate 			if (l < 0)
580*0Sstevel@tonic-gate 				{
581*0Sstevel@tonic-gate 				perror("read error");
582*0Sstevel@tonic-gate 				Exit=6;
583*0Sstevel@tonic-gate 				goto problems;
584*0Sstevel@tonic-gate 				}
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 			if (bflag && !flag3)
587*0Sstevel@tonic-gate 				for (i=0; i<l; i+=8)
588*0Sstevel@tonic-gate 					DES_ecb_encrypt(
589*0Sstevel@tonic-gate 						(DES_cblock *)&(buf[i]),
590*0Sstevel@tonic-gate 						(DES_cblock *)&(obuf[i]),
591*0Sstevel@tonic-gate 						&ks,do_encrypt);
592*0Sstevel@tonic-gate 			else if (flag3 && bflag)
593*0Sstevel@tonic-gate 				for (i=0; i<l; i+=8)
594*0Sstevel@tonic-gate 					DES_ecb2_encrypt(
595*0Sstevel@tonic-gate 						(DES_cblock *)&(buf[i]),
596*0Sstevel@tonic-gate 						(DES_cblock *)&(obuf[i]),
597*0Sstevel@tonic-gate 						&ks,&ks2,do_encrypt);
598*0Sstevel@tonic-gate 			else if (flag3 && !bflag)
599*0Sstevel@tonic-gate 				{
600*0Sstevel@tonic-gate 				DES_3cbc_encrypt(
601*0Sstevel@tonic-gate 					(DES_cblock *)buf,(DES_cblock *)obuf,
602*0Sstevel@tonic-gate 					(long)l,ks,ks2,&iv,
603*0Sstevel@tonic-gate 					&iv2,do_encrypt);
604*0Sstevel@tonic-gate 				}
605*0Sstevel@tonic-gate 			else
606*0Sstevel@tonic-gate 				{
607*0Sstevel@tonic-gate 				DES_cbc_encrypt(
608*0Sstevel@tonic-gate 					buf,obuf,
609*0Sstevel@tonic-gate 				 	(long)l,&ks,&iv,do_encrypt);
610*0Sstevel@tonic-gate 				if (l >= 8) memcpy(iv,&(buf[l-8]),8);
611*0Sstevel@tonic-gate 				}
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 			if (uflag)
614*0Sstevel@tonic-gate 				ll=uufread(&(buf[rem]),1,BUFSIZE,DES_IN);
615*0Sstevel@tonic-gate 			else
616*0Sstevel@tonic-gate 				ll=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
617*0Sstevel@tonic-gate 			ll+=rem;
618*0Sstevel@tonic-gate 			rem=ll%8;
619*0Sstevel@tonic-gate 			ll-=rem;
620*0Sstevel@tonic-gate 			if (feof(DES_IN) && (ll == 0))
621*0Sstevel@tonic-gate 				{
622*0Sstevel@tonic-gate 				last=obuf[l-1];
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 				if ((last > 7) || (last < 0))
625*0Sstevel@tonic-gate 					{
626*0Sstevel@tonic-gate 					fputs("The file was not decrypted correctly.\n",
627*0Sstevel@tonic-gate 						stderr);
628*0Sstevel@tonic-gate 					Exit=8;
629*0Sstevel@tonic-gate 					last=0;
630*0Sstevel@tonic-gate 					}
631*0Sstevel@tonic-gate 				l=l-8+last;
632*0Sstevel@tonic-gate 				}
633*0Sstevel@tonic-gate 			i=0;
634*0Sstevel@tonic-gate 			if (cflag) DES_cbc_cksum(obuf,
635*0Sstevel@tonic-gate 				(DES_cblock *)cksum,(long)l/8*8,&ks,
636*0Sstevel@tonic-gate 				(DES_cblock *)cksum);
637*0Sstevel@tonic-gate 			while (i != l)
638*0Sstevel@tonic-gate 				{
639*0Sstevel@tonic-gate 				j=fwrite(obuf,1,(unsigned int)l-i,DES_OUT);
640*0Sstevel@tonic-gate 				if (j == -1)
641*0Sstevel@tonic-gate 					{
642*0Sstevel@tonic-gate 					perror("Write error");
643*0Sstevel@tonic-gate 					Exit=7;
644*0Sstevel@tonic-gate 					goto problems;
645*0Sstevel@tonic-gate 					}
646*0Sstevel@tonic-gate 				i+=j;
647*0Sstevel@tonic-gate 				}
648*0Sstevel@tonic-gate 			l=ll;
649*0Sstevel@tonic-gate 			if ((l == 0) && feof(DES_IN)) break;
650*0Sstevel@tonic-gate 			}
651*0Sstevel@tonic-gate 		}
652*0Sstevel@tonic-gate 	if (cflag)
653*0Sstevel@tonic-gate 		{
654*0Sstevel@tonic-gate 		l=0;
655*0Sstevel@tonic-gate 		if (cksumname[0] != '\0')
656*0Sstevel@tonic-gate 			{
657*0Sstevel@tonic-gate 			if ((O=fopen(cksumname,"w")) != NULL)
658*0Sstevel@tonic-gate 				{
659*0Sstevel@tonic-gate 				CKSUM_OUT=O;
660*0Sstevel@tonic-gate 				l=1;
661*0Sstevel@tonic-gate 				}
662*0Sstevel@tonic-gate 			}
663*0Sstevel@tonic-gate 		for (i=0; i<8; i++)
664*0Sstevel@tonic-gate 			fprintf(CKSUM_OUT,"%02X",cksum[i]);
665*0Sstevel@tonic-gate 		fprintf(CKSUM_OUT,"\n");
666*0Sstevel@tonic-gate 		if (l) fclose(CKSUM_OUT);
667*0Sstevel@tonic-gate 		}
668*0Sstevel@tonic-gate problems:
669*0Sstevel@tonic-gate 	OPENSSL_cleanse(buf,sizeof(buf));
670*0Sstevel@tonic-gate 	OPENSSL_cleanse(obuf,sizeof(obuf));
671*0Sstevel@tonic-gate 	OPENSSL_cleanse(&ks,sizeof(ks));
672*0Sstevel@tonic-gate 	OPENSSL_cleanse(&ks2,sizeof(ks2));
673*0Sstevel@tonic-gate 	OPENSSL_cleanse(iv,sizeof(iv));
674*0Sstevel@tonic-gate 	OPENSSL_cleanse(iv2,sizeof(iv2));
675*0Sstevel@tonic-gate 	OPENSSL_cleanse(kk,sizeof(kk));
676*0Sstevel@tonic-gate 	OPENSSL_cleanse(k2,sizeof(k2));
677*0Sstevel@tonic-gate 	OPENSSL_cleanse(uubuf,sizeof(uubuf));
678*0Sstevel@tonic-gate 	OPENSSL_cleanse(b,sizeof(b));
679*0Sstevel@tonic-gate 	OPENSSL_cleanse(bb,sizeof(bb));
680*0Sstevel@tonic-gate 	OPENSSL_cleanse(cksum,sizeof(cksum));
681*0Sstevel@tonic-gate 	if (Exit) EXIT(Exit);
682*0Sstevel@tonic-gate 	}
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate /*    We ignore this parameter but it should be > ~50 I believe    */
uufwrite(unsigned char * data,int size,unsigned int num,FILE * fp)685*0Sstevel@tonic-gate int uufwrite(unsigned char *data, int size, unsigned int num, FILE *fp)
686*0Sstevel@tonic-gate 	{
687*0Sstevel@tonic-gate 	int i,j,left,rem,ret=num;
688*0Sstevel@tonic-gate 	static int start=1;
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate 	if (start)
691*0Sstevel@tonic-gate 		{
692*0Sstevel@tonic-gate 		fprintf(fp,"begin 600 %s\n",
693*0Sstevel@tonic-gate 			(uuname[0] == '\0')?"text.d":uuname);
694*0Sstevel@tonic-gate 		start=0;
695*0Sstevel@tonic-gate 		}
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 	if (uubufnum)
698*0Sstevel@tonic-gate 		{
699*0Sstevel@tonic-gate 		if (uubufnum+num < 45)
700*0Sstevel@tonic-gate 			{
701*0Sstevel@tonic-gate 			memcpy(&(uubuf[uubufnum]),data,(unsigned int)num);
702*0Sstevel@tonic-gate 			uubufnum+=num;
703*0Sstevel@tonic-gate 			return(num);
704*0Sstevel@tonic-gate 			}
705*0Sstevel@tonic-gate 		else
706*0Sstevel@tonic-gate 			{
707*0Sstevel@tonic-gate 			i=45-uubufnum;
708*0Sstevel@tonic-gate 			memcpy(&(uubuf[uubufnum]),data,(unsigned int)i);
709*0Sstevel@tonic-gate 			j=uuencode((unsigned char *)uubuf,45,b);
710*0Sstevel@tonic-gate 			fwrite(b,1,(unsigned int)j,fp);
711*0Sstevel@tonic-gate 			uubufnum=0;
712*0Sstevel@tonic-gate 			data+=i;
713*0Sstevel@tonic-gate 			num-=i;
714*0Sstevel@tonic-gate 			}
715*0Sstevel@tonic-gate 		}
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 	for (i=0; i<(((int)num)-INUUBUFN); i+=INUUBUFN)
718*0Sstevel@tonic-gate 		{
719*0Sstevel@tonic-gate 		j=uuencode(&(data[i]),INUUBUFN,b);
720*0Sstevel@tonic-gate 		fwrite(b,1,(unsigned int)j,fp);
721*0Sstevel@tonic-gate 		}
722*0Sstevel@tonic-gate 	rem=(num-i)%45;
723*0Sstevel@tonic-gate 	left=(num-i-rem);
724*0Sstevel@tonic-gate 	if (left)
725*0Sstevel@tonic-gate 		{
726*0Sstevel@tonic-gate 		j=uuencode(&(data[i]),left,b);
727*0Sstevel@tonic-gate 		fwrite(b,1,(unsigned int)j,fp);
728*0Sstevel@tonic-gate 		i+=left;
729*0Sstevel@tonic-gate 		}
730*0Sstevel@tonic-gate 	if (i != num)
731*0Sstevel@tonic-gate 		{
732*0Sstevel@tonic-gate 		memcpy(uubuf,&(data[i]),(unsigned int)rem);
733*0Sstevel@tonic-gate 		uubufnum=rem;
734*0Sstevel@tonic-gate 		}
735*0Sstevel@tonic-gate 	return(ret);
736*0Sstevel@tonic-gate 	}
737*0Sstevel@tonic-gate 
uufwriteEnd(FILE * fp)738*0Sstevel@tonic-gate void uufwriteEnd(FILE *fp)
739*0Sstevel@tonic-gate 	{
740*0Sstevel@tonic-gate 	int j;
741*0Sstevel@tonic-gate 	static const char *end=" \nend\n";
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 	if (uubufnum != 0)
744*0Sstevel@tonic-gate 		{
745*0Sstevel@tonic-gate 		uubuf[uubufnum]='\0';
746*0Sstevel@tonic-gate 		uubuf[uubufnum+1]='\0';
747*0Sstevel@tonic-gate 		uubuf[uubufnum+2]='\0';
748*0Sstevel@tonic-gate 		j=uuencode(uubuf,uubufnum,b);
749*0Sstevel@tonic-gate 		fwrite(b,1,(unsigned int)j,fp);
750*0Sstevel@tonic-gate 		}
751*0Sstevel@tonic-gate 	fwrite(end,1,strlen(end),fp);
752*0Sstevel@tonic-gate 	}
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate /* int size:  should always be > ~ 60; I actually ignore this parameter :-)    */
uufread(unsigned char * out,int size,unsigned int num,FILE * fp)755*0Sstevel@tonic-gate int uufread(unsigned char *out, int size, unsigned int num, FILE *fp)
756*0Sstevel@tonic-gate 	{
757*0Sstevel@tonic-gate 	int i,j,tot;
758*0Sstevel@tonic-gate 	static int done=0;
759*0Sstevel@tonic-gate 	static int valid=0;
760*0Sstevel@tonic-gate 	static int start=1;
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 	if (start)
763*0Sstevel@tonic-gate 		{
764*0Sstevel@tonic-gate 		for (;;)
765*0Sstevel@tonic-gate 			{
766*0Sstevel@tonic-gate 			b[0]='\0';
767*0Sstevel@tonic-gate 			fgets((char *)b,300,fp);
768*0Sstevel@tonic-gate 			if (b[0] == '\0')
769*0Sstevel@tonic-gate 				{
770*0Sstevel@tonic-gate 				fprintf(stderr,"no 'begin' found in uuencoded input\n");
771*0Sstevel@tonic-gate 				return(-1);
772*0Sstevel@tonic-gate 				}
773*0Sstevel@tonic-gate 			if (strncmp((char *)b,"begin ",6) == 0) break;
774*0Sstevel@tonic-gate 			}
775*0Sstevel@tonic-gate 		start=0;
776*0Sstevel@tonic-gate 		}
777*0Sstevel@tonic-gate 	if (done) return(0);
778*0Sstevel@tonic-gate 	tot=0;
779*0Sstevel@tonic-gate 	if (valid)
780*0Sstevel@tonic-gate 		{
781*0Sstevel@tonic-gate 		memcpy(out,bb,(unsigned int)valid);
782*0Sstevel@tonic-gate 		tot=valid;
783*0Sstevel@tonic-gate 		valid=0;
784*0Sstevel@tonic-gate 		}
785*0Sstevel@tonic-gate 	for (;;)
786*0Sstevel@tonic-gate 		{
787*0Sstevel@tonic-gate 		b[0]='\0';
788*0Sstevel@tonic-gate 		fgets((char *)b,300,fp);
789*0Sstevel@tonic-gate 		if (b[0] == '\0') break;
790*0Sstevel@tonic-gate 		i=strlen((char *)b);
791*0Sstevel@tonic-gate 		if ((b[0] == 'e') && (b[1] == 'n') && (b[2] == 'd'))
792*0Sstevel@tonic-gate 			{
793*0Sstevel@tonic-gate 			done=1;
794*0Sstevel@tonic-gate 			while (!feof(fp))
795*0Sstevel@tonic-gate 				{
796*0Sstevel@tonic-gate 				fgets((char *)b,300,fp);
797*0Sstevel@tonic-gate 				}
798*0Sstevel@tonic-gate 			break;
799*0Sstevel@tonic-gate 			}
800*0Sstevel@tonic-gate 		i=uudecode(b,i,bb);
801*0Sstevel@tonic-gate 		if (i < 0) break;
802*0Sstevel@tonic-gate 		if ((i+tot+8) > num)
803*0Sstevel@tonic-gate 			{
804*0Sstevel@tonic-gate 			/* num to copy to make it a multiple of 8 */
805*0Sstevel@tonic-gate 			j=(num/8*8)-tot-8;
806*0Sstevel@tonic-gate 			memcpy(&(out[tot]),bb,(unsigned int)j);
807*0Sstevel@tonic-gate 			tot+=j;
808*0Sstevel@tonic-gate 			memcpy(bb,&(bb[j]),(unsigned int)i-j);
809*0Sstevel@tonic-gate 			valid=i-j;
810*0Sstevel@tonic-gate 			break;
811*0Sstevel@tonic-gate 			}
812*0Sstevel@tonic-gate 		memcpy(&(out[tot]),bb,(unsigned int)i);
813*0Sstevel@tonic-gate 		tot+=i;
814*0Sstevel@tonic-gate 		}
815*0Sstevel@tonic-gate 	return(tot);
816*0Sstevel@tonic-gate 	}
817*0Sstevel@tonic-gate 
818*0Sstevel@tonic-gate #define ccc2l(c,l)      (l =((DES_LONG)(*((c)++)))<<16, \
819*0Sstevel@tonic-gate 			 l|=((DES_LONG)(*((c)++)))<< 8, \
820*0Sstevel@tonic-gate 		 	 l|=((DES_LONG)(*((c)++))))
821*0Sstevel@tonic-gate 
822*0Sstevel@tonic-gate #define l2ccc(l,c)      (*((c)++)=(unsigned char)(((l)>>16)&0xff), \
823*0Sstevel@tonic-gate                     *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
824*0Sstevel@tonic-gate                     *((c)++)=(unsigned char)(((l)    )&0xff))
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate 
uuencode(unsigned char * in,int num,unsigned char * out)827*0Sstevel@tonic-gate int uuencode(unsigned char *in, int num, unsigned char *out)
828*0Sstevel@tonic-gate 	{
829*0Sstevel@tonic-gate 	int j,i,n,tot=0;
830*0Sstevel@tonic-gate 	DES_LONG l;
831*0Sstevel@tonic-gate 	register unsigned char *p;
832*0Sstevel@tonic-gate 	p=out;
833*0Sstevel@tonic-gate 
834*0Sstevel@tonic-gate 	for (j=0; j<num; j+=45)
835*0Sstevel@tonic-gate 		{
836*0Sstevel@tonic-gate 		if (j+45 > num)
837*0Sstevel@tonic-gate 			i=(num-j);
838*0Sstevel@tonic-gate 		else	i=45;
839*0Sstevel@tonic-gate 		*(p++)=i+' ';
840*0Sstevel@tonic-gate 		for (n=0; n<i; n+=3)
841*0Sstevel@tonic-gate 			{
842*0Sstevel@tonic-gate 			ccc2l(in,l);
843*0Sstevel@tonic-gate 			*(p++)=((l>>18)&0x3f)+' ';
844*0Sstevel@tonic-gate 			*(p++)=((l>>12)&0x3f)+' ';
845*0Sstevel@tonic-gate 			*(p++)=((l>> 6)&0x3f)+' ';
846*0Sstevel@tonic-gate 			*(p++)=((l    )&0x3f)+' ';
847*0Sstevel@tonic-gate 			tot+=4;
848*0Sstevel@tonic-gate 			}
849*0Sstevel@tonic-gate 		*(p++)='\n';
850*0Sstevel@tonic-gate 		tot+=2;
851*0Sstevel@tonic-gate 		}
852*0Sstevel@tonic-gate 	*p='\0';
853*0Sstevel@tonic-gate 	l=0;
854*0Sstevel@tonic-gate 	return(tot);
855*0Sstevel@tonic-gate 	}
856*0Sstevel@tonic-gate 
uudecode(unsigned char * in,int num,unsigned char * out)857*0Sstevel@tonic-gate int uudecode(unsigned char *in, int num, unsigned char *out)
858*0Sstevel@tonic-gate 	{
859*0Sstevel@tonic-gate 	int j,i,k;
860*0Sstevel@tonic-gate 	unsigned int n=0,space=0;
861*0Sstevel@tonic-gate 	DES_LONG l;
862*0Sstevel@tonic-gate 	DES_LONG w,x,y,z;
863*0Sstevel@tonic-gate 	unsigned int blank=(unsigned int)'\n'-' ';
864*0Sstevel@tonic-gate 
865*0Sstevel@tonic-gate 	for (j=0; j<num; )
866*0Sstevel@tonic-gate 		{
867*0Sstevel@tonic-gate 		n= *(in++)-' ';
868*0Sstevel@tonic-gate 		if (n == blank)
869*0Sstevel@tonic-gate 			{
870*0Sstevel@tonic-gate 			n=0;
871*0Sstevel@tonic-gate 			in--;
872*0Sstevel@tonic-gate 			}
873*0Sstevel@tonic-gate 		if (n > 60)
874*0Sstevel@tonic-gate 			{
875*0Sstevel@tonic-gate 			fprintf(stderr,"uuencoded line length too long\n");
876*0Sstevel@tonic-gate 			return(-1);
877*0Sstevel@tonic-gate 			}
878*0Sstevel@tonic-gate 		j++;
879*0Sstevel@tonic-gate 
880*0Sstevel@tonic-gate 		for (i=0; i<n; j+=4,i+=3)
881*0Sstevel@tonic-gate 			{
882*0Sstevel@tonic-gate 			/* the following is for cases where spaces are
883*0Sstevel@tonic-gate 			 * removed from lines.
884*0Sstevel@tonic-gate 			 */
885*0Sstevel@tonic-gate 			if (space)
886*0Sstevel@tonic-gate 				{
887*0Sstevel@tonic-gate 				w=x=y=z=0;
888*0Sstevel@tonic-gate 				}
889*0Sstevel@tonic-gate 			else
890*0Sstevel@tonic-gate 				{
891*0Sstevel@tonic-gate 				w= *(in++)-' ';
892*0Sstevel@tonic-gate 				x= *(in++)-' ';
893*0Sstevel@tonic-gate 				y= *(in++)-' ';
894*0Sstevel@tonic-gate 				z= *(in++)-' ';
895*0Sstevel@tonic-gate 				}
896*0Sstevel@tonic-gate 			if ((w > 63) || (x > 63) || (y > 63) || (z > 63))
897*0Sstevel@tonic-gate 				{
898*0Sstevel@tonic-gate 				k=0;
899*0Sstevel@tonic-gate 				if (w == blank) k=1;
900*0Sstevel@tonic-gate 				if (x == blank) k=2;
901*0Sstevel@tonic-gate 				if (y == blank) k=3;
902*0Sstevel@tonic-gate 				if (z == blank) k=4;
903*0Sstevel@tonic-gate 				space=1;
904*0Sstevel@tonic-gate 				switch (k) {
905*0Sstevel@tonic-gate 				case 1:	w=0; in--;
906*0Sstevel@tonic-gate 				case 2: x=0; in--;
907*0Sstevel@tonic-gate 				case 3: y=0; in--;
908*0Sstevel@tonic-gate 				case 4: z=0; in--;
909*0Sstevel@tonic-gate 					break;
910*0Sstevel@tonic-gate 				case 0:
911*0Sstevel@tonic-gate 					space=0;
912*0Sstevel@tonic-gate 					fprintf(stderr,"bad uuencoded data values\n");
913*0Sstevel@tonic-gate 					w=x=y=z=0;
914*0Sstevel@tonic-gate 					return(-1);
915*0Sstevel@tonic-gate 					break;
916*0Sstevel@tonic-gate 					}
917*0Sstevel@tonic-gate 				}
918*0Sstevel@tonic-gate 			l=(w<<18)|(x<<12)|(y<< 6)|(z    );
919*0Sstevel@tonic-gate 			l2ccc(l,out);
920*0Sstevel@tonic-gate 			}
921*0Sstevel@tonic-gate 		if (*(in++) != '\n')
922*0Sstevel@tonic-gate 			{
923*0Sstevel@tonic-gate 			fprintf(stderr,"missing nl in uuencoded line\n");
924*0Sstevel@tonic-gate 			w=x=y=z=0;
925*0Sstevel@tonic-gate 			return(-1);
926*0Sstevel@tonic-gate 			}
927*0Sstevel@tonic-gate 		j++;
928*0Sstevel@tonic-gate 		}
929*0Sstevel@tonic-gate 	*out='\0';
930*0Sstevel@tonic-gate 	w=x=y=z=0;
931*0Sstevel@tonic-gate 	return(n);
932*0Sstevel@tonic-gate 	}
933