⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crypt_aes.c

📁 ralink 2870 usb无线网卡 最新驱动
💻 C
📖 第 1 页 / 共 3 页
字号:
            Temp = aes_sbox_enc[TempWord[1]]^((Temprcon >> 24) & 0xff);
            TempWord[1] = aes_sbox_enc[TempWord[2]]^((Temprcon >> 16) & 0xff);
            TempWord[2] = aes_sbox_enc[TempWord[3]]^((Temprcon >>  8) & 0xff);
            TempWord[3] = aes_sbox_enc[TempWord[0]]^((Temprcon      ) & 0xff);
            TempWord[0] = Temp;
        } else if ((NumberOfWordOfKey > 6) && ((KeyIndex % NumberOfWordOfKey) == 4)) {
            Temp = aes_sbox_enc[TempWord[0]];
            TempWord[1] = aes_sbox_enc[TempWord[1]];
            TempWord[2] = aes_sbox_enc[TempWord[2]];
            TempWord[3] = aes_sbox_enc[TempWord[3]];
            TempWord[0] = Temp;
        }
        paes_ctx->KeyWordExpansion[0][KeyIndex] = paes_ctx->KeyWordExpansion[0][KeyIndex - NumberOfWordOfKey]^TempWord[0];
        paes_ctx->KeyWordExpansion[1][KeyIndex] = paes_ctx->KeyWordExpansion[1][KeyIndex - NumberOfWordOfKey]^TempWord[1];
        paes_ctx->KeyWordExpansion[2][KeyIndex] = paes_ctx->KeyWordExpansion[2][KeyIndex - NumberOfWordOfKey]^TempWord[2];
        paes_ctx->KeyWordExpansion[3][KeyIndex] = paes_ctx->KeyWordExpansion[3][KeyIndex - NumberOfWordOfKey]^TempWord[3];
        KeyIndex++;
    } /* End of while */
} /* End of AES_KeyExpansion */


/*
========================================================================
Routine Description:
    AES encryption

Arguments:
    PlainBlock       The block of plain text, 16 bytes(128 bits) each block
    PlainBlockSize   The length of block of plain text in bytes
    Key              Cipher key, it may be 16, 24, or 32 bytes (128, 192, or 256 bits)
    KeyLength        The length of cipher key in bytes
    CipherBlockSize  The length of allocated cipher block in bytes

Return Value:
    CipherBlock      Return cipher text
    CipherBlockSize  Return the length of real used cipher block in bytes

Note:
    Reference to FIPS-PUB 197
    1. Check if block size is 16 bytes(128 bits) and if key length is 16, 24, or 32 bytes(128, 192, or 256 bits)
    2. Transfer the plain block to state block 
    3. Main encryption rounds
    4. Transfer the state block to cipher block
    ------------------------------------------
       NumberOfRound = (key length / 4) + 6;
       state block = plain block;
       
       AddRoundKey(state block, key);
       for round = 1 to NumberOfRound
           SubBytes(state block)
           ShiftRows(state block)
           MixColumns(state block)
           AddRoundKey(state block, key);
       end for

       SubBytes(state block)
       ShiftRows(state block)
       AddRoundKey(state block, key);

       cipher block = state block;
========================================================================
*/
VOID AES_Encrypt (
    IN UINT8 PlainBlock[],
    IN UINT PlainBlockSize,
    IN UINT8 Key[],
    IN UINT KeyLength,
    OUT UINT8 CipherBlock[],
    INOUT UINT *CipherBlockSize)
{
    AES_CTX_STRUC aes_ctx;
    UINT RowIndex, ColumnIndex;
    UINT RoundIndex, NumberOfRound = 0;
    UINT8 Temp, Row0, Row1, Row2, Row3;

    /*   
     * 1. Check if block size is 16 bytes(128 bits) and if key length is 16, 24, or 32 bytes(128, 192, or 256 bits) 
     */
    if (PlainBlockSize != AES_BLOCK_SIZES) {
    	DBGPRINT(RT_DEBUG_ERROR, ("AES_Encrypt: plain block size is %d bytes, it must be %d bytes(128 bits).\n", 
            PlainBlockSize, AES_BLOCK_SIZES));
        return;
    } /* End of if */
    if ((KeyLength != AES_KEY128_LENGTH) && (KeyLength != AES_KEY192_LENGTH) && (KeyLength != AES_KEY256_LENGTH)) {
    	DBGPRINT(RT_DEBUG_ERROR, ("AES_Encrypt: key length is %d bytes, it must be %d, %d, or %d bytes(128, 192, or 256 bits).\n", 
            KeyLength, AES_KEY128_LENGTH, AES_KEY192_LENGTH, AES_KEY256_LENGTH));
        return;
    } /* End of if */
    if (*CipherBlockSize < AES_BLOCK_SIZES) {
    	DBGPRINT(RT_DEBUG_ERROR, ("AES_Encrypt: cipher block size is %d bytes, it must be %d bytes(128 bits).\n", 
            *CipherBlockSize, AES_BLOCK_SIZES));
        return;
    } /* End of if */

    /* 
     * 2. Transfer the plain block to state block 
     */
    for (RowIndex = 0; RowIndex < AES_STATE_ROWS;RowIndex++)
        for (ColumnIndex = 0; ColumnIndex < AES_STATE_COLUMNS;ColumnIndex++)
            aes_ctx.State[RowIndex][ColumnIndex] = PlainBlock[RowIndex + 4*ColumnIndex];

    /* 
     *  3. Main encryption rounds
     */
    AES_KeyExpansion(Key, KeyLength, &aes_ctx);
    NumberOfRound = (KeyLength >> 2) + 6;

    /* AES_AddRoundKey */
    RoundIndex = 0;
    for (RowIndex = 0; RowIndex < AES_STATE_ROWS;RowIndex++)
        for (ColumnIndex = 0; ColumnIndex < AES_STATE_COLUMNS;ColumnIndex++)
            aes_ctx.State[RowIndex][ColumnIndex] ^= aes_ctx.KeyWordExpansion[RowIndex][(RoundIndex*((UINT) AES_STATE_COLUMNS)) + ColumnIndex];

    for (RoundIndex = 1; RoundIndex < NumberOfRound;RoundIndex++)
    {
        /* AES_SubBytes */
        for (RowIndex = 0; RowIndex < AES_STATE_ROWS;RowIndex++)
            for (ColumnIndex = 0; ColumnIndex < AES_STATE_COLUMNS;ColumnIndex++)
                aes_ctx.State[RowIndex][ColumnIndex] = aes_sbox_enc[aes_ctx.State[RowIndex][ColumnIndex]];

        /* AES_ShiftRows */
        Temp = aes_ctx.State[1][0];
        aes_ctx.State[1][0] = aes_ctx.State[1][1];
        aes_ctx.State[1][1] = aes_ctx.State[1][2];
        aes_ctx.State[1][2] = aes_ctx.State[1][3];
        aes_ctx.State[1][3] = Temp;
        Temp = aes_ctx.State[2][0];
        aes_ctx.State[2][0] = aes_ctx.State[2][2];
        aes_ctx.State[2][2] = Temp;
        Temp = aes_ctx.State[2][1];
        aes_ctx.State[2][1] = aes_ctx.State[2][3];
        aes_ctx.State[2][3] = Temp;
        Temp = aes_ctx.State[3][3];
        aes_ctx.State[3][3] = aes_ctx.State[3][2];
        aes_ctx.State[3][2] = aes_ctx.State[3][1];
        aes_ctx.State[3][1] = aes_ctx.State[3][0];
        aes_ctx.State[3][0] = Temp;

        /* AES_MixColumns */
        for (ColumnIndex = 0; ColumnIndex < AES_STATE_COLUMNS;ColumnIndex++)
        {
            Row0 = aes_ctx.State[0][ColumnIndex];
            Row1 = aes_ctx.State[1][ColumnIndex];
            Row2 = aes_ctx.State[2][ColumnIndex];
            Row3 = aes_ctx.State[3][ColumnIndex];
            aes_ctx.State[0][ColumnIndex] = aes_mul_2[Row0]^aes_mul_3[Row1]^Row2^Row3;
            aes_ctx.State[1][ColumnIndex] = Row0^aes_mul_2[Row1]^aes_mul_3[Row2]^Row3;
            aes_ctx.State[2][ColumnIndex] = Row0^Row1^aes_mul_2[Row2]^aes_mul_3[Row3];
            aes_ctx.State[3][ColumnIndex] = aes_mul_3[Row0]^Row1^Row2^aes_mul_2[Row3];
        }

        /* AES_AddRoundKey */
        for (RowIndex = 0; RowIndex < AES_STATE_ROWS;RowIndex++)
            for (ColumnIndex = 0; ColumnIndex < AES_STATE_COLUMNS;ColumnIndex++)
                aes_ctx.State[RowIndex][ColumnIndex] ^= aes_ctx.KeyWordExpansion[RowIndex][(RoundIndex*((UINT) AES_STATE_COLUMNS)) + ColumnIndex];
    } /* End of for */

    /* AES_SubBytes */
    for (RowIndex = 0; RowIndex < AES_STATE_ROWS;RowIndex++)
        for (ColumnIndex = 0; ColumnIndex < AES_STATE_COLUMNS;ColumnIndex++)
            aes_ctx.State[RowIndex][ColumnIndex] = aes_sbox_enc[aes_ctx.State[RowIndex][ColumnIndex]];
    /* AES_ShiftRows */
    Temp = aes_ctx.State[1][0];
    aes_ctx.State[1][0] = aes_ctx.State[1][1];
    aes_ctx.State[1][1] = aes_ctx.State[1][2];
    aes_ctx.State[1][2] = aes_ctx.State[1][3];
    aes_ctx.State[1][3] = Temp;
    Temp = aes_ctx.State[2][0];
    aes_ctx.State[2][0] = aes_ctx.State[2][2];
    aes_ctx.State[2][2] = Temp;
    Temp = aes_ctx.State[2][1];
    aes_ctx.State[2][1] = aes_ctx.State[2][3];
    aes_ctx.State[2][3] = Temp;
    Temp = aes_ctx.State[3][3];
    aes_ctx.State[3][3] = aes_ctx.State[3][2];
    aes_ctx.State[3][2] = aes_ctx.State[3][1];
    aes_ctx.State[3][1] = aes_ctx.State[3][0];
    aes_ctx.State[3][0] = Temp;
    /* AES_AddRoundKey */
    for (RowIndex = 0; RowIndex < AES_STATE_ROWS;RowIndex++)
        for (ColumnIndex = 0; ColumnIndex < AES_STATE_COLUMNS;ColumnIndex++)
            aes_ctx.State[RowIndex][ColumnIndex] ^= aes_ctx.KeyWordExpansion[RowIndex][(RoundIndex*((UINT) AES_STATE_COLUMNS)) + ColumnIndex];

    /* 
     * 4. Transfer the state block to cipher block 
     */
    for (RowIndex = 0; RowIndex < AES_STATE_ROWS;RowIndex++)
        for (ColumnIndex = 0; ColumnIndex < AES_STATE_COLUMNS;ColumnIndex++)
            CipherBlock[RowIndex + 4*ColumnIndex] = aes_ctx.State[RowIndex][ColumnIndex];

    *CipherBlockSize = ((UINT) AES_STATE_ROWS)*((UINT) AES_STATE_COLUMNS);
} /* End of AES_Encrypt */


/*
========================================================================
Routine Description:
    AES decryption

Arguments:
    CipherBlock      The block of cipher text, 16 bytes(128 bits) each block
    CipherBlockSize  The length of block of cipher text in bytes
    Key              Cipher key, it may be 16, 24, or 32 bytes (128, 192, or 256 bits)
    KeyLength        The length of cipher key in bytes
    PlainBlockSize   The length of allocated plain block in bytes

Return Value:
    PlainBlock       Return plain text
    PlainBlockSize  Return the length of real used plain block in bytes

Note:
    Reference to FIPS-PUB 197
    1. Check if block size is 16 bytes(128 bits) and if key length is 16, 24, or 32 bytes(128, 192, or 256 bits)
    2. Transfer the cipher block to state block 
    3. Main decryption rounds
    4. Transfer the state block to plain block
    ------------------------------------------
       NumberOfRound = (key length / 4) + 6;
       state block = cipher block;
       
       AddRoundKey(state block, key);
       for round = NumberOfRound to 1
           InvSubBytes(state block)
           InvShiftRows(state block)
           InvMixColumns(state block)
           AddRoundKey(state block, key);
       end for

       InvSubBytes(state block)
       InvShiftRows(state block)
       AddRoundKey(state block, key);

       plain block = state block;
========================================================================
*/
VOID AES_Decrypt (
    IN UINT8 CipherBlock[],
    IN UINT CipherBlockSize,
    IN UINT8 Key[],
    IN UINT KeyLength,
    OUT UINT8 PlainBlock[],
    INOUT UINT *PlainBlockSize)
{
    AES_CTX_STRUC aes_ctx;
    UINT RowIndex, ColumnIndex;
    UINT RoundIndex, NumberOfRound = 0;
    UINT8 Temp, Row0, Row1, Row2, Row3;

    /*   
     * 1. Check if block size is 16 bytes(128 bits) and if key length is 16, 24, or 32 bytes(128, 192, or 256 bits) 
     */
    if (*PlainBlockSize < AES_BLOCK_SIZES) {
    	DBGPRINT(RT_DEBUG_ERROR, ("AES_Decrypt: plain block size is %d bytes, it must be %d bytes(128 bits).\n", 
            *PlainBlockSize, AES_BLOCK_SIZES));
        return;
    } /* End of if */
    if ((KeyLength != AES_KEY128_LENGTH) && (KeyLength != AES_KEY192_LENGTH) && (KeyLength != AES_KEY256_LENGTH)) {
    	DBGPRINT(RT_DEBUG_ERROR, ("AES_Decrypt: key length is %d bytes, it must be %d, %d, or %d bytes(128, 192, or 256 bits).\n", 
            KeyLength, AES_KEY128_LENGTH, AES_KEY192_LENGTH, AES_KEY256_LENGTH));
        return;
    } /* End of if */
    if (CipherBlockSize != AES_BLOCK_SIZES) {
    	DBGPRINT(RT_DEBUG_ERROR, ("AES_Decrypt: cipher block size is %d bytes, it must be %d bytes(128 bits).\n", 
            CipherBlockSize, AES_BLOCK_SIZES));
        return;
    } /* End of if */

    /* 
     * 2. Transfer the cipher block to state block 
     */
    for (RowIndex = 0; RowIndex < AES_STATE_ROWS;RowIndex++)
        for (ColumnIndex = 0; ColumnIndex < AES_STATE_COLUMNS;ColumnIndex++)
            aes_ctx.State[RowIndex][ColumnIndex] = CipherBlock[RowIndex + 4*ColumnIndex];

    /* 
     *  3. Main decryption rounds
     */
    AES_KeyExpansion(Key, KeyLength, &aes_ctx);
    NumberOfRound = (KeyLength >> 2) + 6;

    /* AES_AddRoundKey */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -