2 * seqiv: Sequence Number IV Generator
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.
7 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
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)
16 #include <crypto/internal/geniv.h>
17 #include <crypto/internal/skcipher.h>
18 #include <crypto/rng.h>
19 #include <crypto/scatterwalk.h>
20 #include <linux/err.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/string.h>
30 u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
33 static void seqiv_free(struct crypto_instance *inst);
35 static void seqiv_complete2(struct skcipher_givcrypt_request *req, int err)
37 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
38 struct crypto_ablkcipher *geniv;
40 if (err == -EINPROGRESS)
46 geniv = skcipher_givcrypt_reqtfm(req);
47 memcpy(req->creq.info, subreq->info, crypto_ablkcipher_ivsize(geniv));
53 static void seqiv_complete(struct crypto_async_request *base, int err)
55 struct skcipher_givcrypt_request *req = base->data;
57 seqiv_complete2(req, err);
58 skcipher_givcrypt_complete(req, err);
61 static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
63 struct aead_request *subreq = aead_request_ctx(req);
64 struct crypto_aead *geniv;
66 if (err == -EINPROGRESS)
72 geniv = crypto_aead_reqtfm(req);
73 memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
79 static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
82 struct aead_request *req = base->data;
84 seqiv_aead_encrypt_complete2(req, err);
85 aead_request_complete(req, err);
88 static void seqiv_geniv(struct seqiv_ctx *ctx, u8 *info, u64 seq,
91 unsigned int len = ivsize;
93 if (ivsize > sizeof(u64)) {
94 memset(info, 0, ivsize - sizeof(u64));
97 seq = cpu_to_be64(seq);
98 memcpy(info + ivsize - len, &seq, len);
99 crypto_xor(info, ctx->salt, ivsize);
102 static int seqiv_givencrypt(struct skcipher_givcrypt_request *req)
104 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
105 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
106 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
107 crypto_completion_t compl;
113 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
115 compl = req->creq.base.complete;
116 data = req->creq.base.data;
117 info = req->creq.info;
119 ivsize = crypto_ablkcipher_ivsize(geniv);
121 if (unlikely(!IS_ALIGNED((unsigned long)info,
122 crypto_ablkcipher_alignmask(geniv) + 1))) {
123 info = kmalloc(ivsize, req->creq.base.flags &
124 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
129 compl = seqiv_complete;
133 ablkcipher_request_set_callback(subreq, req->creq.base.flags, compl,
135 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
136 req->creq.nbytes, info);
138 seqiv_geniv(ctx, info, req->seq, ivsize);
139 memcpy(req->giv, info, ivsize);
141 err = crypto_ablkcipher_encrypt(subreq);
142 if (unlikely(info != req->creq.info))
143 seqiv_complete2(req, err);
147 static int seqiv_aead_encrypt(struct aead_request *req)
149 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
150 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
151 struct aead_request *subreq = aead_request_ctx(req);
152 crypto_completion_t compl;
155 unsigned int ivsize = 8;
158 if (req->cryptlen < ivsize)
161 aead_request_set_tfm(subreq, ctx->child);
163 compl = req->base.complete;
164 data = req->base.data;
167 if (req->src != req->dst) {
168 struct blkcipher_desc desc = {
172 err = crypto_blkcipher_encrypt(&desc, req->dst, req->src,
173 req->assoclen + req->cryptlen);
178 if (unlikely(!IS_ALIGNED((unsigned long)info,
179 crypto_aead_alignmask(geniv) + 1))) {
180 info = kmalloc(ivsize, req->base.flags &
181 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
186 memcpy(info, req->iv, ivsize);
187 compl = seqiv_aead_encrypt_complete;
191 aead_request_set_callback(subreq, req->base.flags, compl, data);
192 aead_request_set_crypt(subreq, req->dst, req->dst,
193 req->cryptlen - ivsize, info);
194 aead_request_set_ad(subreq, req->assoclen + ivsize);
196 crypto_xor(info, ctx->salt, ivsize);
197 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
199 err = crypto_aead_encrypt(subreq);
200 if (unlikely(info != req->iv))
201 seqiv_aead_encrypt_complete2(req, err);
205 static int seqiv_aead_decrypt(struct aead_request *req)
207 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
208 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
209 struct aead_request *subreq = aead_request_ctx(req);
210 crypto_completion_t compl;
212 unsigned int ivsize = 8;
214 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
217 aead_request_set_tfm(subreq, ctx->child);
219 compl = req->base.complete;
220 data = req->base.data;
222 aead_request_set_callback(subreq, req->base.flags, compl, data);
223 aead_request_set_crypt(subreq, req->src, req->dst,
224 req->cryptlen - ivsize, req->iv);
225 aead_request_set_ad(subreq, req->assoclen + ivsize);
227 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
229 return crypto_aead_decrypt(subreq);
232 static int seqiv_init(struct crypto_tfm *tfm)
234 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
235 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
238 spin_lock_init(&ctx->lock);
240 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
243 if (!crypto_get_default_rng()) {
244 crypto_ablkcipher_crt(geniv)->givencrypt = seqiv_givencrypt;
245 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
246 crypto_ablkcipher_ivsize(geniv));
247 crypto_put_default_rng();
250 return err ?: skcipher_geniv_init(tfm);
253 static int seqiv_ablkcipher_create(struct crypto_template *tmpl,
256 struct crypto_instance *inst;
259 inst = skcipher_geniv_alloc(tmpl, tb, 0, 0);
262 return PTR_ERR(inst);
265 if (inst->alg.cra_ablkcipher.ivsize < sizeof(u64))
268 inst->alg.cra_init = seqiv_init;
269 inst->alg.cra_exit = skcipher_geniv_exit;
271 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
272 inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx);
274 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
276 err = crypto_register_instance(tmpl, inst);
284 skcipher_geniv_free(inst);
288 static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
290 struct aead_instance *inst;
291 struct crypto_aead_spawn *spawn;
292 struct aead_alg *alg;
295 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
298 return PTR_ERR(inst);
300 inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
302 spawn = aead_instance_ctx(inst);
303 alg = crypto_spawn_aead_alg(spawn);
306 if (inst->alg.ivsize != sizeof(u64))
309 inst->alg.encrypt = seqiv_aead_encrypt;
310 inst->alg.decrypt = seqiv_aead_decrypt;
312 inst->alg.init = aead_init_geniv;
313 inst->alg.exit = aead_exit_geniv;
315 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
316 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
318 err = aead_register_instance(tmpl, inst);
326 aead_geniv_free(inst);
330 static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
332 struct crypto_attr_type *algt;
335 algt = crypto_get_attr_type(tb);
337 return PTR_ERR(algt);
339 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
340 err = seqiv_ablkcipher_create(tmpl, tb);
342 err = seqiv_aead_create(tmpl, tb);
347 static void seqiv_free(struct crypto_instance *inst)
349 if ((inst->alg.cra_flags ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
350 skcipher_geniv_free(inst);
352 aead_geniv_free(aead_instance(inst));
355 static struct crypto_template seqiv_tmpl = {
357 .create = seqiv_create,
359 .module = THIS_MODULE,
362 static int __init seqiv_module_init(void)
364 return crypto_register_template(&seqiv_tmpl);
367 static void __exit seqiv_module_exit(void)
369 crypto_unregister_template(&seqiv_tmpl);
372 module_init(seqiv_module_init);
373 module_exit(seqiv_module_exit);
375 MODULE_LICENSE("GPL");
376 MODULE_DESCRIPTION("Sequence Number IV Generator");
377 MODULE_ALIAS_CRYPTO("seqiv");