Skip to main content

Random number generation

In addition to providing a global RNG (i.e., a pointer g_pRandomContext that is initialized upon NanoCrypto library initialization), 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 Random seed for information on initial seeding):

randomContext *pRandCtx = NULL;
CRYPTO_INTERFACE_RANDOM_acquireContextEx(&pRandCtx, MODE_DRBG_CTR);

The buffer is filled with pseudo-random bytes via the API:

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):

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:

CRYPTO_INTERFACE_NIST_CTRDRBG_reseed(pRandCtx, pNewEntropy,
newEntropyLen, pAdditionalInput, additionalInputLen);

When finished with the random context, free the allocated memory by calling:

CRYPTO_INTERFACE_RANDOM_releaseContextEx(&pRandCtx);

A complete example may be found at:

${MSS_SRC_PKG}/src/crypto_interface/example/crypto_interface_random_example.c