odes_enc.c revision 1.1 1 1.1 christos /* crypto/des/des_enc.c */
2 1.1 christos /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
3 1.1 christos * All rights reserved.
4 1.1 christos *
5 1.1 christos * This package is an SSL implementation written
6 1.1 christos * by Eric Young (eay (at) cryptsoft.com).
7 1.1 christos * The implementation was written so as to conform with Netscapes SSL.
8 1.1 christos *
9 1.1 christos * This library is free for commercial and non-commercial use as long as
10 1.1 christos * the following conditions are aheared to. The following conditions
11 1.1 christos * apply to all code found in this distribution, be it the RC4, RSA,
12 1.1 christos * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 1.1 christos * included with this distribution is covered by the same copyright terms
14 1.1 christos * except that the holder is Tim Hudson (tjh (at) cryptsoft.com).
15 1.1 christos *
16 1.1 christos * Copyright remains Eric Young's, and as such any Copyright notices in
17 1.1 christos * the code are not to be removed.
18 1.1 christos * If this package is used in a product, Eric Young should be given attribution
19 1.1 christos * as the author of the parts of the library used.
20 1.1 christos * This can be in the form of a textual message at program startup or
21 1.1 christos * in documentation (online or textual) provided with the package.
22 1.1 christos *
23 1.1 christos * Redistribution and use in source and binary forms, with or without
24 1.1 christos * modification, are permitted provided that the following conditions
25 1.1 christos * are met:
26 1.1 christos * 1. Redistributions of source code must retain the copyright
27 1.1 christos * notice, this list of conditions and the following disclaimer.
28 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
29 1.1 christos * notice, this list of conditions and the following disclaimer in the
30 1.1 christos * documentation and/or other materials provided with the distribution.
31 1.1 christos * 3. All advertising materials mentioning features or use of this software
32 1.1 christos * must display the following acknowledgement:
33 1.1 christos * "This product includes cryptographic software written by
34 1.1 christos * Eric Young (eay (at) cryptsoft.com)"
35 1.1 christos * The word 'cryptographic' can be left out if the rouines from the library
36 1.1 christos * being used are not cryptographic related :-).
37 1.1 christos * 4. If you include any Windows specific code (or a derivative thereof) from
38 1.1 christos * the apps directory (application code) you must include an acknowledgement:
39 1.1 christos * "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)"
40 1.1 christos *
41 1.1 christos * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 1.1 christos * SUCH DAMAGE.
52 1.1 christos *
53 1.1 christos * The licence and distribution terms for any publically available version or
54 1.1 christos * derivative of this code cannot be changed. i.e. this code cannot simply be
55 1.1 christos * copied and put under another distribution licence
56 1.1 christos * [including the GNU Public Licence.]
57 1.1 christos */
58 1.1 christos
59 1.1 christos #include "des_locl.h"
60 1.1 christos
61 1.1 christos void des_encrypt1(DES_LONG *data, des_key_schedule ks, int enc)
62 1.1 christos {
63 1.1 christos register DES_LONG l,r,t,u;
64 1.1 christos #ifdef DES_PTR
65 1.1 christos register const unsigned char *des_SP=(const unsigned char *)des_SPtrans;
66 1.1 christos #endif
67 1.1 christos #ifndef DES_UNROLL
68 1.1 christos register int i;
69 1.1 christos #endif
70 1.1 christos register DES_LONG *s;
71 1.1 christos
72 1.1 christos r=data[0];
73 1.1 christos l=data[1];
74 1.1 christos
75 1.1 christos IP(r,l);
76 1.1 christos /* Things have been modified so that the initial rotate is
77 1.1 christos * done outside the loop. This required the
78 1.1 christos * des_SPtrans values in sp.h to be rotated 1 bit to the right.
79 1.1 christos * One perl script later and things have a 5% speed up on a sparc2.
80 1.1 christos * Thanks to Richard Outerbridge <71755.204 (at) CompuServe.COM>
81 1.1 christos * for pointing this out. */
82 1.1 christos /* clear the top bits on machines with 8byte longs */
83 1.1 christos /* shift left by 2 */
84 1.1 christos r=ROTATE(r,29)&0xffffffffL;
85 1.1 christos l=ROTATE(l,29)&0xffffffffL;
86 1.1 christos
87 1.1 christos s=ks;
88 1.1 christos /* I don't know if it is worth the effort of loop unrolling the
89 1.1 christos * inner loop */
90 1.1 christos if (enc)
91 1.1 christos {
92 1.1 christos #ifdef DES_UNROLL
93 1.1 christos D_ENCRYPT(l,r, 0); /* 1 */
94 1.1 christos D_ENCRYPT(r,l, 2); /* 2 */
95 1.1 christos D_ENCRYPT(l,r, 4); /* 3 */
96 1.1 christos D_ENCRYPT(r,l, 6); /* 4 */
97 1.1 christos D_ENCRYPT(l,r, 8); /* 5 */
98 1.1 christos D_ENCRYPT(r,l,10); /* 6 */
99 1.1 christos D_ENCRYPT(l,r,12); /* 7 */
100 1.1 christos D_ENCRYPT(r,l,14); /* 8 */
101 1.1 christos D_ENCRYPT(l,r,16); /* 9 */
102 1.1 christos D_ENCRYPT(r,l,18); /* 10 */
103 1.1 christos D_ENCRYPT(l,r,20); /* 11 */
104 1.1 christos D_ENCRYPT(r,l,22); /* 12 */
105 1.1 christos D_ENCRYPT(l,r,24); /* 13 */
106 1.1 christos D_ENCRYPT(r,l,26); /* 14 */
107 1.1 christos D_ENCRYPT(l,r,28); /* 15 */
108 1.1 christos D_ENCRYPT(r,l,30); /* 16 */
109 1.1 christos #else
110 1.1 christos for (i=0; i<32; i+=8)
111 1.1 christos {
112 1.1 christos D_ENCRYPT(l,r,i+0); /* 1 */
113 1.1 christos D_ENCRYPT(r,l,i+2); /* 2 */
114 1.1 christos D_ENCRYPT(l,r,i+4); /* 3 */
115 1.1 christos D_ENCRYPT(r,l,i+6); /* 4 */
116 1.1 christos }
117 1.1 christos #endif
118 1.1 christos }
119 1.1 christos else
120 1.1 christos {
121 1.1 christos #ifdef DES_UNROLL
122 1.1 christos D_ENCRYPT(l,r,30); /* 16 */
123 1.1 christos D_ENCRYPT(r,l,28); /* 15 */
124 1.1 christos D_ENCRYPT(l,r,26); /* 14 */
125 1.1 christos D_ENCRYPT(r,l,24); /* 13 */
126 1.1 christos D_ENCRYPT(l,r,22); /* 12 */
127 1.1 christos D_ENCRYPT(r,l,20); /* 11 */
128 1.1 christos D_ENCRYPT(l,r,18); /* 10 */
129 1.1 christos D_ENCRYPT(r,l,16); /* 9 */
130 1.1 christos D_ENCRYPT(l,r,14); /* 8 */
131 1.1 christos D_ENCRYPT(r,l,12); /* 7 */
132 1.1 christos D_ENCRYPT(l,r,10); /* 6 */
133 1.1 christos D_ENCRYPT(r,l, 8); /* 5 */
134 1.1 christos D_ENCRYPT(l,r, 6); /* 4 */
135 1.1 christos D_ENCRYPT(r,l, 4); /* 3 */
136 1.1 christos D_ENCRYPT(l,r, 2); /* 2 */
137 1.1 christos D_ENCRYPT(r,l, 0); /* 1 */
138 1.1 christos #else
139 1.1 christos for (i=30; i>0; i-=8)
140 1.1 christos {
141 1.1 christos D_ENCRYPT(l,r,i-0); /* 16 */
142 1.1 christos D_ENCRYPT(r,l,i-2); /* 15 */
143 1.1 christos D_ENCRYPT(l,r,i-4); /* 14 */
144 1.1 christos D_ENCRYPT(r,l,i-6); /* 13 */
145 1.1 christos }
146 1.1 christos #endif
147 1.1 christos }
148 1.1 christos
149 1.1 christos /* rotate and clear the top bits on machines with 8byte longs */
150 1.1 christos l=ROTATE(l,3)&0xffffffffL;
151 1.1 christos r=ROTATE(r,3)&0xffffffffL;
152 1.1 christos
153 1.1 christos FP(r,l);
154 1.1 christos data[0]=l;
155 1.1 christos data[1]=r;
156 1.1 christos l=r=t=u=0;
157 1.1 christos }
158 1.1 christos
159 1.1 christos void des_encrypt2(DES_LONG *data, des_key_schedule ks, int enc)
160 1.1 christos {
161 1.1 christos register DES_LONG l,r,t,u;
162 1.1 christos #ifdef DES_PTR
163 1.1 christos register const unsigned char *des_SP=(const unsigned char *)des_SPtrans;
164 1.1 christos #endif
165 1.1 christos #ifndef DES_UNROLL
166 1.1 christos register int i;
167 1.1 christos #endif
168 1.1 christos register DES_LONG *s;
169 1.1 christos
170 1.1 christos r=data[0];
171 1.1 christos l=data[1];
172 1.1 christos
173 1.1 christos /* Things have been modified so that the initial rotate is
174 1.1 christos * done outside the loop. This required the
175 1.1 christos * des_SPtrans values in sp.h to be rotated 1 bit to the right.
176 1.1 christos * One perl script later and things have a 5% speed up on a sparc2.
177 1.1 christos * Thanks to Richard Outerbridge <71755.204 (at) CompuServe.COM>
178 1.1 christos * for pointing this out. */
179 1.1 christos /* clear the top bits on machines with 8byte longs */
180 1.1 christos r=ROTATE(r,29)&0xffffffffL;
181 1.1 christos l=ROTATE(l,29)&0xffffffffL;
182 1.1 christos
183 1.1 christos s=ks;
184 1.1 christos /* I don't know if it is worth the effort of loop unrolling the
185 1.1 christos * inner loop */
186 1.1 christos if (enc)
187 1.1 christos {
188 1.1 christos #ifdef DES_UNROLL
189 1.1 christos D_ENCRYPT(l,r, 0); /* 1 */
190 1.1 christos D_ENCRYPT(r,l, 2); /* 2 */
191 1.1 christos D_ENCRYPT(l,r, 4); /* 3 */
192 1.1 christos D_ENCRYPT(r,l, 6); /* 4 */
193 1.1 christos D_ENCRYPT(l,r, 8); /* 5 */
194 1.1 christos D_ENCRYPT(r,l,10); /* 6 */
195 1.1 christos D_ENCRYPT(l,r,12); /* 7 */
196 1.1 christos D_ENCRYPT(r,l,14); /* 8 */
197 1.1 christos D_ENCRYPT(l,r,16); /* 9 */
198 1.1 christos D_ENCRYPT(r,l,18); /* 10 */
199 1.1 christos D_ENCRYPT(l,r,20); /* 11 */
200 1.1 christos D_ENCRYPT(r,l,22); /* 12 */
201 1.1 christos D_ENCRYPT(l,r,24); /* 13 */
202 1.1 christos D_ENCRYPT(r,l,26); /* 14 */
203 1.1 christos D_ENCRYPT(l,r,28); /* 15 */
204 1.1 christos D_ENCRYPT(r,l,30); /* 16 */
205 1.1 christos #else
206 1.1 christos for (i=0; i<32; i+=8)
207 1.1 christos {
208 1.1 christos D_ENCRYPT(l,r,i+0); /* 1 */
209 1.1 christos D_ENCRYPT(r,l,i+2); /* 2 */
210 1.1 christos D_ENCRYPT(l,r,i+4); /* 3 */
211 1.1 christos D_ENCRYPT(r,l,i+6); /* 4 */
212 1.1 christos }
213 1.1 christos #endif
214 1.1 christos }
215 1.1 christos else
216 1.1 christos {
217 1.1 christos #ifdef DES_UNROLL
218 1.1 christos D_ENCRYPT(l,r,30); /* 16 */
219 1.1 christos D_ENCRYPT(r,l,28); /* 15 */
220 1.1 christos D_ENCRYPT(l,r,26); /* 14 */
221 1.1 christos D_ENCRYPT(r,l,24); /* 13 */
222 1.1 christos D_ENCRYPT(l,r,22); /* 12 */
223 1.1 christos D_ENCRYPT(r,l,20); /* 11 */
224 1.1 christos D_ENCRYPT(l,r,18); /* 10 */
225 1.1 christos D_ENCRYPT(r,l,16); /* 9 */
226 1.1 christos D_ENCRYPT(l,r,14); /* 8 */
227 1.1 christos D_ENCRYPT(r,l,12); /* 7 */
228 1.1 christos D_ENCRYPT(l,r,10); /* 6 */
229 1.1 christos D_ENCRYPT(r,l, 8); /* 5 */
230 1.1 christos D_ENCRYPT(l,r, 6); /* 4 */
231 1.1 christos D_ENCRYPT(r,l, 4); /* 3 */
232 1.1 christos D_ENCRYPT(l,r, 2); /* 2 */
233 1.1 christos D_ENCRYPT(r,l, 0); /* 1 */
234 1.1 christos #else
235 1.1 christos for (i=30; i>0; i-=8)
236 1.1 christos {
237 1.1 christos D_ENCRYPT(l,r,i-0); /* 16 */
238 1.1 christos D_ENCRYPT(r,l,i-2); /* 15 */
239 1.1 christos D_ENCRYPT(l,r,i-4); /* 14 */
240 1.1 christos D_ENCRYPT(r,l,i-6); /* 13 */
241 1.1 christos }
242 1.1 christos #endif
243 1.1 christos }
244 1.1 christos /* rotate and clear the top bits on machines with 8byte longs */
245 1.1 christos data[0]=ROTATE(l,3)&0xffffffffL;
246 1.1 christos data[1]=ROTATE(r,3)&0xffffffffL;
247 1.1 christos l=r=t=u=0;
248 1.1 christos }
249 1.1 christos
250 1.1 christos void des_encrypt3(DES_LONG *data, des_key_schedule ks1, des_key_schedule ks2,
251 1.1 christos des_key_schedule ks3)
252 1.1 christos {
253 1.1 christos register DES_LONG l,r;
254 1.1 christos
255 1.1 christos l=data[0];
256 1.1 christos r=data[1];
257 1.1 christos IP(l,r);
258 1.1 christos data[0]=l;
259 1.1 christos data[1]=r;
260 1.1 christos des_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT);
261 1.1 christos des_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT);
262 1.1 christos des_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT);
263 1.1 christos l=data[0];
264 1.1 christos r=data[1];
265 1.1 christos FP(r,l);
266 1.1 christos data[0]=l;
267 1.1 christos data[1]=r;
268 1.1 christos }
269 1.1 christos
270 1.1 christos void des_decrypt3(DES_LONG *data, des_key_schedule ks1, des_key_schedule ks2,
271 1.1 christos des_key_schedule ks3)
272 1.1 christos {
273 1.1 christos register DES_LONG l,r;
274 1.1 christos
275 1.1 christos l=data[0];
276 1.1 christos r=data[1];
277 1.1 christos IP(l,r);
278 1.1 christos data[0]=l;
279 1.1 christos data[1]=r;
280 1.1 christos des_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT);
281 1.1 christos des_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT);
282 1.1 christos des_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT);
283 1.1 christos l=data[0];
284 1.1 christos r=data[1];
285 1.1 christos FP(r,l);
286 1.1 christos data[0]=l;
287 1.1 christos data[1]=r;
288 1.1 christos }
289 1.1 christos
290 1.1 christos #ifndef DES_DEFAULT_OPTIONS
291 1.1 christos
292 1.1 christos #undef CBC_ENC_C__DONT_UPDATE_IV
293 1.1 christos #include "oncbc_enc.c" /* des_ncbc_encrypt */
294 1.1 christos
295 1.1 christos void des_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
296 1.1 christos long length, des_key_schedule ks1, des_key_schedule ks2,
297 1.1 christos des_key_schedule ks3, des_cblock *ivec, int enc)
298 1.1 christos {
299 1.1 christos register DES_LONG tin0,tin1;
300 1.1 christos register DES_LONG tout0,tout1,xor0,xor1;
301 1.1 christos register const unsigned char *in;
302 1.1 christos unsigned char *out;
303 1.1 christos register long l=length;
304 1.1 christos DES_LONG tin[2];
305 1.1 christos unsigned char *iv;
306 1.1 christos
307 1.1 christos in=input;
308 1.1 christos out=output;
309 1.1 christos iv = &(*ivec)[0];
310 1.1 christos
311 1.1 christos if (enc)
312 1.1 christos {
313 1.1 christos c2l(iv,tout0);
314 1.1 christos c2l(iv,tout1);
315 1.1 christos for (l-=8; l>=0; l-=8)
316 1.1 christos {
317 1.1 christos c2l(in,tin0);
318 1.1 christos c2l(in,tin1);
319 1.1 christos tin0^=tout0;
320 1.1 christos tin1^=tout1;
321 1.1 christos
322 1.1 christos tin[0]=tin0;
323 1.1 christos tin[1]=tin1;
324 1.1 christos des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
325 1.1 christos tout0=tin[0];
326 1.1 christos tout1=tin[1];
327 1.1 christos
328 1.1 christos l2c(tout0,out);
329 1.1 christos l2c(tout1,out);
330 1.1 christos }
331 1.1 christos if (l != -8)
332 1.1 christos {
333 1.1 christos c2ln(in,tin0,tin1,l+8);
334 1.1 christos tin0^=tout0;
335 1.1 christos tin1^=tout1;
336 1.1 christos
337 1.1 christos tin[0]=tin0;
338 1.1 christos tin[1]=tin1;
339 1.1 christos des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
340 1.1 christos tout0=tin[0];
341 1.1 christos tout1=tin[1];
342 1.1 christos
343 1.1 christos l2c(tout0,out);
344 1.1 christos l2c(tout1,out);
345 1.1 christos }
346 1.1 christos iv = &(*ivec)[0];
347 1.1 christos l2c(tout0,iv);
348 1.1 christos l2c(tout1,iv);
349 1.1 christos }
350 1.1 christos else
351 1.1 christos {
352 1.1 christos register DES_LONG t0,t1;
353 1.1 christos
354 1.1 christos c2l(iv,xor0);
355 1.1 christos c2l(iv,xor1);
356 1.1 christos for (l-=8; l>=0; l-=8)
357 1.1 christos {
358 1.1 christos c2l(in,tin0);
359 1.1 christos c2l(in,tin1);
360 1.1 christos
361 1.1 christos t0=tin0;
362 1.1 christos t1=tin1;
363 1.1 christos
364 1.1 christos tin[0]=tin0;
365 1.1 christos tin[1]=tin1;
366 1.1 christos des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
367 1.1 christos tout0=tin[0];
368 1.1 christos tout1=tin[1];
369 1.1 christos
370 1.1 christos tout0^=xor0;
371 1.1 christos tout1^=xor1;
372 1.1 christos l2c(tout0,out);
373 1.1 christos l2c(tout1,out);
374 1.1 christos xor0=t0;
375 1.1 christos xor1=t1;
376 1.1 christos }
377 1.1 christos if (l != -8)
378 1.1 christos {
379 1.1 christos c2l(in,tin0);
380 1.1 christos c2l(in,tin1);
381 1.1 christos
382 1.1 christos t0=tin0;
383 1.1 christos t1=tin1;
384 1.1 christos
385 1.1 christos tin[0]=tin0;
386 1.1 christos tin[1]=tin1;
387 1.1 christos des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
388 1.1 christos tout0=tin[0];
389 1.1 christos tout1=tin[1];
390 1.1 christos
391 1.1 christos tout0^=xor0;
392 1.1 christos tout1^=xor1;
393 1.1 christos l2cn(tout0,tout1,out,l+8);
394 1.1 christos xor0=t0;
395 1.1 christos xor1=t1;
396 1.1 christos }
397 1.1 christos
398 1.1 christos iv = &(*ivec)[0];
399 1.1 christos l2c(xor0,iv);
400 1.1 christos l2c(xor1,iv);
401 1.1 christos }
402 1.1 christos tin0=tin1=tout0=tout1=xor0=xor1=0;
403 1.1 christos tin[0]=tin[1]=0;
404 1.1 christos }
405 1.1 christos
406 1.1 christos #endif /* DES_DEFAULT_OPTIONS */
407