diff options
author | Herbert Xu <herbert@gondor.apana.org.au> | 2006-09-20 21:39:29 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2006-09-20 21:39:29 -0400 |
commit | 6bfd48096ff8ecabf955958b51ddfa7988eb0a14 (patch) | |
tree | 813799f00d8402348ba6817953b1c631541be66c /crypto/api.c | |
parent | 492e2b63eb10c28f4f0b694264d74a8755cd1be0 (diff) |
[CRYPTO] api: Added spawns
Spawns lock a specific crypto algorithm in place. They can then be used
with crypto_spawn_tfm to allocate a tfm for that algorithm. When the base
algorithm of a spawn is deregistered, all its spawns will be automatically
removed.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'crypto/api.c')
-rw-r--r-- | crypto/api.c | 95 |
1 files changed, 65 insertions, 30 deletions
diff --git a/crypto/api.c b/crypto/api.c index ddf6a767acdd..7e5522cf856e 100644 --- a/crypto/api.c +++ b/crypto/api.c | |||
@@ -15,11 +15,13 @@ | |||
15 | * | 15 | * |
16 | */ | 16 | */ |
17 | 17 | ||
18 | #include <linux/err.h> | ||
18 | #include <linux/errno.h> | 19 | #include <linux/errno.h> |
19 | #include <linux/kernel.h> | 20 | #include <linux/kernel.h> |
20 | #include <linux/kmod.h> | 21 | #include <linux/kmod.h> |
21 | #include <linux/module.h> | 22 | #include <linux/module.h> |
22 | #include <linux/param.h> | 23 | #include <linux/param.h> |
24 | #include <linux/sched.h> | ||
23 | #include <linux/slab.h> | 25 | #include <linux/slab.h> |
24 | #include <linux/string.h> | 26 | #include <linux/string.h> |
25 | #include "internal.h" | 27 | #include "internal.h" |
@@ -38,12 +40,6 @@ static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg) | |||
38 | return alg; | 40 | return alg; |
39 | } | 41 | } |
40 | 42 | ||
41 | static inline void crypto_alg_put(struct crypto_alg *alg) | ||
42 | { | ||
43 | if (atomic_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy) | ||
44 | alg->cra_destroy(alg); | ||
45 | } | ||
46 | |||
47 | struct crypto_alg *crypto_mod_get(struct crypto_alg *alg) | 43 | struct crypto_alg *crypto_mod_get(struct crypto_alg *alg) |
48 | { | 44 | { |
49 | return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL; | 45 | return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL; |
@@ -65,6 +61,9 @@ struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type, u32 mask) | |||
65 | list_for_each_entry(q, &crypto_alg_list, cra_list) { | 61 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
66 | int exact, fuzzy; | 62 | int exact, fuzzy; |
67 | 63 | ||
64 | if (crypto_is_moribund(q)) | ||
65 | continue; | ||
66 | |||
68 | if ((q->cra_flags ^ type) & mask) | 67 | if ((q->cra_flags ^ type) & mask) |
69 | continue; | 68 | continue; |
70 | 69 | ||
@@ -111,7 +110,7 @@ static struct crypto_alg *crypto_larval_alloc(const char *name, u32 type, | |||
111 | 110 | ||
112 | larval = kzalloc(sizeof(*larval), GFP_KERNEL); | 111 | larval = kzalloc(sizeof(*larval), GFP_KERNEL); |
113 | if (!larval) | 112 | if (!larval) |
114 | return NULL; | 113 | return ERR_PTR(-ENOMEM); |
115 | 114 | ||
116 | larval->mask = mask; | 115 | larval->mask = mask; |
117 | larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type; | 116 | larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type; |
@@ -153,8 +152,11 @@ static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg) | |||
153 | 152 | ||
154 | wait_for_completion_interruptible_timeout(&larval->completion, 60 * HZ); | 153 | wait_for_completion_interruptible_timeout(&larval->completion, 60 * HZ); |
155 | alg = larval->adult; | 154 | alg = larval->adult; |
156 | if (alg && !crypto_mod_get(alg)) | 155 | if (alg) { |
157 | alg = NULL; | 156 | if (!crypto_mod_get(alg)) |
157 | alg = ERR_PTR(-EAGAIN); | ||
158 | } else | ||
159 | alg = ERR_PTR(-ENOENT); | ||
158 | crypto_mod_put(&larval->alg); | 160 | crypto_mod_put(&larval->alg); |
159 | 161 | ||
160 | return alg; | 162 | return alg; |
@@ -165,9 +167,6 @@ static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type, | |||
165 | { | 167 | { |
166 | struct crypto_alg *alg; | 168 | struct crypto_alg *alg; |
167 | 169 | ||
168 | if (!name) | ||
169 | return NULL; | ||
170 | |||
171 | down_read(&crypto_alg_sem); | 170 | down_read(&crypto_alg_sem); |
172 | alg = __crypto_alg_lookup(name, type, mask); | 171 | alg = __crypto_alg_lookup(name, type, mask); |
173 | up_read(&crypto_alg_sem); | 172 | up_read(&crypto_alg_sem); |
@@ -181,7 +180,10 @@ struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask) | |||
181 | struct crypto_alg *larval; | 180 | struct crypto_alg *larval; |
182 | int ok; | 181 | int ok; |
183 | 182 | ||
184 | mask &= ~CRYPTO_ALG_LARVAL; | 183 | if (!name) |
184 | return ERR_PTR(-ENOENT); | ||
185 | |||
186 | mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD); | ||
185 | type &= mask; | 187 | type &= mask; |
186 | 188 | ||
187 | alg = try_then_request_module(crypto_alg_lookup(name, type, mask), | 189 | alg = try_then_request_module(crypto_alg_lookup(name, type, mask), |
@@ -190,7 +192,7 @@ struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask) | |||
190 | return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg; | 192 | return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg; |
191 | 193 | ||
192 | larval = crypto_larval_alloc(name, type, mask); | 194 | larval = crypto_larval_alloc(name, type, mask); |
193 | if (!larval || !crypto_is_larval(larval)) | 195 | if (IS_ERR(larval) || !crypto_is_larval(larval)) |
194 | return larval; | 196 | return larval; |
195 | 197 | ||
196 | ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval); | 198 | ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval); |
@@ -203,7 +205,7 @@ struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask) | |||
203 | alg = crypto_larval_wait(larval); | 205 | alg = crypto_larval_wait(larval); |
204 | else { | 206 | else { |
205 | crypto_mod_put(larval); | 207 | crypto_mod_put(larval); |
206 | alg = NULL; | 208 | alg = ERR_PTR(-ENOENT); |
207 | } | 209 | } |
208 | crypto_larval_kill(larval); | 210 | crypto_larval_kill(larval); |
209 | return alg; | 211 | return alg; |
@@ -298,31 +300,40 @@ static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags) | |||
298 | return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1)); | 300 | return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1)); |
299 | } | 301 | } |
300 | 302 | ||
301 | struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags) | 303 | void crypto_shoot_alg(struct crypto_alg *alg) |
304 | { | ||
305 | down_write(&crypto_alg_sem); | ||
306 | alg->cra_flags |= CRYPTO_ALG_DYING; | ||
307 | up_write(&crypto_alg_sem); | ||
308 | } | ||
309 | EXPORT_SYMBOL_GPL(crypto_shoot_alg); | ||
310 | |||
311 | struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 flags) | ||
302 | { | 312 | { |
303 | struct crypto_tfm *tfm = NULL; | 313 | struct crypto_tfm *tfm = NULL; |
304 | struct crypto_alg *alg; | ||
305 | unsigned int tfm_size; | 314 | unsigned int tfm_size; |
306 | 315 | int err = -ENOMEM; | |
307 | alg = crypto_alg_mod_lookup(name, 0, 0); | ||
308 | if (alg == NULL) | ||
309 | goto out; | ||
310 | 316 | ||
311 | tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags); | 317 | tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags); |
312 | tfm = kzalloc(tfm_size, GFP_KERNEL); | 318 | tfm = kzalloc(tfm_size, GFP_KERNEL); |
313 | if (tfm == NULL) | 319 | if (tfm == NULL) |
314 | goto out_put; | 320 | goto out; |
315 | 321 | ||
316 | tfm->__crt_alg = alg; | 322 | tfm->__crt_alg = alg; |
317 | 323 | ||
318 | if (crypto_init_flags(tfm, flags)) | 324 | err = crypto_init_flags(tfm, flags); |
325 | if (err) | ||
319 | goto out_free_tfm; | 326 | goto out_free_tfm; |
320 | 327 | ||
321 | if (crypto_init_ops(tfm)) | 328 | err = crypto_init_ops(tfm); |
329 | if (err) | ||
322 | goto out_free_tfm; | 330 | goto out_free_tfm; |
323 | 331 | ||
324 | if (alg->cra_init && alg->cra_init(tfm)) | 332 | if (alg->cra_init && (err = alg->cra_init(tfm))) { |
333 | if (err == -EAGAIN) | ||
334 | crypto_shoot_alg(alg); | ||
325 | goto cra_init_failed; | 335 | goto cra_init_failed; |
336 | } | ||
326 | 337 | ||
327 | goto out; | 338 | goto out; |
328 | 339 | ||
@@ -330,12 +341,36 @@ cra_init_failed: | |||
330 | crypto_exit_ops(tfm); | 341 | crypto_exit_ops(tfm); |
331 | out_free_tfm: | 342 | out_free_tfm: |
332 | kfree(tfm); | 343 | kfree(tfm); |
333 | tfm = NULL; | 344 | tfm = ERR_PTR(err); |
334 | out_put: | ||
335 | crypto_mod_put(alg); | ||
336 | out: | 345 | out: |
337 | return tfm; | 346 | return tfm; |
338 | } | 347 | } |
348 | EXPORT_SYMBOL_GPL(__crypto_alloc_tfm); | ||
349 | |||
350 | struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags) | ||
351 | { | ||
352 | struct crypto_tfm *tfm = NULL; | ||
353 | int err; | ||
354 | |||
355 | do { | ||
356 | struct crypto_alg *alg; | ||
357 | |||
358 | alg = crypto_alg_mod_lookup(name, 0, 0); | ||
359 | err = PTR_ERR(alg); | ||
360 | if (IS_ERR(alg)) | ||
361 | continue; | ||
362 | |||
363 | tfm = __crypto_alloc_tfm(alg, flags); | ||
364 | err = 0; | ||
365 | if (IS_ERR(tfm)) { | ||
366 | crypto_mod_put(alg); | ||
367 | err = PTR_ERR(tfm); | ||
368 | tfm = NULL; | ||
369 | } | ||
370 | } while (err == -EAGAIN && !signal_pending(current)); | ||
371 | |||
372 | return tfm; | ||
373 | } | ||
339 | 374 | ||
340 | void crypto_free_tfm(struct crypto_tfm *tfm) | 375 | void crypto_free_tfm(struct crypto_tfm *tfm) |
341 | { | 376 | { |
@@ -361,7 +396,7 @@ int crypto_alg_available(const char *name, u32 flags) | |||
361 | int ret = 0; | 396 | int ret = 0; |
362 | struct crypto_alg *alg = crypto_alg_mod_lookup(name, 0, 0); | 397 | struct crypto_alg *alg = crypto_alg_mod_lookup(name, 0, 0); |
363 | 398 | ||
364 | if (alg) { | 399 | if (!IS_ERR(alg)) { |
365 | crypto_mod_put(alg); | 400 | crypto_mod_put(alg); |
366 | ret = 1; | 401 | ret = 1; |
367 | } | 402 | } |