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