mbedTLS RSA encryption question

mpulis
Posts: 13
Joined: Mon Aug 07, 2017 7:53 am

mbedTLS RSA encryption question

Postby mpulis » Fri Nov 24, 2017 10:34 am

My aim is to load a 2048-bit RSA public key from a file which was generated using OpenSSL and then use this to encrypt a message. To do this I'm using mbedTLS functions, specifically as laid out in this tutorial: https://tls.mbed.org/kb/how-to/encrypt- ... t-with-rsa.

The key is being loaded into memory (or at least, the parsing function returns a success) but I'm getting the following error when calling the encrypt function:

Code: Select all

Guru Meditation Error of type InstrFetchProhibited occurred on core  1. Exception was unhandled.
Register dump:
PC      : 0x00000000  PS      : 0x00060030  A0      : 0x80101b10  A1      : 0x3ffd02a0
A2      : 0x3ffb64e8  A3      : 0x00000000  A4      : 0x00000000  A5      : 0x00000000
A6      : 0x00000180  A7      : 0x0000000a  A8      : 0x80101a12  A9      : 0x3ffd0290
A10     : 0x00000000  A11     : 0x3ffd02a0  A12     : 0x00000000  A13     : 0x3ffd0420
A14     : 0xacb68920  A15     : 0x00000018  SAR     : 0x00000004  EXCCAUSE: 0x00000014
EXCVADDR: 0x00000000  LBEG    : 0x4000c46c  LEND    : 0x4000c477  LCOUNT  : 0x00000000

Backtrace: 0x00000000:0x3ffd02a0 0x40101b10:0x3ffd0440 0x40101ba4:0x3ffd04a0 0x400fc985:0x3ffd04c0 0x400fca4c:0x3ffd04f0 0x40109f66:0x3ffd0520 0x40101c60:0x3ffd0550 0x400f6ae0:0x3ffd0580 0x400f6992:0x3ffd1180
0x40101b10: mbedtls_ctr_drbg_random_with_add at C:/msys32/home/esp-idf/components/mbedtls/library/ctr_drbg.c:355

0x40101ba4: mbedtls_ctr_drbg_random at C:/msys32/home/esp-idf/components/mbedtls/library/ctr_drbg.c:416

0x400fc985: mbedtls_rsa_rsaes_pkcs1_v15_encrypt at C:/msys32/home/esp-idf/components/mbedtls/library/rsa.c:1613

0x400fca4c: mbedtls_rsa_pkcs1_encrypt at C:/msys32/home/esp-idf/components/mbedtls/library/rsa.c:1613

0x40109f66: rsa_encrypt_wrap at C:/msys32/home/esp-idf/components/mbedtls/library/pk_wrap.c:56

0x40101c60: mbedtls_pk_encrypt at C:/msys32/home/esp-idf/components/mbedtls/library/pk.c:296

0x400f6ae0: rsaTest at C:/Users/aaquilina.ESDL/Desktop/Projects/ESP32/msys32/msys32/home/TG_MODEM2/main/tls_cipher.c:135

0x400f6992: sslTask at C:/Users/aaquilina.ESDL/Desktop/Projects/ESP32/msys32/msys32/home/TG_MODEM2/main/network.c:325 (discriminator 1)


The code I'm using is the following:

Code: Select all

mbedtls_pk_init( &pk );
   
   if((mbedtls_pk_parse_public_keyfile(&pk, "/spiffs/pubkey.pem")) != 0)
   {
      ESP_LOGE(TAG, "rsaInit: Public key could not be found!");
   }
   else
   {
      ESP_LOGI(TAG, "rsaInit: Public key loaded");
   }
   
   unsigned char plaintext[15] = "123456789abcde";
   unsigned char enctext[3000] = {0};
   size_t olen = 0;
   ret = 0;
   
   ret = mbedtls_pk_encrypt(&pk, plaintext, strlen("123456789abcde"),
                                    enctext, &olen, 3000,
                                    mbedtls_ctr_drbg_random, &ctr_drbg);
    if(ret != 0 )
    {
       ESP_LOGE(TAG, "Failure to encrypt message with error value%d", ret);
    }


Can anyone tell me why this is occuring?

User avatar
kolban
Posts: 1437
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: mbedTLS RSA encryption question

Postby kolban » Sun Mar 11, 2018 12:15 am

Looking at your trace, the failure appears to be occurring within the ctr_drbg module. In your snippet, you aren't showing the initialization of ctr_drbg. Are you doing that correctly?

When I get errors like this, I have found that spending time with JTAG debugging and stepping into the source to identify the actual source statement and contextual data has helped me well.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Who is online

Users browsing this forum: No registered users and 6 guests