]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/des/des.c
2 /* Copyright (C) 1995-1997 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
);
113 #define EXIT(a) exit(a&0x10000000)
115 #define EXIT(a) exit(a)
118 #define BUFSIZE (8*1024)
121 #define KEYSIZB 1024 /* should hit tty line limit first :-) */
123 int do_encrypt
,longk
=0;
124 FILE *DES_IN
,*DES_OUT
,*CKSUM_OUT
;
126 unsigned char uubuf
[50];
128 #define INUUBUFN (45*100)
129 #define OUTUUBUF (65*100)
130 unsigned char b
[OUTUUBUF
];
131 unsigned char bb
[300];
132 des_cblock cksum
={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
133 char cksumname
[200]="";
135 int vflag
,cflag
,eflag
,dflag
,kflag
,bflag
,fflag
,sflag
,uflag
,flag3
,hflag
,error
;
142 struct stat ins
,outs
;
144 char *in
=NULL
,*out
=NULL
;
146 vflag
=cflag
=eflag
=dflag
=kflag
=hflag
=bflag
=fflag
=sflag
=uflag
=flag3
=0;
148 memset(key
,0,sizeof(key
));
150 for (i
=1; i
<argc
; i
++)
153 if ((p
[0] == '-') && (p
[1] != '\0'))
166 strncpy(cksumname
,p
,200);
167 p
+=strlen(cksumname
);
172 strncpy(cksumname
,p
,200);
173 p
+=strlen(cksumname
);
203 strncpy(uuname
,p
,200);
213 fputs("must have a key with the -k option\n",stderr
);
221 strncpy(key
,argv
[i
],KEYSIZB
);
222 for (j
=strlen(argv
[i
])-1; j
>=0; j
--)
227 fprintf(stderr
,"'%c' unknown flag\n",p
[-1]);
237 else if (out
== NULL
)
248 * do decrypt then ckecksum or
249 * do checksum then encrypt
251 if (((eflag
+dflag
) == 1) || cflag
)
253 if (eflag
) do_encrypt
=DES_ENCRYPT
;
254 if (dflag
) do_encrypt
=DES_DECRYPT
;
261 fprintf(stderr
,"des(1) built with %s\n",libdes_version
);
269 if (vflag
) fprintf(stderr
,"des(1) built with %s\n",libdes_version
);
274 (stat(in
,&ins
) != -1) &&
275 (stat(out
,&outs
) != -1) &&
276 (ins
.st_dev
== outs
.st_dev
) &&
277 (ins
.st_ino
== outs
.st_ino
))
279 (strcmp(in
,out
) == 0))
282 fputs("input and output file are the same\n",stderr
);
287 if (des_read_pw_string(key
,KEYSIZB
+1,"Enter key:",eflag
?VERIFY
:0))
289 fputs("password error\n",stderr
);
295 else if ((DES_IN
=fopen(in
,"r")) == NULL
)
297 perror("opening input file");
307 else if ((DES_OUT
=fopen(out
,"w")) == NULL
)
309 perror("opening output file");
314 /* This should set the file to binary mode. */
317 if (!(uflag
&& dflag
))
318 setmode(fileno(DES_IN
),O_BINARY
);
319 if (!(uflag
&& eflag
))
320 setmode(fileno(DES_OUT
),O_BINARY
);
333 static const char *Usage
[]={
334 "des <options> [input-file [output-file]]",
336 "-v : des(1) version number",
337 "-e : encrypt using sunOS compatible user key to DES key conversion.",
339 "-d : decrypt using sunOS compatible user key to DES key conversion.",
341 "-c[ckname] : generate a cbc_cksum using sunOS compatible user key to",
342 " DES key conversion and output to ckname (stdout default,",
343 " stderr if data being output on stdout). The checksum is",
344 " generated before encryption and after decryption if used",
345 " in conjunction with -[eEdD].",
346 "-C[ckname] : generate a cbc_cksum as for -c but compatible with -[ED].",
347 "-k key : use key 'key'",
348 "-h : the key that is entered will be a hexidecimal number",
349 " that is used directly as the des key",
350 "-u[uuname] : input file is uudecoded if -[dD] or output uuencoded data if -[eE]",
351 " (uuname is the filename to put in the uuencode header).",
352 "-b : encrypt using DES in ecb encryption mode, the defaut is cbc mode.",
353 "-3 : encrypt using tripple DES encryption. This uses 2 keys",
354 " generated from the input key. If the input key is less",
355 " than 8 characters long, this is equivelent to normal",
356 " encryption. Default is tripple cbc, -b makes it tripple ecb.",
359 for (u
=(char **)Usage
; *u
; u
++)
371 extern int srandom();
373 extern unsigned long time();
377 des_key_schedule ks
,ks2
;
378 unsigned char iv
[8],iv2
[8];
380 int num
=0,j
,k
,l
,rem
,ll
,len
,last
,ex
=0;
385 static unsigned char buf
[BUFSIZE
+8],obuf
[BUFSIZE
+8];
387 static unsigned char *buf
=NULL
,*obuf
=NULL
;
391 if ( (( buf
=(unsigned char *)Malloc(BUFSIZE
+8)) == NULL
) ||
392 ((obuf
=(unsigned char *)Malloc(BUFSIZE
+8)) == NULL
))
394 fputs("Not enough memory\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
);
421 if ((*p
<= '9') && (*p
>= '0'))
423 else if ((*p
<= 'f') && (*p
>= 'a'))
425 else if ((*p
<= 'F') && (*p
>= 'A'))
429 fputs("Bad hex key\n",stderr
);
439 des_set_key((C_Block
*)k2
,ks2
);
440 memset(k2
,0,sizeof(k2
));
442 else if (longk
|| flag3
)
446 des_string_to_2keys(key
,(C_Block
*)kk
,(C_Block
*)k2
);
447 des_set_key((C_Block
*)k2
,ks2
);
448 memset(k2
,0,sizeof(k2
));
451 des_string_to_key(key
,(C_Block
*)kk
);
454 for (i
=0; i
<KEYSIZ
; i
++)
469 des_set_key((C_Block
*)kk
,ks
);
470 memset(key
,0,sizeof(key
));
471 memset(kk
,0,sizeof(kk
));
472 /* woops - A bug that does not showup under unix :-( */
473 memset(iv
,0,sizeof(iv
));
474 memset(iv2
,0,sizeof(iv2
));
479 if (eflag
|| (!dflag
&& cflag
))
483 num
=l
=fread(&(buf
[rem
]),1,BUFSIZE
,DES_IN
);
488 perror("read error");
497 srandom((unsigned int)time(NULL
));
498 for (i
=7-rem
; i
>0; i
--)
499 buf
[l
++]=random()&0xff;
509 des_cbc_cksum((C_Block
*)buf
,(C_Block
*)cksum
,
510 (long)len
,ks
,(C_Block
*)cksum
);
513 if (feof(DES_IN
)) break;
521 (des_cblock
*)&(buf
[i
]),
522 (des_cblock
*)&(obuf
[i
]),
524 else if (flag3
&& bflag
)
527 (des_cblock
*)&(buf
[i
]),
528 (des_cblock
*)&(obuf
[i
]),
530 else if (flag3
&& !bflag
)
534 if (rem
) memcpy(tmpbuf
,&(buf
[l
]),
537 (des_cblock
*)buf
,(des_cblock
*)obuf
,
538 (long)l
,ks
,ks2
,(des_cblock
*)iv
,
539 (des_cblock
*)iv2
,do_encrypt
);
540 if (rem
) memcpy(&(buf
[l
]),tmpbuf
,
546 (des_cblock
*)buf
,(des_cblock
*)obuf
,
547 (long)l
,ks
,(des_cblock
*)iv
,do_encrypt
);
548 if (l
>= 8) memcpy(iv
,&(obuf
[l
-8]),8);
550 if (rem
) memcpy(buf
,&(buf
[l
]),(unsigned int)rem
);
556 j
=uufwrite(obuf
,1,(unsigned int)l
-i
,
559 j
=fwrite(obuf
,1,(unsigned int)l
-i
,
563 perror("Write error");
571 if (uflag
) uufwriteEnd(DES_OUT
);
583 l
=uufread(buf
,1,BUFSIZE
,DES_IN
);
585 l
=fread(buf
,1,BUFSIZE
,DES_IN
);
592 perror("read error");
600 (des_cblock
*)&(buf
[i
]),
601 (des_cblock
*)&(obuf
[i
]),
603 else if (flag3
&& bflag
)
606 (des_cblock
*)&(buf
[i
]),
607 (des_cblock
*)&(obuf
[i
]),
609 else if (flag3
&& !bflag
)
612 (des_cblock
*)buf
,(des_cblock
*)obuf
,
613 (long)l
,ks
,ks2
,(des_cblock
*)iv
,
614 (des_cblock
*)iv2
,do_encrypt
);
619 (des_cblock
*)buf
,(des_cblock
*)obuf
,
620 (long)l
,ks
,(des_cblock
*)iv
,do_encrypt
);
621 if (l
>= 8) memcpy(iv
,&(buf
[l
-8]),8);
625 ll
=uufread(&(buf
[rem
]),1,BUFSIZE
,DES_IN
);
627 ll
=fread(&(buf
[rem
]),1,BUFSIZE
,DES_IN
);
631 if (feof(DES_IN
) && (ll
== 0))
635 if ((last
> 7) || (last
< 0))
637 fputs("The file was not decrypted correctly.\n",
645 if (cflag
) des_cbc_cksum((C_Block
*)obuf
,
646 (C_Block
*)cksum
,(long)l
/8*8,ks
,
650 j
=fwrite(obuf
,1,(unsigned int)l
-i
,DES_OUT
);
653 perror("Write error");
660 if ((l
== 0) && feof(DES_IN
)) break;
666 if (cksumname
[0] != '\0')
668 if ((O
=fopen(cksumname
,"w")) != NULL
)
675 fprintf(CKSUM_OUT
,"%02X",cksum
[i
]);
676 fprintf(CKSUM_OUT
,"\n");
677 if (l
) fclose(CKSUM_OUT
);
680 memset(buf
,0,sizeof(buf
));
681 memset(obuf
,0,sizeof(obuf
));
682 memset(ks
,0,sizeof(ks
));
683 memset(ks2
,0,sizeof(ks2
));
684 memset(iv
,0,sizeof(iv
));
685 memset(iv2
,0,sizeof(iv2
));
686 memset(kk
,0,sizeof(kk
));
687 memset(k2
,0,sizeof(k2
));
688 memset(uubuf
,0,sizeof(uubuf
));
689 memset(b
,0,sizeof(b
));
690 memset(bb
,0,sizeof(bb
));
691 memset(cksum
,0,sizeof(cksum
));
692 if (Exit
) EXIT(Exit
);
695 int uufwrite(data
, size
, num
, fp
)
701 /* We ignore this parameter but it should be > ~50 I believe */
705 int i
,j
,left
,rem
,ret
=num
;
710 fprintf(fp
,"begin 600 %s\n",
711 (uuname
[0] == '\0')?"text.d":uuname
);
717 if (uubufnum
+num
< 45)
719 memcpy(&(uubuf
[uubufnum
]),data
,(unsigned int)num
);
726 memcpy(&(uubuf
[uubufnum
]),data
,(unsigned int)i
);
727 j
=uuencode((unsigned char *)uubuf
,45,b
);
728 fwrite(b
,1,(unsigned int)j
,fp
);
735 for (i
=0; i
<(((int)num
)-INUUBUFN
); i
+=INUUBUFN
)
737 j
=uuencode(&(data
[i
]),INUUBUFN
,b
);
738 fwrite(b
,1,(unsigned int)j
,fp
);
744 j
=uuencode(&(data
[i
]),left
,b
);
745 fwrite(b
,1,(unsigned int)j
,fp
);
750 memcpy(uubuf
,&(data
[i
]),(unsigned int)rem
);
760 static const char *end
=" \nend\n";
764 uubuf
[uubufnum
]='\0';
765 uubuf
[uubufnum
+1]='\0';
766 uubuf
[uubufnum
+2]='\0';
767 j
=uuencode(uubuf
,uubufnum
,b
);
768 fwrite(b
,1,(unsigned int)j
,fp
);
770 fwrite(end
,1,strlen(end
),fp
);
773 int uufread(out
, size
, num
, fp
)
775 int size
; /* should always be > ~ 60; I actually ignore this parameter :-) */
789 fgets((char *)b
,300,fp
);
792 fprintf(stderr
,"no 'begin' found in uuencoded input\n");
795 if (strncmp((char *)b
,"begin ",6) == 0) break;
803 memcpy(out
,bb
,(unsigned int)valid
);
810 fgets((char *)b
,300,fp
);
811 if (b
[0] == '\0') break;
813 if ((b
[0] == 'e') && (b
[1] == 'n') && (b
[2] == 'd'))
818 fgets((char *)b
,300,fp
);
826 /* num to copy to make it a multiple of 8 */
828 memcpy(&(out
[tot
]),bb
,(unsigned int)j
);
830 memcpy(bb
,&(bb
[j
]),(unsigned int)i
-j
);
834 memcpy(&(out
[tot
]),bb
,(unsigned int)i
);
840 #define ccc2l(c,l) (l =((DES_LONG)(*((c)++)))<<16, \
841 l|=((DES_LONG)(*((c)++)))<< 8, \
842 l|=((DES_LONG)(*((c)++))))
844 #define l2ccc(l,c) (*((c)++)=(unsigned char)(((l)>>16)&0xff), \
845 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
846 *((c)++)=(unsigned char)(((l) )&0xff))
849 int uuencode(in
, num
, out
)
856 register unsigned char *p
;
859 for (j
=0; j
<num
; j
+=45)
868 *(p
++)=((l
>>18)&0x3f)+' ';
869 *(p
++)=((l
>>12)&0x3f)+' ';
870 *(p
++)=((l
>> 6)&0x3f)+' ';
871 *(p
++)=((l
)&0x3f)+' ';
882 int uudecode(in
, num
, out
)
888 unsigned int n
=0,space
=0;
891 unsigned int blank
=(unsigned int)'\n'-' ';
903 fprintf(stderr
,"uuencoded line length too long\n");
908 for (i
=0; i
<n
; j
+=4,i
+=3)
910 /* the following is for cases where spaces are
911 * removed from lines.
924 if ((w
> 63) || (x
> 63) || (y
> 63) || (z
> 63))
940 fprintf(stderr
,"bad uuencoded data values\n");
946 l
=(w
<<18)|(x
<<12)|(y
<< 6)|(z
);
951 fprintf(stderr
,"missing nl in uuencoded line\n");