⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 des.txt

📁 关于黑客的论坛的下载资料
💻 TXT
📖 第 1 页 / 共 3 页
字号:
 * bits zero.
 */
static ufc_long do_pc2[8][128];

/*
 * efp: undo an extra e selection and do final
 *      permutation giving the DES result.
 *
 *      Invoked 6 bit a time on two 48 bit values
 *      giving two 32 bit longs.
 */
static ufc_long efp[16][64][2];

static unsigned char bytemask[8]  = {
  0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
};

static ufc_long longmask[32] = {
  0x80000000, 0x40000000, 0x20000000, 0x10000000,
  0x08000000, 0x04000000, 0x02000000, 0x01000000,
  0x00800000, 0x00400000, 0x00200000, 0x00100000,
  0x00080000, 0x00040000, 0x00020000, 0x00010000,
  0x00008000, 0x00004000, 0x00002000, 0x00001000,
  0x00000800, 0x00000400, 0x00000200, 0x00000100,
  0x00000080, 0x00000040, 0x00000020, 0x00000010,
  0x00000008, 0x00000004, 0x00000002, 0x00000001
};

#ifdef DEBUG

pr_bits(a, n)
  ufc_long *a;
  int n;
  { ufc_long i, j, t, tmp;
    n /= 8;
    for(i = 0; i < n; i++) {
      tmp=0;
      for(j = 0; j < 8; j++) {
        t=8*i+j;
        tmp|=(a[t/24] & BITMASK(t % 24))?bytemask[j]:0;
      }
      (void)printf("%02x ",tmp);
    }
    printf(" ");
  }

static set_bits(v, b)
  ufc_long v;
  ufc_long *b;
  { ufc_long i;
    *b = 0;
    for(i = 0; i < 24; i++) {
      if(v & longmask[8 + i])
        *b |= BITMASK(i);
    }
  }

#endif

/*
 * Silly rewrite of 'bzero'. I do so
 * because some machines don't have
 * bzero and some don't have memset.
 */

STATIC void clearmem(start, cnt)
  char *start;
  int cnt;
  { while(cnt--)
      *start++ = '\0';
  }

static int initialized = 0;

/* lookup a 6 bit value in sbox */

#define s_lookup(i,s) sbox[(i)][(((s)>>4) & 0x2)|((s) & 0x1)][((s)>>1) & 0xf];

/*
 * Initialize unit - may be invoked directly
 * by fcrypt users.
 */

void init_des()
  { int comes_from_bit;
    int bit, sg;
    ufc_long j;
    ufc_long mask1, mask2;

    /*
     * Create the do_pc1 table used
     * to affect pc1 permutation
     * when generating keys
     */
    for(bit = 0; bit < 56; bit++) {
      comes_from_bit  = pc1[bit] - 1;
      mask1 = bytemask[comes_from_bit % 8 + 1];
      mask2 = longmask[bit % 28 + 4];
      for(j = 0; j < 128; j++) {
        if(j & mask1)
          do_pc1[comes_from_bit / 8][bit / 28][j] |= mask2;
      }
    }

    /*
     * Create the do_pc2 table used
     * to affect pc2 permutation when
     * generating keys
     */
    for(bit = 0; bit < 48; bit++) {
      comes_from_bit  = pc2[bit] - 1;
      mask1 = bytemask[comes_from_bit % 7 + 1];
      mask2 = BITMASK(bit % 24);
      for(j = 0; j < 128; j++) {
        if(j & mask1)
          do_pc2[comes_from_bit / 7][j] |= mask2;
      }
    }

    /*
     * Now generate the table used to do combined
     * 32 bit permutation and e expansion
     *
     * We use it because we have to permute 16384 32 bit
     * longs into 48 bit in order to initialize sb.
     *
     * Looping 48 rounds per permutation becomes
     * just too slow...
     *
     */

    clearmem((char*)eperm32tab, sizeof(eperm32tab));

    for(bit = 0; bit < 48; bit++) {
      ufc_long mask1,comes_from;

      comes_from = perm32[esel[bit]-1]-1;
      mask1      = bytemask[comes_from % 8];

      for(j = 256; j--;) {
        if(j & mask1)
          eperm32tab[comes_from / 8][j][bit / 24] |= BITMASK(bit % 24);
      }
    }

    /*
     * Create the sb tables:
     *
     * For each 12 bit segment of an 48 bit intermediate
     * result, the sb table precomputes the two 4 bit
     * values of the sbox lookups done with the two 6
     * bit halves, shifts them to their proper place,
     * sends them through perm32 and finally E expands
     * them so that they are ready for the next
     * DES round.
     *
     */
    for(sg = 0; sg < 4; sg++) {
      int j1, j2;
      int s1, s2;

      for(j1 = 0; j1 < 64; j1++) {
        s1 = s_lookup(2 * sg, j1);
        for(j2 = 0; j2 < 64; j2++) {
          ufc_long to_permute, inx;

          s2         = s_lookup(2 * sg + 1, j2);
          to_permute = ((s1 << 4)  | s2) << (24 - 8 * sg);

#ifdef _UFC_32_
          inx = ((j1 << 6)  | j2) << 1;
          sb[sg][inx  ]  = eperm32tab[0][(to_permute >> 24) & 0xff][0];
          sb[sg][inx+1]  = eperm32tab[0][(to_permute >> 24) & 0xff][1];
          sb[sg][inx  ] |= eperm32tab[1][(to_permute >> 16) & 0xff][0];
          sb[sg][inx+1] |= eperm32tab[1][(to_permute >> 16) & 0xff][1];
          sb[sg][inx  ] |= eperm32tab[2][(to_permute >>  8) & 0xff][0];
          sb[sg][inx+1] |= eperm32tab[2][(to_permute >>  8) & 0xff][1];
          sb[sg][inx  ] |= eperm32tab[3][(to_permute)       & 0xff][0];
          sb[sg][inx+1] |= eperm32tab[3][(to_permute)       & 0xff][1];
#endif
#ifdef _UFC_64_
          inx = ((j1 << 6)  | j2);
          sb[sg][inx]  =
            ((long64)eperm32tab[0][(to_permute >> 24) & 0xff][0] << 32) |
             (long64)eperm32tab[0][(to_permute >> 24) & 0xff][1];
          sb[sg][inx] |=
            ((long64)eperm32tab[1][(to_permute >> 16) & 0xff][0] << 32) |
             (long64)eperm32tab[1][(to_permute >> 16) & 0xff][1];
          sb[sg][inx] |=
            ((long64)eperm32tab[2][(to_permute >>  8) & 0xff][0] << 32) |
             (long64)eperm32tab[2][(to_permute >>  8) & 0xff][1];
          sb[sg][inx] |=
            ((long64)eperm32tab[3][(to_permute)       & 0xff][0] << 32) |
             (long64)eperm32tab[3][(to_permute)       & 0xff][1];
#endif
        }
      }
    }

    /*
     * Create an inverse matrix for esel telling
     * where to plug out bits if undoing it
     */
    for(bit=48; bit--;) {
      e_inverse[esel[bit] - 1     ] = bit;
      e_inverse[esel[bit] - 1 + 32] = bit + 48;
    }

    /*
     * create efp: the matrix used to
     * undo the E expansion and effect final permutation
     */
    clearmem((char*)efp, sizeof efp);
    for(bit = 0; bit < 64; bit++) {
      int o_bit, o_long;
      ufc_long word_value, mask1, mask2;
      int comes_from_f_bit, comes_from_e_bit;
      int comes_from_word, bit_within_word;

      /* See where bit i belongs in the two 32 bit long's */
      o_long = bit / 32; /* 0..1  */
      o_bit  = bit % 32; /* 0..31 */

      /*
       * And find a bit in the e permutated value setting this bit.
       *
       * Note: the e selection may have selected the same bit several
       * times. By the initialization of e_inverse, we only look
       * for one specific instance.
       */
      comes_from_f_bit = final_perm[bit] - 1;         /* 0..63 */
      comes_from_e_bit = e_inverse[comes_from_f_bit]; /* 0..95 */
      comes_from_word  = comes_from_e_bit / 6;        /* 0..15 */
      bit_within_word  = comes_from_e_bit % 6;        /* 0..5  */

      mask1 = longmask[bit_within_word + 26];
      mask2 = longmask[o_bit];

      for(word_value = 64; word_value--;) {
        if(word_value & mask1)
          efp[comes_from_word][word_value][o_long] |= 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 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]);
      if(c < 0 || c > 63)
        c = 0;
      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;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -