|
|
1.1 ! root 1: /* ! 2: * Crypt subroutines - which use the ! 3: * DES (Data Encryption Standard) ! 4: * as published in National Bureau of Standards, ! 5: * "Encryption Algorithm for Computer Data Encryption", ! 6: * Vol. 40, No. 52, March 17, p.. 12134-12139. ! 7: * (Reprinted in Cryptologia, Vol. I, No. 3, July 1977). ! 8: */ ! 9: ! 10: #include <stdio.h> ! 11: #include <string.h> ! 12: ! 13: #define NBPC 8 /* Bits in a char */ ! 14: #define NIB 64 /* Input bits in a unit */ ! 15: #define NKB 64 /* Number of key bits */ ! 16: #define NUKB 56 /* Number of used key bits */ ! 17: #define NOKB 48 /* Number out from each key iteration */ ! 18: #define NITER 16 /* Number of iterations of S-functions */ ! 19: #define NSBOX 8 /* Number of S-boxes */ ! 20: #define NSCSET 64 /* Number in small character set */ ! 21: ! 22: /* ! 23: * Table of Initial permutation of each 64 bit entity. ! 24: */ ! 25: static char IP[NIB] = { ! 26: 57, 49, 41, 33, 25, 17, 9, 1, ! 27: 59, 51, 43, 35, 27, 19, 11, 3, ! 28: 61, 53, 45, 37, 29, 21, 13, 5, ! 29: 63, 55, 47, 39, 31, 23, 15, 7, ! 30: 56, 48, 40, 32, 24, 16, 8, 0, ! 31: 58, 50, 42, 34, 26, 18, 10, 2, ! 32: 60, 52, 44, 36, 28, 20, 12, 4, ! 33: 62, 54, 46, 38, 30, 22, 14, 6 ! 34: }; ! 35: ! 36: /* ! 37: * Inverted intial permutation ( IP -1) ! 38: */ ! 39: static char IP1[NIB] = { ! 40: 39, 7, 47, 15, 55, 23, 63, 31, ! 41: 38, 6, 46, 14, 54, 22, 62, 30, ! 42: 37, 5, 45, 13, 53, 21, 61, 29, ! 43: 36, 4, 44, 12, 52, 20, 60, 28, ! 44: 35, 3, 43, 11, 51, 19, 59, 27, ! 45: 34, 2, 42, 10, 50, 18, 58, 26, ! 46: 33, 1, 41, 9, 49, 17, 57, 25, ! 47: 32, 0, 40, 8, 48, 16, 56, 24 ! 48: }; ! 49: ! 50: /* ! 51: * E bit-selection table ! 52: */ ! 53: static char E[NOKB] = { ! 54: 31, 0, 1, 2, 3, 4, ! 55: 3, 4, 5, 6, 7, 8, ! 56: 7, 8, 9, 10, 11, 12, ! 57: 11, 12, 13, 14, 15, 16, ! 58: 15, 16, 17, 18, 19, 20, ! 59: 19, 20, 21, 22, 23, 24, ! 60: 23, 24, 25, 26, 27, 28, ! 61: 27, 28, 29, 30, 31, 0 ! 62: }; ! 63: ! 64: /* ! 65: * A saved copy of the E-table for ! 66: * crypt to perturb. ! 67: */ ! 68: static char saveE[NOKB] = { ! 69: 31, 0, 1, 2, 3, 4, ! 70: 3, 4, 5, 6, 7, 8, ! 71: 7, 8, 9, 10, 11, 12, ! 72: 11, 12, 13, 14, 15, 16, ! 73: 15, 16, 17, 18, 19, 20, ! 74: 19, 20, 21, 22, 23, 24, ! 75: 23, 24, 25, 26, 27, 28, ! 76: 27, 28, 29, 30, 31, 0 ! 77: }; ! 78: ! 79: /* ! 80: * Permutation of 32-bits onto 32 bits ! 81: * known as "P" ! 82: */ ! 83: static char P[NIB/2] = { ! 84: 15, 6, 19, 20, ! 85: 28, 11, 27, 16, ! 86: 0, 14, 22, 25, ! 87: 4, 17, 30, 9, ! 88: 1, 7, 23, 13, ! 89: 31, 26, 2, 8, ! 90: 18, 12, 29, 5, ! 91: 21, 10, 3, 24 ! 92: }; ! 93: ! 94: /* ! 95: * The following are the 8 S-box functions ! 96: * (S1, S2, S3, S4, ..., S8) ! 97: * Each turns a 6-bit quantity into a four bit number. ! 98: */ ! 99: static char Sboxes[NSBOX][4][16] = { ! 100: /* S1 */ ! 101: 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, ! 102: 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, ! 103: 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, ! 104: 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13, ! 105: /* S2 */ ! 106: 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, ! 107: 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, ! 108: 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, ! 109: 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9, ! 110: /* S3 */ ! 111: 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, ! 112: 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, ! 113: 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, ! 114: 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12, ! 115: /* S4 */ ! 116: 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, ! 117: 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, ! 118: 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, ! 119: 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14, ! 120: /* S5 */ ! 121: 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, ! 122: 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, ! 123: 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, ! 124: 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3, ! 125: /* S6 */ ! 126: 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, ! 127: 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, ! 128: 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, ! 129: 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13, ! 130: /* S7 */ ! 131: 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, ! 132: 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, ! 133: 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, ! 134: 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12, ! 135: /* S8 */ ! 136: 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, ! 137: 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, ! 138: 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, ! 139: 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 ! 140: }; ! 141: ! 142: /* ! 143: * Permuted choice tables ! 144: * PC-1 is used for first iteration of KS ! 145: * and PC-2 is used therafter. ! 146: */ ! 147: static char PC1[NUKB] = { ! 148: 56, 48, 40, 32, 24, 16, 8, ! 149: 0, 57, 49, 41, 33, 25, 17, ! 150: 9, 1, 58, 50, 42, 34, 26, ! 151: 18, 10, 2, 59, 51, 43, 35, ! 152: /* Division between Co and Do */ ! 153: 62, 54, 46, 38, 30, 22, 14, ! 154: 6, 61, 53, 45, 37, 29, 21, ! 155: 13, 5, 60, 52, 44, 36, 28, ! 156: 20, 12, 4, 27, 19, 11, 3 ! 157: }; ! 158: ! 159: static char PC2[NOKB] = { ! 160: 13, 16, 10, 23, 0, 4, ! 161: 2, 27, 14, 5, 20, 9, ! 162: 22, 18, 11, 3, 25, 7, ! 163: 15, 6, 26, 19, 12, 1, ! 164: 40, 51, 30, 36, 46, 54, ! 165: 29, 39, 50, 44, 32, 47, ! 166: 43, 48, 38, 55, 33, 52, ! 167: 45, 41, 49, 35, 28, 31 ! 168: }; ! 169: ! 170: /* ! 171: * Left shift table ! 172: */ ! 173: static char shifts[NITER] = { ! 174: 1, 1, 2, 2, 2, 2, 2, 2, ! 175: 1, 2, 2, 2, 2, 2, 2, 1 ! 176: }; ! 177: ! 178: /* ! 179: * Table to map a 6-bit integer onto the ! 180: * smaller than ascii character set ! 181: * ([a-zA-Z0-9./]). ! 182: */ ! 183: static char maptab[NSCSET] = { ! 184: 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', ! 185: 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', ! 186: 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', ! 187: 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', ! 188: 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', ! 189: 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', ! 190: 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', ! 191: '4', '5', '6', '7', '8', '9', '.', '/' ! 192: }; ! 193: ! 194: static char Ki[NITER][NOKB]; ! 195: ! 196: /* ! 197: * Encrypt the block (one bit per byte) ! 198: * using the (broken) DES algorithm. ! 199: * The `edflag' is zero for encrypt, ! 200: * non-zero for decrypt. ! 201: */ ! 202: encrypt(block, edflag) ! 203: char block[NIB]; ! 204: { ! 205: char block1[NIB], block2[NIB]; ! 206: ! 207: permute(block, block1, IP, NIB); ! 208: cypher(block1, block2, edflag); ! 209: permute(block2, block, IP1, NIB); ! 210: } ! 211: ! 212: /* ! 213: * Setkey takes an array of NIB (64) ! 214: * bytes called `key', each of which ! 215: * contains one bit of the desired key. ! 216: * This uses the DES Key Schedule (KS) function ! 217: * to iteratively produce each of the NITER (16) ! 218: * Ki's from the original key. ! 219: */ ! 220: setkey(key) ! 221: char key[NKB]; ! 222: { ! 223: char cd[NUKB]; ! 224: register ni; ! 225: ! 226: permute(key, cd, PC1, NUKB); ! 227: for (ni=0; ni<NITER; ni++) { ! 228: lrot(cd, shifts[ni], NUKB/2); ! 229: lrot(&cd[NUKB/2], shifts[ni], NUKB/2); ! 230: permute(cd, &Ki[ni][0], PC2, NOKB); ! 231: } ! 232: } ! 233: ! 234: /* ! 235: * Crypt - the password encryption routine. ! 236: * The key here, is the user's typed password ! 237: * and the second is a 2-character salt in ! 238: * the set "[a-zA-Z0-9./]". The salt ! 239: * changes the E-table in one of 4096 ways ! 240: * to frustrate the use of hardware to crack ! 241: * the DES code. ! 242: */ ! 243: char * ! 244: crypt(key, salt) ! 245: char *key, *salt; ! 246: { ! 247: int sixbit; ! 248: char stuff[NIB]; ! 249: char bkey[NKB]; ! 250: static char passwd[16]; ! 251: register char *cp, *bp; ! 252: register i; ! 253: ! 254: perturb(salt); ! 255: cp = key; ! 256: bp = bkey; ! 257: while (*cp!='\0' && bp<&bkey[NKB/NBPC]) ! 258: *bp++ = *cp++; ! 259: while (bp < &bkey[NKB/NBPC]) ! 260: *bp++ = '\0'; ! 261: unpack(bkey, NKB); ! 262: setkey(bkey); ! 263: memcpy(stuff, "Password", NIB/NBPC); ! 264: unpack(stuff, NIB); ! 265: encrypt(stuff, 0); ! 266: cp = passwd; ! 267: *cp++ = salt[0]; ! 268: *cp++ = salt[1]; ! 269: bp = stuff; ! 270: i = sixbit = 0; ! 271: while (i < NIB) { ! 272: sixbit = (sixbit<<1) + *bp++; ! 273: if ((i++%6) == 6-1) { ! 274: *cp++ = maptab[sixbit]; ! 275: sixbit = 0; ! 276: } ! 277: } ! 278: *cp++ = maptab[sixbit]; ! 279: *cp = '\0'; ! 280: return (passwd); ! 281: } ! 282: ! 283: /* ! 284: * Perturb the E-table based on 2 6-bit characters ! 285: * in the salt. It produces a random permutation for ! 286: * the E-table. ! 287: */ ! 288: static ! 289: perturb(salt) ! 290: char salt[2]; ! 291: { ! 292: register char *cp; ! 293: register i1, i2; ! 294: int salti; ! 295: ! 296: salti = 01<<8 | 01; ! 297: i1 = salt[0]; ! 298: i2 = salt[1]; ! 299: for (cp = maptab; cp < &maptab[NSCSET]; cp++) { ! 300: if (i1 == *cp) ! 301: salti |= (cp-maptab)<<1; ! 302: if (i2 == *cp) ! 303: salti |= (cp-maptab)<<9; ! 304: } ! 305: srand(salti); ! 306: /* ! 307: * Make new E-table, from the saved E-table ! 308: * with random junk added to it (mod NOKB). ! 309: */ ! 310: for (i1 = 0; i1 < NOKB; i1++) ! 311: E[i1] = (saveE[i1] + (rand()>>6)) % (NIB/2); ! 312: } ! 313: ! 314: /* ! 315: * Permute incoming bits into outgoing ! 316: * chunk with permutation table (`ptab'). ! 317: * `nbites' is the number of bits to ! 318: * produce from the transformation. ! 319: */ ! 320: static ! 321: permute(ibits, obits, ptab, nbits) ! 322: register char *ibits; ! 323: register char *obits; ! 324: char *ptab; ! 325: { ! 326: register bitno; ! 327: ! 328: for (bitno = 0; bitno<nbits; bitno++) ! 329: *obits++ = ibits[ptab[bitno]]; ! 330: } ! 331: ! 332: /* ! 333: * The intermediate cypher function ! 334: * It does a chunk of NIB (64) bits ! 335: * through the NITER (16) iterations ! 336: * of the cypher function `f'. ! 337: */ ! 338: static ! 339: cypher(ibits, obits, edflag) ! 340: int edflag; ! 341: char ibits[NIB]; ! 342: char obits[NIB]; ! 343: { ! 344: char t1bits[NOKB]; ! 345: char t2bits[NIB/2]; ! 346: char left[NIB/2], right[NIB/2]; ! 347: register int n; ! 348: register int i; ! 349: ! 350: memcpy(left, ibits, NIB/2); ! 351: memcpy(right, &ibits[NIB/2], NIB/2); ! 352: for (n=0; n<NITER; n++) { ! 353: /* ! 354: * L' = R ! 355: * R' = L + f(R,K) ! 356: */ ! 357: permute(right, t1bits, E, NOKB); ! 358: i = edflag ? NITER-1-n : n; ! 359: m2add(t1bits, &Ki[i][0], t1bits, NOKB); ! 360: dosboxes(t1bits, t2bits); ! 361: permute(t2bits, t1bits, P, NIB/2); ! 362: m2add(t1bits, left, t2bits, NIB/2); ! 363: memcpy(left, right, NIB/2); ! 364: memcpy(right, t2bits, NIB/2); ! 365: } ! 366: memcpy(obits, right, NIB/2); ! 367: memcpy(&obits[NIB/2], left, NIB/2); ! 368: } ! 369: ! 370: /* ! 371: * Rotate (what DES calls shift) left ! 372: * `nbits' `bits' by `ns' places. ! 373: * The shift count is assumed less than ! 374: * NBPC (8). ! 375: */ ! 376: static ! 377: lrot(bits, ns, nbits) ! 378: char *bits; ! 379: { ! 380: register i, nb; ! 381: register char *bp; ! 382: int byte; ! 383: ! 384: for (i=0; i<ns; i++) { ! 385: bp = bits; ! 386: byte = *bp; ! 387: nb = nbits-1; ! 388: do { ! 389: bp[0] = bp[1]; ! 390: bp++; ! 391: } while (--nb); ! 392: *bp = byte; ! 393: } ! 394: } ! 395: ! 396: /* ! 397: * Modulo 2 addition of bit vectors ! 398: * `i1' and `i2' into `o'. ! 399: * `nbits' is the number of bits in ! 400: * each vector ! 401: */ ! 402: static ! 403: m2add(i1, i2, o, nbits) ! 404: register char *i1, *i2; ! 405: register nbits; ! 406: register char *o; ! 407: { ! 408: do { ! 409: *o++ = *i1++ ^ *i2++; ! 410: } while (--nbits); ! 411: } ! 412: ! 413: /* ! 414: * Implement the "S-boxes" ! 415: * Transforming from ibits (NOKB or 48 bits) ! 416: * to obits (NIB/2 or 32 bits). ! 417: * For each of these boxes, there is a ! 418: * 6-bit chunk of the input with the row being ! 419: * the first and last of these and the column ! 420: * being the middle 4 bits. ! 421: */ ! 422: static ! 423: dosboxes(ibits, obits) ! 424: char ibits[NOKB]; ! 425: char obits[NIB/2]; ! 426: { ! 427: register row, col; ! 428: register boxno; ! 429: int tempout; ! 430: ! 431: tempout = 0; ! 432: for (boxno=0; boxno < NSBOX; boxno++) { ! 433: row = col = 0; ! 434: row = row<<1 | *ibits++; ! 435: col = col<<1 | *ibits++; ! 436: col = col<<1 | *ibits++; ! 437: col = col<<1 | *ibits++; ! 438: col = col<<1 | *ibits++; ! 439: row = row<<1 | *ibits++; ! 440: tempout = (tempout<<4) | Sboxes[boxno][row][col]; ! 441: for (col=0; col<4; col++) { ! 442: *obits++ = (tempout>>3) & 01; ! 443: tempout <<= 1; ! 444: } ! 445: } ! 446: } ! 447: ! 448: /* ! 449: * Unpack byte vector into bit vector ! 450: */ ! 451: static ! 452: unpack(bytes, nbits) ! 453: char *bytes; ! 454: unsigned nbits; ! 455: { ! 456: register char *bitp; ! 457: register char *bp; ! 458: register i; ! 459: int byte; ! 460: ! 461: bitp = &bytes[nbits]; ! 462: bp = &bytes[(nbits+NBPC-1)/NBPC]; ! 463: for (i=nbits; i>0; i--) { ! 464: if ((i % NBPC) == 0) ! 465: byte = *--bp; ! 466: *--bitp = byte&01; ! 467: byte >>= 1; ! 468: } ! 469: } ! 470: ! 471: /* ! 472: * Pack bit vector into byte data. ! 473: */ ! 474: static ! 475: pack(bp, nbits) ! 476: register char *bp; ! 477: unsigned nbits; ! 478: { ! 479: register i; ! 480: register char *bitp; ! 481: int byte; ! 482: ! 483: byte = 0; ! 484: bitp = bp; ! 485: for (i=0; i<nbits; i++) { ! 486: byte = byte<<1 | *bitp++; ! 487: if ((i%NBPC) == NBPC-1) { ! 488: *bp++ = byte; ! 489: byte = 0; ! 490: } ! 491: } ! 492: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.