#include #include "print.h" #include "READER.h" #include "random_generator.h" unsigned char ciphertext_to_binary[48] ; //用来存储算法逆运算每轮的中间数据 需要定义成全局变量 unsigned char lsfr[48]; unsigned char lsfr_length; unsigned char decode_printcipher( unsigned char ciphertext[] , unsigned char long_key[] , unsigned char short_key[] )//解密函数 { unsigned char a[6][8]; unsigned char i , j ; unsigned char t ; unsigned char k ; //k是用在sbox中的 unsigned char num ; //主循环 unsigned char long_key_to_binary[48] ; unsigned char temp[48] ; unsigned char short_key_to_binary[32] ; unsigned char rev_counter[6] = {1,0,0,1,0,0}; unsigned char temp_rev_counter[6]; //----------------------------------ciphertext-------------------------------------------------// for(i=0 ; i<6 ; ++i) // { // for(j=0 ; j<8 ; ++j) // { // 该作用是把密文以二进制保存在数组中ciphertext a[i][8-1-j] = ciphertext[i] % 2 ; // a[7]应该是二进制最低位,a[0]最高位 ciphertext[i] /= 2 ; // } // } // for(i=0 ; i<6 ; ++i) { for(j=0 ; j<8 ; ++j) { ciphertext_to_binary[i*8+j] = a[i][j] ; } } //printf("%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",ciphertext_to_binary[0],ciphertext_to_binary[1],ciphertext_to_binary[2],\ ciphertext_to_binary[3],ciphertext_to_binary[4],ciphertext_to_binary[5],ciphertext_to_binary[6],ciphertext_to_binary[7],\ ciphertext_to_binary[40],ciphertext_to_binary[41],ciphertext_to_binary[42],ciphertext_to_binary[43],ciphertext_to_binary[44],ciphertext_to_binary[45],ciphertext_to_binary[46],ciphertext_to_binary[47]); //-------------------------------------ciphertext---------------------------------------------// //-------------------------------------long_key----------------------------------------------// for(i=0 ; i<6 ; ++i) // { // for(j=0 ; j<8 ; ++j) // { // 该作用是把密文以二进制保存在数组中long_key a[i][8-1-j] = long_key[i] % 2 ; // long_key[i] /= 2; // } // } // for(i=0 ; i<6 ; ++i) { for(j=0 ; j<8 ; ++j) { long_key_to_binary[i*8+j] = a[i][j] ; } } //printf("%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",long_key_to_binary[0],long_key_to_binary[1],long_key_to_binary[2],\ long_key_to_binary[3],long_key_to_binary[4],long_key_to_binary[5],long_key_to_binary[6],long_key_to_binary[7],\ long_key_to_binary[40],long_key_to_binary[41],long_key_to_binary[42],long_key_to_binary[43],long_key_to_binary[44],long_key_to_binary[45],long_key_to_binary[46],ciphertext_to_binary[47]); //-------------------------------- long_key---------------------------------------------------// //-------------------------------- short_key-------------------------------------------------// for(i=0 ; i<4 ; ++i) // { // for(j=0 ; j<8 ; ++j) // { // 该作用是把密文以二进制保存在数组中short_key a[i][8-1-j] = short_key[i] % 2 ; // short_key[i] /= 2 ; // } // } // for(i=0 ; i<4 ; ++i) { for(j=0 ; j<8 ; ++j) { short_key_to_binary[i*8+j] = a[i][j] ; } } //printf("%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",short_key_to_binary[0],short_key_to_binary[1],short_key_to_binary[2],\ short_key_to_binary[3],short_key_to_binary[4],short_key_to_binary[5],short_key_to_binary[6],short_key_to_binary[7],\ short_key_to_binary[24],short_key_to_binary[25],short_key_to_binary[26],short_key_to_binary[27],short_key_to_binary[28],short_key_to_binary[29],short_key_to_binary[30],short_key_to_binary[31]); //-------------------------------- short_key-------------------------------------------------// for(num = 0 ; num < 48 ; num++) { for(k=0 ; k<16 ; k++) { if((short_key_to_binary[k*2]==0) &&(short_key_to_binary[k*2+1]==0) ) { if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 0; temp[k*3+1]= 0; temp[k*3+2]= 0; } if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 0; temp[k*3+1]= 0; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 1; temp[k*3+1]= 1; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 0; temp[k*3+1]= 1; temp[k*3+2]= 0; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 1; temp[k*3+1]= 0; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 1; temp[k*3+1]= 1; temp[k*3+2]= 0; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 0; temp[k*3+1]= 1; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 1; temp[k*3+1]= 0; temp[k*3+2]= 0; } } //////////////////////////////01 if((short_key_to_binary[k*2]==0) &&(short_key_to_binary[k*2+1]==1) ) { if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 0; temp[k*3+1]= 0; temp[k*3+2]= 0; } if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 0; temp[k*3+1]= 0; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 1; temp[k*3+1]= 1; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 1; temp[k*3+1]= 0; temp[k*3+2]= 0; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 0; temp[k*3+1]= 1; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 1; temp[k*3+1]= 1; temp[k*3+2]= 0; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 1; temp[k*3+1]= 0; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 0; temp[k*3+1]= 1; temp[k*3+2]= 0; } } //////////////////////////////////////////10 if((short_key_to_binary[k*2]==1) &&(short_key_to_binary[k*2+1]==0) ) { if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 0; temp[k*3+1]= 0; temp[k*3+2]= 0; } if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 0; temp[k*3+1]= 1; temp[k*3+2]= 0; } if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 1; temp[k*3+1]= 1; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 0; temp[k*3+1]= 0; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 1; temp[k*3+1]= 1; temp[k*3+2]= 0; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 1; temp[k*3+1]= 0; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 0; temp[k*3+1]= 1; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 1; temp[k*3+1]= 0; temp[k*3+2]= 0; } } ////////////////////////////////////////////11 if((short_key_to_binary[k*2]==1) &&(short_key_to_binary[k*2+1]==1) ) { if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 0; temp[k*3+1]= 0; temp[k*3+2]= 0; } if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 1; temp[k*3+1]= 0; temp[k*3+2]= 0; } if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 1; temp[k*3+1]= 1; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==0) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 0; temp[k*3+1]= 1; temp[k*3+2]= 0; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 1; temp[k*3+1]= 0; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==0)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 0; temp[k*3+1]= 1; temp[k*3+2]= 1; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==0)) { temp[k*3] = 1; temp[k*3+1]= 1; temp[k*3+2]= 0; } if((ciphertext_to_binary[k*3]==1) && (ciphertext_to_binary[k*3+1]==1)&&(ciphertext_to_binary[k*3+2]==1)) { temp[k*3] = 0; temp[k*3+1]= 0; temp[k*3+2]= 1; } } } // printf("%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8],temp[9],temp[10],temp[11],temp[12],temp[13],temp[14],temp[15],temp[40],temp[41],temp[42],temp[43],temp[44],temp[45],temp[46],temp[47]); //下面的函数是对RC的逆运算 t = 1 ^ rev_counter[0] ^ rev_counter[5]; temp_rev_counter [0] = rev_counter[1] ; temp_rev_counter [1] = rev_counter[2] ; temp_rev_counter [2] = rev_counter[3] ; temp_rev_counter [3] = rev_counter[4] ; temp_rev_counter [4] = rev_counter[5] ; temp_rev_counter [5] = t ; for(i=0 ; i<6 ; i++ ) { rev_counter [i] = temp_rev_counter[i] ; //rev_counter[5]是最高位 } temp[47] = rev_counter[0] ^ temp[47] ; temp[46] = rev_counter[1] ^ temp[46] ; temp[45] = rev_counter[2] ^ temp[45] ; temp[44] = rev_counter[3] ^ temp[44] ; temp[43] = rev_counter[4] ^ temp[43] ; temp[42] = rev_counter[5] ^ temp[42] ; // printf("%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \n",temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8],temp[9],temp[10],temp[11],temp[12],temp[13],temp[14],temp[15],temp[40],temp[41],temp[42],temp[43],temp[44],temp[45],temp[46],temp[47]); //下面是对linear diffusion的逆运算 ciphertext_to_binary[0] = temp[0] ; ciphertext_to_binary[1] = temp[3] ; ciphertext_to_binary[2] = temp[6] ; ciphertext_to_binary[3] = temp[9] ; ciphertext_to_binary[4] = temp[12] ; ciphertext_to_binary[5] = temp[15] ; ciphertext_to_binary[6] = temp[18] ; ciphertext_to_binary[7] = temp[21] ; ciphertext_to_binary[8] = temp[24] ; ciphertext_to_binary[9] = temp[27] ; ciphertext_to_binary[10] = temp[30] ; ciphertext_to_binary[11] = temp[33] ; ciphertext_to_binary[12] = temp[36] ; ciphertext_to_binary[13] = temp[39] ; ciphertext_to_binary[14] = temp[42] ; ciphertext_to_binary[15] = temp[45] ; ciphertext_to_binary[16] = temp[1] ; ciphertext_to_binary[17] = temp[4] ; ciphertext_to_binary[18] = temp[7] ; ciphertext_to_binary[19] = temp[10] ; ciphertext_to_binary[20] = temp[13] ; ciphertext_to_binary[21] = temp[16] ; ciphertext_to_binary[22] = temp[19] ; ciphertext_to_binary[23] = temp[22] ; ciphertext_to_binary[24] = temp[25] ; ciphertext_to_binary[25] = temp[28] ; ciphertext_to_binary[26] = temp[31] ; ciphertext_to_binary[27] = temp[34] ; ciphertext_to_binary[28] = temp[37] ; ciphertext_to_binary[29] = temp[40] ; ciphertext_to_binary[30] = temp[43] ; ciphertext_to_binary[31] = temp[46] ; ciphertext_to_binary[32] = temp[2] ; ciphertext_to_binary[33] = temp[5] ; ciphertext_to_binary[34] = temp[8] ; ciphertext_to_binary[35] = temp[11] ; ciphertext_to_binary[36] = temp[14] ; ciphertext_to_binary[37] = temp[17] ; ciphertext_to_binary[38] = temp[20] ; ciphertext_to_binary[39] = temp[23] ; ciphertext_to_binary[40] = temp[26] ; ciphertext_to_binary[41] = temp[29] ; ciphertext_to_binary[42] = temp[32] ; ciphertext_to_binary[43] = temp[35] ; ciphertext_to_binary[44] = temp[38] ; ciphertext_to_binary[45] = temp[41] ; ciphertext_to_binary[46] = temp[44] ; ciphertext_to_binary[47] = temp[47] ; for(i=0 ; i<48 ; i++) { ciphertext_to_binary[i] = ciphertext_to_binary[i] ^ long_key_to_binary[i] ; } } // printf (" %x %x %x %x %x %x %x %x\n %x %x %x %x %x %x %x %x\n %x %x %x %x %x %x %x %x\n %x %x %x %x %x %x %x %x\n %x %x %x %x %x %x %x %x\n %x %x %x %x %x %x %x %x\n",ciphertext_to_binary[0],ciphertext_to_binary[1],ciphertext_to_binary[2],ciphertext_to_binary[3],ciphertext_to_binary[4],ciphertext_to_binary[5],ciphertext_to_binary[6],ciphertext_to_binary[7],\ ciphertext_to_binary[8],ciphertext_to_binary[9],ciphertext_to_binary[10],ciphertext_to_binary[11],ciphertext_to_binary[12],ciphertext_to_binary[13],ciphertext_to_binary[14],ciphertext_to_binary[15],\ ciphertext_to_binary[16],ciphertext_to_binary[17],ciphertext_to_binary[18],ciphertext_to_binary[19],ciphertext_to_binary[20],ciphertext_to_binary[21],ciphertext_to_binary[22],ciphertext_to_binary[23],\ ciphertext_to_binary[24],ciphertext_to_binary[25],ciphertext_to_binary[26],ciphertext_to_binary[27],ciphertext_to_binary[28],ciphertext_to_binary[29],ciphertext_to_binary[30],ciphertext_to_binary[31],\ ciphertext_to_binary[32],ciphertext_to_binary[33],ciphertext_to_binary[34],ciphertext_to_binary[35],ciphertext_to_binary[36],ciphertext_to_binary[37],ciphertext_to_binary[38],ciphertext_to_binary[39],\ ciphertext_to_binary[40],ciphertext_to_binary[41],ciphertext_to_binary[42],ciphertext_to_binary[43],ciphertext_to_binary[44],ciphertext_to_binary[45],ciphertext_to_binary[46],ciphertext_to_binary[47] ); return 0 ; } void Print_AUTH(unsigned char *key,unsigned char *TOKEN,unsigned char *rece_buff)//print 认证 { unsigned char i,j; unsigned char long_key[6] = {0xff,0xff,0xff,0xff,0xff,0xff}; //长key unsigned char short_key[4] = {0xff,0xff,0xff,0xff}; //短key,注意短key的最后一个字节是整个key的第一个字节 unsigned char PWD_key[10] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//tag的秘钥占10个字节 for(i = 0;i<10;i++) { PWD_key[i] = key[9-i];//需要把PWD 倒顺序一下,做下反向,需要特别注意,之前的密码需要倒序一下 } memcpy(&long_key[0],PWD_key,6); memcpy(&short_key[0],&PWD_key[6],4); decode_printcipher(TOKEN, &long_key[0] , &short_key[0]); for(i=0 ; i<6 ; i++) { for(j=0 ; j<8 ; j++) { rece_buff[i] = (rece_buff[i]<<1)+ciphertext_to_binary[i*8+j] ; //把最终的结果赋值给明文中 } } } unsigned char lsfr_fm458(void) //length 这里是byte { char j ; unsigned char temp; unsigned char temp1; temp1 = lsfr[47]; temp = lsfr[47] ^ lsfr[27] ^ lsfr[26] ^ lsfr[0]; for (j = 46; j >= 0; j--) { lsfr[j + 1] = lsfr[j]; } lsfr[0] = temp; return temp1; } void lsfr_init(unsigned char *S022_Token2) { unsigned char z,k; unsigned char load_key[6]; //备份初始密钥流 memcpy(load_key, S022_Token2, 6); //把R2和R3的结果加载进移位寄存器 for (z = 0; z < 6; z++) { for (k = 0; k <= 7; k++) { if ((unsigned char)((load_key[z] << k) & 0x80) == 0) lsfr[47 - (k + z * 8)] = 0; else lsfr[47 - (k + z * 8)] = 1; } } //lsfr_length = 48; //printf("lsfr = %x%x%x%x%x%x%x%x\r\n",lsfr[0],lsfr[1],lsfr[2],lsfr[3],lsfr[4],lsfr[5],lsfr[6],lsfr[7]); } //data_len 最多16byte //unsigned char PRINT_Calc_sendData(unsigned char *data_buf, unsigned char data_len, BOOL half_byte) unsigned char PRINT_Calc_sendData(unsigned char *data_buf, unsigned char data_len) { unsigned char i,y; unsigned char round; unsigned char temp[8]; unsigned char temp1[8]; unsigned char encry[64]; unsigned char senddata[16]; round = data_len * 8; for (y = 0; y < round; y++) { encry[y] = lsfr_fm458(); } for (y = 0; y < data_len; y++) { for(i=0; i<8; i++) { temp[i] = (unsigned char)((data_buf[y] >> i) & 0x01); } //发送的byte按位异或 加密 for(i=0 ; i<8 ;i++) { temp1[i] = temp[i] ^ encry[i + y * 8]; //异或就是和密码流进行加密 } senddata[y] = (unsigned char)(temp1[0] * 1 + temp1[1] * 2 + temp1[2] * 4 + temp1[3] * 8 + temp1[4] * 16 + temp1[5] * 32 + temp1[6] * 64 + temp1[7] * 128); } memcpy(data_buf, senddata, data_len); // authstat = CAL_COMMAND_FLOW; return SUCCESS; } //unsigned char PRINT_Calc_recvData(unsigned char *data_buf, unsigned char data_len, BOOL half_byte) unsigned char PRINT_Calc_recvData(unsigned char *data_buf, unsigned char data_len) { unsigned char encry[128]; unsigned char i,y; unsigned char temp[8]; unsigned char temp1[8]; unsigned char recvdata[16]; unsigned char round; round = (data_len) * 8; if (data_len == 0x01) //该添加为了类似写指令回发ACK等4bit的加密流方法。 { round = round / 2; } for (y = 0; y < round; y++) { encry[y] = lsfr_fm458(); } //已经有了加密数据,就要想办法进行异或操作 for (y = 0; y < data_len; y++) { for (i = 0; i < 8; i++) { temp[i] = (unsigned char)((data_buf[y] >> i) & 0x01); } //发送的byte按位异或 加密 for (i = 0; i < 8; i++) { temp1[i] = temp[i] ^ encry[i + y * 8]; //异或就是和密码流进行加密 } recvdata[y] = (unsigned char)(temp1[0] * 1 + temp1[1] * 2 + temp1[2] * 4 + temp1[3] * 8 + temp1[4] * 16 + temp1[5] * 32 + temp1[6] * 64 + temp1[7] * 128); } memcpy(data_buf, recvdata, data_len); return SUCCESS; }