我目前正在使用 HAL 试用 STM32WB55 的 AES 加速器。它本身运行良好,但我无法获得与 MbedTLS 匹配的结果。因此,加速器生成的密文和标签与 MbedTLS 生成的密文和标签不匹配,并且使用 MbedTLS 对加速器密文的解密失败(标签和明文不匹配)。
在检查不同配置选项的过程中,我偶然发现了标头大小。STM32CubeMX 在没有明显原因的情况下强制标头大小为 1。我尝试了标题大小为 0 和 1 的 Accelerator 和 MbedTLS(通过手动更改 CubeMX 生成的代码,使用 MbedTLS 称为附加数据),两者都不匹配。
CubeMX 强制标头大小为 1 是否有原因?
我附加了相关代码,在本例中标题大小为 0。我是不是在某处犯了错误?还是有其他隐藏的原因?
- CRYP_HandleTypeDef hcryp1;
- __ALIGN_BEGIN uint32_t pKeyAES1[8] __ALIGN_END = {
- 0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000};
- __ALIGN_BEGIN uint32_t pInitVectAES1[4] __ALIGN_END = {
- 0x00000000,0x00000000,0x00000000,0x00000000};
- __ALIGN_BEGIN uint32_t HeaderAES1[1] __ALIGN_END = {
- 0x00000000};
- void MX_AES1_Init(void)
- {
- hcryp1.Instance = AES1;
- hcryp1.Init.DataType = CRYP_DATATYPE_8B;
- hcryp1.Init.KeySize = CRYP_KEYSIZE_256B;
- hcryp1.Init.pKey = (uint32_t *)pKeyAES1;
- hcryp1.Init.pInitVect = (uint32_t *)pInitVectAES1;
- hcryp1.Init.Algorithm = CRYP_AES_GCM_GMAC;
- hcryp1.Init.Header = NULL;
- hcryp1.Init.HeaderSize = 0;
- hcryp1.Init.DataWidthUnit = CRYP_DATAWIDTHUNIT_BYTE;
- hcryp1.Init.KeyIVConfigSkip = CRYP_KEYIVCONFIG_ALWAYS;
- if (HAL_CRYP_Init(&hcryp1) != HAL_OK)
- {
- Error_Handler();
- }
- }
- int main(void)
- {
- // HAL Initialization...
- MX_AES1_Init();
- #define GCM_TAG_LEN 16
- #define IV_LEN 16
- #define KEYSIZE 32
- #define DATA_SIZE 256
- printf("Debugging AES accelerator...n");
- // Initialize data
- uint8_t dummyData[DATA_SIZE];
- memset(dummyData, 5, DATA_SIZE);
- uint8_t dummyKey[KEYSIZE];
- memset(dummyKey, 9, KEYSIZE);
- uint8_t dummyIV[IV_LEN];
- memset(dummyIV, 1, IV_LEN);
- uint8_t dummyHeader[4] = {0};
- //Encrypt and decrypt dummy data using the AES-Accelerator & AES-GCM
- uint8_t accCipher[DATA_SIZE];
- uint8_t accTag[GCM_TAG_LEN];
- memcpy(pKeyAES1, dummyKey, KEYSIZE);
- memcpy(pInitVectAES1, dummyIV, IV_LEN);
- HAL_StatusTypeDef halstatus = HAL_CRYP_Encrypt(&hcryp1, (uint32_t *) dummyData, DATA_SIZE, (uint32_t *) accCipher, 1000);
- if (halstatus != HAL_OK) {
- printf("Debug: HAL failed during encryption of data!");
- }
- halstatus = HAL_CRYPEx_AESGCM_GenerateAuthTAG(&hcryp1, (uint32_t *) accTag, 1000);
- if (halstatus != HAL_OK) {
- printf("Debug: HAL failed during encryption -> tagging of data!");
- }
- uint8_t accCleartext[DATA_SIZE];
- uint8_t accTag2[GCM_TAG_LEN];
- halstatus = HAL_CRYP_Decrypt(&hcryp1, (uint32_t *) accCipher, DATA_SIZE, (uint32_t *) accCleartext, 1000);
- if (halstatus != HAL_OK) {
- printf("Debug: HAL failed during decryption of data!");
- }
- HAL_CRYPEx_AESGCM_GenerateAuthTAG(&hcryp1, (uint32_t *) accTag2, 1000);
- //check accelerator result for inconsistencies
- if (halstatus != HAL_OK) {
- printf("Debug: HAL failed during decryption -> tagging of data!");
- }
- if (memcmp(dummyData, accCleartext, DATA_SIZE) != 0) {
- printf("Debug: AES encryption / decryption did not match!n");
- }
- if (memcmp(accTag, accTag2, GCM_TAG_LEN) != 0) {
- printf("Debug: AES Tags did not match.n");
- }
- // Encrypt and decrypt dummy data using MbedTLS & AES-GCM
- uint8_t mbedCipher[DATA_SIZE];
- uint8_t mbedTag[GCM_TAG_LEN];
- mbedtls_gcm_context c;
- mbedtls_gcm_init(&c);
- mbedtls_gcm_setkey(&c, MBEDTLS_CIPHER_ID_AES, dummyKey, KEYSIZE*8);
- int ret2 = mbedtls_gcm_crypt_and_tag(&c, MBEDTLS_GCM_ENCRYPT, DATA_SIZE, dummyIV, IV_LEN, NULL, 0, dummyData, mbedCipher, GCM_TAG_LEN, mbedTag);
- uint8_t mbedCleartext[DATA_SIZE];
- uint8_t mbedTag2[GCM_TAG_LEN];
- int ret3 = mbedtls_gcm_crypt_and_tag(&c, MBEDTLS_GCM_DECRYPT, DATA_SIZE, dummyIV, IV_LEN, NULL, 0, mbedCipher, mbedCleartext, GCM_TAG_LEN, mbedTag2);
- if (ret2 != 0 || ret3 != 0) {
- printf("Debug: MbedTLS failed.n");
- }
- // Check MbedTLS result against accelerator results & for internal MbedTLS inconsistencies
- if (memcmp(mbedCipher, accCipher, DATA_SIZE) != 0) {
- printf("Debug: Ciphers do not match between AES acc and Mbed!n");
- }
- if (memcmp(mbedCleartext, accCleartext, DATA_SIZE) != 0) {
- printf("Debug: Clear text does not match between AES acc and Mbed!n");
- }
- if (memcmp(mbedTag, accTag, GCM_TAG_LEN) != 0) {
- printf("Debug: Tags do not match between AES acc and Mbed!n");
- }
- if (memcmp(dummyData, mbedCleartext, DATA_SIZE) != 0) {
- printf("Debug: MbedTLS encrypted data does not match initial data!n");
- }
- if (memcmp(mbedTag, mbedTag2, GCM_TAG_LEN) != 0) {
- printf("Debug: MbedTLS tags do not match each other!n");
- }
- // Try do decrypt accelerator Ciphertext using MbedTLS
- uint8_t accToMbedCleartext[DATA_SIZE];
- uint8_t accToMbedTag2[GCM_TAG_LEN];
- int ret4 = mbedtls_gcm_crypt_and_tag(&c, MBEDTLS_GCM_DECRYPT, DATA_SIZE, dummyIV, IV_LEN, NULL, 0, accCipher, accToMbedCleartext, GCM_TAG_LEN, accToMbedTag2);
- if(ret4 != 0) {
- printf("MbedTLS failed.");
- }
- // Check resulting cleartext against initial data and MbedTLS tag against accelerator tag
- if(memcmp(accToMbedCleartext, dummyData, DATA_SIZE) != 0) {
- printf("Debug: MbedTLS decryption of accelerator ciphertext does not match initial data.n");
- }
- if(memcmp(accToMbedTag2, accTag, GCM_TAG_LEN) != 0) {
- printf("Debug: MbedTLS decryption tag does not match accelerator tag.n");
- }
- }
这是结果输出:
- Debugging AES accelerator...
- Debug: Ciphers do not match between AES acc and Mbed!
- Debug: Tags do not match between AES acc and Mbed!
- Debug: MbedTLS decryption of accelerator ciphertext does not match initial data.
- Debug: MbedTLS decryption tag does not match accelerator tag.
0
|
|
|
|