1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#ifndef _DRBG_H
40#define _DRBG_H
41
42
43#include <linux/random.h>
44#include <linux/scatterlist.h>
45#include <crypto/hash.h>
46#include <crypto/skcipher.h>
47#include <linux/module.h>
48#include <linux/crypto.h>
49#include <linux/slab.h>
50#include <crypto/internal/rng.h>
51#include <crypto/rng.h>
52#include <linux/fips.h>
53#include <linux/mutex.h>
54#include <linux/list.h>
55#include <linux/workqueue.h>
56
57
58
59
60
61
62
63
64
65
66
67struct drbg_string {
68 const unsigned char *buf;
69 size_t len;
70 struct list_head list;
71};
72
73static inline void drbg_string_fill(struct drbg_string *string,
74 const unsigned char *buf, size_t len)
75{
76 string->buf = buf;
77 string->len = len;
78 INIT_LIST_HEAD(&string->list);
79}
80
81struct drbg_state;
82typedef uint32_t drbg_flag_t;
83
84struct drbg_core {
85 drbg_flag_t flags;
86 __u8 statelen;
87 __u8 blocklen_bytes;
88 char cra_name[CRYPTO_MAX_ALG_NAME];
89
90 char backend_cra_name[CRYPTO_MAX_ALG_NAME];
91};
92
93struct drbg_state_ops {
94 int (*update)(struct drbg_state *drbg, struct list_head *seed,
95 int reseed);
96 int (*generate)(struct drbg_state *drbg,
97 unsigned char *buf, unsigned int buflen,
98 struct list_head *addtl);
99 int (*crypto_init)(struct drbg_state *drbg);
100 int (*crypto_fini)(struct drbg_state *drbg);
101
102};
103
104struct drbg_test_data {
105 struct drbg_string *testentropy;
106};
107
108struct drbg_state {
109 struct mutex drbg_mutex;
110 unsigned char *V;
111 unsigned char *Vbuf;
112
113 unsigned char *C;
114 unsigned char *Cbuf;
115
116 size_t reseed_ctr;
117 size_t reseed_threshold;
118
119 unsigned char *scratchpad;
120 unsigned char *scratchpadbuf;
121 void *priv_data;
122
123 struct crypto_skcipher *ctr_handle;
124 struct skcipher_request *ctr_req;
125 __u8 *ctr_null_value_buf;
126 __u8 *ctr_null_value;
127 __u8 *outscratchpadbuf;
128 __u8 *outscratchpad;
129 struct completion ctr_completion;
130 int ctr_async_err;
131
132 bool seeded;
133 bool pr;
134 struct work_struct seed_work;
135 struct crypto_rng *jent;
136 const struct drbg_state_ops *d_ops;
137 const struct drbg_core *core;
138 struct drbg_string test_data;
139 struct random_ready_callback random_ready;
140};
141
142static inline __u8 drbg_statelen(struct drbg_state *drbg)
143{
144 if (drbg && drbg->core)
145 return drbg->core->statelen;
146 return 0;
147}
148
149static inline __u8 drbg_blocklen(struct drbg_state *drbg)
150{
151 if (drbg && drbg->core)
152 return drbg->core->blocklen_bytes;
153 return 0;
154}
155
156static inline __u8 drbg_keylen(struct drbg_state *drbg)
157{
158 if (drbg && drbg->core)
159 return (drbg->core->statelen - drbg->core->blocklen_bytes);
160 return 0;
161}
162
163static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
164{
165
166 return (1 << 16);
167}
168
169static inline size_t drbg_max_addtl(struct drbg_state *drbg)
170{
171
172#if (__BITS_PER_LONG == 32)
173
174
175
176
177
178 return (SIZE_MAX - 1);
179#else
180 return (1UL<<35);
181#endif
182}
183
184static inline size_t drbg_max_requests(struct drbg_state *drbg)
185{
186
187#if (__BITS_PER_LONG == 32)
188 return SIZE_MAX;
189#else
190 return (1UL<<48);
191#endif
192}
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
208 unsigned char *outbuf, unsigned int outlen,
209 struct drbg_string *addtl)
210{
211 return crypto_rng_generate(drng, addtl->buf, addtl->len,
212 outbuf, outlen);
213}
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
233 unsigned char *outbuf, unsigned int outlen,
234 struct drbg_string *addtl,
235 struct drbg_test_data *test_data)
236{
237 crypto_rng_set_entropy(drng, test_data->testentropy->buf,
238 test_data->testentropy->len);
239 return crypto_rng_generate(drng, addtl->buf, addtl->len,
240 outbuf, outlen);
241}
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
258 struct drbg_string *pers,
259 struct drbg_test_data *test_data)
260{
261 crypto_rng_set_entropy(drng, test_data->testentropy->buf,
262 test_data->testentropy->len);
263 return crypto_rng_reset(drng, pers->buf, pers->len);
264}
265
266
267#define DRBG_CTR ((drbg_flag_t)1<<0)
268#define DRBG_HMAC ((drbg_flag_t)1<<1)
269#define DRBG_HASH ((drbg_flag_t)1<<2)
270#define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)
271
272#define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)
273#define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)
274#define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)
275#define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
276 DRBG_STRENGTH256)
277
278enum drbg_prefixes {
279 DRBG_PREFIX0 = 0x00,
280 DRBG_PREFIX1,
281 DRBG_PREFIX2,
282 DRBG_PREFIX3
283};
284
285#endif
286