espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.46k stars 7.25k forks source link

Discussion: Is there a way to access the MPI functions in the ROM? (IDFGH-13645) #14527

Closed ricmoo closed 2 weeks ago

ricmoo commented 4 weeks ago

Answers checklist.

General issue report

I found the following file while porting the hardware MPI support to the Stub and was wondering if there is a way to access these functions directly from the ROM (including the hardware accelerated mbedtls_mpi_exp_mod).

Thanks.

nopnop2002 commented 2 weeks ago

I may not understand your request accurately, but we can build this.

#include <stdio.h>

#include "mbedtls/bignum.h"

// python: hex(random.randrange(2 ** 1024))
const char *A = "49dd4a46f532d1e3c395875d0f43747367e1fe078ac571a541e17c1e1a505cbb9df94e8fbe4b6acee05134a3ccb8780cbf19abded6dee43af23c547046ab309090eccfd8d837ce33b7489421c9a93201b7598f32bf4d97de5302604d581e31e1d1f786f826d00678c62222d0ed97910a786a9ca2a24cd768ee4241e761d71494";
const char *E = "8e51ee5226332eea3e694e62919fe8a3e2c6be653cd63d5c87027848636293dfe42025ab64df0f75cacab381400e4b734b08b4cbb1c4a28f699ae81dab87838f501f090e72e5d546f8de82f273416c204fa66176c0b48aea5df5cb081b80b56e63e3bfa545157b259baeebcdbf50b7299953440a681f414ea4cfca9d8f2c0b91";
const char *N = "ff3f5df10b8db6aab53eb55c3c979ce9250287fb48ac031b650ecbe35a42b2f0d2edfdb2252023e0b5769574ba0d6e7a5dec6989b75c82bc0364a0f24c7e3acdd12720573b8bdbd879d65cf3d8fb7ae9324774bb910aac64dc9f233ff58472809ec260089ef66b304368e86b1d159ce330867c3c49758757305f3de52bf958a7";

void app_main()
{
    mbedtls_mpi a, b, m, r;
    int ret;

    mbedtls_mpi_init(&a);
    mbedtls_mpi_init(&b);
    mbedtls_mpi_init(&m);
    mbedtls_mpi_init(&r);

    mbedtls_mpi_read_string(&a, 16, "2");
    mbedtls_mpi_read_string(&b, 16, "2");
    mbedtls_mpi_read_string(&m, 16, "10");

    ret = mbedtls_mpi_exp_mod(&r, &a, &b, &m, 0);
    printf("mbedtls_mpi_exp_mod ret=%d\n", ret);

    mbedtls_mpi_read_string(&a, 16, A);
    mbedtls_mpi_read_string(&b, 16, E);
    mbedtls_mpi_read_string(&m, 16, N);

    ret = mbedtls_mpi_exp_mod(&r, &a, &b, &m, 0);
    printf("mbedtls_mpi_exp_mod ret=%d\n", ret);
}
AdityaHPatwardhan commented 2 weeks ago

Hi @ricmoo,

At the moment only the esp32c2 SoC contains some functions from mbedTLS in its ROM. These functions are the pure software implementation from the mbedTLS 2.16.12 version. However, They shall not provide the hardware acceleration. For hardware acceleration you need to involve the port layer from mbedtls.

The list of mbedtls functions that are present in the ROM of esp32c2 SoC can be found out at https://github.com/espressif/esp-idf/blob/59e18382702b6986be3d3f55e9ac7763c1397cf7/components/esp_rom/esp32c2/ld/esp32c2.rom.mbedtls.ld

ricmoo commented 2 weeks ago

I see. I was hoping to remove some functions from my code (like add, mul, etc.) in favour of using the ROM versions, but there isn’t really any function on the list I use for prime number generation. :)

Thanks! :)