cancel
Showing results for 
Search instead for 
Did you mean: 

CRYP_AES Funtions Always Return 0

argps gtprest
Associate
Posted on March 28, 2017 at 12:38

Hi

I m using stm32f4 for my project. My project has AES encryption and decryption parts. I using standard peripheral librarys.

Standard peripheral library has

http://www.hanese.nl/STM32/stm32f4stdlibrary/html/group___c_r_y_p.html

. I use this library for AES process.

I generate codes like

https://github.com/g4lvanix/STM32F4-workarea/blob/master/Project/STM32F4xx_StdPeriph_Examples/CRYP/CRYP_AESmodes/main.c

and my codes is following lines.

uint8_t AES256key[32] = {0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,

0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81, 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7, 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4};

uint8_t IV_1[16] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,

0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};

uint8_t Plaintext[AES_TEXT_SIZE] =

{0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96, 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a, 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c, 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51, 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11, 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef, 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17, 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10};

uint8_t Ciphertext[AES_TEXT_SIZE] =

{0x76,0x49,0xab,0xac,0x81,0x19,0xb2,0x46, 0xce,0xe9,0x8e,0x9b,0x12,0xe9,0x19,0x7d, 0x50,0x86,0xcb,0x9b,0x50,0x72,0x19,0xee, 0x95,0xdb,0x11,0x3a,0x91,0x76,0x78,0xb2, 0x73,0xbe,0xd6,0xb8,0xe3,0xc1,0x74,0x3b, 0x71,0x16,0xe6,0x9e,0x22,0x22,0x95,0x16, 0x3f,0xf1,0xca,0xa1,0x68,0x1f,0xac,0x09, 0x12,0x0e,0xca,0x30,0x75,0x86,0xe1,0xa7};

uint8_t Encryptedtext[AES_TEXT_SIZE];

uint8_t Decryptedtext[AES_TEXT_SIZE];

uint32_t multiplier;

void TM_Delay_Init(void) {

RCC_ClocksTypeDef RCC_Clocks; RCC_GetClocksFreq(&RCC_Clocks); multiplier = RCC_Clocks.HCLK_Frequency / 4000000; }

void TM_DelayMicros(uint32_t micros) {

micros = micros * multiplier - 10; while (micros--); }

void TM_DelayMillis(uint32_t millis) {

millis = 1000 * millis * multiplier - 10; while (millis--); }

static void Display_PlainData(uint32_t datalength)

{ uint32_t BufferCounter =0; uint32_t count = 0; char str[128];

serial1WriteStr(' Plain Data :\n');

for(BufferCounter = 0; BufferCounter < datalength; BufferCounter++)

{ sprintf(str,'%d , ', Plaintext[BufferCounter]); serial1WriteStr(str); count++;

if(count == 16)

{ count = 0; sprintf(str,' Block %d \n\r', BufferCounter/16); serial1WriteStr(str); } } memset(str, 0, sizeof(str)); TM_DelayMillis(125); } static void Display_EncryptedData(uint32_t datalength) {

uint32_t BufferCounter =0;

uint32_t count = 0; char str[128];

serial1WriteStr(' Encrypted Data :\n');

for(BufferCounter = 0; BufferCounter < datalength; BufferCounter++)

{ sprintf(str,'%d , ', Encryptedtext[BufferCounter]); serial1WriteStr(str); count++;

if(count == 16)

{ count = 0; sprintf(str,' Block %d \n\r', BufferCounter/16); serial1WriteStr(str); } } memset(str, 0, sizeof(str)); TM_DelayMillis(125); }

static void Display_DecryptedData(uint32_t datalength)

{

uint32_t BufferCounter =0;

uint32_t count = 0; char str[128];

serial1WriteStr(' Decrypted Data :\n');

for(BufferCounter = 0; BufferCounter < datalength; BufferCounter++)

{ sprintf(str,'%d , ', Decryptedtext[BufferCounter]); serial1WriteStr(str); count++;

if(count == 16)

{ count = 0; sprintf(str,' Block %d \n\r', BufferCounter/16); serial1WriteStr(str); } } memset(str, 0, sizeof(str)); TM_DelayMillis(125); } int main(void) {

RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE);

serial1Init(115200); TM_Delay_Init(); Display_PlainData(AES_TEXT_SIZE);

while(1)

{

if(CRYP_AES_CTR(MODE_ENCRYPT,IV_1,AES256key,256,Plaintext,AES_TEXT_SIZE,Encryptedtext) == SUCCESS)

{ Display_EncryptedData(AES_TEXT_SIZE); }

if(CRYP_AES_CTR(MODE_DECRYPT,IV_1,AES256key, 256,Ciphertext, AES_TEXT_SIZE,Decryptedtext) == SUCCESS)

{ Display_DecryptedData(AES_TEXT_SIZE); }

TM_DelayMillis(3000);

} }

When i run the codes i see following outputs,

 Plain Data :
107 , 193 , 190 , 226 , 46 , 64 , 159 , 150 , 233 , 61 , 126 , 17 , 115 , 147 , 23 , 42 , Block 0
174 , 45 , 138 , 87 , 30 , 3 , 172 , 156 , 158 , 183 , 111 , 172 , 69 , 175 , 142 , 81 , Block 1
48 , 200 , 28 , 70 , 163 , 92 , 228 , 17 , 229 , 251 , 193 , 25 , 26 , 10 , 82 , 239 , Block 2 
46 , 159 , 36 , 69 , 223 , 79 , 155 , 23 , 173 , 43 , 65 , 123 , 230 , 108 , 55 , 16 , Block 3
Encrypted Data :
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , Block 0
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , Block 1 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , Block 2 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , Block 3 
Decrypted Data :
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , Block 0 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , Block 1
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , Block 2 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , Block 3

The encrypted and decrypted datas always return zero. What am i mistake ?

#iar #crypto #aes #stm32f407 #stm32-standard-peripheral-librar
4 REPLIES 4
Nesrine M_O
Lead II
Posted on March 28, 2017 at 14:57

Hi

test.argprs

,

Could you please precise the STM32F4 part that you are using?

-Nesrine-

Posted on March 28, 2017 at 15:05

Unfortunately your code doesn't compile, the delay routines might be folded by the compiler.

Make sure the PLL is enabled, and that the Q-tap clocks at, or below, 48 MHz

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
argps gtprest
Associate
Posted on March 28, 2017 at 17:37

Thanks for your fast responses

ELMHIRI.Syrine

‌ im using STM32F40

7VGT6

Turvey.Clive.002

‌ The error may be caused of my PLL settings my PLL is enabled but sytem clock is 168Mhz. I cant find Q-tap clocks value.

Nesrine M_O
Lead II
Posted on March 29, 2017 at 10:35

Hi

test.argprs

,

The

STM32F40

7VGT6 does not support theCryptographic processor.

I

recommend

you to use the

STM32 cryptographic library package (

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-expansion-software/x-cube-cryptolib.html

).

-Nesrine-