The API is defined in the header file sysroot-*-kos/include/kos/random/random_api.h
from the KasperskyOS SDK.
The API generates random numbers and includes functions that can be used to ensure high entropy (high level of unpredictability) of the seed value of the random number generator. The start number of the random number generator (seed) determines the sequence of the generated random numbers. In other words, if the same seed value is set, the generator creates identical sequences of random numbers. (The entropy of these numbers is fully determined by the entropy of the seed value, which means that these numbers are not entirely random, but pseudorandom.)
The random number generator of one process does not depend on the random number generators of other processes.
Information about API functions is provided in the table below.
Using the API
To generate a sequence of random byte values, call the KosRandomGenerate()
or KosRandomGenerateEx()
function.
Example use of the KosRandomGenerate()
function:
size_t random_number;
if (KosRandomGenerate(sizeof random_number, &random_number) == rcOk) {
...
}
The KosRandomGenerateEx()
function gets the quality level of the generated random values through the output parameter quality
. The quality level can be high or low. High-quality random values are random values that are generated while fulfilling all of the following conditions:
To register the entropy source, call the KosRandomRegisterSrc()
function. This function uses the callback
parameter to receive the pointer to a callback function of the following type:
typedef Retcode (*KosRandomSeedMethod)(void *context,
rtl_size_t size,
void *output);
Using the context
parameters, this callback function receives data with the specified size
of bytes from the entropy source and writes this data to the output
buffer. If the function returns rcOk
, it is assumed that data was successfully received from the entropy source. An entropy source can be a digitalized signal of a sensor or a hardware-based random number generator, for example.
To deregister an entropy source, call the KosRandomUnregisterSrc()
function.
If the quality level is low, it cannot be changed to high without initializing the random number generator.
To initialize a random number generator, call the KosRandomInitSeed()
function. The entropy of data passed through the seed
parameter must be guaranteed by the calling process.
If at least one of these conditions is not fulfilled, the generated random values are deemed low-quality values.
When a process is started, the seed value is automatically defined by the system. Then the seed value is modified when doing the following:
Each successful call of the KosRandomGenerateEx()
function with the size
parameter greater than zero and each successful call of the KosRandomGenerate()
function results in a change of the seed value of the random number generator, but not each seed change results in the receipt of data from registered entropy sources. The registered entropy sources are used only when condition 3 or 4 is not fulfilled. If at least one entropy source is registered, the time counter for the change in the seed value is reset. If data from at least one entropy source is successfully received, the counter of generated random byte values is also reset.
The quality level may change from high to low.
Each successful call of the KosRandomInitSeed()
function changes the seed value by using the data that is passed through the seed
parameter and received from registered entropy sources. If at least one registered entropy source is available, the counters for generated byte values and the time of a change in the seed value are reset. Otherwise, only the counter of generated random byte values is reset.
The quality level may change from high to low, and vice versa.
Each successful call of the KosRandomRegisterSrc()
function changes the seed value by using the data only from the registered entropy source. The counter of the generated random byte values is also reset.
The quality level may not change.
A possible scenario for generating high-quality random values includes the following steps:
KosRandomRegisterSrc()
function.KosRandomGenerateEx()
function.If the quality level is low, initialize the random number generator by calling the KosRandomInitSeed()
function, and proceed to step 2.
If the quality level is high, proceed to step 4.
To get the quality level without generating random values, call the KosRandomGenerateEx()
function with the values 0
and RTL_NULL
in the size
and output
parameters, respectively.
Information about API functions
random_api.h functions
Function |
Information about the function |
---|---|
|
Purpose Initializes the random number generator. Parameters
Returned values If successful, the function returns |
|
Purpose Generates a sequence of random byte values. Parameters
Returned values If successful, the function returns |
|
Purpose Generates a sequence of random byte values. Parameters
Returned values If successful, the function returns |
|
Purpose Registers an entropy source. Parameters
Returned values If successful, the function returns |
|
Purpose Deregisters the source of entropy. Parameters
Returned values If successful, the function returns |