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