This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Cannot run MBEDTLS Example

I am trying to run the example of generating a random string from MBEDTLS website , below is my code , before running this code I have done the following changes.

Set MBEDTLS_CONFIG_FILE to nrf_mbedtls_config_file.h , included the necessary library path i.e /external/mbedtls/include to the pre processor in SES Build Config

I got the error from mbedtls/config.h saying MBEDTLS_TIMING_C requires unix or windows , so I naively commented the MBEDTLS_CONFIG_C , and sunk the entropy collector , now my

mbedtls_entropy_init() returns MBEDTLS_ERR_ENTROPY_SOURCE_FAILED.

See my code below for generating random code : this one works just fine on my ESP8266 no problems with that I dont want to write large parts of my C code by appending specific nrf* apis.

mbedtls_entropy_context entropy;
    mbedtls_entropy_init(&entropy); //Code is probably blown here
    mbedtls_ctr_drbg_context ctr_drbg;
    unsigned char *personalization = (unsigned char *)"bhavar kumavat";
    mbedtls_ctr_drbg_init(&ctr_drbg);
    const int size_of_buffer = 17;
    unsigned char random_bucket[size_of_buffer];
    unsigned char random_return_bucket[size_of_buffer];

    memset(random_bucket, '\0', sizeof(unsigned char)*size_of_buffer);
    memset(random_return_bucket, '\0', sizeof(unsigned char)*size_of_buffer);

    int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *)personalization, strlen((char *)personalization));
    printf("Return Code %d\n",ret); // I get the notification here
        if (ret == 0)
    {
        mbedtls_ctr_drbg_random(&ctr_drbg, random_bucket, sizeof(random_bucket));
        for (register int counter = 0; counter < size_of_buffer; counter++)
        {
            random_return_bucket[counter] = RANGE_FUNCTION(random_bucket[counter]);
        }
        random_return_bucket[size_of_buffer] = '\0';
                printf("generate_random_string : %s\n",random_return_bucket);
    }
        else{
        printf( " failed\n ! mbedtls_ctr_drbg_init returned -0x%04x\n", -ret );
        }

Let me know if someone can help me , thank you.

Let me also know if I cant use mbedtls the way I am using it in ESP8266 / ESP32 here in NRF52

I am new to Embedded Development but can compensate it with excitement to learn :) ,

OS : Ubuntu 18.04

IDE : SES

Project Template : Blinky Project

CMSIS Config : I did'nt touch it.

  • Hi,

    Could you upload the project ?

    What SDK version are you using?

    PS: Note that we have AES crypto examples using mbedtls backend here. Also see this and this page.

  • Hey, There Thank you for responding

    I am using the latest and the greatest SDK 15.3.0. all my previous code uses lmbedcrypto lib , so all my functions are like mbedtls_***() I wanna keep them that way Slight smile , However I could not understand my aes example from mbedtls website runs just fine on my NRF52 DK board. Just the entropy generator fails.

    https://tls.mbed.org/kb/how-to/encrypt-with-aes-cbc this code runs fine as is no changes required , to generate IV(I am using static array right now as IV) I wanna use the mbedtls entropy generator (some thing from here tls.mbed.org/.../add-a-random-generator) , This is where I am stuck. The ECB mode would run ok. no problem in that , entropy is stuck.

  • so all my functions are like mbedtls_***() I wanna keep them that way

    I understand, but it might be useful to see how nrf_crypto uses mbedtls_* functions. E.g. we are seeding and adding the rng backend like this:

    // Callback function used by mbed TLS to seed and reseed.
    static int entropy_callback(void * p_entropy, unsigned char * p_buffer, size_t size)
    {
        UNUSED_PARAMETER(p_entropy);
    
        nrf_drv_rng_block_rand(p_buffer, size);
    
        return 0;
    }
    
    
    ret_code_t nrf_crypto_rng_backend_init(void * const p_context, void * const p_temp_buffer)
    {
        ret_code_t                  ret_val;
        int                         mbedtls_ret_val;
        mbedtls_ctr_drbg_context  * p_mbedtls_context =
            &((nrf_crypto_backend_rng_context_t *)p_context)->mbedtls_context;
    
        UNUSED_PARAMETER(p_temp_buffer);
    
        ret_val = nrf_drv_rng_init(NULL);
    
        if (ret_val != NRF_SUCCESS)
        {
            return ret_val;
        }
    
        mbedtls_ctr_drbg_init(p_mbedtls_context);
    
        // Initial seeding. The nrf_crypto_rng API does not support additional entropy in the initial
        // seeding. Additional entropy can be provided using nrf_crypto_rng_backend_reseed(),
        // which calls mbedtls_ctr_drbg_reseed().
        mbedtls_ret_val = mbedtls_ctr_drbg_seed(p_mbedtls_context,
                                                entropy_callback,
                                                NULL,
                                                NULL,
                                                0);
    
        ret_val = result_get(mbedtls_ret_val);
    
        return ret_val;
    }

    Anyways, if you upload the project, I could take a closer look at the issue. I could convert this to a private case, if you don't want to share the project publicly.

  • Hello, has this thread been solved i m getting the same error, it tells me:

    #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"

    However, i am using Windows.

Related