Blame view
crypto/pcrypt.c
9.7 KB
a61127c21 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
5068c7a88 crypto: pcrypt - ... |
2 3 4 5 6 |
/* * pcrypt - Parallel crypto wrapper. * * Copyright (C) 2009 secunet Security Networks AG * Copyright (C) 2009 Steffen Klassert <steffen.klassert@secunet.com> |
5068c7a88 crypto: pcrypt - ... |
7 8 9 10 |
*/ #include <crypto/algapi.h> #include <crypto/internal/aead.h> |
a5a22e57f crypto: pcrypt - ... |
11 |
#include <linux/atomic.h> |
5068c7a88 crypto: pcrypt - ... |
12 13 14 15 |
#include <linux/err.h> #include <linux/init.h> #include <linux/module.h> #include <linux/slab.h> |
e15bacbeb padata: Make two ... |
16 |
#include <linux/notifier.h> |
a3fb1e330 pcrypt: Added sys... |
17 |
#include <linux/kobject.h> |
d3f64e46a crypto: pcrypt - ... |
18 |
#include <linux/cpu.h> |
5068c7a88 crypto: pcrypt - ... |
19 |
#include <crypto/pcrypt.h> |
63d357889 crypto: pcrypt - ... |
20 21 |
static struct padata_instance *pencrypt; static struct padata_instance *pdecrypt; |
a3fb1e330 pcrypt: Added sys... |
22 |
static struct kset *pcrypt_kset; |
5068c7a88 crypto: pcrypt - ... |
23 24 |
struct pcrypt_instance_ctx { |
66d948e73 crypto: pcrypt - ... |
25 |
struct crypto_aead_spawn spawn; |
b4c8ed0bf crypto: pcrypt - ... |
26 27 |
struct padata_shell *psenc; struct padata_shell *psdec; |
a5a22e57f crypto: pcrypt - ... |
28 |
atomic_t tfm_count; |
5068c7a88 crypto: pcrypt - ... |
29 30 31 32 33 34 |
}; struct pcrypt_aead_ctx { struct crypto_aead *child; unsigned int cb_cpu; }; |
b4c8ed0bf crypto: pcrypt - ... |
35 36 37 38 39 |
static inline struct pcrypt_instance_ctx *pcrypt_tfm_ictx( struct crypto_aead *tfm) { return aead_instance_ctx(aead_alg_instance(tfm)); } |
5068c7a88 crypto: pcrypt - ... |
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
static int pcrypt_aead_setkey(struct crypto_aead *parent, const u8 *key, unsigned int keylen) { struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent); return crypto_aead_setkey(ctx->child, key, keylen); } static int pcrypt_aead_setauthsize(struct crypto_aead *parent, unsigned int authsize) { struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent); return crypto_aead_setauthsize(ctx->child, authsize); } static void pcrypt_aead_serial(struct padata_priv *padata) { struct pcrypt_request *preq = pcrypt_padata_request(padata); struct aead_request *req = pcrypt_request_ctx(preq); aead_request_complete(req->base.data, padata->info); } |
5068c7a88 crypto: pcrypt - ... |
63 64 65 66 67 68 69 |
static void pcrypt_aead_done(struct crypto_async_request *areq, int err) { struct aead_request *req = areq->data; struct pcrypt_request *preq = aead_request_ctx(req); struct padata_priv *padata = pcrypt_request_padata(preq); padata->info = err; |
5068c7a88 crypto: pcrypt - ... |
70 71 72 73 74 75 76 77 78 79 |
padata_do_serial(padata); } static void pcrypt_aead_enc(struct padata_priv *padata) { struct pcrypt_request *preq = pcrypt_padata_request(padata); struct aead_request *req = pcrypt_request_ctx(preq); padata->info = crypto_aead_encrypt(req); |
5a1436bee crypto: pcrypt - ... |
80 |
if (padata->info == -EINPROGRESS) |
5068c7a88 crypto: pcrypt - ... |
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
return; padata_do_serial(padata); } static int pcrypt_aead_encrypt(struct aead_request *req) { int err; struct pcrypt_request *preq = aead_request_ctx(req); struct aead_request *creq = pcrypt_request_ctx(preq); struct padata_priv *padata = pcrypt_request_padata(preq); struct crypto_aead *aead = crypto_aead_reqtfm(req); struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead); u32 flags = aead_request_flags(req); |
b4c8ed0bf crypto: pcrypt - ... |
95 96 97 |
struct pcrypt_instance_ctx *ictx; ictx = pcrypt_tfm_ictx(aead); |
5068c7a88 crypto: pcrypt - ... |
98 99 100 101 102 103 104 105 106 107 108 |
memset(padata, 0, sizeof(struct padata_priv)); padata->parallel = pcrypt_aead_enc; padata->serial = pcrypt_aead_serial; aead_request_set_tfm(creq, ctx->child); aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP, pcrypt_aead_done, req); aead_request_set_crypt(creq, req->src, req->dst, req->cryptlen, req->iv); |
0496f5606 crypto: pcrypt - ... |
109 |
aead_request_set_ad(creq, req->assoclen); |
5068c7a88 crypto: pcrypt - ... |
110 |
|
b4c8ed0bf crypto: pcrypt - ... |
111 |
err = padata_do_parallel(ictx->psenc, padata, &ctx->cb_cpu); |
83f619f3c padata: make pada... |
112 113 |
if (!err) return -EINPROGRESS; |
5068c7a88 crypto: pcrypt - ... |
114 115 116 117 118 119 120 121 122 123 |
return err; } static void pcrypt_aead_dec(struct padata_priv *padata) { struct pcrypt_request *preq = pcrypt_padata_request(padata); struct aead_request *req = pcrypt_request_ctx(preq); padata->info = crypto_aead_decrypt(req); |
5a1436bee crypto: pcrypt - ... |
124 |
if (padata->info == -EINPROGRESS) |
5068c7a88 crypto: pcrypt - ... |
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
return; padata_do_serial(padata); } static int pcrypt_aead_decrypt(struct aead_request *req) { int err; struct pcrypt_request *preq = aead_request_ctx(req); struct aead_request *creq = pcrypt_request_ctx(preq); struct padata_priv *padata = pcrypt_request_padata(preq); struct crypto_aead *aead = crypto_aead_reqtfm(req); struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead); u32 flags = aead_request_flags(req); |
b4c8ed0bf crypto: pcrypt - ... |
139 140 141 |
struct pcrypt_instance_ctx *ictx; ictx = pcrypt_tfm_ictx(aead); |
5068c7a88 crypto: pcrypt - ... |
142 143 144 145 146 147 148 149 150 151 152 |
memset(padata, 0, sizeof(struct padata_priv)); padata->parallel = pcrypt_aead_dec; padata->serial = pcrypt_aead_serial; aead_request_set_tfm(creq, ctx->child); aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP, pcrypt_aead_done, req); aead_request_set_crypt(creq, req->src, req->dst, req->cryptlen, req->iv); |
0496f5606 crypto: pcrypt - ... |
153 |
aead_request_set_ad(creq, req->assoclen); |
5068c7a88 crypto: pcrypt - ... |
154 |
|
b4c8ed0bf crypto: pcrypt - ... |
155 |
err = padata_do_parallel(ictx->psdec, padata, &ctx->cb_cpu); |
83f619f3c padata: make pada... |
156 157 |
if (!err) return -EINPROGRESS; |
5068c7a88 crypto: pcrypt - ... |
158 159 160 |
return err; } |
0496f5606 crypto: pcrypt - ... |
161 |
static int pcrypt_aead_init_tfm(struct crypto_aead *tfm) |
5068c7a88 crypto: pcrypt - ... |
162 163 |
{ int cpu, cpu_index; |
0496f5606 crypto: pcrypt - ... |
164 165 166 |
struct aead_instance *inst = aead_alg_instance(tfm); struct pcrypt_instance_ctx *ictx = aead_instance_ctx(inst); struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm); |
5068c7a88 crypto: pcrypt - ... |
167 |
struct crypto_aead *cipher; |
a5a22e57f crypto: pcrypt - ... |
168 169 |
cpu_index = (unsigned int)atomic_inc_return(&ictx->tfm_count) % cpumask_weight(cpu_online_mask); |
5068c7a88 crypto: pcrypt - ... |
170 |
|
fbf0ca1bf crypto: pcrypt - ... |
171 |
ctx->cb_cpu = cpumask_first(cpu_online_mask); |
5068c7a88 crypto: pcrypt - ... |
172 |
for (cpu = 0; cpu < cpu_index; cpu++) |
fbf0ca1bf crypto: pcrypt - ... |
173 |
ctx->cb_cpu = cpumask_next(ctx->cb_cpu, cpu_online_mask); |
5068c7a88 crypto: pcrypt - ... |
174 |
|
0496f5606 crypto: pcrypt - ... |
175 |
cipher = crypto_spawn_aead(&ictx->spawn); |
5068c7a88 crypto: pcrypt - ... |
176 177 178 179 180 |
if (IS_ERR(cipher)) return PTR_ERR(cipher); ctx->child = cipher; |
0496f5606 crypto: pcrypt - ... |
181 182 183 |
crypto_aead_set_reqsize(tfm, sizeof(struct pcrypt_request) + sizeof(struct aead_request) + crypto_aead_reqsize(cipher)); |
5068c7a88 crypto: pcrypt - ... |
184 185 186 |
return 0; } |
0496f5606 crypto: pcrypt - ... |
187 |
static void pcrypt_aead_exit_tfm(struct crypto_aead *tfm) |
5068c7a88 crypto: pcrypt - ... |
188 |
{ |
0496f5606 crypto: pcrypt - ... |
189 |
struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm); |
5068c7a88 crypto: pcrypt - ... |
190 191 192 |
crypto_free_aead(ctx->child); } |
d76c68109 crypto: pcrypt - ... |
193 194 195 196 197 |
static void pcrypt_free(struct aead_instance *inst) { struct pcrypt_instance_ctx *ctx = aead_instance_ctx(inst); crypto_drop_aead(&ctx->spawn); |
b4c8ed0bf crypto: pcrypt - ... |
198 199 |
padata_free_shell(ctx->psdec); padata_free_shell(ctx->psenc); |
d76c68109 crypto: pcrypt - ... |
200 201 |
kfree(inst); } |
66d948e73 crypto: pcrypt - ... |
202 203 |
static int pcrypt_init_instance(struct crypto_instance *inst, struct crypto_alg *alg) |
5068c7a88 crypto: pcrypt - ... |
204 |
{ |
5068c7a88 crypto: pcrypt - ... |
205 206 |
if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "pcrypt(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
66d948e73 crypto: pcrypt - ... |
207 |
return -ENAMETOOLONG; |
5068c7a88 crypto: pcrypt - ... |
208 209 |
memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); |
5068c7a88 crypto: pcrypt - ... |
210 211 212 |
inst->alg.cra_priority = alg->cra_priority + 100; inst->alg.cra_blocksize = alg->cra_blocksize; inst->alg.cra_alignmask = alg->cra_alignmask; |
66d948e73 crypto: pcrypt - ... |
213 |
return 0; |
5068c7a88 crypto: pcrypt - ... |
214 |
} |
0496f5606 crypto: pcrypt - ... |
215 216 |
static int pcrypt_create_aead(struct crypto_template *tmpl, struct rtattr **tb, u32 type, u32 mask) |
5068c7a88 crypto: pcrypt - ... |
217 |
{ |
66d948e73 crypto: pcrypt - ... |
218 |
struct pcrypt_instance_ctx *ctx; |
846f97df8 crypto: pcrypt - ... |
219 |
struct crypto_attr_type *algt; |
0496f5606 crypto: pcrypt - ... |
220 221 |
struct aead_instance *inst; struct aead_alg *alg; |
66d948e73 crypto: pcrypt - ... |
222 223 |
const char *name; int err; |
846f97df8 crypto: pcrypt - ... |
224 225 226 |
algt = crypto_get_attr_type(tb); if (IS_ERR(algt)) return PTR_ERR(algt); |
66d948e73 crypto: pcrypt - ... |
227 228 |
name = crypto_attr_alg_name(tb[1]); if (IS_ERR(name)) |
0496f5606 crypto: pcrypt - ... |
229 |
return PTR_ERR(name); |
66d948e73 crypto: pcrypt - ... |
230 231 232 |
inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); if (!inst) |
0496f5606 crypto: pcrypt - ... |
233 |
return -ENOMEM; |
66d948e73 crypto: pcrypt - ... |
234 |
|
b4c8ed0bf crypto: pcrypt - ... |
235 |
err = -ENOMEM; |
0496f5606 crypto: pcrypt - ... |
236 |
ctx = aead_instance_ctx(inst); |
b4c8ed0bf crypto: pcrypt - ... |
237 238 239 240 241 242 243 |
ctx->psenc = padata_alloc_shell(pencrypt); if (!ctx->psenc) goto out_free_inst; ctx->psdec = padata_alloc_shell(pdecrypt); if (!ctx->psdec) goto out_free_psenc; |
0496f5606 crypto: pcrypt - ... |
244 |
crypto_set_aead_spawn(&ctx->spawn, aead_crypto_instance(inst)); |
5068c7a88 crypto: pcrypt - ... |
245 |
|
5e4b8c1fc crypto: aead - Re... |
246 |
err = crypto_grab_aead(&ctx->spawn, name, 0, 0); |
66d948e73 crypto: pcrypt - ... |
247 |
if (err) |
b4c8ed0bf crypto: pcrypt - ... |
248 |
goto out_free_psdec; |
5068c7a88 crypto: pcrypt - ... |
249 |
|
0496f5606 crypto: pcrypt - ... |
250 251 |
alg = crypto_spawn_aead_alg(&ctx->spawn); err = pcrypt_init_instance(aead_crypto_instance(inst), &alg->base); |
66d948e73 crypto: pcrypt - ... |
252 253 |
if (err) goto out_drop_aead; |
5068c7a88 crypto: pcrypt - ... |
254 |
|
846f97df8 crypto: pcrypt - ... |
255 |
inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC; |
846f97df8 crypto: pcrypt - ... |
256 |
|
0496f5606 crypto: pcrypt - ... |
257 258 |
inst->alg.ivsize = crypto_aead_alg_ivsize(alg); inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg); |
5068c7a88 crypto: pcrypt - ... |
259 |
|
0496f5606 crypto: pcrypt - ... |
260 |
inst->alg.base.cra_ctxsize = sizeof(struct pcrypt_aead_ctx); |
5068c7a88 crypto: pcrypt - ... |
261 |
|
0496f5606 crypto: pcrypt - ... |
262 263 |
inst->alg.init = pcrypt_aead_init_tfm; inst->alg.exit = pcrypt_aead_exit_tfm; |
5068c7a88 crypto: pcrypt - ... |
264 |
|
0496f5606 crypto: pcrypt - ... |
265 266 267 268 |
inst->alg.setkey = pcrypt_aead_setkey; inst->alg.setauthsize = pcrypt_aead_setauthsize; inst->alg.encrypt = pcrypt_aead_encrypt; inst->alg.decrypt = pcrypt_aead_decrypt; |
5068c7a88 crypto: pcrypt - ... |
269 |
|
d76c68109 crypto: pcrypt - ... |
270 |
inst->free = pcrypt_free; |
0496f5606 crypto: pcrypt - ... |
271 272 273 |
err = aead_register_instance(tmpl, inst); if (err) goto out_drop_aead; |
5068c7a88 crypto: pcrypt - ... |
274 |
|
66d948e73 crypto: pcrypt - ... |
275 |
out: |
0496f5606 crypto: pcrypt - ... |
276 |
return err; |
66d948e73 crypto: pcrypt - ... |
277 278 279 |
out_drop_aead: crypto_drop_aead(&ctx->spawn); |
b4c8ed0bf crypto: pcrypt - ... |
280 281 282 283 |
out_free_psdec: padata_free_shell(ctx->psdec); out_free_psenc: padata_free_shell(ctx->psenc); |
66d948e73 crypto: pcrypt - ... |
284 285 |
out_free_inst: kfree(inst); |
66d948e73 crypto: pcrypt - ... |
286 |
goto out; |
5068c7a88 crypto: pcrypt - ... |
287 |
} |
0496f5606 crypto: pcrypt - ... |
288 |
static int pcrypt_create(struct crypto_template *tmpl, struct rtattr **tb) |
5068c7a88 crypto: pcrypt - ... |
289 290 291 292 293 |
{ struct crypto_attr_type *algt; algt = crypto_get_attr_type(tb); if (IS_ERR(algt)) |
0496f5606 crypto: pcrypt - ... |
294 |
return PTR_ERR(algt); |
5068c7a88 crypto: pcrypt - ... |
295 296 297 |
switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) { case CRYPTO_ALG_TYPE_AEAD: |
0496f5606 crypto: pcrypt - ... |
298 |
return pcrypt_create_aead(tmpl, tb, algt->type, algt->mask); |
5068c7a88 crypto: pcrypt - ... |
299 |
} |
0496f5606 crypto: pcrypt - ... |
300 |
return -EINVAL; |
5068c7a88 crypto: pcrypt - ... |
301 |
} |
a3fb1e330 pcrypt: Added sys... |
302 303 304 305 306 |
static int pcrypt_sysfs_add(struct padata_instance *pinst, const char *name) { int ret; pinst->kobj.kset = pcrypt_kset; |
b1e3874c7 pcrypt: use forma... |
307 |
ret = kobject_add(&pinst->kobj, NULL, "%s", name); |
a3fb1e330 pcrypt: Added sys... |
308 309 310 311 312 |
if (!ret) kobject_uevent(&pinst->kobj, KOBJ_ADD); return ret; } |
63d357889 crypto: pcrypt - ... |
313 |
static int pcrypt_init_padata(struct padata_instance **pinst, const char *name) |
e15bacbeb padata: Make two ... |
314 315 |
{ int ret = -ENOMEM; |
e15bacbeb padata: Make two ... |
316 |
|
63d357889 crypto: pcrypt - ... |
317 318 319 |
*pinst = padata_alloc_possible(name); if (!*pinst) return ret; |
e15bacbeb padata: Make two ... |
320 |
|
63d357889 crypto: pcrypt - ... |
321 |
ret = pcrypt_sysfs_add(*pinst, name); |
a3fb1e330 pcrypt: Added sys... |
322 |
if (ret) |
63d357889 crypto: pcrypt - ... |
323 |
padata_free(*pinst); |
a3fb1e330 pcrypt: Added sys... |
324 |
|
e15bacbeb padata: Make two ... |
325 |
return ret; |
e15bacbeb padata: Make two ... |
326 |
} |
63d357889 crypto: pcrypt - ... |
327 |
static void pcrypt_fini_padata(struct padata_instance *pinst) |
e15bacbeb padata: Make two ... |
328 |
{ |
63d357889 crypto: pcrypt - ... |
329 330 |
padata_stop(pinst); padata_free(pinst); |
e15bacbeb padata: Make two ... |
331 |
} |
5068c7a88 crypto: pcrypt - ... |
332 333 |
static struct crypto_template pcrypt_tmpl = { .name = "pcrypt", |
0496f5606 crypto: pcrypt - ... |
334 |
.create = pcrypt_create, |
5068c7a88 crypto: pcrypt - ... |
335 336 337 338 339 |
.module = THIS_MODULE, }; static int __init pcrypt_init(void) { |
a3fb1e330 pcrypt: Added sys... |
340 341 342 343 344 |
int err = -ENOMEM; pcrypt_kset = kset_create_and_add("pcrypt", NULL, kernel_kobj); if (!pcrypt_kset) goto err; |
5068c7a88 crypto: pcrypt - ... |
345 |
|
c57e842ef crypto: pcrypt - ... |
346 |
err = pcrypt_init_padata(&pencrypt, "pencrypt"); |
4c8791702 padata: Check for... |
347 |
if (err) |
a3fb1e330 pcrypt: Added sys... |
348 |
goto err_unreg_kset; |
4c8791702 padata: Check for... |
349 |
|
c57e842ef crypto: pcrypt - ... |
350 |
err = pcrypt_init_padata(&pdecrypt, "pdecrypt"); |
4c8791702 padata: Check for... |
351 |
if (err) |
e15bacbeb padata: Make two ... |
352 |
goto err_deinit_pencrypt; |
4c8791702 padata: Check for... |
353 |
|
63d357889 crypto: pcrypt - ... |
354 355 |
padata_start(pencrypt); padata_start(pdecrypt); |
5068c7a88 crypto: pcrypt - ... |
356 |
|
e15bacbeb padata: Make two ... |
357 |
return crypto_register_template(&pcrypt_tmpl); |
5068c7a88 crypto: pcrypt - ... |
358 |
|
e15bacbeb padata: Make two ... |
359 |
err_deinit_pencrypt: |
63d357889 crypto: pcrypt - ... |
360 |
pcrypt_fini_padata(pencrypt); |
a3fb1e330 pcrypt: Added sys... |
361 362 |
err_unreg_kset: kset_unregister(pcrypt_kset); |
5068c7a88 crypto: pcrypt - ... |
363 |
err: |
4c8791702 padata: Check for... |
364 |
return err; |
5068c7a88 crypto: pcrypt - ... |
365 366 367 368 |
} static void __exit pcrypt_exit(void) { |
00d58d0b5 crypto: pcrypt - ... |
369 |
crypto_unregister_template(&pcrypt_tmpl); |
63d357889 crypto: pcrypt - ... |
370 371 |
pcrypt_fini_padata(pencrypt); pcrypt_fini_padata(pdecrypt); |
5068c7a88 crypto: pcrypt - ... |
372 |
|
a3fb1e330 pcrypt: Added sys... |
373 |
kset_unregister(pcrypt_kset); |
5068c7a88 crypto: pcrypt - ... |
374 |
} |
c4741b230 crypto: run initc... |
375 |
subsys_initcall(pcrypt_init); |
5068c7a88 crypto: pcrypt - ... |
376 377 378 379 380 |
module_exit(pcrypt_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>"); MODULE_DESCRIPTION("Parallel crypto wrapper"); |
4943ba16b crypto: include c... |
381 |
MODULE_ALIAS_CRYPTO("pcrypt"); |