Thomas Gleixner | a61127c | 2019-05-29 16:57:49 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 2 | /* |
| 3 | * pcrypt - Parallel crypto wrapper. |
| 4 | * |
| 5 | * Copyright (C) 2009 secunet Security Networks AG |
| 6 | * Copyright (C) 2009 Steffen Klassert <steffen.klassert@secunet.com> |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <crypto/algapi.h> |
| 10 | #include <crypto/internal/aead.h> |
Herbert Xu | a5a22e5 | 2015-05-22 20:34:22 +0800 | [diff] [blame] | 11 | #include <linux/atomic.h> |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 12 | #include <linux/err.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/slab.h> |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 16 | #include <linux/notifier.h> |
Dan Kruchinin | a3fb1e3 | 2010-07-14 14:34:15 +0400 | [diff] [blame] | 17 | #include <linux/kobject.h> |
Steffen Klassert | d3f64e4 | 2010-07-27 07:18:46 +0200 | [diff] [blame] | 18 | #include <linux/cpu.h> |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 19 | #include <crypto/pcrypt.h> |
| 20 | |
Steffen Klassert | c57e842 | 2010-07-27 07:16:33 +0200 | [diff] [blame] | 21 | struct padata_pcrypt { |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 22 | struct padata_instance *pinst; |
| 23 | struct workqueue_struct *wq; |
| 24 | |
| 25 | /* |
| 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. |
| 30 | * |
| 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. |
| 40 | */ |
| 41 | struct pcrypt_cpumask { |
| 42 | cpumask_var_t mask; |
| 43 | } *cb_cpumask; |
| 44 | struct notifier_block nblock; |
| 45 | }; |
| 46 | |
Steffen Klassert | c57e842 | 2010-07-27 07:16:33 +0200 | [diff] [blame] | 47 | static struct padata_pcrypt pencrypt; |
| 48 | static struct padata_pcrypt pdecrypt; |
Dan Kruchinin | a3fb1e3 | 2010-07-14 14:34:15 +0400 | [diff] [blame] | 49 | static struct kset *pcrypt_kset; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 50 | |
| 51 | struct pcrypt_instance_ctx { |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 52 | struct crypto_aead_spawn spawn; |
Herbert Xu | a5a22e5 | 2015-05-22 20:34:22 +0800 | [diff] [blame] | 53 | atomic_t tfm_count; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | struct pcrypt_aead_ctx { |
| 57 | struct crypto_aead *child; |
| 58 | unsigned int cb_cpu; |
| 59 | }; |
| 60 | |
| 61 | static int pcrypt_do_parallel(struct padata_priv *padata, unsigned int *cb_cpu, |
Steffen Klassert | c57e842 | 2010-07-27 07:16:33 +0200 | [diff] [blame] | 62 | struct padata_pcrypt *pcrypt) |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 63 | { |
| 64 | unsigned int cpu_index, cpu, i; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 65 | struct pcrypt_cpumask *cpumask; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 66 | |
| 67 | cpu = *cb_cpu; |
| 68 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 69 | rcu_read_lock_bh(); |
Mathias Krause | 3110e40 | 2013-11-28 19:20:04 +0100 | [diff] [blame] | 70 | cpumask = rcu_dereference_bh(pcrypt->cb_cpumask); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 71 | if (cpumask_test_cpu(cpu, cpumask->mask)) |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 72 | goto out; |
| 73 | |
Steffen Klassert | cc74f4b | 2010-07-20 08:52:20 +0200 | [diff] [blame] | 74 | if (!cpumask_weight(cpumask->mask)) |
| 75 | goto out; |
| 76 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 77 | cpu_index = cpu % cpumask_weight(cpumask->mask); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 78 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 79 | cpu = cpumask_first(cpumask->mask); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 80 | for (i = 0; i < cpu_index; i++) |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 81 | cpu = cpumask_next(cpu, cpumask->mask); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 82 | |
| 83 | *cb_cpu = cpu; |
| 84 | |
| 85 | out: |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 86 | rcu_read_unlock_bh(); |
| 87 | return padata_do_parallel(pcrypt->pinst, padata, cpu); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static int pcrypt_aead_setkey(struct crypto_aead *parent, |
| 91 | const u8 *key, unsigned int keylen) |
| 92 | { |
| 93 | struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent); |
| 94 | |
| 95 | return crypto_aead_setkey(ctx->child, key, keylen); |
| 96 | } |
| 97 | |
| 98 | static int pcrypt_aead_setauthsize(struct crypto_aead *parent, |
| 99 | unsigned int authsize) |
| 100 | { |
| 101 | struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent); |
| 102 | |
| 103 | return crypto_aead_setauthsize(ctx->child, authsize); |
| 104 | } |
| 105 | |
| 106 | static void pcrypt_aead_serial(struct padata_priv *padata) |
| 107 | { |
| 108 | struct pcrypt_request *preq = pcrypt_padata_request(padata); |
| 109 | struct aead_request *req = pcrypt_request_ctx(preq); |
| 110 | |
| 111 | aead_request_complete(req->base.data, padata->info); |
| 112 | } |
| 113 | |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 114 | static void pcrypt_aead_done(struct crypto_async_request *areq, int err) |
| 115 | { |
| 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); |
| 119 | |
| 120 | padata->info = err; |
| 121 | req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; |
| 122 | |
| 123 | padata_do_serial(padata); |
| 124 | } |
| 125 | |
| 126 | static void pcrypt_aead_enc(struct padata_priv *padata) |
| 127 | { |
| 128 | struct pcrypt_request *preq = pcrypt_padata_request(padata); |
| 129 | struct aead_request *req = pcrypt_request_ctx(preq); |
| 130 | |
| 131 | padata->info = crypto_aead_encrypt(req); |
| 132 | |
Steffen Klassert | 5a1436b | 2010-02-04 11:40:17 +1100 | [diff] [blame] | 133 | if (padata->info == -EINPROGRESS) |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 134 | return; |
| 135 | |
| 136 | padata_do_serial(padata); |
| 137 | } |
| 138 | |
| 139 | static int pcrypt_aead_encrypt(struct aead_request *req) |
| 140 | { |
| 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); |
| 148 | |
| 149 | memset(padata, 0, sizeof(struct padata_priv)); |
| 150 | |
| 151 | padata->parallel = pcrypt_aead_enc; |
| 152 | padata->serial = pcrypt_aead_serial; |
| 153 | |
| 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); |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 159 | aead_request_set_ad(creq, req->assoclen); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 160 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 161 | err = pcrypt_do_parallel(padata, &ctx->cb_cpu, &pencrypt); |
Steffen Klassert | 83f619f | 2010-07-07 15:32:02 +0200 | [diff] [blame] | 162 | if (!err) |
| 163 | return -EINPROGRESS; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 164 | |
| 165 | return err; |
| 166 | } |
| 167 | |
| 168 | static void pcrypt_aead_dec(struct padata_priv *padata) |
| 169 | { |
| 170 | struct pcrypt_request *preq = pcrypt_padata_request(padata); |
| 171 | struct aead_request *req = pcrypt_request_ctx(preq); |
| 172 | |
| 173 | padata->info = crypto_aead_decrypt(req); |
| 174 | |
Steffen Klassert | 5a1436b | 2010-02-04 11:40:17 +1100 | [diff] [blame] | 175 | if (padata->info == -EINPROGRESS) |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 176 | return; |
| 177 | |
| 178 | padata_do_serial(padata); |
| 179 | } |
| 180 | |
| 181 | static int pcrypt_aead_decrypt(struct aead_request *req) |
| 182 | { |
| 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); |
| 190 | |
| 191 | memset(padata, 0, sizeof(struct padata_priv)); |
| 192 | |
| 193 | padata->parallel = pcrypt_aead_dec; |
| 194 | padata->serial = pcrypt_aead_serial; |
| 195 | |
| 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); |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 201 | aead_request_set_ad(creq, req->assoclen); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 202 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 203 | err = pcrypt_do_parallel(padata, &ctx->cb_cpu, &pdecrypt); |
Steffen Klassert | 83f619f | 2010-07-07 15:32:02 +0200 | [diff] [blame] | 204 | if (!err) |
| 205 | return -EINPROGRESS; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 206 | |
| 207 | return err; |
| 208 | } |
| 209 | |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 210 | static int pcrypt_aead_init_tfm(struct crypto_aead *tfm) |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 211 | { |
| 212 | int cpu, cpu_index; |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 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); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 216 | struct crypto_aead *cipher; |
| 217 | |
Herbert Xu | a5a22e5 | 2015-05-22 20:34:22 +0800 | [diff] [blame] | 218 | cpu_index = (unsigned int)atomic_inc_return(&ictx->tfm_count) % |
| 219 | cpumask_weight(cpu_online_mask); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 220 | |
Steffen Klassert | fbf0ca1 | 2012-03-28 08:51:03 +0200 | [diff] [blame] | 221 | ctx->cb_cpu = cpumask_first(cpu_online_mask); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 222 | for (cpu = 0; cpu < cpu_index; cpu++) |
Steffen Klassert | fbf0ca1 | 2012-03-28 08:51:03 +0200 | [diff] [blame] | 223 | ctx->cb_cpu = cpumask_next(ctx->cb_cpu, cpu_online_mask); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 224 | |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 225 | cipher = crypto_spawn_aead(&ictx->spawn); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 226 | |
| 227 | if (IS_ERR(cipher)) |
| 228 | return PTR_ERR(cipher); |
| 229 | |
| 230 | ctx->child = cipher; |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 231 | crypto_aead_set_reqsize(tfm, sizeof(struct pcrypt_request) + |
| 232 | sizeof(struct aead_request) + |
| 233 | crypto_aead_reqsize(cipher)); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 238 | static void pcrypt_aead_exit_tfm(struct crypto_aead *tfm) |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 239 | { |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 240 | struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 241 | |
| 242 | crypto_free_aead(ctx->child); |
| 243 | } |
| 244 | |
Eric Biggers | d76c681 | 2017-12-20 14:28:25 -0800 | [diff] [blame] | 245 | static void pcrypt_free(struct aead_instance *inst) |
| 246 | { |
| 247 | struct pcrypt_instance_ctx *ctx = aead_instance_ctx(inst); |
| 248 | |
| 249 | crypto_drop_aead(&ctx->spawn); |
| 250 | kfree(inst); |
| 251 | } |
| 252 | |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 253 | static int pcrypt_init_instance(struct crypto_instance *inst, |
| 254 | struct crypto_alg *alg) |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 255 | { |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 256 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 257 | "pcrypt(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 258 | return -ENAMETOOLONG; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 259 | |
| 260 | memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); |
| 261 | |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 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; |
| 265 | |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 266 | return 0; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 267 | } |
| 268 | |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 269 | static int pcrypt_create_aead(struct crypto_template *tmpl, struct rtattr **tb, |
| 270 | u32 type, u32 mask) |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 271 | { |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 272 | struct pcrypt_instance_ctx *ctx; |
Herbert Xu | 846f97d | 2015-07-09 07:17:18 +0800 | [diff] [blame] | 273 | struct crypto_attr_type *algt; |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 274 | struct aead_instance *inst; |
| 275 | struct aead_alg *alg; |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 276 | const char *name; |
| 277 | int err; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 278 | |
Herbert Xu | 846f97d | 2015-07-09 07:17:18 +0800 | [diff] [blame] | 279 | algt = crypto_get_attr_type(tb); |
| 280 | if (IS_ERR(algt)) |
| 281 | return PTR_ERR(algt); |
| 282 | |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 283 | name = crypto_attr_alg_name(tb[1]); |
| 284 | if (IS_ERR(name)) |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 285 | return PTR_ERR(name); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 286 | |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 287 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
| 288 | if (!inst) |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 289 | return -ENOMEM; |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 290 | |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 291 | ctx = aead_instance_ctx(inst); |
| 292 | crypto_set_aead_spawn(&ctx->spawn, aead_crypto_instance(inst)); |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 293 | |
Herbert Xu | 5e4b8c1 | 2015-08-13 17:29:06 +0800 | [diff] [blame] | 294 | err = crypto_grab_aead(&ctx->spawn, name, 0, 0); |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 295 | if (err) |
| 296 | goto out_free_inst; |
| 297 | |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 298 | alg = crypto_spawn_aead_alg(&ctx->spawn); |
| 299 | err = pcrypt_init_instance(aead_crypto_instance(inst), &alg->base); |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 300 | if (err) |
| 301 | goto out_drop_aead; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 302 | |
Herbert Xu | 846f97d | 2015-07-09 07:17:18 +0800 | [diff] [blame] | 303 | inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC; |
Herbert Xu | 846f97d | 2015-07-09 07:17:18 +0800 | [diff] [blame] | 304 | |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 305 | inst->alg.ivsize = crypto_aead_alg_ivsize(alg); |
| 306 | inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 307 | |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 308 | inst->alg.base.cra_ctxsize = sizeof(struct pcrypt_aead_ctx); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 309 | |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 310 | inst->alg.init = pcrypt_aead_init_tfm; |
| 311 | inst->alg.exit = pcrypt_aead_exit_tfm; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 312 | |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 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; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 317 | |
Eric Biggers | d76c681 | 2017-12-20 14:28:25 -0800 | [diff] [blame] | 318 | inst->free = pcrypt_free; |
| 319 | |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 320 | err = aead_register_instance(tmpl, inst); |
| 321 | if (err) |
| 322 | goto out_drop_aead; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 323 | |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 324 | out: |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 325 | return err; |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 326 | |
| 327 | out_drop_aead: |
| 328 | crypto_drop_aead(&ctx->spawn); |
| 329 | out_free_inst: |
| 330 | kfree(inst); |
Herbert Xu | 66d948e | 2015-05-21 15:10:58 +0800 | [diff] [blame] | 331 | goto out; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 332 | } |
| 333 | |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 334 | static int pcrypt_create(struct crypto_template *tmpl, struct rtattr **tb) |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 335 | { |
| 336 | struct crypto_attr_type *algt; |
| 337 | |
| 338 | algt = crypto_get_attr_type(tb); |
| 339 | if (IS_ERR(algt)) |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 340 | return PTR_ERR(algt); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 341 | |
| 342 | switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) { |
| 343 | case CRYPTO_ALG_TYPE_AEAD: |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 344 | return pcrypt_create_aead(tmpl, tb, algt->type, algt->mask); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 345 | } |
| 346 | |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 347 | return -EINVAL; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 348 | } |
| 349 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 350 | static int pcrypt_cpumask_change_notify(struct notifier_block *self, |
| 351 | unsigned long val, void *data) |
| 352 | { |
Steffen Klassert | c57e842 | 2010-07-27 07:16:33 +0200 | [diff] [blame] | 353 | struct padata_pcrypt *pcrypt; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 354 | struct pcrypt_cpumask *new_mask, *old_mask; |
Steffen Klassert | d3f64e4 | 2010-07-27 07:18:46 +0200 | [diff] [blame] | 355 | struct padata_cpumask *cpumask = (struct padata_cpumask *)data; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 356 | |
| 357 | if (!(val & PADATA_CPU_SERIAL)) |
| 358 | return 0; |
| 359 | |
Steffen Klassert | c57e842 | 2010-07-27 07:16:33 +0200 | [diff] [blame] | 360 | pcrypt = container_of(self, struct padata_pcrypt, nblock); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 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; |
| 367 | } |
| 368 | |
| 369 | old_mask = pcrypt->cb_cpumask; |
| 370 | |
Steffen Klassert | d3f64e4 | 2010-07-27 07:18:46 +0200 | [diff] [blame] | 371 | cpumask_copy(new_mask->mask, cpumask->cbcpu); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 372 | rcu_assign_pointer(pcrypt->cb_cpumask, new_mask); |
Paul E. McKenney | a0076e1 | 2018-11-05 16:57:40 -0800 | [diff] [blame] | 373 | synchronize_rcu(); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 374 | |
| 375 | free_cpumask_var(old_mask->mask); |
| 376 | kfree(old_mask); |
| 377 | return 0; |
| 378 | } |
| 379 | |
Dan Kruchinin | a3fb1e3 | 2010-07-14 14:34:15 +0400 | [diff] [blame] | 380 | static int pcrypt_sysfs_add(struct padata_instance *pinst, const char *name) |
| 381 | { |
| 382 | int ret; |
| 383 | |
| 384 | pinst->kobj.kset = pcrypt_kset; |
Colin Ian King | b1e3874 | 2018-10-27 15:49:26 +0100 | [diff] [blame] | 385 | ret = kobject_add(&pinst->kobj, NULL, "%s", name); |
Dan Kruchinin | a3fb1e3 | 2010-07-14 14:34:15 +0400 | [diff] [blame] | 386 | if (!ret) |
| 387 | kobject_uevent(&pinst->kobj, KOBJ_ADD); |
| 388 | |
| 389 | return ret; |
| 390 | } |
| 391 | |
Steffen Klassert | c57e842 | 2010-07-27 07:16:33 +0200 | [diff] [blame] | 392 | static int pcrypt_init_padata(struct padata_pcrypt *pcrypt, |
| 393 | const char *name) |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 394 | { |
| 395 | int ret = -ENOMEM; |
| 396 | struct pcrypt_cpumask *mask; |
| 397 | |
Steffen Klassert | d3f64e4 | 2010-07-27 07:18:46 +0200 | [diff] [blame] | 398 | get_online_cpus(); |
| 399 | |
Kees Cook | d853754 | 2013-07-03 15:04:57 -0700 | [diff] [blame] | 400 | pcrypt->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE, |
| 401 | 1, name); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 402 | if (!pcrypt->wq) |
| 403 | goto err; |
| 404 | |
Steffen Klassert | e6cc117 | 2010-07-27 07:14:28 +0200 | [diff] [blame] | 405 | pcrypt->pinst = padata_alloc_possible(pcrypt->wq); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 406 | if (!pcrypt->pinst) |
| 407 | goto err_destroy_workqueue; |
| 408 | |
| 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; |
| 415 | } |
| 416 | |
Steffen Klassert | fbf0ca1 | 2012-03-28 08:51:03 +0200 | [diff] [blame] | 417 | cpumask_and(mask->mask, cpu_possible_mask, cpu_online_mask); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 418 | rcu_assign_pointer(pcrypt->cb_cpumask, mask); |
| 419 | |
| 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; |
| 424 | |
Dan Kruchinin | a3fb1e3 | 2010-07-14 14:34:15 +0400 | [diff] [blame] | 425 | ret = pcrypt_sysfs_add(pcrypt->pinst, name); |
| 426 | if (ret) |
| 427 | goto err_unregister_notifier; |
| 428 | |
Steffen Klassert | d3f64e4 | 2010-07-27 07:18:46 +0200 | [diff] [blame] | 429 | put_online_cpus(); |
| 430 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 431 | return ret; |
Steffen Klassert | d3f64e4 | 2010-07-27 07:18:46 +0200 | [diff] [blame] | 432 | |
Dan Kruchinin | a3fb1e3 | 2010-07-14 14:34:15 +0400 | [diff] [blame] | 433 | err_unregister_notifier: |
| 434 | padata_unregister_cpumask_notifier(pcrypt->pinst, &pcrypt->nblock); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 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: |
Steffen Klassert | d3f64e4 | 2010-07-27 07:18:46 +0200 | [diff] [blame] | 443 | put_online_cpus(); |
| 444 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 445 | return ret; |
| 446 | } |
| 447 | |
Steffen Klassert | c57e842 | 2010-07-27 07:16:33 +0200 | [diff] [blame] | 448 | static void pcrypt_fini_padata(struct padata_pcrypt *pcrypt) |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 449 | { |
| 450 | free_cpumask_var(pcrypt->cb_cpumask->mask); |
| 451 | kfree(pcrypt->cb_cpumask); |
| 452 | |
| 453 | padata_stop(pcrypt->pinst); |
| 454 | padata_unregister_cpumask_notifier(pcrypt->pinst, &pcrypt->nblock); |
| 455 | destroy_workqueue(pcrypt->wq); |
| 456 | padata_free(pcrypt->pinst); |
| 457 | } |
| 458 | |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 459 | static struct crypto_template pcrypt_tmpl = { |
| 460 | .name = "pcrypt", |
Herbert Xu | 0496f56 | 2015-05-28 22:08:00 +0800 | [diff] [blame] | 461 | .create = pcrypt_create, |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 462 | .module = THIS_MODULE, |
| 463 | }; |
| 464 | |
| 465 | static int __init pcrypt_init(void) |
| 466 | { |
Dan Kruchinin | a3fb1e3 | 2010-07-14 14:34:15 +0400 | [diff] [blame] | 467 | int err = -ENOMEM; |
| 468 | |
| 469 | pcrypt_kset = kset_create_and_add("pcrypt", NULL, kernel_kobj); |
| 470 | if (!pcrypt_kset) |
| 471 | goto err; |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 472 | |
Steffen Klassert | c57e842 | 2010-07-27 07:16:33 +0200 | [diff] [blame] | 473 | err = pcrypt_init_padata(&pencrypt, "pencrypt"); |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 474 | if (err) |
Dan Kruchinin | a3fb1e3 | 2010-07-14 14:34:15 +0400 | [diff] [blame] | 475 | goto err_unreg_kset; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 476 | |
Steffen Klassert | c57e842 | 2010-07-27 07:16:33 +0200 | [diff] [blame] | 477 | err = pcrypt_init_padata(&pdecrypt, "pdecrypt"); |
Steffen Klassert | 4c87917 | 2010-07-07 15:30:10 +0200 | [diff] [blame] | 478 | if (err) |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 479 | goto err_deinit_pencrypt; |
Steffen Klassert | 4c87917 | 2010-07-07 15:30:10 +0200 | [diff] [blame] | 480 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 481 | padata_start(pencrypt.pinst); |
| 482 | padata_start(pdecrypt.pinst); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 483 | |
| 484 | return crypto_register_template(&pcrypt_tmpl); |
| 485 | |
Dan Kruchinin | e15bacb | 2010-07-14 14:31:57 +0400 | [diff] [blame] | 486 | err_deinit_pencrypt: |
Steffen Klassert | c57e842 | 2010-07-27 07:16:33 +0200 | [diff] [blame] | 487 | pcrypt_fini_padata(&pencrypt); |
Dan Kruchinin | a3fb1e3 | 2010-07-14 14:34:15 +0400 | [diff] [blame] | 488 | err_unreg_kset: |
| 489 | kset_unregister(pcrypt_kset); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 490 | err: |
Steffen Klassert | 4c87917 | 2010-07-07 15:30:10 +0200 | [diff] [blame] | 491 | return err; |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static void __exit pcrypt_exit(void) |
| 495 | { |
Steffen Klassert | c57e842 | 2010-07-27 07:16:33 +0200 | [diff] [blame] | 496 | pcrypt_fini_padata(&pencrypt); |
| 497 | pcrypt_fini_padata(&pdecrypt); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 498 | |
Dan Kruchinin | a3fb1e3 | 2010-07-14 14:34:15 +0400 | [diff] [blame] | 499 | kset_unregister(pcrypt_kset); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 500 | crypto_unregister_template(&pcrypt_tmpl); |
| 501 | } |
| 502 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 503 | subsys_initcall(pcrypt_init); |
Steffen Klassert | 5068c7a | 2010-01-07 15:57:19 +1100 | [diff] [blame] | 504 | module_exit(pcrypt_exit); |
| 505 | |
| 506 | MODULE_LICENSE("GPL"); |
| 507 | MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>"); |
| 508 | MODULE_DESCRIPTION("Parallel crypto wrapper"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 509 | MODULE_ALIAS_CRYPTO("pcrypt"); |