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