Statistics
| Branch: | Revision:

root / d3des.c @ d7e4b87e

History | View | Annotate | Download (15 kB)

1 6fd27407 ths
/*
2 6fd27407 ths
 * This is D3DES (V5.09) by Richard Outerbridge with the double and
3 6fd27407 ths
 * triple-length support removed for use in VNC.  Also the bytebit[] array
4 6fd27407 ths
 * has been reversed so that the most significant bit in each byte of the
5 6fd27407 ths
 * key is ignored, not the least significant.
6 6fd27407 ths
 *
7 6fd27407 ths
 * These changes are:
8 6fd27407 ths
 *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
9 6fd27407 ths
 *
10 6fd27407 ths
 * This software is distributed in the hope that it will be useful,
11 6fd27407 ths
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 6fd27407 ths
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 6fd27407 ths
 */
14 6fd27407 ths
15 6fd27407 ths
/* D3DES (V5.09) -
16 6fd27407 ths
 *
17 6fd27407 ths
 * A portable, public domain, version of the Data Encryption Standard.
18 6fd27407 ths
 *
19 6fd27407 ths
 * Written with Symantec's THINK (Lightspeed) C by Richard Outerbridge.
20 6fd27407 ths
 * Thanks to: Dan Hoey for his excellent Initial and Inverse permutation
21 6fd27407 ths
 * code;  Jim Gillogly & Phil Karn for the DES key schedule code; Dennis
22 6fd27407 ths
 * Ferguson, Eric Young and Dana How for comparing notes; and Ray Lau,
23 6fd27407 ths
 * for humouring me on.
24 6fd27407 ths
 *
25 6fd27407 ths
 * Copyright (c) 1988,1989,1990,1991,1992 by Richard Outerbridge.
26 6fd27407 ths
 * (GEnie : OUTER; CIS : [71755,204]) Graven Imagery, 1992.
27 6fd27407 ths
 */
28 6fd27407 ths
29 6fd27407 ths
#include "d3des.h"
30 6fd27407 ths
31 6fd27407 ths
static void scrunch(unsigned char *, unsigned long *);
32 6fd27407 ths
static void unscrun(unsigned long *, unsigned char *);
33 6fd27407 ths
static void desfunc(unsigned long *, unsigned long *);
34 6fd27407 ths
static void cookey(unsigned long *);
35 6fd27407 ths
36 6fd27407 ths
static unsigned long KnL[32] = { 0L };
37 6fd27407 ths
38 6fd27407 ths
static unsigned short bytebit[8]        = {
39 6fd27407 ths
        01, 02, 04, 010, 020, 040, 0100, 0200 };
40 6fd27407 ths
41 6fd27407 ths
static unsigned long bigbyte[24] = {
42 6fd27407 ths
        0x800000L,        0x400000L,        0x200000L,        0x100000L,
43 6fd27407 ths
        0x80000L,        0x40000L,        0x20000L,        0x10000L,
44 6fd27407 ths
        0x8000L,        0x4000L,        0x2000L,        0x1000L,
45 6fd27407 ths
        0x800L,         0x400L,         0x200L,         0x100L,
46 6fd27407 ths
        0x80L,                0x40L,                0x20L,                0x10L,
47 6fd27407 ths
        0x8L,                0x4L,                0x2L,                0x1L        };
48 6fd27407 ths
49 6fd27407 ths
/* Use the key schedule specified in the Standard (ANSI X3.92-1981). */
50 6fd27407 ths
51 6fd27407 ths
static unsigned char pc1[56] = {
52 6fd27407 ths
        56, 48, 40, 32, 24, 16,  8,         0, 57, 49, 41, 33, 25, 17,
53 6fd27407 ths
         9,  1, 58, 50, 42, 34, 26,        18, 10,  2, 59, 51, 43, 35,
54 6fd27407 ths
        62, 54, 46, 38, 30, 22, 14,         6, 61, 53, 45, 37, 29, 21,
55 6fd27407 ths
        13,  5, 60, 52, 44, 36, 28,        20, 12,  4, 27, 19, 11,  3 };
56 6fd27407 ths
57 6fd27407 ths
static unsigned char totrot[16] = {
58 6fd27407 ths
        1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 };
59 6fd27407 ths
60 6fd27407 ths
static unsigned char pc2[48] = {
61 6fd27407 ths
        13, 16, 10, 23,  0,  4,  2, 27, 14,  5, 20,  9,
62 6fd27407 ths
        22, 18, 11,  3, 25,  7, 15,  6, 26, 19, 12,  1,
63 6fd27407 ths
        40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
64 6fd27407 ths
        43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 };
65 6fd27407 ths
66 6fd27407 ths
void deskey(key, edf)        /* Thanks to James Gillogly & Phil Karn! */
67 6fd27407 ths
unsigned char *key;
68 6fd27407 ths
int edf;
69 6fd27407 ths
{
70 6fd27407 ths
        register int i, j, l, m, n;
71 6fd27407 ths
        unsigned char pc1m[56], pcr[56];
72 6fd27407 ths
        unsigned long kn[32];
73 6fd27407 ths
74 6fd27407 ths
        for ( j = 0; j < 56; j++ ) {
75 6fd27407 ths
                l = pc1[j];
76 6fd27407 ths
                m = l & 07;
77 6fd27407 ths
                pc1m[j] = (key[l >> 3] & bytebit[m]) ? 1 : 0;
78 6fd27407 ths
                }
79 6fd27407 ths
        for( i = 0; i < 16; i++ ) {
80 6fd27407 ths
                if( edf == DE1 ) m = (15 - i) << 1;
81 6fd27407 ths
                else m = i << 1;
82 6fd27407 ths
                n = m + 1;
83 6fd27407 ths
                kn[m] = kn[n] = 0L;
84 6fd27407 ths
                for( j = 0; j < 28; j++ ) {
85 6fd27407 ths
                        l = j + totrot[i];
86 6fd27407 ths
                        if( l < 28 ) pcr[j] = pc1m[l];
87 6fd27407 ths
                        else pcr[j] = pc1m[l - 28];
88 6fd27407 ths
                        }
89 6fd27407 ths
                for( j = 28; j < 56; j++ ) {
90 6fd27407 ths
                    l = j + totrot[i];
91 6fd27407 ths
                    if( l < 56 ) pcr[j] = pc1m[l];
92 6fd27407 ths
                    else pcr[j] = pc1m[l - 28];
93 6fd27407 ths
                    }
94 6fd27407 ths
                for( j = 0; j < 24; j++ ) {
95 6fd27407 ths
                        if( pcr[pc2[j]] ) kn[m] |= bigbyte[j];
96 6fd27407 ths
                        if( pcr[pc2[j+24]] ) kn[n] |= bigbyte[j];
97 6fd27407 ths
                        }
98 6fd27407 ths
                }
99 6fd27407 ths
        cookey(kn);
100 6fd27407 ths
        return;
101 6fd27407 ths
        }
102 6fd27407 ths
103 6fd27407 ths
static void cookey(raw1)
104 6fd27407 ths
register unsigned long *raw1;
105 6fd27407 ths
{
106 6fd27407 ths
        register unsigned long *cook, *raw0;
107 6fd27407 ths
        unsigned long dough[32];
108 6fd27407 ths
        register int i;
109 6fd27407 ths
110 6fd27407 ths
        cook = dough;
111 6fd27407 ths
        for( i = 0; i < 16; i++, raw1++ ) {
112 6fd27407 ths
                raw0 = raw1++;
113 6fd27407 ths
                *cook         = (*raw0 & 0x00fc0000L) << 6;
114 6fd27407 ths
                *cook        |= (*raw0 & 0x00000fc0L) << 10;
115 6fd27407 ths
                *cook        |= (*raw1 & 0x00fc0000L) >> 10;
116 6fd27407 ths
                *cook++ |= (*raw1 & 0x00000fc0L) >> 6;
117 6fd27407 ths
                *cook         = (*raw0 & 0x0003f000L) << 12;
118 6fd27407 ths
                *cook        |= (*raw0 & 0x0000003fL) << 16;
119 6fd27407 ths
                *cook        |= (*raw1 & 0x0003f000L) >> 4;
120 6fd27407 ths
                *cook++ |= (*raw1 & 0x0000003fL);
121 6fd27407 ths
                }
122 6fd27407 ths
        usekey(dough);
123 6fd27407 ths
        return;
124 6fd27407 ths
        }
125 6fd27407 ths
126 6fd27407 ths
void cpkey(into)
127 6fd27407 ths
register unsigned long *into;
128 6fd27407 ths
{
129 6fd27407 ths
        register unsigned long *from, *endp;
130 6fd27407 ths
131 6fd27407 ths
        from = KnL, endp = &KnL[32];
132 6fd27407 ths
        while( from < endp ) *into++ = *from++;
133 6fd27407 ths
        return;
134 6fd27407 ths
        }
135 6fd27407 ths
136 6fd27407 ths
void usekey(from)
137 6fd27407 ths
register unsigned long *from;
138 6fd27407 ths
{
139 6fd27407 ths
        register unsigned long *to, *endp;
140 6fd27407 ths
141 6fd27407 ths
        to = KnL, endp = &KnL[32];
142 6fd27407 ths
        while( to < endp ) *to++ = *from++;
143 6fd27407 ths
        return;
144 6fd27407 ths
        }
145 6fd27407 ths
146 6fd27407 ths
void des(inblock, outblock)
147 6fd27407 ths
unsigned char *inblock, *outblock;
148 6fd27407 ths
{
149 6fd27407 ths
        unsigned long work[2];
150 6fd27407 ths
151 6fd27407 ths
        scrunch(inblock, work);
152 6fd27407 ths
        desfunc(work, KnL);
153 6fd27407 ths
        unscrun(work, outblock);
154 6fd27407 ths
        return;
155 6fd27407 ths
        }
156 6fd27407 ths
157 6fd27407 ths
static void scrunch(outof, into)
158 6fd27407 ths
register unsigned char *outof;
159 6fd27407 ths
register unsigned long *into;
160 6fd27407 ths
{
161 6fd27407 ths
        *into         = (*outof++ & 0xffL) << 24;
162 6fd27407 ths
        *into        |= (*outof++ & 0xffL) << 16;
163 6fd27407 ths
        *into        |= (*outof++ & 0xffL) << 8;
164 6fd27407 ths
        *into++ |= (*outof++ & 0xffL);
165 6fd27407 ths
        *into         = (*outof++ & 0xffL) << 24;
166 6fd27407 ths
        *into        |= (*outof++ & 0xffL) << 16;
167 6fd27407 ths
        *into        |= (*outof++ & 0xffL) << 8;
168 6fd27407 ths
        *into        |= (*outof   & 0xffL);
169 6fd27407 ths
        return;
170 6fd27407 ths
        }
171 6fd27407 ths
172 6fd27407 ths
static void unscrun(outof, into)
173 6fd27407 ths
register unsigned long *outof;
174 6fd27407 ths
register unsigned char *into;
175 6fd27407 ths
{
176 6fd27407 ths
        *into++ = (unsigned char)((*outof >> 24) & 0xffL);
177 6fd27407 ths
        *into++ = (unsigned char)((*outof >> 16) & 0xffL);
178 6fd27407 ths
        *into++ = (unsigned char)((*outof >>  8) & 0xffL);
179 6fd27407 ths
        *into++ = (unsigned char)(*outof++         & 0xffL);
180 6fd27407 ths
        *into++ = (unsigned char)((*outof >> 24) & 0xffL);
181 6fd27407 ths
        *into++ = (unsigned char)((*outof >> 16) & 0xffL);
182 6fd27407 ths
        *into++ = (unsigned char)((*outof >>  8) & 0xffL);
183 6fd27407 ths
        *into        =  (unsigned char)(*outof         & 0xffL);
184 6fd27407 ths
        return;
185 6fd27407 ths
        }
186 6fd27407 ths
187 6fd27407 ths
static unsigned long SP1[64] = {
188 6fd27407 ths
        0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L,
189 6fd27407 ths
        0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L,
190 6fd27407 ths
        0x00000400L, 0x01010400L, 0x01010404L, 0x00000400L,
191 6fd27407 ths
        0x01000404L, 0x01010004L, 0x01000000L, 0x00000004L,
192 6fd27407 ths
        0x00000404L, 0x01000400L, 0x01000400L, 0x00010400L,
193 6fd27407 ths
        0x00010400L, 0x01010000L, 0x01010000L, 0x01000404L,
194 6fd27407 ths
        0x00010004L, 0x01000004L, 0x01000004L, 0x00010004L,
195 6fd27407 ths
        0x00000000L, 0x00000404L, 0x00010404L, 0x01000000L,
196 6fd27407 ths
        0x00010000L, 0x01010404L, 0x00000004L, 0x01010000L,
197 6fd27407 ths
        0x01010400L, 0x01000000L, 0x01000000L, 0x00000400L,
198 6fd27407 ths
        0x01010004L, 0x00010000L, 0x00010400L, 0x01000004L,
199 6fd27407 ths
        0x00000400L, 0x00000004L, 0x01000404L, 0x00010404L,
200 6fd27407 ths
        0x01010404L, 0x00010004L, 0x01010000L, 0x01000404L,
201 6fd27407 ths
        0x01000004L, 0x00000404L, 0x00010404L, 0x01010400L,
202 6fd27407 ths
        0x00000404L, 0x01000400L, 0x01000400L, 0x00000000L,
203 6fd27407 ths
        0x00010004L, 0x00010400L, 0x00000000L, 0x01010004L };
204 6fd27407 ths
205 6fd27407 ths
static unsigned long SP2[64] = {
206 6fd27407 ths
        0x80108020L, 0x80008000L, 0x00008000L, 0x00108020L,
207 6fd27407 ths
        0x00100000L, 0x00000020L, 0x80100020L, 0x80008020L,
208 6fd27407 ths
        0x80000020L, 0x80108020L, 0x80108000L, 0x80000000L,
209 6fd27407 ths
        0x80008000L, 0x00100000L, 0x00000020L, 0x80100020L,
210 6fd27407 ths
        0x00108000L, 0x00100020L, 0x80008020L, 0x00000000L,
211 6fd27407 ths
        0x80000000L, 0x00008000L, 0x00108020L, 0x80100000L,
212 6fd27407 ths
        0x00100020L, 0x80000020L, 0x00000000L, 0x00108000L,
213 6fd27407 ths
        0x00008020L, 0x80108000L, 0x80100000L, 0x00008020L,
214 6fd27407 ths
        0x00000000L, 0x00108020L, 0x80100020L, 0x00100000L,
215 6fd27407 ths
        0x80008020L, 0x80100000L, 0x80108000L, 0x00008000L,
216 6fd27407 ths
        0x80100000L, 0x80008000L, 0x00000020L, 0x80108020L,
217 6fd27407 ths
        0x00108020L, 0x00000020L, 0x00008000L, 0x80000000L,
218 6fd27407 ths
        0x00008020L, 0x80108000L, 0x00100000L, 0x80000020L,
219 6fd27407 ths
        0x00100020L, 0x80008020L, 0x80000020L, 0x00100020L,
220 6fd27407 ths
        0x00108000L, 0x00000000L, 0x80008000L, 0x00008020L,
221 6fd27407 ths
        0x80000000L, 0x80100020L, 0x80108020L, 0x00108000L };
222 6fd27407 ths
223 6fd27407 ths
static unsigned long SP3[64] = {
224 6fd27407 ths
        0x00000208L, 0x08020200L, 0x00000000L, 0x08020008L,
225 6fd27407 ths
        0x08000200L, 0x00000000L, 0x00020208L, 0x08000200L,
226 6fd27407 ths
        0x00020008L, 0x08000008L, 0x08000008L, 0x00020000L,
227 6fd27407 ths
        0x08020208L, 0x00020008L, 0x08020000L, 0x00000208L,
228 6fd27407 ths
        0x08000000L, 0x00000008L, 0x08020200L, 0x00000200L,
229 6fd27407 ths
        0x00020200L, 0x08020000L, 0x08020008L, 0x00020208L,
230 6fd27407 ths
        0x08000208L, 0x00020200L, 0x00020000L, 0x08000208L,
231 6fd27407 ths
        0x00000008L, 0x08020208L, 0x00000200L, 0x08000000L,
232 6fd27407 ths
        0x08020200L, 0x08000000L, 0x00020008L, 0x00000208L,
233 6fd27407 ths
        0x00020000L, 0x08020200L, 0x08000200L, 0x00000000L,
234 6fd27407 ths
        0x00000200L, 0x00020008L, 0x08020208L, 0x08000200L,
235 6fd27407 ths
        0x08000008L, 0x00000200L, 0x00000000L, 0x08020008L,
236 6fd27407 ths
        0x08000208L, 0x00020000L, 0x08000000L, 0x08020208L,
237 6fd27407 ths
        0x00000008L, 0x00020208L, 0x00020200L, 0x08000008L,
238 6fd27407 ths
        0x08020000L, 0x08000208L, 0x00000208L, 0x08020000L,
239 6fd27407 ths
        0x00020208L, 0x00000008L, 0x08020008L, 0x00020200L };
240 6fd27407 ths
241 6fd27407 ths
static unsigned long SP4[64] = {
242 6fd27407 ths
        0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
243 6fd27407 ths
        0x00802080L, 0x00800081L, 0x00800001L, 0x00002001L,
244 6fd27407 ths
        0x00000000L, 0x00802000L, 0x00802000L, 0x00802081L,
245 6fd27407 ths
        0x00000081L, 0x00000000L, 0x00800080L, 0x00800001L,
246 6fd27407 ths
        0x00000001L, 0x00002000L, 0x00800000L, 0x00802001L,
247 6fd27407 ths
        0x00000080L, 0x00800000L, 0x00002001L, 0x00002080L,
248 6fd27407 ths
        0x00800081L, 0x00000001L, 0x00002080L, 0x00800080L,
249 6fd27407 ths
        0x00002000L, 0x00802080L, 0x00802081L, 0x00000081L,
250 6fd27407 ths
        0x00800080L, 0x00800001L, 0x00802000L, 0x00802081L,
251 6fd27407 ths
        0x00000081L, 0x00000000L, 0x00000000L, 0x00802000L,
252 6fd27407 ths
        0x00002080L, 0x00800080L, 0x00800081L, 0x00000001L,
253 6fd27407 ths
        0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
254 6fd27407 ths
        0x00802081L, 0x00000081L, 0x00000001L, 0x00002000L,
255 6fd27407 ths
        0x00800001L, 0x00002001L, 0x00802080L, 0x00800081L,
256 6fd27407 ths
        0x00002001L, 0x00002080L, 0x00800000L, 0x00802001L,
257 6fd27407 ths
        0x00000080L, 0x00800000L, 0x00002000L, 0x00802080L };
258 6fd27407 ths
259 6fd27407 ths
static unsigned long SP5[64] = {
260 6fd27407 ths
        0x00000100L, 0x02080100L, 0x02080000L, 0x42000100L,
261 6fd27407 ths
        0x00080000L, 0x00000100L, 0x40000000L, 0x02080000L,
262 6fd27407 ths
        0x40080100L, 0x00080000L, 0x02000100L, 0x40080100L,
263 6fd27407 ths
        0x42000100L, 0x42080000L, 0x00080100L, 0x40000000L,
264 6fd27407 ths
        0x02000000L, 0x40080000L, 0x40080000L, 0x00000000L,
265 6fd27407 ths
        0x40000100L, 0x42080100L, 0x42080100L, 0x02000100L,
266 6fd27407 ths
        0x42080000L, 0x40000100L, 0x00000000L, 0x42000000L,
267 6fd27407 ths
        0x02080100L, 0x02000000L, 0x42000000L, 0x00080100L,
268 6fd27407 ths
        0x00080000L, 0x42000100L, 0x00000100L, 0x02000000L,
269 6fd27407 ths
        0x40000000L, 0x02080000L, 0x42000100L, 0x40080100L,
270 6fd27407 ths
        0x02000100L, 0x40000000L, 0x42080000L, 0x02080100L,
271 6fd27407 ths
        0x40080100L, 0x00000100L, 0x02000000L, 0x42080000L,
272 6fd27407 ths
        0x42080100L, 0x00080100L, 0x42000000L, 0x42080100L,
273 6fd27407 ths
        0x02080000L, 0x00000000L, 0x40080000L, 0x42000000L,
274 6fd27407 ths
        0x00080100L, 0x02000100L, 0x40000100L, 0x00080000L,
275 6fd27407 ths
        0x00000000L, 0x40080000L, 0x02080100L, 0x40000100L };
276 6fd27407 ths
277 6fd27407 ths
static unsigned long SP6[64] = {
278 6fd27407 ths
        0x20000010L, 0x20400000L, 0x00004000L, 0x20404010L,
279 6fd27407 ths
        0x20400000L, 0x00000010L, 0x20404010L, 0x00400000L,
280 6fd27407 ths
        0x20004000L, 0x00404010L, 0x00400000L, 0x20000010L,
281 6fd27407 ths
        0x00400010L, 0x20004000L, 0x20000000L, 0x00004010L,
282 6fd27407 ths
        0x00000000L, 0x00400010L, 0x20004010L, 0x00004000L,
283 6fd27407 ths
        0x00404000L, 0x20004010L, 0x00000010L, 0x20400010L,
284 6fd27407 ths
        0x20400010L, 0x00000000L, 0x00404010L, 0x20404000L,
285 6fd27407 ths
        0x00004010L, 0x00404000L, 0x20404000L, 0x20000000L,
286 6fd27407 ths
        0x20004000L, 0x00000010L, 0x20400010L, 0x00404000L,
287 6fd27407 ths
        0x20404010L, 0x00400000L, 0x00004010L, 0x20000010L,
288 6fd27407 ths
        0x00400000L, 0x20004000L, 0x20000000L, 0x00004010L,
289 6fd27407 ths
        0x20000010L, 0x20404010L, 0x00404000L, 0x20400000L,
290 6fd27407 ths
        0x00404010L, 0x20404000L, 0x00000000L, 0x20400010L,
291 6fd27407 ths
        0x00000010L, 0x00004000L, 0x20400000L, 0x00404010L,
292 6fd27407 ths
        0x00004000L, 0x00400010L, 0x20004010L, 0x00000000L,
293 6fd27407 ths
        0x20404000L, 0x20000000L, 0x00400010L, 0x20004010L };
294 6fd27407 ths
295 6fd27407 ths
static unsigned long SP7[64] = {
296 6fd27407 ths
        0x00200000L, 0x04200002L, 0x04000802L, 0x00000000L,
297 6fd27407 ths
        0x00000800L, 0x04000802L, 0x00200802L, 0x04200800L,
298 6fd27407 ths
        0x04200802L, 0x00200000L, 0x00000000L, 0x04000002L,
299 6fd27407 ths
        0x00000002L, 0x04000000L, 0x04200002L, 0x00000802L,
300 6fd27407 ths
        0x04000800L, 0x00200802L, 0x00200002L, 0x04000800L,
301 6fd27407 ths
        0x04000002L, 0x04200000L, 0x04200800L, 0x00200002L,
302 6fd27407 ths
        0x04200000L, 0x00000800L, 0x00000802L, 0x04200802L,
303 6fd27407 ths
        0x00200800L, 0x00000002L, 0x04000000L, 0x00200800L,
304 6fd27407 ths
        0x04000000L, 0x00200800L, 0x00200000L, 0x04000802L,
305 6fd27407 ths
        0x04000802L, 0x04200002L, 0x04200002L, 0x00000002L,
306 6fd27407 ths
        0x00200002L, 0x04000000L, 0x04000800L, 0x00200000L,
307 6fd27407 ths
        0x04200800L, 0x00000802L, 0x00200802L, 0x04200800L,
308 6fd27407 ths
        0x00000802L, 0x04000002L, 0x04200802L, 0x04200000L,
309 6fd27407 ths
        0x00200800L, 0x00000000L, 0x00000002L, 0x04200802L,
310 6fd27407 ths
        0x00000000L, 0x00200802L, 0x04200000L, 0x00000800L,
311 6fd27407 ths
        0x04000002L, 0x04000800L, 0x00000800L, 0x00200002L };
312 6fd27407 ths
313 6fd27407 ths
static unsigned long SP8[64] = {
314 6fd27407 ths
        0x10001040L, 0x00001000L, 0x00040000L, 0x10041040L,
315 6fd27407 ths
        0x10000000L, 0x10001040L, 0x00000040L, 0x10000000L,
316 6fd27407 ths
        0x00040040L, 0x10040000L, 0x10041040L, 0x00041000L,
317 6fd27407 ths
        0x10041000L, 0x00041040L, 0x00001000L, 0x00000040L,
318 6fd27407 ths
        0x10040000L, 0x10000040L, 0x10001000L, 0x00001040L,
319 6fd27407 ths
        0x00041000L, 0x00040040L, 0x10040040L, 0x10041000L,
320 6fd27407 ths
        0x00001040L, 0x00000000L, 0x00000000L, 0x10040040L,
321 6fd27407 ths
        0x10000040L, 0x10001000L, 0x00041040L, 0x00040000L,
322 6fd27407 ths
        0x00041040L, 0x00040000L, 0x10041000L, 0x00001000L,
323 6fd27407 ths
        0x00000040L, 0x10040040L, 0x00001000L, 0x00041040L,
324 6fd27407 ths
        0x10001000L, 0x00000040L, 0x10000040L, 0x10040000L,
325 6fd27407 ths
        0x10040040L, 0x10000000L, 0x00040000L, 0x10001040L,
326 6fd27407 ths
        0x00000000L, 0x10041040L, 0x00040040L, 0x10000040L,
327 6fd27407 ths
        0x10040000L, 0x10001000L, 0x10001040L, 0x00000000L,
328 6fd27407 ths
        0x10041040L, 0x00041000L, 0x00041000L, 0x00001040L,
329 6fd27407 ths
        0x00001040L, 0x00040040L, 0x10000000L, 0x10041000L };
330 6fd27407 ths
331 6fd27407 ths
static void desfunc(block, keys)
332 6fd27407 ths
register unsigned long *block, *keys;
333 6fd27407 ths
{
334 6fd27407 ths
        register unsigned long fval, work, right, leftt;
335 6fd27407 ths
        register int round;
336 6fd27407 ths
337 6fd27407 ths
        leftt = block[0];
338 6fd27407 ths
        right = block[1];
339 6fd27407 ths
        work = ((leftt >> 4) ^ right) & 0x0f0f0f0fL;
340 6fd27407 ths
        right ^= work;
341 6fd27407 ths
        leftt ^= (work << 4);
342 6fd27407 ths
        work = ((leftt >> 16) ^ right) & 0x0000ffffL;
343 6fd27407 ths
        right ^= work;
344 6fd27407 ths
        leftt ^= (work << 16);
345 6fd27407 ths
        work = ((right >> 2) ^ leftt) & 0x33333333L;
346 6fd27407 ths
        leftt ^= work;
347 6fd27407 ths
        right ^= (work << 2);
348 6fd27407 ths
        work = ((right >> 8) ^ leftt) & 0x00ff00ffL;
349 6fd27407 ths
        leftt ^= work;
350 6fd27407 ths
        right ^= (work << 8);
351 6fd27407 ths
        right = ((right << 1) | ((right >> 31) & 1L)) & 0xffffffffL;
352 6fd27407 ths
        work = (leftt ^ right) & 0xaaaaaaaaL;
353 6fd27407 ths
        leftt ^= work;
354 6fd27407 ths
        right ^= work;
355 6fd27407 ths
        leftt = ((leftt << 1) | ((leftt >> 31) & 1L)) & 0xffffffffL;
356 6fd27407 ths
357 6fd27407 ths
        for( round = 0; round < 8; round++ ) {
358 6fd27407 ths
                work  = (right << 28) | (right >> 4);
359 6fd27407 ths
                work ^= *keys++;
360 6fd27407 ths
                fval  = SP7[ work                 & 0x3fL];
361 6fd27407 ths
                fval |= SP5[(work >>  8) & 0x3fL];
362 6fd27407 ths
                fval |= SP3[(work >> 16) & 0x3fL];
363 6fd27407 ths
                fval |= SP1[(work >> 24) & 0x3fL];
364 6fd27407 ths
                work  = right ^ *keys++;
365 6fd27407 ths
                fval |= SP8[ work                 & 0x3fL];
366 6fd27407 ths
                fval |= SP6[(work >>  8) & 0x3fL];
367 6fd27407 ths
                fval |= SP4[(work >> 16) & 0x3fL];
368 6fd27407 ths
                fval |= SP2[(work >> 24) & 0x3fL];
369 6fd27407 ths
                leftt ^= fval;
370 6fd27407 ths
                work  = (leftt << 28) | (leftt >> 4);
371 6fd27407 ths
                work ^= *keys++;
372 6fd27407 ths
                fval  = SP7[ work                 & 0x3fL];
373 6fd27407 ths
                fval |= SP5[(work >>  8) & 0x3fL];
374 6fd27407 ths
                fval |= SP3[(work >> 16) & 0x3fL];
375 6fd27407 ths
                fval |= SP1[(work >> 24) & 0x3fL];
376 6fd27407 ths
                work  = leftt ^ *keys++;
377 6fd27407 ths
                fval |= SP8[ work                 & 0x3fL];
378 6fd27407 ths
                fval |= SP6[(work >>  8) & 0x3fL];
379 6fd27407 ths
                fval |= SP4[(work >> 16) & 0x3fL];
380 6fd27407 ths
                fval |= SP2[(work >> 24) & 0x3fL];
381 6fd27407 ths
                right ^= fval;
382 6fd27407 ths
                }
383 6fd27407 ths
384 6fd27407 ths
        right = (right << 31) | (right >> 1);
385 6fd27407 ths
        work = (leftt ^ right) & 0xaaaaaaaaL;
386 6fd27407 ths
        leftt ^= work;
387 6fd27407 ths
        right ^= work;
388 6fd27407 ths
        leftt = (leftt << 31) | (leftt >> 1);
389 6fd27407 ths
        work = ((leftt >> 8) ^ right) & 0x00ff00ffL;
390 6fd27407 ths
        right ^= work;
391 6fd27407 ths
        leftt ^= (work << 8);
392 6fd27407 ths
        work = ((leftt >> 2) ^ right) & 0x33333333L;
393 6fd27407 ths
        right ^= work;
394 6fd27407 ths
        leftt ^= (work << 2);
395 6fd27407 ths
        work = ((right >> 16) ^ leftt) & 0x0000ffffL;
396 6fd27407 ths
        leftt ^= work;
397 6fd27407 ths
        right ^= (work << 16);
398 6fd27407 ths
        work = ((right >> 4) ^ leftt) & 0x0f0f0f0fL;
399 6fd27407 ths
        leftt ^= work;
400 6fd27407 ths
        right ^= (work << 4);
401 6fd27407 ths
        *block++ = right;
402 6fd27407 ths
        *block = leftt;
403 6fd27407 ths
        return;
404 6fd27407 ths
        }
405 6fd27407 ths
406 6fd27407 ths
/* Validation sets:
407 6fd27407 ths
 *
408 6fd27407 ths
 * Single-length key, single-length plaintext -
409 6fd27407 ths
 * Key          : 0123 4567 89ab cdef
410 6fd27407 ths
 * Plain  : 0123 4567 89ab cde7
411 6fd27407 ths
 * Cipher : c957 4425 6a5e d31d
412 6fd27407 ths
 *
413 6fd27407 ths
 * Double-length key, single-length plaintext -
414 6fd27407 ths
 * Key          : 0123 4567 89ab cdef fedc ba98 7654 3210
415 6fd27407 ths
 * Plain  : 0123 4567 89ab cde7
416 6fd27407 ths
 * Cipher : 7f1d 0a77 826b 8aff
417 6fd27407 ths
 *
418 6fd27407 ths
 * Double-length key, double-length plaintext -
419 6fd27407 ths
 * Key          : 0123 4567 89ab cdef fedc ba98 7654 3210
420 6fd27407 ths
 * Plain  : 0123 4567 89ab cdef 0123 4567 89ab cdff
421 6fd27407 ths
 * Cipher : 27a0 8440 406a df60 278f 47cf 42d6 15d7
422 6fd27407 ths
 *
423 6fd27407 ths
 * Triple-length key, single-length plaintext -
424 6fd27407 ths
 * Key          : 0123 4567 89ab cdef fedc ba98 7654 3210 89ab cdef 0123 4567
425 6fd27407 ths
 * Plain  : 0123 4567 89ab cde7
426 6fd27407 ths
 * Cipher : de0b 7c06 ae5e 0ed5
427 6fd27407 ths
 *
428 6fd27407 ths
 * Triple-length key, double-length plaintext -
429 6fd27407 ths
 * Key          : 0123 4567 89ab cdef fedc ba98 7654 3210 89ab cdef 0123 4567
430 6fd27407 ths
 * Plain  : 0123 4567 89ab cdef 0123 4567 89ab cdff
431 6fd27407 ths
 * Cipher : ad0d 1b30 ac17 cf07 0ed1 1c63 81e4 4de5
432 6fd27407 ths
 *
433 6fd27407 ths
 * d3des V5.0a rwo 9208.07 18:44 Graven Imagery
434 6fd27407 ths
 **********************************************************************/