STM32 cryptografic AES CCM libraries giving wrong results
Hello everyone
I am trying to use the crypto library to perform an AES CCM encryption decryption. I am using an STM32F423CH MCU. The frames I am receiving are comming from other system and the authentication is always failing.
I implemted the validation scheme that appears in the NIST Special Publication 800-38C for CCM.
//FROM THE EXAMPLE IN nvlpubs.nist.gov
const uint8_t key[] = {0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F};
uint8_t nonce[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
uint8_t header[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
uint8_t data[] = {0x20, 0x21, 0x22, 0x23};
uint8_t outputData[40];
uint8_t outputTag[8];
uint8_t recoveredData[32];
int32_t outputLength = 4;
int32_t outputTagLength;
volatile uint32_t error_status = AES_SUCCESS;
AESCCMctx_stt AESCCMctx;
AESCCMctx_stt* AESctx = &AESCCMctx;
AESctx->mNonceSize = 7;
AESctx->mKeySize = CRL_AES128_KEY;
AESctx->mFlags = E_SK_DEFAULT;
AESctx->mAssDataSize = 8;
AESctx->mTagSize = 4;
AESctx->mPayloadSize = 4;
AESctx->pmTag = outputTag;
error_status = AES_CCM_Encrypt_Init(AESctx, key, nonce);
error_status = AES_CCM_Header_Append(AESctx, header, 8);
error_status = AES_CCM_Encrypt_Append(AESctx, data, 4, outputData, &outputLength);
error_status = AES_CCM_Encrypt_Finish(AESctx, outputData + 4, &outputTagLength);I am using the SMALL implementation from the library. The expected output is:
MSG:0x71 0x62 0x01 0x5b
MAC: 0x4d 0xac 0x25 0x5d
But the result I am getting is:
MSG: 0x02 0x20 0x1d 0x49
MAC: 0x49 0x37 0x36 0xab
Though the decryption with the same libraries gives a positive result
AESctx->mNonceSize = 7;
AESctx->mKeySize = CRL_AES128_KEY;
AESctx->mFlags = E_SK_DEFAULT;
AESctx->mAssDataSize = 8;
AESctx->mTagSize = 4;
AESctx->mPayloadSize = 4;
AESctx->pmTag = outputData + 4;
error_status = AES_CCM_Decrypt_Init(AESctx, key, nonce);
error_status = AES_CCM_Header_Append(AESctx, header, 8);
error_status = AES_CCM_Decrypt_Append(AESctx, outputData, 4, recoveredData, &outputLength);
error_status = AES_CCM_Decrypt_Finish(AESctx, recoveredData, &outputLength);Error_status = 1003 (SUCCESS) and the message match with the original.
Am I doing something wrong? Using the mbedtls libraries is working fine.
Thank you in advance for any support.
