xref: /netbsd-src/crypto/external/bsd/openssl/lib/libdes/ocfb64ede.c (revision 49d46fa3c871a76b2e79b95237192a7724d66dc3)
1*49d46fa3Schristos /* crypto/des/cfb64ede.c */
2*49d46fa3Schristos /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*49d46fa3Schristos  * All rights reserved.
4*49d46fa3Schristos  *
5*49d46fa3Schristos  * This package is an SSL implementation written
6*49d46fa3Schristos  * by Eric Young (eay@cryptsoft.com).
7*49d46fa3Schristos  * The implementation was written so as to conform with Netscapes SSL.
8*49d46fa3Schristos  *
9*49d46fa3Schristos  * This library is free for commercial and non-commercial use as long as
10*49d46fa3Schristos  * the following conditions are aheared to.  The following conditions
11*49d46fa3Schristos  * apply to all code found in this distribution, be it the RC4, RSA,
12*49d46fa3Schristos  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*49d46fa3Schristos  * included with this distribution is covered by the same copyright terms
14*49d46fa3Schristos  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*49d46fa3Schristos  *
16*49d46fa3Schristos  * Copyright remains Eric Young's, and as such any Copyright notices in
17*49d46fa3Schristos  * the code are not to be removed.
18*49d46fa3Schristos  * If this package is used in a product, Eric Young should be given attribution
19*49d46fa3Schristos  * as the author of the parts of the library used.
20*49d46fa3Schristos  * This can be in the form of a textual message at program startup or
21*49d46fa3Schristos  * in documentation (online or textual) provided with the package.
22*49d46fa3Schristos  *
23*49d46fa3Schristos  * Redistribution and use in source and binary forms, with or without
24*49d46fa3Schristos  * modification, are permitted provided that the following conditions
25*49d46fa3Schristos  * are met:
26*49d46fa3Schristos  * 1. Redistributions of source code must retain the copyright
27*49d46fa3Schristos  *    notice, this list of conditions and the following disclaimer.
28*49d46fa3Schristos  * 2. Redistributions in binary form must reproduce the above copyright
29*49d46fa3Schristos  *    notice, this list of conditions and the following disclaimer in the
30*49d46fa3Schristos  *    documentation and/or other materials provided with the distribution.
31*49d46fa3Schristos  * 3. All advertising materials mentioning features or use of this software
32*49d46fa3Schristos  *    must display the following acknowledgement:
33*49d46fa3Schristos  *    "This product includes cryptographic software written by
34*49d46fa3Schristos  *     Eric Young (eay@cryptsoft.com)"
35*49d46fa3Schristos  *    The word 'cryptographic' can be left out if the rouines from the library
36*49d46fa3Schristos  *    being used are not cryptographic related :-).
37*49d46fa3Schristos  * 4. If you include any Windows specific code (or a derivative thereof) from
38*49d46fa3Schristos  *    the apps directory (application code) you must include an acknowledgement:
39*49d46fa3Schristos  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*49d46fa3Schristos  *
41*49d46fa3Schristos  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*49d46fa3Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*49d46fa3Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*49d46fa3Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*49d46fa3Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*49d46fa3Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*49d46fa3Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*49d46fa3Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*49d46fa3Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*49d46fa3Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*49d46fa3Schristos  * SUCH DAMAGE.
52*49d46fa3Schristos  *
53*49d46fa3Schristos  * The licence and distribution terms for any publically available version or
54*49d46fa3Schristos  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*49d46fa3Schristos  * copied and put under another distribution licence
56*49d46fa3Schristos  * [including the GNU Public Licence.]
57*49d46fa3Schristos  */
58*49d46fa3Schristos 
59*49d46fa3Schristos #include "des_locl.h"
60*49d46fa3Schristos 
61*49d46fa3Schristos /* The input and output encrypted as though 64bit cfb mode is being
62*49d46fa3Schristos  * used.  The extra state information to record how much of the
63*49d46fa3Schristos  * 64bit block we have used is contained in *num;
64*49d46fa3Schristos  */
65*49d46fa3Schristos 
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)66*49d46fa3Schristos void des_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out,
67*49d46fa3Schristos 	     long length, des_key_schedule ks1, des_key_schedule ks2,
68*49d46fa3Schristos 	     des_key_schedule ks3, des_cblock *ivec, int *num, int enc)
69*49d46fa3Schristos 	{
70*49d46fa3Schristos 	register DES_LONG v0,v1;
71*49d46fa3Schristos 	register long l=length;
72*49d46fa3Schristos 	register int n= *num;
73*49d46fa3Schristos 	DES_LONG ti[2];
74*49d46fa3Schristos 	unsigned char *iv,c,cc;
75*49d46fa3Schristos 
76*49d46fa3Schristos 	iv=&(*ivec)[0];
77*49d46fa3Schristos 	if (enc)
78*49d46fa3Schristos 		{
79*49d46fa3Schristos 		while (l--)
80*49d46fa3Schristos 			{
81*49d46fa3Schristos 			if (n == 0)
82*49d46fa3Schristos 				{
83*49d46fa3Schristos 				c2l(iv,v0);
84*49d46fa3Schristos 				c2l(iv,v1);
85*49d46fa3Schristos 
86*49d46fa3Schristos 				ti[0]=v0;
87*49d46fa3Schristos 				ti[1]=v1;
88*49d46fa3Schristos 				des_encrypt3(ti,ks1,ks2,ks3);
89*49d46fa3Schristos 				v0=ti[0];
90*49d46fa3Schristos 				v1=ti[1];
91*49d46fa3Schristos 
92*49d46fa3Schristos 				iv = &(*ivec)[0];
93*49d46fa3Schristos 				l2c(v0,iv);
94*49d46fa3Schristos 				l2c(v1,iv);
95*49d46fa3Schristos 				iv = &(*ivec)[0];
96*49d46fa3Schristos 				}
97*49d46fa3Schristos 			c= *(in++)^iv[n];
98*49d46fa3Schristos 			*(out++)=c;
99*49d46fa3Schristos 			iv[n]=c;
100*49d46fa3Schristos 			n=(n+1)&0x07;
101*49d46fa3Schristos 			}
102*49d46fa3Schristos 		}
103*49d46fa3Schristos 	else
104*49d46fa3Schristos 		{
105*49d46fa3Schristos 		while (l--)
106*49d46fa3Schristos 			{
107*49d46fa3Schristos 			if (n == 0)
108*49d46fa3Schristos 				{
109*49d46fa3Schristos 				c2l(iv,v0);
110*49d46fa3Schristos 				c2l(iv,v1);
111*49d46fa3Schristos 
112*49d46fa3Schristos 				ti[0]=v0;
113*49d46fa3Schristos 				ti[1]=v1;
114*49d46fa3Schristos 				des_encrypt3(ti,ks1,ks2,ks3);
115*49d46fa3Schristos 				v0=ti[0];
116*49d46fa3Schristos 				v1=ti[1];
117*49d46fa3Schristos 
118*49d46fa3Schristos 				iv = &(*ivec)[0];
119*49d46fa3Schristos 				l2c(v0,iv);
120*49d46fa3Schristos 				l2c(v1,iv);
121*49d46fa3Schristos 				iv = &(*ivec)[0];
122*49d46fa3Schristos 				}
123*49d46fa3Schristos 			cc= *(in++);
124*49d46fa3Schristos 			c=iv[n];
125*49d46fa3Schristos 			iv[n]=cc;
126*49d46fa3Schristos 			*(out++)=c^cc;
127*49d46fa3Schristos 			n=(n+1)&0x07;
128*49d46fa3Schristos 			}
129*49d46fa3Schristos 		}
130*49d46fa3Schristos 	v0=v1=ti[0]=ti[1]=c=cc=0;
131*49d46fa3Schristos 	*num=n;
132*49d46fa3Schristos 	}
133*49d46fa3Schristos 
134*49d46fa3Schristos #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)135*49d46fa3Schristos void des_ede2_cfb64_encrypt(unsigned char *in, unsigned char *out, long length,
136*49d46fa3Schristos 	     des_key_schedule ks1, des_key_schedule ks2, des_cblock (*ivec),
137*49d46fa3Schristos 	     int *num, int enc)
138*49d46fa3Schristos 	{
139*49d46fa3Schristos 	des_ede3_cfb64_encrypt(in,out,length,ks1,ks2,ks1,ivec,num,enc);
140*49d46fa3Schristos 	}
141*49d46fa3Schristos #endif
142