📄 fcrypt.c
字号:
0x00020000L,0x08020000L,0x00020008L,0x08020008L,
0x00020400L,0x08020400L,0x00020408L,0x08020408L,
0x00000001L,0x08000001L,0x00000009L,0x08000009L,
0x00000401L,0x08000401L,0x00000409L,0x08000409L,
0x00020001L,0x08020001L,0x00020009L,0x08020009L,
0x00020401L,0x08020401L,0x00020409L,0x08020409L,
0x02000000L,0x0A000000L,0x02000008L,0x0A000008L,
0x02000400L,0x0A000400L,0x02000408L,0x0A000408L,
0x02020000L,0x0A020000L,0x02020008L,0x0A020008L,
0x02020400L,0x0A020400L,0x02020408L,0x0A020408L,
0x02000001L,0x0A000001L,0x02000009L,0x0A000009L,
0x02000401L,0x0A000401L,0x02000409L,0x0A000409L,
0x02020001L,0x0A020001L,0x02020009L,0x0A020009L,
0x02020401L,0x0A020401L,0x02020409L,0x0A020409L,
},{
/* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */
0x00000000L,0x00000100L,0x00080000L,0x00080100L,
0x01000000L,0x01000100L,0x01080000L,0x01080100L,
0x00000010L,0x00000110L,0x00080010L,0x00080110L,
0x01000010L,0x01000110L,0x01080010L,0x01080110L,
0x00200000L,0x00200100L,0x00280000L,0x00280100L,
0x01200000L,0x01200100L,0x01280000L,0x01280100L,
0x00200010L,0x00200110L,0x00280010L,0x00280110L,
0x01200010L,0x01200110L,0x01280010L,0x01280110L,
0x00000200L,0x00000300L,0x00080200L,0x00080300L,
0x01000200L,0x01000300L,0x01080200L,0x01080300L,
0x00000210L,0x00000310L,0x00080210L,0x00080310L,
0x01000210L,0x01000310L,0x01080210L,0x01080310L,
0x00200200L,0x00200300L,0x00280200L,0x00280300L,
0x01200200L,0x01200300L,0x01280200L,0x01280300L,
0x00200210L,0x00200310L,0x00280210L,0x00280310L,
0x01200210L,0x01200310L,0x01280210L,0x01280310L,
},{
/* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */
0x00000000L,0x04000000L,0x00040000L,0x04040000L,
0x00000002L,0x04000002L,0x00040002L,0x04040002L,
0x00002000L,0x04002000L,0x00042000L,0x04042000L,
0x00002002L,0x04002002L,0x00042002L,0x04042002L,
0x00000020L,0x04000020L,0x00040020L,0x04040020L,
0x00000022L,0x04000022L,0x00040022L,0x04040022L,
0x00002020L,0x04002020L,0x00042020L,0x04042020L,
0x00002022L,0x04002022L,0x00042022L,0x04042022L,
0x00000800L,0x04000800L,0x00040800L,0x04040800L,
0x00000802L,0x04000802L,0x00040802L,0x04040802L,
0x00002800L,0x04002800L,0x00042800L,0x04042800L,
0x00002802L,0x04002802L,0x00042802L,0x04042802L,
0x00000820L,0x04000820L,0x00040820L,0x04040820L,
0x00000822L,0x04000822L,0x00040822L,0x04040822L,
0x00002820L,0x04002820L,0x00042820L,0x04042820L,
0x00002822L,0x04002822L,0x00042822L,0x04042822L,
} };
/* 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 const int CONSTANT_DATA shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
#ifdef PROTO
static int body(unsigned long *out0, unsigned long *out1,
des_key_schedule ks, unsigned long Eswap0, unsigned long Eswap1);
static int des_set_key(des_cblock (*key), des_key_schedule schedule);
#else
static int body();
static int des_set_key();
#endif
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,0x0f0f0f0fL);
HPERM_OP(c,t,-2,0xcccc0000L);
HPERM_OP(d,t,-2,0xcccc0000L);
PERM_OP (d,c,t,1,0x55555555L);
PERM_OP (c,d,t,8,0x00ff00ffL);
PERM_OP (d,c,t,1,0x55555555L);
d= (((d&0x000000ffL)<<16)| (d&0x0000ff00L) |
((d&0x00ff0000L)>>16)|((c&0xf0000000L)>>4));
c&=0x0fffffffL;
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&=0x0fffffffL;
d&=0x0fffffffL;
/* 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>> 6L)&0x03)|((c>> 7L)&0x3c)]|
skb[2][((c>>13L)&0x0f)|((c>>14L)&0x30)]|
skb[3][((c>>20L)&0x01)|((c>>21L)&0x06) |
((c>>22L)&0x38)];
t= skb[4][ (d )&0x3f ]|
skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
skb[6][ (d>>15L)&0x3f ]|
skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
/* table contained 0213 4657 */
*(k++)=((t<<16)|(s&0x0000ffffL))&0xffffffffL;
s= ((s>>16)|(t&0xffff0000L));
s=(s<<4)|(s>>28);
*(k++)=s&0xffffffffL;
}
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 DES_USE_PTR
#define D_ENCRYPT(L,R,S) \
t=(R^(R>>16)); \
u=(t&E0); \
t=(t&E1); \
u=((u^(u<<16))^R^s[S ])<<2; \
t=(t^(t<<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) \
t=(R^(R>>16)); \
u=(t&E0); \
t=(t&E1); \
u=(u^(u<<16))^R^s[S ]; \
t=(t^(t<<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
static unsigned const char CONSTANT_DATA 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,
};
static unsigned const char CONSTANT_DATA 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
};
#ifdef PROTO
#ifdef PERL5
char *des_crypt(char *buf,char *salt);
#else
char *crypt(char *buf,char *salt);
#endif
#else
#ifdef PERL5
char *des_crypt();
#else
char *crypt();
#endif
#endif
#ifdef PERL5
char *des_crypt(buf,salt)
#else
char *crypt(buf,salt)
#endif
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 :-(. */
x=buff[0]=((salt[0] == '\0')?'A':salt[0]);
Eswap0=con_salt[x];
x=buff[1]=((salt[1] == '\0')?'A':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];
}
buff[13]='\0';
return((char *)buff);
}
static int body(out0, out1, ks, Eswap0, Eswap1)
unsigned long *out0;
unsigned long *out1;
des_key_schedule ks;
unsigned long Eswap0;
unsigned long Eswap1;
{
register unsigned long l,r,t,u;
#ifdef DES_USE_PTR
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>>1L)|(l<<31L);
l=(t>>1L)|(t<<31L);
/* clear the top bits on machines with 8byte longs */
l&=0xffffffffL;
r&=0xffffffffL;
PERM_OP(r,l,t, 1,0x55555555L);
PERM_OP(l,r,t, 8,0x00ff00ffL);
PERM_OP(r,l,t, 2,0x33333333L);
PERM_OP(l,r,t,16,0x0000ffffL);
PERM_OP(r,l,t, 4,0x0f0f0f0fL);
*out0=l;
*out1=r;
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -