[CRYPTO] api: Add cryptomgr
[linux-2.6/btrfs-unstable.git] / crypto / api.c
blob67cd6f87b74a5289d2ac7b3be0b9da2dc322c6a9
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)
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 exact = !strcmp(q->cra_driver_name, name);
69 fuzzy = !strcmp(q->cra_name, name);
70 if (!exact && !(fuzzy && q->cra_priority > best))
71 continue;
73 if (unlikely(!crypto_mod_get(q)))
74 continue;
76 best = q->cra_priority;
77 if (alg)
78 crypto_mod_put(alg);
79 alg = q;
81 if (exact)
82 break;
85 return alg;
87 EXPORT_SYMBOL_GPL(__crypto_alg_lookup);
89 static void crypto_larval_destroy(struct crypto_alg *alg)
91 struct crypto_larval *larval = (void *)alg;
93 BUG_ON(!crypto_is_larval(alg));
94 if (larval->adult)
95 crypto_mod_put(larval->adult);
96 kfree(larval);
99 static struct crypto_alg *crypto_larval_alloc(const char *name)
101 struct crypto_alg *alg;
102 struct crypto_larval *larval;
104 larval = kzalloc(sizeof(*larval), GFP_KERNEL);
105 if (!larval)
106 return NULL;
108 larval->alg.cra_flags = CRYPTO_ALG_LARVAL;
109 larval->alg.cra_priority = -1;
110 larval->alg.cra_destroy = crypto_larval_destroy;
112 atomic_set(&larval->alg.cra_refcnt, 2);
113 strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
114 init_completion(&larval->completion);
116 down_write(&crypto_alg_sem);
117 alg = __crypto_alg_lookup(name);
118 if (!alg) {
119 alg = &larval->alg;
120 list_add(&alg->cra_list, &crypto_alg_list);
122 up_write(&crypto_alg_sem);
124 if (alg != &larval->alg)
125 kfree(larval);
127 return alg;
130 static void crypto_larval_kill(struct crypto_alg *alg)
132 struct crypto_larval *larval = (void *)alg;
134 down_write(&crypto_alg_sem);
135 list_del(&alg->cra_list);
136 up_write(&crypto_alg_sem);
137 complete(&larval->completion);
138 crypto_alg_put(alg);
141 static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
143 struct crypto_larval *larval = (void *)alg;
145 wait_for_completion_interruptible_timeout(&larval->completion, 60 * HZ);
146 alg = larval->adult;
147 if (alg && !crypto_mod_get(alg))
148 alg = NULL;
149 crypto_mod_put(&larval->alg);
151 return alg;
154 static struct crypto_alg *crypto_alg_lookup(const char *name)
156 struct crypto_alg *alg;
158 if (!name)
159 return NULL;
161 down_read(&crypto_alg_sem);
162 alg = __crypto_alg_lookup(name);
163 up_read(&crypto_alg_sem);
165 return alg;
168 /* A far more intelligent version of this is planned. For now, just
169 * try an exact match on the name of the algorithm. */
170 static struct crypto_alg *crypto_alg_mod_lookup(const char *name)
172 struct crypto_alg *alg;
173 struct crypto_alg *larval;
174 int ok;
176 alg = try_then_request_module(crypto_alg_lookup(name), name);
177 if (alg)
178 return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
180 larval = crypto_larval_alloc(name);
181 if (!larval || !crypto_is_larval(larval))
182 return larval;
184 ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval);
185 if (ok == NOTIFY_DONE) {
186 request_module("cryptomgr");
187 ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval);
190 if (ok == NOTIFY_STOP)
191 alg = crypto_larval_wait(larval);
192 else {
193 crypto_mod_put(larval);
194 alg = NULL;
196 crypto_larval_kill(larval);
197 return alg;
200 static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
202 tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
203 flags &= ~CRYPTO_TFM_REQ_MASK;
205 switch (crypto_tfm_alg_type(tfm)) {
206 case CRYPTO_ALG_TYPE_CIPHER:
207 return crypto_init_cipher_flags(tfm, flags);
209 case CRYPTO_ALG_TYPE_DIGEST:
210 return crypto_init_digest_flags(tfm, flags);
212 case CRYPTO_ALG_TYPE_COMPRESS:
213 return crypto_init_compress_flags(tfm, flags);
215 default:
216 break;
219 BUG();
220 return -EINVAL;
223 static int crypto_init_ops(struct crypto_tfm *tfm)
225 switch (crypto_tfm_alg_type(tfm)) {
226 case CRYPTO_ALG_TYPE_CIPHER:
227 return crypto_init_cipher_ops(tfm);
229 case CRYPTO_ALG_TYPE_DIGEST:
230 return crypto_init_digest_ops(tfm);
232 case CRYPTO_ALG_TYPE_COMPRESS:
233 return crypto_init_compress_ops(tfm);
235 default:
236 break;
239 BUG();
240 return -EINVAL;
243 static void crypto_exit_ops(struct crypto_tfm *tfm)
245 switch (crypto_tfm_alg_type(tfm)) {
246 case CRYPTO_ALG_TYPE_CIPHER:
247 crypto_exit_cipher_ops(tfm);
248 break;
250 case CRYPTO_ALG_TYPE_DIGEST:
251 crypto_exit_digest_ops(tfm);
252 break;
254 case CRYPTO_ALG_TYPE_COMPRESS:
255 crypto_exit_compress_ops(tfm);
256 break;
258 default:
259 BUG();
264 static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
266 unsigned int len;
268 switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
269 default:
270 BUG();
272 case CRYPTO_ALG_TYPE_CIPHER:
273 len = crypto_cipher_ctxsize(alg, flags);
274 break;
276 case CRYPTO_ALG_TYPE_DIGEST:
277 len = crypto_digest_ctxsize(alg, flags);
278 break;
280 case CRYPTO_ALG_TYPE_COMPRESS:
281 len = crypto_compress_ctxsize(alg, flags);
282 break;
285 return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
288 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
290 struct crypto_tfm *tfm = NULL;
291 struct crypto_alg *alg;
292 unsigned int tfm_size;
294 alg = crypto_alg_mod_lookup(name);
295 if (alg == NULL)
296 goto out;
298 tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
299 tfm = kzalloc(tfm_size, GFP_KERNEL);
300 if (tfm == NULL)
301 goto out_put;
303 tfm->__crt_alg = alg;
305 if (crypto_init_flags(tfm, flags))
306 goto out_free_tfm;
308 if (crypto_init_ops(tfm))
309 goto out_free_tfm;
311 if (alg->cra_init && alg->cra_init(tfm))
312 goto cra_init_failed;
314 goto out;
316 cra_init_failed:
317 crypto_exit_ops(tfm);
318 out_free_tfm:
319 kfree(tfm);
320 tfm = NULL;
321 out_put:
322 crypto_mod_put(alg);
323 out:
324 return tfm;
327 void crypto_free_tfm(struct crypto_tfm *tfm)
329 struct crypto_alg *alg;
330 int size;
332 if (unlikely(!tfm))
333 return;
335 alg = tfm->__crt_alg;
336 size = sizeof(*tfm) + alg->cra_ctxsize;
338 if (alg->cra_exit)
339 alg->cra_exit(tfm);
340 crypto_exit_ops(tfm);
341 crypto_mod_put(alg);
342 memset(tfm, 0, size);
343 kfree(tfm);
346 int crypto_alg_available(const char *name, u32 flags)
348 int ret = 0;
349 struct crypto_alg *alg = crypto_alg_mod_lookup(name);
351 if (alg) {
352 crypto_mod_put(alg);
353 ret = 1;
356 return ret;
359 EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
360 EXPORT_SYMBOL_GPL(crypto_free_tfm);
361 EXPORT_SYMBOL_GPL(crypto_alg_available);