Skip to main content

psa_random_reseed

Function psa_random_reseed 

Source
pub unsafe extern "C" fn psa_random_reseed(
    perso: *const u8,
    perso_size: usize,
) -> psa_status_t
Expand description

Force an immediate reseed of the PSA random generator.

The entropy source(s) are the ones configured at compile time.

The random generator is always seeded automatically before use, and it is reseeded as needed based on the configured policy, so most applications do not need to call this function.

The main reason to call this function is in scenarios where the process state is cloned (i.e. duplicated) while the random generator is active. In such scenarios, you must call this function in every clone of the original process before performing any cryptographic operation that uses randomness. (Note that any operation that uses a private or secret key may use randomness internally even if the result is not randomized, but hashing and signature verification are ok.) For example:

  • If the process is part of a live virtual machine that is cloned, call this function after cloning so that the new instance has a distinct random generator state.
  • If the process is part of a hibernated image that may be resumed multiple times, call this function after resuming so that each resumed instance has a distinct random generator state.
  • If the process is cloned through the fork() system call, the child process should call this function before using the random generator.

An additional consideration applies in configurations where there is no actual entropy source, only a nonvolatile seed (i.e. #MBEDTLS_ENTROPY_NV_SEED is enabled, #MBEDTLS_NO_PLATFORM_ENTROPY is enabled and #MBEDTLS_ENTROPY_HARDWARE_ALT is disabled). In such configurations, simply calling psa_random_reseed() in multiple cloned processes would result in the same random generator state in all the clones. To avoid this, in such configurations, you must pass a unique \p perso string in every clone.

\note This function has no effect when the compilation option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled.

\note In client-server builds, this function may not be available from clients, since the decision to reseed is generally based on the server state.

\note If the entropy source fails, the random generator remains usable: subsequent calls to generate random data will succeed until the random generator itself decides to reseed. If you want to force a reseed, either treat the failure as a fatal error, or call psa_random_deplete() instead of this function (or in addition).

\param[in] perso A personalization string, i.e. a byte string to inject into the random generator state in addition to entropy obtained from the normal source(s). In most cases, it is fine for \c perso to be empty. The main use case for a personalization string is when the random generator state is cloned, as described above, and there is no actual entropy source. \param perso_size Length of \c perso in bytes.

\retval #PSA_SUCCESS The reseed succeeded. \retval #PSA_ERROR_BAD_STATE The PSA random generator is not active. \retval #PSA_ERROR_NOT_SUPPORTED PSA uses an external random generator because the compilation option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled. This configuration does not support explicit reseeding. \retval #PSA_ERROR_INSUFFICIENT_ENTROPY The entropy source failed.