[CRYPTO] api: Allow algorithm lookup by type
[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, u32 type, u32 mask)
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                 if ((q->cra_flags ^ type) & mask)
69                         continue;
70
71                 if (crypto_is_larval(q) &&
72                     ((struct crypto_larval *)q)->mask != mask)
73                         continue;
74
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;
79
80                 if (unlikely(!crypto_mod_get(q)))
81                         continue;
82
83                 best = q->cra_priority;
84                 if (alg)
85                         crypto_mod_put(alg);
86                 alg = q;
87
88                 if (exact)
89                         break;
90         }
91
92         return alg;
93 }
94 EXPORT_SYMBOL_GPL(__crypto_alg_lookup);
95
96 static void crypto_larval_destroy(struct crypto_alg *alg)
97 {
98         struct crypto_larval *larval = (void *)alg;
99
100         BUG_ON(!crypto_is_larval(alg));
101         if (larval->adult)
102                 crypto_mod_put(larval->adult);
103         kfree(larval);
104 }
105
106 static struct crypto_alg *crypto_larval_alloc(const char *name, u32 type,
107                                               u32 mask)
108 {
109         struct crypto_alg *alg;
110         struct crypto_larval *larval;
111
112         larval = kzalloc(sizeof(*larval), GFP_KERNEL);
113         if (!larval)
114                 return NULL;
115
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;
120
121         atomic_set(&larval->alg.cra_refcnt, 2);
122         strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
123         init_completion(&larval->completion);
124
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);
130         }
131         up_write(&crypto_alg_sem);
132
133         if (alg != &larval->alg)
134                 kfree(larval);
135
136         return alg;
137 }
138
139 static void crypto_larval_kill(struct crypto_alg *alg)
140 {
141         struct crypto_larval *larval = (void *)alg;
142
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);
148 }
149
150 static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
151 {
152         struct crypto_larval *larval = (void *)alg;
153
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);
159
160         return alg;
161 }
162
163 static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
164                                             u32 mask)
165 {
166         struct crypto_alg *alg;
167
168         if (!name)
169                 return NULL;
170
171         down_read(&crypto_alg_sem);
172         alg = __crypto_alg_lookup(name, type, mask);
173         up_read(&crypto_alg_sem);
174
175         return alg;
176 }
177
178 struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
179 {
180         struct crypto_alg *alg;
181         struct crypto_alg *larval;
182         int ok;
183
184         mask &= ~CRYPTO_ALG_LARVAL;
185         type &= mask;
186
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;
191
192         larval = crypto_larval_alloc(name, type, mask);
193         if (!larval || !crypto_is_larval(larval))
194                 return larval;
195
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);
200         }
201
202         if (ok == NOTIFY_STOP)
203                 alg = crypto_larval_wait(larval);
204         else {
205                 crypto_mod_put(larval);
206                 alg = NULL;
207         }
208         crypto_larval_kill(larval);
209         return alg;
210 }
211 EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
212
213 static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
214 {
215         tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
216         flags &= ~CRYPTO_TFM_REQ_MASK;
217         
218         switch (crypto_tfm_alg_type(tfm)) {
219         case CRYPTO_ALG_TYPE_CIPHER:
220                 return crypto_init_cipher_flags(tfm, flags);
221                 
222         case CRYPTO_ALG_TYPE_DIGEST:
223                 return crypto_init_digest_flags(tfm, flags);
224                 
225         case CRYPTO_ALG_TYPE_COMPRESS:
226                 return crypto_init_compress_flags(tfm, flags);
227         
228         default:
229                 break;
230         }
231         
232         BUG();
233         return -EINVAL;
234 }
235
236 static int crypto_init_ops(struct crypto_tfm *tfm)
237 {
238         switch (crypto_tfm_alg_type(tfm)) {
239         case CRYPTO_ALG_TYPE_CIPHER:
240                 return crypto_init_cipher_ops(tfm);
241                 
242         case CRYPTO_ALG_TYPE_DIGEST:
243                 return crypto_init_digest_ops(tfm);
244                 
245         case CRYPTO_ALG_TYPE_COMPRESS:
246                 return crypto_init_compress_ops(tfm);
247         
248         default:
249                 break;
250         }
251         
252         BUG();
253         return -EINVAL;
254 }
255
256 static void crypto_exit_ops(struct crypto_tfm *tfm)
257 {
258         switch (crypto_tfm_alg_type(tfm)) {
259         case CRYPTO_ALG_TYPE_CIPHER:
260                 crypto_exit_cipher_ops(tfm);
261                 break;
262                 
263         case CRYPTO_ALG_TYPE_DIGEST:
264                 crypto_exit_digest_ops(tfm);
265                 break;
266                 
267         case CRYPTO_ALG_TYPE_COMPRESS:
268                 crypto_exit_compress_ops(tfm);
269                 break;
270         
271         default:
272                 BUG();
273                 
274         }
275 }
276
277 static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
278 {
279         unsigned int len;
280
281         switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
282         default:
283                 BUG();
284
285         case CRYPTO_ALG_TYPE_CIPHER:
286                 len = crypto_cipher_ctxsize(alg, flags);
287                 break;
288                 
289         case CRYPTO_ALG_TYPE_DIGEST:
290                 len = crypto_digest_ctxsize(alg, flags);
291                 break;
292                 
293         case CRYPTO_ALG_TYPE_COMPRESS:
294                 len = crypto_compress_ctxsize(alg, flags);
295                 break;
296         }
297
298         return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
299 }
300
301 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
302 {
303         struct crypto_tfm *tfm = NULL;
304         struct crypto_alg *alg;
305         unsigned int tfm_size;
306
307         alg = crypto_alg_mod_lookup(name, 0, 0);
308         if (alg == NULL)
309                 goto out;
310
311         tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
312         tfm = kzalloc(tfm_size, GFP_KERNEL);
313         if (tfm == NULL)
314                 goto out_put;
315
316         tfm->__crt_alg = alg;
317         
318         if (crypto_init_flags(tfm, flags))
319                 goto out_free_tfm;
320                 
321         if (crypto_init_ops(tfm))
322                 goto out_free_tfm;
323
324         if (alg->cra_init && alg->cra_init(tfm))
325                 goto cra_init_failed;
326
327         goto out;
328
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;
338 }
339
340 void crypto_free_tfm(struct crypto_tfm *tfm)
341 {
342         struct crypto_alg *alg;
343         int size;
344
345         if (unlikely(!tfm))
346                 return;
347
348         alg = tfm->__crt_alg;
349         size = sizeof(*tfm) + alg->cra_ctxsize;
350
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);
357 }
358
359 int crypto_alg_available(const char *name, u32 flags)
360 {
361         int ret = 0;
362         struct crypto_alg *alg = crypto_alg_mod_lookup(name, 0, 0);
363         
364         if (alg) {
365                 crypto_mod_put(alg);
366                 ret = 1;
367         }
368         
369         return ret;
370 }
371
372 EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
373 EXPORT_SYMBOL_GPL(crypto_free_tfm);
374 EXPORT_SYMBOL_GPL(crypto_alg_available);