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 | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 24 | static LIST_HEAD(crypto_template_list); |
| 25 | |
Herbert Xu | 492e2b6 | 2006-09-21 11:35:17 +1000 | [diff] [blame] | 26 | void crypto_larval_error(const char *name, u32 type, u32 mask) |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 27 | { |
| 28 | struct crypto_alg *alg; |
| 29 | |
| 30 | down_read(&crypto_alg_sem); |
Herbert Xu | 492e2b6 | 2006-09-21 11:35:17 +1000 | [diff] [blame] | 31 | alg = __crypto_alg_lookup(name, type, mask); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 32 | up_read(&crypto_alg_sem); |
| 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 | |
| 66 | if (alg->cra_alignmask & alg->cra_blocksize) |
| 67 | return -EINVAL; |
| 68 | |
| 69 | if (alg->cra_blocksize > PAGE_SIZE / 8) |
| 70 | return -EINVAL; |
| 71 | |
| 72 | if (alg->cra_priority < 0) |
| 73 | return -EINVAL; |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 74 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 75 | return crypto_set_driver_name(alg); |
| 76 | } |
| 77 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 78 | static void crypto_destroy_instance(struct crypto_alg *alg) |
| 79 | { |
| 80 | struct crypto_instance *inst = (void *)alg; |
| 81 | struct crypto_template *tmpl = inst->tmpl; |
| 82 | |
| 83 | tmpl->free(inst); |
| 84 | crypto_tmpl_put(tmpl); |
| 85 | } |
| 86 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 87 | static void crypto_remove_spawn(struct crypto_spawn *spawn, |
| 88 | struct list_head *list, |
| 89 | struct list_head *secondary_spawns) |
| 90 | { |
| 91 | struct crypto_instance *inst = spawn->inst; |
| 92 | struct crypto_template *tmpl = inst->tmpl; |
| 93 | |
| 94 | list_del_init(&spawn->list); |
| 95 | spawn->alg = NULL; |
| 96 | |
| 97 | if (crypto_is_dead(&inst->alg)) |
| 98 | return; |
| 99 | |
| 100 | inst->alg.cra_flags |= CRYPTO_ALG_DEAD; |
| 101 | if (!tmpl || !crypto_tmpl_get(tmpl)) |
| 102 | return; |
| 103 | |
| 104 | crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg); |
| 105 | list_move(&inst->alg.cra_list, list); |
| 106 | hlist_del(&inst->list); |
| 107 | inst->alg.cra_destroy = crypto_destroy_instance; |
| 108 | |
| 109 | list_splice(&inst->alg.cra_users, secondary_spawns); |
| 110 | } |
| 111 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 112 | static void crypto_remove_spawns(struct list_head *spawns, |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 113 | struct list_head *list, u32 new_type) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 114 | { |
| 115 | struct crypto_spawn *spawn, *n; |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 116 | LIST_HEAD(secondary_spawns); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 117 | |
| 118 | list_for_each_entry_safe(spawn, n, spawns, list) { |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 119 | if ((spawn->alg->cra_flags ^ new_type) & spawn->mask) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 120 | continue; |
| 121 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 122 | crypto_remove_spawn(spawn, list, &secondary_spawns); |
| 123 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 124 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 125 | while (!list_empty(&secondary_spawns)) { |
| 126 | list_for_each_entry_safe(spawn, n, &secondary_spawns, list) |
| 127 | crypto_remove_spawn(spawn, list, &secondary_spawns); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | static int __crypto_register_alg(struct crypto_alg *alg, |
| 132 | struct list_head *list) |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 133 | { |
| 134 | struct crypto_alg *q; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 135 | int ret = -EAGAIN; |
| 136 | |
| 137 | if (crypto_is_dead(alg)) |
| 138 | goto out; |
| 139 | |
| 140 | INIT_LIST_HEAD(&alg->cra_users); |
| 141 | |
| 142 | ret = -EEXIST; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 143 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 144 | atomic_set(&alg->cra_refcnt, 1); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 145 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 146 | if (q == alg) |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 147 | goto out; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 148 | |
| 149 | if (crypto_is_moribund(q)) |
| 150 | continue; |
| 151 | |
| 152 | if (crypto_is_larval(q)) { |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 153 | struct crypto_larval *larval = (void *)q; |
| 154 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 155 | if (strcmp(alg->cra_name, q->cra_name) && |
| 156 | strcmp(alg->cra_driver_name, q->cra_name)) |
| 157 | continue; |
| 158 | |
| 159 | if (larval->adult) |
| 160 | continue; |
Herbert Xu | 492e2b6 | 2006-09-21 11:35:17 +1000 | [diff] [blame] | 161 | if ((q->cra_flags ^ alg->cra_flags) & larval->mask) |
| 162 | continue; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 163 | if (!crypto_mod_get(alg)) |
| 164 | continue; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 165 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 166 | larval->adult = alg; |
Herbert Xu | fe3c520 | 2007-05-19 17:51:40 +1000 | [diff] [blame] | 167 | complete_all(&larval->completion); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 168 | continue; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 169 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 170 | |
| 171 | if (strcmp(alg->cra_name, q->cra_name)) |
| 172 | continue; |
| 173 | |
| 174 | if (strcmp(alg->cra_driver_name, q->cra_driver_name) && |
| 175 | q->cra_priority > alg->cra_priority) |
| 176 | continue; |
| 177 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 178 | crypto_remove_spawns(&q->cra_users, list, alg->cra_flags); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | list_add(&alg->cra_list, &crypto_alg_list); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 182 | |
| 183 | crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 184 | ret = 0; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 185 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 186 | out: |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 187 | return ret; |
| 188 | } |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 189 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 190 | static void crypto_remove_final(struct list_head *list) |
| 191 | { |
| 192 | struct crypto_alg *alg; |
| 193 | struct crypto_alg *n; |
| 194 | |
| 195 | list_for_each_entry_safe(alg, n, list, cra_list) { |
| 196 | list_del_init(&alg->cra_list); |
| 197 | crypto_alg_put(alg); |
| 198 | } |
| 199 | } |
| 200 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 201 | int crypto_register_alg(struct crypto_alg *alg) |
| 202 | { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 203 | LIST_HEAD(list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 204 | int err; |
| 205 | |
| 206 | err = crypto_check_alg(alg); |
| 207 | if (err) |
| 208 | return err; |
| 209 | |
| 210 | down_write(&crypto_alg_sem); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 211 | err = __crypto_register_alg(alg, &list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 212 | up_write(&crypto_alg_sem); |
| 213 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 214 | crypto_remove_final(&list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 215 | return err; |
| 216 | } |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 217 | EXPORT_SYMBOL_GPL(crypto_register_alg); |
| 218 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 219 | static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list) |
| 220 | { |
| 221 | if (unlikely(list_empty(&alg->cra_list))) |
| 222 | return -ENOENT; |
| 223 | |
| 224 | alg->cra_flags |= CRYPTO_ALG_DEAD; |
| 225 | |
| 226 | crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg); |
| 227 | list_del_init(&alg->cra_list); |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 228 | crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 233 | int crypto_unregister_alg(struct crypto_alg *alg) |
| 234 | { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 235 | int ret; |
| 236 | LIST_HEAD(list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 237 | |
| 238 | down_write(&crypto_alg_sem); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 239 | ret = crypto_remove_alg(alg, &list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 240 | up_write(&crypto_alg_sem); |
| 241 | |
| 242 | if (ret) |
| 243 | return ret; |
| 244 | |
| 245 | BUG_ON(atomic_read(&alg->cra_refcnt) != 1); |
| 246 | if (alg->cra_destroy) |
| 247 | alg->cra_destroy(alg); |
| 248 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 249 | crypto_remove_final(&list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 250 | return 0; |
| 251 | } |
| 252 | EXPORT_SYMBOL_GPL(crypto_unregister_alg); |
| 253 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 254 | int crypto_register_template(struct crypto_template *tmpl) |
| 255 | { |
| 256 | struct crypto_template *q; |
| 257 | int err = -EEXIST; |
| 258 | |
| 259 | down_write(&crypto_alg_sem); |
| 260 | |
| 261 | list_for_each_entry(q, &crypto_template_list, list) { |
| 262 | if (q == tmpl) |
| 263 | goto out; |
| 264 | } |
| 265 | |
| 266 | list_add(&tmpl->list, &crypto_template_list); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 267 | crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 268 | err = 0; |
| 269 | out: |
| 270 | up_write(&crypto_alg_sem); |
| 271 | return err; |
| 272 | } |
| 273 | EXPORT_SYMBOL_GPL(crypto_register_template); |
| 274 | |
| 275 | void crypto_unregister_template(struct crypto_template *tmpl) |
| 276 | { |
| 277 | struct crypto_instance *inst; |
| 278 | struct hlist_node *p, *n; |
| 279 | struct hlist_head *list; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 280 | LIST_HEAD(users); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 281 | |
| 282 | down_write(&crypto_alg_sem); |
| 283 | |
| 284 | BUG_ON(list_empty(&tmpl->list)); |
| 285 | list_del_init(&tmpl->list); |
| 286 | |
| 287 | list = &tmpl->instances; |
| 288 | hlist_for_each_entry(inst, p, list, list) { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 289 | int err = crypto_remove_alg(&inst->alg, &users); |
| 290 | BUG_ON(err); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 291 | } |
| 292 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 293 | crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl); |
| 294 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 295 | up_write(&crypto_alg_sem); |
| 296 | |
| 297 | hlist_for_each_entry_safe(inst, p, n, list, list) { |
| 298 | BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1); |
| 299 | tmpl->free(inst); |
| 300 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 301 | crypto_remove_final(&users); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 302 | } |
| 303 | EXPORT_SYMBOL_GPL(crypto_unregister_template); |
| 304 | |
| 305 | static struct crypto_template *__crypto_lookup_template(const char *name) |
| 306 | { |
| 307 | struct crypto_template *q, *tmpl = NULL; |
| 308 | |
| 309 | down_read(&crypto_alg_sem); |
| 310 | list_for_each_entry(q, &crypto_template_list, list) { |
| 311 | if (strcmp(q->name, name)) |
| 312 | continue; |
| 313 | if (unlikely(!crypto_tmpl_get(q))) |
| 314 | continue; |
| 315 | |
| 316 | tmpl = q; |
| 317 | break; |
| 318 | } |
| 319 | up_read(&crypto_alg_sem); |
| 320 | |
| 321 | return tmpl; |
| 322 | } |
| 323 | |
| 324 | struct crypto_template *crypto_lookup_template(const char *name) |
| 325 | { |
| 326 | return try_then_request_module(__crypto_lookup_template(name), name); |
| 327 | } |
| 328 | EXPORT_SYMBOL_GPL(crypto_lookup_template); |
| 329 | |
| 330 | int crypto_register_instance(struct crypto_template *tmpl, |
| 331 | struct crypto_instance *inst) |
| 332 | { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 333 | LIST_HEAD(list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 334 | int err = -EINVAL; |
| 335 | |
| 336 | if (inst->alg.cra_destroy) |
| 337 | goto err; |
| 338 | |
| 339 | err = crypto_check_alg(&inst->alg); |
| 340 | if (err) |
| 341 | goto err; |
| 342 | |
| 343 | inst->alg.cra_module = tmpl->module; |
| 344 | |
| 345 | down_write(&crypto_alg_sem); |
| 346 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 347 | err = __crypto_register_alg(&inst->alg, &list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 348 | if (err) |
| 349 | goto unlock; |
| 350 | |
| 351 | hlist_add_head(&inst->list, &tmpl->instances); |
| 352 | inst->tmpl = tmpl; |
| 353 | |
| 354 | unlock: |
| 355 | up_write(&crypto_alg_sem); |
| 356 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 357 | crypto_remove_final(&list); |
| 358 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 359 | err: |
| 360 | return err; |
| 361 | } |
| 362 | EXPORT_SYMBOL_GPL(crypto_register_instance); |
| 363 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 364 | int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg, |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 365 | struct crypto_instance *inst, u32 mask) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 366 | { |
| 367 | int err = -EAGAIN; |
| 368 | |
| 369 | spawn->inst = inst; |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 370 | spawn->mask = mask; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 371 | |
| 372 | down_write(&crypto_alg_sem); |
| 373 | if (!crypto_is_moribund(alg)) { |
| 374 | list_add(&spawn->list, &alg->cra_users); |
| 375 | spawn->alg = alg; |
| 376 | err = 0; |
| 377 | } |
| 378 | up_write(&crypto_alg_sem); |
| 379 | |
| 380 | return err; |
| 381 | } |
| 382 | EXPORT_SYMBOL_GPL(crypto_init_spawn); |
| 383 | |
| 384 | void crypto_drop_spawn(struct crypto_spawn *spawn) |
| 385 | { |
| 386 | down_write(&crypto_alg_sem); |
| 387 | list_del(&spawn->list); |
| 388 | up_write(&crypto_alg_sem); |
| 389 | } |
| 390 | EXPORT_SYMBOL_GPL(crypto_drop_spawn); |
| 391 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 392 | struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type, |
| 393 | u32 mask) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 394 | { |
| 395 | struct crypto_alg *alg; |
| 396 | struct crypto_alg *alg2; |
| 397 | struct crypto_tfm *tfm; |
| 398 | |
| 399 | down_read(&crypto_alg_sem); |
| 400 | alg = spawn->alg; |
| 401 | alg2 = alg; |
| 402 | if (alg2) |
| 403 | alg2 = crypto_mod_get(alg2); |
| 404 | up_read(&crypto_alg_sem); |
| 405 | |
| 406 | if (!alg2) { |
| 407 | if (alg) |
| 408 | crypto_shoot_alg(alg); |
| 409 | return ERR_PTR(-EAGAIN); |
| 410 | } |
| 411 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 412 | tfm = ERR_PTR(-EINVAL); |
| 413 | if (unlikely((alg->cra_flags ^ type) & mask)) |
| 414 | goto out_put_alg; |
| 415 | |
Herbert Xu | 27d2a33 | 2007-01-24 20:50:26 +1100 | [diff] [blame] | 416 | tfm = __crypto_alloc_tfm(alg, type, mask); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 417 | if (IS_ERR(tfm)) |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 418 | goto out_put_alg; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 419 | |
| 420 | return tfm; |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 421 | |
| 422 | out_put_alg: |
| 423 | crypto_mod_put(alg); |
| 424 | return tfm; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 425 | } |
| 426 | EXPORT_SYMBOL_GPL(crypto_spawn_tfm); |
| 427 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 428 | int crypto_register_notifier(struct notifier_block *nb) |
| 429 | { |
| 430 | return blocking_notifier_chain_register(&crypto_chain, nb); |
| 431 | } |
| 432 | EXPORT_SYMBOL_GPL(crypto_register_notifier); |
| 433 | |
| 434 | int crypto_unregister_notifier(struct notifier_block *nb) |
| 435 | { |
| 436 | return blocking_notifier_chain_unregister(&crypto_chain, nb); |
| 437 | } |
| 438 | EXPORT_SYMBOL_GPL(crypto_unregister_notifier); |
| 439 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 440 | struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 441 | { |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 442 | struct rtattr *rta = tb[CRYPTOA_TYPE - 1]; |
| 443 | struct crypto_attr_type *algt; |
| 444 | |
| 445 | if (!rta) |
| 446 | return ERR_PTR(-ENOENT); |
| 447 | if (RTA_PAYLOAD(rta) < sizeof(*algt)) |
| 448 | return ERR_PTR(-EINVAL); |
| 449 | |
| 450 | algt = RTA_DATA(rta); |
| 451 | |
| 452 | return algt; |
| 453 | } |
| 454 | EXPORT_SYMBOL_GPL(crypto_get_attr_type); |
| 455 | |
| 456 | int crypto_check_attr_type(struct rtattr **tb, u32 type) |
| 457 | { |
| 458 | struct crypto_attr_type *algt; |
| 459 | |
| 460 | algt = crypto_get_attr_type(tb); |
| 461 | if (IS_ERR(algt)) |
| 462 | return PTR_ERR(algt); |
| 463 | |
| 464 | if ((algt->type ^ type) & algt->mask) |
| 465 | return -EINVAL; |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | EXPORT_SYMBOL_GPL(crypto_check_attr_type); |
| 470 | |
| 471 | struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb, u32 type, u32 mask) |
| 472 | { |
| 473 | struct rtattr *rta = tb[CRYPTOA_ALG - 1]; |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 474 | struct crypto_attr_alg *alga; |
| 475 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 476 | if (!rta) |
| 477 | return ERR_PTR(-ENOENT); |
| 478 | if (RTA_PAYLOAD(rta) < sizeof(*alga)) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 479 | return ERR_PTR(-EINVAL); |
| 480 | |
| 481 | alga = RTA_DATA(rta); |
| 482 | alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0; |
| 483 | |
| 484 | return crypto_alg_mod_lookup(alga->name, type, mask); |
| 485 | } |
| 486 | EXPORT_SYMBOL_GPL(crypto_get_attr_alg); |
| 487 | |
| 488 | struct crypto_instance *crypto_alloc_instance(const char *name, |
| 489 | struct crypto_alg *alg) |
| 490 | { |
| 491 | struct crypto_instance *inst; |
| 492 | struct crypto_spawn *spawn; |
| 493 | int err; |
| 494 | |
| 495 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 496 | if (!inst) |
| 497 | return ERR_PTR(-ENOMEM); |
| 498 | |
| 499 | err = -ENAMETOOLONG; |
| 500 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name, |
| 501 | alg->cra_name) >= CRYPTO_MAX_ALG_NAME) |
| 502 | goto err_free_inst; |
| 503 | |
| 504 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", |
| 505 | name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
| 506 | goto err_free_inst; |
| 507 | |
| 508 | spawn = crypto_instance_ctx(inst); |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 509 | err = crypto_init_spawn(spawn, alg, inst, |
| 510 | CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 511 | |
| 512 | if (err) |
| 513 | goto err_free_inst; |
| 514 | |
| 515 | return inst; |
| 516 | |
| 517 | err_free_inst: |
| 518 | kfree(inst); |
| 519 | return ERR_PTR(err); |
| 520 | } |
| 521 | EXPORT_SYMBOL_GPL(crypto_alloc_instance); |
| 522 | |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 523 | void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen) |
| 524 | { |
| 525 | INIT_LIST_HEAD(&queue->list); |
| 526 | queue->backlog = &queue->list; |
| 527 | queue->qlen = 0; |
| 528 | queue->max_qlen = max_qlen; |
| 529 | } |
| 530 | EXPORT_SYMBOL_GPL(crypto_init_queue); |
| 531 | |
| 532 | int crypto_enqueue_request(struct crypto_queue *queue, |
| 533 | struct crypto_async_request *request) |
| 534 | { |
| 535 | int err = -EINPROGRESS; |
| 536 | |
| 537 | if (unlikely(queue->qlen >= queue->max_qlen)) { |
| 538 | err = -EBUSY; |
| 539 | if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) |
| 540 | goto out; |
| 541 | if (queue->backlog == &queue->list) |
| 542 | queue->backlog = &request->list; |
| 543 | } |
| 544 | |
| 545 | queue->qlen++; |
| 546 | list_add_tail(&request->list, &queue->list); |
| 547 | |
| 548 | out: |
| 549 | return err; |
| 550 | } |
| 551 | EXPORT_SYMBOL_GPL(crypto_enqueue_request); |
| 552 | |
| 553 | struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue) |
| 554 | { |
| 555 | struct list_head *request; |
| 556 | |
| 557 | if (unlikely(!queue->qlen)) |
| 558 | return NULL; |
| 559 | |
| 560 | queue->qlen--; |
| 561 | |
| 562 | if (queue->backlog != &queue->list) |
| 563 | queue->backlog = queue->backlog->next; |
| 564 | |
| 565 | request = queue->list.next; |
| 566 | list_del(request); |
| 567 | |
| 568 | return list_entry(request, struct crypto_async_request, list); |
| 569 | } |
| 570 | EXPORT_SYMBOL_GPL(crypto_dequeue_request); |
| 571 | |
| 572 | int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm) |
| 573 | { |
| 574 | struct crypto_async_request *req; |
| 575 | |
| 576 | list_for_each_entry(req, &queue->list, list) { |
| 577 | if (req->tfm == tfm) |
| 578 | return 1; |
| 579 | } |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | EXPORT_SYMBOL_GPL(crypto_tfm_in_queue); |
| 584 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 585 | static int __init crypto_algapi_init(void) |
| 586 | { |
| 587 | crypto_init_proc(); |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | static void __exit crypto_algapi_exit(void) |
| 592 | { |
| 593 | crypto_exit_proc(); |
| 594 | } |
| 595 | |
| 596 | module_init(crypto_algapi_init); |
| 597 | module_exit(crypto_algapi_exit); |
| 598 | |
| 599 | MODULE_LICENSE("GPL"); |
| 600 | MODULE_DESCRIPTION("Cryptographic algorithms API"); |