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