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) |
| 6 | * |
| 7 | * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no> |
| 8 | * and Nettle, by Niels Möller. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the Free |
| 12 | * Software Foundation; either version 2 of the License, or (at your option) |
| 13 | * any later version. |
| 14 | * |
| 15 | */ |
Jesper Juhl | a61cc44 | 2005-07-06 13:54:31 -0700 | [diff] [blame^] | 16 | |
| 17 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/crypto.h> |
| 20 | #include <linux/errno.h> |
Adrian Bunk | 176c365 | 2005-07-06 13:53:09 -0700 | [diff] [blame] | 21 | #include <linux/kmod.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/rwsem.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include "internal.h" |
| 25 | |
| 26 | LIST_HEAD(crypto_alg_list); |
| 27 | DECLARE_RWSEM(crypto_alg_sem); |
| 28 | |
| 29 | static inline int crypto_alg_get(struct crypto_alg *alg) |
| 30 | { |
| 31 | return try_module_get(alg->cra_module); |
| 32 | } |
| 33 | |
| 34 | static inline void crypto_alg_put(struct crypto_alg *alg) |
| 35 | { |
| 36 | module_put(alg->cra_module); |
| 37 | } |
| 38 | |
Adrian Bunk | 176c365 | 2005-07-06 13:53:09 -0700 | [diff] [blame] | 39 | static struct crypto_alg *crypto_alg_lookup(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
| 41 | struct crypto_alg *q, *alg = NULL; |
| 42 | |
| 43 | if (!name) |
| 44 | return NULL; |
| 45 | |
| 46 | down_read(&crypto_alg_sem); |
| 47 | |
| 48 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
| 49 | if (!(strcmp(q->cra_name, name))) { |
| 50 | if (crypto_alg_get(q)) |
| 51 | alg = q; |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | up_read(&crypto_alg_sem); |
| 57 | return alg; |
| 58 | } |
| 59 | |
Adrian Bunk | 176c365 | 2005-07-06 13:53:09 -0700 | [diff] [blame] | 60 | /* A far more intelligent version of this is planned. For now, just |
| 61 | * try an exact match on the name of the algorithm. */ |
| 62 | static inline struct crypto_alg *crypto_alg_mod_lookup(const char *name) |
| 63 | { |
| 64 | return try_then_request_module(crypto_alg_lookup(name), name); |
| 65 | } |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags) |
| 68 | { |
| 69 | tfm->crt_flags = 0; |
| 70 | |
| 71 | switch (crypto_tfm_alg_type(tfm)) { |
| 72 | case CRYPTO_ALG_TYPE_CIPHER: |
| 73 | return crypto_init_cipher_flags(tfm, flags); |
| 74 | |
| 75 | case CRYPTO_ALG_TYPE_DIGEST: |
| 76 | return crypto_init_digest_flags(tfm, flags); |
| 77 | |
| 78 | case CRYPTO_ALG_TYPE_COMPRESS: |
| 79 | return crypto_init_compress_flags(tfm, flags); |
| 80 | |
| 81 | default: |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | BUG(); |
| 86 | return -EINVAL; |
| 87 | } |
| 88 | |
| 89 | static int crypto_init_ops(struct crypto_tfm *tfm) |
| 90 | { |
| 91 | switch (crypto_tfm_alg_type(tfm)) { |
| 92 | case CRYPTO_ALG_TYPE_CIPHER: |
| 93 | return crypto_init_cipher_ops(tfm); |
| 94 | |
| 95 | case CRYPTO_ALG_TYPE_DIGEST: |
| 96 | return crypto_init_digest_ops(tfm); |
| 97 | |
| 98 | case CRYPTO_ALG_TYPE_COMPRESS: |
| 99 | return crypto_init_compress_ops(tfm); |
| 100 | |
| 101 | default: |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | BUG(); |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | |
| 109 | static void crypto_exit_ops(struct crypto_tfm *tfm) |
| 110 | { |
| 111 | switch (crypto_tfm_alg_type(tfm)) { |
| 112 | case CRYPTO_ALG_TYPE_CIPHER: |
| 113 | crypto_exit_cipher_ops(tfm); |
| 114 | break; |
| 115 | |
| 116 | case CRYPTO_ALG_TYPE_DIGEST: |
| 117 | crypto_exit_digest_ops(tfm); |
| 118 | break; |
| 119 | |
| 120 | case CRYPTO_ALG_TYPE_COMPRESS: |
| 121 | crypto_exit_compress_ops(tfm); |
| 122 | break; |
| 123 | |
| 124 | default: |
| 125 | BUG(); |
| 126 | |
| 127 | } |
| 128 | } |
| 129 | |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 130 | static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags) |
| 131 | { |
| 132 | unsigned int len; |
| 133 | |
| 134 | switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) { |
| 135 | default: |
| 136 | BUG(); |
| 137 | |
| 138 | case CRYPTO_ALG_TYPE_CIPHER: |
| 139 | len = crypto_cipher_ctxsize(alg, flags); |
| 140 | break; |
| 141 | |
| 142 | case CRYPTO_ALG_TYPE_DIGEST: |
| 143 | len = crypto_digest_ctxsize(alg, flags); |
| 144 | break; |
| 145 | |
| 146 | case CRYPTO_ALG_TYPE_COMPRESS: |
| 147 | len = crypto_compress_ctxsize(alg, flags); |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | return len + alg->cra_alignmask; |
| 152 | } |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags) |
| 155 | { |
| 156 | struct crypto_tfm *tfm = NULL; |
| 157 | struct crypto_alg *alg; |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 158 | unsigned int tfm_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | alg = crypto_alg_mod_lookup(name); |
| 161 | if (alg == NULL) |
| 162 | goto out; |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 163 | |
| 164 | tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags); |
| 165 | tfm = kmalloc(tfm_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | if (tfm == NULL) |
| 167 | goto out_put; |
| 168 | |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 169 | memset(tfm, 0, tfm_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | tfm->__crt_alg = alg; |
| 172 | |
| 173 | if (crypto_init_flags(tfm, flags)) |
| 174 | goto out_free_tfm; |
| 175 | |
| 176 | if (crypto_init_ops(tfm)) { |
| 177 | crypto_exit_ops(tfm); |
| 178 | goto out_free_tfm; |
| 179 | } |
| 180 | |
| 181 | goto out; |
| 182 | |
| 183 | out_free_tfm: |
| 184 | kfree(tfm); |
| 185 | tfm = NULL; |
| 186 | out_put: |
| 187 | crypto_alg_put(alg); |
| 188 | out: |
| 189 | return tfm; |
| 190 | } |
| 191 | |
| 192 | void crypto_free_tfm(struct crypto_tfm *tfm) |
| 193 | { |
Jesper Juhl | a61cc44 | 2005-07-06 13:54:31 -0700 | [diff] [blame^] | 194 | struct crypto_alg *alg; |
| 195 | int size; |
| 196 | |
| 197 | if (unlikely(!tfm)) |
| 198 | return; |
| 199 | |
| 200 | alg = tfm->__crt_alg; |
| 201 | size = sizeof(*tfm) + alg->cra_ctxsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | crypto_exit_ops(tfm); |
| 204 | crypto_alg_put(alg); |
| 205 | memset(tfm, 0, size); |
| 206 | kfree(tfm); |
| 207 | } |
| 208 | |
| 209 | int crypto_register_alg(struct crypto_alg *alg) |
| 210 | { |
| 211 | int ret = 0; |
| 212 | struct crypto_alg *q; |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 213 | |
| 214 | if (alg->cra_alignmask & (alg->cra_alignmask + 1)) |
| 215 | return -EINVAL; |
| 216 | |
| 217 | if (alg->cra_alignmask > PAGE_SIZE) |
| 218 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | down_write(&crypto_alg_sem); |
| 221 | |
| 222 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
| 223 | if (!(strcmp(q->cra_name, alg->cra_name))) { |
| 224 | ret = -EEXIST; |
| 225 | goto out; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | list_add_tail(&alg->cra_list, &crypto_alg_list); |
| 230 | out: |
| 231 | up_write(&crypto_alg_sem); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | int crypto_unregister_alg(struct crypto_alg *alg) |
| 236 | { |
| 237 | int ret = -ENOENT; |
| 238 | struct crypto_alg *q; |
| 239 | |
| 240 | BUG_ON(!alg->cra_module); |
| 241 | |
| 242 | down_write(&crypto_alg_sem); |
| 243 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
| 244 | if (alg == q) { |
| 245 | list_del(&alg->cra_list); |
| 246 | ret = 0; |
| 247 | goto out; |
| 248 | } |
| 249 | } |
| 250 | out: |
| 251 | up_write(&crypto_alg_sem); |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | int crypto_alg_available(const char *name, u32 flags) |
| 256 | { |
| 257 | int ret = 0; |
| 258 | struct crypto_alg *alg = crypto_alg_mod_lookup(name); |
| 259 | |
| 260 | if (alg) { |
| 261 | crypto_alg_put(alg); |
| 262 | ret = 1; |
| 263 | } |
| 264 | |
| 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | static int __init init_crypto(void) |
| 269 | { |
| 270 | printk(KERN_INFO "Initializing Cryptographic API\n"); |
| 271 | crypto_init_proc(); |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | __initcall(init_crypto); |
| 276 | |
| 277 | EXPORT_SYMBOL_GPL(crypto_register_alg); |
| 278 | EXPORT_SYMBOL_GPL(crypto_unregister_alg); |
| 279 | EXPORT_SYMBOL_GPL(crypto_alloc_tfm); |
| 280 | EXPORT_SYMBOL_GPL(crypto_free_tfm); |
| 281 | EXPORT_SYMBOL_GPL(crypto_alg_available); |