This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C129 AES CBC模式加解密官方例子都没有看到在哪里设置填充模式

但是整个工程是可以实现加密解密, 我测试过不用16的倍数,加解密出来的密文长度与明文长度是一致。 

有点疑惑,哪位帮忙解疑一下

  • //*****************************************************************************
    //
    //! Used to process(transform) blocks of data, either encrypt or decrypt it.
    //!
    //! \param ui32Base is the base address of the AES module.
    //! \param pui32Src is a pointer to the memory location where the input data
    //! is stored. The data must be padded to the 16-byte boundary.
    //! \param pui32Dest is a pointer to the memory location output is written.
    //! The space for written data must be rounded up to the 16-byte boundary.
    //! \param ui32Length is the length of the cryptographic data in bytes.
    //!
    //! This function iterates the encryption or decryption mechanism number over
    //! the data length. Before calling this function, ensure that the AES
    //! module is properly configured the key, data size, mode, etc. Only ECB,
    //! CBC, CTR, ICM, CFB, XTS and F8 operating modes should be used. The data
    //! is processed in 4-word (16-byte) blocks.
    //!
    //! \note This function only supports values of \e ui32Length less than 2^32,
    //! because the memory size is restricted to between 0 to 2^32 bytes.
    //!
    //! \return Returns true if data was processed successfully. Returns false
    //! if data processing failed.
    //
    //*****************************************************************************
    bool AESDataProcess(uint32_t ui32Base, uint32_t *pui32Src, uint32_t *pui32Dest, uint32_t ui32Length)
    {
    uint32_t ui32Count;

    //
    // Check the arguments.
    //
    ASSERT(ui32Base == AES_BASE);

    //
    // Write the length register first, which triggers the engine to start
    // using this context.
    //
    AESLengthSet(AES_BASE, (uint64_t)ui32Length);

    //
    // Now loop until the blocks are written.
    //
    for(ui32Count = 0; ui32Count < ui32Length; ui32Count += 16) {//19
    //
    // Write the data registers.
    //
    AESDataWrite(ui32Base, pui32Src + (ui32Count / 4));

    //
    // Read the data registers.
    //
    AESDataRead(ui32Base, pui32Dest + (ui32Count / 4));
    }

    //
    // Return true to indicate successful completion of the function.
    //
    return(true);
    }

    //*****************************************************************************
    //
    //! Used to set the write crypto data length in the AES module.
    //!
    //! \param ui32Base is the base address of the AES module.
    //! \param ui64Length is the crypto data length in bytes.
    //!
    //! This function stores the cryptographic data length in blocks for all modes.
    //! Data lengths up to (2^61 - 1) bytes are allowed. For GCM, any value up
    //! to (2^36 - 2) bytes are allowed because a 32-bit block counter is used.
    //! For basic modes (ECB/CBC/CTR/ICM/CFB128), zero can be programmed into the
    //! length field, indicating that the length is infinite.
    //!
    //! When this function is called, the engine is triggered to start using
    //! this context.
    //!
    //! \note This length does not include the authentication-only data used in
    //! some modes. Use the AESAuthLengthSet() function to specify the
    //! authentication data length.
    //!
    //! \return None
    //
    //*****************************************************************************
    void AESLengthSet(uint32_t ui32Base, uint64_t ui64Length)
    {
    //
    // Check the arguments.
    //
    ASSERT(ui32Base == AES_BASE);

    //
    // Write the length register by shifting the 64-bit ui64Length.
    //
    HWREG(ui32Base + AES_O_C_LENGTH_0) = (uint32_t)(ui64Length);
    HWREG(ui32Base + AES_O_C_LENGTH_1) = (uint32_t)(ui64Length >> 32);
    }

    AESDataProcess这个函数定义明文与密文是16字节的倍数,而我的程序试过好多不是16的倍数都可以正确的加密解密
    #define ENCRYPT_LEN_MAX 19
    uint8_t g_pui8AESPlainText[ENCRYPT_LEN_MAX];
    uint8_t g_pui8AESCipherText[ENCRYPT_LEN_MAX];
    uint32_t ui32Keysize, startTick,endTick;
    void AES_cbcTest(void)
    { uint32_t i;
    #if 1
    for(i=0;i<ENCRYPT_LEN_MAX;i++)g_pui8AESPlainText[i]=i;
    AESCBCEncrypt(gTcpAesPara.Mode, (uint32_t*)g_pui8AESPlainText, (uint32_t*)g_pui8AESCipherText,
    (uint32_t*)gTcpAesPara.Aes192,
    (uint32_t*)gTcpAesPara.AESIV,
    ENCRYPT_LEN_MAX); //字节数
    //解密

    for(i=0;i<ENCRYPT_LEN_MAX;i++)g_pui8AESPlainText[i]=0;
    AESCBCDecrypt(gTcpAesPara.Mode,
    (uint32_t*)g_pui8AESCipherText,
    (uint32_t*)g_pui8AESPlainText,
    (uint32_t*)gTcpAesPara.Aes192,
    (uint32_t*)gTcpAesPara.AESIV,
    ENCRYPT_LEN_MAX);

    #endif
    }
  • 这个问题我已经转给了相关的同事,请您等待一下
  • 已经知道 明文若不是16的倍数,实际操作的是16的倍数,因此明文需要16的倍数!