crypto: rng - Convert low-level crypto_rng to new style
[deliverable/linux.git] / crypto / rng.c
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
15 #include <linux/atomic.h>
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>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24 #include <linux/cryptouser.h>
25 #include <net/netlink.h>
26
27 #include "internal.h"
28
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
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
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
44 static int generate(struct crypto_rng *tfm, const u8 *src, unsigned int slen,
45 u8 *dst, unsigned int dlen)
46 {
47 return crypto_old_rng_alg(tfm)->rng_make_random(tfm, dst, dlen);
48 }
49
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
66 err = crypto_old_rng_alg(tfm)->rng_reset(tfm, src, slen);
67
68 kzfree(buf);
69 return err;
70 }
71
72 int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed, unsigned int slen)
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
86 err = tfm->seed(tfm, seed, slen);
87
88 kfree(buf);
89 return err;
90 }
91 EXPORT_SYMBOL_GPL(crypto_rng_reset);
92
93 static int crypto_rng_init_tfm(struct crypto_tfm *tfm)
94 {
95 struct crypto_rng *rng = __crypto_rng_cast(tfm);
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 }
105
106 rng->generate = alg->generate;
107 rng->seed = alg->seed;
108 rng->seedsize = alg->seedsize;
109
110 return 0;
111 }
112
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
121 #ifdef CONFIG_NET
122 static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
123 {
124 struct crypto_report_rng rrng;
125
126 strncpy(rrng.type, "rng", sizeof(rrng.type));
127
128 rrng.seedsize = seedsize(alg);
129
130 if (nla_put(skb, CRYPTOCFGA_REPORT_RNG,
131 sizeof(struct crypto_report_rng), &rrng))
132 goto nla_put_failure;
133 return 0;
134
135 nla_put_failure:
136 return -EMSGSIZE;
137 }
138 #else
139 static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
140 {
141 return -ENOSYS;
142 }
143 #endif
144
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");
150 seq_printf(m, "seedsize : %u\n", seedsize(alg));
151 }
152
153 const struct crypto_type crypto_rng_type = {
154 .extsize = crypto_alg_extsize,
155 .init_tfm = crypto_rng_init_tfm,
156 #ifdef CONFIG_PROC_FS
157 .show = crypto_rng_show,
158 #endif
159 .report = crypto_rng_report,
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),
164 };
165 EXPORT_SYMBOL_GPL(crypto_rng_type);
166
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
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
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
236 MODULE_LICENSE("GPL");
237 MODULE_DESCRIPTION("Random Number Generator");
This page took 0.036402 seconds and 6 git commands to generate.