155714Skris/* crypto/des/enc_read.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include <errno.h>
6155714Skris#include "cryptlib.h"
6255714Skris#include "des_locl.h"
6355714Skris
6455714Skris/* This has some uglies in it but it works - even over sockets. */
6555714Skris/*extern int errno;*/
66238405SjkimOPENSSL_IMPLEMENT_GLOBAL(int,DES_rw_mode,DES_PCBC_MODE)
6755714Skris
6855714Skris
6955714Skris/*
7055714Skris * WARNINGS:
7155714Skris *
72109998Smarkm *  -  The data format used by DES_enc_write() and DES_enc_read()
7355714Skris *     has a cryptographic weakness: When asked to write more
74109998Smarkm *     than MAXWRITE bytes, DES_enc_write will split the data
7555714Skris *     into several chunks that are all encrypted
7655714Skris *     using the same IV.  So don't use these functions unless you
7755714Skris *     are sure you know what you do (in which case you might
7855714Skris *     not want to use them anyway).
7955714Skris *
8055714Skris *  -  This code cannot handle non-blocking sockets.
8155714Skris *
8255714Skris *  -  This function uses an internal state and thus cannot be
8355714Skris *     used on multiple files.
8455714Skris */
8555714Skris
8655714Skris
87109998Smarkmint DES_enc_read(int fd, void *buf, int len, DES_key_schedule *sched,
88109998Smarkm		 DES_cblock *iv)
8955714Skris	{
90238405Sjkim#if defined(OPENSSL_NO_POSIX_IO)
91238405Sjkim	return(0);
92238405Sjkim#else
9355714Skris	/* data to be unencrypted */
9455714Skris	int net_num=0;
9555714Skris	static unsigned char *net=NULL;
9655714Skris	/* extra unencrypted data
9755714Skris	 * for when a block of 100 comes in but is des_read one byte at
9855714Skris	 * a time. */
9955714Skris	static unsigned char *unnet=NULL;
10055714Skris	static int unnet_start=0;
10155714Skris	static int unnet_left=0;
10255714Skris	static unsigned char *tmpbuf=NULL;
10355714Skris	int i;
10455714Skris	long num=0,rnum;
10555714Skris	unsigned char *p;
10655714Skris
10755714Skris	if (tmpbuf == NULL)
10855714Skris		{
10968651Skris		tmpbuf=OPENSSL_malloc(BSIZE);
11055714Skris		if (tmpbuf == NULL) return(-1);
11155714Skris		}
11255714Skris	if (net == NULL)
11355714Skris		{
11468651Skris		net=OPENSSL_malloc(BSIZE);
11555714Skris		if (net == NULL) return(-1);
11655714Skris		}
11755714Skris	if (unnet == NULL)
11855714Skris		{
11968651Skris		unnet=OPENSSL_malloc(BSIZE);
12055714Skris		if (unnet == NULL) return(-1);
12155714Skris		}
12255714Skris	/* left over data from last decrypt */
12355714Skris	if (unnet_left != 0)
12455714Skris		{
12555714Skris		if (unnet_left < len)
12655714Skris			{
12755714Skris			/* we still still need more data but will return
12855714Skris			 * with the number of bytes we have - should always
12955714Skris			 * check the return value */
13055714Skris			memcpy(buf,&(unnet[unnet_start]),
13155714Skris			       unnet_left);
13255714Skris			/* eay 26/08/92 I had the next 2 lines
13355714Skris			 * reversed :-( */
13455714Skris			i=unnet_left;
13555714Skris			unnet_start=unnet_left=0;
13655714Skris			}
13755714Skris		else
13855714Skris			{
13955714Skris			memcpy(buf,&(unnet[unnet_start]),len);
14055714Skris			unnet_start+=len;
14155714Skris			unnet_left-=len;
14255714Skris			i=len;
14355714Skris			}
14455714Skris		return(i);
14555714Skris		}
14655714Skris
14755714Skris	/* We need to get more data. */
14855714Skris	if (len > MAXWRITE) len=MAXWRITE;
14955714Skris
15055714Skris	/* first - get the length */
15155714Skris	while (net_num < HDRSIZE)
15255714Skris		{
153238405Sjkim#ifndef OPENSSL_SYS_WIN32
15459191Skris		i=read(fd,(void *)&(net[net_num]),HDRSIZE-net_num);
155194206Ssimon#else
156194206Ssimon		i=_read(fd,(void *)&(net[net_num]),HDRSIZE-net_num);
157194206Ssimon#endif
15855714Skris#ifdef EINTR
15955714Skris		if ((i == -1) && (errno == EINTR)) continue;
16055714Skris#endif
16155714Skris		if (i <= 0) return(0);
16255714Skris		net_num+=i;
16355714Skris		}
16455714Skris
16555714Skris	/* we now have at net_num bytes in net */
16655714Skris	p=net;
16755714Skris	/* num=0;  */
16855714Skris	n2l(p,num);
16955714Skris	/* num should be rounded up to the next group of eight
17055714Skris	 * we make sure that we have read a multiple of 8 bytes from the net.
17155714Skris	 */
17255714Skris	if ((num > MAXWRITE) || (num < 0)) /* error */
17355714Skris		return(-1);
17455714Skris	rnum=(num < 8)?8:((num+7)/8*8);
17555714Skris
17655714Skris	net_num=0;
17755714Skris	while (net_num < rnum)
17855714Skris		{
179238405Sjkim#ifndef OPENSSL_SYS_WIN32
18059191Skris		i=read(fd,(void *)&(net[net_num]),rnum-net_num);
181238405Sjkim#else
182238405Sjkim		i=_read(fd,(void *)&(net[net_num]),rnum-net_num);
183238405Sjkim#endif
18455714Skris#ifdef EINTR
18555714Skris		if ((i == -1) && (errno == EINTR)) continue;
18655714Skris#endif
18755714Skris		if (i <= 0) return(0);
18855714Skris		net_num+=i;
18955714Skris		}
19055714Skris
19155714Skris	/* Check if there will be data left over. */
19255714Skris	if (len < num)
19355714Skris		{
194109998Smarkm		if (DES_rw_mode & DES_PCBC_MODE)
195109998Smarkm			DES_pcbc_encrypt(net,unnet,num,sched,iv,DES_DECRYPT);
19655714Skris		else
197109998Smarkm			DES_cbc_encrypt(net,unnet,num,sched,iv,DES_DECRYPT);
19855714Skris		memcpy(buf,unnet,len);
19955714Skris		unnet_start=len;
20055714Skris		unnet_left=num-len;
20155714Skris
20255714Skris		/* The following line is done because we return num
20355714Skris		 * as the number of bytes read. */
20455714Skris		num=len;
20555714Skris		}
20655714Skris	else
20755714Skris		{
20855714Skris		/* >output is a multiple of 8 byes, if len < rnum
20955714Skris		 * >we must be careful.  The user must be aware that this
21055714Skris		 * >routine will write more bytes than he asked for.
21155714Skris		 * >The length of the buffer must be correct.
21255714Skris		 * FIXED - Should be ok now 18-9-90 - eay */
21355714Skris		if (len < rnum)
21455714Skris			{
21555714Skris
216109998Smarkm			if (DES_rw_mode & DES_PCBC_MODE)
217109998Smarkm				DES_pcbc_encrypt(net,tmpbuf,num,sched,iv,
21855714Skris						 DES_DECRYPT);
21955714Skris			else
220109998Smarkm				DES_cbc_encrypt(net,tmpbuf,num,sched,iv,
22155714Skris						DES_DECRYPT);
22255714Skris
22355714Skris			/* eay 26/08/92 fix a bug that returned more
22455714Skris			 * bytes than you asked for (returned len bytes :-( */
22555714Skris			memcpy(buf,tmpbuf,num);
22655714Skris			}
22755714Skris		else
22855714Skris			{
229109998Smarkm			if (DES_rw_mode & DES_PCBC_MODE)
230109998Smarkm				DES_pcbc_encrypt(net,buf,num,sched,iv,
23155714Skris						 DES_DECRYPT);
23255714Skris			else
233109998Smarkm				DES_cbc_encrypt(net,buf,num,sched,iv,
23455714Skris						DES_DECRYPT);
23555714Skris			}
23655714Skris		}
23755714Skris	return num;
238238405Sjkim#endif /* OPENSSL_NO_POSIX_IO */
23955714Skris	}
24055714Skris
241