Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Scatterlist Cryptographic API. |
| 3 | * |
| 4 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
| 5 | * Copyright (c) 2002 David S. Miller (davem@redhat.com) |
Herbert Xu | 5cb1454 | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 6 | * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no> |
| 9 | * and Nettle, by Niels Möller. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by the Free |
| 13 | * Software Foundation; either version 2 of the License, or (at your option) |
| 14 | * any later version. |
| 15 | * |
| 16 | */ |
Jesper Juhl | a61cc44 | 2005-07-06 13:54:31 -0700 | [diff] [blame] | 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/errno.h> |
Herbert Xu | 5cb1454 | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Adrian Bunk | 176c365 | 2005-07-06 13:53:09 -0700 | [diff] [blame] | 20 | #include <linux/kmod.h> |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame^] | 21 | #include <linux/module.h> |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 22 | #include <linux/param.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/slab.h> |
Herbert Xu | 5cb1454 | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 24 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "internal.h" |
| 26 | |
| 27 | LIST_HEAD(crypto_alg_list); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 28 | EXPORT_SYMBOL_GPL(crypto_alg_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | DECLARE_RWSEM(crypto_alg_sem); |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 30 | EXPORT_SYMBOL_GPL(crypto_alg_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 32 | BLOCKING_NOTIFIER_HEAD(crypto_chain); |
| 33 | EXPORT_SYMBOL_GPL(crypto_chain); |
| 34 | |
Herbert Xu | 6521f30 | 2006-08-06 20:28:44 +1000 | [diff] [blame] | 35 | static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
Herbert Xu | 6521f30 | 2006-08-06 20:28:44 +1000 | [diff] [blame] | 37 | atomic_inc(&alg->cra_refcnt); |
| 38 | return alg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Herbert Xu | 6521f30 | 2006-08-06 20:28:44 +1000 | [diff] [blame] | 41 | static inline void crypto_alg_put(struct crypto_alg *alg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Herbert Xu | 6521f30 | 2006-08-06 20:28:44 +1000 | [diff] [blame] | 43 | if (atomic_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy) |
| 44 | alg->cra_destroy(alg); |
| 45 | } |
| 46 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 47 | struct crypto_alg *crypto_mod_get(struct crypto_alg *alg) |
Herbert Xu | 6521f30 | 2006-08-06 20:28:44 +1000 | [diff] [blame] | 48 | { |
| 49 | return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL; |
| 50 | } |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 51 | EXPORT_SYMBOL_GPL(crypto_mod_get); |
Herbert Xu | 6521f30 | 2006-08-06 20:28:44 +1000 | [diff] [blame] | 52 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 53 | void crypto_mod_put(struct crypto_alg *alg) |
Herbert Xu | 6521f30 | 2006-08-06 20:28:44 +1000 | [diff] [blame] | 54 | { |
| 55 | crypto_alg_put(alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | module_put(alg->cra_module); |
| 57 | } |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 58 | EXPORT_SYMBOL_GPL(crypto_mod_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 60 | struct crypto_alg *__crypto_alg_lookup(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
| 62 | struct crypto_alg *q, *alg = NULL; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 63 | int best = -2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
Herbert Xu | 5cb1454 | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 66 | int exact, fuzzy; |
| 67 | |
| 68 | exact = !strcmp(q->cra_driver_name, name); |
| 69 | fuzzy = !strcmp(q->cra_name, name); |
| 70 | if (!exact && !(fuzzy && q->cra_priority > best)) |
| 71 | continue; |
| 72 | |
Herbert Xu | 72fa491 | 2006-05-28 09:05:24 +1000 | [diff] [blame] | 73 | if (unlikely(!crypto_mod_get(q))) |
Herbert Xu | 5cb1454 | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 74 | continue; |
| 75 | |
| 76 | best = q->cra_priority; |
| 77 | if (alg) |
Herbert Xu | 72fa491 | 2006-05-28 09:05:24 +1000 | [diff] [blame] | 78 | crypto_mod_put(alg); |
Herbert Xu | 5cb1454 | 2005-11-05 16:58:14 +1100 | [diff] [blame] | 79 | alg = q; |
| 80 | |
| 81 | if (exact) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 84 | |
| 85 | return alg; |
| 86 | } |
| 87 | EXPORT_SYMBOL_GPL(__crypto_alg_lookup); |
| 88 | |
| 89 | static void crypto_larval_destroy(struct crypto_alg *alg) |
| 90 | { |
| 91 | struct crypto_larval *larval = (void *)alg; |
| 92 | |
| 93 | BUG_ON(!crypto_is_larval(alg)); |
| 94 | if (larval->adult) |
| 95 | crypto_mod_put(larval->adult); |
| 96 | kfree(larval); |
| 97 | } |
| 98 | |
| 99 | static struct crypto_alg *crypto_larval_alloc(const char *name) |
| 100 | { |
| 101 | struct crypto_alg *alg; |
| 102 | struct crypto_larval *larval; |
| 103 | |
| 104 | larval = kzalloc(sizeof(*larval), GFP_KERNEL); |
| 105 | if (!larval) |
| 106 | return NULL; |
| 107 | |
| 108 | larval->alg.cra_flags = CRYPTO_ALG_LARVAL; |
| 109 | larval->alg.cra_priority = -1; |
| 110 | larval->alg.cra_destroy = crypto_larval_destroy; |
| 111 | |
| 112 | atomic_set(&larval->alg.cra_refcnt, 2); |
| 113 | strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME); |
| 114 | init_completion(&larval->completion); |
| 115 | |
| 116 | down_write(&crypto_alg_sem); |
| 117 | alg = __crypto_alg_lookup(name); |
| 118 | if (!alg) { |
| 119 | alg = &larval->alg; |
| 120 | list_add(&alg->cra_list, &crypto_alg_list); |
| 121 | } |
| 122 | up_write(&crypto_alg_sem); |
| 123 | |
| 124 | if (alg != &larval->alg) |
| 125 | kfree(larval); |
| 126 | |
| 127 | return alg; |
| 128 | } |
| 129 | |
| 130 | static void crypto_larval_kill(struct crypto_alg *alg) |
| 131 | { |
| 132 | struct crypto_larval *larval = (void *)alg; |
| 133 | |
| 134 | down_write(&crypto_alg_sem); |
| 135 | list_del(&alg->cra_list); |
| 136 | up_write(&crypto_alg_sem); |
| 137 | complete(&larval->completion); |
| 138 | crypto_alg_put(alg); |
| 139 | } |
| 140 | |
| 141 | static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg) |
| 142 | { |
| 143 | struct crypto_larval *larval = (void *)alg; |
| 144 | |
| 145 | wait_for_completion_interruptible_timeout(&larval->completion, 60 * HZ); |
| 146 | alg = larval->adult; |
| 147 | if (alg && !crypto_mod_get(alg)) |
| 148 | alg = NULL; |
| 149 | crypto_mod_put(&larval->alg); |
| 150 | |
| 151 | return alg; |
| 152 | } |
| 153 | |
| 154 | static struct crypto_alg *crypto_alg_lookup(const char *name) |
| 155 | { |
| 156 | struct crypto_alg *alg; |
| 157 | |
| 158 | if (!name) |
| 159 | return NULL; |
| 160 | |
| 161 | down_read(&crypto_alg_sem); |
| 162 | alg = __crypto_alg_lookup(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | up_read(&crypto_alg_sem); |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | return alg; |
| 166 | } |
| 167 | |
Adrian Bunk | 176c365 | 2005-07-06 13:53:09 -0700 | [diff] [blame] | 168 | /* A far more intelligent version of this is planned. For now, just |
| 169 | * try an exact match on the name of the algorithm. */ |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 170 | static struct crypto_alg *crypto_alg_mod_lookup(const char *name) |
Adrian Bunk | 176c365 | 2005-07-06 13:53:09 -0700 | [diff] [blame] | 171 | { |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 172 | struct crypto_alg *alg; |
| 173 | struct crypto_alg *larval; |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame^] | 174 | int ok; |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 175 | |
| 176 | alg = try_then_request_module(crypto_alg_lookup(name), name); |
| 177 | if (alg) |
| 178 | return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg; |
| 179 | |
| 180 | larval = crypto_larval_alloc(name); |
| 181 | if (!larval || !crypto_is_larval(larval)) |
| 182 | return larval; |
| 183 | |
Herbert Xu | 2b8c19d | 2006-09-21 11:31:44 +1000 | [diff] [blame^] | 184 | ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval); |
| 185 | if (ok == NOTIFY_DONE) { |
| 186 | request_module("cryptomgr"); |
| 187 | ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval); |
| 188 | } |
| 189 | |
| 190 | if (ok == NOTIFY_STOP) |
Herbert Xu | 2825982 | 2006-08-06 21:23:26 +1000 | [diff] [blame] | 191 | alg = crypto_larval_wait(larval); |
| 192 | else { |
| 193 | crypto_mod_put(larval); |
| 194 | alg = NULL; |
| 195 | } |
| 196 | crypto_larval_kill(larval); |
| 197 | return alg; |
Adrian Bunk | 176c365 | 2005-07-06 13:53:09 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags) |
| 201 | { |
Herbert Xu | 64baf3c | 2005-09-01 17:43:05 -0700 | [diff] [blame] | 202 | tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK; |
| 203 | flags &= ~CRYPTO_TFM_REQ_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | switch (crypto_tfm_alg_type(tfm)) { |
| 206 | case CRYPTO_ALG_TYPE_CIPHER: |
| 207 | return crypto_init_cipher_flags(tfm, flags); |
| 208 | |
| 209 | case CRYPTO_ALG_TYPE_DIGEST: |
| 210 | return crypto_init_digest_flags(tfm, flags); |
| 211 | |
| 212 | case CRYPTO_ALG_TYPE_COMPRESS: |
| 213 | return crypto_init_compress_flags(tfm, flags); |
| 214 | |
| 215 | default: |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | BUG(); |
| 220 | return -EINVAL; |
| 221 | } |
| 222 | |
| 223 | static int crypto_init_ops(struct crypto_tfm *tfm) |
| 224 | { |
| 225 | switch (crypto_tfm_alg_type(tfm)) { |
| 226 | case CRYPTO_ALG_TYPE_CIPHER: |
| 227 | return crypto_init_cipher_ops(tfm); |
| 228 | |
| 229 | case CRYPTO_ALG_TYPE_DIGEST: |
| 230 | return crypto_init_digest_ops(tfm); |
| 231 | |
| 232 | case CRYPTO_ALG_TYPE_COMPRESS: |
| 233 | return crypto_init_compress_ops(tfm); |
| 234 | |
| 235 | default: |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | BUG(); |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | |
| 243 | static void crypto_exit_ops(struct crypto_tfm *tfm) |
| 244 | { |
| 245 | switch (crypto_tfm_alg_type(tfm)) { |
| 246 | case CRYPTO_ALG_TYPE_CIPHER: |
| 247 | crypto_exit_cipher_ops(tfm); |
| 248 | break; |
| 249 | |
| 250 | case CRYPTO_ALG_TYPE_DIGEST: |
| 251 | crypto_exit_digest_ops(tfm); |
| 252 | break; |
| 253 | |
| 254 | case CRYPTO_ALG_TYPE_COMPRESS: |
| 255 | crypto_exit_compress_ops(tfm); |
| 256 | break; |
| 257 | |
| 258 | default: |
| 259 | BUG(); |
| 260 | |
| 261 | } |
| 262 | } |
| 263 | |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 264 | static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags) |
| 265 | { |
| 266 | unsigned int len; |
| 267 | |
| 268 | switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) { |
| 269 | default: |
| 270 | BUG(); |
| 271 | |
| 272 | case CRYPTO_ALG_TYPE_CIPHER: |
| 273 | len = crypto_cipher_ctxsize(alg, flags); |
| 274 | break; |
| 275 | |
| 276 | case CRYPTO_ALG_TYPE_DIGEST: |
| 277 | len = crypto_digest_ctxsize(alg, flags); |
| 278 | break; |
| 279 | |
| 280 | case CRYPTO_ALG_TYPE_COMPRESS: |
| 281 | len = crypto_compress_ctxsize(alg, flags); |
| 282 | break; |
| 283 | } |
| 284 | |
Herbert Xu | f10b789 | 2006-01-25 22:34:01 +1100 | [diff] [blame] | 285 | return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1)); |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags) |
| 289 | { |
| 290 | struct crypto_tfm *tfm = NULL; |
| 291 | struct crypto_alg *alg; |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 292 | unsigned int tfm_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | alg = crypto_alg_mod_lookup(name); |
| 295 | if (alg == NULL) |
| 296 | goto out; |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 297 | |
| 298 | tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags); |
Eric Sesterhenn | bbeb563 | 2006-03-06 21:42:07 +1100 | [diff] [blame] | 299 | tfm = kzalloc(tfm_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | if (tfm == NULL) |
| 301 | goto out_put; |
| 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | tfm->__crt_alg = alg; |
| 304 | |
| 305 | if (crypto_init_flags(tfm, flags)) |
| 306 | goto out_free_tfm; |
| 307 | |
Herbert Xu | c7fc059 | 2006-05-24 13:02:26 +1000 | [diff] [blame] | 308 | if (crypto_init_ops(tfm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | goto out_free_tfm; |
Herbert Xu | c7fc059 | 2006-05-24 13:02:26 +1000 | [diff] [blame] | 310 | |
| 311 | if (alg->cra_init && alg->cra_init(tfm)) |
| 312 | goto cra_init_failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | goto out; |
| 315 | |
Herbert Xu | c7fc059 | 2006-05-24 13:02:26 +1000 | [diff] [blame] | 316 | cra_init_failed: |
| 317 | crypto_exit_ops(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | out_free_tfm: |
| 319 | kfree(tfm); |
| 320 | tfm = NULL; |
| 321 | out_put: |
Herbert Xu | 72fa491 | 2006-05-28 09:05:24 +1000 | [diff] [blame] | 322 | crypto_mod_put(alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | out: |
| 324 | return tfm; |
| 325 | } |
| 326 | |
| 327 | void crypto_free_tfm(struct crypto_tfm *tfm) |
| 328 | { |
Jesper Juhl | a61cc44 | 2005-07-06 13:54:31 -0700 | [diff] [blame] | 329 | struct crypto_alg *alg; |
| 330 | int size; |
| 331 | |
| 332 | if (unlikely(!tfm)) |
| 333 | return; |
| 334 | |
| 335 | alg = tfm->__crt_alg; |
| 336 | size = sizeof(*tfm) + alg->cra_ctxsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Herbert Xu | c7fc059 | 2006-05-24 13:02:26 +1000 | [diff] [blame] | 338 | if (alg->cra_exit) |
| 339 | alg->cra_exit(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | crypto_exit_ops(tfm); |
Herbert Xu | 72fa491 | 2006-05-28 09:05:24 +1000 | [diff] [blame] | 341 | crypto_mod_put(alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | memset(tfm, 0, size); |
| 343 | kfree(tfm); |
| 344 | } |
| 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | int crypto_alg_available(const char *name, u32 flags) |
| 347 | { |
| 348 | int ret = 0; |
| 349 | struct crypto_alg *alg = crypto_alg_mod_lookup(name); |
| 350 | |
| 351 | if (alg) { |
Herbert Xu | 72fa491 | 2006-05-28 09:05:24 +1000 | [diff] [blame] | 352 | crypto_mod_put(alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | ret = 1; |
| 354 | } |
| 355 | |
| 356 | return ret; |
| 357 | } |
| 358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | EXPORT_SYMBOL_GPL(crypto_alloc_tfm); |
| 360 | EXPORT_SYMBOL_GPL(crypto_free_tfm); |
| 361 | EXPORT_SYMBOL_GPL(crypto_alg_available); |