blob: 5a0d6a17cfd70ae0b47bb826981d496cceb4e51f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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)
Herbert Xu5cb14542005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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 */
Jesper Juhla61cc442005-07-06 13:54:31 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/errno.h>
Herbert Xu5cb14542005-11-05 16:58:14 +110019#include <linux/kernel.h>
Adrian Bunk176c3652005-07-06 13:53:09 -070020#include <linux/kmod.h>
Herbert Xu28259822006-08-06 21:23:26 +100021#include <linux/param.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/slab.h>
Herbert Xu5cb14542005-11-05 16:58:14 +110023#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "internal.h"
25
26LIST_HEAD(crypto_alg_list);
Herbert Xucce9e062006-08-21 21:08:13 +100027EXPORT_SYMBOL_GPL(crypto_alg_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028DECLARE_RWSEM(crypto_alg_sem);
Herbert Xucce9e062006-08-21 21:08:13 +100029EXPORT_SYMBOL_GPL(crypto_alg_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Herbert Xu28259822006-08-06 21:23:26 +100031BLOCKING_NOTIFIER_HEAD(crypto_chain);
32EXPORT_SYMBOL_GPL(crypto_chain);
33
Herbert Xu6521f302006-08-06 20:28:44 +100034static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Herbert Xu6521f302006-08-06 20:28:44 +100036 atomic_inc(&alg->cra_refcnt);
37 return alg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
Herbert Xu6521f302006-08-06 20:28:44 +100040static inline void crypto_alg_put(struct crypto_alg *alg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Herbert Xu6521f302006-08-06 20:28:44 +100042 if (atomic_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
43 alg->cra_destroy(alg);
44}
45
Herbert Xu28259822006-08-06 21:23:26 +100046struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
Herbert Xu6521f302006-08-06 20:28:44 +100047{
48 return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
49}
Herbert Xu28259822006-08-06 21:23:26 +100050EXPORT_SYMBOL_GPL(crypto_mod_get);
Herbert Xu6521f302006-08-06 20:28:44 +100051
Herbert Xu28259822006-08-06 21:23:26 +100052void crypto_mod_put(struct crypto_alg *alg)
Herbert Xu6521f302006-08-06 20:28:44 +100053{
54 crypto_alg_put(alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 module_put(alg->cra_module);
56}
Herbert Xu28259822006-08-06 21:23:26 +100057EXPORT_SYMBOL_GPL(crypto_mod_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Herbert Xu28259822006-08-06 21:23:26 +100059struct crypto_alg *__crypto_alg_lookup(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 struct crypto_alg *q, *alg = NULL;
Herbert Xu28259822006-08-06 21:23:26 +100062 int best = -2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xu5cb14542005-11-05 16:58:14 +110065 int exact, fuzzy;
66
67 exact = !strcmp(q->cra_driver_name, name);
68 fuzzy = !strcmp(q->cra_name, name);
69 if (!exact && !(fuzzy && q->cra_priority > best))
70 continue;
71
Herbert Xu72fa4912006-05-28 09:05:24 +100072 if (unlikely(!crypto_mod_get(q)))
Herbert Xu5cb14542005-11-05 16:58:14 +110073 continue;
74
75 best = q->cra_priority;
76 if (alg)
Herbert Xu72fa4912006-05-28 09:05:24 +100077 crypto_mod_put(alg);
Herbert Xu5cb14542005-11-05 16:58:14 +110078 alg = q;
79
80 if (exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Herbert Xu28259822006-08-06 21:23:26 +100083
84 return alg;
85}
86EXPORT_SYMBOL_GPL(__crypto_alg_lookup);
87
88static void crypto_larval_destroy(struct crypto_alg *alg)
89{
90 struct crypto_larval *larval = (void *)alg;
91
92 BUG_ON(!crypto_is_larval(alg));
93 if (larval->adult)
94 crypto_mod_put(larval->adult);
95 kfree(larval);
96}
97
98static struct crypto_alg *crypto_larval_alloc(const char *name)
99{
100 struct crypto_alg *alg;
101 struct crypto_larval *larval;
102
103 larval = kzalloc(sizeof(*larval), GFP_KERNEL);
104 if (!larval)
105 return NULL;
106
107 larval->alg.cra_flags = CRYPTO_ALG_LARVAL;
108 larval->alg.cra_priority = -1;
109 larval->alg.cra_destroy = crypto_larval_destroy;
110
111 atomic_set(&larval->alg.cra_refcnt, 2);
112 strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
113 init_completion(&larval->completion);
114
115 down_write(&crypto_alg_sem);
116 alg = __crypto_alg_lookup(name);
117 if (!alg) {
118 alg = &larval->alg;
119 list_add(&alg->cra_list, &crypto_alg_list);
120 }
121 up_write(&crypto_alg_sem);
122
123 if (alg != &larval->alg)
124 kfree(larval);
125
126 return alg;
127}
128
129static void crypto_larval_kill(struct crypto_alg *alg)
130{
131 struct crypto_larval *larval = (void *)alg;
132
133 down_write(&crypto_alg_sem);
134 list_del(&alg->cra_list);
135 up_write(&crypto_alg_sem);
136 complete(&larval->completion);
137 crypto_alg_put(alg);
138}
139
140static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
141{
142 struct crypto_larval *larval = (void *)alg;
143
144 wait_for_completion_interruptible_timeout(&larval->completion, 60 * HZ);
145 alg = larval->adult;
146 if (alg && !crypto_mod_get(alg))
147 alg = NULL;
148 crypto_mod_put(&larval->alg);
149
150 return alg;
151}
152
153static struct crypto_alg *crypto_alg_lookup(const char *name)
154{
155 struct crypto_alg *alg;
156
157 if (!name)
158 return NULL;
159
160 down_read(&crypto_alg_sem);
161 alg = __crypto_alg_lookup(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 up_read(&crypto_alg_sem);
Herbert Xu28259822006-08-06 21:23:26 +1000163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return alg;
165}
166
Adrian Bunk176c3652005-07-06 13:53:09 -0700167/* A far more intelligent version of this is planned. For now, just
168 * try an exact match on the name of the algorithm. */
Herbert Xu28259822006-08-06 21:23:26 +1000169static struct crypto_alg *crypto_alg_mod_lookup(const char *name)
Adrian Bunk176c3652005-07-06 13:53:09 -0700170{
Herbert Xu28259822006-08-06 21:23:26 +1000171 struct crypto_alg *alg;
172 struct crypto_alg *larval;
173
174 alg = try_then_request_module(crypto_alg_lookup(name), name);
175 if (alg)
176 return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
177
178 larval = crypto_larval_alloc(name);
179 if (!larval || !crypto_is_larval(larval))
180 return larval;
181
182 if (crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval) == NOTIFY_STOP)
183 alg = crypto_larval_wait(larval);
184 else {
185 crypto_mod_put(larval);
186 alg = NULL;
187 }
188 crypto_larval_kill(larval);
189 return alg;
Adrian Bunk176c3652005-07-06 13:53:09 -0700190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
193{
Herbert Xu64baf3c2005-09-01 17:43:05 -0700194 tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
195 flags &= ~CRYPTO_TFM_REQ_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 switch (crypto_tfm_alg_type(tfm)) {
198 case CRYPTO_ALG_TYPE_CIPHER:
199 return crypto_init_cipher_flags(tfm, flags);
200
201 case CRYPTO_ALG_TYPE_DIGEST:
202 return crypto_init_digest_flags(tfm, flags);
203
204 case CRYPTO_ALG_TYPE_COMPRESS:
205 return crypto_init_compress_flags(tfm, flags);
206
207 default:
208 break;
209 }
210
211 BUG();
212 return -EINVAL;
213}
214
215static int crypto_init_ops(struct crypto_tfm *tfm)
216{
217 switch (crypto_tfm_alg_type(tfm)) {
218 case CRYPTO_ALG_TYPE_CIPHER:
219 return crypto_init_cipher_ops(tfm);
220
221 case CRYPTO_ALG_TYPE_DIGEST:
222 return crypto_init_digest_ops(tfm);
223
224 case CRYPTO_ALG_TYPE_COMPRESS:
225 return crypto_init_compress_ops(tfm);
226
227 default:
228 break;
229 }
230
231 BUG();
232 return -EINVAL;
233}
234
235static void crypto_exit_ops(struct crypto_tfm *tfm)
236{
237 switch (crypto_tfm_alg_type(tfm)) {
238 case CRYPTO_ALG_TYPE_CIPHER:
239 crypto_exit_cipher_ops(tfm);
240 break;
241
242 case CRYPTO_ALG_TYPE_DIGEST:
243 crypto_exit_digest_ops(tfm);
244 break;
245
246 case CRYPTO_ALG_TYPE_COMPRESS:
247 crypto_exit_compress_ops(tfm);
248 break;
249
250 default:
251 BUG();
252
253 }
254}
255
Herbert Xufbdae9f2005-07-06 13:53:29 -0700256static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
257{
258 unsigned int len;
259
260 switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
261 default:
262 BUG();
263
264 case CRYPTO_ALG_TYPE_CIPHER:
265 len = crypto_cipher_ctxsize(alg, flags);
266 break;
267
268 case CRYPTO_ALG_TYPE_DIGEST:
269 len = crypto_digest_ctxsize(alg, flags);
270 break;
271
272 case CRYPTO_ALG_TYPE_COMPRESS:
273 len = crypto_compress_ctxsize(alg, flags);
274 break;
275 }
276
Herbert Xuf10b7892006-01-25 22:34:01 +1100277 return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
Herbert Xufbdae9f2005-07-06 13:53:29 -0700278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
281{
282 struct crypto_tfm *tfm = NULL;
283 struct crypto_alg *alg;
Herbert Xufbdae9f2005-07-06 13:53:29 -0700284 unsigned int tfm_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 alg = crypto_alg_mod_lookup(name);
287 if (alg == NULL)
288 goto out;
Herbert Xufbdae9f2005-07-06 13:53:29 -0700289
290 tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
Eric Sesterhennbbeb5632006-03-06 21:42:07 +1100291 tfm = kzalloc(tfm_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (tfm == NULL)
293 goto out_put;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 tfm->__crt_alg = alg;
296
297 if (crypto_init_flags(tfm, flags))
298 goto out_free_tfm;
299
Herbert Xuc7fc0592006-05-24 13:02:26 +1000300 if (crypto_init_ops(tfm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goto out_free_tfm;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000302
303 if (alg->cra_init && alg->cra_init(tfm))
304 goto cra_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 goto out;
307
Herbert Xuc7fc0592006-05-24 13:02:26 +1000308cra_init_failed:
309 crypto_exit_ops(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310out_free_tfm:
311 kfree(tfm);
312 tfm = NULL;
313out_put:
Herbert Xu72fa4912006-05-28 09:05:24 +1000314 crypto_mod_put(alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315out:
316 return tfm;
317}
318
319void crypto_free_tfm(struct crypto_tfm *tfm)
320{
Jesper Juhla61cc442005-07-06 13:54:31 -0700321 struct crypto_alg *alg;
322 int size;
323
324 if (unlikely(!tfm))
325 return;
326
327 alg = tfm->__crt_alg;
328 size = sizeof(*tfm) + alg->cra_ctxsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Herbert Xuc7fc0592006-05-24 13:02:26 +1000330 if (alg->cra_exit)
331 alg->cra_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 crypto_exit_ops(tfm);
Herbert Xu72fa4912006-05-28 09:05:24 +1000333 crypto_mod_put(alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 memset(tfm, 0, size);
335 kfree(tfm);
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338int crypto_alg_available(const char *name, u32 flags)
339{
340 int ret = 0;
341 struct crypto_alg *alg = crypto_alg_mod_lookup(name);
342
343 if (alg) {
Herbert Xu72fa4912006-05-28 09:05:24 +1000344 crypto_mod_put(alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 ret = 1;
346 }
347
348 return ret;
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
352EXPORT_SYMBOL_GPL(crypto_free_tfm);
353EXPORT_SYMBOL_GPL(crypto_alg_available);