diff options
author | Herbert Xu <herbert@gondor.apana.org.au> | 2008-08-14 08:21:31 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2008-08-29 01:50:06 -0400 |
commit | a0f000ec9b61b99111757df138b11144236fc59b (patch) | |
tree | bd698163c48b5cd6d6a3c05c8d34d862ebcd86ed /crypto/chainiv.c | |
parent | 17f0f4a47df9aea9ee26c939f8057c35e0be1847 (diff) |
crypto: skcipher - Use RNG interface instead of get_random_bytes
This patch makes the IV generators use the new RNG interface so
that the user can pick an RNG other than the default get_random_bytes.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/chainiv.c')
-rw-r--r-- | crypto/chainiv.c | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/crypto/chainiv.c b/crypto/chainiv.c index cf68d4365347..7c37a497b860 100644 --- a/crypto/chainiv.c +++ b/crypto/chainiv.c | |||
@@ -14,11 +14,11 @@ | |||
14 | */ | 14 | */ |
15 | 15 | ||
16 | #include <crypto/internal/skcipher.h> | 16 | #include <crypto/internal/skcipher.h> |
17 | #include <crypto/rng.h> | ||
17 | #include <linux/err.h> | 18 | #include <linux/err.h> |
18 | #include <linux/init.h> | 19 | #include <linux/init.h> |
19 | #include <linux/kernel.h> | 20 | #include <linux/kernel.h> |
20 | #include <linux/module.h> | 21 | #include <linux/module.h> |
21 | #include <linux/random.h> | ||
22 | #include <linux/spinlock.h> | 22 | #include <linux/spinlock.h> |
23 | #include <linux/string.h> | 23 | #include <linux/string.h> |
24 | #include <linux/workqueue.h> | 24 | #include <linux/workqueue.h> |
@@ -83,6 +83,7 @@ static int chainiv_givencrypt_first(struct skcipher_givcrypt_request *req) | |||
83 | { | 83 | { |
84 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); | 84 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
85 | struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); | 85 | struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
86 | int err = 0; | ||
86 | 87 | ||
87 | spin_lock_bh(&ctx->lock); | 88 | spin_lock_bh(&ctx->lock); |
88 | if (crypto_ablkcipher_crt(geniv)->givencrypt != | 89 | if (crypto_ablkcipher_crt(geniv)->givencrypt != |
@@ -90,11 +91,15 @@ static int chainiv_givencrypt_first(struct skcipher_givcrypt_request *req) | |||
90 | goto unlock; | 91 | goto unlock; |
91 | 92 | ||
92 | crypto_ablkcipher_crt(geniv)->givencrypt = chainiv_givencrypt; | 93 | crypto_ablkcipher_crt(geniv)->givencrypt = chainiv_givencrypt; |
93 | get_random_bytes(ctx->iv, crypto_ablkcipher_ivsize(geniv)); | 94 | err = crypto_rng_get_bytes(crypto_default_rng, ctx->iv, |
95 | crypto_ablkcipher_ivsize(geniv)); | ||
94 | 96 | ||
95 | unlock: | 97 | unlock: |
96 | spin_unlock_bh(&ctx->lock); | 98 | spin_unlock_bh(&ctx->lock); |
97 | 99 | ||
100 | if (err) | ||
101 | return err; | ||
102 | |||
98 | return chainiv_givencrypt(req); | 103 | return chainiv_givencrypt(req); |
99 | } | 104 | } |
100 | 105 | ||
@@ -203,6 +208,7 @@ static int async_chainiv_givencrypt_first(struct skcipher_givcrypt_request *req) | |||
203 | { | 208 | { |
204 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); | 209 | struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); |
205 | struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); | 210 | struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); |
211 | int err = 0; | ||
206 | 212 | ||
207 | if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state)) | 213 | if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state)) |
208 | goto out; | 214 | goto out; |
@@ -212,11 +218,15 @@ static int async_chainiv_givencrypt_first(struct skcipher_givcrypt_request *req) | |||
212 | goto unlock; | 218 | goto unlock; |
213 | 219 | ||
214 | crypto_ablkcipher_crt(geniv)->givencrypt = async_chainiv_givencrypt; | 220 | crypto_ablkcipher_crt(geniv)->givencrypt = async_chainiv_givencrypt; |
215 | get_random_bytes(ctx->iv, crypto_ablkcipher_ivsize(geniv)); | 221 | err = crypto_rng_get_bytes(crypto_default_rng, ctx->iv, |
222 | crypto_ablkcipher_ivsize(geniv)); | ||
216 | 223 | ||
217 | unlock: | 224 | unlock: |
218 | clear_bit(CHAINIV_STATE_INUSE, &ctx->state); | 225 | clear_bit(CHAINIV_STATE_INUSE, &ctx->state); |
219 | 226 | ||
227 | if (err) | ||
228 | return err; | ||
229 | |||
220 | out: | 230 | out: |
221 | return async_chainiv_givencrypt(req); | 231 | return async_chainiv_givencrypt(req); |
222 | } | 232 | } |
@@ -284,9 +294,13 @@ static struct crypto_instance *chainiv_alloc(struct rtattr **tb) | |||
284 | if (IS_ERR(algt)) | 294 | if (IS_ERR(algt)) |
285 | return ERR_PTR(err); | 295 | return ERR_PTR(err); |
286 | 296 | ||
297 | err = crypto_get_default_rng(); | ||
298 | if (err) | ||
299 | return ERR_PTR(err); | ||
300 | |||
287 | inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0, 0); | 301 | inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0, 0); |
288 | if (IS_ERR(inst)) | 302 | if (IS_ERR(inst)) |
289 | goto out; | 303 | goto put_rng; |
290 | 304 | ||
291 | inst->alg.cra_ablkcipher.givencrypt = chainiv_givencrypt_first; | 305 | inst->alg.cra_ablkcipher.givencrypt = chainiv_givencrypt_first; |
292 | 306 | ||
@@ -311,12 +325,22 @@ static struct crypto_instance *chainiv_alloc(struct rtattr **tb) | |||
311 | 325 | ||
312 | out: | 326 | out: |
313 | return inst; | 327 | return inst; |
328 | |||
329 | put_rng: | ||
330 | crypto_put_default_rng(); | ||
331 | goto out; | ||
332 | } | ||
333 | |||
334 | static void chainiv_free(struct crypto_instance *inst) | ||
335 | { | ||
336 | skcipher_geniv_free(inst); | ||
337 | crypto_put_default_rng(); | ||
314 | } | 338 | } |
315 | 339 | ||
316 | static struct crypto_template chainiv_tmpl = { | 340 | static struct crypto_template chainiv_tmpl = { |
317 | .name = "chainiv", | 341 | .name = "chainiv", |
318 | .alloc = chainiv_alloc, | 342 | .alloc = chainiv_alloc, |
319 | .free = skcipher_geniv_free, | 343 | .free = chainiv_free, |
320 | .module = THIS_MODULE, | 344 | .module = THIS_MODULE, |
321 | }; | 345 | }; |
322 | 346 | ||