2008-10-22 09:13 AES128加密算法与实现2

/*F*****************************************************************************
* PARAMS:   state       4*4的状态表首指针
*
* RETURN:   None
*-------------------------------------------------------------------------------
* PURPOSE: 列混合操作。事实上是一种替代操作,用 State 字节列的值进行数学域加
*           和域乘的结果代替每个字节。
*           包括S-box变换,行变换,列变换
*-------------------------------------------------------------------------------
* EXAMPLE:
*-------------------------------------------------------------------------------
* NOTE:     MixColumns 使用特殊的加法和乘法,是基于近代数学的域论的有限域GF(28).
*           GF(28)由一组从 0x00 到 0xff 的256个值组成,加上加法和乘法。 GF代表
*           伽罗瓦域,以发明这一理论的数学家的名字命名。GF(28) 的一个特性是一个
*           加法或乘法的操作的结果必须是在{0x00 ... 0xff}这组数中。虽然域论是相
*           当深奥的,但GF(28)加法的最终结果却很简单。GF(28) 加法就是异或(XOR)
*           操作。
*
*         GF(28)的乘法有点繁难。AES的加密和解密例程需要知道怎样只用七个常量
*           0x01、0x02、0x03、0x09、0x0b、0x0d 和 0x0e 来相乘。只是针对这七种特殊
*           情况进行说明。
*
*         在GF(28)中用0x01的乘法是特殊的;它相当于普通算术中用1做乘法并且结果
*           也同样—任何值乘0x01等于其自身。
*
*         用0x02做乘法。和加法的情况相同,理论是深奥的,但最终结果十分简单。只要
*           被乘的值小于0x80,这时乘法的结果就是该值左移1比特位。如果被乘的值大于
*           或等于0x80,这时乘法的结果就是左移1比特位再用值0x1b异或。它防止了“域溢
*           出”并保持乘法的乘积在范围以内。
*
*           清楚在GF(28)中用0x02建立了加法和乘法,就可以用任何常量去定义乘法。用
*           0x03做乘法时,你可以将0x03分解为2的幂之和。为了用0x03乘以任意字节b,
*           因为 0x03 = 0x02 + 0x01,因此:
*               b * 0x03 = b * (0x02 + 0x01) = (b * 0x02) + (b * 0x01)
*******************************************************************************/
static void MixSubColumns(AES_U8 *state)
{
AES_U8 newstate[4 * Nc];    //用于缓存新的"state"数组

    // 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];
    }
}

你可能感兴趣的:(2008-10-22 09:13 AES128加密算法与实现2)