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
, u32 type
, u32 mask
)
62 struct crypto_alg
*q
, *alg
= NULL
;
65 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
68 if ((q
->cra_flags
^ type
) & mask
)
71 if (crypto_is_larval(q
) &&
72 ((struct crypto_larval
*)q
)->mask
!= mask
)
75 exact
= !strcmp(q
->cra_driver_name
, name
);
76 fuzzy
= !strcmp(q
->cra_name
, name
);
77 if (!exact
&& !(fuzzy
&& q
->cra_priority
> best
))
80 if (unlikely(!crypto_mod_get(q
)))
83 best
= q
->cra_priority
;
94 EXPORT_SYMBOL_GPL(__crypto_alg_lookup
);
96 static void crypto_larval_destroy(struct crypto_alg
*alg
)
98 struct crypto_larval
*larval
= (void *)alg
;
100 BUG_ON(!crypto_is_larval(alg
));
102 crypto_mod_put(larval
->adult
);
106 static struct crypto_alg
*crypto_larval_alloc(const char *name
, u32 type
,
109 struct crypto_alg
*alg
;
110 struct crypto_larval
*larval
;
112 larval
= kzalloc(sizeof(*larval
), GFP_KERNEL
);
117 larval
->alg
.cra_flags
= CRYPTO_ALG_LARVAL
| type
;
118 larval
->alg
.cra_priority
= -1;
119 larval
->alg
.cra_destroy
= crypto_larval_destroy
;
121 atomic_set(&larval
->alg
.cra_refcnt
, 2);
122 strlcpy(larval
->alg
.cra_name
, name
, CRYPTO_MAX_ALG_NAME
);
123 init_completion(&larval
->completion
);
125 down_write(&crypto_alg_sem
);
126 alg
= __crypto_alg_lookup(name
, type
, mask
);
129 list_add(&alg
->cra_list
, &crypto_alg_list
);
131 up_write(&crypto_alg_sem
);
133 if (alg
!= &larval
->alg
)
139 static void crypto_larval_kill(struct crypto_alg
*alg
)
141 struct crypto_larval
*larval
= (void *)alg
;
143 down_write(&crypto_alg_sem
);
144 list_del(&alg
->cra_list
);
145 up_write(&crypto_alg_sem
);
146 complete(&larval
->completion
);
150 static struct crypto_alg
*crypto_larval_wait(struct crypto_alg
*alg
)
152 struct crypto_larval
*larval
= (void *)alg
;
154 wait_for_completion_interruptible_timeout(&larval
->completion
, 60 * HZ
);
156 if (alg
&& !crypto_mod_get(alg
))
158 crypto_mod_put(&larval
->alg
);
163 static struct crypto_alg
*crypto_alg_lookup(const char *name
, u32 type
,
166 struct crypto_alg
*alg
;
171 down_read(&crypto_alg_sem
);
172 alg
= __crypto_alg_lookup(name
, type
, mask
);
173 up_read(&crypto_alg_sem
);
178 struct crypto_alg
*crypto_alg_mod_lookup(const char *name
, u32 type
, u32 mask
)
180 struct crypto_alg
*alg
;
181 struct crypto_alg
*larval
;
184 mask
&= ~CRYPTO_ALG_LARVAL
;
187 alg
= try_then_request_module(crypto_alg_lookup(name
, type
, mask
),
190 return crypto_is_larval(alg
) ? crypto_larval_wait(alg
) : alg
;
192 larval
= crypto_larval_alloc(name
, type
, mask
);
193 if (!larval
|| !crypto_is_larval(larval
))
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
);
202 if (ok
== NOTIFY_STOP
)
203 alg
= crypto_larval_wait(larval
);
205 crypto_mod_put(larval
);
208 crypto_larval_kill(larval
);
211 EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup
);
213 static int crypto_init_flags(struct crypto_tfm
*tfm
, u32 flags
)
215 tfm
->crt_flags
= flags
& CRYPTO_TFM_REQ_MASK
;
216 flags
&= ~CRYPTO_TFM_REQ_MASK
;
218 switch (crypto_tfm_alg_type(tfm
)) {
219 case CRYPTO_ALG_TYPE_CIPHER
:
220 return crypto_init_cipher_flags(tfm
, flags
);
222 case CRYPTO_ALG_TYPE_DIGEST
:
223 return crypto_init_digest_flags(tfm
, flags
);
225 case CRYPTO_ALG_TYPE_COMPRESS
:
226 return crypto_init_compress_flags(tfm
, flags
);
236 static int crypto_init_ops(struct crypto_tfm
*tfm
)
238 switch (crypto_tfm_alg_type(tfm
)) {
239 case CRYPTO_ALG_TYPE_CIPHER
:
240 return crypto_init_cipher_ops(tfm
);
242 case CRYPTO_ALG_TYPE_DIGEST
:
243 return crypto_init_digest_ops(tfm
);
245 case CRYPTO_ALG_TYPE_COMPRESS
:
246 return crypto_init_compress_ops(tfm
);
256 static void crypto_exit_ops(struct crypto_tfm
*tfm
)
258 switch (crypto_tfm_alg_type(tfm
)) {
259 case CRYPTO_ALG_TYPE_CIPHER
:
260 crypto_exit_cipher_ops(tfm
);
263 case CRYPTO_ALG_TYPE_DIGEST
:
264 crypto_exit_digest_ops(tfm
);
267 case CRYPTO_ALG_TYPE_COMPRESS
:
268 crypto_exit_compress_ops(tfm
);
277 static unsigned int crypto_ctxsize(struct crypto_alg
*alg
, int flags
)
281 switch (alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
) {
285 case CRYPTO_ALG_TYPE_CIPHER
:
286 len
= crypto_cipher_ctxsize(alg
, flags
);
289 case CRYPTO_ALG_TYPE_DIGEST
:
290 len
= crypto_digest_ctxsize(alg
, flags
);
293 case CRYPTO_ALG_TYPE_COMPRESS
:
294 len
= crypto_compress_ctxsize(alg
, flags
);
298 return len
+ (alg
->cra_alignmask
& ~(crypto_tfm_ctx_alignment() - 1));
301 struct crypto_tfm
*crypto_alloc_tfm(const char *name
, u32 flags
)
303 struct crypto_tfm
*tfm
= NULL
;
304 struct crypto_alg
*alg
;
305 unsigned int tfm_size
;
307 alg
= crypto_alg_mod_lookup(name
, 0, 0);
311 tfm_size
= sizeof(*tfm
) + crypto_ctxsize(alg
, flags
);
312 tfm
= kzalloc(tfm_size
, GFP_KERNEL
);
316 tfm
->__crt_alg
= alg
;
318 if (crypto_init_flags(tfm
, flags
))
321 if (crypto_init_ops(tfm
))
324 if (alg
->cra_init
&& alg
->cra_init(tfm
))
325 goto cra_init_failed
;
330 crypto_exit_ops(tfm
);
340 void crypto_free_tfm(struct crypto_tfm
*tfm
)
342 struct crypto_alg
*alg
;
348 alg
= tfm
->__crt_alg
;
349 size
= sizeof(*tfm
) + alg
->cra_ctxsize
;
353 crypto_exit_ops(tfm
);
355 memset(tfm
, 0, size
);
359 int crypto_alg_available(const char *name
, u32 flags
)
362 struct crypto_alg
*alg
= crypto_alg_mod_lookup(name
, 0, 0);
372 EXPORT_SYMBOL_GPL(crypto_alloc_tfm
);
373 EXPORT_SYMBOL_GPL(crypto_free_tfm
);
374 EXPORT_SYMBOL_GPL(crypto_alg_available
);