sl@0
|
1 |
/* crypto/des/cfb64ede.c */
|
sl@0
|
2 |
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* This package is an SSL implementation written
|
sl@0
|
6 |
* by Eric Young (eay@cryptsoft.com).
|
sl@0
|
7 |
* The implementation was written so as to conform with Netscapes SSL.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is free for commercial and non-commercial use as long as
|
sl@0
|
10 |
* the following conditions are aheared to. The following conditions
|
sl@0
|
11 |
* apply to all code found in this distribution, be it the RC4, RSA,
|
sl@0
|
12 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
sl@0
|
13 |
* included with this distribution is covered by the same copyright terms
|
sl@0
|
14 |
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
sl@0
|
15 |
*
|
sl@0
|
16 |
* Copyright remains Eric Young's, and as such any Copyright notices in
|
sl@0
|
17 |
* the code are not to be removed.
|
sl@0
|
18 |
* If this package is used in a product, Eric Young should be given attribution
|
sl@0
|
19 |
* as the author of the parts of the library used.
|
sl@0
|
20 |
* This can be in the form of a textual message at program startup or
|
sl@0
|
21 |
* in documentation (online or textual) provided with the package.
|
sl@0
|
22 |
*
|
sl@0
|
23 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
24 |
* modification, are permitted provided that the following conditions
|
sl@0
|
25 |
* are met:
|
sl@0
|
26 |
* 1. Redistributions of source code must retain the copyright
|
sl@0
|
27 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
28 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
29 |
* notice, this list of conditions and the following disclaimer in the
|
sl@0
|
30 |
* documentation and/or other materials provided with the distribution.
|
sl@0
|
31 |
* 3. All advertising materials mentioning features or use of this software
|
sl@0
|
32 |
* must display the following acknowledgement:
|
sl@0
|
33 |
* "This product includes cryptographic software written by
|
sl@0
|
34 |
* Eric Young (eay@cryptsoft.com)"
|
sl@0
|
35 |
* The word 'cryptographic' can be left out if the rouines from the library
|
sl@0
|
36 |
* being used are not cryptographic related :-).
|
sl@0
|
37 |
* 4. If you include any Windows specific code (or a derivative thereof) from
|
sl@0
|
38 |
* the apps directory (application code) you must include an acknowledgement:
|
sl@0
|
39 |
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
sl@0
|
40 |
*
|
sl@0
|
41 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
sl@0
|
42 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
43 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
sl@0
|
44 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
sl@0
|
45 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
sl@0
|
46 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
sl@0
|
47 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
sl@0
|
49 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
sl@0
|
50 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
sl@0
|
51 |
* SUCH DAMAGE.
|
sl@0
|
52 |
*
|
sl@0
|
53 |
* The licence and distribution terms for any publically available version or
|
sl@0
|
54 |
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
sl@0
|
55 |
* copied and put under another distribution licence
|
sl@0
|
56 |
* [including the GNU Public Licence.]
|
sl@0
|
57 |
*/
|
sl@0
|
58 |
|
sl@0
|
59 |
#include "des_locl.h"
|
sl@0
|
60 |
#include "e_os.h"
|
sl@0
|
61 |
|
sl@0
|
62 |
/* The input and output encrypted as though 64bit cfb mode is being
|
sl@0
|
63 |
* used. The extra state information to record how much of the
|
sl@0
|
64 |
* 64bit block we have used is contained in *num;
|
sl@0
|
65 |
*/
|
sl@0
|
66 |
|
sl@0
|
67 |
EXPORT_C void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out,
|
sl@0
|
68 |
long length, DES_key_schedule *ks1,
|
sl@0
|
69 |
DES_key_schedule *ks2, DES_key_schedule *ks3,
|
sl@0
|
70 |
DES_cblock *ivec, int *num, int enc)
|
sl@0
|
71 |
{
|
sl@0
|
72 |
register DES_LONG v0,v1;
|
sl@0
|
73 |
register long l=length;
|
sl@0
|
74 |
register int n= *num;
|
sl@0
|
75 |
DES_LONG ti[2];
|
sl@0
|
76 |
unsigned char *iv,c,cc;
|
sl@0
|
77 |
|
sl@0
|
78 |
iv=&(*ivec)[0];
|
sl@0
|
79 |
if (enc)
|
sl@0
|
80 |
{
|
sl@0
|
81 |
while (l--)
|
sl@0
|
82 |
{
|
sl@0
|
83 |
if (n == 0)
|
sl@0
|
84 |
{
|
sl@0
|
85 |
c2l(iv,v0);
|
sl@0
|
86 |
c2l(iv,v1);
|
sl@0
|
87 |
|
sl@0
|
88 |
ti[0]=v0;
|
sl@0
|
89 |
ti[1]=v1;
|
sl@0
|
90 |
DES_encrypt3(ti,ks1,ks2,ks3);
|
sl@0
|
91 |
v0=ti[0];
|
sl@0
|
92 |
v1=ti[1];
|
sl@0
|
93 |
|
sl@0
|
94 |
iv = &(*ivec)[0];
|
sl@0
|
95 |
l2c(v0,iv);
|
sl@0
|
96 |
l2c(v1,iv);
|
sl@0
|
97 |
iv = &(*ivec)[0];
|
sl@0
|
98 |
}
|
sl@0
|
99 |
c= *(in++)^iv[n];
|
sl@0
|
100 |
*(out++)=c;
|
sl@0
|
101 |
iv[n]=c;
|
sl@0
|
102 |
n=(n+1)&0x07;
|
sl@0
|
103 |
}
|
sl@0
|
104 |
}
|
sl@0
|
105 |
else
|
sl@0
|
106 |
{
|
sl@0
|
107 |
while (l--)
|
sl@0
|
108 |
{
|
sl@0
|
109 |
if (n == 0)
|
sl@0
|
110 |
{
|
sl@0
|
111 |
c2l(iv,v0);
|
sl@0
|
112 |
c2l(iv,v1);
|
sl@0
|
113 |
|
sl@0
|
114 |
ti[0]=v0;
|
sl@0
|
115 |
ti[1]=v1;
|
sl@0
|
116 |
DES_encrypt3(ti,ks1,ks2,ks3);
|
sl@0
|
117 |
v0=ti[0];
|
sl@0
|
118 |
v1=ti[1];
|
sl@0
|
119 |
|
sl@0
|
120 |
iv = &(*ivec)[0];
|
sl@0
|
121 |
l2c(v0,iv);
|
sl@0
|
122 |
l2c(v1,iv);
|
sl@0
|
123 |
iv = &(*ivec)[0];
|
sl@0
|
124 |
}
|
sl@0
|
125 |
cc= *(in++);
|
sl@0
|
126 |
c=iv[n];
|
sl@0
|
127 |
iv[n]=cc;
|
sl@0
|
128 |
*(out++)=c^cc;
|
sl@0
|
129 |
n=(n+1)&0x07;
|
sl@0
|
130 |
}
|
sl@0
|
131 |
}
|
sl@0
|
132 |
v0=v1=ti[0]=ti[1]=c=cc=0;
|
sl@0
|
133 |
*num=n;
|
sl@0
|
134 |
}
|
sl@0
|
135 |
|
sl@0
|
136 |
#ifdef undef /* MACRO */
|
sl@0
|
137 |
EXPORT_C void DES_ede2_cfb64_encrypt(unsigned char *in, unsigned char *out, long length,
|
sl@0
|
138 |
DES_key_schedule ks1, DES_key_schedule ks2, DES_cblock (*ivec),
|
sl@0
|
139 |
int *num, int enc)
|
sl@0
|
140 |
{
|
sl@0
|
141 |
DES_ede3_cfb64_encrypt(in,out,length,ks1,ks2,ks1,ivec,num,enc);
|
sl@0
|
142 |
}
|
sl@0
|
143 |
#endif
|
sl@0
|
144 |
|
sl@0
|
145 |
/* This is compatible with the single key CFB-r for DES, even thought that's
|
sl@0
|
146 |
* not what EVP needs.
|
sl@0
|
147 |
*/
|
sl@0
|
148 |
|
sl@0
|
149 |
EXPORT_C void DES_ede3_cfb_encrypt(const unsigned char *in,unsigned char *out,
|
sl@0
|
150 |
int numbits,long length,DES_key_schedule *ks1,
|
sl@0
|
151 |
DES_key_schedule *ks2,DES_key_schedule *ks3,
|
sl@0
|
152 |
DES_cblock *ivec,int enc)
|
sl@0
|
153 |
{
|
sl@0
|
154 |
register DES_LONG d0,d1,v0,v1;
|
sl@0
|
155 |
register unsigned long l=length,n=((unsigned int)numbits+7)/8;
|
sl@0
|
156 |
register int num=numbits,i;
|
sl@0
|
157 |
DES_LONG ti[2];
|
sl@0
|
158 |
unsigned char *iv;
|
sl@0
|
159 |
unsigned char ovec[16];
|
sl@0
|
160 |
|
sl@0
|
161 |
if (num > 64) return;
|
sl@0
|
162 |
iv = &(*ivec)[0];
|
sl@0
|
163 |
c2l(iv,v0);
|
sl@0
|
164 |
c2l(iv,v1);
|
sl@0
|
165 |
if (enc)
|
sl@0
|
166 |
{
|
sl@0
|
167 |
while (l >= n)
|
sl@0
|
168 |
{
|
sl@0
|
169 |
l-=n;
|
sl@0
|
170 |
ti[0]=v0;
|
sl@0
|
171 |
ti[1]=v1;
|
sl@0
|
172 |
DES_encrypt3(ti,ks1,ks2,ks3);
|
sl@0
|
173 |
c2ln(in,d0,d1,n);
|
sl@0
|
174 |
in+=n;
|
sl@0
|
175 |
d0^=ti[0];
|
sl@0
|
176 |
d1^=ti[1];
|
sl@0
|
177 |
l2cn(d0,d1,out,n);
|
sl@0
|
178 |
out+=n;
|
sl@0
|
179 |
/* 30-08-94 - eay - changed because l>>32 and
|
sl@0
|
180 |
* l<<32 are bad under gcc :-( */
|
sl@0
|
181 |
if (num == 32)
|
sl@0
|
182 |
{ v0=v1; v1=d0; }
|
sl@0
|
183 |
else if (num == 64)
|
sl@0
|
184 |
{ v0=d0; v1=d1; }
|
sl@0
|
185 |
else
|
sl@0
|
186 |
{
|
sl@0
|
187 |
iv=&ovec[0];
|
sl@0
|
188 |
l2c(v0,iv);
|
sl@0
|
189 |
l2c(v1,iv);
|
sl@0
|
190 |
l2c(d0,iv);
|
sl@0
|
191 |
l2c(d1,iv);
|
sl@0
|
192 |
/* shift ovec left most of the bits... */
|
sl@0
|
193 |
memmove(ovec,ovec+num/8,8+(num%8 ? 1 : 0));
|
sl@0
|
194 |
/* now the remaining bits */
|
sl@0
|
195 |
if(num%8 != 0)
|
sl@0
|
196 |
for(i=0 ; i < 8 ; ++i)
|
sl@0
|
197 |
{
|
sl@0
|
198 |
ovec[i]<<=num%8;
|
sl@0
|
199 |
ovec[i]|=ovec[i+1]>>(8-num%8);
|
sl@0
|
200 |
}
|
sl@0
|
201 |
iv=&ovec[0];
|
sl@0
|
202 |
c2l(iv,v0);
|
sl@0
|
203 |
c2l(iv,v1);
|
sl@0
|
204 |
}
|
sl@0
|
205 |
}
|
sl@0
|
206 |
}
|
sl@0
|
207 |
else
|
sl@0
|
208 |
{
|
sl@0
|
209 |
while (l >= n)
|
sl@0
|
210 |
{
|
sl@0
|
211 |
l-=n;
|
sl@0
|
212 |
ti[0]=v0;
|
sl@0
|
213 |
ti[1]=v1;
|
sl@0
|
214 |
DES_encrypt3(ti,ks1,ks2,ks3);
|
sl@0
|
215 |
c2ln(in,d0,d1,n);
|
sl@0
|
216 |
in+=n;
|
sl@0
|
217 |
/* 30-08-94 - eay - changed because l>>32 and
|
sl@0
|
218 |
* l<<32 are bad under gcc :-( */
|
sl@0
|
219 |
if (num == 32)
|
sl@0
|
220 |
{ v0=v1; v1=d0; }
|
sl@0
|
221 |
else if (num == 64)
|
sl@0
|
222 |
{ v0=d0; v1=d1; }
|
sl@0
|
223 |
else
|
sl@0
|
224 |
{
|
sl@0
|
225 |
iv=&ovec[0];
|
sl@0
|
226 |
l2c(v0,iv);
|
sl@0
|
227 |
l2c(v1,iv);
|
sl@0
|
228 |
l2c(d0,iv);
|
sl@0
|
229 |
l2c(d1,iv);
|
sl@0
|
230 |
/* shift ovec left most of the bits... */
|
sl@0
|
231 |
memmove(ovec,ovec+num/8,8+(num%8 ? 1 : 0));
|
sl@0
|
232 |
/* now the remaining bits */
|
sl@0
|
233 |
if(num%8 != 0)
|
sl@0
|
234 |
for(i=0 ; i < 8 ; ++i)
|
sl@0
|
235 |
{
|
sl@0
|
236 |
ovec[i]<<=num%8;
|
sl@0
|
237 |
ovec[i]|=ovec[i+1]>>(8-num%8);
|
sl@0
|
238 |
}
|
sl@0
|
239 |
iv=&ovec[0];
|
sl@0
|
240 |
c2l(iv,v0);
|
sl@0
|
241 |
c2l(iv,v1);
|
sl@0
|
242 |
}
|
sl@0
|
243 |
d0^=ti[0];
|
sl@0
|
244 |
d1^=ti[1];
|
sl@0
|
245 |
l2cn(d0,d1,out,n);
|
sl@0
|
246 |
out+=n;
|
sl@0
|
247 |
}
|
sl@0
|
248 |
}
|
sl@0
|
249 |
iv = &(*ivec)[0];
|
sl@0
|
250 |
l2c(v0,iv);
|
sl@0
|
251 |
l2c(v1,iv);
|
sl@0
|
252 |
v0=v1=d0=d1=ti[0]=ti[1]=0;
|
sl@0
|
253 |
}
|
sl@0
|
254 |
|