mux/src/crypt/crypt-private.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void _ufc_doit_r (ufc_long itr, struct crypt_data *__data, ufc_long *res)
void __init_des_r (struct crypt_data *__data)
void __init_des (void)
char * fcrypt (const char *key, const char *salt)
void _ufc_setup_salt_r (const char *s, struct crypt_data *__data)
void _ufc_mk_keytab_r (char *key, struct crypt_data *__data)
void _ufc_dofinalperm_r (ufc_long *res, struct crypt_data *__data)
void _ufc_output_conversion_r (ufc_long v1, ufc_long v2, const char *salt, struct crypt_data *__data)


Function Documentation

void __init_des ( void   ) 

Definition at line 510 of file crypt_util.cpp.

References __init_des_r(), and _ufc_foobar.

00511 {
00512   __init_des_r(&_ufc_foobar);
00513 }

void __init_des_r ( struct crypt_data __data  ) 

Definition at line 312 of file crypt_util.cpp.

References BITMASK, bytemask, do_pc1, do_pc2, efp, eperm32tab, esel, final_perm, crypt_data::initialized, longmask, pc1, pc2, perm32, s_lookup, crypt_data::sb0, crypt_data::sb1, crypt_data::sb2, and crypt_data::sb3.

Referenced by __init_des(), and _ufc_setup_salt_r().

00313 {
00314   int comes_from_bit;
00315   int bit, sg;
00316   ufc_long j;
00317   ufc_long mask1, mask2;
00318   int e_inverse[64];
00319   static volatile int small_tables_initialized = 0;
00320 
00321 #ifdef _UFC_32_
00322   long32 *sb[4];
00323   sb[0] = (long32*)__data->sb0; sb[1] = (long32*)__data->sb1;
00324   sb[2] = (long32*)__data->sb2; sb[3] = (long32*)__data->sb3;
00325 #endif
00326 #ifdef _UFC_64_
00327   long64 *sb[4];
00328   sb[0] = (long64*)__data->sb0; sb[1] = (long64*)__data->sb1;
00329   sb[2] = (long64*)__data->sb2; sb[3] = (long64*)__data->sb3;
00330 #endif
00331 
00332   if(small_tables_initialized == 0) {
00333 #ifdef __GNU_LIBRARY__
00334     __libc_lock_lock (_ufc_tables_lock);
00335     if(small_tables_initialized)
00336       goto small_tables_done;
00337 #endif
00338 
00339     /*
00340      * Create the do_pc1 table used
00341      * to affect pc1 permutation
00342      * when generating keys
00343      */
00344     memset(do_pc1, 0, sizeof(do_pc1));
00345     for(bit = 0; bit < 56; bit++) {
00346       comes_from_bit  = pc1[bit] - 1;
00347       mask1 = bytemask[comes_from_bit % 8 + 1];
00348       mask2 = longmask[bit % 28 + 4];
00349       for(j = 0; j < 128; j++) {
00350     if(j & mask1)
00351       do_pc1[comes_from_bit / 8][bit / 28][j] |= mask2;
00352       }
00353     }
00354 
00355     /*
00356      * Create the do_pc2 table used
00357      * to affect pc2 permutation when
00358      * generating keys
00359      */
00360     memset(do_pc2, 0, sizeof(do_pc2));
00361     for(bit = 0; bit < 48; bit++) {
00362       comes_from_bit  = pc2[bit] - 1;
00363       mask1 = bytemask[comes_from_bit % 7 + 1];
00364       mask2 = BITMASK[bit % 24];
00365       for(j = 0; j < 128; j++) {
00366     if(j & mask1)
00367       do_pc2[comes_from_bit / 7][j] |= mask2;
00368       }
00369     }
00370 
00371     /*
00372      * Now generate the table used to do combined
00373      * 32 bit permutation and e expansion
00374      *
00375      * We use it because we have to permute 16384 32 bit
00376      * longs into 48 bit in order to initialize sb.
00377      *
00378      * Looping 48 rounds per permutation becomes
00379      * just too slow...
00380      *
00381      */
00382 
00383     memset(eperm32tab, 0, sizeof(eperm32tab));
00384     for(bit = 0; bit < 48; bit++) {
00385       ufc_long mask1,comes_from;
00386       comes_from = perm32[esel[bit]-1]-1;
00387       mask1      = bytemask[comes_from % 8];
00388       for(j = 256; j--;) {
00389     if(j & mask1)
00390       eperm32tab[comes_from / 8][j][bit / 24] |= BITMASK[bit % 24];
00391       }
00392     }
00393 
00394     /*
00395      * Create an inverse matrix for esel telling
00396      * where to plug out bits if undoing it
00397      */
00398     for(bit=48; bit--;) {
00399       e_inverse[esel[bit] - 1     ] = bit;
00400       e_inverse[esel[bit] - 1 + 32] = bit + 48;
00401     }
00402 
00403     /*
00404      * create efp: the matrix used to
00405      * undo the E expansion and effect final permutation
00406      */
00407     memset(efp, 0, sizeof efp);
00408     for(bit = 0; bit < 64; bit++) {
00409       int o_bit, o_long;
00410       ufc_long word_value, mask1, mask2;
00411       int comes_from_f_bit, comes_from_e_bit;
00412       int comes_from_word, bit_within_word;
00413 
00414       /* See where bit i belongs in the two 32 bit long's */
00415       o_long = bit / 32; /* 0..1  */
00416       o_bit  = bit % 32; /* 0..31 */
00417 
00418       /*
00419        * And find a bit in the e permutated value setting this bit.
00420        *
00421        * Note: the e selection may have selected the same bit several
00422        * times. By the initialization of e_inverse, we only look
00423        * for one specific instance.
00424        */
00425       comes_from_f_bit = final_perm[bit] - 1;         /* 0..63 */
00426       comes_from_e_bit = e_inverse[comes_from_f_bit]; /* 0..95 */
00427       comes_from_word  = comes_from_e_bit / 6;        /* 0..15 */
00428       bit_within_word  = comes_from_e_bit % 6;        /* 0..5  */
00429 
00430       mask1 = longmask[bit_within_word + 26];
00431       mask2 = longmask[o_bit];
00432 
00433       for(word_value = 64; word_value--;) {
00434     if(word_value & mask1)
00435       efp[comes_from_word][word_value][o_long] |= mask2;
00436       }
00437     }
00438     small_tables_initialized = 1;
00439 #ifdef __GNU_LIBRARY__
00440 small_tables_done:
00441     __libc_lock_unlock(_ufc_tables_lock);
00442 #endif
00443   }
00444 
00445   /*
00446    * Create the sb tables:
00447    *
00448    * For each 12 bit segment of an 48 bit intermediate
00449    * result, the sb table precomputes the two 4 bit
00450    * values of the sbox lookups done with the two 6
00451    * bit halves, shifts them to their proper place,
00452    * sends them through perm32 and finally E expands
00453    * them so that they are ready for the next
00454    * DES round.
00455    *
00456    */
00457 
00458   memset(__data->sb0, 0, sizeof(__data->sb0));
00459   memset(__data->sb1, 0, sizeof(__data->sb1));
00460   memset(__data->sb2, 0, sizeof(__data->sb2));
00461   memset(__data->sb3, 0, sizeof(__data->sb3));
00462 
00463   for(sg = 0; sg < 4; sg++) {
00464     int j1, j2;
00465     int s1, s2;
00466 
00467     for(j1 = 0; j1 < 64; j1++) {
00468       s1 = s_lookup(2 * sg, j1);
00469       for(j2 = 0; j2 < 64; j2++) {
00470     ufc_long to_permute, inx;
00471 
00472     s2         = s_lookup(2 * sg + 1, j2);
00473     to_permute = (((ufc_long)s1 << 4)  |
00474               (ufc_long)s2) << (24 - 8 * (ufc_long)sg);
00475 
00476 #ifdef _UFC_32_
00477     inx = ((j1 << 6)  | j2) << 1;
00478     sb[sg][inx  ]  = eperm32tab[0][(to_permute >> 24) & 0xff][0];
00479     sb[sg][inx+1]  = eperm32tab[0][(to_permute >> 24) & 0xff][1];
00480     sb[sg][inx  ] |= eperm32tab[1][(to_permute >> 16) & 0xff][0];
00481     sb[sg][inx+1] |= eperm32tab[1][(to_permute >> 16) & 0xff][1];
00482     sb[sg][inx  ] |= eperm32tab[2][(to_permute >>  8) & 0xff][0];
00483     sb[sg][inx+1] |= eperm32tab[2][(to_permute >>  8) & 0xff][1];
00484     sb[sg][inx  ] |= eperm32tab[3][(to_permute)       & 0xff][0];
00485     sb[sg][inx+1] |= eperm32tab[3][(to_permute)       & 0xff][1];
00486 #endif
00487 #ifdef _UFC_64_
00488     inx = ((j1 << 6)  | j2);
00489     sb[sg][inx]  =
00490       ((long64)eperm32tab[0][(to_permute >> 24) & 0xff][0] << 32) |
00491        (long64)eperm32tab[0][(to_permute >> 24) & 0xff][1];
00492     sb[sg][inx] |=
00493       ((long64)eperm32tab[1][(to_permute >> 16) & 0xff][0] << 32) |
00494        (long64)eperm32tab[1][(to_permute >> 16) & 0xff][1];
00495     sb[sg][inx] |=
00496       ((long64)eperm32tab[2][(to_permute >>  8) & 0xff][0] << 32) |
00497        (long64)eperm32tab[2][(to_permute >>  8) & 0xff][1];
00498     sb[sg][inx] |=
00499       ((long64)eperm32tab[3][(to_permute)       & 0xff][0] << 32) |
00500        (long64)eperm32tab[3][(to_permute)       & 0xff][1];
00501 #endif
00502       }
00503     }
00504   }
00505 
00506   __data->initialized++;
00507 }

void _ufc_dofinalperm_r ( ufc_long res,
struct crypt_data __data 
)

Definition at line 659 of file crypt_util.cpp.

References crypt_data::current_saltbits, and efp.

00660 {
00661   ufc_long v1, v2, x;
00662   ufc_long l1,l2,r1,r2;
00663 
00664   l1 = res[0]; l2 = res[1];
00665   r1 = res[2]; r2 = res[3];
00666 
00667   x = (l1 ^ l2) & __data->current_saltbits; l1 ^= x; l2 ^= x;
00668   x = (r1 ^ r2) & __data->current_saltbits; r1 ^= x; r2 ^= x;
00669 
00670   v1=v2=0; l1 >>= 3; l2 >>= 3; r1 >>= 3; r2 >>= 3;
00671 
00672   v1 |= efp[15][ r2         & 0x3f][0]; v2 |= efp[15][ r2 & 0x3f][1];
00673   v1 |= efp[14][(r2 >>= 6)  & 0x3f][0]; v2 |= efp[14][ r2 & 0x3f][1];
00674   v1 |= efp[13][(r2 >>= 10) & 0x3f][0]; v2 |= efp[13][ r2 & 0x3f][1];
00675   v1 |= efp[12][(r2 >>= 6)  & 0x3f][0]; v2 |= efp[12][ r2 & 0x3f][1];
00676 
00677   v1 |= efp[11][ r1         & 0x3f][0]; v2 |= efp[11][ r1 & 0x3f][1];
00678   v1 |= efp[10][(r1 >>= 6)  & 0x3f][0]; v2 |= efp[10][ r1 & 0x3f][1];
00679   v1 |= efp[ 9][(r1 >>= 10) & 0x3f][0]; v2 |= efp[ 9][ r1 & 0x3f][1];
00680   v1 |= efp[ 8][(r1 >>= 6)  & 0x3f][0]; v2 |= efp[ 8][ r1 & 0x3f][1];
00681 
00682   v1 |= efp[ 7][ l2         & 0x3f][0]; v2 |= efp[ 7][ l2 & 0x3f][1];
00683   v1 |= efp[ 6][(l2 >>= 6)  & 0x3f][0]; v2 |= efp[ 6][ l2 & 0x3f][1];
00684   v1 |= efp[ 5][(l2 >>= 10) & 0x3f][0]; v2 |= efp[ 5][ l2 & 0x3f][1];
00685   v1 |= efp[ 4][(l2 >>= 6)  & 0x3f][0]; v2 |= efp[ 4][ l2 & 0x3f][1];
00686 
00687   v1 |= efp[ 3][ l1         & 0x3f][0]; v2 |= efp[ 3][ l1 & 0x3f][1];
00688   v1 |= efp[ 2][(l1 >>= 6)  & 0x3f][0]; v2 |= efp[ 2][ l1 & 0x3f][1];
00689   v1 |= efp[ 1][(l1 >>= 10) & 0x3f][0]; v2 |= efp[ 1][ l1 & 0x3f][1];
00690   v1 |= efp[ 0][(l1 >>= 6)  & 0x3f][0]; v2 |= efp[ 0][ l1 & 0x3f][1];
00691 
00692   res[0] = v1; res[1] = v2;
00693 }

void _ufc_doit_r ( ufc_long  itr,
struct crypt_data __data,
ufc_long res 
)

Definition at line 40 of file crypt.cpp.

References crypt_data::keysched, crypt_data::sb0, crypt_data::sb2, and SBA.

Referenced by __encrypt_r(), and crypt_r().

00041 {
00042   int i;
00043   long32 s, *k;
00044   long32 *sb01 = (long32*)__data->sb0;
00045   long32 *sb23 = (long32*)__data->sb2;
00046   long32 l1, l2, r1, r2;
00047 
00048   l1 = (long32)res[0]; l2 = (long32)res[1];
00049   r1 = (long32)res[2]; r2 = (long32)res[3];
00050 
00051   while(itr--) {
00052     k = (long32*)__data->keysched;
00053     for(i=8; i--; ) {
00054       s = *k++ ^ r1;
00055       l1 ^= SBA(sb01, s & 0xffff); l2 ^= SBA(sb01, (s & 0xffff)+4);
00056       l1 ^= SBA(sb01, s >>= 16  ); l2 ^= SBA(sb01, (s         )+4);
00057       s = *k++ ^ r2;
00058       l1 ^= SBA(sb23, s & 0xffff); l2 ^= SBA(sb23, (s & 0xffff)+4);
00059       l1 ^= SBA(sb23, s >>= 16  ); l2 ^= SBA(sb23, (s         )+4);
00060 
00061       s = *k++ ^ l1;
00062       r1 ^= SBA(sb01, s & 0xffff); r2 ^= SBA(sb01, (s & 0xffff)+4);
00063       r1 ^= SBA(sb01, s >>= 16  ); r2 ^= SBA(sb01, (s         )+4);
00064       s = *k++ ^ l2;
00065       r1 ^= SBA(sb23, s & 0xffff); r2 ^= SBA(sb23, (s & 0xffff)+4);
00066       r1 ^= SBA(sb23, s >>= 16  ); r2 ^= SBA(sb23, (s         )+4);
00067     }
00068     s=l1; l1=r1; r1=s; s=l2; l2=r2; r2=s;
00069   }
00070   res[0] = l1; res[1] = l2; res[2] = r1; res[3] = r2;
00071 }

void _ufc_mk_keytab_r ( char *  key,
struct crypt_data __data 
)

Definition at line 601 of file crypt_util.cpp.

References crypt_data::direction, do_pc1, do_pc2, crypt_data::keysched, and rots.

00602 {
00603   ufc_long v1, v2, *k1;
00604   int i;
00605 #ifdef _UFC_32_
00606   long32 v, *k2;
00607   k2 = (long32*)__data->keysched;
00608 #endif
00609 #ifdef _UFC_64_
00610   long64 v, *k2;
00611   k2 = (long64*)__data->keysched;
00612 #endif
00613 
00614   v1 = v2 = 0; k1 = &do_pc1[0][0][0];
00615   for(i = 8; i--;) {
00616     v1 |= k1[*key   & 0x7f]; k1 += 128;
00617     v2 |= k1[*key++ & 0x7f]; k1 += 128;
00618   }
00619 
00620   for(i = 0; i < 16; i++) {
00621     k1 = &do_pc2[0][0];
00622 
00623     v1 = (v1 << rots[i]) | (v1 >> (28 - rots[i]));
00624     v  = k1[(v1 >> 21) & 0x7f]; k1 += 128;
00625     v |= k1[(v1 >> 14) & 0x7f]; k1 += 128;
00626     v |= k1[(v1 >>  7) & 0x7f]; k1 += 128;
00627     v |= k1[(v1      ) & 0x7f]; k1 += 128;
00628 
00629 #ifdef _UFC_32_
00630     *k2++ = (v | 0x00008000);
00631     v = 0;
00632 #endif
00633 #ifdef _UFC_64_
00634     v = (v << 32);
00635 #endif
00636 
00637     v2 = (v2 << rots[i]) | (v2 >> (28 - rots[i]));
00638     v |= k1[(v2 >> 21) & 0x7f]; k1 += 128;
00639     v |= k1[(v2 >> 14) & 0x7f]; k1 += 128;
00640     v |= k1[(v2 >>  7) & 0x7f]; k1 += 128;
00641     v |= k1[(v2      ) & 0x7f];
00642 
00643 #ifdef _UFC_32_
00644     *k2++ = (v | 0x00008000);
00645 #endif
00646 #ifdef _UFC_64_
00647     *k2++ = v | 0x0000800000008000l;
00648 #endif
00649   }
00650 
00651   __data->direction = 0;
00652 }

void _ufc_output_conversion_r ( ufc_long  v1,
ufc_long  v2,
const char *  salt,
struct crypt_data __data 
)

Definition at line 701 of file crypt_util.cpp.

References bin_to_ascii, and crypt_data::crypt_3_buf.

00702 {
00703   int i, s, shf;
00704 
00705   __data->crypt_3_buf[0] = salt[0];
00706   __data->crypt_3_buf[1] = salt[1] ? salt[1] : salt[0];
00707 
00708   for(i = 0; i < 5; i++) {
00709     shf = (26 - 6 * i); /* to cope with MSC compiler bug */
00710     __data->crypt_3_buf[i + 2] = bin_to_ascii((v1 >> shf) & 0x3f);
00711   }
00712 
00713   s  = (v2 & 0xf) << 2;
00714   v2 = (v2 >> 2) | ((v1 & 0x3) << 30);
00715 
00716   for(i = 5; i < 10; i++) {
00717     shf = (56 - 6 * i);
00718     __data->crypt_3_buf[i + 2] = bin_to_ascii((v2 >> shf) & 0x3f);
00719   }
00720 
00721   __data->crypt_3_buf[12] = bin_to_ascii(s);
00722   __data->crypt_3_buf[13] = 0;
00723 }

void _ufc_setup_salt_r ( const char *  s,
struct crypt_data __data 
)

Definition at line 555 of file crypt_util.cpp.

References __init_des_r(), ascii_to_bin, BITMASK, crypt_data::current_salt, crypt_data::current_saltbits, crypt_data::initialized, LONGG, crypt_data::sb0, crypt_data::sb1, crypt_data::sb2, crypt_data::sb3, and shuffle_sb().

00556 {
00557   ufc_long i, j, saltbits;
00558 
00559   if(__data->initialized == 0)
00560     __init_des_r(__data);
00561 
00562   if(s[0] == __data->current_salt[0] && s[1] == __data->current_salt[1])
00563     return;
00564   __data->current_salt[0] = s[0]; __data->current_salt[1] = s[1];
00565 
00566   /*
00567    * This is the only crypt change to DES:
00568    * entries are swapped in the expansion table
00569    * according to the bits set in the salt.
00570    */
00571   saltbits = 0;
00572   for(i = 0; i < 2; i++) {
00573     long c=ascii_to_bin(s[i]);
00574     for(j = 0; j < 6; j++) {
00575       if((c >> j) & 0x1)
00576     saltbits |= BITMASK[6 * i + j];
00577     }
00578   }
00579 
00580   /*
00581    * Permute the sb table values
00582    * to reflect the changed e
00583    * selection table
00584    */
00585 #ifdef _UFC_32_
00586 #define LONGG long32*
00587 #endif
00588 #ifdef _UFC_64_
00589 #define LONGG long64*
00590 #endif
00591 
00592   shuffle_sb((LONGG)__data->sb0, __data->current_saltbits ^ saltbits);
00593   shuffle_sb((LONGG)__data->sb1, __data->current_saltbits ^ saltbits);
00594   shuffle_sb((LONGG)__data->sb2, __data->current_saltbits ^ saltbits);
00595   shuffle_sb((LONGG)__data->sb3, __data->current_saltbits ^ saltbits);
00596 
00597   __data->current_saltbits = saltbits;
00598 }

char* fcrypt ( const char *  key,
const char *  salt 
)


Generated on Mon May 28 04:40:14 2007 for MUX by  doxygen 1.4.7