APIs for Accessing Random Number Generator

Symbian platform provides different APIs by means of which the Cryptographically Secure Random Number Generator in Kernel (CSPRNG) implemented in the kernel can be accessed.

The following table lists the various APIs with their descriptions:

Random Number Generator APIs Description
Kernel-side access: Base porters can use the Kern::Random and Kern::SecureRandom() functions to obtain random data from the kernel's CSPRNG. For information the use of these functions, see CSPRNG Implementation in Kernel.
Kern::Random() Returns 32 bits of random data from the CSPRNG.

Use this function to get high-quality random number which may not be cryptographically secure.

Kern::SecureRandom() Returns random data of length specified in the descriptor passed as parameter to this function.

This function is used to get cryptographically secure random numbers. If the returned data is not guaranteed to be cryptographically secure, the function returns a KErrNotReady argument along with random data that can be used for non-cryptographic purposes.

User-side access to the Kernel APIs: Developers can use Math::Random() and Math::RandomL() that call the Kernel's Random APIs to get random data. For information on how these functions are used, see CSPRNG Implementation in Kernel.
Math::Random() Internally calls Kern::Random() and returns random data of length specified in the descriptor passed as parameter to this function (Math::Random()). The random data returned may not be cryptographically secure.
Math::RandomL() Internally calls Kern::SecureRandom() and returns random data of length specified in the descriptor passed as parameter to this function (Math::RandomL()).
Access from the OS Security Package (TRandom):
The TRandom functions provide means for the legacy crypto components ( cryptography.dll, hash.dll and random.dll) to access the CSPRNG through CryptoSPI and generate random numbers. For information on the legacy crypto components, see CryptoSPI Overview (weak build). For information on TRandom, see Random Number Generation Overview.
Note: If you do not wish to use TRandom classes for random number generation, you can directly use the CRandom class for accessing the CSPRNG.
TRandom::RandomL() Internally calls Kern::SecureRandom() (by means of CryptoSPI) for accessing the CSPRNG and generating random numbers. It does not return any argument indicating whether the data returned is cryptographically secure.
TRandom::SecureRandomL() Internally calls Kern::SecureRandom() (by means of CryptoSPI) for accessing the CSPRNG and generating random numbers.

This function returns cryptographically secure random data. If the returned data is not guaranteed to be cryptographically secure, the function returns a KErrNotSecure argument along with random data that can be used for non-cryptographic purposes.

Access from the OS Security Package (CRandom):

CRandom is a CryptoSPI class that provides functions to access CSPRNG and generate random data. For information on generating random data using CRandom, see Generating Random Bytes.

CRandom::GenerateRandomBytesL() Internally calls Kern::SecureRandom() for accessing the CSPRNG and generating cryptographically secure random numbers. f the returned data is not guaranteed to be cryptographically secure, the function returns a KErrNotSecure argument along with random data that can be used for non-cryptographic purposes.