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/skcipher.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/random.h>
22 #include <linux/spinlock.h>
23 #include <linux/string.h>
27 u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
30 static void seqiv_complete2(struct skcipher_givcrypt_request *req, int err)
32 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
33 struct crypto_ablkcipher *geniv;
35 if (err == -EINPROGRESS)
41 geniv = skcipher_givcrypt_reqtfm(req);
42 memcpy(req->creq.info, subreq->info, crypto_ablkcipher_ivsize(geniv));
48 static void seqiv_complete(struct crypto_async_request *base, int err)
50 struct skcipher_givcrypt_request *req = base->data;
52 seqiv_complete2(req, err);
53 skcipher_givcrypt_complete(req, err);
56 static int seqiv_givencrypt(struct skcipher_givcrypt_request *req)
58 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
59 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
60 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
61 crypto_completion_t complete;
69 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
71 complete = req->creq.base.complete;
72 data = req->creq.base.data;
73 info = req->creq.info;
75 ivsize = crypto_ablkcipher_ivsize(geniv);
77 if (unlikely(!IS_ALIGNED((unsigned long)info,
78 crypto_ablkcipher_alignmask(geniv) + 1))) {
79 info = kmalloc(ivsize, req->creq.base.flags &
80 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
85 complete = seqiv_complete;
89 ablkcipher_request_set_callback(subreq, req->creq.base.flags, complete,
91 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
92 req->creq.nbytes, info);
95 if (ivsize > sizeof(u64)) {
96 memset(info, 0, ivsize - sizeof(u64));
99 seq = cpu_to_be64(req->seq);
100 memcpy(info + ivsize - len, &seq, len);
101 crypto_xor(info, ctx->salt, ivsize);
103 memcpy(req->giv, info, ivsize);
105 err = crypto_ablkcipher_encrypt(subreq);
106 if (unlikely(info != req->creq.info))
107 seqiv_complete2(req, err);
111 static int seqiv_givencrypt_first(struct skcipher_givcrypt_request *req)
113 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
114 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
116 spin_lock_bh(&ctx->lock);
117 if (crypto_ablkcipher_crt(geniv)->givencrypt != seqiv_givencrypt_first)
120 crypto_ablkcipher_crt(geniv)->givencrypt = seqiv_givencrypt;
121 get_random_bytes(ctx->salt, crypto_ablkcipher_ivsize(geniv));
124 spin_unlock_bh(&ctx->lock);
126 return seqiv_givencrypt(req);
129 static int seqiv_init(struct crypto_tfm *tfm)
131 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
132 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
134 spin_lock_init(&ctx->lock);
136 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
138 return skcipher_geniv_init(tfm);
141 static struct crypto_template seqiv_tmpl;
143 static struct crypto_instance *seqiv_alloc(struct rtattr **tb)
145 struct crypto_instance *inst;
147 inst = skcipher_geniv_alloc(&seqiv_tmpl, tb, 0, 0);
151 inst->alg.cra_ablkcipher.givencrypt = seqiv_givencrypt_first;
153 inst->alg.cra_init = seqiv_init;
154 inst->alg.cra_exit = skcipher_geniv_exit;
156 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
158 inst->alg.cra_ctxsize = sizeof(struct seqiv_ctx);
159 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
165 static struct crypto_template seqiv_tmpl = {
167 .alloc = seqiv_alloc,
168 .free = skcipher_geniv_free,
169 .module = THIS_MODULE,
172 static int __init seqiv_module_init(void)
174 return crypto_register_template(&seqiv_tmpl);
177 static void __exit seqiv_module_exit(void)
179 crypto_unregister_template(&seqiv_tmpl);
182 module_init(seqiv_module_init);
183 module_exit(seqiv_module_exit);
185 MODULE_LICENSE("GPL");
186 MODULE_DESCRIPTION("Sequence Number IV Generator");