spi: sprd: adi: Change hwlock to be optional
[linux/fpc-iii.git] / crypto / pcrypt.c
blob0edf5b54fc778d6f2f159821496f8bac267bea50
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * pcrypt - Parallel crypto wrapper.
5 * Copyright (C) 2009 secunet Security Networks AG
6 * Copyright (C) 2009 Steffen Klassert <steffen.klassert@secunet.com>
7 */
9 #include <crypto/algapi.h>
10 #include <crypto/internal/aead.h>
11 #include <linux/atomic.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/notifier.h>
17 #include <linux/kobject.h>
18 #include <linux/cpu.h>
19 #include <crypto/pcrypt.h>
21 struct padata_pcrypt {
22 struct padata_instance *pinst;
23 struct workqueue_struct *wq;
26 * Cpumask for callback CPUs. It should be
27 * equal to serial cpumask of corresponding padata instance,
28 * so it is updated when padata notifies us about serial
29 * cpumask change.
31 * cb_cpumask is protected by RCU. This fact prevents us from
32 * using cpumask_var_t directly because the actual type of
33 * cpumsak_var_t depends on kernel configuration(particularly on
34 * CONFIG_CPUMASK_OFFSTACK macro). Depending on the configuration
35 * cpumask_var_t may be either a pointer to the struct cpumask
36 * or a variable allocated on the stack. Thus we can not safely use
37 * cpumask_var_t with RCU operations such as rcu_assign_pointer or
38 * rcu_dereference. So cpumask_var_t is wrapped with struct
39 * pcrypt_cpumask which makes possible to use it with RCU.
41 struct pcrypt_cpumask {
42 cpumask_var_t mask;
43 } *cb_cpumask;
44 struct notifier_block nblock;
47 static struct padata_pcrypt pencrypt;
48 static struct padata_pcrypt pdecrypt;
49 static struct kset *pcrypt_kset;
51 struct pcrypt_instance_ctx {
52 struct crypto_aead_spawn spawn;
53 atomic_t tfm_count;
56 struct pcrypt_aead_ctx {
57 struct crypto_aead *child;
58 unsigned int cb_cpu;
61 static int pcrypt_do_parallel(struct padata_priv *padata, unsigned int *cb_cpu,
62 struct padata_pcrypt *pcrypt)
64 unsigned int cpu_index, cpu, i;
65 struct pcrypt_cpumask *cpumask;
67 cpu = *cb_cpu;
69 rcu_read_lock_bh();
70 cpumask = rcu_dereference_bh(pcrypt->cb_cpumask);
71 if (cpumask_test_cpu(cpu, cpumask->mask))
72 goto out;
74 if (!cpumask_weight(cpumask->mask))
75 goto out;
77 cpu_index = cpu % cpumask_weight(cpumask->mask);
79 cpu = cpumask_first(cpumask->mask);
80 for (i = 0; i < cpu_index; i++)
81 cpu = cpumask_next(cpu, cpumask->mask);
83 *cb_cpu = cpu;
85 out:
86 rcu_read_unlock_bh();
87 return padata_do_parallel(pcrypt->pinst, padata, cpu);
90 static int pcrypt_aead_setkey(struct crypto_aead *parent,
91 const u8 *key, unsigned int keylen)
93 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
95 return crypto_aead_setkey(ctx->child, key, keylen);
98 static int pcrypt_aead_setauthsize(struct crypto_aead *parent,
99 unsigned int authsize)
101 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
103 return crypto_aead_setauthsize(ctx->child, authsize);
106 static void pcrypt_aead_serial(struct padata_priv *padata)
108 struct pcrypt_request *preq = pcrypt_padata_request(padata);
109 struct aead_request *req = pcrypt_request_ctx(preq);
111 aead_request_complete(req->base.data, padata->info);
114 static void pcrypt_aead_done(struct crypto_async_request *areq, int err)
116 struct aead_request *req = areq->data;
117 struct pcrypt_request *preq = aead_request_ctx(req);
118 struct padata_priv *padata = pcrypt_request_padata(preq);
120 padata->info = err;
121 req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
123 padata_do_serial(padata);
126 static void pcrypt_aead_enc(struct padata_priv *padata)
128 struct pcrypt_request *preq = pcrypt_padata_request(padata);
129 struct aead_request *req = pcrypt_request_ctx(preq);
131 padata->info = crypto_aead_encrypt(req);
133 if (padata->info == -EINPROGRESS)
134 return;
136 padata_do_serial(padata);
139 static int pcrypt_aead_encrypt(struct aead_request *req)
141 int err;
142 struct pcrypt_request *preq = aead_request_ctx(req);
143 struct aead_request *creq = pcrypt_request_ctx(preq);
144 struct padata_priv *padata = pcrypt_request_padata(preq);
145 struct crypto_aead *aead = crypto_aead_reqtfm(req);
146 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
147 u32 flags = aead_request_flags(req);
149 memset(padata, 0, sizeof(struct padata_priv));
151 padata->parallel = pcrypt_aead_enc;
152 padata->serial = pcrypt_aead_serial;
154 aead_request_set_tfm(creq, ctx->child);
155 aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
156 pcrypt_aead_done, req);
157 aead_request_set_crypt(creq, req->src, req->dst,
158 req->cryptlen, req->iv);
159 aead_request_set_ad(creq, req->assoclen);
161 err = pcrypt_do_parallel(padata, &ctx->cb_cpu, &pencrypt);
162 if (!err)
163 return -EINPROGRESS;
165 return err;
168 static void pcrypt_aead_dec(struct padata_priv *padata)
170 struct pcrypt_request *preq = pcrypt_padata_request(padata);
171 struct aead_request *req = pcrypt_request_ctx(preq);
173 padata->info = crypto_aead_decrypt(req);
175 if (padata->info == -EINPROGRESS)
176 return;
178 padata_do_serial(padata);
181 static int pcrypt_aead_decrypt(struct aead_request *req)
183 int err;
184 struct pcrypt_request *preq = aead_request_ctx(req);
185 struct aead_request *creq = pcrypt_request_ctx(preq);
186 struct padata_priv *padata = pcrypt_request_padata(preq);
187 struct crypto_aead *aead = crypto_aead_reqtfm(req);
188 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
189 u32 flags = aead_request_flags(req);
191 memset(padata, 0, sizeof(struct padata_priv));
193 padata->parallel = pcrypt_aead_dec;
194 padata->serial = pcrypt_aead_serial;
196 aead_request_set_tfm(creq, ctx->child);
197 aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
198 pcrypt_aead_done, req);
199 aead_request_set_crypt(creq, req->src, req->dst,
200 req->cryptlen, req->iv);
201 aead_request_set_ad(creq, req->assoclen);
203 err = pcrypt_do_parallel(padata, &ctx->cb_cpu, &pdecrypt);
204 if (!err)
205 return -EINPROGRESS;
207 return err;
210 static int pcrypt_aead_init_tfm(struct crypto_aead *tfm)
212 int cpu, cpu_index;
213 struct aead_instance *inst = aead_alg_instance(tfm);
214 struct pcrypt_instance_ctx *ictx = aead_instance_ctx(inst);
215 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm);
216 struct crypto_aead *cipher;
218 cpu_index = (unsigned int)atomic_inc_return(&ictx->tfm_count) %
219 cpumask_weight(cpu_online_mask);
221 ctx->cb_cpu = cpumask_first(cpu_online_mask);
222 for (cpu = 0; cpu < cpu_index; cpu++)
223 ctx->cb_cpu = cpumask_next(ctx->cb_cpu, cpu_online_mask);
225 cipher = crypto_spawn_aead(&ictx->spawn);
227 if (IS_ERR(cipher))
228 return PTR_ERR(cipher);
230 ctx->child = cipher;
231 crypto_aead_set_reqsize(tfm, sizeof(struct pcrypt_request) +
232 sizeof(struct aead_request) +
233 crypto_aead_reqsize(cipher));
235 return 0;
238 static void pcrypt_aead_exit_tfm(struct crypto_aead *tfm)
240 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm);
242 crypto_free_aead(ctx->child);
245 static void pcrypt_free(struct aead_instance *inst)
247 struct pcrypt_instance_ctx *ctx = aead_instance_ctx(inst);
249 crypto_drop_aead(&ctx->spawn);
250 kfree(inst);
253 static int pcrypt_init_instance(struct crypto_instance *inst,
254 struct crypto_alg *alg)
256 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
257 "pcrypt(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
258 return -ENAMETOOLONG;
260 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
262 inst->alg.cra_priority = alg->cra_priority + 100;
263 inst->alg.cra_blocksize = alg->cra_blocksize;
264 inst->alg.cra_alignmask = alg->cra_alignmask;
266 return 0;
269 static int pcrypt_create_aead(struct crypto_template *tmpl, struct rtattr **tb,
270 u32 type, u32 mask)
272 struct pcrypt_instance_ctx *ctx;
273 struct crypto_attr_type *algt;
274 struct aead_instance *inst;
275 struct aead_alg *alg;
276 const char *name;
277 int err;
279 algt = crypto_get_attr_type(tb);
280 if (IS_ERR(algt))
281 return PTR_ERR(algt);
283 name = crypto_attr_alg_name(tb[1]);
284 if (IS_ERR(name))
285 return PTR_ERR(name);
287 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
288 if (!inst)
289 return -ENOMEM;
291 ctx = aead_instance_ctx(inst);
292 crypto_set_aead_spawn(&ctx->spawn, aead_crypto_instance(inst));
294 err = crypto_grab_aead(&ctx->spawn, name, 0, 0);
295 if (err)
296 goto out_free_inst;
298 alg = crypto_spawn_aead_alg(&ctx->spawn);
299 err = pcrypt_init_instance(aead_crypto_instance(inst), &alg->base);
300 if (err)
301 goto out_drop_aead;
303 inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC;
305 inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
306 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
308 inst->alg.base.cra_ctxsize = sizeof(struct pcrypt_aead_ctx);
310 inst->alg.init = pcrypt_aead_init_tfm;
311 inst->alg.exit = pcrypt_aead_exit_tfm;
313 inst->alg.setkey = pcrypt_aead_setkey;
314 inst->alg.setauthsize = pcrypt_aead_setauthsize;
315 inst->alg.encrypt = pcrypt_aead_encrypt;
316 inst->alg.decrypt = pcrypt_aead_decrypt;
318 inst->free = pcrypt_free;
320 err = aead_register_instance(tmpl, inst);
321 if (err)
322 goto out_drop_aead;
324 out:
325 return err;
327 out_drop_aead:
328 crypto_drop_aead(&ctx->spawn);
329 out_free_inst:
330 kfree(inst);
331 goto out;
334 static int pcrypt_create(struct crypto_template *tmpl, struct rtattr **tb)
336 struct crypto_attr_type *algt;
338 algt = crypto_get_attr_type(tb);
339 if (IS_ERR(algt))
340 return PTR_ERR(algt);
342 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
343 case CRYPTO_ALG_TYPE_AEAD:
344 return pcrypt_create_aead(tmpl, tb, algt->type, algt->mask);
347 return -EINVAL;
350 static int pcrypt_cpumask_change_notify(struct notifier_block *self,
351 unsigned long val, void *data)
353 struct padata_pcrypt *pcrypt;
354 struct pcrypt_cpumask *new_mask, *old_mask;
355 struct padata_cpumask *cpumask = (struct padata_cpumask *)data;
357 if (!(val & PADATA_CPU_SERIAL))
358 return 0;
360 pcrypt = container_of(self, struct padata_pcrypt, nblock);
361 new_mask = kmalloc(sizeof(*new_mask), GFP_KERNEL);
362 if (!new_mask)
363 return -ENOMEM;
364 if (!alloc_cpumask_var(&new_mask->mask, GFP_KERNEL)) {
365 kfree(new_mask);
366 return -ENOMEM;
369 old_mask = pcrypt->cb_cpumask;
371 cpumask_copy(new_mask->mask, cpumask->cbcpu);
372 rcu_assign_pointer(pcrypt->cb_cpumask, new_mask);
373 synchronize_rcu();
375 free_cpumask_var(old_mask->mask);
376 kfree(old_mask);
377 return 0;
380 static int pcrypt_sysfs_add(struct padata_instance *pinst, const char *name)
382 int ret;
384 pinst->kobj.kset = pcrypt_kset;
385 ret = kobject_add(&pinst->kobj, NULL, "%s", name);
386 if (!ret)
387 kobject_uevent(&pinst->kobj, KOBJ_ADD);
389 return ret;
392 static int pcrypt_init_padata(struct padata_pcrypt *pcrypt,
393 const char *name)
395 int ret = -ENOMEM;
396 struct pcrypt_cpumask *mask;
398 get_online_cpus();
400 pcrypt->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE,
401 1, name);
402 if (!pcrypt->wq)
403 goto err;
405 pcrypt->pinst = padata_alloc_possible(pcrypt->wq);
406 if (!pcrypt->pinst)
407 goto err_destroy_workqueue;
409 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
410 if (!mask)
411 goto err_free_padata;
412 if (!alloc_cpumask_var(&mask->mask, GFP_KERNEL)) {
413 kfree(mask);
414 goto err_free_padata;
417 cpumask_and(mask->mask, cpu_possible_mask, cpu_online_mask);
418 rcu_assign_pointer(pcrypt->cb_cpumask, mask);
420 pcrypt->nblock.notifier_call = pcrypt_cpumask_change_notify;
421 ret = padata_register_cpumask_notifier(pcrypt->pinst, &pcrypt->nblock);
422 if (ret)
423 goto err_free_cpumask;
425 ret = pcrypt_sysfs_add(pcrypt->pinst, name);
426 if (ret)
427 goto err_unregister_notifier;
429 put_online_cpus();
431 return ret;
433 err_unregister_notifier:
434 padata_unregister_cpumask_notifier(pcrypt->pinst, &pcrypt->nblock);
435 err_free_cpumask:
436 free_cpumask_var(mask->mask);
437 kfree(mask);
438 err_free_padata:
439 padata_free(pcrypt->pinst);
440 err_destroy_workqueue:
441 destroy_workqueue(pcrypt->wq);
442 err:
443 put_online_cpus();
445 return ret;
448 static void pcrypt_fini_padata(struct padata_pcrypt *pcrypt)
450 free_cpumask_var(pcrypt->cb_cpumask->mask);
451 kfree(pcrypt->cb_cpumask);
453 padata_stop(pcrypt->pinst);
454 padata_unregister_cpumask_notifier(pcrypt->pinst, &pcrypt->nblock);
455 destroy_workqueue(pcrypt->wq);
456 padata_free(pcrypt->pinst);
459 static struct crypto_template pcrypt_tmpl = {
460 .name = "pcrypt",
461 .create = pcrypt_create,
462 .module = THIS_MODULE,
465 static int __init pcrypt_init(void)
467 int err = -ENOMEM;
469 pcrypt_kset = kset_create_and_add("pcrypt", NULL, kernel_kobj);
470 if (!pcrypt_kset)
471 goto err;
473 err = pcrypt_init_padata(&pencrypt, "pencrypt");
474 if (err)
475 goto err_unreg_kset;
477 err = pcrypt_init_padata(&pdecrypt, "pdecrypt");
478 if (err)
479 goto err_deinit_pencrypt;
481 padata_start(pencrypt.pinst);
482 padata_start(pdecrypt.pinst);
484 return crypto_register_template(&pcrypt_tmpl);
486 err_deinit_pencrypt:
487 pcrypt_fini_padata(&pencrypt);
488 err_unreg_kset:
489 kset_unregister(pcrypt_kset);
490 err:
491 return err;
494 static void __exit pcrypt_exit(void)
496 pcrypt_fini_padata(&pencrypt);
497 pcrypt_fini_padata(&pdecrypt);
499 kset_unregister(pcrypt_kset);
500 crypto_unregister_template(&pcrypt_tmpl);
503 subsys_initcall(pcrypt_init);
504 module_exit(pcrypt_exit);
506 MODULE_LICENSE("GPL");
507 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
508 MODULE_DESCRIPTION("Parallel crypto wrapper");
509 MODULE_ALIAS_CRYPTO("pcrypt");