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