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