Random Number Algorithm Definitions¶
- struct
rng_alg¶ random number generator definition
Definition
struct rng_alg { int (*generate)(struct crypto_rng *tfm,const u8 *src, unsigned int slen, u8 *dst, unsigned int dlen); int (*seed)(struct crypto_rng *tfm, const u8 *seed, unsigned int slen); void (*set_ent)(struct crypto_rng *tfm, const u8 *data, unsigned int len); unsigned int seedsize; struct crypto_alg base;};Members
generate- The function defined by this variable obtains arandom number. The random number generator transformmust generate the random number out of the contextprovided with this call, plus any additional dataif provided to the call.
seed- Seed or reseed the random number generator. With theinvocation of this function call, the random numbergenerator shall become ready for generation. If therandom number generator requires a seed for settingup a new state, the seed must be provided by theconsumer while invoking this function. The requiredsize of the seed is defined withseedsize .
set_ent- Set entropy that would otherwise be obtained fromentropy source. Internal use only.
seedsize- The seed size required for a random number generatorinitialization defined with this variable. Somerandom number generators does not require a seedas the seeding is implemented internally withoutthe need of support by the consumer. In this case,the seed size is set to zero.
base- Common crypto API algorithm data structure.
Crypto API Random Number API¶
The random number generator API is used with the ciphers of typeCRYPTO_ALG_TYPE_RNG (listed as type “rng” in /proc/crypto)
- struct crypto_rng *
crypto_alloc_rng(const char * alg_name, u32 type, u32 mask)¶ - allocate RNG handle
Parameters
constchar*alg_name- is the cra_name / name or cra_driver_name / driver name of themessage digest cipher
u32type- specifies the type of the cipher
u32mask- specifies the mask for the cipher
Description
Allocate a cipher handle for a random number generator. The returned structcrypto_rng is the cipher handle that is required for any subsequentAPI invocation for that random number generator.
For all random number generators, this call creates a new private copy ofthe random number generator that does not share a state with otherinstances. The only exception is the “krng” random number generator whichis a kernel crypto API use case for the get_random_bytes() function of the/dev/random driver.
Return
- allocated cipher handle in case of success; IS_ERR() is true in case
- of an error, PTR_ERR() returns the error code.
Parameters
structcrypto_rng*tfm- cipher handle
Description
Return the generic name (cra_name) of the initialized random number generator
Return
generic name string
- void
crypto_free_rng(struct crypto_rng * tfm)¶ zeroize and free RNG handle
Parameters
structcrypto_rng*tfm- cipher handle to be freed
- int
crypto_rng_generate(struct crypto_rng * tfm, const u8 * src, unsigned int slen, u8 * dst, unsigned int dlen)¶ get random number
Parameters
structcrypto_rng*tfm- cipher handle
constu8*src- Input buffer holding additional data, may be NULL
unsignedintslen- Length of additional data
u8*dst- output buffer holding the random numbers
unsignedintdlen- length of the output buffer
Description
This function fills the caller-allocated buffer with randomnumbers using the random number generator referenced by thecipher handle.
Return
0 function was successful; < 0 if an error occurred
- int
crypto_rng_get_bytes(struct crypto_rng * tfm, u8 * rdata, unsigned int dlen)¶ get random number
Parameters
structcrypto_rng*tfm- cipher handle
u8*rdata- output buffer holding the random numbers
unsignedintdlen- length of the output buffer
Description
This function fills the caller-allocated buffer with random numbers using therandom number generator referenced by the cipher handle.
Return
0 function was successful; < 0 if an error occurred
- int
crypto_rng_reset(struct crypto_rng * tfm, const u8 * seed, unsigned int slen)¶ re-initialize the RNG
Parameters
structcrypto_rng*tfm- cipher handle
constu8*seed- seed input data
unsignedintslen- length of the seed input data
Description
The reset function completely re-initializes the random number generatorreferenced by the cipher handle by clearing the current state. The new stateis initialized with the caller provided seed or automatically, dependingon the random number generator type (the ANSI X9.31 RNG requirescaller-provided seed, the SP800-90A DRBGs perform an automatic seeding).The seed is provided as a parameter to this function call. The provided seedshould have the length of the seed size defined for the random numbergenerator as defined by crypto_rng_seedsize.
Return
0 if the setting of the key was successful; < 0 if an error occurred
- int
crypto_rng_seedsize(struct crypto_rng * tfm)¶ obtain seed size of RNG
Parameters
structcrypto_rng*tfm- cipher handle
Description
The function returns the seed size for the random number generatorreferenced by the cipher handle. This value may be zero if the randomnumber generator does not implement or require a reseeding. For example,the SP800-90A DRBGs implement an automated reseeding after reaching apre-defined threshold.
Return
seed size for the random number generator