1 /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
4 * This file is part of an SSL implementation written
5 * by Eric Young (eay@mincom.oz.au).
6 * The implementation was written so as to conform with Netscapes SSL
7 * specification. This library and applications are
8 * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
9 * as long as the following conditions are aheared to.
11 * Copyright remains Eric Young's, and as such any Copyright notices in
12 * the code are not to be removed. If this code is used in a product,
13 * Eric Young should be given attribution as the author of the parts used.
14 * This can be in the form of a textual message at program startup or
15 * in documentation (online or textual) provided with the package.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by Eric Young (eay@mincom.oz.au)
29 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * The licence and distribution terms for any publically available version or
42 * derivative of this code cannot be changed. i.e. this code cannot simply be
43 * copied and put under another distribution licence
44 * [including the GNU Public Licence.]
49 RCSID("$Id: encrypt.c,v 1.4 1999/06/17 18:47:26 assar Exp $");
51 /* replacements for htonl and ntohl since I have no idea what to do
52 * when faced with machines with 8 byte longs. */
55 #define n2l(c,l) (l =((u_int32_t)(*((c)++)))<<24, \
56 l|=((u_int32_t)(*((c)++)))<<16, \
57 l|=((u_int32_t)(*((c)++)))<< 8, \
58 l|=((u_int32_t)(*((c)++))))
60 #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
61 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
62 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
63 *((c)++)=(unsigned char)(((l) )&0xff))
65 /* This has some uglies in it but it works - even over sockets. */
67 int des_rw_mode=DES_PCBC_MODE;
68 int LEFT_JUSTIFIED = 0;
71 des_enc_read(int fd, char *buf, int len, struct des_ks_struct *sched, des_cblock *iv)
73 /* data to be unencrypted */
75 unsigned char net[DES_RW_BSIZE];
76 /* extra unencrypted data
77 * for when a block of 100 comes in but is des_read one byte at
79 static char unnet[DES_RW_BSIZE];
80 static int unnet_start=0;
81 static int unnet_left=0;
86 /* left over data from last decrypt */
91 /* we still still need more data but will return
92 * with the number of bytes we have - should always
93 * check the return value */
94 memcpy(buf,&(unnet[unnet_start]),unnet_left);
95 /* eay 26/08/92 I had the next 2 lines
98 unnet_start=unnet_left=0;
102 memcpy(buf,&(unnet[unnet_start]),len);
110 /* We need to get more data. */
111 if (len > DES_RW_MAXWRITE) len=DES_RW_MAXWRITE;
113 /* first - get the length */
115 while (net_num < HDRSIZE)
117 i=read(fd,&(net[net_num]),(unsigned int)HDRSIZE-net_num);
118 if ((i == -1) && (errno == EINTR)) continue;
119 if (i <= 0) return(0);
123 /* we now have at net_num bytes in net */
127 /* num should be rounded up to the next group of eight
128 * we make sure that we have read a multiple of 8 bytes from the net.
130 if ((num > DES_RW_MAXWRITE) || (num < 0)) /* error */
132 rnum=(num < 8)?8:((num+7)/8*8);
135 while (net_num < rnum)
137 i=read(fd,&(net[net_num]),(unsigned int)rnum-net_num);
138 if ((i == -1) && (errno == EINTR)) continue;
139 if (i <= 0) return(0);
143 /* Check if there will be data left over. */
146 if (des_rw_mode & DES_PCBC_MODE)
147 des_pcbc_encrypt((des_cblock *)net,(des_cblock *)unnet,
148 num,sched,iv,DES_DECRYPT);
150 des_cbc_encrypt((des_cblock *)net,(des_cblock *)unnet,
151 num,sched,iv,DES_DECRYPT);
152 memcpy(buf,unnet,len);
156 /* The following line is done because we return num
157 * as the number of bytes read. */
162 /* >output is a multiple of 8 byes, if len < rnum
163 * >we must be careful. The user must be aware that this
164 * >routine will write more bytes than he asked for.
165 * >The length of the buffer must be correct.
166 * FIXED - Should be ok now 18-9-90 - eay */
169 char tmpbuf[DES_RW_BSIZE];
171 if (des_rw_mode & DES_PCBC_MODE)
172 des_pcbc_encrypt((des_cblock *)net,
173 (des_cblock *)tmpbuf,
174 num,sched,iv,DES_DECRYPT);
176 des_cbc_encrypt((des_cblock *)net,
177 (des_cblock *)tmpbuf,
178 num,sched,iv,DES_DECRYPT);
180 /* eay 26/08/92 fix a bug that returned more
181 * bytes than you asked for (returned len bytes :-( */
182 if (LEFT_JUSTIFIED || (len >= 8))
183 memcpy(buf,tmpbuf,num);
185 memcpy(buf,tmpbuf+(8-num),num); /* Right justified */
189 if (des_rw_mode & DES_PCBC_MODE)
190 des_pcbc_encrypt((des_cblock *)net,
191 (des_cblock *)buf,num,sched,iv,
194 des_cbc_encrypt((des_cblock *)net,
195 (des_cblock *)buf,num,sched,iv,
200 if (des_rw_mode & DES_PCBC_MODE)
201 des_pcbc_encrypt((des_cblock *)net,
202 (des_cblock *)buf,8,sched,iv,
205 des_cbc_encrypt((des_cblock *)net,
206 (des_cblock *)buf,8,sched,iv,
209 memcpy(buf, buf+(8-num), num); /* Right justified */
216 des_enc_write(int fd, char *buf, int len, struct des_ks_struct *sched, des_cblock *iv)
220 char outbuf[DES_RW_BSIZE+HDRSIZE];
225 /* If we are sending less than 8 bytes, the same char will look
226 * the same if we don't pad it out with random bytes */
233 /* lets recurse if we want to send the data in small chunks */
234 if (len > DES_RW_MAXWRITE)
237 for (i=0; i<len; i+=k)
239 k=des_enc_write(fd,&(buf[i]),
240 ((len-i) > DES_RW_MAXWRITE)?DES_RW_MAXWRITE:(len-i),sched,iv);
249 /* write length first */
253 /* pad short strings */
259 memcpy(shortbuf,buf,(unsigned int)len);
260 for (i=len; i<8; i++)
267 for (i=0; i<8-len; i++)
269 memcpy(shortbuf + 8 - len, buf, len);
276 rnum=((len+7)/8*8); /* round up to nearest eight */
279 if (des_rw_mode & DES_PCBC_MODE)
280 des_pcbc_encrypt((des_cblock *)p,(des_cblock *)&(outbuf[HDRSIZE]),
281 (long)((len<8)?8:len),sched,iv,DES_ENCRYPT);
283 des_cbc_encrypt((des_cblock *)p,(des_cblock *)&(outbuf[HDRSIZE]),
284 (long)((len<8)?8:len),sched,iv,DES_ENCRYPT);
289 for (j=0; j<outnum; j+=i)
291 /* eay 26/08/92 I was not doing writing from where we
293 i=write(fd,&(outbuf[j]),(unsigned int)(outnum-j));
298 else /* This is really a bad error - very bad
299 * It will stuff-up both ends. */