Annotation of cci/usr/src/lib/libc/gen/crypt.c, revision 1.1

1.1     ! root        1: /*     \@(#)crypt.c    2.1     */
        !             2: /*LINTLIBRARY*/
        !             3: /*
        !             4:  * This program implements a data encryption algorithm to encrypt passwords.
        !             5:  */
        !             6: 
        !             7: static char IP[] = {
        !             8:        58,50,42,34,26,18,10, 2,
        !             9:        60,52,44,36,28,20,12, 4,
        !            10:        62,54,46,38,30,22,14, 6,
        !            11:        64,56,48,40,32,24,16, 8,
        !            12:        57,49,41,33,25,17, 9, 1,
        !            13:        59,51,43,35,27,19,11, 3,
        !            14:        61,53,45,37,29,21,13, 5,
        !            15:        63,55,47,39,31,23,15, 7,
        !            16: };
        !            17: 
        !            18: static char FP[] = {
        !            19:        40, 8,48,16,56,24,64,32,
        !            20:        39, 7,47,15,55,23,63,31,
        !            21:        38, 6,46,14,54,22,62,30,
        !            22:        37, 5,45,13,53,21,61,29,
        !            23:        36, 4,44,12,52,20,60,28,
        !            24:        35, 3,43,11,51,19,59,27,
        !            25:        34, 2,42,10,50,18,58,26,
        !            26:        33, 1,41, 9,49,17,57,25,
        !            27: };
        !            28: 
        !            29: static char PC1_C[] = {
        !            30:        57,49,41,33,25,17, 9,
        !            31:         1,58,50,42,34,26,18,
        !            32:        10, 2,59,51,43,35,27,
        !            33:        19,11, 3,60,52,44,36,
        !            34: };
        !            35: 
        !            36: static char PC1_D[] = {
        !            37:        63,55,47,39,31,23,15,
        !            38:         7,62,54,46,38,30,22,
        !            39:        14, 6,61,53,45,37,29,
        !            40:        21,13, 5,28,20,12, 4,
        !            41: };
        !            42: 
        !            43: static char shifts[] = { 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1, };
        !            44: 
        !            45: static char PC2_C[] = {
        !            46:        14,17,11,24, 1, 5,
        !            47:         3,28,15, 6,21,10,
        !            48:        23,19,12, 4,26, 8,
        !            49:        16, 7,27,20,13, 2,
        !            50: };
        !            51: 
        !            52: static char PC2_D[] = {
        !            53:        41,52,31,37,47,55,
        !            54:        30,40,51,45,33,48,
        !            55:        44,49,39,56,34,53,
        !            56:        46,42,50,36,29,32,
        !            57: };
        !            58: 
        !            59: static char C[28];
        !            60: static char D[28];
        !            61: static char KS[16][48];
        !            62: 
        !            63: static char E[48];
        !            64: static char e2[] = {
        !            65:        32, 1, 2, 3, 4, 5,
        !            66:         4, 5, 6, 7, 8, 9,
        !            67:         8, 9,10,11,12,13,
        !            68:        12,13,14,15,16,17,
        !            69:        16,17,18,19,20,21,
        !            70:        20,21,22,23,24,25,
        !            71:        24,25,26,27,28,29,
        !            72:        28,29,30,31,32, 1,
        !            73: };
        !            74: 
        !            75: static char S[8][64] = {
        !            76:        14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
        !            77:         0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
        !            78:         4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
        !            79:        15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13,
        !            80: 
        !            81:        15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
        !            82:         3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
        !            83:         0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
        !            84:        13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9,
        !            85: 
        !            86:        10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
        !            87:        13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
        !            88:        13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
        !            89:         1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12,
        !            90: 
        !            91:         7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
        !            92:        13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
        !            93:        10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
        !            94:         3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14,
        !            95: 
        !            96:         2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
        !            97:        14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
        !            98:         4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
        !            99:        11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3,
        !           100: 
        !           101:        12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
        !           102:        10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,
        !           103:         9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
        !           104:         4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13,
        !           105: 
        !           106:         4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
        !           107:        13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,
        !           108:         1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
        !           109:         6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12,
        !           110: 
        !           111:        13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
        !           112:         1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
        !           113:         7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
        !           114:         2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11,
        !           115: };
        !           116: 
        !           117: static char P[] = {
        !           118:        16, 7,20,21,
        !           119:        29,12,28,17,
        !           120:         1,15,23,26,
        !           121:         5,18,31,10,
        !           122:         2, 8,24,14,
        !           123:        32,27, 3, 9,
        !           124:        19,13,30, 6,
        !           125:        22,11, 4,25,
        !           126: };
        !           127: 
        !           128: static char L[32], R[32];
        !           129: static char tempL[32];
        !           130: static char f[32];
        !           131: 
        !           132: static char preS[48];
        !           133: 
        !           134: void
        !           135: encrypt(block)
        !           136: char   *block;
        !           137: {
        !           138:        int      ii;
        !           139:        register int t, j, k;
        !           140: 
        !           141:        for(j=0; j < 64; j++)
        !           142:                L[j] = block[IP[j]-1];
        !           143:        for(ii=0; ii < 16; ii++) {
        !           144:                for(j=0; j < 32; j++)
        !           145:                        tempL[j] = R[j];
        !           146:                for(j=0; j < 48; j++)
        !           147:                        preS[j] = R[E[j]-1] ^ KS[ii][j];
        !           148:                for(j=0; j < 8; j++) {
        !           149:                        t = 6*j;
        !           150:                        k = S[j][(preS[t+0]<<5)+
        !           151:                                (preS[t+1]<<3)+
        !           152:                                (preS[t+2]<<2)+
        !           153:                                (preS[t+3]<<1)+
        !           154:                                (preS[t+4]<<0)+
        !           155:                                (preS[t+5]<<4)];
        !           156:                        t = 4*j;
        !           157:                        f[t+0] = (k>>3)&01;
        !           158:                        f[t+1] = (k>>2)&01;
        !           159:                        f[t+2] = (k>>1)&01;
        !           160:                        f[t+3] = (k>>0)&01;
        !           161:                }
        !           162:                for(j=0; j < 32; j++)
        !           163:                        R[j] = L[j] ^ f[P[j]-1];
        !           164:                for(j=0; j < 32; j++)
        !           165:                        L[j] = tempL[j];
        !           166:        }
        !           167:        for(j=0; j < 32; j++) {
        !           168:                t = L[j];
        !           169:                L[j] = R[j];
        !           170:                R[j] = t;
        !           171:        }
        !           172:        for(j=0; j < 64; j++)
        !           173:                block[j] = L[FP[j]-1];
        !           174: }
        !           175: 
        !           176: char *
        !           177: crypt(pw, salt)
        !           178: char   *pw, *salt;
        !           179: {
        !           180:        register int c, i, j, k;
        !           181:        int     t, temp;
        !           182:        static char block[66], iobuf[16];
        !           183: 
        !           184:        for(i=0; i < 66; i++)
        !           185:                block[i] = 0;
        !           186:        for(i=0; (c= *pw) && i < 64; pw++) {
        !           187:                for(j=0; j < 7; j++, i++)
        !           188:                        block[i] = (c>>(6-j)) & 01;
        !           189:                i++;
        !           190:        }
        !           191:        
        !           192:        for(i=0; i < 28; i++) {
        !           193:                C[i] = block[PC1_C[i]-1];
        !           194:                D[i] = block[PC1_D[i]-1];
        !           195:        }
        !           196:        for(i=0; i < 16; i++) {
        !           197:                for(k=0; k < shifts[i]; k++) {
        !           198:                        t = C[0];
        !           199:                        for(j=0; j < 28-1; j++)
        !           200:                                C[j] = C[j+1];
        !           201:                        C[27] = t;
        !           202:                        t = D[0];
        !           203:                        for(j=0; j < 28-1; j++)
        !           204:                                D[j] = D[j+1];
        !           205:                        D[27] = t;
        !           206:                }
        !           207:                for(j=0; j < 24; j++) {
        !           208:                        KS[i][j] = C[PC2_C[j]-1];
        !           209:                        KS[i][j+24] = D[PC2_D[j]-28-1];
        !           210:                }
        !           211:        }
        !           212: 
        !           213:        for(i=0; i < 48; i++)
        !           214:                E[i] = e2[i];
        !           215:        
        !           216:        for(i=0; i < 66; i++)
        !           217:                block[i] = 0;
        !           218: 
        !           219:        for(i=0; i < 2; i++) {
        !           220:                c = *salt++;
        !           221:                iobuf[i] = c;
        !           222:                if(c > 'Z')
        !           223:                        c -= 6;
        !           224:                if(c > '9')
        !           225:                        c -= 7;
        !           226:                c -= '.';
        !           227:                for(j=0; j < 6; j++) {
        !           228:                        if((c>>j) & 01) {
        !           229:                                temp = E[6*i+j];
        !           230:                                E[6*i+j] = E[6*i+j+24];
        !           231:                                E[6*i+j+24] = temp;
        !           232:                        }
        !           233:                }
        !           234:        }
        !           235:        
        !           236:        for(i=0; i < 25; i++)
        !           237:                encrypt(block);
        !           238:        
        !           239:        for(i=0; i < 11; i++) {
        !           240:                c = 0;
        !           241:                for(j=0; j < 6; j++) {
        !           242:                        c <<= 1;
        !           243:                        c |= block[6*i+j];
        !           244:                }
        !           245:                c += '.';
        !           246:                if(c > '9')
        !           247:                        c += 7;
        !           248:                if(c > 'Z')
        !           249:                        c += 6;
        !           250:                iobuf[i+2] = c;
        !           251:        }
        !           252:        iobuf[i+2] = 0;
        !           253:        if(iobuf[1] == 0)
        !           254:                iobuf[1] = iobuf[0];
        !           255:        return(iobuf);
        !           256: }
        !           257: 
        !           258: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.