|
|
1.1.1.3 root 1: /* Deprecated/legacy */
2:
1.1 root 3: /* crypto/des/des_locl.org */
4: /* Copyright (C) 1995-1997 Eric Young ([email protected])
5: * All rights reserved.
6: *
7: * This package is an SSL implementation written
8: * by Eric Young ([email protected]).
9: * The implementation was written so as to conform with Netscapes SSL.
10: *
11: * This library is free for commercial and non-commercial use as long as
12: * the following conditions are aheared to. The following conditions
13: * apply to all code found in this distribution, be it the RC4, RSA,
14: * lhash, DES, etc., code; not just the SSL code. The SSL documentation
15: * included with this distribution is covered by the same copyright terms
16: * except that the holder is Tim Hudson ([email protected]).
17: *
18: * Copyright remains Eric Young's, and as such any Copyright notices in
19: * the code are not to be removed.
20: * If this package is used in a product, Eric Young should be given attribution
21: * as the author of the parts of the library used.
22: * This can be in the form of a textual message at program startup or
23: * in documentation (online or textual) provided with the package.
24: *
25: * Redistribution and use in source and binary forms, with or without
26: * modification, are permitted provided that the following conditions
27: * are met:
28: * 1. Redistributions of source code must retain the copyright
29: * notice, this list of conditions and the following disclaimer.
30: * 2. Redistributions in binary form must reproduce the above copyright
31: * notice, this list of conditions and the following disclaimer in the
32: * documentation and/or other materials provided with the distribution.
33: * 3. All advertising materials mentioning features or use of this software
34: * must display the following acknowledgement:
35: * "This product includes cryptographic software written by
36: * Eric Young ([email protected])"
37: * The word 'cryptographic' can be left out if the rouines from the library
38: * being used are not cryptographic related :-).
39: * 4. If you include any Windows specific code (or a derivative thereof) from
40: * the apps directory (application code) you must include an acknowledgement:
41: * "This product includes software written by Tim Hudson ([email protected])"
42: *
43: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53: * SUCH DAMAGE.
54: *
55: * The licence and distribution terms for any publically available version or
56: * derivative of this code cannot be changed. i.e. this code cannot simply be
57: * copied and put under another distribution licence
58: * [including the GNU Public Licence.]
59: */
60:
61: /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
62: *
63: * Always modify des_locl.org since des_locl.h is automatically generated from
64: * it during SSLeay configuration.
65: *
66: * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
67: */
68:
69: #ifndef HEADER_DES_LOCL_H
70: #define HEADER_DES_LOCL_H
71:
72: #if defined(WIN32) || defined(WIN16)
73: #ifndef MSDOS
74: #define MSDOS
75: #endif
76: #endif
77:
78: #include <stdio.h>
79: #include <stdlib.h>
80: #ifndef MSDOS
81: #include <unistd.h>
82: #endif
1.1.1.2 root 83: #include "Des.h"
1.1 root 84:
85: #ifndef DES_DEFAULT_OPTIONS
86: /* the following is tweaked from a config script, that is why it is a
87: * protected undef/define */
88: #ifndef DES_PTR
89: #define DES_PTR
90: #endif
91:
92: /* This helps C compiler generate the correct code for multiple functional
93: * units. It reduces register dependancies at the expense of 2 more
94: * registers */
95: #ifndef DES_RISC1
96: #define DES_RISC1
97: #endif
98:
99: #ifndef DES_RISC2
100: #undef DES_RISC2
101: #endif
102:
103: #if defined(DES_RISC1) && defined(DES_RISC2)
104: YOU SHOULD NOT HAVE BOTH DES_RISC1 AND DES_RISC2 DEFINED!!!!!
105: #endif
106:
107: /* Unroll the inner loop, this sometimes helps, sometimes hinders.
108: * Very mucy CPU dependant */
109: #ifndef DES_UNROLL
110: #define DES_UNROLL
111: #endif
112:
113: /* These default values were supplied by
114: * Peter Gutman <[email protected]>
115: * They are only used if nothing else has been defined */
116: #if !defined(DES_PTR) && !defined(DES_RISC1) && !defined(DES_RISC2) && !defined(DES_UNROLL)
117: /* Special defines which change the way the code is built depending on the
118: CPU and OS. For SGI machines you can use _MIPS_SZLONG (32 or 64) to find
119: even newer MIPS CPU's, but at the moment one size fits all for
120: optimization options. Older Sparc's work better with only UNROLL, but
121: there's no way to tell at compile time what it is you're running on */
122:
123: #if defined( sun ) /* Newer Sparc's */
124: #define DES_PTR
125: #define DES_RISC1
126: #define DES_UNROLL
127: #elif defined( __ultrix ) /* Older MIPS */
128: #define DES_PTR
129: #define DES_RISC2
130: #define DES_UNROLL
131: #elif defined( __osf1__ ) /* Alpha */
132: #define DES_PTR
133: #define DES_RISC2
134: #elif defined ( _AIX ) /* RS6000 */
135: /* Unknown */
136: #elif defined( __hpux ) /* HP-PA */
137: /* Unknown */
138: #elif defined( __aux ) /* 68K */
139: /* Unknown */
140: #elif defined( __dgux ) /* 88K (but P6 in latest boxes) */
141: #define DES_UNROLL
142: #elif defined( __sgi ) /* Newer MIPS */
143: #define DES_PTR
144: #define DES_RISC2
145: #define DES_UNROLL
146: #elif defined( i386 ) /* x86 boxes, should be gcc */
147: #define DES_PTR
148: #define DES_RISC1
149: #define DES_UNROLL
150: #endif /* Systems-specific speed defines */
151: #endif
152:
153: #endif /* DES_DEFAULT_OPTIONS */
154:
155: #ifdef MSDOS /* Visual C++ 2.1 (Windows NT/95) */
156: #include <stdlib.h>
157: #include <errno.h>
158: #include <time.h>
159: #include <io.h>
160: #ifndef RAND
161: #define RAND
162: #endif
163: #undef NOPROTO
164: #endif
165:
166: #if defined(__STDC__) || defined(VMS) || defined(M_XENIX) || defined(MSDOS)
167: #include <string.h>
168: #endif
169:
170: #ifndef RAND
171: #define RAND
172: #endif
173:
174: #ifdef linux
175: #undef RAND
176: #endif
177:
178: #ifdef MSDOS
179: #define getpid() 2
180: #define RAND
181: #undef NOPROTO
182: #endif
183:
184: #if defined(NOCONST)
185: #define const
186: #endif
187:
188: #ifdef __STDC__
189: #undef NOPROTO
190: #endif
191:
192: #ifdef RAND
193: #define srandom(s) srand(s)
194: #define random rand
195: #endif
196:
197: #define ITERATIONS 16
198: #define HALF_ITERATIONS 8
199:
200: /* used in des_read and des_write */
201: #define MAXWRITE (1024*16)
202: #define BSIZE (MAXWRITE+4)
203:
204: #define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
205: l|=((DES_LONG)(*((c)++)))<< 8L, \
206: l|=((DES_LONG)(*((c)++)))<<16L, \
207: l|=((DES_LONG)(*((c)++)))<<24L)
208:
209: /* NOTE - c is not incremented as per c2l */
210: #define c2ln(c,l1,l2,n) { \
211: c+=n; \
212: l1=l2=0; \
213: switch (n) { \
214: case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
215: case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
216: case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
217: case 5: l2|=((DES_LONG)(*(--(c)))); \
218: case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
219: case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
220: case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
221: case 1: l1|=((DES_LONG)(*(--(c)))); \
222: } \
223: }
224:
225: #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
226: *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
227: *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
228: *((c)++)=(unsigned char)(((l)>>24L)&0xff))
229:
230: /* replacements for htonl and ntohl since I have no idea what to do
231: * when faced with machines with 8 byte longs. */
232: #define HDRSIZE 4
233:
234: #define n2l(c,l) (l =((DES_LONG)(*((c)++)))<<24L, \
235: l|=((DES_LONG)(*((c)++)))<<16L, \
236: l|=((DES_LONG)(*((c)++)))<< 8L, \
237: l|=((DES_LONG)(*((c)++))))
238:
239: #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
240: *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
241: *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
242: *((c)++)=(unsigned char)(((l) )&0xff))
243:
244: /* NOTE - c is not incremented as per l2c */
245: #define l2cn(l1,l2,c,n) { \
246: c+=n; \
247: switch (n) { \
248: case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
249: case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
250: case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
251: case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
252: case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
253: case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
254: case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
255: case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
256: } \
257: }
258:
259: //#if defined(WIN32)
260: //#define ROTATE(a,n) (_lrotr(a,n))
261: //#else
262: #define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n))))
263: //#endif
264:
265: /* Don't worry about the LOAD_DATA() stuff, that is used by
266: * fcrypt() to add it's little bit to the front */
267:
268: #ifdef DES_FCRYPT
269:
270: #define LOAD_DATA_tmp(R,S,u,t,E0,E1) \
271: { DES_LONG tmp; LOAD_DATA(R,S,u,t,E0,E1,tmp); }
272:
273: #define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
274: t=R^(R>>16L); \
275: u=t&E0; t&=E1; \
276: tmp=(u<<16); u^=R^s[S ]; u^=tmp; \
277: tmp=(t<<16); t^=R^s[S+1]; t^=tmp
278: #else
279: #define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
280: #define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
281: u=R^s[S ]; \
282: t=R^s[S+1]
283: #endif
284:
285: /* The changes to this macro may help or hinder, depending on the
286: * compiler and the achitecture. gcc2 always seems to do well :-).
287: * Inspired by Dana How <[email protected]>
288: * DO NOT use the alternative version on machines with 8 byte longs.
289: * It does not seem to work on the Alpha, even when DES_LONG is 4
290: * bytes, probably an issue of accessing non-word aligned objects :-( */
291: #ifdef DES_PTR
292:
293: /* It recently occured to me that 0^0^0^0^0^0^0 == 0, so there
294: * is no reason to not xor all the sub items together. This potentially
295: * saves a register since things can be xored directly into L */
296:
297: #if defined(DES_RISC1) || defined(DES_RISC2)
298: #ifdef DES_RISC1
299: #define D_ENCRYPT(LL,R,S) { \
300: unsigned int u1,u2,u3; \
301: LOAD_DATA(R,S,u,t,E0,E1,u1); \
302: u2=(int)u>>8L; \
303: u1=(int)u&0xfc; \
304: u2&=0xfc; \
305: t=ROTATE(t,4); \
306: u>>=16L; \
307: LL^= *(DES_LONG *)((unsigned char *)des_SP +u1); \
308: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x200+u2); \
309: u3=(int)(u>>8L); \
310: u1=(int)u&0xfc; \
311: u3&=0xfc; \
312: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x400+u1); \
313: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x600+u3); \
314: u2=(int)t>>8L; \
315: u1=(int)t&0xfc; \
316: u2&=0xfc; \
317: t>>=16L; \
318: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x100+u1); \
319: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x300+u2); \
320: u3=(int)t>>8L; \
321: u1=(int)t&0xfc; \
322: u3&=0xfc; \
323: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x500+u1); \
324: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x700+u3); }
325: #endif
326: #ifdef DES_RISC2
327: #define D_ENCRYPT(LL,R,S) { \
328: unsigned int u1,u2,s1,s2; \
329: LOAD_DATA(R,S,u,t,E0,E1,u1); \
330: u2=(int)u>>8L; \
331: u1=(int)u&0xfc; \
332: u2&=0xfc; \
333: t=ROTATE(t,4); \
334: LL^= *(DES_LONG *)((unsigned char *)des_SP +u1); \
335: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x200+u2); \
336: s1=(int)(u>>16L); \
337: s2=(int)(u>>24L); \
338: s1&=0xfc; \
339: s2&=0xfc; \
340: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x400+s1); \
341: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x600+s2); \
342: u2=(int)t>>8L; \
343: u1=(int)t&0xfc; \
344: u2&=0xfc; \
345: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x100+u1); \
346: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x300+u2); \
347: s1=(int)(t>>16L); \
348: s2=(int)(t>>24L); \
349: s1&=0xfc; \
350: s2&=0xfc; \
351: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x500+s1); \
352: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x700+s2); }
353: #endif
354: #else
355: #define D_ENCRYPT(LL,R,S) { \
356: LOAD_DATA_tmp(R,S,u,t,E0,E1); \
357: t=ROTATE(t,4); \
358: LL^= \
359: *(DES_LONG *)((unsigned char *)des_SP +((u )&0xfc))^ \
360: *(DES_LONG *)((unsigned char *)des_SP+0x200+((u>> 8L)&0xfc))^ \
361: *(DES_LONG *)((unsigned char *)des_SP+0x400+((u>>16L)&0xfc))^ \
362: *(DES_LONG *)((unsigned char *)des_SP+0x600+((u>>24L)&0xfc))^ \
363: *(DES_LONG *)((unsigned char *)des_SP+0x100+((t )&0xfc))^ \
364: *(DES_LONG *)((unsigned char *)des_SP+0x300+((t>> 8L)&0xfc))^ \
365: *(DES_LONG *)((unsigned char *)des_SP+0x500+((t>>16L)&0xfc))^ \
366: *(DES_LONG *)((unsigned char *)des_SP+0x700+((t>>24L)&0xfc)); }
367: #endif
368:
369: #else /* original version */
370:
371: #if defined(DES_RISC1) || defined(DES_RISC2)
372: #ifdef DES_RISC1
373: #define D_ENCRYPT(LL,R,S) {\
374: unsigned int u1,u2,u3; \
375: LOAD_DATA(R,S,u,t,E0,E1,u1); \
376: u>>=2L; \
377: t=ROTATE(t,6); \
378: u2=(int)u>>8L; \
379: u1=(int)u&0x3f; \
380: u2&=0x3f; \
381: u>>=16L; \
382: LL^=des_SPtrans[0][u1]; \
383: LL^=des_SPtrans[2][u2]; \
384: u3=(int)u>>8L; \
385: u1=(int)u&0x3f; \
386: u3&=0x3f; \
387: LL^=des_SPtrans[4][u1]; \
388: LL^=des_SPtrans[6][u3]; \
389: u2=(int)t>>8L; \
390: u1=(int)t&0x3f; \
391: u2&=0x3f; \
392: t>>=16L; \
393: LL^=des_SPtrans[1][u1]; \
394: LL^=des_SPtrans[3][u2]; \
395: u3=(int)t>>8L; \
396: u1=(int)t&0x3f; \
397: u3&=0x3f; \
398: LL^=des_SPtrans[5][u1]; \
399: LL^=des_SPtrans[7][u3]; }
400: #endif
401: #ifdef DES_RISC2
402: #define D_ENCRYPT(LL,R,S) {\
403: unsigned int u1,u2,s1,s2; \
404: LOAD_DATA(R,S,u,t,E0,E1,u1); \
405: u>>=2L; \
406: t=ROTATE(t,6); \
407: u2=(int)u>>8L; \
408: u1=(int)u&0x3f; \
409: u2&=0x3f; \
410: LL^=des_SPtrans[0][u1]; \
411: LL^=des_SPtrans[2][u2]; \
412: s1=(int)u>>16L; \
413: s2=(int)u>>24L; \
414: s1&=0x3f; \
415: s2&=0x3f; \
416: LL^=des_SPtrans[4][s1]; \
417: LL^=des_SPtrans[6][s2]; \
418: u2=(int)t>>8L; \
419: u1=(int)t&0x3f; \
420: u2&=0x3f; \
421: LL^=des_SPtrans[1][u1]; \
422: LL^=des_SPtrans[3][u2]; \
423: s1=(int)t>>16; \
424: s2=(int)t>>24L; \
425: s1&=0x3f; \
426: s2&=0x3f; \
427: LL^=des_SPtrans[5][s1]; \
428: LL^=des_SPtrans[7][s2]; }
429: #endif
430:
431: #else
432:
433: #define D_ENCRYPT(LL,R,S) {\
434: LOAD_DATA_tmp(R,S,u,t,E0,E1); \
435: t=ROTATE(t,4); \
436: LL^=\
437: des_SPtrans[0][(u>> 2L)&0x3f]^ \
438: des_SPtrans[2][(u>>10L)&0x3f]^ \
439: des_SPtrans[4][(u>>18L)&0x3f]^ \
440: des_SPtrans[6][(u>>26L)&0x3f]^ \
441: des_SPtrans[1][(t>> 2L)&0x3f]^ \
442: des_SPtrans[3][(t>>10L)&0x3f]^ \
443: des_SPtrans[5][(t>>18L)&0x3f]^ \
444: des_SPtrans[7][(t>>26L)&0x3f]; }
445: #endif
446: #endif
447:
448: /* IP and FP
449: * The problem is more of a geometric problem that random bit fiddling.
450: 0 1 2 3 4 5 6 7 62 54 46 38 30 22 14 6
451: 8 9 10 11 12 13 14 15 60 52 44 36 28 20 12 4
452: 16 17 18 19 20 21 22 23 58 50 42 34 26 18 10 2
453: 24 25 26 27 28 29 30 31 to 56 48 40 32 24 16 8 0
454:
455: 32 33 34 35 36 37 38 39 63 55 47 39 31 23 15 7
456: 40 41 42 43 44 45 46 47 61 53 45 37 29 21 13 5
457: 48 49 50 51 52 53 54 55 59 51 43 35 27 19 11 3
458: 56 57 58 59 60 61 62 63 57 49 41 33 25 17 9 1
459:
460: The output has been subject to swaps of the form
461: 0 1 -> 3 1 but the odd and even bits have been put into
462: 2 3 2 0
463: different words. The main trick is to remember that
464: t=((l>>size)^r)&(mask);
465: r^=t;
466: l^=(t<<size);
467: can be used to swap and move bits between words.
468:
469: So l = 0 1 2 3 r = 16 17 18 19
470: 4 5 6 7 20 21 22 23
471: 8 9 10 11 24 25 26 27
472: 12 13 14 15 28 29 30 31
473: becomes (for size == 2 and mask == 0x3333)
474: t = 2^16 3^17 -- -- l = 0 1 16 17 r = 2 3 18 19
475: 6^20 7^21 -- -- 4 5 20 21 6 7 22 23
476: 10^24 11^25 -- -- 8 9 24 25 10 11 24 25
477: 14^28 15^29 -- -- 12 13 28 29 14 15 28 29
478:
479: Thanks for hints from Richard Outerbridge - he told me IP&FP
480: could be done in 15 xor, 10 shifts and 5 ands.
481: When I finally started to think of the problem in 2D
482: I first got ~42 operations without xors. When I remembered
483: how to use xors :-) I got it to its final state.
484: */
485: #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
486: (b)^=(t),\
487: (a)^=((t)<<(n)))
488:
489: #define IP(l,r) \
490: { \
491: register DES_LONG tt; \
492: PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
493: PERM_OP(l,r,tt,16,0x0000ffffL); \
494: PERM_OP(r,l,tt, 2,0x33333333L); \
495: PERM_OP(l,r,tt, 8,0x00ff00ffL); \
496: PERM_OP(r,l,tt, 1,0x55555555L); \
497: }
498:
499: #define FP(l,r) \
500: { \
501: register DES_LONG tt; \
502: PERM_OP(l,r,tt, 1,0x55555555L); \
503: PERM_OP(r,l,tt, 8,0x00ff00ffL); \
504: PERM_OP(l,r,tt, 2,0x33333333L); \
505: PERM_OP(r,l,tt,16,0x0000ffffL); \
506: PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
507: }
508:
509: extern const DES_LONG des_SPtrans[8][64];
510:
511: #ifndef NOPROTO
512: void fcrypt_body(DES_LONG *out,des_key_schedule ks,
513: DES_LONG Eswap0, DES_LONG Eswap1);
514: #else
515: void fcrypt_body();
516: #endif
517:
518: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.