des.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/opensslconf.h>
#ifndef OPENSSL_SYS_MSDOS
#ifndef OPENSSL_SYS_VMS
#include OPENSSL_UNISTD
#else /* OPENSSL_SYS_VMS */
#ifdef __DECC
#include <unistd.h>
#else /* not __DECC */
#include <math.h>
#endif /* __DECC */
#endif /* OPENSSL_SYS_VMS */
#else /* OPENSSL_SYS_MSDOS */
#include <io.h>
#endif
#include <time.h>
#include "des_ver.h"
#ifdef OPENSSL_SYS_VMS
#include <types.h>
#include <stat.h>
#else
#ifndef _IRIX
#endif
#endif
#include <openssl/ui_compat.h>
void usage(void);
void doencryption(void);
#ifdef OPENSSL_SYS_VMS
#else
#endif
#define VERIFY 1
#define KEYSIZ 8
int do_encrypt,longk=0;
char uuname[200];
unsigned char uubuf[50];
int uubufnum=0;
unsigned char b[OUTUUBUF];
unsigned char bb[300];
{
int i;
char *p;
error=0;
for (i=1; i<argc; i++)
{
p=argv[i];
if ((p[0] == '-') && (p[1] != '\0'))
{
p++;
while (*p)
{
switch (*(p++))
{
case '3':
flag3=1;
longk=1;
break;
case 'c':
cflag=1;
break;
case 'C':
cflag=1;
longk=1;
break;
case 'e':
eflag=1;
break;
case 'v':
vflag=1;
break;
case 'E':
eflag=1;
longk=1;
break;
case 'd':
dflag=1;
break;
case 'D':
dflag=1;
longk=1;
break;
case 'b':
bflag=1;
break;
case 'f':
fflag=1;
break;
case 's':
sflag=1;
break;
case 'u':
uflag=1;
break;
case 'h':
hflag=1;
break;
case 'k':
kflag=1;
if ((i+1) == argc)
{
error=1;
}
else
{
int j;
i++;
argv[i][j]='\0';
}
break;
default:
error=1;
break;
}
}
}
else
{
else
error=1;
}
}
/* We either
* do checksum or
* do encrypt or
* do decrypt or
* do decrypt then ckecksum or
* do checksum then encrypt
*/
{
}
else
{
if (vflag)
{
#ifndef _Windows
#endif
EXIT(1);
}
else usage();
}
#ifndef _Windows
#endif
#ifndef OPENSSL_SYS_MSDOS
#else /* OPENSSL_SYS_MSDOS */
#endif
{
EXIT(3);
}
if (!kflag)
{
EXIT(2);
}
{
perror("opening input file");
EXIT(4);
}
{
}
{
perror("opening output file");
EXIT(5);
}
#ifdef OPENSSL_SYS_MSDOS
/* This should set the file to binary mode. */
{
#include <fcntl.h>
}
#endif
doencryption();
EXIT(0);
}
void usage(void)
{
char **u;
static const char *Usage[]={
"des <options> [input-file [output-file]]",
"options:",
"-v : des(1) version number",
"-e : encrypt using SunOS compatible user key to DES key conversion.",
"-E : encrypt ",
"-d : decrypt using SunOS compatible user key to DES key conversion.",
"-D : decrypt ",
"-c[ckname] : generate a cbc_cksum using SunOS compatible user key to",
" DES key conversion and output to ckname (stdout default,",
" stderr if data being output on stdout). The checksum is",
" generated before encryption and after decryption if used",
" in conjunction with -[eEdD].",
"-C[ckname] : generate a cbc_cksum as for -c but compatible with -[ED].",
"-k key : use key 'key'",
"-h : the key that is entered will be a hexadecimal number",
" that is used directly as the des key",
"-u[uuname] : input file is uudecoded if -[dD] or output uuencoded data if -[eE]",
" (uuname is the filename to put in the uuencode header).",
"-b : encrypt using DES in ecb encryption mode, the default is cbc mode.",
"-3 : encrypt using triple DES encryption. This uses 2 keys",
" generated from the input key. If the input key is less",
" than 8 characters long, this is equivalent to normal",
" encryption. Default is triple cbc, -b makes it triple ecb.",
};
for (u=(char **)Usage; *u; u++)
{
}
EXIT(1);
}
void doencryption(void)
{
#ifdef _LIBC
extern unsigned long time();
#endif
register int i;
char *p;
FILE *O;
int Exit=0;
#ifndef OPENSSL_SYS_MSDOS
#else
{
{
Exit=10;
goto problems;
}
}
#endif
if (hflag)
{
p=key;
for (i=0; i<j; i++)
{
k=0;
if ((*p <= '9') && (*p >= '0'))
k=(*p-'0')<<4;
else if ((*p <= 'f') && (*p >= 'a'))
k=(*p-'a'+10)<<4;
else if ((*p <= 'F') && (*p >= 'A'))
k=(*p-'A'+10)<<4;
else
{
Exit=9;
goto problems;
}
p++;
if ((*p <= '9') && (*p >= '0'))
k|=(*p-'0');
else if ((*p <= 'f') && (*p >= 'a'))
k|=(*p-'a'+10);
else if ((*p <= 'F') && (*p >= 'A'))
k|=(*p-'A'+10);
else
{
Exit=9;
goto problems;
}
p++;
if (i < 8)
kk[i]=k;
else
k2[i-8]=k;
}
}
{
if (flag3)
{
}
else
}
else
for (i=0; i<KEYSIZ; i++)
{
l=0;
k=key[i];
for (j=0; j<8; j++)
{
if (k&1) l++;
k>>=1;
}
if (l & 1)
else
}
/* woops - A bug that does not showup under unix :-( */
l=1;
rem=0;
/* first read */
{
for (;;)
{
l+=rem;
if (l < 0)
{
perror("read error");
Exit=6;
goto problems;
}
rem=l%8;
{
for (i=7-rem; i>0; i--)
ex=1;
}
else
l-=rem;
if (cflag)
{
if (!eflag)
{
else continue;
}
}
for (i=0; i<l; i+=8)
(DES_cblock *)&(buf[i]),
(DES_cblock *)&(obuf[i]),
&ks,do_encrypt);
for (i=0; i<l; i+=8)
(DES_cblock *)&(buf[i]),
(DES_cblock *)&(obuf[i]),
{
char tmpbuf[8];
(unsigned int)rem);
&iv2,do_encrypt);
(unsigned int)rem);
}
else
{
}
i=0;
while (i < l)
{
if (uflag)
DES_OUT);
else
DES_OUT);
if (j == -1)
{
perror("Write error");
Exit=7;
goto problems;
}
i+=j;
}
{
break;
}
}
}
else /* decrypt */
{
ex=1;
for (;;)
{
if (ex) {
if (uflag)
else
ex=0;
rem=l%8;
l-=rem;
}
if (l < 0)
{
perror("read error");
Exit=6;
goto problems;
}
for (i=0; i<l; i+=8)
(DES_cblock *)&(buf[i]),
(DES_cblock *)&(obuf[i]),
&ks,do_encrypt);
for (i=0; i<l; i+=8)
(DES_cblock *)&(buf[i]),
(DES_cblock *)&(obuf[i]),
{
&iv2,do_encrypt);
}
else
{
}
if (uflag)
else
{
{
fputs("The file was not decrypted correctly.\n",
stderr);
Exit=8;
last=0;
}
l=l-8+last;
}
i=0;
(DES_cblock *)cksum);
while (i != l)
{
if (j == -1)
{
perror("Write error");
Exit=7;
goto problems;
}
i+=j;
}
l=ll;
}
}
if (cflag)
{
l=0;
if (cksumname[0] != '\0')
{
{
CKSUM_OUT=O;
l=1;
}
}
for (i=0; i<8; i++)
}
OPENSSL_cleanse(b,sizeof(b));
}
/* We ignore this parameter but it should be > ~50 I believe */
{
static int start=1;
if (start)
{
start=0;
}
if (uubufnum)
{
{
return(num);
}
else
{
i=45-uubufnum;
uubufnum=0;
data+=i;
num-=i;
}
}
{
}
if (left)
{
i+=left;
}
if (i != num)
{
}
return(ret);
}
{
int j;
static const char *end=" \nend\n";
if (uubufnum != 0)
{
}
}
/* int size: should always be > ~ 60; I actually ignore this parameter :-) */
{
int i,j,tot;
static int done=0;
static int valid=0;
static int start=1;
if (start)
{
for (;;)
{
b[0]='\0';
if (b[0] == '\0')
{
return(-1);
}
}
start=0;
}
if (done) return(0);
tot=0;
if (valid)
{
valid=0;
}
for (;;)
{
b[0]='\0';
if (b[0] == '\0') break;
i=strlen((char *)b);
if ((b[0] == 'e') && (b[1] == 'n') && (b[2] == 'd'))
{
done=1;
{
}
break;
}
if (i < 0) break;
{
/* num to copy to make it a multiple of 8 */
tot+=j;
valid=i-j;
break;
}
tot+=i;
}
return(tot);
}
l|=((DES_LONG)(*((c)++)))<< 8, \
l|=((DES_LONG)(*((c)++))))
*((c)++)=(unsigned char)(((l)>> 8)&0xff), \
*((c)++)=(unsigned char)(((l) )&0xff))
{
int j,i,n,tot=0;
DES_LONG l;
register unsigned char *p;
p=out;
for (j=0; j<num; j+=45)
{
if (j+45 > num)
i=(num-j);
else i=45;
*(p++)=i+' ';
for (n=0; n<i; n+=3)
{
*(p++)=((l>>18)&0x3f)+' ';
*(p++)=((l>>12)&0x3f)+' ';
*(p++)=((l>> 6)&0x3f)+' ';
*(p++)=((l )&0x3f)+' ';
tot+=4;
}
*(p++)='\n';
tot+=2;
}
*p='\0';
l=0;
return(tot);
}
{
int j,i,k;
unsigned int n=0,space=0;
DES_LONG l;
DES_LONG w,x,y,z;
for (j=0; j<num; )
{
n= *(in++)-' ';
if (n == blank)
{
n=0;
in--;
}
if (n > 60)
{
return(-1);
}
j++;
for (i=0; i<n; j+=4,i+=3)
{
/* the following is for cases where spaces are
* removed from lines.
*/
if (space)
{
w=x=y=z=0;
}
else
{
w= *(in++)-' ';
x= *(in++)-' ';
y= *(in++)-' ';
z= *(in++)-' ';
}
if ((w > 63) || (x > 63) || (y > 63) || (z > 63))
{
k=0;
if (w == blank) k=1;
if (x == blank) k=2;
if (y == blank) k=3;
if (z == blank) k=4;
space=1;
switch (k) {
case 1: w=0; in--;
case 2: x=0; in--;
case 3: y=0; in--;
case 4: z=0; in--;
break;
case 0:
space=0;
w=x=y=z=0;
return(-1);
break;
}
}
l=(w<<18)|(x<<12)|(y<< 6)|(z );
}
if (*(in++) != '\n')
{
w=x=y=z=0;
return(-1);
}
j++;
}
*out='\0';
w=x=y=z=0;
return(n);
}