📄 crypt_util.c
字号:
for(word_value = 64; word_value--;) { if(word_value & mask1) efp[comes_from_word][word_value][o_long] |= mask2; } } /* * Create revfinal: an array to undo final * the effects of efp */ clearmem((char*)revfinal, sizeof(revfinal)); for(bit = 0; bit < 96; bit++) { int ibit = initial_perm[esel[bit % 48] - 1 + ((bit >= 48) ? 32 : 0)] - 1; mask1 = bytemask[ibit % 6 + 2]; mask2 = BITMASK(bit % 24); for(j = 64; j--;) { if(j & mask1) { revfinal[ibit / 6][j][bit / 24] |= mask2; } } } initialized++; }/* * Process the elements of the sb table permuting the * bits swapped in the expansion by the current salt. */#ifdef _UFC_32_STATIC void shuffle_sb(k, saltbits) long32 *k; ufc_long saltbits; { ufc_long j; long32 x; for(j=4096; j--;) { x = (k[0] ^ k[1]) & (long32)saltbits; *k++ ^= x; *k++ ^= x; } }#endif#ifdef _UFC_64_STATIC void shuffle_sb(k, saltbits) long64 *k; ufc_long saltbits; { ufc_long j; long64 x; for(j=4096; j--;) { x = ((*k >> 32) ^ *k) & (long64)saltbits; *k++ ^= (x << 32) | x; } }#endif/* * Setup the unit for a new salt * Hopefully we'll not see a new salt in each crypt call. */static unsigned char current_salt[3] = "&&"; /* invalid value */static ufc_long current_saltbits = 0;static int direction = 0;STATIC void setup_salt(s) char *s; { ufc_long i, j, saltbits; if(!initialized) init_des(); if(s[0] == current_salt[0] && s[1] == current_salt[1]) return; current_salt[0] = s[0]; current_salt[1] = s[1]; /* * This is the only crypt change to DES: * entries are swapped in the expansion table * according to the bits set in the salt. */ saltbits = 0; for(i = 0; i < 2; i++) { long c=ascii_to_bin(s[i]);#ifdef notdef /* * Some applications do rely on illegal * salts. It seems that UFC-crypt behaves * identically to standard crypt * implementations on illegal salts -- glad */ if(c < 0 || c > 63) c = 0;#endif for(j = 0; j < 6; j++) { if((c >> j) & 0x1) saltbits |= BITMASK(6 * i + j); } } /* * Permute the sb table values * to reflect the changed e * selection table */ shuffle_sb(_ufc_sb0, current_saltbits ^ saltbits); shuffle_sb(_ufc_sb1, current_saltbits ^ saltbits); shuffle_sb(_ufc_sb2, current_saltbits ^ saltbits); shuffle_sb(_ufc_sb3, current_saltbits ^ saltbits); current_saltbits = saltbits; }STATIC void ufc_mk_keytab(key) char *key; { ufc_long v1, v2, *k1; int i;#ifdef _UFC_32_ long32 v, *k2 = &_ufc_keytab[0][0];#endif#ifdef _UFC_64_ long64 v, *k2 = &_ufc_keytab[0];#endif v1 = v2 = 0; k1 = &do_pc1[0][0][0]; for(i = 8; i--;) { v1 |= k1[*key & 0x7f]; k1 += 128; v2 |= k1[*key++ & 0x7f]; k1 += 128; } for(i = 0; i < 16; i++) { k1 = &do_pc2[0][0]; v1 = (v1 << rots[i]) | (v1 >> (28 - rots[i])); v = k1[(v1 >> 21) & 0x7f]; k1 += 128; v |= k1[(v1 >> 14) & 0x7f]; k1 += 128; v |= k1[(v1 >> 7) & 0x7f]; k1 += 128; v |= k1[(v1 ) & 0x7f]; k1 += 128;#ifdef _UFC_32_ *k2++ = v; v = 0;#endif#ifdef _UFC_64_ v <<= 32;#endif v2 = (v2 << rots[i]) | (v2 >> (28 - rots[i])); v |= k1[(v2 >> 21) & 0x7f]; k1 += 128; v |= k1[(v2 >> 14) & 0x7f]; k1 += 128; v |= k1[(v2 >> 7) & 0x7f]; k1 += 128; v |= k1[(v2 ) & 0x7f]; *k2++ = v; } direction = 0; }/* * Undo an extra E selection and do final permutations */ufc_long *_ufc_dofinalperm(l1, l2, r1, r2) ufc_long l1,l2,r1,r2; { ufc_long v1, v2, x; static ufc_long ary[2]; x = (l1 ^ l2) & current_saltbits; l1 ^= x; l2 ^= x; x = (r1 ^ r2) & current_saltbits; r1 ^= x; r2 ^= x; v1=v2=0; l1 >>= 3; l2 >>= 3; r1 >>= 3; r2 >>= 3; v1 |= efp[15][ r2 & 0x3f][0]; v2 |= efp[15][ r2 & 0x3f][1]; v1 |= efp[14][(r2 >>= 6) & 0x3f][0]; v2 |= efp[14][ r2 & 0x3f][1]; v1 |= efp[13][(r2 >>= 10) & 0x3f][0]; v2 |= efp[13][ r2 & 0x3f][1]; v1 |= efp[12][(r2 >>= 6) & 0x3f][0]; v2 |= efp[12][ r2 & 0x3f][1]; v1 |= efp[11][ r1 & 0x3f][0]; v2 |= efp[11][ r1 & 0x3f][1]; v1 |= efp[10][(r1 >>= 6) & 0x3f][0]; v2 |= efp[10][ r1 & 0x3f][1]; v1 |= efp[ 9][(r1 >>= 10) & 0x3f][0]; v2 |= efp[ 9][ r1 & 0x3f][1]; v1 |= efp[ 8][(r1 >>= 6) & 0x3f][0]; v2 |= efp[ 8][ r1 & 0x3f][1]; v1 |= efp[ 7][ l2 & 0x3f][0]; v2 |= efp[ 7][ l2 & 0x3f][1]; v1 |= efp[ 6][(l2 >>= 6) & 0x3f][0]; v2 |= efp[ 6][ l2 & 0x3f][1]; v1 |= efp[ 5][(l2 >>= 10) & 0x3f][0]; v2 |= efp[ 5][ l2 & 0x3f][1]; v1 |= efp[ 4][(l2 >>= 6) & 0x3f][0]; v2 |= efp[ 4][ l2 & 0x3f][1]; v1 |= efp[ 3][ l1 & 0x3f][0]; v2 |= efp[ 3][ l1 & 0x3f][1]; v1 |= efp[ 2][(l1 >>= 6) & 0x3f][0]; v2 |= efp[ 2][ l1 & 0x3f][1]; v1 |= efp[ 1][(l1 >>= 10) & 0x3f][0]; v2 |= efp[ 1][ l1 & 0x3f][1]; v1 |= efp[ 0][(l1 >>= 6) & 0x3f][0]; v2 |= efp[ 0][ l1 & 0x3f][1]; ary[0] = v1; ary[1] = v2; return ary; }/* * crypt only: convert from 64 bit to 11 bit ASCII * prefixing with the salt */STATIC char *output_conversion(v1, v2, salt) ufc_long v1, v2; char *salt; { static char outbuf[14]; int i, s, shf; outbuf[0] = salt[0]; outbuf[1] = salt[1] ? salt[1] : salt[0]; for(i = 0; i < 5; i++) { shf = (26 - 6 * i); /* to cope with MSC compiler bug */ outbuf[i + 2] = bin_to_ascii((v1 >> shf) & 0x3f); } s = (v2 & 0xf) << 2; v2 = (v2 >> 2) | ((v1 & 0x3) << 30); for(i = 5; i < 10; i++) { shf = (56 - 6 * i); outbuf[i + 2] = bin_to_ascii((v2 >> shf) & 0x3f); } outbuf[12] = bin_to_ascii(s); outbuf[13] = 0; return outbuf; }ufc_long *_ufc_doit();/* * UNIX crypt function */ char *crypt(key, salt) char *key, *salt; { ufc_long *s; char ktab[9]; /* * Hack DES tables according to salt */ setup_salt(salt); /* * Setup key schedule */ clearmem(ktab, sizeof ktab); (void)strncpy(ktab, key, 8); ufc_mk_keytab(ktab); /* * Go for the 25 DES encryptions */ s = _ufc_doit((ufc_long)0, (ufc_long)0, (ufc_long)0, (ufc_long)0, (ufc_long)25); /* * Do final permutations */ s = _ufc_dofinalperm(s[0], s[1], s[2], s[3]); /* * And convert back to 6 bit ASCII */ return output_conversion(s[0], s[1], salt); }/* * To make fcrypt users happy. * They don't need to call init_des. */char *fcrypt(key, salt) char *key; char *salt; { return crypt(key, salt); }/* * UNIX encrypt function. Takes a bitvector * represented by one byte per bit and * encrypt/decrypt according to edflag */void encrypt(block, edflag) char *block; int edflag; { ufc_long l1, l2, r1, r2, *s; int i; /* * Undo any salt changes to E expansion */ setup_salt(".."); /* * Reverse key table if * changing operation (encrypt/decrypt) */ if((edflag == 0) != (direction == 0)) { for(i = 0; i < 8; i++) {#ifdef _UFC_32_ long32 x; x = _ufc_keytab[15-i][0]; _ufc_keytab[15-i][0] = _ufc_keytab[i][0]; _ufc_keytab[i][0] = x; x = _ufc_keytab[15-i][1]; _ufc_keytab[15-i][1] = _ufc_keytab[i][1]; _ufc_keytab[i][1] = x;#endif#ifdef _UFC_64_ long64 x; x = _ufc_keytab[15-i]; _ufc_keytab[15-i] = _ufc_keytab[i]; _ufc_keytab[i] = x;#endif } direction = edflag; } /* * Do initial permutation + E expansion */ i = 0; for(l1 = 0; i < 24; i++) { if(block[initial_perm[esel[i]-1]-1]) l1 |= BITMASK(i); } for(l2 = 0; i < 48; i++) { if(block[initial_perm[esel[i]-1]-1]) l2 |= BITMASK(i-24); } i = 0; for(r1 = 0; i < 24; i++) { if(block[initial_perm[esel[i]-1+32]-1]) r1 |= BITMASK(i); } for(r2 = 0; i < 48; i++) { if(block[initial_perm[esel[i]-1+32]-1]) r2 |= BITMASK(i-24); } /* * Do DES inner loops + final conversion */ s = _ufc_doit(l1, l2, r1, r2, (ufc_long)1); /* * Do final permutations */ s = _ufc_dofinalperm(s[0], s[1], s[2], s[3]); /* * And convert to bit array */ l1 = s[0]; r1 = s[1]; for(i = 0; i < 32; i++) { *block++ = (l1 & longmask[i]) != 0; } for(i = 0; i < 32; i++) { *block++ = (r1 & longmask[i]) != 0; } }/* * UNIX setkey function. Take a 64 bit DES * key and setup the machinery. */void setkey(key) char *key; { int i,j; unsigned char c; unsigned char ktab[8]; setup_salt(".."); /* be sure we're initialized */ for(i = 0; i < 8; i++) { for(j = 0, c = 0; j < 8; j++) c = c << 1 | *key++; ktab[i] = c >> 1; } ufc_mk_keytab(ktab); }/* * Ultrix crypt16 function, thanks to pcl@convex.oxford.ac.uk (Paul Leyland) */ char *crypt16(key, salt) char *key, *salt; { ufc_long *s, *t; char ktab[9], ttab[9]; static char q[14], res[25]; /* * Hack DES tables according to salt */ setup_salt(salt); /* * Setup key schedule */ clearmem(ktab, sizeof ktab); (void)strncpy(ktab, key, 8); ufc_mk_keytab(ktab); /* * Go for first 20 DES encryptions */ s = _ufc_doit((ufc_long)0, (ufc_long)0, (ufc_long)0, (ufc_long)0, (ufc_long)20); /* * And convert back to 6 bit ASCII */ strcpy (res, output_conversion(s[0], s[1], salt)); clearmem(ttab, sizeof ttab); if (strlen (key) > 8) (void)strncpy(ttab, key+8, 8); ufc_mk_keytab(ttab); /* * Go for second 5 DES encryptions */ t = _ufc_doit((ufc_long)0, (ufc_long)0, (ufc_long)0, (ufc_long)0, (ufc_long)5); /* * And convert back to 6 bit ASCII */ strcpy (q, output_conversion(t[0], t[1], salt)); strcpy (res+13, q+2); clearmem(ktab, sizeof ktab); (void)strncpy(ktab, key, 8); ufc_mk_keytab(ktab); return res; }/* * Experimental -- not supported -- may choke your dog */void ufc_setup_password(cookie, s) long *cookie; char *s; { char c; int i; ufc_long x; ufc_long dl1, dl2, dr1, dr2; setup_salt(s); dl1 = dl2 = dr1 = dr2 = 0; for(i = 0, s += 2; c = *s++; i++) { int x = ascii_to_bin(c); dl1 |= revfinal[i][x][0]; dl2 |= revfinal[i][x][1]; dr1 |= revfinal[i][x][2]; dr2 |= revfinal[i][x][3]; } x = (dl1 ^ dl2) & current_saltbits; x = (dr1 ^ dr2) & current_saltbits; cookie[0] = dl1 ^ x; cookie[1] = dl2 ^ x; cookie[2] = dr1 ^ x; cookie[3] = dr2 ^ x; }void ufc_do_pw(cookie, guess) long *cookie; char *guess; { char ktab[9]; ufc_long *s; clearmem(ktab, sizeof ktab); (void)strncpy(ktab, guess, 8); ufc_mk_keytab(ktab); s = _ufc_doit((ufc_long)0, (ufc_long)0, (ufc_long)0, (ufc_long)0, (ufc_long)25); cookie[0] = s[0]; cookie[1] = s[1]; cookie[2] = s[2]; cookie[3] = s[3]; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -