|
|
1.1 root 1: /* crypto/des/des_locl.org */
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: /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
60: *
61: * Always modify des_locl.org since des_locl.h is automatically generated from
62: * it during SSLeay configuration.
63: *
64: * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
65: */
66:
67: #ifndef HEADER_DES_LOCL_H
68: #define HEADER_DES_LOCL_H
69:
70: #if defined(WIN32) || defined(WIN16)
71: #ifndef MSDOS
72: #define MSDOS
73: #endif
74: #endif
75:
1.1.1.2 ! root 76: #ifndef LINUX_DRIVER
1.1 root 77: #include <stdio.h>
78: #include <stdlib.h>
79: #ifndef MSDOS
80: #include <unistd.h>
81: #endif
1.1.1.2 ! root 82: #endif
! 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)
1.1.1.2 ! root 167: #ifndef LINUX_DRIVER
1.1 root 168: #include <string.h>
169: #endif
1.1.1.2 ! root 170: #endif
1.1 root 171:
172: #ifndef RAND
173: #define RAND
174: #endif
175:
176: #ifdef linux
177: #undef RAND
178: #endif
179:
180: #ifdef MSDOS
181: #define getpid() 2
182: #define RAND
183: #undef NOPROTO
184: #endif
185:
186: #if defined(NOCONST)
187: #define const
188: #endif
189:
190: #ifdef __STDC__
191: #undef NOPROTO
192: #endif
193:
194: #ifdef RAND
195: #define srandom(s) srand(s)
196: #define random rand
197: #endif
198:
199: #define ITERATIONS 16
200: #define HALF_ITERATIONS 8
201:
202: /* used in des_read and des_write */
203: #define MAXWRITE (1024*16)
204: #define BSIZE (MAXWRITE+4)
205:
206: #define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
207: l|=((DES_LONG)(*((c)++)))<< 8L, \
208: l|=((DES_LONG)(*((c)++)))<<16L, \
209: l|=((DES_LONG)(*((c)++)))<<24L)
210:
211: /* NOTE - c is not incremented as per c2l */
212: #define c2ln(c,l1,l2,n) { \
213: c+=n; \
214: l1=l2=0; \
215: switch (n) { \
216: case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
217: case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
218: case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
219: case 5: l2|=((DES_LONG)(*(--(c)))); \
220: case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
221: case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
222: case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
223: case 1: l1|=((DES_LONG)(*(--(c)))); \
224: } \
225: }
226:
227: #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
228: *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
229: *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
230: *((c)++)=(unsigned char)(((l)>>24L)&0xff))
231:
232: /* replacements for htonl and ntohl since I have no idea what to do
233: * when faced with machines with 8 byte longs. */
234: #define HDRSIZE 4
235:
236: #define n2l(c,l) (l =((DES_LONG)(*((c)++)))<<24L, \
237: l|=((DES_LONG)(*((c)++)))<<16L, \
238: l|=((DES_LONG)(*((c)++)))<< 8L, \
239: l|=((DES_LONG)(*((c)++))))
240:
241: #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
242: *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
243: *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
244: *((c)++)=(unsigned char)(((l) )&0xff))
245:
246: /* NOTE - c is not incremented as per l2c */
247: #define l2cn(l1,l2,c,n) { \
248: c+=n; \
249: switch (n) { \
250: case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
251: case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
252: case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
253: case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
254: case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
255: case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
256: case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
257: case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
258: } \
259: }
260:
261: //#if defined(WIN32)
262: //#define ROTATE(a,n) (_lrotr(a,n))
263: //#else
264: #define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n))))
265: //#endif
266:
267: /* Don't worry about the LOAD_DATA() stuff, that is used by
268: * fcrypt() to add it's little bit to the front */
269:
270: #ifdef DES_FCRYPT
271:
272: #define LOAD_DATA_tmp(R,S,u,t,E0,E1) \
273: { DES_LONG tmp; LOAD_DATA(R,S,u,t,E0,E1,tmp); }
274:
275: #define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
276: t=R^(R>>16L); \
277: u=t&E0; t&=E1; \
278: tmp=(u<<16); u^=R^s[S ]; u^=tmp; \
279: tmp=(t<<16); t^=R^s[S+1]; t^=tmp
280: #else
281: #define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
282: #define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
283: u=R^s[S ]; \
284: t=R^s[S+1]
285: #endif
286:
287: /* The changes to this macro may help or hinder, depending on the
288: * compiler and the achitecture. gcc2 always seems to do well :-).
289: * Inspired by Dana How <[email protected]>
290: * DO NOT use the alternative version on machines with 8 byte longs.
291: * It does not seem to work on the Alpha, even when DES_LONG is 4
292: * bytes, probably an issue of accessing non-word aligned objects :-( */
293: #ifdef DES_PTR
294:
295: /* It recently occured to me that 0^0^0^0^0^0^0 == 0, so there
296: * is no reason to not xor all the sub items together. This potentially
297: * saves a register since things can be xored directly into L */
298:
299: #if defined(DES_RISC1) || defined(DES_RISC2)
300: #ifdef DES_RISC1
301: #define D_ENCRYPT(LL,R,S) { \
302: unsigned int u1,u2,u3; \
303: LOAD_DATA(R,S,u,t,E0,E1,u1); \
304: u2=(int)u>>8L; \
305: u1=(int)u&0xfc; \
306: u2&=0xfc; \
307: t=ROTATE(t,4); \
308: u>>=16L; \
309: LL^= *(DES_LONG *)((unsigned char *)des_SP +u1); \
310: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x200+u2); \
311: u3=(int)(u>>8L); \
312: u1=(int)u&0xfc; \
313: u3&=0xfc; \
314: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x400+u1); \
315: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x600+u3); \
316: u2=(int)t>>8L; \
317: u1=(int)t&0xfc; \
318: u2&=0xfc; \
319: t>>=16L; \
320: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x100+u1); \
321: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x300+u2); \
322: u3=(int)t>>8L; \
323: u1=(int)t&0xfc; \
324: u3&=0xfc; \
325: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x500+u1); \
326: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x700+u3); }
327: #endif
328: #ifdef DES_RISC2
329: #define D_ENCRYPT(LL,R,S) { \
330: unsigned int u1,u2,s1,s2; \
331: LOAD_DATA(R,S,u,t,E0,E1,u1); \
332: u2=(int)u>>8L; \
333: u1=(int)u&0xfc; \
334: u2&=0xfc; \
335: t=ROTATE(t,4); \
336: LL^= *(DES_LONG *)((unsigned char *)des_SP +u1); \
337: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x200+u2); \
338: s1=(int)(u>>16L); \
339: s2=(int)(u>>24L); \
340: s1&=0xfc; \
341: s2&=0xfc; \
342: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x400+s1); \
343: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x600+s2); \
344: u2=(int)t>>8L; \
345: u1=(int)t&0xfc; \
346: u2&=0xfc; \
347: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x100+u1); \
348: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x300+u2); \
349: s1=(int)(t>>16L); \
350: s2=(int)(t>>24L); \
351: s1&=0xfc; \
352: s2&=0xfc; \
353: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x500+s1); \
354: LL^= *(DES_LONG *)((unsigned char *)des_SP+0x700+s2); }
355: #endif
356: #else
357: #define D_ENCRYPT(LL,R,S) { \
358: LOAD_DATA_tmp(R,S,u,t,E0,E1); \
359: t=ROTATE(t,4); \
360: LL^= \
361: *(DES_LONG *)((unsigned char *)des_SP +((u )&0xfc))^ \
362: *(DES_LONG *)((unsigned char *)des_SP+0x200+((u>> 8L)&0xfc))^ \
363: *(DES_LONG *)((unsigned char *)des_SP+0x400+((u>>16L)&0xfc))^ \
364: *(DES_LONG *)((unsigned char *)des_SP+0x600+((u>>24L)&0xfc))^ \
365: *(DES_LONG *)((unsigned char *)des_SP+0x100+((t )&0xfc))^ \
366: *(DES_LONG *)((unsigned char *)des_SP+0x300+((t>> 8L)&0xfc))^ \
367: *(DES_LONG *)((unsigned char *)des_SP+0x500+((t>>16L)&0xfc))^ \
368: *(DES_LONG *)((unsigned char *)des_SP+0x700+((t>>24L)&0xfc)); }
369: #endif
370:
371: #else /* original version */
372:
373: #if defined(DES_RISC1) || defined(DES_RISC2)
374: #ifdef DES_RISC1
375: #define D_ENCRYPT(LL,R,S) {\
376: unsigned int u1,u2,u3; \
377: LOAD_DATA(R,S,u,t,E0,E1,u1); \
378: u>>=2L; \
379: t=ROTATE(t,6); \
380: u2=(int)u>>8L; \
381: u1=(int)u&0x3f; \
382: u2&=0x3f; \
383: u>>=16L; \
384: LL^=des_SPtrans[0][u1]; \
385: LL^=des_SPtrans[2][u2]; \
386: u3=(int)u>>8L; \
387: u1=(int)u&0x3f; \
388: u3&=0x3f; \
389: LL^=des_SPtrans[4][u1]; \
390: LL^=des_SPtrans[6][u3]; \
391: u2=(int)t>>8L; \
392: u1=(int)t&0x3f; \
393: u2&=0x3f; \
394: t>>=16L; \
395: LL^=des_SPtrans[1][u1]; \
396: LL^=des_SPtrans[3][u2]; \
397: u3=(int)t>>8L; \
398: u1=(int)t&0x3f; \
399: u3&=0x3f; \
400: LL^=des_SPtrans[5][u1]; \
401: LL^=des_SPtrans[7][u3]; }
402: #endif
403: #ifdef DES_RISC2
404: #define D_ENCRYPT(LL,R,S) {\
405: unsigned int u1,u2,s1,s2; \
406: LOAD_DATA(R,S,u,t,E0,E1,u1); \
407: u>>=2L; \
408: t=ROTATE(t,6); \
409: u2=(int)u>>8L; \
410: u1=(int)u&0x3f; \
411: u2&=0x3f; \
412: LL^=des_SPtrans[0][u1]; \
413: LL^=des_SPtrans[2][u2]; \
414: s1=(int)u>>16L; \
415: s2=(int)u>>24L; \
416: s1&=0x3f; \
417: s2&=0x3f; \
418: LL^=des_SPtrans[4][s1]; \
419: LL^=des_SPtrans[6][s2]; \
420: u2=(int)t>>8L; \
421: u1=(int)t&0x3f; \
422: u2&=0x3f; \
423: LL^=des_SPtrans[1][u1]; \
424: LL^=des_SPtrans[3][u2]; \
425: s1=(int)t>>16; \
426: s2=(int)t>>24L; \
427: s1&=0x3f; \
428: s2&=0x3f; \
429: LL^=des_SPtrans[5][s1]; \
430: LL^=des_SPtrans[7][s2]; }
431: #endif
432:
433: #else
434:
435: #define D_ENCRYPT(LL,R,S) {\
436: LOAD_DATA_tmp(R,S,u,t,E0,E1); \
437: t=ROTATE(t,4); \
438: LL^=\
439: des_SPtrans[0][(u>> 2L)&0x3f]^ \
440: des_SPtrans[2][(u>>10L)&0x3f]^ \
441: des_SPtrans[4][(u>>18L)&0x3f]^ \
442: des_SPtrans[6][(u>>26L)&0x3f]^ \
443: des_SPtrans[1][(t>> 2L)&0x3f]^ \
444: des_SPtrans[3][(t>>10L)&0x3f]^ \
445: des_SPtrans[5][(t>>18L)&0x3f]^ \
446: des_SPtrans[7][(t>>26L)&0x3f]; }
447: #endif
448: #endif
449:
450: /* IP and FP
451: * The problem is more of a geometric problem that random bit fiddling.
452: 0 1 2 3 4 5 6 7 62 54 46 38 30 22 14 6
453: 8 9 10 11 12 13 14 15 60 52 44 36 28 20 12 4
454: 16 17 18 19 20 21 22 23 58 50 42 34 26 18 10 2
455: 24 25 26 27 28 29 30 31 to 56 48 40 32 24 16 8 0
456:
457: 32 33 34 35 36 37 38 39 63 55 47 39 31 23 15 7
458: 40 41 42 43 44 45 46 47 61 53 45 37 29 21 13 5
459: 48 49 50 51 52 53 54 55 59 51 43 35 27 19 11 3
460: 56 57 58 59 60 61 62 63 57 49 41 33 25 17 9 1
461:
462: The output has been subject to swaps of the form
463: 0 1 -> 3 1 but the odd and even bits have been put into
464: 2 3 2 0
465: different words. The main trick is to remember that
466: t=((l>>size)^r)&(mask);
467: r^=t;
468: l^=(t<<size);
469: can be used to swap and move bits between words.
470:
471: So l = 0 1 2 3 r = 16 17 18 19
472: 4 5 6 7 20 21 22 23
473: 8 9 10 11 24 25 26 27
474: 12 13 14 15 28 29 30 31
475: becomes (for size == 2 and mask == 0x3333)
476: t = 2^16 3^17 -- -- l = 0 1 16 17 r = 2 3 18 19
477: 6^20 7^21 -- -- 4 5 20 21 6 7 22 23
478: 10^24 11^25 -- -- 8 9 24 25 10 11 24 25
479: 14^28 15^29 -- -- 12 13 28 29 14 15 28 29
480:
481: Thanks for hints from Richard Outerbridge - he told me IP&FP
482: could be done in 15 xor, 10 shifts and 5 ands.
483: When I finally started to think of the problem in 2D
484: I first got ~42 operations without xors. When I remembered
485: how to use xors :-) I got it to its final state.
486: */
487: #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
488: (b)^=(t),\
489: (a)^=((t)<<(n)))
490:
491: #define IP(l,r) \
492: { \
493: register DES_LONG tt; \
494: PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
495: PERM_OP(l,r,tt,16,0x0000ffffL); \
496: PERM_OP(r,l,tt, 2,0x33333333L); \
497: PERM_OP(l,r,tt, 8,0x00ff00ffL); \
498: PERM_OP(r,l,tt, 1,0x55555555L); \
499: }
500:
501: #define FP(l,r) \
502: { \
503: register DES_LONG tt; \
504: PERM_OP(l,r,tt, 1,0x55555555L); \
505: PERM_OP(r,l,tt, 8,0x00ff00ffL); \
506: PERM_OP(l,r,tt, 2,0x33333333L); \
507: PERM_OP(r,l,tt,16,0x0000ffffL); \
508: PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
509: }
510:
511: extern const DES_LONG des_SPtrans[8][64];
512:
513: #ifndef NOPROTO
514: void fcrypt_body(DES_LONG *out,des_key_schedule ks,
515: DES_LONG Eswap0, DES_LONG Eswap1);
516: #else
517: void fcrypt_body();
518: #endif
519:
520: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.