Source Listing 9-SEP-1997 11:09:50 DEC C V5.0-003 Page 1 30-SEP-1996 09:27:36 X$SRC:[CSO.PH]CRYPT.C;1 1 /*#if defined(LIBC_SCCS) && !defined(lint)*/ 2 #ifdef LIBC_SCCS X 3 #ifndef lint X 4 static char sccsid[] = "@(#)crypt.c 5.2 (Berkeley) 3/9/86"; X 5 #endif X 6 #endif LIBC_SCCS and not lint 7 8 /* 9 * This program implements the 10 * Proposed Federal Information Processing 11 * Data Encryption Standard. 12 * See Federal Register, March 17, 1975 (40FR12134) 13 */ 14 15 /* 16 * Initial permutation, 17 */ 18 static char IP[] = { 19 58,50,42,34,26,18,10, 2, 20 60,52,44,36,28,20,12, 4, 21 62,54,46,38,30,22,14, 6, 22 64,56,48,40,32,24,16, 8, 23 57,49,41,33,25,17, 9, 1, 24 59,51,43,35,27,19,11, 3, 25 61,53,45,37,29,21,13, 5, 26 63,55,47,39,31,23,15, 7, 27 }; 28 29 /* 30 * Final permutation, FP = IP^(-1) 31 */ 32 static char FP[] = { 33 40, 8,48,16,56,24,64,32, 34 39, 7,47,15,55,23,63,31, 35 38, 6,46,14,54,22,62,30, 36 37, 5,45,13,53,21,61,29, 37 36, 4,44,12,52,20,60,28, 38 35, 3,43,11,51,19,59,27, 39 34, 2,42,10,50,18,58,26, 40 33, 1,41, 9,49,17,57,25, 41 }; 42 43 /* 44 * Permuted-choice 1 from the key bits 45 * to yield C and D. 46 * Note that bits 8,16... are left out: 47 * They are intended for a parity check. 48 */ 49 static char PC1_C[] = { 50 57,49,41,33,25,17, 9, 51 1,58,50,42,34,26,18, 52 10, 2,59,51,43,35,27, 53 19,11, 3,60,52,44,36, 54 }; 55 56 static char PC1_D[] = { 57 63,55,47,39,31,23,15, Source Listing 9-SEP-1997 11:09:50 DEC C V5.0-003 Page 2 30-SEP-1996 09:27:36 X$SRC:[CSO.PH]CRYPT.C;1 58 7,62,54,46,38,30,22, 59 14, 6,61,53,45,37,29, 60 21,13, 5,28,20,12, 4, 61 }; 62 63 /* 64 * Sequence of shifts used for the key schedule. 65 */ 66 static char shifts[] = { 67 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1, 68 }; 69 70 /* 71 * Permuted-choice 2, to pick out the bits from 72 * the CD array that generate the key schedule. 73 */ 74 static char PC2_C[] = { 75 14,17,11,24, 1, 5, 76 3,28,15, 6,21,10, 77 23,19,12, 4,26, 8, 78 16, 7,27,20,13, 2, 79 }; 80 81 static char PC2_D[] = { 82 41,52,31,37,47,55, 83 30,40,51,45,33,48, 84 44,49,39,56,34,53, 85 46,42,50,36,29,32, 86 }; 87 88 /* 89 * The C and D arrays used to calculate the key schedule. 90 */ 91 92 static char C[28]; 93 static char D[28]; 94 /* 95 * The key schedule. 96 * Generated from the key. 97 */ 98 static char KS[16][48]; 99 100 /* 101 * The E bit-selection table. 102 */ 103 static char E[48]; 104 static char e[] = { 105 32, 1, 2, 3, 4, 5, 106 4, 5, 6, 7, 8, 9, 107 8, 9,10,11,12,13, 108 12,13,14,15,16,17, 109 16,17,18,19,20,21, 110 20,21,22,23,24,25, 111 24,25,26,27,28,29, 112 28,29,30,31,32, 1, 113 }; 114 Source Listing 9-SEP-1997 11:09:50 DEC C V5.0-003 Page 3 30-SEP-1996 09:27:36 X$SRC:[CSO.PH]CRYPT.C;1 115 /* 116 * Set up the key schedule from the key. 117 */ 118 119 setkey(key) 120 char *key; 121 { 122 register i, j, k; 123 int t; 124 125 /* 126 * First, generate C and D by permuting 127 * the key. The low order bit of each 128 * 8-bit char is not used, so C and D are only 28 129 * bits apiece. 130 */ 131 for (i=0; i<28; i++) { 132 C[i] = key[PC1_C[i]-1]; 133 D[i] = key[PC1_D[i]-1]; 134 } 135 /* 136 * To generate Ki, rotate C and D according 137 * to schedule and pick up a permutation 138 * using PC2. 139 */ 140 for (i=0; i<16; i++) { 141 /* 142 * rotate. 143 */ 144 for (k=0; k>3)&01; 300 f[t+1] = (k>>2)&01; 301 f[t+2] = (k>>1)&01; 302 f[t+3] = (k>>0)&01; 303 } 304 /* 305 * The new R is L ^ f(R, K). 306 * The f here has to be permuted first, though. 307 */ 308 for (j=0; j<32; j++) 309 R[j] = L[j] ^ f[P[j]-1]; 310 /* 311 * Finally, the new L (the original R) 312 * is copied back. 313 */ 314 for (j=0; j<32; j++) 315 L[j] = tempL[j]; 316 } 317 /* 318 * The output L and R are reversed. 319 */ 320 for (j=0; j<32; j++) { 321 t = L[j]; 322 L[j] = R[j]; 323 R[j] = t; 324 } 325 /* 326 * The final output 327 * gets the inverse permutation of the very original. 328 */ 329 for (j=0; j<64; j++) 330 block[j] = L[FP[j]-1]; 331 } 332 333 char * 334 crypt(pw,salt) 335 char *pw; 336 char *salt; 337 { 338 register i, j, c; 339 int temp; 340 static char block[66], iobuf[16]; 341 342 for(i=0; i<66; i++) Source Listing 9-SEP-1997 11:09:50 DEC C V5.0-003 Page 7 30-SEP-1996 09:27:36 X$SRC:[CSO.PH]CRYPT.C;1 343 block[i] = 0; 344 for(i=0; (c= *pw) && i<64; pw++){ 345 for(j=0; j<7; j++, i++) 346 block[i] = (c>>(6-j)) & 01; 347 i++; 348 } 349 350 setkey(block); 351 352 for(i=0; i<66; i++) 353 block[i] = 0; 354 355 for(i=0;i<2;i++){ 356 c = *salt++; 357 iobuf[i] = c; 358 if(c>'Z') c -= 6; 359 if(c>'9') c -= 7; 360 c -= '.'; 361 for(j=0;j<6;j++){ 362 if((c>>j) & 01){ 363 temp = E[6*i+j]; 364 E[6*i+j] = E[6*i+j+24]; 365 E[6*i+j+24] = temp; 366 } 367 } 368 } 369 370 for(i=0; i<25; i++) 371 encrypt(block,0); 372 373 for(i=0; i<11; i++){ 374 c = 0; 375 for(j=0; j<6; j++){ 376 c <<= 1; 377 c |= block[6*i+j]; 378 } 379 c += '.'; 380 if(c>'9') c += 7; 381 if(c>'Z') c += 6; 382 iobuf[i+2] = c; 383 } 384 iobuf[i+2] = 0; 385 if(iobuf[1]==0) 386 iobuf[1] = iobuf[0]; 387 return(iobuf); 388 } Command Line ------- ---- CC/NOANSI_ALIAS/ANALYSIS_DATA/ASSUME=(ACCURACY_SENSITIVE,ALIGNED_OBJECTS,WRITABLE_STRING_LITERALS) /CROSS_REFERENCE/NOCOMMENTS/DIAGNOSTICS/DEBUG=(SYMBOLS,TRACEBACK/ENDIAN=LITTLE /EXTERN_MODEL=RELAXED_REFDEF/FLOAT=(G_FLOAT)/GRANULARITY=QUADWORD/INSTRUCTION_SET=FLOATING_POINT /LINE_CONTROL/L_DOUBLE_SIZE=128/LINE_DIRECTIVES/LIST/NOMACHINE_CODE/MEMBER_ALIGNMENT /NAMES=UPPERCASE/NESTED_INCLUDE_DIRECTORY=INCLUDE_FILE/OBJECT/NOOPTIMIZE /PREFIX=(ALL_ENTRIES)/PSECT_MODEL=NOMULTILANGUAGE/ROUNDING_MODE=NEAREST Source Listing 9-SEP-1997 11:09:50 DEC C V5.0-003 Page 8 30-SEP-1996 09:27:36 X$SRC:[CSO.PH]CRYPT.C;1 /SHOW=(HEADER,SOURCE)/SIGNED_CHAR/STANDARD=VAXC/REENTRANCY=TOLERANT/NOTRAP_UNINITIALIZED_VARIABLES /WARNINGS .