Getting started with RNG and Crypto

We need to do some crypto.  The first task being to generate a random number with rng. I have been fighting through the sdk_config.h defines, and the list of modules that need to be added to our build for the nRF52840 and ended up with a module that gets to the cc310_backend_rng.c, calls CRYS_RndInit and simply reboots.

So I go to the example in nrf5_sdk\examples\crypto\nrf_crypto\rng.  That won't build on IAR, it ends up with a build error:

Error[Lt009]: Inconsistent wchar_t size 
            ssi_aes.c.obj(libnrf_cc310_0.9.13.a) and 64 other objects in libnrf_cc310_0.9.13.a have wchar_t size 16 bits 
            aes.o and 187 other objects, some of them in dl7M_tlf.a and 13 other libraries have wchar_t size 32 bits 

Is there a document somewhere that explains "getting started with RNG on the nRF82840"?  What RNG backend should I be using?  Which defines should I be setting?  What source is required? 

Were using IAR 8.50.9.33462, nRF5 Software Development Kit v17.1.0, and a custom board with a MAXQ1065. 

  • Code I am using to call the RNG is:

    static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
    {
        ret_code_t err_code = nrf_crypto_init();
        nrf_crypto_rng_init(NULL, NULL);
        memset(output, 0xaa, len);
        nrf_crypto_rng_vector_generate(output, len);
        return 0;
    }

  • When defining NRF_CRYPTO_BACKEND_CC310_RNG, I've run into the error:


    Warning[Li026]: the module "crys_rnd.c.obj(libnrf_cc310_0.9.13.a)" (and 14 other modules) do not contain information to support Virtual Function Elimination

    Does this mean I have used the wrong version of libnrf_cc310?  Which is the correct lib?

  • Hi 

    The SDK release notes have some information on this, including some workarounds:

    Libraries for IAR 8 require wchar_t to be of size 32 bits while IAR 7 requires 16 bits.
      
    To run a project using IAR 8, follow these instructions:
     1. Open the IAR project in IAR 8. The IAR workbench will automatically generate an IAR 8 compatible project file.
     2. If the project contains one of the precompiled libraries listed below, replace it
        with the IAR 8 compatible alternative (there are no projects targeting nRF51 in this SDK).
     3. Save the project.
     4. When building the project, you might get the warning: "The header file 'cmsis_iar.h' is obsolete and should not be used. [...]".
        - The problem is described in DevZone post: https://devzone.nordicsemi.com/f/nordic-q-a/31123/iar-ewarm-8-22-1-complains-about-cmsis_iar-h
          The solution is to remove all occurrences of #include <cmsis_iar.h>.
         
    The affected libraries are:
     - micro-ecc crypto:
        - IAR7: Includes library located in the folder named “…_iar\…”.
        - IAR8: Switch to using the library from the folder named “…_armgcc\…”.
     - nrf_cc310, nrf_cc310_bl, and nrf_oberon:
        - IAR7: Link to a library where “short_wchar” is part of the folder name.
        - IAR8: Link to a library without “short_wchar” in the folder name.
     - Gazell, NFC Tag, and 802.15.4:
        - IAR7: Includes the library where the file name ends with “_iar”.
        - IAR8: Switch to using the library with similar file name that ends with “_gcc”.
    *****

    Best regards
    Torbjørn

  • Thanks for the tip to use the non-short-wchar library from the SDK release notes.  This solved the issue with getting the rng example to build.  With the rng example, I found that I was missing a call to SaSi_LibInit() before calling nrf_crypto_init.  I then tripped over the issue in nrf_crypto_init ERROR Initialization or startup of RNG failed.  I'm now happily generating random numbers.

    Thanks again.

Related