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;
141 int main(int argc, char **argv)
144 struct stat ins,outs;
146 char *in=NULL,*out=NULL;
148 vflag=cflag=eflag=dflag=kflag=hflag=bflag=fflag=sflag=uflag=flag3=0;
150 memset(key,0,sizeof(key));
152 for (i=1; i<argc; i++)
155 if ((p[0] == '-') && (p[1] != '\0'))
168 strncpy(cksumname,p,200);
169 p+=strlen(cksumname);
174 strncpy(cksumname,p,200);
175 p+=strlen(cksumname);
205 strncpy(uuname,p,200);
215 fputs("must have a key with the -k option\n",stderr);
223 strncpy(key,argv[i],KEYSIZB);
224 for (j=strlen(argv[i])-1; j>=0; j--)
229 fprintf(stderr,"'%c' unknown flag\n",p[-1]);
239 else if (out == NULL)
250 * do decrypt then ckecksum or
251 * do checksum then encrypt
253 if (((eflag+dflag) == 1) || cflag)
255 if (eflag) do_encrypt=DES_ENCRYPT;
256 if (dflag) do_encrypt=DES_DECRYPT;
263 fprintf(stderr,"des(1) built with %s\n",libdes_version);
271 if (vflag) fprintf(stderr,"des(1) built with %s\n",libdes_version);
276 (stat(in,&ins) != -1) &&
277 (stat(out,&outs) != -1) &&
278 (ins.st_dev == outs.st_dev) &&
279 (ins.st_ino == outs.st_ino))
281 (strcmp(in,out) == 0))
284 fputs("input and output file are the same\n",stderr);
289 if (des_read_pw_string(key,KEYSIZB+1,"Enter key:",eflag?VERIFY:0))
291 fputs("password error\n",stderr);
297 else if ((DES_IN=fopen(in,"r")) == NULL)
299 perror("opening input file");
309 else if ((DES_OUT=fopen(out,"w")) == NULL)
311 perror("opening output file");
316 /* This should set the file to binary mode. */
319 if (!(uflag && dflag))
320 setmode(fileno(DES_IN),O_BINARY);
321 if (!(uflag && eflag))
322 setmode(fileno(DES_OUT),O_BINARY);
335 static const char *Usage[]={
336 "des <options> [input-file [output-file]]",
338 "-v : des(1) version number",
339 "-e : encrypt using sunOS compatible user key to DES key conversion.",
341 "-d : decrypt using sunOS compatible user key to DES key conversion.",
343 "-c[ckname] : generate a cbc_cksum using sunOS compatible user key to",
344 " DES key conversion and output to ckname (stdout default,",
345 " stderr if data being output on stdout). The checksum is",
346 " generated before encryption and after decryption if used",
347 " in conjunction with -[eEdD].",
348 "-C[ckname] : generate a cbc_cksum as for -c but compatible with -[ED].",
349 "-k key : use key 'key'",
350 "-h : the key that is entered will be a hexidecimal number",
351 " that is used directly as the des key",
352 "-u[uuname] : input file is uudecoded if -[dD] or output uuencoded data if -[eE]",
353 " (uuname is the filename to put in the uuencode header).",
354 "-b : encrypt using DES in ecb encryption mode, the defaut is cbc mode.",
355 "-3 : encrypt using tripple DES encryption. This uses 2 keys",
356 " generated from the input key. If the input key is less",
357 " than 8 characters long, this is equivelent to normal",
358 " encryption. Default is triple cbc, -b makes it triple ecb.",
361 for (u=(char **)Usage; *u; u++)
370 void doencryption(void)
373 extern int srandom();
375 extern unsigned long time();
379 des_key_schedule ks,ks2;
380 unsigned char iv[8],iv2[8];
382 int num=0,j,k,l,rem,ll,len,last,ex=0;
387 static unsigned char buf[BUFSIZE+8],obuf[BUFSIZE+8];
389 static unsigned char *buf=NULL,*obuf=NULL;
393 if ( (( buf=(unsigned char *)Malloc(BUFSIZE+8)) == NULL) ||
394 ((obuf=(unsigned char *)Malloc(BUFSIZE+8)) == NULL))
396 fputs("Not enough memory\n",stderr);
410 if ((*p <= '9') && (*p >= '0'))
412 else if ((*p <= 'f') && (*p >= 'a'))
414 else if ((*p <= 'F') && (*p >= 'A'))
418 fputs("Bad hex key\n",stderr);
423 if ((*p <= '9') && (*p >= '0'))
425 else if ((*p <= 'f') && (*p >= 'a'))
427 else if ((*p <= 'F') && (*p >= 'A'))
431 fputs("Bad hex key\n",stderr);
441 des_set_key((C_Block *)k2,ks2);
442 memset(k2,0,sizeof(k2));
444 else if (longk || flag3)
448 des_string_to_2keys(key,(C_Block *)kk,(C_Block *)k2);
449 des_set_key((C_Block *)k2,ks2);
450 memset(k2,0,sizeof(k2));
453 des_string_to_key(key,(C_Block *)kk);
456 for (i=0; i<KEYSIZ; i++)
471 des_set_key((C_Block *)kk,ks);
472 memset(key,0,sizeof(key));
473 memset(kk,0,sizeof(kk));
474 /* woops - A bug that does not showup under unix :-( */
475 memset(iv,0,sizeof(iv));
476 memset(iv2,0,sizeof(iv2));
481 if (eflag || (!dflag && cflag))
485 num=l=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
490 perror("read error");
499 srandom((unsigned int)time(NULL));
500 for (i=7-rem; i>0; i--)
501 buf[l++]=random()&0xff;
511 des_cbc_cksum((C_Block *)buf,(C_Block *)cksum,
512 (long)len,ks,(C_Block *)cksum);
515 if (feof(DES_IN)) break;
523 (des_cblock *)&(buf[i]),
524 (des_cblock *)&(obuf[i]),
526 else if (flag3 && bflag)
529 (des_cblock *)&(buf[i]),
530 (des_cblock *)&(obuf[i]),
532 else if (flag3 && !bflag)
536 if (rem) memcpy(tmpbuf,&(buf[l]),
539 (des_cblock *)buf,(des_cblock *)obuf,
540 (long)l,ks,ks2,(des_cblock *)iv,
541 (des_cblock *)iv2,do_encrypt);
542 if (rem) memcpy(&(buf[l]),tmpbuf,
548 (des_cblock *)buf,(des_cblock *)obuf,
549 (long)l,ks,(des_cblock *)iv,do_encrypt);
550 if (l >= 8) memcpy(iv,&(obuf[l-8]),8);
552 if (rem) memcpy(buf,&(buf[l]),(unsigned int)rem);
558 j=uufwrite(obuf,1,(unsigned int)l-i,
561 j=fwrite(obuf,1,(unsigned int)l-i,
565 perror("Write error");
573 if (uflag) uufwriteEnd(DES_OUT);
585 l=uufread(buf,1,BUFSIZE,DES_IN);
587 l=fread(buf,1,BUFSIZE,DES_IN);
594 perror("read error");
602 (des_cblock *)&(buf[i]),
603 (des_cblock *)&(obuf[i]),
605 else if (flag3 && bflag)
608 (des_cblock *)&(buf[i]),
609 (des_cblock *)&(obuf[i]),
611 else if (flag3 && !bflag)
614 (des_cblock *)buf,(des_cblock *)obuf,
615 (long)l,ks,ks2,(des_cblock *)iv,
616 (des_cblock *)iv2,do_encrypt);
621 (des_cblock *)buf,(des_cblock *)obuf,
622 (long)l,ks,(des_cblock *)iv,do_encrypt);
623 if (l >= 8) memcpy(iv,&(buf[l-8]),8);
627 ll=uufread(&(buf[rem]),1,BUFSIZE,DES_IN);
629 ll=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
633 if (feof(DES_IN) && (ll == 0))
637 if ((last > 7) || (last < 0))
639 fputs("The file was not decrypted correctly.\n",
647 if (cflag) des_cbc_cksum((C_Block *)obuf,
648 (C_Block *)cksum,(long)l/8*8,ks,
652 j=fwrite(obuf,1,(unsigned int)l-i,DES_OUT);
655 perror("Write error");
662 if ((l == 0) && feof(DES_IN)) break;
668 if (cksumname[0] != '\0')
670 if ((O=fopen(cksumname,"w")) != NULL)
677 fprintf(CKSUM_OUT,"%02X",cksum[i]);
678 fprintf(CKSUM_OUT,"\n");
679 if (l) fclose(CKSUM_OUT);
682 memset(buf,0,sizeof(buf));
683 memset(obuf,0,sizeof(obuf));
684 memset(ks,0,sizeof(ks));
685 memset(ks2,0,sizeof(ks2));
686 memset(iv,0,sizeof(iv));
687 memset(iv2,0,sizeof(iv2));
688 memset(kk,0,sizeof(kk));
689 memset(k2,0,sizeof(k2));
690 memset(uubuf,0,sizeof(uubuf));
691 memset(b,0,sizeof(b));
692 memset(bb,0,sizeof(bb));
693 memset(cksum,0,sizeof(cksum));
694 if (Exit) EXIT(Exit);
697 /* We ignore this parameter but it should be > ~50 I believe */
698 int uufwrite(unsigned char *data, int size, unsigned int num, FILE *fp)
700 int i,j,left,rem,ret=num;
705 fprintf(fp,"begin 600 %s\n",
706 (uuname[0] == '\0')?"text.d":uuname);
712 if (uubufnum+num < 45)
714 memcpy(&(uubuf[uubufnum]),data,(unsigned int)num);
721 memcpy(&(uubuf[uubufnum]),data,(unsigned int)i);
722 j=uuencode((unsigned char *)uubuf,45,b);
723 fwrite(b,1,(unsigned int)j,fp);
730 for (i=0; i<(((int)num)-INUUBUFN); i+=INUUBUFN)
732 j=uuencode(&(data[i]),INUUBUFN,b);
733 fwrite(b,1,(unsigned int)j,fp);
739 j=uuencode(&(data[i]),left,b);
740 fwrite(b,1,(unsigned int)j,fp);
745 memcpy(uubuf,&(data[i]),(unsigned int)rem);
751 void uufwriteEnd(FILE *fp)
754 static const char *end=" \nend\n";
758 uubuf[uubufnum]='\0';
759 uubuf[uubufnum+1]='\0';
760 uubuf[uubufnum+2]='\0';
761 j=uuencode(uubuf,uubufnum,b);
762 fwrite(b,1,(unsigned int)j,fp);
764 fwrite(end,1,strlen(end),fp);
767 /* int size: should always be > ~ 60; I actually ignore this parameter :-) */
768 int uufread(unsigned char *out, int size, unsigned int num, FILE *fp)
780 fgets((char *)b,300,fp);
783 fprintf(stderr,"no 'begin' found in uuencoded input\n");
786 if (strncmp((char *)b,"begin ",6) == 0) break;
794 memcpy(out,bb,(unsigned int)valid);
801 fgets((char *)b,300,fp);
802 if (b[0] == '\0') break;
804 if ((b[0] == 'e') && (b[1] == 'n') && (b[2] == 'd'))
809 fgets((char *)b,300,fp);
817 /* num to copy to make it a multiple of 8 */
819 memcpy(&(out[tot]),bb,(unsigned int)j);
821 memcpy(bb,&(bb[j]),(unsigned int)i-j);
825 memcpy(&(out[tot]),bb,(unsigned int)i);
831 #define ccc2l(c,l) (l =((DES_LONG)(*((c)++)))<<16, \
832 l|=((DES_LONG)(*((c)++)))<< 8, \
833 l|=((DES_LONG)(*((c)++))))
835 #define l2ccc(l,c) (*((c)++)=(unsigned char)(((l)>>16)&0xff), \
836 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
837 *((c)++)=(unsigned char)(((l) )&0xff))
840 int uuencode(unsigned char *in, int num, unsigned char *out)
844 register unsigned char *p;
847 for (j=0; j<num; j+=45)
856 *(p++)=((l>>18)&0x3f)+' ';
857 *(p++)=((l>>12)&0x3f)+' ';
858 *(p++)=((l>> 6)&0x3f)+' ';
859 *(p++)=((l )&0x3f)+' ';
870 int uudecode(unsigned char *in, int num, unsigned char *out)
873 unsigned int n=0,space=0;
876 unsigned int blank=(unsigned int)'\n'-' ';
888 fprintf(stderr,"uuencoded line length too long\n");
893 for (i=0; i<n; j+=4,i+=3)
895 /* the following is for cases where spaces are
896 * removed from lines.
909 if ((w > 63) || (x > 63) || (y > 63) || (z > 63))
925 fprintf(stderr,"bad uuencoded data values\n");
931 l=(w<<18)|(x<<12)|(y<< 6)|(z );
936 fprintf(stderr,"missing nl in uuencoded line\n");