Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API for algorithms (i.e., low-level API). |
| 3 | * |
| 4 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 13 | #include <linux/err.h> |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 14 | #include <linux/errno.h> |
Herbert Xu | 3133d76 | 2015-04-22 13:25:53 +0800 | [diff] [blame] | 15 | #include <linux/fips.h> |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 18 | #include <linux/list.h> |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 19 | #include <linux/module.h> |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 20 | #include <linux/rtnetlink.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 22 | #include <linux/string.h> |
| 23 | |
| 24 | #include "internal.h" |
| 25 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 26 | static LIST_HEAD(crypto_template_list); |
| 27 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 28 | static inline int crypto_set_driver_name(struct crypto_alg *alg) |
| 29 | { |
| 30 | static const char suffix[] = "-generic"; |
| 31 | char *driver_name = alg->cra_driver_name; |
| 32 | int len; |
| 33 | |
| 34 | if (*driver_name) |
| 35 | return 0; |
| 36 | |
| 37 | len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); |
| 38 | if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME) |
| 39 | return -ENAMETOOLONG; |
| 40 | |
| 41 | memcpy(driver_name + len, suffix, sizeof(suffix)); |
| 42 | return 0; |
| 43 | } |
| 44 | |
Jarod Wilson | 002c77a | 2014-07-02 15:37:30 -0400 | [diff] [blame] | 45 | static inline void crypto_check_module_sig(struct module *mod) |
| 46 | { |
Herbert Xu | 59afdc7 | 2015-04-22 11:28:46 +0800 | [diff] [blame] | 47 | if (fips_enabled && mod && !module_sig_ok(mod)) |
Jarod Wilson | 002c77a | 2014-07-02 15:37:30 -0400 | [diff] [blame] | 48 | panic("Module %s signature verification failed in FIPS mode\n", |
Herbert Xu | bd4a7c6 | 2015-04-23 14:48:05 +0800 | [diff] [blame] | 49 | module_name(mod)); |
Jarod Wilson | 002c77a | 2014-07-02 15:37:30 -0400 | [diff] [blame] | 50 | } |
| 51 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 52 | static int crypto_check_alg(struct crypto_alg *alg) |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 53 | { |
Jarod Wilson | 002c77a | 2014-07-02 15:37:30 -0400 | [diff] [blame] | 54 | crypto_check_module_sig(alg->cra_module); |
| 55 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 56 | if (alg->cra_alignmask & (alg->cra_alignmask + 1)) |
| 57 | return -EINVAL; |
| 58 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 59 | if (alg->cra_blocksize > PAGE_SIZE / 8) |
| 60 | return -EINVAL; |
| 61 | |
| 62 | if (alg->cra_priority < 0) |
| 63 | return -EINVAL; |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 64 | |
Herbert Xu | e9b8e5b | 2015-04-09 17:40:35 +0800 | [diff] [blame] | 65 | atomic_set(&alg->cra_refcnt, 1); |
| 66 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 67 | return crypto_set_driver_name(alg); |
| 68 | } |
| 69 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 70 | static void crypto_destroy_instance(struct crypto_alg *alg) |
| 71 | { |
| 72 | struct crypto_instance *inst = (void *)alg; |
| 73 | struct crypto_template *tmpl = inst->tmpl; |
| 74 | |
| 75 | tmpl->free(inst); |
| 76 | crypto_tmpl_put(tmpl); |
| 77 | } |
| 78 | |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 79 | static struct list_head *crypto_more_spawns(struct crypto_alg *alg, |
| 80 | struct list_head *stack, |
| 81 | struct list_head *top, |
| 82 | struct list_head *secondary_spawns) |
| 83 | { |
| 84 | struct crypto_spawn *spawn, *n; |
| 85 | |
| 86 | if (list_empty(stack)) |
| 87 | return NULL; |
| 88 | |
| 89 | spawn = list_first_entry(stack, struct crypto_spawn, list); |
| 90 | n = list_entry(spawn->list.next, struct crypto_spawn, list); |
| 91 | |
| 92 | if (spawn->alg && &n->list != stack && !n->alg) |
| 93 | n->alg = (n->list.next == stack) ? alg : |
| 94 | &list_entry(n->list.next, struct crypto_spawn, |
| 95 | list)->inst->alg; |
| 96 | |
| 97 | list_move(&spawn->list, secondary_spawns); |
| 98 | |
| 99 | return &n->list == stack ? top : &n->inst->alg.cra_users; |
| 100 | } |
| 101 | |
Herbert Xu | 1f72371 | 2015-04-02 22:31:22 +0800 | [diff] [blame] | 102 | static void crypto_remove_instance(struct crypto_instance *inst, |
| 103 | struct list_head *list) |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 104 | { |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 105 | struct crypto_template *tmpl = inst->tmpl; |
| 106 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 107 | if (crypto_is_dead(&inst->alg)) |
| 108 | return; |
| 109 | |
| 110 | inst->alg.cra_flags |= CRYPTO_ALG_DEAD; |
Herbert Xu | 38cb241 | 2007-11-17 22:09:38 +0800 | [diff] [blame] | 111 | if (hlist_unhashed(&inst->list)) |
| 112 | return; |
| 113 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 114 | if (!tmpl || !crypto_tmpl_get(tmpl)) |
| 115 | return; |
| 116 | |
| 117 | crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg); |
| 118 | list_move(&inst->alg.cra_list, list); |
| 119 | hlist_del(&inst->list); |
| 120 | inst->alg.cra_destroy = crypto_destroy_instance; |
| 121 | |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 122 | BUG_ON(!list_empty(&inst->alg.cra_users)); |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 123 | } |
| 124 | |
Steffen Klassert | 89b596b | 2011-09-27 07:22:08 +0200 | [diff] [blame] | 125 | void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list, |
| 126 | struct crypto_alg *nalg) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 127 | { |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 128 | u32 new_type = (nalg ?: alg)->cra_flags; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 129 | struct crypto_spawn *spawn, *n; |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 130 | LIST_HEAD(secondary_spawns); |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 131 | struct list_head *spawns; |
| 132 | LIST_HEAD(stack); |
| 133 | LIST_HEAD(top); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 134 | |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 135 | spawns = &alg->cra_users; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 136 | list_for_each_entry_safe(spawn, n, spawns, list) { |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 137 | if ((spawn->alg->cra_flags ^ new_type) & spawn->mask) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 138 | continue; |
| 139 | |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 140 | list_move(&spawn->list, &top); |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 141 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 142 | |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 143 | spawns = ⊤ |
| 144 | do { |
| 145 | while (!list_empty(spawns)) { |
| 146 | struct crypto_instance *inst; |
| 147 | |
| 148 | spawn = list_first_entry(spawns, struct crypto_spawn, |
| 149 | list); |
| 150 | inst = spawn->inst; |
| 151 | |
| 152 | BUG_ON(&inst->alg == alg); |
| 153 | |
| 154 | list_move(&spawn->list, &stack); |
| 155 | |
| 156 | if (&inst->alg == nalg) |
| 157 | break; |
| 158 | |
| 159 | spawn->alg = NULL; |
| 160 | spawns = &inst->alg.cra_users; |
| 161 | } |
| 162 | } while ((spawns = crypto_more_spawns(alg, &stack, &top, |
| 163 | &secondary_spawns))); |
| 164 | |
| 165 | list_for_each_entry_safe(spawn, n, &secondary_spawns, list) { |
| 166 | if (spawn->alg) |
| 167 | list_move(&spawn->list, &spawn->alg->cra_users); |
| 168 | else |
Herbert Xu | 1f72371 | 2015-04-02 22:31:22 +0800 | [diff] [blame] | 169 | crypto_remove_instance(spawn->inst, list); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 170 | } |
| 171 | } |
Steffen Klassert | 89b596b | 2011-09-27 07:22:08 +0200 | [diff] [blame] | 172 | EXPORT_SYMBOL_GPL(crypto_remove_spawns); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 173 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 174 | static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg) |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 175 | { |
| 176 | struct crypto_alg *q; |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 177 | struct crypto_larval *larval; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 178 | int ret = -EAGAIN; |
| 179 | |
| 180 | if (crypto_is_dead(alg)) |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 181 | goto err; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 182 | |
| 183 | INIT_LIST_HEAD(&alg->cra_users); |
| 184 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 185 | /* No cheating! */ |
| 186 | alg->cra_flags &= ~CRYPTO_ALG_TESTED; |
| 187 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 188 | ret = -EEXIST; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 189 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 190 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 191 | if (q == alg) |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 192 | goto err; |
| 193 | |
Herbert Xu | b8e1599 | 2009-01-28 14:09:59 +1100 | [diff] [blame] | 194 | if (crypto_is_moribund(q)) |
| 195 | continue; |
| 196 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 197 | if (crypto_is_larval(q)) { |
| 198 | if (!strcmp(alg->cra_driver_name, q->cra_driver_name)) |
| 199 | goto err; |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | if (!strcmp(q->cra_driver_name, alg->cra_name) || |
| 204 | !strcmp(q->cra_name, alg->cra_driver_name)) |
| 205 | goto err; |
| 206 | } |
| 207 | |
| 208 | larval = crypto_larval_alloc(alg->cra_name, |
| 209 | alg->cra_flags | CRYPTO_ALG_TESTED, 0); |
| 210 | if (IS_ERR(larval)) |
| 211 | goto out; |
| 212 | |
| 213 | ret = -ENOENT; |
| 214 | larval->adult = crypto_mod_get(alg); |
| 215 | if (!larval->adult) |
| 216 | goto free_larval; |
| 217 | |
| 218 | atomic_set(&larval->alg.cra_refcnt, 1); |
| 219 | memcpy(larval->alg.cra_driver_name, alg->cra_driver_name, |
| 220 | CRYPTO_MAX_ALG_NAME); |
| 221 | larval->alg.cra_priority = alg->cra_priority; |
| 222 | |
| 223 | list_add(&alg->cra_list, &crypto_alg_list); |
| 224 | list_add(&larval->alg.cra_list, &crypto_alg_list); |
| 225 | |
Richard Hartmann | 5357c6c | 2010-02-16 20:25:21 +0800 | [diff] [blame] | 226 | out: |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 227 | return larval; |
| 228 | |
| 229 | free_larval: |
| 230 | kfree(larval); |
| 231 | err: |
| 232 | larval = ERR_PTR(ret); |
| 233 | goto out; |
| 234 | } |
| 235 | |
| 236 | void crypto_alg_tested(const char *name, int err) |
| 237 | { |
| 238 | struct crypto_larval *test; |
| 239 | struct crypto_alg *alg; |
| 240 | struct crypto_alg *q; |
| 241 | LIST_HEAD(list); |
| 242 | |
| 243 | down_write(&crypto_alg_sem); |
| 244 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
Herbert Xu | b8e1599 | 2009-01-28 14:09:59 +1100 | [diff] [blame] | 245 | if (crypto_is_moribund(q) || !crypto_is_larval(q)) |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 246 | continue; |
| 247 | |
| 248 | test = (struct crypto_larval *)q; |
| 249 | |
| 250 | if (!strcmp(q->cra_driver_name, name)) |
| 251 | goto found; |
| 252 | } |
| 253 | |
| 254 | printk(KERN_ERR "alg: Unexpected test result for %s: %d\n", name, err); |
| 255 | goto unlock; |
| 256 | |
| 257 | found: |
Herbert Xu | b8e1599 | 2009-01-28 14:09:59 +1100 | [diff] [blame] | 258 | q->cra_flags |= CRYPTO_ALG_DEAD; |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 259 | alg = test->adult; |
| 260 | if (err || list_empty(&alg->cra_list)) |
| 261 | goto complete; |
| 262 | |
| 263 | alg->cra_flags |= CRYPTO_ALG_TESTED; |
| 264 | |
| 265 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
| 266 | if (q == alg) |
| 267 | continue; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 268 | |
| 269 | if (crypto_is_moribund(q)) |
| 270 | continue; |
| 271 | |
| 272 | if (crypto_is_larval(q)) { |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 273 | struct crypto_larval *larval = (void *)q; |
| 274 | |
Herbert Xu | d805848 | 2007-10-02 22:27:29 +0800 | [diff] [blame] | 275 | /* |
| 276 | * Check to see if either our generic name or |
| 277 | * specific name can satisfy the name requested |
| 278 | * by the larval entry q. |
| 279 | */ |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 280 | if (strcmp(alg->cra_name, q->cra_name) && |
| 281 | strcmp(alg->cra_driver_name, q->cra_name)) |
| 282 | continue; |
| 283 | |
| 284 | if (larval->adult) |
| 285 | continue; |
Herbert Xu | 492e2b6 | 2006-09-21 11:35:17 +1000 | [diff] [blame] | 286 | if ((q->cra_flags ^ alg->cra_flags) & larval->mask) |
| 287 | continue; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 288 | if (!crypto_mod_get(alg)) |
| 289 | continue; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 290 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 291 | larval->adult = alg; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 292 | continue; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 293 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 294 | |
| 295 | if (strcmp(alg->cra_name, q->cra_name)) |
| 296 | continue; |
| 297 | |
| 298 | if (strcmp(alg->cra_driver_name, q->cra_driver_name) && |
| 299 | q->cra_priority > alg->cra_priority) |
| 300 | continue; |
| 301 | |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 302 | crypto_remove_spawns(q, &list, alg); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 303 | } |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 304 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 305 | complete: |
| 306 | complete_all(&test->completion); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 307 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 308 | unlock: |
| 309 | up_write(&crypto_alg_sem); |
| 310 | |
| 311 | crypto_remove_final(&list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 312 | } |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 313 | EXPORT_SYMBOL_GPL(crypto_alg_tested); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 314 | |
Steffen Klassert | 22e5b20 | 2011-09-27 07:23:07 +0200 | [diff] [blame] | 315 | void crypto_remove_final(struct list_head *list) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 316 | { |
| 317 | struct crypto_alg *alg; |
| 318 | struct crypto_alg *n; |
| 319 | |
| 320 | list_for_each_entry_safe(alg, n, list, cra_list) { |
| 321 | list_del_init(&alg->cra_list); |
| 322 | crypto_alg_put(alg); |
| 323 | } |
| 324 | } |
Steffen Klassert | 22e5b20 | 2011-09-27 07:23:07 +0200 | [diff] [blame] | 325 | EXPORT_SYMBOL_GPL(crypto_remove_final); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 326 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 327 | static void crypto_wait_for_test(struct crypto_larval *larval) |
| 328 | { |
| 329 | int err; |
| 330 | |
| 331 | err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult); |
| 332 | if (err != NOTIFY_STOP) { |
| 333 | if (WARN_ON(err != NOTIFY_DONE)) |
| 334 | goto out; |
| 335 | crypto_alg_tested(larval->alg.cra_driver_name, 0); |
| 336 | } |
| 337 | |
| 338 | err = wait_for_completion_interruptible(&larval->completion); |
| 339 | WARN_ON(err); |
| 340 | |
| 341 | out: |
| 342 | crypto_larval_kill(&larval->alg); |
| 343 | } |
| 344 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 345 | int crypto_register_alg(struct crypto_alg *alg) |
| 346 | { |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 347 | struct crypto_larval *larval; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 348 | int err; |
| 349 | |
| 350 | err = crypto_check_alg(alg); |
| 351 | if (err) |
| 352 | return err; |
| 353 | |
| 354 | down_write(&crypto_alg_sem); |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 355 | larval = __crypto_register_alg(alg); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 356 | up_write(&crypto_alg_sem); |
| 357 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 358 | if (IS_ERR(larval)) |
| 359 | return PTR_ERR(larval); |
| 360 | |
| 361 | crypto_wait_for_test(larval); |
| 362 | return 0; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 363 | } |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 364 | EXPORT_SYMBOL_GPL(crypto_register_alg); |
| 365 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 366 | static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list) |
| 367 | { |
| 368 | if (unlikely(list_empty(&alg->cra_list))) |
| 369 | return -ENOENT; |
| 370 | |
| 371 | alg->cra_flags |= CRYPTO_ALG_DEAD; |
| 372 | |
| 373 | crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg); |
| 374 | list_del_init(&alg->cra_list); |
Herbert Xu | 2bf2901 | 2009-08-31 15:56:54 +1000 | [diff] [blame] | 375 | crypto_remove_spawns(alg, list, NULL); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 380 | int crypto_unregister_alg(struct crypto_alg *alg) |
| 381 | { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 382 | int ret; |
| 383 | LIST_HEAD(list); |
Richard Hartmann | 5357c6c | 2010-02-16 20:25:21 +0800 | [diff] [blame] | 384 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 385 | down_write(&crypto_alg_sem); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 386 | ret = crypto_remove_alg(alg, &list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 387 | up_write(&crypto_alg_sem); |
| 388 | |
| 389 | if (ret) |
| 390 | return ret; |
| 391 | |
| 392 | BUG_ON(atomic_read(&alg->cra_refcnt) != 1); |
| 393 | if (alg->cra_destroy) |
| 394 | alg->cra_destroy(alg); |
| 395 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 396 | crypto_remove_final(&list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | EXPORT_SYMBOL_GPL(crypto_unregister_alg); |
| 400 | |
Mark Brown | 4b00434 | 2012-01-17 23:34:26 +0000 | [diff] [blame] | 401 | int crypto_register_algs(struct crypto_alg *algs, int count) |
| 402 | { |
| 403 | int i, ret; |
| 404 | |
| 405 | for (i = 0; i < count; i++) { |
| 406 | ret = crypto_register_alg(&algs[i]); |
| 407 | if (ret) |
| 408 | goto err; |
| 409 | } |
| 410 | |
| 411 | return 0; |
| 412 | |
| 413 | err: |
| 414 | for (--i; i >= 0; --i) |
| 415 | crypto_unregister_alg(&algs[i]); |
| 416 | |
| 417 | return ret; |
| 418 | } |
| 419 | EXPORT_SYMBOL_GPL(crypto_register_algs); |
| 420 | |
| 421 | int crypto_unregister_algs(struct crypto_alg *algs, int count) |
| 422 | { |
| 423 | int i, ret; |
| 424 | |
| 425 | for (i = 0; i < count; i++) { |
| 426 | ret = crypto_unregister_alg(&algs[i]); |
| 427 | if (ret) |
| 428 | pr_err("Failed to unregister %s %s: %d\n", |
| 429 | algs[i].cra_driver_name, algs[i].cra_name, ret); |
| 430 | } |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | EXPORT_SYMBOL_GPL(crypto_unregister_algs); |
| 435 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 436 | int crypto_register_template(struct crypto_template *tmpl) |
| 437 | { |
| 438 | struct crypto_template *q; |
| 439 | int err = -EEXIST; |
| 440 | |
| 441 | down_write(&crypto_alg_sem); |
| 442 | |
Jarod Wilson | 002c77a | 2014-07-02 15:37:30 -0400 | [diff] [blame] | 443 | crypto_check_module_sig(tmpl->module); |
| 444 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 445 | list_for_each_entry(q, &crypto_template_list, list) { |
| 446 | if (q == tmpl) |
| 447 | goto out; |
| 448 | } |
| 449 | |
| 450 | list_add(&tmpl->list, &crypto_template_list); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 451 | crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 452 | err = 0; |
| 453 | out: |
| 454 | up_write(&crypto_alg_sem); |
| 455 | return err; |
| 456 | } |
| 457 | EXPORT_SYMBOL_GPL(crypto_register_template); |
| 458 | |
| 459 | void crypto_unregister_template(struct crypto_template *tmpl) |
| 460 | { |
| 461 | struct crypto_instance *inst; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 462 | struct hlist_node *n; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 463 | struct hlist_head *list; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 464 | LIST_HEAD(users); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 465 | |
| 466 | down_write(&crypto_alg_sem); |
| 467 | |
| 468 | BUG_ON(list_empty(&tmpl->list)); |
| 469 | list_del_init(&tmpl->list); |
| 470 | |
| 471 | list = &tmpl->instances; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 472 | hlist_for_each_entry(inst, list, list) { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 473 | int err = crypto_remove_alg(&inst->alg, &users); |
Joshua I. James | 0efcb8d | 2014-12-05 15:00:10 +0900 | [diff] [blame] | 474 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 475 | BUG_ON(err); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 476 | } |
| 477 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 478 | crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl); |
| 479 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 480 | up_write(&crypto_alg_sem); |
| 481 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 482 | hlist_for_each_entry_safe(inst, n, list, list) { |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 483 | BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1); |
| 484 | tmpl->free(inst); |
| 485 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 486 | crypto_remove_final(&users); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 487 | } |
| 488 | EXPORT_SYMBOL_GPL(crypto_unregister_template); |
| 489 | |
| 490 | static struct crypto_template *__crypto_lookup_template(const char *name) |
| 491 | { |
| 492 | struct crypto_template *q, *tmpl = NULL; |
| 493 | |
| 494 | down_read(&crypto_alg_sem); |
| 495 | list_for_each_entry(q, &crypto_template_list, list) { |
| 496 | if (strcmp(q->name, name)) |
| 497 | continue; |
| 498 | if (unlikely(!crypto_tmpl_get(q))) |
| 499 | continue; |
| 500 | |
| 501 | tmpl = q; |
| 502 | break; |
| 503 | } |
| 504 | up_read(&crypto_alg_sem); |
| 505 | |
| 506 | return tmpl; |
| 507 | } |
| 508 | |
| 509 | struct crypto_template *crypto_lookup_template(const char *name) |
| 510 | { |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 511 | return try_then_request_module(__crypto_lookup_template(name), |
| 512 | "crypto-%s", name); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 513 | } |
| 514 | EXPORT_SYMBOL_GPL(crypto_lookup_template); |
| 515 | |
| 516 | int crypto_register_instance(struct crypto_template *tmpl, |
| 517 | struct crypto_instance *inst) |
| 518 | { |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 519 | struct crypto_larval *larval; |
| 520 | int err; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 521 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 522 | err = crypto_check_alg(&inst->alg); |
| 523 | if (err) |
Stephan Mueller | 9c521a2 | 2015-04-09 12:09:55 +0200 | [diff] [blame] | 524 | return err; |
| 525 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 526 | inst->alg.cra_module = tmpl->module; |
Steffen Klassert | 64a947b | 2011-09-27 07:21:26 +0200 | [diff] [blame] | 527 | inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 528 | |
Herbert Xu | 34c9a0f | 2015-04-16 11:07:13 +0800 | [diff] [blame] | 529 | if (unlikely(!crypto_mod_get(&inst->alg))) |
| 530 | return -EAGAIN; |
| 531 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 532 | down_write(&crypto_alg_sem); |
| 533 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 534 | larval = __crypto_register_alg(&inst->alg); |
| 535 | if (IS_ERR(larval)) |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 536 | goto unlock; |
| 537 | |
| 538 | hlist_add_head(&inst->list, &tmpl->instances); |
| 539 | inst->tmpl = tmpl; |
| 540 | |
| 541 | unlock: |
| 542 | up_write(&crypto_alg_sem); |
| 543 | |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 544 | err = PTR_ERR(larval); |
| 545 | if (IS_ERR(larval)) |
| 546 | goto err; |
| 547 | |
| 548 | crypto_wait_for_test(larval); |
Stephan Mueller | 9c521a2 | 2015-04-09 12:09:55 +0200 | [diff] [blame] | 549 | |
| 550 | /* Remove instance if test failed */ |
| 551 | if (!(inst->alg.cra_flags & CRYPTO_ALG_TESTED)) |
| 552 | crypto_unregister_instance(inst); |
Herbert Xu | 73d3864 | 2008-08-03 21:15:23 +0800 | [diff] [blame] | 553 | err = 0; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 554 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 555 | err: |
Stephan Mueller | 9c521a2 | 2015-04-09 12:09:55 +0200 | [diff] [blame] | 556 | crypto_mod_put(&inst->alg); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 557 | return err; |
| 558 | } |
| 559 | EXPORT_SYMBOL_GPL(crypto_register_instance); |
| 560 | |
Herbert Xu | 87b1675 | 2015-04-02 22:39:40 +0800 | [diff] [blame] | 561 | int crypto_unregister_instance(struct crypto_instance *inst) |
Steffen Klassert | ce3fd84 | 2011-11-08 10:09:17 +0100 | [diff] [blame] | 562 | { |
Herbert Xu | 1f72371 | 2015-04-02 22:31:22 +0800 | [diff] [blame] | 563 | LIST_HEAD(list); |
Steffen Klassert | ce3fd84 | 2011-11-08 10:09:17 +0100 | [diff] [blame] | 564 | |
Steffen Klassert | ce3fd84 | 2011-11-08 10:09:17 +0100 | [diff] [blame] | 565 | down_write(&crypto_alg_sem); |
| 566 | |
Herbert Xu | 87b1675 | 2015-04-02 22:39:40 +0800 | [diff] [blame] | 567 | crypto_remove_spawns(&inst->alg, &list, NULL); |
Herbert Xu | 1f72371 | 2015-04-02 22:31:22 +0800 | [diff] [blame] | 568 | crypto_remove_instance(inst, &list); |
Steffen Klassert | ce3fd84 | 2011-11-08 10:09:17 +0100 | [diff] [blame] | 569 | |
| 570 | up_write(&crypto_alg_sem); |
| 571 | |
Herbert Xu | 1f72371 | 2015-04-02 22:31:22 +0800 | [diff] [blame] | 572 | crypto_remove_final(&list); |
Steffen Klassert | ce3fd84 | 2011-11-08 10:09:17 +0100 | [diff] [blame] | 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | EXPORT_SYMBOL_GPL(crypto_unregister_instance); |
| 577 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 578 | int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg, |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 579 | struct crypto_instance *inst, u32 mask) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 580 | { |
| 581 | int err = -EAGAIN; |
| 582 | |
| 583 | spawn->inst = inst; |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 584 | spawn->mask = mask; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 585 | |
| 586 | down_write(&crypto_alg_sem); |
| 587 | if (!crypto_is_moribund(alg)) { |
| 588 | list_add(&spawn->list, &alg->cra_users); |
| 589 | spawn->alg = alg; |
| 590 | err = 0; |
| 591 | } |
| 592 | up_write(&crypto_alg_sem); |
| 593 | |
| 594 | return err; |
| 595 | } |
| 596 | EXPORT_SYMBOL_GPL(crypto_init_spawn); |
| 597 | |
Herbert Xu | 97eedce | 2009-07-08 15:55:52 +0800 | [diff] [blame] | 598 | int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg, |
| 599 | struct crypto_instance *inst, |
| 600 | const struct crypto_type *frontend) |
| 601 | { |
| 602 | int err = -EINVAL; |
| 603 | |
Dan Carpenter | c614e10 | 2010-05-03 11:08:15 +0800 | [diff] [blame] | 604 | if ((alg->cra_flags ^ frontend->type) & frontend->maskset) |
Herbert Xu | 97eedce | 2009-07-08 15:55:52 +0800 | [diff] [blame] | 605 | goto out; |
| 606 | |
| 607 | spawn->frontend = frontend; |
| 608 | err = crypto_init_spawn(spawn, alg, inst, frontend->maskset); |
| 609 | |
| 610 | out: |
| 611 | return err; |
| 612 | } |
| 613 | EXPORT_SYMBOL_GPL(crypto_init_spawn2); |
| 614 | |
Herbert Xu | d6ef2f1 | 2015-05-11 17:47:39 +0800 | [diff] [blame^] | 615 | int crypto_grab_spawn(struct crypto_spawn *spawn, const char *name, |
| 616 | u32 type, u32 mask) |
| 617 | { |
| 618 | struct crypto_alg *alg; |
| 619 | int err; |
| 620 | |
| 621 | alg = crypto_find_alg(name, spawn->frontend, type, mask); |
| 622 | if (IS_ERR(alg)) |
| 623 | return PTR_ERR(alg); |
| 624 | |
| 625 | err = crypto_init_spawn(spawn, alg, spawn->inst, mask); |
| 626 | crypto_mod_put(alg); |
| 627 | return err; |
| 628 | } |
| 629 | EXPORT_SYMBOL_GPL(crypto_grab_spawn); |
| 630 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 631 | void crypto_drop_spawn(struct crypto_spawn *spawn) |
| 632 | { |
Herbert Xu | 7ede5a5 | 2009-07-09 11:34:06 +0800 | [diff] [blame] | 633 | if (!spawn->alg) |
| 634 | return; |
| 635 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 636 | down_write(&crypto_alg_sem); |
| 637 | list_del(&spawn->list); |
| 638 | up_write(&crypto_alg_sem); |
| 639 | } |
| 640 | EXPORT_SYMBOL_GPL(crypto_drop_spawn); |
| 641 | |
Herbert Xu | 97eedce | 2009-07-08 15:55:52 +0800 | [diff] [blame] | 642 | static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 643 | { |
| 644 | struct crypto_alg *alg; |
| 645 | struct crypto_alg *alg2; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 646 | |
| 647 | down_read(&crypto_alg_sem); |
| 648 | alg = spawn->alg; |
| 649 | alg2 = alg; |
| 650 | if (alg2) |
| 651 | alg2 = crypto_mod_get(alg2); |
| 652 | up_read(&crypto_alg_sem); |
| 653 | |
| 654 | if (!alg2) { |
| 655 | if (alg) |
| 656 | crypto_shoot_alg(alg); |
| 657 | return ERR_PTR(-EAGAIN); |
| 658 | } |
| 659 | |
Herbert Xu | 97eedce | 2009-07-08 15:55:52 +0800 | [diff] [blame] | 660 | return alg; |
| 661 | } |
| 662 | |
| 663 | struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type, |
| 664 | u32 mask) |
| 665 | { |
| 666 | struct crypto_alg *alg; |
| 667 | struct crypto_tfm *tfm; |
| 668 | |
| 669 | alg = crypto_spawn_alg(spawn); |
| 670 | if (IS_ERR(alg)) |
| 671 | return ERR_CAST(alg); |
| 672 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 673 | tfm = ERR_PTR(-EINVAL); |
| 674 | if (unlikely((alg->cra_flags ^ type) & mask)) |
| 675 | goto out_put_alg; |
| 676 | |
Herbert Xu | 27d2a33 | 2007-01-24 20:50:26 +1100 | [diff] [blame] | 677 | tfm = __crypto_alloc_tfm(alg, type, mask); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 678 | if (IS_ERR(tfm)) |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 679 | goto out_put_alg; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 680 | |
| 681 | return tfm; |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 682 | |
| 683 | out_put_alg: |
| 684 | crypto_mod_put(alg); |
| 685 | return tfm; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 686 | } |
| 687 | EXPORT_SYMBOL_GPL(crypto_spawn_tfm); |
| 688 | |
Herbert Xu | 97eedce | 2009-07-08 15:55:52 +0800 | [diff] [blame] | 689 | void *crypto_spawn_tfm2(struct crypto_spawn *spawn) |
| 690 | { |
| 691 | struct crypto_alg *alg; |
| 692 | struct crypto_tfm *tfm; |
| 693 | |
| 694 | alg = crypto_spawn_alg(spawn); |
| 695 | if (IS_ERR(alg)) |
| 696 | return ERR_CAST(alg); |
| 697 | |
| 698 | tfm = crypto_create_tfm(alg, spawn->frontend); |
| 699 | if (IS_ERR(tfm)) |
| 700 | goto out_put_alg; |
| 701 | |
| 702 | return tfm; |
| 703 | |
| 704 | out_put_alg: |
| 705 | crypto_mod_put(alg); |
| 706 | return tfm; |
| 707 | } |
| 708 | EXPORT_SYMBOL_GPL(crypto_spawn_tfm2); |
| 709 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 710 | int crypto_register_notifier(struct notifier_block *nb) |
| 711 | { |
| 712 | return blocking_notifier_chain_register(&crypto_chain, nb); |
| 713 | } |
| 714 | EXPORT_SYMBOL_GPL(crypto_register_notifier); |
| 715 | |
| 716 | int crypto_unregister_notifier(struct notifier_block *nb) |
| 717 | { |
| 718 | return blocking_notifier_chain_unregister(&crypto_chain, nb); |
| 719 | } |
| 720 | EXPORT_SYMBOL_GPL(crypto_unregister_notifier); |
| 721 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 722 | struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 723 | { |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 724 | struct rtattr *rta = tb[0]; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 725 | struct crypto_attr_type *algt; |
| 726 | |
| 727 | if (!rta) |
| 728 | return ERR_PTR(-ENOENT); |
| 729 | if (RTA_PAYLOAD(rta) < sizeof(*algt)) |
| 730 | return ERR_PTR(-EINVAL); |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 731 | if (rta->rta_type != CRYPTOA_TYPE) |
| 732 | return ERR_PTR(-EINVAL); |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 733 | |
| 734 | algt = RTA_DATA(rta); |
| 735 | |
| 736 | return algt; |
| 737 | } |
| 738 | EXPORT_SYMBOL_GPL(crypto_get_attr_type); |
| 739 | |
| 740 | int crypto_check_attr_type(struct rtattr **tb, u32 type) |
| 741 | { |
| 742 | struct crypto_attr_type *algt; |
| 743 | |
| 744 | algt = crypto_get_attr_type(tb); |
| 745 | if (IS_ERR(algt)) |
| 746 | return PTR_ERR(algt); |
| 747 | |
| 748 | if ((algt->type ^ type) & algt->mask) |
| 749 | return -EINVAL; |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | EXPORT_SYMBOL_GPL(crypto_check_attr_type); |
| 754 | |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 755 | const char *crypto_attr_alg_name(struct rtattr *rta) |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 756 | { |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 757 | struct crypto_attr_alg *alga; |
| 758 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 759 | if (!rta) |
| 760 | return ERR_PTR(-ENOENT); |
| 761 | if (RTA_PAYLOAD(rta) < sizeof(*alga)) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 762 | return ERR_PTR(-EINVAL); |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 763 | if (rta->rta_type != CRYPTOA_ALG) |
| 764 | return ERR_PTR(-EINVAL); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 765 | |
| 766 | alga = RTA_DATA(rta); |
| 767 | alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0; |
| 768 | |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 769 | return alga->name; |
| 770 | } |
| 771 | EXPORT_SYMBOL_GPL(crypto_attr_alg_name); |
| 772 | |
Herbert Xu | d06854f | 2009-07-08 17:53:16 +0800 | [diff] [blame] | 773 | struct crypto_alg *crypto_attr_alg2(struct rtattr *rta, |
| 774 | const struct crypto_type *frontend, |
| 775 | u32 type, u32 mask) |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 776 | { |
| 777 | const char *name; |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 778 | |
| 779 | name = crypto_attr_alg_name(rta); |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 780 | if (IS_ERR(name)) |
Julia Lawall | 3e8afe3 | 2013-01-22 12:29:26 +0100 | [diff] [blame] | 781 | return ERR_CAST(name); |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 782 | |
Herbert Xu | d06854f | 2009-07-08 17:53:16 +0800 | [diff] [blame] | 783 | return crypto_find_alg(name, frontend, type, mask); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 784 | } |
Herbert Xu | d06854f | 2009-07-08 17:53:16 +0800 | [diff] [blame] | 785 | EXPORT_SYMBOL_GPL(crypto_attr_alg2); |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 786 | |
| 787 | int crypto_attr_u32(struct rtattr *rta, u32 *num) |
| 788 | { |
| 789 | struct crypto_attr_u32 *nu32; |
| 790 | |
| 791 | if (!rta) |
| 792 | return -ENOENT; |
| 793 | if (RTA_PAYLOAD(rta) < sizeof(*nu32)) |
| 794 | return -EINVAL; |
| 795 | if (rta->rta_type != CRYPTOA_U32) |
| 796 | return -EINVAL; |
| 797 | |
| 798 | nu32 = RTA_DATA(rta); |
| 799 | *num = nu32->num; |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | EXPORT_SYMBOL_GPL(crypto_attr_u32); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 804 | |
Herbert Xu | 70ec7bb | 2009-07-07 14:07:37 +0800 | [diff] [blame] | 805 | void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg, |
| 806 | unsigned int head) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 807 | { |
| 808 | struct crypto_instance *inst; |
Herbert Xu | 70ec7bb | 2009-07-07 14:07:37 +0800 | [diff] [blame] | 809 | char *p; |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 810 | int err; |
| 811 | |
Herbert Xu | 70ec7bb | 2009-07-07 14:07:37 +0800 | [diff] [blame] | 812 | p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn), |
| 813 | GFP_KERNEL); |
| 814 | if (!p) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 815 | return ERR_PTR(-ENOMEM); |
| 816 | |
Herbert Xu | 70ec7bb | 2009-07-07 14:07:37 +0800 | [diff] [blame] | 817 | inst = (void *)(p + head); |
| 818 | |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 819 | err = -ENAMETOOLONG; |
| 820 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name, |
| 821 | alg->cra_name) >= CRYPTO_MAX_ALG_NAME) |
| 822 | goto err_free_inst; |
| 823 | |
| 824 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", |
| 825 | name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
| 826 | goto err_free_inst; |
| 827 | |
Herbert Xu | 70ec7bb | 2009-07-07 14:07:37 +0800 | [diff] [blame] | 828 | return p; |
| 829 | |
| 830 | err_free_inst: |
| 831 | kfree(p); |
| 832 | return ERR_PTR(err); |
| 833 | } |
| 834 | EXPORT_SYMBOL_GPL(crypto_alloc_instance2); |
| 835 | |
| 836 | struct crypto_instance *crypto_alloc_instance(const char *name, |
| 837 | struct crypto_alg *alg) |
| 838 | { |
| 839 | struct crypto_instance *inst; |
| 840 | struct crypto_spawn *spawn; |
| 841 | int err; |
| 842 | |
| 843 | inst = crypto_alloc_instance2(name, alg, 0); |
| 844 | if (IS_ERR(inst)) |
| 845 | goto out; |
| 846 | |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 847 | spawn = crypto_instance_ctx(inst); |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 848 | err = crypto_init_spawn(spawn, alg, inst, |
| 849 | CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 850 | |
| 851 | if (err) |
| 852 | goto err_free_inst; |
| 853 | |
| 854 | return inst; |
| 855 | |
| 856 | err_free_inst: |
| 857 | kfree(inst); |
Herbert Xu | 70ec7bb | 2009-07-07 14:07:37 +0800 | [diff] [blame] | 858 | inst = ERR_PTR(err); |
| 859 | |
| 860 | out: |
| 861 | return inst; |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 862 | } |
| 863 | EXPORT_SYMBOL_GPL(crypto_alloc_instance); |
| 864 | |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 865 | void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen) |
| 866 | { |
| 867 | INIT_LIST_HEAD(&queue->list); |
| 868 | queue->backlog = &queue->list; |
| 869 | queue->qlen = 0; |
| 870 | queue->max_qlen = max_qlen; |
| 871 | } |
| 872 | EXPORT_SYMBOL_GPL(crypto_init_queue); |
| 873 | |
| 874 | int crypto_enqueue_request(struct crypto_queue *queue, |
| 875 | struct crypto_async_request *request) |
| 876 | { |
| 877 | int err = -EINPROGRESS; |
| 878 | |
| 879 | if (unlikely(queue->qlen >= queue->max_qlen)) { |
| 880 | err = -EBUSY; |
| 881 | if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) |
| 882 | goto out; |
| 883 | if (queue->backlog == &queue->list) |
| 884 | queue->backlog = &request->list; |
| 885 | } |
| 886 | |
| 887 | queue->qlen++; |
| 888 | list_add_tail(&request->list, &queue->list); |
| 889 | |
| 890 | out: |
| 891 | return err; |
| 892 | } |
| 893 | EXPORT_SYMBOL_GPL(crypto_enqueue_request); |
| 894 | |
Herbert Xu | 0c7d400 | 2009-08-29 20:44:04 +1000 | [diff] [blame] | 895 | void *__crypto_dequeue_request(struct crypto_queue *queue, unsigned int offset) |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 896 | { |
| 897 | struct list_head *request; |
| 898 | |
| 899 | if (unlikely(!queue->qlen)) |
| 900 | return NULL; |
| 901 | |
| 902 | queue->qlen--; |
| 903 | |
| 904 | if (queue->backlog != &queue->list) |
| 905 | queue->backlog = queue->backlog->next; |
| 906 | |
| 907 | request = queue->list.next; |
| 908 | list_del(request); |
| 909 | |
Herbert Xu | 0c7d400 | 2009-08-29 20:44:04 +1000 | [diff] [blame] | 910 | return (char *)list_entry(request, struct crypto_async_request, list) - |
| 911 | offset; |
| 912 | } |
| 913 | EXPORT_SYMBOL_GPL(__crypto_dequeue_request); |
| 914 | |
| 915 | struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue) |
| 916 | { |
| 917 | return __crypto_dequeue_request(queue, 0); |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 918 | } |
| 919 | EXPORT_SYMBOL_GPL(crypto_dequeue_request); |
| 920 | |
| 921 | int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm) |
| 922 | { |
| 923 | struct crypto_async_request *req; |
| 924 | |
| 925 | list_for_each_entry(req, &queue->list, list) { |
| 926 | if (req->tfm == tfm) |
| 927 | return 1; |
| 928 | } |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | EXPORT_SYMBOL_GPL(crypto_tfm_in_queue); |
| 933 | |
Herbert Xu | 7613636 | 2007-11-20 17:26:06 +0800 | [diff] [blame] | 934 | static inline void crypto_inc_byte(u8 *a, unsigned int size) |
| 935 | { |
| 936 | u8 *b = (a + size); |
| 937 | u8 c; |
| 938 | |
| 939 | for (; size; size--) { |
| 940 | c = *--b + 1; |
| 941 | *b = c; |
| 942 | if (c) |
| 943 | break; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | void crypto_inc(u8 *a, unsigned int size) |
| 948 | { |
| 949 | __be32 *b = (__be32 *)(a + size); |
| 950 | u32 c; |
| 951 | |
| 952 | for (; size >= 4; size -= 4) { |
| 953 | c = be32_to_cpu(*--b) + 1; |
| 954 | *b = cpu_to_be32(c); |
| 955 | if (c) |
| 956 | return; |
| 957 | } |
| 958 | |
| 959 | crypto_inc_byte(a, size); |
| 960 | } |
| 961 | EXPORT_SYMBOL_GPL(crypto_inc); |
| 962 | |
| 963 | static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size) |
| 964 | { |
| 965 | for (; size; size--) |
| 966 | *a++ ^= *b++; |
| 967 | } |
| 968 | |
| 969 | void crypto_xor(u8 *dst, const u8 *src, unsigned int size) |
| 970 | { |
| 971 | u32 *a = (u32 *)dst; |
| 972 | u32 *b = (u32 *)src; |
| 973 | |
| 974 | for (; size >= 4; size -= 4) |
| 975 | *a++ ^= *b++; |
| 976 | |
| 977 | crypto_xor_byte((u8 *)a, (u8 *)b, size); |
| 978 | } |
| 979 | EXPORT_SYMBOL_GPL(crypto_xor); |
| 980 | |
Herbert Xu | 38d2143 | 2015-04-20 13:39:00 +0800 | [diff] [blame] | 981 | unsigned int crypto_alg_extsize(struct crypto_alg *alg) |
| 982 | { |
| 983 | return alg->cra_ctxsize; |
| 984 | } |
| 985 | EXPORT_SYMBOL_GPL(crypto_alg_extsize); |
| 986 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 987 | static int __init crypto_algapi_init(void) |
| 988 | { |
| 989 | crypto_init_proc(); |
| 990 | return 0; |
| 991 | } |
| 992 | |
| 993 | static void __exit crypto_algapi_exit(void) |
| 994 | { |
| 995 | crypto_exit_proc(); |
| 996 | } |
| 997 | |
| 998 | module_init(crypto_algapi_init); |
| 999 | module_exit(crypto_algapi_exit); |
| 1000 | |
| 1001 | MODULE_LICENSE("GPL"); |
| 1002 | MODULE_DESCRIPTION("Cryptographic algorithms API"); |