[CRYPTO] api: Allow algorithm lookup by type
[linux-2.6.git] / crypto / api.c
blobddf6a767acdd72a155c44ea5dbcc62bce2450dc4
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/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/kmod.h>
21 #include <linux/module.h>
22 #include <linux/param.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include "internal.h"
27 LIST_HEAD(crypto_alg_list);
28 EXPORT_SYMBOL_GPL(crypto_alg_list);
29 DECLARE_RWSEM(crypto_alg_sem);
30 EXPORT_SYMBOL_GPL(crypto_alg_sem);
32 BLOCKING_NOTIFIER_HEAD(crypto_chain);
33 EXPORT_SYMBOL_GPL(crypto_chain);
35 static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
37 atomic_inc(&alg->cra_refcnt);
38 return alg;
41 static inline void crypto_alg_put(struct crypto_alg *alg)
43 if (atomic_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
44 alg->cra_destroy(alg);
47 struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
49 return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
51 EXPORT_SYMBOL_GPL(crypto_mod_get);
53 void crypto_mod_put(struct crypto_alg *alg)
55 crypto_alg_put(alg);
56 module_put(alg->cra_module);
58 EXPORT_SYMBOL_GPL(crypto_mod_put);
60 struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type, u32 mask)
62 struct crypto_alg *q, *alg = NULL;
63 int best = -2;
65 list_for_each_entry(q, &crypto_alg_list, cra_list) {
66 int exact, fuzzy;
68 if ((q->cra_flags ^ type) & mask)
69 continue;
71 if (crypto_is_larval(q) &&
72 ((struct crypto_larval *)q)->mask != mask)
73 continue;
75 exact = !strcmp(q->cra_driver_name, name);
76 fuzzy = !strcmp(q->cra_name, name);
77 if (!exact && !(fuzzy && q->cra_priority > best))
78 continue;
80 if (unlikely(!crypto_mod_get(q)))
81 continue;
83 best = q->cra_priority;
84 if (alg)
85 crypto_mod_put(alg);
86 alg = q;
88 if (exact)
89 break;
92 return alg;
94 EXPORT_SYMBOL_GPL(__crypto_alg_lookup);
96 static void crypto_larval_destroy(struct crypto_alg *alg)
98 struct crypto_larval *larval = (void *)alg;
100 BUG_ON(!crypto_is_larval(alg));
101 if (larval->adult)
102 crypto_mod_put(larval->adult);
103 kfree(larval);
106 static struct crypto_alg *crypto_larval_alloc(const char *name, u32 type,
107 u32 mask)
109 struct crypto_alg *alg;
110 struct crypto_larval *larval;
112 larval = kzalloc(sizeof(*larval), GFP_KERNEL);
113 if (!larval)
114 return NULL;
116 larval->mask = mask;
117 larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type;
118 larval->alg.cra_priority = -1;
119 larval->alg.cra_destroy = crypto_larval_destroy;
121 atomic_set(&larval->alg.cra_refcnt, 2);
122 strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
123 init_completion(&larval->completion);
125 down_write(&crypto_alg_sem);
126 alg = __crypto_alg_lookup(name, type, mask);
127 if (!alg) {
128 alg = &larval->alg;
129 list_add(&alg->cra_list, &crypto_alg_list);
131 up_write(&crypto_alg_sem);
133 if (alg != &larval->alg)
134 kfree(larval);
136 return alg;
139 static void crypto_larval_kill(struct crypto_alg *alg)
141 struct crypto_larval *larval = (void *)alg;
143 down_write(&crypto_alg_sem);
144 list_del(&alg->cra_list);
145 up_write(&crypto_alg_sem);
146 complete(&larval->completion);
147 crypto_alg_put(alg);
150 static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
152 struct crypto_larval *larval = (void *)alg;
154 wait_for_completion_interruptible_timeout(&larval->completion, 60 * HZ);
155 alg = larval->adult;
156 if (alg && !crypto_mod_get(alg))
157 alg = NULL;
158 crypto_mod_put(&larval->alg);
160 return alg;
163 static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
164 u32 mask)
166 struct crypto_alg *alg;
168 if (!name)
169 return NULL;
171 down_read(&crypto_alg_sem);
172 alg = __crypto_alg_lookup(name, type, mask);
173 up_read(&crypto_alg_sem);
175 return alg;
178 struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
180 struct crypto_alg *alg;
181 struct crypto_alg *larval;
182 int ok;
184 mask &= ~CRYPTO_ALG_LARVAL;
185 type &= mask;
187 alg = try_then_request_module(crypto_alg_lookup(name, type, mask),
188 name);
189 if (alg)
190 return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
192 larval = crypto_larval_alloc(name, type, mask);
193 if (!larval || !crypto_is_larval(larval))
194 return larval;
196 ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval);
197 if (ok == NOTIFY_DONE) {
198 request_module("cryptomgr");
199 ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval);
202 if (ok == NOTIFY_STOP)
203 alg = crypto_larval_wait(larval);
204 else {
205 crypto_mod_put(larval);
206 alg = NULL;
208 crypto_larval_kill(larval);
209 return alg;
211 EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
213 static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
215 tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
216 flags &= ~CRYPTO_TFM_REQ_MASK;
218 switch (crypto_tfm_alg_type(tfm)) {
219 case CRYPTO_ALG_TYPE_CIPHER:
220 return crypto_init_cipher_flags(tfm, flags);
222 case CRYPTO_ALG_TYPE_DIGEST:
223 return crypto_init_digest_flags(tfm, flags);
225 case CRYPTO_ALG_TYPE_COMPRESS:
226 return crypto_init_compress_flags(tfm, flags);
228 default:
229 break;
232 BUG();
233 return -EINVAL;
236 static int crypto_init_ops(struct crypto_tfm *tfm)
238 switch (crypto_tfm_alg_type(tfm)) {
239 case CRYPTO_ALG_TYPE_CIPHER:
240 return crypto_init_cipher_ops(tfm);
242 case CRYPTO_ALG_TYPE_DIGEST:
243 return crypto_init_digest_ops(tfm);
245 case CRYPTO_ALG_TYPE_COMPRESS:
246 return crypto_init_compress_ops(tfm);
248 default:
249 break;
252 BUG();
253 return -EINVAL;
256 static void crypto_exit_ops(struct crypto_tfm *tfm)
258 switch (crypto_tfm_alg_type(tfm)) {
259 case CRYPTO_ALG_TYPE_CIPHER:
260 crypto_exit_cipher_ops(tfm);
261 break;
263 case CRYPTO_ALG_TYPE_DIGEST:
264 crypto_exit_digest_ops(tfm);
265 break;
267 case CRYPTO_ALG_TYPE_COMPRESS:
268 crypto_exit_compress_ops(tfm);
269 break;
271 default:
272 BUG();
277 static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
279 unsigned int len;
281 switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
282 default:
283 BUG();
285 case CRYPTO_ALG_TYPE_CIPHER:
286 len = crypto_cipher_ctxsize(alg, flags);
287 break;
289 case CRYPTO_ALG_TYPE_DIGEST:
290 len = crypto_digest_ctxsize(alg, flags);
291 break;
293 case CRYPTO_ALG_TYPE_COMPRESS:
294 len = crypto_compress_ctxsize(alg, flags);
295 break;
298 return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
301 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
303 struct crypto_tfm *tfm = NULL;
304 struct crypto_alg *alg;
305 unsigned int tfm_size;
307 alg = crypto_alg_mod_lookup(name, 0, 0);
308 if (alg == NULL)
309 goto out;
311 tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
312 tfm = kzalloc(tfm_size, GFP_KERNEL);
313 if (tfm == NULL)
314 goto out_put;
316 tfm->__crt_alg = alg;
318 if (crypto_init_flags(tfm, flags))
319 goto out_free_tfm;
321 if (crypto_init_ops(tfm))
322 goto out_free_tfm;
324 if (alg->cra_init && alg->cra_init(tfm))
325 goto cra_init_failed;
327 goto out;
329 cra_init_failed:
330 crypto_exit_ops(tfm);
331 out_free_tfm:
332 kfree(tfm);
333 tfm = NULL;
334 out_put:
335 crypto_mod_put(alg);
336 out:
337 return tfm;
340 void crypto_free_tfm(struct crypto_tfm *tfm)
342 struct crypto_alg *alg;
343 int size;
345 if (unlikely(!tfm))
346 return;
348 alg = tfm->__crt_alg;
349 size = sizeof(*tfm) + alg->cra_ctxsize;
351 if (alg->cra_exit)
352 alg->cra_exit(tfm);
353 crypto_exit_ops(tfm);
354 crypto_mod_put(alg);
355 memset(tfm, 0, size);
356 kfree(tfm);
359 int crypto_alg_available(const char *name, u32 flags)
361 int ret = 0;
362 struct crypto_alg *alg = crypto_alg_mod_lookup(name, 0, 0);
364 if (alg) {
365 crypto_mod_put(alg);
366 ret = 1;
369 return ret;
372 EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
373 EXPORT_SYMBOL_GPL(crypto_free_tfm);
374 EXPORT_SYMBOL_GPL(crypto_alg_available);