/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* RNG: Random Number Generator algorithms under the crypto API3*4* Copyright (c) 2008 Neil Horman <[email protected]>5* Copyright (c) 2015 Herbert Xu <[email protected]>6*/78#ifndef _CRYPTO_RNG_H9#define _CRYPTO_RNG_H1011#include <linux/atomic.h>12#include <linux/container_of.h>13#include <linux/crypto.h>1415struct crypto_rng;1617/**18* struct rng_alg - random number generator definition19*20* @generate: The function defined by this variable obtains a21* random number. The random number generator transform22* must generate the random number out of the context23* provided with this call, plus any additional data24* if provided to the call.25* @seed: Seed or reseed the random number generator. With the26* invocation of this function call, the random number27* generator shall become ready for generation. If the28* random number generator requires a seed for setting29* up a new state, the seed must be provided by the30* consumer while invoking this function. The required31* size of the seed is defined with @seedsize .32* @set_ent: Set entropy that would otherwise be obtained from33* entropy source. Internal use only.34* @seedsize: The seed size required for a random number generator35* initialization defined with this variable. Some36* random number generators does not require a seed37* as the seeding is implemented internally without38* the need of support by the consumer. In this case,39* the seed size is set to zero.40* @base: Common crypto API algorithm data structure.41*/42struct rng_alg {43int (*generate)(struct crypto_rng *tfm,44const u8 *src, unsigned int slen,45u8 *dst, unsigned int dlen);46int (*seed)(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);47void (*set_ent)(struct crypto_rng *tfm, const u8 *data,48unsigned int len);4950unsigned int seedsize;5152struct crypto_alg base;53};5455struct crypto_rng {56struct crypto_tfm base;57};5859extern struct crypto_rng *crypto_default_rng;6061int crypto_get_default_rng(void);62void crypto_put_default_rng(void);6364/**65* DOC: Random number generator API66*67* The random number generator API is used with the ciphers of type68* CRYPTO_ALG_TYPE_RNG (listed as type "rng" in /proc/crypto)69*/7071/**72* crypto_alloc_rng() -- allocate RNG handle73* @alg_name: is the cra_name / name or cra_driver_name / driver name of the74* message digest cipher75* @type: specifies the type of the cipher76* @mask: specifies the mask for the cipher77*78* Allocate a cipher handle for a random number generator. The returned struct79* crypto_rng is the cipher handle that is required for any subsequent80* API invocation for that random number generator.81*82* For all random number generators, this call creates a new private copy of83* the random number generator that does not share a state with other84* instances. The only exception is the "krng" random number generator which85* is a kernel crypto API use case for the get_random_bytes() function of the86* /dev/random driver.87*88* Return: allocated cipher handle in case of success; IS_ERR() is true in case89* of an error, PTR_ERR() returns the error code.90*/91struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask);9293static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)94{95return &tfm->base;96}9798static inline struct rng_alg *__crypto_rng_alg(struct crypto_alg *alg)99{100return container_of(alg, struct rng_alg, base);101}102103/**104* crypto_rng_alg() - obtain 'struct rng_alg' pointer from RNG handle105* @tfm: RNG handle106*107* Return: Pointer to 'struct rng_alg', derived from @tfm RNG handle108*/109static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)110{111return __crypto_rng_alg(crypto_rng_tfm(tfm)->__crt_alg);112}113114/**115* crypto_free_rng() - zeroize and free RNG handle116* @tfm: cipher handle to be freed117*118* If @tfm is a NULL or error pointer, this function does nothing.119*/120static inline void crypto_free_rng(struct crypto_rng *tfm)121{122crypto_destroy_tfm(tfm, crypto_rng_tfm(tfm));123}124125/**126* crypto_rng_generate() - get random number127* @tfm: cipher handle128* @src: Input buffer holding additional data, may be NULL129* @slen: Length of additional data130* @dst: output buffer holding the random numbers131* @dlen: length of the output buffer132*133* This function fills the caller-allocated buffer with random134* numbers using the random number generator referenced by the135* cipher handle.136*137* Return: 0 function was successful; < 0 if an error occurred138*/139static inline int crypto_rng_generate(struct crypto_rng *tfm,140const u8 *src, unsigned int slen,141u8 *dst, unsigned int dlen)142{143return crypto_rng_alg(tfm)->generate(tfm, src, slen, dst, dlen);144}145146/**147* crypto_rng_get_bytes() - get random number148* @tfm: cipher handle149* @rdata: output buffer holding the random numbers150* @dlen: length of the output buffer151*152* This function fills the caller-allocated buffer with random numbers using the153* random number generator referenced by the cipher handle.154*155* Return: 0 function was successful; < 0 if an error occurred156*/157static inline int crypto_rng_get_bytes(struct crypto_rng *tfm,158u8 *rdata, unsigned int dlen)159{160return crypto_rng_generate(tfm, NULL, 0, rdata, dlen);161}162163/**164* crypto_rng_reset() - re-initialize the RNG165* @tfm: cipher handle166* @seed: seed input data167* @slen: length of the seed input data168*169* The reset function completely re-initializes the random number generator170* referenced by the cipher handle by clearing the current state. The new state171* is initialized with the caller provided seed or automatically, depending172* on the random number generator type (the ANSI X9.31 RNG requires173* caller-provided seed, the SP800-90A DRBGs perform an automatic seeding).174* The seed is provided as a parameter to this function call. The provided seed175* should have the length of the seed size defined for the random number176* generator as defined by crypto_rng_seedsize.177*178* Return: 0 if the setting of the key was successful; < 0 if an error occurred179*/180int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed,181unsigned int slen);182183/**184* crypto_rng_seedsize() - obtain seed size of RNG185* @tfm: cipher handle186*187* The function returns the seed size for the random number generator188* referenced by the cipher handle. This value may be zero if the random189* number generator does not implement or require a reseeding. For example,190* the SP800-90A DRBGs implement an automated reseeding after reaching a191* pre-defined threshold.192*193* Return: seed size for the random number generator194*/195static inline int crypto_rng_seedsize(struct crypto_rng *tfm)196{197return crypto_rng_alg(tfm)->seedsize;198}199200#endif201202203