c++ - STM32F4 AES Functions Always Return 0 -
i m using stm32f4 project. project has aes encryption , decryption parts. using standard peripheral librarys.
standard peripheral library has cryp.h. use library aes process.
i generate codes this , codes 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 run codes 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 246 , 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 , decrypted datas return zero. mistake ?
Comments
Post a Comment