2 * Scatterlist Cryptographic API.
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>
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
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)
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>
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
);
32 BLOCKING_NOTIFIER_HEAD(crypto_chain
);
33 EXPORT_SYMBOL_GPL(crypto_chain
);
35 static inline struct crypto_alg
*crypto_alg_get(struct crypto_alg
*alg
)
37 atomic_inc(&alg
->cra_refcnt
);
41 static inline void crypto_alg_put(struct crypto_alg
*alg
)
43 if (atomic_dec_and_test(&alg
->cra_refcnt
) && alg
->cra_destroy
)
44 alg
->cra_destroy(alg
);
47 struct crypto_alg
*crypto_mod_get(struct crypto_alg
*alg
)
49 return try_module_get(alg
->cra_module
) ? crypto_alg_get(alg
) : NULL
;
51 EXPORT_SYMBOL_GPL(crypto_mod_get
);
53 void crypto_mod_put(struct crypto_alg
*alg
)
56 module_put(alg
->cra_module
);
58 EXPORT_SYMBOL_GPL(crypto_mod_put
);
60 struct crypto_alg
*__crypto_alg_lookup(const char *name
)
62 struct crypto_alg
*q
, *alg
= NULL
;
65 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
68 exact
= !strcmp(q
->cra_driver_name
, name
);
69 fuzzy
= !strcmp(q
->cra_name
, name
);
70 if (!exact
&& !(fuzzy
&& q
->cra_priority
> best
))
73 if (unlikely(!crypto_mod_get(q
)))
76 best
= q
->cra_priority
;
87 EXPORT_SYMBOL_GPL(__crypto_alg_lookup
);
89 static void crypto_larval_destroy(struct crypto_alg
*alg
)
91 struct crypto_larval
*larval
= (void *)alg
;
93 BUG_ON(!crypto_is_larval(alg
));
95 crypto_mod_put(larval
->adult
);
99 static struct crypto_alg
*crypto_larval_alloc(const char *name
)
101 struct crypto_alg
*alg
;
102 struct crypto_larval
*larval
;
104 larval
= kzalloc(sizeof(*larval
), GFP_KERNEL
);
108 larval
->alg
.cra_flags
= CRYPTO_ALG_LARVAL
;
109 larval
->alg
.cra_priority
= -1;
110 larval
->alg
.cra_destroy
= crypto_larval_destroy
;
112 atomic_set(&larval
->alg
.cra_refcnt
, 2);
113 strlcpy(larval
->alg
.cra_name
, name
, CRYPTO_MAX_ALG_NAME
);
114 init_completion(&larval
->completion
);
116 down_write(&crypto_alg_sem
);
117 alg
= __crypto_alg_lookup(name
);
120 list_add(&alg
->cra_list
, &crypto_alg_list
);
122 up_write(&crypto_alg_sem
);
124 if (alg
!= &larval
->alg
)
130 static void crypto_larval_kill(struct crypto_alg
*alg
)
132 struct crypto_larval
*larval
= (void *)alg
;
134 down_write(&crypto_alg_sem
);
135 list_del(&alg
->cra_list
);
136 up_write(&crypto_alg_sem
);
137 complete(&larval
->completion
);
141 static struct crypto_alg
*crypto_larval_wait(struct crypto_alg
*alg
)
143 struct crypto_larval
*larval
= (void *)alg
;
145 wait_for_completion_interruptible_timeout(&larval
->completion
, 60 * HZ
);
147 if (alg
&& !crypto_mod_get(alg
))
149 crypto_mod_put(&larval
->alg
);
154 static struct crypto_alg
*crypto_alg_lookup(const char *name
)
156 struct crypto_alg
*alg
;
161 down_read(&crypto_alg_sem
);
162 alg
= __crypto_alg_lookup(name
);
163 up_read(&crypto_alg_sem
);
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
)
172 struct crypto_alg
*alg
;
173 struct crypto_alg
*larval
;
176 alg
= try_then_request_module(crypto_alg_lookup(name
), name
);
178 return crypto_is_larval(alg
) ? crypto_larval_wait(alg
) : alg
;
180 larval
= crypto_larval_alloc(name
);
181 if (!larval
|| !crypto_is_larval(larval
))
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
);
190 if (ok
== NOTIFY_STOP
)
191 alg
= crypto_larval_wait(larval
);
193 crypto_mod_put(larval
);
196 crypto_larval_kill(larval
);
200 static int crypto_init_flags(struct crypto_tfm
*tfm
, u32 flags
)
202 tfm
->crt_flags
= flags
& CRYPTO_TFM_REQ_MASK
;
203 flags
&= ~CRYPTO_TFM_REQ_MASK
;
205 switch (crypto_tfm_alg_type(tfm
)) {
206 case CRYPTO_ALG_TYPE_CIPHER
:
207 return crypto_init_cipher_flags(tfm
, flags
);
209 case CRYPTO_ALG_TYPE_DIGEST
:
210 return crypto_init_digest_flags(tfm
, flags
);
212 case CRYPTO_ALG_TYPE_COMPRESS
:
213 return crypto_init_compress_flags(tfm
, flags
);
223 static int crypto_init_ops(struct crypto_tfm
*tfm
)
225 switch (crypto_tfm_alg_type(tfm
)) {
226 case CRYPTO_ALG_TYPE_CIPHER
:
227 return crypto_init_cipher_ops(tfm
);
229 case CRYPTO_ALG_TYPE_DIGEST
:
230 return crypto_init_digest_ops(tfm
);
232 case CRYPTO_ALG_TYPE_COMPRESS
:
233 return crypto_init_compress_ops(tfm
);
243 static void crypto_exit_ops(struct crypto_tfm
*tfm
)
245 switch (crypto_tfm_alg_type(tfm
)) {
246 case CRYPTO_ALG_TYPE_CIPHER
:
247 crypto_exit_cipher_ops(tfm
);
250 case CRYPTO_ALG_TYPE_DIGEST
:
251 crypto_exit_digest_ops(tfm
);
254 case CRYPTO_ALG_TYPE_COMPRESS
:
255 crypto_exit_compress_ops(tfm
);
264 static unsigned int crypto_ctxsize(struct crypto_alg
*alg
, int flags
)
268 switch (alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
) {
272 case CRYPTO_ALG_TYPE_CIPHER
:
273 len
= crypto_cipher_ctxsize(alg
, flags
);
276 case CRYPTO_ALG_TYPE_DIGEST
:
277 len
= crypto_digest_ctxsize(alg
, flags
);
280 case CRYPTO_ALG_TYPE_COMPRESS
:
281 len
= crypto_compress_ctxsize(alg
, flags
);
285 return len
+ (alg
->cra_alignmask
& ~(crypto_tfm_ctx_alignment() - 1));
288 struct crypto_tfm
*crypto_alloc_tfm(const char *name
, u32 flags
)
290 struct crypto_tfm
*tfm
= NULL
;
291 struct crypto_alg
*alg
;
292 unsigned int tfm_size
;
294 alg
= crypto_alg_mod_lookup(name
);
298 tfm_size
= sizeof(*tfm
) + crypto_ctxsize(alg
, flags
);
299 tfm
= kzalloc(tfm_size
, GFP_KERNEL
);
303 tfm
->__crt_alg
= alg
;
305 if (crypto_init_flags(tfm
, flags
))
308 if (crypto_init_ops(tfm
))
311 if (alg
->cra_init
&& alg
->cra_init(tfm
))
312 goto cra_init_failed
;
317 crypto_exit_ops(tfm
);
327 void crypto_free_tfm(struct crypto_tfm
*tfm
)
329 struct crypto_alg
*alg
;
335 alg
= tfm
->__crt_alg
;
336 size
= sizeof(*tfm
) + alg
->cra_ctxsize
;
340 crypto_exit_ops(tfm
);
342 memset(tfm
, 0, size
);
346 int crypto_alg_available(const char *name
, u32 flags
)
349 struct crypto_alg
*alg
= crypto_alg_mod_lookup(name
);
359 EXPORT_SYMBOL_GPL(crypto_alloc_tfm
);
360 EXPORT_SYMBOL_GPL(crypto_free_tfm
);
361 EXPORT_SYMBOL_GPL(crypto_alg_available
);