blob: b4340018c8d47527068a65142876ac86e8728701 [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 Xu341476d2015-06-21 19:11:46 +080086 int err = 0;
Herbert Xu7f470732007-11-27 23:17:23 +080087
Herbert Xu7f470732007-11-27 23:17:23 +080088 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
89
Herbert Xu341476d2015-06-21 19:11:46 +080090 if (iv) {
91 err = crypto_rng_get_bytes(crypto_default_rng, iv,
92 crypto_ablkcipher_ivsize(geniv));
93 crypto_put_default_rng();
94 }
95
96 return err ?: skcipher_geniv_init(tfm);
Herbert Xu7f470732007-11-27 23:17:23 +080097}
98
Herbert Xue7cd2512007-12-14 22:28:14 +080099static int chainiv_init(struct crypto_tfm *tfm)
100{
Herbert Xu341476d2015-06-21 19:11:46 +0800101 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
Herbert Xue7cd2512007-12-14 22:28:14 +0800102 struct chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu341476d2015-06-21 19:11:46 +0800103 char *iv;
Herbert Xue7cd2512007-12-14 22:28:14 +0800104
105 spin_lock_init(&ctx->lock);
106
Herbert Xu341476d2015-06-21 19:11:46 +0800107 iv = NULL;
108 if (!crypto_get_default_rng()) {
109 crypto_ablkcipher_crt(geniv)->givencrypt = chainiv_givencrypt;
110 iv = ctx->iv;
111 }
112
113 return chainiv_init_common(tfm, iv);
Herbert Xue7cd2512007-12-14 22:28:14 +0800114}
115
116static int async_chainiv_schedule_work(struct async_chainiv_ctx *ctx)
117{
118 int queued;
Herbert Xu872ac872008-07-10 17:42:36 +0800119 int err = ctx->err;
Herbert Xue7cd2512007-12-14 22:28:14 +0800120
121 if (!ctx->queue.qlen) {
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100122 smp_mb__before_atomic();
Herbert Xue7cd2512007-12-14 22:28:14 +0800123 clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
124
125 if (!ctx->queue.qlen ||
126 test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
127 goto out;
128 }
129
Huang Ying0a2e8212009-02-19 14:44:02 +0800130 queued = queue_work(kcrypto_wq, &ctx->postponed);
Herbert Xue7cd2512007-12-14 22:28:14 +0800131 BUG_ON(!queued);
132
133out:
Herbert Xu872ac872008-07-10 17:42:36 +0800134 return err;
Herbert Xue7cd2512007-12-14 22:28:14 +0800135}
136
137static int async_chainiv_postpone_request(struct skcipher_givcrypt_request *req)
138{
139 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
140 struct async_chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
141 int err;
142
143 spin_lock_bh(&ctx->lock);
144 err = skcipher_enqueue_givcrypt(&ctx->queue, req);
145 spin_unlock_bh(&ctx->lock);
146
147 if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
148 return err;
149
150 ctx->err = err;
151 return async_chainiv_schedule_work(ctx);
152}
153
154static int async_chainiv_givencrypt_tail(struct skcipher_givcrypt_request *req)
155{
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);
160
161 memcpy(req->giv, ctx->iv, ivsize);
162 memcpy(subreq->info, ctx->iv, ivsize);
163
164 ctx->err = crypto_ablkcipher_encrypt(subreq);
165 if (ctx->err)
166 goto out;
167
168 memcpy(ctx->iv, subreq->info, ivsize);
169
170out:
171 return async_chainiv_schedule_work(ctx);
172}
173
174static int async_chainiv_givencrypt(struct skcipher_givcrypt_request *req)
175{
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);
179
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);
186
187 if (test_and_set_bit(CHAINIV_STATE_INUSE, &ctx->state))
188 goto postpone;
189
190 if (ctx->queue.qlen) {
191 clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
192 goto postpone;
193 }
194
195 return async_chainiv_givencrypt_tail(req);
196
197postpone:
198 return async_chainiv_postpone_request(req);
199}
200
Herbert Xue7cd2512007-12-14 22:28:14 +0800201static void async_chainiv_do_postponed(struct work_struct *work)
202{
203 struct async_chainiv_ctx *ctx = container_of(work,
204 struct async_chainiv_ctx,
205 postponed);
206 struct skcipher_givcrypt_request *req;
207 struct ablkcipher_request *subreq;
Herbert Xu872ac872008-07-10 17:42:36 +0800208 int err;
Herbert Xue7cd2512007-12-14 22:28:14 +0800209
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);
214
215 if (!req) {
216 async_chainiv_schedule_work(ctx);
217 return;
218 }
219
220 subreq = skcipher_givcrypt_reqctx(req);
221 subreq->base.flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
222
Herbert Xu872ac872008-07-10 17:42:36 +0800223 err = async_chainiv_givencrypt_tail(req);
224
225 local_bh_disable();
226 skcipher_givcrypt_complete(req, err);
227 local_bh_enable();
Herbert Xue7cd2512007-12-14 22:28:14 +0800228}
229
230static int async_chainiv_init(struct crypto_tfm *tfm)
231{
Herbert Xu341476d2015-06-21 19:11:46 +0800232 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
Herbert Xue7cd2512007-12-14 22:28:14 +0800233 struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu341476d2015-06-21 19:11:46 +0800234 char *iv;
Herbert Xue7cd2512007-12-14 22:28:14 +0800235
236 spin_lock_init(&ctx->lock);
237
238 crypto_init_queue(&ctx->queue, 100);
239 INIT_WORK(&ctx->postponed, async_chainiv_do_postponed);
240
Herbert Xu341476d2015-06-21 19:11:46 +0800241 iv = NULL;
242 if (!crypto_get_default_rng()) {
243 crypto_ablkcipher_crt(geniv)->givencrypt =
244 async_chainiv_givencrypt;
245 iv = ctx->iv;
246 }
247
248 return chainiv_init_common(tfm, iv);
Herbert Xue7cd2512007-12-14 22:28:14 +0800249}
250
251static void async_chainiv_exit(struct crypto_tfm *tfm)
252{
253 struct async_chainiv_ctx *ctx = crypto_tfm_ctx(tfm);
254
255 BUG_ON(test_bit(CHAINIV_STATE_INUSE, &ctx->state) || ctx->queue.qlen);
256
257 skcipher_geniv_exit(tfm);
258}
259
Herbert Xu7f470732007-11-27 23:17:23 +0800260static struct crypto_template chainiv_tmpl;
261
262static struct crypto_instance *chainiv_alloc(struct rtattr **tb)
263{
Herbert Xue7cd2512007-12-14 22:28:14 +0800264 struct crypto_attr_type *algt;
Herbert Xu7f470732007-11-27 23:17:23 +0800265 struct crypto_instance *inst;
266
Herbert Xue7cd2512007-12-14 22:28:14 +0800267 algt = crypto_get_attr_type(tb);
Herbert Xue7cd2512007-12-14 22:28:14 +0800268 if (IS_ERR(algt))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100269 return ERR_CAST(algt);
Herbert Xue7cd2512007-12-14 22:28:14 +0800270
271 inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0, 0);
Herbert Xu7f470732007-11-27 23:17:23 +0800272 if (IS_ERR(inst))
Herbert Xu341476d2015-06-21 19:11:46 +0800273 goto out;
Herbert Xu7f470732007-11-27 23:17:23 +0800274
275 inst->alg.cra_init = chainiv_init;
276 inst->alg.cra_exit = skcipher_geniv_exit;
277
Herbert Xue7cd2512007-12-14 22:28:14 +0800278 inst->alg.cra_ctxsize = sizeof(struct chainiv_ctx);
279
280 if (!crypto_requires_sync(algt->type, algt->mask)) {
281 inst->alg.cra_flags |= CRYPTO_ALG_ASYNC;
282
Herbert Xue7cd2512007-12-14 22:28:14 +0800283 inst->alg.cra_init = async_chainiv_init;
284 inst->alg.cra_exit = async_chainiv_exit;
285
286 inst->alg.cra_ctxsize = sizeof(struct async_chainiv_ctx);
287 }
288
289 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
Herbert Xu7f470732007-11-27 23:17:23 +0800290
291out:
292 return inst;
293}
294
295static struct crypto_template chainiv_tmpl = {
296 .name = "chainiv",
297 .alloc = chainiv_alloc,
Herbert Xu341476d2015-06-21 19:11:46 +0800298 .free = skcipher_geniv_free,
Herbert Xu7f470732007-11-27 23:17:23 +0800299 .module = THIS_MODULE,
300};
301
Herbert Xu5be5e662008-08-17 18:04:30 +1000302static int __init chainiv_module_init(void)
Herbert Xu7f470732007-11-27 23:17:23 +0800303{
304 return crypto_register_template(&chainiv_tmpl);
305}
306
Herbert Xu5be5e662008-08-17 18:04:30 +1000307static void chainiv_module_exit(void)
Herbert Xu7f470732007-11-27 23:17:23 +0800308{
309 crypto_unregister_template(&chainiv_tmpl);
310}
Herbert Xu5be5e662008-08-17 18:04:30 +1000311
312module_init(chainiv_module_init);
313module_exit(chainiv_module_exit);
314
315MODULE_LICENSE("GPL");
316MODULE_DESCRIPTION("Chain IV Generator");
Kees Cook4943ba12014-11-24 16:32:38 -0800317MODULE_ALIAS_CRYPTO("chainiv");