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 cksumname[sizeof(cksumname)-1]='\0';
157 p+=strlen(cksumname);
162 strncpy(cksumname,p,200);
163 cksumname[sizeof(cksumname)-1]='\0';
164 p+=strlen(cksumname);
194 strncpy(uuname,p,200);
195 uuname[sizeof(uuname)-1]='\0';
205 fputs("must have a key with the -k option\n",stderr);
213 strncpy(key,argv[i],KEYSIZB);
214 for (j=strlen(argv[i])-1; j>=0; j--)
219 fprintf(stderr,"'%c' unknown flag\n",p[-1]);
229 else if (out == NULL)
241 * do decrypt then ckecksum or
242 * do checksum then encrypt
244 if (((eflag+dflag) == 1) || cflag)
246 if (eflag) do_encrypt=DES_ENCRYPT;
247 if (dflag) do_encrypt=DES_DECRYPT;
254 fprintf(stderr,"des(1) built with %s\n",libdes_version);
262 if (vflag) fprintf(stderr,"des(1) built with %s\n",libdes_version);
266 #ifndef OPENSSL_SYS_MSDOS
267 (stat(in,&ins) != -1) &&
268 (stat(out,&outs) != -1) &&
269 (ins.st_dev == outs.st_dev) &&
270 (ins.st_ino == outs.st_ino))
271 #else /* OPENSSL_SYS_MSDOS */
272 (strcmp(in,out) == 0))
275 fputs("input and output file are the same\n",stderr);
280 if (des_read_pw_string(key,KEYSIZB+1,"Enter key:",eflag?VERIFY:0))
282 fputs("password error\n",stderr);
288 else if ((DES_IN=fopen(in,"r")) == NULL)
290 perror("opening input file");
300 else if ((DES_OUT=fopen(out,"w")) == NULL)
302 perror("opening output file");
306 #ifdef OPENSSL_SYS_MSDOS
307 /* This should set the file to binary mode. */
310 if (!(uflag && dflag))
311 setmode(fileno(DES_IN),O_BINARY);
312 if (!(uflag && eflag))
313 setmode(fileno(DES_OUT),O_BINARY);
326 static const char *Usage[]={
327 "des <options> [input-file [output-file]]",
329 "-v : des(1) version number",
330 "-e : encrypt using SunOS compatible user key to DES key conversion.",
332 "-d : decrypt using SunOS compatible user key to DES key conversion.",
334 "-c[ckname] : generate a cbc_cksum using SunOS compatible user key to",
335 " DES key conversion and output to ckname (stdout default,",
336 " stderr if data being output on stdout). The checksum is",
337 " generated before encryption and after decryption if used",
338 " in conjunction with -[eEdD].",
339 "-C[ckname] : generate a cbc_cksum as for -c but compatible with -[ED].",
340 "-k key : use key 'key'",
341 "-h : the key that is entered will be a hexadecimal number",
342 " that is used directly as the des key",
343 "-u[uuname] : input file is uudecoded if -[dD] or output uuencoded data if -[eE]",
344 " (uuname is the filename to put in the uuencode header).",
345 "-b : encrypt using DES in ecb encryption mode, the default is cbc mode.",
346 "-3 : encrypt using triple DES encryption. This uses 2 keys",
347 " generated from the input key. If the input key is less",
348 " than 8 characters long, this is equivalent to normal",
349 " encryption. Default is triple cbc, -b makes it triple ecb.",
352 for (u=(char **)Usage; *u; u++)
361 void doencryption(void)
364 extern unsigned long time();
368 DES_key_schedule ks,ks2;
371 int num=0,j,k,l,rem,ll,len,last,ex=0;
375 #ifndef OPENSSL_SYS_MSDOS
376 static unsigned char buf[BUFSIZE+8],obuf[BUFSIZE+8];
378 static unsigned char *buf=NULL,*obuf=NULL;
382 if ( (( buf=OPENSSL_malloc(BUFSIZE+8)) == NULL) ||
383 ((obuf=OPENSSL_malloc(BUFSIZE+8)) == NULL))
385 fputs("Not enough memory\n",stderr);
399 if ((*p <= '9') && (*p >= '0'))
401 else if ((*p <= 'f') && (*p >= 'a'))
403 else if ((*p <= 'F') && (*p >= 'A'))
407 fputs("Bad hex key\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);
430 DES_set_key_unchecked(&k2,&ks2);
431 OPENSSL_cleanse(k2,sizeof(k2));
433 else if (longk || flag3)
437 DES_string_to_2keys(key,&kk,&k2);
438 DES_set_key_unchecked(&k2,&ks2);
439 OPENSSL_cleanse(k2,sizeof(k2));
442 DES_string_to_key(key,&kk);
445 for (i=0; i<KEYSIZ; i++)
460 DES_set_key_unchecked(&kk,&ks);
461 OPENSSL_cleanse(key,sizeof(key));
462 OPENSSL_cleanse(kk,sizeof(kk));
463 /* woops - A bug that does not showup under unix :-( */
464 memset(iv,0,sizeof(iv));
465 memset(iv2,0,sizeof(iv2));
470 if (eflag || (!dflag && cflag))
474 num=l=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
479 perror("read error");
488 for (i=7-rem; i>0; i--)
489 RAND_pseudo_bytes(buf + l++, 1);
499 DES_cbc_cksum(buf,&cksum,
500 (long)len,&ks,&cksum);
503 if (feof(DES_IN)) break;
511 (DES_cblock *)&(buf[i]),
512 (DES_cblock *)&(obuf[i]),
514 else if (flag3 && bflag)
517 (DES_cblock *)&(buf[i]),
518 (DES_cblock *)&(obuf[i]),
519 &ks,&ks2,do_encrypt);
520 else if (flag3 && !bflag)
524 if (rem) memcpy(tmpbuf,&(buf[l]),
527 (DES_cblock *)buf,(DES_cblock *)obuf,
530 if (rem) memcpy(&(buf[l]),tmpbuf,
537 (long)l,&ks,&iv,do_encrypt);
538 if (l >= 8) memcpy(iv,&(obuf[l-8]),8);
540 if (rem) memcpy(buf,&(buf[l]),(unsigned int)rem);
546 j=uufwrite(obuf,1,(unsigned int)l-i,
549 j=fwrite(obuf,1,(unsigned int)l-i,
553 perror("Write error");
561 if (uflag) uufwriteEnd(DES_OUT);
573 l=uufread(buf,1,BUFSIZE,DES_IN);
575 l=fread(buf,1,BUFSIZE,DES_IN);
582 perror("read error");
590 (DES_cblock *)&(buf[i]),
591 (DES_cblock *)&(obuf[i]),
593 else if (flag3 && bflag)
596 (DES_cblock *)&(buf[i]),
597 (DES_cblock *)&(obuf[i]),
598 &ks,&ks2,do_encrypt);
599 else if (flag3 && !bflag)
602 (DES_cblock *)buf,(DES_cblock *)obuf,
610 (long)l,&ks,&iv,do_encrypt);
611 if (l >= 8) memcpy(iv,&(buf[l-8]),8);
615 ll=uufread(&(buf[rem]),1,BUFSIZE,DES_IN);
617 ll=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
621 if (feof(DES_IN) && (ll == 0))
625 if ((last > 7) || (last < 0))
627 fputs("The file was not decrypted correctly.\n",
635 if (cflag) DES_cbc_cksum(obuf,
636 (DES_cblock *)cksum,(long)l/8*8,&ks,
637 (DES_cblock *)cksum);
640 j=fwrite(obuf,1,(unsigned int)l-i,DES_OUT);
643 perror("Write error");
650 if ((l == 0) && feof(DES_IN)) break;
656 if (cksumname[0] != '\0')
658 if ((O=fopen(cksumname,"w")) != NULL)
665 fprintf(CKSUM_OUT,"%02X",cksum[i]);
666 fprintf(CKSUM_OUT,"\n");
667 if (l) fclose(CKSUM_OUT);
670 OPENSSL_cleanse(buf,sizeof(buf));
671 OPENSSL_cleanse(obuf,sizeof(obuf));
672 OPENSSL_cleanse(&ks,sizeof(ks));
673 OPENSSL_cleanse(&ks2,sizeof(ks2));
674 OPENSSL_cleanse(iv,sizeof(iv));
675 OPENSSL_cleanse(iv2,sizeof(iv2));
676 OPENSSL_cleanse(kk,sizeof(kk));
677 OPENSSL_cleanse(k2,sizeof(k2));
678 OPENSSL_cleanse(uubuf,sizeof(uubuf));
679 OPENSSL_cleanse(b,sizeof(b));
680 OPENSSL_cleanse(bb,sizeof(bb));
681 OPENSSL_cleanse(cksum,sizeof(cksum));
682 if (Exit) EXIT(Exit);
685 /* We ignore this parameter but it should be > ~50 I believe */
686 int uufwrite(unsigned char *data, int size, unsigned int num, FILE *fp)
688 int i,j,left,rem,ret=num;
693 fprintf(fp,"begin 600 %s\n",
694 (uuname[0] == '\0')?"text.d":uuname);
700 if (uubufnum+num < 45)
702 memcpy(&(uubuf[uubufnum]),data,(unsigned int)num);
709 memcpy(&(uubuf[uubufnum]),data,(unsigned int)i);
710 j=uuencode((unsigned char *)uubuf,45,b);
711 fwrite(b,1,(unsigned int)j,fp);
718 for (i=0; i<(((int)num)-INUUBUFN); i+=INUUBUFN)
720 j=uuencode(&(data[i]),INUUBUFN,b);
721 fwrite(b,1,(unsigned int)j,fp);
727 j=uuencode(&(data[i]),left,b);
728 fwrite(b,1,(unsigned int)j,fp);
733 memcpy(uubuf,&(data[i]),(unsigned int)rem);
739 void uufwriteEnd(FILE *fp)
742 static const char *end=" \nend\n";
746 uubuf[uubufnum]='\0';
747 uubuf[uubufnum+1]='\0';
748 uubuf[uubufnum+2]='\0';
749 j=uuencode(uubuf,uubufnum,b);
750 fwrite(b,1,(unsigned int)j,fp);
752 fwrite(end,1,strlen(end),fp);
755 /* int size: should always be > ~ 60; I actually ignore this parameter :-) */
756 int uufread(unsigned char *out, int size, unsigned int num, FILE *fp)
768 fgets((char *)b,300,fp);
771 fprintf(stderr,"no 'begin' found in uuencoded input\n");
774 if (strncmp((char *)b,"begin ",6) == 0) break;
782 memcpy(out,bb,(unsigned int)valid);
789 fgets((char *)b,300,fp);
790 if (b[0] == '\0') break;
792 if ((b[0] == 'e') && (b[1] == 'n') && (b[2] == 'd'))
797 fgets((char *)b,300,fp);
805 /* num to copy to make it a multiple of 8 */
807 memcpy(&(out[tot]),bb,(unsigned int)j);
809 memcpy(bb,&(bb[j]),(unsigned int)i-j);
813 memcpy(&(out[tot]),bb,(unsigned int)i);
819 #define ccc2l(c,l) (l =((DES_LONG)(*((c)++)))<<16, \
820 l|=((DES_LONG)(*((c)++)))<< 8, \
821 l|=((DES_LONG)(*((c)++))))
823 #define l2ccc(l,c) (*((c)++)=(unsigned char)(((l)>>16)&0xff), \
824 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
825 *((c)++)=(unsigned char)(((l) )&0xff))
828 int uuencode(unsigned char *in, int num, unsigned char *out)
832 register unsigned char *p;
835 for (j=0; j<num; j+=45)
844 *(p++)=((l>>18)&0x3f)+' ';
845 *(p++)=((l>>12)&0x3f)+' ';
846 *(p++)=((l>> 6)&0x3f)+' ';
847 *(p++)=((l )&0x3f)+' ';
858 int uudecode(unsigned char *in, int num, unsigned char *out)
861 unsigned int n=0,space=0;
864 unsigned int blank=(unsigned int)'\n'-' ';
876 fprintf(stderr,"uuencoded line length too long\n");
881 for (i=0; i<n; j+=4,i+=3)
883 /* the following is for cases where spaces are
884 * removed from lines.
897 if ((w > 63) || (x > 63) || (y > 63) || (z > 63))
913 fprintf(stderr,"bad uuencoded data values\n");
919 l=(w<<18)|(x<<12)|(y<< 6)|(z );
924 fprintf(stderr,"missing nl in uuencoded line\n");