blob: 67cd6f87b74a5289d2ac7b3be0b9da2dc322c6a9 [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 Xu5cb1454b2005-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 Xu5cb1454b2005-11-05 16:58:14 +110019#include <linux/kernel.h>
Adrian Bunk176c3652005-07-06 13:53:09 -070020#include <linux/kmod.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100021#include <linux/module.h>
Herbert Xu28259822006-08-06 21:23:26 +100022#include <linux/param.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
Herbert Xu5cb1454b2005-11-05 16:58:14 +110024#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "internal.h"
26
27LIST_HEAD(crypto_alg_list);
Herbert Xucce9e062006-08-21 21:08:13 +100028EXPORT_SYMBOL_GPL(crypto_alg_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029DECLARE_RWSEM(crypto_alg_sem);
Herbert Xucce9e062006-08-21 21:08:13 +100030EXPORT_SYMBOL_GPL(crypto_alg_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Herbert Xu28259822006-08-06 21:23:26 +100032BLOCKING_NOTIFIER_HEAD(crypto_chain);
33EXPORT_SYMBOL_GPL(crypto_chain);
34
Herbert Xu6521f302006-08-06 20:28:44 +100035static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Herbert Xu6521f302006-08-06 20:28:44 +100037 atomic_inc(&alg->cra_refcnt);
38 return alg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Herbert Xu6521f302006-08-06 20:28:44 +100041static inline void crypto_alg_put(struct crypto_alg *alg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Herbert Xu6521f302006-08-06 20:28:44 +100043 if (atomic_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
44 alg->cra_destroy(alg);
45}
46
Herbert Xu28259822006-08-06 21:23:26 +100047struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
Herbert Xu6521f302006-08-06 20:28:44 +100048{
49 return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
50}
Herbert Xu28259822006-08-06 21:23:26 +100051EXPORT_SYMBOL_GPL(crypto_mod_get);
Herbert Xu6521f302006-08-06 20:28:44 +100052
Herbert Xu28259822006-08-06 21:23:26 +100053void crypto_mod_put(struct crypto_alg *alg)
Herbert Xu6521f302006-08-06 20:28:44 +100054{
55 crypto_alg_put(alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 module_put(alg->cra_module);
57}
Herbert Xu28259822006-08-06 21:23:26 +100058EXPORT_SYMBOL_GPL(crypto_mod_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Herbert Xu28259822006-08-06 21:23:26 +100060struct crypto_alg *__crypto_alg_lookup(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 struct crypto_alg *q, *alg = NULL;
Herbert Xu28259822006-08-06 21:23:26 +100063 int best = -2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xu5cb1454b2005-11-05 16:58:14 +110066 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
Herbert Xu72fa4912006-05-28 09:05:24 +100073 if (unlikely(!crypto_mod_get(q)))
Herbert Xu5cb1454b2005-11-05 16:58:14 +110074 continue;
75
76 best = q->cra_priority;
77 if (alg)
Herbert Xu72fa4912006-05-28 09:05:24 +100078 crypto_mod_put(alg);
Herbert Xu5cb1454b2005-11-05 16:58:14 +110079 alg = q;
80
81 if (exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
Herbert Xu28259822006-08-06 21:23:26 +100084
85 return alg;
86}
87EXPORT_SYMBOL_GPL(__crypto_alg_lookup);
88
89static 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
99static 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
130static 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
141static 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
154static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 up_read(&crypto_alg_sem);
Herbert Xu28259822006-08-06 21:23:26 +1000164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return alg;
166}
167
Adrian Bunk176c3652005-07-06 13:53:09 -0700168/* A far more intelligent version of this is planned. For now, just
169 * try an exact match on the name of the algorithm. */
Herbert Xu28259822006-08-06 21:23:26 +1000170static struct crypto_alg *crypto_alg_mod_lookup(const char *name)
Adrian Bunk176c3652005-07-06 13:53:09 -0700171{
Herbert Xu28259822006-08-06 21:23:26 +1000172 struct crypto_alg *alg;
173 struct crypto_alg *larval;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000174 int ok;
Herbert Xu28259822006-08-06 21:23:26 +1000175
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
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000184 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)
Herbert Xu28259822006-08-06 21:23:26 +1000191 alg = crypto_larval_wait(larval);
192 else {
193 crypto_mod_put(larval);
194 alg = NULL;
195 }
196 crypto_larval_kill(larval);
197 return alg;
Adrian Bunk176c3652005-07-06 13:53:09 -0700198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
201{
Herbert Xu64baf3c2005-09-01 17:43:05 -0700202 tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
203 flags &= ~CRYPTO_TFM_REQ_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
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
223static 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
243static 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
Herbert Xufbdae9f2005-07-06 13:53:29 -0700264static 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
Herbert Xuf10b7892006-01-25 22:34:01 +1100285 return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
Herbert Xufbdae9f2005-07-06 13:53:29 -0700286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
289{
290 struct crypto_tfm *tfm = NULL;
291 struct crypto_alg *alg;
Herbert Xufbdae9f2005-07-06 13:53:29 -0700292 unsigned int tfm_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 alg = crypto_alg_mod_lookup(name);
295 if (alg == NULL)
296 goto out;
Herbert Xufbdae9f2005-07-06 13:53:29 -0700297
298 tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
Eric Sesterhennbbeb563f2006-03-06 21:42:07 +1100299 tfm = kzalloc(tfm_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (tfm == NULL)
301 goto out_put;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 tfm->__crt_alg = alg;
304
305 if (crypto_init_flags(tfm, flags))
306 goto out_free_tfm;
307
Herbert Xuc7fc0592006-05-24 13:02:26 +1000308 if (crypto_init_ops(tfm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto out_free_tfm;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000310
311 if (alg->cra_init && alg->cra_init(tfm))
312 goto cra_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 goto out;
315
Herbert Xuc7fc0592006-05-24 13:02:26 +1000316cra_init_failed:
317 crypto_exit_ops(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318out_free_tfm:
319 kfree(tfm);
320 tfm = NULL;
321out_put:
Herbert Xu72fa4912006-05-28 09:05:24 +1000322 crypto_mod_put(alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323out:
324 return tfm;
325}
326
327void crypto_free_tfm(struct crypto_tfm *tfm)
328{
Jesper Juhla61cc442005-07-06 13:54:31 -0700329 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Herbert Xuc7fc0592006-05-24 13:02:26 +1000338 if (alg->cra_exit)
339 alg->cra_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 crypto_exit_ops(tfm);
Herbert Xu72fa4912006-05-28 09:05:24 +1000341 crypto_mod_put(alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 memset(tfm, 0, size);
343 kfree(tfm);
344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346int 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) {
Herbert Xu72fa4912006-05-28 09:05:24 +1000352 crypto_mod_put(alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 ret = 1;
354 }
355
356 return ret;
357}
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
360EXPORT_SYMBOL_GPL(crypto_free_tfm);
361EXPORT_SYMBOL_GPL(crypto_alg_available);