2 * Scatterlist Cryptographic API.
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
7 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
8 * and Nettle, by Niels Möller.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
17 #include <linux/compiler.h>
18 #include <linux/init.h>
19 #include <linux/crypto.h>
20 #include <linux/errno.h>
21 #include <linux/kmod.h>
22 #include <linux/rwsem.h>
23 #include <linux/slab.h>
26 LIST_HEAD(crypto_alg_list);
27 DECLARE_RWSEM(crypto_alg_sem);
29 static inline int crypto_alg_get(struct crypto_alg *alg)
31 return try_module_get(alg->cra_module);
34 static inline void crypto_alg_put(struct crypto_alg *alg)
36 module_put(alg->cra_module);
39 static struct crypto_alg *crypto_alg_lookup(const char *name)
41 struct crypto_alg *q, *alg = NULL;
46 down_read(&crypto_alg_sem);
48 list_for_each_entry(q, &crypto_alg_list, cra_list) {
49 if (!(strcmp(q->cra_name, name))) {
50 if (crypto_alg_get(q))
56 up_read(&crypto_alg_sem);
60 /* A far more intelligent version of this is planned. For now, just
61 * try an exact match on the name of the algorithm. */
62 static inline struct crypto_alg *crypto_alg_mod_lookup(const char *name)
64 return try_then_request_module(crypto_alg_lookup(name), name);
67 static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
71 switch (crypto_tfm_alg_type(tfm)) {
72 case CRYPTO_ALG_TYPE_CIPHER:
73 return crypto_init_cipher_flags(tfm, flags);
75 case CRYPTO_ALG_TYPE_DIGEST:
76 return crypto_init_digest_flags(tfm, flags);
78 case CRYPTO_ALG_TYPE_COMPRESS:
79 return crypto_init_compress_flags(tfm, flags);
89 static int crypto_init_ops(struct crypto_tfm *tfm)
91 switch (crypto_tfm_alg_type(tfm)) {
92 case CRYPTO_ALG_TYPE_CIPHER:
93 return crypto_init_cipher_ops(tfm);
95 case CRYPTO_ALG_TYPE_DIGEST:
96 return crypto_init_digest_ops(tfm);
98 case CRYPTO_ALG_TYPE_COMPRESS:
99 return crypto_init_compress_ops(tfm);
109 static void crypto_exit_ops(struct crypto_tfm *tfm)
111 switch (crypto_tfm_alg_type(tfm)) {
112 case CRYPTO_ALG_TYPE_CIPHER:
113 crypto_exit_cipher_ops(tfm);
116 case CRYPTO_ALG_TYPE_DIGEST:
117 crypto_exit_digest_ops(tfm);
120 case CRYPTO_ALG_TYPE_COMPRESS:
121 crypto_exit_compress_ops(tfm);
130 static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
134 switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
138 case CRYPTO_ALG_TYPE_CIPHER:
139 len = crypto_cipher_ctxsize(alg, flags);
142 case CRYPTO_ALG_TYPE_DIGEST:
143 len = crypto_digest_ctxsize(alg, flags);
146 case CRYPTO_ALG_TYPE_COMPRESS:
147 len = crypto_compress_ctxsize(alg, flags);
151 return len + alg->cra_alignmask;
154 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
156 struct crypto_tfm *tfm = NULL;
157 struct crypto_alg *alg;
158 unsigned int tfm_size;
160 alg = crypto_alg_mod_lookup(name);
164 tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
165 tfm = kmalloc(tfm_size, GFP_KERNEL);
169 memset(tfm, 0, tfm_size);
171 tfm->__crt_alg = alg;
173 if (crypto_init_flags(tfm, flags))
176 if (crypto_init_ops(tfm)) {
177 crypto_exit_ops(tfm);
192 void crypto_free_tfm(struct crypto_tfm *tfm)
194 struct crypto_alg *alg;
200 alg = tfm->__crt_alg;
201 size = sizeof(*tfm) + alg->cra_ctxsize;
203 crypto_exit_ops(tfm);
205 memset(tfm, 0, size);
209 int crypto_register_alg(struct crypto_alg *alg)
212 struct crypto_alg *q;
214 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
217 if (alg->cra_alignmask > PAGE_SIZE)
220 down_write(&crypto_alg_sem);
222 list_for_each_entry(q, &crypto_alg_list, cra_list) {
223 if (!(strcmp(q->cra_name, alg->cra_name))) {
229 list_add_tail(&alg->cra_list, &crypto_alg_list);
231 up_write(&crypto_alg_sem);
235 int crypto_unregister_alg(struct crypto_alg *alg)
238 struct crypto_alg *q;
240 BUG_ON(!alg->cra_module);
242 down_write(&crypto_alg_sem);
243 list_for_each_entry(q, &crypto_alg_list, cra_list) {
245 list_del(&alg->cra_list);
251 up_write(&crypto_alg_sem);
255 int crypto_alg_available(const char *name, u32 flags)
258 struct crypto_alg *alg = crypto_alg_mod_lookup(name);
268 static int __init init_crypto(void)
270 printk(KERN_INFO "Initializing Cryptographic API\n");
275 __initcall(init_crypto);
277 EXPORT_SYMBOL_GPL(crypto_register_alg);
278 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
279 EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
280 EXPORT_SYMBOL_GPL(crypto_free_tfm);
281 EXPORT_SYMBOL_GPL(crypto_alg_available);