This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2640R2F: TRNG build problems

Part Number: CC2640R2F

I am attempting to add TRNG support to my project to generate 128 bit random numbers. I found the a code example and modified it slightly to compile, however it doesn't link:

int myRandomTest()
{
  TRNG_Handle handle;
  int_fast16_t result;

  TRNG_init();
  handle = TRNG_open(0, NULL);

  if (!handle) {
    // Handle error
    return -1; // REVISIT
  }

  CryptoKeyPlaintext_initKey(&entropyKey, entropyBuffer, KEY_LENGTH_BYTES);

  result = TRNG_generateEntropy(handle, &entropyKey);

  if (result != TRNG_STATUS_SUCCESS) {
    // Handle error
    return -1; // REVISIT
  }

  TRNG_close(handle);

  return 0;
}

I had to find the source code for CryptoKeyPlaintext_initKey() and add to my file as apparently the crytpoutils source code didn't automatically build.

The link issue is related to TRNG_count and TRNG_config symbols are not available and the link fails. However, the board file CC2640R2_LAUNCHXL.c contains TRNGCC26XX_config and the enum value CC2640R2_LAUNCHXL_TRNGCOUNT. The TRNG_config and TRNGCC26XX_config are identical declarations, just different names.

I can create the TRNG_count variable and assign it the value of CC2640R2_LAUNCHXL_TRNGCOUNT, but when I rename TRNGCC26XX_config to TRNG_config, the link still fails as it is expecting TRNGCC26XX_config to be defined.

 

Any examples of source code that can be used to generate a 128 bit random number would be appreciated. 

  • If you want to generate a random number, look into this post:
    e2e.ti.com/.../497127

    You could also take a look at this one:
    e2e.ti.com/.../711386
  • Thanks for the links, they were helpful. Here's my final solution:

    union random_u {
        uint32_t longnum[4];
        uint8_t bytenum[16];
    } random;

    void secureGen128BitKey()
    {
        uint16_t hwiKey;
     
        Power_setDependency(PowerCC26XX_PERIPH_TRNG);
        hwiKey = (uint16_t) Hwi_disable(); // Disable hardware interrupts.
     
        TRNGConfigure(0x400,0x10000,0);
        TRNGIntClear(TRNG_NUMBER_READY);
        TRNGEnable();
        while(!(TRNGStatusGet() & TRNG_NUMBER_READY));
        TRNGDisable();

        random.longnum[0] = HWREG(TRNG_BASE + TRNG_O_OUT0);
        random.longnum[1] = HWREG(TRNG_BASE + TRNG_O_OUT1);
     
        TRNGIntClear(TRNG_NUMBER_READY);
        TRNGEnable();
        while(!(TRNGStatusGet() & TRNG_NUMBER_READY));
        random.longnum[2] = HWREG(TRNG_BASE + TRNG_O_OUT0);
        random.longnum[3] = HWREG(TRNG_BASE + TRNG_O_OUT1);
        TRNGDisable();
     
        Hwi_restore(hwiKey); // Enable hardware interrupts.
        Power_releaseDependency(PowerCC26XX_PERIPH_TRNG);
    }