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