blob: 5ac4388f50e13715a68f78e70a2398a3200920f8 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Neil Horman17f0f4a2008-08-14 22:15:52 +10002/*
3 * RNG: Random Number Generator algorithms under the crypto API
4 *
5 * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
Herbert Xu94f1bb12015-04-21 10:46:46 +08006 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
Neil Horman17f0f4a2008-08-14 22:15:52 +10007 */
8
9#ifndef _CRYPTO_RNG_H
10#define _CRYPTO_RNG_H
11
Herbert Xu9807e492023-02-16 18:35:23 +080012#include <linux/atomic.h>
13#include <linux/container_of.h>
Neil Horman17f0f4a2008-08-14 22:15:52 +100014#include <linux/crypto.h>
15
Herbert Xuacec27f2015-04-21 10:46:38 +080016struct 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
Masanari Iida12f7c142015-06-04 00:01:21 +090028 * generator shall become ready for generation. If the
Herbert Xuacec27f2015-04-21 10:46:38 +080029 * 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 .
Herbert Xu7ca99d82015-04-21 10:46:39 +080033 * @set_ent: Set entropy that would otherwise be obtained from
34 * entropy source. Internal use only.
Herbert Xuacec27f2015-04-21 10:46:38 +080035 * @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 */
43struct 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);
Herbert Xu7ca99d82015-04-21 10:46:39 +080048 void (*set_ent)(struct crypto_rng *tfm, const u8 *data,
49 unsigned int len);
Herbert Xuacec27f2015-04-21 10:46:38 +080050
51 unsigned int seedsize;
52
53 struct crypto_alg base;
54};
55
Herbert Xud0e83052015-04-20 13:39:03 +080056struct crypto_rng {
Herbert Xud0e83052015-04-20 13:39:03 +080057 struct crypto_tfm base;
58};
59
Neil Horman17f0f4a2008-08-14 22:15:52 +100060extern struct crypto_rng *crypto_default_rng;
61
62int crypto_get_default_rng(void);
63void crypto_put_default_rng(void);
64
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010065/**
66 * DOC: Random number generator API
67 *
68 * The random number generator API is used with the ciphers of type
69 * CRYPTO_ALG_TYPE_RNG (listed as type "rng" in /proc/crypto)
70 */
71
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010072/**
73 * crypto_alloc_rng() -- allocate RNG handle
74 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
75 * message digest cipher
76 * @type: specifies the type of the cipher
77 * @mask: specifies the mask for the cipher
78 *
79 * Allocate a cipher handle for a random number generator. The returned struct
80 * crypto_rng is the cipher handle that is required for any subsequent
81 * API invocation for that random number generator.
82 *
83 * For all random number generators, this call creates a new private copy of
84 * the random number generator that does not share a state with other
85 * instances. The only exception is the "krng" random number generator which
86 * is a kernel crypto API use case for the get_random_bytes() function of the
87 * /dev/random driver.
88 *
89 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
90 * of an error, PTR_ERR() returns the error code.
91 */
Herbert Xud0e83052015-04-20 13:39:03 +080092struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask);
Neil Horman17f0f4a2008-08-14 22:15:52 +100093
94static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
95{
96 return &tfm->base;
97}
98
Herbert Xu9807e492023-02-16 18:35:23 +080099static inline struct rng_alg *__crypto_rng_alg(struct crypto_alg *alg)
100{
101 return container_of(alg, struct rng_alg, base);
102}
103
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100104/**
105 * crypto_rng_alg - obtain name of RNG
106 * @tfm: cipher handle
107 *
108 * Return the generic name (cra_name) of the initialized random number generator
109 *
110 * Return: generic name string
111 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000112static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
113{
Herbert Xu9807e492023-02-16 18:35:23 +0800114 return __crypto_rng_alg(crypto_rng_tfm(tfm)->__crt_alg);
Neil Horman17f0f4a2008-08-14 22:15:52 +1000115}
116
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100117/**
118 * crypto_free_rng() - zeroize and free RNG handle
119 * @tfm: cipher handle to be freed
Ard Biesheuvel83681f22021-03-02 21:33:03 +0100120 *
121 * If @tfm is a NULL or error pointer, this function does nothing.
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100122 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000123static inline void crypto_free_rng(struct crypto_rng *tfm)
124{
Herbert Xud0e83052015-04-20 13:39:03 +0800125 crypto_destroy_tfm(tfm, crypto_rng_tfm(tfm));
Neil Horman17f0f4a2008-08-14 22:15:52 +1000126}
127
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100128/**
Herbert Xuff030b02015-04-20 13:39:04 +0800129 * crypto_rng_generate() - get random number
130 * @tfm: cipher handle
131 * @src: Input buffer holding additional data, may be NULL
132 * @slen: Length of additional data
133 * @dst: output buffer holding the random numbers
134 * @dlen: length of the output buffer
135 *
136 * This function fills the caller-allocated buffer with random
137 * numbers using the random number generator referenced by the
138 * cipher handle.
139 *
140 * Return: 0 function was successful; < 0 if an error occurred
141 */
142static inline int crypto_rng_generate(struct crypto_rng *tfm,
143 const u8 *src, unsigned int slen,
144 u8 *dst, unsigned int dlen)
145{
Eric Biggers29ce50e2024-03-12 20:48:21 -0700146 return crypto_rng_alg(tfm)->generate(tfm, src, slen, dst, dlen);
Herbert Xuff030b02015-04-20 13:39:04 +0800147}
148
149/**
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100150 * crypto_rng_get_bytes() - get random number
151 * @tfm: cipher handle
152 * @rdata: output buffer holding the random numbers
153 * @dlen: length of the output buffer
154 *
155 * This function fills the caller-allocated buffer with random numbers using the
156 * random number generator referenced by the cipher handle.
157 *
Stephan Muellercde001e2015-03-06 08:26:31 +0100158 * Return: 0 function was successful; < 0 if an error occurred
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100159 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000160static inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
161 u8 *rdata, unsigned int dlen)
162{
Herbert Xuff030b02015-04-20 13:39:04 +0800163 return crypto_rng_generate(tfm, NULL, 0, rdata, dlen);
Neil Horman17f0f4a2008-08-14 22:15:52 +1000164}
165
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100166/**
167 * crypto_rng_reset() - re-initialize the RNG
168 * @tfm: cipher handle
169 * @seed: seed input data
170 * @slen: length of the seed input data
171 *
172 * The reset function completely re-initializes the random number generator
173 * referenced by the cipher handle by clearing the current state. The new state
174 * is initialized with the caller provided seed or automatically, depending
175 * on the random number generator type (the ANSI X9.31 RNG requires
176 * caller-provided seed, the SP800-90A DRBGs perform an automatic seeding).
177 * The seed is provided as a parameter to this function call. The provided seed
178 * should have the length of the seed size defined for the random number
179 * generator as defined by crypto_rng_seedsize.
180 *
181 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
182 */
Herbert Xu3c5d8fa2015-04-21 10:46:37 +0800183int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed,
184 unsigned int slen);
Neil Horman17f0f4a2008-08-14 22:15:52 +1000185
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100186/**
187 * crypto_rng_seedsize() - obtain seed size of RNG
188 * @tfm: cipher handle
189 *
190 * The function returns the seed size for the random number generator
191 * referenced by the cipher handle. This value may be zero if the random
192 * number generator does not implement or require a reseeding. For example,
193 * the SP800-90A DRBGs implement an automated reseeding after reaching a
194 * pre-defined threshold.
195 *
196 * Return: seed size for the random number generator
197 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000198static inline int crypto_rng_seedsize(struct crypto_rng *tfm)
199{
Herbert Xu94f1bb12015-04-21 10:46:46 +0800200 return crypto_rng_alg(tfm)->seedsize;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000201}
202
203#endif