Question
X-CUBE-CRYPTOLIB(GCC) on STM32F207: AES incorrect encrypt/decrypt
Posted on February 16, 2016 at 16:01
Hi! I'm using the X-CUBE-CRYPTOLIB (AES-256-ECB) on the STM3220 devboard using System Workbench (GCC) with no luck.
I set the config.h file (tried every permutation nearly), my project compiles successfully, and I am able to run my test program in which I want to encrypt an array then decrypt it immediately. Upon doing so I don't get the original plaintext back (some mumbo-jumbo only). I think I might be missing something small but significant (I used the non X- version in Keil before, and it had a Crypto_Deinit() function which this X- version doesn't have). I pasted my sample code below, can anyone help me?uint8_t key_enc_256[CRL_AES256_KEY]= AES256_KEY;
uint8_t iv[CRL_AES_BLOCK]= AES256_IV;
AESECBctx_stt AESctx_enc; /* The AES context */
AESECBctx_stt AESctx_dec; /* The AES context */
uint8_t test_plain[256];
uint8_t test_cypher[256];
/**
* \brief Initializes AES encrypt/decrypt contexts
* \param None
* \retval None
*/
void AES_init()
{
uint8_t retval;
int32_t i;
/* Initialize Context Flag with default value */
AESctx_enc.mFlags = E_SK_DEFAULT;
/* Set Iv size to 16 NOT USED IN ECB*/
AESctx_enc.mIvSize=32;
/* Set key size to 32 */
AESctx_enc.mKeySize=CRL_AES256_KEY;
/* Initialize Context Flag with default value */
AESctx_dec.mFlags = E_SK_DEFAULT;
/* Set Iv size to 16 NOT USED IN ECB*/
AESctx_dec.mIvSize=32;
/* Set key size to 32 */
AESctx_dec.mKeySize=CRL_AES256_KEY;
retval = AES_ECB_Encrypt_Init(&AESctx_enc, key_enc_256, iv);
if(retval != AES_SUCCESS)
{ while(1); }
retval = AES_ECB_Decrypt_Init(&AESctx_dec, key_enc_256, iv);
if(retval != AES_SUCCESS)
{ while(1); }
for(i = 0; i < 256; i++)
{
test_plain[i] = 0;
}
retval = AES_ECB_Encrypt_Append(&AESctx_enc,test_plain,256,test_cypher,&i);
if(retval != AES_SUCCESS)
{ while(1); }
retval = AES_ECB_Decrypt_Append(&AESctx_dec,test_cypher,256,test_plain,&i);
if(retval != AES_SUCCESS)
{ while(1); }
}
Kind regards,
Daniel
#stm32f407 #crypto #solved