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 | |
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 | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 84 | static void crypto_remove_spawn(struct crypto_spawn *spawn, |
| 85 | struct list_head *list, |
| 86 | struct list_head *secondary_spawns) |
| 87 | { |
| 88 | struct crypto_instance *inst = spawn->inst; |
| 89 | struct crypto_template *tmpl = inst->tmpl; |
| 90 | |
| 91 | list_del_init(&spawn->list); |
| 92 | spawn->alg = NULL; |
| 93 | |
| 94 | if (crypto_is_dead(&inst->alg)) |
| 95 | return; |
| 96 | |
| 97 | inst->alg.cra_flags |= CRYPTO_ALG_DEAD; |
Herbert Xu | 38cb241 | 2007-11-17 22:09:38 +0800 | [diff] [blame] | 98 | if (hlist_unhashed(&inst->list)) |
| 99 | return; |
| 100 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 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 | d805848 | 2007-10-02 22:27:29 +0800 | [diff] [blame] | 155 | /* |
| 156 | * Check to see if either our generic name or |
| 157 | * specific name can satisfy the name requested |
| 158 | * by the larval entry q. |
| 159 | */ |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 160 | if (strcmp(alg->cra_name, q->cra_name) && |
| 161 | strcmp(alg->cra_driver_name, q->cra_name)) |
| 162 | continue; |
| 163 | |
| 164 | if (larval->adult) |
| 165 | continue; |
Herbert Xu | 492e2b6 | 2006-09-21 11:35:17 +1000 | [diff] [blame] | 166 | if ((q->cra_flags ^ alg->cra_flags) & larval->mask) |
| 167 | continue; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 168 | if (!crypto_mod_get(alg)) |
| 169 | continue; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 170 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 171 | larval->adult = alg; |
Herbert Xu | fe3c520 | 2007-05-19 17:51:40 +1000 | [diff] [blame] | 172 | complete_all(&larval->completion); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 173 | continue; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 174 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 175 | |
| 176 | if (strcmp(alg->cra_name, q->cra_name)) |
| 177 | continue; |
| 178 | |
| 179 | if (strcmp(alg->cra_driver_name, q->cra_driver_name) && |
| 180 | q->cra_priority > alg->cra_priority) |
| 181 | continue; |
| 182 | |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 183 | crypto_remove_spawns(&q->cra_users, list, alg->cra_flags); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | list_add(&alg->cra_list, &crypto_alg_list); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 187 | |
| 188 | crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 189 | ret = 0; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 190 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 191 | out: |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 192 | return ret; |
| 193 | } |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 194 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 195 | static void crypto_remove_final(struct list_head *list) |
| 196 | { |
| 197 | struct crypto_alg *alg; |
| 198 | struct crypto_alg *n; |
| 199 | |
| 200 | list_for_each_entry_safe(alg, n, list, cra_list) { |
| 201 | list_del_init(&alg->cra_list); |
| 202 | crypto_alg_put(alg); |
| 203 | } |
| 204 | } |
| 205 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 206 | int crypto_register_alg(struct crypto_alg *alg) |
| 207 | { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 208 | LIST_HEAD(list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 209 | int err; |
| 210 | |
| 211 | err = crypto_check_alg(alg); |
| 212 | if (err) |
| 213 | return err; |
| 214 | |
| 215 | down_write(&crypto_alg_sem); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 216 | err = __crypto_register_alg(alg, &list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 217 | up_write(&crypto_alg_sem); |
| 218 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 219 | crypto_remove_final(&list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 220 | return err; |
| 221 | } |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 222 | EXPORT_SYMBOL_GPL(crypto_register_alg); |
| 223 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 224 | static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list) |
| 225 | { |
| 226 | if (unlikely(list_empty(&alg->cra_list))) |
| 227 | return -ENOENT; |
| 228 | |
| 229 | alg->cra_flags |= CRYPTO_ALG_DEAD; |
| 230 | |
| 231 | crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg); |
| 232 | list_del_init(&alg->cra_list); |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 233 | crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 238 | int crypto_unregister_alg(struct crypto_alg *alg) |
| 239 | { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 240 | int ret; |
| 241 | LIST_HEAD(list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 242 | |
| 243 | down_write(&crypto_alg_sem); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 244 | ret = crypto_remove_alg(alg, &list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 245 | up_write(&crypto_alg_sem); |
| 246 | |
| 247 | if (ret) |
| 248 | return ret; |
| 249 | |
| 250 | BUG_ON(atomic_read(&alg->cra_refcnt) != 1); |
| 251 | if (alg->cra_destroy) |
| 252 | alg->cra_destroy(alg); |
| 253 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 254 | crypto_remove_final(&list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | EXPORT_SYMBOL_GPL(crypto_unregister_alg); |
| 258 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 259 | int crypto_register_template(struct crypto_template *tmpl) |
| 260 | { |
| 261 | struct crypto_template *q; |
| 262 | int err = -EEXIST; |
| 263 | |
| 264 | down_write(&crypto_alg_sem); |
| 265 | |
| 266 | list_for_each_entry(q, &crypto_template_list, list) { |
| 267 | if (q == tmpl) |
| 268 | goto out; |
| 269 | } |
| 270 | |
| 271 | list_add(&tmpl->list, &crypto_template_list); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 272 | crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 273 | err = 0; |
| 274 | out: |
| 275 | up_write(&crypto_alg_sem); |
| 276 | return err; |
| 277 | } |
| 278 | EXPORT_SYMBOL_GPL(crypto_register_template); |
| 279 | |
| 280 | void crypto_unregister_template(struct crypto_template *tmpl) |
| 281 | { |
| 282 | struct crypto_instance *inst; |
| 283 | struct hlist_node *p, *n; |
| 284 | struct hlist_head *list; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 285 | LIST_HEAD(users); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 286 | |
| 287 | down_write(&crypto_alg_sem); |
| 288 | |
| 289 | BUG_ON(list_empty(&tmpl->list)); |
| 290 | list_del_init(&tmpl->list); |
| 291 | |
| 292 | list = &tmpl->instances; |
| 293 | hlist_for_each_entry(inst, p, list, list) { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 294 | int err = crypto_remove_alg(&inst->alg, &users); |
| 295 | BUG_ON(err); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 296 | } |
| 297 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 298 | crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl); |
| 299 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 300 | up_write(&crypto_alg_sem); |
| 301 | |
| 302 | hlist_for_each_entry_safe(inst, p, n, list, list) { |
| 303 | BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1); |
| 304 | tmpl->free(inst); |
| 305 | } |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 306 | crypto_remove_final(&users); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 307 | } |
| 308 | EXPORT_SYMBOL_GPL(crypto_unregister_template); |
| 309 | |
| 310 | static struct crypto_template *__crypto_lookup_template(const char *name) |
| 311 | { |
| 312 | struct crypto_template *q, *tmpl = NULL; |
| 313 | |
| 314 | down_read(&crypto_alg_sem); |
| 315 | list_for_each_entry(q, &crypto_template_list, list) { |
| 316 | if (strcmp(q->name, name)) |
| 317 | continue; |
| 318 | if (unlikely(!crypto_tmpl_get(q))) |
| 319 | continue; |
| 320 | |
| 321 | tmpl = q; |
| 322 | break; |
| 323 | } |
| 324 | up_read(&crypto_alg_sem); |
| 325 | |
| 326 | return tmpl; |
| 327 | } |
| 328 | |
| 329 | struct crypto_template *crypto_lookup_template(const char *name) |
| 330 | { |
| 331 | return try_then_request_module(__crypto_lookup_template(name), name); |
| 332 | } |
| 333 | EXPORT_SYMBOL_GPL(crypto_lookup_template); |
| 334 | |
| 335 | int crypto_register_instance(struct crypto_template *tmpl, |
| 336 | struct crypto_instance *inst) |
| 337 | { |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 338 | LIST_HEAD(list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 339 | int err = -EINVAL; |
| 340 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 341 | err = crypto_check_alg(&inst->alg); |
| 342 | if (err) |
| 343 | goto err; |
| 344 | |
| 345 | inst->alg.cra_module = tmpl->module; |
| 346 | |
| 347 | down_write(&crypto_alg_sem); |
| 348 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 349 | err = __crypto_register_alg(&inst->alg, &list); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 350 | if (err) |
| 351 | goto unlock; |
| 352 | |
| 353 | hlist_add_head(&inst->list, &tmpl->instances); |
| 354 | inst->tmpl = tmpl; |
| 355 | |
| 356 | unlock: |
| 357 | up_write(&crypto_alg_sem); |
| 358 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 359 | crypto_remove_final(&list); |
| 360 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 361 | err: |
| 362 | return err; |
| 363 | } |
| 364 | EXPORT_SYMBOL_GPL(crypto_register_instance); |
| 365 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 366 | int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg, |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 367 | struct crypto_instance *inst, u32 mask) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 368 | { |
| 369 | int err = -EAGAIN; |
| 370 | |
| 371 | spawn->inst = inst; |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 372 | spawn->mask = mask; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 373 | |
| 374 | down_write(&crypto_alg_sem); |
| 375 | if (!crypto_is_moribund(alg)) { |
| 376 | list_add(&spawn->list, &alg->cra_users); |
| 377 | spawn->alg = alg; |
| 378 | err = 0; |
| 379 | } |
| 380 | up_write(&crypto_alg_sem); |
| 381 | |
| 382 | return err; |
| 383 | } |
| 384 | EXPORT_SYMBOL_GPL(crypto_init_spawn); |
| 385 | |
| 386 | void crypto_drop_spawn(struct crypto_spawn *spawn) |
| 387 | { |
| 388 | down_write(&crypto_alg_sem); |
| 389 | list_del(&spawn->list); |
| 390 | up_write(&crypto_alg_sem); |
| 391 | } |
| 392 | EXPORT_SYMBOL_GPL(crypto_drop_spawn); |
| 393 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 394 | struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type, |
| 395 | u32 mask) |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 396 | { |
| 397 | struct crypto_alg *alg; |
| 398 | struct crypto_alg *alg2; |
| 399 | struct crypto_tfm *tfm; |
| 400 | |
| 401 | down_read(&crypto_alg_sem); |
| 402 | alg = spawn->alg; |
| 403 | alg2 = alg; |
| 404 | if (alg2) |
| 405 | alg2 = crypto_mod_get(alg2); |
| 406 | up_read(&crypto_alg_sem); |
| 407 | |
| 408 | if (!alg2) { |
| 409 | if (alg) |
| 410 | crypto_shoot_alg(alg); |
| 411 | return ERR_PTR(-EAGAIN); |
| 412 | } |
| 413 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 414 | tfm = ERR_PTR(-EINVAL); |
| 415 | if (unlikely((alg->cra_flags ^ type) & mask)) |
| 416 | goto out_put_alg; |
| 417 | |
Herbert Xu | 27d2a33 | 2007-01-24 20:50:26 +1100 | [diff] [blame] | 418 | tfm = __crypto_alloc_tfm(alg, type, mask); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 419 | if (IS_ERR(tfm)) |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 420 | goto out_put_alg; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 421 | |
| 422 | return tfm; |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 423 | |
| 424 | out_put_alg: |
| 425 | crypto_mod_put(alg); |
| 426 | return tfm; |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 427 | } |
| 428 | EXPORT_SYMBOL_GPL(crypto_spawn_tfm); |
| 429 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 430 | int crypto_register_notifier(struct notifier_block *nb) |
| 431 | { |
| 432 | return blocking_notifier_chain_register(&crypto_chain, nb); |
| 433 | } |
| 434 | EXPORT_SYMBOL_GPL(crypto_register_notifier); |
| 435 | |
| 436 | int crypto_unregister_notifier(struct notifier_block *nb) |
| 437 | { |
| 438 | return blocking_notifier_chain_unregister(&crypto_chain, nb); |
| 439 | } |
| 440 | EXPORT_SYMBOL_GPL(crypto_unregister_notifier); |
| 441 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 442 | struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 443 | { |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 444 | struct rtattr *rta = tb[0]; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 445 | struct crypto_attr_type *algt; |
| 446 | |
| 447 | if (!rta) |
| 448 | return ERR_PTR(-ENOENT); |
| 449 | if (RTA_PAYLOAD(rta) < sizeof(*algt)) |
| 450 | return ERR_PTR(-EINVAL); |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 451 | if (rta->rta_type != CRYPTOA_TYPE) |
| 452 | return ERR_PTR(-EINVAL); |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 453 | |
| 454 | algt = RTA_DATA(rta); |
| 455 | |
| 456 | return algt; |
| 457 | } |
| 458 | EXPORT_SYMBOL_GPL(crypto_get_attr_type); |
| 459 | |
| 460 | int crypto_check_attr_type(struct rtattr **tb, u32 type) |
| 461 | { |
| 462 | struct crypto_attr_type *algt; |
| 463 | |
| 464 | algt = crypto_get_attr_type(tb); |
| 465 | if (IS_ERR(algt)) |
| 466 | return PTR_ERR(algt); |
| 467 | |
| 468 | if ((algt->type ^ type) & algt->mask) |
| 469 | return -EINVAL; |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | EXPORT_SYMBOL_GPL(crypto_check_attr_type); |
| 474 | |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 475 | const char *crypto_attr_alg_name(struct rtattr *rta) |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 476 | { |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 477 | struct crypto_attr_alg *alga; |
| 478 | |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 479 | if (!rta) |
| 480 | return ERR_PTR(-ENOENT); |
| 481 | if (RTA_PAYLOAD(rta) < sizeof(*alga)) |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 482 | return ERR_PTR(-EINVAL); |
Herbert Xu | 39e1ee01 | 2007-08-29 19:27:26 +0800 | [diff] [blame] | 483 | if (rta->rta_type != CRYPTOA_ALG) |
| 484 | return ERR_PTR(-EINVAL); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 485 | |
| 486 | alga = RTA_DATA(rta); |
| 487 | alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0; |
| 488 | |
Herbert Xu | 68b6c7d | 2007-12-07 20:18:17 +0800 | [diff] [blame] | 489 | return alga->name; |
| 490 | } |
| 491 | EXPORT_SYMBOL_GPL(crypto_attr_alg_name); |
| 492 | |
| 493 | struct crypto_alg *crypto_attr_alg(struct rtattr *rta, u32 type, u32 mask) |
| 494 | { |
| 495 | const char *name; |
| 496 | int err; |
| 497 | |
| 498 | name = crypto_attr_alg_name(rta); |
| 499 | err = PTR_ERR(name); |
| 500 | if (IS_ERR(name)) |
| 501 | return ERR_PTR(err); |
| 502 | |
| 503 | return crypto_alg_mod_lookup(name, type, mask); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 504 | } |
Herbert Xu | 3c09f17 | 2007-08-30 16:24:15 +0800 | [diff] [blame] | 505 | EXPORT_SYMBOL_GPL(crypto_attr_alg); |
| 506 | |
| 507 | int crypto_attr_u32(struct rtattr *rta, u32 *num) |
| 508 | { |
| 509 | struct crypto_attr_u32 *nu32; |
| 510 | |
| 511 | if (!rta) |
| 512 | return -ENOENT; |
| 513 | if (RTA_PAYLOAD(rta) < sizeof(*nu32)) |
| 514 | return -EINVAL; |
| 515 | if (rta->rta_type != CRYPTOA_U32) |
| 516 | return -EINVAL; |
| 517 | |
| 518 | nu32 = RTA_DATA(rta); |
| 519 | *num = nu32->num; |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | EXPORT_SYMBOL_GPL(crypto_attr_u32); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 524 | |
| 525 | struct crypto_instance *crypto_alloc_instance(const char *name, |
| 526 | struct crypto_alg *alg) |
| 527 | { |
| 528 | struct crypto_instance *inst; |
| 529 | struct crypto_spawn *spawn; |
| 530 | int err; |
| 531 | |
| 532 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
| 533 | if (!inst) |
| 534 | return ERR_PTR(-ENOMEM); |
| 535 | |
| 536 | err = -ENAMETOOLONG; |
| 537 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name, |
| 538 | alg->cra_name) >= CRYPTO_MAX_ALG_NAME) |
| 539 | goto err_free_inst; |
| 540 | |
| 541 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", |
| 542 | name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
| 543 | goto err_free_inst; |
| 544 | |
| 545 | spawn = crypto_instance_ctx(inst); |
Herbert Xu | a73e699 | 2007-04-08 21:31:36 +1000 | [diff] [blame] | 546 | err = crypto_init_spawn(spawn, alg, inst, |
| 547 | CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 548 | |
| 549 | if (err) |
| 550 | goto err_free_inst; |
| 551 | |
| 552 | return inst; |
| 553 | |
| 554 | err_free_inst: |
| 555 | kfree(inst); |
| 556 | return ERR_PTR(err); |
| 557 | } |
| 558 | EXPORT_SYMBOL_GPL(crypto_alloc_instance); |
| 559 | |
Herbert Xu | b5b7f08 | 2007-04-16 20:48:54 +1000 | [diff] [blame] | 560 | void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen) |
| 561 | { |
| 562 | INIT_LIST_HEAD(&queue->list); |
| 563 | queue->backlog = &queue->list; |
| 564 | queue->qlen = 0; |
| 565 | queue->max_qlen = max_qlen; |
| 566 | } |
| 567 | EXPORT_SYMBOL_GPL(crypto_init_queue); |
| 568 | |
| 569 | int crypto_enqueue_request(struct crypto_queue *queue, |
| 570 | struct crypto_async_request *request) |
| 571 | { |
| 572 | int err = -EINPROGRESS; |
| 573 | |
| 574 | if (unlikely(queue->qlen >= queue->max_qlen)) { |
| 575 | err = -EBUSY; |
| 576 | if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) |
| 577 | goto out; |
| 578 | if (queue->backlog == &queue->list) |
| 579 | queue->backlog = &request->list; |
| 580 | } |
| 581 | |
| 582 | queue->qlen++; |
| 583 | list_add_tail(&request->list, &queue->list); |
| 584 | |
| 585 | out: |
| 586 | return err; |
| 587 | } |
| 588 | EXPORT_SYMBOL_GPL(crypto_enqueue_request); |
| 589 | |
| 590 | struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue) |
| 591 | { |
| 592 | struct list_head *request; |
| 593 | |
| 594 | if (unlikely(!queue->qlen)) |
| 595 | return NULL; |
| 596 | |
| 597 | queue->qlen--; |
| 598 | |
| 599 | if (queue->backlog != &queue->list) |
| 600 | queue->backlog = queue->backlog->next; |
| 601 | |
| 602 | request = queue->list.next; |
| 603 | list_del(request); |
| 604 | |
| 605 | return list_entry(request, struct crypto_async_request, list); |
| 606 | } |
| 607 | EXPORT_SYMBOL_GPL(crypto_dequeue_request); |
| 608 | |
| 609 | int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm) |
| 610 | { |
| 611 | struct crypto_async_request *req; |
| 612 | |
| 613 | list_for_each_entry(req, &queue->list, list) { |
| 614 | if (req->tfm == tfm) |
| 615 | return 1; |
| 616 | } |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | EXPORT_SYMBOL_GPL(crypto_tfm_in_queue); |
| 621 | |
Herbert Xu | 7613636 | 2007-11-20 17:26:06 +0800 | [diff] [blame] | 622 | static inline void crypto_inc_byte(u8 *a, unsigned int size) |
| 623 | { |
| 624 | u8 *b = (a + size); |
| 625 | u8 c; |
| 626 | |
| 627 | for (; size; size--) { |
| 628 | c = *--b + 1; |
| 629 | *b = c; |
| 630 | if (c) |
| 631 | break; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | void crypto_inc(u8 *a, unsigned int size) |
| 636 | { |
| 637 | __be32 *b = (__be32 *)(a + size); |
| 638 | u32 c; |
| 639 | |
| 640 | for (; size >= 4; size -= 4) { |
| 641 | c = be32_to_cpu(*--b) + 1; |
| 642 | *b = cpu_to_be32(c); |
| 643 | if (c) |
| 644 | return; |
| 645 | } |
| 646 | |
| 647 | crypto_inc_byte(a, size); |
| 648 | } |
| 649 | EXPORT_SYMBOL_GPL(crypto_inc); |
| 650 | |
| 651 | static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size) |
| 652 | { |
| 653 | for (; size; size--) |
| 654 | *a++ ^= *b++; |
| 655 | } |
| 656 | |
| 657 | void crypto_xor(u8 *dst, const u8 *src, unsigned int size) |
| 658 | { |
| 659 | u32 *a = (u32 *)dst; |
| 660 | u32 *b = (u32 *)src; |
| 661 | |
| 662 | for (; size >= 4; size -= 4) |
| 663 | *a++ ^= *b++; |
| 664 | |
| 665 | crypto_xor_byte((u8 *)a, (u8 *)b, size); |
| 666 | } |
| 667 | EXPORT_SYMBOL_GPL(crypto_xor); |
| 668 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 669 | static int __init crypto_algapi_init(void) |
| 670 | { |
| 671 | crypto_init_proc(); |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | static void __exit crypto_algapi_exit(void) |
| 676 | { |
| 677 | crypto_exit_proc(); |
| 678 | } |
| 679 | |
| 680 | module_init(crypto_algapi_init); |
| 681 | module_exit(crypto_algapi_exit); |
| 682 | |
| 683 | MODULE_LICENSE("GPL"); |
| 684 | MODULE_DESCRIPTION("Cryptographic algorithms API"); |