blob: be0bd521c46f1fa3cda099e3c88f69d26e569a64 [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>
Huang Ying0a2e8212009-02-19 14:44:02 +080018#include <crypto/crypto_wq.h>
Herbert Xu7f470732007-11-27 23:17:23 +080019#include <linux/err.h>
20#include <linux/init.h>
Herbert Xue7cd2512007-12-14 22:28:14 +080021#include <linux/kernel.h>
Herbert Xu7f470732007-11-27 23:17:23 +080022#include <linux/module.h>
Herbert Xu7f470732007-11-27 23:17:23 +080023#include <linux/spinlock.h>
24#include <linux/string.h>
Herbert Xue7cd2512007-12-14 22:28:14 +080025#include <linux/workqueue.h>
26
27enum {
28 CHAINIV_STATE_INUSE = 0,
29};
Herbert Xu7f470732007-11-27 23:17:23 +080030
31struct chainiv_ctx {
32 spinlock_t lock;
33 char iv[];
34};
35
Herbert Xue7cd2512007-12-14 22:28:14 +080036struct async_chainiv_ctx {
37 unsigned long state;
38
39 spinlock_t lock;
40 int err;
41
42 struct crypto_queue queue;
43 struct work_struct postponed;
44
45 char iv[];
46};
47
Herbert Xu7f470732007-11-27 23:17:23 +080048static int chainiv_givencrypt(struct skcipher_givcrypt_request *req)
49{
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);
53 unsigned int ivsize;
54 int err;
55
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,
60 req->creq.base.data);
61 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
62 req->creq.nbytes, req->creq.info);
63
64 spin_lock_bh(&ctx->lock);
65
66 ivsize = crypto_ablkcipher_ivsize(geniv);
67
68 memcpy(req->giv, ctx->iv, ivsize);
69 memcpy(subreq->info, ctx->iv, ivsize);
70
71 err = crypto_ablkcipher_encrypt(subreq);
72 if (err)
73 goto unlock;
74
75 memcpy(ctx->iv, subreq->info, ivsize);
76
77unlock:
78 spin_unlock_bh(&ctx->lock);
79
80 return err;
81}
82
Herbert Xu65fe6742015-06-03 14:49:23 +080083static int chainiv_init_common(struct crypto_tfm *tfm, char iv[])
Herbert Xu7f470732007-11-27 23:17:23 +080084{
Herbert Xu65fe6742015-06-03 14:49:23 +080085 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
Herbert Xu7f470732007-11-27 23:17:23 +080086
Herbert Xu7f470732007-11-27 23:17:23 +080087 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
88
Herbert Xu65fe6742015-06-03 14:49:23 +080089 return crypto_rng_get_bytes(crypto_default_rng, iv,
90 crypto_ablkcipher_ivsize(geniv)) ?:
91 skcipher_geniv_init(tfm);
Herbert Xu7f470732007-11-27 23:17:23 +080092}
93
Herbert Xue7cd2512007-12-14 22:28:14 +080094static int chainiv_init(struct crypto_tfm *tfm)
95{
96 struct chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
97
98 spin_lock_init(&ctx->lock);
99
Herbert Xu65fe6742015-06-03 14:49:23 +0800100 return chainiv_init_common(tfm, ctx->iv);
Herbert Xue7cd2512007-12-14 22:28:14 +0800101}
102
103static int async_chainiv_schedule_work(struct async_chainiv_ctx *ctx)
104{
105 int queued;
Herbert Xu872ac872008-07-10 17:42:36 +0800106 int err = ctx->err;
Herbert Xue7cd2512007-12-14 22:28:14 +0800107
108 if (!ctx->queue.qlen) {
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100109 smp_mb__before_atomic();
Herbert Xue7cd2512007-12-14 22:28:14 +0800110 clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
111
112 if (!ctx->queue.qlen ||
113 test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
114 goto out;
115 }
116
Huang Ying0a2e8212009-02-19 14:44:02 +0800117 queued = queue_work(kcrypto_wq, &ctx->postponed);
Herbert Xue7cd2512007-12-14 22:28:14 +0800118 BUG_ON(!queued);
119
120out:
Herbert Xu872ac872008-07-10 17:42:36 +0800121 return err;
Herbert Xue7cd2512007-12-14 22:28:14 +0800122}
123
124static int async_chainiv_postpone_request(struct skcipher_givcrypt_request *req)
125{
126 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
127 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
128 int err;
129
130 spin_lock_bh(&ctx->lock);
131 err = skcipher_enqueue_givcrypt(&ctx->queue, req);
132 spin_unlock_bh(&ctx->lock);
133
134 if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
135 return err;
136
137 ctx->err = err;
138 return async_chainiv_schedule_work(ctx);
139}
140
141static int async_chainiv_givencrypt_tail(struct skcipher_givcrypt_request *req)
142{
143 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
144 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
145 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
146 unsigned int ivsize = crypto_ablkcipher_ivsize(geniv);
147
148 memcpy(req->giv, ctx->iv, ivsize);
149 memcpy(subreq->info, ctx->iv, ivsize);
150
151 ctx->err = crypto_ablkcipher_encrypt(subreq);
152 if (ctx->err)
153 goto out;
154
155 memcpy(ctx->iv, subreq->info, ivsize);
156
157out:
158 return async_chainiv_schedule_work(ctx);
159}
160
161static int async_chainiv_givencrypt(struct skcipher_givcrypt_request *req)
162{
163 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
164 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
165 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
166
167 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
168 ablkcipher_request_set_callback(subreq, req->creq.base.flags,
169 req->creq.base.complete,
170 req->creq.base.data);
171 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
172 req->creq.nbytes, req->creq.info);
173
174 if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
175 goto postpone;
176
177 if (ctx->queue.qlen) {
178 clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
179 goto postpone;
180 }
181
182 return async_chainiv_givencrypt_tail(req);
183
184postpone:
185 return async_chainiv_postpone_request(req);
186}
187
Herbert Xue7cd2512007-12-14 22:28:14 +0800188static void async_chainiv_do_postponed(struct work_struct *work)
189{
190 struct async_chainiv_ctx *ctx = container_of(work,
191 struct async_chainiv_ctx,
192 postponed);
193 struct skcipher_givcrypt_request *req;
194 struct ablkcipher_request *subreq;
Herbert Xu872ac872008-07-10 17:42:36 +0800195 int err;
Herbert Xue7cd2512007-12-14 22:28:14 +0800196
197 /* Only handle one request at a time to avoid hogging keventd. */
198 spin_lock_bh(&ctx->lock);
199 req = skcipher_dequeue_givcrypt(&ctx->queue);
200 spin_unlock_bh(&ctx->lock);
201
202 if (!req) {
203 async_chainiv_schedule_work(ctx);
204 return;
205 }
206
207 subreq = skcipher_givcrypt_reqctx(req);
208 subreq->base.flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
209
Herbert Xu872ac872008-07-10 17:42:36 +0800210 err = async_chainiv_givencrypt_tail(req);
211
212 local_bh_disable();
213 skcipher_givcrypt_complete(req, err);
214 local_bh_enable();
Herbert Xue7cd2512007-12-14 22:28:14 +0800215}
216
217static int async_chainiv_init(struct crypto_tfm *tfm)
218{
219 struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
220
221 spin_lock_init(&ctx->lock);
222
223 crypto_init_queue(&ctx->queue, 100);
224 INIT_WORK(&ctx->postponed, async_chainiv_do_postponed);
225
Herbert Xu65fe6742015-06-03 14:49:23 +0800226 return chainiv_init_common(tfm, ctx->iv);
Herbert Xue7cd2512007-12-14 22:28:14 +0800227}
228
229static void async_chainiv_exit(struct crypto_tfm *tfm)
230{
231 struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
232
233 BUG_ON(test_bit(CHAINIV_STATE_INUSE, &ctx->state) || ctx->queue.qlen);
234
235 skcipher_geniv_exit(tfm);
236}
237
Herbert Xu7f470732007-11-27 23:17:23 +0800238static struct crypto_template chainiv_tmpl;
239
240static struct crypto_instance *chainiv_alloc(struct rtattr **tb)
241{
Herbert Xue7cd2512007-12-14 22:28:14 +0800242 struct crypto_attr_type *algt;
Herbert Xu7f470732007-11-27 23:17:23 +0800243 struct crypto_instance *inst;
Herbert Xue7cd2512007-12-14 22:28:14 +0800244 int err;
Herbert Xu7f470732007-11-27 23:17:23 +0800245
Herbert Xue7cd2512007-12-14 22:28:14 +0800246 algt = crypto_get_attr_type(tb);
Herbert Xue7cd2512007-12-14 22:28:14 +0800247 if (IS_ERR(algt))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100248 return ERR_CAST(algt);
Herbert Xue7cd2512007-12-14 22:28:14 +0800249
Herbert Xua0f000e2008-08-14 22:21:31 +1000250 err = crypto_get_default_rng();
251 if (err)
252 return ERR_PTR(err);
253
Herbert Xue7cd2512007-12-14 22:28:14 +0800254 inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0, 0);
Herbert Xu7f470732007-11-27 23:17:23 +0800255 if (IS_ERR(inst))
Herbert Xua0f000e2008-08-14 22:21:31 +1000256 goto put_rng;
Herbert Xu7f470732007-11-27 23:17:23 +0800257
Herbert Xu65fe6742015-06-03 14:49:23 +0800258 inst->alg.cra_ablkcipher.givencrypt = chainiv_givencrypt;
Herbert Xu7f470732007-11-27 23:17:23 +0800259
260 inst->alg.cra_init = chainiv_init;
261 inst->alg.cra_exit = skcipher_geniv_exit;
262
Herbert Xue7cd2512007-12-14 22:28:14 +0800263 inst->alg.cra_ctxsize = sizeof(struct chainiv_ctx);
264
265 if (!crypto_requires_sync(algt->type, algt->mask)) {
266 inst->alg.cra_flags |= CRYPTO_ALG_ASYNC;
267
Herbert Xu65fe6742015-06-03 14:49:23 +0800268 inst->alg.cra_ablkcipher.givencrypt = async_chainiv_givencrypt;
Herbert Xue7cd2512007-12-14 22:28:14 +0800269
270 inst->alg.cra_init = async_chainiv_init;
271 inst->alg.cra_exit = async_chainiv_exit;
272
273 inst->alg.cra_ctxsize = sizeof(struct async_chainiv_ctx);
274 }
275
276 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
Herbert Xu7f470732007-11-27 23:17:23 +0800277
278out:
279 return inst;
Herbert Xua0f000e2008-08-14 22:21:31 +1000280
281put_rng:
282 crypto_put_default_rng();
283 goto out;
284}
285
286static void chainiv_free(struct crypto_instance *inst)
287{
288 skcipher_geniv_free(inst);
289 crypto_put_default_rng();
Herbert Xu7f470732007-11-27 23:17:23 +0800290}
291
292static struct crypto_template chainiv_tmpl = {
293 .name = "chainiv",
294 .alloc = chainiv_alloc,
Herbert Xua0f000e2008-08-14 22:21:31 +1000295 .free = chainiv_free,
Herbert Xu7f470732007-11-27 23:17:23 +0800296 .module = THIS_MODULE,
297};
298
Herbert Xu5be5e662008-08-17 18:04:30 +1000299static int __init chainiv_module_init(void)
Herbert Xu7f470732007-11-27 23:17:23 +0800300{
301 return crypto_register_template(&chainiv_tmpl);
302}
303
Herbert Xu5be5e662008-08-17 18:04:30 +1000304static void chainiv_module_exit(void)
Herbert Xu7f470732007-11-27 23:17:23 +0800305{
306 crypto_unregister_template(&chainiv_tmpl);
307}
Herbert Xu5be5e662008-08-17 18:04:30 +1000308
309module_init(chainiv_module_init);
310module_exit(chainiv_module_exit);
311
312MODULE_LICENSE("GPL");
313MODULE_DESCRIPTION("Chain IV Generator");
Kees Cook4943ba12014-11-24 16:32:38 -0800314MODULE_ALIAS_CRYPTO("chainiv");