Module imxrt_hal::trng

source ·
Expand description

True random number generator.

Provides basic support for the True Random Number Generator. The TRNG generates truly random data and is intended for use as a generator of entropy.

The TRNG is fairly slow - 15 minutes to an hour to generate 1 megabyte - so it probably should only be used to generate a relatively small amount of entropy for a cryptographic algorithm. Occasionally retrieving entropy from it won’t necessarily need to block, as this driver retrieves 512 bits at a time.

RngCore Support

When the crate feature rand_core is enabled, the TRNG can be wrapped in a struct that implements rand_core’s RngCore trait (via into_rng()). The rand crate’s Rng trait automatically implements high-level functions on top of RngCore.

Note that only the try_fill_bytes function of RngCore allows reporting an error. The others will panic if the TRNG reports an error. Errors appear to be extremely rare in the default configuration (none were seen over 3GB of data), but it’s possible they will be more common in certain situations, such as extreme temperatures or an inconsistent power supply. The non-public Security Reference Manual may have more information.

If you intend to use the RngCore wrapper, you should set a larger retry count. The default retry count should be sufficient.

Example

Enable the TRNG clock gate, wait to generate random data.

use imxrt_hal as hal;
use imxrt_ral as ral;

let mut ccm = unsafe { ral::ccm::CCM::instance() };
hal::ccm::clock_gate::trng().set(&mut ccm, hal::ccm::clock_gate::ON);

let mut trng = hal::trng::Trng::new(
    unsafe { ral::trng::TRNG::instance() },
    hal::trng::SampleMode::default(),
    hal::trng::RetryCount::default(),
);

let random_data = nb::block!(trng.next_u32()).ok()?;

Structs

  • A TRNG error occurred, such as a statistical test failing.
  • Specific errors that may occur during entropy generation
  • The number of retry attempts.
  • The true random number generator.

Enums