[CRYPTO] api: Add cryptomgr
[safe/jmp/linux-2.6] / crypto / api.c
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  * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
7  *
8  * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9  * and Nettle, by Niels Möller.
10  *
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.
15  *
16  */
17
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"
26
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);
31
32 BLOCKING_NOTIFIER_HEAD(crypto_chain);
33 EXPORT_SYMBOL_GPL(crypto_chain);
34
35 static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
36 {
37         atomic_inc(&alg->cra_refcnt);
38         return alg;
39 }
40
41 static inline void crypto_alg_put(struct crypto_alg *alg)
42 {
43         if (atomic_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
44                 alg->cra_destroy(alg);
45 }
46
47 struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
48 {
49         return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
50 }
51 EXPORT_SYMBOL_GPL(crypto_mod_get);
52
53 void crypto_mod_put(struct crypto_alg *alg)
54 {
55         crypto_alg_put(alg);
56         module_put(alg->cra_module);
57 }
58 EXPORT_SYMBOL_GPL(crypto_mod_put);
59
60 struct crypto_alg *__crypto_alg_lookup(const char *name)
61 {
62         struct crypto_alg *q, *alg = NULL;
63         int best = -2;
64
65         list_for_each_entry(q, &crypto_alg_list, cra_list) {
66                 int exact, fuzzy;
67
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;
72
73                 if (unlikely(!crypto_mod_get(q)))
74                         continue;
75
76                 best = q->cra_priority;
77                 if (alg)
78                         crypto_mod_put(alg);
79                 alg = q;
80
81                 if (exact)
82                         break;
83         }
84
85         return alg;
86 }
87 EXPORT_SYMBOL_GPL(__crypto_alg_lookup);
88
89 static void crypto_larval_destroy(struct crypto_alg *alg)
90 {
91         struct crypto_larval *larval = (void *)alg;
92
93         BUG_ON(!crypto_is_larval(alg));
94         if (larval->adult)
95                 crypto_mod_put(larval->adult);
96         kfree(larval);
97 }
98
99 static struct crypto_alg *crypto_larval_alloc(const char *name)
100 {
101         struct crypto_alg *alg;
102         struct crypto_larval *larval;
103
104         larval = kzalloc(sizeof(*larval), GFP_KERNEL);
105         if (!larval)
106                 return NULL;
107
108         larval->alg.cra_flags = CRYPTO_ALG_LARVAL;
109         larval->alg.cra_priority = -1;
110         larval->alg.cra_destroy = crypto_larval_destroy;
111
112         atomic_set(&larval->alg.cra_refcnt, 2);
113         strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
114         init_completion(&larval->completion);
115
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);
121         }
122         up_write(&crypto_alg_sem);
123
124         if (alg != &larval->alg)
125                 kfree(larval);
126
127         return alg;
128 }
129
130 static void crypto_larval_kill(struct crypto_alg *alg)
131 {
132         struct crypto_larval *larval = (void *)alg;
133
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);
139 }
140
141 static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
142 {
143         struct crypto_larval *larval = (void *)alg;
144
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);
150
151         return alg;
152 }
153
154 static struct crypto_alg *crypto_alg_lookup(const char *name)
155 {
156         struct crypto_alg *alg;
157
158         if (!name)
159                 return NULL;
160
161         down_read(&crypto_alg_sem);
162         alg = __crypto_alg_lookup(name);
163         up_read(&crypto_alg_sem);
164
165         return alg;
166 }
167
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)
171 {
172         struct crypto_alg *alg;
173         struct crypto_alg *larval;
174         int ok;
175
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;
179
180         larval = crypto_larval_alloc(name);
181         if (!larval || !crypto_is_larval(larval))
182                 return larval;
183
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);
188         }
189
190         if (ok == NOTIFY_STOP)
191                 alg = crypto_larval_wait(larval);
192         else {
193                 crypto_mod_put(larval);
194                 alg = NULL;
195         }
196         crypto_larval_kill(larval);
197         return alg;
198 }
199
200 static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
201 {
202         tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
203         flags &= ~CRYPTO_TFM_REQ_MASK;
204         
205         switch (crypto_tfm_alg_type(tfm)) {
206         case CRYPTO_ALG_TYPE_CIPHER:
207                 return crypto_init_cipher_flags(tfm, flags);
208                 
209         case CRYPTO_ALG_TYPE_DIGEST:
210                 return crypto_init_digest_flags(tfm, flags);
211                 
212         case CRYPTO_ALG_TYPE_COMPRESS:
213                 return crypto_init_compress_flags(tfm, flags);
214         
215         default:
216                 break;
217         }
218         
219         BUG();
220         return -EINVAL;
221 }
222
223 static int crypto_init_ops(struct crypto_tfm *tfm)
224 {
225         switch (crypto_tfm_alg_type(tfm)) {
226         case CRYPTO_ALG_TYPE_CIPHER:
227                 return crypto_init_cipher_ops(tfm);
228                 
229         case CRYPTO_ALG_TYPE_DIGEST:
230                 return crypto_init_digest_ops(tfm);
231                 
232         case CRYPTO_ALG_TYPE_COMPRESS:
233                 return crypto_init_compress_ops(tfm);
234         
235         default:
236                 break;
237         }
238         
239         BUG();
240         return -EINVAL;
241 }
242
243 static void crypto_exit_ops(struct crypto_tfm *tfm)
244 {
245         switch (crypto_tfm_alg_type(tfm)) {
246         case CRYPTO_ALG_TYPE_CIPHER:
247                 crypto_exit_cipher_ops(tfm);
248                 break;
249                 
250         case CRYPTO_ALG_TYPE_DIGEST:
251                 crypto_exit_digest_ops(tfm);
252                 break;
253                 
254         case CRYPTO_ALG_TYPE_COMPRESS:
255                 crypto_exit_compress_ops(tfm);
256                 break;
257         
258         default:
259                 BUG();
260                 
261         }
262 }
263
264 static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
265 {
266         unsigned int len;
267
268         switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
269         default:
270                 BUG();
271
272         case CRYPTO_ALG_TYPE_CIPHER:
273                 len = crypto_cipher_ctxsize(alg, flags);
274                 break;
275                 
276         case CRYPTO_ALG_TYPE_DIGEST:
277                 len = crypto_digest_ctxsize(alg, flags);
278                 break;
279                 
280         case CRYPTO_ALG_TYPE_COMPRESS:
281                 len = crypto_compress_ctxsize(alg, flags);
282                 break;
283         }
284
285         return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
286 }
287
288 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
289 {
290         struct crypto_tfm *tfm = NULL;
291         struct crypto_alg *alg;
292         unsigned int tfm_size;
293
294         alg = crypto_alg_mod_lookup(name);
295         if (alg == NULL)
296                 goto out;
297
298         tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
299         tfm = kzalloc(tfm_size, GFP_KERNEL);
300         if (tfm == NULL)
301                 goto out_put;
302
303         tfm->__crt_alg = alg;
304         
305         if (crypto_init_flags(tfm, flags))
306                 goto out_free_tfm;
307                 
308         if (crypto_init_ops(tfm))
309                 goto out_free_tfm;
310
311         if (alg->cra_init && alg->cra_init(tfm))
312                 goto cra_init_failed;
313
314         goto out;
315
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;
325 }
326
327 void crypto_free_tfm(struct crypto_tfm *tfm)
328 {
329         struct crypto_alg *alg;
330         int size;
331
332         if (unlikely(!tfm))
333                 return;
334
335         alg = tfm->__crt_alg;
336         size = sizeof(*tfm) + alg->cra_ctxsize;
337
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);
344 }
345
346 int crypto_alg_available(const char *name, u32 flags)
347 {
348         int ret = 0;
349         struct crypto_alg *alg = crypto_alg_mod_lookup(name);
350         
351         if (alg) {
352                 crypto_mod_put(alg);
353                 ret = 1;
354         }
355         
356         return ret;
357 }
358
359 EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
360 EXPORT_SYMBOL_GPL(crypto_free_tfm);
361 EXPORT_SYMBOL_GPL(crypto_alg_available);