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

📄 fcrypt.c

📁 Netscape公司提供的安全套接字层
💻 C
📖 第 1 页 / 共 2 页
字号:
0x01200010,0x01200110,0x01280010,0x01280110,
0x00000200,0x00000300,0x00080200,0x00080300,
0x01000200,0x01000300,0x01080200,0x01080300,
0x00000210,0x00000310,0x00080210,0x00080310,
0x01000210,0x01000310,0x01080210,0x01080310,
0x00200200,0x00200300,0x00280200,0x00280300,
0x01200200,0x01200300,0x01280200,0x01280300,
0x00200210,0x00200310,0x00280210,0x00280310,
0x01200210,0x01200310,0x01280210,0x01280310,
/* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */
0x00000000,0x04000000,0x00040000,0x04040000,
0x00000002,0x04000002,0x00040002,0x04040002,
0x00002000,0x04002000,0x00042000,0x04042000,
0x00002002,0x04002002,0x00042002,0x04042002,
0x00000020,0x04000020,0x00040020,0x04040020,
0x00000022,0x04000022,0x00040022,0x04040022,
0x00002020,0x04002020,0x00042020,0x04042020,
0x00002022,0x04002022,0x00042022,0x04042022,
0x00000800,0x04000800,0x00040800,0x04040800,
0x00000802,0x04000802,0x00040802,0x04040802,
0x00002800,0x04002800,0x00042800,0x04042800,
0x00002802,0x04002802,0x00042802,0x04042802,
0x00000820,0x04000820,0x00040820,0x04040820,
0x00000822,0x04000822,0x00040822,0x04040822,
0x00002820,0x04002820,0x00042820,0x04042820,
0x00002822,0x04002822,0x00042822,0x04042822,
};

/* See ecb_encrypt.c for a pseudo description of these macros. */
#define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
    (b)^=(t),\
    (a)^=((t)<<(n)))

#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
    (a)=(a)^(t)^(t>>(16-(n))))\

static char shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};

static int body();
static int des_set_key();

static int des_set_key(key,schedule)
des_cblock *key;
des_key_schedule schedule;
    {
    register unsigned long c,d,t,s;
    register unsigned char *in;
    register unsigned long *k;
    register int i;

    k=(unsigned long *)schedule;
    in=(unsigned char *)key;

    c2l(in,c);
    c2l(in,d);

    /* I now do it in 47 simple operations :-)
     * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
     * for the inspiration. :-) */
    PERM_OP (d,c,t,4,0x0f0f0f0f);
    HPERM_OP(c,t,-2,0xcccc0000);
    HPERM_OP(d,t,-2,0xcccc0000);
    PERM_OP (d,c,t,1,0x55555555);
    PERM_OP (c,d,t,8,0x00ff00ff);
    PERM_OP (d,c,t,1,0x55555555);
    d=  (((d&0x000000ff)<<16)| (d&0x0000ff00)     |
         ((d&0x00ff0000)>>16)|((c&0xf0000000)>>4));
    c&=0x0fffffff;

    for (i=0; i<ITERATIONS; i++)
        {
        if (shifts2[i])
            { c=((c>>2)|(c<<26)); d=((d>>2)|(d<<26)); }
        else
            { c=((c>>1)|(c<<27)); d=((d>>1)|(d<<27)); }
        c&=0x0fffffff;
        d&=0x0fffffff;
        /* could be a few less shifts but I am to lazy at this
         * point in time to investigate */
        s=  skb[0][ (c    )&0x3f                ]|
            skb[1][((c>> 6)&0x03)|((c>> 7)&0x3c)]|
            skb[2][((c>>13)&0x0f)|((c>>14)&0x30)]|
            skb[3][((c>>20)&0x01)|((c>>21)&0x06) |
                                  ((c>>22)&0x38)];
        t=  skb[4][ (d    )&0x3f                ]|
            skb[5][((d>> 7)&0x03)|((d>> 8)&0x3c)]|
            skb[6][ (d>>15)&0x3f                ]|
            skb[7][((d>>21)&0x0f)|((d>>22)&0x30)];

        /* table contained 0213 4657 */
        *(k++)=((t<<16)|(s&0x0000ffff))&0xffffffff;
        s=     ((s>>16)|(t&0xffff0000));
        
        s=(s<<4)|(s>>28);
        *(k++)=s&0xffffffff;
        }
    return(0);
    }

/******************************************************************
 * modified stuff for crypt.
 ******************************************************************/

/* The changes to this macro may help or hinder, depending on the
 * compiler and the achitecture.  gcc2 always seems to do well :-). 
 * Inspired by Dana How <how@isl.stanford.edu>
 * DO NOT use the alternative version on machines with 8 byte longs.
 */
#ifdef ALT_ECB
#define D_ENCRYPT(L,R,S) \
    v=(R^(R>>16)); \
    u=(v&E0); \
    v=(v&E1); \
    u=((u^(u<<16))^R^s[S  ])<<2; \
    t=(v^(v<<16))^R^s[S+1]; \
    t=(t>>2)|(t<<30); \
    L^= \
    *(unsigned long *)(des_SP+0x0100+((t    )&0xfc))+ \
    *(unsigned long *)(des_SP+0x0300+((t>> 8)&0xfc))+ \
    *(unsigned long *)(des_SP+0x0500+((t>>16)&0xfc))+ \
    *(unsigned long *)(des_SP+0x0700+((t>>24)&0xfc))+ \
    *(unsigned long *)(des_SP+       ((u    )&0xfc))+ \
    *(unsigned long *)(des_SP+0x0200+((u>> 8)&0xfc))+ \
    *(unsigned long *)(des_SP+0x0400+((u>>16)&0xfc))+ \
    *(unsigned long *)(des_SP+0x0600+((u>>24)&0xfc));
#else /* original version */
#define D_ENCRYPT(L,R,S)    \
    v=(R^(R>>16)); \
    u=(v&E0); \
    v=(v&E1); \
    u=(u^(u<<16))^R^s[S  ]; \
    t=(v^(v<<16))^R^s[S+1]; \
    t=(t>>4)|(t<<28); \
    L^= SPtrans[1][(t    )&0x3f]| \
        SPtrans[3][(t>> 8)&0x3f]| \
        SPtrans[5][(t>>16)&0x3f]| \
        SPtrans[7][(t>>24)&0x3f]| \
        SPtrans[0][(u    )&0x3f]| \
        SPtrans[2][(u>> 8)&0x3f]| \
        SPtrans[4][(u>>16)&0x3f]| \
        SPtrans[6][(u>>24)&0x3f];
#endif

unsigned char con_salt[128]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
0x0A,0x0B,0x05,0x06,0x07,0x08,0x09,0x0A,
0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,
0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,
0x1B,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,
0x23,0x24,0x25,0x20,0x21,0x22,0x23,0x24,
0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,
0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,
0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,
0x3D,0x3E,0x3F,0x00,0x00,0x00,0x00,0x00,
};

unsigned char cov_2char[64]={
0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
};

char *crypt(buf,salt)
char *buf;
char *salt;
    {
    unsigned int i,j,x,y;
    unsigned long Eswap0=0,Eswap1=0;
    unsigned long out[2],ll;
    des_cblock key;
    des_key_schedule ks;
    static unsigned char buff[20];
    unsigned char bb[9];
    unsigned char *b=bb;
    unsigned char c,u;

    /* eay 25/08/92
     * If you call crypt("pwd","*") as often happens when you
     * have * as the pwd field in /etc/passwd, the function
     * returns *\0XXXXXXXXX
     * The \0 makes the string look like * so the pwd "*" would
     * crypt to "*".  This was found when replacing the crypt in
     * our shared libraries.  People found that the disbled
     * accounts effectivly had no passwd :-(. */
    if (salt[0] == '\0') salt[0]='A';
    if (salt[1] == '\0') salt[1]='A';
    x=buff[0]=salt[0];
    Eswap0=con_salt[x];
    x=buff[1]=salt[1];
    Eswap1=con_salt[x]<<4;

    for (i=0; i<8; i++)
        {
        c= *(buf++);
        if (!c) break;
        key[i]=(c<<1);
        }
    for (; i<8; i++)
        key[i]=0;

    des_set_key((des_cblock *)(key),ks);
    body(&out[0],&out[1],ks,Eswap0,Eswap1);

    ll=out[0]; l2c(ll,b);
    ll=out[1]; l2c(ll,b);
    y=0;
    u=0x80;
    bb[8]=0;
    for (i=2; i<13; i++)
        {
        c=0;
        for (j=0; j<6; j++)
            {
            c<<=1;
            if (bb[y] & u) c|=1;
            u>>=1;
            if (!u)
                {
                y++;
                u=0x80;
                }
            }
        buff[i]=cov_2char[c];
        }
    return((char *)buff);
    }

static int body(out0,out1,ks,Eswap0,Eswap1)
unsigned long *out0,*out1;
des_key_schedule *ks;
unsigned long Eswap0,Eswap1;
    {
    register unsigned long l,r,t,u,v;
#ifdef ALT_ECB
    register unsigned char *des_SP=(unsigned char *)SPtrans;
#endif
    register unsigned long *s;
    register int i,j;
    register unsigned long E0,E1;

    l=0;
    r=0;

    s=(unsigned long *)ks;
    E0=Eswap0;
    E1=Eswap1;

    for (j=0; j<25; j++)
        {
        for (i=0; i<(ITERATIONS*2); i+=4)
            {
            D_ENCRYPT(l,r,  i); /*  1 */
            D_ENCRYPT(r,l,  i+2);   /*  2 */
            }
        t=l;
        l=r;
        r=t;
        }
    t=r;
    r=(l>>1)|(l<<31);
    l=(t>>1)|(t<<31);
    /* clear the top bits on machines with 8byte longs */
    l&=0xffffffff;
    r&=0xffffffff;

    PERM_OP(r,l,t, 1,0x55555555);
    PERM_OP(l,r,t, 8,0x00ff00ff);
    PERM_OP(r,l,t, 2,0x33333333);
    PERM_OP(l,r,t,16,0x0000ffff);
    PERM_OP(r,l,t, 4,0x0f0f0f0f);

    *out0=l;
    *out1=r;
    return(0);
    }

⌨️ 快捷键说明

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