1998-12-21 10:52:47 +00:00
|
|
|
/* crypto/des/enc_writ.c */
|
1998-12-21 10:56:39 +00:00
|
|
|
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
1998-12-21 10:52:47 +00:00
|
|
|
* 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
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 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 <errno.h>
|
|
|
|
#include <time.h>
|
1999-03-14 01:16:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "cryptlib.h"
|
1998-12-21 10:52:47 +00:00
|
|
|
#include "des_locl.h"
|
1999-07-15 23:47:02 +00:00
|
|
|
#include <openssl/rand.h>
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-03-29 16:07:36 +00:00
|
|
|
/*
|
|
|
|
* WARNINGS:
|
|
|
|
*
|
|
|
|
* - The data format used by des_enc_write() and des_enc_read()
|
|
|
|
* has a cryptographic weakness: When asked to write more
|
|
|
|
* than MAXWRITE bytes, des_enc_write will split the data
|
|
|
|
* into several chunks that are all encrypted
|
|
|
|
* using the same IV. So don't use these functions unless you
|
|
|
|
* are sure you know what you do (in which case you might
|
|
|
|
* not want to use them anyway).
|
|
|
|
*
|
|
|
|
* - This code cannot handle non-blocking sockets.
|
|
|
|
*/
|
|
|
|
|
1999-04-23 15:01:15 +00:00
|
|
|
int des_enc_write(int fd, const void *_buf, int len,
|
1999-05-16 12:26:16 +00:00
|
|
|
des_key_schedule sched, des_cblock *iv)
|
1998-12-21 10:52:47 +00:00
|
|
|
{
|
|
|
|
#ifdef _LIBC
|
|
|
|
extern unsigned long time();
|
|
|
|
extern int write();
|
|
|
|
#endif
|
1999-04-23 15:01:15 +00:00
|
|
|
const unsigned char *buf=_buf;
|
1998-12-21 10:52:47 +00:00
|
|
|
long rnum;
|
|
|
|
int i,j,k,outnum;
|
1999-04-20 22:50:42 +00:00
|
|
|
static unsigned char *outbuf=NULL;
|
|
|
|
unsigned char shortbuf[8];
|
1999-04-23 15:01:15 +00:00
|
|
|
unsigned char *p;
|
1999-04-20 22:50:42 +00:00
|
|
|
const unsigned char *cp;
|
1998-12-21 10:52:47 +00:00
|
|
|
static int start=1;
|
|
|
|
|
|
|
|
if (outbuf == NULL)
|
|
|
|
{
|
2000-06-08 11:00:37 +00:00
|
|
|
outbuf=OPENSSL_malloc(BSIZE+HDRSIZE);
|
1998-12-21 10:52:47 +00:00
|
|
|
if (outbuf == NULL) return(-1);
|
|
|
|
}
|
|
|
|
/* If we are sending less than 8 bytes, the same char will look
|
|
|
|
* the same if we don't pad it out with random bytes */
|
|
|
|
if (start)
|
|
|
|
{
|
|
|
|
start=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lets recurse if we want to send the data in small chunks */
|
|
|
|
if (len > MAXWRITE)
|
|
|
|
{
|
|
|
|
j=0;
|
|
|
|
for (i=0; i<len; i+=k)
|
|
|
|
{
|
|
|
|
k=des_enc_write(fd,&(buf[i]),
|
|
|
|
((len-i) > MAXWRITE)?MAXWRITE:(len-i),sched,iv);
|
|
|
|
if (k < 0)
|
|
|
|
return(k);
|
|
|
|
else
|
|
|
|
j+=k;
|
|
|
|
}
|
|
|
|
return(j);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write length first */
|
|
|
|
p=outbuf;
|
|
|
|
l2n(len,p);
|
|
|
|
|
|
|
|
/* pad short strings */
|
|
|
|
if (len < 8)
|
|
|
|
{
|
1999-02-13 18:52:38 +00:00
|
|
|
cp=shortbuf;
|
1999-04-21 13:25:40 +00:00
|
|
|
memcpy(shortbuf,buf,len);
|
2000-01-21 01:15:56 +00:00
|
|
|
RAND_pseudo_bytes(shortbuf+len, 8-len);
|
1998-12-21 10:52:47 +00:00
|
|
|
rnum=8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-01-30 22:20:28 +00:00
|
|
|
cp=buf;
|
1998-12-21 10:52:47 +00:00
|
|
|
rnum=((len+7)/8*8); /* round up to nearest eight */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (des_rw_mode & DES_PCBC_MODE)
|
1999-02-13 18:52:38 +00:00
|
|
|
des_pcbc_encrypt(cp,&(outbuf[HDRSIZE]),(len<8)?8:len,sched,iv,
|
|
|
|
DES_ENCRYPT);
|
1998-12-21 10:52:47 +00:00
|
|
|
else
|
1999-02-13 18:52:38 +00:00
|
|
|
des_cbc_encrypt(cp,&(outbuf[HDRSIZE]),(len<8)?8:len,sched,iv,
|
|
|
|
DES_ENCRYPT);
|
1998-12-21 10:52:47 +00:00
|
|
|
|
|
|
|
/* output */
|
1999-04-21 13:25:40 +00:00
|
|
|
outnum=rnum+HDRSIZE;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
|
|
|
for (j=0; j<outnum; j+=i)
|
|
|
|
{
|
|
|
|
/* eay 26/08/92 I was not doing writing from where we
|
2000-02-03 23:23:24 +00:00
|
|
|
* got up to. */
|
1999-12-19 16:07:19 +00:00
|
|
|
i=write(fd,(void *)&(outbuf[j]),outnum-j);
|
1998-12-21 10:52:47 +00:00
|
|
|
if (i == -1)
|
|
|
|
{
|
1999-12-19 16:07:19 +00:00
|
|
|
#ifdef EINTR
|
1998-12-21 10:52:47 +00:00
|
|
|
if (errno == EINTR)
|
|
|
|
i=0;
|
1999-12-19 16:07:19 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
/* This is really a bad error - very bad
|
1998-12-21 10:52:47 +00:00
|
|
|
* It will stuff-up both ends. */
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(len);
|
|
|
|
}
|