Blame view
crypto/pcrypt.c
9.15 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> |
a3fb1e330 pcrypt: Added sys... |
16 |
#include <linux/kobject.h> |
d3f64e46a crypto: pcrypt - ... |
17 |
#include <linux/cpu.h> |
5068c7a88 crypto: pcrypt - ... |
18 |
#include <crypto/pcrypt.h> |
63d357889 crypto: pcrypt - ... |
19 20 |
static struct padata_instance *pencrypt; static struct padata_instance *pdecrypt; |
a3fb1e330 pcrypt: Added sys... |
21 |
static struct kset *pcrypt_kset; |
5068c7a88 crypto: pcrypt - ... |
22 23 |
struct pcrypt_instance_ctx { |
66d948e73 crypto: pcrypt - ... |
24 |
struct crypto_aead_spawn spawn; |
bbefa1dd6 crypto: pcrypt - ... |
25 26 |
struct padata_shell *psenc; struct padata_shell *psdec; |
a5a22e57f crypto: pcrypt - ... |
27 |
atomic_t tfm_count; |
5068c7a88 crypto: pcrypt - ... |
28 29 30 31 32 33 |
}; struct pcrypt_aead_ctx { struct crypto_aead *child; unsigned int cb_cpu; }; |
bbefa1dd6 crypto: pcrypt - ... |
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 crypto: pcrypt - ... |
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 crypto: pcrypt - ... |
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 crypto: pcrypt - ... |
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 crypto: pcrypt - ... |
79 |
if (padata->info == -EINPROGRESS) |
5068c7a88 crypto: pcrypt - ... |
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 crypto: pcrypt - ... |
94 95 96 |
struct pcrypt_instance_ctx *ictx; ictx = pcrypt_tfm_ictx(aead); |
5068c7a88 crypto: pcrypt - ... |
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 crypto: pcrypt - ... |
108 |
aead_request_set_ad(creq, req->assoclen); |
5068c7a88 crypto: pcrypt - ... |
109 |
|
bbefa1dd6 crypto: pcrypt - ... |
110 |
err = padata_do_parallel(ictx->psenc, padata, &ctx->cb_cpu); |
83f619f3c padata: make pada... |
111 112 |
if (!err) return -EINPROGRESS; |
5068c7a88 crypto: pcrypt - ... |
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 crypto: pcrypt - ... |
123 |
if (padata->info == -EINPROGRESS) |
5068c7a88 crypto: pcrypt - ... |
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 crypto: pcrypt - ... |
138 139 140 |
struct pcrypt_instance_ctx *ictx; ictx = pcrypt_tfm_ictx(aead); |
5068c7a88 crypto: pcrypt - ... |
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 crypto: pcrypt - ... |
152 |
aead_request_set_ad(creq, req->assoclen); |
5068c7a88 crypto: pcrypt - ... |
153 |
|
bbefa1dd6 crypto: pcrypt - ... |
154 |
err = padata_do_parallel(ictx->psdec, padata, &ctx->cb_cpu); |
83f619f3c padata: make pada... |
155 156 |
if (!err) return -EINPROGRESS; |
5068c7a88 crypto: pcrypt - ... |
157 158 159 |
return err; } |
0496f5606 crypto: pcrypt - ... |
160 |
static int pcrypt_aead_init_tfm(struct crypto_aead *tfm) |
5068c7a88 crypto: pcrypt - ... |
161 162 |
{ int cpu, cpu_index; |
0496f5606 crypto: pcrypt - ... |
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 crypto: pcrypt - ... |
166 |
struct crypto_aead *cipher; |
a5a22e57f crypto: pcrypt - ... |
167 168 |
cpu_index = (unsigned int)atomic_inc_return(&ictx->tfm_count) % cpumask_weight(cpu_online_mask); |
5068c7a88 crypto: pcrypt - ... |
169 |
|
fbf0ca1bf crypto: pcrypt - ... |
170 |
ctx->cb_cpu = cpumask_first(cpu_online_mask); |
5068c7a88 crypto: pcrypt - ... |
171 |
for (cpu = 0; cpu < cpu_index; cpu++) |
fbf0ca1bf crypto: pcrypt - ... |
172 |
ctx->cb_cpu = cpumask_next(ctx->cb_cpu, cpu_online_mask); |
5068c7a88 crypto: pcrypt - ... |
173 |
|
0496f5606 crypto: pcrypt - ... |
174 |
cipher = crypto_spawn_aead(&ictx->spawn); |
5068c7a88 crypto: pcrypt - ... |
175 176 177 178 179 |
if (IS_ERR(cipher)) return PTR_ERR(cipher); ctx->child = cipher; |
0496f5606 crypto: pcrypt - ... |
180 181 182 |
crypto_aead_set_reqsize(tfm, sizeof(struct pcrypt_request) + sizeof(struct aead_request) + crypto_aead_reqsize(cipher)); |
5068c7a88 crypto: pcrypt - ... |
183 184 185 |
return 0; } |
0496f5606 crypto: pcrypt - ... |
186 |
static void pcrypt_aead_exit_tfm(struct crypto_aead *tfm) |
5068c7a88 crypto: pcrypt - ... |
187 |
{ |
0496f5606 crypto: pcrypt - ... |
188 |
struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm); |
5068c7a88 crypto: pcrypt - ... |
189 190 191 |
crypto_free_aead(ctx->child); } |
d76c68109 crypto: pcrypt - ... |
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 crypto: pcrypt - ... |
197 198 |
padata_free_shell(ctx->psdec); padata_free_shell(ctx->psenc); |
d76c68109 crypto: pcrypt - ... |
199 200 |
kfree(inst); } |
66d948e73 crypto: pcrypt - ... |
201 202 |
static int pcrypt_init_instance(struct crypto_instance *inst, struct crypto_alg *alg) |
5068c7a88 crypto: pcrypt - ... |
203 |
{ |
5068c7a88 crypto: pcrypt - ... |
204 205 |
if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "pcrypt(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
66d948e73 crypto: pcrypt - ... |
206 |
return -ENAMETOOLONG; |
5068c7a88 crypto: pcrypt - ... |
207 208 |
memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); |
5068c7a88 crypto: pcrypt - ... |
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 crypto: pcrypt - ... |
212 |
return 0; |
5068c7a88 crypto: pcrypt - ... |
213 |
} |
0496f5606 crypto: pcrypt - ... |
214 |
static int pcrypt_create_aead(struct crypto_template *tmpl, struct rtattr **tb, |
7bcb2c99f crypto: algapi - ... |
215 |
struct crypto_attr_type *algt) |
5068c7a88 crypto: pcrypt - ... |
216 |
{ |
66d948e73 crypto: pcrypt - ... |
217 |
struct pcrypt_instance_ctx *ctx; |
0496f5606 crypto: pcrypt - ... |
218 219 |
struct aead_instance *inst; struct aead_alg *alg; |
7bcb2c99f crypto: algapi - ... |
220 |
u32 mask = crypto_algt_inherited_mask(algt); |
66d948e73 crypto: pcrypt - ... |
221 |
int err; |
66d948e73 crypto: pcrypt - ... |
222 223 |
inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); if (!inst) |
0496f5606 crypto: pcrypt - ... |
224 |
return -ENOMEM; |
66d948e73 crypto: pcrypt - ... |
225 |
|
bbefa1dd6 crypto: pcrypt - ... |
226 |
err = -ENOMEM; |
0496f5606 crypto: pcrypt - ... |
227 |
ctx = aead_instance_ctx(inst); |
bbefa1dd6 crypto: pcrypt - ... |
228 229 |
ctx->psenc = padata_alloc_shell(pencrypt); if (!ctx->psenc) |
07b24c7c0 crypto: pcrypt - ... |
230 |
goto err_free_inst; |
bbefa1dd6 crypto: pcrypt - ... |
231 232 233 |
ctx->psdec = padata_alloc_shell(pdecrypt); if (!ctx->psdec) |
07b24c7c0 crypto: pcrypt - ... |
234 |
goto err_free_inst; |
bbefa1dd6 crypto: pcrypt - ... |
235 |
|
cd900f0ca crypto: aead - pa... |
236 |
err = crypto_grab_aead(&ctx->spawn, aead_crypto_instance(inst), |
7bcb2c99f crypto: algapi - ... |
237 |
crypto_attr_alg_name(tb[1]), 0, mask); |
66d948e73 crypto: pcrypt - ... |
238 |
if (err) |
07b24c7c0 crypto: pcrypt - ... |
239 |
goto err_free_inst; |
5068c7a88 crypto: pcrypt - ... |
240 |
|
0496f5606 crypto: pcrypt - ... |
241 242 |
alg = crypto_spawn_aead_alg(&ctx->spawn); err = pcrypt_init_instance(aead_crypto_instance(inst), &alg->base); |
66d948e73 crypto: pcrypt - ... |
243 |
if (err) |
07b24c7c0 crypto: pcrypt - ... |
244 |
goto err_free_inst; |
5068c7a88 crypto: pcrypt - ... |
245 |
|
7bcb2c99f crypto: algapi - ... |
246 |
inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC; |
846f97df8 crypto: pcrypt - ... |
247 |
|
0496f5606 crypto: pcrypt - ... |
248 249 |
inst->alg.ivsize = crypto_aead_alg_ivsize(alg); inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg); |
5068c7a88 crypto: pcrypt - ... |
250 |
|
0496f5606 crypto: pcrypt - ... |
251 |
inst->alg.base.cra_ctxsize = sizeof(struct pcrypt_aead_ctx); |
5068c7a88 crypto: pcrypt - ... |
252 |
|
0496f5606 crypto: pcrypt - ... |
253 254 |
inst->alg.init = pcrypt_aead_init_tfm; inst->alg.exit = pcrypt_aead_exit_tfm; |
5068c7a88 crypto: pcrypt - ... |
255 |
|
0496f5606 crypto: pcrypt - ... |
256 257 258 259 |
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 - ... |
260 |
|
d76c68109 crypto: pcrypt - ... |
261 |
inst->free = pcrypt_free; |
0496f5606 crypto: pcrypt - ... |
262 |
err = aead_register_instance(tmpl, inst); |
07b24c7c0 crypto: pcrypt - ... |
263 264 265 266 |
if (err) { err_free_inst: pcrypt_free(inst); } |
0496f5606 crypto: pcrypt - ... |
267 |
return err; |
5068c7a88 crypto: pcrypt - ... |
268 |
} |
0496f5606 crypto: pcrypt - ... |
269 |
static int pcrypt_create(struct crypto_template *tmpl, struct rtattr **tb) |
5068c7a88 crypto: pcrypt - ... |
270 271 272 273 274 |
{ struct crypto_attr_type *algt; algt = crypto_get_attr_type(tb); if (IS_ERR(algt)) |
0496f5606 crypto: pcrypt - ... |
275 |
return PTR_ERR(algt); |
5068c7a88 crypto: pcrypt - ... |
276 277 278 |
switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) { case CRYPTO_ALG_TYPE_AEAD: |
7bcb2c99f crypto: algapi - ... |
279 |
return pcrypt_create_aead(tmpl, tb, algt); |
5068c7a88 crypto: pcrypt - ... |
280 |
} |
0496f5606 crypto: pcrypt - ... |
281 |
return -EINVAL; |
5068c7a88 crypto: pcrypt - ... |
282 |
} |
a3fb1e330 pcrypt: Added sys... |
283 284 285 286 287 |
static int pcrypt_sysfs_add(struct padata_instance *pinst, const char *name) { int ret; pinst->kobj.kset = pcrypt_kset; |
b1e3874c7 pcrypt: use forma... |
288 |
ret = kobject_add(&pinst->kobj, NULL, "%s", name); |
a3fb1e330 pcrypt: Added sys... |
289 290 291 292 293 |
if (!ret) kobject_uevent(&pinst->kobj, KOBJ_ADD); return ret; } |
63d357889 crypto: pcrypt - ... |
294 |
static int pcrypt_init_padata(struct padata_instance **pinst, const char *name) |
e15bacbeb padata: Make two ... |
295 296 |
{ int ret = -ENOMEM; |
e15bacbeb padata: Make two ... |
297 |
|
3f257191d padata: fold pada... |
298 |
*pinst = padata_alloc(name); |
63d357889 crypto: pcrypt - ... |
299 300 |
if (!*pinst) return ret; |
e15bacbeb padata: Make two ... |
301 |
|
63d357889 crypto: pcrypt - ... |
302 |
ret = pcrypt_sysfs_add(*pinst, name); |
a3fb1e330 pcrypt: Added sys... |
303 |
if (ret) |
63d357889 crypto: pcrypt - ... |
304 |
padata_free(*pinst); |
a3fb1e330 pcrypt: Added sys... |
305 |
|
e15bacbeb padata: Make two ... |
306 |
return ret; |
e15bacbeb padata: Make two ... |
307 |
} |
5068c7a88 crypto: pcrypt - ... |
308 309 |
static struct crypto_template pcrypt_tmpl = { .name = "pcrypt", |
0496f5606 crypto: pcrypt - ... |
310 |
.create = pcrypt_create, |
5068c7a88 crypto: pcrypt - ... |
311 312 313 314 315 |
.module = THIS_MODULE, }; static int __init pcrypt_init(void) { |
a3fb1e330 pcrypt: Added sys... |
316 317 318 319 320 |
int err = -ENOMEM; pcrypt_kset = kset_create_and_add("pcrypt", NULL, kernel_kobj); if (!pcrypt_kset) goto err; |
5068c7a88 crypto: pcrypt - ... |
321 |
|
c57e842ef crypto: pcrypt - ... |
322 |
err = pcrypt_init_padata(&pencrypt, "pencrypt"); |
4c8791702 padata: Check for... |
323 |
if (err) |
a3fb1e330 pcrypt: Added sys... |
324 |
goto err_unreg_kset; |
4c8791702 padata: Check for... |
325 |
|
c57e842ef crypto: pcrypt - ... |
326 |
err = pcrypt_init_padata(&pdecrypt, "pdecrypt"); |
4c8791702 padata: Check for... |
327 |
if (err) |
e15bacbeb padata: Make two ... |
328 |
goto err_deinit_pencrypt; |
4c8791702 padata: Check for... |
329 |
|
e15bacbeb padata: Make two ... |
330 |
return crypto_register_template(&pcrypt_tmpl); |
5068c7a88 crypto: pcrypt - ... |
331 |
|
e15bacbeb padata: Make two ... |
332 |
err_deinit_pencrypt: |
350ef051d padata: remove st... |
333 |
padata_free(pencrypt); |
a3fb1e330 pcrypt: Added sys... |
334 335 |
err_unreg_kset: kset_unregister(pcrypt_kset); |
5068c7a88 crypto: pcrypt - ... |
336 |
err: |
4c8791702 padata: Check for... |
337 |
return err; |
5068c7a88 crypto: pcrypt - ... |
338 339 340 341 |
} static void __exit pcrypt_exit(void) { |
07bfd9bdf crypto: pcrypt - ... |
342 |
crypto_unregister_template(&pcrypt_tmpl); |
350ef051d padata: remove st... |
343 344 |
padata_free(pencrypt); padata_free(pdecrypt); |
5068c7a88 crypto: pcrypt - ... |
345 |
|
a3fb1e330 pcrypt: Added sys... |
346 |
kset_unregister(pcrypt_kset); |
5068c7a88 crypto: pcrypt - ... |
347 |
} |
c4741b230 crypto: run initc... |
348 |
subsys_initcall(pcrypt_init); |
5068c7a88 crypto: pcrypt - ... |
349 350 351 352 353 |
module_exit(pcrypt_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>"); MODULE_DESCRIPTION("Parallel crypto wrapper"); |
4943ba16b crypto: include c... |
354 |
MODULE_ALIAS_CRYPTO("pcrypt"); |