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