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