Making rand() work on NRF52 + NRF53 with NRF Connect SDK [continued]

I accidentally pressed "verified" on https://devzone.nordicsemi.com/f/nordic-q-a/91104/making-rand-work-on-nrf52-nrf53-with-nrf-connect-sdk when it wasn't and I don't seem to be able to un-press "verified", so here is a follow-up question:

We have an NRFConnect SDK/Zephyr project that must (a) compile both for NRF52840 and NRF5340, and, (b) needs a working rand(); nothing fancy, pseudo-random would be perfectly fine, preferable in fact since this is only used in test code and it would be good to have the sequences repeatable.  As instructed by the replies to the support ticket above, we have the following lines in KConfig:

CONFIG_ENTROPY_GENERATOR=y
CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR=y
CONFIG_ENTROPY_CC3XX=y

However, though we get no KConfig errors, both NRF52 and NRF53 crash inside rand() with this setting, see below.  What KConfig setting(s) are we missing? How do we end up with a single prj.conf file that will give us a plain-old pseudo-random rand() on both platforms?

NRF53 does this:

2022-08-20 12:31:32.242 U_GNSS_PRIVATE_TEST: test decoding NMEA message $GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76
2022-08-20 12:31:49.932 E: ***** MPU FAULT *****
2022-08-20 12:31:49.950 E: Data Access Violation
2022-08-20 12:31:49.966 E: MMFAR Address: 0xc
2022-08-20 12:31:49.981 E: r0/a1: 0x200057b0 r1/a2: 0x00000000 r2/a3: 0x200015e8
2022-08-20 12:31:49.998 E: r3/a4: 0x200015e8 r12/ip: 0x00000000 r14/lr: 0x000459ed
2022-08-20 12:31:50.013 E: xpsr: 0x29000000
2022-08-20 12:31:50.029 E: r4/v1: 0x00000000 r5/v2: 0xffffffff r6/v3: 0xffffffff
2022-08-20 12:31:50.044 E: r7/v4: 0x00000000 r8/v5: 0x00000000 r9/v6: 0x2002a6c1
2022-08-20 12:31:50.060 E: r10/v7: 0x200051ec r11/v8: 0xffffffff psp: 0x2002a580
2022-08-20 12:31:50.075 E: EXC_RETURN: 0xfffffffd
2022-08-20 12:31:50.090 E: Faulting instruction address (r15/pc): 0x000459ec
2022-08-20 12:31:50.107 E: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0
2022-08-20 12:31:50.121 E: Current thread: 0x200015e8 (main)
2022-08-20 12:31:50.137 ### Dumping current thread (main) ###
2022-08-20 12:31:50.153 0x000459ec: z_impl_k_mutex_lock /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/kernel/mutex.c:107
2022-08-20 12:31:50.169 0x000459e8: k_spin_lock /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/include/spinlock.h:141
2022-08-20 12:31:50.184 (inlined by) z_impl_k_mutex_lock /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/kernel/mutex.c:105
2022-08-20 12:31:50.200 0x00032586: k_mutex_lock /home/jenkinsslave-mlm/jenkins_workspace/workspace/iv_feature_gnss_transparent_rmea_4/_jenkins_work/17/build/zephyr/include/generated/syscalls/kernel.h:674
2022-08-20 12:31:50.215 (inlined by) mutex_lock_platform /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/nrfxlib/crypto/nrf_cc312_platform/src/nrf_cc3xx_platform_mutex_zephyr.c:291
2022-08-20 12:31:50.229 0x0004dae2: cc_mbedtls_ctr_drbg_random_with_add ??:?
2022-08-20 12:31:50.246 0x0004c4e6: nrf_cc3xx_platform_ctr_drbg_get ??:?
2022-08-20 12:31:50.262 0x0003e984: entropy_cc3xx_rng_get_entropy /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/nrf/drivers/entropy/entropy_cc310.c:77
2022-08-20 12:31:50.278 0x0003b8a8: z_impl_entropy_get_entropy /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/include/drivers/entropy.h:77
2022-08-20 12:31:50.294 (inlined by) entropy_get_entropy /home/jenkinsslave-mlm/jenkins_workspace/workspace/iv_feature_gnss_transparent_rmea_4/_jenkins_work/17/build/zephyr/include/generated/syscalls/entropy.h:33
2022-08-20 12:31:50.310 (inlined by) z_impl_sys_rand32_get /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/subsys/random/rand32_entropy_device.c:33
2022-08-20 12:31:50.326 0x00056c24: sys_rand32_get /home/jenkinsslave-mlm/jenkins_workspace/workspace/iv_feature_gnss_transparent_rmea_4/_jenkins_work/17/build/zephyr/include/generated/syscalls/rand32.h:33
2022-08-20 12:31:50.341 (inlined by) rand port/platform/zephyr/src/u_port_clib.c:81
...and NRF52 does this:

2022-08-20 12:35:39 u_monitor_15.1 - U_GNSS_PRIVATE_TEST: test decoding NMEA message $GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76
2022-08-20 12:35:57 u_monitor_15.1 - E: ***** MPU FAULT *****
2022-08-20 12:35:57 u_monitor_15.1 - E: Data Access Violation
2022-08-20 12:35:57 u_monitor_15.1 - E: MMFAR Address: 0xc
2022-08-20 12:35:57 u_monitor_15.1 - E: r0/a1: 0x20006814 r1/a2: 0x00000000 r2/a3: 0x20002490
2022-08-20 12:35:57 u_monitor_15.1 - E: r3/a4: 0x20002490 r12/ip: 0x00000000 r14/lr: 0x0005604d
2022-08-20 12:35:57 u_monitor_15.1 - E: xpsr: 0x21000000
2022-08-20 12:35:57 u_monitor_15.1 - E: r4/v1: 0x00000000 r5/v2: 0xffffffff r6/v3: 0xffffffff
2022-08-20 12:35:57 u_monitor_15.1 - E: r7/v4: 0x00000000 r8/v5: 0x00000000 r9/v6: 0x2002bf1c
2022-08-20 12:35:57 u_monitor_15.1 - E: r10/v7: 0x20005770 r11/v8: 0x00000000 psp: 0x2002bdf0
2022-08-20 12:35:57 u_monitor_15.1 - E: EXC_RETURN: 0xfffffffd
2022-08-20 12:35:57 u_monitor_15.1 - E: Faulting instruction address (r15/pc): 0x0005604c
2022-08-20 12:35:57 u_monitor_15.1 - E: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0
2022-08-20 12:35:57 u_monitor_15.1 - E: Current thread: 0x20002490 (main)
2022-08-20 12:35:57 u_monitor_15.1 - ### Dumping current thread (main) ###
2022-08-20 12:35:57 u_monitor_15.1 - 0x0005604c: z_impl_k_mutex_lock /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/kernel/mutex.c:107
2022-08-20 12:35:57 u_monitor_15.1 - 0x00056048: k_spin_lock /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/include/spinlock.h:141
2022-08-20 12:35:57 u_monitor_15.1 - (inlined by) z_impl_k_mutex_lock /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/kernel/mutex.c:105
2022-08-20 12:35:57 u_monitor_15.1 - 0x00046b34: k_mutex_lock /var/jenkins/workspace/iv_feature_gnss_transparent_rmea_2/_jenkins_work/15.1/build/zephyr/include/generated/syscalls/kernel.h:674
2022-08-20 12:35:57 u_monitor_15.1 - (inlined by) mutex_lock_platform /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/nrfxlib/crypto/nrf_cc310_platform/src/nrf_cc3xx_platform_mutex_zephyr.c:291
2022-08-20 12:35:57 u_monitor_15.1 - 0x0005e0be: cc_mbedtls_ctr_drbg_random_with_add ??:?
2022-08-20 12:35:57 u_monitor_15.1 - 0x0005ca8e: nrf_cc3xx_platform_ctr_drbg_get ??:?
2022-08-20 12:35:57 u_monitor_15.1 - 0x0004f800: entropy_cc3xx_rng_get_entropy /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/nrf/drivers/entropy/entropy_cc310.c:77
2022-08-20 12:35:57 u_monitor_15.1 - 0x0004c984: z_impl_entropy_get_entropy /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/include/drivers/entropy.h:77
2022-08-20 12:35:57 u_monitor_15.1 - (inlined by) entropy_get_entropy /var/jenkins/workspace/iv_feature_gnss_transparent_rmea_2/_jenkins_work/15.1/build/zephyr/include/generated/syscalls/entropy.h:33
2022-08-20 12:35:57 u_monitor_15.1 - (inlined by) z_impl_sys_rand32_get /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/subsys/random/rand32_entropy_device.c:33
2022-08-20 12:35:57 u_monitor_15.1 - 0x000668f2: sys_rand32_get /var/jenkins/workspace/iv_feature_gnss_transparent_rmea_2/_jenkins_work/15.1/build/zephyr/include/generated/syscalls/rand32.h:33
2022-08-20 12:35:57 u_monitor_15.1 - (inlined by) rand port/platform/zephyr/src/u_port_clib.c:81

  • Hi,

    I have tested the crypto RNG sample for nrf52840dk_nrf52840, nrf5340dk_nrf5340_cpuapp and nrf5340dk_nrf5340_cpuapp_ns, and in all cases the build was successful.

    Regarding number generators, Crypto RNG sample already includes the following options
    CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR=y
    CONFIG_CSPRING_ENABLED=y
    CONFIG_HARDWARE_DEVICE_CS_GENERATOR=y

    The same sample also includes 
    CONFIG_ENTROPY_GENERATOR=y
    CONFIG_ENTROPY_CC3XX=y

    All above-mentioned configuration options do not need to be separately included in prj.conf.

    It should be enough to have the same configuration options as already defined in prj.conf for the sample.
    CONFIG_NRF_SECURITY=y
    CONFIG_MBEDTLS_PSA_CRYPTO_C=y

    CONFIG_PSA_CRYPTO_DRIVER_OBERON=n
    CONFIG_PSA_CRYPTO_DRIVER_CC3XX=y

    Best regards,
    Dejan

  • I agree that compilation works, that's not the problem, it is when I run the code and it calls rand() that the problem occurs.

    I'm just taking the absolutely standard board definitions, nrf52840dk_nrf52840 and nrf5340dk_nrf5340_cpuapp from version 1.6.1 NCS Connect as defined by your good selves, and need to know the additions to place in my prj.conf over and above what you already define for the two platforms.

    If I have understood you correctly, I should be able to put the following in my prj.conf file with the otherwise standard board definitions on NCS Connect 1.6.1:

    CONFIG_NRF_SECURITY=y
    CONFIG_MBEDTLS_PSA_CRYPTO_C=y
    CONFIG_PSA_CRYPTO_DRIVER_OBERON=n
    CONFIG_PSA_CRYPTO_DRIVER_CC3XX=y

    ...to achieve a usable rand().  Is that correct?  I have tried this of course and I get the following KConfig error:

    warning: MBEDTLS_PSA_CRYPTO_C (defined at /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/nrfxlib/nrf_security/Kconfig:1689) was assigned the value 'y' but got the value 'n'. Check these unsatisfied dependencies: NRF_SECURITY_ANY_BACKEND (=n), NORDIC_SECURITY_BACKEND (=n)

    How do I resolve that KConfig error?  I could randomly start adding stuff to find out but that might take a little while...

  • Hi,

    Do you get the same warning with both board definitions which you mentioned?

    Were builds successful?

    For both boards, are there any other errors apart from this Kconfig warning? 

    Best regards,
    Dejan

  • Darn, accidentally pressed "verified" instead of "reply": please ignore that!

  • The warnings were the same in both cases.  We have our builds set up to not proceed if there is a KConfig warning: those things [potentially] mess you up.  Let me see if I can disable that...

Related