2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
76 #include <sys/types.h>
85 #if defined(__STDC__) || defined(VMS) || defined(M_XENIX) || defined(MSDOS)
91 #define srandom(s) srand(s)
96 void doencryption(void);
97 int uufwrite(unsigned char *data, int size, unsigned int num, FILE *fp);
98 void uufwriteEnd(FILE *fp);
99 int uufread(unsigned char *out,int size,unsigned int num,FILE *fp);
100 int uuencode(unsigned char *in,int num,unsigned char *out);
101 int uudecode(unsigned char *in,int num,unsigned char *out);
102 void des_3cbc_encrypt(des_cblock *input,des_cblock *output,long length,
103 des_key_schedule sk1,des_key_schedule sk2,
104 des_cblock *ivec1,des_cblock *ivec2,int enc);
113 void des_3cbc_encrypt();
117 #define EXIT(a) exit(a&0x10000000L)
119 #define EXIT(a) exit(a)
122 #define BUFSIZE (8*1024)
125 #define KEYSIZB 1024 /* should hit tty line limit first :-) */
127 int do_encrypt,longk=0;
128 FILE *DES_IN,*DES_OUT,*CKSUM_OUT;
130 unsigned char uubuf[50];
132 #define INUUBUFN (45*100)
133 #define OUTUUBUF (65*100)
134 unsigned char b[OUTUUBUF];
135 unsigned char bb[300];
136 des_cblock cksum={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
137 char cksumname[200]="";
139 int vflag,cflag,eflag,dflag,kflag,bflag,fflag,sflag,uflag,flag3,hflag,error;
146 struct stat ins,outs;
148 char *in=NULL,*out=NULL;
150 vflag=cflag=eflag=dflag=kflag=hflag=bflag=fflag=sflag=uflag=flag3=0;
152 memset(key,0,sizeof(key));
154 for (i=1; i<argc; i++)
157 if ((p[0] == '-') && (p[1] != '\0'))
170 strncpy(cksumname,p,200);
171 p+=strlen(cksumname);
176 strncpy(cksumname,p,200);
177 p+=strlen(cksumname);
207 strncpy(uuname,p,200);
217 fputs("must have a key with the -k option\n",stderr);
225 strncpy(key,argv[i],KEYSIZB);
226 for (j=strlen(argv[i])-1; j>=0; j--)
231 fprintf(stderr,"'%c' unknown flag\n",p[-1]);
241 else if (out == NULL)
252 * do decrypt then ckecksum or
253 * do checksum then encrypt
255 if (((eflag+dflag) == 1) || cflag)
257 if (eflag) do_encrypt=DES_ENCRYPT;
258 if (dflag) do_encrypt=DES_DECRYPT;
265 fprintf(stderr,"des(1) built with %s\n",libdes_version);
273 if (vflag) fprintf(stderr,"des(1) built with %s\n",libdes_version);
278 (stat(in,&ins) != -1) &&
279 (stat(out,&outs) != -1) &&
280 (ins.st_dev == outs.st_dev) &&
281 (ins.st_ino == outs.st_ino))
283 (strcmp(in,out) == 0))
286 fputs("input and output file are the same\n",stderr);
291 if (des_read_pw_string(key,KEYSIZB+1,"Enter key:",eflag?VERIFY:0))
293 fputs("password error\n",stderr);
299 else if ((DES_IN=fopen(in,"r")) == NULL)
301 perror("opening input file");
311 else if ((DES_OUT=fopen(out,"w")) == NULL)
313 perror("opening output file");
318 /* This should set the file to binary mode. */
321 if (!(uflag && dflag))
322 setmode(fileno(DES_IN),O_BINARY);
323 if (!(uflag && eflag))
324 setmode(fileno(DES_OUT),O_BINARY);
337 static const char *Usage[]={
338 "des <options> [input-file [output-file]]",
340 "-v : des(1) version number",
341 "-e : encrypt using sunOS compatible user key to DES key conversion.",
343 "-d : decrypt using sunOS compatible user key to DES key conversion.",
345 "-c[ckname] : generate a cbc_cksum using sunOS compatible user key to",
346 " DES key conversion and output to ckname (stdout default,",
347 " stderr if data being output on stdout). The checksum is",
348 " generated before encryption and after decryption if used",
349 " in conjunction with -[eEdD].",
350 "-C[ckname] : generate a cbc_cksum as for -c but compatible with -[ED].",
351 "-k key : use key 'key'",
352 "-h : the key that is entered will be a hexidecimal number",
353 " that is used directly as the des key",
354 "-u[uuname] : input file is uudecoded if -[dD] or output uuencoded data if -[eE]",
355 " (uuname is the filename to put in the uuencode header).",
356 "-b : encrypt using DES in ecb encryption mode, the defaut is cbc mode.",
357 "-3 : encrypt using tripple DES encryption. This uses 2 keys",
358 " generated from the input key. If the input key is less",
359 " than 8 characters long, this is equivelent to normal",
360 " encryption. Default is tripple cbc, -b makes it tripple ecb.",
363 for (u=(char **)Usage; *u; u++)
375 extern int srandom();
377 extern unsigned long time();
381 des_key_schedule ks,ks2;
382 unsigned char iv[8],iv2[8];
384 int num=0,j,k,l,rem,ll,len,last,ex=0;
389 static unsigned char buf[BUFSIZE+8],obuf[BUFSIZE+8];
391 static unsigned char *buf=NULL,*obuf=NULL;
395 if ( (( buf=(unsigned char *)Malloc(BUFSIZE+8)) == NULL) ||
396 ((obuf=(unsigned char *)Malloc(BUFSIZE+8)) == NULL))
398 fputs("Not enough memory\n",stderr);
412 if ((*p <= '9') && (*p >= '0'))
414 else if ((*p <= 'f') && (*p >= 'a'))
416 else if ((*p <= 'F') && (*p >= 'A'))
420 fputs("Bad hex key\n",stderr);
425 if ((*p <= '9') && (*p >= '0'))
427 else if ((*p <= 'f') && (*p >= 'a'))
429 else if ((*p <= 'F') && (*p >= 'A'))
433 fputs("Bad hex key\n",stderr);
443 des_set_key((C_Block *)k2,ks2);
444 memset(k2,0,sizeof(k2));
446 else if (longk || flag3)
450 des_string_to_2keys(key,(C_Block *)kk,(C_Block *)k2);
451 des_set_key((C_Block *)k2,ks2);
452 memset(k2,0,sizeof(k2));
455 des_string_to_key(key,(C_Block *)kk);
458 for (i=0; i<KEYSIZ; i++)
473 des_set_key((C_Block *)kk,ks);
474 memset(key,0,sizeof(key));
475 memset(kk,0,sizeof(kk));
476 /* woops - A bug that does not showup under unix :-( */
477 memset(iv,0,sizeof(iv));
478 memset(iv2,0,sizeof(iv2));
483 if (eflag || (!dflag && cflag))
487 num=l=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
492 perror("read error");
501 srandom((unsigned int)time(NULL));
502 for (i=7-rem; i>0; i--)
503 buf[l++]=random()&0xff;
513 des_cbc_cksum((C_Block *)buf,(C_Block *)cksum,
514 (long)len,ks,(C_Block *)cksum);
517 if (feof(DES_IN)) break;
525 (des_cblock *)&(buf[i]),
526 (des_cblock *)&(obuf[i]),
528 else if (flag3 && bflag)
531 (des_cblock *)&(buf[i]),
532 (des_cblock *)&(obuf[i]),
534 else if (flag3 && !bflag)
538 if (rem) memcpy(tmpbuf,&(buf[l]),
541 (des_cblock *)buf,(des_cblock *)obuf,
542 (long)l,ks,ks2,(des_cblock *)iv,
543 (des_cblock *)iv2,do_encrypt);
544 if (rem) memcpy(&(buf[l]),tmpbuf,
550 (des_cblock *)buf,(des_cblock *)obuf,
551 (long)l,ks,(des_cblock *)iv,do_encrypt);
552 if (l >= 8) memcpy(iv,&(obuf[l-8]),8);
554 if (rem) memcpy(buf,&(buf[l]),(unsigned int)rem);
560 j=uufwrite(obuf,1,(unsigned int)l-i,
563 j=fwrite(obuf,1,(unsigned int)l-i,
567 perror("Write error");
575 if (uflag) uufwriteEnd(DES_OUT);
587 l=uufread(buf,1,BUFSIZE,DES_IN);
589 l=fread(buf,1,BUFSIZE,DES_IN);
596 perror("read error");
604 (des_cblock *)&(buf[i]),
605 (des_cblock *)&(obuf[i]),
607 else if (flag3 && bflag)
610 (des_cblock *)&(buf[i]),
611 (des_cblock *)&(obuf[i]),
613 else if (flag3 && !bflag)
616 (des_cblock *)buf,(des_cblock *)obuf,
617 (long)l,ks,ks2,(des_cblock *)iv,
618 (des_cblock *)iv2,do_encrypt);
623 (des_cblock *)buf,(des_cblock *)obuf,
624 (long)l,ks,(des_cblock *)iv,do_encrypt);
625 if (l >= 8) memcpy(iv,&(buf[l-8]),8);
629 ll=uufread(&(buf[rem]),1,BUFSIZE,DES_IN);
631 ll=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
635 if (feof(DES_IN) && (ll == 0))
639 if ((last > 7) || (last < 0))
641 fputs("The file was not decrypted correctly.\n",
649 if (cflag) des_cbc_cksum((C_Block *)obuf,
650 (C_Block *)cksum,(long)l/8*8,ks,
654 j=fwrite(obuf,1,(unsigned int)l-i,DES_OUT);
657 perror("Write error");
664 if ((l == 0) && feof(DES_IN)) break;
670 if (cksumname[0] != '\0')
672 if ((O=fopen(cksumname,"w")) != NULL)
679 fprintf(CKSUM_OUT,"%02X",cksum[i]);
680 fprintf(CKSUM_OUT,"\n");
681 if (l) fclose(CKSUM_OUT);
684 memset(buf,0,sizeof(buf));
685 memset(obuf,0,sizeof(obuf));
686 memset(ks,0,sizeof(ks));
687 memset(ks2,0,sizeof(ks2));
688 memset(iv,0,sizeof(iv));
689 memset(iv2,0,sizeof(iv2));
690 memset(kk,0,sizeof(kk));
691 memset(k2,0,sizeof(k2));
692 memset(uubuf,0,sizeof(uubuf));
693 memset(b,0,sizeof(b));
694 memset(bb,0,sizeof(bb));
695 memset(cksum,0,sizeof(cksum));
696 if (Exit) EXIT(Exit);
699 int uufwrite(data, size, num, fp)
705 /* We ignore this parameter but it should be > ~50 I believe */
709 int i,j,left,rem,ret=num;
714 fprintf(fp,"begin 600 %s\n",
715 (uuname[0] == '\0')?"text.d":uuname);
721 if (uubufnum+num < 45)
723 memcpy(&(uubuf[uubufnum]),data,(unsigned int)num);
730 memcpy(&(uubuf[uubufnum]),data,(unsigned int)i);
731 j=uuencode((unsigned char *)uubuf,45,b);
732 fwrite(b,1,(unsigned int)j,fp);
739 for (i=0; i<(((int)num)-INUUBUFN); i+=INUUBUFN)
741 j=uuencode(&(data[i]),INUUBUFN,b);
742 fwrite(b,1,(unsigned int)j,fp);
748 j=uuencode(&(data[i]),left,b);
749 fwrite(b,1,(unsigned int)j,fp);
754 memcpy(uubuf,&(data[i]),(unsigned int)rem);
764 static const char *end=" \nend\n";
768 uubuf[uubufnum]='\0';
769 uubuf[uubufnum+1]='\0';
770 uubuf[uubufnum+2]='\0';
771 j=uuencode(uubuf,uubufnum,b);
772 fwrite(b,1,(unsigned int)j,fp);
774 fwrite(end,1,strlen(end),fp);
777 int uufread(out, size, num, fp)
779 int size; /* should always be > ~ 60; I actually ignore this parameter :-) */
793 fgets((char *)b,300,fp);
796 fprintf(stderr,"no 'begin' found in uuencoded input\n");
799 if (strncmp((char *)b,"begin ",6) == 0) break;
807 memcpy(out,bb,(unsigned int)valid);
814 fgets((char *)b,300,fp);
815 if (b[0] == '\0') break;
817 if ((b[0] == 'e') && (b[1] == 'n') && (b[2] == 'd'))
822 fgets((char *)b,300,fp);
830 /* num to copy to make it a multiple of 8 */
832 memcpy(&(out[tot]),bb,(unsigned int)j);
834 memcpy(bb,&(bb[j]),(unsigned int)i-j);
838 memcpy(&(out[tot]),bb,(unsigned int)i);
844 #define ccc2l(c,l) (l =((DES_LONG)(*((c)++)))<<16, \
845 l|=((DES_LONG)(*((c)++)))<< 8, \
846 l|=((DES_LONG)(*((c)++))))
848 #define l2ccc(l,c) (*((c)++)=(unsigned char)(((l)>>16)&0xff), \
849 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
850 *((c)++)=(unsigned char)(((l) )&0xff))
853 int uuencode(in, num, out)
860 register unsigned char *p;
863 for (j=0; j<num; j+=45)
872 *(p++)=((l>>18)&0x3f)+' ';
873 *(p++)=((l>>12)&0x3f)+' ';
874 *(p++)=((l>> 6)&0x3f)+' ';
875 *(p++)=((l )&0x3f)+' ';
886 int uudecode(in, num, out)
892 unsigned int n=0,space=0;
895 unsigned int blank=(unsigned int)'\n'-' ';
907 fprintf(stderr,"uuencoded line length too long\n");
912 for (i=0; i<n; j+=4,i+=3)
914 /* the following is for cases where spaces are
915 * removed from lines.
928 if ((w > 63) || (x > 63) || (y > 63) || (z > 63))
944 fprintf(stderr,"bad uuencoded data values\n");
950 l=(w<<18)|(x<<12)|(y<< 6)|(z );
955 fprintf(stderr,"missing nl in uuencoded line\n");