📄 des.cpp
字号:
if (shifts2[i])
{ c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
else
{ c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
c&=0x0fffffffL;
d&=0x0fffffffL;
/* could be a few less shifts but I am to lazy at this
* point in time to investigate */
s= des_skb[0][ (c )&0x3f ]|
des_skb[1][((c>> 6L)&0x03)|((c>> 7L)&0x3c)]|
des_skb[2][((c>>13L)&0x0f)|((c>>14L)&0x30)]|
des_skb[3][((c>>20L)&0x01)|((c>>21L)&0x06) |
((c>>22L)&0x38)];
t= des_skb[4][ (d )&0x3f ]|
des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
des_skb[6][ (d>>15L)&0x3f ]|
des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
/* table contained 0213 4657 */
t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
*(k++)=i_ROTATE(t2,30)&0xffffffffL;
t2=((s>>16L)|(t&0xffff0000L));
*(k++)=i_ROTATE(t2,26)&0xffffffffL;
}
}
void i_DES_encrypt3(i_DES_LONG *data, i_DES_key_schedule *ks1, i_DES_key_schedule *ks2, i_DES_key_schedule *ks3)
{
register i_DES_LONG l, r;
l = data[0];
r = data[1];
IP(l, r);
data[0] = l;
data[1] = r;
i_DES_encrypt2((i_DES_LONG *)data, ks1, i_DES_ENCRYPT);
i_DES_encrypt2((i_DES_LONG *)data, ks2, i_DES_DECRYPT);
i_DES_encrypt2((i_DES_LONG *)data, ks3, i_DES_ENCRYPT);
l = data[0];
r = data[1];
FP(r, l);
data[0] = l;
data[1] = r;
}
void i_DES_decrypt3(i_DES_LONG *data, i_DES_key_schedule *ks1, i_DES_key_schedule *ks2, i_DES_key_schedule *ks3)
{
register i_DES_LONG l, r;
l = data[0];
r = data[1];
IP(l, r);
data[0] = l;
data[1] = r;
i_DES_encrypt2((i_DES_LONG *)data, ks3, i_DES_DECRYPT);
i_DES_encrypt2((i_DES_LONG *)data, ks2, i_DES_ENCRYPT);
i_DES_encrypt2((i_DES_LONG *)data, ks1, i_DES_DECRYPT);
l = data[0];
r = data[1];
FP(r, l);
data[0] = l;
data[1] = r;
}
void i_DES_encrypt2(i_DES_LONG *data, i_DES_key_schedule *ks, int enc)
{
register i_DES_LONG l, r, t, u;
#ifndef i_DES_UNROLL
register int i;
#endif
register i_DES_LONG *s;
r = data[0];
l = data[1];
/* Things have been modified so that the initial rotate is
* done outside the loop. This required the
* DES_SPtrans values in sp.h to be rotated 1 bit to the right.
* One perl script later and things have a 5% speed up on a sparc2.
* Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
* for pointing this out. */
/* clear the top bits on machines with 8byte longs */
r = i_ROTATE(r, 29) & 0xffffffffL;
l = i_ROTATE(l, 29) & 0xffffffffL;
s = ks->ks->deslong;
/* I don't know if it is worth the effort of loop unrolling the
* inner loop */
if (enc)
{
#ifdef i_DES_UNROLL
D_ENCRYPT(l, r, 0); /* 1 */
D_ENCRYPT(r, l, 2); /* 2 */
D_ENCRYPT(l, r, 4); /* 3 */
D_ENCRYPT(r, l, 6); /* 4 */
D_ENCRYPT(l, r, 8); /* 5 */
D_ENCRYPT(r, l, 10); /* 6 */
D_ENCRYPT(l, r, 12); /* 7 */
D_ENCRYPT(r, l, 14); /* 8 */
D_ENCRYPT(l, r, 16); /* 9 */
D_ENCRYPT(r, l, 18); /* 10 */
D_ENCRYPT(l, r, 20); /* 11 */
D_ENCRYPT(r, l, 22); /* 12 */
D_ENCRYPT(l, r, 24); /* 13 */
D_ENCRYPT(r, l, 26); /* 14 */
D_ENCRYPT(l, r, 28); /* 15 */
D_ENCRYPT(r, l, 30); /* 16 */
#else
for (i = 0;i < 32; i += 8)
{
D_ENCRYPT(l, r, i + 0); /* 1 */
D_ENCRYPT(r, l, i + 2); /* 2 */
D_ENCRYPT(l, r, i + 4); /* 3 */
D_ENCRYPT(r, l, i + 6); /* 4 */
}
#endif
}
else
{
#ifdef i_DES_UNROLL
D_ENCRYPT(l, r, 30); /* 16 */
D_ENCRYPT(r, l, 28); /* 15 */
D_ENCRYPT(l, r, 26); /* 14 */
D_ENCRYPT(r, l, 24); /* 13 */
D_ENCRYPT(l, r, 22); /* 12 */
D_ENCRYPT(r, l, 20); /* 11 */
D_ENCRYPT(l, r, 18); /* 10 */
D_ENCRYPT(r, l, 16); /* 9 */
D_ENCRYPT(l, r, 14); /* 8 */
D_ENCRYPT(r, l, 12); /* 7 */
D_ENCRYPT(l, r, 10); /* 6 */
D_ENCRYPT(r, l, 8); /* 5 */
D_ENCRYPT(l, r, 6); /* 4 */
D_ENCRYPT(r, l, 4); /* 3 */
D_ENCRYPT(l, r, 2); /* 2 */
D_ENCRYPT(r, l, 0); /* 1 */
#else
for (i = 30;i > 0;i -= 8)
{
D_ENCRYPT(l, r, i - 0); /* 16 */
D_ENCRYPT(r, l, i - 2); /* 15 */
D_ENCRYPT(l, r, i - 4); /* 14 */
D_ENCRYPT(r, l, i - 6); /* 13 */
}
#endif
}
/* rotate and clear the top bits on machines with 8byte longs */
data[0] = i_ROTATE(l, 3) & 0xffffffffL;
data[1] = i_ROTATE(r, 3) & 0xffffffffL;
l = r = t = u = 0;
}
int i_DES_random_key(i_DES_cblock *ret)
{
srand( time(NULL) );
do
{
for(unsigned int i=0;i<sizeof(i_DES_cblock);i++) ((unsigned char *)ret)[i] = (unsigned char)rand();
}
while (i_DES_is_weak_key(ret));
i_DES_set_odd_parity(ret);
return 1;
}
void i_DES_set_odd_parity(i_DES_cblock *key)
{
unsigned int i;
for (i=0; i<i_DES_KEY_SZ; i++)
(*key)[i] = odd_parity[(*key)[i]];
}
void i_DES_ecb3_encrypt(i_const_DES_cblock *input, i_DES_cblock *output, i_DES_key_schedule *ks1, i_DES_key_schedule *ks2, i_DES_key_schedule *ks3, int enc)
{
register i_DES_LONG l0, l1;
i_DES_LONG ll[2];
const unsigned char *in = &(*input)[0];
unsigned char *out = &(*output)[0];
c2l(in,l0);
c2l(in,l1);
ll[0] = l0;
ll[1] = l1;
if( enc )
i_DES_encrypt3(ll, ks1, ks2, ks3);
else
i_DES_decrypt3(ll, ks1, ks2, ks3);
l0 = ll[0];
l1 = ll[1];
l2c(l0, out);
l2c(l1, out);
}
void i_DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output, long length, i_DES_key_schedule *ks1, i_DES_key_schedule *ks2, i_DES_key_schedule *ks3, i_DES_cblock *ivec, int enc)
{
register i_DES_LONG tin0,tin1;
register i_DES_LONG tout0,tout1,xor0,xor1;
register const unsigned char *in;
unsigned char *out;
register long l=length;
i_DES_LONG tin[2];
unsigned char *iv;
in=input;
out=output;
iv = &(*ivec)[0];
if (enc)
{
c2l(iv,tout0);
c2l(iv,tout1);
for (l-=8; l>=0; l-=8)
{
c2l(in,tin0);
c2l(in,tin1);
tin0^=tout0;
tin1^=tout1;
tin[0]=tin0;
tin[1]=tin1;
i_DES_encrypt3((i_DES_LONG *)tin,ks1,ks2,ks3);
tout0=tin[0];
tout1=tin[1];
l2c(tout0,out);
l2c(tout1,out);
}
if (l != -8)
{
c2ln(in,tin0,tin1,l+8);
tin0^=tout0;
tin1^=tout1;
tin[0]=tin0;
tin[1]=tin1;
i_DES_encrypt3((i_DES_LONG *)tin,ks1,ks2,ks3);
tout0=tin[0];
tout1=tin[1];
l2c(tout0,out);
l2c(tout1,out);
}
iv = &(*ivec)[0];
l2c(tout0,iv);
l2c(tout1,iv);
}
else
{
register i_DES_LONG t0,t1;
c2l(iv,xor0);
c2l(iv,xor1);
for (l-=8; l>=0; l-=8)
{
c2l(in,tin0);
c2l(in,tin1);
t0=tin0;
t1=tin1;
tin[0]=tin0;
tin[1]=tin1;
i_DES_decrypt3((i_DES_LONG *)tin,ks1,ks2,ks3);
tout0=tin[0];
tout1=tin[1];
tout0^=xor0;
tout1^=xor1;
l2c(tout0,out);
l2c(tout1,out);
xor0=t0;
xor1=t1;
}
if (l != -8)
{
c2l(in,tin0);
c2l(in,tin1);
t0=tin0;
t1=tin1;
tin[0]=tin0;
tin[1]=tin1;
i_DES_decrypt3((i_DES_LONG *)tin,ks1,ks2,ks3);
tout0=tin[0];
tout1=tin[1];
tout0^=xor0;
tout1^=xor1;
l2cn(tout0,tout1,out,l+8);
xor0=t0;
xor1=t1;
}
iv = &(*ivec)[0];
l2c(xor0,iv);
l2c(xor1,iv);
}
tin0=tin1=tout0=tout1=xor0=xor1=0;
tin[0]=tin[1]=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -