]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - crypto/api.c
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/param.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
26 LIST_HEAD(crypto_alg_list
);
27 EXPORT_SYMBOL_GPL(crypto_alg_list
);
28 DECLARE_RWSEM(crypto_alg_sem
);
29 EXPORT_SYMBOL_GPL(crypto_alg_sem
);
31 BLOCKING_NOTIFIER_HEAD(crypto_chain
);
32 EXPORT_SYMBOL_GPL(crypto_chain
);
34 static inline struct crypto_alg
*crypto_alg_get(struct crypto_alg
*alg
)
36 atomic_inc(&alg
->cra_refcnt
);
40 static inline void crypto_alg_put(struct crypto_alg
*alg
)
42 if (atomic_dec_and_test(&alg
->cra_refcnt
) && alg
->cra_destroy
)
43 alg
->cra_destroy(alg
);
46 struct crypto_alg
*crypto_mod_get(struct crypto_alg
*alg
)
48 return try_module_get(alg
->cra_module
) ? crypto_alg_get(alg
) : NULL
;
50 EXPORT_SYMBOL_GPL(crypto_mod_get
);
52 void crypto_mod_put(struct crypto_alg
*alg
)
55 module_put(alg
->cra_module
);
57 EXPORT_SYMBOL_GPL(crypto_mod_put
);
59 struct crypto_alg
*__crypto_alg_lookup(const char *name
)
61 struct crypto_alg
*q
, *alg
= NULL
;
64 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
67 exact
= !strcmp(q
->cra_driver_name
, name
);
68 fuzzy
= !strcmp(q
->cra_name
, name
);
69 if (!exact
&& !(fuzzy
&& q
->cra_priority
> best
))
72 if (unlikely(!crypto_mod_get(q
)))
75 best
= q
->cra_priority
;
86 EXPORT_SYMBOL_GPL(__crypto_alg_lookup
);
88 static void crypto_larval_destroy(struct crypto_alg
*alg
)
90 struct crypto_larval
*larval
= (void *)alg
;
92 BUG_ON(!crypto_is_larval(alg
));
94 crypto_mod_put(larval
->adult
);
98 static struct crypto_alg
*crypto_larval_alloc(const char *name
)
100 struct crypto_alg
*alg
;
101 struct crypto_larval
*larval
;
103 larval
= kzalloc(sizeof(*larval
), GFP_KERNEL
);
107 larval
->alg
.cra_flags
= CRYPTO_ALG_LARVAL
;
108 larval
->alg
.cra_priority
= -1;
109 larval
->alg
.cra_destroy
= crypto_larval_destroy
;
111 atomic_set(&larval
->alg
.cra_refcnt
, 2);
112 strlcpy(larval
->alg
.cra_name
, name
, CRYPTO_MAX_ALG_NAME
);
113 init_completion(&larval
->completion
);
115 down_write(&crypto_alg_sem
);
116 alg
= __crypto_alg_lookup(name
);
119 list_add(&alg
->cra_list
, &crypto_alg_list
);
121 up_write(&crypto_alg_sem
);
123 if (alg
!= &larval
->alg
)
129 static void crypto_larval_kill(struct crypto_alg
*alg
)
131 struct crypto_larval
*larval
= (void *)alg
;
133 down_write(&crypto_alg_sem
);
134 list_del(&alg
->cra_list
);
135 up_write(&crypto_alg_sem
);
136 complete(&larval
->completion
);
140 static struct crypto_alg
*crypto_larval_wait(struct crypto_alg
*alg
)
142 struct crypto_larval
*larval
= (void *)alg
;
144 wait_for_completion_interruptible_timeout(&larval
->completion
, 60 * HZ
);
146 if (alg
&& !crypto_mod_get(alg
))
148 crypto_mod_put(&larval
->alg
);
153 static struct crypto_alg
*crypto_alg_lookup(const char *name
)
155 struct crypto_alg
*alg
;
160 down_read(&crypto_alg_sem
);
161 alg
= __crypto_alg_lookup(name
);
162 up_read(&crypto_alg_sem
);
167 /* A far more intelligent version of this is planned. For now, just
168 * try an exact match on the name of the algorithm. */
169 static struct crypto_alg
*crypto_alg_mod_lookup(const char *name
)
171 struct crypto_alg
*alg
;
172 struct crypto_alg
*larval
;
174 alg
= try_then_request_module(crypto_alg_lookup(name
), name
);
176 return crypto_is_larval(alg
) ? crypto_larval_wait(alg
) : alg
;
178 larval
= crypto_larval_alloc(name
);
179 if (!larval
|| !crypto_is_larval(larval
))
182 if (crypto_notify(CRYPTO_MSG_ALG_REQUEST
, larval
) == NOTIFY_STOP
)
183 alg
= crypto_larval_wait(larval
);
185 crypto_mod_put(larval
);
188 crypto_larval_kill(larval
);
192 static int crypto_init_flags(struct crypto_tfm
*tfm
, u32 flags
)
194 tfm
->crt_flags
= flags
& CRYPTO_TFM_REQ_MASK
;
195 flags
&= ~CRYPTO_TFM_REQ_MASK
;
197 switch (crypto_tfm_alg_type(tfm
)) {
198 case CRYPTO_ALG_TYPE_CIPHER
:
199 return crypto_init_cipher_flags(tfm
, flags
);
201 case CRYPTO_ALG_TYPE_DIGEST
:
202 return crypto_init_digest_flags(tfm
, flags
);
204 case CRYPTO_ALG_TYPE_COMPRESS
:
205 return crypto_init_compress_flags(tfm
, flags
);
215 static int crypto_init_ops(struct crypto_tfm
*tfm
)
217 switch (crypto_tfm_alg_type(tfm
)) {
218 case CRYPTO_ALG_TYPE_CIPHER
:
219 return crypto_init_cipher_ops(tfm
);
221 case CRYPTO_ALG_TYPE_DIGEST
:
222 return crypto_init_digest_ops(tfm
);
224 case CRYPTO_ALG_TYPE_COMPRESS
:
225 return crypto_init_compress_ops(tfm
);
235 static void crypto_exit_ops(struct crypto_tfm
*tfm
)
237 switch (crypto_tfm_alg_type(tfm
)) {
238 case CRYPTO_ALG_TYPE_CIPHER
:
239 crypto_exit_cipher_ops(tfm
);
242 case CRYPTO_ALG_TYPE_DIGEST
:
243 crypto_exit_digest_ops(tfm
);
246 case CRYPTO_ALG_TYPE_COMPRESS
:
247 crypto_exit_compress_ops(tfm
);
256 static unsigned int crypto_ctxsize(struct crypto_alg
*alg
, int flags
)
260 switch (alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
) {
264 case CRYPTO_ALG_TYPE_CIPHER
:
265 len
= crypto_cipher_ctxsize(alg
, flags
);
268 case CRYPTO_ALG_TYPE_DIGEST
:
269 len
= crypto_digest_ctxsize(alg
, flags
);
272 case CRYPTO_ALG_TYPE_COMPRESS
:
273 len
= crypto_compress_ctxsize(alg
, flags
);
277 return len
+ (alg
->cra_alignmask
& ~(crypto_tfm_ctx_alignment() - 1));
280 struct crypto_tfm
*crypto_alloc_tfm(const char *name
, u32 flags
)
282 struct crypto_tfm
*tfm
= NULL
;
283 struct crypto_alg
*alg
;
284 unsigned int tfm_size
;
286 alg
= crypto_alg_mod_lookup(name
);
290 tfm_size
= sizeof(*tfm
) + crypto_ctxsize(alg
, flags
);
291 tfm
= kzalloc(tfm_size
, GFP_KERNEL
);
295 tfm
->__crt_alg
= alg
;
297 if (crypto_init_flags(tfm
, flags
))
300 if (crypto_init_ops(tfm
))
303 if (alg
->cra_init
&& alg
->cra_init(tfm
))
304 goto cra_init_failed
;
309 crypto_exit_ops(tfm
);
319 void crypto_free_tfm(struct crypto_tfm
*tfm
)
321 struct crypto_alg
*alg
;
327 alg
= tfm
->__crt_alg
;
328 size
= sizeof(*tfm
) + alg
->cra_ctxsize
;
332 crypto_exit_ops(tfm
);
334 memset(tfm
, 0, size
);
338 int crypto_alg_available(const char *name
, u32 flags
)
341 struct crypto_alg
*alg
= crypto_alg_mod_lookup(name
);
351 EXPORT_SYMBOL_GPL(crypto_alloc_tfm
);
352 EXPORT_SYMBOL_GPL(crypto_free_tfm
);
353 EXPORT_SYMBOL_GPL(crypto_alg_available
);