18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * RNG: Random Number Generator  algorithms under the crypto API
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
68c2ecf20Sopenharmony_ci * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef _CRYPTO_RNG_H
108c2ecf20Sopenharmony_ci#define _CRYPTO_RNG_H
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/crypto.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistruct crypto_rng;
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/**
178c2ecf20Sopenharmony_ci * struct rng_alg - random number generator definition
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * @generate:	The function defined by this variable obtains a
208c2ecf20Sopenharmony_ci *		random number. The random number generator transform
218c2ecf20Sopenharmony_ci *		must generate the random number out of the context
228c2ecf20Sopenharmony_ci *		provided with this call, plus any additional data
238c2ecf20Sopenharmony_ci *		if provided to the call.
248c2ecf20Sopenharmony_ci * @seed:	Seed or reseed the random number generator.  With the
258c2ecf20Sopenharmony_ci *		invocation of this function call, the random number
268c2ecf20Sopenharmony_ci *		generator shall become ready for generation.  If the
278c2ecf20Sopenharmony_ci *		random number generator requires a seed for setting
288c2ecf20Sopenharmony_ci *		up a new state, the seed must be provided by the
298c2ecf20Sopenharmony_ci *		consumer while invoking this function. The required
308c2ecf20Sopenharmony_ci *		size of the seed is defined with @seedsize .
318c2ecf20Sopenharmony_ci * @set_ent:	Set entropy that would otherwise be obtained from
328c2ecf20Sopenharmony_ci *		entropy source.  Internal use only.
338c2ecf20Sopenharmony_ci * @seedsize:	The seed size required for a random number generator
348c2ecf20Sopenharmony_ci *		initialization defined with this variable. Some
358c2ecf20Sopenharmony_ci *		random number generators does not require a seed
368c2ecf20Sopenharmony_ci *		as the seeding is implemented internally without
378c2ecf20Sopenharmony_ci *		the need of support by the consumer. In this case,
388c2ecf20Sopenharmony_ci *		the seed size is set to zero.
398c2ecf20Sopenharmony_ci * @base:	Common crypto API algorithm data structure.
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_cistruct rng_alg {
428c2ecf20Sopenharmony_ci	int (*generate)(struct crypto_rng *tfm,
438c2ecf20Sopenharmony_ci			const u8 *src, unsigned int slen,
448c2ecf20Sopenharmony_ci			u8 *dst, unsigned int dlen);
458c2ecf20Sopenharmony_ci	int (*seed)(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
468c2ecf20Sopenharmony_ci	void (*set_ent)(struct crypto_rng *tfm, const u8 *data,
478c2ecf20Sopenharmony_ci			unsigned int len);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	unsigned int seedsize;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	struct crypto_alg base;
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistruct crypto_rng {
558c2ecf20Sopenharmony_ci	struct crypto_tfm base;
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ciextern struct crypto_rng *crypto_default_rng;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciint crypto_get_default_rng(void);
618c2ecf20Sopenharmony_civoid crypto_put_default_rng(void);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/**
648c2ecf20Sopenharmony_ci * DOC: Random number generator API
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci * The random number generator API is used with the ciphers of type
678c2ecf20Sopenharmony_ci * CRYPTO_ALG_TYPE_RNG (listed as type "rng" in /proc/crypto)
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/**
718c2ecf20Sopenharmony_ci * crypto_alloc_rng() -- allocate RNG handle
728c2ecf20Sopenharmony_ci * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
738c2ecf20Sopenharmony_ci *	      message digest cipher
748c2ecf20Sopenharmony_ci * @type: specifies the type of the cipher
758c2ecf20Sopenharmony_ci * @mask: specifies the mask for the cipher
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * Allocate a cipher handle for a random number generator. The returned struct
788c2ecf20Sopenharmony_ci * crypto_rng is the cipher handle that is required for any subsequent
798c2ecf20Sopenharmony_ci * API invocation for that random number generator.
808c2ecf20Sopenharmony_ci *
818c2ecf20Sopenharmony_ci * For all random number generators, this call creates a new private copy of
828c2ecf20Sopenharmony_ci * the random number generator that does not share a state with other
838c2ecf20Sopenharmony_ci * instances. The only exception is the "krng" random number generator which
848c2ecf20Sopenharmony_ci * is a kernel crypto API use case for the get_random_bytes() function of the
858c2ecf20Sopenharmony_ci * /dev/random driver.
868c2ecf20Sopenharmony_ci *
878c2ecf20Sopenharmony_ci * Return: allocated cipher handle in case of success; IS_ERR() is true in case
888c2ecf20Sopenharmony_ci *	   of an error, PTR_ERR() returns the error code.
898c2ecf20Sopenharmony_ci */
908c2ecf20Sopenharmony_cistruct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	return &tfm->base;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/**
988c2ecf20Sopenharmony_ci * crypto_rng_alg - obtain name of RNG
998c2ecf20Sopenharmony_ci * @tfm: cipher handle
1008c2ecf20Sopenharmony_ci *
1018c2ecf20Sopenharmony_ci * Return the generic name (cra_name) of the initialized random number generator
1028c2ecf20Sopenharmony_ci *
1038c2ecf20Sopenharmony_ci * Return: generic name string
1048c2ecf20Sopenharmony_ci */
1058c2ecf20Sopenharmony_cistatic inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	return container_of(crypto_rng_tfm(tfm)->__crt_alg,
1088c2ecf20Sopenharmony_ci			    struct rng_alg, base);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/**
1128c2ecf20Sopenharmony_ci * crypto_free_rng() - zeroize and free RNG handle
1138c2ecf20Sopenharmony_ci * @tfm: cipher handle to be freed
1148c2ecf20Sopenharmony_ci *
1158c2ecf20Sopenharmony_ci * If @tfm is a NULL or error pointer, this function does nothing.
1168c2ecf20Sopenharmony_ci */
1178c2ecf20Sopenharmony_cistatic inline void crypto_free_rng(struct crypto_rng *tfm)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	crypto_destroy_tfm(tfm, crypto_rng_tfm(tfm));
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci/**
1238c2ecf20Sopenharmony_ci * crypto_rng_generate() - get random number
1248c2ecf20Sopenharmony_ci * @tfm: cipher handle
1258c2ecf20Sopenharmony_ci * @src: Input buffer holding additional data, may be NULL
1268c2ecf20Sopenharmony_ci * @slen: Length of additional data
1278c2ecf20Sopenharmony_ci * @dst: output buffer holding the random numbers
1288c2ecf20Sopenharmony_ci * @dlen: length of the output buffer
1298c2ecf20Sopenharmony_ci *
1308c2ecf20Sopenharmony_ci * This function fills the caller-allocated buffer with random
1318c2ecf20Sopenharmony_ci * numbers using the random number generator referenced by the
1328c2ecf20Sopenharmony_ci * cipher handle.
1338c2ecf20Sopenharmony_ci *
1348c2ecf20Sopenharmony_ci * Return: 0 function was successful; < 0 if an error occurred
1358c2ecf20Sopenharmony_ci */
1368c2ecf20Sopenharmony_cistatic inline int crypto_rng_generate(struct crypto_rng *tfm,
1378c2ecf20Sopenharmony_ci				      const u8 *src, unsigned int slen,
1388c2ecf20Sopenharmony_ci				      u8 *dst, unsigned int dlen)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	struct crypto_alg *alg = tfm->base.__crt_alg;
1418c2ecf20Sopenharmony_ci	int ret;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	crypto_stats_get(alg);
1448c2ecf20Sopenharmony_ci	ret = crypto_rng_alg(tfm)->generate(tfm, src, slen, dst, dlen);
1458c2ecf20Sopenharmony_ci	crypto_stats_rng_generate(alg, dlen, ret);
1468c2ecf20Sopenharmony_ci	return ret;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci/**
1508c2ecf20Sopenharmony_ci * crypto_rng_get_bytes() - get random number
1518c2ecf20Sopenharmony_ci * @tfm: cipher handle
1528c2ecf20Sopenharmony_ci * @rdata: output buffer holding the random numbers
1538c2ecf20Sopenharmony_ci * @dlen: length of the output buffer
1548c2ecf20Sopenharmony_ci *
1558c2ecf20Sopenharmony_ci * This function fills the caller-allocated buffer with random numbers using the
1568c2ecf20Sopenharmony_ci * random number generator referenced by the cipher handle.
1578c2ecf20Sopenharmony_ci *
1588c2ecf20Sopenharmony_ci * Return: 0 function was successful; < 0 if an error occurred
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_cistatic inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
1618c2ecf20Sopenharmony_ci				       u8 *rdata, unsigned int dlen)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	return crypto_rng_generate(tfm, NULL, 0, rdata, dlen);
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci/**
1678c2ecf20Sopenharmony_ci * crypto_rng_reset() - re-initialize the RNG
1688c2ecf20Sopenharmony_ci * @tfm: cipher handle
1698c2ecf20Sopenharmony_ci * @seed: seed input data
1708c2ecf20Sopenharmony_ci * @slen: length of the seed input data
1718c2ecf20Sopenharmony_ci *
1728c2ecf20Sopenharmony_ci * The reset function completely re-initializes the random number generator
1738c2ecf20Sopenharmony_ci * referenced by the cipher handle by clearing the current state. The new state
1748c2ecf20Sopenharmony_ci * is initialized with the caller provided seed or automatically, depending
1758c2ecf20Sopenharmony_ci * on the random number generator type (the ANSI X9.31 RNG requires
1768c2ecf20Sopenharmony_ci * caller-provided seed, the SP800-90A DRBGs perform an automatic seeding).
1778c2ecf20Sopenharmony_ci * The seed is provided as a parameter to this function call. The provided seed
1788c2ecf20Sopenharmony_ci * should have the length of the seed size defined for the random number
1798c2ecf20Sopenharmony_ci * generator as defined by crypto_rng_seedsize.
1808c2ecf20Sopenharmony_ci *
1818c2ecf20Sopenharmony_ci * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1828c2ecf20Sopenharmony_ci */
1838c2ecf20Sopenharmony_ciint crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed,
1848c2ecf20Sopenharmony_ci		     unsigned int slen);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci/**
1878c2ecf20Sopenharmony_ci * crypto_rng_seedsize() - obtain seed size of RNG
1888c2ecf20Sopenharmony_ci * @tfm: cipher handle
1898c2ecf20Sopenharmony_ci *
1908c2ecf20Sopenharmony_ci * The function returns the seed size for the random number generator
1918c2ecf20Sopenharmony_ci * referenced by the cipher handle. This value may be zero if the random
1928c2ecf20Sopenharmony_ci * number generator does not implement or require a reseeding. For example,
1938c2ecf20Sopenharmony_ci * the SP800-90A DRBGs implement an automated reseeding after reaching a
1948c2ecf20Sopenharmony_ci * pre-defined threshold.
1958c2ecf20Sopenharmony_ci *
1968c2ecf20Sopenharmony_ci * Return: seed size for the random number generator
1978c2ecf20Sopenharmony_ci */
1988c2ecf20Sopenharmony_cistatic inline int crypto_rng_seedsize(struct crypto_rng *tfm)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	return crypto_rng_alg(tfm)->seedsize;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci#endif
204