2 * chainiv: Chain IV Generator
4 * Generate IVs simply be using the last block of the previous encryption.
5 * This is mainly useful for CBC with a synchronous algorithm.
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 <crypto/rng.h>
18 #include <crypto/crypto_wq.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/spinlock.h>
24 #include <linux/string.h>
25 #include <linux/workqueue.h>
28 CHAINIV_STATE_INUSE = 0,
36 struct async_chainiv_ctx {
42 struct crypto_queue queue;
43 struct work_struct postponed;
48 static int chainiv_givencrypt(struct skcipher_givcrypt_request *req)
50 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
51 struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
52 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
56 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
57 ablkcipher_request_set_callback(subreq, req->creq.base.flags &
58 ~CRYPTO_TFM_REQ_MAY_SLEEP,
59 req->creq.base.complete,
61 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
62 req->creq.nbytes, req->creq.info);
64 spin_lock_bh(&ctx->lock);
66 ivsize = crypto_ablkcipher_ivsize(geniv);
68 memcpy(req->giv, ctx->iv, ivsize);
69 memcpy(subreq->info, ctx->iv, ivsize);
71 err = crypto_ablkcipher_encrypt(subreq);
75 memcpy(ctx->iv, subreq->info, ivsize);
78 spin_unlock_bh(&ctx->lock);
83 static int chainiv_init_common(struct crypto_tfm *tfm, char iv[])
85 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
88 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
91 err = crypto_rng_get_bytes(crypto_default_rng, iv,
92 crypto_ablkcipher_ivsize(geniv));
93 crypto_put_default_rng();
96 return err ?: skcipher_geniv_init(tfm);
99 static int chainiv_init(struct crypto_tfm *tfm)
101 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
102 struct chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
105 spin_lock_init(&ctx->lock);
108 if (!crypto_get_default_rng()) {
109 crypto_ablkcipher_crt(geniv)->givencrypt = chainiv_givencrypt;
113 return chainiv_init_common(tfm, iv);
116 static int async_chainiv_schedule_work(struct async_chainiv_ctx *ctx)
121 if (!ctx->queue.qlen) {
122 smp_mb__before_atomic();
123 clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
125 if (!ctx->queue.qlen ||
126 test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
130 queued = queue_work(kcrypto_wq, &ctx->postponed);
137 static int async_chainiv_postpone_request(struct skcipher_givcrypt_request *req)
139 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
140 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
143 spin_lock_bh(&ctx->lock);
144 err = skcipher_enqueue_givcrypt(&ctx->queue, req);
145 spin_unlock_bh(&ctx->lock);
147 if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
151 return async_chainiv_schedule_work(ctx);
154 static int async_chainiv_givencrypt_tail(struct skcipher_givcrypt_request *req)
156 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
157 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
158 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
159 unsigned int ivsize = crypto_ablkcipher_ivsize(geniv);
161 memcpy(req->giv, ctx->iv, ivsize);
162 memcpy(subreq->info, ctx->iv, ivsize);
164 ctx->err = crypto_ablkcipher_encrypt(subreq);
168 memcpy(ctx->iv, subreq->info, ivsize);
171 return async_chainiv_schedule_work(ctx);
174 static int async_chainiv_givencrypt(struct skcipher_givcrypt_request *req)
176 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
177 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
178 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
180 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
181 ablkcipher_request_set_callback(subreq, req->creq.base.flags,
182 req->creq.base.complete,
183 req->creq.base.data);
184 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
185 req->creq.nbytes, req->creq.info);
187 if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
190 if (ctx->queue.qlen) {
191 clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
195 return async_chainiv_givencrypt_tail(req);
198 return async_chainiv_postpone_request(req);
201 static void async_chainiv_do_postponed(struct work_struct *work)
203 struct async_chainiv_ctx *ctx = container_of(work,
204 struct async_chainiv_ctx,
206 struct skcipher_givcrypt_request *req;
207 struct ablkcipher_request *subreq;
210 /* Only handle one request at a time to avoid hogging keventd. */
211 spin_lock_bh(&ctx->lock);
212 req = skcipher_dequeue_givcrypt(&ctx->queue);
213 spin_unlock_bh(&ctx->lock);
216 async_chainiv_schedule_work(ctx);
220 subreq = skcipher_givcrypt_reqctx(req);
221 subreq->base.flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
223 err = async_chainiv_givencrypt_tail(req);
226 skcipher_givcrypt_complete(req, err);
230 static int async_chainiv_init(struct crypto_tfm *tfm)
232 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
233 struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
236 spin_lock_init(&ctx->lock);
238 crypto_init_queue(&ctx->queue, 100);
239 INIT_WORK(&ctx->postponed, async_chainiv_do_postponed);
242 if (!crypto_get_default_rng()) {
243 crypto_ablkcipher_crt(geniv)->givencrypt =
244 async_chainiv_givencrypt;
248 return chainiv_init_common(tfm, iv);
251 static void async_chainiv_exit(struct crypto_tfm *tfm)
253 struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
255 BUG_ON(test_bit(CHAINIV_STATE_INUSE, &ctx->state) || ctx->queue.qlen);
257 skcipher_geniv_exit(tfm);
260 static struct crypto_template chainiv_tmpl;
262 static struct crypto_instance *chainiv_alloc(struct rtattr **tb)
264 struct crypto_attr_type *algt;
265 struct crypto_instance *inst;
267 algt = crypto_get_attr_type(tb);
269 return ERR_CAST(algt);
271 inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0, 0);
275 inst->alg.cra_init = chainiv_init;
276 inst->alg.cra_exit = skcipher_geniv_exit;
278 inst->alg.cra_ctxsize = sizeof(struct chainiv_ctx);
280 if (!crypto_requires_sync(algt->type, algt->mask)) {
281 inst->alg.cra_flags |= CRYPTO_ALG_ASYNC;
283 inst->alg.cra_init = async_chainiv_init;
284 inst->alg.cra_exit = async_chainiv_exit;
286 inst->alg.cra_ctxsize = sizeof(struct async_chainiv_ctx);
289 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
295 static struct crypto_template chainiv_tmpl = {
297 .alloc = chainiv_alloc,
298 .free = skcipher_geniv_free,
299 .module = THIS_MODULE,
302 static int __init chainiv_module_init(void)
304 return crypto_register_template(&chainiv_tmpl);
307 static void chainiv_module_exit(void)
309 crypto_unregister_template(&chainiv_tmpl);
312 module_init(chainiv_module_init);
313 module_exit(chainiv_module_exit);
315 MODULE_LICENSE("GPL");
316 MODULE_DESCRIPTION("Chain IV Generator");
317 MODULE_ALIAS_CRYPTO("chainiv");