- Encrypts AES using CBC block mode.
密码分组链接模式(Cipher Block Chaining (CBC))
这种模式是先将明文切分成若干小段,然后每一小段与初始块或者上一段的密文段进行异或运算后,再与密钥进行加密。
void TestAesCbc(void)
{
static const uint8_t keyAes128[] __attribute__((aligned)) = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
static const uint8_t plainAes128[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
static const uint8_t ive[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
static const uint8_t cipherAes128[] = {0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d};
#if DCP_TEST_USE_OTP_KEY
#warning Please update cipherAes128 variables to match expected AES ciphertext for your OTP key.
#endif
uint8_t cipher[16];
uint8_t output[16];
status_t status;
dcp_handle_t m_handle;
m_handle.channel = kDCP_Channel0;
m_handle.swapConfig = kDCP_NoSwap;
#if DCP_TEST_USE_OTP_KEY
m_handle.keySlot = kDCP_OtpKey;
#else
m_handle.keySlot = kDCP_KeySlot0;
#endif
/* sets the AES key for encryption/decryption with the dcp_handle_t structure */
status = DCP_AES_SetKey(DCP, &m_handle, keyAes128, 16);
TEST_ASSERT(kStatus_Success == status);
/* 对plainAes128数据进行加密 输出加密文件 cipher */
DCP_AES_EncryptCbc(DCP, &m_handle, plainAes128, cipher, 16, ive);
TEST_ASSERT(memcmp(cipher, cipherAes128, 16) == 0);
/* 对cipher文件进行解密 并输出解密文件 output */
DCP_AES_DecryptCbc(DCP, &m_handle, cipher, output, 16, ive);
/* 比较解密文件output 和 原始文件planAes128 */
TEST_ASSERT(memcmp(output, plainAes128, 16) == 0);
PRINTF("AES CBC Test pass\r\n");
}