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