ADDING TRNG MODULE IN DA14531

I am trying to add the True random number generator module in my project to give the seed to my rand() function to create a byte array of random number with size=16. But I am not able to understand how the TRNG works and how I should include it in my project.

Please help me get better clarity on the same.

Top Replies

Parents
  • Hi There,

    Thank you for posting your question online.
    You should add the trng.c file in the sdk_driver folder and compile your project. 
    Keep in mind that you should define CFG_TRNG in the da1458x_config_advanced.h file as well:

    /****************************************************************************************************************/
    /* Enables True Random Number Generator. A true random number, generated at system initialization, is used to   */
    /* seed any random number generator (C standard library, ChaCha20, etc.). The following supported options       */
    /* provide a trade-off between code size and start-up latency.                                                  */
    /* - undefined (or 0): TRNG is disabled.                                                                        */
    /* -   32:  Enables TRNG with   32 Bytes Buffer.                                                                */
    /* -   64:  Enables TRNG with   64 Bytes Buffer.                                                                */
    /* -  128:  Enables TRNG with  128 Bytes Buffer.                                                                */
    /* -  256:  Enables TRNG with  256 Bytes Buffer.                                                                */
    /* -  512:  Enables TRNG with  512 Bytes Buffer.                                                                */
    /* - 1024:  Enables TRNG with 1024 Bytes Buffer.                                                                */
    /****************************************************************************************************************/
    #define CFG_TRNG (1024)

    By defining the CFG_TRNG the init_rand_seed_from_trng is being executed in the arch_system.h file:
        // Initialize random number generator seed using random bits acquired from TRNG
    #if (USE_TRNG)
        init_rand_seed_from_trng();
    #endif

     This means that the SDK will initialize random number generator seed automatically.  In case you want to generate random numbers, you should just use the rand() function.
    In case you do not define the CFG_TRNG you should call the init_rand_seed_from_trng on your own.

    Kind Regards,
    OV_Renesas



  • Hello sir,

    I have defined all the macros as you mentioned and wrote this function in my main project

    void get_random_seed(void){
    init_rand_seed_from_trng();
    for(uint8_t i=0; i<ENC_DATA_LEN; i++){
    uint8_t result = rand()%0xFF;
    if(result<=0x0F)
    arch_printf("0%x",result);
    else
    arch_printf("%x",result);
    }
    }

    It does give me a random challenge of 16 bytes but upon rebooting the firmware, I get the same set of random values which is wrong. It should generate different random values everytime I reboot the firmware

    The highlighted values after user app init is the output of the rand() function.

    Please help me sort this issue out

  • Hi There,

    Thank you for the reply.
    If you have defined the CFG_TRNG you will not have to call the init_rand_seed_from_trng function.
    Let me also try to recreate this from my side and I will share any results.

    Kind Regards,
    OV_Renesas

Reply Children
  • Hello sir,

    I have tried both ways- by adding the init_rand_seed_from_trng and by removing it. It still shows the same. Please help me solve this issue.

  • Hi There,

    Apologies for the delay.
    Please refer on the DA14531 SW Platform Reference, on chapter 4.2.4. True Random Number Generation (TRNG).
    You can get a better understanding on how TRNG works from there.
    If you go on the trng.c file you can see the implementation of the TRNG feature. As you can see the TRNG uses the values of some RF registers to calculate a true random number:

        // Implement Save-Modify-Restore for the prefered settings that will be changed for the TRNG mode
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////
        save_RF_ENABLE_CONFIG1_REG  = GetWord16(RF_ENABLE_CONFIG1_REG);  // LNA off
        save_RF_ENABLE_CONFIG2_REG  = GetWord16(RF_ENABLE_CONFIG2_REG);  // Mixer off
        save_RF_DC_OFFSET_CTRL1_REG = GetWord16(RF_DC_OFFSET_CTRL1_REG); // Fixed DC offset compensation values for I and Q
        save_RF_DC_OFFSET_CTRL2_REG = GetWord16(RF_DC_OFFSET_CTRL2_REG); // Use the manual DC offset compensation values
        save_RF_ENABLE_CONFIG4_REG  = GetWord16(RF_ENABLE_CONFIG4_REG);  // VCO_LDO_EN=0, MD_LDO_EN=0. You need this for more isolation from the RF input
        save_RF_ENABLE_CONFIG14_REG = GetWord16(RF_ENABLE_CONFIG14_REG); // LOBUF_RXIQ_EN=0, DIV2_EN=0. This increases the noise floor for some reason. So you get more entropy. Need to understand it and then decide...
        save_RF_SPARE1_REG          = GetWord16(RF_SPARE1_REG);          // Set the IFF in REAL transfer function, to remove I-Q correlation. But it affects the DC offsets!
        save_RF_AGC_CTRL2_REG       = GetWord16(RF_AGC_CTRL2_REG);       // AGC=0 i.e. max RX gain

    So the value of these registers should be changed before generating another random number.
    I create a small project based on the ble_app_peripheral example.
    I used your get_random_seed function and I placed it inside the user_periph_setup.c file.
    First I call the get_random_seed function at the end of the periph_init function inside the user_periph_setup.c file and then I call the get_random_seed on user_app_init, on user_app_connection and on user_app_disconnection. 
    As you can see from the Tera Term logs, since each time the RF registers have been modified either during initialization, connection or disconnection I am getting always a different value.
    My get_random_seed function:
    void get_random_seed(void){
    	
    		for(uint8_t i=0; i<ENC_DATA_LEN; i++){
    			uint8_t result = rand()%0xFF;
    			
    			if(result<=0x0F)
    			{
    					arch_printf("0%x",result);
    			
    			}
    			else
    			{
    					arch_printf("%x",result);
    				
    			}
      	}
    		arch_printf("\r\n");
    			
    }

    Inside the periph_init function:
        // Set pad functionality
        set_pad_functions();
    		arch_printf("periph_init \r\n");
      	get_random_seed();
        // Enable the pads
        GPIO_set_pad_latch_en(true);

    The same logic applies for user_app_init, user_app_connection and user_app_disconnect:


    Kind Regards,
    OV_Renesas


  • Thank you so much for the efforts sir. I will test and check. Just one question- even after rebooting the development board also you are getting different numbers right?