blob: cc22e52a129aaf2edc9ff9ce6ec79f9a0d6421c3 [file] [log] [blame]
Neil Horman17f0f4a2008-08-14 22:15:52 +10001/*
2 * RNG: Random Number Generator algorithms under the crypto API
3 *
4 * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#ifndef _CRYPTO_RNG_H
14#define _CRYPTO_RNG_H
15
16#include <linux/crypto.h>
17
Herbert Xuacec27f2015-04-21 10:46:38 +080018struct 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 fo 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 .
Herbert Xu7ca99d82015-04-21 10:46:39 +080035 * @set_ent: Set entropy that would otherwise be obtained from
36 * entropy source. Internal use only.
Herbert Xuacec27f2015-04-21 10:46:38 +080037 * @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);
Herbert Xu7ca99d82015-04-21 10:46:39 +080050 void (*set_ent)(struct crypto_rng *tfm, const u8 *data,
51 unsigned int len);
Herbert Xuacec27f2015-04-21 10:46:38 +080052
53 unsigned int seedsize;
54
55 struct crypto_alg base;
56};
57
Herbert Xud0e83052015-04-20 13:39:03 +080058struct crypto_rng {
Herbert Xuff030b02015-04-20 13:39:04 +080059 int (*generate)(struct crypto_rng *tfm,
60 const u8 *src, unsigned int slen,
61 u8 *dst, unsigned int dlen);
Herbert Xu3c5d8fa2015-04-21 10:46:37 +080062 int (*seed)(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
Herbert Xuacec27f2015-04-21 10:46:38 +080063 unsigned int seedsize;
Herbert Xud0e83052015-04-20 13:39:03 +080064 struct crypto_tfm base;
65};
66
Neil Horman17f0f4a2008-08-14 22:15:52 +100067extern struct crypto_rng *crypto_default_rng;
68
69int crypto_get_default_rng(void);
70void crypto_put_default_rng(void);
71
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010072/**
73 * DOC: Random number generator API
74 *
75 * The random number generator API is used with the ciphers of type
76 * CRYPTO_ALG_TYPE_RNG (listed as type "rng" in /proc/crypto)
77 */
78
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +010079/**
80 * crypto_alloc_rng() -- allocate RNG handle
81 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
82 * message digest cipher
83 * @type: specifies the type of the cipher
84 * @mask: specifies the mask for the cipher
85 *
86 * Allocate a cipher handle for a random number generator. The returned struct
87 * crypto_rng is the cipher handle that is required for any subsequent
88 * API invocation for that random number generator.
89 *
90 * For all random number generators, this call creates a new private copy of
91 * the random number generator that does not share a state with other
92 * instances. The only exception is the "krng" random number generator which
93 * is a kernel crypto API use case for the get_random_bytes() function of the
94 * /dev/random driver.
95 *
96 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
97 * of an error, PTR_ERR() returns the error code.
98 */
Herbert Xud0e83052015-04-20 13:39:03 +080099struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask);
Neil Horman17f0f4a2008-08-14 22:15:52 +1000100
101static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
102{
103 return &tfm->base;
104}
105
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100106/**
107 * crypto_rng_alg - obtain name of RNG
108 * @tfm: cipher handle
109 *
110 * Return the generic name (cra_name) of the initialized random number generator
111 *
112 * Return: generic name string
113 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000114static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
115{
Herbert Xuacec27f2015-04-21 10:46:38 +0800116 return container_of(crypto_rng_tfm(tfm)->__crt_alg,
117 struct rng_alg, base);
Neil Horman17f0f4a2008-08-14 22:15:52 +1000118}
119
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100120/**
121 * crypto_free_rng() - zeroize and free RNG handle
122 * @tfm: cipher handle to be freed
123 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000124static inline void crypto_free_rng(struct crypto_rng *tfm)
125{
Herbert Xud0e83052015-04-20 13:39:03 +0800126 crypto_destroy_tfm(tfm, crypto_rng_tfm(tfm));
Neil Horman17f0f4a2008-08-14 22:15:52 +1000127}
128
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100129/**
Herbert Xuff030b02015-04-20 13:39:04 +0800130 * crypto_rng_generate() - get random number
131 * @tfm: cipher handle
132 * @src: Input buffer holding additional data, may be NULL
133 * @slen: Length of additional data
134 * @dst: output buffer holding the random numbers
135 * @dlen: length of the output buffer
136 *
137 * This function fills the caller-allocated buffer with random
138 * numbers using the random number generator referenced by the
139 * cipher handle.
140 *
141 * Return: 0 function was successful; < 0 if an error occurred
142 */
143static inline int crypto_rng_generate(struct crypto_rng *tfm,
144 const u8 *src, unsigned int slen,
145 u8 *dst, unsigned int dlen)
146{
147 return tfm->generate(tfm, src, slen, dst, dlen);
148}
149
150/**
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100151 * crypto_rng_get_bytes() - get random number
152 * @tfm: cipher handle
153 * @rdata: output buffer holding the random numbers
154 * @dlen: length of the output buffer
155 *
156 * This function fills the caller-allocated buffer with random numbers using the
157 * random number generator referenced by the cipher handle.
158 *
Stephan Muellercde001e2015-03-06 08:26:31 +0100159 * Return: 0 function was successful; < 0 if an error occurred
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100160 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000161static inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
162 u8 *rdata, unsigned int dlen)
163{
Herbert Xuff030b02015-04-20 13:39:04 +0800164 return crypto_rng_generate(tfm, NULL, 0, rdata, dlen);
Neil Horman17f0f4a2008-08-14 22:15:52 +1000165}
166
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100167/**
168 * crypto_rng_reset() - re-initialize the RNG
169 * @tfm: cipher handle
170 * @seed: seed input data
171 * @slen: length of the seed input data
172 *
173 * The reset function completely re-initializes the random number generator
174 * referenced by the cipher handle by clearing the current state. The new state
175 * is initialized with the caller provided seed or automatically, depending
176 * on the random number generator type (the ANSI X9.31 RNG requires
177 * caller-provided seed, the SP800-90A DRBGs perform an automatic seeding).
178 * The seed is provided as a parameter to this function call. The provided seed
179 * should have the length of the seed size defined for the random number
180 * generator as defined by crypto_rng_seedsize.
181 *
182 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
183 */
Herbert Xu3c5d8fa2015-04-21 10:46:37 +0800184int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed,
185 unsigned int slen);
Neil Horman17f0f4a2008-08-14 22:15:52 +1000186
Stephan Muelleraa1b6fb2014-11-12 05:25:31 +0100187/**
188 * crypto_rng_seedsize() - obtain seed size of RNG
189 * @tfm: cipher handle
190 *
191 * The function returns the seed size for the random number generator
192 * referenced by the cipher handle. This value may be zero if the random
193 * number generator does not implement or require a reseeding. For example,
194 * the SP800-90A DRBGs implement an automated reseeding after reaching a
195 * pre-defined threshold.
196 *
197 * Return: seed size for the random number generator
198 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000199static inline int crypto_rng_seedsize(struct crypto_rng *tfm)
200{
Herbert Xuacec27f2015-04-21 10:46:38 +0800201 return tfm->seedsize;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000202}
203
204#endif