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é°ˆler.
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 "kmap_types.h"
18 #include <linux/init.h>
19 #include <linux/module.h>
20 //#include <linux/crypto.h>
21 #include "rtl_crypto.h"
22 #include <linux/errno.h>
23 #include <linux/rwsem.h>
24 #include <linux/slab.h>
27 LIST_HEAD(crypto_alg_list);
28 DECLARE_RWSEM(crypto_alg_sem);
30 static inline int crypto_alg_get(struct crypto_alg *alg)
32 return try_inc_mod_count(alg->cra_module);
35 static inline void crypto_alg_put(struct crypto_alg *alg)
38 __MOD_DEC_USE_COUNT(alg->cra_module);
41 struct crypto_alg *crypto_alg_lookup(const char *name)
43 struct crypto_alg *q, *alg = NULL;
48 down_read(&crypto_alg_sem);
50 list_for_each_entry(q, &crypto_alg_list, cra_list) {
51 if (!(strcmp(q->cra_name, name))) {
52 if (crypto_alg_get(q))
58 up_read(&crypto_alg_sem);
62 static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
66 switch (crypto_tfm_alg_type(tfm)) {
67 case CRYPTO_ALG_TYPE_CIPHER:
68 return crypto_init_cipher_flags(tfm, flags);
70 case CRYPTO_ALG_TYPE_DIGEST:
71 return crypto_init_digest_flags(tfm, flags);
73 case CRYPTO_ALG_TYPE_COMPRESS:
74 return crypto_init_compress_flags(tfm, flags);
84 static int crypto_init_ops(struct crypto_tfm *tfm)
86 switch (crypto_tfm_alg_type(tfm)) {
87 case CRYPTO_ALG_TYPE_CIPHER:
88 return crypto_init_cipher_ops(tfm);
90 case CRYPTO_ALG_TYPE_DIGEST:
91 return crypto_init_digest_ops(tfm);
93 case CRYPTO_ALG_TYPE_COMPRESS:
94 return crypto_init_compress_ops(tfm);
104 static void crypto_exit_ops(struct crypto_tfm *tfm)
106 switch (crypto_tfm_alg_type(tfm)) {
107 case CRYPTO_ALG_TYPE_CIPHER:
108 crypto_exit_cipher_ops(tfm);
111 case CRYPTO_ALG_TYPE_DIGEST:
112 crypto_exit_digest_ops(tfm);
115 case CRYPTO_ALG_TYPE_COMPRESS:
116 crypto_exit_compress_ops(tfm);
125 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
127 struct crypto_tfm *tfm = NULL;
128 struct crypto_alg *alg;
130 alg = crypto_alg_mod_lookup(name);
134 tfm = kmalloc(sizeof(*tfm) + alg->cra_ctxsize, GFP_KERNEL);
138 memset(tfm, 0, sizeof(*tfm) + alg->cra_ctxsize);
140 tfm->__crt_alg = alg;
142 if (crypto_init_flags(tfm, flags))
145 if (crypto_init_ops(tfm)) {
146 crypto_exit_ops(tfm);
161 void crypto_free_tfm(struct crypto_tfm *tfm)
163 struct crypto_alg *alg = tfm->__crt_alg;
164 int size = sizeof(*tfm) + alg->cra_ctxsize;
166 crypto_exit_ops(tfm);
168 memset(tfm, 0, size);
172 int crypto_register_alg(struct crypto_alg *alg)
175 struct crypto_alg *q;
177 down_write(&crypto_alg_sem);
179 list_for_each_entry(q, &crypto_alg_list, cra_list) {
180 if (!(strcmp(q->cra_name, alg->cra_name))) {
186 list_add_tail(&alg->cra_list, &crypto_alg_list);
188 up_write(&crypto_alg_sem);
192 int crypto_unregister_alg(struct crypto_alg *alg)
195 struct crypto_alg *q;
197 BUG_ON(!alg->cra_module);
199 down_write(&crypto_alg_sem);
200 list_for_each_entry(q, &crypto_alg_list, cra_list) {
202 list_del(&alg->cra_list);
208 up_write(&crypto_alg_sem);
212 int crypto_alg_available(const char *name, u32 flags)
215 struct crypto_alg *alg = crypto_alg_mod_lookup(name);
225 static int __init init_crypto(void)
227 printk(KERN_INFO "Initializing Cryptographic API\n");
232 __initcall(init_crypto);
235 EXPORT_SYMBOL_GPL(crypto_register_alg);
236 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
237 EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
238 EXPORT_SYMBOL_GPL(crypto_free_tfm);
239 EXPORT_SYMBOL_GPL(crypto_alg_available);
242 EXPORT_SYMBOL_NOVERS(crypto_register_alg);
243 EXPORT_SYMBOL_NOVERS(crypto_unregister_alg);
244 EXPORT_SYMBOL_NOVERS(crypto_alloc_tfm);
245 EXPORT_SYMBOL_NOVERS(crypto_free_tfm);
246 EXPORT_SYMBOL_NOVERS(crypto_alg_available);