/*------------------------------------------------------------------- * Rijndael encryption function. Takes 16-byte input and creates * 16-byte output (using round keys already derived from 16-byte * key). *-----------------------------------------------------------------*/ void RijndaelEncrypt( u8 input[16], u8 output[16] ) { u8 state[4][4]; int i, r; /* initialise state array from input byte string */ for (i=0; i<16; i++) state[i & 0x3][i>>2] = input[i]; /* add first round_key */ KeyAdd(state, roundKeys, 0); /* do lots of full rounds */ for (r=1; r<=9; r++) { ByteSub(state); ShiftRow(state); MixColumn(state); KeyAdd(state, roundKeys, r); } /* final round */ ByteSub(state); ShiftRow(state); KeyAdd(state, roundKeys, r); /* produce output byte string from state array */ for (i=0; i<16; i++) { output[i] = state[i & 0x3][i>>2]; } return; } /* end of function RijndaelEncrypt */
//============================================================================== uint32_t SubDWord(uint32_t a) //Функция замены словаиз таблицы { uint8_t b[4]; unpack(a,b); b[0]= ByteSub(b[0]); b[1]= ByteSub(b[1]); b[2]= ByteSub(b[2]); b[3]= ByteSub(b[3]); return pack(b); }
///////////////////////////////////////////////////////////////////////////////////////////////// // LongNumSub - Subtract length-bit Multiprecision numbers (Big Integer) // * INPUT : length-bit number (A) and length-bit number (b) // * OUTPUT: Result = a - b, and a carry out (carryFlag) // a(n-1)a(n-2) ... a(1)a(0) // - // b(n-1)b(n-2) ... b(1)b(0) // ---------------------------- // carryFlag Result(n-1)Result(n-2) ... Result(1)Result(0) ///////////////////////////////////////////////////////////////////////////////////////////////// void LongNumSub(Byte * A, Byte * B, Byte * result, NumCarry *carryFlag, int length) { int i; *carryFlag = 0; for (i = 0; i < length; i++) ByteSub(A[i], B[i], &result[i], carryFlag); }
static void gentables(void) { /* generate tables */ int i; u8 y,b[4]; /* use 3 as primitive root to generate power and log tables */ ltab[0]=0; ptab[0]=1; ltab[1]=0; ptab[1]=3; ltab[3]=1; for (i=2; i<256; i++) { ptab[i]=ptab[i-1]^xtime(ptab[i-1]); ltab[ptab[i]]=i; } /* affine transformation:- each bit is xored with itself shifted one bit */ fbsub[0]=0x63; rbsub[0x63]=0; for (i=1; i<256; i++) { y=ByteSub((u8)i); fbsub[i]=y; rbsub[y]=i; } for (i=0,y=1; i<30; i++) { rco[i]=y; y=xtime(y); } /* calculate forward and reverse tables */ for (i=0; i<256; i++) { y=fbsub[i]; b[3]=y^xtime(y); b[2]=y; b[1]=y; b[0]=xtime(y); ftable[i]=pack(b); y=rbsub[i]; b[3]=bmul(InCo[0],y); b[2]=bmul(InCo[1],y); b[1]=bmul(InCo[2],y); b[0]=bmul(InCo[3],y); rtable[i]=pack(b); } }
//============================================================================== void SubBytes(uint8_t *s, uint8_t direct) //Функция замены байта в массиве { uint8_t j; //Счетчик switch(direct) { case ENCRYPT: for(j=0; j<Nb*4; j++) s[j] = ByteSub((uint8_t) s[j]); break; case DECRYPT: for(j=0; j<Nb*4; j++) s[j] = InvByteSub((uint8_t) s[j]); break; } }
/*初始化*/ void gentables(void) { //初始化矩阵 int i; BYTE y,b[4]; ltab[0]=0; ptab[0]=1; ptab[1]=3; ltab[1]=0; ltab[3]=1; for (i=2;i<256;i++) { ptab[i]=ptab[i-1]^xtime(ptab[i-1]); ltab[ptab[i]]=i; } // 进行移位操作 fbsub[0]=0x63; rbsub[0x63]=0; for (i=1;i<256;i++) { y=ByteSub((BYTE)i); fbsub[i]=y; rbsub[y]=i; } for (i=0,y=1;i<30;i++) { rco[i]=y; y=xtime(y); } for (i=0;i<256;i++) { y=fbsub[i]; b[3]=y^xtime(y); b[2]=y; b[1]=y; ftable[i]=pack(b); b[0]=xtime(y); y=rbsub[i]; b[3]=bmul(InCo[0],y); b[2]=bmul(InCo[1],y); b[1]=bmul(InCo[2],y); b[0]=bmul(InCo[3],y); rtable[i]=pack(b); } }