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 | |
Herbert Xu | c51b6c8 | 2008-08-04 11:44:59 +0800 | [diff] [blame] | 30 | alg = crypto_alg_lookup(name, type, mask); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 31 | |
| 32 | if (alg) { |
| 33 | if (crypto_is_larval(alg)) { |
| 34 | struct crypto_larval *larval = (void *)alg; |
Herbert Xu | fe3c520 | 2007-05-19 17:51:40 +1000 | [diff] [blame] | 35 | complete_all(&larval->completion); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 36 | } |
| 37 | crypto_mod_put(alg); |
| 38 | } |
| 39 | } |
| 40 | EXPORT_SYMBOL_GPL(crypto_larval_error); |
| 41 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 42 | static inline int crypto_set_driver_name(struct crypto_alg *alg) |
| 43 | { |
| 44 | static const char suffix[] = "-generic"; |
| 45 | char *driver_name = alg->cra_driver_name; |
| 46 | int len; |
| 47 | |
| 48 | if (*driver_name) |
| 49 | return 0; |
| 50 | |
| 51 | len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); |
| 52 | if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME) |
| 53 | return -ENAMETOOLONG; |
| 54 | |
| 55 | memcpy(driver_name + len, suffix, sizeof(suffix)); |
| 56 | return 0; |
| 57 | } |
| 58 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 59 | static int crypto_check_alg(struct crypto_alg *alg) |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 60 | { |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 61 | if (alg->cra_alignmask & (alg->cra_alignmask + 1)) |
| 62 | return -EINVAL; |
| 63 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 64 | if (alg->cra_blocksize > PAGE_SIZE / 8) |
| 65 | return -EINVAL; |
| 66 | |
| 67 | if (alg->cra_priority < 0) |
| 68 | return -EINVAL; |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 69 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 70 | return crypto_set_driver_name(alg); |
| 71 | } |
| 72 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 73 | static void crypto_destroy_instance(struct crypto_alg *alg) |
| 74 | { |
| 75 | struct crypto_instance *inst = (void *)alg; |
| 76 | struct crypto_template *tmpl = inst->tmpl; |
| 77 | |
| 78 | tmpl->free(inst); |
| 79 | crypto_tmpl_put(tmpl); |
| 80 | } |
| 81 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 82 | static void crypto_remove_spawn(struct crypto_spawn *spawn, |
| 83 | struct list_head *list, |
| 84 | struct list_head *secondary_spawns) |
| 85 | { |
| 86 | struct crypto_instance *inst = spawn->inst; |
| 87 | struct crypto_template *tmpl = inst->tmpl; |
| 88 | |
| 89 | list_del_init(&spawn->list); |
| 90 | spawn->alg = NULL; |
| 91 | |
| 92 | if (crypto_is_dead(&inst->alg)) |
| 93 | return; |
| 94 | |
| 95 | inst->alg.cra_flags |= CRYPTO_ALG_DEAD; |
Herbert Xu | 38cb241 | 2007-11-17 22:09:38 +0800 | [diff] [blame] | 96 | if (hlist_unhashed(&inst->list)) |
| 97 | return; |
| 98 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 99 | if (!tmpl || !crypto_tmpl_get(tmpl)) |
| 100 | return; |
| 101 | |
| 102 | crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg); |
| 103 | list_move(&inst->alg.cra_list, list); |
| 104 | hlist_del(&inst->list); |
| 105 | inst->alg.cra_destroy = crypto_destroy_instance; |
| 106 | |
| 107 | list_splice(&inst->alg.cra_users, secondary_spawns); |
| 108 | } |
| 109 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 110 | static void crypto_remove_spawns(struct list_head *spawns, |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 111 | struct list_head *list, u32 new_type) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 112 | { |
| 113 | struct crypto_spawn *spawn, *n; |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 114 | LIST_HEAD(secondary_spawns); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 115 | |
| 116 | list_for_each_entry_safe(spawn, n, spawns, list) { |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 117 | if ((spawn->alg->cra_flags ^ new_type) & spawn->mask) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 118 | continue; |
| 119 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 120 | crypto_remove_spawn(spawn, list, &secondary_spawns); |
| 121 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 122 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 123 | while (!list_empty(&secondary_spawns)) { |
| 124 | list_for_each_entry_safe(spawn, n, &secondary_spawns, list) |
| 125 | crypto_remove_spawn(spawn, list, &secondary_spawns); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | static int __crypto_register_alg(struct crypto_alg *alg, |
| 130 | struct list_head *list) |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 131 | { |
| 132 | struct crypto_alg *q; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 133 | int ret = -EAGAIN; |
| 134 | |
| 135 | if (crypto_is_dead(alg)) |
| 136 | goto out; |
| 137 | |
| 138 | INIT_LIST_HEAD(&alg->cra_users); |
| 139 | |
| 140 | ret = -EEXIST; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 141 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 142 | atomic_set(&alg->cra_refcnt, 1); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 143 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 144 | if (q == alg) |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 145 | goto out; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 146 | |
| 147 | if (crypto_is_moribund(q)) |
| 148 | continue; |
| 149 | |
| 150 | if (crypto_is_larval(q)) { |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 151 | struct crypto_larval *larval = (void *)q; |
| 152 | |
Herbert Xu | d805848 | 2007-10-02 22:27:29 +0800 | [diff] [blame] | 153 | /* |
| 154 | * Check to see if either our generic name or |
| 155 | * specific name can satisfy the name requested |
| 156 | * by the larval entry q. |
| 157 | */ |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 158 | if (strcmp(alg->cra_name, q->cra_name) && |
| 159 | strcmp(alg->cra_driver_name, q->cra_name)) |
| 160 | continue; |
| 161 | |
| 162 | if (larval->adult) |
| 163 | continue; |
Herbert Xu | 492e2b6 | 2006-09-21 11:35:17 +1000 | [diff] [blame] | 164 | if ((q->cra_flags ^ alg->cra_flags) & larval->mask) |
| 165 | continue; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 166 | if (!crypto_mod_get(alg)) |
| 167 | continue; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 168 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 169 | larval->adult = alg; |
Herbert Xu | fe3c520 | 2007-05-19 17:51:40 +1000 | [diff] [blame] | 170 | complete_all(&larval->completion); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 171 | continue; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 172 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 173 | |
| 174 | if (strcmp(alg->cra_name, q->cra_name)) |
| 175 | continue; |
| 176 | |
| 177 | if (strcmp(alg->cra_driver_name, q->cra_driver_name) && |
| 178 | q->cra_priority > alg->cra_priority) |
| 179 | continue; |
| 180 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 181 | crypto_remove_spawns(&q->cra_users, list, alg->cra_flags); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | list_add(&alg->cra_list, &crypto_alg_list); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 185 | |
| 186 | crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 187 | ret = 0; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 188 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 189 | out: |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 190 | return ret; |
| 191 | } |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 192 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 193 | static void crypto_remove_final(struct list_head *list) |
| 194 | { |
| 195 | struct crypto_alg *alg; |
| 196 | struct crypto_alg *n; |
| 197 | |
| 198 | list_for_each_entry_safe(alg, n, list, cra_list) { |
| 199 | list_del_init(&alg->cra_list); |
| 200 | crypto_alg_put(alg); |
| 201 | } |
| 202 | } |
| 203 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 204 | int crypto_register_alg(struct crypto_alg *alg) |
| 205 | { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 206 | LIST_HEAD(list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 207 | int err; |
| 208 | |
| 209 | err = crypto_check_alg(alg); |
| 210 | if (err) |
| 211 | return err; |
| 212 | |
| 213 | down_write(&crypto_alg_sem); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 214 | err = __crypto_register_alg(alg, &list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 215 | up_write(&crypto_alg_sem); |
| 216 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 217 | crypto_remove_final(&list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 218 | return err; |
| 219 | } |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 220 | EXPORT_SYMBOL_GPL(crypto_register_alg); |
| 221 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 222 | static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list) |
| 223 | { |
| 224 | if (unlikely(list_empty(&alg->cra_list))) |
| 225 | return -ENOENT; |
| 226 | |
| 227 | alg->cra_flags |= CRYPTO_ALG_DEAD; |
| 228 | |
| 229 | crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg); |
| 230 | list_del_init(&alg->cra_list); |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 231 | crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 236 | int crypto_unregister_alg(struct crypto_alg *alg) |
| 237 | { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 238 | int ret; |
| 239 | LIST_HEAD(list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 240 | |
| 241 | down_write(&crypto_alg_sem); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 242 | ret = crypto_remove_alg(alg, &list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 243 | up_write(&crypto_alg_sem); |
| 244 | |
| 245 | if (ret) |
| 246 | return ret; |
| 247 | |
| 248 | BUG_ON(atomic_read(&alg->cra_refcnt) != 1); |
| 249 | if (alg->cra_destroy) |
| 250 | alg->cra_destroy(alg); |
| 251 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 252 | crypto_remove_final(&list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | EXPORT_SYMBOL_GPL(crypto_unregister_alg); |
| 256 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 257 | int crypto_register_template(struct crypto_template *tmpl) |
| 258 | { |
| 259 | struct crypto_template *q; |
| 260 | int err = -EEXIST; |
| 261 | |
| 262 | down_write(&crypto_alg_sem); |
| 263 | |
| 264 | list_for_each_entry(q, &crypto_template_list, list) { |
| 265 | if (q == tmpl) |
| 266 | goto out; |
| 267 | } |
| 268 | |
| 269 | list_add(&tmpl->list, &crypto_template_list); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 270 | crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 271 | err = 0; |
| 272 | out: |
| 273 | up_write(&crypto_alg_sem); |
| 274 | return err; |
| 275 | } |
| 276 | EXPORT_SYMBOL_GPL(crypto_register_template); |
| 277 | |
| 278 | void crypto_unregister_template(struct crypto_template *tmpl) |
| 279 | { |
| 280 | struct crypto_instance *inst; |
| 281 | struct hlist_node *p, *n; |
| 282 | struct hlist_head *list; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 283 | LIST_HEAD(users); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 284 | |
| 285 | down_write(&crypto_alg_sem); |
| 286 | |
| 287 | BUG_ON(list_empty(&tmpl->list)); |
| 288 | list_del_init(&tmpl->list); |
| 289 | |
| 290 | list = &tmpl->instances; |
| 291 | hlist_for_each_entry(inst, p, list, list) { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 292 | int err = crypto_remove_alg(&inst->alg, &users); |
| 293 | BUG_ON(err); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 294 | } |
| 295 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 296 | crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl); |
| 297 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 298 | up_write(&crypto_alg_sem); |
| 299 | |
| 300 | hlist_for_each_entry_safe(inst, p, n, list, list) { |
| 301 | BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1); |
| 302 | tmpl->free(inst); |
| 303 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 304 | crypto_remove_final(&users); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 305 | } |
| 306 | EXPORT_SYMBOL_GPL(crypto_unregister_template); |
| 307 | |
| 308 | static struct crypto_template *__crypto_lookup_template(const char *name) |
| 309 | { |
| 310 | struct crypto_template *q, *tmpl = NULL; |
| 311 | |
| 312 | down_read(&crypto_alg_sem); |
| 313 | list_for_each_entry(q, &crypto_template_list, list) { |
| 314 | if (strcmp(q->name, name)) |
| 315 | continue; |
| 316 | if (unlikely(!crypto_tmpl_get(q))) |
| 317 | continue; |
| 318 | |
| 319 | tmpl = q; |
| 320 | break; |
| 321 | } |
| 322 | up_read(&crypto_alg_sem); |
| 323 | |
| 324 | return tmpl; |
| 325 | } |
| 326 | |
| 327 | struct crypto_template *crypto_lookup_template(const char *name) |
| 328 | { |
| 329 | return try_then_request_module(__crypto_lookup_template(name), name); |
| 330 | } |
| 331 | EXPORT_SYMBOL_GPL(crypto_lookup_template); |
| 332 | |
| 333 | int crypto_register_instance(struct crypto_template *tmpl, |
| 334 | struct crypto_instance *inst) |
| 335 | { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 336 | LIST_HEAD(list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 337 | int err = -EINVAL; |
| 338 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 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 | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 442 | struct rtattr *rta = tb[0]; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 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); |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 449 | if (rta->rta_type != CRYPTOA_TYPE) |
| 450 | return ERR_PTR(-EINVAL); |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 451 | |
| 452 | algt = RTA_DATA(rta); |
| 453 | |
| 454 | return algt; |
| 455 | } |
| 456 | EXPORT_SYMBOL_GPL(crypto_get_attr_type); |
| 457 | |
| 458 | int crypto_check_attr_type(struct rtattr **tb, u32 type) |
| 459 | { |
| 460 | struct crypto_attr_type *algt; |
| 461 | |
| 462 | algt = crypto_get_attr_type(tb); |
| 463 | if (IS_ERR(algt)) |
| 464 | return PTR_ERR(algt); |
| 465 | |
| 466 | if ((algt->type ^ type) & algt->mask) |
| 467 | return -EINVAL; |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | EXPORT_SYMBOL_GPL(crypto_check_attr_type); |
| 472 | |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 473 | const char *crypto_attr_alg_name(struct rtattr *rta) |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 474 | { |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 475 | struct crypto_attr_alg *alga; |
| 476 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 477 | if (!rta) |
| 478 | return ERR_PTR(-ENOENT); |
| 479 | if (RTA_PAYLOAD(rta) < sizeof(*alga)) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 480 | return ERR_PTR(-EINVAL); |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 481 | if (rta->rta_type != CRYPTOA_ALG) |
| 482 | return ERR_PTR(-EINVAL); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 483 | |
| 484 | alga = RTA_DATA(rta); |
| 485 | alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0; |
| 486 | |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 487 | return alga->name; |
| 488 | } |
| 489 | EXPORT_SYMBOL_GPL(crypto_attr_alg_name); |
| 490 | |
| 491 | struct crypto_alg *crypto_attr_alg(struct rtattr *rta, u32 type, u32 mask) |
| 492 | { |
| 493 | const char *name; |
| 494 | int err; |
| 495 | |
| 496 | name = crypto_attr_alg_name(rta); |
| 497 | err = PTR_ERR(name); |
| 498 | if (IS_ERR(name)) |
| 499 | return ERR_PTR(err); |
| 500 | |
| 501 | return crypto_alg_mod_lookup(name, type, mask); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 502 | } |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 503 | EXPORT_SYMBOL_GPL(crypto_attr_alg); |
| 504 | |
| 505 | int crypto_attr_u32(struct rtattr *rta, u32 *num) |
| 506 | { |
| 507 | struct crypto_attr_u32 *nu32; |
| 508 | |
| 509 | if (!rta) |
| 510 | return -ENOENT; |
| 511 | if (RTA_PAYLOAD(rta) < sizeof(*nu32)) |
| 512 | return -EINVAL; |
| 513 | if (rta->rta_type != CRYPTOA_U32) |
| 514 | return -EINVAL; |
| 515 | |
| 516 | nu32 = RTA_DATA(rta); |
| 517 | *num = nu32->num; |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | EXPORT_SYMBOL_GPL(crypto_attr_u32); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 522 | |
| 523 | struct crypto_instance *crypto_alloc_instance(const char *name, |
| 524 | struct crypto_alg *alg) |
| 525 | { |
| 526 | struct crypto_instance *inst; |
| 527 | struct crypto_spawn *spawn; |
| 528 | int err; |
| 529 | |
| 530 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 531 | if (!inst) |
| 532 | return ERR_PTR(-ENOMEM); |
| 533 | |
| 534 | err = -ENAMETOOLONG; |
| 535 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name, |
| 536 | alg->cra_name) >= CRYPTO_MAX_ALG_NAME) |
| 537 | goto err_free_inst; |
| 538 | |
| 539 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", |
| 540 | name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
| 541 | goto err_free_inst; |
| 542 | |
| 543 | spawn = crypto_instance_ctx(inst); |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 544 | err = crypto_init_spawn(spawn, alg, inst, |
| 545 | CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 546 | |
| 547 | if (err) |
| 548 | goto err_free_inst; |
| 549 | |
| 550 | return inst; |
| 551 | |
| 552 | err_free_inst: |
| 553 | kfree(inst); |
| 554 | return ERR_PTR(err); |
| 555 | } |
| 556 | EXPORT_SYMBOL_GPL(crypto_alloc_instance); |
| 557 | |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 558 | void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen) |
| 559 | { |
| 560 | INIT_LIST_HEAD(&queue->list); |
| 561 | queue->backlog = &queue->list; |
| 562 | queue->qlen = 0; |
| 563 | queue->max_qlen = max_qlen; |
| 564 | } |
| 565 | EXPORT_SYMBOL_GPL(crypto_init_queue); |
| 566 | |
| 567 | int crypto_enqueue_request(struct crypto_queue *queue, |
| 568 | struct crypto_async_request *request) |
| 569 | { |
| 570 | int err = -EINPROGRESS; |
| 571 | |
| 572 | if (unlikely(queue->qlen >= queue->max_qlen)) { |
| 573 | err = -EBUSY; |
| 574 | if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) |
| 575 | goto out; |
| 576 | if (queue->backlog == &queue->list) |
| 577 | queue->backlog = &request->list; |
| 578 | } |
| 579 | |
| 580 | queue->qlen++; |
| 581 | list_add_tail(&request->list, &queue->list); |
| 582 | |
| 583 | out: |
| 584 | return err; |
| 585 | } |
| 586 | EXPORT_SYMBOL_GPL(crypto_enqueue_request); |
| 587 | |
| 588 | struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue) |
| 589 | { |
| 590 | struct list_head *request; |
| 591 | |
| 592 | if (unlikely(!queue->qlen)) |
| 593 | return NULL; |
| 594 | |
| 595 | queue->qlen--; |
| 596 | |
| 597 | if (queue->backlog != &queue->list) |
| 598 | queue->backlog = queue->backlog->next; |
| 599 | |
| 600 | request = queue->list.next; |
| 601 | list_del(request); |
| 602 | |
| 603 | return list_entry(request, struct crypto_async_request, list); |
| 604 | } |
| 605 | EXPORT_SYMBOL_GPL(crypto_dequeue_request); |
| 606 | |
| 607 | int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm) |
| 608 | { |
| 609 | struct crypto_async_request *req; |
| 610 | |
| 611 | list_for_each_entry(req, &queue->list, list) { |
| 612 | if (req->tfm == tfm) |
| 613 | return 1; |
| 614 | } |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | EXPORT_SYMBOL_GPL(crypto_tfm_in_queue); |
| 619 | |
Herbert Xu | 7613636 | 2007-11-20 17:26:06 +0800 | [diff] [blame] | 620 | static inline void crypto_inc_byte(u8 *a, unsigned int size) |
| 621 | { |
| 622 | u8 *b = (a + size); |
| 623 | u8 c; |
| 624 | |
| 625 | for (; size; size--) { |
| 626 | c = *--b + 1; |
| 627 | *b = c; |
| 628 | if (c) |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | void crypto_inc(u8 *a, unsigned int size) |
| 634 | { |
| 635 | __be32 *b = (__be32 *)(a + size); |
| 636 | u32 c; |
| 637 | |
| 638 | for (; size >= 4; size -= 4) { |
| 639 | c = be32_to_cpu(*--b) + 1; |
| 640 | *b = cpu_to_be32(c); |
| 641 | if (c) |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | crypto_inc_byte(a, size); |
| 646 | } |
| 647 | EXPORT_SYMBOL_GPL(crypto_inc); |
| 648 | |
| 649 | static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size) |
| 650 | { |
| 651 | for (; size; size--) |
| 652 | *a++ ^= *b++; |
| 653 | } |
| 654 | |
| 655 | void crypto_xor(u8 *dst, const u8 *src, unsigned int size) |
| 656 | { |
| 657 | u32 *a = (u32 *)dst; |
| 658 | u32 *b = (u32 *)src; |
| 659 | |
| 660 | for (; size >= 4; size -= 4) |
| 661 | *a++ ^= *b++; |
| 662 | |
| 663 | crypto_xor_byte((u8 *)a, (u8 *)b, size); |
| 664 | } |
| 665 | EXPORT_SYMBOL_GPL(crypto_xor); |
| 666 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 667 | static int __init crypto_algapi_init(void) |
| 668 | { |
| 669 | crypto_init_proc(); |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static void __exit crypto_algapi_exit(void) |
| 674 | { |
| 675 | crypto_exit_proc(); |
| 676 | } |
| 677 | |
| 678 | module_init(crypto_algapi_init); |
| 679 | module_exit(crypto_algapi_exit); |
| 680 | |
| 681 | MODULE_LICENSE("GPL"); |
| 682 | MODULE_DESCRIPTION("Cryptographic algorithms API"); |