• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <crypto/rng.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/spinlock.h>
23 #include <linux/string.h>
24 #include <linux/workqueue.h>
25 
26 enum {
27 	CHAINIV_STATE_INUSE = 0,
28 };
29 
30 struct chainiv_ctx {
31 	spinlock_t lock;
32 	char iv[];
33 };
34 
35 struct 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 
chainiv_givencrypt(struct skcipher_givcrypt_request * req)47 static 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 
76 unlock:
77 	spin_unlock_bh(&ctx->lock);
78 
79 	return err;
80 }
81 
chainiv_givencrypt_first(struct skcipher_givcrypt_request * req)82 static 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 	int err = 0;
87 
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;
94 	err = crypto_rng_get_bytes(crypto_default_rng, ctx->iv,
95 				   crypto_ablkcipher_ivsize(geniv));
96 
97 unlock:
98 	spin_unlock_bh(&ctx->lock);
99 
100 	if (err)
101 		return err;
102 
103 	return chainiv_givencrypt(req);
104 }
105 
chainiv_init_common(struct crypto_tfm * tfm)106 static int chainiv_init_common(struct crypto_tfm *tfm)
107 {
108 	tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
109 
110 	return skcipher_geniv_init(tfm);
111 }
112 
chainiv_init(struct crypto_tfm * tfm)113 static 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 
async_chainiv_schedule_work(struct async_chainiv_ctx * ctx)122 static int async_chainiv_schedule_work(struct async_chainiv_ctx *ctx)
123 {
124 	int queued;
125 	int err = ctx->err;
126 
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 
139 out:
140 	return err;
141 }
142 
async_chainiv_postpone_request(struct skcipher_givcrypt_request * req)143 static 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 
async_chainiv_givencrypt_tail(struct skcipher_givcrypt_request * req)160 static 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 
176 out:
177 	return async_chainiv_schedule_work(ctx);
178 }
179 
async_chainiv_givencrypt(struct skcipher_givcrypt_request * req)180 static 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 
203 postpone:
204 	return async_chainiv_postpone_request(req);
205 }
206 
async_chainiv_givencrypt_first(struct skcipher_givcrypt_request * req)207 static 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);
211 	int err = 0;
212 
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;
221 	err = crypto_rng_get_bytes(crypto_default_rng, ctx->iv,
222 				   crypto_ablkcipher_ivsize(geniv));
223 
224 unlock:
225 	clear_bit(CHAINIV_STATE_INUSE, &ctx->state);
226 
227 	if (err)
228 		return err;
229 
230 out:
231 	return async_chainiv_givencrypt(req);
232 }
233 
async_chainiv_do_postponed(struct work_struct * work)234 static 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;
241 	int err;
242 
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 
256 	err = async_chainiv_givencrypt_tail(req);
257 
258 	local_bh_disable();
259 	skcipher_givcrypt_complete(req, err);
260 	local_bh_enable();
261 }
262 
async_chainiv_init(struct crypto_tfm * tfm)263 static 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 
async_chainiv_exit(struct crypto_tfm * tfm)275 static 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 
284 static struct crypto_template chainiv_tmpl;
285 
chainiv_alloc(struct rtattr ** tb)286 static struct crypto_instance *chainiv_alloc(struct rtattr **tb)
287 {
288 	struct crypto_attr_type *algt;
289 	struct crypto_instance *inst;
290 	int err;
291 
292 	algt = crypto_get_attr_type(tb);
293 	err = PTR_ERR(algt);
294 	if (IS_ERR(algt))
295 		return ERR_PTR(err);
296 
297 	err = crypto_get_default_rng();
298 	if (err)
299 		return ERR_PTR(err);
300 
301 	inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0, 0);
302 	if (IS_ERR(inst))
303 		goto put_rng;
304 
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 
310 	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;
325 
326 out:
327 	return inst;
328 
329 put_rng:
330 	crypto_put_default_rng();
331 	goto out;
332 }
333 
chainiv_free(struct crypto_instance * inst)334 static void chainiv_free(struct crypto_instance *inst)
335 {
336 	skcipher_geniv_free(inst);
337 	crypto_put_default_rng();
338 }
339 
340 static struct crypto_template chainiv_tmpl = {
341 	.name = "chainiv",
342 	.alloc = chainiv_alloc,
343 	.free = chainiv_free,
344 	.module = THIS_MODULE,
345 };
346 
chainiv_module_init(void)347 static int __init chainiv_module_init(void)
348 {
349 	return crypto_register_template(&chainiv_tmpl);
350 }
351 
chainiv_module_exit(void)352 static void chainiv_module_exit(void)
353 {
354 	crypto_unregister_template(&chainiv_tmpl);
355 }
356 
357 module_init(chainiv_module_init);
358 module_exit(chainiv_module_exit);
359 
360 MODULE_LICENSE("GPL");
361 MODULE_DESCRIPTION("Chain IV Generator");
362