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