--- title: "Random Number Generation" source_url: https://dev.digicert.com/trustcore-sdk/crypto-interface/random-number-generation.html --- In addition to providing a global RNG (i.e., a [pointer g_pRandomContext that is initialized upon NanoCrypto library initialization](https://dev.digicert.com/md/trustcore-sdk/nanocrypto/initialization.md#initialize-random-number-generator)), Crypto Interface provides APIs to create a local RNG (a local `randomContext` object) that may also be used in any API needing it. The supported RNG algorithm through Crypto Interface is the NIST-CTR MODE DRBG using AES or TDES (NIST SP800-90A). The Crypto Interface does not support APIs for NIST_FIPS186, but that is supported by NanoCrypto directly. To create and initialize a local `randomContext,` make the following declaration and call to seed the local RNG using the method configured via the initialization call (refer to [Initialization](https://dev.digicert.com/md/trustcore-sdk/nanocrypto/initialization.md#initialize-random-seed) for information on initial seeding): ```c randomContext *pRandCtx = NULL; CRYPTO_INTERFACE_RANDOM_acquireContextEx(&pRandCtx, MODE_DRBG_CTR); ``` The buffer is filled with pseudo-random bytes via the API: ```c RANDOM_numberGenerator(pRandCtx, pBuffer, numBytesRequested); ``` For a cipher that requires a callback method for random entropy, the random context may be used with the shell callback `RANDOM_rngFun` (found in `${MSS_SRC_PKG}/src/common/random.h`): ```c CRYPTO_INTERFACE_ECDSA_signDigestAux(pPrivKey, RANDOM_rngFun, pRandCtx, ...); ``` If a random context needs to be reseeded, a negative error code is returned when reseeding is required: `ERR_NIST_RNG_DBRG_RESEED_NEEDED`. Reseeding may be performed only by first obtaining appropriate entropy (and additional data if appropriate), and then calling: ```c CRYPTO_INTERFACE_NIST_CTRDRBG_reseed(pRandCtx, pNewEntropy, newEntropyLen, pAdditionalInput, additionalInputLen); ``` When finished with the random context, free the allocated memory by calling: ```c CRYPTO_INTERFACE_RANDOM_releaseContextEx(&pRandCtx); ``` A complete example may be found at: ``` ${MSS_SRC_PKG}/src/crypto_interface/example/crypto_interface_random_example.c ```