Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /* SPDX-License-Identifier: GPL-2.0-or-later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * RNG: Random Number Generator  algorithms under the crypto API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #ifndef _CRYPTO_RNG_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define _CRYPTO_RNG_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) struct crypto_rng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * struct rng_alg - random number generator definition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * @generate:	The function defined by this variable obtains a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *		random number. The random number generator transform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *		must generate the random number out of the context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *		provided with this call, plus any additional data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *		if provided to the call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * @seed:	Seed or reseed the random number generator.  With the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *		invocation of this function call, the random number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *		generator shall become ready for generation.  If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *		random number generator requires a seed for setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *		up a new state, the seed must be provided by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *		consumer while invoking this function. The required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *		size of the seed is defined with @seedsize .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * @set_ent:	Set entropy that would otherwise be obtained from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *		entropy source.  Internal use only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * @seedsize:	The seed size required for a random number generator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *		initialization defined with this variable. Some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  *		random number generators does not require a seed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  *		as the seeding is implemented internally without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  *		the need of support by the consumer. In this case,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  *		the seed size is set to zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * @base:	Common crypto API algorithm data structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) struct rng_alg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	int (*generate)(struct crypto_rng *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 			const u8 *src, unsigned int slen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			u8 *dst, unsigned int dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	int (*seed)(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	void (*set_ent)(struct crypto_rng *tfm, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			unsigned int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	unsigned int seedsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct crypto_alg base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) struct crypto_rng {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct crypto_tfm base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) extern struct crypto_rng *crypto_default_rng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) int crypto_get_default_rng(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) void crypto_put_default_rng(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * DOC: Random number generator API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * The random number generator API is used with the ciphers of type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * CRYPTO_ALG_TYPE_RNG (listed as type "rng" in /proc/crypto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * crypto_alloc_rng() -- allocate RNG handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  *	      message digest cipher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * @type: specifies the type of the cipher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * @mask: specifies the mask for the cipher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * Allocate a cipher handle for a random number generator. The returned struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * crypto_rng is the cipher handle that is required for any subsequent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  * API invocation for that random number generator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * For all random number generators, this call creates a new private copy of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * the random number generator that does not share a state with other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * instances. The only exception is the "krng" random number generator which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * is a kernel crypto API use case for the get_random_bytes() function of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * /dev/random driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  *	   of an error, PTR_ERR() returns the error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	return &tfm->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * crypto_rng_alg - obtain name of RNG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * @tfm: cipher handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * Return the generic name (cra_name) of the initialized random number generator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * Return: generic name string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return container_of(crypto_rng_tfm(tfm)->__crt_alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			    struct rng_alg, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * crypto_free_rng() - zeroize and free RNG handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * @tfm: cipher handle to be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  * If @tfm is a NULL or error pointer, this function does nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static inline void crypto_free_rng(struct crypto_rng *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	crypto_destroy_tfm(tfm, crypto_rng_tfm(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  * crypto_rng_generate() - get random number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  * @tfm: cipher handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  * @src: Input buffer holding additional data, may be NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  * @slen: Length of additional data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  * @dst: output buffer holding the random numbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  * @dlen: length of the output buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)  * This function fills the caller-allocated buffer with random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  * numbers using the random number generator referenced by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  * cipher handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  * Return: 0 function was successful; < 0 if an error occurred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static inline int crypto_rng_generate(struct crypto_rng *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 				      const u8 *src, unsigned int slen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 				      u8 *dst, unsigned int dlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct crypto_alg *alg = tfm->base.__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	crypto_stats_get(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	ret = crypto_rng_alg(tfm)->generate(tfm, src, slen, dst, dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	crypto_stats_rng_generate(alg, dlen, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  * crypto_rng_get_bytes() - get random number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  * @tfm: cipher handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  * @rdata: output buffer holding the random numbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * @dlen: length of the output buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * This function fills the caller-allocated buffer with random numbers using the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  * random number generator referenced by the cipher handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * Return: 0 function was successful; < 0 if an error occurred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				       u8 *rdata, unsigned int dlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return crypto_rng_generate(tfm, NULL, 0, rdata, dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  * crypto_rng_reset() - re-initialize the RNG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * @tfm: cipher handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  * @seed: seed input data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  * @slen: length of the seed input data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  * The reset function completely re-initializes the random number generator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  * referenced by the cipher handle by clearing the current state. The new state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  * is initialized with the caller provided seed or automatically, depending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  * on the random number generator type (the ANSI X9.31 RNG requires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * caller-provided seed, the SP800-90A DRBGs perform an automatic seeding).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  * The seed is provided as a parameter to this function call. The provided seed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  * should have the length of the seed size defined for the random number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  * generator as defined by crypto_rng_seedsize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		     unsigned int slen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  * crypto_rng_seedsize() - obtain seed size of RNG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  * @tfm: cipher handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  * The function returns the seed size for the random number generator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  * referenced by the cipher handle. This value may be zero if the random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  * number generator does not implement or require a reseeding. For example,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  * the SP800-90A DRBGs implement an automated reseeding after reaching a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  * pre-defined threshold.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  * Return: seed size for the random number generator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static inline int crypto_rng_seedsize(struct crypto_rng *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return crypto_rng_alg(tfm)->seedsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) #endif