V4L/DVB (3843): Cxusb: use fe_lgh06xf.h
[linux-2.6/suspend2-2.6.18.git] / crypto / api.c
blob80bba637fba7c1d325e0718bcfa800659f46251e
1 /*
2 * Scatterlist Cryptographic API.
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
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.
18 #include <linux/compiler.h>
19 #include <linux/init.h>
20 #include <linux/crypto.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/kmod.h>
24 #include <linux/rwsem.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include "internal.h"
29 LIST_HEAD(crypto_alg_list);
30 DECLARE_RWSEM(crypto_alg_sem);
32 static inline int crypto_alg_get(struct crypto_alg *alg)
34 return try_module_get(alg->cra_module);
37 static inline void crypto_alg_put(struct crypto_alg *alg)
39 module_put(alg->cra_module);
42 static struct crypto_alg *crypto_alg_lookup(const char *name)
44 struct crypto_alg *q, *alg = NULL;
45 int best = -1;
47 if (!name)
48 return NULL;
50 down_read(&crypto_alg_sem);
52 list_for_each_entry(q, &crypto_alg_list, cra_list) {
53 int exact, fuzzy;
55 exact = !strcmp(q->cra_driver_name, name);
56 fuzzy = !strcmp(q->cra_name, name);
57 if (!exact && !(fuzzy && q->cra_priority > best))
58 continue;
60 if (unlikely(!crypto_alg_get(q)))
61 continue;
63 best = q->cra_priority;
64 if (alg)
65 crypto_alg_put(alg);
66 alg = q;
68 if (exact)
69 break;
72 up_read(&crypto_alg_sem);
73 return alg;
76 /* A far more intelligent version of this is planned. For now, just
77 * try an exact match on the name of the algorithm. */
78 static inline struct crypto_alg *crypto_alg_mod_lookup(const char *name)
80 return try_then_request_module(crypto_alg_lookup(name), name);
83 static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
85 tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
86 flags &= ~CRYPTO_TFM_REQ_MASK;
88 switch (crypto_tfm_alg_type(tfm)) {
89 case CRYPTO_ALG_TYPE_CIPHER:
90 return crypto_init_cipher_flags(tfm, flags);
92 case CRYPTO_ALG_TYPE_DIGEST:
93 return crypto_init_digest_flags(tfm, flags);
95 case CRYPTO_ALG_TYPE_COMPRESS:
96 return crypto_init_compress_flags(tfm, flags);
98 default:
99 break;
102 BUG();
103 return -EINVAL;
106 static int crypto_init_ops(struct crypto_tfm *tfm)
108 switch (crypto_tfm_alg_type(tfm)) {
109 case CRYPTO_ALG_TYPE_CIPHER:
110 return crypto_init_cipher_ops(tfm);
112 case CRYPTO_ALG_TYPE_DIGEST:
113 return crypto_init_digest_ops(tfm);
115 case CRYPTO_ALG_TYPE_COMPRESS:
116 return crypto_init_compress_ops(tfm);
118 default:
119 break;
122 BUG();
123 return -EINVAL;
126 static void crypto_exit_ops(struct crypto_tfm *tfm)
128 switch (crypto_tfm_alg_type(tfm)) {
129 case CRYPTO_ALG_TYPE_CIPHER:
130 crypto_exit_cipher_ops(tfm);
131 break;
133 case CRYPTO_ALG_TYPE_DIGEST:
134 crypto_exit_digest_ops(tfm);
135 break;
137 case CRYPTO_ALG_TYPE_COMPRESS:
138 crypto_exit_compress_ops(tfm);
139 break;
141 default:
142 BUG();
147 static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
149 unsigned int len;
151 switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
152 default:
153 BUG();
155 case CRYPTO_ALG_TYPE_CIPHER:
156 len = crypto_cipher_ctxsize(alg, flags);
157 break;
159 case CRYPTO_ALG_TYPE_DIGEST:
160 len = crypto_digest_ctxsize(alg, flags);
161 break;
163 case CRYPTO_ALG_TYPE_COMPRESS:
164 len = crypto_compress_ctxsize(alg, flags);
165 break;
168 return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
171 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
173 struct crypto_tfm *tfm = NULL;
174 struct crypto_alg *alg;
175 unsigned int tfm_size;
177 alg = crypto_alg_mod_lookup(name);
178 if (alg == NULL)
179 goto out;
181 tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
182 tfm = kzalloc(tfm_size, GFP_KERNEL);
183 if (tfm == NULL)
184 goto out_put;
186 tfm->__crt_alg = alg;
188 if (crypto_init_flags(tfm, flags))
189 goto out_free_tfm;
191 if (crypto_init_ops(tfm)) {
192 crypto_exit_ops(tfm);
193 goto out_free_tfm;
196 goto out;
198 out_free_tfm:
199 kfree(tfm);
200 tfm = NULL;
201 out_put:
202 crypto_alg_put(alg);
203 out:
204 return tfm;
207 void crypto_free_tfm(struct crypto_tfm *tfm)
209 struct crypto_alg *alg;
210 int size;
212 if (unlikely(!tfm))
213 return;
215 alg = tfm->__crt_alg;
216 size = sizeof(*tfm) + alg->cra_ctxsize;
218 crypto_exit_ops(tfm);
219 crypto_alg_put(alg);
220 memset(tfm, 0, size);
221 kfree(tfm);
224 static inline int crypto_set_driver_name(struct crypto_alg *alg)
226 static const char suffix[] = "-generic";
227 char *driver_name = (char *)alg->cra_driver_name;
228 int len;
230 if (*driver_name)
231 return 0;
233 len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
234 if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
235 return -ENAMETOOLONG;
237 memcpy(driver_name + len, suffix, sizeof(suffix));
238 return 0;
241 int crypto_register_alg(struct crypto_alg *alg)
243 int ret;
244 struct crypto_alg *q;
246 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
247 return -EINVAL;
249 if (alg->cra_alignmask & alg->cra_blocksize)
250 return -EINVAL;
252 if (alg->cra_blocksize > PAGE_SIZE / 8)
253 return -EINVAL;
255 if (alg->cra_priority < 0)
256 return -EINVAL;
258 ret = crypto_set_driver_name(alg);
259 if (unlikely(ret))
260 return ret;
262 down_write(&crypto_alg_sem);
264 list_for_each_entry(q, &crypto_alg_list, cra_list) {
265 if (!strcmp(q->cra_driver_name, alg->cra_driver_name)) {
266 ret = -EEXIST;
267 goto out;
271 list_add_tail(&alg->cra_list, &crypto_alg_list);
272 out:
273 up_write(&crypto_alg_sem);
274 return ret;
277 int crypto_unregister_alg(struct crypto_alg *alg)
279 int ret = -ENOENT;
280 struct crypto_alg *q;
282 BUG_ON(!alg->cra_module);
284 down_write(&crypto_alg_sem);
285 list_for_each_entry(q, &crypto_alg_list, cra_list) {
286 if (alg == q) {
287 list_del(&alg->cra_list);
288 ret = 0;
289 goto out;
292 out:
293 up_write(&crypto_alg_sem);
294 return ret;
297 int crypto_alg_available(const char *name, u32 flags)
299 int ret = 0;
300 struct crypto_alg *alg = crypto_alg_mod_lookup(name);
302 if (alg) {
303 crypto_alg_put(alg);
304 ret = 1;
307 return ret;
310 static int __init init_crypto(void)
312 printk(KERN_INFO "Initializing Cryptographic API\n");
313 crypto_init_proc();
314 return 0;
317 __initcall(init_crypto);
319 EXPORT_SYMBOL_GPL(crypto_register_alg);
320 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
321 EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
322 EXPORT_SYMBOL_GPL(crypto_free_tfm);
323 EXPORT_SYMBOL_GPL(crypto_alg_available);