162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * RNG: Random Number Generator  algorithms under the crypto API
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
662306a36Sopenharmony_ci * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#ifndef _CRYPTO_RNG_H
1062306a36Sopenharmony_ci#define _CRYPTO_RNG_H
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/atomic.h>
1362306a36Sopenharmony_ci#include <linux/container_of.h>
1462306a36Sopenharmony_ci#include <linux/crypto.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cistruct crypto_rng;
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci/*
1962306a36Sopenharmony_ci * struct crypto_istat_rng: statistics for RNG algorithm
2062306a36Sopenharmony_ci * @generate_cnt:	number of RNG generate requests
2162306a36Sopenharmony_ci * @generate_tlen:	total data size of generated data by the RNG
2262306a36Sopenharmony_ci * @seed_cnt:		number of times the RNG was seeded
2362306a36Sopenharmony_ci * @err_cnt:		number of error for RNG requests
2462306a36Sopenharmony_ci */
2562306a36Sopenharmony_cistruct crypto_istat_rng {
2662306a36Sopenharmony_ci	atomic64_t generate_cnt;
2762306a36Sopenharmony_ci	atomic64_t generate_tlen;
2862306a36Sopenharmony_ci	atomic64_t seed_cnt;
2962306a36Sopenharmony_ci	atomic64_t err_cnt;
3062306a36Sopenharmony_ci};
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/**
3362306a36Sopenharmony_ci * struct rng_alg - random number generator definition
3462306a36Sopenharmony_ci *
3562306a36Sopenharmony_ci * @generate:	The function defined by this variable obtains a
3662306a36Sopenharmony_ci *		random number. The random number generator transform
3762306a36Sopenharmony_ci *		must generate the random number out of the context
3862306a36Sopenharmony_ci *		provided with this call, plus any additional data
3962306a36Sopenharmony_ci *		if provided to the call.
4062306a36Sopenharmony_ci * @seed:	Seed or reseed the random number generator.  With the
4162306a36Sopenharmony_ci *		invocation of this function call, the random number
4262306a36Sopenharmony_ci *		generator shall become ready for generation.  If the
4362306a36Sopenharmony_ci *		random number generator requires a seed for setting
4462306a36Sopenharmony_ci *		up a new state, the seed must be provided by the
4562306a36Sopenharmony_ci *		consumer while invoking this function. The required
4662306a36Sopenharmony_ci *		size of the seed is defined with @seedsize .
4762306a36Sopenharmony_ci * @set_ent:	Set entropy that would otherwise be obtained from
4862306a36Sopenharmony_ci *		entropy source.  Internal use only.
4962306a36Sopenharmony_ci * @stat:	Statistics for rng algorithm
5062306a36Sopenharmony_ci * @seedsize:	The seed size required for a random number generator
5162306a36Sopenharmony_ci *		initialization defined with this variable. Some
5262306a36Sopenharmony_ci *		random number generators does not require a seed
5362306a36Sopenharmony_ci *		as the seeding is implemented internally without
5462306a36Sopenharmony_ci *		the need of support by the consumer. In this case,
5562306a36Sopenharmony_ci *		the seed size is set to zero.
5662306a36Sopenharmony_ci * @base:	Common crypto API algorithm data structure.
5762306a36Sopenharmony_ci */
5862306a36Sopenharmony_cistruct rng_alg {
5962306a36Sopenharmony_ci	int (*generate)(struct crypto_rng *tfm,
6062306a36Sopenharmony_ci			const u8 *src, unsigned int slen,
6162306a36Sopenharmony_ci			u8 *dst, unsigned int dlen);
6262306a36Sopenharmony_ci	int (*seed)(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
6362306a36Sopenharmony_ci	void (*set_ent)(struct crypto_rng *tfm, const u8 *data,
6462306a36Sopenharmony_ci			unsigned int len);
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#ifdef CONFIG_CRYPTO_STATS
6762306a36Sopenharmony_ci	struct crypto_istat_rng stat;
6862306a36Sopenharmony_ci#endif
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	unsigned int seedsize;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	struct crypto_alg base;
7362306a36Sopenharmony_ci};
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cistruct crypto_rng {
7662306a36Sopenharmony_ci	struct crypto_tfm base;
7762306a36Sopenharmony_ci};
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciextern struct crypto_rng *crypto_default_rng;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciint crypto_get_default_rng(void);
8262306a36Sopenharmony_civoid crypto_put_default_rng(void);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci/**
8562306a36Sopenharmony_ci * DOC: Random number generator API
8662306a36Sopenharmony_ci *
8762306a36Sopenharmony_ci * The random number generator API is used with the ciphers of type
8862306a36Sopenharmony_ci * CRYPTO_ALG_TYPE_RNG (listed as type "rng" in /proc/crypto)
8962306a36Sopenharmony_ci */
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci/**
9262306a36Sopenharmony_ci * crypto_alloc_rng() -- allocate RNG handle
9362306a36Sopenharmony_ci * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
9462306a36Sopenharmony_ci *	      message digest cipher
9562306a36Sopenharmony_ci * @type: specifies the type of the cipher
9662306a36Sopenharmony_ci * @mask: specifies the mask for the cipher
9762306a36Sopenharmony_ci *
9862306a36Sopenharmony_ci * Allocate a cipher handle for a random number generator. The returned struct
9962306a36Sopenharmony_ci * crypto_rng is the cipher handle that is required for any subsequent
10062306a36Sopenharmony_ci * API invocation for that random number generator.
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci * For all random number generators, this call creates a new private copy of
10362306a36Sopenharmony_ci * the random number generator that does not share a state with other
10462306a36Sopenharmony_ci * instances. The only exception is the "krng" random number generator which
10562306a36Sopenharmony_ci * is a kernel crypto API use case for the get_random_bytes() function of the
10662306a36Sopenharmony_ci * /dev/random driver.
10762306a36Sopenharmony_ci *
10862306a36Sopenharmony_ci * Return: allocated cipher handle in case of success; IS_ERR() is true in case
10962306a36Sopenharmony_ci *	   of an error, PTR_ERR() returns the error code.
11062306a36Sopenharmony_ci */
11162306a36Sopenharmony_cistruct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask);
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cistatic inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	return &tfm->base;
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic inline struct rng_alg *__crypto_rng_alg(struct crypto_alg *alg)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	return container_of(alg, struct rng_alg, base);
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci/**
12462306a36Sopenharmony_ci * crypto_rng_alg - obtain name of RNG
12562306a36Sopenharmony_ci * @tfm: cipher handle
12662306a36Sopenharmony_ci *
12762306a36Sopenharmony_ci * Return the generic name (cra_name) of the initialized random number generator
12862306a36Sopenharmony_ci *
12962306a36Sopenharmony_ci * Return: generic name string
13062306a36Sopenharmony_ci */
13162306a36Sopenharmony_cistatic inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
13262306a36Sopenharmony_ci{
13362306a36Sopenharmony_ci	return __crypto_rng_alg(crypto_rng_tfm(tfm)->__crt_alg);
13462306a36Sopenharmony_ci}
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci/**
13762306a36Sopenharmony_ci * crypto_free_rng() - zeroize and free RNG handle
13862306a36Sopenharmony_ci * @tfm: cipher handle to be freed
13962306a36Sopenharmony_ci *
14062306a36Sopenharmony_ci * If @tfm is a NULL or error pointer, this function does nothing.
14162306a36Sopenharmony_ci */
14262306a36Sopenharmony_cistatic inline void crypto_free_rng(struct crypto_rng *tfm)
14362306a36Sopenharmony_ci{
14462306a36Sopenharmony_ci	crypto_destroy_tfm(tfm, crypto_rng_tfm(tfm));
14562306a36Sopenharmony_ci}
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_cistatic inline struct crypto_istat_rng *rng_get_stat(struct rng_alg *alg)
14862306a36Sopenharmony_ci{
14962306a36Sopenharmony_ci#ifdef CONFIG_CRYPTO_STATS
15062306a36Sopenharmony_ci	return &alg->stat;
15162306a36Sopenharmony_ci#else
15262306a36Sopenharmony_ci	return NULL;
15362306a36Sopenharmony_ci#endif
15462306a36Sopenharmony_ci}
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_cistatic inline int crypto_rng_errstat(struct rng_alg *alg, int err)
15762306a36Sopenharmony_ci{
15862306a36Sopenharmony_ci	if (!IS_ENABLED(CONFIG_CRYPTO_STATS))
15962306a36Sopenharmony_ci		return err;
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	if (err && err != -EINPROGRESS && err != -EBUSY)
16262306a36Sopenharmony_ci		atomic64_inc(&rng_get_stat(alg)->err_cnt);
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	return err;
16562306a36Sopenharmony_ci}
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci/**
16862306a36Sopenharmony_ci * crypto_rng_generate() - get random number
16962306a36Sopenharmony_ci * @tfm: cipher handle
17062306a36Sopenharmony_ci * @src: Input buffer holding additional data, may be NULL
17162306a36Sopenharmony_ci * @slen: Length of additional data
17262306a36Sopenharmony_ci * @dst: output buffer holding the random numbers
17362306a36Sopenharmony_ci * @dlen: length of the output buffer
17462306a36Sopenharmony_ci *
17562306a36Sopenharmony_ci * This function fills the caller-allocated buffer with random
17662306a36Sopenharmony_ci * numbers using the random number generator referenced by the
17762306a36Sopenharmony_ci * cipher handle.
17862306a36Sopenharmony_ci *
17962306a36Sopenharmony_ci * Return: 0 function was successful; < 0 if an error occurred
18062306a36Sopenharmony_ci */
18162306a36Sopenharmony_cistatic inline int crypto_rng_generate(struct crypto_rng *tfm,
18262306a36Sopenharmony_ci				      const u8 *src, unsigned int slen,
18362306a36Sopenharmony_ci				      u8 *dst, unsigned int dlen)
18462306a36Sopenharmony_ci{
18562306a36Sopenharmony_ci	struct rng_alg *alg = crypto_rng_alg(tfm);
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	if (IS_ENABLED(CONFIG_CRYPTO_STATS)) {
18862306a36Sopenharmony_ci		struct crypto_istat_rng *istat = rng_get_stat(alg);
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci		atomic64_inc(&istat->generate_cnt);
19162306a36Sopenharmony_ci		atomic64_add(dlen, &istat->generate_tlen);
19262306a36Sopenharmony_ci	}
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	return crypto_rng_errstat(alg,
19562306a36Sopenharmony_ci				  alg->generate(tfm, src, slen, dst, dlen));
19662306a36Sopenharmony_ci}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci/**
19962306a36Sopenharmony_ci * crypto_rng_get_bytes() - get random number
20062306a36Sopenharmony_ci * @tfm: cipher handle
20162306a36Sopenharmony_ci * @rdata: output buffer holding the random numbers
20262306a36Sopenharmony_ci * @dlen: length of the output buffer
20362306a36Sopenharmony_ci *
20462306a36Sopenharmony_ci * This function fills the caller-allocated buffer with random numbers using the
20562306a36Sopenharmony_ci * random number generator referenced by the cipher handle.
20662306a36Sopenharmony_ci *
20762306a36Sopenharmony_ci * Return: 0 function was successful; < 0 if an error occurred
20862306a36Sopenharmony_ci */
20962306a36Sopenharmony_cistatic inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
21062306a36Sopenharmony_ci				       u8 *rdata, unsigned int dlen)
21162306a36Sopenharmony_ci{
21262306a36Sopenharmony_ci	return crypto_rng_generate(tfm, NULL, 0, rdata, dlen);
21362306a36Sopenharmony_ci}
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci/**
21662306a36Sopenharmony_ci * crypto_rng_reset() - re-initialize the RNG
21762306a36Sopenharmony_ci * @tfm: cipher handle
21862306a36Sopenharmony_ci * @seed: seed input data
21962306a36Sopenharmony_ci * @slen: length of the seed input data
22062306a36Sopenharmony_ci *
22162306a36Sopenharmony_ci * The reset function completely re-initializes the random number generator
22262306a36Sopenharmony_ci * referenced by the cipher handle by clearing the current state. The new state
22362306a36Sopenharmony_ci * is initialized with the caller provided seed or automatically, depending
22462306a36Sopenharmony_ci * on the random number generator type (the ANSI X9.31 RNG requires
22562306a36Sopenharmony_ci * caller-provided seed, the SP800-90A DRBGs perform an automatic seeding).
22662306a36Sopenharmony_ci * The seed is provided as a parameter to this function call. The provided seed
22762306a36Sopenharmony_ci * should have the length of the seed size defined for the random number
22862306a36Sopenharmony_ci * generator as defined by crypto_rng_seedsize.
22962306a36Sopenharmony_ci *
23062306a36Sopenharmony_ci * Return: 0 if the setting of the key was successful; < 0 if an error occurred
23162306a36Sopenharmony_ci */
23262306a36Sopenharmony_ciint crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed,
23362306a36Sopenharmony_ci		     unsigned int slen);
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci/**
23662306a36Sopenharmony_ci * crypto_rng_seedsize() - obtain seed size of RNG
23762306a36Sopenharmony_ci * @tfm: cipher handle
23862306a36Sopenharmony_ci *
23962306a36Sopenharmony_ci * The function returns the seed size for the random number generator
24062306a36Sopenharmony_ci * referenced by the cipher handle. This value may be zero if the random
24162306a36Sopenharmony_ci * number generator does not implement or require a reseeding. For example,
24262306a36Sopenharmony_ci * the SP800-90A DRBGs implement an automated reseeding after reaching a
24362306a36Sopenharmony_ci * pre-defined threshold.
24462306a36Sopenharmony_ci *
24562306a36Sopenharmony_ci * Return: seed size for the random number generator
24662306a36Sopenharmony_ci */
24762306a36Sopenharmony_cistatic inline int crypto_rng_seedsize(struct crypto_rng *tfm)
24862306a36Sopenharmony_ci{
24962306a36Sopenharmony_ci	return crypto_rng_alg(tfm)->seedsize;
25062306a36Sopenharmony_ci}
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci#endif
253