Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 1 | /* |
| 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> |
| 27 | #include <crypto/pcrypt.h> |
| 28 | |
| 29 | static struct padata_instance *pcrypt_enc_padata; |
| 30 | static struct padata_instance *pcrypt_dec_padata; |
| 31 | static struct workqueue_struct *encwq; |
| 32 | static struct workqueue_struct *decwq; |
| 33 | |
| 34 | struct pcrypt_instance_ctx { |
| 35 | struct crypto_spawn spawn; |
| 36 | unsigned int tfm_count; |
| 37 | }; |
| 38 | |
| 39 | struct pcrypt_aead_ctx { |
| 40 | struct crypto_aead *child; |
| 41 | unsigned int cb_cpu; |
| 42 | }; |
| 43 | |
| 44 | static int pcrypt_do_parallel(struct padata_priv *padata, unsigned int *cb_cpu, |
| 45 | struct padata_instance *pinst) |
| 46 | { |
| 47 | unsigned int cpu_index, cpu, i; |
| 48 | |
| 49 | cpu = *cb_cpu; |
| 50 | |
| 51 | if (cpumask_test_cpu(cpu, cpu_active_mask)) |
| 52 | goto out; |
| 53 | |
| 54 | cpu_index = cpu % cpumask_weight(cpu_active_mask); |
| 55 | |
| 56 | cpu = cpumask_first(cpu_active_mask); |
| 57 | for (i = 0; i < cpu_index; i++) |
| 58 | cpu = cpumask_next(cpu, cpu_active_mask); |
| 59 | |
| 60 | *cb_cpu = cpu; |
| 61 | |
| 62 | out: |
| 63 | return padata_do_parallel(pinst, padata, cpu); |
| 64 | } |
| 65 | |
| 66 | static int pcrypt_aead_setkey(struct crypto_aead *parent, |
| 67 | const u8 *key, unsigned int keylen) |
| 68 | { |
| 69 | struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent); |
| 70 | |
| 71 | return crypto_aead_setkey(ctx->child, key, keylen); |
| 72 | } |
| 73 | |
| 74 | static int pcrypt_aead_setauthsize(struct crypto_aead *parent, |
| 75 | unsigned int authsize) |
| 76 | { |
| 77 | struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent); |
| 78 | |
| 79 | return crypto_aead_setauthsize(ctx->child, authsize); |
| 80 | } |
| 81 | |
| 82 | static void pcrypt_aead_serial(struct padata_priv *padata) |
| 83 | { |
| 84 | struct pcrypt_request *preq = pcrypt_padata_request(padata); |
| 85 | struct aead_request *req = pcrypt_request_ctx(preq); |
| 86 | |
| 87 | aead_request_complete(req->base.data, padata->info); |
| 88 | } |
| 89 | |
| 90 | static void pcrypt_aead_giv_serial(struct padata_priv *padata) |
| 91 | { |
| 92 | struct pcrypt_request *preq = pcrypt_padata_request(padata); |
| 93 | struct aead_givcrypt_request *req = pcrypt_request_ctx(preq); |
| 94 | |
| 95 | aead_request_complete(req->areq.base.data, padata->info); |
| 96 | } |
| 97 | |
| 98 | static void pcrypt_aead_done(struct crypto_async_request *areq, int err) |
| 99 | { |
| 100 | struct aead_request *req = areq->data; |
| 101 | struct pcrypt_request *preq = aead_request_ctx(req); |
| 102 | struct padata_priv *padata = pcrypt_request_padata(preq); |
| 103 | |
| 104 | padata->info = err; |
| 105 | req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; |
| 106 | |
| 107 | padata_do_serial(padata); |
| 108 | } |
| 109 | |
| 110 | static void pcrypt_aead_enc(struct padata_priv *padata) |
| 111 | { |
| 112 | struct pcrypt_request *preq = pcrypt_padata_request(padata); |
| 113 | struct aead_request *req = pcrypt_request_ctx(preq); |
| 114 | |
| 115 | padata->info = crypto_aead_encrypt(req); |
| 116 | |
Steffen Klassert | 5a1436b | 2010-02-04 11:40:17 +1100 | [diff] [blame] | 117 | if (padata->info == -EINPROGRESS) |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 118 | return; |
| 119 | |
| 120 | padata_do_serial(padata); |
| 121 | } |
| 122 | |
| 123 | static int pcrypt_aead_encrypt(struct aead_request *req) |
| 124 | { |
| 125 | int err; |
| 126 | struct pcrypt_request *preq = aead_request_ctx(req); |
| 127 | struct aead_request *creq = pcrypt_request_ctx(preq); |
| 128 | struct padata_priv *padata = pcrypt_request_padata(preq); |
| 129 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 130 | struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead); |
| 131 | u32 flags = aead_request_flags(req); |
| 132 | |
| 133 | memset(padata, 0, sizeof(struct padata_priv)); |
| 134 | |
| 135 | padata->parallel = pcrypt_aead_enc; |
| 136 | padata->serial = pcrypt_aead_serial; |
| 137 | |
| 138 | aead_request_set_tfm(creq, ctx->child); |
| 139 | aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP, |
| 140 | pcrypt_aead_done, req); |
| 141 | aead_request_set_crypt(creq, req->src, req->dst, |
| 142 | req->cryptlen, req->iv); |
| 143 | aead_request_set_assoc(creq, req->assoc, req->assoclen); |
| 144 | |
| 145 | err = pcrypt_do_parallel(padata, &ctx->cb_cpu, pcrypt_enc_padata); |
Steffen Klassert | 83f619f | 2010-07-07 15:32:02 +0200 | [diff] [blame] | 146 | if (!err) |
| 147 | return -EINPROGRESS; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 148 | |
| 149 | return err; |
| 150 | } |
| 151 | |
| 152 | static void pcrypt_aead_dec(struct padata_priv *padata) |
| 153 | { |
| 154 | struct pcrypt_request *preq = pcrypt_padata_request(padata); |
| 155 | struct aead_request *req = pcrypt_request_ctx(preq); |
| 156 | |
| 157 | padata->info = crypto_aead_decrypt(req); |
| 158 | |
Steffen Klassert | 5a1436b | 2010-02-04 11:40:17 +1100 | [diff] [blame] | 159 | if (padata->info == -EINPROGRESS) |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 160 | return; |
| 161 | |
| 162 | padata_do_serial(padata); |
| 163 | } |
| 164 | |
| 165 | static int pcrypt_aead_decrypt(struct aead_request *req) |
| 166 | { |
| 167 | int err; |
| 168 | struct pcrypt_request *preq = aead_request_ctx(req); |
| 169 | struct aead_request *creq = pcrypt_request_ctx(preq); |
| 170 | struct padata_priv *padata = pcrypt_request_padata(preq); |
| 171 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 172 | struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead); |
| 173 | u32 flags = aead_request_flags(req); |
| 174 | |
| 175 | memset(padata, 0, sizeof(struct padata_priv)); |
| 176 | |
| 177 | padata->parallel = pcrypt_aead_dec; |
| 178 | padata->serial = pcrypt_aead_serial; |
| 179 | |
| 180 | aead_request_set_tfm(creq, ctx->child); |
| 181 | aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP, |
| 182 | pcrypt_aead_done, req); |
| 183 | aead_request_set_crypt(creq, req->src, req->dst, |
| 184 | req->cryptlen, req->iv); |
| 185 | aead_request_set_assoc(creq, req->assoc, req->assoclen); |
| 186 | |
| 187 | err = pcrypt_do_parallel(padata, &ctx->cb_cpu, pcrypt_dec_padata); |
Steffen Klassert | 83f619f | 2010-07-07 15:32:02 +0200 | [diff] [blame] | 188 | if (!err) |
| 189 | return -EINPROGRESS; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 190 | |
| 191 | return err; |
| 192 | } |
| 193 | |
| 194 | static void pcrypt_aead_givenc(struct padata_priv *padata) |
| 195 | { |
| 196 | struct pcrypt_request *preq = pcrypt_padata_request(padata); |
| 197 | struct aead_givcrypt_request *req = pcrypt_request_ctx(preq); |
| 198 | |
| 199 | padata->info = crypto_aead_givencrypt(req); |
| 200 | |
Steffen Klassert | 5a1436b | 2010-02-04 11:40:17 +1100 | [diff] [blame] | 201 | if (padata->info == -EINPROGRESS) |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 202 | return; |
| 203 | |
| 204 | padata_do_serial(padata); |
| 205 | } |
| 206 | |
| 207 | static int pcrypt_aead_givencrypt(struct aead_givcrypt_request *req) |
| 208 | { |
| 209 | int err; |
| 210 | struct aead_request *areq = &req->areq; |
| 211 | struct pcrypt_request *preq = aead_request_ctx(areq); |
| 212 | struct aead_givcrypt_request *creq = pcrypt_request_ctx(preq); |
| 213 | struct padata_priv *padata = pcrypt_request_padata(preq); |
| 214 | struct crypto_aead *aead = aead_givcrypt_reqtfm(req); |
| 215 | struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead); |
| 216 | u32 flags = aead_request_flags(areq); |
| 217 | |
| 218 | memset(padata, 0, sizeof(struct padata_priv)); |
| 219 | |
| 220 | padata->parallel = pcrypt_aead_givenc; |
| 221 | padata->serial = pcrypt_aead_giv_serial; |
| 222 | |
| 223 | aead_givcrypt_set_tfm(creq, ctx->child); |
| 224 | aead_givcrypt_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP, |
| 225 | pcrypt_aead_done, areq); |
| 226 | aead_givcrypt_set_crypt(creq, areq->src, areq->dst, |
| 227 | areq->cryptlen, areq->iv); |
| 228 | aead_givcrypt_set_assoc(creq, areq->assoc, areq->assoclen); |
| 229 | aead_givcrypt_set_giv(creq, req->giv, req->seq); |
| 230 | |
| 231 | err = pcrypt_do_parallel(padata, &ctx->cb_cpu, pcrypt_enc_padata); |
Steffen Klassert | 83f619f | 2010-07-07 15:32:02 +0200 | [diff] [blame] | 232 | if (!err) |
| 233 | return -EINPROGRESS; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 234 | |
| 235 | return err; |
| 236 | } |
| 237 | |
| 238 | static int pcrypt_aead_init_tfm(struct crypto_tfm *tfm) |
| 239 | { |
| 240 | int cpu, cpu_index; |
| 241 | struct crypto_instance *inst = crypto_tfm_alg_instance(tfm); |
| 242 | struct pcrypt_instance_ctx *ictx = crypto_instance_ctx(inst); |
| 243 | struct pcrypt_aead_ctx *ctx = crypto_tfm_ctx(tfm); |
| 244 | struct crypto_aead *cipher; |
| 245 | |
| 246 | ictx->tfm_count++; |
| 247 | |
| 248 | cpu_index = ictx->tfm_count % cpumask_weight(cpu_active_mask); |
| 249 | |
| 250 | ctx->cb_cpu = cpumask_first(cpu_active_mask); |
| 251 | for (cpu = 0; cpu < cpu_index; cpu++) |
| 252 | ctx->cb_cpu = cpumask_next(ctx->cb_cpu, cpu_active_mask); |
| 253 | |
| 254 | cipher = crypto_spawn_aead(crypto_instance_ctx(inst)); |
| 255 | |
| 256 | if (IS_ERR(cipher)) |
| 257 | return PTR_ERR(cipher); |
| 258 | |
| 259 | ctx->child = cipher; |
| 260 | tfm->crt_aead.reqsize = sizeof(struct pcrypt_request) |
| 261 | + sizeof(struct aead_givcrypt_request) |
| 262 | + crypto_aead_reqsize(cipher); |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static void pcrypt_aead_exit_tfm(struct crypto_tfm *tfm) |
| 268 | { |
| 269 | struct pcrypt_aead_ctx *ctx = crypto_tfm_ctx(tfm); |
| 270 | |
| 271 | crypto_free_aead(ctx->child); |
| 272 | } |
| 273 | |
| 274 | static struct crypto_instance *pcrypt_alloc_instance(struct crypto_alg *alg) |
| 275 | { |
| 276 | struct crypto_instance *inst; |
| 277 | struct pcrypt_instance_ctx *ctx; |
| 278 | int err; |
| 279 | |
| 280 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
| 281 | if (!inst) { |
| 282 | inst = ERR_PTR(-ENOMEM); |
| 283 | goto out; |
| 284 | } |
| 285 | |
| 286 | err = -ENAMETOOLONG; |
| 287 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 288 | "pcrypt(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
| 289 | goto out_free_inst; |
| 290 | |
| 291 | memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); |
| 292 | |
| 293 | ctx = crypto_instance_ctx(inst); |
| 294 | err = crypto_init_spawn(&ctx->spawn, alg, inst, |
| 295 | CRYPTO_ALG_TYPE_MASK); |
| 296 | if (err) |
| 297 | goto out_free_inst; |
| 298 | |
| 299 | inst->alg.cra_priority = alg->cra_priority + 100; |
| 300 | inst->alg.cra_blocksize = alg->cra_blocksize; |
| 301 | inst->alg.cra_alignmask = alg->cra_alignmask; |
| 302 | |
| 303 | out: |
| 304 | return inst; |
| 305 | |
| 306 | out_free_inst: |
| 307 | kfree(inst); |
| 308 | inst = ERR_PTR(err); |
| 309 | goto out; |
| 310 | } |
| 311 | |
Dan Carpenter | 80a6d7d | 2010-03-24 21:35:23 +0800 | [diff] [blame] | 312 | static struct crypto_instance *pcrypt_alloc_aead(struct rtattr **tb, |
| 313 | u32 type, u32 mask) |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 314 | { |
| 315 | struct crypto_instance *inst; |
| 316 | struct crypto_alg *alg; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 317 | |
Dan Carpenter | 80a6d7d | 2010-03-24 21:35:23 +0800 | [diff] [blame] | 318 | alg = crypto_get_attr_alg(tb, type, (mask & CRYPTO_ALG_TYPE_MASK)); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 319 | if (IS_ERR(alg)) |
| 320 | return ERR_CAST(alg); |
| 321 | |
| 322 | inst = pcrypt_alloc_instance(alg); |
| 323 | if (IS_ERR(inst)) |
| 324 | goto out_put_alg; |
| 325 | |
| 326 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC; |
| 327 | inst->alg.cra_type = &crypto_aead_type; |
| 328 | |
| 329 | inst->alg.cra_aead.ivsize = alg->cra_aead.ivsize; |
| 330 | inst->alg.cra_aead.geniv = alg->cra_aead.geniv; |
| 331 | inst->alg.cra_aead.maxauthsize = alg->cra_aead.maxauthsize; |
| 332 | |
| 333 | inst->alg.cra_ctxsize = sizeof(struct pcrypt_aead_ctx); |
| 334 | |
| 335 | inst->alg.cra_init = pcrypt_aead_init_tfm; |
| 336 | inst->alg.cra_exit = pcrypt_aead_exit_tfm; |
| 337 | |
| 338 | inst->alg.cra_aead.setkey = pcrypt_aead_setkey; |
| 339 | inst->alg.cra_aead.setauthsize = pcrypt_aead_setauthsize; |
| 340 | inst->alg.cra_aead.encrypt = pcrypt_aead_encrypt; |
| 341 | inst->alg.cra_aead.decrypt = pcrypt_aead_decrypt; |
| 342 | inst->alg.cra_aead.givencrypt = pcrypt_aead_givencrypt; |
| 343 | |
| 344 | out_put_alg: |
| 345 | crypto_mod_put(alg); |
| 346 | return inst; |
| 347 | } |
| 348 | |
| 349 | static struct crypto_instance *pcrypt_alloc(struct rtattr **tb) |
| 350 | { |
| 351 | struct crypto_attr_type *algt; |
| 352 | |
| 353 | algt = crypto_get_attr_type(tb); |
| 354 | if (IS_ERR(algt)) |
| 355 | return ERR_CAST(algt); |
| 356 | |
| 357 | switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) { |
| 358 | case CRYPTO_ALG_TYPE_AEAD: |
Dan Carpenter | 80a6d7d | 2010-03-24 21:35:23 +0800 | [diff] [blame] | 359 | return pcrypt_alloc_aead(tb, algt->type, algt->mask); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | return ERR_PTR(-EINVAL); |
| 363 | } |
| 364 | |
| 365 | static void pcrypt_free(struct crypto_instance *inst) |
| 366 | { |
| 367 | struct pcrypt_instance_ctx *ctx = crypto_instance_ctx(inst); |
| 368 | |
| 369 | crypto_drop_spawn(&ctx->spawn); |
| 370 | kfree(inst); |
| 371 | } |
| 372 | |
| 373 | static struct crypto_template pcrypt_tmpl = { |
| 374 | .name = "pcrypt", |
| 375 | .alloc = pcrypt_alloc, |
| 376 | .free = pcrypt_free, |
| 377 | .module = THIS_MODULE, |
| 378 | }; |
| 379 | |
| 380 | static int __init pcrypt_init(void) |
| 381 | { |
Steffen Klassert | 4c87917 | 2010-07-07 15:30:10 +0200 | [diff] [blame] | 382 | int err = -ENOMEM; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 383 | encwq = create_workqueue("pencrypt"); |
| 384 | if (!encwq) |
| 385 | goto err; |
| 386 | |
| 387 | decwq = create_workqueue("pdecrypt"); |
| 388 | if (!decwq) |
| 389 | goto err_destroy_encwq; |
| 390 | |
| 391 | |
| 392 | pcrypt_enc_padata = padata_alloc(cpu_possible_mask, encwq); |
| 393 | if (!pcrypt_enc_padata) |
| 394 | goto err_destroy_decwq; |
| 395 | |
| 396 | pcrypt_dec_padata = padata_alloc(cpu_possible_mask, decwq); |
| 397 | if (!pcrypt_dec_padata) |
Steffen Klassert | 4c87917 | 2010-07-07 15:30:10 +0200 | [diff] [blame] | 398 | goto err_free_enc_padata; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 399 | |
Steffen Klassert | 4c87917 | 2010-07-07 15:30:10 +0200 | [diff] [blame] | 400 | err = padata_start(pcrypt_enc_padata); |
| 401 | if (err) |
| 402 | goto err_free_dec_padata; |
| 403 | |
| 404 | err = padata_start(pcrypt_dec_padata); |
| 405 | if (err) |
| 406 | goto err_free_dec_padata; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 407 | |
| 408 | return crypto_register_template(&pcrypt_tmpl); |
| 409 | |
Steffen Klassert | 4c87917 | 2010-07-07 15:30:10 +0200 | [diff] [blame] | 410 | err_free_dec_padata: |
| 411 | padata_free(pcrypt_dec_padata); |
| 412 | |
| 413 | err_free_enc_padata: |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 414 | padata_free(pcrypt_enc_padata); |
| 415 | |
| 416 | err_destroy_decwq: |
| 417 | destroy_workqueue(decwq); |
| 418 | |
| 419 | err_destroy_encwq: |
| 420 | destroy_workqueue(encwq); |
| 421 | |
| 422 | err: |
Steffen Klassert | 4c87917 | 2010-07-07 15:30:10 +0200 | [diff] [blame] | 423 | return err; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static void __exit pcrypt_exit(void) |
| 427 | { |
| 428 | padata_stop(pcrypt_enc_padata); |
| 429 | padata_stop(pcrypt_dec_padata); |
| 430 | |
| 431 | destroy_workqueue(encwq); |
| 432 | destroy_workqueue(decwq); |
| 433 | |
| 434 | padata_free(pcrypt_enc_padata); |
| 435 | padata_free(pcrypt_dec_padata); |
| 436 | |
| 437 | crypto_unregister_template(&pcrypt_tmpl); |
| 438 | } |
| 439 | |
| 440 | module_init(pcrypt_init); |
| 441 | module_exit(pcrypt_exit); |
| 442 | |
| 443 | MODULE_LICENSE("GPL"); |
| 444 | MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>"); |
| 445 | MODULE_DESCRIPTION("Parallel crypto wrapper"); |