![]() |
TrustCore SDK NanoCrypto API reference
version 7.0
|
Random Number Generator (RNG) API header. More...
Go to the source code of this file.
Typedefs | |
typedef sbyte4(* | RNGFun) (void *rngFunArg, ubyte4 length, ubyte *buffer) |
Function pointer type for a method that produces (pseudo) random bytes. More... | |
Enumerations | |
enum | randomContextType { NIST_FIPS186 = 0, NIST_CTR_DRBG = 2, MOC_RAND = 3 } |
Functions | |
MOC_EXTERN ubyte * | GetNullPersonalizationString (ubyte4 *pLen) |
Returns a NULL string. More... | |
MOC_EXTERN MSTATUS | RANDOM_acquireContext (randomContext **pp_randomContext) |
Create an RNG (random number generator) context data structure. More... | |
MOC_EXTERN MSTATUS | RANDOM_acquireContextEx (randomContext **pp_randomContext, ubyte algoId) |
MOC_EXTERN MSTATUS | RANDOM_addEntropyBit (randomContext *pRandomContext, ubyte entropyBit) |
Add entropy to the RNG (random number generator) module. More... | |
MOC_EXTERN MSTATUS | RANDOM_addEntropyBitEx (randomContext *pRandomContext, ubyte entropyBit) |
MOC_EXTERN MSTATUS | RANDOM_deleteFIPS186Context (randomContext **ppRandomContext) |
Deletes a FIPS186 RNG context. More... | |
MOC_EXTERN MSTATUS | RANDOM_generateASCIIString (randomContext *pRandomContext, ubyte *pBuffer, ubyte4 bufferLen) |
Function to return a random ASCII character string. More... | |
MOC_EXTERN MSTATUS | RANDOM_getAutoSeedBytes (ubyte *pSeedBytes, ubyte4 numBytes) |
Collect entropy bytes for seed material. More... | |
MOC_EXTERN ubyte | RANDOM_getEntropySource (void) |
Gets the global entropy source flag. More... | |
MOC_EXTERN MSTATUS | RANDOM_isMocSymContext (randomContext **ppRandomContext, intBoolean *pIsMocSym) |
Determine if a randomContext refers to a MocSym random implementation. More... | |
MOC_EXTERN MSTATUS | RANDOM_KSrcGenerator (randomContext *pRandomContext, ubyte buffer[40]) |
MOC_EXTERN MSTATUS | RANDOM_launchAutoSeed (randomContext *pCtx) |
Seed a random context using the built-in entropy collection. More... | |
MOC_EXTERN MSTATUS | RANDOM_newFIPS186Context (randomContext **ppRandomContext, ubyte b, const ubyte pXKey[], sbyte4 seedLen, const ubyte pXSeed[]) |
Generate FIPS-specific RNG context data structure using provided seed. More... | |
MOC_EXTERN MSTATUS | RANDOM_numberGenerator (randomContext *pRandomContext, ubyte *pBuffer, sbyte4 bufSize) |
Generate the specified number of random bits. More... | |
MOC_EXTERN MSTATUS | RANDOM_numberGeneratorAdd (randomContext *pRandomContext, ubyte *pRetRandomBytes, ubyte4 numRandomBytes, ubyte *pAdditionalData, ubyte4 additionalDataLen) |
Generate random bytes with optional additional input. More... | |
MOC_EXTERN MSTATUS | RANDOM_numberGeneratorFIPS186 (randomContext *pRandomContext, ubyte *pRetRandomBytes, sbyte4 numRandomBytes) |
Generate the specified number of random bits via FIPS186. More... | |
MOC_EXTERN MSTATUS | RANDOM_releaseContext (randomContext **pp_randomContext) |
Delete RNG (random number generator) context data structure. More... | |
MOC_EXTERN MSTATUS | RANDOM_releaseContextEx (randomContext **pp_randomContext) |
MOC_EXTERN MSTATUS | RANDOM_reseedContext (randomContext *pCtx, ubyte *pEntropy, ubyte4 entropyLen, ubyte *pAdditionalData, ubyte4 additionalDataLen) |
Reseed a random context. More... | |
MOC_EXTERN sbyte4 | RANDOM_rngFun (void *rngFunArg, ubyte4 length, ubyte *buffer) |
Generate a random number. More... | |
MOC_EXTERN MSTATUS | RANDOM_seedFIPS186Context (randomContext *pRandomCtx, ubyte *seed, ubyte4 seedLen) |
Seed a previously allocated FIPS186 Random Context. More... | |
MOC_EXTERN MSTATUS | RANDOM_seedFromDevURandom (randomContext *pCtx, ubyte4 numBytes) |
Seed a random context with bytes from /dev/urandom. More... | |
MOC_EXTERN MSTATUS | RANDOM_seedOldRandom (randomContext *pCtx, ubyte *pSeedBytes, ubyte4 seedLen) |
Seeds randomContexts that are not MocSym Operators. More... | |
MOC_EXTERN MSTATUS | RANDOM_setEntropySource (ubyte EntropySrc) |
Sets the global entropy source flag. More... | |
MOC_EXTERN void | resetRNGFail (void) |
Resets the global RNG fail flag back to FALSE (0). | |
MOC_EXTERN void | triggerRNGFail (void) |
Sets the global RNG fail flag to TRUE (1). | |
Variables | |
MOC_EXTERN_RANDOM_H randomContext * | g_pRandomContext |
This file documents the definitions, enumerations, structures, and function of the Digicert Security of Things (SoT) Platform Random Number Generator.
Whether the following flag is defined determines which functions are declared:
__DISABLE_MOCANA_ADD_ENTROPY__
typedef sbyte4(* RNGFun) (void *rngFunArg, ubyte4 length, ubyte *buffer) |
Function pointer type for a method that produces (pseudo) random bytes.
rngFunArg | Optional argument that may be needed by your implementation. Often this will be a random context. |
length | The number of bytes requested. |
buffer | Buffer to hold the resuling output bytes. |
OK
(0) if successful and non-zero if unsuccessful.MOC_EXTERN ubyte* GetNullPersonalizationString | ( | ubyte4 * | pLen | ) |
pLen | Contents will be set to the length of a NULL string, ie 0. |
NULL
pointer. MOC_EXTERN MSTATUS RANDOM_acquireContext | ( | randomContext ** | pp_randomContext | ) |
This function creates a new RNG (random number generator) context data structure.
There are no flag dependencies to enable this function.
pp_randomContext | On return, pointer to new RNG context. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro.MOC_EXTERN MSTATUS RANDOM_addEntropyBit | ( | randomContext * | pRandomContext, |
ubyte | entropyBit | ||
) |
This function adds entropy to the RNG (random number generator) module, thereby increasing randomness.
To enable this function, the following flag must not be defined:
__DISABLE_MOCANA_ADD_ENTROPY__
pRandomContext | Pointer to RNG context. |
entropyBit | Entropy to add. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro.MOC_EXTERN MSTATUS RANDOM_deleteFIPS186Context | ( | randomContext ** | ppRandomContext | ) |
Deletes a FIPS186 RNG context.
ppRandomContext | Pointer to the location of the context to be deleted. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro.MOC_EXTERN MSTATUS RANDOM_generateASCIIString | ( | randomContext * | pRandomContext, |
ubyte * | pBuffer, | ||
ubyte4 | bufferLen | ||
) |
This function is the same as RANDOM_numberGenerator, except all the output will be ASCII characters.
Each byte of output will be a value in the following space.
numbers 0 - 9 0x30 - 0x39
upper-case A - Z 0x41 - 0x5a
lower-case a - z 0x61 - 0x7a
62 possible characters
The caller supplies a randomContext. If you called MOCANA_initMocana, you can pass in g_pRandomContext. If you did not call MOCANA_initMocana, you can call RANDOM_acquireContext to get one.
The caller provides the buffer into which the function will place the random characters. The caller also specifies how many random ASCII bytes to generate.
[in] | pRandomContext | The random object to use to generate. |
[in,out] | pBuffer | The buffer into which the function will deposit the random ASCII characters. |
[in] | bufferLen | The number of bytes to generate. |
MOC_EXTERN MSTATUS RANDOM_getAutoSeedBytes | ( | ubyte * | pSeedBytes, |
ubyte4 | numBytes | ||
) |
pSeedBytes | Pointer to caller allocated buffer that will recieve the computed entropy bytes. |
numBytes | Number of bytes to collect, must be a multiple of 8 between 8 and 64. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro. __DISABLE_MOCANA_RNG__
MOC_EXTERN ubyte RANDOM_getEntropySource | ( | void | ) |
MOC_EXTERN MSTATUS RANDOM_isMocSymContext | ( | randomContext ** | ppRandomContext, |
intBoolean * | pIsMocSym | ||
) |
This function takes a pointer to a randomContext pointer, determines if it refers to a MocSym random implementation, and stores the result in the boolean pointed to by the second parameter.
ppRandomContext | Pointer to a randomContext pointer. |
pIsMocSym | Pointer to a intBoolean that will recieve the result. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro.__ENABLE_MOCANA_SYM__
__DISABLE_MOCANA_RNG__
MOC_EXTERN MSTATUS RANDOM_launchAutoSeed | ( | randomContext * | pCtx | ) |
Launch the automatic entropy collection to gather seed material, then use that material to seed the random context.
pCtx | Pointer to an allocated random context. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro.__DISABLE_MOCANA_RNG__
MOC_EXTERN MSTATUS RANDOM_newFIPS186Context | ( | randomContext ** | ppRandomContext, |
ubyte | b, | ||
const ubyte | pXKey[], | ||
sbyte4 | seedLen, | ||
const ubyte | pXSeed[] | ||
) |
This function generates a FIPS-specific RNG context data structure, using the provided seed.
There are no flag dependencies to enable this function.
ppRandomContext | Pointer to address of RNG context. |
b | Number of bytes in the X key (pXKey ). |
pXKey | X key value. |
seedLen | Number of bytes in the X seed (pXSeed ). |
pXSeed | X seed value. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro.MOC_EXTERN MSTATUS RANDOM_numberGenerator | ( | randomContext * | pRandomContext, |
ubyte * | pBuffer, | ||
sbyte4 | bufSize | ||
) |
This function generates a specified number of random bits, which can be used as needed by your application code.
pBuffer
parameter is at least bufSize
bytes long.There are no flag dependencies to enable this function.
pRandomContext | Pointer to RNG context. |
pBuffer | Pointer to buffer at least bufSize bytes long in which to store the generated random bytes. |
bufSize | Number of random bytes to return. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro.MOC_EXTERN MSTATUS RANDOM_numberGeneratorAdd | ( | randomContext * | pRandomContext, |
ubyte * | pRetRandomBytes, | ||
ubyte4 | numRandomBytes, | ||
ubyte * | pAdditionalData, | ||
ubyte4 | additionalDataLen | ||
) |
pRandomContext | Random context to use for generation. |
pRetRandomBytes | Buffer to place the generated bytes. |
numRandomBytes | Number of random bytes to generate. |
pAdditionalData | Optional additional data. |
additionalDataLen | Length in bytes of the additional data. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro. MOC_EXTERN MSTATUS RANDOM_numberGeneratorFIPS186 | ( | randomContext * | pRandomContext, |
ubyte * | pRetRandomBytes, | ||
sbyte4 | numRandomBytes | ||
) |
This function generates a specified number of random bits via FIPS188, which can be used as needed by your application code.
pRetRandomBytes
parameter is at least numRandomBytes
bytes long.There are no flag dependencies to enable this function.
pRandomContext | Pointer to a FIPS186 RNG context. |
pRetRandomBytes | Pointer to buffer at least numRandomBytes bytes long in which to store the generated random bytes. |
numRandomBytes | Number of random bytes to return. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro.MOC_EXTERN MSTATUS RANDOM_releaseContext | ( | randomContext ** | pp_randomContext | ) |
This function deletes an RNG (random number generator) context and frees associated memory. To avoid memory leaks, your application must call this function.
There are no flag dependencies to enable this function.
pp_randomContext | Pointer to RNG context pointer to free and delete. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro.MOC_EXTERN MSTATUS RANDOM_reseedContext | ( | randomContext * | pCtx, |
ubyte * | pEntropy, | ||
ubyte4 | entropyLen, | ||
ubyte * | pAdditionalData, | ||
ubyte4 | additionalDataLen | ||
) |
This function will reseed the provided random context. If the underlying context does not accept direct entropy material for reseeding (ie, uses a previously established function pointer for entropy collection), then the entropy bytes will be unused.
pCtx | The random context to reseed. |
pEntropy | Entropy input to use for the reseeding. May be NULL if the underlying random object does not accept direct entropy injection. |
entropyLen | Length in bytes of the entropy material. |
pAdditionalData | Optional additional data. |
additionalDataLen | Length in bytes of the additional data. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro. MOC_EXTERN sbyte4 RANDOM_rngFun | ( | void * | rngFunArg, |
ubyte4 | length, | ||
ubyte * | buffer | ||
) |
This function generates a random number. (This function provides a common prototype, or wrapper, around the random number generator function — typically the NanoCrypto RANDOM_numberGenerator() function.)
There are no flag dependencies to enable this function.
rngFunArg | Pointer to RNG function argument. |
length | Number of bytes in the output buffer, buffer . |
buffer | Pointer to buffer in which to store the resultant random number. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro.MOC_EXTERN MSTATUS RANDOM_seedFIPS186Context | ( | randomContext * | pRandomCtx, |
ubyte * | seed, | ||
ubyte4 | seedLen | ||
) |
pRandomCtx | Pointer to a previously allocated random context. |
seed | Pointer to a buffer containing the new seed material. |
seedLen | Length in bytes of the new seed material, must be between 20 and 64 bytes. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro. MOC_EXTERN MSTATUS RANDOM_seedFromDevURandom | ( | randomContext * | pCtx, |
ubyte4 | numBytes | ||
) |
pCtx | Pointer to the random context being seeded. |
numBytes | Number of bytes of seed material to collect. Must be <= 64. |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro. MOC_EXTERN MSTATUS RANDOM_seedOldRandom | ( | randomContext * | pCtx, |
ubyte * | pSeedBytes, | ||
ubyte4 | seedLen | ||
) |
This function will seed the random context implementation pointed to by pWrap with the bytes provided in the pSeedBytes buffer. Currently there are only two random context implementations that are not MocSym Operators, the NIST CTR DRBG and the FIPS186 PRNG. If the random context type is one of those two this function will call the appropriate seed function, otherwise it will return an error.
pCtx | Pointer to a previously allocated randomContext. |
pSeedBytes | Pointer to buffer which contains new seed material. |
seedLen | Length in bytes of the seed material. |
MOC_EXTERN MSTATUS RANDOM_setEntropySource | ( | ubyte | EntropySrc | ) |
Sets the global entropy source flag.
There are no flag dependencies to enable this function but if + __DISABLE_MOCANA_RAND_ENTROPY_THREADS__
is defined then only the ENTROPY_SRC_EXTERNAL
flag is allowed.
EntropySrc | The input flag. This is either ENTROPY_SRC_EXTERNAL , or if internal entropy is not disabled, ENTROPY_SRC_INTERNAL . |
OK
(0) if successful; otherwise a negative number error code definition from merrors.h. To retrieve a string containing an English text error identifier corresponding to the function's returned error status, use the DISPLAY_ERROR
macro.