📄 desmain.c
字号:
#include <std.h>
#include <stdio.h>
#include "des.h"
#include "spr.h"
#include "podd.h"
#include "sk.h"
//#pragma CODE_SECTION(des3_cbc_encrypt, ".text:des3_cbc_encrypt");
//#pragma CODE_SECTION(des3_cbc_decrypt, ".text:des3_cbc_decrypt");
#pragma CODE_SECTION(des_encrypt_core, ".text:des_encrypt_core");
//#pragma CODE_SECTION(des3_encrypt_block, ".text:des3_encrypt_block");
//#pragma CODE_SECTION(des_set_odd_parity, ".text:des_set_odd_parity");
#pragma CODE_SECTION(des_is_weak_key, ".text:des_is_weak_key");
#pragma CODE_SECTION(des_set_key, ".text:des_set_key");
#define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n))))
#define IP(l,r) \
{ \
register DES_LONG tt; \
PERM_OP(r,l,tt, 4,0x0f0f0f0f); \
PERM_OP(l,r,tt,16,0x0000ffff); \
PERM_OP(r,l,tt, 2,0x33333333); \
PERM_OP(l,r,tt, 8,0x00ff00ff); \
PERM_OP(r,l,tt, 1,0x55555555); \
}
#define FP(l,r) \
{ \
register DES_LONG tt; \
PERM_OP(l,r,tt, 1,0x55555555); \
PERM_OP(r,l,tt, 8,0x00ff00ff); \
PERM_OP(l,r,tt, 2,0x33333333); \
PERM_OP(r,l,tt,16,0x0000ffff); \
PERM_OP(l,r,tt, 4,0x0f0f0f0f); \
}
#define EXTRACT(x,l,r) (x << l) >> r
#define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
(b)^=(t),\
(a)^=((t)<<(n)))
#define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
(b)^=(t),\
(a)^=((t)<<(n)))
#define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
l|=((DES_LONG)(*((c)++)))<< 8, \
l|=((DES_LONG)(*((c)++)))<<16, \
l|=((DES_LONG)(*((c)++)))<<24)
#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
(a)=(a)^(t)^(t>>(16-(n))))
/*volatile des_cblock enc_key1={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
volatile des_cblock enc_key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
volatile des_cblock enc_key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
volatile des_cblock dec_key1={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
volatile des_cblock dec_key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
volatile des_cblock dec_key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
*/
unsigned char ivec[8] ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
unsigned char ivec_copy[8] ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
des_key_schedule sch[3];
des_key_schedule dec_sch[3];
int des_check_key=0;
static int check_parity(key)
des_cblock (*key);
{
int i;
for (i=0; i<DES_KEY_SZ; i++)
{
if ((*key)[i] != odd_parity[(*key)[i]])
return(0);
}
return(1);
}
int memcmp(const void *cs, const void *ct, size_t n)
{
if (n)
{
const unsigned char *mem1 = (unsigned char *)cs - 1;
const unsigned char *mem2 = (unsigned char *)ct - 1;
int cp;
while ((cp = *++mem1) == *++mem2 && --n);
return cp - *mem2;
}
return 0;
}
#define NUM_WEAK_KEY 16
static des_cblock weak_keys[NUM_WEAK_KEY]={
/* weak keys */
{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
{0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
{0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
{0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
/* semi-weak keys */
{0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
{0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
{0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
{0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
{0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
{0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
{0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
{0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
{0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
{0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
{0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
{0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
int des_is_weak_key(key)
des_cblock (*key);
{
int i;
for (i=0; i<NUM_WEAK_KEY; i++)
if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);
return(0);
}
int des_set_key(key, schedule)
des_cblock (*key);
volatile des_key_schedule schedule;
{
register DES_LONG c,d,t,s,t2;
register unsigned char *in;
register DES_LONG *k;
register int i;
if (des_check_key)
{
if (!check_parity(key))
return(-1);
if (des_is_weak_key(key))
return(-2);
}
k=(DES_LONG *)schedule;
in=(unsigned char *)key;
c2l(in,c);
c2l(in,d);
/* do PC1 in 60 simple operations */
/* PERM_OP(d,c,t,4,0x0f0f0f0fL);
HPERM_OP(c,t,-2, 0xcccc0000L);
HPERM_OP(c,t,-1, 0xaaaa0000L);
HPERM_OP(c,t, 8, 0x00ff0000L);
HPERM_OP(c,t,-1, 0xaaaa0000L);
HPERM_OP(d,t,-8, 0xff000000L);
HPERM_OP(d,t, 8, 0x00ff0000L);
HPERM_OP(d,t, 2, 0x33330000L);
d=((d&0x00aa00aaL)<<7L)|((d&0x55005500L)>>7L)|(d&0xaa55aa55L);
d=(d>>8)|((c&0xf0000000L)>>4);
c&=0x0fffffffL; */
/* 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&0x0000ff00L) |
((d&0x00ff0000)>>16)|((c&0xf0000000)>>4));
c&=0x0fffffff;
c=(((c>>1)|(c<<27))&0x0fffffff); /* shifts2: 1-2 */
d=(((d>>1)|(d<<27))&0x0fffffff);
s= des_skb[0][ (c )&0x3f ]|
des_skb[1][((c>> 6)&0x03)|((c>> 7)&0x3c)]|
des_skb[2][((c>>13)&0x0f)|((c>>14)&0x30)]|
des_skb[3][((c>>20)&0x01)|((c>>21)&0x06) |
((c>>22)&0x38)];
t= des_skb[4][ (d )&0x3f ]|
des_skb[5][((d>> 7)&0x03)|((d>> 8)&0x3c)]|
des_skb[6][ (d>>15)&0x3f ]|
des_skb[7][((d>>21)&0x0f)|((d>>22)&0x30)];
/* table contained 0213 4657 */
t2=((t<<16)|(s&0x0000ffff))&0xffffffff;
*(k++)=ROTATE(t2,30)&0xffffffff;
t2=((s>>16)|(t&0xffff0000));
*(k++)=ROTATE(t2,26)&0xffffffff;
c=(((c>>1)|(c<<27))&0x0fffffff); /* shifts2: 1-2 */
d=(((d>>1)|(d<<27))&0x0fffffff);
s= des_skb[0][ (c )&0x3f ]|
des_skb[1][((c>> 6)&0x03)|((c>> 7)&0x3c)]|
des_skb[2][((c>>13)&0x0f)|((c>>14)&0x30)]|
des_skb[3][((c>>20)&0x01)|((c>>21)&0x06) |
((c>>22)&0x38)];
t= des_skb[4][ (d )&0x3f ]|
des_skb[5][((d>> 7)&0x03)|((d>> 8)&0x3c)]|
des_skb[6][ (d>>15)&0x3f ]|
des_skb[7][((d>>21)&0x0f)|((d>>22)&0x30)];
t2=((t<<16)|(s&0x0000ffff))&0xffffffff;
*(k++)=ROTATE(t2,30)&0xffffffff;
t2=((s>>16)|(t&0xffff0000));
*(k++)=ROTATE(t2,26)&0xffffffff;
c=(((c>>1)|(c<<27))&0x0fffffff); /* shifts2: 1-2 */
d=(((d>>1)|(d<<27))&0x0fffffff);
s= des_skb[0][ (c )&0x3f ]|
des_skb[1][((c>> 6)&0x03)|((c>> 7)&0x3c)]|
des_skb[2][((c>>13)&0x0f)|((c>>14)&0x30)]|
des_skb[3][((c>>20)&0x01)|((c>>21)&0x06) |
((c>>22)&0x38)];
t= des_skb[4][ (d )&0x3f ]|
des_skb[5][((d>> 7)&0x03)|((d>> 8)&0x3c)]|
des_skb[6][ (d>>15)&0x3f ]|
des_skb[7][((d>>21)&0x0f)|((d>>22)&0x30)];
t2=((t<<16)|(s&0x0000ffff))&0xffffffff;
*(k++)=ROTATE(t2,30)&0xffffffff;
t2=((s>>16)|(t&0xffff0000));
*(k++)=ROTATE(t2,26)&0xffffffff;
for (i=0; i<6; i++)
{
c=(((c>>2)|(c<<26))&0x0fffffff); /* shifts2: 3-8 */
d=(((d>>2)|(d<<26))&0x0fffffff);
s= des_skb[0][ (c )&0x3f ]|
des_skb[1][((c>> 6)&0x03)|((c>> 7)&0x3c)]|
des_skb[2][((c>>13)&0x0f)|((c>>14)&0x30)]|
des_skb[3][((c>>20)&0x01)|((c>>21)&0x06) |
((c>>22)&0x38)];
t= des_skb[4][ (d )&0x3f ]|
des_skb[5][((d>> 7)&0x03)|((d>> 8)&0x3c)]|
des_skb[6][ (d>>15)&0x3f ]|
des_skb[7][((d>>21)&0x0f)|((d>>22)&0x30)];
/* table contained 0213 4657 */
t2=((t<<16)|(s&0x0000ffff))&0xffffffff;
*(k++)=ROTATE(t2,30)&0xffffffff;
t2=((s>>16)|(t&0xffff0000));
*(k++)=ROTATE(t2,26)&0xffffffff;
}
c=(((c>>1)|(c<<27))&0x0fffffff); /* shifts2: 9 */
d=(((d>>1)|(d<<27))&0x0fffffff);
s= des_skb[0][ (c )&0x3f ]|
des_skb[1][((c>> 6)&0x03)|((c>> 7)&0x3c)]|
des_skb[2][((c>>13)&0x0f)|((c>>14)&0x30)]|
des_skb[3][((c>>20)&0x01)|((c>>21)&0x06) |
((c>>22)&0x38)];
t= des_skb[4][ (d )&0x3f ]|
des_skb[5][((d>> 7)&0x03)|((d>> 8)&0x3c)]|
des_skb[6][ (d>>15)&0x3f ]|
des_skb[7][((d>>21)&0x0f)|((d>>22)&0x30)];
/* table contained 0213 4657 */
t2=((t<<16)|(s&0x0000ffff))&0xffffffff;
*(k++)=ROTATE(t2,30)&0xffffffff;
t2=((s>>16)|(t&0xffff0000));
*(k++)=ROTATE(t2,26)&0xffffffff;
for (i=0; i<6; i++)
{
c=(((c>>2)|(c<<26))&0x0fffffff); /* shifts2: 10-15 */
d=(((d>>2)|(d<<26))&0x0fffffff);
s= des_skb[0][ (c )&0x3f ]|
des_skb[1][((c>> 6)&0x03)|((c>> 7)&0x3c)]|
des_skb[2][((c>>13)&0x0f)|((c>>14)&0x30)]|
des_skb[3][((c>>20)&0x01)|((c>>21)&0x06) |
((c>>22)&0x38)];
t= des_skb[4][ (d )&0x3f ]|
des_skb[5][((d>> 7)&0x03)|((d>> 8)&0x3c)]|
des_skb[6][ (d>>15)&0x3f ]|
des_skb[7][((d>>21)&0x0f)|((d>>22)&0x30)];
t2=((t<<16)|(s&0x0000ffff))&0xffffffff;
*(k++)=ROTATE(t2,30)&0xffffffff;
t2=((s>>16)|(t&0xffff0000));
*(k++)=ROTATE(t2,26)&0xffffffff;
}
c=(((c>>1)|(c<<27))&0x0fffffff); /* shifts2: 16 */
d=(((d>>1)|(d<<27))&0x0fffffff);
s= des_skb[0][ (c )&0x3f ]|
des_skb[1][((c>> 6)&0x03)|((c>> 7)&0x3c)]|
des_skb[2][((c>>13)&0x0f)|((c>>14)&0x30)]|
des_skb[3][((c>>20)&0x01)|((c>>21)&0x06) |
((c>>22)&0x38)];
t= des_skb[4][ (d )&0x3f ]|
des_skb[5][((d>> 7)&0x03)|((d>> 8)&0x3c)]|
des_skb[6][ (d>>15)&0x3f ]|
des_skb[7][((d>>21)&0x0f)|((d>>22)&0x30)];
t2=((t<<16)|(s&0x0000ffff))&0xffffffff;
*(k++)=ROTATE(t2,30)&0xffffffff;
t2=((s>>16)|(t&0xffff0000));
*(k++)=ROTATE(t2,26)&0xffffffff;
return(0);
}
void des_encrypt_core(data, ks)
DES_LONG *data;
des_key_schedule ks;
{
DES_LONG l,r,t,u,temp, *s;
DES_LONG lor1, lor2, lor3, lor4;
unsigned int u1, u2, u3, u4;
unsigned int t1, t2, t3, t4;
int i;
/* 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.
* Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
* for pointing this out. */
s=(DES_LONG *)ks;
r= data[0];
l= data[1];
for (i=0; i<32; i+=2)
{
u=r^s[i ];
t=r^s[i+1];
t=ROTATE(t,4);
u1 = EXTRACT(u, 24, 26);
u2 = EXTRACT(u, 16, 26);
u3 = EXTRACT(u, 8, 26);
u4 = EXTRACT(u, 0, 26);
lor1 = des_SPtrans[0][u1];
#ifdef DUAL_BANK
lor2 = des_SPtrans2[4][u3];
#else
lor2 = des_SPtrans[4][u3];
#endif
lor1 ^= des_SPtrans[2][u2];
#ifdef DUAL_BANK
lor2 ^= des_SPtrans2[6][u4];
#else
lor2 ^= des_SPtrans[6][u4];
#endif
t1 = EXTRACT(t, 24, 26);
t2 = EXTRACT(t, 16, 26);
t3 = EXTRACT(t, 8, 26);
t4 = EXTRACT(t, 0, 26);
lor3 = des_SPtrans[1][t1];
#ifdef DUAL_BANK
lor4 = des_SPtrans2[5][t3];
#else
lor4 = des_SPtrans[5][t3];
#endif
lor3 ^= des_SPtrans[3][t2];
#ifdef DUAL_BANK
lor4 ^= des_SPtrans2[7][t4];
#else
lor4 ^= des_SPtrans[7][t4];
#endif
lor1 ^= lor2;
lor3 ^= lor4;
lor1 ^= lor3;
temp = l ^ lor1;
l = r;
r = temp;
}
data[0] = l;
data[1] = r;
}
void des_encrypt_block(data, ks)
DES_LONG *data;
des_key_schedule ks;
{
register DES_LONG l,r,temp;
l=data[0];
r=data[1];
IP(l,r);
data[0]=ROTATE(l,29);
data[1]=ROTATE(r,29);
des_encrypt_core(data, ks);
temp=data[0];
l=ROTATE(temp,3);
temp=data[1];
r=ROTATE(temp,3);
FP(r,l);
data[0]=l;
data[1]=r;
}
/******************************************************************************/
/* des_encrypt_block_3ch */
/* This is the Data Encryption Standard (DES) block encryption */
/* routine. des_encrypt_block_3ch(...) encrypts three 64 bit blocks */
/* of data using a signle 56 bit key. This key is individually */
/* specifiable for each of the three blocks of data. This algorithm */
/* performs the initial permutations and rotations of this data */
/* which are characteristic of the DES algorithm before calling the */
/* core DES routine (des_encrypt_core_3ch). These rotations and */
/* permutations do not increase the security of the algorithm and */
/* have predominantly historical value. They are included because */
/* they are part of the definition of DES but may be removed to */
/* increase performance. */
/* */
/* This algorithm is exactly equivalent to des_encrypt_block above, */
/* but with three of everything. It has been modified to handle */
/* three channels in parallel as an optimization. */
/******************************************************************************/
void des_encrypt_block_3ch(data1, ks1, data2, ks2, data3, ks3)
DES_LONG *data1;
DES_LONG *data2;
DES_LONG *data3;
des_key_schedule ks1;
des_key_schedule ks2;
des_key_schedule ks3;
{
register DES_LONG l1,r1,l2,r2,l3,r3, temp;
l1=data1[0];
r1=data1[1];
IP(l1,r1);
l2=data2[0];
r2=data2[1];
IP(l2,r2);
l3=data3[0];
r3=data3[1];
IP(l3,r3);
data1[0]=ROTATE(l1,29);
data1[1]=ROTATE(r1,29);
data2[0]=ROTATE(l2,29);
data2[1]=ROTATE(r2,29);
data3[0]=ROTATE(l3,29);
data3[1]=ROTATE(r3,29);
des_encrypt_core_3ch(data1, ks1, data2, ks2, data3, ks3);
temp=data1[0];
l1=ROTATE(temp,3);
temp=data1[1];
r1=ROTATE(temp,3);
temp=data2[0];
l2=ROTATE(temp,3);
temp=data2[1];
r2=ROTATE(temp,3);
temp=data3[0];
l3=ROTATE(temp,3);
temp=data3[1];
r3=ROTATE(temp,3);
FP(r1,l1);
data1[0]=l1;
data1[1]=r1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -