aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/seqiv.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/seqiv.c')
-rw-r--r--crypto/seqiv.c27
1 files changed, 23 insertions, 4 deletions
diff --git a/crypto/seqiv.c b/crypto/seqiv.c
index b903aab3157..5a013a8bf87 100644
--- a/crypto/seqiv.c
+++ b/crypto/seqiv.c
@@ -15,11 +15,11 @@
15 15
16#include <crypto/internal/aead.h> 16#include <crypto/internal/aead.h>
17#include <crypto/internal/skcipher.h> 17#include <crypto/internal/skcipher.h>
18#include <crypto/rng.h>
18#include <linux/err.h> 19#include <linux/err.h>
19#include <linux/init.h> 20#include <linux/init.h>
20#include <linux/kernel.h> 21#include <linux/kernel.h>
21#include <linux/module.h> 22#include <linux/module.h>
22#include <linux/random.h>
23#include <linux/spinlock.h> 23#include <linux/spinlock.h>
24#include <linux/string.h> 24#include <linux/string.h>
25 25
@@ -189,17 +189,22 @@ static int seqiv_givencrypt_first(struct skcipher_givcrypt_request *req)
189{ 189{
190 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req); 190 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
191 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv); 191 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
192 int err = 0;
192 193
193 spin_lock_bh(&ctx->lock); 194 spin_lock_bh(&ctx->lock);
194 if (crypto_ablkcipher_crt(geniv)->givencrypt != seqiv_givencrypt_first) 195 if (crypto_ablkcipher_crt(geniv)->givencrypt != seqiv_givencrypt_first)
195 goto unlock; 196 goto unlock;
196 197
197 crypto_ablkcipher_crt(geniv)->givencrypt = seqiv_givencrypt; 198 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));
199 201
200unlock: 202unlock:
201 spin_unlock_bh(&ctx->lock); 203 spin_unlock_bh(&ctx->lock);
202 204
205 if (err)
206 return err;
207
203 return seqiv_givencrypt(req); 208 return seqiv_givencrypt(req);
204} 209}
205 210
@@ -207,17 +212,22 @@ static int seqiv_aead_givencrypt_first(struct aead_givcrypt_request *req)
207{ 212{
208 struct crypto_aead *geniv = aead_givcrypt_reqtfm(req); 213 struct crypto_aead *geniv = aead_givcrypt_reqtfm(req);
209 struct seqiv_ctx *ctx = crypto_aead_ctx(geniv); 214 struct seqiv_ctx *ctx = crypto_aead_ctx(geniv);
215 int err = 0;
210 216
211 spin_lock_bh(&ctx->lock); 217 spin_lock_bh(&ctx->lock);
212 if (crypto_aead_crt(geniv)->givencrypt != seqiv_aead_givencrypt_first) 218 if (crypto_aead_crt(geniv)->givencrypt != seqiv_aead_givencrypt_first)
213 goto unlock; 219 goto unlock;
214 220
215 crypto_aead_crt(geniv)->givencrypt = seqiv_aead_givencrypt; 221 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));
217 224
218unlock: 225unlock:
219 spin_unlock_bh(&ctx->lock); 226 spin_unlock_bh(&ctx->lock);
220 227
228 if (err)
229 return err;
230
221 return seqiv_aead_givencrypt(req); 231 return seqiv_aead_givencrypt(req);
222} 232}
223 233
@@ -298,19 +308,27 @@ static struct crypto_instance *seqiv_alloc(struct rtattr **tb)
298 if (IS_ERR(algt)) 308 if (IS_ERR(algt))
299 return ERR_PTR(err); 309 return ERR_PTR(err);
300 310
311 err = crypto_get_default_rng();
312 if (err)
313 return ERR_PTR(err);
314
301 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK) 315 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
302 inst = seqiv_ablkcipher_alloc(tb); 316 inst = seqiv_ablkcipher_alloc(tb);
303 else 317 else
304 inst = seqiv_aead_alloc(tb); 318 inst = seqiv_aead_alloc(tb);
305 319
306 if (IS_ERR(inst)) 320 if (IS_ERR(inst))
307 goto out; 321 goto put_rng;
308 322
309 inst->alg.cra_alignmask |= __alignof__(u32) - 1; 323 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
310 inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx); 324 inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx);
311 325
312out: 326out:
313 return inst; 327 return inst;
328
329put_rng:
330 crypto_put_default_rng();
331 goto out;
314} 332}
315 333
316static void seqiv_free(struct crypto_instance *inst) 334static void seqiv_free(struct crypto_instance *inst)
@@ -319,6 +337,7 @@ static void seqiv_free(struct crypto_instance *inst)
319 skcipher_geniv_free(inst); 337 skcipher_geniv_free(inst);
320 else 338 else
321 aead_geniv_free(inst); 339 aead_geniv_free(inst);
340 crypto_put_default_rng();
322} 341}
323 342
324static struct crypto_template seqiv_tmpl = { 343static struct crypto_template seqiv_tmpl = {