📄 function_e0.cpp
字号:
#include "stdio.h"
#define int_64bit double
#define int_32bit float
#define int_16bit short unsigned int
#define int_8bit unsigned char
bool shift_LFSR1(bool SR, bool * LFSR)
{
//each variable in the following dimensions represents a flip-flop, so it must be a static one.
static int unsigned count;
count++;
//realize feedback
if (count>25)
{
if(count>49) SR=0;//when feedback switch is close/on
LFSR[0]=SR^LFSR[25]^LFSR[20]^LFSR[12]^LFSR[ 8];
}
else
//when feedback switch is open/off
LFSR[0]=SR;
//realize shift
for(int short i=24;i>=0;i--) LFSR[i+1]=LFSR[i];
//the following source code is only used to debug
//You can observe the output of this function
////////////////////////////////////////////////////debug////////////////////////////////////////////////////////////////////
//*
int test_int[5];
for(i=0;i<5;i++) test_int[i]=0;
for(i=0;i<3;i++) test_int[i]=LFSR[i*8+8]*128+LFSR[i*8+7]*64+LFSR[i*8+6]*32+LFSR[i*8+5]*16+LFSR[i*8+4]*8+LFSR[i*8+3]*4+LFSR[i*8+2]*2+LFSR[i*8+1];
if(i==3) test_int[i]=LFSR[i*8+1];
printf(" ");
for(i=0;i<4;i++) printf("%2x",test_int[3-i]);
return LFSR[24];
}
bool shift_LFSR2(bool SR,bool * LFSR)
{
//each variable in the following dimensions represents a flip-flop, so it must be a static one.
static int unsigned count;
count++;
//realize feedback
if (count>31)
{
if(count>56) SR=0;
//when feedback switch is close/on
LFSR[0]=SR^LFSR[31]^LFSR[24]^LFSR[16]^LFSR[12];
}
else
//when feedback switch is open/off
LFSR[0]=SR;
//realize shift
for(int short i=30;i>=0;i--) LFSR[i+1]=LFSR[i];
//the following source code is only used to debug
//You can observe the output of this function
////////////////////////////////////////////////////debug////////////////////////////////////////////////////////////////////
//*
int test_int[5];
for(i=0;i<5;i++) test_int[i]=0;
for(i=0;i<3;i++) test_int[i]=LFSR[i*8+8]*128+LFSR[i*8+7]*64+LFSR[i*8+6]*32+LFSR[i*8+5]*16+LFSR[i*8+4]*8+LFSR[i*8+3]*4+LFSR[i*8+2]*2+LFSR[i*8+1];
if(i==3) test_int[i]=LFSR[i*8+7]*64+LFSR[i*8+6]*32+LFSR[i*8+5]*16+LFSR[i*8+4]*8+LFSR[i*8+3]*4+LFSR[i*8+2]*2+LFSR[i*8+1];
printf(" ");
for(i=0;i<4;i++) printf("%2x",test_int[3-i]);
return LFSR[24];
}
bool shift_LFSR3(bool SR,bool* LFSR)
{
//each variable in the following dimensions represents a flip-flop, so it must be a static one.
static int unsigned count;
count++;
//realize feedback
if (count>33)
{
if(count>49) SR=0;
//when feedback switch is close/on
LFSR[0]=SR^LFSR[33]^LFSR[28]^LFSR[24]^LFSR[4];
}
else
//when feedback switch is open/off
LFSR[0]=SR;
//realize shift
for(int short i=32;i>=0;i--) LFSR[i+1]=LFSR[i];
//the following source code is only used to debug
//You can observe the output of this function
////////////////////////////////////////////////////debug////////////////////////////////////////////////////////////////////
//*
int test_int[5];
for(i=0;i<5;i++) test_int[i]=0;
for(i=0;i<4;i++) test_int[i]=LFSR[i*8+8]*128+LFSR[i*8+7]*64+LFSR[i*8+6]*32+LFSR[i*8+5]*16+LFSR[i*8+4]*8+LFSR[i*8+3]*4+LFSR[i*8+2]*2+LFSR[i*8+1];
if(i==4) test_int[i]=LFSR[i*8+1];
printf(" ");
for(i=0;i<5;i++) printf("%2x",test_int[4-i]);
return LFSR[32];
}
bool shift_LFSR4(bool SR,bool* LFSR)
//each variable in the following dimensions represents a flip-flop, so it must be a static one.
{
static int unsigned count;
count++;
//realize feedback
if (count>39)
{
if(count>55) SR=0;
//when feedback switch is close/on
LFSR[0]=SR^LFSR[39]^LFSR[36]^LFSR[28]^LFSR[4];
}
else
//when feedback switch is open/off
LFSR[0]=SR;
//realize shift
for(int short i=38;i>=0;i--) LFSR[i+1]=LFSR[i];
//the following source code is only used to debug
//You can observe the output of this function
////////////////////////////////////////////////////debug////////////////////////////////////////////////////////////////////
//*
int test_int[5];
for(i=0;i<5;i++) test_int[i]=0;
for(i=0;i<4;i++) test_int[i]=LFSR[i*8+8]*128+LFSR[i*8+7]*64+LFSR[i*8+6]*32+LFSR[i*8+5]*16+LFSR[i*8+4]*8+LFSR[i*8+3]*4+LFSR[i*8+2]*2+LFSR[i*8+1];
if(i==4) test_int[i]=LFSR[i*8+7]*64+LFSR[i*8+6]*32+LFSR[i*8+5]*16+LFSR[i*8+4]*8+LFSR[i*8+3]*4+LFSR[i*8+2]*2+LFSR[i*8+1];
printf(" ");
for(i=0;i<5;i++) printf("%2x",test_int[4-i]);
return LFSR[32];
}
bool blend(bool * input,int count)
{
static int_8bit Ct0_out,Ct1,Ct_1_out;
int_8bit Ct0_in,Ct_1_in,yt,st,T1,T2;
bool Z;
yt=input[0]+input[1]+input[2]+input[3];
st=(yt+Ct0_out)/2;
Z=((input[0]^input[1]^input[2]^input[3]^(Ct0_out % 2))==1)?1:0;
switch (Ct_1_out)
{
case 0: T2=0; break;
case 1: T2=3; break;
case 2: T2=1; break;
default : T2=2;
}
T1=Ct0_out;
Ct1=T1^T2^st;
Ct0_in=Ct1;
Ct_1_in=Ct0_out;
//Z=input[0]^input[1]^input[2]^input[3]^(Ct0 % 2);
if(count==239) Z=((input[0]^input[1]^input[2]^input[3]^(Ct1 % 2))==1)?1:0;
printf(" %1d %1d %1d %1d %1d %2d %2d %2d",input[0],input[1],input[2],input[3],Z,Ct1,Ct0_out,Ct_1_out);
if(count>37&&count!=238) Ct0_out=Ct0_in,Ct_1_out=Ct_1_in;
return Z;
}
void function_E0()
{
//intializtion Kc1,Addr1,CL1
//sample data1
/*
int_8bit Kc[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int_8bit Addr[16]={0,0,0,0,0,0};
int_8bit CL[3]={0,0,0};
bool CL24=0,CL25=0;
//*/
//sample data2
/*
int_8bit Kc[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int_8bit Addr[16]={0,0,0,0,0,0};
int_8bit CL[3]={0,0,0};
bool CL24=1,CL25=1;
//*/
//sample data3
//*
int_8bit Kc[16]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
int_8bit Addr[6]={0xff,0xff,0xff,0xff,0xff,0xff};
int_8bit CL[3]={0xff,0xff,0xff};
bool CL24=1,CL25=1;
//*/
//sample data4
/*
int_8bit Kc[16]={0x21,0x87,0xf0,0x4a,0xba,0x90,0x31,0xd0,0x78,0x0d,0x4c,0x53,0xe0,0x15,0x3a,0x63};
int_8bit Addr[6]={0x2c,0x7f,0x94,0x56,0x0f,0x1b};
int_8bit CL[3]={0x5f,0x1a,0x00};
bool CL24=0,CL25=1;
//*/
static bool LFSR1[26],LFSR2[32],LFSR3[34],LFSR4[40], Z[129];
static int_8bit Z1[16];
//generate input stream from Kc1,Addr1,CL1
bool input1[56],input2[56],input3[56],input4[56];
for(int_8bit i=0;i<56;i++) input1[i]=input2[i]=input3[i]=input4[i]=0;
//prepare for the input stream of LFSRs, it is too complex to be simplify
//for LFSR1
//0
input1[0]=CL24;
//1~8
for( i=0;i<8;i++) input1[i+1]= ((Kc[0]>>i) % 2)==1?1:0 ;
//9~16
for( i=0;i<8;i++) input1[i+9]=(Kc[4]>>i) % 2==1?1:0 ;
//17~24
for( i=0;i<8;i++) input1[i+17]=(Kc[8]>>i) % 2==1?1:0 ;
//25~32
for( i=0;i<8;i++) input1[i+25]=(Kc[12]>>i) % 2==1?1:0 ;
//33~40
for( i=0;i<8;i++) input1[i+33]=(CL[1]>>i) % 2==1?1:0 ;
//41~48
for( i=0;i<8;i++) input1[i+41]=(Addr[2]>>i) % 2==1?1:0 ;
//for LFSR2
//0~2
input2[0]=1;input2[1]=0;input2[2]=0;
//3~6
for( i=0;i<4;i++) input2[i+3]=(CL[0]>>i)% 2 ==1?1:0 ;
//7~14
for( i=0;i<8;i++) input2[i+7]=(Kc[1]>>i) %2 ==1?1:0 ;
//15~22
for( i=0;i<8;i++) input2[i+15]=(Kc[5]>>i)% 2 ==1?1:0 ;
//23~30
for( i=0;i<8;i++) input2[i+23]=(Kc[9]>>i) % 2==1?1:0 ;
//31~38
for( i=0;i<8;i++) input2[i+31]=(Kc[13]>>i)% 2 ==1?1:0 ;
//39~46
for( i=0;i<8;i++) input2[i+39]=(Addr[0]>>i) % 2 ==1?1:0 ;
//47~54
for( i=0;i<8;i++) input2[i+47]=(Addr[3]>>i) % 2==1?1:0 ;
//for LFSR3
//0
input3[0]=CL25;
//1~8
for( i=0;i<8;i++) input3[i+1]=(Kc[2]>>i)% 2 ==1?1:0 ;
//9~16
for( i=0;i<8;i++) input3[i+9]=(Kc[6]>>i) % 2 ==1?1:0 ;
//17~24
for( i=0;i<8;i++) input3[i+17]=(Kc[10]>>i) % 2 ==1?1:0 ;
//25~32
for( i=0;i<8;i++) input3[i+25]=(Kc[14]>>i) % 2==1?1:0 ;
//33~40
for( i=0;i<8;i++) input3[i+33]=(CL[2]>>i) % 2==1?1:0 ;
//41~48
for( i=0;i<8;i++) input3[i+41]=(Addr[4]>>i) % 2==1?1:0 ;
//for LFSR4
//0~2
input4[0]=1;input4[1]=1;input4[2]=1;
//3~6
for( i=0;i<4;i++) input4[i+3]=(CL[0]>>(i+4)) % 2==1?1:0 ;
//7~14
for( i=0;i<8;i++) input4[i+7]=(Kc[3]>>i) % 2==1?1:0 ;
//15~22
for( i=0;i<8;i++) input4[i+15]=(Kc[7]>>i) % 2==1?1:0 ;
//23~30
for( i=0;i<8;i++) input4[i+23]=(Kc[11]>>i) % 2==1?1:0 ;
//31~38
for( i=0;i<8;i++) input4[i+31]=(Kc[15]>>i) % 2==1?1:0 ;
//39~46
for( i=0;i<8;i++) input4[i+39]=(Addr[1]>>i) % 2==1?1:0 ;
//47~54
for( i=0;i<8;i++) input4[i+47]=(Addr[5]>>i) % 2==1?1:0 ;
//in the first 239 clocks , the LFSRs and blend funciton will gengerate the real key used in encrypt data stream
//output is Z
for(int count=0;count<239;count++)
{
bool input[4];
printf("\n%3d",count+1);
input[0]=shift_LFSR1(input1[count],LFSR1);
input[1]=shift_LFSR2(input2[count],LFSR2);
input[2]=shift_LFSR3(input3[count],LFSR3);
input[3]=shift_LFSR4(input4[count],LFSR4);
if(count>110)
Z[(count-111)]=blend(input,count);
else
blend(input,count);
}
//when the real encrypt key is made , we need to reload them into the LFSRs,
//but the Ct0,Ct-1 in Blend function remain unchaged.
////////////////////////////////////////////////////////////////////////////////////////
for(i=0;i<56;i++) input1[i]=input2[i]=input3[i]=input4[i]=0;
//prepare for the input stream of LFSRs for the second time , the data needed is from Dimention Z
// input1 which is used in the LFSR1
// 0~7 8~15 16~23 24
for(i=0;i<8;i++) input1[i+1]=Z[i ], input1[i+8+1]=Z[i+4*8],input1[i+16+1]=Z[i+8*8]; input1[24+1]=Z[12*8];
// input2 which is used in the LFSR2
// 0~7 8~15 16~23
for(i=0;i<8;i++) input2[i+1]=Z[i+1*8], input2[i+8+1]=Z[i+5*8],input2[i+16+1]=Z[i+9*8];
// 24~30
for(i=0;i<7;i++) input2[i+24+1]=Z[12*8+1+i];
// input3 which is used in the LFSR3
// 0~7 8~15 16~23
for(i=0;i<8;i++) input3[i+1]=Z[i+2*8], input3[i+1+8]=Z[i+6*8],input3[i+1+16]=Z[i+10*8],input3[i+1+24]=Z[i+13*8];input3[32+1]=Z[15*8];
// input4 which is used in the LFSR4
// 0~7 8~15 16~23
for(i=0;i<8;i++) input4[i+1]=Z[i+3*8], input4[i+1+8]=Z[i+7*8],input4[i+1+16]=Z[i+11*8],input4[i+1+24]=Z[i+14*8];
for(i=0;i<7;i++) input4[i+1+32]=Z[15*8+1+i];
//the following code is just only used for debug
//*
printf("\n");
for(i=0;i<25;i++) printf("%1d",input1[i]);
printf("\n");
for(i=0;i<31;i++) printf("%1d",input2[i]);
printf("\n");
for(i=0;i<33;i++) printf("%1d",input3[i]);
printf("\n");
for(i=0;i<40;i++) printf("%1d",input4[i]);
//*/
bool encrypt_stream[200];
bool input[4];
input[0]=input1[24]; input[1]=input2[24];input[2]=input3[32];input[3]=input4[32];
/////////////////////////////////////////////////////////////////////////////////
//from now the real bit stream used in encryption or decryption is made.
//at the clock 240, the first bit of encryption come into being.
//and the output is dimension encrypt_stream
encrypt_stream[0]=blend(input,38);
for( count=0;count<124;count++)
{
// if(!count) blend(input,count,0);
printf("\n%3d",count+241);
input[0]=shift_LFSR1(0,input1);
input[1]=shift_LFSR2(0,input2);
input[2]=shift_LFSR3(0,input3);
input[3]=shift_LFSR4(0,input4);
encrypt_stream[count+1]=blend(input,38);
}
printf("\n\n\n\n");
for(i=0;i<125;i++) printf("%1x",encrypt_stream[i]);
//
}
void main()
{
function_E0();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -