Skip to content

Commit a0f000e

Browse files
committed
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 <[email protected]>
1 parent 17f0f4a commit a0f000e

File tree

4 files changed

+75
-13
lines changed

4 files changed

+75
-13
lines changed

crypto/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ config CRYPTO_AEAD
4141
config CRYPTO_BLKCIPHER
4242
tristate
4343
select CRYPTO_ALGAPI
44+
select CRYPTO_RNG
4445

4546
config CRYPTO_HASH
4647
tristate
@@ -125,6 +126,7 @@ config CRYPTO_SEQIV
125126
tristate "Sequence Number IV Generator"
126127
select CRYPTO_AEAD
127128
select CRYPTO_BLKCIPHER
129+
select CRYPTO_RNG
128130
help
129131
This IV generator generates an IV based on a sequence number by
130132
xoring it with a salt. This algorithm is mainly useful for CTR

crypto/chainiv.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
*/
1515

1616
#include <crypto/internal/skcipher.h>
17+
#include <crypto/rng.h>
1718
#include <linux/err.h>
1819
#include <linux/init.h>
1920
#include <linux/kernel.h>
2021
#include <linux/module.h>
21-
#include <linux/random.h>
2222
#include <linux/spinlock.h>
2323
#include <linux/string.h>
2424
#include <linux/workqueue.h>
@@ -83,18 +83,23 @@ static int chainiv_givencrypt_first(struct skcipher_givcrypt_request *req)
8383
{
8484
struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
8585
struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
86+
int err = 0;
8687

8788
spin_lock_bh(&ctx->lock);
8889
if (crypto_ablkcipher_crt(geniv)->givencrypt !=
8990
chainiv_givencrypt_first)
9091
goto unlock;
9192

9293
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));
9496

9597
unlock:
9698
spin_unlock_bh(&ctx->lock);
9799

100+
if (err)
101+
return err;
102+
98103
return chainiv_givencrypt(req);
99104
}
100105

@@ -203,6 +208,7 @@ static int async_chainiv_givencrypt_first(struct skcipher_givcrypt_request *req)
203208
{
204209
struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
205210
struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
211+
int err = 0;
206212

207213
if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
208214
goto out;
@@ -212,11 +218,15 @@ static int async_chainiv_givencrypt_first(struct skcipher_givcrypt_request *req)
212218
goto unlock;
213219

214220
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));
216223

217224
unlock:
218225
clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
219226

227+
if (err)
228+
return err;
229+
220230
out:
221231
return async_chainiv_givencrypt(req);
222232
}
@@ -284,9 +294,13 @@ static struct crypto_instance *chainiv_alloc(struct rtattr **tb)
284294
if (IS_ERR(algt))
285295
return ERR_PTR(err);
286296

297+
err = crypto_get_default_rng();
298+
if (err)
299+
return ERR_PTR(err);
300+
287301
inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0, 0);
288302
if (IS_ERR(inst))
289-
goto out;
303+
goto put_rng;
290304

291305
inst->alg.cra_ablkcipher.givencrypt = chainiv_givencrypt_first;
292306

@@ -311,12 +325,22 @@ static struct crypto_instance *chainiv_alloc(struct rtattr **tb)
311325

312326
out:
313327
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();
314338
}
315339

316340
static struct crypto_template chainiv_tmpl = {
317341
.name = "chainiv",
318342
.alloc = chainiv_alloc,
319-
.free = skcipher_geniv_free,
343+
.free = chainiv_free,
320344
.module = THIS_MODULE,
321345
};
322346

crypto/eseqiv.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
*/
1717

1818
#include <crypto/internal/skcipher.h>
19+
#include <crypto/rng.h>
1920
#include <crypto/scatterwalk.h>
2021
#include <linux/err.h>
2122
#include <linux/init.h>
2223
#include <linux/kernel.h>
2324
#include <linux/mm.h>
2425
#include <linux/module.h>
25-
#include <linux/random.h>
2626
#include <linux/scatterlist.h>
2727
#include <linux/spinlock.h>
2828
#include <linux/string.h>
@@ -163,17 +163,22 @@ static int eseqiv_givencrypt_first(struct skcipher_givcrypt_request *req)
163163
{
164164
struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
165165
struct eseqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
166+
int err = 0;
166167

167168
spin_lock_bh(&ctx->lock);
168169
if (crypto_ablkcipher_crt(geniv)->givencrypt != eseqiv_givencrypt_first)
169170
goto unlock;
170171

171172
crypto_ablkcipher_crt(geniv)->givencrypt = eseqiv_givencrypt;
172-
get_random_bytes(ctx->salt, crypto_ablkcipher_ivsize(geniv));
173+
err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
174+
crypto_ablkcipher_ivsize(geniv));
173175

174176
unlock:
175177
spin_unlock_bh(&ctx->lock);
176178

179+
if (err)
180+
return err;
181+
177182
return eseqiv_givencrypt(req);
178183
}
179184

@@ -216,9 +221,13 @@ static struct crypto_instance *eseqiv_alloc(struct rtattr **tb)
216221
struct crypto_instance *inst;
217222
int err;
218223

224+
err = crypto_get_default_rng();
225+
if (err)
226+
return ERR_PTR(err);
227+
219228
inst = skcipher_geniv_alloc(&eseqiv_tmpl, tb, 0, 0);
220229
if (IS_ERR(inst))
221-
goto out;
230+
goto put_rng;
222231

223232
err = -EINVAL;
224233
if (inst->alg.cra_ablkcipher.ivsize != inst->alg.cra_blocksize)
@@ -238,13 +247,21 @@ static struct crypto_instance *eseqiv_alloc(struct rtattr **tb)
238247
free_inst:
239248
skcipher_geniv_free(inst);
240249
inst = ERR_PTR(err);
250+
put_rng:
251+
crypto_put_default_rng();
241252
goto out;
242253
}
243254

255+
static void eseqiv_free(struct crypto_instance *inst)
256+
{
257+
skcipher_geniv_free(inst);
258+
crypto_put_default_rng();
259+
}
260+
244261
static struct crypto_template eseqiv_tmpl = {
245262
.name = "eseqiv",
246263
.alloc = eseqiv_alloc,
247-
.free = skcipher_geniv_free,
264+
.free = eseqiv_free,
248265
.module = THIS_MODULE,
249266
};
250267

crypto/seqiv.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
#include <crypto/internal/aead.h>
1717
#include <crypto/internal/skcipher.h>
18+
#include <crypto/rng.h>
1819
#include <linux/err.h>
1920
#include <linux/init.h>
2021
#include <linux/kernel.h>
2122
#include <linux/module.h>
22-
#include <linux/random.h>
2323
#include <linux/spinlock.h>
2424
#include <linux/string.h>
2525

@@ -189,35 +189,45 @@ static int seqiv_givencrypt_first(struct skcipher_givcrypt_request *req)
189189
{
190190
struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
191191
struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
192+
int err = 0;
192193

193194
spin_lock_bh(&ctx->lock);
194195
if (crypto_ablkcipher_crt(geniv)->givencrypt != seqiv_givencrypt_first)
195196
goto unlock;
196197

197198
crypto_ablkcipher_crt(geniv)->givencrypt = seqiv_givencrypt;
198-
get_random_bytes(ctx->salt, crypto_ablkcipher_ivsize(geniv));
199+
err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
200+
crypto_ablkcipher_ivsize(geniv));
199201

200202
unlock:
201203
spin_unlock_bh(&ctx->lock);
202204

205+
if (err)
206+
return err;
207+
203208
return seqiv_givencrypt(req);
204209
}
205210

206211
static int seqiv_aead_givencrypt_first(struct aead_givcrypt_request *req)
207212
{
208213
struct crypto_aead *geniv = aead_givcrypt_reqtfm(req);
209214
struct seqiv_ctx *ctx = crypto_aead_ctx(geniv);
215+
int err = 0;
210216

211217
spin_lock_bh(&ctx->lock);
212218
if (crypto_aead_crt(geniv)->givencrypt != seqiv_aead_givencrypt_first)
213219
goto unlock;
214220

215221
crypto_aead_crt(geniv)->givencrypt = seqiv_aead_givencrypt;
216-
get_random_bytes(ctx->salt, crypto_aead_ivsize(geniv));
222+
err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
223+
crypto_aead_ivsize(geniv));
217224

218225
unlock:
219226
spin_unlock_bh(&ctx->lock);
220227

228+
if (err)
229+
return err;
230+
221231
return seqiv_aead_givencrypt(req);
222232
}
223233

@@ -298,19 +308,27 @@ static struct crypto_instance *seqiv_alloc(struct rtattr **tb)
298308
if (IS_ERR(algt))
299309
return ERR_PTR(err);
300310

311+
err = crypto_get_default_rng();
312+
if (err)
313+
return ERR_PTR(err);
314+
301315
if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
302316
inst = seqiv_ablkcipher_alloc(tb);
303317
else
304318
inst = seqiv_aead_alloc(tb);
305319

306320
if (IS_ERR(inst))
307-
goto out;
321+
goto put_rng;
308322

309323
inst->alg.cra_alignmask |= __alignof__(u32) - 1;
310324
inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx);
311325

312326
out:
313327
return inst;
328+
329+
put_rng:
330+
crypto_put_default_rng();
331+
goto out;
314332
}
315333

316334
static void seqiv_free(struct crypto_instance *inst)
@@ -319,6 +337,7 @@ static void seqiv_free(struct crypto_instance *inst)
319337
skcipher_geniv_free(inst);
320338
else
321339
aead_geniv_free(inst);
340+
crypto_put_default_rng();
322341
}
323342

324343
static struct crypto_template seqiv_tmpl = {

0 commit comments

Comments
 (0)