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