📄 function_e1_e2_e3.cpp
字号:
#include "stdio.h"
#define int_64bit double
#define int_32bit float
#define int_16bit short unsigned int
#define int_8bit unsigned char
//////////////////////////////////////function B///////////////////////////////////////////////////
//used in function roundkeygenerate
//Realize Fuction Bi, i=2~17,Bi is a bias vector of Ki
//The Function is
//Bi=B[p][i]= ((45**(45**(17*p+i+1) mod 257) mod 257)mod 256 )
// for i=0~15 p=1~17
//and by p is meant the number of round
//i is defined as the number of K
//The output will be convert to a table
//because the reslut is constant
//it's unnecessary to calculate every time
//to simple the function B , a table is made as following
int_8bit B[17][16]={
0x77,0xf1,0x56,0x24,0x7e,0x47,0x1b,0x86,0xbd,0x70,0x8e,0x1e,0x3b,0x73,0x16,0x03,
0x64,0xac,0x28,0x5a,0xc9,0xb3,0x37,0xc5,0x0a,0x10,0xb7,0xa3,0xba,0xb1,0x97,0x46,
0x3d,0x05,0xdc,0x66,0x6e,0xf6,0x9a,0xf8,0x0d,0x58,0x95,0x67,0xc6,0xaa,0xab,0xec,
0xa0,0x68,0x9b,0x96,0xd4,0xeb,0xbf,0x43,0x49,0x36,0xe9,0x6a,0x89,0xd8,0xc3,0x8a,
0x94,0x63,0x99,0xbc,0x7b,0xbe,0xc1,0x22,0xbb,0x5c,0x71,0xd5,0x1f,0x92,0x57,0x5d,
0x8f,0x44,0x41,0x1d,0x51,0xe6,0x40,0x17,0xfb,0xfd,0x19,0x32,0x34,0xb8,0x61,0x2a,
0xca,0x23,0x6f,0xda,0x39,0xf7,0xa2,0x01,0x7f,0xd6,0x31,0xe7,0xde,0x80,0x04,0xdd,
0x2c,0x59,0x82,0xaf,0xa8,0xe0,0x0f,0xcd,0xa1,0x12,0x3e,0x30,0xd1,0x1c,0xd0,0x3a,
0x33,0x72,0x2e,0x4f,0x90,0x02,0x13,0x06,0x75,0xce,0x87,0xc2,0xef,0xb2,0xad,0x7d,
0x38,0x15,0xe1,0x52,0x9f,0x7a,0x6c,0x2f,0x27,0xc4,0xe2,0x81,0xa9,0xcf,0x8d,0xc0,
0xd7,0xdf,0xff,0x60,0x76,0x14,0x8c,0x5e,0x55,0x09,0xe4,0x08,0xc7,0x42,0x20,0xfc,
0xd2,0x50,0x91,0xd9,0x4c,0x62,0x9e,0xe8,0xb9,0xa6,0xf9,0x1a,0x00,0x21,0x0b,0xfa,
0x35,0x9c,0x4e,0x4b,0x69,0x48,0xcb,0x0e,0xc8,0xa4,0x5b,0xea,0x84,0x07,0xb4,0x18,
0xf4,0xae,0x6b,0xdb,0xa7,0xcc,0x3f,0x8b,0x4a,0x0c,0x3c,0x25,0xe5,0x54,0x4d,0x45,
0x83,0xed,0x11,0xf0,0xb0,0x53,0x93,0xf2,0x74,0x26,0xb5,0x9d,0x6d,0x7c,0xf3,0x2d,
0xf1,0x56,0x24,0x7e,0x47,0x1b,0x86,0xbd,0x70,0x8e,0x1e,0x3b,0x73,0x16,0x03,0xb6,
0xac,0x28,0x5a,0xc9,0xb3,0x37,0xc5,0x0a,0x10,0xb7,0xa3,0xba,0xb1,0x97,0x46,0x88 };
///////////////////////////////////////function B//////////////////////////////////////////////
////////////////////////////////////Funciton e/////////////////////////////////
//This function can realize function e
//function e : ( 45**i (mod 257) ) (mod 256)
//function l is the reverse function of function e
//in the main function it will be made of two table as the etable the and ltable
//this funciton will be used in function roundkeygenerate
int_8bit etable[256];
int_8bit ltable[256];
int_8bit e(int_8bit i)
{
int temp = 1;
int_8bit counter;
for(counter=0;counter<i;counter++)
{
temp *= 45;
temp = temp % 257;
}
return(temp % 256);
}
////////////////////////////////////Funciton e & Function l/////////////////////////////////
////////////////////////////////////Funcion bitwise///////////////////////////////
//realize the calculation of the rand number and the roundkey by bit.
//this funciton will be used in function roundkeygenerate
void bitwise(int_8bit *data, int_8bit *key)
{
data[0] ^= key[0];
data[1] += key[1];
data[2] += key[2];
data[3] ^= key[3];
data[4] ^= key[4];
data[5] += key[5];
data[6] += key[6];
data[7] ^= key[7];
data[8] ^= key[8];
data[9] += key[9];
data[10] += key[10];
data[11] ^= key[11];
data[12] ^= key[12];
data[13] += key[13];
data[14] += key[14];
data[15] ^= key[15];
}
////////////////////////////////////Funcion bitwise///////////////////////////////
///////////////////////////////////Funciton addition/////////////////////////////
//realize the calculation of the rand number and the roundkey by bit.
//this funciton will be used in function roundkeygenerate
void addtion(int_8bit *data, int_8bit *key)
{
data[0] += key[0];
data[1] ^= key[1];
data[2] ^= key[2];
data[3] += key[3];
data[4] += key[4];
data[5] ^= key[5];
data[6] ^= key[6];
data[7] += key[7];
data[8] += key[8];
data[9] ^= key[9];
data[10] ^= key[10];
data[11] += key[11];
data[12] += key[12];
data[13] ^= key[13];
data[14] ^= key[14];
data[15] += key[15];
}
///////////////////////////////////Funciton addition/////////////////////////////
///////////////////////////////////fuction exp45////////////////////////////////
//this funciton realize the etable and ltable
//used in function roundkeygenerate
void exp45(int_8bit *data)
{
data[0] = etable[data[0]];
data[1] = ltable[data[1]];
data[2] = ltable[data[2]];
data[3] = etable[data[3]];
data[4] = etable[data[4]];
data[5] = ltable[data[5]];
data[6] = ltable[data[6]];
data[7] = etable[data[7]];
data[8] = etable[data[8]];
data[9] = ltable[data[9]];
data[10] = ltable[data[10]];
data[11] = etable[data[11]];
data[12] = etable[data[12]];
data[13] = ltable[data[13]];
data[14] = ltable[data[14]];
data[15] = etable[data[15]];
}
///////////////////////////////////fuction exp45////////////////////////////////
//////////////////////////////////Funciton pht//////////////////////////////////
//realize the PHT convertion
//this function will be used in function roundkeygenerate
void pht(int_8bit *data)
{
int_8bit i;
int temp;
for(i=0;i<8;i++)
{
temp = data[2*i] + data[2*i] + data[2*i+1];
data[2*i + 1] = data[2*i] + data[2*i+1];
data[2*i] = temp;
}
}
//////////////////////////////////Funciton pht//////////////////////////////////
//////////////////////////////////Funciton premute//////////////////////////////////
//this function will be used in function roundkeygenerate
void premute(int_8bit *data)
{
int i;
int_8bit temp[16];
for(i=0;i<16;i++)temp[i] = data[i];
data[0] = temp[8];
data[1] = temp[11];
data[2] = temp[12];
data[3] = temp[15];
data[4] = temp[2];
data[5] = temp[1];
data[6] = temp[6];
data[7] = temp[5];
data[8] = temp[10];
data[9] = temp[9];
data[10] = temp[14];
data[11] = temp[13];
data[12] = temp[0];
data[13] = temp[7];
data[14] = temp[4];
data[15] = temp[3];
}
//////////////////////////////////Funciton premute//////////////////////////////////
////////////////////////////////////roundkeygenerate/////////////////////////////////////////////////
// used in function roundencrypt
//int_8bit roundkey[17][16];
//define the shift(or shit!) function which can rotate each octet of the (128bit+8bit) key left by 3 bit positions
void shift(int_8bit tmp_key[17])
{
/* int_8bit tmp1; //tmp1 contain the previous 3 bit of *tmp_key
tmp1=*(tmp_key) / 32; //
int_8bit tmp2; //tmp1 contain the previous 3 bit of *(tmp_key+i) i=1~16
for(int_8bit m=0;m<16;m++)
{
*(tmp_key+m)=*(tmp_key+m)<<3;
tmp2=(*(tmp_key+m+1)) / 32;
*(tmp_key+m)=*(tmp_key+m)+tmp2;
}
*(tmp_key+16)=*(tmp_key+16)<<3;
*(tmp_key+16)=*(tmp_key+16)+tmp1;
printf("\n");
for(int q=0;q<17;q++) printf("%2x",tmp_key[q]);
*/
int_8bit tmp1;
for(int_8bit m=0;m<17;m++)
{
tmp1=*(tmp_key+m) /32;
*(tmp_key+m)=*(tmp_key+m)<<3;
*(tmp_key+m)=*(tmp_key+m)+tmp1;
}
return;
}
void roundkeygenerate(int_8bit inputkey[16],int_8bit roundkey[17][16])
{
//Realize Fuction Ki, i=1~17
//Ki is a round key used in Function Ar
// How to use the function? You can see it in BlueTooth V1.1 core specification Page 174
//the input parameter is a 128 bit key grouped in 16 octets
//so in the main function the only need is to define 128 bit key and function B
//then the function Ki is called
//this function need the Function B as an input parameter
//to convenience calculate, the 128 bit key is define as a dimension contained 17 elements
//and the last element(17) can be find out througth the other 16 elements
int_8bit key[17],tmp_key[17];
//roundkey represents output Ki,i=1~17
//initialize ki,
key[16]=0;
for(int_8bit i=0;i<16;i++)
{
key[i]=inputkey[i];
key[16]=key[16]^key[i];
}
//key[16]=0;
//this step can get roundkey k1
//and the sametime get tmp_key, tmp_key will be ratate for every time
for(i=0;i<16;i++)
{
roundkey[0][i]=key[i];
tmp_key[i]=key[i];
}
tmp_key[16]=key[16];
//this recycle can get roundkey k2~k17
for(i=0;i<16;i++)
{
shift(tmp_key);
//tmp_key[16]=tmp_key[0]^tmp_key[1]^tmp_key[14]^tmp_key[15];
for(int j=0;j<16;j++)
{
roundkey[i+1][j]=tmp_key[(j+i+1) % 17]+B[i+1][15-j] % 256 ;
}
}
}
////////////////////////////////roundkeygenerate///////////////////////////////////////////////////////
//////////////////////////////////////roundencrypt////////////////////////////////////////////////
//used in function Ar or function Ar'
void roundencrypt (int_8bit k,int_8bit* rand,int_8bit roundkey[17][16])
{
printf("\nround[%2d]=0x",k+1);
for( int m=0;m<16;m++)
printf("%2x",*(rand+m));
printf("\n key[%2d]=0x",2*k+1);
for( m=0;m<16;m++)
printf("%2x",roundkey[2*k][m]);
printf("\n key[%2d]=0x",2*k+2);
for(m=0;m<16;m++)
printf("%2x",roundkey[2*k+1][m]);
// getchar();
for( m=0;m<16;m++)
{
switch(m)
{
case 0: case 3: case 4: case 7:
case 8: case 11: case 12: case 15:
*(rand+m)^=roundkey[2*k][m];
break;
default: *(rand+m)=(*(rand+m)+roundkey[2*k][m]) ;
break;
}
}
exp45(rand);
for( m=0;m<16;m++)
{
switch(m)
{
case 0: case 3: case 4: case 7:
case 8: case 11: case 12: case 15:
*(rand+m)=(*(rand+m)+roundkey[2*k+1][m]) ;
break;
default:
*(rand+m)^=roundkey[2*k+1][m];
break;
}
}
pht(rand);
premute(rand);
pht(rand);
premute(rand);
pht(rand);
premute(rand);
pht(rand);
}
//////////////////////////////////////roundencrypt////////////////////////////////////////////////
//////////////////////////////////////Function Ar/////////////////////////////////////////////////
//used in function E1
void function_Ar(int_8bit* rand, int_8bit roundkey[17][16])
{
for(int_8bit k=0;k<8;k++)
{
roundencrypt (k, rand, roundkey);
if(k==7)
{
printf("\nkey [17]=0x");
for(int m=0;m<16;m++)
printf("%2x",roundkey[16][m]);
}
}
for( int_8bit m=0;m<16;m++)
{
switch(m)
{
case 0:
case 3:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -