// mixing column 0
newstate[0] = Xtime2Sbox[state[0]] ^ Xtime3Sbox[state[5]] ^ Sbox[state[10]] ^ Sbox[state[15]];
newstate[1] = Sbox[state[0]] ^ Xtime2Sbox[state[5]] ^ Xtime3Sbox[state[10]] ^ Sbox[state[15]];
newstate[2] = Sbox[state[0]] ^ Sbox[state[5]] ^ Xtime2Sbox[state[10]] ^ Xtime3Sbox[state[15]];
newstate[3] = Xtime3Sbox[state[0]] ^ Sbox[state[5]] ^ Sbox[state[10]] ^ Xtime2Sbox[state[15]];
// mixing column 1
newstate[4] = Xtime2Sbox[state[4]] ^ Xtime3Sbox[state[9]] ^ Sbox[state[14]] ^ Sbox[state[3]];
newstate[5] = Sbox[state[4]] ^ Xtime2Sbox[state[9]] ^ Xtime3Sbox[state[14]] ^ Sbox[state[3]];
newstate[6] = Sbox[state[4]] ^ Sbox[state[9]] ^ Xtime2Sbox[state[14]] ^ Xtime3Sbox[state[3]];
newstate[7] = Xtime3Sbox[state[4]] ^ Sbox[state[9]] ^ Sbox[state[14]] ^ Xtime2Sbox[state[3]];
// mixing column 2
newstate[8] = Xtime2Sbox[state[8]] ^ Xtime3Sbox[state[13]] ^ Sbox[state[2]] ^ Sbox[state[7]];
newstate[9] = Sbox[state[8]] ^ Xtime2Sbox[state[13]] ^ Xtime3Sbox[state[2]] ^ Sbox[state[7]];
newstate[10] = Sbox[state[8]] ^ Sbox[state[13]] ^ Xtime2Sbox[state[2]] ^ Xtime3Sbox[state[7]];
newstate[11] = Xtime3Sbox[state[8]] ^ Sbox[state[13]] ^ Sbox[state[2]] ^ Xtime2Sbox[state[7]];
// mixing column 3
newstate[12] = Xtime2Sbox[state[12]] ^ Xtime3Sbox[state[1]] ^ Sbox[state[6]] ^ Sbox[state[11]];
newstate[13] = Sbox[state[12]] ^ Xtime2Sbox[state[1]] ^ Xtime3Sbox[state[6]] ^ Sbox[state[11]];
newstate[14] = Sbox[state[12]] ^ Sbox[state[1]] ^ Xtime2Sbox[state[6]] ^ Xtime3Sbox[state[11]];
newstate[15] = Xtime3Sbox[state[12]] ^ Sbox[state[1]] ^ Sbox[state[6]] ^ Xtime2Sbox[state[11]];
memcpy (state, newstate, sizeof(newstate));
}
/*F*****************************************************************************
* PARAMS: state 4*4的状态表首指针
*
* RETURN: None
*-------------------------------------------------------------------------------
* PURPOSE: 反列混合运算
* 包括
*-------------------------------------------------------------------------------
* EXAMPLE:
*-------------------------------------------------------------------------------
* NOTE:
*******************************************************************************/
static void InvMixSubColumns(AES_U8 *state)
{
AES_U8 newstate[4 * Nc]; //用于缓存新的"state"数组
AES_U16 i;
// restore column 0
newstate[0] = XtimeE[state[0]] ^ XtimeB[state[1]] ^ XtimeD[state[2]] ^ Xtime9[state[3]];
newstate[5] = Xtime9[state[0]] ^ XtimeE[state[1]] ^ XtimeB[state[2]] ^ XtimeD[state[3]];
newstate[10] = XtimeD[state[0]] ^ Xtime9[state[1]] ^ XtimeE[state[2]] ^ XtimeB[state[3]];
newstate[15] = XtimeB[state[0]] ^ XtimeD[state[1]] ^ Xtime9[state[2]] ^ XtimeE[state[3]];
// restore column 1
newstate[4] = XtimeE[state[4]] ^ XtimeB[state[5]] ^ XtimeD[state[6]] ^ Xtime9[state[7]];
newstate[9] = Xtime9[state[4]] ^ XtimeE[state[5]] ^ XtimeB[state[6]] ^ XtimeD[state[7]];
newstate[14] = XtimeD[state[4]] ^ Xtime9[state[5]] ^ XtimeE[state[6]] ^ XtimeB[state[7]];
newstate[3] = XtimeB[state[4]] ^ XtimeD[state[5]] ^ Xtime9[state[6]] ^ XtimeE[state[7]];
// restore column 2
newstate[8] = XtimeE[state[8]] ^ XtimeB[state[9]] ^ XtimeD[state[10]] ^ Xtime9[state[11]];
newstate[13] = Xtime9[state[8]] ^ XtimeE[state[9]] ^ XtimeB[state[10]] ^ XtimeD[state[11]];
newstate[2] = XtimeD[state[8]] ^ Xtime9[state[9]] ^ XtimeE[state[10]] ^ XtimeB[state[11]];
newstate[7] = XtimeB[state[8]] ^ XtimeD[state[9]] ^ Xtime9[state[10]] ^ XtimeE[state[11]];
// restore column 3
newstate[12] = XtimeE[state[12]] ^ XtimeB[state[13]] ^ XtimeD[state[14]] ^ Xtime9[state[15]];
newstate[1] = Xtime9[state[12]] ^ XtimeE[state[13]] ^ XtimeB[state[14]] ^ XtimeD[state[15]];
newstate[6] = XtimeD[state[12]] ^ Xtime9[state[13]] ^ XtimeE[state[14]] ^ XtimeB[state[15]];
newstate[11] = XtimeB[state[12]] ^ XtimeD[state[13]] ^ Xtime9[state[14]] ^ XtimeE[state[15]];
for (i=0; i<(4 * Nc); i++)
{
state[i] = InvSbox[newstate[i]];
}
}
/*F*****************************************************************************
* PARAMS: state 4*4的状态表首指针
* key 密钥表首指针
* RETURN: None
*-------------------------------------------------------------------------------
* PURPOSE: 轮密钥加操作
*-------------------------------------------------------------------------------
* EXAMPLE:
*-------------------------------------------------------------------------------
* NOTE: 在加密和解密过程中均用到
*******************************************************************************/
static void AddRoundKey(AES_U8 *state, AES_U8 *key)
{
AES_U16 idx; //索引
for (idx=0; idx<4; idx++)
{
state[idx] ^= key[idx];
}
}
/*F*****************************************************************************
* PARAMS: key 密钥表首指针
* expKey 扩展密钥表首指针
* RETURN: None
*-------------------------------------------------------------------------------
* PURPOSE: 扩展密钥加操作,相当于初始化AES算法
*-------------------------------------------------------------------------------
* EXAMPLE:
*-------------------------------------------------------------------------------
* NOTE: 在加密和解密过程中均用到,每轮扩展Nk字节
*******************************************************************************/
void AES_ExpandKey(AES_U8 *key, AES_U8 *expKey)
{
AES_U8 tmp0, tmp1, tmp2, tmp3, tmp4;
AES_U16 idx; //索引
for (idx=0; idx<Nk; idx++) //把数组key中的每个元素复制到数组expKey相对应中的位置,Nk=4
{
expKey[4*idx+0] = key[4 * idx + 0];
expKey[4*idx+1] = key[4 * idx + 1];
expKey[4*idx+2] = key[4 * idx + 2];
expKey[4*idx+3] = key[4 * idx + 3];
}
for (idx=Nk; idx<(Nc * (Nr + 1)); idx++) //
{
tmp0 = expKey[4*idx - 4];
tmp1 = expKey[4*idx - 3];
tmp2 = expKey[4*idx - 2];
tmp3 = expKey[4*idx - 1];
if ( !(idx % Nk) )
{
tmp4 = tmp3;
tmp3 = Sbox[tmp0];
tmp0 = Sbox[tmp1] ^ Rcon[idx/Nk];
tmp1 = Sbox[tmp2];
tmp2 = Sbox[tmp4];
} //End of if
//convert from longs to bytes
expKey[4*idx+0] = expKey[4*idx - 4*Nk + 0] ^ tmp0;
expKey[4*idx+1] = expKey[4*idx - 4*Nk + 1] ^ tmp1;
expKey[4*idx+2] = expKey[4*idx - 4*Nk + 2] ^ tmp2;
expKey[4*idx+3] = expKey[4*idx - 4*Nk + 3] ^ tmp3;
} //End of for
}
/*F*****************************************************************************
* PARAMS: in 输入缓冲区指针
* expKey 扩展密钥表首指针
* out 输出缓冲区指针
* RETURN: None
*-------------------------------------------------------------------------------
* PURPOSE: 128Bit加密函数
*-------------------------------------------------------------------------------
* EXAMPLE:
*-------------------------------------------------------------------------------
* NOTE: 每次仅限于128Bit数据块
*******************************************************************************/
void AES_Encrypt(AES_U8 *in, AES_U8 *expKey, AES_U8 *out)
{
AES_U8 state[Nc * 4]; //state数组,用于数据分组
AES_U16 round; //加密轮数计数器
AES_U16 idx; //索引
//128位数据分组,把输入缓冲区中的数据放到4*4state数组中
for (idx=0; idx<Nc; idx++ )
{
state[4*idx+0] = *in++;
state[4*idx+1] = *in++;
state[4*idx+2] = *in++;
state[4*idx+3] = *in++;
}
//与扩展密钥进行“异或”操作
AddRoundKey ((AES_U8 *)state, (AES_U8 *)expKey);
//Nr轮加密操作
for (round=1; round<(Nr + 1); round++)
{
if (round < Nr)
{
MixSubColumns(state); //包含S-box变换,行变换,列变换
}
else
{
ShiftRows (state); //S-box变换,行变换,最后一轮不进行列变换
} //End of if
//与扩展密钥进行“异或”操作
AddRoundKey ((AES_U8 *)state, (AES_U8 *)expKey + round * Nc);
} //End of for
//输出128位加密后的数据
for (idx=0; idx<Nc; idx++)
{
*out++ = state[4*idx+0];
*out++ = state[4*idx+1];
*out++ = state[4*idx+2];
*out++ = state[4*idx+3];
}
}
/*F*****************************************************************************
* PARAMS: in 输入缓冲区指针
* expKey 扩展密钥表首指针
* out 输出缓冲区指针
* RETURN: None
*-------------------------------------------------------------------------------
* PURPOSE: 128Bit解密函数
*-------------------------------------------------------------------------------
* EXAMPLE:
*-------------------------------------------------------------------------------
* NOTE: 每次仅限于128Bit数据块
*******************************************************************************/
void AES_Decrypt(AES_U8 *in, AES_U8 *expKey, AES_U8 *out)
{
AES_U8 state[Nc * 4]; //state数组,用于加密数据分组
AES_U16 round; //加密轮数计数器
AES_U16 idx; //索引
//128位加密数据分组
for (idx=0; idx<Nc; idx++)
{
state[4*idx+0] = *in++;
state[4*idx+1] = *in++;
state[4*idx+2] = *in++;
state[4*idx+3] = *in++;
}
//与扩展密钥进行“异或”操作
AddRoundKey ((AES_U8 *)state, (AES_U8 *)expKey + Nr * Nc);
round = Nr;
//反S-box变换,反行变换
InvShiftRows(state);
while (round--)
{
//与扩展密钥进行“异或”操作
AddRoundKey((AES_U8 *)state, (AES_U8 *)expKey + round * Nc);
if (round)
{
//反S-box变换,反行变换,反列变换
InvMixSubColumns (state);
} //End of if
} //End of while
//输出128位解密数据
for (idx=0; idx<Nc; idx++ )
{
*out++ = state[4*idx+0];
*out++ = state[4*idx+1];
*out++ = state[4*idx+2];
*out++ = state[4*idx+3];
}
}