blob: c9662e25595eb0fb6e3f3e1c7a1d02662d65b413 [file] [log] [blame]
Steffen Klassert5068c7a2010-01-07 15:57:19 +11001/*
2 * pcrypt - Parallel crypto wrapper.
3 *
4 * Copyright (C) 2009 secunet Security Networks AG
5 * Copyright (C) 2009 Steffen Klassert <steffen.klassert@secunet.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include <crypto/algapi.h>
22#include <crypto/internal/aead.h>
23#include <linux/err.h>
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/slab.h>
Dan Kruchinine15bacb2010-07-14 14:31:57 +040027#include <linux/notifier.h>
Steffen Klassert5068c7a2010-01-07 15:57:19 +110028#include <crypto/pcrypt.h>
29
Dan Kruchinine15bacb2010-07-14 14:31:57 +040030struct pcrypt_instance {
31 struct padata_instance *pinst;
32 struct workqueue_struct *wq;
33
34 /*
35 * Cpumask for callback CPUs. It should be
36 * equal to serial cpumask of corresponding padata instance,
37 * so it is updated when padata notifies us about serial
38 * cpumask change.
39 *
40 * cb_cpumask is protected by RCU. This fact prevents us from
41 * using cpumask_var_t directly because the actual type of
42 * cpumsak_var_t depends on kernel configuration(particularly on
43 * CONFIG_CPUMASK_OFFSTACK macro). Depending on the configuration
44 * cpumask_var_t may be either a pointer to the struct cpumask
45 * or a variable allocated on the stack. Thus we can not safely use
46 * cpumask_var_t with RCU operations such as rcu_assign_pointer or
47 * rcu_dereference. So cpumask_var_t is wrapped with struct
48 * pcrypt_cpumask which makes possible to use it with RCU.
49 */
50 struct pcrypt_cpumask {
51 cpumask_var_t mask;
52 } *cb_cpumask;
53 struct notifier_block nblock;
54};
55
56static struct pcrypt_instance pencrypt;
57static struct pcrypt_instance pdecrypt;
58
Steffen Klassert5068c7a2010-01-07 15:57:19 +110059
60struct pcrypt_instance_ctx {
61 struct crypto_spawn spawn;
62 unsigned int tfm_count;
63};
64
65struct pcrypt_aead_ctx {
66 struct crypto_aead *child;
67 unsigned int cb_cpu;
68};
69
70static int pcrypt_do_parallel(struct padata_priv *padata, unsigned int *cb_cpu,
Dan Kruchinine15bacb2010-07-14 14:31:57 +040071 struct pcrypt_instance *pcrypt)
Steffen Klassert5068c7a2010-01-07 15:57:19 +110072{
73 unsigned int cpu_index, cpu, i;
Dan Kruchinine15bacb2010-07-14 14:31:57 +040074 struct pcrypt_cpumask *cpumask;
Steffen Klassert5068c7a2010-01-07 15:57:19 +110075
76 cpu = *cb_cpu;
77
Dan Kruchinine15bacb2010-07-14 14:31:57 +040078 rcu_read_lock_bh();
79 cpumask = rcu_dereference(pcrypt->cb_cpumask);
80 if (cpumask_test_cpu(cpu, cpumask->mask))
Steffen Klassert5068c7a2010-01-07 15:57:19 +110081 goto out;
82
Dan Kruchinine15bacb2010-07-14 14:31:57 +040083 cpu_index = cpu % cpumask_weight(cpumask->mask);
Steffen Klassert5068c7a2010-01-07 15:57:19 +110084
Dan Kruchinine15bacb2010-07-14 14:31:57 +040085 cpu = cpumask_first(cpumask->mask);
Steffen Klassert5068c7a2010-01-07 15:57:19 +110086 for (i = 0; i < cpu_index; i++)
Dan Kruchinine15bacb2010-07-14 14:31:57 +040087 cpu = cpumask_next(cpu, cpumask->mask);
Steffen Klassert5068c7a2010-01-07 15:57:19 +110088
89 *cb_cpu = cpu;
90
91out:
Dan Kruchinine15bacb2010-07-14 14:31:57 +040092 rcu_read_unlock_bh();
93 return padata_do_parallel(pcrypt->pinst, padata, cpu);
Steffen Klassert5068c7a2010-01-07 15:57:19 +110094}
95
96static int pcrypt_aead_setkey(struct crypto_aead *parent,
97 const u8 *key, unsigned int keylen)
98{
99 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
100
101 return crypto_aead_setkey(ctx->child, key, keylen);
102}
103
104static int pcrypt_aead_setauthsize(struct crypto_aead *parent,
105 unsigned int authsize)
106{
107 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
108
109 return crypto_aead_setauthsize(ctx->child, authsize);
110}
111
112static void pcrypt_aead_serial(struct padata_priv *padata)
113{
114 struct pcrypt_request *preq = pcrypt_padata_request(padata);
115 struct aead_request *req = pcrypt_request_ctx(preq);
116
117 aead_request_complete(req->base.data, padata->info);
118}
119
120static void pcrypt_aead_giv_serial(struct padata_priv *padata)
121{
122 struct pcrypt_request *preq = pcrypt_padata_request(padata);
123 struct aead_givcrypt_request *req = pcrypt_request_ctx(preq);
124
125 aead_request_complete(req->areq.base.data, padata->info);
126}
127
128static void pcrypt_aead_done(struct crypto_async_request *areq, int err)
129{
130 struct aead_request *req = areq->data;
131 struct pcrypt_request *preq = aead_request_ctx(req);
132 struct padata_priv *padata = pcrypt_request_padata(preq);
133
134 padata->info = err;
135 req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
136
137 padata_do_serial(padata);
138}
139
140static void pcrypt_aead_enc(struct padata_priv *padata)
141{
142 struct pcrypt_request *preq = pcrypt_padata_request(padata);
143 struct aead_request *req = pcrypt_request_ctx(preq);
144
145 padata->info = crypto_aead_encrypt(req);
146
Steffen Klassert5a1436b2010-02-04 11:40:17 +1100147 if (padata->info == -EINPROGRESS)
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100148 return;
149
150 padata_do_serial(padata);
151}
152
153static int pcrypt_aead_encrypt(struct aead_request *req)
154{
155 int err;
156 struct pcrypt_request *preq = aead_request_ctx(req);
157 struct aead_request *creq = pcrypt_request_ctx(preq);
158 struct padata_priv *padata = pcrypt_request_padata(preq);
159 struct crypto_aead *aead = crypto_aead_reqtfm(req);
160 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
161 u32 flags = aead_request_flags(req);
162
163 memset(padata, 0, sizeof(struct padata_priv));
164
165 padata->parallel = pcrypt_aead_enc;
166 padata->serial = pcrypt_aead_serial;
167
168 aead_request_set_tfm(creq, ctx->child);
169 aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
170 pcrypt_aead_done, req);
171 aead_request_set_crypt(creq, req->src, req->dst,
172 req->cryptlen, req->iv);
173 aead_request_set_assoc(creq, req->assoc, req->assoclen);
174
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400175 err = pcrypt_do_parallel(padata, &ctx->cb_cpu, &pencrypt);
Steffen Klassert83f619f2010-07-07 15:32:02 +0200176 if (!err)
177 return -EINPROGRESS;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100178
179 return err;
180}
181
182static void pcrypt_aead_dec(struct padata_priv *padata)
183{
184 struct pcrypt_request *preq = pcrypt_padata_request(padata);
185 struct aead_request *req = pcrypt_request_ctx(preq);
186
187 padata->info = crypto_aead_decrypt(req);
188
Steffen Klassert5a1436b2010-02-04 11:40:17 +1100189 if (padata->info == -EINPROGRESS)
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100190 return;
191
192 padata_do_serial(padata);
193}
194
195static int pcrypt_aead_decrypt(struct aead_request *req)
196{
197 int err;
198 struct pcrypt_request *preq = aead_request_ctx(req);
199 struct aead_request *creq = pcrypt_request_ctx(preq);
200 struct padata_priv *padata = pcrypt_request_padata(preq);
201 struct crypto_aead *aead = crypto_aead_reqtfm(req);
202 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
203 u32 flags = aead_request_flags(req);
204
205 memset(padata, 0, sizeof(struct padata_priv));
206
207 padata->parallel = pcrypt_aead_dec;
208 padata->serial = pcrypt_aead_serial;
209
210 aead_request_set_tfm(creq, ctx->child);
211 aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
212 pcrypt_aead_done, req);
213 aead_request_set_crypt(creq, req->src, req->dst,
214 req->cryptlen, req->iv);
215 aead_request_set_assoc(creq, req->assoc, req->assoclen);
216
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400217 err = pcrypt_do_parallel(padata, &ctx->cb_cpu, &pdecrypt);
Steffen Klassert83f619f2010-07-07 15:32:02 +0200218 if (!err)
219 return -EINPROGRESS;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100220
221 return err;
222}
223
224static void pcrypt_aead_givenc(struct padata_priv *padata)
225{
226 struct pcrypt_request *preq = pcrypt_padata_request(padata);
227 struct aead_givcrypt_request *req = pcrypt_request_ctx(preq);
228
229 padata->info = crypto_aead_givencrypt(req);
230
Steffen Klassert5a1436b2010-02-04 11:40:17 +1100231 if (padata->info == -EINPROGRESS)
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100232 return;
233
234 padata_do_serial(padata);
235}
236
237static int pcrypt_aead_givencrypt(struct aead_givcrypt_request *req)
238{
239 int err;
240 struct aead_request *areq = &req->areq;
241 struct pcrypt_request *preq = aead_request_ctx(areq);
242 struct aead_givcrypt_request *creq = pcrypt_request_ctx(preq);
243 struct padata_priv *padata = pcrypt_request_padata(preq);
244 struct crypto_aead *aead = aead_givcrypt_reqtfm(req);
245 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
246 u32 flags = aead_request_flags(areq);
247
248 memset(padata, 0, sizeof(struct padata_priv));
249
250 padata->parallel = pcrypt_aead_givenc;
251 padata->serial = pcrypt_aead_giv_serial;
252
253 aead_givcrypt_set_tfm(creq, ctx->child);
254 aead_givcrypt_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
255 pcrypt_aead_done, areq);
256 aead_givcrypt_set_crypt(creq, areq->src, areq->dst,
257 areq->cryptlen, areq->iv);
258 aead_givcrypt_set_assoc(creq, areq->assoc, areq->assoclen);
259 aead_givcrypt_set_giv(creq, req->giv, req->seq);
260
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400261 err = pcrypt_do_parallel(padata, &ctx->cb_cpu, &pencrypt);
Steffen Klassert83f619f2010-07-07 15:32:02 +0200262 if (!err)
263 return -EINPROGRESS;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100264
265 return err;
266}
267
268static int pcrypt_aead_init_tfm(struct crypto_tfm *tfm)
269{
270 int cpu, cpu_index;
271 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
272 struct pcrypt_instance_ctx *ictx = crypto_instance_ctx(inst);
273 struct pcrypt_aead_ctx *ctx = crypto_tfm_ctx(tfm);
274 struct crypto_aead *cipher;
275
276 ictx->tfm_count++;
277
278 cpu_index = ictx->tfm_count % cpumask_weight(cpu_active_mask);
279
280 ctx->cb_cpu = cpumask_first(cpu_active_mask);
281 for (cpu = 0; cpu < cpu_index; cpu++)
282 ctx->cb_cpu = cpumask_next(ctx->cb_cpu, cpu_active_mask);
283
284 cipher = crypto_spawn_aead(crypto_instance_ctx(inst));
285
286 if (IS_ERR(cipher))
287 return PTR_ERR(cipher);
288
289 ctx->child = cipher;
290 tfm->crt_aead.reqsize = sizeof(struct pcrypt_request)
291 + sizeof(struct aead_givcrypt_request)
292 + crypto_aead_reqsize(cipher);
293
294 return 0;
295}
296
297static void pcrypt_aead_exit_tfm(struct crypto_tfm *tfm)
298{
299 struct pcrypt_aead_ctx *ctx = crypto_tfm_ctx(tfm);
300
301 crypto_free_aead(ctx->child);
302}
303
304static struct crypto_instance *pcrypt_alloc_instance(struct crypto_alg *alg)
305{
306 struct crypto_instance *inst;
307 struct pcrypt_instance_ctx *ctx;
308 int err;
309
310 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
311 if (!inst) {
312 inst = ERR_PTR(-ENOMEM);
313 goto out;
314 }
315
316 err = -ENAMETOOLONG;
317 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
318 "pcrypt(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
319 goto out_free_inst;
320
321 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
322
323 ctx = crypto_instance_ctx(inst);
324 err = crypto_init_spawn(&ctx->spawn, alg, inst,
325 CRYPTO_ALG_TYPE_MASK);
326 if (err)
327 goto out_free_inst;
328
329 inst->alg.cra_priority = alg->cra_priority + 100;
330 inst->alg.cra_blocksize = alg->cra_blocksize;
331 inst->alg.cra_alignmask = alg->cra_alignmask;
332
333out:
334 return inst;
335
336out_free_inst:
337 kfree(inst);
338 inst = ERR_PTR(err);
339 goto out;
340}
341
Dan Carpenter80a6d7d2010-03-24 21:35:23 +0800342static struct crypto_instance *pcrypt_alloc_aead(struct rtattr **tb,
343 u32 type, u32 mask)
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100344{
345 struct crypto_instance *inst;
346 struct crypto_alg *alg;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100347
Dan Carpenter80a6d7d2010-03-24 21:35:23 +0800348 alg = crypto_get_attr_alg(tb, type, (mask & CRYPTO_ALG_TYPE_MASK));
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100349 if (IS_ERR(alg))
350 return ERR_CAST(alg);
351
352 inst = pcrypt_alloc_instance(alg);
353 if (IS_ERR(inst))
354 goto out_put_alg;
355
356 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
357 inst->alg.cra_type = &crypto_aead_type;
358
359 inst->alg.cra_aead.ivsize = alg->cra_aead.ivsize;
360 inst->alg.cra_aead.geniv = alg->cra_aead.geniv;
361 inst->alg.cra_aead.maxauthsize = alg->cra_aead.maxauthsize;
362
363 inst->alg.cra_ctxsize = sizeof(struct pcrypt_aead_ctx);
364
365 inst->alg.cra_init = pcrypt_aead_init_tfm;
366 inst->alg.cra_exit = pcrypt_aead_exit_tfm;
367
368 inst->alg.cra_aead.setkey = pcrypt_aead_setkey;
369 inst->alg.cra_aead.setauthsize = pcrypt_aead_setauthsize;
370 inst->alg.cra_aead.encrypt = pcrypt_aead_encrypt;
371 inst->alg.cra_aead.decrypt = pcrypt_aead_decrypt;
372 inst->alg.cra_aead.givencrypt = pcrypt_aead_givencrypt;
373
374out_put_alg:
375 crypto_mod_put(alg);
376 return inst;
377}
378
379static struct crypto_instance *pcrypt_alloc(struct rtattr **tb)
380{
381 struct crypto_attr_type *algt;
382
383 algt = crypto_get_attr_type(tb);
384 if (IS_ERR(algt))
385 return ERR_CAST(algt);
386
387 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
388 case CRYPTO_ALG_TYPE_AEAD:
Dan Carpenter80a6d7d2010-03-24 21:35:23 +0800389 return pcrypt_alloc_aead(tb, algt->type, algt->mask);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100390 }
391
392 return ERR_PTR(-EINVAL);
393}
394
395static void pcrypt_free(struct crypto_instance *inst)
396{
397 struct pcrypt_instance_ctx *ctx = crypto_instance_ctx(inst);
398
399 crypto_drop_spawn(&ctx->spawn);
400 kfree(inst);
401}
402
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400403static int pcrypt_cpumask_change_notify(struct notifier_block *self,
404 unsigned long val, void *data)
405{
406 struct pcrypt_instance *pcrypt;
407 struct pcrypt_cpumask *new_mask, *old_mask;
408
409 if (!(val & PADATA_CPU_SERIAL))
410 return 0;
411
412 pcrypt = container_of(self, struct pcrypt_instance, nblock);
413 new_mask = kmalloc(sizeof(*new_mask), GFP_KERNEL);
414 if (!new_mask)
415 return -ENOMEM;
416 if (!alloc_cpumask_var(&new_mask->mask, GFP_KERNEL)) {
417 kfree(new_mask);
418 return -ENOMEM;
419 }
420
421 old_mask = pcrypt->cb_cpumask;
422
423 padata_get_cpumask(pcrypt->pinst, PADATA_CPU_SERIAL, new_mask->mask);
424 rcu_assign_pointer(pcrypt->cb_cpumask, new_mask);
425 synchronize_rcu_bh();
426
427 free_cpumask_var(old_mask->mask);
428 kfree(old_mask);
429 return 0;
430}
431
432static int __pcrypt_init_instance(struct pcrypt_instance *pcrypt,
433 const char *name)
434{
435 int ret = -ENOMEM;
436 struct pcrypt_cpumask *mask;
437
438 pcrypt->wq = create_workqueue(name);
439 if (!pcrypt->wq)
440 goto err;
441
442 pcrypt->pinst = padata_alloc(pcrypt->wq);
443 if (!pcrypt->pinst)
444 goto err_destroy_workqueue;
445
446 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
447 if (!mask)
448 goto err_free_padata;
449 if (!alloc_cpumask_var(&mask->mask, GFP_KERNEL)) {
450 kfree(mask);
451 goto err_free_padata;
452 }
453
454 padata_get_cpumask(pcrypt->pinst, PADATA_CPU_SERIAL, mask->mask);
455 rcu_assign_pointer(pcrypt->cb_cpumask, mask);
456
457 pcrypt->nblock.notifier_call = pcrypt_cpumask_change_notify;
458 ret = padata_register_cpumask_notifier(pcrypt->pinst, &pcrypt->nblock);
459 if (ret)
460 goto err_free_cpumask;
461
462 return ret;
463err_free_cpumask:
464 free_cpumask_var(mask->mask);
465 kfree(mask);
466err_free_padata:
467 padata_free(pcrypt->pinst);
468err_destroy_workqueue:
469 destroy_workqueue(pcrypt->wq);
470err:
471 return ret;
472}
473
474static void __pcrypt_deinit_instance(struct pcrypt_instance *pcrypt)
475{
476 free_cpumask_var(pcrypt->cb_cpumask->mask);
477 kfree(pcrypt->cb_cpumask);
478
479 padata_stop(pcrypt->pinst);
480 padata_unregister_cpumask_notifier(pcrypt->pinst, &pcrypt->nblock);
481 destroy_workqueue(pcrypt->wq);
482 padata_free(pcrypt->pinst);
483}
484
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100485static struct crypto_template pcrypt_tmpl = {
486 .name = "pcrypt",
487 .alloc = pcrypt_alloc,
488 .free = pcrypt_free,
489 .module = THIS_MODULE,
490};
491
492static int __init pcrypt_init(void)
493{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400494 int err;
495
496 err = __pcrypt_init_instance(&pencrypt, "pencrypt");
497 if (err)
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100498 goto err;
499
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400500 err = __pcrypt_init_instance(&pdecrypt, "pdecrypt");
Steffen Klassert4c879172010-07-07 15:30:10 +0200501 if (err)
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400502 goto err_deinit_pencrypt;
Steffen Klassert4c879172010-07-07 15:30:10 +0200503
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400504 padata_start(pencrypt.pinst);
505 padata_start(pdecrypt.pinst);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100506
507 return crypto_register_template(&pcrypt_tmpl);
508
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400509err_deinit_pencrypt:
510 __pcrypt_deinit_instance(&pencrypt);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100511err:
Steffen Klassert4c879172010-07-07 15:30:10 +0200512 return err;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100513}
514
515static void __exit pcrypt_exit(void)
516{
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400517 __pcrypt_deinit_instance(&pencrypt);
518 __pcrypt_deinit_instance(&pdecrypt);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100519
520 crypto_unregister_template(&pcrypt_tmpl);
521}
522
523module_init(pcrypt_init);
524module_exit(pcrypt_exit);
525
526MODULE_LICENSE("GPL");
527MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
528MODULE_DESCRIPTION("Parallel crypto wrapper");