Question
AES_CBC_Encrypt and AES_CBC_Decrypt don't yield the same result again.
I'm using an STM32L4A6ZG (as a nucleo board). This is the code I am using:
#include "err_codes.h"
#include "sk.h"
#include "aes_common.h"
#include "aes_cbc.h"
#define AES_BLOCK 16
#define IV_LENGTH 16
#define SIZE 32__ALIGN_BEGIN static const uint8_t pKeyAES[16] __ALIGN_END = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00};
__ALIGN_BEGIN static const uint8_t pInitVectAES[16] __ALIGN_END = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00};
// Test Data and some return values for debugging
// 19 32 4b 64 7d 96 c8 fa 18 31 4a 63 7c 95 19 18 0 1 5 9 6d 1d 63 58 6c 6b 3b 1e 9 6 3 39
uint8_t Plaintext[SIZE] = {25,50,75,100,125,150,200,250,
24,49,74,99,124,149,25,24,
0, 1, 5, 9, 109, 29, 99, 88,
108, 107, 59, 30, 9, 6, 3, 57};
uint8_t tx[SIZE] = {0};AESCBCctx_stt AESctx;
uint32_t error_status = AES_SUCCESS;
int32_t outputLength = 0;
/* Set flag field to default value */
AESctx.mFlags = E_SK_DEFAULT; //AccHw_E_SK_DEFAULT
AESctx.mKeySize = AES_BLOCK;
/* Set iv size field to IvLength*/
AESctx.mIvSize = IV_LENGTH;
error_status = AES_CBC_Encrypt_Init(&AESctx, pKeyAES,
pInitVectAES );
/* check for initialization errors */
if (error_status == AES_SUCCESS) {
/* Encrypt Data */
error_status = AES_CBC_Encrypt_Append(&AESctx,Plaintext,SIZE,
tx,&outputLength);
if (error_status == AES_SUCCESS) {
/* Do the Finalization */
error_status = AES_CBC_Encrypt_Finish(&AESctx, tx +
SIZE, &outputLength);
}
}
AESCBCctx_stt AESctxD;
outputLength = 0;
/* Set flag field to default value */
AESctxD.mFlags = E_SK_DEFAULT; //AccHw_E_SK_DEFAULT
AESctxD.mKeySize = AES_BLOCK;
/* Set iv size field to IvLength*/
AESctxD.mIvSize = IV_LENGTH;
error_status = AES_CBC_Decrypt_Init(&AESctxD, pKeyAES,
pInitVectAES );
/* check for initialization errors */
if (error_status == AES_SUCCESS) {
/* Encrypt Data */
error_status = AES_CBC_Decrypt_Append(&AESctxD,tx,SIZE,
Plaintext,&outputLength);
if (error_status == AES_SUCCESS) {
/* Do the Finalization */
error_status = AES_CBC_Decrypt_Finish(&AESctxD, Plaintext +
SIZE, &outputLength);
}
}I have tried this with b128 and b256 keys. The results are different from the inputs, but the decrypted text isn't the plain text again.
If I'm correct, the encrypted message should be
6f ad 39 3a e1 c9 8c 33 05 d4 03 91 a5 7d bc 66 83 32 66 37 79 ec 45 d3 1b 75 bd d8 cd f9 a6 81but the output is different, namely it starts with 3d ed...
Any ideas?
