blob: 8145310d7985dcfde689bf04dfe949d2ee6908f6 [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
18#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
20#include <linux/crypto.h>
21#include <linux/errno.h>
Herbert Xu5cb14542005-11-05 16:58:14 +110022#include <linux/kernel.h>
Adrian Bunk176c3652005-07-06 13:53:09 -070023#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/rwsem.h>
25#include <linux/slab.h>
Herbert Xu5cb14542005-11-05 16:58:14 +110026#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "internal.h"
28
29LIST_HEAD(crypto_alg_list);
30DECLARE_RWSEM(crypto_alg_sem);
31
32static inline int crypto_alg_get(struct crypto_alg *alg)
33{
34 return try_module_get(alg->cra_module);
35}
36
37static inline void crypto_alg_put(struct crypto_alg *alg)
38{
39 module_put(alg->cra_module);
40}
41
Adrian Bunk176c3652005-07-06 13:53:09 -070042static struct crypto_alg *crypto_alg_lookup(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 struct crypto_alg *q, *alg = NULL;
Herbert Xu5cb14542005-11-05 16:58:14 +110045 int best = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 if (!name)
48 return NULL;
49
50 down_read(&crypto_alg_sem);
51
52 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xu5cb14542005-11-05 16:58:14 +110053 int exact, fuzzy;
54
55 exact = !strcmp(q->cra_driver_name, name);
56 fuzzy = !strcmp(q->cra_name, name);
57 if (!exact && !(fuzzy && q->cra_priority > best))
58 continue;
59
60 if (unlikely(!crypto_alg_get(q)))
61 continue;
62
63 best = q->cra_priority;
64 if (alg)
65 crypto_alg_put(alg);
66 alg = q;
67
68 if (exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71
72 up_read(&crypto_alg_sem);
73 return alg;
74}
75
Adrian Bunk176c3652005-07-06 13:53:09 -070076/* A far more intelligent version of this is planned. For now, just
77 * try an exact match on the name of the algorithm. */
78static inline struct crypto_alg *crypto_alg_mod_lookup(const char *name)
79{
80 return try_then_request_module(crypto_alg_lookup(name), name);
81}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
84{
Herbert Xu64baf3c2005-09-01 17:43:05 -070085 tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
86 flags &= ~CRYPTO_TFM_REQ_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 switch (crypto_tfm_alg_type(tfm)) {
89 case CRYPTO_ALG_TYPE_CIPHER:
90 return crypto_init_cipher_flags(tfm, flags);
91
92 case CRYPTO_ALG_TYPE_DIGEST:
93 return crypto_init_digest_flags(tfm, flags);
94
95 case CRYPTO_ALG_TYPE_COMPRESS:
96 return crypto_init_compress_flags(tfm, flags);
97
98 default:
99 break;
100 }
101
102 BUG();
103 return -EINVAL;
104}
105
106static int crypto_init_ops(struct crypto_tfm *tfm)
107{
108 switch (crypto_tfm_alg_type(tfm)) {
109 case CRYPTO_ALG_TYPE_CIPHER:
110 return crypto_init_cipher_ops(tfm);
111
112 case CRYPTO_ALG_TYPE_DIGEST:
113 return crypto_init_digest_ops(tfm);
114
115 case CRYPTO_ALG_TYPE_COMPRESS:
116 return crypto_init_compress_ops(tfm);
117
118 default:
119 break;
120 }
121
122 BUG();
123 return -EINVAL;
124}
125
126static void crypto_exit_ops(struct crypto_tfm *tfm)
127{
128 switch (crypto_tfm_alg_type(tfm)) {
129 case CRYPTO_ALG_TYPE_CIPHER:
130 crypto_exit_cipher_ops(tfm);
131 break;
132
133 case CRYPTO_ALG_TYPE_DIGEST:
134 crypto_exit_digest_ops(tfm);
135 break;
136
137 case CRYPTO_ALG_TYPE_COMPRESS:
138 crypto_exit_compress_ops(tfm);
139 break;
140
141 default:
142 BUG();
143
144 }
145}
146
Herbert Xufbdae9f2005-07-06 13:53:29 -0700147static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
148{
149 unsigned int len;
150
151 switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
152 default:
153 BUG();
154
155 case CRYPTO_ALG_TYPE_CIPHER:
156 len = crypto_cipher_ctxsize(alg, flags);
157 break;
158
159 case CRYPTO_ALG_TYPE_DIGEST:
160 len = crypto_digest_ctxsize(alg, flags);
161 break;
162
163 case CRYPTO_ALG_TYPE_COMPRESS:
164 len = crypto_compress_ctxsize(alg, flags);
165 break;
166 }
167
Herbert Xuf10b7892006-01-25 22:34:01 +1100168 return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
Herbert Xufbdae9f2005-07-06 13:53:29 -0700169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
172{
173 struct crypto_tfm *tfm = NULL;
174 struct crypto_alg *alg;
Herbert Xufbdae9f2005-07-06 13:53:29 -0700175 unsigned int tfm_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 alg = crypto_alg_mod_lookup(name);
178 if (alg == NULL)
179 goto out;
Herbert Xufbdae9f2005-07-06 13:53:29 -0700180
181 tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
Eric Sesterhennbbeb5632006-03-06 21:42:07 +1100182 tfm = kzalloc(tfm_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (tfm == NULL)
184 goto out_put;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 tfm->__crt_alg = alg;
187
188 if (crypto_init_flags(tfm, flags))
189 goto out_free_tfm;
190
Herbert Xuc7fc0592006-05-24 13:02:26 +1000191 if (crypto_init_ops(tfm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 goto out_free_tfm;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000193
194 if (alg->cra_init && alg->cra_init(tfm))
195 goto cra_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 goto out;
198
Herbert Xuc7fc0592006-05-24 13:02:26 +1000199cra_init_failed:
200 crypto_exit_ops(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201out_free_tfm:
202 kfree(tfm);
203 tfm = NULL;
204out_put:
205 crypto_alg_put(alg);
206out:
207 return tfm;
208}
209
210void crypto_free_tfm(struct crypto_tfm *tfm)
211{
Jesper Juhla61cc442005-07-06 13:54:31 -0700212 struct crypto_alg *alg;
213 int size;
214
215 if (unlikely(!tfm))
216 return;
217
218 alg = tfm->__crt_alg;
219 size = sizeof(*tfm) + alg->cra_ctxsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Herbert Xuc7fc0592006-05-24 13:02:26 +1000221 if (alg->cra_exit)
222 alg->cra_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 crypto_exit_ops(tfm);
224 crypto_alg_put(alg);
225 memset(tfm, 0, size);
226 kfree(tfm);
227}
228
Herbert Xu5cb14542005-11-05 16:58:14 +1100229static inline int crypto_set_driver_name(struct crypto_alg *alg)
230{
231 static const char suffix[] = "-generic";
232 char *driver_name = (char *)alg->cra_driver_name;
233 int len;
234
235 if (*driver_name)
236 return 0;
237
238 len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
239 if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
240 return -ENAMETOOLONG;
241
242 memcpy(driver_name + len, suffix, sizeof(suffix));
243 return 0;
244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246int crypto_register_alg(struct crypto_alg *alg)
247{
Herbert Xu5cb14542005-11-05 16:58:14 +1100248 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct crypto_alg *q;
Herbert Xu95477372005-07-06 13:52:09 -0700250
251 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
252 return -EINVAL;
253
Herbert Xu1b40efd2005-10-03 15:15:36 +1000254 if (alg->cra_alignmask & alg->cra_blocksize)
255 return -EINVAL;
256
Herbert Xu73025332006-01-07 16:24:15 +1100257 if (alg->cra_blocksize > PAGE_SIZE / 8)
Herbert Xu95477372005-07-06 13:52:09 -0700258 return -EINVAL;
Herbert Xu5cb14542005-11-05 16:58:14 +1100259
260 if (alg->cra_priority < 0)
261 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Herbert Xu5cb14542005-11-05 16:58:14 +1100263 ret = crypto_set_driver_name(alg);
264 if (unlikely(ret))
265 return ret;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 down_write(&crypto_alg_sem);
268
269 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xu5cb14542005-11-05 16:58:14 +1100270 if (!strcmp(q->cra_driver_name, alg->cra_driver_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 ret = -EEXIST;
272 goto out;
273 }
274 }
275
276 list_add_tail(&alg->cra_list, &crypto_alg_list);
277out:
278 up_write(&crypto_alg_sem);
279 return ret;
280}
281
282int crypto_unregister_alg(struct crypto_alg *alg)
283{
284 int ret = -ENOENT;
285 struct crypto_alg *q;
286
287 BUG_ON(!alg->cra_module);
288
289 down_write(&crypto_alg_sem);
290 list_for_each_entry(q, &crypto_alg_list, cra_list) {
291 if (alg == q) {
292 list_del(&alg->cra_list);
293 ret = 0;
294 goto out;
295 }
296 }
297out:
298 up_write(&crypto_alg_sem);
299 return ret;
300}
301
302int crypto_alg_available(const char *name, u32 flags)
303{
304 int ret = 0;
305 struct crypto_alg *alg = crypto_alg_mod_lookup(name);
306
307 if (alg) {
308 crypto_alg_put(alg);
309 ret = 1;
310 }
311
312 return ret;
313}
314
315static int __init init_crypto(void)
316{
317 printk(KERN_INFO "Initializing Cryptographic API\n");
318 crypto_init_proc();
319 return 0;
320}
321
322__initcall(init_crypto);
323
324EXPORT_SYMBOL_GPL(crypto_register_alg);
325EXPORT_SYMBOL_GPL(crypto_unregister_alg);
326EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
327EXPORT_SYMBOL_GPL(crypto_free_tfm);
328EXPORT_SYMBOL_GPL(crypto_alg_available);