10Sstevel@tonic-gate /* crypto/des/cfb64ede.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include "des_locl.h"
60*2139Sjp161948 #include "e_os.h"
610Sstevel@tonic-gate
620Sstevel@tonic-gate /* The input and output encrypted as though 64bit cfb mode is being
630Sstevel@tonic-gate * used. The extra state information to record how much of the
640Sstevel@tonic-gate * 64bit block we have used is contained in *num;
650Sstevel@tonic-gate */
660Sstevel@tonic-gate
DES_ede3_cfb64_encrypt(const unsigned char * in,unsigned char * out,long length,DES_key_schedule * ks1,DES_key_schedule * ks2,DES_key_schedule * ks3,DES_cblock * ivec,int * num,int enc)670Sstevel@tonic-gate void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out,
680Sstevel@tonic-gate long length, DES_key_schedule *ks1,
690Sstevel@tonic-gate DES_key_schedule *ks2, DES_key_schedule *ks3,
700Sstevel@tonic-gate DES_cblock *ivec, int *num, int enc)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate register DES_LONG v0,v1;
730Sstevel@tonic-gate register long l=length;
740Sstevel@tonic-gate register int n= *num;
750Sstevel@tonic-gate DES_LONG ti[2];
760Sstevel@tonic-gate unsigned char *iv,c,cc;
770Sstevel@tonic-gate
780Sstevel@tonic-gate iv=&(*ivec)[0];
790Sstevel@tonic-gate if (enc)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate while (l--)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate if (n == 0)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate c2l(iv,v0);
860Sstevel@tonic-gate c2l(iv,v1);
870Sstevel@tonic-gate
880Sstevel@tonic-gate ti[0]=v0;
890Sstevel@tonic-gate ti[1]=v1;
900Sstevel@tonic-gate DES_encrypt3(ti,ks1,ks2,ks3);
910Sstevel@tonic-gate v0=ti[0];
920Sstevel@tonic-gate v1=ti[1];
930Sstevel@tonic-gate
940Sstevel@tonic-gate iv = &(*ivec)[0];
950Sstevel@tonic-gate l2c(v0,iv);
960Sstevel@tonic-gate l2c(v1,iv);
970Sstevel@tonic-gate iv = &(*ivec)[0];
980Sstevel@tonic-gate }
990Sstevel@tonic-gate c= *(in++)^iv[n];
1000Sstevel@tonic-gate *(out++)=c;
1010Sstevel@tonic-gate iv[n]=c;
1020Sstevel@tonic-gate n=(n+1)&0x07;
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate else
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate while (l--)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate if (n == 0)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate c2l(iv,v0);
1120Sstevel@tonic-gate c2l(iv,v1);
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate ti[0]=v0;
1150Sstevel@tonic-gate ti[1]=v1;
1160Sstevel@tonic-gate DES_encrypt3(ti,ks1,ks2,ks3);
1170Sstevel@tonic-gate v0=ti[0];
1180Sstevel@tonic-gate v1=ti[1];
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate iv = &(*ivec)[0];
1210Sstevel@tonic-gate l2c(v0,iv);
1220Sstevel@tonic-gate l2c(v1,iv);
1230Sstevel@tonic-gate iv = &(*ivec)[0];
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate cc= *(in++);
1260Sstevel@tonic-gate c=iv[n];
1270Sstevel@tonic-gate iv[n]=cc;
1280Sstevel@tonic-gate *(out++)=c^cc;
1290Sstevel@tonic-gate n=(n+1)&0x07;
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate v0=v1=ti[0]=ti[1]=c=cc=0;
1330Sstevel@tonic-gate *num=n;
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate #ifdef undef /* MACRO */
DES_ede2_cfb64_encrypt(unsigned char * in,unsigned char * out,long length,DES_key_schedule ks1,DES_key_schedule ks2,DES_cblock (* ivec),int * num,int enc)1370Sstevel@tonic-gate void DES_ede2_cfb64_encrypt(unsigned char *in, unsigned char *out, long length,
1380Sstevel@tonic-gate DES_key_schedule ks1, DES_key_schedule ks2, DES_cblock (*ivec),
1390Sstevel@tonic-gate int *num, int enc)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate DES_ede3_cfb64_encrypt(in,out,length,ks1,ks2,ks1,ivec,num,enc);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate #endif
144*2139Sjp161948
145*2139Sjp161948 /* This is compatible with the single key CFB-r for DES, even thought that's
146*2139Sjp161948 * not what EVP needs.
147*2139Sjp161948 */
148*2139Sjp161948
DES_ede3_cfb_encrypt(const unsigned char * in,unsigned char * out,int numbits,long length,DES_key_schedule * ks1,DES_key_schedule * ks2,DES_key_schedule * ks3,DES_cblock * ivec,int enc)149*2139Sjp161948 void DES_ede3_cfb_encrypt(const unsigned char *in,unsigned char *out,
150*2139Sjp161948 int numbits,long length,DES_key_schedule *ks1,
151*2139Sjp161948 DES_key_schedule *ks2,DES_key_schedule *ks3,
152*2139Sjp161948 DES_cblock *ivec,int enc)
153*2139Sjp161948 {
154*2139Sjp161948 register DES_LONG d0,d1,v0,v1;
155*2139Sjp161948 register unsigned long l=length,n=((unsigned int)numbits+7)/8;
156*2139Sjp161948 register int num=numbits,i;
157*2139Sjp161948 DES_LONG ti[2];
158*2139Sjp161948 unsigned char *iv;
159*2139Sjp161948 unsigned char ovec[16];
160*2139Sjp161948
161*2139Sjp161948 if (num > 64) return;
162*2139Sjp161948 iv = &(*ivec)[0];
163*2139Sjp161948 c2l(iv,v0);
164*2139Sjp161948 c2l(iv,v1);
165*2139Sjp161948 if (enc)
166*2139Sjp161948 {
167*2139Sjp161948 while (l >= n)
168*2139Sjp161948 {
169*2139Sjp161948 l-=n;
170*2139Sjp161948 ti[0]=v0;
171*2139Sjp161948 ti[1]=v1;
172*2139Sjp161948 DES_encrypt3(ti,ks1,ks2,ks3);
173*2139Sjp161948 c2ln(in,d0,d1,n);
174*2139Sjp161948 in+=n;
175*2139Sjp161948 d0^=ti[0];
176*2139Sjp161948 d1^=ti[1];
177*2139Sjp161948 l2cn(d0,d1,out,n);
178*2139Sjp161948 out+=n;
179*2139Sjp161948 /* 30-08-94 - eay - changed because l>>32 and
180*2139Sjp161948 * l<<32 are bad under gcc :-( */
181*2139Sjp161948 if (num == 32)
182*2139Sjp161948 { v0=v1; v1=d0; }
183*2139Sjp161948 else if (num == 64)
184*2139Sjp161948 { v0=d0; v1=d1; }
185*2139Sjp161948 else
186*2139Sjp161948 {
187*2139Sjp161948 iv=&ovec[0];
188*2139Sjp161948 l2c(v0,iv);
189*2139Sjp161948 l2c(v1,iv);
190*2139Sjp161948 l2c(d0,iv);
191*2139Sjp161948 l2c(d1,iv);
192*2139Sjp161948 /* shift ovec left most of the bits... */
193*2139Sjp161948 memmove(ovec,ovec+num/8,8+(num%8 ? 1 : 0));
194*2139Sjp161948 /* now the remaining bits */
195*2139Sjp161948 if(num%8 != 0)
196*2139Sjp161948 for(i=0 ; i < 8 ; ++i)
197*2139Sjp161948 {
198*2139Sjp161948 ovec[i]<<=num%8;
199*2139Sjp161948 ovec[i]|=ovec[i+1]>>(8-num%8);
200*2139Sjp161948 }
201*2139Sjp161948 iv=&ovec[0];
202*2139Sjp161948 c2l(iv,v0);
203*2139Sjp161948 c2l(iv,v1);
204*2139Sjp161948 }
205*2139Sjp161948 }
206*2139Sjp161948 }
207*2139Sjp161948 else
208*2139Sjp161948 {
209*2139Sjp161948 while (l >= n)
210*2139Sjp161948 {
211*2139Sjp161948 l-=n;
212*2139Sjp161948 ti[0]=v0;
213*2139Sjp161948 ti[1]=v1;
214*2139Sjp161948 DES_encrypt3(ti,ks1,ks2,ks3);
215*2139Sjp161948 c2ln(in,d0,d1,n);
216*2139Sjp161948 in+=n;
217*2139Sjp161948 /* 30-08-94 - eay - changed because l>>32 and
218*2139Sjp161948 * l<<32 are bad under gcc :-( */
219*2139Sjp161948 if (num == 32)
220*2139Sjp161948 { v0=v1; v1=d0; }
221*2139Sjp161948 else if (num == 64)
222*2139Sjp161948 { v0=d0; v1=d1; }
223*2139Sjp161948 else
224*2139Sjp161948 {
225*2139Sjp161948 iv=&ovec[0];
226*2139Sjp161948 l2c(v0,iv);
227*2139Sjp161948 l2c(v1,iv);
228*2139Sjp161948 l2c(d0,iv);
229*2139Sjp161948 l2c(d1,iv);
230*2139Sjp161948 /* shift ovec left most of the bits... */
231*2139Sjp161948 memmove(ovec,ovec+num/8,8+(num%8 ? 1 : 0));
232*2139Sjp161948 /* now the remaining bits */
233*2139Sjp161948 if(num%8 != 0)
234*2139Sjp161948 for(i=0 ; i < 8 ; ++i)
235*2139Sjp161948 {
236*2139Sjp161948 ovec[i]<<=num%8;
237*2139Sjp161948 ovec[i]|=ovec[i+1]>>(8-num%8);
238*2139Sjp161948 }
239*2139Sjp161948 iv=&ovec[0];
240*2139Sjp161948 c2l(iv,v0);
241*2139Sjp161948 c2l(iv,v1);
242*2139Sjp161948 }
243*2139Sjp161948 d0^=ti[0];
244*2139Sjp161948 d1^=ti[1];
245*2139Sjp161948 l2cn(d0,d1,out,n);
246*2139Sjp161948 out+=n;
247*2139Sjp161948 }
248*2139Sjp161948 }
249*2139Sjp161948 iv = &(*ivec)[0];
250*2139Sjp161948 l2c(v0,iv);
251*2139Sjp161948 l2c(v1,iv);
252*2139Sjp161948 v0=v1=d0=d1=ti[0]=ti[1]=0;
253*2139Sjp161948 }
254*2139Sjp161948
255