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.]
62 #include <openssl/opensslconf.h>
63 #ifndef OPENSSL_SYS_MSDOS
64 #ifndef OPENSSL_SYS_VMS
65 #include OPENSSL_UNISTD
66 #else /* OPENSSL_SYS_VMS */
69 #else /* not __DECC */
72 #endif /* OPENSSL_SYS_VMS */
73 #else /* OPENSSL_SYS_MSDOS */
80 #ifdef OPENSSL_SYS_VMS
85 #include <sys/types.h>
89 #include <openssl/des.h>
90 #include <openssl/rand.h>
91 #include <openssl/ui_compat.h>
94 void doencryption(void);
95 int uufwrite(unsigned char *data, int size, unsigned int num, FILE *fp);
96 void uufwriteEnd(FILE *fp);
97 int uufread(unsigned char *out,int size,unsigned int num,FILE *fp);
98 int uuencode(unsigned char *in,int num,unsigned char *out);
99 int uudecode(unsigned char *in,int num,unsigned char *out);
100 void des_3cbc_encrypt(des_cblock *input,des_cblock *output,long length,
101 des_key_schedule sk1,des_key_schedule sk2,
102 des_cblock *ivec1,des_cblock *ivec2,int enc);
103 #ifdef OPENSSL_SYS_VMS
104 #define EXIT(a) exit(a&0x10000000L)
106 #define EXIT(a) exit(a)
109 #define BUFSIZE (8*1024)
112 #define KEYSIZB 1024 /* should hit tty line limit first :-) */
114 int do_encrypt,longk=0;
115 FILE *DES_IN,*DES_OUT,*CKSUM_OUT;
117 unsigned char uubuf[50];
119 #define INUUBUFN (45*100)
120 #define OUTUUBUF (65*100)
121 unsigned char b[OUTUUBUF];
122 unsigned char bb[300];
123 des_cblock cksum={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
124 char cksumname[200]="";
126 int vflag,cflag,eflag,dflag,kflag,bflag,fflag,sflag,uflag,flag3,hflag,error;
128 int main(int argc, char **argv)
131 struct stat ins,outs;
133 char *in=NULL,*out=NULL;
135 vflag=cflag=eflag=dflag=kflag=hflag=bflag=fflag=sflag=uflag=flag3=0;
137 memset(key,0,sizeof(key));
139 for (i=1; i<argc; i++)
142 if ((p[0] == '-') && (p[1] != '\0'))
155 strncpy(cksumname,p,200);
156 p+=strlen(cksumname);
161 strncpy(cksumname,p,200);
162 p+=strlen(cksumname);
192 strncpy(uuname,p,200);
202 fputs("must have a key with the -k option\n",stderr);
210 strncpy(key,argv[i],KEYSIZB);
211 for (j=strlen(argv[i])-1; j>=0; j--)
216 fprintf(stderr,"'%c' unknown flag\n",p[-1]);
226 else if (out == NULL)
237 * do decrypt then ckecksum or
238 * do checksum then encrypt
240 if (((eflag+dflag) == 1) || cflag)
242 if (eflag) do_encrypt=DES_ENCRYPT;
243 if (dflag) do_encrypt=DES_DECRYPT;
250 fprintf(stderr,"des(1) built with %s\n",libdes_version);
258 if (vflag) fprintf(stderr,"des(1) built with %s\n",libdes_version);
262 #ifndef OPENSSL_SYS_MSDOS
263 (stat(in,&ins) != -1) &&
264 (stat(out,&outs) != -1) &&
265 (ins.st_dev == outs.st_dev) &&
266 (ins.st_ino == outs.st_ino))
267 #else /* OPENSSL_SYS_MSDOS */
268 (strcmp(in,out) == 0))
271 fputs("input and output file are the same\n",stderr);
276 if (des_read_pw_string(key,KEYSIZB+1,"Enter key:",eflag?VERIFY:0))
278 fputs("password error\n",stderr);
284 else if ((DES_IN=fopen(in,"r")) == NULL)
286 perror("opening input file");
296 else if ((DES_OUT=fopen(out,"w")) == NULL)
298 perror("opening output file");
302 #ifdef OPENSSL_SYS_MSDOS
303 /* This should set the file to binary mode. */
306 if (!(uflag && dflag))
307 setmode(fileno(DES_IN),O_BINARY);
308 if (!(uflag && eflag))
309 setmode(fileno(DES_OUT),O_BINARY);
322 static const char *Usage[]={
323 "des <options> [input-file [output-file]]",
325 "-v : des(1) version number",
326 "-e : encrypt using SunOS compatible user key to DES key conversion.",
328 "-d : decrypt using SunOS compatible user key to DES key conversion.",
330 "-c[ckname] : generate a cbc_cksum using SunOS compatible user key to",
331 " DES key conversion and output to ckname (stdout default,",
332 " stderr if data being output on stdout). The checksum is",
333 " generated before encryption and after decryption if used",
334 " in conjunction with -[eEdD].",
335 "-C[ckname] : generate a cbc_cksum as for -c but compatible with -[ED].",
336 "-k key : use key 'key'",
337 "-h : the key that is entered will be a hexadecimal number",
338 " that is used directly as the des key",
339 "-u[uuname] : input file is uudecoded if -[dD] or output uuencoded data if -[eE]",
340 " (uuname is the filename to put in the uuencode header).",
341 "-b : encrypt using DES in ecb encryption mode, the default is cbc mode.",
342 "-3 : encrypt using triple DES encryption. This uses 2 keys",
343 " generated from the input key. If the input key is less",
344 " than 8 characters long, this is equivalent to normal",
345 " encryption. Default is triple cbc, -b makes it triple ecb.",
348 for (u=(char **)Usage; *u; u++)
357 void doencryption(void)
360 extern unsigned long time();
364 des_key_schedule ks,ks2;
367 int num=0,j,k,l,rem,ll,len,last,ex=0;
371 #ifndef OPENSSL_SYS_MSDOS
372 static unsigned char buf[BUFSIZE+8],obuf[BUFSIZE+8];
374 static unsigned char *buf=NULL,*obuf=NULL;
378 if ( (( buf=OPENSSL_malloc(BUFSIZE+8)) == NULL) ||
379 ((obuf=OPENSSL_malloc(BUFSIZE+8)) == NULL))
381 fputs("Not enough memory\n",stderr);
395 if ((*p <= '9') && (*p >= '0'))
397 else if ((*p <= 'f') && (*p >= 'a'))
399 else if ((*p <= 'F') && (*p >= 'A'))
403 fputs("Bad hex key\n",stderr);
408 if ((*p <= '9') && (*p >= '0'))
410 else if ((*p <= 'f') && (*p >= 'a'))
412 else if ((*p <= 'F') && (*p >= 'A'))
416 fputs("Bad hex key\n",stderr);
426 des_set_key_unchecked(&k2,ks2);
427 memset(k2,0,sizeof(k2));
429 else if (longk || flag3)
433 des_string_to_2keys(key,&kk,&k2);
434 des_set_key_unchecked(&k2,ks2);
435 memset(k2,0,sizeof(k2));
438 des_string_to_key(key,&kk);
441 for (i=0; i<KEYSIZ; i++)
456 des_set_key_unchecked(&kk,ks);
457 memset(key,0,sizeof(key));
458 memset(kk,0,sizeof(kk));
459 /* woops - A bug that does not showup under unix :-( */
460 memset(iv,0,sizeof(iv));
461 memset(iv2,0,sizeof(iv2));
466 if (eflag || (!dflag && cflag))
470 num=l=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
475 perror("read error");
484 for (i=7-rem; i>0; i--)
485 RAND_pseudo_bytes(buf + l++, 1);
495 des_cbc_cksum(buf,&cksum,
496 (long)len,ks,&cksum);
499 if (feof(DES_IN)) break;
507 (des_cblock *)&(buf[i]),
508 (des_cblock *)&(obuf[i]),
510 else if (flag3 && bflag)
513 (des_cblock *)&(buf[i]),
514 (des_cblock *)&(obuf[i]),
516 else if (flag3 && !bflag)
520 if (rem) memcpy(tmpbuf,&(buf[l]),
523 (des_cblock *)buf,(des_cblock *)obuf,
526 if (rem) memcpy(&(buf[l]),tmpbuf,
533 (long)l,ks,&iv,do_encrypt);
534 if (l >= 8) memcpy(iv,&(obuf[l-8]),8);
536 if (rem) memcpy(buf,&(buf[l]),(unsigned int)rem);
542 j=uufwrite(obuf,1,(unsigned int)l-i,
545 j=fwrite(obuf,1,(unsigned int)l-i,
549 perror("Write error");
557 if (uflag) uufwriteEnd(DES_OUT);
569 l=uufread(buf,1,BUFSIZE,DES_IN);
571 l=fread(buf,1,BUFSIZE,DES_IN);
578 perror("read error");
586 (des_cblock *)&(buf[i]),
587 (des_cblock *)&(obuf[i]),
589 else if (flag3 && bflag)
592 (des_cblock *)&(buf[i]),
593 (des_cblock *)&(obuf[i]),
595 else if (flag3 && !bflag)
598 (des_cblock *)buf,(des_cblock *)obuf,
606 (long)l,ks,&iv,do_encrypt);
607 if (l >= 8) memcpy(iv,&(buf[l-8]),8);
611 ll=uufread(&(buf[rem]),1,BUFSIZE,DES_IN);
613 ll=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
617 if (feof(DES_IN) && (ll == 0))
621 if ((last > 7) || (last < 0))
623 fputs("The file was not decrypted correctly.\n",
631 if (cflag) des_cbc_cksum(obuf,
632 (des_cblock *)cksum,(long)l/8*8,ks,
633 (des_cblock *)cksum);
636 j=fwrite(obuf,1,(unsigned int)l-i,DES_OUT);
639 perror("Write error");
646 if ((l == 0) && feof(DES_IN)) break;
652 if (cksumname[0] != '\0')
654 if ((O=fopen(cksumname,"w")) != NULL)
661 fprintf(CKSUM_OUT,"%02X",cksum[i]);
662 fprintf(CKSUM_OUT,"\n");
663 if (l) fclose(CKSUM_OUT);
666 memset(buf,0,sizeof(buf));
667 memset(obuf,0,sizeof(obuf));
668 memset(ks,0,sizeof(ks));
669 memset(ks2,0,sizeof(ks2));
670 memset(iv,0,sizeof(iv));
671 memset(iv2,0,sizeof(iv2));
672 memset(kk,0,sizeof(kk));
673 memset(k2,0,sizeof(k2));
674 memset(uubuf,0,sizeof(uubuf));
675 memset(b,0,sizeof(b));
676 memset(bb,0,sizeof(bb));
677 memset(cksum,0,sizeof(cksum));
678 if (Exit) EXIT(Exit);
681 /* We ignore this parameter but it should be > ~50 I believe */
682 int uufwrite(unsigned char *data, int size, unsigned int num, FILE *fp)
684 int i,j,left,rem,ret=num;
689 fprintf(fp,"begin 600 %s\n",
690 (uuname[0] == '\0')?"text.d":uuname);
696 if (uubufnum+num < 45)
698 memcpy(&(uubuf[uubufnum]),data,(unsigned int)num);
705 memcpy(&(uubuf[uubufnum]),data,(unsigned int)i);
706 j=uuencode((unsigned char *)uubuf,45,b);
707 fwrite(b,1,(unsigned int)j,fp);
714 for (i=0; i<(((int)num)-INUUBUFN); i+=INUUBUFN)
716 j=uuencode(&(data[i]),INUUBUFN,b);
717 fwrite(b,1,(unsigned int)j,fp);
723 j=uuencode(&(data[i]),left,b);
724 fwrite(b,1,(unsigned int)j,fp);
729 memcpy(uubuf,&(data[i]),(unsigned int)rem);
735 void uufwriteEnd(FILE *fp)
738 static const char *end=" \nend\n";
742 uubuf[uubufnum]='\0';
743 uubuf[uubufnum+1]='\0';
744 uubuf[uubufnum+2]='\0';
745 j=uuencode(uubuf,uubufnum,b);
746 fwrite(b,1,(unsigned int)j,fp);
748 fwrite(end,1,strlen(end),fp);
751 /* int size: should always be > ~ 60; I actually ignore this parameter :-) */
752 int uufread(unsigned char *out, int size, unsigned int num, FILE *fp)
764 fgets((char *)b,300,fp);
767 fprintf(stderr,"no 'begin' found in uuencoded input\n");
770 if (strncmp((char *)b,"begin ",6) == 0) break;
778 memcpy(out,bb,(unsigned int)valid);
785 fgets((char *)b,300,fp);
786 if (b[0] == '\0') break;
788 if ((b[0] == 'e') && (b[1] == 'n') && (b[2] == 'd'))
793 fgets((char *)b,300,fp);
801 /* num to copy to make it a multiple of 8 */
803 memcpy(&(out[tot]),bb,(unsigned int)j);
805 memcpy(bb,&(bb[j]),(unsigned int)i-j);
809 memcpy(&(out[tot]),bb,(unsigned int)i);
815 #define ccc2l(c,l) (l =((DES_LONG)(*((c)++)))<<16, \
816 l|=((DES_LONG)(*((c)++)))<< 8, \
817 l|=((DES_LONG)(*((c)++))))
819 #define l2ccc(l,c) (*((c)++)=(unsigned char)(((l)>>16)&0xff), \
820 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
821 *((c)++)=(unsigned char)(((l) )&0xff))
824 int uuencode(unsigned char *in, int num, unsigned char *out)
828 register unsigned char *p;
831 for (j=0; j<num; j+=45)
840 *(p++)=((l>>18)&0x3f)+' ';
841 *(p++)=((l>>12)&0x3f)+' ';
842 *(p++)=((l>> 6)&0x3f)+' ';
843 *(p++)=((l )&0x3f)+' ';
854 int uudecode(unsigned char *in, int num, unsigned char *out)
857 unsigned int n=0,space=0;
860 unsigned int blank=(unsigned int)'\n'-' ';
872 fprintf(stderr,"uuencoded line length too long\n");
877 for (i=0; i<n; j+=4,i+=3)
879 /* the following is for cases where spaces are
880 * removed from lines.
893 if ((w > 63) || (x > 63) || (y > 63) || (z > 63))
909 fprintf(stderr,"bad uuencoded data values\n");
915 l=(w<<18)|(x<<12)|(y<< 6)|(z );
920 fprintf(stderr,"missing nl in uuencoded line\n");