blob: 7c37a497b860a27733eac07c4f7ff018c0abb4a3 [file] [log] [blame]
Herbert Xu7f470732007-11-27 23:17:23 +08001/*
2 * chainiv: Chain IV Generator
3 *
4 * Generate IVs simply be using the last block of the previous encryption.
5 * This is mainly useful for CBC with a synchronous algorithm.
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/skcipher.h>
Herbert Xua0f000e2008-08-14 22:21:31 +100017#include <crypto/rng.h>
Herbert Xu7f470732007-11-27 23:17:23 +080018#include <linux/err.h>
19#include <linux/init.h>
Herbert Xue7cd2512007-12-14 22:28:14 +080020#include <linux/kernel.h>
Herbert Xu7f470732007-11-27 23:17:23 +080021#include <linux/module.h>
Herbert Xu7f470732007-11-27 23:17:23 +080022#include <linux/spinlock.h>
23#include <linux/string.h>
Herbert Xue7cd2512007-12-14 22:28:14 +080024#include <linux/workqueue.h>
25
26enum {
27 CHAINIV_STATE_INUSE = 0,
28};
Herbert Xu7f470732007-11-27 23:17:23 +080029
30struct chainiv_ctx {
31 spinlock_t lock;
32 char iv[];
33};
34
Herbert Xue7cd2512007-12-14 22:28:14 +080035struct async_chainiv_ctx {
36 unsigned long state;
37
38 spinlock_t lock;
39 int err;
40
41 struct crypto_queue queue;
42 struct work_struct postponed;
43
44 char iv[];
45};
46
Herbert Xu7f470732007-11-27 23:17:23 +080047static int chainiv_givencrypt(struct skcipher_givcrypt_request *req)
48{
49 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
50 struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
51 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
52 unsigned int ivsize;
53 int err;
54
55 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
56 ablkcipher_request_set_callback(subreq, req->creq.base.flags &
57 ~CRYPTO_TFM_REQ_MAY_SLEEP,
58 req->creq.base.complete,
59 req->creq.base.data);
60 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
61 req->creq.nbytes, req->creq.info);
62
63 spin_lock_bh(&ctx->lock);
64
65 ivsize = crypto_ablkcipher_ivsize(geniv);
66
67 memcpy(req->giv, ctx->iv, ivsize);
68 memcpy(subreq->info, ctx->iv, ivsize);
69
70 err = crypto_ablkcipher_encrypt(subreq);
71 if (err)
72 goto unlock;
73
74 memcpy(ctx->iv, subreq->info, ivsize);
75
76unlock:
77 spin_unlock_bh(&ctx->lock);
78
79 return err;
80}
81
82static int chainiv_givencrypt_first(struct skcipher_givcrypt_request *req)
83{
84 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
85 struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
Herbert Xua0f000e2008-08-14 22:21:31 +100086 int err = 0;
Herbert Xu7f470732007-11-27 23:17:23 +080087
88 spin_lock_bh(&ctx->lock);
89 if (crypto_ablkcipher_crt(geniv)->givencrypt !=
90 chainiv_givencrypt_first)
91 goto unlock;
92
93 crypto_ablkcipher_crt(geniv)->givencrypt = chainiv_givencrypt;
Herbert Xua0f000e2008-08-14 22:21:31 +100094 err = crypto_rng_get_bytes(crypto_default_rng, ctx->iv,
95 crypto_ablkcipher_ivsize(geniv));
Herbert Xu7f470732007-11-27 23:17:23 +080096
97unlock:
98 spin_unlock_bh(&ctx->lock);
99
Herbert Xua0f000e2008-08-14 22:21:31 +1000100 if (err)
101 return err;
102
Herbert Xu7f470732007-11-27 23:17:23 +0800103 return chainiv_givencrypt(req);
104}
105
Herbert Xue7cd2512007-12-14 22:28:14 +0800106static int chainiv_init_common(struct crypto_tfm *tfm)
Herbert Xu7f470732007-11-27 23:17:23 +0800107{
Herbert Xu7f470732007-11-27 23:17:23 +0800108 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
109
110 return skcipher_geniv_init(tfm);
111}
112
Herbert Xue7cd2512007-12-14 22:28:14 +0800113static int chainiv_init(struct crypto_tfm *tfm)
114{
115 struct chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
116
117 spin_lock_init(&ctx->lock);
118
119 return chainiv_init_common(tfm);
120}
121
122static int async_chainiv_schedule_work(struct async_chainiv_ctx *ctx)
123{
124 int queued;
Herbert Xu872ac872008-07-10 17:42:36 +0800125 int err = ctx->err;
Herbert Xue7cd2512007-12-14 22:28:14 +0800126
127 if (!ctx->queue.qlen) {
128 smp_mb__before_clear_bit();
129 clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
130
131 if (!ctx->queue.qlen ||
132 test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
133 goto out;
134 }
135
136 queued = schedule_work(&ctx->postponed);
137 BUG_ON(!queued);
138
139out:
Herbert Xu872ac872008-07-10 17:42:36 +0800140 return err;
Herbert Xue7cd2512007-12-14 22:28:14 +0800141}
142
143static int async_chainiv_postpone_request(struct skcipher_givcrypt_request *req)
144{
145 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
146 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
147 int err;
148
149 spin_lock_bh(&ctx->lock);
150 err = skcipher_enqueue_givcrypt(&ctx->queue, req);
151 spin_unlock_bh(&ctx->lock);
152
153 if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
154 return err;
155
156 ctx->err = err;
157 return async_chainiv_schedule_work(ctx);
158}
159
160static int async_chainiv_givencrypt_tail(struct skcipher_givcrypt_request *req)
161{
162 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
163 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
164 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
165 unsigned int ivsize = crypto_ablkcipher_ivsize(geniv);
166
167 memcpy(req->giv, ctx->iv, ivsize);
168 memcpy(subreq->info, ctx->iv, ivsize);
169
170 ctx->err = crypto_ablkcipher_encrypt(subreq);
171 if (ctx->err)
172 goto out;
173
174 memcpy(ctx->iv, subreq->info, ivsize);
175
176out:
177 return async_chainiv_schedule_work(ctx);
178}
179
180static int async_chainiv_givencrypt(struct skcipher_givcrypt_request *req)
181{
182 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
183 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
184 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
185
186 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
187 ablkcipher_request_set_callback(subreq, req->creq.base.flags,
188 req->creq.base.complete,
189 req->creq.base.data);
190 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
191 req->creq.nbytes, req->creq.info);
192
193 if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
194 goto postpone;
195
196 if (ctx->queue.qlen) {
197 clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
198 goto postpone;
199 }
200
201 return async_chainiv_givencrypt_tail(req);
202
203postpone:
204 return async_chainiv_postpone_request(req);
205}
206
207static int async_chainiv_givencrypt_first(struct skcipher_givcrypt_request *req)
208{
209 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
210 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
Herbert Xua0f000e2008-08-14 22:21:31 +1000211 int err = 0;
Herbert Xue7cd2512007-12-14 22:28:14 +0800212
213 if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
214 goto out;
215
216 if (crypto_ablkcipher_crt(geniv)->givencrypt !=
217 async_chainiv_givencrypt_first)
218 goto unlock;
219
220 crypto_ablkcipher_crt(geniv)->givencrypt = async_chainiv_givencrypt;
Herbert Xua0f000e2008-08-14 22:21:31 +1000221 err = crypto_rng_get_bytes(crypto_default_rng, ctx->iv,
222 crypto_ablkcipher_ivsize(geniv));
Herbert Xue7cd2512007-12-14 22:28:14 +0800223
224unlock:
225 clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
226
Herbert Xua0f000e2008-08-14 22:21:31 +1000227 if (err)
228 return err;
229
Herbert Xue7cd2512007-12-14 22:28:14 +0800230out:
231 return async_chainiv_givencrypt(req);
232}
233
234static void async_chainiv_do_postponed(struct work_struct *work)
235{
236 struct async_chainiv_ctx *ctx = container_of(work,
237 struct async_chainiv_ctx,
238 postponed);
239 struct skcipher_givcrypt_request *req;
240 struct ablkcipher_request *subreq;
Herbert Xu872ac872008-07-10 17:42:36 +0800241 int err;
Herbert Xue7cd2512007-12-14 22:28:14 +0800242
243 /* Only handle one request at a time to avoid hogging keventd. */
244 spin_lock_bh(&ctx->lock);
245 req = skcipher_dequeue_givcrypt(&ctx->queue);
246 spin_unlock_bh(&ctx->lock);
247
248 if (!req) {
249 async_chainiv_schedule_work(ctx);
250 return;
251 }
252
253 subreq = skcipher_givcrypt_reqctx(req);
254 subreq->base.flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
255
Herbert Xu872ac872008-07-10 17:42:36 +0800256 err = async_chainiv_givencrypt_tail(req);
257
258 local_bh_disable();
259 skcipher_givcrypt_complete(req, err);
260 local_bh_enable();
Herbert Xue7cd2512007-12-14 22:28:14 +0800261}
262
263static int async_chainiv_init(struct crypto_tfm *tfm)
264{
265 struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
266
267 spin_lock_init(&ctx->lock);
268
269 crypto_init_queue(&ctx->queue, 100);
270 INIT_WORK(&ctx->postponed, async_chainiv_do_postponed);
271
272 return chainiv_init_common(tfm);
273}
274
275static void async_chainiv_exit(struct crypto_tfm *tfm)
276{
277 struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
278
279 BUG_ON(test_bit(CHAINIV_STATE_INUSE, &ctx->state) || ctx->queue.qlen);
280
281 skcipher_geniv_exit(tfm);
282}
283
Herbert Xu7f470732007-11-27 23:17:23 +0800284static struct crypto_template chainiv_tmpl;
285
286static struct crypto_instance *chainiv_alloc(struct rtattr **tb)
287{
Herbert Xue7cd2512007-12-14 22:28:14 +0800288 struct crypto_attr_type *algt;
Herbert Xu7f470732007-11-27 23:17:23 +0800289 struct crypto_instance *inst;
Herbert Xue7cd2512007-12-14 22:28:14 +0800290 int err;
Herbert Xu7f470732007-11-27 23:17:23 +0800291
Herbert Xue7cd2512007-12-14 22:28:14 +0800292 algt = crypto_get_attr_type(tb);
293 err = PTR_ERR(algt);
294 if (IS_ERR(algt))
295 return ERR_PTR(err);
296
Herbert Xua0f000e2008-08-14 22:21:31 +1000297 err = crypto_get_default_rng();
298 if (err)
299 return ERR_PTR(err);
300
Herbert Xue7cd2512007-12-14 22:28:14 +0800301 inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0, 0);
Herbert Xu7f470732007-11-27 23:17:23 +0800302 if (IS_ERR(inst))
Herbert Xua0f000e2008-08-14 22:21:31 +1000303 goto put_rng;
Herbert Xu7f470732007-11-27 23:17:23 +0800304
305 inst->alg.cra_ablkcipher.givencrypt = chainiv_givencrypt_first;
306
307 inst->alg.cra_init = chainiv_init;
308 inst->alg.cra_exit = skcipher_geniv_exit;
309
Herbert Xue7cd2512007-12-14 22:28:14 +0800310 inst->alg.cra_ctxsize = sizeof(struct chainiv_ctx);
311
312 if (!crypto_requires_sync(algt->type, algt->mask)) {
313 inst->alg.cra_flags |= CRYPTO_ALG_ASYNC;
314
315 inst->alg.cra_ablkcipher.givencrypt =
316 async_chainiv_givencrypt_first;
317
318 inst->alg.cra_init = async_chainiv_init;
319 inst->alg.cra_exit = async_chainiv_exit;
320
321 inst->alg.cra_ctxsize = sizeof(struct async_chainiv_ctx);
322 }
323
324 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
Herbert Xu7f470732007-11-27 23:17:23 +0800325
326out:
327 return inst;
Herbert Xua0f000e2008-08-14 22:21:31 +1000328
329put_rng:
330 crypto_put_default_rng();
331 goto out;
332}
333
334static void chainiv_free(struct crypto_instance *inst)
335{
336 skcipher_geniv_free(inst);
337 crypto_put_default_rng();
Herbert Xu7f470732007-11-27 23:17:23 +0800338}
339
340static struct crypto_template chainiv_tmpl = {
341 .name = "chainiv",
342 .alloc = chainiv_alloc,
Herbert Xua0f000e2008-08-14 22:21:31 +1000343 .free = chainiv_free,
Herbert Xu7f470732007-11-27 23:17:23 +0800344 .module = THIS_MODULE,
345};
346
Herbert Xu5be5e662008-08-17 18:04:30 +1000347static int __init chainiv_module_init(void)
Herbert Xu7f470732007-11-27 23:17:23 +0800348{
349 return crypto_register_template(&chainiv_tmpl);
350}
351
Herbert Xu5be5e662008-08-17 18:04:30 +1000352static void chainiv_module_exit(void)
Herbert Xu7f470732007-11-27 23:17:23 +0800353{
354 crypto_unregister_template(&chainiv_tmpl);
355}
Herbert Xu5be5e662008-08-17 18:04:30 +1000356
357module_init(chainiv_module_init);
358module_exit(chainiv_module_exit);
359
360MODULE_LICENSE("GPL");
361MODULE_DESCRIPTION("Chain IV Generator");