]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/des/des.c
215d7413c0766bd13ecbe7a02d02fe7a7cd0b5df
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.]
64 #include <openssl/opensslconf.h>
65 #include OPENSSL_UNISTD
69 #else /* not __DECC */
85 #include <sys/types.h>
89 #include <openssl/des.h>
90 #include <openssl/rand.h>
93 void doencryption(void);
94 int uufwrite(unsigned char *data
, int size
, unsigned int num
, FILE *fp
);
95 void uufwriteEnd(FILE *fp
);
96 int uufread(unsigned char *out
,int size
,unsigned int num
,FILE *fp
);
97 int uuencode(unsigned char *in
,int num
,unsigned char *out
);
98 int uudecode(unsigned char *in
,int num
,unsigned char *out
);
99 void des_3cbc_encrypt(des_cblock
*input
,des_cblock
*output
,long length
,
100 des_key_schedule sk1
,des_key_schedule sk2
,
101 des_cblock
*ivec1
,des_cblock
*ivec2
,int enc
);
103 #define EXIT(a) exit(a&0x10000000L)
105 #define EXIT(a) exit(a)
108 #define BUFSIZE (8*1024)
111 #define KEYSIZB 1024 /* should hit tty line limit first :-) */
113 int do_encrypt
,longk
=0;
114 FILE *DES_IN
,*DES_OUT
,*CKSUM_OUT
;
116 unsigned char uubuf
[50];
118 #define INUUBUFN (45*100)
119 #define OUTUUBUF (65*100)
120 unsigned char b
[OUTUUBUF
];
121 unsigned char bb
[300];
122 des_cblock cksum
={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
123 char cksumname
[200]="";
125 int vflag
,cflag
,eflag
,dflag
,kflag
,bflag
,fflag
,sflag
,uflag
,flag3
,hflag
,error
;
127 int main(int argc
, char **argv
)
130 struct stat ins
,outs
;
132 char *in
=NULL
,*out
=NULL
;
134 vflag
=cflag
=eflag
=dflag
=kflag
=hflag
=bflag
=fflag
=sflag
=uflag
=flag3
=0;
136 memset(key
,0,sizeof(key
));
138 for (i
=1; i
<argc
; i
++)
141 if ((p
[0] == '-') && (p
[1] != '\0'))
154 strncpy(cksumname
,p
,200);
155 p
+=strlen(cksumname
);
160 strncpy(cksumname
,p
,200);
161 p
+=strlen(cksumname
);
191 strncpy(uuname
,p
,200);
201 fputs("must have a key with the -k option\n",stderr
);
209 strncpy(key
,argv
[i
],KEYSIZB
);
210 for (j
=strlen(argv
[i
])-1; j
>=0; j
--)
215 fprintf(stderr
,"'%c' unknown flag\n",p
[-1]);
225 else if (out
== NULL
)
236 * do decrypt then ckecksum or
237 * do checksum then encrypt
239 if (((eflag
+dflag
) == 1) || cflag
)
241 if (eflag
) do_encrypt
=DES_ENCRYPT
;
242 if (dflag
) do_encrypt
=DES_DECRYPT
;
249 fprintf(stderr
,"des(1) built with %s\n",libdes_version
);
257 if (vflag
) fprintf(stderr
,"des(1) built with %s\n",libdes_version
);
262 (stat(in
,&ins
) != -1) &&
263 (stat(out
,&outs
) != -1) &&
264 (ins
.st_dev
== outs
.st_dev
) &&
265 (ins
.st_ino
== outs
.st_ino
))
267 (strcmp(in
,out
) == 0))
270 fputs("input and output file are the same\n",stderr
);
275 if (des_read_pw_string(key
,KEYSIZB
+1,"Enter key:",eflag
?VERIFY
:0))
277 fputs("password error\n",stderr
);
283 else if ((DES_IN
=fopen(in
,"r")) == NULL
)
285 perror("opening input file");
295 else if ((DES_OUT
=fopen(out
,"w")) == NULL
)
297 perror("opening output file");
302 /* This should set the file to binary mode. */
305 if (!(uflag
&& dflag
))
306 setmode(fileno(DES_IN
),O_BINARY
);
307 if (!(uflag
&& eflag
))
308 setmode(fileno(DES_OUT
),O_BINARY
);
321 static const char *Usage
[]={
322 "des <options> [input-file [output-file]]",
324 "-v : des(1) version number",
325 "-e : encrypt using SunOS compatible user key to DES key conversion.",
327 "-d : decrypt using SunOS compatible user key to DES key conversion.",
329 "-c[ckname] : generate a cbc_cksum using SunOS compatible user key to",
330 " DES key conversion and output to ckname (stdout default,",
331 " stderr if data being output on stdout). The checksum is",
332 " generated before encryption and after decryption if used",
333 " in conjunction with -[eEdD].",
334 "-C[ckname] : generate a cbc_cksum as for -c but compatible with -[ED].",
335 "-k key : use key 'key'",
336 "-h : the key that is entered will be a hexadecimal number",
337 " that is used directly as the des key",
338 "-u[uuname] : input file is uudecoded if -[dD] or output uuencoded data if -[eE]",
339 " (uuname is the filename to put in the uuencode header).",
340 "-b : encrypt using DES in ecb encryption mode, the default is cbc mode.",
341 "-3 : encrypt using triple DES encryption. This uses 2 keys",
342 " generated from the input key. If the input key is less",
343 " than 8 characters long, this is equivalent to normal",
344 " encryption. Default is triple cbc, -b makes it triple ecb.",
347 for (u
=(char **)Usage
; *u
; u
++)
356 void doencryption(void)
359 extern unsigned long time();
363 des_key_schedule ks
,ks2
;
366 int num
=0,j
,k
,l
,rem
,ll
,len
,last
,ex
=0;
371 static unsigned char buf
[BUFSIZE
+8],obuf
[BUFSIZE
+8];
373 static unsigned char *buf
=NULL
,*obuf
=NULL
;
377 if ( (( buf
=OPENSSL_malloc(BUFSIZE
+8)) == NULL
) ||
378 ((obuf
=OPENSSL_malloc(BUFSIZE
+8)) == NULL
))
380 fputs("Not enough memory\n",stderr
);
394 if ((*p
<= '9') && (*p
>= '0'))
396 else if ((*p
<= 'f') && (*p
>= 'a'))
398 else if ((*p
<= 'F') && (*p
>= 'A'))
402 fputs("Bad hex key\n",stderr
);
407 if ((*p
<= '9') && (*p
>= '0'))
409 else if ((*p
<= 'f') && (*p
>= 'a'))
411 else if ((*p
<= 'F') && (*p
>= 'A'))
415 fputs("Bad hex key\n",stderr
);
425 des_set_key_unchecked(&k2
,ks2
);
426 memset(k2
,0,sizeof(k2
));
428 else if (longk
|| flag3
)
432 des_string_to_2keys(key
,&kk
,&k2
);
433 des_set_key_unchecked(&k2
,ks2
);
434 memset(k2
,0,sizeof(k2
));
437 des_string_to_key(key
,&kk
);
440 for (i
=0; i
<KEYSIZ
; i
++)
455 des_set_key_unchecked(&kk
,ks
);
456 memset(key
,0,sizeof(key
));
457 memset(kk
,0,sizeof(kk
));
458 /* woops - A bug that does not showup under unix :-( */
459 memset(iv
,0,sizeof(iv
));
460 memset(iv2
,0,sizeof(iv2
));
465 if (eflag
|| (!dflag
&& cflag
))
469 num
=l
=fread(&(buf
[rem
]),1,BUFSIZE
,DES_IN
);
474 perror("read error");
483 for (i
=7-rem
; i
>0; i
--)
484 RAND_pseudo_bytes(buf
+ l
++, 1);
494 des_cbc_cksum(buf
,&cksum
,
495 (long)len
,ks
,&cksum
);
498 if (feof(DES_IN
)) break;
506 (des_cblock
*)&(buf
[i
]),
507 (des_cblock
*)&(obuf
[i
]),
509 else if (flag3
&& bflag
)
512 (des_cblock
*)&(buf
[i
]),
513 (des_cblock
*)&(obuf
[i
]),
515 else if (flag3
&& !bflag
)
519 if (rem
) memcpy(tmpbuf
,&(buf
[l
]),
522 (des_cblock
*)buf
,(des_cblock
*)obuf
,
525 if (rem
) memcpy(&(buf
[l
]),tmpbuf
,
532 (long)l
,ks
,&iv
,do_encrypt
);
533 if (l
>= 8) memcpy(iv
,&(obuf
[l
-8]),8);
535 if (rem
) memcpy(buf
,&(buf
[l
]),(unsigned int)rem
);
541 j
=uufwrite(obuf
,1,(unsigned int)l
-i
,
544 j
=fwrite(obuf
,1,(unsigned int)l
-i
,
548 perror("Write error");
556 if (uflag
) uufwriteEnd(DES_OUT
);
568 l
=uufread(buf
,1,BUFSIZE
,DES_IN
);
570 l
=fread(buf
,1,BUFSIZE
,DES_IN
);
577 perror("read error");
585 (des_cblock
*)&(buf
[i
]),
586 (des_cblock
*)&(obuf
[i
]),
588 else if (flag3
&& bflag
)
591 (des_cblock
*)&(buf
[i
]),
592 (des_cblock
*)&(obuf
[i
]),
594 else if (flag3
&& !bflag
)
597 (des_cblock
*)buf
,(des_cblock
*)obuf
,
605 (long)l
,ks
,&iv
,do_encrypt
);
606 if (l
>= 8) memcpy(iv
,&(buf
[l
-8]),8);
610 ll
=uufread(&(buf
[rem
]),1,BUFSIZE
,DES_IN
);
612 ll
=fread(&(buf
[rem
]),1,BUFSIZE
,DES_IN
);
616 if (feof(DES_IN
) && (ll
== 0))
620 if ((last
> 7) || (last
< 0))
622 fputs("The file was not decrypted correctly.\n",
630 if (cflag
) des_cbc_cksum(obuf
,
631 (des_cblock
*)cksum
,(long)l
/8*8,ks
,
632 (des_cblock
*)cksum
);
635 j
=fwrite(obuf
,1,(unsigned int)l
-i
,DES_OUT
);
638 perror("Write error");
645 if ((l
== 0) && feof(DES_IN
)) break;
651 if (cksumname
[0] != '\0')
653 if ((O
=fopen(cksumname
,"w")) != NULL
)
660 fprintf(CKSUM_OUT
,"%02X",cksum
[i
]);
661 fprintf(CKSUM_OUT
,"\n");
662 if (l
) fclose(CKSUM_OUT
);
665 memset(buf
,0,sizeof(buf
));
666 memset(obuf
,0,sizeof(obuf
));
667 memset(ks
,0,sizeof(ks
));
668 memset(ks2
,0,sizeof(ks2
));
669 memset(iv
,0,sizeof(iv
));
670 memset(iv2
,0,sizeof(iv2
));
671 memset(kk
,0,sizeof(kk
));
672 memset(k2
,0,sizeof(k2
));
673 memset(uubuf
,0,sizeof(uubuf
));
674 memset(b
,0,sizeof(b
));
675 memset(bb
,0,sizeof(bb
));
676 memset(cksum
,0,sizeof(cksum
));
677 if (Exit
) EXIT(Exit
);
680 /* We ignore this parameter but it should be > ~50 I believe */
681 int uufwrite(unsigned char *data
, int size
, unsigned int num
, FILE *fp
)
683 int i
,j
,left
,rem
,ret
=num
;
688 fprintf(fp
,"begin 600 %s\n",
689 (uuname
[0] == '\0')?"text.d":uuname
);
695 if (uubufnum
+num
< 45)
697 memcpy(&(uubuf
[uubufnum
]),data
,(unsigned int)num
);
704 memcpy(&(uubuf
[uubufnum
]),data
,(unsigned int)i
);
705 j
=uuencode((unsigned char *)uubuf
,45,b
);
706 fwrite(b
,1,(unsigned int)j
,fp
);
713 for (i
=0; i
<(((int)num
)-INUUBUFN
); i
+=INUUBUFN
)
715 j
=uuencode(&(data
[i
]),INUUBUFN
,b
);
716 fwrite(b
,1,(unsigned int)j
,fp
);
722 j
=uuencode(&(data
[i
]),left
,b
);
723 fwrite(b
,1,(unsigned int)j
,fp
);
728 memcpy(uubuf
,&(data
[i
]),(unsigned int)rem
);
734 void uufwriteEnd(FILE *fp
)
737 static const char *end
=" \nend\n";
741 uubuf
[uubufnum
]='\0';
742 uubuf
[uubufnum
+1]='\0';
743 uubuf
[uubufnum
+2]='\0';
744 j
=uuencode(uubuf
,uubufnum
,b
);
745 fwrite(b
,1,(unsigned int)j
,fp
);
747 fwrite(end
,1,strlen(end
),fp
);
750 /* int size: should always be > ~ 60; I actually ignore this parameter :-) */
751 int uufread(unsigned char *out
, int size
, unsigned int num
, FILE *fp
)
763 fgets((char *)b
,300,fp
);
766 fprintf(stderr
,"no 'begin' found in uuencoded input\n");
769 if (strncmp((char *)b
,"begin ",6) == 0) break;
777 memcpy(out
,bb
,(unsigned int)valid
);
784 fgets((char *)b
,300,fp
);
785 if (b
[0] == '\0') break;
787 if ((b
[0] == 'e') && (b
[1] == 'n') && (b
[2] == 'd'))
792 fgets((char *)b
,300,fp
);
800 /* num to copy to make it a multiple of 8 */
802 memcpy(&(out
[tot
]),bb
,(unsigned int)j
);
804 memcpy(bb
,&(bb
[j
]),(unsigned int)i
-j
);
808 memcpy(&(out
[tot
]),bb
,(unsigned int)i
);
814 #define ccc2l(c,l) (l =((DES_LONG)(*((c)++)))<<16, \
815 l|=((DES_LONG)(*((c)++)))<< 8, \
816 l|=((DES_LONG)(*((c)++))))
818 #define l2ccc(l,c) (*((c)++)=(unsigned char)(((l)>>16)&0xff), \
819 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
820 *((c)++)=(unsigned char)(((l) )&0xff))
823 int uuencode(unsigned char *in
, int num
, unsigned char *out
)
827 register unsigned char *p
;
830 for (j
=0; j
<num
; j
+=45)
839 *(p
++)=((l
>>18)&0x3f)+' ';
840 *(p
++)=((l
>>12)&0x3f)+' ';
841 *(p
++)=((l
>> 6)&0x3f)+' ';
842 *(p
++)=((l
)&0x3f)+' ';
853 int uudecode(unsigned char *in
, int num
, unsigned char *out
)
856 unsigned int n
=0,space
=0;
859 unsigned int blank
=(unsigned int)'\n'-' ';
871 fprintf(stderr
,"uuencoded line length too long\n");
876 for (i
=0; i
<n
; j
+=4,i
+=3)
878 /* the following is for cases where spaces are
879 * removed from lines.
892 if ((w
> 63) || (x
> 63) || (y
> 63) || (z
> 63))
908 fprintf(stderr
,"bad uuencoded data values\n");
914 l
=(w
<<18)|(x
<<12)|(y
<< 6)|(z
);
919 fprintf(stderr
,"missing nl in uuencoded line\n");