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