seqiv.c (c1f3ee120bb61045b1c0a3ead620d1d65af47130) seqiv.c (a0f000ec9b61b99111757df138b11144236fc59b)
1/*
2 * seqiv: Sequence Number IV Generator
3 *
4 * This generator generates an IV based on a sequence number by xoring it
5 * with a salt. This algorithm is mainly useful for CTR and similar modes.
6 *
7 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 */
15
16#include <crypto/internal/aead.h>
17#include <crypto/internal/skcipher.h>
1/*
2 * seqiv: Sequence Number IV Generator
3 *
4 * This generator generates an IV based on a sequence number by xoring it
5 * with a salt. This algorithm is mainly useful for CTR and similar modes.
6 *
7 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 */
15
16#include <crypto/internal/aead.h>
17#include <crypto/internal/skcipher.h>
18#include <crypto/rng.h>
18#include <linux/err.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
22#include <linux/random.h>
23#include <linux/spinlock.h>
24#include <linux/string.h>
25
26struct seqiv_ctx {
27 spinlock_t lock;
28 u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
29};
30

--- 153 unchanged lines hidden (view full) ---

184 seqiv_aead_complete2(req, err);
185 return err;
186}
187
188static int seqiv_givencrypt_first(struct skcipher_givcrypt_request *req)
189{
190 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
191 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
23#include <linux/spinlock.h>
24#include <linux/string.h>
25
26struct seqiv_ctx {
27 spinlock_t lock;
28 u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
29};
30

--- 153 unchanged lines hidden (view full) ---

184 seqiv_aead_complete2(req, err);
185 return err;
186}
187
188static int seqiv_givencrypt_first(struct skcipher_givcrypt_request *req)
189{
190 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
191 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
192 int err = 0;
192
193 spin_lock_bh(&ctx->lock);
194 if (crypto_ablkcipher_crt(geniv)->givencrypt != seqiv_givencrypt_first)
195 goto unlock;
196
197 crypto_ablkcipher_crt(geniv)->givencrypt = seqiv_givencrypt;
193
194 spin_lock_bh(&ctx->lock);
195 if (crypto_ablkcipher_crt(geniv)->givencrypt != seqiv_givencrypt_first)
196 goto unlock;
197
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
200unlock:
201 spin_unlock_bh(&ctx->lock);
202
201
202unlock:
203 spin_unlock_bh(&ctx->lock);
204
205 if (err)
206 return err;
207
203 return seqiv_givencrypt(req);
204}
205
206static int seqiv_aead_givencrypt_first(struct aead_givcrypt_request *req)
207{
208 struct crypto_aead *geniv = aead_givcrypt_reqtfm(req);
209 struct seqiv_ctx *ctx = crypto_aead_ctx(geniv);
208 return seqiv_givencrypt(req);
209}
210
211static int seqiv_aead_givencrypt_first(struct aead_givcrypt_request *req)
212{
213 struct crypto_aead *geniv = aead_givcrypt_reqtfm(req);
214 struct seqiv_ctx *ctx = crypto_aead_ctx(geniv);
215 int err = 0;
210
211 spin_lock_bh(&ctx->lock);
212 if (crypto_aead_crt(geniv)->givencrypt != seqiv_aead_givencrypt_first)
213 goto unlock;
214
215 crypto_aead_crt(geniv)->givencrypt = seqiv_aead_givencrypt;
216
217 spin_lock_bh(&ctx->lock);
218 if (crypto_aead_crt(geniv)->givencrypt != seqiv_aead_givencrypt_first)
219 goto unlock;
220
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
218unlock:
219 spin_unlock_bh(&ctx->lock);
220
224
225unlock:
226 spin_unlock_bh(&ctx->lock);
227
228 if (err)
229 return err;
230
221 return seqiv_aead_givencrypt(req);
222}
223
224static int seqiv_init(struct crypto_tfm *tfm)
225{
226 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
227 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
228

--- 64 unchanged lines hidden (view full) ---

293 struct crypto_instance *inst;
294 int err;
295
296 algt = crypto_get_attr_type(tb);
297 err = PTR_ERR(algt);
298 if (IS_ERR(algt))
299 return ERR_PTR(err);
300
231 return seqiv_aead_givencrypt(req);
232}
233
234static int seqiv_init(struct crypto_tfm *tfm)
235{
236 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
237 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
238

--- 64 unchanged lines hidden (view full) ---

303 struct crypto_instance *inst;
304 int err;
305
306 algt = crypto_get_attr_type(tb);
307 err = PTR_ERR(algt);
308 if (IS_ERR(algt))
309 return ERR_PTR(err);
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)
302 inst = seqiv_ablkcipher_alloc(tb);
303 else
304 inst = seqiv_aead_alloc(tb);
305
306 if (IS_ERR(inst))
315 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
316 inst = seqiv_ablkcipher_alloc(tb);
317 else
318 inst = seqiv_aead_alloc(tb);
319
320 if (IS_ERR(inst))
307 goto out;
321 goto put_rng;
308
309 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
310 inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx);
311
312out:
313 return inst;
322
323 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
324 inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx);
325
326out:
327 return inst;
328
329put_rng:
330 crypto_put_default_rng();
331 goto out;
314}
315
316static void seqiv_free(struct crypto_instance *inst)
317{
318 if ((inst->alg.cra_flags ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
319 skcipher_geniv_free(inst);
320 else
321 aead_geniv_free(inst);
332}
333
334static void seqiv_free(struct crypto_instance *inst)
335{
336 if ((inst->alg.cra_flags ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
337 skcipher_geniv_free(inst);
338 else
339 aead_geniv_free(inst);
340 crypto_put_default_rng();
322}
323
324static struct crypto_template seqiv_tmpl = {
325 .name = "seqiv",
326 .alloc = seqiv_alloc,
327 .free = seqiv_free,
328 .module = THIS_MODULE,
329};

--- 16 unchanged lines hidden ---
341}
342
343static struct crypto_template seqiv_tmpl = {
344 .name = "seqiv",
345 .alloc = seqiv_alloc,
346 .free = seqiv_free,
347 .module = THIS_MODULE,
348};

--- 16 unchanged lines hidden ---