Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Cryptographic API for algorithms (i.e., low-level API). |
| 4 | * |
| 5 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 6 | */ |
| 7 | |
Salvatore Mesoraca | 13c935b | 2018-04-09 15:54:46 +0200 | [diff] [blame] | 8 | #include <crypto/algapi.h> |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 9 | #include <linux/err.h> |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 10 | #include <linux/errno.h> |
Herbert Xu | 3133d76 | 2015-04-22 13:25:53 +0800 | [diff] [blame] | 11 | #include <linux/fips.h> |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/kernel.h> |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 14 | #include <linux/list.h> |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 15 | #include <linux/module.h> |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 16 | #include <linux/rtnetlink.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 18 | #include <linux/string.h> |
| 19 | |
| 20 | #include "internal.h" |
| 21 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 22 | static LIST_HEAD(crypto_template_list); |
| 23 | |
Jarod Wilson | 002c77a | 2014-07-02 15:37:30 -0400 | [diff] [blame] | 24 | static inline void crypto_check_module_sig(struct module *mod) |
| 25 | { |
Herbert Xu | 59afdc7 | 2015-04-22 11:28:46 +0800 | [diff] [blame] | 26 | if (fips_enabled && mod && !module_sig_ok(mod)) |
Jarod Wilson | 002c77a | 2014-07-02 15:37:30 -0400 | [diff] [blame] | 27 | panic("Module %s signature verification failed in FIPS mode\n", |
Herbert Xu | bd4a7c6 | 2015-04-23 14:48:05 +0800 | [diff] [blame] | 28 | module_name(mod)); |
Jarod Wilson | 002c77a | 2014-07-02 15:37:30 -0400 | [diff] [blame] | 29 | } |
| 30 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 31 | static int crypto_check_alg(struct crypto_alg *alg) |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 32 | { |
Jarod Wilson | 002c77a | 2014-07-02 15:37:30 -0400 | [diff] [blame] | 33 | crypto_check_module_sig(alg->cra_module); |
| 34 | |
Eric Biggers | 177f87d | 2019-06-02 22:40:58 -0700 | [diff] [blame] | 35 | if (!alg->cra_name[0] || !alg->cra_driver_name[0]) |
| 36 | return -EINVAL; |
| 37 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 38 | if (alg->cra_alignmask & (alg->cra_alignmask + 1)) |
| 39 | return -EINVAL; |
| 40 | |
Kees Cook | a9f7f88 | 2018-08-07 14:18:40 -0700 | [diff] [blame] | 41 | /* General maximums for all algs. */ |
| 42 | if (alg->cra_alignmask > MAX_ALGAPI_ALIGNMASK) |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 43 | return -EINVAL; |
| 44 | |
Kees Cook | a9f7f88 | 2018-08-07 14:18:40 -0700 | [diff] [blame] | 45 | if (alg->cra_blocksize > MAX_ALGAPI_BLOCKSIZE) |
| 46 | return -EINVAL; |
| 47 | |
| 48 | /* Lower maximums for specific alg types. */ |
Salvatore Mesoraca | 13c935b | 2018-04-09 15:54:46 +0200 | [diff] [blame] | 49 | if (!alg->cra_type && (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == |
| 50 | CRYPTO_ALG_TYPE_CIPHER) { |
| 51 | if (alg->cra_alignmask > MAX_CIPHER_ALIGNMASK) |
| 52 | return -EINVAL; |
| 53 | |
| 54 | if (alg->cra_blocksize > MAX_CIPHER_BLOCKSIZE) |
| 55 | return -EINVAL; |
| 56 | } |
| 57 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 58 | if (alg->cra_priority < 0) |
| 59 | return -EINVAL; |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 60 | |
Eric Biggers | ce8614a | 2017-12-29 10:00:46 -0600 | [diff] [blame] | 61 | refcount_set(&alg->cra_refcnt, 1); |
Herbert Xu | e9b8e5b | 2015-04-09 17:40:35 +0800 | [diff] [blame] | 62 | |
Eric Biggers | 177f87d | 2019-06-02 22:40:58 -0700 | [diff] [blame] | 63 | return 0; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 64 | } |
| 65 | |
Herbert Xu | 319382a | 2015-07-09 07:17:15 +0800 | [diff] [blame] | 66 | static void crypto_free_instance(struct crypto_instance *inst) |
| 67 | { |
| 68 | if (!inst->alg.cra_type->free) { |
| 69 | inst->tmpl->free(inst); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | inst->alg.cra_type->free(inst); |
| 74 | } |
| 75 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 76 | static void crypto_destroy_instance(struct crypto_alg *alg) |
| 77 | { |
| 78 | struct crypto_instance *inst = (void *)alg; |
| 79 | struct crypto_template *tmpl = inst->tmpl; |
| 80 | |
Herbert Xu | 319382a | 2015-07-09 07:17:15 +0800 | [diff] [blame] | 81 | crypto_free_instance(inst); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 82 | crypto_tmpl_put(tmpl); |
| 83 | } |
| 84 | |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 85 | static struct list_head *crypto_more_spawns(struct crypto_alg *alg, |
| 86 | struct list_head *stack, |
| 87 | struct list_head *top, |
| 88 | struct list_head *secondary_spawns) |
| 89 | { |
| 90 | struct crypto_spawn *spawn, *n; |
| 91 | |
Geliang Tang | 304e481 | 2015-11-16 22:37:14 +0800 | [diff] [blame] | 92 | spawn = list_first_entry_or_null(stack, struct crypto_spawn, list); |
| 93 | if (!spawn) |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 94 | return NULL; |
| 95 | |
Herbert Xu | 4f87ee1 | 2019-12-07 22:15:17 +0800 | [diff] [blame^] | 96 | n = list_prev_entry(spawn, list); |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 97 | list_move(&spawn->list, secondary_spawns); |
| 98 | |
Herbert Xu | 4f87ee1 | 2019-12-07 22:15:17 +0800 | [diff] [blame^] | 99 | if (list_is_last(&n->list, stack)) |
| 100 | return top; |
| 101 | |
| 102 | n = list_next_entry(n, list); |
| 103 | if (!spawn->dead) |
| 104 | n->dead = false; |
| 105 | |
| 106 | return &n->inst->alg.cra_users; |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 107 | } |
| 108 | |
Herbert Xu | 1f72371 | 2015-04-02 22:31:22 +0800 | [diff] [blame] | 109 | static void crypto_remove_instance(struct crypto_instance *inst, |
| 110 | struct list_head *list) |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 111 | { |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 112 | struct crypto_template *tmpl = inst->tmpl; |
| 113 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 114 | if (crypto_is_dead(&inst->alg)) |
| 115 | return; |
| 116 | |
| 117 | inst->alg.cra_flags |= CRYPTO_ALG_DEAD; |
Herbert Xu | 38cb241 | 2007-11-17 22:09:38 +0800 | [diff] [blame] | 118 | if (hlist_unhashed(&inst->list)) |
| 119 | return; |
| 120 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 121 | if (!tmpl || !crypto_tmpl_get(tmpl)) |
| 122 | return; |
| 123 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 124 | list_move(&inst->alg.cra_list, list); |
| 125 | hlist_del(&inst->list); |
| 126 | inst->alg.cra_destroy = crypto_destroy_instance; |
| 127 | |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 128 | BUG_ON(!list_empty(&inst->alg.cra_users)); |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 129 | } |
| 130 | |
Steffen Klassert | 89b596b | 2011-09-27 07:22:08 +0200 | [diff] [blame] | 131 | void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list, |
| 132 | struct crypto_alg *nalg) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 133 | { |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 134 | u32 new_type = (nalg ?: alg)->cra_flags; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 135 | struct crypto_spawn *spawn, *n; |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 136 | LIST_HEAD(secondary_spawns); |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 137 | struct list_head *spawns; |
| 138 | LIST_HEAD(stack); |
| 139 | LIST_HEAD(top); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 140 | |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 141 | spawns = &alg->cra_users; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 142 | list_for_each_entry_safe(spawn, n, spawns, list) { |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 143 | if ((spawn->alg->cra_flags ^ new_type) & spawn->mask) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 144 | continue; |
| 145 | |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 146 | list_move(&spawn->list, &top); |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 147 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 148 | |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 149 | spawns = ⊤ |
| 150 | do { |
| 151 | while (!list_empty(spawns)) { |
| 152 | struct crypto_instance *inst; |
| 153 | |
| 154 | spawn = list_first_entry(spawns, struct crypto_spawn, |
| 155 | list); |
| 156 | inst = spawn->inst; |
| 157 | |
| 158 | BUG_ON(&inst->alg == alg); |
| 159 | |
| 160 | list_move(&spawn->list, &stack); |
| 161 | |
| 162 | if (&inst->alg == nalg) |
| 163 | break; |
| 164 | |
Herbert Xu | 4f87ee1 | 2019-12-07 22:15:17 +0800 | [diff] [blame^] | 165 | spawn->dead = true; |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 166 | spawns = &inst->alg.cra_users; |
Eric Biggers | 9a00674 | 2017-12-29 14:30:19 -0600 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * We may encounter an unregistered instance here, since |
| 170 | * an instance's spawns are set up prior to the instance |
| 171 | * being registered. An unregistered instance will have |
| 172 | * NULL ->cra_users.next, since ->cra_users isn't |
| 173 | * properly initialized until registration. But an |
| 174 | * unregistered instance cannot have any users, so treat |
| 175 | * it the same as ->cra_users being empty. |
| 176 | */ |
| 177 | if (spawns->next == NULL) |
| 178 | break; |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 179 | } |
| 180 | } while ((spawns = crypto_more_spawns(alg, &stack, &top, |
| 181 | &secondary_spawns))); |
| 182 | |
| 183 | list_for_each_entry_safe(spawn, n, &secondary_spawns, list) { |
Herbert Xu | 4f87ee1 | 2019-12-07 22:15:17 +0800 | [diff] [blame^] | 184 | if (!spawn->dead) |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 185 | list_move(&spawn->list, &spawn->alg->cra_users); |
| 186 | else |
Herbert Xu | 1f72371 | 2015-04-02 22:31:22 +0800 | [diff] [blame] | 187 | crypto_remove_instance(spawn->inst, list); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 188 | } |
| 189 | } |
Steffen Klassert | 89b596b | 2011-09-27 07:22:08 +0200 | [diff] [blame] | 190 | EXPORT_SYMBOL_GPL(crypto_remove_spawns); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 191 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 192 | static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg) |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 193 | { |
| 194 | struct crypto_alg *q; |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 195 | struct crypto_larval *larval; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 196 | int ret = -EAGAIN; |
| 197 | |
| 198 | if (crypto_is_dead(alg)) |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 199 | goto err; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 200 | |
| 201 | INIT_LIST_HEAD(&alg->cra_users); |
| 202 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 203 | /* No cheating! */ |
| 204 | alg->cra_flags &= ~CRYPTO_ALG_TESTED; |
| 205 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 206 | ret = -EEXIST; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 207 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 208 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 209 | if (q == alg) |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 210 | goto err; |
| 211 | |
Herbert Xu | b8e1599 | 2009-01-28 14:09:59 +1100 | [diff] [blame] | 212 | if (crypto_is_moribund(q)) |
| 213 | continue; |
| 214 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 215 | if (crypto_is_larval(q)) { |
| 216 | if (!strcmp(alg->cra_driver_name, q->cra_driver_name)) |
| 217 | goto err; |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | if (!strcmp(q->cra_driver_name, alg->cra_name) || |
| 222 | !strcmp(q->cra_name, alg->cra_driver_name)) |
| 223 | goto err; |
| 224 | } |
| 225 | |
| 226 | larval = crypto_larval_alloc(alg->cra_name, |
| 227 | alg->cra_flags | CRYPTO_ALG_TESTED, 0); |
| 228 | if (IS_ERR(larval)) |
| 229 | goto out; |
| 230 | |
| 231 | ret = -ENOENT; |
| 232 | larval->adult = crypto_mod_get(alg); |
| 233 | if (!larval->adult) |
| 234 | goto free_larval; |
| 235 | |
Eric Biggers | ce8614a | 2017-12-29 10:00:46 -0600 | [diff] [blame] | 236 | refcount_set(&larval->alg.cra_refcnt, 1); |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 237 | memcpy(larval->alg.cra_driver_name, alg->cra_driver_name, |
| 238 | CRYPTO_MAX_ALG_NAME); |
| 239 | larval->alg.cra_priority = alg->cra_priority; |
| 240 | |
| 241 | list_add(&alg->cra_list, &crypto_alg_list); |
| 242 | list_add(&larval->alg.cra_list, &crypto_alg_list); |
| 243 | |
Corentin Labbe | 1f6669b | 2018-11-29 14:42:26 +0000 | [diff] [blame] | 244 | crypto_stats_init(alg); |
Corentin Labbe | cac5818 | 2018-09-19 10:10:54 +0000 | [diff] [blame] | 245 | |
Richard Hartmann | 5357c6c | 2010-02-16 20:25:21 +0800 | [diff] [blame] | 246 | out: |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 247 | return larval; |
| 248 | |
| 249 | free_larval: |
| 250 | kfree(larval); |
| 251 | err: |
| 252 | larval = ERR_PTR(ret); |
| 253 | goto out; |
| 254 | } |
| 255 | |
| 256 | void crypto_alg_tested(const char *name, int err) |
| 257 | { |
| 258 | struct crypto_larval *test; |
| 259 | struct crypto_alg *alg; |
| 260 | struct crypto_alg *q; |
| 261 | LIST_HEAD(list); |
| 262 | |
| 263 | down_write(&crypto_alg_sem); |
| 264 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
Herbert Xu | b8e1599 | 2009-01-28 14:09:59 +1100 | [diff] [blame] | 265 | if (crypto_is_moribund(q) || !crypto_is_larval(q)) |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 266 | continue; |
| 267 | |
| 268 | test = (struct crypto_larval *)q; |
| 269 | |
| 270 | if (!strcmp(q->cra_driver_name, name)) |
| 271 | goto found; |
| 272 | } |
| 273 | |
Karim Eshapa | c723585 | 2017-05-13 21:05:19 +0200 | [diff] [blame] | 274 | pr_err("alg: Unexpected test result for %s: %d\n", name, err); |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 275 | goto unlock; |
| 276 | |
| 277 | found: |
Herbert Xu | b8e1599 | 2009-01-28 14:09:59 +1100 | [diff] [blame] | 278 | q->cra_flags |= CRYPTO_ALG_DEAD; |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 279 | alg = test->adult; |
| 280 | if (err || list_empty(&alg->cra_list)) |
| 281 | goto complete; |
| 282 | |
| 283 | alg->cra_flags |= CRYPTO_ALG_TESTED; |
| 284 | |
| 285 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
| 286 | if (q == alg) |
| 287 | continue; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 288 | |
| 289 | if (crypto_is_moribund(q)) |
| 290 | continue; |
| 291 | |
| 292 | if (crypto_is_larval(q)) { |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 293 | struct crypto_larval *larval = (void *)q; |
| 294 | |
Herbert Xu | d805848 | 2007-10-02 22:27:29 +0800 | [diff] [blame] | 295 | /* |
| 296 | * Check to see if either our generic name or |
| 297 | * specific name can satisfy the name requested |
| 298 | * by the larval entry q. |
| 299 | */ |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 300 | if (strcmp(alg->cra_name, q->cra_name) && |
| 301 | strcmp(alg->cra_driver_name, q->cra_name)) |
| 302 | continue; |
| 303 | |
| 304 | if (larval->adult) |
| 305 | continue; |
Herbert Xu | 492e2b6 | 2006-09-21 11:35:17 +1000 | [diff] [blame] | 306 | if ((q->cra_flags ^ alg->cra_flags) & larval->mask) |
| 307 | continue; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 308 | if (!crypto_mod_get(alg)) |
| 309 | continue; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 310 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 311 | larval->adult = alg; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 312 | continue; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 313 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 314 | |
| 315 | if (strcmp(alg->cra_name, q->cra_name)) |
| 316 | continue; |
| 317 | |
| 318 | if (strcmp(alg->cra_driver_name, q->cra_driver_name) && |
| 319 | q->cra_priority > alg->cra_priority) |
| 320 | continue; |
| 321 | |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 322 | crypto_remove_spawns(q, &list, alg); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 323 | } |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 324 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 325 | complete: |
| 326 | complete_all(&test->completion); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 327 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 328 | unlock: |
| 329 | up_write(&crypto_alg_sem); |
| 330 | |
| 331 | crypto_remove_final(&list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 332 | } |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 333 | EXPORT_SYMBOL_GPL(crypto_alg_tested); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 334 | |
Steffen Klassert | 22e5b20 | 2011-09-27 07:23:07 +0200 | [diff] [blame] | 335 | void crypto_remove_final(struct list_head *list) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 336 | { |
| 337 | struct crypto_alg *alg; |
| 338 | struct crypto_alg *n; |
| 339 | |
| 340 | list_for_each_entry_safe(alg, n, list, cra_list) { |
| 341 | list_del_init(&alg->cra_list); |
| 342 | crypto_alg_put(alg); |
| 343 | } |
| 344 | } |
Steffen Klassert | 22e5b20 | 2011-09-27 07:23:07 +0200 | [diff] [blame] | 345 | EXPORT_SYMBOL_GPL(crypto_remove_final); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 346 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 347 | static void crypto_wait_for_test(struct crypto_larval *larval) |
| 348 | { |
| 349 | int err; |
| 350 | |
| 351 | err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult); |
| 352 | if (err != NOTIFY_STOP) { |
| 353 | if (WARN_ON(err != NOTIFY_DONE)) |
| 354 | goto out; |
| 355 | crypto_alg_tested(larval->alg.cra_driver_name, 0); |
| 356 | } |
| 357 | |
Herbert Xu | 3fc89ad | 2015-10-19 18:23:57 +0800 | [diff] [blame] | 358 | err = wait_for_completion_killable(&larval->completion); |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 359 | WARN_ON(err); |
Martin K. Petersen | dd8b083 | 2018-08-30 11:00:14 -0400 | [diff] [blame] | 360 | if (!err) |
| 361 | crypto_probing_notify(CRYPTO_MSG_ALG_LOADED, larval); |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 362 | |
| 363 | out: |
| 364 | crypto_larval_kill(&larval->alg); |
| 365 | } |
| 366 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 367 | int crypto_register_alg(struct crypto_alg *alg) |
| 368 | { |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 369 | struct crypto_larval *larval; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 370 | int err; |
| 371 | |
Salvatore Benedetto | d604076 | 2017-01-13 11:54:08 +0000 | [diff] [blame] | 372 | alg->cra_flags &= ~CRYPTO_ALG_DEAD; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 373 | err = crypto_check_alg(alg); |
| 374 | if (err) |
| 375 | return err; |
| 376 | |
| 377 | down_write(&crypto_alg_sem); |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 378 | larval = __crypto_register_alg(alg); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 379 | up_write(&crypto_alg_sem); |
| 380 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 381 | if (IS_ERR(larval)) |
| 382 | return PTR_ERR(larval); |
| 383 | |
| 384 | crypto_wait_for_test(larval); |
| 385 | return 0; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 386 | } |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 387 | EXPORT_SYMBOL_GPL(crypto_register_alg); |
| 388 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 389 | static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list) |
| 390 | { |
| 391 | if (unlikely(list_empty(&alg->cra_list))) |
| 392 | return -ENOENT; |
| 393 | |
| 394 | alg->cra_flags |= CRYPTO_ALG_DEAD; |
| 395 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 396 | list_del_init(&alg->cra_list); |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 397 | crypto_remove_spawns(alg, list, NULL); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 402 | int crypto_unregister_alg(struct crypto_alg *alg) |
| 403 | { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 404 | int ret; |
| 405 | LIST_HEAD(list); |
Richard Hartmann | 5357c6c | 2010-02-16 20:25:21 +0800 | [diff] [blame] | 406 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 407 | down_write(&crypto_alg_sem); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 408 | ret = crypto_remove_alg(alg, &list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 409 | up_write(&crypto_alg_sem); |
| 410 | |
| 411 | if (ret) |
| 412 | return ret; |
| 413 | |
Eric Biggers | ce8614a | 2017-12-29 10:00:46 -0600 | [diff] [blame] | 414 | BUG_ON(refcount_read(&alg->cra_refcnt) != 1); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 415 | if (alg->cra_destroy) |
| 416 | alg->cra_destroy(alg); |
| 417 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 418 | crypto_remove_final(&list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | EXPORT_SYMBOL_GPL(crypto_unregister_alg); |
| 422 | |
Mark Brown | 4b00434 | 2012-01-17 23:34:26 +0000 | [diff] [blame] | 423 | int crypto_register_algs(struct crypto_alg *algs, int count) |
| 424 | { |
| 425 | int i, ret; |
| 426 | |
| 427 | for (i = 0; i < count; i++) { |
| 428 | ret = crypto_register_alg(&algs[i]); |
| 429 | if (ret) |
| 430 | goto err; |
| 431 | } |
| 432 | |
| 433 | return 0; |
| 434 | |
| 435 | err: |
| 436 | for (--i; i >= 0; --i) |
| 437 | crypto_unregister_alg(&algs[i]); |
| 438 | |
| 439 | return ret; |
| 440 | } |
| 441 | EXPORT_SYMBOL_GPL(crypto_register_algs); |
| 442 | |
| 443 | int crypto_unregister_algs(struct crypto_alg *algs, int count) |
| 444 | { |
| 445 | int i, ret; |
| 446 | |
| 447 | for (i = 0; i < count; i++) { |
| 448 | ret = crypto_unregister_alg(&algs[i]); |
| 449 | if (ret) |
| 450 | pr_err("Failed to unregister %s %s: %d\n", |
| 451 | algs[i].cra_driver_name, algs[i].cra_name, ret); |
| 452 | } |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | EXPORT_SYMBOL_GPL(crypto_unregister_algs); |
| 457 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 458 | int crypto_register_template(struct crypto_template *tmpl) |
| 459 | { |
| 460 | struct crypto_template *q; |
| 461 | int err = -EEXIST; |
| 462 | |
| 463 | down_write(&crypto_alg_sem); |
| 464 | |
Jarod Wilson | 002c77a | 2014-07-02 15:37:30 -0400 | [diff] [blame] | 465 | crypto_check_module_sig(tmpl->module); |
| 466 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 467 | list_for_each_entry(q, &crypto_template_list, list) { |
| 468 | if (q == tmpl) |
| 469 | goto out; |
| 470 | } |
| 471 | |
| 472 | list_add(&tmpl->list, &crypto_template_list); |
| 473 | err = 0; |
| 474 | out: |
| 475 | up_write(&crypto_alg_sem); |
| 476 | return err; |
| 477 | } |
| 478 | EXPORT_SYMBOL_GPL(crypto_register_template); |
| 479 | |
Xiongfeng Wang | 9572442 | 2019-01-18 13:58:11 +0800 | [diff] [blame] | 480 | int crypto_register_templates(struct crypto_template *tmpls, int count) |
| 481 | { |
| 482 | int i, err; |
| 483 | |
| 484 | for (i = 0; i < count; i++) { |
| 485 | err = crypto_register_template(&tmpls[i]); |
| 486 | if (err) |
| 487 | goto out; |
| 488 | } |
| 489 | return 0; |
| 490 | |
| 491 | out: |
| 492 | for (--i; i >= 0; --i) |
| 493 | crypto_unregister_template(&tmpls[i]); |
| 494 | return err; |
| 495 | } |
| 496 | EXPORT_SYMBOL_GPL(crypto_register_templates); |
| 497 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 498 | void crypto_unregister_template(struct crypto_template *tmpl) |
| 499 | { |
| 500 | struct crypto_instance *inst; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 501 | struct hlist_node *n; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 502 | struct hlist_head *list; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 503 | LIST_HEAD(users); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 504 | |
| 505 | down_write(&crypto_alg_sem); |
| 506 | |
| 507 | BUG_ON(list_empty(&tmpl->list)); |
| 508 | list_del_init(&tmpl->list); |
| 509 | |
| 510 | list = &tmpl->instances; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 511 | hlist_for_each_entry(inst, list, list) { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 512 | int err = crypto_remove_alg(&inst->alg, &users); |
Joshua I. James | 0efcb8d | 2014-12-05 15:00:10 +0900 | [diff] [blame] | 513 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 514 | BUG_ON(err); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | up_write(&crypto_alg_sem); |
| 518 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 519 | hlist_for_each_entry_safe(inst, n, list, list) { |
Eric Biggers | ce8614a | 2017-12-29 10:00:46 -0600 | [diff] [blame] | 520 | BUG_ON(refcount_read(&inst->alg.cra_refcnt) != 1); |
Herbert Xu | 319382a | 2015-07-09 07:17:15 +0800 | [diff] [blame] | 521 | crypto_free_instance(inst); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 522 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 523 | crypto_remove_final(&users); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 524 | } |
| 525 | EXPORT_SYMBOL_GPL(crypto_unregister_template); |
| 526 | |
Xiongfeng Wang | 9572442 | 2019-01-18 13:58:11 +0800 | [diff] [blame] | 527 | void crypto_unregister_templates(struct crypto_template *tmpls, int count) |
| 528 | { |
| 529 | int i; |
| 530 | |
| 531 | for (i = count - 1; i >= 0; --i) |
| 532 | crypto_unregister_template(&tmpls[i]); |
| 533 | } |
| 534 | EXPORT_SYMBOL_GPL(crypto_unregister_templates); |
| 535 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 536 | static struct crypto_template *__crypto_lookup_template(const char *name) |
| 537 | { |
| 538 | struct crypto_template *q, *tmpl = NULL; |
| 539 | |
| 540 | down_read(&crypto_alg_sem); |
| 541 | list_for_each_entry(q, &crypto_template_list, list) { |
| 542 | if (strcmp(q->name, name)) |
| 543 | continue; |
| 544 | if (unlikely(!crypto_tmpl_get(q))) |
| 545 | continue; |
| 546 | |
| 547 | tmpl = q; |
| 548 | break; |
| 549 | } |
| 550 | up_read(&crypto_alg_sem); |
| 551 | |
| 552 | return tmpl; |
| 553 | } |
| 554 | |
| 555 | struct crypto_template *crypto_lookup_template(const char *name) |
| 556 | { |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 557 | return try_then_request_module(__crypto_lookup_template(name), |
| 558 | "crypto-%s", name); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 559 | } |
| 560 | EXPORT_SYMBOL_GPL(crypto_lookup_template); |
| 561 | |
| 562 | int crypto_register_instance(struct crypto_template *tmpl, |
| 563 | struct crypto_instance *inst) |
| 564 | { |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 565 | struct crypto_larval *larval; |
| 566 | int err; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 567 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 568 | err = crypto_check_alg(&inst->alg); |
| 569 | if (err) |
Stephan Mueller | 9c521a2 | 2015-04-09 12:09:55 +0200 | [diff] [blame] | 570 | return err; |
| 571 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 572 | inst->alg.cra_module = tmpl->module; |
Steffen Klassert | 64a947b | 2011-09-27 07:21:26 +0200 | [diff] [blame] | 573 | inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 574 | |
| 575 | down_write(&crypto_alg_sem); |
| 576 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 577 | larval = __crypto_register_alg(&inst->alg); |
| 578 | if (IS_ERR(larval)) |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 579 | goto unlock; |
| 580 | |
| 581 | hlist_add_head(&inst->list, &tmpl->instances); |
| 582 | inst->tmpl = tmpl; |
| 583 | |
| 584 | unlock: |
| 585 | up_write(&crypto_alg_sem); |
| 586 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 587 | err = PTR_ERR(larval); |
| 588 | if (IS_ERR(larval)) |
| 589 | goto err; |
| 590 | |
| 591 | crypto_wait_for_test(larval); |
| 592 | err = 0; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 593 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 594 | err: |
| 595 | return err; |
| 596 | } |
| 597 | EXPORT_SYMBOL_GPL(crypto_register_instance); |
| 598 | |
Herbert Xu | 87b1675 | 2015-04-02 22:39:40 +0800 | [diff] [blame] | 599 | int crypto_unregister_instance(struct crypto_instance *inst) |
Steffen Klassert | ce3fd84 | 2011-11-08 10:09:17 +0100 | [diff] [blame] | 600 | { |
Herbert Xu | 1f72371 | 2015-04-02 22:31:22 +0800 | [diff] [blame] | 601 | LIST_HEAD(list); |
Steffen Klassert | ce3fd84 | 2011-11-08 10:09:17 +0100 | [diff] [blame] | 602 | |
Steffen Klassert | ce3fd84 | 2011-11-08 10:09:17 +0100 | [diff] [blame] | 603 | down_write(&crypto_alg_sem); |
| 604 | |
Herbert Xu | 87b1675 | 2015-04-02 22:39:40 +0800 | [diff] [blame] | 605 | crypto_remove_spawns(&inst->alg, &list, NULL); |
Herbert Xu | 1f72371 | 2015-04-02 22:31:22 +0800 | [diff] [blame] | 606 | crypto_remove_instance(inst, &list); |
Steffen Klassert | ce3fd84 | 2011-11-08 10:09:17 +0100 | [diff] [blame] | 607 | |
| 608 | up_write(&crypto_alg_sem); |
| 609 | |
Herbert Xu | 1f72371 | 2015-04-02 22:31:22 +0800 | [diff] [blame] | 610 | crypto_remove_final(&list); |
Steffen Klassert | ce3fd84 | 2011-11-08 10:09:17 +0100 | [diff] [blame] | 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | EXPORT_SYMBOL_GPL(crypto_unregister_instance); |
| 615 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 616 | int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg, |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 617 | struct crypto_instance *inst, u32 mask) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 618 | { |
| 619 | int err = -EAGAIN; |
| 620 | |
Eric Biggers | 6b47666 | 2019-01-06 12:46:06 -0800 | [diff] [blame] | 621 | if (WARN_ON_ONCE(inst == NULL)) |
| 622 | return -EINVAL; |
| 623 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 624 | spawn->inst = inst; |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 625 | spawn->mask = mask; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 626 | |
| 627 | down_write(&crypto_alg_sem); |
| 628 | if (!crypto_is_moribund(alg)) { |
| 629 | list_add(&spawn->list, &alg->cra_users); |
| 630 | spawn->alg = alg; |
| 631 | err = 0; |
| 632 | } |
| 633 | up_write(&crypto_alg_sem); |
| 634 | |
| 635 | return err; |
| 636 | } |
| 637 | EXPORT_SYMBOL_GPL(crypto_init_spawn); |
| 638 | |
Herbert Xu | 97eedce | 2009-07-08 15:55:52 +0800 | [diff] [blame] | 639 | int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg, |
| 640 | struct crypto_instance *inst, |
| 641 | const struct crypto_type *frontend) |
| 642 | { |
| 643 | int err = -EINVAL; |
| 644 | |
Dan Carpenter | c614e10 | 2010-05-03 11:08:15 +0800 | [diff] [blame] | 645 | if ((alg->cra_flags ^ frontend->type) & frontend->maskset) |
Herbert Xu | 97eedce | 2009-07-08 15:55:52 +0800 | [diff] [blame] | 646 | goto out; |
| 647 | |
| 648 | spawn->frontend = frontend; |
| 649 | err = crypto_init_spawn(spawn, alg, inst, frontend->maskset); |
| 650 | |
| 651 | out: |
| 652 | return err; |
| 653 | } |
| 654 | EXPORT_SYMBOL_GPL(crypto_init_spawn2); |
| 655 | |
Herbert Xu | d6ef2f1 | 2015-05-11 17:47:39 +0800 | [diff] [blame] | 656 | int crypto_grab_spawn(struct crypto_spawn *spawn, const char *name, |
| 657 | u32 type, u32 mask) |
| 658 | { |
| 659 | struct crypto_alg *alg; |
| 660 | int err; |
| 661 | |
| 662 | alg = crypto_find_alg(name, spawn->frontend, type, mask); |
| 663 | if (IS_ERR(alg)) |
| 664 | return PTR_ERR(alg); |
| 665 | |
| 666 | err = crypto_init_spawn(spawn, alg, spawn->inst, mask); |
| 667 | crypto_mod_put(alg); |
| 668 | return err; |
| 669 | } |
| 670 | EXPORT_SYMBOL_GPL(crypto_grab_spawn); |
| 671 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 672 | void crypto_drop_spawn(struct crypto_spawn *spawn) |
| 673 | { |
| 674 | down_write(&crypto_alg_sem); |
Herbert Xu | 4f87ee1 | 2019-12-07 22:15:17 +0800 | [diff] [blame^] | 675 | if (!spawn->dead) |
Herbert Xu | 7db3b61 | 2019-12-06 13:55:17 +0800 | [diff] [blame] | 676 | list_del(&spawn->list); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 677 | up_write(&crypto_alg_sem); |
| 678 | } |
| 679 | EXPORT_SYMBOL_GPL(crypto_drop_spawn); |
| 680 | |
Herbert Xu | 97eedce | 2009-07-08 15:55:52 +0800 | [diff] [blame] | 681 | static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 682 | { |
| 683 | struct crypto_alg *alg; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 684 | |
| 685 | down_read(&crypto_alg_sem); |
| 686 | alg = spawn->alg; |
Herbert Xu | 4f87ee1 | 2019-12-07 22:15:17 +0800 | [diff] [blame^] | 687 | if (!spawn->dead && !crypto_mod_get(alg)) { |
Herbert Xu | 73669cc | 2019-12-07 22:15:15 +0800 | [diff] [blame] | 688 | alg->cra_flags |= CRYPTO_ALG_DYING; |
| 689 | alg = NULL; |
| 690 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 691 | up_read(&crypto_alg_sem); |
| 692 | |
Herbert Xu | 73669cc | 2019-12-07 22:15:15 +0800 | [diff] [blame] | 693 | return alg ?: ERR_PTR(-EAGAIN); |
Herbert Xu | 97eedce | 2009-07-08 15:55:52 +0800 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type, |
| 697 | u32 mask) |
| 698 | { |
| 699 | struct crypto_alg *alg; |
| 700 | struct crypto_tfm *tfm; |
| 701 | |
| 702 | alg = crypto_spawn_alg(spawn); |
| 703 | if (IS_ERR(alg)) |
| 704 | return ERR_CAST(alg); |
| 705 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 706 | tfm = ERR_PTR(-EINVAL); |
| 707 | if (unlikely((alg->cra_flags ^ type) & mask)) |
| 708 | goto out_put_alg; |
| 709 | |
Herbert Xu | 27d2a33 | 2007-01-24 20:50:26 +1100 | [diff] [blame] | 710 | tfm = __crypto_alloc_tfm(alg, type, mask); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 711 | if (IS_ERR(tfm)) |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 712 | goto out_put_alg; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 713 | |
| 714 | return tfm; |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 715 | |
| 716 | out_put_alg: |
| 717 | crypto_mod_put(alg); |
| 718 | return tfm; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 719 | } |
| 720 | EXPORT_SYMBOL_GPL(crypto_spawn_tfm); |
| 721 | |
Herbert Xu | 97eedce | 2009-07-08 15:55:52 +0800 | [diff] [blame] | 722 | void *crypto_spawn_tfm2(struct crypto_spawn *spawn) |
| 723 | { |
| 724 | struct crypto_alg *alg; |
| 725 | struct crypto_tfm *tfm; |
| 726 | |
| 727 | alg = crypto_spawn_alg(spawn); |
| 728 | if (IS_ERR(alg)) |
| 729 | return ERR_CAST(alg); |
| 730 | |
| 731 | tfm = crypto_create_tfm(alg, spawn->frontend); |
| 732 | if (IS_ERR(tfm)) |
| 733 | goto out_put_alg; |
| 734 | |
| 735 | return tfm; |
| 736 | |
| 737 | out_put_alg: |
| 738 | crypto_mod_put(alg); |
| 739 | return tfm; |
| 740 | } |
| 741 | EXPORT_SYMBOL_GPL(crypto_spawn_tfm2); |
| 742 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 743 | int crypto_register_notifier(struct notifier_block *nb) |
| 744 | { |
| 745 | return blocking_notifier_chain_register(&crypto_chain, nb); |
| 746 | } |
| 747 | EXPORT_SYMBOL_GPL(crypto_register_notifier); |
| 748 | |
| 749 | int crypto_unregister_notifier(struct notifier_block *nb) |
| 750 | { |
| 751 | return blocking_notifier_chain_unregister(&crypto_chain, nb); |
| 752 | } |
| 753 | EXPORT_SYMBOL_GPL(crypto_unregister_notifier); |
| 754 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 755 | struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 756 | { |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 757 | struct rtattr *rta = tb[0]; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 758 | struct crypto_attr_type *algt; |
| 759 | |
| 760 | if (!rta) |
| 761 | return ERR_PTR(-ENOENT); |
| 762 | if (RTA_PAYLOAD(rta) < sizeof(*algt)) |
| 763 | return ERR_PTR(-EINVAL); |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 764 | if (rta->rta_type != CRYPTOA_TYPE) |
| 765 | return ERR_PTR(-EINVAL); |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 766 | |
| 767 | algt = RTA_DATA(rta); |
| 768 | |
| 769 | return algt; |
| 770 | } |
| 771 | EXPORT_SYMBOL_GPL(crypto_get_attr_type); |
| 772 | |
| 773 | int crypto_check_attr_type(struct rtattr **tb, u32 type) |
| 774 | { |
| 775 | struct crypto_attr_type *algt; |
| 776 | |
| 777 | algt = crypto_get_attr_type(tb); |
| 778 | if (IS_ERR(algt)) |
| 779 | return PTR_ERR(algt); |
| 780 | |
| 781 | if ((algt->type ^ type) & algt->mask) |
| 782 | return -EINVAL; |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | EXPORT_SYMBOL_GPL(crypto_check_attr_type); |
| 787 | |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 788 | const char *crypto_attr_alg_name(struct rtattr *rta) |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 789 | { |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 790 | struct crypto_attr_alg *alga; |
| 791 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 792 | if (!rta) |
| 793 | return ERR_PTR(-ENOENT); |
| 794 | if (RTA_PAYLOAD(rta) < sizeof(*alga)) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 795 | return ERR_PTR(-EINVAL); |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 796 | if (rta->rta_type != CRYPTOA_ALG) |
| 797 | return ERR_PTR(-EINVAL); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 798 | |
| 799 | alga = RTA_DATA(rta); |
| 800 | alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0; |
| 801 | |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 802 | return alga->name; |
| 803 | } |
| 804 | EXPORT_SYMBOL_GPL(crypto_attr_alg_name); |
| 805 | |
Herbert Xu | d06854f | 2009-07-08 17:53:16 +0800 | [diff] [blame] | 806 | struct crypto_alg *crypto_attr_alg2(struct rtattr *rta, |
| 807 | const struct crypto_type *frontend, |
| 808 | u32 type, u32 mask) |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 809 | { |
| 810 | const char *name; |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 811 | |
| 812 | name = crypto_attr_alg_name(rta); |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 813 | if (IS_ERR(name)) |
Julia Lawall | 3e8afe3 | 2013-01-22 12:29:26 +0100 | [diff] [blame] | 814 | return ERR_CAST(name); |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 815 | |
Herbert Xu | d06854f | 2009-07-08 17:53:16 +0800 | [diff] [blame] | 816 | return crypto_find_alg(name, frontend, type, mask); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 817 | } |
Herbert Xu | d06854f | 2009-07-08 17:53:16 +0800 | [diff] [blame] | 818 | EXPORT_SYMBOL_GPL(crypto_attr_alg2); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 819 | |
| 820 | int crypto_attr_u32(struct rtattr *rta, u32 *num) |
| 821 | { |
| 822 | struct crypto_attr_u32 *nu32; |
| 823 | |
| 824 | if (!rta) |
| 825 | return -ENOENT; |
| 826 | if (RTA_PAYLOAD(rta) < sizeof(*nu32)) |
| 827 | return -EINVAL; |
| 828 | if (rta->rta_type != CRYPTOA_U32) |
| 829 | return -EINVAL; |
| 830 | |
| 831 | nu32 = RTA_DATA(rta); |
| 832 | *num = nu32->num; |
| 833 | |
| 834 | return 0; |
| 835 | } |
| 836 | EXPORT_SYMBOL_GPL(crypto_attr_u32); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 837 | |
Herbert Xu | 32f27c7 | 2016-06-29 18:04:13 +0800 | [diff] [blame] | 838 | int crypto_inst_setname(struct crypto_instance *inst, const char *name, |
| 839 | struct crypto_alg *alg) |
| 840 | { |
| 841 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name, |
| 842 | alg->cra_name) >= CRYPTO_MAX_ALG_NAME) |
| 843 | return -ENAMETOOLONG; |
| 844 | |
| 845 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", |
| 846 | name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
| 847 | return -ENAMETOOLONG; |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | EXPORT_SYMBOL_GPL(crypto_inst_setname); |
| 852 | |
Eric Biggers | 14aa1a8 | 2019-01-03 20:16:25 -0800 | [diff] [blame] | 853 | void *crypto_alloc_instance(const char *name, struct crypto_alg *alg, |
| 854 | unsigned int head) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 855 | { |
| 856 | struct crypto_instance *inst; |
Herbert Xu | 70ec7bb | 2009-07-07 14:07:37 +0800 | [diff] [blame] | 857 | char *p; |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 858 | int err; |
| 859 | |
Herbert Xu | 70ec7bb | 2009-07-07 14:07:37 +0800 | [diff] [blame] | 860 | p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn), |
| 861 | GFP_KERNEL); |
| 862 | if (!p) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 863 | return ERR_PTR(-ENOMEM); |
| 864 | |
Herbert Xu | 70ec7bb | 2009-07-07 14:07:37 +0800 | [diff] [blame] | 865 | inst = (void *)(p + head); |
| 866 | |
Herbert Xu | 32f27c7 | 2016-06-29 18:04:13 +0800 | [diff] [blame] | 867 | err = crypto_inst_setname(inst, name, alg); |
| 868 | if (err) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 869 | goto err_free_inst; |
| 870 | |
Herbert Xu | 70ec7bb | 2009-07-07 14:07:37 +0800 | [diff] [blame] | 871 | return p; |
| 872 | |
| 873 | err_free_inst: |
| 874 | kfree(p); |
| 875 | return ERR_PTR(err); |
| 876 | } |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 877 | EXPORT_SYMBOL_GPL(crypto_alloc_instance); |
| 878 | |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 879 | void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen) |
| 880 | { |
| 881 | INIT_LIST_HEAD(&queue->list); |
| 882 | queue->backlog = &queue->list; |
| 883 | queue->qlen = 0; |
| 884 | queue->max_qlen = max_qlen; |
| 885 | } |
| 886 | EXPORT_SYMBOL_GPL(crypto_init_queue); |
| 887 | |
| 888 | int crypto_enqueue_request(struct crypto_queue *queue, |
| 889 | struct crypto_async_request *request) |
| 890 | { |
| 891 | int err = -EINPROGRESS; |
| 892 | |
| 893 | if (unlikely(queue->qlen >= queue->max_qlen)) { |
Gilad Ben-Yossef | 6b80ea3 | 2017-10-18 08:00:33 +0100 | [diff] [blame] | 894 | if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) { |
| 895 | err = -ENOSPC; |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 896 | goto out; |
Gilad Ben-Yossef | 6b80ea3 | 2017-10-18 08:00:33 +0100 | [diff] [blame] | 897 | } |
| 898 | err = -EBUSY; |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 899 | if (queue->backlog == &queue->list) |
| 900 | queue->backlog = &request->list; |
| 901 | } |
| 902 | |
| 903 | queue->qlen++; |
| 904 | list_add_tail(&request->list, &queue->list); |
| 905 | |
| 906 | out: |
| 907 | return err; |
| 908 | } |
| 909 | EXPORT_SYMBOL_GPL(crypto_enqueue_request); |
| 910 | |
Herbert Xu | 31d228c | 2015-07-08 11:55:30 +0800 | [diff] [blame] | 911 | struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue) |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 912 | { |
| 913 | struct list_head *request; |
| 914 | |
| 915 | if (unlikely(!queue->qlen)) |
| 916 | return NULL; |
| 917 | |
| 918 | queue->qlen--; |
| 919 | |
| 920 | if (queue->backlog != &queue->list) |
| 921 | queue->backlog = queue->backlog->next; |
| 922 | |
| 923 | request = queue->list.next; |
| 924 | list_del(request); |
| 925 | |
Herbert Xu | 31d228c | 2015-07-08 11:55:30 +0800 | [diff] [blame] | 926 | return list_entry(request, struct crypto_async_request, list); |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 927 | } |
| 928 | EXPORT_SYMBOL_GPL(crypto_dequeue_request); |
| 929 | |
Herbert Xu | 7613636 | 2007-11-20 17:26:06 +0800 | [diff] [blame] | 930 | static inline void crypto_inc_byte(u8 *a, unsigned int size) |
| 931 | { |
| 932 | u8 *b = (a + size); |
| 933 | u8 c; |
| 934 | |
| 935 | for (; size; size--) { |
| 936 | c = *--b + 1; |
| 937 | *b = c; |
| 938 | if (c) |
| 939 | break; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | void crypto_inc(u8 *a, unsigned int size) |
| 944 | { |
| 945 | __be32 *b = (__be32 *)(a + size); |
| 946 | u32 c; |
| 947 | |
Ard Biesheuvel | db91af0 | 2017-02-05 10:06:12 +0000 | [diff] [blame] | 948 | if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) || |
Ard Biesheuvel | 27c539a | 2017-02-14 21:51:02 +0000 | [diff] [blame] | 949 | IS_ALIGNED((unsigned long)b, __alignof__(*b))) |
Ard Biesheuvel | db91af0 | 2017-02-05 10:06:12 +0000 | [diff] [blame] | 950 | for (; size >= 4; size -= 4) { |
| 951 | c = be32_to_cpu(*--b) + 1; |
| 952 | *b = cpu_to_be32(c); |
Ard Biesheuvel | 27c539a | 2017-02-14 21:51:02 +0000 | [diff] [blame] | 953 | if (likely(c)) |
Ard Biesheuvel | db91af0 | 2017-02-05 10:06:12 +0000 | [diff] [blame] | 954 | return; |
| 955 | } |
Herbert Xu | 7613636 | 2007-11-20 17:26:06 +0800 | [diff] [blame] | 956 | |
| 957 | crypto_inc_byte(a, size); |
| 958 | } |
| 959 | EXPORT_SYMBOL_GPL(crypto_inc); |
| 960 | |
Ard Biesheuvel | a7c391f | 2017-07-24 11:28:03 +0100 | [diff] [blame] | 961 | void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int len) |
Herbert Xu | 7613636 | 2007-11-20 17:26:06 +0800 | [diff] [blame] | 962 | { |
Ard Biesheuvel | db91af0 | 2017-02-05 10:06:12 +0000 | [diff] [blame] | 963 | int relalign = 0; |
| 964 | |
| 965 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) { |
| 966 | int size = sizeof(unsigned long); |
Ard Biesheuvel | a7c391f | 2017-07-24 11:28:03 +0100 | [diff] [blame] | 967 | int d = (((unsigned long)dst ^ (unsigned long)src1) | |
| 968 | ((unsigned long)dst ^ (unsigned long)src2)) & |
| 969 | (size - 1); |
Ard Biesheuvel | db91af0 | 2017-02-05 10:06:12 +0000 | [diff] [blame] | 970 | |
| 971 | relalign = d ? 1 << __ffs(d) : size; |
| 972 | |
| 973 | /* |
| 974 | * If we care about alignment, process as many bytes as |
| 975 | * needed to advance dst and src to values whose alignments |
| 976 | * equal their relative alignment. This will allow us to |
| 977 | * process the remainder of the input using optimal strides. |
| 978 | */ |
| 979 | while (((unsigned long)dst & (relalign - 1)) && len > 0) { |
Ard Biesheuvel | a7c391f | 2017-07-24 11:28:03 +0100 | [diff] [blame] | 980 | *dst++ = *src1++ ^ *src2++; |
Ard Biesheuvel | db91af0 | 2017-02-05 10:06:12 +0000 | [diff] [blame] | 981 | len--; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | while (IS_ENABLED(CONFIG_64BIT) && len >= 8 && !(relalign & 7)) { |
Ard Biesheuvel | a7c391f | 2017-07-24 11:28:03 +0100 | [diff] [blame] | 986 | *(u64 *)dst = *(u64 *)src1 ^ *(u64 *)src2; |
Ard Biesheuvel | db91af0 | 2017-02-05 10:06:12 +0000 | [diff] [blame] | 987 | dst += 8; |
Ard Biesheuvel | a7c391f | 2017-07-24 11:28:03 +0100 | [diff] [blame] | 988 | src1 += 8; |
| 989 | src2 += 8; |
Ard Biesheuvel | db91af0 | 2017-02-05 10:06:12 +0000 | [diff] [blame] | 990 | len -= 8; |
| 991 | } |
| 992 | |
| 993 | while (len >= 4 && !(relalign & 3)) { |
Ard Biesheuvel | a7c391f | 2017-07-24 11:28:03 +0100 | [diff] [blame] | 994 | *(u32 *)dst = *(u32 *)src1 ^ *(u32 *)src2; |
Ard Biesheuvel | db91af0 | 2017-02-05 10:06:12 +0000 | [diff] [blame] | 995 | dst += 4; |
Ard Biesheuvel | a7c391f | 2017-07-24 11:28:03 +0100 | [diff] [blame] | 996 | src1 += 4; |
| 997 | src2 += 4; |
Ard Biesheuvel | db91af0 | 2017-02-05 10:06:12 +0000 | [diff] [blame] | 998 | len -= 4; |
| 999 | } |
| 1000 | |
| 1001 | while (len >= 2 && !(relalign & 1)) { |
Ard Biesheuvel | a7c391f | 2017-07-24 11:28:03 +0100 | [diff] [blame] | 1002 | *(u16 *)dst = *(u16 *)src1 ^ *(u16 *)src2; |
Ard Biesheuvel | db91af0 | 2017-02-05 10:06:12 +0000 | [diff] [blame] | 1003 | dst += 2; |
Ard Biesheuvel | a7c391f | 2017-07-24 11:28:03 +0100 | [diff] [blame] | 1004 | src1 += 2; |
| 1005 | src2 += 2; |
Ard Biesheuvel | db91af0 | 2017-02-05 10:06:12 +0000 | [diff] [blame] | 1006 | len -= 2; |
| 1007 | } |
| 1008 | |
| 1009 | while (len--) |
Ard Biesheuvel | a7c391f | 2017-07-24 11:28:03 +0100 | [diff] [blame] | 1010 | *dst++ = *src1++ ^ *src2++; |
Herbert Xu | 7613636 | 2007-11-20 17:26:06 +0800 | [diff] [blame] | 1011 | } |
Ard Biesheuvel | db91af0 | 2017-02-05 10:06:12 +0000 | [diff] [blame] | 1012 | EXPORT_SYMBOL_GPL(__crypto_xor); |
Herbert Xu | 7613636 | 2007-11-20 17:26:06 +0800 | [diff] [blame] | 1013 | |
Herbert Xu | 38d2143 | 2015-04-20 13:39:00 +0800 | [diff] [blame] | 1014 | unsigned int crypto_alg_extsize(struct crypto_alg *alg) |
| 1015 | { |
Herbert Xu | c2110f2 | 2015-05-28 22:07:56 +0800 | [diff] [blame] | 1016 | return alg->cra_ctxsize + |
| 1017 | (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1)); |
Herbert Xu | 38d2143 | 2015-04-20 13:39:00 +0800 | [diff] [blame] | 1018 | } |
| 1019 | EXPORT_SYMBOL_GPL(crypto_alg_extsize); |
| 1020 | |
Herbert Xu | f2aefda | 2016-01-23 13:51:01 +0800 | [diff] [blame] | 1021 | int crypto_type_has_alg(const char *name, const struct crypto_type *frontend, |
| 1022 | u32 type, u32 mask) |
| 1023 | { |
| 1024 | int ret = 0; |
| 1025 | struct crypto_alg *alg = crypto_find_alg(name, frontend, type, mask); |
| 1026 | |
| 1027 | if (!IS_ERR(alg)) { |
| 1028 | crypto_mod_put(alg); |
| 1029 | ret = 1; |
| 1030 | } |
| 1031 | |
| 1032 | return ret; |
| 1033 | } |
| 1034 | EXPORT_SYMBOL_GPL(crypto_type_has_alg); |
| 1035 | |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1036 | #ifdef CONFIG_CRYPTO_STATS |
Corentin Labbe | 1f6669b | 2018-11-29 14:42:26 +0000 | [diff] [blame] | 1037 | void crypto_stats_init(struct crypto_alg *alg) |
| 1038 | { |
| 1039 | memset(&alg->stats, 0, sizeof(alg->stats)); |
| 1040 | } |
| 1041 | EXPORT_SYMBOL_GPL(crypto_stats_init); |
| 1042 | |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1043 | void crypto_stats_get(struct crypto_alg *alg) |
| 1044 | { |
| 1045 | crypto_alg_get(alg); |
| 1046 | } |
| 1047 | EXPORT_SYMBOL_GPL(crypto_stats_get); |
| 1048 | |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1049 | void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, |
| 1050 | int ret) |
| 1051 | { |
| 1052 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1053 | atomic64_inc(&alg->stats.aead.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1054 | } else { |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1055 | atomic64_inc(&alg->stats.aead.encrypt_cnt); |
| 1056 | atomic64_add(cryptlen, &alg->stats.aead.encrypt_tlen); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1057 | } |
| 1058 | crypto_alg_put(alg); |
| 1059 | } |
| 1060 | EXPORT_SYMBOL_GPL(crypto_stats_aead_encrypt); |
| 1061 | |
| 1062 | void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, |
| 1063 | int ret) |
| 1064 | { |
| 1065 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1066 | atomic64_inc(&alg->stats.aead.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1067 | } else { |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1068 | atomic64_inc(&alg->stats.aead.decrypt_cnt); |
| 1069 | atomic64_add(cryptlen, &alg->stats.aead.decrypt_tlen); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1070 | } |
| 1071 | crypto_alg_put(alg); |
| 1072 | } |
| 1073 | EXPORT_SYMBOL_GPL(crypto_stats_aead_decrypt); |
| 1074 | |
| 1075 | void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, |
| 1076 | struct crypto_alg *alg) |
| 1077 | { |
| 1078 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1079 | atomic64_inc(&alg->stats.akcipher.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1080 | } else { |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1081 | atomic64_inc(&alg->stats.akcipher.encrypt_cnt); |
| 1082 | atomic64_add(src_len, &alg->stats.akcipher.encrypt_tlen); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1083 | } |
| 1084 | crypto_alg_put(alg); |
| 1085 | } |
| 1086 | EXPORT_SYMBOL_GPL(crypto_stats_akcipher_encrypt); |
| 1087 | |
| 1088 | void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, |
| 1089 | struct crypto_alg *alg) |
| 1090 | { |
| 1091 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1092 | atomic64_inc(&alg->stats.akcipher.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1093 | } else { |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1094 | atomic64_inc(&alg->stats.akcipher.decrypt_cnt); |
| 1095 | atomic64_add(src_len, &alg->stats.akcipher.decrypt_tlen); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1096 | } |
| 1097 | crypto_alg_put(alg); |
| 1098 | } |
| 1099 | EXPORT_SYMBOL_GPL(crypto_stats_akcipher_decrypt); |
| 1100 | |
| 1101 | void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg) |
| 1102 | { |
| 1103 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1104 | atomic64_inc(&alg->stats.akcipher.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1105 | else |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1106 | atomic64_inc(&alg->stats.akcipher.sign_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1107 | crypto_alg_put(alg); |
| 1108 | } |
| 1109 | EXPORT_SYMBOL_GPL(crypto_stats_akcipher_sign); |
| 1110 | |
| 1111 | void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg) |
| 1112 | { |
| 1113 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1114 | atomic64_inc(&alg->stats.akcipher.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1115 | else |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1116 | atomic64_inc(&alg->stats.akcipher.verify_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1117 | crypto_alg_put(alg); |
| 1118 | } |
| 1119 | EXPORT_SYMBOL_GPL(crypto_stats_akcipher_verify); |
| 1120 | |
| 1121 | void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg) |
| 1122 | { |
| 1123 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1124 | atomic64_inc(&alg->stats.compress.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1125 | } else { |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1126 | atomic64_inc(&alg->stats.compress.compress_cnt); |
| 1127 | atomic64_add(slen, &alg->stats.compress.compress_tlen); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1128 | } |
| 1129 | crypto_alg_put(alg); |
| 1130 | } |
| 1131 | EXPORT_SYMBOL_GPL(crypto_stats_compress); |
| 1132 | |
| 1133 | void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg) |
| 1134 | { |
| 1135 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1136 | atomic64_inc(&alg->stats.compress.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1137 | } else { |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1138 | atomic64_inc(&alg->stats.compress.decompress_cnt); |
| 1139 | atomic64_add(slen, &alg->stats.compress.decompress_tlen); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1140 | } |
| 1141 | crypto_alg_put(alg); |
| 1142 | } |
| 1143 | EXPORT_SYMBOL_GPL(crypto_stats_decompress); |
| 1144 | |
| 1145 | void crypto_stats_ahash_update(unsigned int nbytes, int ret, |
| 1146 | struct crypto_alg *alg) |
| 1147 | { |
| 1148 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1149 | atomic64_inc(&alg->stats.hash.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1150 | else |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1151 | atomic64_add(nbytes, &alg->stats.hash.hash_tlen); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1152 | crypto_alg_put(alg); |
| 1153 | } |
| 1154 | EXPORT_SYMBOL_GPL(crypto_stats_ahash_update); |
| 1155 | |
| 1156 | void crypto_stats_ahash_final(unsigned int nbytes, int ret, |
| 1157 | struct crypto_alg *alg) |
| 1158 | { |
| 1159 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1160 | atomic64_inc(&alg->stats.hash.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1161 | } else { |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1162 | atomic64_inc(&alg->stats.hash.hash_cnt); |
| 1163 | atomic64_add(nbytes, &alg->stats.hash.hash_tlen); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1164 | } |
| 1165 | crypto_alg_put(alg); |
| 1166 | } |
| 1167 | EXPORT_SYMBOL_GPL(crypto_stats_ahash_final); |
| 1168 | |
| 1169 | void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret) |
| 1170 | { |
| 1171 | if (ret) |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1172 | atomic64_inc(&alg->stats.kpp.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1173 | else |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1174 | atomic64_inc(&alg->stats.kpp.setsecret_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1175 | crypto_alg_put(alg); |
| 1176 | } |
| 1177 | EXPORT_SYMBOL_GPL(crypto_stats_kpp_set_secret); |
| 1178 | |
| 1179 | void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret) |
| 1180 | { |
| 1181 | if (ret) |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1182 | atomic64_inc(&alg->stats.kpp.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1183 | else |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1184 | atomic64_inc(&alg->stats.kpp.generate_public_key_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1185 | crypto_alg_put(alg); |
| 1186 | } |
| 1187 | EXPORT_SYMBOL_GPL(crypto_stats_kpp_generate_public_key); |
| 1188 | |
| 1189 | void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret) |
| 1190 | { |
| 1191 | if (ret) |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1192 | atomic64_inc(&alg->stats.kpp.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1193 | else |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1194 | atomic64_inc(&alg->stats.kpp.compute_shared_secret_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1195 | crypto_alg_put(alg); |
| 1196 | } |
| 1197 | EXPORT_SYMBOL_GPL(crypto_stats_kpp_compute_shared_secret); |
| 1198 | |
| 1199 | void crypto_stats_rng_seed(struct crypto_alg *alg, int ret) |
| 1200 | { |
| 1201 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1202 | atomic64_inc(&alg->stats.rng.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1203 | else |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1204 | atomic64_inc(&alg->stats.rng.seed_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1205 | crypto_alg_put(alg); |
| 1206 | } |
| 1207 | EXPORT_SYMBOL_GPL(crypto_stats_rng_seed); |
| 1208 | |
| 1209 | void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, |
| 1210 | int ret) |
| 1211 | { |
| 1212 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1213 | atomic64_inc(&alg->stats.rng.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1214 | } else { |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1215 | atomic64_inc(&alg->stats.rng.generate_cnt); |
| 1216 | atomic64_add(dlen, &alg->stats.rng.generate_tlen); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1217 | } |
| 1218 | crypto_alg_put(alg); |
| 1219 | } |
| 1220 | EXPORT_SYMBOL_GPL(crypto_stats_rng_generate); |
| 1221 | |
| 1222 | void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, |
| 1223 | struct crypto_alg *alg) |
| 1224 | { |
| 1225 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1226 | atomic64_inc(&alg->stats.cipher.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1227 | } else { |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1228 | atomic64_inc(&alg->stats.cipher.encrypt_cnt); |
| 1229 | atomic64_add(cryptlen, &alg->stats.cipher.encrypt_tlen); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1230 | } |
| 1231 | crypto_alg_put(alg); |
| 1232 | } |
| 1233 | EXPORT_SYMBOL_GPL(crypto_stats_skcipher_encrypt); |
| 1234 | |
| 1235 | void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, |
| 1236 | struct crypto_alg *alg) |
| 1237 | { |
| 1238 | if (ret && ret != -EINPROGRESS && ret != -EBUSY) { |
Corentin Labbe | 44f1313 | 2018-11-29 14:42:25 +0000 | [diff] [blame] | 1239 | atomic64_inc(&alg->stats.cipher.err_cnt); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1240 | } else { |
Corentin Labbe | 17c18f9 | 2018-11-29 14:42:24 +0000 | [diff] [blame] | 1241 | atomic64_inc(&alg->stats.cipher.decrypt_cnt); |
| 1242 | atomic64_add(cryptlen, &alg->stats.cipher.decrypt_tlen); |
Corentin Labbe | f7d76e0 | 2018-11-29 14:42:21 +0000 | [diff] [blame] | 1243 | } |
| 1244 | crypto_alg_put(alg); |
| 1245 | } |
| 1246 | EXPORT_SYMBOL_GPL(crypto_stats_skcipher_decrypt); |
| 1247 | #endif |
| 1248 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 1249 | static int __init crypto_algapi_init(void) |
| 1250 | { |
| 1251 | crypto_init_proc(); |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | static void __exit crypto_algapi_exit(void) |
| 1256 | { |
| 1257 | crypto_exit_proc(); |
| 1258 | } |
| 1259 | |
| 1260 | module_init(crypto_algapi_init); |
| 1261 | module_exit(crypto_algapi_exit); |
| 1262 | |
| 1263 | MODULE_LICENSE("GPL"); |
| 1264 | MODULE_DESCRIPTION("Cryptographic algorithms API"); |