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)
16 #include <linux/init.h>
17 #include <linux/crypto.h>
18 #include <linux/errno.h>
19 #include <linux/rwsem.h>
20 #include <linux/slab.h>
23 LIST_HEAD(crypto_alg_list);
24 DECLARE_RWSEM(crypto_alg_sem);
26 static inline int crypto_alg_get(struct crypto_alg *alg)
28 return try_inc_mod_count(alg->cra_module);
31 static inline void crypto_alg_put(struct crypto_alg *alg)
34 __MOD_DEC_USE_COUNT(alg->cra_module);
37 struct crypto_alg *crypto_alg_lookup(const char *name)
39 struct crypto_alg *q, *alg = NULL;
44 down_read(&crypto_alg_sem);
46 list_for_each_entry(q, &crypto_alg_list, cra_list) {
47 if (!(strcmp(q->cra_name, name))) {
48 if (crypto_alg_get(q))
54 up_read(&crypto_alg_sem);
58 static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
62 switch (crypto_tfm_alg_type(tfm)) {
63 case CRYPTO_ALG_TYPE_CIPHER:
64 return crypto_init_cipher_flags(tfm, flags);
66 case CRYPTO_ALG_TYPE_DIGEST:
67 return crypto_init_digest_flags(tfm, flags);
69 case CRYPTO_ALG_TYPE_COMPRESS:
70 return crypto_init_compress_flags(tfm, flags);
80 static int crypto_init_ops(struct crypto_tfm *tfm)
82 switch (crypto_tfm_alg_type(tfm)) {
83 case CRYPTO_ALG_TYPE_CIPHER:
84 return crypto_init_cipher_ops(tfm);
86 case CRYPTO_ALG_TYPE_DIGEST:
87 return crypto_init_digest_ops(tfm);
89 case CRYPTO_ALG_TYPE_COMPRESS:
90 return crypto_init_compress_ops(tfm);
100 static void crypto_exit_ops(struct crypto_tfm *tfm)
102 switch (crypto_tfm_alg_type(tfm)) {
103 case CRYPTO_ALG_TYPE_CIPHER:
104 crypto_exit_cipher_ops(tfm);
107 case CRYPTO_ALG_TYPE_DIGEST:
108 crypto_exit_digest_ops(tfm);
111 case CRYPTO_ALG_TYPE_COMPRESS:
112 crypto_exit_compress_ops(tfm);
121 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
123 struct crypto_tfm *tfm = NULL;
124 struct crypto_alg *alg;
126 alg = crypto_alg_mod_lookup(name);
130 tfm = kmalloc(sizeof(*tfm) + alg->cra_ctxsize, GFP_KERNEL);
134 memset(tfm, 0, sizeof(*tfm) + alg->cra_ctxsize);
136 tfm->__crt_alg = alg;
138 if (crypto_init_flags(tfm, flags))
141 if (crypto_init_ops(tfm)) {
142 crypto_exit_ops(tfm);
157 void crypto_free_tfm(struct crypto_tfm *tfm)
159 struct crypto_alg *alg = tfm->__crt_alg;
160 int size = sizeof(*tfm) + alg->cra_ctxsize;
162 crypto_exit_ops(tfm);
164 memset(tfm, 0, size);
168 int crypto_register_alg(struct crypto_alg *alg)
171 struct crypto_alg *q;
173 down_write(&crypto_alg_sem);
175 list_for_each_entry(q, &crypto_alg_list, cra_list) {
176 if (!(strcmp(q->cra_name, alg->cra_name))) {
182 list_add_tail(&alg->cra_list, &crypto_alg_list);
184 up_write(&crypto_alg_sem);
188 int crypto_unregister_alg(struct crypto_alg *alg)
191 struct crypto_alg *q;
193 BUG_ON(!alg->cra_module);
195 down_write(&crypto_alg_sem);
196 list_for_each_entry(q, &crypto_alg_list, cra_list) {
198 list_del(&alg->cra_list);
204 up_write(&crypto_alg_sem);
208 int crypto_alg_available(const char *name, u32 flags)
211 struct crypto_alg *alg = crypto_alg_mod_lookup(name);
221 static int __init init_crypto(void)
223 printk(KERN_INFO "Initializing Cryptographic API\n");
228 __initcall(init_crypto);
230 EXPORT_SYMBOL_GPL(crypto_register_alg);
231 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
232 EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
233 EXPORT_SYMBOL_GPL(crypto_free_tfm);
234 EXPORT_SYMBOL_GPL(crypto_alg_available);