Aes CBC crashes

eowesi
Posts: 11
Joined: Sat Jun 09, 2018 8:55 am

Aes CBC crashes

Postby eowesi » Mon Jul 09, 2018 2:13 pm

This is my code but it crashes:

Code: Select all

mbedtls_aes_context aes;

size_t _length = 16;
unsigned char iv[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
unsigned char key[16] = {0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};

unsigned char input[16] = "HelloEsp32AES**";
unsigned char encrypt_output[48];
unsigned char decrypt_output[48];
void app_main()
{
	int temp = nvs_flash_init();
    if (temp == ESP_ERR_NVS_NO_FREE_PAGES) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        temp = nvs_flash_init();
    }
    memset(encrypt_output, 0, 48);
    memset(decrypt_output, 0, 48);
	size_t iv_offset = 0;
	mbedtls_aes_init(&aes);

	mbedtls_aes_setkey_enc(&aes, key, 128);
	mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_ENCRYPT, 128, &iv_offset, iv, input, encrypt_output);
	mbedtls_aes_free(&aes);
}
It gave me this error:

Code: Select all

Guru Meditation Error: Core  0 panic'ed (StoreProhibited)
. Exception was unhandled.
Can you say where I made a mistake?

ESP_Sprite
Posts: 9711
Joined: Thu Nov 26, 2015 4:08 am

Re: Aes CBC crashes

Postby ESP_Sprite » Tue Jul 10, 2018 2:17 am

It's easier to say if you give the decoded backtrace. Either use 'make monitor' which will decode the backtrace automatically, or throw the backtrace hex addresses into addr2line.

eowesi
Posts: 11
Joined: Sat Jun 09, 2018 8:55 am

Re: Aes CBC crashes

Postby eowesi » Tue Jul 10, 2018 5:12 am

Can you supply simple aes cbc code example?

chegewara
Posts: 2362
Joined: Wed Jun 14, 2017 9:00 pm

Re: Aes CBC crashes

Postby chegewara » Wed Jul 11, 2018 2:21 am

This little trick fix the issue:

Code: Select all

void task(void* p)
{
    memset(encrypt_output, 0, 48);
    memset(decrypt_output, 0, 48);
   size_t iv_offset = 0;
   mbedtls_aes_init(&aes);

   mbedtls_aes_setkey_enc(&aes, key, 128);
   mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_ENCRYPT, 128, &iv_offset, iv, input, encrypt_output);
   mbedtls_aes_free(&aes);
   while(1){
   vTaskDelay(100);
   }
}
void app_main()
{
   
xTaskCreate(task, "task", 1024*10, NULL, 5, NULL); // <--- 10kB is just for example, most likely much less will be good
}
Seems there is not enough in app_main to handle this simple code:

Code: Select all

Guru Meditation Error: Core  0 panic'ed (StoreProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x40084405  PS      : 0x00060333  A0      : 0x80086a23  A1      : 0x3ffb5830
0x40084405: uxPortCompareSet at D:/msys32/home/imper/esp/esp-idf/components/freertos/tasks.c:4609
 (inlined by) vPortCPUAcquireMutexIntsDisabledInternal at D:/msys32/home/imper/esp/esp-idf/components/freertos/portmux_impl.inc.h:86
 (inlined by) vPortCPUAcquireMutexIntsDisabled at D:/msys32/home/imper/esp/esp-idf/components/freertos/portmux_impl.h:98
 (inlined by) vTaskEnterCritical at D:/msys32/home/imper/esp/esp-idf/components/freertos/tasks.c:4254

A2      : 0xe6b8d24f  A3      : 0x00060023  A4      : 0x00060020  A5      : 0x3ffb5930
A6      : 0x00000000  A7      : 0x00000000  A8      : 0x0000cdcd  A9      : 0x0000cdcd
A10     : 0xb33fffff  A11     : 0x0000abab  A12     : 0x00000000  A13     : 0x00000001
A14     : 0x00060320  A15     : 0x00000000  SAR     : 0x00000000  EXCCAUSE: 0x0000001d
EXCVADDR: 0xe6b8d24f  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000

Backtrace: 0x40084405:0x3ffb5830 0x40086a20:0x3ffb5850 0x400826ea:0x3ffb5870 0x40082c49:0x3ffb5890 0x4000bec7:0x3ffb58b0 0x40083f15:0x3ffb58d0 0x40084ab6:0x3ffb58f0 0x40084acb:0x3ffb5910
0x40084405: uxPortCompareSet at D:/msys32/home/imper/esp/esp-idf/components/freertos/tasks.c:4609
 (inlined by) vPortCPUAcquireMutexIntsDisabledInternal at D:/msys32/home/imper/esp/esp-idf/components/freertos/portmux_impl.inc.h:86
 (inlined by) vPortCPUAcquireMutexIntsDisabled at D:/msys32/home/imper/esp/esp-idf/components/freertos/portmux_impl.h:98
 (inlined by) vTaskEnterCritical at D:/msys32/home/imper/esp/esp-idf/components/freertos/tasks.c:4254

0x40086a20: multi_heap_internal_lock at D:/msys32/home/imper/esp/esp-idf/components/heap/multi_heap.c:372
 (inlined by) multi_heap_free_impl at D:/msys32/home/imper/esp/esp-idf/components/heap/multi_heap.c:471

0x400826ea: heap_caps_free at D:/msys32/home/imper/esp/esp-idf/components/heap/heap_caps.c:262

0x40082c49: _free_r at D:/msys32/home/imper/esp/esp-idf/components/newlib/syscalls.c:42

0x40083f15: prvDeleteTCB at D:/msys32/home/imper/esp/esp-idf/components/freertos/tasks.c:4609

0x40084ab6: prvCheckTasksWaitingTermination at D:/msys32/home/imper/esp/esp-idf/components/freertos/tasks.c:4609

0x40084acb: prvIdleTask at D:/msys32/home/imper/esp/esp-idf/components/freertos/tasks.c:4609

eowesi
Posts: 11
Joined: Sat Jun 09, 2018 8:55 am

Re: Aes CBC crashes

Postby eowesi » Wed Jul 11, 2018 8:28 am

Your answer solved my problem.But When I write print into the task, gives me same error.
Full Code:

Code: Select all

mbedtls_aes_context aes;

size_t _length = 16;
unsigned char iv[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
unsigned char key[16] = {0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};

unsigned char input[16] = "HelloEsp32AES**"; // 15 Chars + 1 (NULL)
unsigned char encrypt_output[48];
unsigned char decrypt_output[48];
TaskHandle_t xHandle;
void task(void* p)
{
    memset(encrypt_output, 0, 48);
    memset(decrypt_output, 0, 48);
   size_t iv_offset = 0;
   mbedtls_aes_init(&aes);

   mbedtls_aes_setkey_enc(&aes, key, 128);
   mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_ENCRYPT, 128, &iv_offset, iv, input, encrypt_output);
   mbedtls_aes_free(&aes);
   for(int i = 0; i < 48; i++) // 
    {
      printf(":%d", (int)encrypt_output[i]); <= Print statement causing the fault.
    }
   printf("\n");
   while(1){
   vTaskDelay(100);
   }
}

void app_main()
{
	int temp = nvs_flash_init();
    if (temp == ESP_ERR_NVS_NO_FREE_PAGES) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        temp = nvs_flash_init();
    }
    xTaskCreate(task, "task", 1024*10, NULL, 5, &xHandle);
}
Error:

Code: Select all

Guru Meditation Error: Core  1 panic'ed (StoreProhibited)
. Exception was unhandled.
Core 1 register dump:
PC      : 0x40085031  PS      : 0x00060b33  A0      : 0x800868e9  A1      : 0x3ffc8eb0  
A2      : 0xfcbece63  A3      : 0x00000000  A4      : 0x00060023  A5      : 0x3ffc7948  
A6      : 0x00000001  A7      : 0x00000001  A8      : 0x0000abab  A9      : 0x0000abab  
A10     : 0xb33fffff  A11     : 0x0000cdcd  A12     : 0x00060020  A13     : 0x00000001  
A14     : 0x00060b23  A15     : 0x00000000  SAR     : 0x00000008  EXCCAUSE: 0x0000001d  
EXCVADDR: 0xfcbece63  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  

Backtrace: 0x40085031:0x3ffc8eb0 0x400868e6:0x3ffc8ed0 0x40082680:0x3ffc8ef0 0x400826b1:0x3ffc8f10 0x40082b51:0x3ffc8f30 0x4000beaf:0x3ffc8f50 0x400840f2:0x3ffc8f70 0x40084324:0x3ffc8f90 0x40082bf6:0x3ffc8fb0 0x40082c1c:0x3ffc8fd0 0x40082d61:0x3ffc9000 0x400d89d6:0x3ffc9020 0x400d26e5:0x3ffc9330 0x400d2171:0x3ffc9380

Rebooting...
ets Jun  8 2016 00:22:57

Who is online

Users browsing this forum: Gaston1980 and 83 guests