blob: 9affadee32879c5114afab56d4bc82428c01dcf6 [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>
17#include <linux/err.h>
18#include <linux/init.h>
Herbert Xue7cd2512007-12-14 22:28:14 +080019#include <linux/kernel.h>
Herbert Xu7f470732007-11-27 23:17:23 +080020#include <linux/module.h>
21#include <linux/random.h>
22#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);
86
87 spin_lock_bh(&ctx->lock);
88 if (crypto_ablkcipher_crt(geniv)->givencrypt !=
89 chainiv_givencrypt_first)
90 goto unlock;
91
92 crypto_ablkcipher_crt(geniv)->givencrypt = chainiv_givencrypt;
93 get_random_bytes(ctx->iv, crypto_ablkcipher_ivsize(geniv));
94
95unlock:
96 spin_unlock_bh(&ctx->lock);
97
98 return chainiv_givencrypt(req);
99}
100
Herbert Xue7cd2512007-12-14 22:28:14 +0800101static int chainiv_init_common(struct crypto_tfm *tfm)
Herbert Xu7f470732007-11-27 23:17:23 +0800102{
Herbert Xu7f470732007-11-27 23:17:23 +0800103 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
104
105 return skcipher_geniv_init(tfm);
106}
107
Herbert Xue7cd2512007-12-14 22:28:14 +0800108static int chainiv_init(struct crypto_tfm *tfm)
109{
110 struct chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
111
112 spin_lock_init(&ctx->lock);
113
114 return chainiv_init_common(tfm);
115}
116
117static int async_chainiv_schedule_work(struct async_chainiv_ctx *ctx)
118{
119 int queued;
Herbert Xu872ac872008-07-10 17:42:36 +0800120 int err = ctx->err;
Herbert Xue7cd2512007-12-14 22:28:14 +0800121
122 if (!ctx->queue.qlen) {
123 smp_mb__before_clear_bit();
124 clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
125
126 if (!ctx->queue.qlen ||
127 test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
128 goto out;
129 }
130
131 queued = schedule_work(&ctx->postponed);
132 BUG_ON(!queued);
133
134out:
Herbert Xu872ac872008-07-10 17:42:36 +0800135 return err;
Herbert Xue7cd2512007-12-14 22:28:14 +0800136}
137
138static int async_chainiv_postpone_request(struct skcipher_givcrypt_request *req)
139{
140 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
141 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
142 int err;
143
144 spin_lock_bh(&ctx->lock);
145 err = skcipher_enqueue_givcrypt(&ctx->queue, req);
146 spin_unlock_bh(&ctx->lock);
147
148 if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
149 return err;
150
151 ctx->err = err;
152 return async_chainiv_schedule_work(ctx);
153}
154
155static int async_chainiv_givencrypt_tail(struct skcipher_givcrypt_request *req)
156{
157 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
158 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
159 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
160 unsigned int ivsize = crypto_ablkcipher_ivsize(geniv);
161
162 memcpy(req->giv, ctx->iv, ivsize);
163 memcpy(subreq->info, ctx->iv, ivsize);
164
165 ctx->err = crypto_ablkcipher_encrypt(subreq);
166 if (ctx->err)
167 goto out;
168
169 memcpy(ctx->iv, subreq->info, ivsize);
170
171out:
172 return async_chainiv_schedule_work(ctx);
173}
174
175static int async_chainiv_givencrypt(struct skcipher_givcrypt_request *req)
176{
177 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
178 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
179 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
180
181 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
182 ablkcipher_request_set_callback(subreq, req->creq.base.flags,
183 req->creq.base.complete,
184 req->creq.base.data);
185 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
186 req->creq.nbytes, req->creq.info);
187
188 if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
189 goto postpone;
190
191 if (ctx->queue.qlen) {
192 clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
193 goto postpone;
194 }
195
196 return async_chainiv_givencrypt_tail(req);
197
198postpone:
199 return async_chainiv_postpone_request(req);
200}
201
202static int async_chainiv_givencrypt_first(struct skcipher_givcrypt_request *req)
203{
204 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
205 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
206
207 if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
208 goto out;
209
210 if (crypto_ablkcipher_crt(geniv)->givencrypt !=
211 async_chainiv_givencrypt_first)
212 goto unlock;
213
214 crypto_ablkcipher_crt(geniv)->givencrypt = async_chainiv_givencrypt;
215 get_random_bytes(ctx->iv, crypto_ablkcipher_ivsize(geniv));
216
217unlock:
218 clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
219
220out:
221 return async_chainiv_givencrypt(req);
222}
223
224static void async_chainiv_do_postponed(struct work_struct *work)
225{
226 struct async_chainiv_ctx *ctx = container_of(work,
227 struct async_chainiv_ctx,
228 postponed);
229 struct skcipher_givcrypt_request *req;
230 struct ablkcipher_request *subreq;
Herbert Xu872ac872008-07-10 17:42:36 +0800231 int err;
Herbert Xue7cd2512007-12-14 22:28:14 +0800232
233 /* Only handle one request at a time to avoid hogging keventd. */
234 spin_lock_bh(&ctx->lock);
235 req = skcipher_dequeue_givcrypt(&ctx->queue);
236 spin_unlock_bh(&ctx->lock);
237
238 if (!req) {
239 async_chainiv_schedule_work(ctx);
240 return;
241 }
242
243 subreq = skcipher_givcrypt_reqctx(req);
244 subreq->base.flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
245
Herbert Xu872ac872008-07-10 17:42:36 +0800246 err = async_chainiv_givencrypt_tail(req);
247
248 local_bh_disable();
249 skcipher_givcrypt_complete(req, err);
250 local_bh_enable();
Herbert Xue7cd2512007-12-14 22:28:14 +0800251}
252
253static int async_chainiv_init(struct crypto_tfm *tfm)
254{
255 struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
256
257 spin_lock_init(&ctx->lock);
258
259 crypto_init_queue(&ctx->queue, 100);
260 INIT_WORK(&ctx->postponed, async_chainiv_do_postponed);
261
262 return chainiv_init_common(tfm);
263}
264
265static void async_chainiv_exit(struct crypto_tfm *tfm)
266{
267 struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
268
269 BUG_ON(test_bit(CHAINIV_STATE_INUSE, &ctx->state) || ctx->queue.qlen);
270
271 skcipher_geniv_exit(tfm);
272}
273
Herbert Xu7f470732007-11-27 23:17:23 +0800274static struct crypto_template chainiv_tmpl;
275
276static struct crypto_instance *chainiv_alloc(struct rtattr **tb)
277{
Herbert Xue7cd2512007-12-14 22:28:14 +0800278 struct crypto_attr_type *algt;
Herbert Xu7f470732007-11-27 23:17:23 +0800279 struct crypto_instance *inst;
Herbert Xue7cd2512007-12-14 22:28:14 +0800280 int err;
Herbert Xu7f470732007-11-27 23:17:23 +0800281
Herbert Xue7cd2512007-12-14 22:28:14 +0800282 algt = crypto_get_attr_type(tb);
283 err = PTR_ERR(algt);
284 if (IS_ERR(algt))
285 return ERR_PTR(err);
286
287 inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0, 0);
Herbert Xu7f470732007-11-27 23:17:23 +0800288 if (IS_ERR(inst))
289 goto out;
290
291 inst->alg.cra_ablkcipher.givencrypt = chainiv_givencrypt_first;
292
293 inst->alg.cra_init = chainiv_init;
294 inst->alg.cra_exit = skcipher_geniv_exit;
295
Herbert Xue7cd2512007-12-14 22:28:14 +0800296 inst->alg.cra_ctxsize = sizeof(struct chainiv_ctx);
297
298 if (!crypto_requires_sync(algt->type, algt->mask)) {
299 inst->alg.cra_flags |= CRYPTO_ALG_ASYNC;
300
301 inst->alg.cra_ablkcipher.givencrypt =
302 async_chainiv_givencrypt_first;
303
304 inst->alg.cra_init = async_chainiv_init;
305 inst->alg.cra_exit = async_chainiv_exit;
306
307 inst->alg.cra_ctxsize = sizeof(struct async_chainiv_ctx);
308 }
309
310 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
Herbert Xu7f470732007-11-27 23:17:23 +0800311
312out:
313 return inst;
314}
315
316static struct crypto_template chainiv_tmpl = {
317 .name = "chainiv",
318 .alloc = chainiv_alloc,
319 .free = skcipher_geniv_free,
320 .module = THIS_MODULE,
321};
322
Herbert Xu76fc60a2008-02-23 11:12:06 +0800323int __init chainiv_module_init(void)
Herbert Xu7f470732007-11-27 23:17:23 +0800324{
325 return crypto_register_template(&chainiv_tmpl);
326}
327
Herbert Xuf13ba2f2008-03-08 20:29:43 +0800328void chainiv_module_exit(void)
Herbert Xu7f470732007-11-27 23:17:23 +0800329{
330 crypto_unregister_template(&chainiv_tmpl);
331}