]>
Commit | Line | Data |
---|---|---|
17f0f4a4 NH |
1 | /* |
2 | * Cryptographic API. | |
3 | * | |
4 | * RNG operations. | |
5 | * | |
6 | * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the Free | |
10 | * Software Foundation; either version 2 of the License, or (at your option) | |
11 | * any later version. | |
12 | * | |
13 | */ | |
14 | ||
60063497 | 15 | #include <linux/atomic.h> |
17f0f4a4 NH |
16 | #include <crypto/internal/rng.h> |
17 | #include <linux/err.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/mutex.h> | |
20 | #include <linux/random.h> | |
21 | #include <linux/seq_file.h> | |
5a0e3ad6 | 22 | #include <linux/slab.h> |
17f0f4a4 | 23 | #include <linux/string.h> |
792608e9 SK |
24 | #include <linux/cryptouser.h> |
25 | #include <net/netlink.h> | |
17f0f4a4 | 26 | |
d0e83059 HX |
27 | #include "internal.h" |
28 | ||
17f0f4a4 NH |
29 | static DEFINE_MUTEX(crypto_default_rng_lock); |
30 | struct crypto_rng *crypto_default_rng; | |
31 | EXPORT_SYMBOL_GPL(crypto_default_rng); | |
32 | static int crypto_default_rng_refcnt; | |
33 | ||
d0e83059 HX |
34 | static inline struct crypto_rng *__crypto_rng_cast(struct crypto_tfm *tfm) |
35 | { | |
36 | return container_of(tfm, struct crypto_rng, base); | |
37 | } | |
38 | ||
acec27ff HX |
39 | static inline struct old_rng_alg *crypto_old_rng_alg(struct crypto_rng *tfm) |
40 | { | |
41 | return &crypto_rng_tfm(tfm)->__crt_alg->cra_rng; | |
42 | } | |
43 | ||
ff030b09 HX |
44 | static int generate(struct crypto_rng *tfm, const u8 *src, unsigned int slen, |
45 | u8 *dst, unsigned int dlen) | |
46 | { | |
acec27ff | 47 | return crypto_old_rng_alg(tfm)->rng_make_random(tfm, dst, dlen); |
ff030b09 HX |
48 | } |
49 | ||
3c5d8fa9 HX |
50 | static int rngapi_reset(struct crypto_rng *tfm, const u8 *seed, |
51 | unsigned int slen) | |
52 | { | |
53 | u8 *buf = NULL; | |
54 | u8 *src = (u8 *)seed; | |
55 | int err; | |
56 | ||
57 | if (slen) { | |
58 | buf = kmalloc(slen, GFP_KERNEL); | |
59 | if (!buf) | |
60 | return -ENOMEM; | |
61 | ||
62 | memcpy(buf, seed, slen); | |
63 | src = buf; | |
64 | } | |
65 | ||
acec27ff | 66 | err = crypto_old_rng_alg(tfm)->rng_reset(tfm, src, slen); |
3c5d8fa9 HX |
67 | |
68 | kzfree(buf); | |
69 | return err; | |
70 | } | |
71 | ||
72 | int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed, unsigned int slen) | |
17f0f4a4 NH |
73 | { |
74 | u8 *buf = NULL; | |
75 | int err; | |
76 | ||
77 | if (!seed && slen) { | |
78 | buf = kmalloc(slen, GFP_KERNEL); | |
79 | if (!buf) | |
80 | return -ENOMEM; | |
81 | ||
82 | get_random_bytes(buf, slen); | |
83 | seed = buf; | |
84 | } | |
85 | ||
3c5d8fa9 | 86 | err = tfm->seed(tfm, seed, slen); |
17f0f4a4 NH |
87 | |
88 | kfree(buf); | |
89 | return err; | |
90 | } | |
3c5d8fa9 | 91 | EXPORT_SYMBOL_GPL(crypto_rng_reset); |
17f0f4a4 | 92 | |
d0e83059 | 93 | static int crypto_rng_init_tfm(struct crypto_tfm *tfm) |
17f0f4a4 | 94 | { |
d0e83059 | 95 | struct crypto_rng *rng = __crypto_rng_cast(tfm); |
acec27ff HX |
96 | struct rng_alg *alg = crypto_rng_alg(rng); |
97 | struct old_rng_alg *oalg = crypto_old_rng_alg(rng); | |
98 | ||
99 | if (oalg->rng_make_random) { | |
100 | rng->generate = generate; | |
101 | rng->seed = rngapi_reset; | |
102 | rng->seedsize = oalg->seedsize; | |
103 | return 0; | |
104 | } | |
17f0f4a4 | 105 | |
acec27ff HX |
106 | rng->generate = alg->generate; |
107 | rng->seed = alg->seed; | |
108 | rng->seedsize = alg->seedsize; | |
17f0f4a4 NH |
109 | |
110 | return 0; | |
111 | } | |
112 | ||
acec27ff HX |
113 | static unsigned int seedsize(struct crypto_alg *alg) |
114 | { | |
115 | struct rng_alg *ralg = container_of(alg, struct rng_alg, base); | |
116 | ||
117 | return alg->cra_rng.rng_make_random ? | |
118 | alg->cra_rng.seedsize : ralg->seedsize; | |
119 | } | |
120 | ||
3acc8473 | 121 | #ifdef CONFIG_NET |
792608e9 SK |
122 | static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg) |
123 | { | |
124 | struct crypto_report_rng rrng; | |
125 | ||
9a5467bf | 126 | strncpy(rrng.type, "rng", sizeof(rrng.type)); |
792608e9 | 127 | |
acec27ff | 128 | rrng.seedsize = seedsize(alg); |
792608e9 | 129 | |
6662df33 DM |
130 | if (nla_put(skb, CRYPTOCFGA_REPORT_RNG, |
131 | sizeof(struct crypto_report_rng), &rrng)) | |
132 | goto nla_put_failure; | |
792608e9 SK |
133 | return 0; |
134 | ||
135 | nla_put_failure: | |
136 | return -EMSGSIZE; | |
137 | } | |
3acc8473 HX |
138 | #else |
139 | static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg) | |
140 | { | |
141 | return -ENOSYS; | |
142 | } | |
143 | #endif | |
792608e9 | 144 | |
17f0f4a4 NH |
145 | static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg) |
146 | __attribute__ ((unused)); | |
147 | static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg) | |
148 | { | |
149 | seq_printf(m, "type : rng\n"); | |
acec27ff | 150 | seq_printf(m, "seedsize : %u\n", seedsize(alg)); |
17f0f4a4 NH |
151 | } |
152 | ||
17f0f4a4 | 153 | const struct crypto_type crypto_rng_type = { |
d0e83059 HX |
154 | .extsize = crypto_alg_extsize, |
155 | .init_tfm = crypto_rng_init_tfm, | |
17f0f4a4 NH |
156 | #ifdef CONFIG_PROC_FS |
157 | .show = crypto_rng_show, | |
158 | #endif | |
792608e9 | 159 | .report = crypto_rng_report, |
d0e83059 HX |
160 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
161 | .maskset = CRYPTO_ALG_TYPE_MASK, | |
162 | .type = CRYPTO_ALG_TYPE_RNG, | |
163 | .tfmsize = offsetof(struct crypto_rng, base), | |
17f0f4a4 NH |
164 | }; |
165 | EXPORT_SYMBOL_GPL(crypto_rng_type); | |
166 | ||
d0e83059 HX |
167 | struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask) |
168 | { | |
169 | return crypto_alloc_tfm(alg_name, &crypto_rng_type, type, mask); | |
170 | } | |
171 | EXPORT_SYMBOL_GPL(crypto_alloc_rng); | |
172 | ||
17f0f4a4 NH |
173 | int crypto_get_default_rng(void) |
174 | { | |
175 | struct crypto_rng *rng; | |
176 | int err; | |
177 | ||
178 | mutex_lock(&crypto_default_rng_lock); | |
179 | if (!crypto_default_rng) { | |
180 | rng = crypto_alloc_rng("stdrng", 0, 0); | |
181 | err = PTR_ERR(rng); | |
182 | if (IS_ERR(rng)) | |
183 | goto unlock; | |
184 | ||
185 | err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng)); | |
186 | if (err) { | |
187 | crypto_free_rng(rng); | |
188 | goto unlock; | |
189 | } | |
190 | ||
191 | crypto_default_rng = rng; | |
192 | } | |
193 | ||
194 | crypto_default_rng_refcnt++; | |
195 | err = 0; | |
196 | ||
197 | unlock: | |
198 | mutex_unlock(&crypto_default_rng_lock); | |
199 | ||
200 | return err; | |
201 | } | |
202 | EXPORT_SYMBOL_GPL(crypto_get_default_rng); | |
203 | ||
204 | void crypto_put_default_rng(void) | |
205 | { | |
206 | mutex_lock(&crypto_default_rng_lock); | |
207 | if (!--crypto_default_rng_refcnt) { | |
208 | crypto_free_rng(crypto_default_rng); | |
209 | crypto_default_rng = NULL; | |
210 | } | |
211 | mutex_unlock(&crypto_default_rng_lock); | |
212 | } | |
213 | EXPORT_SYMBOL_GPL(crypto_put_default_rng); | |
214 | ||
acec27ff HX |
215 | int crypto_register_rng(struct rng_alg *alg) |
216 | { | |
217 | struct crypto_alg *base = &alg->base; | |
218 | ||
219 | if (alg->seedsize > PAGE_SIZE / 8) | |
220 | return -EINVAL; | |
221 | ||
222 | base->cra_type = &crypto_rng_type; | |
223 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; | |
224 | base->cra_flags |= CRYPTO_ALG_TYPE_RNG; | |
225 | ||
226 | return crypto_register_alg(base); | |
227 | } | |
228 | EXPORT_SYMBOL_GPL(crypto_register_rng); | |
229 | ||
230 | void crypto_unregister_rng(struct rng_alg *alg) | |
231 | { | |
232 | crypto_unregister_alg(&alg->base); | |
233 | } | |
234 | EXPORT_SYMBOL_GPL(crypto_unregister_rng); | |
235 | ||
881cd6c5 HX |
236 | int crypto_register_rngs(struct rng_alg *algs, int count) |
237 | { | |
238 | int i, ret; | |
239 | ||
240 | for (i = 0; i < count; i++) { | |
241 | ret = crypto_register_rng(algs + i); | |
242 | if (ret) | |
243 | goto err; | |
244 | } | |
245 | ||
246 | return 0; | |
247 | ||
248 | err: | |
249 | for (--i; i >= 0; --i) | |
250 | crypto_unregister_rng(algs + i); | |
251 | ||
252 | return ret; | |
253 | } | |
254 | EXPORT_SYMBOL_GPL(crypto_register_rngs); | |
255 | ||
256 | void crypto_unregister_rngs(struct rng_alg *algs, int count) | |
257 | { | |
258 | int i; | |
259 | ||
260 | for (i = count - 1; i >= 0; --i) | |
261 | crypto_unregister_rng(algs + i); | |
262 | } | |
263 | EXPORT_SYMBOL_GPL(crypto_unregister_rngs); | |
264 | ||
17f0f4a4 | 265 | MODULE_LICENSE("GPL"); |
a8ccc393 | 266 | MODULE_DESCRIPTION("Random Number Generator"); |