crypto: krng - Convert to new rng interface
[deliverable/linux.git] / include / crypto / rng.h
CommitLineData
17f0f4a4
NH
1/*
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
acec27ff
HX
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 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 .
7ca99d81
HX
35 * @set_ent: Set entropy that would otherwise be obtained from
36 * entropy source. Internal use only.
acec27ff
HX
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);
7ca99d81
HX
50 void (*set_ent)(struct crypto_rng *tfm, const u8 *data,
51 unsigned int len);
acec27ff
HX
52
53 unsigned int seedsize;
54
55 struct crypto_alg base;
56};
57
d0e83059 58struct crypto_rng {
ff030b09
HX
59 int (*generate)(struct crypto_rng *tfm,
60 const u8 *src, unsigned int slen,
61 u8 *dst, unsigned int dlen);
3c5d8fa9 62 int (*seed)(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
acec27ff 63 unsigned int seedsize;
d0e83059
HX
64 struct crypto_tfm base;
65};
66
17f0f4a4
NH
67extern struct crypto_rng *crypto_default_rng;
68
69int crypto_get_default_rng(void);
70void crypto_put_default_rng(void);
71
aa1b6fbc
SM
72/**
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
aa1b6fbc
SM
79/**
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 */
d0e83059 99struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask);
17f0f4a4
NH
100
101static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
102{
103 return &tfm->base;
104}
105
aa1b6fbc
SM
106/**
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 */
17f0f4a4
NH
114static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
115{
acec27ff
HX
116 return container_of(crypto_rng_tfm(tfm)->__crt_alg,
117 struct rng_alg, base);
17f0f4a4
NH
118}
119
aa1b6fbc
SM
120/**
121 * crypto_free_rng() - zeroize and free RNG handle
122 * @tfm: cipher handle to be freed
123 */
17f0f4a4
NH
124static inline void crypto_free_rng(struct crypto_rng *tfm)
125{
d0e83059 126 crypto_destroy_tfm(tfm, crypto_rng_tfm(tfm));
17f0f4a4
NH
127}
128
ff030b09
HX
129/**
130 * 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
aa1b6fbc
SM
150/**
151 * 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 *
cde001e4 159 * Return: 0 function was successful; < 0 if an error occurred
aa1b6fbc 160 */
17f0f4a4
NH
161static inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
162 u8 *rdata, unsigned int dlen)
163{
ff030b09 164 return crypto_rng_generate(tfm, NULL, 0, rdata, dlen);
17f0f4a4
NH
165}
166
aa1b6fbc
SM
167/**
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 */
3c5d8fa9
HX
184int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed,
185 unsigned int slen);
17f0f4a4 186
aa1b6fbc
SM
187/**
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 */
17f0f4a4
NH
199static inline int crypto_rng_seedsize(struct crypto_rng *tfm)
200{
acec27ff 201 return tfm->seedsize;
17f0f4a4
NH
202}
203
204#endif
This page took 0.398124 seconds and 5 git commands to generate.